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