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