add free_field as standard for struct field
[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 #include <sysexits.h>
14
15 #define NDATES 100
16
17 #define FORMAT "=%<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%>"
18
19 static struct swit switches[] = {
20 #define FORMSW  0
21         { "form formatfile", 0 },
22 #define VERSIONSW  1
23         { "Version", 0 },
24 #define HELPSW  2
25         { "help", 0 },
26         { NULL, 0 }
27 };
28
29 static struct format *fmt;
30
31 static int dat[5];
32
33 /*
34 ** static prototypes
35 */
36 static int process(char *);
37
38
39 int
40 main(int argc, char **argv)
41 {
42         int datep = 0, status = 0;
43         char *cp, *form = NULL, *fmtstr;
44         char buf[BUFSIZ], **argp, **arguments;
45         char *dates[NDATES];
46
47         setlocale(LC_ALL, "");
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                                 exit(EX_USAGE);
62                         case UNKWNSW:
63                                 adios(EX_USAGE, 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(argc == 2 ? EX_OK : EX_USAGE);
69                         case VERSIONSW:
70                                 print_version(invo_name);
71                                 exit(argc == 2 ? EX_OK : EX_USAGE);
72
73                         case FORMSW:
74                                 if (!(form = *argp++) || *form == '-')
75                                         adios(EX_USAGE, NULL, "missing argument to %s",
76                                                         argp[-2]);
77                                 continue;
78
79                         }
80                 }
81                 if (datep > NDATES)
82                         adios(EX_USAGE, NULL, "more than %d dates", NDATES);
83                 else
84                         dates[datep++] = cp;
85         }
86         dates[datep] = NULL;
87
88         if (datep == 0)
89                 adios(EX_USAGE, 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         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 }