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>
17 # define _ptr _p /* Gag */
18 # define _cnt _w /* Wretch */
21 #define MAXSCANL 256 /* longest possible scan line */
24 ** Buffer size for content part of header fields. We want this
25 ** to be large enough so that we don't do a lot of extra FLDPLUS
30 static struct format *fmt;
32 static struct comp *datecomp; /* pntr to "date" comp */
33 static int ncomps = 0; /* # of interesting components */
34 static char **compbuffers = NULL; /* buffers for component text */
35 static struct comp **used_buf = NULL; /* stack for comp that use buffers */
37 static int dat[5]; /* aux. data for format routine */
39 char *scanl = NULL; /* text of most recent scanline */
42 if (fputs(buf, scnout) == EOF)\
43 adios(scnmsg, "write error on");\
49 int sc_width(void); /* from termsbr.c */
51 #ifdef MULTIBYTE_SUPPORT
52 #define SCAN_CHARWIDTH MB_CUR_MAX
54 #define SCAN_CHARWIDTH 1
58 scan(FILE *inb, int innum, int outnum, char *fmtstr, int width, int curflg,
62 int i, compnum, state;
63 unsigned char *cp, *tmpbuf;
66 struct comp **savecomp;
70 int incing = (outnum != SCN_MBOX && outnum != SCN_FOLD);
71 int scanfolder = (outnum == SCN_FOLD);
75 /* first-time only initialization */
82 if ((width = sc_width()) < WIDTH/2)
84 else if (width > MAXSCANL)
87 dat[3] = slwidth = width;
88 scanl = (char *) mh_xmalloc((size_t) SCAN_CHARWIDTH *
89 (slwidth + 2)); /* probably for \n and \0 */
90 /* Compile format string */
91 ncomps = fmt_compile(fmtstr, &fmt) + 1;
92 FINDCOMP(datecomp, "date");
98 nxtbuf = compbuffers = (char **) calloc((size_t) ncomps,
101 adios(NULL, "unable to allocate component buffers");
102 used_buf = (struct comp **) calloc((size_t) (ncomps+1),
103 sizeof(struct comp *));
105 adios(NULL, "unable to allocate component buffer stack");
106 /* NULL-terminate array */
109 /* allocate space for the items */
110 for (i = ncomps; i--; )
111 *nxtbuf++ = mh_xmalloc(SBUFSIZ);
115 ** each-message initialization
117 nxtbuf = compbuffers;
120 dat[0] = innum ? innum : outnum;
126 ** Get the first field. If the message is non-empty
127 ** and we're doing an "inc", open the output file.
129 if ((state = m_getfld(FLD, name, tmpbuf, SBUFSIZ, inb)) == FILEEOF) {
131 advise("read", "unable to"); /* "read error" */
139 scnmsg = m_name(outnum);
140 if (*scnmsg == '?') /* msg num out of range */
142 if (!(scnout = fopen(scnmsg, "w")))
143 adios(scnmsg, "unable to write");
146 /* scan - main loop */
148 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb)) {
159 ** if we're interested in this component, save
160 ** a pointer to the component text, then start
161 ** using our next free buffer as the component
162 ** temp buffer (buffer switching saves an extra
163 ** copy of the component text).
165 if (fmtstr && (cptr = wantcomp[CHASH(name)])) {
167 if (mh_strcasecmp(name, cptr->c_name)!=0) {
171 cptr->c_text = tmpbuf;
172 cp = tmpbuf+strlen(tmpbuf)-1;
173 for (; cp >= tmpbuf; cp--) {
183 } while ((cptr = cptr->c_next));
186 while (state == FLDPLUS) {
187 state = m_getfld(state, name, tmpbuf, SBUFSIZ,
197 /* stop here if we scan a msg in a folder */
201 /* otherwise (mbox): snarf the body */
207 while (state == BODY) {
208 state = m_getfld(state, name, tmpbuf, SBUFSIZ,
218 fprintf(stderr, innum ? "??Format error (message %d) in " : "??Format error in ", outnum ? outnum : innum);
219 fprintf(stderr, "component %d\n", compnum);
222 FPUTS("\n\nBAD MSG:\n");
228 /* fall through if we scan only */
234 adios(NULL, "getfld() returned %d", state);
240 /* Format and output the scan line. */
242 advise("read", "unable to");
247 if ((dat[2] = ftell(scnout)) == EOF)
248 adios(scnmsg, "write error on");
249 } else if (!scanfolder) {
250 if ((dat[2] = ftell(inb)) == EOF)
251 adios(scnmsg, "write error on");
256 fstat(fileno(inb), &st);
260 if (datecomp && !datecomp->c_text) {
261 if (!datecomp->c_text) {
262 if (!datecomp->c_tws)
263 datecomp->c_tws = (struct tws *) calloc((size_t) 1, sizeof(*datecomp->c_tws));
264 if (!datecomp->c_tws)
265 adios(NULL, "unable to allocate tws buffer");
266 *datecomp->c_tws = *dlocaltime((time_t *) &st.st_mtime);
267 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
269 datecomp->c_flags &= ~CF_DATEFAB;
273 /* print the scan line */
274 fmt_scan(fmt, scanl, slwidth, dat);
275 fputs(scanl, stdout);
278 /* return dynamically allocated buffers to pool */
279 while ((cptr = *savecomp++)) {
280 *--nxtbuf = cptr->c_text;
285 if (incing && (ferror(scnout) || fclose(scnout) == EOF))
286 adios(scnmsg, "write error on");
288 return (state != FILEEOF ? SCNERR : SCNMSG);