]>
diplodocus.org Git - nmh/blob - uip/scansbr.c
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 * Buffer size for content part of header fields. We want this
19 * to be large enough so that we don't do a lot of extra FLDPLUS
20 * calls on m_getfld but small enough so that we don't snarf
21 * the entire message body when we're only going to display 30
26 static struct format
*fmt
;
27 static struct comp
*datecomp
; /* pntr to "date" comp */
28 static struct comp
*bodycomp
; /* pntr to "body" pseudo-comp *
30 static int ncomps
= 0; /* # of interesting components */
31 static char **compbuffers
= 0; /* buffers for component text */
32 static struct comp
**used_buf
= 0; /* stack for comp that use buffers */
34 static int dat
[5]; /* aux. data for format routine */
36 static m_getfld_state_t gstate
; /* for accessor functions below */
38 #define DIEWRERR() adios (scnmsg, "write error on")
41 if (fputs(buf,scnout) == EOF)\
46 scan (FILE *inb
, int innum
, int outnum
, char *nfs
, int width
, int curflg
,
47 int unseen
, char *folder
, long size
, int noisy
, charstring_t
*scanl
)
49 int i
, compnum
, encrypted
, state
;
50 char *cp
, *tmpbuf
, *startbody
, **nxtbuf
;
51 char *saved_c_text
= NULL
;
53 struct comp
**savecomp
;
58 static int rlwidth
, slwidth
;
60 /* first-time only initialization, which will always happen the
61 way the code is now, with callers initializing *scanl to NULL.
62 scanl used to be a global. */
65 /* Default: width of the terminal, but at least WIDTH/2. */
66 if ((width
= sc_width ()) < WIDTH
/2)
68 } else if (width
== 0) {
69 /* Unlimited width. */
72 dat
[3] = slwidth
= width
;
73 *scanl
= charstring_create (width
< NMH_BUFSIZ
? width
: NMH_BUFSIZ
);
77 /* Compile format string */
78 ncomps
= fmt_compile (nfs
, &fmt
, 1) + 2;
80 bodycomp
= fmt_findcomp("body");
81 datecomp
= fmt_findcomp("date");
82 cptr
= fmt_findcomp("folder");
84 cptr
->c_text
= mh_xstrdup(folder
);
85 if (fmt_addcompentry("encrypted")) {
88 cptr
= fmt_findcomp("dtimenow");
90 cptr
->c_text
= getcpy(dtimenow (0));
93 * In other programs I got rid of this complicated buffer switching,
94 * but since scan reads lots of messages at once and this complicated
95 * memory management, I decided to keep it; otherwise there was
96 * the potential for a lot of malloc() and free()s, and I could
97 * see the malloc() pool really getting fragmented. Maybe it
98 * wouldn't be an issue in practice; perhaps this will get
101 * So, some notes for what's going on:
103 * nxtbuf is an array of pointers that contains malloc()'d buffers
104 * to hold our component text. used_buf is an array of struct comp
105 * pointers that holds pointers to component structures we found while
106 * processing a message.
108 * We read in the message with m_getfld(), using "tmpbuf" as our
109 * input buffer. tmpbuf is set at the start of message processing
110 * to the first buffer in our buffer pool (nxtbuf).
112 * Every time we find a component we care about, we set that component's
113 * text buffer to the current value of tmpbuf, and then switch tmpbuf
114 * to the next buffer in our pool. We also add that component to
117 * When we're done, we go back and zero out all of the component
118 * text buffer pointers that we saved in used_buf.
120 * Note that this means c_text memory is NOT owned by the fmt_module
121 * and it's our responsibility to free it.
124 nxtbuf
= compbuffers
= mh_xcalloc(ncomps
, sizeof *nxtbuf
);
125 used_buf
= mh_xcalloc(ncomps
+ 1, sizeof *used_buf
);
126 used_buf
+= ncomps
+1; *--used_buf
= 0;
127 rlwidth
= bodycomp
&& (width
> SBUFSIZ
)
128 ? min (width
, NMH_BUFSIZ
)
130 for (i
= ncomps
; i
--; )
131 *nxtbuf
++ = mh_xmalloc(rlwidth
);
135 * each-message initialization
137 nxtbuf
= compbuffers
;
141 dat
[0] = innum
? innum
: outnum
;
146 * Get the first field. If the message is non-empty
147 * and we're doing an "inc", open the output file.
150 m_getfld_state_reset (&gstate
);
151 if ((state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
)) == FILEEOF
) {
153 advise("read", "unable to"); /* "read error" */
161 scnmsg
= m_name (outnum
);
162 if (*scnmsg
== '?') /* msg num out of range */
165 scnmsg
= "/dev/null";
167 if ((scnout
= fopen (scnmsg
, "w")) == NULL
)
168 adios (scnmsg
, "unable to write");
171 /* scan - main loop */
173 bufsz
= rlwidth
, state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
)) {
180 if ( putc (':', scnout
) == EOF
) DIEWRERR();
184 * if we're interested in this component, save a pointer
185 * to the component text, then start using our next free
186 * buffer as the component temp buffer (buffer switching
187 * saves an extra copy of the component text).
189 if ((cptr
= fmt_findcasecomp(name
))) {
190 if (! cptr
->c_text
) {
191 cptr
->c_text
= tmpbuf
;
192 for (cp
= tmpbuf
+ strlen (tmpbuf
) - 1;
194 if (isspace ((unsigned char) *cp
))
203 while (state
== FLDPLUS
) {
205 state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
);
213 * A slight hack ... if we have less than rlwidth characters
214 * in the buffer, call m_getfld again.
217 if ((i
= strlen(tmpbuf
)) < rlwidth
) {
219 state
= m_getfld (&gstate
, name
, tmpbuf
+ i
, &bufsz
, inb
);
223 state
= FILEEOF
; /* stop now if scan cmd */
224 if (bodycomp
&& startbody
== NULL
)
228 if (putc ('\n', scnout
) == EOF
) DIEWRERR();
231 * The previous code here used to call m_getfld() using
232 * pointers to the underlying output stdio buffers to
233 * avoid the extra copy. Tests by Markus Schnalke show
234 * no noticeable performance loss on larger mailboxes
235 * if we incur an extra copy, and messing around with
236 * internal stdio buffers is becoming more and more
237 * unportable as times go on. So from now on just deal
238 * with the overhead of an extra copy.
240 * Subtle change - with the previous code tmpbuf wasn't
241 * used, so we could reuse it for the {body} component.
242 * Now since we're using tmpbuf as our read buffer we
243 * need to save the beginning of the body for later.
244 * See the above (and below) use of startbody.
247 if (bodycomp
&& startbody
== NULL
) {
252 while (state
== BODY
) {
254 state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
);
262 fprintf (stderr
, "??Format error (message %d) in ",
263 outnum
? outnum
: innum
);
265 fprintf (stderr
, "??Format error in ");
267 fprintf (stderr
, "component %d\n", compnum
);
270 FPUTS ("\n\nBAD MSG:\n");
272 if (putc ('\n', scnout
) == EOF
) DIEWRERR();
282 adios (NULL
, "getfld() returned %d", state
);
287 * format and output the scan line.
291 advise("read", "unable to"); /* "read error" */
295 /* Save and restore buffer so we don't trash our dynamic pool! */
297 saved_c_text
= bodycomp
->c_text
;
298 bodycomp
->c_text
= startbody
;
305 dat
[2] = ftell(scnout
);
306 if (dat
[2] == EOF
) DIEWRERR();
309 if ((datecomp
&& !datecomp
->c_text
) || (!size
&& !outnum
)) {
312 fstat (fileno(inb
), &st
);
313 if (!size
&& !outnum
)
316 if (! datecomp
->c_text
) {
317 if (datecomp
->c_tws
== NULL
)
318 NEW0(datecomp
->c_tws
);
319 *datecomp
->c_tws
= *dlocaltime ((time_t *) &st
.st_mtime
);
320 datecomp
->c_flags
|= CF_DATEFAB
|CF_TRUE
;
322 datecomp
->c_flags
&= ~CF_DATEFAB
;
327 fmt_scan (fmt
, *scanl
, slwidth
, dat
, NULL
);
330 bodycomp
->c_text
= saved_c_text
;
333 fputs (charstring_buffer (*scanl
), stdout
);
335 cptr
= fmt_findcomp ("encrypted");
336 encrypted
= cptr
&& cptr
->c_text
;
338 /* return dynamically allocated buffers to pool */
339 while ((cptr
= *savecomp
++)) {
343 if (outnum
&& (ferror(scnout
) || fclose (scnout
) == EOF
))
346 return (state
!= FILEEOF
? SCNERR
: encrypted
? SCNENC
: SCNMSG
);
350 /* The following two functions allow access to the global gstate above. */
353 m_getfld_state_destroy (&gstate
);
357 scan_detect_mbox_style (FILE *f
) {
358 m_unknown (&gstate
, f
);