]>
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/smatch.h"
10 #include "sbr/context_save.h"
11 #include "sbr/ambigsw.h"
12 #include "sbr/print_version.h"
13 #include "sbr/print_help.h"
14 #include "sbr/error.h"
17 #include "h/fmt_scan.h"
19 #include "sbr/terminal.h"
25 #define FORMAT "%<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%>"
28 X("form formatfile", 0, FORMSW) \
29 X("format string", 5, FMTSW) \
30 X("width columns", 0, WIDTHSW) \
31 X("version", 0, VERSIONSW) \
32 X("help", 0, HELPSW) \
34 #define X(sw, minchars, id) id,
35 DEFINE_SWITCH_ENUM(DP
);
38 #define X(sw, minchars, id) { sw, minchars, id },
39 DEFINE_SWITCH_ARRAY(DP
, switches
);
42 static struct format
*fmt
;
49 static int process (char *, int);
53 main (int argc
, char **argv
)
55 int datep
= 0, width
= -1, status
= 0;
56 char *cp
, *form
= NULL
, *format
= NULL
, *nfs
;
57 char buf
[BUFSIZ
], **argp
;
58 char *dates
[NDATES
+ 1]; /* Includes terminating NULL. */
60 if (nmh_init(argv
[0], true, false)) { return 1; }
62 argp
= getarguments (invo_name
, argc
, argv
, 1);
63 while ((cp
= *argp
++)) {
65 switch (smatch (++cp
, switches
)) {
67 ambigsw (cp
, switches
);
70 die("-%s unknown", cp
);
73 snprintf (buf
, sizeof(buf
), "%s [switches] dates ...",
75 print_help (buf
, switches
, 1);
78 print_version(invo_name
);
82 if (!(form
= *argp
++) || *form
== '-')
83 die("missing argument to %s", argp
[-2]);
87 if (!(format
= *argp
++) || *format
== '-')
88 die("missing argument to %s", argp
[-2]);
93 if (!(cp
= *argp
++) || *cp
== '-')
94 die("missing argument to %s", argp
[-2]);
100 die("more than %d dates", NDATES
);
106 die("usage: %s [switches] dates ...", invo_name
);
108 /* get new format string */
109 nfs
= new_fs (form
, format
, FORMAT
);
112 if ((width
= sc_width ()) < WIDTH
/ 2) {
113 /* Default: width of the terminal, but at least WIDTH/2. */
117 } else if (width
== 0) {
118 /* Unlimited width. */
121 fmt_compile (nfs
, &fmt
, 1);
129 for (datep
= 0; dates
[datep
]; datep
++)
130 status
+= process (dates
[datep
], width
);
132 context_save (); /* save the context file */
140 process (char *date
, int length
)
144 charstring_create (length
< NMH_BUFSIZ
? length
: NMH_BUFSIZ
);
147 cptr
= fmt_findcomp ("text");
150 cptr
->c_text
= mh_xstrdup(date
);
152 fmt_scan (fmt
, scanl
, length
, dat
, NULL
);
153 fputs (charstring_buffer (scanl
), stdout
);
154 charstring_free (scanl
);