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