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
37 static struct format *fmt;
39 static struct comp *datecomp; /* pntr to "date" comp */
40 static int ncomps = 0; /* # of interesting components */
41 static char **compbuffers = 0; /* buffers for component text */
42 static struct comp **used_buf = 0; /* stack for comp that use buffers */
44 static int dat[5]; /* aux. data for format routine */
46 char *scanl = 0; /* text of most recent scanline */
48 #define DIEWRERR() adios(scnmsg, "write error on")
51 if (mh_fputs(buf,scnout) == EOF)\
58 int sc_width(void); /* from termsbr.c */
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;
75 struct comp **savecomp;
82 char returnpath[BUFSIZ];
83 char deliverydate[BUFSIZ];
86 /* first-time only initialization */
89 if ((width = sc_width()) < WIDTH/2)
91 else if (width > MAXSCANL)
94 dat[3] = slwidth = width;
95 scanl = (char *) mh_xmalloc((size_t) SCAN_CHARWIDTH *
100 /* Compile format string */
101 ncomps = fmt_compile(nfs, &fmt) + 1;
103 FINDCOMP(datecomp, "date");
104 FINDCOMP(cptr, "folder");
106 cptr->c_text = folder;
107 FINDCOMP(cptr, "encrypted");
109 if ((cptr = (struct comp *)calloc(1, sizeof(*cptr)))) {
110 cptr->c_name = "encrypted";
111 cptr->c_next = wantcomp[i=CHASH(cptr->c_name)];
115 FINDCOMP(cptr, "dtimenow");
117 cptr->c_text = getcpy(dtimenow(0));
118 nxtbuf = compbuffers = (char **) calloc((size_t) ncomps,
121 adios(NULL, "unable to allocate component buffers");
122 used_buf = (struct comp **) calloc((size_t) (ncomps+1),
123 sizeof(struct comp *));
124 if (used_buf == NULL)
125 adios(NULL, "unable to allocate component buffer stack");
126 used_buf += ncomps+1; *--used_buf = 0;
127 for (i = ncomps; i--; )
128 *nxtbuf++ = mh_xmalloc(SBUFSIZ);
132 ** each-message initialization
134 nxtbuf = compbuffers;
137 dat[0] = innum ? innum : outnum;
142 ** Get the first field. If the message is non-empty
143 ** and we're doing an "inc", open the output file.
145 if ((state = m_getfld(FLD, name, tmpbuf, SBUFSIZ, inb)) == FILEEOF) {
147 advise("read", "unable to"); /* "read error" */
156 scnmsg = m_name(outnum);
157 if (*scnmsg == '?') /* msg num out of range */
160 scnmsg = "/dev/null";
162 if ((scnout = fopen(scnmsg, "w")) == NULL)
163 adios(scnmsg, "unable to write");
166 ** Add the Return-Path and Delivery-Date
167 ** header fields to message.
169 if (get_returnpath(returnpath, sizeof(returnpath),
170 deliverydate, sizeof(deliverydate))) {
171 FPUTS("Return-Path: ");
173 FPUTS("Delivery-Date: ");
179 /* scan - main loop */
181 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb)) {
188 if (putc(':', scnout) == EOF)
193 ** if we're interested in this component, save
194 ** a pointer to the component text, then start
195 ** using our next free buffer as the component
196 ** temp buffer (buffer switching saves an extra
197 ** copy of the component text).
199 if ((cptr = wantcomp[CHASH(name)])) {
201 if (!mh_strcasecmp(name, cptr->c_name)) {
202 if (! cptr->c_text) {
203 cptr->c_text = tmpbuf;
204 for (cp = tmpbuf + strlen(tmpbuf) - 1; cp >= tmpbuf; cp--)
214 } while ((cptr = cptr->c_next));
217 while (state == FLDPLUS) {
218 state = m_getfld(state, name, tmpbuf, SBUFSIZ,
231 fprintf(stderr, innum ? "??Format error (message %d) in " : "??Format error in ", outnum ? outnum : innum);
232 fprintf(stderr, "component %d\n", compnum);
235 FPUTS("\n\nBAD MSG:\n");
237 if (putc('\n', scnout) == EOF)
246 adios(NULL, "getfld() returned %d", state);
251 ** format and output the scan line.
255 advise("read", "unable to"); /* "read error" */
261 else if (outnum > 0) {
262 dat[2] = ftell(scnout);
267 if ((datecomp && !datecomp->c_text) || (!size && !outnum)) {
270 fstat(fileno(inb), &st);
271 if (!size && !outnum)
274 if (! datecomp->c_text) {
275 if (datecomp->c_tws == NULL)
276 datecomp->c_tws = (struct tws *) calloc((size_t) 1, sizeof(*datecomp->c_tws));
277 if (datecomp->c_tws == NULL)
278 adios(NULL, "unable to allocate tws buffer");
279 *datecomp->c_tws = *dlocaltime((time_t *) &st.st_mtime);
280 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
282 datecomp->c_flags &= ~CF_DATEFAB;
287 fmt_scan(fmt, scanl, slwidth, dat);
290 fputs(scanl, stdout);
292 FINDCOMP(cptr, "encrypted");
293 encrypted = cptr && cptr->c_text;
295 /* return dynamically allocated buffers to pool */
296 while ((cptr = *savecomp++)) {
297 *--nxtbuf = cptr->c_text;
302 if (outnum && (ferror(scnout) || fclose(scnout) == EOF))
305 return (state != FILEEOF ? SCNERR : encrypted ? SCNENC : SCNMSG);
309 mh_fputs(char *s, FILE *stream)
314 if (putc(c,stream) == EOF )