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 **) mh_xcalloc((size_t) ncomps,
103 used_buf = (struct comp **) mh_xcalloc((size_t) (ncomps+1),
104 sizeof(struct comp *));
105 /* NULL-terminate array */
108 /* allocate space for the items */
109 for (i = ncomps; i--; )
110 *nxtbuf++ = mh_xmalloc(SBUFSIZ);
114 ** each-message initialization
116 nxtbuf = compbuffers;
119 dat[0] = innum ? innum : outnum;
125 ** Get the first field. If the message is non-empty
126 ** and we're doing an "inc", open the output file.
128 if ((state = m_getfld(FLD, name, tmpbuf, SBUFSIZ, inb)) == FILEEOF) {
130 advise("read", "unable to"); /* "read error" */
138 scnmsg = m_name(outnum);
139 if (*scnmsg == '?') /* msg num out of range */
141 if (!(scnout = fopen(scnmsg, "w")))
142 adios(EX_IOERR, scnmsg, "unable to write");
145 /* scan - main loop */
147 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb)) {
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).
164 if (fmtstr && (cptr = wantcomp[CHASH(name)])) {
166 if (mh_strcasecmp(name, cptr->c_name)!=0) {
170 cptr->c_text = tmpbuf;
171 cp = tmpbuf+strlen(tmpbuf)-1;
172 for (; cp >= tmpbuf; cp--) {
182 } while ((cptr = cptr->c_next));
185 while (state == FLDPLUS) {
186 state = m_getfld(state, name, tmpbuf, SBUFSIZ,
196 /* stop here if we scan a msg in a folder */
200 /* otherwise (mbox): snarf the body */
206 while (state == BODY) {
207 state = m_getfld(state, name, tmpbuf, SBUFSIZ,
217 fprintf(stderr, innum ? "??Format error (message %d) in " : "??Format error in ", outnum ? outnum : innum);
218 fprintf(stderr, "component %d\n", compnum);
221 FPUTS("\n\nBAD MSG:\n");
227 /* fall through if we scan only */
233 adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
239 /* Format and output the scan line. */
241 advise("read", "unable to");
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");
255 fstat(fileno(inb), &st);
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;
268 datecomp->c_flags &= ~CF_DATEFAB;
272 /* print the scan line */
273 fmt_scan(fmt, scanl, slwidth, dat);
274 fputs(scanl, stdout);
277 /* return dynamically allocated buffers to pool */
278 while ((cptr = *savecomp++)) {
279 *--nxtbuf = cptr->c_text;
284 if (incing && (ferror(scnout) || fclose(scnout) == EOF))
285 adios(EX_IOERR, scnmsg, "write error on");
287 return (state != FILEEOF ? SCNERR : SCNMSG);