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