b6efbe842d9d48b2ad651bb396281791a0f0faef
[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 #include <locale.h>
13
14 #define NDATES 100
15
16 #define FORMAT "=%<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%>"
17
18 static struct swit switches[] = {
19 #define FORMSW  0
20         { "form formatfile", 0 },
21 #define VERSIONSW  1
22         { "Version", 0 },
23 #define HELPSW  2
24         { "help", 0 },
25         { NULL, 0 }
26 };
27
28 static struct format *fmt;
29
30 static int dat[5];
31
32 /*
33 ** static prototypes
34 */
35 static int process(char *);
36
37
38 int
39 main(int argc, char **argv)
40 {
41         int datep = 0, status = 0;
42         char *cp, *form = NULL, *fmtstr;
43         char buf[BUFSIZ], **argp, **arguments;
44         char *dates[NDATES];
45
46         setlocale(LC_ALL, "");
47         invo_name = mhbasename(argv[0]);
48
49         /* read user profile/context */
50         context_read();
51
52         arguments = getarguments(invo_name, argc, argv, 1);
53         argp = arguments;
54
55         while ((cp = *argp++)) {
56                 if (*cp == '-') {
57                         switch (smatch(++cp, switches)) {
58                         case AMBIGSW:
59                                 ambigsw(cp, switches);
60                                 /* sysexits.h EX_USAGE */
61                                 exit(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                                 exit(0);
69                         case VERSIONSW:
70                                 print_version(invo_name);
71                                 exit(0);
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         return status;
107 }
108
109
110 static int
111 process(char *date)
112 {
113         int status = 0;
114         char buffer[BUFSIZ + 1];
115         register struct comp *cptr;
116
117         FINDCOMP(cptr, "text");
118         if (cptr)
119                 cptr->c_text = date;
120         fmt_scan(fmt, buffer, BUFSIZ, dat);
121         fputs(buffer, stdout);
122
123         return status;
124 }