scan(): Symbolic names instead of magic numbers.
[mmh] / uip / scansbr.c
1 /*
2 ** scansbr.c -- routines to help scan along...
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <h/addrsbr.h>
11 #include <h/fmt_scan.h>
12 #include <h/scansbr.h>
13 #include <h/tws.h>
14 #include <h/utils.h>
15
16 #ifdef _FSTDIO
17 # define _ptr _p  /* Gag */
18 # define _cnt _w  /* Wretch */
19 #endif
20
21 #ifdef SCO_5_STDIO
22 # define _ptr  __ptr
23 # define _cnt  __cnt
24 # define _base __base
25 # define _filbuf(fp)  ((fp)->__cnt = 0, __filbuf(fp))
26 #endif
27
28 #define MAXSCANL 256  /* longest possible scan line */
29
30 /*
31 ** Buffer size for content part of header fields.  We want this
32 ** to be large enough so that we don't do a lot of extra FLDPLUS
33 ** calls on m_getfld.
34 */
35 #define SBUFSIZ 512
36
37 static struct format *fmt;
38
39 static struct comp *datecomp;  /* pntr to "date" comp */
40 static int ncomps = 0;  /* # of interesting components */
41 static char **compbuffers = NULL;  /* buffers for component text */
42 static struct comp **used_buf = NULL;  /* stack for comp that use buffers */
43
44 static int dat[5];  /* aux. data for format routine */
45
46 char *scanl = NULL;  /* text of most recent scanline */
47
48 #define FPUTS(buf) {\
49                 if (fputs(buf, scnout) == EOF)\
50                         adios(scnmsg, "write error on");\
51         }
52
53 /*
54 ** prototypes
55 */
56 int sc_width(void);  /* from termsbr.c */
57
58 #ifdef MULTIBYTE_SUPPORT
59 #define SCAN_CHARWIDTH MB_CUR_MAX
60 #else
61 #define SCAN_CHARWIDTH 1
62 #endif
63
64 int
65 scan(FILE *inb, int innum, int outnum, char *fmtstr, int width, int curflg,
66         int unseen)
67 {
68         static int slwidth;
69         int i, compnum, state;
70         unsigned char *cp, *tmpbuf;
71         char **nxtbuf;
72         struct comp *cptr;
73         struct comp **savecomp;
74         char *scnmsg = NULL;
75         FILE *scnout = NULL;
76         char name[NAMESZ];
77         char returnpath[BUFSIZ];
78         char deliverydate[BUFSIZ];
79         int incing = (outnum != SCN_MBOX && outnum != SCN_FOLD);
80         int scanfolder = (outnum == SCN_FOLD);
81         long fpos;
82
83         /* first-time only initialization */
84         if (!scanl) {
85                 if (width == 0) {
86                         if ((width = sc_width()) < WIDTH/2)
87                                 width = WIDTH/2;
88                         else if (width > MAXSCANL)
89                                 width = MAXSCANL;
90                 }
91                 dat[3] = slwidth = width;
92                 scanl = (char *) mh_xmalloc((size_t) SCAN_CHARWIDTH *
93                                 (slwidth + 2));  /* probably for \n and \0 */
94                 if (incing)
95                         umask(~m_gmprot());
96
97                 /* Compile format string */
98                 ncomps = fmt_compile(fmtstr, &fmt) + 1;
99
100                 FINDCOMP(datecomp, "date");
101                 nxtbuf = compbuffers = (char **) calloc((size_t) ncomps,
102                                 sizeof(char *));
103                 if (!nxtbuf)
104                         adios(NULL, "unable to allocate component buffers");
105                 used_buf = (struct comp **) calloc((size_t) (ncomps+1),
106                                 sizeof(struct comp *));
107                 if (!used_buf)
108                         adios(NULL, "unable to allocate component buffer stack");
109                 /* NULL-terminate array */
110                 used_buf += ncomps;
111                 *used_buf = NULL;
112                 /* allocate space for the items */
113                 for (i = ncomps; i--; )
114                         *nxtbuf++ = mh_xmalloc(SBUFSIZ);
115         }
116
117         /*
118         ** each-message initialization
119         */
120         nxtbuf = compbuffers;
121         savecomp = used_buf;
122         tmpbuf = *nxtbuf++;
123         dat[0] = innum ? innum : outnum;
124         dat[1] = curflg;
125         dat[4] = unseen;
126         fpos = ftell(inb);
127
128         /*
129         ** Get the first field.  If the message is non-empty
130         ** and we're doing an "inc", open the output file.
131         */
132         if ((state = m_getfld(FLD, name, tmpbuf, SBUFSIZ, inb)) == FILEEOF) {
133                 if (ferror(inb)) {
134                         advise("read", "unable to"); /* "read error" */
135                         return SCNFAT;
136                 } else {
137                         return SCNEOF;
138                 }
139         }
140
141         if (incing) {
142                 scnmsg = m_name(outnum);
143                 if (*scnmsg == '?')  /* msg num out of range */
144                         return SCNNUM;
145                 if (!(scnout = fopen(scnmsg, "w")))
146                         adios(scnmsg, "unable to write");
147                 /*
148                 ** Add the Return-Path and Delivery-Date
149                 ** header fields to message.
150                 */
151                 if (get_returnpath(returnpath, sizeof(returnpath),
152                                 deliverydate, sizeof(deliverydate))) {
153                         FPUTS("Return-Path: ");
154                         FPUTS(returnpath);
155                         FPUTS("Delivery-Date: ");
156                         FPUTS(deliverydate);
157                 }
158         }
159
160         /* scan - main loop */
161         for (compnum = 1; ;
162                         state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb)) {
163                 switch (state) {
164                 case FLD:
165                 case FLDPLUS:
166                         compnum++;
167                         if (incing) {
168                                 FPUTS(name);
169                                 FPUTS(":");
170                                 FPUTS(tmpbuf);
171                         }
172                         /*
173                         ** if we're interested in this component, save
174                         ** a pointer to the component text, then start
175                         ** using our next free buffer as the component
176                         ** temp buffer (buffer switching saves an extra
177                         ** copy of the component text).
178                         */
179                         if ((cptr = wantcomp[CHASH(name)])) {
180                                 do {
181                                         if (!mh_strcasecmp(name, cptr->c_name)) {
182                                                 if (! cptr->c_text) {
183                                                         cptr->c_text = tmpbuf;
184                                                         for (cp = tmpbuf + strlen(tmpbuf) - 1; cp >= tmpbuf; cp--)
185                                                                 if (isspace(*cp))
186                                                                         *cp = 0;
187                                                                 else
188                                                                         break;
189                                                         *--savecomp = cptr;
190                                                         tmpbuf = *nxtbuf++;
191                                                 }
192                                                 break;
193                                         }
194                                 } while ((cptr = cptr->c_next));
195                         }
196
197                         while (state == FLDPLUS) {
198                                 state = m_getfld(state, name, tmpbuf, SBUFSIZ,
199                                                 inb);
200                                 if (incing)
201                                         FPUTS(tmpbuf);
202                         }
203                         break;
204
205                 case BODY:
206                         compnum = -1;
207                         if (scanfolder) {
208                                 /* stop here if we scan a msg in a folder */
209                                 state = FILEEOF;
210                                 goto finished;
211                         }
212                         /* otherwise (mbox): snarf the body */
213                         if (incing) {
214                                 FPUTS("\n");
215                                 FPUTS(tmpbuf);
216                         }
217 body:;
218                         while (state == BODY) {
219                                 state = m_getfld(state, name, tmpbuf, SBUFSIZ,
220                                                 inb);
221                                 if (incing) {
222                                         FPUTS(tmpbuf);
223                                 }
224                         }
225                         goto finished;
226
227                 case LENERR:
228                 case FMTERR:
229                         fprintf(stderr, innum ? "??Format error (message %d) in " : "??Format error in ", outnum ? outnum : innum);
230                         fprintf(stderr, "component %d\n", compnum);
231
232                         if (incing) {
233                                 FPUTS("\n\nBAD MSG:\n");
234                                 FPUTS(name);
235                                 FPUTS("\n");
236                                 state = BODY;
237                                 goto body;
238                         }
239                         /* fall through if we scan only */
240
241                 case FILEEOF:
242                         goto finished;
243
244                 default:
245                         adios(NULL, "getfld() returned %d", state);
246                 }
247         }
248
249 finished:
250
251         /* Format and output the scan line. */
252         if (ferror(inb)) {
253                 advise("read", "unable to");
254                 return SCNFAT;
255         }
256
257         if (incing) {
258                 if ((dat[2] = ftell(scnout)) == EOF)
259                         adios(scnmsg, "write error on");
260         } else if (!scanfolder) {
261                 if ((dat[2] = ftell(inb)) == EOF)
262                         adios(scnmsg, "write error on");
263                 dat[2] -= fpos;
264         }
265
266         if ((datecomp && !datecomp->c_text) || scanfolder) {
267                 struct stat st;
268
269                 fstat(fileno(inb), &st);
270                 if (scanfolder)
271                         dat[2] = st.st_size;
272                 if (datecomp) {
273                         if (!datecomp->c_text) {
274                                 if (!datecomp->c_tws)
275                                         datecomp->c_tws = (struct tws *) calloc((size_t) 1, sizeof(*datecomp->c_tws));
276                                 if (!datecomp->c_tws)
277                                         adios(NULL, "unable to allocate tws buffer");
278                                 *datecomp->c_tws = *dlocaltime((time_t *) &st.st_mtime);
279                                 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
280                         } else {
281                                 datecomp->c_flags &= ~CF_DATEFAB;
282                         }
283                 }
284         }
285
286         /* print the scan line */
287         fmt_scan(fmt, scanl, slwidth, dat);
288         if (fmtstr)
289                 fputs(scanl, stdout);
290
291         /* return dynamically allocated buffers to pool */
292         while ((cptr = *savecomp++)) {
293                 *--nxtbuf = cptr->c_text;
294                 cptr->c_text = NULL;
295         }
296         *--nxtbuf = tmpbuf;
297
298         if (incing && (ferror(scnout) || fclose(scnout) == EOF))
299                 adios(scnmsg, "write error on");
300
301         return (state != FILEEOF ? SCNERR : SCNMSG);
302 }