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