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 char returnpath[BUFSIZ];
78 char deliverydate[BUFSIZ];
79 int incing = (outnum != SCN_MBOX && outnum != SCN_FOLD);
80 int scanfolder = (outnum == SCN_FOLD);
84 /* first-time only initialization */
91 if ((width = sc_width()) < WIDTH/2)
93 else if (width > MAXSCANL)
96 dat[3] = slwidth = width;
97 scanl = (char *) mh_xmalloc((size_t) SCAN_CHARWIDTH *
98 (slwidth + 2)); /* probably for \n and \0 */
99 /* Compile format string */
100 ncomps = fmt_compile(fmtstr, &fmt) + 1;
101 FINDCOMP(datecomp, "date");
107 nxtbuf = compbuffers = (char **) calloc((size_t) ncomps,
110 adios(NULL, "unable to allocate component buffers");
111 used_buf = (struct comp **) calloc((size_t) (ncomps+1),
112 sizeof(struct comp *));
114 adios(NULL, "unable to allocate component buffer stack");
115 /* NULL-terminate array */
118 /* allocate space for the items */
119 for (i = ncomps; i--; )
120 *nxtbuf++ = mh_xmalloc(SBUFSIZ);
124 ** each-message initialization
126 nxtbuf = compbuffers;
129 dat[0] = innum ? innum : outnum;
135 ** Get the first field. If the message is non-empty
136 ** and we're doing an "inc", open the output file.
138 if ((state = m_getfld(FLD, name, tmpbuf, SBUFSIZ, inb)) == FILEEOF) {
140 advise("read", "unable to"); /* "read error" */
148 scnmsg = m_name(outnum);
149 if (*scnmsg == '?') /* msg num out of range */
151 if (!(scnout = fopen(scnmsg, "w")))
152 adios(scnmsg, "unable to write");
154 ** Add the Return-Path and Delivery-Date
155 ** header fields to message.
157 if (get_returnpath(returnpath, sizeof(returnpath),
158 deliverydate, sizeof(deliverydate))) {
159 FPUTS("Return-Path: ");
161 FPUTS("Delivery-Date: ");
166 /* scan - main loop */
168 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb)) {
179 ** if we're interested in this component, save
180 ** a pointer to the component text, then start
181 ** using our next free buffer as the component
182 ** temp buffer (buffer switching saves an extra
183 ** copy of the component text).
185 if (fmtstr && (cptr = wantcomp[CHASH(name)])) {
187 if (mh_strcasecmp(name, cptr->c_name)!=0) {
191 cptr->c_text = tmpbuf;
192 cp = tmpbuf+strlen(tmpbuf)-1;
193 for (; cp >= tmpbuf; cp--) {
203 } while ((cptr = cptr->c_next));
206 while (state == FLDPLUS) {
207 state = m_getfld(state, name, tmpbuf, SBUFSIZ,
217 /* stop here if we scan a msg in a folder */
221 /* otherwise (mbox): snarf the body */
227 while (state == BODY) {
228 state = m_getfld(state, name, tmpbuf, SBUFSIZ,
238 fprintf(stderr, innum ? "??Format error (message %d) in " : "??Format error in ", outnum ? outnum : innum);
239 fprintf(stderr, "component %d\n", compnum);
242 FPUTS("\n\nBAD MSG:\n");
248 /* fall through if we scan only */
254 adios(NULL, "getfld() returned %d", state);
260 /* Format and output the scan line. */
262 advise("read", "unable to");
267 if ((dat[2] = ftell(scnout)) == EOF)
268 adios(scnmsg, "write error on");
269 } else if (!scanfolder) {
270 if ((dat[2] = ftell(inb)) == EOF)
271 adios(scnmsg, "write error on");
276 fstat(fileno(inb), &st);
280 if (datecomp && !datecomp->c_text) {
281 if (!datecomp->c_text) {
282 if (!datecomp->c_tws)
283 datecomp->c_tws = (struct tws *) calloc((size_t) 1, sizeof(*datecomp->c_tws));
284 if (!datecomp->c_tws)
285 adios(NULL, "unable to allocate tws buffer");
286 *datecomp->c_tws = *dlocaltime((time_t *) &st.st_mtime);
287 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
289 datecomp->c_flags &= ~CF_DATEFAB;
293 /* print the scan line */
294 fmt_scan(fmt, scanl, slwidth, dat);
295 fputs(scanl, stdout);
298 /* return dynamically allocated buffers to pool */
299 while ((cptr = *savecomp++)) {
300 *--nxtbuf = cptr->c_text;
305 if (incing && (ferror(scnout) || fclose(scnout) == EOF))
306 adios(scnmsg, "write error on");
308 return (state != FILEEOF ? SCNERR : SCNMSG);