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