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