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 struct comp *bodycomp; /* pntr to "body" pseudo-comp (if referenced) */
43 static int ncomps = 0; /* # of interesting components */
44 static char **compbuffers = 0; /* buffers for component text */
45 static struct comp **used_buf = 0; /* stack for comp that use buffers */
47 static int dat[5]; /* aux. data for format routine */
49 char *scanl = 0; /* text of most recent scanline */
51 #define DIEWRERR() adios(scnmsg, "write error on")
54 if (mh_fputs(buf,scnout) == EOF)\
61 int sc_width(void); /* from termsbr.c */
62 static int mh_fputs(char *, FILE *);
64 #ifdef MULTIBYTE_SUPPORT
65 #define SCAN_CHARWIDTH MB_CUR_MAX
67 #define SCAN_CHARWIDTH 1
71 scan(FILE *inb, int innum, int outnum, char *nfs, int width, int curflg,
72 int unseen, char *folder, long size, int noisy)
74 int i, compnum, encrypted, state;
75 unsigned char *cp, *tmpbuf;
77 char *saved_c_text = NULL;
79 struct comp **savecomp;
83 static int rlwidth, slwidth;
86 char returnpath[BUFSIZ];
87 char deliverydate[BUFSIZ];
90 /* first-time only initialization */
93 if ((width = sc_width()) < WIDTH/2)
95 else if (width > MAXSCANL)
98 dat[3] = slwidth = width;
99 scanl = (char *) mh_xmalloc((size_t) SCAN_CHARWIDTH *
104 /* Compile format string */
105 ncomps = fmt_compile(nfs, &fmt) + 1;
107 FINDCOMP(bodycomp, "body");
108 FINDCOMP(datecomp, "date");
109 FINDCOMP(cptr, "folder");
111 cptr->c_text = folder;
112 FINDCOMP(cptr, "encrypted");
114 if ((cptr = (struct comp *)calloc(1, sizeof(*cptr)))) {
115 cptr->c_name = "encrypted";
116 cptr->c_next = wantcomp[i = CHASH(cptr->c_name)];
120 FINDCOMP(cptr, "dtimenow");
122 cptr->c_text = getcpy(dtimenow(0));
123 nxtbuf = compbuffers = (char **) calloc((size_t) ncomps,
126 adios(NULL, "unable to allocate component buffers");
127 used_buf = (struct comp **) calloc((size_t) (ncomps+1),
128 sizeof(struct comp *));
129 if (used_buf == NULL)
130 adios(NULL, "unable to allocate component buffer stack");
131 used_buf += ncomps+1; *--used_buf = 0;
132 rlwidth = bodycomp && (width > SBUFSIZ) ? width : SBUFSIZ;
133 for (i = ncomps; i--; )
134 *nxtbuf++ = mh_xmalloc(rlwidth);
138 ** each-message initialization
140 nxtbuf = compbuffers;
143 dat[0] = innum ? innum : outnum;
148 ** Get the first field. If the message is non-empty
149 ** and we're doing an "inc", open the output file.
151 if ((state = m_getfld(FLD, name, tmpbuf, rlwidth, inb)) == FILEEOF) {
153 advise("read", "unable to"); /* "read error" */
162 scnmsg = m_name(outnum);
163 if (*scnmsg == '?') /* msg num out of range */
166 scnmsg = "/dev/null";
168 if ((scnout = fopen(scnmsg, "w")) == NULL)
169 adios(scnmsg, "unable to write");
172 ** Add the Return-Path and Delivery-Date
173 ** header fields to message.
175 if (get_returnpath(returnpath, sizeof(returnpath),
176 deliverydate, sizeof(deliverydate))) {
177 FPUTS("Return-Path: ");
179 FPUTS("Delivery-Date: ");
185 /* scan - main loop */
187 state = m_getfld(state, name, tmpbuf, rlwidth, inb)) {
194 if (putc(':', scnout) == EOF)
199 ** if we're interested in this component, save
200 ** a pointer to the component text, then start
201 ** using our next free buffer as the component
202 ** temp buffer (buffer switching saves an extra
203 ** copy of the component text).
205 if ((cptr = wantcomp[CHASH(name)])) {
207 if (!mh_strcasecmp(name, cptr->c_name)) {
208 if (! cptr->c_text) {
209 cptr->c_text = tmpbuf;
210 for (cp = tmpbuf + strlen(tmpbuf) - 1; cp >= tmpbuf; cp--)
220 } while ((cptr = cptr->c_next));
223 while (state == FLDPLUS) {
224 state = m_getfld(state, name, tmpbuf, rlwidth,
234 state = FILEEOF; /* stop now if scan cmd */
237 if (putc('\n', scnout) == EOF) DIEWRERR();
240 ** performance hack: some people like to run "inc"
241 ** on things like net.sources or large digests.
242 ** We do a copy directly into the output buffer
243 ** rather than going through an intermediate buffer.
245 ** We need the amount of data m_getfld found &
246 ** don't want to do a strlen on the long buffer so
247 ** there's a hack in m_getfld to save the amount
248 ** of data it returned in the global "msg_count".
251 while (state == BODY) {
253 if (scnout->_IO_write_ptr == scnout->_IO_write_end) {
254 #elif defined(__DragonFly__)
255 if (((struct __FILE_public *)scnout)->_w <= 0) {
257 if (scnout->_cnt <= 0) {
259 if (fflush(scnout) == EOF)
263 state = m_getfld(state, name, scnout->_IO_write_ptr, (long)scnout->_IO_write_ptr-(long)scnout->_IO_write_end , inb);
264 scnout->_IO_write_ptr += msg_count;
265 #elif defined(__DragonFly__)
266 state = m_getfld(state, name, ((struct __FILE_public *)scnout)->_p, -(((struct __FILE_public *)scnout)->_w), inb);
267 ((struct __FILE_public *)scnout)->_w -= msg_count;
268 ((struct __FILE_public *)scnout)->_p += msg_count;
270 state = m_getfld( state, name, scnout->_ptr, -(scnout->_cnt), inb);
271 scnout->_cnt -= msg_count;
272 scnout->_ptr += msg_count;
279 fprintf(stderr, innum ? "??Format error (message %d) in " : "??Format error in ", outnum ? outnum : innum);
280 fprintf(stderr, "component %d\n", compnum);
283 FPUTS("\n\nBAD MSG:\n");
285 if (putc('\n', scnout) == EOF)
296 adios(NULL, "getfld() returned %d", state);
301 ** format and output the scan line.
305 advise("read", "unable to"); /* "read error" */
309 /* Save and restore buffer so we don't trash our dynamic pool! */
311 saved_c_text = bodycomp->c_text;
312 bodycomp->c_text = tmpbuf;
317 else if (outnum > 0) {
318 dat[2] = ftell(scnout);
323 if ((datecomp && !datecomp->c_text) || (!size && !outnum)) {
326 fstat(fileno(inb), &st);
327 if (!size && !outnum)
330 if (! datecomp->c_text) {
331 if (datecomp->c_tws == NULL)
332 datecomp->c_tws = (struct tws *) calloc((size_t) 1, sizeof(*datecomp->c_tws));
333 if (datecomp->c_tws == NULL)
334 adios(NULL, "unable to allocate tws buffer");
335 *datecomp->c_tws = *dlocaltime((time_t *) &st.st_mtime);
336 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
338 datecomp->c_flags &= ~CF_DATEFAB;
343 fmt_scan(fmt, scanl, slwidth, dat);
346 bodycomp->c_text = saved_c_text;
349 fputs(scanl, stdout);
351 FINDCOMP(cptr, "encrypted");
352 encrypted = cptr && cptr->c_text;
354 /* return dynamically allocated buffers to pool */
355 while ((cptr = *savecomp++)) {
356 *--nxtbuf = cptr->c_text;
361 if (outnum && (ferror(scnout) || fclose(scnout) == EOF))
364 return (state != FILEEOF ? SCNERR : encrypted ? SCNENC : SCNMSG);
368 mh_fputs(char *s, FILE *stream)
373 if (putc(c,stream) == EOF )