Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / uip / dp.c
1 /* dp.c  - parse dates 822-style */
2 #ifndef lint
3 static char ident[] = "@(#)$Id: dp.c,v 1.7 1992/12/15 00:20:22 jromine Exp $";
4 #endif  /* lint */
5
6 #include "../h/mh.h"
7 #include "../h/formatsbr.h"
8 #include "../zotnet/tws.h"
9 #include <stdio.h>
10 #ifdef LOCALE
11 #include        <locale.h>
12 #endif
13
14
15 #define NDATES  100
16
17 #define WIDTH   78
18 #define WBUFSIZ BUFSIZ
19
20 #define FORMAT  "%<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%>"
21
22 /* \f */
23
24 static struct swit switches[] = {
25 #define FORMSW  0
26     "form formatfile", 0,
27 #define FMTSW   1
28     "format string", 5,
29
30 #define WIDSW   2
31     "width columns", 0,
32
33 #define HELPSW  3
34     "help", 4,
35
36     NULL, 0
37 };
38
39 /* \f */
40
41 static struct format *fmt;
42
43 static int dat[5];
44
45 static int      process();
46 /* \f */
47
48 /* ARGSUSED */
49
50 main (argc, argv)
51 int argc;
52 char **argv;
53 {
54     int     datep = 0,
55             width = 0,
56             status = 0;
57     char   *cp,
58            *form = NULL,
59            *format = NULL,
60            *nfs,
61             buf[80],
62           **ap,
63           **argp,
64            *arguments[MAXARGS],
65            *dates[NDATES];
66
67 #ifdef LOCALE
68         setlocale(LC_ALL, "");
69 #endif
70     invo_name = r1bindex (argv[0], '/');
71     if ((cp = m_find (invo_name)) != NULL) {
72         ap = brkstring (cp = getcpy (cp), " ", "\n");
73         ap = copyip (ap, arguments);
74     }
75     else
76         ap = arguments;
77     (void) copyip (argv + 1, ap);
78     argp = arguments;
79
80 /* \f */
81
82     while (cp = *argp++) {
83         if (*cp == '-')
84             switch (smatch (++cp, switches)) {
85                 case AMBIGSW: 
86                     ambigsw (cp, switches);
87                     done (1);
88                 case UNKWNSW: 
89                     adios (NULLCP, "-%s unknown", cp);
90                 case HELPSW: 
91                     (void) sprintf (buf, "%s [switches] dates ...", invo_name);
92                     help (buf, switches);
93                     done (1);
94
95                 case FORMSW: 
96                     if (!(form = *argp++) || *form == '-')
97                         adios (NULLCP, "missing argument to %s", argp[-2]);
98                     format = NULL;
99                     continue;
100                 case FMTSW: 
101                     if (!(format = *argp++) || *format == '-')
102                         adios (NULLCP, "missing argument to %s", argp[-2]);
103                     form = NULL;
104                     continue;
105
106                 case WIDSW: 
107                     if (!(cp = *argp++) || *cp == '-')
108                         adios (NULLCP, "missing argument to %s", argp[-2]);
109                     width = atoi (cp);
110                     continue;
111             }
112         if (datep > NDATES)
113             adios (NULLCP, "more than %d dates", NDATES);
114         else
115             dates[datep++] = cp;
116     }
117     dates[datep] = NULL;
118
119 /* \f */
120
121     if (datep == 0)
122         adios (NULLCP, "usage: %s [switches] dates ...", invo_name);
123
124     nfs = new_fs (form, format, FORMAT);
125     if (width == 0) {
126         if ((width = sc_width ()) < WIDTH / 2)
127             width = WIDTH / 2;
128         width -= 2;
129     }
130     if (width > WBUFSIZ)
131         width = WBUFSIZ;
132     (void) fmt_compile (nfs, &fmt);
133     dat[0] = dat[1] = dat[2] = dat[4] = 0;
134     dat[3] = width;
135
136     for (datep = 0; dates[datep]; datep++)
137         status += process (dates[datep], width);
138
139     m_update ();
140
141     done (status);
142 }
143
144 /* \f */
145
146 static  int process (date, length)
147 register char   *date;
148 int     length;
149 {
150     int     status = 0;
151     char    buffer[WBUFSIZ + 1];
152     register struct comp   *cptr;
153
154     FINDCOMP (cptr, "text");
155     if (cptr)
156         cptr -> c_text = date;
157     (void) fmtscan (fmt, buffer, length, dat);
158     (void) fputs (buffer, stdout);
159
160     return status;
161 }