]>
diplodocus.org Git - nmh/blob - uip/dp.c
3 * dp.c -- parse dates 822-style
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/fmt_scan.h>
19 #define FORMAT "%<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%>"
22 X("form formatfile", 0, FORMSW) \
23 X("format string", 5, FMTSW) \
24 X("width columns", 0, WIDTHSW) \
25 X("version", 0, VERSIONSW) \
26 X("help", 0, HELPSW) \
28 #define X(sw, minchars, id) id,
29 DEFINE_SWITCH_ENUM(DP
);
32 #define X(sw, minchars, id) { sw, minchars, id },
33 DEFINE_SWITCH_ARRAY(DP
, switches
);
36 static struct format
*fmt
;
43 static int process (char *, int);
47 main (int argc
, char **argv
)
49 int datep
= 0, width
= -1, status
= 0;
50 char *cp
, *form
= NULL
, *format
= NULL
, *nfs
;
51 char buf
[BUFSIZ
], **argp
;
52 char *dates
[NDATES
+ 1]; /* Includes terminating NULL. */
54 if (nmh_init(argv
[0], 2)) { return 1; }
56 argp
= getarguments (invo_name
, argc
, argv
, 1);
57 while ((cp
= *argp
++)) {
59 switch (smatch (++cp
, switches
)) {
61 ambigsw (cp
, switches
);
64 adios (NULL
, "-%s unknown", cp
);
67 snprintf (buf
, sizeof(buf
), "%s [switches] dates ...",
69 print_help (buf
, switches
, 1);
72 print_version(invo_name
);
76 if (!(form
= *argp
++) || *form
== '-')
77 adios (NULL
, "missing argument to %s", argp
[-2]);
81 if (!(format
= *argp
++) || *format
== '-')
82 adios (NULL
, "missing argument to %s", argp
[-2]);
87 if (!(cp
= *argp
++) || *cp
== '-')
88 adios (NULL
, "missing argument to %s", argp
[-2]);
94 adios (NULL
, "more than %d dates", NDATES
);
101 adios (NULL
, "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");
144 mh_xfree(cptr
->c_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
);