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