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