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