sbr/execprog.c: Fix the return value
[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                                 done(1);
60                         case UNKWNSW:
61                                 adios(NULL, "-%s unknown", cp);
62
63                         case HELPSW:
64                                 snprintf(buf, sizeof(buf), "%s [switches] dates ...", invo_name);
65                                 print_help(buf, switches, 1);
66                                 done(1);
67                         case VERSIONSW:
68                                 print_version(invo_name);
69                                 done(1);
70
71                         case FORMSW:
72                                 if (!(form = *argp++) || *form == '-')
73                                         adios(NULL, "missing argument to %s",
74                                                         argp[-2]);
75                                 continue;
76
77                         }
78                 }
79                 if (datep > NDATES)
80                         adios(NULL, "more than %d dates", NDATES);
81                 else
82                         dates[datep++] = cp;
83         }
84         dates[datep] = NULL;
85
86         if (datep == 0)
87                 adios(NULL, "usage: %s [switches] dates ...", invo_name);
88
89         /* get new format string */
90         fmtstr = new_fs(form, FORMAT);
91
92         fmt_compile(fmtstr, &fmt);
93
94         dat[0] = 0;
95         dat[1] = 0;
96         dat[2] = 0;
97         dat[3] = BUFSIZ;
98         dat[4] = 0;
99
100         for (datep = 0; dates[datep]; datep++)
101                 status += process(dates[datep]);
102
103         context_save();  /* save the context file */
104         done(status);
105         return 1;
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 }