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