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