a66beed827644763d04cb46572cedef496b1c6ed
[mmh] / uip / dp.c
1
2 /*
3  * dp.c -- parse dates 822-style
4  *
5  * This code is Copyright (c) 2002, by the authors of nmh.  See the
6  * COPYRIGHT file in the root directory of the nmh distribution for
7  * complete copyright information.
8  */
9
10 #include <h/mh.h>
11 #include <h/fmt_scan.h>
12 #include <h/tws.h>
13
14 #define NDATES 100
15
16 #define WIDTH 78
17 #define WBUFSIZ BUFSIZ
18
19 #define FORMAT "%<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%>"
20
21 static struct swit switches[] = {
22 #define FORMSW                0
23     { "form formatfile", 0 },
24 #define FMTSW                 1
25     { "format string", 5 },
26 #define WIDTHSW               2
27     { "width columns", 0 },
28 #define VERSIONSW             3
29     { "version", 0 },
30 #define HELPSW                4
31     { "help", 0 },
32     { NULL, 0 }
33 };
34
35 static struct format *fmt;
36
37 static int dat[5];
38
39 /*
40  * static prototypes
41  */
42 static int process (char *, int);
43
44
45 int
46 main (int argc, char **argv)
47 {
48     int datep = 0, width = 0, status = 0;
49     char *cp, *form = NULL, *format = NULL, *nfs;
50     char buf[BUFSIZ], **argp, **arguments;
51     char *dates[NDATES];
52
53 #ifdef LOCALE
54     setlocale(LC_ALL, "");
55 #endif
56     invo_name = r1bindex (argv[0], '/');
57
58     /* read user profile/context */
59     context_read();
60
61     arguments = getarguments (invo_name, argc, argv, 1);
62     argp = arguments;
63
64     while ((cp = *argp++)) {
65         if (*cp == '-') {
66             switch (smatch (++cp, switches)) {
67                 case AMBIGSW: 
68                     ambigsw (cp, switches);
69                     done (1);
70                 case UNKWNSW: 
71                     adios (NULL, "-%s unknown", cp);
72
73                 case HELPSW: 
74                     snprintf (buf, sizeof(buf), "%s [switches] dates ...",
75                         invo_name);
76                     print_help (buf, switches, 1);
77                     done (0);
78                 case VERSIONSW:
79                     print_version(invo_name);
80                     done (0);
81
82                 case FORMSW: 
83                     if (!(form = *argp++) || *form == '-')
84                         adios (NULL, "missing argument to %s", argp[-2]);
85                     format = NULL;
86                     continue;
87                 case FMTSW: 
88                     if (!(format = *argp++) || *format == '-')
89                         adios (NULL, "missing argument to %s", argp[-2]);
90                     form = NULL;
91                     continue;
92
93                 case WIDTHSW: 
94                     if (!(cp = *argp++) || *cp == '-')
95                         adios (NULL, "missing argument to %s", argp[-2]);
96                     width = atoi (cp);
97                     continue;
98             }
99         }
100         if (datep > NDATES)
101             adios (NULL, "more than %d dates", NDATES);
102         else
103             dates[datep++] = cp;
104     }
105     dates[datep] = NULL;
106
107     if (datep == 0)
108         adios (NULL, "usage: %s [switches] dates ...", invo_name);
109
110     /* get new format string */
111     nfs = new_fs (form, format, FORMAT);
112
113     if (width == 0) {
114         if ((width = sc_width ()) < WIDTH / 2)
115             width = WIDTH / 2;
116         width -= 2;
117     }
118     if (width > WBUFSIZ)
119         width = WBUFSIZ;
120     fmt_compile (nfs, &fmt);
121
122     dat[0] = 0;
123     dat[1] = 0;
124     dat[2] = 0;
125     dat[3] = width;
126     dat[4] = 0;
127
128     for (datep = 0; dates[datep]; datep++)
129         status += process (dates[datep], width);
130
131     context_save ();    /* save the context file */
132     done (status);
133     return 1;
134 }
135
136
137 static int
138 process (char *date, int length)
139 {
140     int status = 0;
141     char buffer[WBUFSIZ + 1];
142     register struct comp *cptr;
143
144     FINDCOMP (cptr, "text");
145     if (cptr)
146         cptr->c_text = date;
147     fmt_scan (fmt, buffer, length, dat);
148     fputs (buffer, stdout);
149
150     return status;
151 }