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