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