]> diplodocus.org Git - nmh/blob - uip/dp.c
Compare character with EOF using signed comparison because
[nmh] / uip / dp.c
1
2 /*
3 * dp.c -- parse dates 822-style
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <h/fmt_scan.h>
12 #include <h/tws.h>
13
14 #define NDATES 100
15
16 #define WIDTH 78
17 #define WBUFSIZ BUFSIZ
18
19 #define FORMAT "%<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%>"
20
21 #define DP_SWITCHES \
22 X("form formatfile", 0, FORMSW) \
23 X("format string", 5, FMTSW) \
24 X("width columns", 0, WIDTHSW) \
25 X("version", 0, VERSIONSW) \
26 X("help", 0, HELPSW) \
27
28 #define X(sw, minchars, id) id,
29 DEFINE_SWITCH_ENUM(DP);
30 #undef X
31
32 #define X(sw, minchars, id) { sw, minchars, id },
33 DEFINE_SWITCH_ARRAY(DP, switches);
34 #undef X
35
36 static struct format *fmt;
37
38 static int dat[5];
39
40 /*
41 * static prototypes
42 */
43 static int process (char *, int);
44
45
46 int
47 main (int argc, char **argv)
48 {
49 int datep = 0, width = 0, status = 0;
50 char *cp, *form = NULL, *format = NULL, *nfs;
51 char buf[BUFSIZ], **argp, **arguments;
52 char *dates[NDATES];
53
54 if (nmh_init(argv[0], 1)) { return 1; }
55
56 arguments = getarguments (invo_name, argc, argv, 1);
57 argp = arguments;
58
59 while ((cp = *argp++)) {
60 if (*cp == '-') {
61 switch (smatch (++cp, switches)) {
62 case AMBIGSW:
63 ambigsw (cp, switches);
64 done (1);
65 case UNKWNSW:
66 adios (NULL, "-%s unknown", cp);
67
68 case HELPSW:
69 snprintf (buf, sizeof(buf), "%s [switches] dates ...",
70 invo_name);
71 print_help (buf, switches, 1);
72 done (0);
73 case VERSIONSW:
74 print_version(invo_name);
75 done (0);
76
77 case FORMSW:
78 if (!(form = *argp++) || *form == '-')
79 adios (NULL, "missing argument to %s", argp[-2]);
80 format = NULL;
81 continue;
82 case FMTSW:
83 if (!(format = *argp++) || *format == '-')
84 adios (NULL, "missing argument to %s", argp[-2]);
85 form = NULL;
86 continue;
87
88 case WIDTHSW:
89 if (!(cp = *argp++) || *cp == '-')
90 adios (NULL, "missing argument to %s", argp[-2]);
91 width = atoi (cp);
92 continue;
93 }
94 }
95 if (datep > NDATES)
96 adios (NULL, "more than %d dates", NDATES);
97 else
98 dates[datep++] = cp;
99 }
100 dates[datep] = NULL;
101
102 if (datep == 0)
103 adios (NULL, "usage: %s [switches] dates ...", invo_name);
104
105 /* get new format string */
106 nfs = new_fs (form, format, FORMAT);
107
108 if (width == 0) {
109 if ((width = sc_width ()) < WIDTH / 2)
110 width = WIDTH / 2;
111 width -= 2;
112 }
113 if (width > WBUFSIZ)
114 width = WBUFSIZ;
115 fmt_compile (nfs, &fmt, 1);
116
117 dat[0] = 0;
118 dat[1] = 0;
119 dat[2] = 0;
120 dat[3] = width;
121 dat[4] = 0;
122
123 for (datep = 0; dates[datep]; datep++)
124 status += process (dates[datep], width);
125
126 context_save (); /* save the context file */
127 fmt_free (fmt, 1);
128 done (status);
129 return 1;
130 }
131
132
133 static int
134 process (char *date, int length)
135 {
136 int status = 0;
137 char buffer[WBUFSIZ + 1];
138 register struct comp *cptr;
139
140 cptr = fmt_findcomp ("text");
141 if (cptr) {
142 if (cptr->c_text)
143 free(cptr->c_text);
144 cptr->c_text = getcpy(date);
145 }
146 fmt_scan (fmt, buffer, sizeof buffer - 1, length, dat, NULL);
147 fputs (buffer, stdout);
148
149 return status;
150 }