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