]> diplodocus.org Git - nmh/blob - uip/dp.c
8245f60ad66eb06693b333ff46e8ae522de5f604
[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/fmt_scan.h>
12 #include <h/tws.h>
13
14 #define NDATES 100
15
16 #define WIDTH 78
17 #define WBUFSIZ BUFSIZ
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 = 0, status = 0;
50 char *cp, *form = NULL, *format = NULL, *nfs;
51 char buf[BUFSIZ], **argp, **arguments;
52 char *dates[NDATES];
53
54 setlocale(LC_ALL, "");
55 invo_name = r1bindex (argv[0], '/');
56
57 /* read user profile/context */
58 context_read();
59
60 arguments = getarguments (invo_name, argc, argv, 1);
61 argp = arguments;
62
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 adios (NULL, "-%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 adios (NULL, "missing argument to %s", argp[-2]);
84 format = NULL;
85 continue;
86 case FMTSW:
87 if (!(format = *argp++) || *format == '-')
88 adios (NULL, "missing argument to %s", argp[-2]);
89 form = NULL;
90 continue;
91
92 case WIDTHSW:
93 if (!(cp = *argp++) || *cp == '-')
94 adios (NULL, "missing argument to %s", argp[-2]);
95 width = atoi (cp);
96 continue;
97 }
98 }
99 if (datep > NDATES)
100 adios (NULL, "more than %d dates", NDATES);
101 else
102 dates[datep++] = cp;
103 }
104 dates[datep] = NULL;
105
106 if (datep == 0)
107 adios (NULL, "usage: %s [switches] dates ...", invo_name);
108
109 /* get new format string */
110 nfs = new_fs (form, format, FORMAT);
111
112 if (width == 0) {
113 if ((width = sc_width ()) < WIDTH / 2)
114 width = WIDTH / 2;
115 width -= 2;
116 }
117 if (width > WBUFSIZ)
118 width = WBUFSIZ;
119 fmt_compile (nfs, &fmt, 1);
120
121 dat[0] = 0;
122 dat[1] = 0;
123 dat[2] = 0;
124 dat[3] = width;
125 dat[4] = 0;
126
127 for (datep = 0; dates[datep]; datep++)
128 status += process (dates[datep], width);
129
130 context_save (); /* save the context file */
131 fmt_free (fmt, 1);
132 done (status);
133 return 1;
134 }
135
136
137 static int
138 process (char *date, int length)
139 {
140 int status = 0;
141 char buffer[WBUFSIZ + 1];
142 register struct comp *cptr;
143
144 cptr = fmt_findcomp ("text");
145 if (cptr) {
146 if (cptr->c_text)
147 free(cptr->c_text);
148 cptr->c_text = getcpy(date);
149 }
150 fmt_scan (fmt, buffer, sizeof buffer - 1, length, dat, NULL);
151 fputs (buffer, stdout);
152
153 return status;
154 }