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