]>
diplodocus.org Git - nmh/blob - uip/dp.c
1 /* dp.c -- parse dates 822-style
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.
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"
18 #include "h/fmt_scan.h"
20 #include "sbr/terminal.h"
26 #define FORMAT "%<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%>"
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) \
35 #define X(sw, minchars, id) id,
36 DEFINE_SWITCH_ENUM(DP
);
39 #define X(sw, minchars, id) { sw, minchars, id },
40 DEFINE_SWITCH_ARRAY(DP
, switches
);
43 static struct format
*fmt
;
50 static int process (char *, int);
54 main (int argc
, char **argv
)
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. */
61 if (nmh_init(argv
[0], true, false)) { return 1; }
63 argp
= getarguments (invo_name
, argc
, argv
, 1);
64 while ((cp
= *argp
++)) {
66 switch (smatch (++cp
, switches
)) {
68 ambigsw (cp
, switches
);
71 die("-%s unknown", cp
);
74 snprintf (buf
, sizeof(buf
), "%s [switches] dates ...",
76 print_help (buf
, switches
, 1);
79 print_version(invo_name
);
83 if (!(form
= *argp
++) || *form
== '-')
84 die("missing argument to %s", argp
[-2]);
88 if (!(format
= *argp
++) || *format
== '-')
89 die("missing argument to %s", argp
[-2]);
94 if (!(cp
= *argp
++) || *cp
== '-')
95 die("missing argument to %s", argp
[-2]);
101 die("more than %d dates", NDATES
);
107 die("usage: %s [switches] dates ...", invo_name
);
109 /* get new format string */
110 nfs
= new_fs (form
, format
, FORMAT
);
113 if ((width
= sc_width ()) < WIDTH
/ 2) {
114 /* Default: width of the terminal, but at least WIDTH/2. */
118 } else if (width
== 0) {
119 /* Unlimited width. */
122 fmt_compile (nfs
, &fmt
, 1);
130 for (datep
= 0; dates
[datep
]; datep
++)
131 status
+= process (dates
[datep
], width
);
133 context_save (); /* save the context file */
141 process (char *date
, int length
)
145 charstring_create (length
< NMH_BUFSIZ
? length
: NMH_BUFSIZ
);
148 cptr
= fmt_findcomp ("text");
151 cptr
->c_text
= mh_xstrdup(date
);
153 fmt_scan (fmt
, scanl
, length
, dat
, NULL
);
154 fputs (charstring_buffer (scanl
), stdout
);
155 charstring_free (scanl
);