s/nfs/fmtstr/
[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 FORMAT "=%<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%>"
16
17 static struct swit switches[] = {
18 #define FORMSW  0
19         { "form formatfile", 0 },
20 #define VERSIONSW  1
21         { "version", 0 },
22 #define HELPSW  2
23         { "help", 0 },
24         { NULL, 0 }
25 };
26
27 static struct format *fmt;
28
29 static int dat[5];
30
31 /*
32 ** static prototypes
33 */
34 static int process(char *);
35
36
37 int
38 main(int argc, char **argv)
39 {
40         int datep = 0, status = 0;
41         char *cp, *form = NULL, *fmtstr;
42         char buf[BUFSIZ], **argp, **arguments;
43         char *dates[NDATES];
44
45 #ifdef LOCALE
46         setlocale(LC_ALL, "");
47 #endif
48         invo_name = mhbasename(argv[0]);
49
50         /* read user profile/context */
51         context_read();
52
53         arguments = getarguments(invo_name, argc, argv, 1);
54         argp = arguments;
55
56         while ((cp = *argp++)) {
57                 if (*cp == '-') {
58                         switch (smatch(++cp, switches)) {
59                         case AMBIGSW:
60                                 ambigsw(cp, switches);
61                                 done(1);
62                         case UNKWNSW:
63                                 adios(NULL, "-%s unknown", cp);
64
65                         case HELPSW:
66                                 snprintf(buf, sizeof(buf), "%s [switches] dates ...", invo_name);
67                                 print_help(buf, switches, 1);
68                                 done(1);
69                         case VERSIONSW:
70                                 print_version(invo_name);
71                                 done(1);
72
73                         case FORMSW:
74                                 if (!(form = *argp++) || *form == '-')
75                                         adios(NULL, "missing argument to %s",
76                                                         argp[-2]);
77                                 continue;
78
79                         }
80                 }
81                 if (datep > NDATES)
82                         adios(NULL, "more than %d dates", NDATES);
83                 else
84                         dates[datep++] = cp;
85         }
86         dates[datep] = NULL;
87
88         if (datep == 0)
89                 adios(NULL, "usage: %s [switches] dates ...", invo_name);
90
91         /* get new format string */
92         fmtstr = new_fs(form, FORMAT);
93
94         fmt_compile(fmtstr, &fmt);
95
96         dat[0] = 0;
97         dat[1] = 0;
98         dat[2] = 0;
99         dat[3] = BUFSIZ;
100         dat[4] = 0;
101
102         for (datep = 0; dates[datep]; datep++)
103                 status += process(dates[datep]);
104
105         context_save();  /* save the context file */
106         done(status);
107         return 1;
108 }
109
110
111 static int
112 process(char *date)
113 {
114         int status = 0;
115         char buffer[BUFSIZ + 1];
116         register struct comp *cptr;
117
118         FINDCOMP(cptr, "text");
119         if (cptr)
120                 cptr->c_text = date;
121         fmt_scan(fmt, buffer, BUFSIZ, dat);
122         fputs(buffer, stdout);
123
124         return status;
125 }