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