fdfe4cd75ab86ffbafd1b534aa702b1f3a18b4b5
[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 = NULL;  /* buffers for component text */
45 static struct comp **used_buf = NULL;  /* stack for comp that use buffers */
46
47 static int dat[5];  /* aux. data for format routine */
48
49 char *scanl = NULL;  /* text of most recent scanline */
50
51 #define DIEWRERR() adios(scnmsg, "write error on")
52
53 #define FPUTS(buf) {\
54                 if (fputs(buf,scnout) == EOF)\
55                         DIEWRERR();\
56         }
57
58 /*
59 ** prototypes
60 */
61 int sc_width(void);  /* from termsbr.c */
62
63 #ifdef MULTIBYTE_SUPPORT
64 #define SCAN_CHARWIDTH MB_CUR_MAX
65 #else
66 #define SCAN_CHARWIDTH 1
67 #endif
68
69 int
70 scan(FILE *inb, int innum, int outnum, char *fmtstr, int width, int curflg,
71         int unseen)
72 {
73         int i, compnum, state;
74         unsigned char *cp, *tmpbuf;
75         char **nxtbuf;
76         char *saved_c_text = NULL;
77         struct comp *cptr;
78         struct comp **savecomp;
79         char *scnmsg = NULL;
80         FILE *scnout = NULL;
81         char name[NAMESZ];
82         static int rlwidth, slwidth;
83
84 #ifdef RPATHS
85         char returnpath[BUFSIZ];
86         char deliverydate[BUFSIZ];
87 #endif
88
89         /* first-time only initialization */
90         if (!scanl) {
91                 if (width == 0) {
92                         if ((width = sc_width()) < WIDTH/2)
93                                 width = WIDTH/2;
94                         else if (width > MAXSCANL)
95                                 width = MAXSCANL;
96                 }
97                 dat[3] = slwidth = width;
98                 scanl = (char *) mh_xmalloc((size_t) SCAN_CHARWIDTH *
99                                 (slwidth + 2) );
100                 if (outnum)
101                         umask(~m_gmprot());
102
103                 /* Compile format string */
104                 ncomps = fmt_compile(fmtstr, &fmt) + 1;
105
106                 FINDCOMP(bodycomp, "body");
107                 FINDCOMP(datecomp, "date");
108                 nxtbuf = compbuffers = (char **) calloc((size_t) ncomps,
109                                 sizeof(char *));
110                 if (nxtbuf == NULL)
111                         adios(NULL, "unable to allocate component buffers");
112                 used_buf = (struct comp **) calloc((size_t) (ncomps+1),
113                         sizeof(struct comp *));
114                 if (used_buf == NULL)
115                         adios(NULL, "unable to allocate component buffer stack");
116                 used_buf += ncomps+1; *--used_buf = 0;
117                 rlwidth = bodycomp && (width > SBUFSIZ) ? width : SBUFSIZ;
118                 for (i = ncomps; i--; )
119                         *nxtbuf++ = mh_xmalloc(rlwidth);
120         }
121
122         /*
123         ** each-message initialization
124         */
125         nxtbuf = compbuffers;
126         savecomp = used_buf;
127         tmpbuf = *nxtbuf++;
128         dat[0] = innum ? innum : outnum;
129         dat[1] = curflg;
130         dat[4] = unseen;
131
132         /*
133         ** Get the first field.  If the message is non-empty
134         ** and we're doing an "inc", open the output file.
135         */
136         if ((state = m_getfld(FLD, name, tmpbuf, rlwidth, inb)) == FILEEOF) {
137                 if (ferror(inb)) {
138                         advise("read", "unable to"); /* "read error" */
139                         return SCNFAT;
140                 } else {
141                         return SCNEOF;
142                 }
143         }
144
145         if (outnum) {
146                 if (outnum > 0) {
147                         scnmsg = m_name(outnum);
148                         if (*scnmsg == '?')  /* msg num out of range */
149                                 return SCNNUM;
150                 } else {
151                         scnmsg = "/dev/null";
152                 }
153                 if ((scnout = fopen(scnmsg, "w")) == NULL)
154                         adios(scnmsg, "unable to write");
155 #ifdef RPATHS
156                 /*
157                 ** Add the Return-Path and Delivery-Date
158                 ** header fields to message.
159                 */
160                 if (get_returnpath(returnpath, sizeof(returnpath),
161                                 deliverydate, sizeof(deliverydate))) {
162                         FPUTS("Return-Path: ");
163                         FPUTS(returnpath);
164                         FPUTS("Delivery-Date: ");
165                         FPUTS(deliverydate);
166                 }
167 #endif /* RPATHS */
168         }
169
170         /* scan - main loop */
171         for (compnum = 1; ;
172                         state = m_getfld(state, name, tmpbuf, rlwidth, inb)) {
173                 switch (state) {
174                 case FLD:
175                 case FLDPLUS:
176                         compnum++;
177                         if (outnum) {
178                                 FPUTS(name);
179                                 if (putc(':', scnout) == EOF)
180                                         DIEWRERR();
181                                 FPUTS(tmpbuf);
182                         }
183                         /*
184                         ** if we're interested in this component, save
185                         ** a pointer to the component text, then start
186                         ** using our next free buffer as the component
187                         ** temp buffer (buffer switching saves an extra
188                         ** 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; cp >= tmpbuf; cp--)
196                                                                 if (isspace(*cp))
197                                                                         *cp = 0;
198                                                                 else
199                                                                         break;
200                                                         *--savecomp = cptr;
201                                                         tmpbuf = *nxtbuf++;
202                                                 }
203                                                 break;
204                                         }
205                                 } while ((cptr = cptr->c_next));
206                         }
207
208                         while (state == FLDPLUS) {
209                                 state = m_getfld(state, name, tmpbuf, rlwidth,
210                                                 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"
226                         ** on things like net.sources or large digests.
227                         ** We do a copy directly into the output buffer
228                         ** rather than going through an intermediate buffer.
229                         **
230                         ** We need the amount of data m_getfld found &
231                         ** don't want to do a strlen on the long buffer so
232                         ** there's a hack in m_getfld to save the amount
233                         ** of data it 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, (long)scnout->_IO_write_ptr-(long)scnout->_IO_write_end , inb);
249                                 scnout->_IO_write_ptr += msg_count;
250 #elif defined(__DragonFly__)
251                                 state = m_getfld(state, name, ((struct __FILE_public *)scnout)->_p, -(((struct __FILE_public *)scnout)->_w), inb);
252                                 ((struct __FILE_public *)scnout)->_w -= msg_count;
253                                 ((struct __FILE_public *)scnout)->_p += msg_count;
254 #else
255                                 state = m_getfld( state, name, scnout->_ptr, -(scnout->_cnt), inb);
256                                 scnout->_cnt -= msg_count;
257                                 scnout->_ptr += msg_count;
258 #endif
259                         }
260                         goto finished;
261
262                 case LENERR:
263                 case FMTERR:
264                         fprintf(stderr, innum ? "??Format error (message %d) in " : "??Format error in ", outnum ? outnum : innum);
265                         fprintf(stderr, "component %d\n", compnum);
266
267                         if (outnum) {
268                                 FPUTS("\n\nBAD MSG:\n");
269                                 FPUTS(name);
270                                 if (putc('\n', scnout) == EOF)
271                                         DIEWRERR();
272                                 state = BODY;
273                                 goto body;
274                         }
275                         /* fall through */
276
277                 case FILEEOF:
278                         goto finished;
279
280                 default:
281                         adios(NULL, "getfld() returned %d", state);
282                 }
283         }
284
285         /*
286         ** format and output the scan line.
287         */
288 finished:
289         if (ferror(inb)) {
290                 advise("read", "unable to"); /* "read error" */
291                 return SCNFAT;
292         }
293
294         /* Save and restore buffer so we don't trash our dynamic pool! */
295         if (bodycomp) {
296                 saved_c_text = bodycomp->c_text;
297                 bodycomp->c_text = tmpbuf;
298         }
299
300         if (outnum > 0) {
301                 dat[2] = ftell(scnout);
302                 if (dat[2] == EOF)
303                         DIEWRERR();
304         }
305
306         if ((datecomp && !datecomp->c_text) || !outnum) {
307                 struct stat st;
308
309                 fstat(fileno(inb), &st);
310                 if (!outnum)
311                         dat[2] = st.st_size;
312                 if (datecomp) {
313                         if (! datecomp->c_text) {
314                                 if (datecomp->c_tws == NULL)
315                                         datecomp->c_tws = (struct tws *) calloc((size_t) 1, sizeof(*datecomp->c_tws));
316                                 if (datecomp->c_tws == NULL)
317                                         adios(NULL, "unable to allocate tws buffer");
318                                 *datecomp->c_tws = *dlocaltime((time_t *) &st.st_mtime);
319                                 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
320                         } else {
321                                 datecomp->c_flags &= ~CF_DATEFAB;
322                         }
323                 }
324         }
325
326         fmt_scan(fmt, scanl, slwidth, dat);
327
328         if (bodycomp)
329                 bodycomp->c_text = saved_c_text;
330
331         if (fmtstr)
332                 fputs(scanl, stdout);
333
334         /* return dynamically allocated buffers to pool */
335         while ((cptr = *savecomp++)) {
336                 *--nxtbuf = cptr->c_text;
337                 cptr->c_text = NULL;
338         }
339         *--nxtbuf = tmpbuf;
340
341         if (outnum && (ferror(scnout) || fclose(scnout) == EOF))
342                 DIEWRERR();
343
344         return (state != FILEEOF ? SCNERR : SCNMSG);
345 }