No error-checking for error-checking alloc functions
[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 **) mh_xcalloc((size_t) ncomps,
102                                 sizeof(char *));
103                 used_buf = (struct comp **) mh_xcalloc((size_t) (ncomps+1),
104                                 sizeof(struct comp *));
105                 /* NULL-terminate array */
106                 used_buf += ncomps;
107                 *used_buf = NULL;
108                 /* allocate space for the items */
109                 for (i = ncomps; i--; )
110                         *nxtbuf++ = mh_xmalloc(SBUFSIZ);
111         }
112
113         /*
114         ** each-message initialization
115         */
116         nxtbuf = compbuffers;
117         savecomp = used_buf;
118         tmpbuf = *nxtbuf++;
119         dat[0] = innum ? innum : outnum;
120         dat[1] = curflg;
121         dat[4] = unseen;
122         fpos = ftell(inb);
123
124         /*
125         ** Get the first field.  If the message is non-empty
126         ** and we're doing an "inc", open the output file.
127         */
128         if ((state = m_getfld(FLD, name, tmpbuf, SBUFSIZ, inb)) == FILEEOF) {
129                 if (ferror(inb)) {
130                         advise("read", "unable to"); /* "read error" */
131                         return SCNFAT;
132                 } else {
133                         return SCNEOF;
134                 }
135         }
136
137         if (incing) {
138                 scnmsg = m_name(outnum);
139                 if (*scnmsg == '?')  /* msg num out of range */
140                         return SCNNUM;
141                 if (!(scnout = fopen(scnmsg, "w")))
142                         adios(EX_IOERR, scnmsg, "unable to write");
143         }
144
145         /* scan - main loop */
146         for (compnum = 1; ;
147                         state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb)) {
148                 switch (state) {
149                 case FLD:
150                 case FLDPLUS:
151                         compnum++;
152                         if (incing) {
153                                 FPUTS(name);
154                                 FPUTS(":");
155                                 FPUTS(tmpbuf);
156                         }
157                         /*
158                         ** if we're interested in this component, save
159                         ** a pointer to the component text, then start
160                         ** using our next free buffer as the component
161                         ** temp buffer (buffer switching saves an extra
162                         ** copy of the component text).
163                         */
164                         if (fmtstr && (cptr = wantcomp[CHASH(name)])) {
165                                 do {
166                                         if (mh_strcasecmp(name, cptr->c_name)!=0) {
167                                                 continue;
168                                         }
169                                         if (!cptr->c_text) {
170                                                 cptr->c_text = tmpbuf;
171                                                 cp = tmpbuf+strlen(tmpbuf)-1;
172                                                 for (; cp >= tmpbuf; cp--) {
173                                                         if (isspace(*cp))
174                                                                 *cp = '\0';
175                                                         else
176                                                                 break;
177                                                 }
178                                                 *--savecomp = cptr;
179                                                 tmpbuf = *nxtbuf++;
180                                         }
181                                         break;
182                                 } while ((cptr = cptr->c_next));
183                         }
184
185                         while (state == FLDPLUS) {
186                                 state = m_getfld(state, name, tmpbuf, SBUFSIZ,
187                                                 inb);
188                                 if (incing)
189                                         FPUTS(tmpbuf);
190                         }
191                         break;
192
193                 case BODY:
194                         compnum = -1;
195                         if (scanfolder) {
196                                 /* stop here if we scan a msg in a folder */
197                                 state = FILEEOF;
198                                 goto finished;
199                         }
200                         /* otherwise (mbox): snarf the body */
201                         if (incing) {
202                                 FPUTS("\n");
203                                 FPUTS(tmpbuf);
204                         }
205 body:;
206                         while (state == BODY) {
207                                 state = m_getfld(state, name, tmpbuf, SBUFSIZ,
208                                                 inb);
209                                 if (incing) {
210                                         FPUTS(tmpbuf);
211                                 }
212                         }
213                         goto finished;
214
215                 case LENERR:
216                 case FMTERR:
217                         fprintf(stderr, innum ? "??Format error (message %d) in " : "??Format error in ", outnum ? outnum : innum);
218                         fprintf(stderr, "component %d\n", compnum);
219
220                         if (incing) {
221                                 FPUTS("\n\nBAD MSG:\n");
222                                 FPUTS(name);
223                                 FPUTS("\n");
224                                 state = BODY;
225                                 goto body;
226                         }
227                         /* fall through if we scan only */
228
229                 case FILEEOF:
230                         goto finished;
231
232                 default:
233                         adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
234                 }
235         }
236
237 finished:
238
239         /* Format and output the scan line. */
240         if (ferror(inb)) {
241                 advise("read", "unable to");
242                 return SCNFAT;
243         }
244
245         if (incing) {
246                 if ((dat[2] = ftell(scnout)) == EOF)
247                         adios(EX_IOERR, scnmsg, "write error on");
248         } else if (!scanfolder) {
249                 if ((dat[2] = ftell(inb)) == EOF)
250                         adios(EX_IOERR, scnmsg, "write error on");
251                 dat[2] -= fpos;
252         }
253
254         if (fmtstr) {
255                 fstat(fileno(inb), &st);
256                 if (scanfolder) {
257                         dat[2] = st.st_size;
258                 }
259                 if (datecomp && !datecomp->c_text) {
260                         if (!datecomp->c_text) {
261                                 if (!datecomp->c_tws)
262                                         datecomp->c_tws = (struct tws *) mh_xcalloc((size_t) 1, sizeof(*datecomp->c_tws));
263                                 if (!datecomp->c_tws)
264                                         adios(EX_OSERR, NULL, "unable to allocate tws buffer");
265                                 *datecomp->c_tws = *dlocaltime((time_t *) &st.st_mtime);
266                                 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
267                         } else {
268                                 datecomp->c_flags &= ~CF_DATEFAB;
269                         }
270                 }
271
272                 /* print the scan line */
273                 fmt_scan(fmt, scanl, slwidth, dat);
274                 fputs(scanl, stdout);
275         }
276
277         /* return dynamically allocated buffers to pool */
278         while ((cptr = *savecomp++)) {
279                 *--nxtbuf = cptr->c_text;
280                 cptr->c_text = NULL;
281         }
282         *--nxtbuf = tmpbuf;
283
284         if (incing && (ferror(scnout) || fclose(scnout) == EOF))
285                 adios(EX_IOERR, scnmsg, "write error on");
286
287         return (state != FILEEOF ? SCNERR : SCNMSG);
288 }