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