Removed the space between function names and the opening parenthesis.
[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,
206                                 ** save a pointer to the component text,
207                                 ** then start using our next free buffer
208                                 ** as the component temp buffer (buffer
209                                 ** switching saves an extra copy of the
210                                 ** component text).
211                                 */
212                                 if ((cptr = wantcomp[CHASH(name)])) {
213                                         do {
214                                                 if (!mh_strcasecmp(name, cptr->c_name)) {
215                                                         if (! cptr->c_text) {
216                                                                 cptr->c_text = tmpbuf;
217                                                                 for (cp = tmpbuf + strlen(tmpbuf) - 1;
218                                                                                 cp >= tmpbuf; cp--)
219                                                                         if (isspace(*cp))
220                                                                                 *cp = 0;
221                                                                         else
222                                                                                 break;
223                                                                 *--savecomp = cptr;
224                                                                 tmpbuf = *nxtbuf++;
225                                                         }
226                                                         break;
227                                                 }
228                                         } while ((cptr = cptr->c_next));
229                                 }
230
231                                 while (state == FLDPLUS) {
232                                         state = m_getfld(state, name, tmpbuf,
233                                                         rlwidth, inb);
234                                         if (outnum)
235                                                 FPUTS(tmpbuf);
236                                 }
237                                 break;
238
239                         case BODY:
240                                 compnum = -1;
241                                 if (! outnum) {
242                                         state = FILEEOF; /* stop now if scan cmd */
243                                         goto finished;
244                                 }
245                                 if (putc('\n', scnout) == EOF) DIEWRERR();
246                                 FPUTS(tmpbuf);
247                                 /*
248                                 ** performance hack: some people like to
249                                 ** run "inc" on things like net.sources or
250                                 ** large digests.  We do a copy directly
251                                 ** into the output buffer rather than
252                                 ** going through an intermediate buffer.
253                                 **
254                                 ** We need the amount of data m_getfld
255                                 ** found & don't want to do a strlen on
256                                 ** the long buffer so there's a hack in
257                                 ** m_getfld to save the amount of data it
258                                 ** returned in the global "msg_count".
259                                 */
260 body:;
261                                 while (state == BODY) {
262 #ifdef LINUX_STDIO
263                                         if (scnout->_IO_write_ptr == scnout->_IO_write_end) {
264 #elif defined(__DragonFly__)
265                                         if (((struct __FILE_public *)scnout)->_w <= 0) {
266 #else
267                                         if (scnout->_cnt <= 0) {
268 #endif
269                                                 if (fflush(scnout) == EOF)
270                                                         DIEWRERR();
271                                         }
272 #ifdef LINUX_STDIO
273                                         state = m_getfld(state, name, scnout->_IO_write_ptr,
274                                                 (long)scnout->_IO_write_ptr-(long)scnout->_IO_write_end , inb);
275                                         scnout->_IO_write_ptr += msg_count;
276 #elif defined(__DragonFly__)
277                                         state = m_getfld(state, name, ((struct __FILE_public *)scnout)->_p, -(((struct __FILE_public *)scnout)->_w), inb );
278                                         ((struct __FILE_public *)scnout)->_w -= msg_count;
279                                         ((struct __FILE_public *)scnout)->_p += msg_count;
280 #else
281                                         state = m_getfld( state, name, scnout->_ptr, -(scnout->_cnt), inb );
282                                         scnout->_cnt -= msg_count;
283                                         scnout->_ptr += msg_count;
284 #endif
285                                 }
286                                 goto finished;
287
288                         case LENERR:
289                         case FMTERR:
290                                 fprintf(stderr, innum ? "??Format error (message %d) in " : "??Format error in ", outnum ? outnum : innum);
291                                 fprintf(stderr, "component %d\n", compnum);
292
293                                 if (outnum) {
294                                         FPUTS("\n\nBAD MSG:\n");
295                                         FPUTS(name);
296                                         if (putc('\n', scnout) == EOF)
297                                                 DIEWRERR();
298                                         state = BODY;
299                                         goto body;
300                                 }
301                                 /* fall through */
302
303                         case FILEEOF:
304                                 goto finished;
305
306                         default:
307                                 adios(NULL, "getfld() returned %d", state);
308                 }
309         }
310
311         /*
312         ** format and output the scan line.
313         */
314 finished:
315         if (ferror(inb)) {
316                 advise("read", "unable to"); /* "read error" */
317                 return SCNFAT;
318         }
319
320         /* Save and restore buffer so we don't trash our dynamic pool! */
321         if (bodycomp) {
322                 saved_c_text = bodycomp->c_text;
323                 bodycomp->c_text = tmpbuf;
324         }
325
326         if (size)
327                 dat[2] = size;
328         else if (outnum > 0) {
329                 dat[2] = ftell(scnout);
330                 if (dat[2] == EOF)
331                         DIEWRERR();
332         }
333
334         if ((datecomp && !datecomp->c_text) || (!size && !outnum)) {
335                 struct stat st;
336
337                 fstat(fileno(inb), &st);
338                 if (!size && !outnum)
339                         dat[2] = st.st_size;
340                 if (datecomp) {
341                         if (! datecomp->c_text) {
342                                 if (datecomp->c_tws == NULL)
343                                         datecomp->c_tws = (struct tws *) calloc((size_t) 1, sizeof(*datecomp->c_tws));
344                                 if (datecomp->c_tws == NULL)
345                                         adios(NULL, "unable to allocate tws buffer");
346                                 *datecomp->c_tws = *dlocaltime((time_t *) &st.st_mtime);
347                                 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
348                         } else {
349                                 datecomp->c_flags &= ~CF_DATEFAB;
350                         }
351                 }
352         }
353
354         fmt_scan(fmt, scanl, slwidth, dat);
355
356 #if 0
357         fmt = fmt_scan(fmt, scanl, slwidth, dat);
358         if (!fmt)
359                 fmt = fmt_top;  /* reset for old format files */
360 #endif
361
362         if (bodycomp)
363                 bodycomp->c_text = saved_c_text;
364
365         if (noisy)
366                 fputs(scanl, stdout);
367
368         FINDCOMP(cptr, "encrypted");
369         encrypted = cptr && cptr->c_text;
370
371         /* return dynamically allocated buffers to pool */
372         while ((cptr = *savecomp++)) {
373                 *--nxtbuf = cptr->c_text;
374                 cptr->c_text = NULL;
375         }
376         *--nxtbuf = tmpbuf;
377
378         if (outnum && (ferror(scnout) || fclose(scnout) == EOF))
379                 DIEWRERR();
380
381         return (state != FILEEOF ? SCNERR : encrypted ? SCNENC : SCNMSG);
382 }
383
384 static int
385 mh_fputs(char *s, FILE *stream)
386 {
387         char c;
388
389         while ((c = *s++))
390                 if (putc(c,stream) == EOF )
391                         return(EOF);
392         return (0);
393 }