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 #define SCAN_CHARWIDTH MB_CUR_MAX
64 scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg,
65 int unseen, char *folder, long size, int noisy)
67 int i, compnum, encrypted, state;
68 unsigned char *cp, *tmpbuf;
70 char *saved_c_text = NULL;
72 struct comp **savecomp;
76 static int rlwidth, slwidth;
78 /* first-time only initialization */
81 if ((width = sc_width ()) < WIDTH/2)
83 else if (width > MAXSCANL)
86 dat[3] = slwidth = width;
87 scanl = (char *) mh_xmalloc((size_t) SCAN_CHARWIDTH * (slwidth + 2) );
91 /* Compile format string */
92 ncomps = fmt_compile (nfs, &fmt) + 1;
97 FINDCOMP(bodycomp, "body");
98 FINDCOMP(datecomp, "date");
99 FINDCOMP(cptr, "folder");
101 cptr->c_text = folder;
102 FINDCOMP(cptr, "encrypted");
104 if ((cptr = (struct comp *) calloc (1, sizeof(*cptr)))) {
105 cptr->c_name = "encrypted";
106 cptr->c_next = wantcomp[i = CHASH (cptr->c_name)];
110 FINDCOMP (cptr, "dtimenow");
112 cptr->c_text = getcpy(dtimenow (0));
113 nxtbuf = compbuffers = (char **) calloc((size_t) ncomps, sizeof(char *));
115 adios (NULL, "unable to allocate component buffers");
116 used_buf = (struct comp **) calloc((size_t) (ncomps+1),
117 sizeof(struct comp *));
118 if (used_buf == NULL)
119 adios (NULL, "unable to allocate component buffer stack");
120 used_buf += ncomps+1; *--used_buf = 0;
121 rlwidth = bodycomp && (width > SBUFSIZ) ? width : SBUFSIZ;
122 for (i = ncomps; i--; )
123 *nxtbuf++ = mh_xmalloc(rlwidth);
127 * each-message initialization
129 nxtbuf = compbuffers;
132 dat[0] = innum ? innum : outnum;
137 * Get the first field. If the message is non-empty
138 * and we're doing an "inc", open the output file.
140 if ((state = m_getfld (FLD, name, tmpbuf, rlwidth, inb)) == FILEEOF) {
142 advise("read", "unable to"); /* "read error" */
151 scnmsg = m_name (outnum);
152 if (*scnmsg == '?') /* msg num out of range */
155 scnmsg = "/dev/null";
157 if ((scnout = fopen (scnmsg, "w")) == NULL)
158 adios (scnmsg, "unable to write");
161 /* scan - main loop */
162 for (compnum = 1; ; state = m_getfld (state, name, tmpbuf, rlwidth, inb)) {
169 if ( putc (':', scnout) == EOF) DIEWRERR();
173 * if we're interested in this component, save a pointer
174 * to the component text, then start using our next free
175 * buffer as the component temp buffer (buffer switching
176 * saves an extra copy of the component text).
178 if ((cptr = wantcomp[CHASH(name)])) {
180 if (!mh_strcasecmp(name, cptr->c_name)) {
181 if (! cptr->c_text) {
182 cptr->c_text = tmpbuf;
183 for (cp = tmpbuf + strlen (tmpbuf) - 1;
194 } while ((cptr = cptr->c_next));
197 while (state == FLDPLUS) {
198 state = m_getfld (state, name, tmpbuf, rlwidth, inb);
207 * A slight hack ... if we have less than rlwidth characters
208 * in the buffer, call m_getfld again.
211 if ((i = strlen(tmpbuf)) < rlwidth) {
212 state = m_getfld (state, name, tmpbuf + i,
216 state = FILEEOF; /* stop now if scan cmd */
219 if (putc ('\n', scnout) == EOF) DIEWRERR();
222 * performance hack: some people like to run "inc" on
223 * things like net.sources or large digests. We do a
224 * copy directly into the output buffer rather than
225 * going through an intermediate buffer.
227 * We need the amount of data m_getfld found & don't
228 * want to do a strlen on the long buffer so there's
229 * a hack in m_getfld to save the amount of data it
230 * returned in the global "msg_count".
233 while (state == BODY) {
235 if (scnout->_IO_write_ptr == scnout->_IO_write_end) {
236 #elif defined(__DragonFly__)
237 if (((struct __FILE_public *)scnout)->_w <= 0) {
239 if (scnout->_cnt <= 0) {
241 if (fflush(scnout) == EOF)
245 state = m_getfld(state, name, scnout->_IO_write_ptr,
246 (long)scnout->_IO_write_ptr-(long)scnout->_IO_write_end , inb);
247 scnout->_IO_write_ptr += msg_count;
248 #elif defined(__DragonFly__)
249 state = m_getfld( state, name, ((struct __FILE_public *)scnout)->_p, -(((struct __FILE_public *)scnout)->_w), inb );
250 ((struct __FILE_public *)scnout)->_w -= msg_count;
251 ((struct __FILE_public *)scnout)->_p += msg_count;
253 state = m_getfld( state, name, scnout->_ptr, -(scnout->_cnt), inb );
254 scnout->_cnt -= msg_count;
255 scnout->_ptr += msg_count;
263 innum ? "??Format error (message %d) in "
264 : "??Format error in ",
265 outnum ? outnum : innum);
266 fprintf (stderr, "component %d\n", compnum);
269 FPUTS ("\n\nBAD MSG:\n");
271 if (putc ('\n', scnout) == EOF) DIEWRERR();
281 adios (NULL, "getfld() returned %d", state);
286 * format and output the scan line.
290 advise("read", "unable to"); /* "read error" */
294 /* Save and restore buffer so we don't trash our dynamic pool! */
296 saved_c_text = bodycomp->c_text;
297 bodycomp->c_text = tmpbuf;
304 dat[2] = ftell(scnout);
305 if (dat[2] == EOF) DIEWRERR();
308 if ((datecomp && !datecomp->c_text) || (!size && !outnum)) {
311 fstat (fileno(inb), &st);
312 if (!size && !outnum)
315 if (! datecomp->c_text) {
316 if (datecomp->c_tws == NULL)
317 datecomp->c_tws = (struct tws *)
318 calloc((size_t) 1, sizeof(*datecomp->c_tws));
319 if (datecomp->c_tws == NULL)
320 adios (NULL, "unable to allocate tws buffer");
321 *datecomp->c_tws = *dlocaltime ((time_t *) &st.st_mtime);
322 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
324 datecomp->c_flags &= ~CF_DATEFAB;
329 fmt_scan (fmt, scanl, slwidth, dat);
332 fmt = fmt_scan (fmt, scanl, slwidth, dat);
334 fmt = fmt_top; /* reset for old format files */
338 bodycomp->c_text = saved_c_text;
341 fputs (scanl, stdout);
343 FINDCOMP (cptr, "encrypted");
344 encrypted = cptr && cptr->c_text;
346 /* return dynamically allocated buffers to pool */
347 while ((cptr = *savecomp++)) {
348 *--nxtbuf = cptr->c_text;
353 if (outnum && (ferror(scnout) || fclose (scnout) == EOF))
356 return (state != FILEEOF ? SCNERR : encrypted ? SCNENC : SCNMSG);
361 * Cheat: we are loaded with adrparse, which wants a routine called
362 * OfficialName(). We call adrparse:getm() with the correct arguments
363 * to prevent OfficialName() from being called. Hence, the following
364 * is to keep the loader happy.
367 OfficialName (char *name)
374 mh_fputs(char *s, FILE *stream)
379 if (putc (c,stream) == EOF )