]> diplodocus.org Git - nmh/blob - uip/dp.c
Use va_copy() to get a copy of va_list, instead of using original.
[nmh] / uip / dp.c
1 /* dp.c -- parse dates 822-style
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include "h/done.h"
10 #include <h/utils.h>
11 #include <h/fmt_scan.h>
12 #include <h/tws.h>
13 #include "sbr/terminal.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;
52 char *dates[NDATES + 1]; /* Includes terminating NULL. */
53
54 if (nmh_init(argv[0], true, false)) { return 1; }
55
56 argp = getarguments (invo_name, argc, argv, 1);
57 while ((cp = *argp++)) {
58 if (*cp == '-') {
59 switch (smatch (++cp, switches)) {
60 case AMBIGSW:
61 ambigsw (cp, switches);
62 done (1);
63 case UNKWNSW:
64 die("-%s unknown", cp);
65
66 case HELPSW:
67 snprintf (buf, sizeof(buf), "%s [switches] dates ...",
68 invo_name);
69 print_help (buf, switches, 1);
70 done (0);
71 case VERSIONSW:
72 print_version(invo_name);
73 done (0);
74
75 case FORMSW:
76 if (!(form = *argp++) || *form == '-')
77 die("missing argument to %s", argp[-2]);
78 format = NULL;
79 continue;
80 case FMTSW:
81 if (!(format = *argp++) || *format == '-')
82 die("missing argument to %s", argp[-2]);
83 form = NULL;
84 continue;
85
86 case WIDTHSW:
87 if (!(cp = *argp++) || *cp == '-')
88 die("missing argument to %s", argp[-2]);
89 width = atoi (cp);
90 continue;
91 }
92 }
93 if (datep == NDATES)
94 die("more than %d dates", NDATES);
95 dates[datep++] = cp;
96 }
97 dates[datep] = NULL;
98
99 if (datep == 0)
100 die("usage: %s [switches] dates ...", invo_name);
101
102 /* get new format string */
103 nfs = new_fs (form, format, FORMAT);
104
105 if (width == -1) {
106 if ((width = sc_width ()) < WIDTH / 2) {
107 /* Default: width of the terminal, but at least WIDTH/2. */
108 width = WIDTH / 2;
109 }
110 width -= 2;
111 } else if (width == 0) {
112 /* Unlimited width. */
113 width = INT_MAX;
114 }
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 charstring_t scanl =
138 charstring_create (length < NMH_BUFSIZ ? length : NMH_BUFSIZ);
139 struct comp *cptr;
140
141 cptr = fmt_findcomp ("text");
142 if (cptr) {
143 free(cptr->c_text);
144 cptr->c_text = mh_xstrdup(date);
145 }
146 fmt_scan (fmt, scanl, length, dat, NULL);
147 fputs (charstring_buffer (scanl), stdout);
148 charstring_free (scanl);
149
150 return status;
151 }