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 = (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");
101 nxtbuf = compbuffers = (char **) calloc((size_t) ncomps,
104 adios(EX_OSERR, NULL, "unable to allocate component buffers");
105 used_buf = (struct comp **) calloc((size_t) (ncomps+1),
106 sizeof(struct comp *));
108 adios(EX_OSERR, NULL, "unable to allocate component buffer stack");
109 /* NULL-terminate array */
112 /* allocate space for the items */
113 for (i = ncomps; i--; )
114 *nxtbuf++ = mh_xmalloc(SBUFSIZ);
118 ** each-message initialization
120 nxtbuf = compbuffers;
123 dat[0] = innum ? innum : outnum;
129 ** Get the first field. If the message is non-empty
130 ** and we're doing an "inc", open the output file.
132 if ((state = m_getfld(FLD, name, tmpbuf, SBUFSIZ, inb)) == FILEEOF) {
134 advise("read", "unable to"); /* "read error" */
142 scnmsg = m_name(outnum);
143 if (*scnmsg == '?') /* msg num out of range */
145 if (!(scnout = fopen(scnmsg, "w")))
146 adios(EX_IOERR, scnmsg, "unable to write");
149 /* scan - main loop */
151 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb)) {
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).
168 if (fmtstr && (cptr = wantcomp[CHASH(name)])) {
170 if (mh_strcasecmp(name, cptr->c_name)!=0) {
174 cptr->c_text = tmpbuf;
175 cp = tmpbuf+strlen(tmpbuf)-1;
176 for (; cp >= tmpbuf; cp--) {
186 } while ((cptr = cptr->c_next));
189 while (state == FLDPLUS) {
190 state = m_getfld(state, name, tmpbuf, SBUFSIZ,
200 /* stop here if we scan a msg in a folder */
204 /* otherwise (mbox): snarf the body */
210 while (state == BODY) {
211 state = m_getfld(state, name, tmpbuf, SBUFSIZ,
221 fprintf(stderr, innum ? "??Format error (message %d) in " : "??Format error in ", outnum ? outnum : innum);
222 fprintf(stderr, "component %d\n", compnum);
225 FPUTS("\n\nBAD MSG:\n");
231 /* fall through if we scan only */
237 adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
243 /* Format and output the scan line. */
245 advise("read", "unable to");
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");
259 fstat(fileno(inb), &st);
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;
272 datecomp->c_flags &= ~CF_DATEFAB;
276 /* print the scan line */
277 fmt_scan(fmt, scanl, slwidth, dat);
278 fputs(scanl, stdout);
281 /* return dynamically allocated buffers to pool */
282 while ((cptr = *savecomp++)) {
283 *--nxtbuf = cptr->c_text;
288 if (incing && (ferror(scnout) || fclose(scnout) == EOF))
289 adios(EX_IOERR, scnmsg, "write error on");
291 return (state != FILEEOF ? SCNERR : SCNMSG);