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>
20 # define _ptr _p /* Gag */
21 # define _cnt _w /* Wretch */
24 #define MAXSCANL 256 /* longest possible scan line */
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
33 static struct format *fmt;
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 */
40 static int dat[5]; /* aux. data for format routine */
42 char *scanl = NULL; /* text of most recent scanline */
45 if (fputs(buf, scnout) == EOF)\
46 adios(EX_IOERR, scnmsg, "write error on");\
52 int sc_width(void); /* from termsbr.c */
54 #ifdef MULTIBYTE_SUPPORT
55 #define SCAN_CHARWIDTH MB_CUR_MAX
57 #define SCAN_CHARWIDTH 1
61 scan(FILE *inb, int innum, int outnum, char *fmtstr, int width, int curflg,
65 int i, compnum, state;
66 unsigned char *cp, *tmpbuf;
69 struct comp **savecomp;
73 int incing = (outnum != SCN_MBOX && outnum != SCN_FOLD);
74 int scanfolder = (outnum == SCN_FOLD);
78 /* first-time only initialization */
85 if ((width = sc_width()) < WIDTH/2)
87 else if (width > MAXSCANL)
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");
100 nxtbuf = compbuffers = mh_xcalloc(ncomps, sizeof(char *));
101 used_buf = mh_xcalloc(ncomps+1, sizeof(struct comp *));
102 /* NULL-terminate array */
105 /* allocate space for the items */
106 for (i = ncomps; i--; )
107 *nxtbuf++ = mh_xcalloc(SBUFSIZ, sizeof(char));
111 ** each-message initialization
113 nxtbuf = compbuffers;
116 dat[0] = innum ? innum : outnum;
122 ** Get the first field. If the message is non-empty
123 ** and we're doing an "inc", open the output file.
125 if ((state = m_getfld(FLD, name, tmpbuf, SBUFSIZ, inb)) == FILEEOF) {
127 advise("read", "unable to"); /* "read error" */
135 scnmsg = m_name(outnum);
136 if (*scnmsg == '?') /* msg num out of range */
138 if (!(scnout = fopen(scnmsg, "w")))
139 adios(EX_IOERR, scnmsg, "unable to write");
142 /* scan - main loop */
144 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb)) {
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).
161 if (fmtstr && (cptr = wantcomp[CHASH(name)])) {
163 if (mh_strcasecmp(name, cptr->c_name)!=0) {
167 cptr->c_text = tmpbuf;
168 cp = tmpbuf+strlen(tmpbuf)-1;
169 for (; cp >= tmpbuf; cp--) {
179 } while ((cptr = cptr->c_next));
182 while (state == FLDPLUS) {
183 state = m_getfld(state, name, tmpbuf, SBUFSIZ,
193 /* stop here if we scan a msg in a folder */
197 /* otherwise (mbox): snarf the body */
203 while (state == BODY) {
204 state = m_getfld(state, name, tmpbuf, SBUFSIZ,
214 fprintf(stderr, innum ? "??Format error (message %d) in " : "??Format error in ", outnum ? outnum : innum);
215 fprintf(stderr, "component %d\n", compnum);
218 FPUTS("\n\nBAD MSG:\n");
224 /* fall through if we scan only */
230 adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
236 /* Format and output the scan line. */
238 advise("read", "unable to");
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");
252 fstat(fileno(inb), &st);
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;
265 datecomp->c_flags &= ~CF_DATEFAB;
269 /* print the scan line */
270 fmt_scan(fmt, scanl, slwidth, dat);
271 fputs(scanl, stdout);
274 /* return dynamically allocated buffers to pool */
275 while ((cptr = *savecomp++)) {
276 *--nxtbuf = cptr->c_text;
281 if (incing && (ferror(scnout) || fclose(scnout) == EOF))
282 adios(EX_IOERR, scnmsg, "write error on");
284 return (state != FILEEOF ? SCNERR : SCNMSG);