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