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 * A slight hack ... if we have less than rlwidth characters
212 * in the buffer, call m_getfld again.
215 if ((i = strlen(tmpbuf)) < rlwidth) {
216 state = m_getfld (state, name, tmpbuf + i,
220 state = FILEEOF; /* stop now if scan cmd */
223 if (putc ('\n', scnout) == EOF) DIEWRERR();
226 * performance hack: some people like to run "inc" on
227 * things like net.sources or large digests. We do a
228 * copy directly into the output buffer rather than
229 * going through an intermediate buffer.
231 * We need the amount of data m_getfld found & don't
232 * want to do a strlen on the long buffer so there's
233 * a hack in m_getfld to save the amount of data it
234 * returned in the global "msg_count".
237 while (state == BODY) {
239 if (scnout->_IO_write_ptr == scnout->_IO_write_end) {
240 #elif defined(__DragonFly__)
241 if (((struct __FILE_public *)scnout)->_w <= 0) {
243 if (scnout->_cnt <= 0) {
245 if (fflush(scnout) == EOF)
249 state = m_getfld(state, name, scnout->_IO_write_ptr,
250 (long)scnout->_IO_write_ptr-(long)scnout->_IO_write_end , inb);
251 scnout->_IO_write_ptr += msg_count;
252 #elif defined(__DragonFly__)
253 state = m_getfld( state, name, ((struct __FILE_public *)scnout)->_p, -(((struct __FILE_public *)scnout)->_w), inb );
254 ((struct __FILE_public *)scnout)->_w -= msg_count;
255 ((struct __FILE_public *)scnout)->_p += msg_count;
257 state = m_getfld( state, name, scnout->_ptr, -(scnout->_cnt), inb );
258 scnout->_cnt -= msg_count;
259 scnout->_ptr += msg_count;
267 innum ? "??Format error (message %d) in "
268 : "??Format error in ",
269 outnum ? outnum : innum);
270 fprintf (stderr, "component %d\n", compnum);
273 FPUTS ("\n\nBAD MSG:\n");
275 if (putc ('\n', scnout) == EOF) DIEWRERR();
285 adios (NULL, "getfld() returned %d", state);
290 * format and output the scan line.
294 advise("read", "unable to"); /* "read error" */
298 /* Save and restore buffer so we don't trash our dynamic pool! */
300 saved_c_text = bodycomp->c_text;
301 bodycomp->c_text = tmpbuf;
308 dat[2] = ftell(scnout);
309 if (dat[2] == EOF) DIEWRERR();
312 if ((datecomp && !datecomp->c_text) || (!size && !outnum)) {
315 fstat (fileno(inb), &st);
316 if (!size && !outnum)
319 if (! datecomp->c_text) {
320 if (datecomp->c_tws == NULL)
321 datecomp->c_tws = (struct tws *)
322 calloc((size_t) 1, sizeof(*datecomp->c_tws));
323 if (datecomp->c_tws == NULL)
324 adios (NULL, "unable to allocate tws buffer");
325 *datecomp->c_tws = *dlocaltime ((time_t *) &st.st_mtime);
326 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
328 datecomp->c_flags &= ~CF_DATEFAB;
333 fmt_scan (fmt, scanl, slwidth, dat);
336 fmt = fmt_scan (fmt, scanl, slwidth, dat);
338 fmt = fmt_top; /* reset for old format files */
342 bodycomp->c_text = saved_c_text;
345 fputs (scanl, stdout);
347 FINDCOMP (cptr, "encrypted");
348 encrypted = cptr && cptr->c_text;
350 /* return dynamically allocated buffers to pool */
351 while ((cptr = *savecomp++)) {
352 *--nxtbuf = cptr->c_text;
357 if (outnum && (ferror(scnout) || fclose (scnout) == EOF))
360 return (state != FILEEOF ? SCNERR : encrypted ? SCNENC : SCNMSG);
365 mh_fputs(char *s, FILE *stream)
370 if (putc (c,stream) == EOF )