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