]>
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.
10 #include <h/fmt_scan.h>
17 #define FORMAT "%<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%>"
20 X("form formatfile", 0, FORMSW) \
21 X("format string", 5, FMTSW) \
22 X("width columns", 0, WIDTHSW) \
23 X("version", 0, VERSIONSW) \
24 X("help", 0, HELPSW) \
26 #define X(sw, minchars, id) id,
27 DEFINE_SWITCH_ENUM(DP
);
30 #define X(sw, minchars, id) { sw, minchars, id },
31 DEFINE_SWITCH_ARRAY(DP
, switches
);
34 static struct format
*fmt
;
41 static int process (char *, int);
45 main (int argc
, char **argv
)
47 int datep
= 0, width
= -1, status
= 0;
48 char *cp
, *form
= NULL
, *format
= NULL
, *nfs
;
49 char buf
[BUFSIZ
], **argp
;
50 char *dates
[NDATES
+ 1]; /* Includes terminating NULL. */
52 if (nmh_init(argv
[0], 2)) { return 1; }
54 argp
= getarguments (invo_name
, argc
, argv
, 1);
55 while ((cp
= *argp
++)) {
57 switch (smatch (++cp
, switches
)) {
59 ambigsw (cp
, switches
);
62 adios (NULL
, "-%s unknown", cp
);
65 snprintf (buf
, sizeof(buf
), "%s [switches] dates ...",
67 print_help (buf
, switches
, 1);
70 print_version(invo_name
);
74 if (!(form
= *argp
++) || *form
== '-')
75 adios (NULL
, "missing argument to %s", argp
[-2]);
79 if (!(format
= *argp
++) || *format
== '-')
80 adios (NULL
, "missing argument to %s", argp
[-2]);
85 if (!(cp
= *argp
++) || *cp
== '-')
86 adios (NULL
, "missing argument to %s", argp
[-2]);
92 adios (NULL
, "more than %d dates", NDATES
);
99 adios (NULL
, "usage: %s [switches] dates ...", invo_name
);
101 /* get new format string */
102 nfs
= new_fs (form
, format
, FORMAT
);
105 if ((width
= sc_width ()) < WIDTH
/ 2) {
106 /* Default: width of the terminal, but at least WIDTH/2. */
110 } else if (width
== 0) {
111 /* Unlimited width. */
114 fmt_compile (nfs
, &fmt
, 1);
122 for (datep
= 0; dates
[datep
]; datep
++)
123 status
+= process (dates
[datep
], width
);
125 context_save (); /* save the context file */
133 process (char *date
, int length
)
137 charstring_create (length
< NMH_BUFSIZ
? length
: NMH_BUFSIZ
);
140 cptr
= fmt_findcomp ("text");
142 mh_xfree(cptr
->c_text
);
143 cptr
->c_text
= mh_xstrdup(date
);
145 fmt_scan (fmt
, scanl
, length
, dat
, NULL
);
146 fputs (charstring_buffer (scanl
), stdout
);
147 charstring_free (scanl
);