]> diplodocus.org Git - nmh/blob - uip/dp.c
sendsbr.c: Move interface to own file.
[nmh] / uip / dp.c
1 /* dp.c -- parse dates 822-style
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include "h/mh.h"
9 #include "sbr/getarguments.h"
10 #include "sbr/smatch.h"
11 #include "sbr/context_save.h"
12 #include "sbr/ambigsw.h"
13 #include "sbr/print_version.h"
14 #include "sbr/print_help.h"
15 #include "sbr/error.h"
16 #include "h/done.h"
17 #include "h/utils.h"
18 #include "h/fmt_scan.h"
19 #include "h/tws.h"
20 #include "sbr/terminal.h"
21
22 #define NDATES 100
23
24 #define WIDTH 78
25
26 #define FORMAT "%<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%>"
27
28 #define DP_SWITCHES \
29 X("form formatfile", 0, FORMSW) \
30 X("format string", 5, FMTSW) \
31 X("width columns", 0, WIDTHSW) \
32 X("version", 0, VERSIONSW) \
33 X("help", 0, HELPSW) \
34
35 #define X(sw, minchars, id) id,
36 DEFINE_SWITCH_ENUM(DP);
37 #undef X
38
39 #define X(sw, minchars, id) { sw, minchars, id },
40 DEFINE_SWITCH_ARRAY(DP, switches);
41 #undef X
42
43 static struct format *fmt;
44
45 static int dat[5];
46
47 /*
48 * static prototypes
49 */
50 static int process (char *, int);
51
52
53 int
54 main (int argc, char **argv)
55 {
56 int datep = 0, width = -1, status = 0;
57 char *cp, *form = NULL, *format = NULL, *nfs;
58 char buf[BUFSIZ], **argp;
59 char *dates[NDATES + 1]; /* Includes terminating NULL. */
60
61 if (nmh_init(argv[0], true, false)) { return 1; }
62
63 argp = getarguments (invo_name, argc, argv, 1);
64 while ((cp = *argp++)) {
65 if (*cp == '-') {
66 switch (smatch (++cp, switches)) {
67 case AMBIGSW:
68 ambigsw (cp, switches);
69 done (1);
70 case UNKWNSW:
71 die("-%s unknown", cp);
72
73 case HELPSW:
74 snprintf (buf, sizeof(buf), "%s [switches] dates ...",
75 invo_name);
76 print_help (buf, switches, 1);
77 done (0);
78 case VERSIONSW:
79 print_version(invo_name);
80 done (0);
81
82 case FORMSW:
83 if (!(form = *argp++) || *form == '-')
84 die("missing argument to %s", argp[-2]);
85 format = NULL;
86 continue;
87 case FMTSW:
88 if (!(format = *argp++) || *format == '-')
89 die("missing argument to %s", argp[-2]);
90 form = NULL;
91 continue;
92
93 case WIDTHSW:
94 if (!(cp = *argp++) || *cp == '-')
95 die("missing argument to %s", argp[-2]);
96 width = atoi (cp);
97 continue;
98 }
99 }
100 if (datep == NDATES)
101 die("more than %d dates", NDATES);
102 dates[datep++] = cp;
103 }
104 dates[datep] = NULL;
105
106 if (datep == 0)
107 die("usage: %s [switches] dates ...", invo_name);
108
109 /* get new format string */
110 nfs = new_fs (form, format, FORMAT);
111
112 if (width == -1) {
113 if ((width = sc_width ()) < WIDTH / 2) {
114 /* Default: width of the terminal, but at least WIDTH/2. */
115 width = WIDTH / 2;
116 }
117 width -= 2;
118 } else if (width == 0) {
119 /* Unlimited width. */
120 width = INT_MAX;
121 }
122 fmt_compile (nfs, &fmt, 1);
123
124 dat[0] = 0;
125 dat[1] = 0;
126 dat[2] = 0;
127 dat[3] = width;
128 dat[4] = 0;
129
130 for (datep = 0; dates[datep]; datep++)
131 status += process (dates[datep], width);
132
133 context_save (); /* save the context file */
134 fmt_free (fmt, 1);
135 done(!!status);
136 return 1;
137 }
138
139
140 static int
141 process (char *date, int length)
142 {
143 int status = 0;
144 charstring_t scanl =
145 charstring_create (length < NMH_BUFSIZ ? length : NMH_BUFSIZ);
146 struct comp *cptr;
147
148 cptr = fmt_findcomp ("text");
149 if (cptr) {
150 free(cptr->c_text);
151 cptr->c_text = mh_xstrdup(date);
152 }
153 fmt_scan (fmt, scanl, length, dat, NULL);
154 fputs (charstring_buffer (scanl), stdout);
155 charstring_free (scanl);
156
157 return status;
158 }