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