Fix missing va_end call in uip/mhmisc.c
[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 #include <ctype.h>
16 #include <sys/stat.h>
17 #include <sysexits.h>
18
19 #define MAXSCANL 256  /* longest possible scan line */
20
21 static struct format *fmt;
22
23 static struct comp *datecomp;  /* pntr to "date" comp */
24 static int ncomps = 0;  /* # of interesting components */
25
26 static int dat[5];  /* aux. data for format routine */
27
28 char *scanl = NULL;  /* text of most recent scanline */
29
30 #define FPUTS(buf) {\
31                 if (fputs(buf, scnout) == EOF)\
32                         adios(EX_IOERR, scnmsg, "write error on");\
33         }
34
35 /*
36 ** prototypes
37 */
38 int sc_width(void);  /* from termsbr.c */
39
40 #ifdef MULTIBYTE_SUPPORT
41 #define SCAN_CHARWIDTH MB_CUR_MAX
42 #else
43 #define SCAN_CHARWIDTH 1
44 #endif
45
46 int
47 scan(FILE *inb, int innum, int outnum, char *fmtstr, int width, int curflg,
48         int unseen)
49 {
50         static int slwidth;
51         int compnum, i;
52         enum state state;
53         struct field f = {{0}};
54         char *cp;
55         struct comp *cptr;
56         char *scnmsg = NULL;
57         FILE *scnout = NULL;
58         int incing = (outnum != SCN_MBOX && outnum != SCN_FOLD);
59         int scanfolder = (outnum == SCN_FOLD);
60         long fpos;
61         struct stat st;
62         int blankline;
63
64         /* first-time only initialization */
65         if (!scanl) {
66                 if (incing)
67                         umask(~m_gmprot());
68
69                 if (fmtstr) {
70                         if (width == 0) {
71                                 if ((width = sc_width()) < WIDTH/2)
72                                         width = WIDTH/2;
73                                 else if (width > MAXSCANL)
74                                         width = MAXSCANL;
75                         }
76                         dat[3] = slwidth = width;
77                         scanl = mh_xcalloc(slwidth + 2, SCAN_CHARWIDTH);  /* probably for \n and \0 */
78                         /* Compile format string */
79                         ncomps = fmt_compile(fmtstr, &fmt) + 1;
80                         FINDCOMP(datecomp, "date");
81                 } else {
82                         ncomps = 1;
83                         datecomp = NULL;
84                 }
85         }
86
87         if (feof(inb)) {
88                 return SCNEOF;
89         }
90
91         /*
92         ** each-message initialization
93         */
94         dat[0] = innum ? innum : outnum;
95         dat[1] = curflg;
96         dat[4] = unseen;
97         fpos = ftell(inb);
98
99         if (incing) {
100                 scnmsg = m_name(outnum);
101                 if (*scnmsg == '?')  /* msg num out of range */
102                         return SCNNUM;
103                 if (!(scnout = fopen(scnmsg, "w")))
104                         adios(EX_IOERR, scnmsg, "unable to write");
105         }
106         /* scan - main loop */
107         for (compnum = 1, state = FLD2; ; ) {
108                 state = m_getfld2(state, &f, inb);
109                 switch (state) {
110                 case LENERR2:
111                         state = FLD2;
112                         /* FALL */
113                 case FLD2:
114                         compnum++;
115                         if (incing) {
116                                 FPUTS(f.name);
117                                 FPUTS(":");
118                                 FPUTS(f.value);
119                         }
120                         if (fmtstr && (cptr = wantcomp[CHASH(f.name)])) {
121                                 /*
122                                 ** we're interested in this component,
123                                 ** but find the right one in the hash
124                                 ** collision chain ...
125                                 */
126                                 do {
127                                         if (mh_strcasecmp(f.name, cptr->c_name)!=0) {
128                                                 continue;
129                                         }
130                                         if (cptr->c_text) {
131                                                 free(cptr->c_text);
132                                                 cptr->c_text = NULL;
133                                         }
134                                         cptr->c_text = mh_xstrdup(f.value);
135                                         cp = cptr->c_text + strlen(cptr->c_text) - 1;
136                                         for (; cp >= cptr->c_text; cp--) {
137                                                 if (isspace(*cp)) {
138                                                         *cp = '\0';
139                                                 } else {
140                                                         break;
141                                                 }
142                                         }
143                                         break;
144                                 } while ((cptr = cptr->c_next));
145                         }
146                         break;
147
148                 case BODY2:
149                         compnum = -1;
150                         if (scanfolder) {
151                                 /* stop here if we scan a msg in a folder */
152                                 state = FILEEOF2;
153                                 goto finished;
154                         }
155                         /* otherwise (mbox): snarf the body */
156                         if (strncmp("From ", f.value, 5)==0) {
157                                 state = FILEEOF2;
158                                 goto finished;
159                         }
160                         if (incing) {
161                                 FPUTS("\n");
162                                 FPUTS(f.value);
163                         }
164 body:;
165                         blankline = 0;
166                         while ((state = m_getfld2(state, &f, inb)) == BODY2) {
167                                 /*
168                                 ** recognize From lines without blank lines
169                                 ** before them as well.
170                                 */
171                                 if (strncmp("From ", f.value, 5)==0) {
172                                         state = FILEEOF2;
173                                         goto finished;
174                                 }
175                                 /*
176                                 ** delay the printing of blank lines
177                                 ** because if it's the end of the message,
178                                 ** then we must omit the blank line,
179                                 ** as it is not part of the message but
180                                 ** part of the mbox format
181                                 */
182                                 if (blankline) {
183                                         /* print the delayed blank line */
184                                         FPUTS("\n");
185                                         blankline = 0;
186                                 }
187                                 if (strcmp(f.value, "\n")==0) {
188                                         blankline = 1;
189                                         continue;
190                                 }
191                                 if (incing) {
192                                         FPUTS(f.value);
193                                 }
194                         }
195                         goto finished;
196
197                 case FMTERR2:
198                         if (strncmp("From ", f.value, 5)==0) {
199                                 state = FILEEOF2;
200                                 goto finished;
201                         }
202                         state = FLD2;
203                         fprintf(stderr, innum ?
204                                         "??Format error (message %d) in " :
205                                         "??Format error in ",
206                                         outnum ? outnum : innum);
207                         fprintf(stderr, "component %d\n", compnum);
208                         continue;
209
210                 case IOERR2:
211                         fprintf(stderr, innum ?
212                                         "??Format error (message %d) in " :
213                                         "??Format error in ",
214                                         outnum ? outnum : innum);
215                         fprintf(stderr, "component %d\n", compnum);
216
217                         if (incing) {
218                                 FPUTS("\n\nBAD MSG:\n");
219                                 FPUTS(f.name);  /* XXX use f.field? */
220                                 FPUTS("\n");
221                                 state = BODY2;
222                                 goto body;
223                         }
224                         /* fall through if we scan only */
225
226                 case FILEEOF2:
227                         goto finished;
228
229                 default:
230                         adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
231                 }
232         }
233
234 finished:
235         /* Format and output the scan line. */
236         if (ferror(inb)) {
237                 advise("read", "unable to");
238                 return SCNFAT;
239         }
240
241         if (incing) {
242                 if ((dat[2] = ftell(scnout)) == EOF)
243                         adios(EX_IOERR, scnmsg, "write error on");
244         } else if (!scanfolder) {
245                 if ((dat[2] = ftell(inb)) == EOF)
246                         adios(EX_IOERR, scnmsg, "write error on");
247                 dat[2] -= fpos;
248         }
249
250         if (fmtstr) {
251                 fstat(fileno(inb), &st);
252                 if (scanfolder) {
253                         dat[2] = st.st_size;
254                 }
255                 if (datecomp && !datecomp->c_text) {
256                         if (!datecomp->c_text) {
257                                 if (!datecomp->c_tws)
258                                         datecomp->c_tws = mh_xcalloc(1, sizeof(*datecomp->c_tws));
259                                 if (!datecomp->c_tws)
260                                         adios(EX_OSERR, NULL, "unable to allocate tws buffer");
261                                 *datecomp->c_tws = *dlocaltime((time_t *) &st.st_mtime);
262                                 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
263                         } else {
264                                 datecomp->c_flags &= ~CF_DATEFAB;
265                         }
266                 }
267
268                 /* print the scan line */
269                 fmt_scan(fmt, scanl, slwidth, dat);
270                 fputs(scanl, stdout);
271         }
272
273         /* clean up old values */
274         for (i=0; i < sizeof(wantcomp)/sizeof(wantcomp[0]); i++) {
275                 for (cptr=wantcomp[i]; cptr; cptr=cptr->c_next) {
276                         if (cptr->c_text) {
277                                 free(cptr->c_text);
278                                 cptr->c_text = NULL;
279                         }
280                 }
281         }
282
283         if (incing && (ferror(scnout) || fclose(scnout) == EOF))
284                 adios(EX_IOERR, scnmsg, "write error on");
285
286         return (state == FILEEOF2 ? SCNMSG : SCNERR);
287 }