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 int ncomps = 0; /* # of interesting components */
43 static char **compbuffers = NULL; /* buffers for component text */
44 static struct comp **used_buf = NULL; /* stack for comp that use buffers */
46 static int dat[5]; /* aux. data for format routine */
48 char *scanl = NULL; /* text of most recent scanline */
51 if (fputs(buf, scnout) == EOF)\
52 adios(scnmsg, "write error on");\
58 int sc_width(void); /* from termsbr.c */
60 #ifdef MULTIBYTE_SUPPORT
61 #define SCAN_CHARWIDTH MB_CUR_MAX
63 #define SCAN_CHARWIDTH 1
67 scan(FILE *inb, int innum, int outnum, char *fmtstr, int width, int curflg,
70 int i, compnum, state;
71 unsigned char *cp, *tmpbuf;
74 struct comp **savecomp;
79 char returnpath[BUFSIZ];
80 char deliverydate[BUFSIZ];
81 int incing = (outnum > 0);
82 int scanfolder = (outnum == 0);
84 /* first-time only initialization */
87 if ((width = sc_width()) < WIDTH/2)
89 else if (width > MAXSCANL)
92 dat[3] = slwidth = width;
93 scanl = (char *) mh_xmalloc((size_t) SCAN_CHARWIDTH *
98 /* Compile format string */
99 ncomps = fmt_compile(fmtstr, &fmt) + 1;
101 FINDCOMP(datecomp, "date");
102 nxtbuf = compbuffers = (char **) calloc((size_t) ncomps,
105 adios(NULL, "unable to allocate component buffers");
106 used_buf = (struct comp **) calloc((size_t) (ncomps+1),
107 sizeof(struct comp *));
108 if (used_buf == NULL)
109 adios(NULL, "unable to allocate component buffer stack");
110 used_buf += ncomps+1; *--used_buf = 0;
111 for (i = ncomps; i--; )
112 *nxtbuf++ = mh_xmalloc(SBUFSIZ);
116 ** each-message initialization
118 nxtbuf = compbuffers;
121 dat[0] = innum ? innum : outnum;
126 ** Get the first field. If the message is non-empty
127 ** and we're doing an "inc", open the output file.
129 if ((state = m_getfld(FLD, name, tmpbuf, SBUFSIZ, inb)) == FILEEOF) {
131 advise("read", "unable to"); /* "read error" */
139 scnmsg = m_name(outnum);
140 if (*scnmsg == '?') /* msg num out of range */
142 if ((scnout = fopen(scnmsg, "w")) == NULL)
143 adios(scnmsg, "unable to write");
145 ** Add the Return-Path and Delivery-Date
146 ** header fields to message.
148 if (get_returnpath(returnpath, sizeof(returnpath),
149 deliverydate, sizeof(deliverydate))) {
150 FPUTS("Return-Path: ");
152 FPUTS("Delivery-Date: ");
157 /* scan - main loop */
159 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb)) {
170 ** if we're interested in this component, save
171 ** a pointer to the component text, then start
172 ** using our next free buffer as the component
173 ** temp buffer (buffer switching saves an extra
174 ** copy of the component text).
176 if ((cptr = wantcomp[CHASH(name)])) {
178 if (!mh_strcasecmp(name, cptr->c_name)) {
179 if (! cptr->c_text) {
180 cptr->c_text = tmpbuf;
181 for (cp = tmpbuf + strlen(tmpbuf) - 1; cp >= tmpbuf; cp--)
191 } while ((cptr = cptr->c_next));
194 while (state == FLDPLUS) {
195 state = m_getfld(state, name, tmpbuf, SBUFSIZ,
205 state = FILEEOF; /* stop here if scan cmd */
211 ** performance hack: some people like to run "inc"
212 ** on things like net.sources or large digests.
213 ** We do a copy directly into the output buffer
214 ** rather than going through an intermediate buffer.
216 ** We need the amount of data m_getfld found &
217 ** don't want to do a strlen on the long buffer so
218 ** there's a hack in m_getfld to save the amount
219 ** of data it returned in the global "msg_count".
222 while (state == BODY) {
224 if (scnout->_IO_write_ptr == scnout->_IO_write_end) {
225 #elif defined(__DragonFly__)
226 if (((struct __FILE_public *)scnout)->_w <= 0) {
228 if (scnout->_cnt <= 0) {
230 if (fflush(scnout) == EOF)
231 adios(scnmsg, "write error on");
234 state = m_getfld(state, name, scnout->_IO_write_ptr, (long)scnout->_IO_write_ptr-(long)scnout->_IO_write_end , inb);
235 scnout->_IO_write_ptr += msg_count;
236 #elif defined(__DragonFly__)
237 state = m_getfld(state, name, ((struct __FILE_public *)scnout)->_p, -(((struct __FILE_public *)scnout)->_w), inb);
238 ((struct __FILE_public *)scnout)->_w -= msg_count;
239 ((struct __FILE_public *)scnout)->_p += msg_count;
241 state = m_getfld(state, name, scnout->_ptr, -(scnout->_cnt), inb);
242 scnout->_cnt -= msg_count;
243 scnout->_ptr += msg_count;
250 fprintf(stderr, innum ? "??Format error (message %d) in " : "??Format error in ", outnum ? outnum : innum);
251 fprintf(stderr, "component %d\n", compnum);
254 FPUTS("\n\nBAD MSG:\n");
260 /* fall through if we scan only */
266 adios(NULL, "getfld() returned %d", state);
272 /* Format and output the scan line. */
274 advise("read", "unable to");
279 if ((dat[2] = ftell(scnout)) == EOF)
280 adios(scnmsg, "write error on");
283 if ((datecomp && !datecomp->c_text) || scanfolder) {
286 fstat(fileno(inb), &st);
290 if (!datecomp->c_text) {
291 if (datecomp->c_tws == NULL)
292 datecomp->c_tws = (struct tws *) calloc((size_t) 1, sizeof(*datecomp->c_tws));
293 if (datecomp->c_tws == NULL)
294 adios(NULL, "unable to allocate tws buffer");
295 *datecomp->c_tws = *dlocaltime((time_t *) &st.st_mtime);
296 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
298 datecomp->c_flags &= ~CF_DATEFAB;
303 /* print the scan line */
304 fmt_scan(fmt, scanl, slwidth, dat);
306 fputs(scanl, stdout);
308 /* return dynamically allocated buffers to pool */
309 while ((cptr = *savecomp++)) {
310 *--nxtbuf = cptr->c_text;
315 if (incing && (ferror(scnout) || fclose(scnout) == EOF))
316 adios(scnmsg, "write error on");
318 return (state != FILEEOF ? SCNERR : SCNMSG);