]> diplodocus.org Git - nmh/blob - uip/dp.c
Alter HasSuffixC()'s char * to be const.
[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/utils.h>
12 #include <h/fmt_scan.h>
13 #include <h/tws.h>
14
15 #define NDATES 100
16
17 #define WIDTH 78
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 = -1, 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], 2)) { 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 == -1) {
109 if ((width = sc_width ()) < WIDTH / 2) {
110 /* Default: width of the terminal, but at least WIDTH/2. */
111 width = WIDTH / 2;
112 }
113 width -= 2;
114 } else if (width == 0) {
115 /* Unlimited width. */
116 width = INT_MAX;
117 }
118 fmt_compile (nfs, &fmt, 1);
119
120 dat[0] = 0;
121 dat[1] = 0;
122 dat[2] = 0;
123 dat[3] = width;
124 dat[4] = 0;
125
126 for (datep = 0; dates[datep]; datep++)
127 status += process (dates[datep], width);
128
129 context_save (); /* save the context file */
130 fmt_free (fmt, 1);
131 done (status);
132 return 1;
133 }
134
135
136 static int
137 process (char *date, int length)
138 {
139 int status = 0;
140 charstring_t scanl =
141 charstring_create (length < NMH_BUFSIZ ? length : NMH_BUFSIZ);
142 struct comp *cptr;
143
144 cptr = fmt_findcomp ("text");
145 if (cptr) {
146 mh_xfree(cptr->c_text);
147 cptr->c_text = mh_xstrdup(date);
148 }
149 fmt_scan (fmt, scanl, length, dat, NULL);
150 fputs (charstring_buffer (scanl), stdout);
151 charstring_free (scanl);
152
153 return status;
154 }