Reformated comments and long lines
[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,
121                                         sizeof(*cptr)))) {
122                                 cptr->c_name = "encrypted";
123                                 cptr->c_next = wantcomp[i = CHASH (cptr->c_name)];
124                                 wantcomp[i] = cptr;
125                                 ncomps++;
126                 }
127                 FINDCOMP (cptr, "dtimenow");
128                 if (cptr)
129                         cptr->c_text = getcpy(dtimenow (0));
130                 nxtbuf = compbuffers = (char **) calloc((size_t) ncomps,
131                                 sizeof(char *));
132                 if (nxtbuf == NULL)
133                         adios (NULL, "unable to allocate component buffers");
134                 used_buf = (struct comp **) calloc((size_t) (ncomps+1),
135                         sizeof(struct comp *));
136                 if (used_buf == NULL)
137                         adios (NULL, "unable to allocate component buffer stack");
138                 used_buf += ncomps+1; *--used_buf = 0;
139                 rlwidth = bodycomp && (width > SBUFSIZ) ? width : SBUFSIZ;
140                 for (i = ncomps; i--; )
141                         *nxtbuf++ = mh_xmalloc(rlwidth);
142         }
143
144         /*
145         ** each-message initialization
146         */
147         nxtbuf = compbuffers;
148         savecomp = used_buf;
149         tmpbuf = *nxtbuf++;
150         dat[0] = innum ? innum : outnum;
151         dat[1] = curflg;
152         dat[4] = unseen;
153
154         /*
155         ** Get the first field.  If the message is non-empty
156         ** and we're doing an "inc", open the output file.
157         */
158         if ((state = m_getfld (FLD, name, tmpbuf, rlwidth, inb)) == FILEEOF) {
159                 if (ferror(inb)) {
160                         advise("read", "unable to"); /* "read error" */
161                         return SCNFAT;
162                 } else {
163                         return SCNEOF;
164                 }
165         }
166
167         if (outnum) {
168                 if (outnum > 0) {
169                         scnmsg = m_name (outnum);
170                         if (*scnmsg == '?')  /* msg num out of range */
171                                 return SCNNUM;
172                 } else {
173                         scnmsg = "/dev/null";
174                 }
175                 if ((scnout = fopen (scnmsg, "w")) == NULL)
176                         adios (scnmsg, "unable to write");
177 #ifdef RPATHS
178                 /*
179                 ** Add the Return-Path and Delivery-Date
180                 ** header fields to message.
181                 */
182                 if (get_returnpath (returnpath, sizeof(returnpath),
183                                 deliverydate, sizeof(deliverydate))) {
184                         FPUTS ("Return-Path: ");
185                         FPUTS (returnpath);
186                         FPUTS ("Delivery-Date: ");
187                         FPUTS (deliverydate);
188                 }
189 #endif /* RPATHS */
190         }
191
192         /* scan - main loop */
193         for (compnum = 1; ;
194                         state = m_getfld (state, name, tmpbuf, rlwidth, inb)) {
195                 switch (state) {
196                         case FLD:
197                         case FLDPLUS:
198                                 compnum++;
199                                 if (outnum) {
200                                         FPUTS (name);
201                                         if ( putc (':', scnout) == EOF)
202                                                 DIEWRERR();
203                                         FPUTS (tmpbuf);
204                                 }
205                                 /*
206                                 ** if we're interested in this component,
207                                 ** save a pointer to the component text,
208                                 ** then start using our next free buffer
209                                 ** as the component temp buffer (buffer
210                                 ** switching saves an extra copy of the
211                                 ** component text).
212                                 */
213                                 if ((cptr = wantcomp[CHASH(name)])) {
214                                         do {
215                                                 if (!mh_strcasecmp(name, cptr->c_name)) {
216                                                         if (! cptr->c_text) {
217                                                                 cptr->c_text = tmpbuf;
218                                                                 for (cp = tmpbuf + strlen (tmpbuf) - 1;
219                                                                                 cp >= tmpbuf; cp--)
220                                                                         if (isspace (*cp))
221                                                                                 *cp = 0;
222                                                                         else
223                                                                                 break;
224                                                                 *--savecomp = cptr;
225                                                                 tmpbuf = *nxtbuf++;
226                                                         }
227                                                         break;
228                                                 }
229                                         } while ((cptr = cptr->c_next));
230                                 }
231
232                                 while (state == FLDPLUS) {
233                                         state = m_getfld (state, name, tmpbuf, 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) DIEWRERR();
297                                         state = BODY;
298                                         goto body;
299                                 }
300                                 /* fall through */
301
302                         case FILEEOF:
303                                 goto finished;
304
305                         default:
306                                 adios (NULL, "getfld() returned %d", state);
307                 }
308         }
309
310         /*
311         ** format and output the scan line.
312         */
313 finished:
314         if (ferror(inb)) {
315                 advise("read", "unable to"); /* "read error" */
316                 return SCNFAT;
317         }
318
319         /* Save and restore buffer so we don't trash our dynamic pool! */
320         if (bodycomp) {
321                 saved_c_text = bodycomp->c_text;
322                 bodycomp->c_text = tmpbuf;
323         }
324
325         if (size)
326                 dat[2] = size;
327         else if (outnum > 0) {
328                 dat[2] = ftell(scnout);
329                 if (dat[2] == EOF) DIEWRERR();
330         }
331
332         if ((datecomp && !datecomp->c_text) || (!size && !outnum)) {
333                 struct stat st;
334
335                 fstat (fileno(inb), &st);
336                 if (!size && !outnum)
337                         dat[2] = st.st_size;
338                 if (datecomp) {
339                         if (! datecomp->c_text) {
340                                 if (datecomp->c_tws == NULL)
341                                         datecomp->c_tws = (struct tws *)
342                                                 calloc((size_t) 1, sizeof(*datecomp->c_tws));
343                                 if (datecomp->c_tws == NULL)
344                                         adios (NULL, "unable to allocate tws buffer");
345                                 *datecomp->c_tws = *dlocaltime ((time_t *) &st.st_mtime);
346                                 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
347                         } else {
348                                 datecomp->c_flags &= ~CF_DATEFAB;
349                         }
350                 }
351         }
352
353         fmt_scan (fmt, scanl, slwidth, dat);
354
355 #if 0
356         fmt = fmt_scan (fmt, scanl, slwidth, dat);
357         if (!fmt)
358                 fmt = fmt_top;  /* reset for old format files */
359 #endif
360
361         if (bodycomp)
362                 bodycomp->c_text = saved_c_text;
363
364         if (noisy)
365                 fputs (scanl, stdout);
366
367         FINDCOMP (cptr, "encrypted");
368         encrypted = cptr && cptr->c_text;
369
370         /* return dynamically allocated buffers to pool */
371         while ((cptr = *savecomp++)) {
372                 *--nxtbuf = cptr->c_text;
373                 cptr->c_text = NULL;
374         }
375         *--nxtbuf = tmpbuf;
376
377         if (outnum && (ferror(scnout) || fclose (scnout) == EOF))
378                 DIEWRERR();
379
380         return (state != FILEEOF ? SCNERR : encrypted ? SCNENC : SCNMSG);
381 }
382
383 static int
384 mh_fputs(char *s, FILE *stream)
385 {
386         char c;
387
388         while ((c = *s++))
389                 if (putc (c,stream) == EOF )
390                         return(EOF);
391         return (0);
392 }