]>
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.
11 #include <h/fmt_scan.h>
18 #define FORMAT "%<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%>"
21 X("form formatfile", 0, FORMSW) \
22 X("format string", 5, FMTSW) \
23 X("width columns", 0, WIDTHSW) \
24 X("version", 0, VERSIONSW) \
25 X("help", 0, HELPSW) \
27 #define X(sw, minchars, id) id,
28 DEFINE_SWITCH_ENUM(DP
);
31 #define X(sw, minchars, id) { sw, minchars, id },
32 DEFINE_SWITCH_ARRAY(DP
, switches
);
35 static struct format
*fmt
;
42 static int process (char *, int);
46 main (int argc
, char **argv
)
48 int datep
= 0, width
= -1, status
= 0;
49 char *cp
, *form
= NULL
, *format
= NULL
, *nfs
;
50 char buf
[BUFSIZ
], **argp
, **arguments
;
53 if (nmh_init(argv
[0], 1)) { return 1; }
55 arguments
= getarguments (invo_name
, argc
, argv
, 1);
58 while ((cp
= *argp
++)) {
60 switch (smatch (++cp
, switches
)) {
62 ambigsw (cp
, switches
);
65 adios (NULL
, "-%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 adios (NULL
, "missing argument to %s", argp
[-2]);
82 if (!(format
= *argp
++) || *format
== '-')
83 adios (NULL
, "missing argument to %s", argp
[-2]);
88 if (!(cp
= *argp
++) || *cp
== '-')
89 adios (NULL
, "missing argument to %s", argp
[-2]);
95 adios (NULL
, "more than %d dates", NDATES
);
102 adios (NULL
, "usage: %s [switches] dates ...", invo_name
);
104 /* get new format string */
105 nfs
= new_fs (form
, format
, FORMAT
);
108 if ((width
= sc_width ()) < WIDTH
/ 2) {
109 /* Default: width of the terminal, but at least WIDTH/2. */
113 } else if (width
== 0) {
114 /* Unlimited width. */
117 fmt_compile (nfs
, &fmt
, 1);
125 for (datep
= 0; dates
[datep
]; datep
++)
126 status
+= process (dates
[datep
], width
);
128 context_save (); /* save the context file */
136 process (char *date
, int length
)
140 charstring_create (length
< NMH_BUFSIZ
? length
: NMH_BUFSIZ
);
141 register struct comp
*cptr
;
143 cptr
= fmt_findcomp ("text");
147 cptr
->c_text
= getcpy(date
);
149 fmt_scan (fmt
, scanl
, length
, dat
, NULL
);
150 fputs (charstring_buffer (scanl
), stdout
);
151 charstring_free (scanl
);