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