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
33 ** calls on m_getfld but small enough so that we don't snarf
34 ** the entire message body when we're only going to display 30
39 static struct format *fmt;
41 static struct comp *datecomp; /* pntr to "date" comp */
42 static struct comp *bodycomp; /* pntr to "body" pseudo-comp (if referenced) */
43 static int ncomps = 0; /* # of interesting components */
44 static char **compbuffers = NULL; /* buffers for component text */
45 static struct comp **used_buf = NULL; /* stack for comp that use buffers */
47 static int dat[5]; /* aux. data for format routine */
49 char *scanl = NULL; /* text of most recent scanline */
52 if (fputs(buf,scnout) == EOF)\
53 adios(scnmsg, "write error on");\
59 int sc_width(void); /* from termsbr.c */
61 #ifdef MULTIBYTE_SUPPORT
62 #define SCAN_CHARWIDTH MB_CUR_MAX
64 #define SCAN_CHARWIDTH 1
68 scan(FILE *inb, int innum, int outnum, char *fmtstr, int width, int curflg,
71 int i, compnum, state;
72 unsigned char *cp, *tmpbuf;
74 char *saved_c_text = NULL;
76 struct comp **savecomp;
80 static int rlwidth, slwidth;
81 char returnpath[BUFSIZ];
82 char deliverydate[BUFSIZ];
83 int incing = (outnum > 0);
84 int ismbox = (outnum != 0);
86 /* 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 *
100 /* Compile format string */
101 ncomps = fmt_compile(fmtstr, &fmt) + 1;
103 FINDCOMP(bodycomp, "body");
104 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 *));
111 if (used_buf == NULL)
112 adios(NULL, "unable to allocate component buffer stack");
113 used_buf += ncomps+1; *--used_buf = 0;
114 rlwidth = bodycomp && (width > SBUFSIZ) ? width : SBUFSIZ;
115 for (i = ncomps; i--; )
116 *nxtbuf++ = mh_xmalloc(rlwidth);
120 ** each-message initialization
122 nxtbuf = compbuffers;
125 dat[0] = innum ? innum : outnum;
130 ** Get the first field. If the message is non-empty
131 ** and we're doing an "inc", open the output file.
133 if ((state = m_getfld(FLD, name, tmpbuf, rlwidth, inb)) == FILEEOF) {
135 advise("read", "unable to"); /* "read error" */
144 scnmsg = m_name(outnum);
145 if (*scnmsg == '?') /* msg num out of range */
148 scnmsg = "/dev/null";
150 if ((scnout = fopen(scnmsg, "w")) == NULL)
151 adios(scnmsg, "unable to write");
153 ** Add the Return-Path and Delivery-Date
154 ** header fields to message.
156 if (get_returnpath(returnpath, sizeof(returnpath),
157 deliverydate, sizeof(deliverydate))) {
158 FPUTS("Return-Path: ");
160 FPUTS("Delivery-Date: ");
165 /* scan - main loop */
167 state = m_getfld(state, name, tmpbuf, rlwidth, inb)) {
174 if (putc(':', scnout) == EOF)
175 adios(scnmsg, "write error on");
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 ((cptr = wantcomp[CHASH(name)])) {
187 if (!mh_strcasecmp(name, cptr->c_name)) {
188 if (! cptr->c_text) {
189 cptr->c_text = tmpbuf;
190 for (cp = tmpbuf + strlen(tmpbuf) - 1; cp >= tmpbuf; cp--)
200 } while ((cptr = cptr->c_next));
203 while (state == FLDPLUS) {
204 state = m_getfld(state, name, tmpbuf, rlwidth,
214 state = FILEEOF; /* stop now if scan cmd */
217 if (putc('\n', scnout) == EOF)
218 adios(scnmsg, "write error on");
221 ** performance hack: some people like to run "inc"
222 ** on things like net.sources or large digests.
223 ** We do a copy directly into the output buffer
224 ** rather than going through an intermediate buffer.
226 ** We need the amount of data m_getfld found &
227 ** don't want to do a strlen on the long buffer so
228 ** there's a hack in m_getfld to save the amount
229 ** of data it returned in the global "msg_count".
232 while (state == BODY) {
234 if (scnout->_IO_write_ptr == scnout->_IO_write_end) {
235 #elif defined(__DragonFly__)
236 if (((struct __FILE_public *)scnout)->_w <= 0) {
238 if (scnout->_cnt <= 0) {
240 if (fflush(scnout) == EOF)
241 adios(scnmsg, "write error on");
244 state = m_getfld(state, name, scnout->_IO_write_ptr, (long)scnout->_IO_write_ptr-(long)scnout->_IO_write_end , inb);
245 scnout->_IO_write_ptr += msg_count;
246 #elif defined(__DragonFly__)
247 state = m_getfld(state, name, ((struct __FILE_public *)scnout)->_p, -(((struct __FILE_public *)scnout)->_w), inb);
248 ((struct __FILE_public *)scnout)->_w -= msg_count;
249 ((struct __FILE_public *)scnout)->_p += msg_count;
251 state = m_getfld(state, name, scnout->_ptr, -(scnout->_cnt), inb);
252 scnout->_cnt -= msg_count;
253 scnout->_ptr += msg_count;
260 fprintf(stderr, innum ? "??Format error (message %d) in " : "??Format error in ", outnum ? outnum : innum);
261 fprintf(stderr, "component %d\n", compnum);
264 FPUTS("\n\nBAD MSG:\n");
266 if (putc('\n', scnout) == EOF)
267 adios(scnmsg, "write error on");
279 adios(NULL, "getfld() returned %d", state);
285 /* Format and output the scan line. */
287 advise("read", "unable to");
291 /* Save and restore buffer so we don't trash our dynamic pool! */
293 saved_c_text = bodycomp->c_text;
294 bodycomp->c_text = tmpbuf;
298 if ((dat[2] = ftell(scnout)) == EOF)
299 adios(scnmsg, "write error on");
302 if ((datecomp && !datecomp->c_text) || !ismbox) {
305 fstat(fileno(inb), &st);
309 if (! datecomp->c_text) {
310 if (datecomp->c_tws == NULL)
311 datecomp->c_tws = (struct tws *) calloc((size_t) 1, sizeof(*datecomp->c_tws));
312 if (datecomp->c_tws == NULL)
313 adios(NULL, "unable to allocate tws buffer");
314 *datecomp->c_tws = *dlocaltime((time_t *) &st.st_mtime);
315 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
317 datecomp->c_flags &= ~CF_DATEFAB;
322 /* print the scan line */
323 fmt_scan(fmt, scanl, slwidth, dat);
325 fputs(scanl, stdout);
328 bodycomp->c_text = saved_c_text;
330 /* return dynamically allocated buffers to pool */
331 while ((cptr = *savecomp++)) {
332 *--nxtbuf = cptr->c_text;
337 if (ismbox && (ferror(scnout) || fclose(scnout) == EOF))
338 adios(scnmsg, "write error on");
340 return (state != FILEEOF ? SCNERR : SCNMSG);