Part of the patch from bug #4301; clean up our prototypes, a lot. Still
[mmh] / uip / scansbr.c
1
2 /*
3  * scansbr.c -- routines to help scan along...
4  *
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.
8  */
9
10 #include <h/mh.h>
11 #include <h/addrsbr.h>
12 #include <h/fmt_scan.h>
13 #include <h/scansbr.h>
14 #include <h/tws.h>
15 #include <h/utils.h>
16
17 #ifdef _FSTDIO
18 # define _ptr _p                /* Gag    */
19 # define _cnt _w                /* Wretch */
20 #endif
21
22 #define MAXSCANL 256            /* longest possible scan line */
23
24 /*
25  * Buffer size for content part of header fields.  We want this
26  * to be large enough so that we don't do a lot of extra FLDPLUS
27  * calls on m_getfld but small enough so that we don't snarf
28  * the entire message body when we're only going to display 30
29  * characters of it.
30  */
31 #define SBUFSIZ 512
32
33 static struct format *fmt;
34 #ifdef JLR
35 static struct format *fmt_top;
36 #endif /* JLR */
37
38 static struct comp *datecomp;           /* pntr to "date" comp             */
39 static struct comp *bodycomp;           /* pntr to "body" pseudo-comp      *
40                                          * (if referenced)                 */
41 static int ncomps = 0;                  /* # of interesting components     */
42 static char **compbuffers = 0;          /* buffers for component text      */
43 static struct comp **used_buf = 0;      /* stack for comp that use buffers */
44
45 static int dat[5];                      /* aux. data for format routine    */
46
47 char *scanl = 0;                        /* text of most recent scanline    */
48
49 #define DIEWRERR() adios (scnmsg, "write error on")
50
51 #define FPUTS(buf) {\
52                 if (mh_fputs(buf,scnout) == EOF)\
53                     DIEWRERR();\
54                 }
55
56 /*
57  * prototypes
58  */
59 static int mh_fputs(char *, FILE *);
60
61 #ifdef MULTIBYTE_SUPPORT
62 #define SCAN_CHARWIDTH MB_CUR_MAX
63 #else
64 #define SCAN_CHARWIDTH 1
65 #endif
66
67 int
68 scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg,
69       int unseen, char *folder, long size, int noisy)
70 {
71     int i, compnum, encrypted, state;
72     unsigned char *cp, *tmpbuf;
73     char **nxtbuf;
74     char *saved_c_text = NULL;
75     struct comp *cptr;
76     struct comp **savecomp;
77     char *scnmsg = NULL;
78     FILE *scnout = NULL;
79     char name[NAMESZ];
80     static int rlwidth, slwidth;
81
82     /* first-time only initialization */
83     if (!scanl) {
84         if (width == 0) {
85             if ((width = sc_width ()) < WIDTH/2)
86                 width = WIDTH/2;
87             else if (width > MAXSCANL)
88                 width = MAXSCANL;
89         }
90         dat[3] = slwidth = width;
91         scanl = (char *) mh_xmalloc((size_t) SCAN_CHARWIDTH * (slwidth + 2) );
92         if (outnum)
93             umask(~m_gmprot());
94
95         /* Compile format string */
96         ncomps = fmt_compile (nfs, &fmt) + 1;
97
98 #ifdef JLR
99         fmt_top = fmt;
100 #endif  /* JLR */
101         FINDCOMP(bodycomp, "body");
102         FINDCOMP(datecomp, "date");
103         FINDCOMP(cptr, "folder");
104         if (cptr && folder)
105             cptr->c_text = folder;
106         FINDCOMP(cptr, "encrypted");
107         if (!cptr)
108             if ((cptr = (struct comp *) calloc (1, sizeof(*cptr)))) {
109                 cptr->c_name = "encrypted";
110                 cptr->c_next = wantcomp[i = CHASH (cptr->c_name)];
111                 wantcomp[i] = cptr;
112                 ncomps++;
113         }
114         FINDCOMP (cptr, "dtimenow");
115         if (cptr)
116             cptr->c_text = getcpy(dtimenow (0));
117         nxtbuf = compbuffers = (char **) calloc((size_t) ncomps, sizeof(char *));
118         if (nxtbuf == NULL)
119             adios (NULL, "unable to allocate component buffers");
120         used_buf = (struct comp **) calloc((size_t) (ncomps+1),
121             sizeof(struct comp *));
122         if (used_buf == NULL)
123             adios (NULL, "unable to allocate component buffer stack");
124         used_buf += ncomps+1; *--used_buf = 0;
125         rlwidth = bodycomp && (width > SBUFSIZ) ? width : SBUFSIZ;
126         for (i = ncomps; i--; )
127             *nxtbuf++ = mh_xmalloc(rlwidth);
128     }
129
130     /*
131      * each-message initialization
132      */
133     nxtbuf = compbuffers;
134     savecomp = used_buf;
135     tmpbuf = *nxtbuf++;
136     dat[0] = innum ? innum : outnum;
137     dat[1] = curflg;
138     dat[4] = unseen;
139
140     /*
141      * Get the first field.  If the message is non-empty
142      * and we're doing an "inc", open the output file.
143      */
144     if ((state = m_getfld (FLD, name, tmpbuf, rlwidth, inb)) == FILEEOF) {
145         if (ferror(inb)) {
146             advise("read", "unable to"); /* "read error" */
147             return SCNFAT;
148         } else {
149             return SCNEOF;
150         }
151     }
152
153     if (outnum) {
154         if (outnum > 0) {
155             scnmsg = m_name (outnum);
156             if (*scnmsg == '?')         /* msg num out of range */
157                 return SCNNUM;
158         } else {
159             scnmsg = "/dev/null";
160         }
161         if ((scnout = fopen (scnmsg, "w")) == NULL)
162             adios (scnmsg, "unable to write");
163     }
164
165     /* scan - main loop */
166     for (compnum = 1; ; state = m_getfld (state, name, tmpbuf, rlwidth, inb)) {
167         switch (state) {
168             case FLD: 
169             case FLDPLUS: 
170                 compnum++;
171                 if (outnum) {
172                     FPUTS (name);
173                     if ( putc (':', scnout) == EOF) DIEWRERR();
174                     FPUTS (tmpbuf);
175                 }
176                 /*
177                  * if we're interested in this component, save a pointer
178                  * to the component text, then start using our next free
179                  * buffer as the component temp buffer (buffer switching
180                  * saves an extra copy of the component text).
181                  */
182                 if ((cptr = wantcomp[CHASH(name)])) {
183                     do {
184                         if (!mh_strcasecmp(name, cptr->c_name)) {
185                             if (! cptr->c_text) {
186                                 cptr->c_text = tmpbuf;
187                                 for (cp = tmpbuf + strlen (tmpbuf) - 1; 
188                                         cp >= tmpbuf; cp--)
189                                     if (isspace (*cp))
190                                         *cp = 0;
191                                     else
192                                         break;
193                                 *--savecomp = cptr;
194                                 tmpbuf = *nxtbuf++;
195                             }
196                             break;
197                         }
198                     } while ((cptr = cptr->c_next));
199                 }
200
201                 while (state == FLDPLUS) {
202                     state = m_getfld (state, name, tmpbuf, rlwidth, inb);
203                     if (outnum)
204                         FPUTS (tmpbuf);
205                 }
206                 break;
207
208             case BODY: 
209                 compnum = -1;
210                 if (! outnum) {
211                     state = FILEEOF; /* stop now if scan cmd */
212                     goto finished;
213                 }
214                 if (putc ('\n', scnout) == EOF) DIEWRERR();
215                 FPUTS (tmpbuf);
216                 /*
217                  * performance hack: some people like to run "inc" on
218                  * things like net.sources or large digests.  We do a
219                  * copy directly into the output buffer rather than
220                  * going through an intermediate buffer.
221                  *
222                  * We need the amount of data m_getfld found & don't
223                  * want to do a strlen on the long buffer so there's
224                  * a hack in m_getfld to save the amount of data it
225                  * returned in the global "msg_count".
226                  */
227 body:;
228                 while (state == BODY) {
229 #ifdef LINUX_STDIO
230                     if (scnout->_IO_write_ptr == scnout->_IO_write_end) {
231 #elif defined(__DragonFly__)
232                     if (((struct __FILE_public *)scnout)->_w <= 0) {
233 #else
234                     if (scnout->_cnt <= 0) {
235 #endif
236                         if (fflush(scnout) == EOF)
237                             DIEWRERR ();
238                     }
239 #ifdef LINUX_STDIO
240                     state = m_getfld(state, name, scnout->_IO_write_ptr,
241                         (long)scnout->_IO_write_ptr-(long)scnout->_IO_write_end , inb);
242                     scnout->_IO_write_ptr += msg_count;
243 #elif defined(__DragonFly__)
244                     state = m_getfld( state, name, ((struct __FILE_public *)scnout)->_p, -(((struct __FILE_public *)scnout)->_w), inb );
245                     ((struct __FILE_public *)scnout)->_w -= msg_count;
246                     ((struct __FILE_public *)scnout)->_p += msg_count;
247 #else
248                     state = m_getfld( state, name, scnout->_ptr, -(scnout->_cnt), inb );
249                     scnout->_cnt -= msg_count;
250                     scnout->_ptr += msg_count;
251 #endif
252                 }
253                 goto finished;
254
255             case LENERR: 
256             case FMTERR: 
257                 fprintf (stderr, 
258                         innum ? "??Format error (message %d) in "
259                               : "??Format error in ",
260                         outnum ? outnum : innum);
261                 fprintf (stderr, "component %d\n", compnum);
262
263                 if (outnum) {
264                     FPUTS ("\n\nBAD MSG:\n");
265                     FPUTS (name);
266                     if (putc ('\n', scnout) == EOF) DIEWRERR();
267                     state = BODY;
268                     goto body;
269                 }
270                 /* fall through */
271
272             case FILEEOF:
273                 goto finished;
274
275             default: 
276                 adios (NULL, "getfld() returned %d", state);
277         }
278     }
279
280     /*
281      * format and output the scan line.
282      */
283 finished:
284     if (ferror(inb)) {
285         advise("read", "unable to"); /* "read error" */
286         return SCNFAT;
287     }
288
289     /* Save and restore buffer so we don't trash our dynamic pool! */
290     if (bodycomp) {
291         saved_c_text = bodycomp->c_text;
292         bodycomp->c_text = tmpbuf;
293     }
294
295     if (size)
296         dat[2] = size;
297     else if (outnum > 0)
298     {
299         dat[2] = ftell(scnout);
300         if (dat[2] == EOF) DIEWRERR();
301     }
302
303     if ((datecomp && !datecomp->c_text) || (!size && !outnum)) {
304         struct stat st;
305
306         fstat (fileno(inb), &st);
307         if (!size && !outnum)
308             dat[2] = st.st_size;
309         if (datecomp) {
310             if (! datecomp->c_text) {
311                 if (datecomp->c_tws == NULL)
312                     datecomp->c_tws = (struct tws *)
313                         calloc((size_t) 1, sizeof(*datecomp->c_tws));
314                 if (datecomp->c_tws == NULL)
315                     adios (NULL, "unable to allocate tws buffer");
316                 *datecomp->c_tws = *dlocaltime ((time_t *) &st.st_mtime);
317                 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
318             } else {
319                 datecomp->c_flags &= ~CF_DATEFAB;
320             }
321         }
322     }
323
324     fmt_scan (fmt, scanl, slwidth, dat);
325
326 #if 0
327     fmt = fmt_scan (fmt, scanl, slwidth, dat);
328     if (!fmt)
329         fmt = fmt_top;          /* reset for old format files */
330 #endif
331
332     if (bodycomp)
333         bodycomp->c_text = saved_c_text;
334
335     if (noisy)
336         fputs (scanl, stdout);
337
338     FINDCOMP (cptr, "encrypted");
339     encrypted = cptr && cptr->c_text;
340
341     /* return dynamically allocated buffers to pool */
342     while ((cptr = *savecomp++)) {
343         *--nxtbuf = cptr->c_text;
344         cptr->c_text = NULL;
345     }
346     *--nxtbuf = tmpbuf;
347
348     if (outnum && (ferror(scnout) || fclose (scnout) == EOF))
349         DIEWRERR();
350
351     return (state != FILEEOF ? SCNERR : encrypted ? SCNENC : SCNMSG);
352 }
353
354
355 /*
356  * Cheat:  we are loaded with adrparse, which wants a routine called
357  * OfficialName().  We call adrparse:getm() with the correct arguments
358  * to prevent OfficialName() from being called.  Hence, the following
359  * is to keep the loader happy.
360  */
361 char *
362 OfficialName (char *name)
363 {
364     return name;
365 }
366
367
368 static int
369 mh_fputs(char *s, FILE *stream)
370 {
371     char c;
372
373     while ((c = *s++)) 
374         if (putc (c,stream) == EOF )
375             return(EOF);
376     return (0);
377 }
378