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