Merge branch 'm_getfld2-meillo' into master
[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 FLD2:
111                         compnum++;
112                         if (incing) {
113                                 FPUTS(f.name);
114                                 FPUTS(":");
115                                 FPUTS(f.value);
116                         }
117                         if (fmtstr && (cptr = wantcomp[CHASH(f.name)])) {
118                                 /*
119                                 ** we're interested in this component,
120                                 ** but find the right one in the hash
121                                 ** collision chain ...
122                                 */
123                                 do {
124                                         if (mh_strcasecmp(f.name, cptr->c_name)!=0) {
125                                                 continue;
126                                         }
127                                         if (cptr->c_text) {
128                                                 free(cptr->c_text);
129                                                 cptr->c_text = NULL;
130                                         }
131                                         cptr->c_text = mh_xstrdup(f.value);
132                                         cp = cptr->c_text + strlen(cptr->c_text) - 1;
133                                         for (; cp >= cptr->c_text; cp--) {
134                                                 if (isspace(*cp)) {
135                                                         *cp = '\0';
136                                                 } else {
137                                                         break;
138                                                 }
139                                         }
140                                         break;
141                                 } while ((cptr = cptr->c_next));
142                         }
143                         break;
144
145                 case BODY2:
146                         compnum = -1;
147                         if (scanfolder) {
148                                 /* stop here if we scan a msg in a folder */
149                                 state = FILEEOF2;
150                                 goto finished;
151                         }
152                         /* otherwise (mbox): snarf the body */
153                         if (strncmp("From ", f.value, 5)==0) {
154                                 state = FILEEOF2;
155                                 goto finished;
156                         }
157                         if (incing) {
158                                 FPUTS("\n");
159                                 FPUTS(f.value);
160                         }
161 body:;
162                         blankline = 0;
163                         while ((state = m_getfld2(state, &f, inb)) == BODY2) {
164                                 /*
165                                 ** recognize From lines without blank lines
166                                 ** before them as well.
167                                 */
168                                 if (strncmp("From ", f.value, 5)==0) {
169                                         state = FILEEOF2;
170                                         goto finished;
171                                 }
172                                 /*
173                                 ** delay the printing of blank lines
174                                 ** because if it's the end of the message,
175                                 ** then we must omit the blank line,
176                                 ** as it is not part of the message but
177                                 ** part of the mbox format
178                                 */
179                                 if (blankline) {
180                                         /* print the delayed blank line */
181                                         FPUTS("\n");
182                                         blankline = 0;
183                                 }
184                                 if (strcmp(f.value, "\n")==0) {
185                                         blankline = 1;
186                                         continue;
187                                 }
188                                 if (incing) {
189                                         FPUTS(f.value);
190                                 }
191                         }
192                         goto finished;
193
194                 case LENERR2:
195                         advise(NULL, "line \"%s\" too long", trim(f.value));
196                         goto handleerror;
197
198                 case FMTERR2:
199                         if (strncmp("From ", f.value, 5)==0) {
200                                 state = FILEEOF2;
201                                 goto finished;
202                         }
203                         /* FALL */
204
205                 case IOERR2:
206 handleerror:;
207                         fprintf(stderr, innum ?
208                                         "??Format error (message %d) in " :
209                                         "??Format error in ",
210                                         outnum ? outnum : innum);
211                         fprintf(stderr, "component %d\n", compnum);
212
213                         if (incing) {
214                                 FPUTS("\n\nBAD MSG:\n");
215                                 FPUTS(f.name);  /* XXX use f.field? */
216                                 FPUTS("\n");
217                                 state = BODY2;
218                                 goto body;
219                         }
220                         /* fall through if we scan only */
221
222                 case FILEEOF2:
223                         goto finished;
224
225                 default:
226                         adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
227                 }
228         }
229
230 finished:
231         /* Format and output the scan line. */
232         if (ferror(inb)) {
233                 advise("read", "unable to");
234                 return SCNFAT;
235         }
236
237         if (incing) {
238                 if ((dat[2] = ftell(scnout)) == EOF)
239                         adios(EX_IOERR, scnmsg, "write error on");
240         } else if (!scanfolder) {
241                 if ((dat[2] = ftell(inb)) == EOF)
242                         adios(EX_IOERR, scnmsg, "write error on");
243                 dat[2] -= fpos;
244         }
245
246         if (fmtstr) {
247                 fstat(fileno(inb), &st);
248                 if (scanfolder) {
249                         dat[2] = st.st_size;
250                 }
251                 if (datecomp && !datecomp->c_text) {
252                         if (!datecomp->c_text) {
253                                 if (!datecomp->c_tws)
254                                         datecomp->c_tws = mh_xcalloc(1, sizeof(*datecomp->c_tws));
255                                 if (!datecomp->c_tws)
256                                         adios(EX_OSERR, NULL, "unable to allocate tws buffer");
257                                 *datecomp->c_tws = *dlocaltime((time_t *) &st.st_mtime);
258                                 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
259                         } else {
260                                 datecomp->c_flags &= ~CF_DATEFAB;
261                         }
262                 }
263
264                 /* print the scan line */
265                 fmt_scan(fmt, scanl, slwidth, dat);
266                 fputs(scanl, stdout);
267         }
268
269         /* clean up old values */
270         for (i=0; i < sizeof(wantcomp)/sizeof(wantcomp[0]); i++) {
271                 for (cptr=wantcomp[i]; cptr; cptr=cptr->c_next) {
272                         if (cptr->c_text) {
273                                 free(cptr->c_text);
274                                 cptr->c_text = NULL;
275                         }
276                 }
277         }
278
279         if (incing && (ferror(scnout) || fclose(scnout) == EOF))
280                 adios(EX_IOERR, scnmsg, "write error on");
281
282         return (state == FILEEOF2 ? SCNMSG : SCNERR);
283 }