2 ** scansbr.c -- routines to help scan along...
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.
10 #include <h/addrsbr.h>
11 #include <h/fmt_scan.h>
12 #include <h/scansbr.h>
19 # define _ptr _p /* Gag */
20 # define _cnt _w /* Wretch */
23 #define MAXSCANL 256 /* longest possible scan line */
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
32 static struct format *fmt;
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 */
39 static int dat[5]; /* aux. data for format routine */
41 char *scanl = NULL; /* text of most recent scanline */
44 if (fputs(buf, scnout) == EOF)\
45 adios(scnmsg, "write error on");\
51 int sc_width(void); /* from termsbr.c */
53 #ifdef MULTIBYTE_SUPPORT
54 #define SCAN_CHARWIDTH MB_CUR_MAX
56 #define SCAN_CHARWIDTH 1
60 scan(FILE *inb, int innum, int outnum, char *fmtstr, int width, int curflg,
64 int i, compnum, state;
65 unsigned char *cp, *tmpbuf;
68 struct comp **savecomp;
72 int incing = (outnum != SCN_MBOX && outnum != SCN_FOLD);
73 int scanfolder = (outnum == SCN_FOLD);
77 /* first-time only initialization */
84 if ((width = sc_width()) < WIDTH/2)
86 else if (width > MAXSCANL)
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");
100 nxtbuf = compbuffers = (char **) calloc((size_t) ncomps,
103 adios(NULL, "unable to allocate component buffers");
104 used_buf = (struct comp **) calloc((size_t) (ncomps+1),
105 sizeof(struct comp *));
107 adios(NULL, "unable to allocate component buffer stack");
108 /* NULL-terminate array */
111 /* allocate space for the items */
112 for (i = ncomps; i--; )
113 *nxtbuf++ = mh_xmalloc(SBUFSIZ);
117 ** each-message initialization
119 nxtbuf = compbuffers;
122 dat[0] = innum ? innum : outnum;
128 ** Get the first field. If the message is non-empty
129 ** and we're doing an "inc", open the output file.
131 if ((state = m_getfld(FLD, name, tmpbuf, SBUFSIZ, inb)) == FILEEOF) {
133 advise("read", "unable to"); /* "read error" */
141 scnmsg = m_name(outnum);
142 if (*scnmsg == '?') /* msg num out of range */
144 if (!(scnout = fopen(scnmsg, "w")))
145 adios(scnmsg, "unable to write");
148 /* scan - main loop */
150 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb)) {
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).
167 if (fmtstr && (cptr = wantcomp[CHASH(name)])) {
169 if (mh_strcasecmp(name, cptr->c_name)!=0) {
173 cptr->c_text = tmpbuf;
174 cp = tmpbuf+strlen(tmpbuf)-1;
175 for (; cp >= tmpbuf; cp--) {
185 } while ((cptr = cptr->c_next));
188 while (state == FLDPLUS) {
189 state = m_getfld(state, name, tmpbuf, SBUFSIZ,
199 /* stop here if we scan a msg in a folder */
203 /* otherwise (mbox): snarf the body */
209 while (state == BODY) {
210 state = m_getfld(state, name, tmpbuf, SBUFSIZ,
220 fprintf(stderr, innum ? "??Format error (message %d) in " : "??Format error in ", outnum ? outnum : innum);
221 fprintf(stderr, "component %d\n", compnum);
224 FPUTS("\n\nBAD MSG:\n");
230 /* fall through if we scan only */
236 adios(NULL, "getfld() returned %d", state);
242 /* Format and output the scan line. */
244 advise("read", "unable to");
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");
258 fstat(fileno(inb), &st);
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;
271 datecomp->c_flags &= ~CF_DATEFAB;
275 /* print the scan line */
276 fmt_scan(fmt, scanl, slwidth, dat);
277 fputs(scanl, stdout);
280 /* return dynamically allocated buffers to pool */
281 while ((cptr = *savecomp++)) {
282 *--nxtbuf = cptr->c_text;
287 if (incing && (ferror(scnout) || fclose(scnout) == EOF))
288 adios(scnmsg, "write error on");
290 return (state != FILEEOF ? SCNERR : SCNMSG);