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