3 * scansbr.c -- routines to help scan along...
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 #include <h/addrsbr.h>
12 #include <h/fmt_scan.h>
13 #include <h/scansbr.h>
18 # define _ptr _p /* Gag */
19 # define _cnt _w /* Wretch */
22 #define MAXSCANL 256 /* longest possible scan line */
25 * Buffer size for content part of header fields. We want this
26 * to be large enough so that we don't do a lot of extra FLDPLUS
27 * calls on m_getfld but small enough so that we don't snarf
28 * the entire message body when we're only going to display 30
33 static struct format *fmt;
35 static struct format *fmt_top;
38 static struct comp *datecomp; /* pntr to "date" comp */
39 static struct comp *bodycomp; /* pntr to "body" pseudo-comp *
41 static int ncomps = 0; /* # of interesting components */
42 static char **compbuffers = 0; /* buffers for component text */
43 static struct comp **used_buf = 0; /* stack for comp that use buffers */
45 static int dat[5]; /* aux. data for format routine */
47 char *scanl = 0; /* text of most recent scanline */
49 #define DIEWRERR() adios (scnmsg, "write error on")
52 if (mh_fputs(buf,scnout) == EOF)\
59 static int mh_fputs(char *, FILE *);
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 *nfs, int width, int curflg,
69 int unseen, char *folder, long size, int noisy)
71 int i, compnum, encrypted, state;
72 unsigned char *cp, *tmpbuf;
74 char *saved_c_text = NULL;
76 struct comp **savecomp;
80 static int rlwidth, slwidth;
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 * (slwidth + 2) );
95 /* Compile format string */
96 ncomps = fmt_compile (nfs, &fmt) + 1;
101 FINDCOMP(bodycomp, "body");
102 FINDCOMP(datecomp, "date");
103 FINDCOMP(cptr, "folder");
105 cptr->c_text = folder;
106 FINDCOMP(cptr, "encrypted");
108 if ((cptr = (struct comp *) calloc (1, sizeof(*cptr)))) {
109 cptr->c_name = "encrypted";
110 cptr->c_next = wantcomp[i = CHASH (cptr->c_name)];
114 FINDCOMP (cptr, "dtimenow");
116 cptr->c_text = getcpy(dtimenow (0));
117 nxtbuf = compbuffers = (char **) calloc((size_t) ncomps, sizeof(char *));
119 adios (NULL, "unable to allocate component buffers");
120 used_buf = (struct comp **) calloc((size_t) (ncomps+1),
121 sizeof(struct comp *));
122 if (used_buf == NULL)
123 adios (NULL, "unable to allocate component buffer stack");
124 used_buf += ncomps+1; *--used_buf = 0;
125 rlwidth = bodycomp && (width > SBUFSIZ) ? width : SBUFSIZ;
126 for (i = ncomps; i--; )
127 *nxtbuf++ = mh_xmalloc(rlwidth);
131 * each-message initialization
133 nxtbuf = compbuffers;
136 dat[0] = innum ? innum : outnum;
141 * Get the first field. If the message is non-empty
142 * and we're doing an "inc", open the output file.
144 if ((state = m_getfld (FLD, name, tmpbuf, rlwidth, inb)) == FILEEOF) {
146 advise("read", "unable to"); /* "read error" */
155 scnmsg = m_name (outnum);
156 if (*scnmsg == '?') /* msg num out of range */
159 scnmsg = "/dev/null";
161 if ((scnout = fopen (scnmsg, "w")) == NULL)
162 adios (scnmsg, "unable to write");
165 /* scan - main loop */
166 for (compnum = 1; ; state = m_getfld (state, name, tmpbuf, rlwidth, inb)) {
173 if ( putc (':', scnout) == EOF) DIEWRERR();
177 * if we're interested in this component, save a pointer
178 * to the component text, then start using our next free
179 * buffer as the component temp buffer (buffer switching
180 * saves an extra copy of the component text).
182 if ((cptr = wantcomp[CHASH(name)])) {
184 if (!mh_strcasecmp(name, cptr->c_name)) {
185 if (! cptr->c_text) {
186 cptr->c_text = tmpbuf;
187 for (cp = tmpbuf + strlen (tmpbuf) - 1;
198 } while ((cptr = cptr->c_next));
201 while (state == FLDPLUS) {
202 state = m_getfld (state, name, tmpbuf, rlwidth, inb);
211 state = FILEEOF; /* stop now if scan cmd */
214 if (putc ('\n', scnout) == EOF) DIEWRERR();
217 * performance hack: some people like to run "inc" on
218 * things like net.sources or large digests. We do a
219 * copy directly into the output buffer rather than
220 * going through an intermediate buffer.
222 * We need the amount of data m_getfld found & don't
223 * want to do a strlen on the long buffer so there's
224 * a hack in m_getfld to save the amount of data it
225 * returned in the global "msg_count".
228 while (state == BODY) {
230 if (scnout->_IO_write_ptr == scnout->_IO_write_end) {
231 #elif defined(__DragonFly__)
232 if (((struct __FILE_public *)scnout)->_w <= 0) {
234 if (scnout->_cnt <= 0) {
236 if (fflush(scnout) == EOF)
240 state = m_getfld(state, name, scnout->_IO_write_ptr,
241 (long)scnout->_IO_write_ptr-(long)scnout->_IO_write_end , inb);
242 scnout->_IO_write_ptr += msg_count;
243 #elif defined(__DragonFly__)
244 state = m_getfld( state, name, ((struct __FILE_public *)scnout)->_p, -(((struct __FILE_public *)scnout)->_w), inb );
245 ((struct __FILE_public *)scnout)->_w -= msg_count;
246 ((struct __FILE_public *)scnout)->_p += msg_count;
248 state = m_getfld( state, name, scnout->_ptr, -(scnout->_cnt), inb );
249 scnout->_cnt -= msg_count;
250 scnout->_ptr += msg_count;
258 innum ? "??Format error (message %d) in "
259 : "??Format error in ",
260 outnum ? outnum : innum);
261 fprintf (stderr, "component %d\n", compnum);
264 FPUTS ("\n\nBAD MSG:\n");
266 if (putc ('\n', scnout) == EOF) DIEWRERR();
276 adios (NULL, "getfld() returned %d", state);
281 * format and output the scan line.
285 advise("read", "unable to"); /* "read error" */
289 /* Save and restore buffer so we don't trash our dynamic pool! */
291 saved_c_text = bodycomp->c_text;
292 bodycomp->c_text = tmpbuf;
299 dat[2] = ftell(scnout);
300 if (dat[2] == EOF) DIEWRERR();
303 if ((datecomp && !datecomp->c_text) || (!size && !outnum)) {
306 fstat (fileno(inb), &st);
307 if (!size && !outnum)
310 if (! datecomp->c_text) {
311 if (datecomp->c_tws == NULL)
312 datecomp->c_tws = (struct tws *)
313 calloc((size_t) 1, sizeof(*datecomp->c_tws));
314 if (datecomp->c_tws == NULL)
315 adios (NULL, "unable to allocate tws buffer");
316 *datecomp->c_tws = *dlocaltime ((time_t *) &st.st_mtime);
317 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
319 datecomp->c_flags &= ~CF_DATEFAB;
324 fmt_scan (fmt, scanl, slwidth, dat);
327 fmt = fmt_scan (fmt, scanl, slwidth, dat);
329 fmt = fmt_top; /* reset for old format files */
333 bodycomp->c_text = saved_c_text;
336 fputs (scanl, stdout);
338 FINDCOMP (cptr, "encrypted");
339 encrypted = cptr && cptr->c_text;
341 /* return dynamically allocated buffers to pool */
342 while ((cptr = *savecomp++)) {
343 *--nxtbuf = cptr->c_text;
348 if (outnum && (ferror(scnout) || fclose (scnout) == EOF))
351 return (state != FILEEOF ? SCNERR : encrypted ? SCNENC : SCNMSG);
356 * Cheat: we are loaded with adrparse, which wants a routine called
357 * OfficialName(). We call adrparse:getm() with the correct arguments
358 * to prevent OfficialName() from being called. Hence, the following
359 * is to keep the loader happy.
362 OfficialName (char *name)
369 mh_fputs(char *s, FILE *stream)
374 if (putc (c,stream) == EOF )