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