Relayouted all switch statements: case aligns with switch.
[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 = mhbasename(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 ...", invo_name);
79                                 print_help(buf, switches, 1);
80                                 done(1);
81                         case VERSIONSW:
82                                 print_version(invo_name);
83                                 done(1);
84
85                         case FORMSW:
86                                 if (!(form = *argp++) || *form == '-')
87                                         adios(NULL, "missing argument to %s",
88                                                         argp[-2]);
89                                 format = NULL;
90                                 continue;
91                         case FMTSW:
92                                 if (!(format = *argp++) || *format == '-')
93                                         adios(NULL, "missing argument to %s",
94                                                         argp[-2]);
95                                 form = NULL;
96                                 continue;
97
98                         case WIDTHSW:
99                                 if (!(cp = *argp++) || *cp == '-')
100                                         adios(NULL, "missing argument to %s",
101                                                         argp[-2]);
102                                 width = atoi(cp);
103                                 continue;
104                         }
105                 }
106                 if (datep > NDATES)
107                         adios(NULL, "more than %d dates", NDATES);
108                 else
109                         dates[datep++] = cp;
110         }
111         dates[datep] = NULL;
112
113         if (datep == 0)
114                 adios(NULL, "usage: %s [switches] dates ...", invo_name);
115
116         /* get new format string */
117         nfs = new_fs(form, format, FORMAT);
118
119         if (width == 0) {
120                 if ((width = sc_width()) < WIDTH / 2)
121                         width = WIDTH / 2;
122                 width -= 2;
123         }
124         if (width > WBUFSIZ)
125                 width = WBUFSIZ;
126         fmt_compile(nfs, &fmt);
127
128         dat[0] = 0;
129         dat[1] = 0;
130         dat[2] = 0;
131         dat[3] = width;
132         dat[4] = 0;
133
134         for (datep = 0; dates[datep]; datep++)
135                 status += process(dates[datep], width);
136
137         context_save();  /* save the context file */
138         done(status);
139         return 1;
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 }