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