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);
83 /* first-time only initialization */
86 if ((width = sc_width()) < WIDTH/2)
88 else if (width > MAXSCANL)
91 dat[3] = slwidth = width;
92 scanl = (char *) mh_xmalloc((size_t) SCAN_CHARWIDTH *
93 (slwidth + 2)); /* probably for \n and \0 */
97 /* Compile format string */
98 ncomps = fmt_compile(fmtstr, &fmt) + 1;
100 FINDCOMP(datecomp, "date");
101 nxtbuf = compbuffers = (char **) calloc((size_t) ncomps,
104 adios(NULL, "unable to allocate component buffers");
105 used_buf = (struct comp **) calloc((size_t) (ncomps+1),
106 sizeof(struct comp *));
108 adios(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(scnmsg, "unable to write");
148 ** Add the Return-Path and Delivery-Date
149 ** header fields to message.
151 if (get_returnpath(returnpath, sizeof(returnpath),
152 deliverydate, sizeof(deliverydate))) {
153 FPUTS("Return-Path: ");
155 FPUTS("Delivery-Date: ");
160 /* scan - main loop */
162 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb)) {
173 ** if we're interested in this component, save
174 ** a pointer to the component text, then start
175 ** using our next free buffer as the component
176 ** temp buffer (buffer switching saves an extra
177 ** copy of the component text).
179 if ((cptr = wantcomp[CHASH(name)])) {
181 if (!mh_strcasecmp(name, cptr->c_name)) {
182 if (! cptr->c_text) {
183 cptr->c_text = tmpbuf;
184 for (cp = tmpbuf + strlen(tmpbuf) - 1; cp >= tmpbuf; cp--)
194 } while ((cptr = cptr->c_next));
197 while (state == FLDPLUS) {
198 state = m_getfld(state, name, tmpbuf, SBUFSIZ,
208 /* stop here if we scan a msg in a folder */
212 /* otherwise (mbox): snarf the body */
218 while (state == BODY) {
219 state = m_getfld(state, name, tmpbuf, SBUFSIZ,
229 fprintf(stderr, innum ? "??Format error (message %d) in " : "??Format error in ", outnum ? outnum : innum);
230 fprintf(stderr, "component %d\n", compnum);
233 FPUTS("\n\nBAD MSG:\n");
239 /* fall through if we scan only */
245 adios(NULL, "getfld() returned %d", state);
251 /* Format and output the scan line. */
253 advise("read", "unable to");
258 if ((dat[2] = ftell(scnout)) == EOF)
259 adios(scnmsg, "write error on");
260 } else if (!scanfolder) {
261 if ((dat[2] = ftell(inb)) == EOF)
262 adios(scnmsg, "write error on");
266 if ((datecomp && !datecomp->c_text) || scanfolder) {
269 fstat(fileno(inb), &st);
273 if (!datecomp->c_text) {
274 if (!datecomp->c_tws)
275 datecomp->c_tws = (struct tws *) calloc((size_t) 1, sizeof(*datecomp->c_tws));
276 if (!datecomp->c_tws)
277 adios(NULL, "unable to allocate tws buffer");
278 *datecomp->c_tws = *dlocaltime((time_t *) &st.st_mtime);
279 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
281 datecomp->c_flags &= ~CF_DATEFAB;
286 /* print the scan line */
287 fmt_scan(fmt, scanl, slwidth, dat);
289 fputs(scanl, stdout);
291 /* return dynamically allocated buffers to pool */
292 while ((cptr = *savecomp++)) {
293 *--nxtbuf = cptr->c_text;
298 if (incing && (ferror(scnout) || fclose(scnout) == EOF))
299 adios(scnmsg, "write error on");
301 return (state != FILEEOF ? SCNERR : SCNMSG);