Replace done with exit at uip
[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         setlocale(LC_ALL, "");
46         invo_name = mhbasename(argv[0]);
47
48         /* read user profile/context */
49         context_read();
50
51         arguments = getarguments(invo_name, argc, argv, 1);
52         argp = arguments;
53
54         while ((cp = *argp++)) {
55                 if (*cp == '-') {
56                         switch (smatch(++cp, switches)) {
57                         case AMBIGSW:
58                                 ambigsw(cp, switches);
59                                 /* sysexits.h EX_USAGE */
60                                 exit(1);
61                         case UNKWNSW:
62                                 adios(NULL, "-%s unknown", cp);
63
64                         case HELPSW:
65                                 snprintf(buf, sizeof(buf), "%s [switches] dates ...", invo_name);
66                                 print_help(buf, switches, 1);
67                                 exit(0);
68                         case VERSIONSW:
69                                 print_version(invo_name);
70                                 exit(0);
71
72                         case FORMSW:
73                                 if (!(form = *argp++) || *form == '-')
74                                         adios(NULL, "missing argument to %s",
75                                                         argp[-2]);
76                                 continue;
77
78                         }
79                 }
80                 if (datep > NDATES)
81                         adios(NULL, "more than %d dates", NDATES);
82                 else
83                         dates[datep++] = cp;
84         }
85         dates[datep] = NULL;
86
87         if (datep == 0)
88                 adios(NULL, "usage: %s [switches] dates ...", invo_name);
89
90         /* get new format string */
91         fmtstr = new_fs(form, FORMAT);
92
93         fmt_compile(fmtstr, &fmt);
94
95         dat[0] = 0;
96         dat[1] = 0;
97         dat[2] = 0;
98         dat[3] = BUFSIZ;
99         dat[4] = 0;
100
101         for (datep = 0; dates[datep]; datep++)
102                 status += process(dates[datep]);
103
104         context_save();  /* save the context file */
105         return status;
106 }
107
108
109 static int
110 process(char *date)
111 {
112         int status = 0;
113         char buffer[BUFSIZ + 1];
114         register struct comp *cptr;
115
116         FINDCOMP(cptr, "text");
117         if (cptr)
118                 cptr->c_text = date;
119         fmt_scan(fmt, buffer, BUFSIZ, dat);
120         fputs(buffer, stdout);
121
122         return status;
123 }