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