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 > 0);
80 int scanfolder = (outnum == 0);
82 /* 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 */
96 /* Compile format string */
97 ncomps = fmt_compile(fmtstr, &fmt) + 1;
99 FINDCOMP(datecomp, "date");
100 nxtbuf = compbuffers = (char **) calloc((size_t) ncomps,
103 adios(NULL, "unable to allocate component buffers");
104 used_buf = (struct comp **) calloc((size_t) (ncomps+1),
105 sizeof(struct comp *));
107 adios(NULL, "unable to allocate component buffer stack");
108 /* NULL-terminate array */
111 /* allocate space for the items */
112 for (i = ncomps; i--; )
113 *nxtbuf++ = mh_xmalloc(SBUFSIZ);
117 ** each-message initialization
119 nxtbuf = compbuffers;
122 dat[0] = innum ? innum : outnum;
127 ** Get the first field. If the message is non-empty
128 ** and we're doing an "inc", open the output file.
130 if ((state = m_getfld(FLD, name, tmpbuf, SBUFSIZ, inb)) == FILEEOF) {
132 advise("read", "unable to"); /* "read error" */
140 scnmsg = m_name(outnum);
141 if (*scnmsg == '?') /* msg num out of range */
143 if (!(scnout = fopen(scnmsg, "w")))
144 adios(scnmsg, "unable to write");
146 ** Add the Return-Path and Delivery-Date
147 ** header fields to message.
149 if (get_returnpath(returnpath, sizeof(returnpath),
150 deliverydate, sizeof(deliverydate))) {
151 FPUTS("Return-Path: ");
153 FPUTS("Delivery-Date: ");
158 /* scan - main loop */
160 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb)) {
171 ** if we're interested in this component, save
172 ** a pointer to the component text, then start
173 ** using our next free buffer as the component
174 ** temp buffer (buffer switching saves an extra
175 ** copy of the component text).
177 if ((cptr = wantcomp[CHASH(name)])) {
179 if (!mh_strcasecmp(name, cptr->c_name)) {
180 if (! cptr->c_text) {
181 cptr->c_text = tmpbuf;
182 for (cp = tmpbuf + strlen(tmpbuf) - 1; cp >= tmpbuf; cp--)
192 } while ((cptr = cptr->c_next));
195 while (state == FLDPLUS) {
196 state = m_getfld(state, name, tmpbuf, SBUFSIZ,
206 state = FILEEOF; /* stop here if scan cmd */
212 ** performance hack: some people like to run "inc"
213 ** on things like net.sources or large digests.
214 ** We do a copy directly into the output buffer
215 ** rather than going through an intermediate buffer.
217 ** We need the amount of data m_getfld found &
218 ** don't want to do a strlen on the long buffer so
219 ** there's a hack in m_getfld to save the amount
220 ** of data it returned in the global "msg_count".
223 while (state == BODY) {
225 if (scnout->_IO_write_ptr == scnout->_IO_write_end) {
226 #elif defined(__DragonFly__)
227 if (((struct __FILE_public *)scnout)->_w <= 0) {
229 if (scnout->_cnt <= 0) {
231 if (fflush(scnout) == EOF)
232 adios(scnmsg, "write error on");
235 state = m_getfld(state, name, scnout->_IO_write_ptr, (long)scnout->_IO_write_ptr-(long)scnout->_IO_write_end , inb);
236 scnout->_IO_write_ptr += msg_count;
237 #elif defined(__DragonFly__)
238 state = m_getfld(state, name, ((struct __FILE_public *)scnout)->_p, -(((struct __FILE_public *)scnout)->_w), inb);
239 ((struct __FILE_public *)scnout)->_w -= msg_count;
240 ((struct __FILE_public *)scnout)->_p += msg_count;
242 state = m_getfld(state, name, scnout->_ptr, -(scnout->_cnt), inb);
243 scnout->_cnt -= msg_count;
244 scnout->_ptr += msg_count;
251 fprintf(stderr, innum ? "??Format error (message %d) in " : "??Format error in ", outnum ? outnum : innum);
252 fprintf(stderr, "component %d\n", compnum);
255 FPUTS("\n\nBAD MSG:\n");
261 /* fall through if we scan only */
267 adios(NULL, "getfld() returned %d", state);
273 /* Format and output the scan line. */
275 advise("read", "unable to");
280 if ((dat[2] = ftell(scnout)) == EOF)
281 adios(scnmsg, "write error on");
284 if ((datecomp && !datecomp->c_text) || scanfolder) {
287 fstat(fileno(inb), &st);
291 if (!datecomp->c_text) {
292 if (!datecomp->c_tws)
293 datecomp->c_tws = (struct tws *) calloc((size_t) 1, sizeof(*datecomp->c_tws));
294 if (!datecomp->c_tws)
295 adios(NULL, "unable to allocate tws buffer");
296 *datecomp->c_tws = *dlocaltime((time_t *) &st.st_mtime);
297 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
299 datecomp->c_flags &= ~CF_DATEFAB;
304 /* print the scan line */
305 fmt_scan(fmt, scanl, slwidth, dat);
307 fputs(scanl, stdout);
309 /* return dynamically allocated buffers to pool */
310 while ((cptr = *savecomp++)) {
311 *--nxtbuf = cptr->c_text;
316 if (incing && (ferror(scnout) || fclose(scnout) == EOF))
317 adios(scnmsg, "write error on");
319 return (state != FILEEOF ? SCNERR : SCNMSG);