]>
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/context_save.h"
10 #include "sbr/ambigsw.h"
11 #include "sbr/print_version.h"
12 #include "sbr/print_help.h"
13 #include "sbr/error.h"
16 #include "h/fmt_scan.h"
18 #include "sbr/terminal.h"
24 #define FORMAT "%<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%>"
27 X("form formatfile", 0, FORMSW) \
28 X("format string", 5, FMTSW) \
29 X("width columns", 0, WIDTHSW) \
30 X("version", 0, VERSIONSW) \
31 X("help", 0, HELPSW) \
33 #define X(sw, minchars, id) id,
34 DEFINE_SWITCH_ENUM(DP
);
37 #define X(sw, minchars, id) { sw, minchars, id },
38 DEFINE_SWITCH_ARRAY(DP
, switches
);
41 static struct format
*fmt
;
48 static int process (char *, int);
52 main (int argc
, char **argv
)
54 int datep
= 0, width
= -1, status
= 0;
55 char *cp
, *form
= NULL
, *format
= NULL
, *nfs
;
56 char buf
[BUFSIZ
], **argp
;
57 char *dates
[NDATES
+ 1]; /* Includes terminating NULL. */
59 if (nmh_init(argv
[0], true, false)) { return 1; }
61 argp
= getarguments (invo_name
, argc
, argv
, 1);
62 while ((cp
= *argp
++)) {
64 switch (smatch (++cp
, switches
)) {
66 ambigsw (cp
, switches
);
69 die("-%s unknown", cp
);
72 snprintf (buf
, sizeof(buf
), "%s [switches] dates ...",
74 print_help (buf
, switches
, 1);
77 print_version(invo_name
);
81 if (!(form
= *argp
++) || *form
== '-')
82 die("missing argument to %s", argp
[-2]);
86 if (!(format
= *argp
++) || *format
== '-')
87 die("missing argument to %s", argp
[-2]);
92 if (!(cp
= *argp
++) || *cp
== '-')
93 die("missing argument to %s", argp
[-2]);
99 die("more than %d dates", NDATES
);
105 die("usage: %s [switches] dates ...", invo_name
);
107 /* get new format string */
108 nfs
= new_fs (form
, format
, FORMAT
);
111 if ((width
= sc_width ()) < WIDTH
/ 2) {
112 /* Default: width of the terminal, but at least WIDTH/2. */
116 } else if (width
== 0) {
117 /* Unlimited width. */
120 fmt_compile (nfs
, &fmt
, 1);
128 for (datep
= 0; dates
[datep
]; datep
++)
129 status
+= process (dates
[datep
], width
);
131 context_save (); /* save the context file */
139 process (char *date
, int length
)
143 charstring_create (length
< NMH_BUFSIZ
? length
: NMH_BUFSIZ
);
146 cptr
= fmt_findcomp ("text");
149 cptr
->c_text
= mh_xstrdup(date
);
151 fmt_scan (fmt
, scanl
, length
, dat
, NULL
);
152 fputs (charstring_buffer (scanl
), stdout
);
153 charstring_free (scanl
);