3d66bcf5bc9c4bb84262decaf47d72dc403bc71e
[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                 /*
211                  * A slight hack ... if we have less than rlwidth characters
212                  * in the buffer, call m_getfld again.
213                  */
214
215                 if ((i = strlen(tmpbuf)) < rlwidth) {
216                     state = m_getfld (state, name, tmpbuf + i,
217                                       rlwidth - i, inb);
218                 }
219                 if (! outnum) {
220                     state = FILEEOF; /* stop now if scan cmd */
221                     goto finished;
222                 }
223                 if (putc ('\n', scnout) == EOF) DIEWRERR();
224                 FPUTS (tmpbuf);
225                 /*
226                  * performance hack: some people like to run "inc" on
227                  * things like net.sources or large digests.  We do a
228                  * copy directly into the output buffer rather than
229                  * going through an intermediate buffer.
230                  *
231                  * We need the amount of data m_getfld found & don't
232                  * want to do a strlen on the long buffer so there's
233                  * a hack in m_getfld to save the amount of data it
234                  * returned in the global "msg_count".
235                  */
236 body:;
237                 while (state == BODY) {
238 #ifdef LINUX_STDIO
239                     if (scnout->_IO_write_ptr == scnout->_IO_write_end) {
240 #elif defined(__DragonFly__)
241                     if (((struct __FILE_public *)scnout)->_w <= 0) {
242 #else
243                     if (scnout->_cnt <= 0) {
244 #endif
245                         if (fflush(scnout) == EOF)
246                             DIEWRERR ();
247                     }
248 #ifdef LINUX_STDIO
249                     state = m_getfld(state, name, scnout->_IO_write_ptr,
250                         (long)scnout->_IO_write_ptr-(long)scnout->_IO_write_end , inb);
251                     scnout->_IO_write_ptr += msg_count;
252 #elif defined(__DragonFly__)
253                     state = m_getfld( state, name, ((struct __FILE_public *)scnout)->_p, -(((struct __FILE_public *)scnout)->_w), inb );
254                     ((struct __FILE_public *)scnout)->_w -= msg_count;
255                     ((struct __FILE_public *)scnout)->_p += msg_count;
256 #else
257                     state = m_getfld( state, name, scnout->_ptr, -(scnout->_cnt), inb );
258                     scnout->_cnt -= msg_count;
259                     scnout->_ptr += msg_count;
260 #endif
261                 }
262                 goto finished;
263
264             case LENERR: 
265             case FMTERR: 
266                 fprintf (stderr, 
267                         innum ? "??Format error (message %d) in "
268                               : "??Format error in ",
269                         outnum ? outnum : innum);
270                 fprintf (stderr, "component %d\n", compnum);
271
272                 if (outnum) {
273                     FPUTS ("\n\nBAD MSG:\n");
274                     FPUTS (name);
275                     if (putc ('\n', scnout) == EOF) DIEWRERR();
276                     state = BODY;
277                     goto body;
278                 }
279                 /* fall through */
280
281             case FILEEOF:
282                 goto finished;
283
284             default: 
285                 adios (NULL, "getfld() returned %d", state);
286         }
287     }
288
289     /*
290      * format and output the scan line.
291      */
292 finished:
293     if (ferror(inb)) {
294         advise("read", "unable to"); /* "read error" */
295         return SCNFAT;
296     }
297
298     /* Save and restore buffer so we don't trash our dynamic pool! */
299     if (bodycomp) {
300         saved_c_text = bodycomp->c_text;
301         bodycomp->c_text = tmpbuf;
302     }
303
304     if (size)
305         dat[2] = size;
306     else if (outnum > 0)
307     {
308         dat[2] = ftell(scnout);
309         if (dat[2] == EOF) DIEWRERR();
310     }
311
312     if ((datecomp && !datecomp->c_text) || (!size && !outnum)) {
313         struct stat st;
314
315         fstat (fileno(inb), &st);
316         if (!size && !outnum)
317             dat[2] = st.st_size;
318         if (datecomp) {
319             if (! datecomp->c_text) {
320                 if (datecomp->c_tws == NULL)
321                     datecomp->c_tws = (struct tws *)
322                         calloc((size_t) 1, sizeof(*datecomp->c_tws));
323                 if (datecomp->c_tws == NULL)
324                     adios (NULL, "unable to allocate tws buffer");
325                 *datecomp->c_tws = *dlocaltime ((time_t *) &st.st_mtime);
326                 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
327             } else {
328                 datecomp->c_flags &= ~CF_DATEFAB;
329             }
330         }
331     }
332
333     fmt_scan (fmt, scanl, slwidth, dat);
334
335 #if 0
336     fmt = fmt_scan (fmt, scanl, slwidth, dat);
337     if (!fmt)
338         fmt = fmt_top;          /* reset for old format files */
339 #endif
340
341     if (bodycomp)
342         bodycomp->c_text = saved_c_text;
343
344     if (noisy)
345         fputs (scanl, stdout);
346
347     FINDCOMP (cptr, "encrypted");
348     encrypted = cptr && cptr->c_text;
349
350     /* return dynamically allocated buffers to pool */
351     while ((cptr = *savecomp++)) {
352         *--nxtbuf = cptr->c_text;
353         cptr->c_text = NULL;
354     }
355     *--nxtbuf = tmpbuf;
356
357     if (outnum && (ferror(scnout) || fclose (scnout) == EOF))
358         DIEWRERR();
359
360     return (state != FILEEOF ? SCNERR : encrypted ? SCNENC : SCNMSG);
361 }
362
363
364 static int
365 mh_fputs(char *s, FILE *stream)
366 {
367     char c;
368
369     while ((c = *s++)) 
370         if (putc (c,stream) == EOF )
371             return(EOF);
372     return (0);
373 }
374