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 */
25 # define _filbuf(fp) ((fp)->__cnt = 0, __filbuf(fp))
28 #define MAXSCANL 256 /* longest possible scan line */
31 ** Buffer size for content part of header fields. We want this
32 ** to be large enough so that we don't do a lot of extra FLDPLUS
37 static struct format *fmt;
39 static struct comp *datecomp; /* pntr to "date" comp */
40 static int ncomps = 0; /* # of interesting components */
41 static char **compbuffers = NULL; /* buffers for component text */
42 static struct comp **used_buf = NULL; /* stack for comp that use buffers */
44 static int dat[5]; /* aux. data for format routine */
46 char *scanl = NULL; /* text of most recent scanline */
49 if (fputs(buf, scnout) == EOF)\
50 adios(scnmsg, "write error on");\
56 int sc_width(void); /* from termsbr.c */
58 #ifdef MULTIBYTE_SUPPORT
59 #define SCAN_CHARWIDTH MB_CUR_MAX
61 #define SCAN_CHARWIDTH 1
65 scan(FILE *inb, int innum, int outnum, char *fmtstr, int width, int curflg,
69 int i, compnum, state;
70 unsigned char *cp, *tmpbuf;
73 struct comp **savecomp;
77 int incing = (outnum != SCN_MBOX && outnum != SCN_FOLD);
78 int scanfolder = (outnum == SCN_FOLD);
82 /* first-time only initialization */
89 if ((width = sc_width()) < WIDTH/2)
91 else if (width > MAXSCANL)
94 dat[3] = slwidth = width;
95 scanl = (char *) mh_xmalloc((size_t) SCAN_CHARWIDTH *
96 (slwidth + 2)); /* probably for \n and \0 */
97 /* Compile format string */
98 ncomps = fmt_compile(fmtstr, &fmt) + 1;
99 FINDCOMP(datecomp, "date");
105 nxtbuf = compbuffers = (char **) calloc((size_t) ncomps,
108 adios(NULL, "unable to allocate component buffers");
109 used_buf = (struct comp **) calloc((size_t) (ncomps+1),
110 sizeof(struct comp *));
112 adios(NULL, "unable to allocate component buffer stack");
113 /* NULL-terminate array */
116 /* allocate space for the items */
117 for (i = ncomps; i--; )
118 *nxtbuf++ = mh_xmalloc(SBUFSIZ);
122 ** each-message initialization
124 nxtbuf = compbuffers;
127 dat[0] = innum ? innum : outnum;
133 ** Get the first field. If the message is non-empty
134 ** and we're doing an "inc", open the output file.
136 if ((state = m_getfld(FLD, name, tmpbuf, SBUFSIZ, inb)) == FILEEOF) {
138 advise("read", "unable to"); /* "read error" */
146 scnmsg = m_name(outnum);
147 if (*scnmsg == '?') /* msg num out of range */
149 if (!(scnout = fopen(scnmsg, "w")))
150 adios(scnmsg, "unable to write");
153 /* scan - main loop */
155 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb)) {
166 ** if we're interested in this component, save
167 ** a pointer to the component text, then start
168 ** using our next free buffer as the component
169 ** temp buffer (buffer switching saves an extra
170 ** copy of the component text).
172 if (fmtstr && (cptr = wantcomp[CHASH(name)])) {
174 if (mh_strcasecmp(name, cptr->c_name)!=0) {
178 cptr->c_text = tmpbuf;
179 cp = tmpbuf+strlen(tmpbuf)-1;
180 for (; cp >= tmpbuf; cp--) {
190 } while ((cptr = cptr->c_next));
193 while (state == FLDPLUS) {
194 state = m_getfld(state, name, tmpbuf, SBUFSIZ,
204 /* stop here if we scan a msg in a folder */
208 /* otherwise (mbox): snarf the body */
214 while (state == BODY) {
215 state = m_getfld(state, name, tmpbuf, SBUFSIZ,
225 fprintf(stderr, innum ? "??Format error (message %d) in " : "??Format error in ", outnum ? outnum : innum);
226 fprintf(stderr, "component %d\n", compnum);
229 FPUTS("\n\nBAD MSG:\n");
235 /* fall through if we scan only */
241 adios(NULL, "getfld() returned %d", state);
247 /* Format and output the scan line. */
249 advise("read", "unable to");
254 if ((dat[2] = ftell(scnout)) == EOF)
255 adios(scnmsg, "write error on");
256 } else if (!scanfolder) {
257 if ((dat[2] = ftell(inb)) == EOF)
258 adios(scnmsg, "write error on");
263 fstat(fileno(inb), &st);
267 if (datecomp && !datecomp->c_text) {
268 if (!datecomp->c_text) {
269 if (!datecomp->c_tws)
270 datecomp->c_tws = (struct tws *) calloc((size_t) 1, sizeof(*datecomp->c_tws));
271 if (!datecomp->c_tws)
272 adios(NULL, "unable to allocate tws buffer");
273 *datecomp->c_tws = *dlocaltime((time_t *) &st.st_mtime);
274 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
276 datecomp->c_flags &= ~CF_DATEFAB;
280 /* print the scan line */
281 fmt_scan(fmt, scanl, slwidth, dat);
282 fputs(scanl, stdout);
285 /* return dynamically allocated buffers to pool */
286 while ((cptr = *savecomp++)) {
287 *--nxtbuf = cptr->c_text;
292 if (incing && (ferror(scnout) || fclose(scnout) == EOF))
293 adios(scnmsg, "write error on");
295 return (state != FILEEOF ? SCNERR : SCNMSG);