]>
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
, **arguments
;
54 if (nmh_init(argv
[0], 2)) { return 1; }
56 arguments
= getarguments (invo_name
, argc
, argv
, 1);
59 while ((cp
= *argp
++)) {
61 switch (smatch (++cp
, switches
)) {
63 ambigsw (cp
, switches
);
66 adios (NULL
, "-%s unknown", cp
);
69 snprintf (buf
, sizeof(buf
), "%s [switches] dates ...",
71 print_help (buf
, switches
, 1);
74 print_version(invo_name
);
78 if (!(form
= *argp
++) || *form
== '-')
79 adios (NULL
, "missing argument to %s", argp
[-2]);
83 if (!(format
= *argp
++) || *format
== '-')
84 adios (NULL
, "missing argument to %s", argp
[-2]);
89 if (!(cp
= *argp
++) || *cp
== '-')
90 adios (NULL
, "missing argument to %s", argp
[-2]);
96 adios (NULL
, "more than %d dates", NDATES
);
103 adios (NULL
, "usage: %s [switches] dates ...", invo_name
);
105 /* get new format string */
106 nfs
= new_fs (form
, format
, FORMAT
);
109 if ((width
= sc_width ()) < WIDTH
/ 2) {
110 /* Default: width of the terminal, but at least WIDTH/2. */
114 } else if (width
== 0) {
115 /* Unlimited width. */
118 fmt_compile (nfs
, &fmt
, 1);
126 for (datep
= 0; dates
[datep
]; datep
++)
127 status
+= process (dates
[datep
], width
);
129 context_save (); /* save the context file */
137 process (char *date
, int length
)
141 charstring_create (length
< NMH_BUFSIZ
? length
: NMH_BUFSIZ
);
144 cptr
= fmt_findcomp ("text");
146 mh_xfree(cptr
->c_text
);
147 cptr
->c_text
= mh_xstrdup(date
);
149 fmt_scan (fmt
, scanl
, length
, dat
, NULL
);
150 fputs (charstring_buffer (scanl
), stdout
);
151 charstring_free (scanl
);