Remove the peeking into the stdio internals inside of scan(). Hm, this
[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 #define MAXSCANL 256            /* longest possible scan line */
18
19 /*
20  * Buffer size for content part of header fields.  We want this
21  * to be large enough so that we don't do a lot of extra FLDPLUS
22  * calls on m_getfld but small enough so that we don't snarf
23  * the entire message body when we're only going to display 30
24  * characters of it.
25  */
26 #define SBUFSIZ 512
27
28 static struct format *fmt;
29 static struct comp *datecomp;           /* pntr to "date" comp             */
30 static struct comp *bodycomp;           /* pntr to "body" pseudo-comp      *
31                                          * (if referenced)                 */
32 static int ncomps = 0;                  /* # of interesting components     */
33 static char **compbuffers = 0;          /* buffers for component text      */
34 static struct comp **used_buf = 0;      /* stack for comp that use buffers */
35
36 static int dat[5];                      /* aux. data for format routine    */
37
38 char *scanl = 0;                        /* text of most recent scanline    */
39
40 #define DIEWRERR() adios (scnmsg, "write error on")
41
42 #define FPUTS(buf) {\
43                 if (mh_fputs(buf,scnout) == EOF)\
44                     DIEWRERR();\
45                 }
46
47 /*
48  * prototypes
49  */
50 static int mh_fputs(char *, FILE *);
51
52 #ifdef MULTIBYTE_SUPPORT
53 #define SCAN_CHARWIDTH MB_CUR_MAX
54 #else
55 #define SCAN_CHARWIDTH 1
56 #endif
57
58 int
59 scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg,
60       int unseen, char *folder, long size, int noisy)
61 {
62     int i, compnum, encrypted, state;
63     unsigned char *cp, *tmpbuf, *startbody;
64     char **nxtbuf;
65     char *saved_c_text = NULL;
66     struct comp *cptr;
67     struct comp **savecomp;
68     char *scnmsg = NULL;
69     FILE *scnout = NULL;
70     char name[NAMESZ];
71     static int rlwidth, slwidth;
72     static size_t scanl_size;
73
74     /* first-time only initialization */
75     if (!scanl) {
76         if (width == 0) {
77             if ((width = sc_width ()) < WIDTH/2)
78                 width = WIDTH/2;
79             else if (width > MAXSCANL)
80                 width = MAXSCANL;
81         }
82         dat[3] = slwidth = width;
83         /* Arbitrarily allocate 20 * slwidth to provide room for lots
84            of escape sequences. */
85         scanl_size = SCAN_CHARWIDTH * (20 * slwidth + 2);
86         scanl = (char *) mh_xmalloc (scanl_size);
87         if (outnum)
88             umask(~m_gmprot());
89
90         /* Compile format string */
91         ncomps = fmt_compile (nfs, &fmt, 1) + 2;
92
93         bodycomp = fmt_findcomp("body");
94         datecomp = fmt_findcomp("date");
95         cptr = fmt_findcomp("folder");
96         if (cptr && folder)
97             cptr->c_text = getcpy(folder);
98         if (fmt_addcompentry("encrypted")) {
99                 ncomps++;
100         }
101         cptr =  fmt_findcomp("dtimenow");
102         if (cptr)
103             cptr->c_text = getcpy(dtimenow (0));
104
105         /*
106          * In other programs I got rid of this complicated buffer switching,
107          * but since scan reads lots of messages at once and this complicated
108          * memory management, I decided to keep it; otherwise there was
109          * the potential for a lot of malloc() and free()s, and I could
110          * see the malloc() pool really getting fragmented.  Maybe it
111          * wouldn't be an issue in practice; perhaps this will get
112          * revisited someday.
113          *
114          * So, some notes for what's going on:
115          *
116          * nxtbuf is an array of pointers that contains malloc()'d buffers
117          * to hold our component text.  used_buf is an array of struct comp
118          * pointers that holds pointers to component structures we found while
119          * processing a message.
120          *
121          * We read in the message with m_getfld(), using "tmpbuf" as our
122          * input buffer.  tmpbuf is set at the start of message processing
123          * to the first buffer in our buffer pool (nxtbuf).
124          *
125          * Every time we find a component we care about, we set that component's
126          * text buffer to the current value of tmpbuf, and then switch tmpbuf
127          * to the next buffer in our pool.  We also add that component to
128          * our used_buf pool.
129          *
130          * When we're done, we go back and zero out all of the component
131          * text buffer pointers that we saved in used_buf.
132          *
133          * Note that this means c_text memory is NOT owned by the fmt_module
134          * and it's our responsibility to free it.
135          */
136
137         nxtbuf = compbuffers = (char **) calloc((size_t) ncomps, sizeof(char *));
138         if (nxtbuf == NULL)
139             adios (NULL, "unable to allocate component buffers");
140         used_buf = (struct comp **) calloc((size_t) (ncomps+1),
141             sizeof(struct comp *));
142         if (used_buf == NULL)
143             adios (NULL, "unable to allocate component buffer stack");
144         used_buf += ncomps+1; *--used_buf = 0;
145         rlwidth = bodycomp && (width > SBUFSIZ) ? width : SBUFSIZ;
146         for (i = ncomps; i--; )
147             *nxtbuf++ = mh_xmalloc(rlwidth);
148     }
149
150     /*
151      * each-message initialization
152      */
153     nxtbuf = compbuffers;
154     savecomp = used_buf;
155     tmpbuf = *nxtbuf++;
156     startbody = NULL;
157     dat[0] = innum ? innum : outnum;
158     dat[1] = curflg;
159     dat[4] = unseen;
160
161     /*
162      * Get the first field.  If the message is non-empty
163      * and we're doing an "inc", open the output file.
164      */
165     if ((state = m_getfld (FLD, name, tmpbuf, rlwidth, inb)) == FILEEOF) {
166         if (ferror(inb)) {
167             advise("read", "unable to"); /* "read error" */
168             return SCNFAT;
169         } else {
170             return SCNEOF;
171         }
172     }
173
174     if (outnum) {
175         if (outnum > 0) {
176             scnmsg = m_name (outnum);
177             if (*scnmsg == '?')         /* msg num out of range */
178                 return SCNNUM;
179         } else {
180             scnmsg = "/dev/null";
181         }
182         if ((scnout = fopen (scnmsg, "w")) == NULL)
183             adios (scnmsg, "unable to write");
184     }
185
186     /* scan - main loop */
187     for (compnum = 1; ; state = m_getfld (state, name, tmpbuf, rlwidth, inb)) {
188         switch (state) {
189             case FLD: 
190             case FLDPLUS: 
191                 compnum++;
192                 if (outnum) {
193                     FPUTS (name);
194                     if ( putc (':', scnout) == EOF) DIEWRERR();
195                     FPUTS (tmpbuf);
196                 }
197                 /*
198                  * if we're interested in this component, save a pointer
199                  * to the component text, then start using our next free
200                  * buffer as the component temp buffer (buffer switching
201                  * saves an extra copy of the component text).
202                  */
203                 if ((cptr = fmt_findcasecomp(name))) {
204                     if (! cptr->c_text) {
205                         cptr->c_text = tmpbuf;
206                         for (cp = tmpbuf + strlen (tmpbuf) - 1; 
207                                         cp >= tmpbuf; cp--)
208                             if (isspace (*cp))
209                                 *cp = 0;
210                             else
211                                 break;
212                         *--savecomp = cptr;
213                         tmpbuf = *nxtbuf++;
214                     }
215                 }
216
217                 while (state == FLDPLUS) {
218                     state = m_getfld (state, name, tmpbuf, rlwidth, inb);
219                     if (outnum)
220                         FPUTS (tmpbuf);
221                 }
222                 break;
223
224             case BODY: 
225                 compnum = -1;
226                 /*
227                  * A slight hack ... if we have less than rlwidth characters
228                  * in the buffer, call m_getfld again.
229                  */
230
231                 if ((i = strlen(tmpbuf)) < rlwidth) {
232                     state = m_getfld (state, name, tmpbuf + i,
233                                       rlwidth - i, inb);
234                 }
235
236                 if (! outnum) {
237                     state = FILEEOF; /* stop now if scan cmd */
238                     if (bodycomp && startbody == NULL)
239                         startbody = tmpbuf;
240                     goto finished;
241                 }
242                 if (putc ('\n', scnout) == EOF) DIEWRERR();
243                 FPUTS (tmpbuf);
244                 /*
245                  * The previous code here used to call m_getfld() using
246                  * pointers to the underlying output stdio buffers to
247                  * avoid the extra copy.  Tests by Markus Schnalke show
248                  * no noticable performance loss on larger mailboxes
249                  * if we incur an extra copy, and messing around with
250                  * internal stdio buffers is becoming more and more
251                  * unportable as times go on.  So from now on just deal
252                  * with the overhead of an extra copy.
253                  *
254                  * Subtle change - with the previous code tmpbuf wasn't
255                  * used, so we could reuse it for the {body} component.
256                  * Now since we're using tmpbuf as our read buffer we
257                  * need to save the beginning of the body for later.
258                  * See the above (and below) use of startbody.
259                  */
260 body:;
261                 if (bodycomp && startbody == NULL) {
262                     startbody = tmpbuf;
263                     tmpbuf = *nxtbuf++;
264                 }
265
266                 while (state == BODY) {
267                     state = m_getfld(state, name, tmpbuf, rlwidth, inb);
268                     FPUTS(tmpbuf);
269                 }
270                 goto finished;
271
272             case LENERR: 
273             case FMTERR: 
274                 fprintf (stderr, 
275                         innum ? "??Format error (message %d) in "
276                               : "??Format error in ",
277                         outnum ? outnum : innum);
278                 fprintf (stderr, "component %d\n", compnum);
279
280                 if (outnum) {
281                     FPUTS ("\n\nBAD MSG:\n");
282                     FPUTS (name);
283                     if (putc ('\n', scnout) == EOF) DIEWRERR();
284                     state = BODY;
285                     goto body;
286                 }
287                 /* fall through */
288
289             case FILEEOF:
290                 goto finished;
291
292             default: 
293                 adios (NULL, "getfld() returned %d", state);
294         }
295     }
296
297     /*
298      * format and output the scan line.
299      */
300 finished:
301     if (ferror(inb)) {
302         advise("read", "unable to"); /* "read error" */
303         return SCNFAT;
304     }
305
306     /* Save and restore buffer so we don't trash our dynamic pool! */
307     if (bodycomp) {
308         saved_c_text = bodycomp->c_text;
309         bodycomp->c_text = startbody;
310     }
311
312     if (size)
313         dat[2] = size;
314     else if (outnum > 0)
315     {
316         dat[2] = ftell(scnout);
317         if (dat[2] == EOF) DIEWRERR();
318     }
319
320     if ((datecomp && !datecomp->c_text) || (!size && !outnum)) {
321         struct stat st;
322
323         fstat (fileno(inb), &st);
324         if (!size && !outnum)
325             dat[2] = st.st_size;
326         if (datecomp) {
327             if (! datecomp->c_text) {
328                 if (datecomp->c_tws == NULL)
329                     datecomp->c_tws = (struct tws *)
330                         calloc((size_t) 1, sizeof(*datecomp->c_tws));
331                 if (datecomp->c_tws == NULL)
332                     adios (NULL, "unable to allocate tws buffer");
333                 *datecomp->c_tws = *dlocaltime ((time_t *) &st.st_mtime);
334                 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
335             } else {
336                 datecomp->c_flags &= ~CF_DATEFAB;
337             }
338         }
339     }
340
341     fmt_scan (fmt, scanl, scanl_size, slwidth, dat);
342
343     if (bodycomp)
344         bodycomp->c_text = saved_c_text;
345
346     if (noisy)
347         fputs (scanl, stdout);
348
349     cptr = fmt_findcomp ("encrypted");
350     encrypted = cptr && cptr->c_text;
351
352     /* return dynamically allocated buffers to pool */
353     while ((cptr = *savecomp++)) {
354         *--nxtbuf = cptr->c_text;
355         cptr->c_text = NULL;
356     }
357     *--nxtbuf = tmpbuf;
358
359     if (outnum && (ferror(scnout) || fclose (scnout) == EOF))
360         DIEWRERR();
361
362     return (state != FILEEOF ? SCNERR : encrypted ? SCNENC : SCNMSG);
363 }
364
365
366 static int
367 mh_fputs(char *s, FILE *stream)
368 {
369     char c;
370
371     while ((c = *s++)) 
372         if (putc (c,stream) == EOF )
373             return(EOF);
374     return (0);
375 }
376