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