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