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