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>
19 #define MAXSCANL 256 /* longest possible scan line */
21 static struct format *fmt;
23 static struct comp *datecomp; /* pntr to "date" comp */
24 static int ncomps = 0; /* # of interesting components */
26 static int dat[5]; /* aux. data for format routine */
28 char *scanl = NULL; /* text of most recent scanline */
31 if (fputs(buf, scnout) == EOF)\
32 adios(EX_IOERR, scnmsg, "write error on");\
38 int sc_width(void); /* from termsbr.c */
40 #ifdef MULTIBYTE_SUPPORT
41 #define SCAN_CHARWIDTH MB_CUR_MAX
43 #define SCAN_CHARWIDTH 1
47 scan(FILE *inb, int innum, int outnum, char *fmtstr, int width, int curflg,
53 struct field f = {{0}};
58 int incing = (outnum != SCN_MBOX && outnum != SCN_FOLD);
59 int scanfolder = (outnum == SCN_FOLD);
64 /* first-time only initialization */
71 if ((width = sc_width()) < WIDTH/2)
73 else if (width > MAXSCANL)
76 dat[3] = slwidth = width;
77 scanl = mh_xcalloc(slwidth + 2, SCAN_CHARWIDTH); /* probably for \n and \0 */
78 /* Compile format string */
79 ncomps = fmt_compile(fmtstr, &fmt) + 1;
80 FINDCOMP(datecomp, "date");
92 ** each-message initialization
94 dat[0] = innum ? innum : outnum;
100 scnmsg = m_name(outnum);
101 if (*scnmsg == '?') /* msg num out of range */
103 if (!(scnout = fopen(scnmsg, "w")))
104 adios(EX_IOERR, scnmsg, "unable to write");
106 /* scan - main loop */
107 for (compnum = 1, state = FLD2; ; ) {
108 state = m_getfld2(state, &f, inb);
120 if (fmtstr && (cptr = wantcomp[CHASH(f.name)])) {
122 ** we're interested in this component,
123 ** but find the right one in the hash
124 ** collision chain ...
127 if (mh_strcasecmp(f.name, cptr->c_name)!=0) {
134 cptr->c_text = mh_xstrdup(f.value);
135 cp = cptr->c_text + strlen(cptr->c_text) - 1;
136 for (; cp >= cptr->c_text; cp--) {
144 } while ((cptr = cptr->c_next));
151 /* stop here if we scan a msg in a folder */
155 /* otherwise (mbox): snarf the body */
156 if (strncmp("From ", f.value, 5)==0) {
166 while ((state = m_getfld2(state, &f, inb)) == BODY2) {
168 ** recognize From lines without blank lines
169 ** before them as well.
171 if (strncmp("From ", f.value, 5)==0) {
176 ** delay the printing of blank lines
177 ** because if it's the end of the message,
178 ** then we must omit the blank line,
179 ** as it is not part of the message but
180 ** part of the mbox format
183 /* print the delayed blank line */
187 if (strcmp(f.value, "\n")==0) {
198 if (strncmp("From ", f.value, 5)==0) {
203 fprintf(stderr, innum ?
204 "??Format error (message %d) in " :
205 "??Format error in ",
206 outnum ? outnum : innum);
207 fprintf(stderr, "component %d\n", compnum);
211 fprintf(stderr, innum ?
212 "??Format error (message %d) in " :
213 "??Format error in ",
214 outnum ? outnum : innum);
215 fprintf(stderr, "component %d\n", compnum);
218 FPUTS("\n\nBAD MSG:\n");
219 FPUTS(f.name); /* XXX use f.field? */
224 /* fall through if we scan only */
230 adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
235 /* Format and output the scan line. */
237 advise("read", "unable to");
242 if ((dat[2] = ftell(scnout)) == EOF)
243 adios(EX_IOERR, scnmsg, "write error on");
244 } else if (!scanfolder) {
245 if ((dat[2] = ftell(inb)) == EOF)
246 adios(EX_IOERR, scnmsg, "write error on");
251 fstat(fileno(inb), &st);
255 if (datecomp && !datecomp->c_text) {
256 if (!datecomp->c_text) {
257 if (!datecomp->c_tws)
258 datecomp->c_tws = mh_xcalloc(1, sizeof(*datecomp->c_tws));
259 if (!datecomp->c_tws)
260 adios(EX_OSERR, NULL, "unable to allocate tws buffer");
261 *datecomp->c_tws = *dlocaltime((time_t *) &st.st_mtime);
262 datecomp->c_flags |= CF_DATEFAB|CF_TRUE;
264 datecomp->c_flags &= ~CF_DATEFAB;
268 /* print the scan line */
269 fmt_scan(fmt, scanl, slwidth, dat);
270 fputs(scanl, stdout);
273 /* clean up old values */
274 for (i=0; i < sizeof(wantcomp)/sizeof(wantcomp[0]); i++) {
275 for (cptr=wantcomp[i]; cptr; cptr=cptr->c_next) {
283 if (incing && (ferror(scnout) || fclose(scnout) == EOF))
284 adios(EX_IOERR, scnmsg, "write error on");
286 return (state == FILEEOF2 ? SCNMSG : SCNERR);