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