]>
diplodocus.org Git - nmh/blob - uip/dp.c
e6cfa60c601a053b21534cb834fa1f6131a9eb29
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.
12 #include <h/fmt_scan.h>
14 #include "sbr/terminal.h"
20 #define FORMAT "%<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%>"
23 X("form formatfile", 0, FORMSW) \
24 X("format string", 5, FMTSW) \
25 X("width columns", 0, WIDTHSW) \
26 X("version", 0, VERSIONSW) \
27 X("help", 0, HELPSW) \
29 #define X(sw, minchars, id) id,
30 DEFINE_SWITCH_ENUM(DP
);
33 #define X(sw, minchars, id) { sw, minchars, id },
34 DEFINE_SWITCH_ARRAY(DP
, switches
);
37 static struct format
*fmt
;
44 static int process (char *, int);
48 main (int argc
, char **argv
)
50 int datep
= 0, width
= -1, status
= 0;
51 char *cp
, *form
= NULL
, *format
= NULL
, *nfs
;
52 char buf
[BUFSIZ
], **argp
;
53 char *dates
[NDATES
+ 1]; /* Includes terminating NULL. */
55 if (nmh_init(argv
[0], true, false)) { return 1; }
57 argp
= getarguments (invo_name
, argc
, argv
, 1);
58 while ((cp
= *argp
++)) {
60 switch (smatch (++cp
, switches
)) {
62 ambigsw (cp
, switches
);
65 die("-%s unknown", cp
);
68 snprintf (buf
, sizeof(buf
), "%s [switches] dates ...",
70 print_help (buf
, switches
, 1);
73 print_version(invo_name
);
77 if (!(form
= *argp
++) || *form
== '-')
78 die("missing argument to %s", argp
[-2]);
82 if (!(format
= *argp
++) || *format
== '-')
83 die("missing argument to %s", argp
[-2]);
88 if (!(cp
= *argp
++) || *cp
== '-')
89 die("missing argument to %s", argp
[-2]);
95 die("more than %d dates", NDATES
);
101 die("usage: %s [switches] dates ...", invo_name
);
103 /* get new format string */
104 nfs
= new_fs (form
, format
, FORMAT
);
107 if ((width
= sc_width ()) < WIDTH
/ 2) {
108 /* Default: width of the terminal, but at least WIDTH/2. */
112 } else if (width
== 0) {
113 /* Unlimited width. */
116 fmt_compile (nfs
, &fmt
, 1);
124 for (datep
= 0; dates
[datep
]; datep
++)
125 status
+= process (dates
[datep
], width
);
127 context_save (); /* save the context file */
135 process (char *date
, int length
)
139 charstring_create (length
< NMH_BUFSIZ
? length
: NMH_BUFSIZ
);
142 cptr
= fmt_findcomp ("text");
145 cptr
->c_text
= mh_xstrdup(date
);
147 fmt_scan (fmt
, scanl
, length
, dat
, NULL
);
148 fputs (charstring_buffer (scanl
), stdout
);
149 charstring_free (scanl
);