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 format *fmt_top;
44 static struct comp *datecomp; /* pntr to "date" comp */
45 static struct comp *bodycomp; /* pntr to "body" pseudo-comp (if referenced) */
46 static int ncomps = 0; /* # of interesting components */
47 static char **compbuffers = 0; /* buffers for component text */
48 static struct comp **used_buf = 0; /* stack for comp that use buffers */
50 static int dat[5]; /* aux. data for format routine */
52 char *scanl = 0; /* text of most recent scanline */
54 #define DIEWRERR() adios (scnmsg, "write error on")
57 if (mh_fputs(buf,scnout) == EOF)\
64 int sc_width (void); /* from termsbr.c */
65 static int mh_fputs(char *, FILE *);
67 #ifdef MULTIBYTE_SUPPORT
68 #define SCAN_CHARWIDTH MB_CUR_MAX
70 #define SCAN_CHARWIDTH 1
74 scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg,
75 int unseen, char *folder, long size, int noisy)
77 int i, compnum, encrypted, state;
78 unsigned char *cp, *tmpbuf;
80 char *saved_c_text = NULL;
82 struct comp **savecomp;
86 static int rlwidth, slwidth;
89 char returnpath[BUFSIZ];
90 char deliverydate[BUFSIZ];
93 /* first-time only initialization */
96 if ((width = sc_width ()) < WIDTH/2)
98 else if (width > MAXSCANL)
101 dat[3] = slwidth = width;
102 scanl = (char *) mh_xmalloc((size_t) SCAN_CHARWIDTH * (slwidth + 2) );
106 /* Compile format string */
107 ncomps = fmt_compile (nfs, &fmt) + 1;
112 FINDCOMP(bodycomp, "body");
113 FINDCOMP(datecomp, "date");
114 FINDCOMP(cptr, "folder");
116 cptr->c_text = folder;
117 FINDCOMP(cptr, "encrypted");
119 if ((cptr = (struct comp *) calloc (1, sizeof(*cptr)))) {
120 cptr->c_name = "encrypted";
121 cptr->c_next = wantcomp[i = CHASH (cptr->c_name)];
125 FINDCOMP (cptr, "dtimenow");
127 cptr->c_text = getcpy(dtimenow (0));
128 nxtbuf = compbuffers = (char **) calloc((size_t) ncomps, sizeof(char *));
130 adios (NULL, "unable to allocate component buffers");
131 used_buf = (struct comp **) calloc((size_t) (ncomps+1),
132 sizeof(struct comp *));
133 if (used_buf == NULL)
134 adios (NULL, "unable to allocate component buffer stack");
135 used_buf += ncomps+1; *--used_buf = 0;
136 rlwidth = bodycomp && (width > SBUFSIZ) ? width : SBUFSIZ;
137 for (i = ncomps; i--; )
138 *nxtbuf++ = mh_xmalloc(rlwidth);
142 * each-message initialization
144 nxtbuf = compbuffers;
147 dat[0] = innum ? innum : outnum;
152 * Get the first field. If the message is non-empty
153 * and we're doing an "inc", open the output file.
155 if ((state = m_getfld (FLD, name, tmpbuf, rlwidth, inb)) == FILEEOF) {
157 advise("read", "unable to"); /* "read error" */
166 scnmsg = m_name (outnum);
167 if (*scnmsg == '?') /* msg num out of range */
170 scnmsg = "/dev/null";
172 if ((scnout = fopen (scnmsg, "w")) == NULL)
173 adios (scnmsg, "unable to write");
176 * Add the Return-Path and Delivery-Date
177 * header fields to message.
179 if (get_returnpath (returnpath, sizeof(returnpath),
180 deliverydate, sizeof(deliverydate))) {
181 FPUTS ("Return-Path: ");
183 FPUTS ("Delivery-Date: ");
184 FPUTS (deliverydate);
189 /* scan - main loop */
190 for (compnum = 1; ; state = m_getfld (state, name, tmpbuf, rlwidth, inb)) {
197 if ( putc (':', scnout) == EOF) DIEWRERR();
201 * if we're interested in this component, save a pointer
202 * to the component text, then start using our next free
203 * buffer as the component temp buffer (buffer switching
204 * saves an extra copy of the component text).
206 if ((cptr = wantcomp[CHASH(name)])) {
208 if (!mh_strcasecmp(name, cptr->c_name)) {
209 if (! cptr->c_text) {
210 cptr->c_text = tmpbuf;
211 for (cp = tmpbuf + strlen (tmpbuf) - 1;
222 } while ((cptr = cptr->c_next));
225 while (state == FLDPLUS) {
226 state = m_getfld (state, name, tmpbuf, rlwidth, inb);
235 state = FILEEOF; /* stop now if scan cmd */
238 if (putc ('\n', scnout) == EOF) DIEWRERR();
241 * performance hack: some people like to run "inc" on
242 * things like net.sources or large digests. We do a
243 * copy directly into the output buffer rather than
244 * going through an intermediate buffer.
246 * We need the amount of data m_getfld found & don't
247 * want to do a strlen on the long buffer so there's
248 * a hack in m_getfld to save the amount of data it
249 * returned in the global "msg_count".
252 while (state == BODY) {
254 if (scnout->_IO_write_ptr == scnout->_IO_write_end) {
255 #elif defined(__DragonFly__)
256 if (((struct __FILE_public *)scnout)->_w <= 0) {
258 if (scnout->_cnt <= 0) {
260 if (fflush(scnout) == EOF)
264 state = m_getfld(state, name, scnout->_IO_write_ptr,
265 (long)scnout->_IO_write_ptr-(long)scnout->_IO_write_end , inb);
266 scnout->_IO_write_ptr += msg_count;
267 #elif defined(__DragonFly__)
268 state = m_getfld( state, name, ((struct __FILE_public *)scnout)->_p, -(((struct __FILE_public *)scnout)->_w), inb );
269 ((struct __FILE_public *)scnout)->_w -= msg_count;
270 ((struct __FILE_public *)scnout)->_p += msg_count;
272 state = m_getfld( state, name, scnout->_ptr, -(scnout->_cnt), inb );
273 scnout->_cnt -= msg_count;
274 scnout->_ptr += msg_count;
282 innum ? "??Format error (message %d) in "
283 : "??Format error in ",
284 outnum ? outnum : innum);
285 fprintf (stderr, "component %d\n", compnum);
288 FPUTS ("\n\nBAD MSG:\n");
290 if (putc ('\n', scnout) == EOF) DIEWRERR();
300 adios (NULL, "getfld() returned %d", state);
305 * format and output the scan line.
309 advise("read", "unable to"); /* "read error" */
313 /* Save and restore buffer so we don't trash our dynamic pool! */
315 saved_c_text = bodycomp->c_text;
316 bodycomp->c_text = tmpbuf;
323 dat[2] = ftell(scnout);
324 if (dat[2] == EOF) DIEWRERR();
327 if ((datecomp && !datecomp->c_text) || (!size && !outnum)) {
330 fstat (fileno(inb), &st);
331 if (!size && !outnum)
334 if (! datecomp->c_text) {
335 if (datecomp->c_tws == NULL)
336 datecomp->c_tws = (struct tws *)
337 calloc((size_t) 1, sizeof(*datecomp->c_tws));
338 if (datecomp->c_tws == NULL)
339 adios (NULL, "unable to allocate tws buffer");
340 *datecomp->c_tws = *dlocaltime ((time_t *) &st.st_mtime);
341 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
343 datecomp->c_flags &= ~CF_DATEFAB;
348 fmt_scan (fmt, scanl, slwidth, dat);
351 fmt = fmt_scan (fmt, scanl, slwidth, dat);
353 fmt = fmt_top; /* reset for old format files */
357 bodycomp->c_text = saved_c_text;
360 fputs (scanl, stdout);
362 FINDCOMP (cptr, "encrypted");
363 encrypted = cptr && cptr->c_text;
365 /* return dynamically allocated buffers to pool */
366 while ((cptr = *savecomp++)) {
367 *--nxtbuf = cptr->c_text;
372 if (outnum && (ferror(scnout) || fclose (scnout) == EOF))
375 return (state != FILEEOF ? SCNERR : encrypted ? SCNENC : SCNMSG);
379 mh_fputs(char *s, FILE *stream)
384 if (putc (c,stream) == EOF )