]>
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 (mh_fputs(buf,scnout) == EOF)\
48 static int mh_fputs(char *, FILE *);
51 scan (FILE *inb
, int innum
, int outnum
, char *nfs
, int width
, int curflg
,
52 int unseen
, char *folder
, long size
, int noisy
, charstring_t
*scanl
)
54 int i
, compnum
, encrypted
, state
;
55 char *cp
, *tmpbuf
, *startbody
, **nxtbuf
;
56 char *saved_c_text
= NULL
;
58 struct comp
**savecomp
;
63 static int rlwidth
, slwidth
;
65 /* first-time only initialization, which will always happen the
66 way the code is now, with callers initializing *scanl to NULL.
67 scanl used to be a global. */
70 /* Default: width of the terminal, but at least WIDTH/2. */
71 if ((width
= sc_width ()) < WIDTH
/2)
73 } else if (width
== 0) {
74 /* Unlimited width. */
77 dat
[3] = slwidth
= width
;
78 *scanl
= charstring_create (width
< NMH_BUFSIZ
? width
: NMH_BUFSIZ
);
82 /* Compile format string */
83 ncomps
= fmt_compile (nfs
, &fmt
, 1) + 2;
85 bodycomp
= fmt_findcomp("body");
86 datecomp
= fmt_findcomp("date");
87 cptr
= fmt_findcomp("folder");
89 cptr
->c_text
= getcpy(folder
);
90 if (fmt_addcompentry("encrypted")) {
93 cptr
= fmt_findcomp("dtimenow");
95 cptr
->c_text
= getcpy(dtimenow (0));
98 * In other programs I got rid of this complicated buffer switching,
99 * but since scan reads lots of messages at once and this complicated
100 * memory management, I decided to keep it; otherwise there was
101 * the potential for a lot of malloc() and free()s, and I could
102 * see the malloc() pool really getting fragmented. Maybe it
103 * wouldn't be an issue in practice; perhaps this will get
106 * So, some notes for what's going on:
108 * nxtbuf is an array of pointers that contains malloc()'d buffers
109 * to hold our component text. used_buf is an array of struct comp
110 * pointers that holds pointers to component structures we found while
111 * processing a message.
113 * We read in the message with m_getfld(), using "tmpbuf" as our
114 * input buffer. tmpbuf is set at the start of message processing
115 * to the first buffer in our buffer pool (nxtbuf).
117 * Every time we find a component we care about, we set that component's
118 * text buffer to the current value of tmpbuf, and then switch tmpbuf
119 * to the next buffer in our pool. We also add that component to
122 * When we're done, we go back and zero out all of the component
123 * text buffer pointers that we saved in used_buf.
125 * Note that this means c_text memory is NOT owned by the fmt_module
126 * and it's our responsibility to free it.
129 nxtbuf
= compbuffers
= mh_xcalloc(ncomps
, sizeof *nxtbuf
);
130 used_buf
= mh_xcalloc(ncomps
+ 1, sizeof *used_buf
);
131 used_buf
+= ncomps
+1; *--used_buf
= 0;
132 rlwidth
= bodycomp
&& (width
> SBUFSIZ
)
133 ? min (width
, NMH_BUFSIZ
)
135 for (i
= ncomps
; i
--; )
136 *nxtbuf
++ = mh_xmalloc(rlwidth
);
140 * each-message initialization
142 nxtbuf
= compbuffers
;
146 dat
[0] = innum
? innum
: outnum
;
151 * Get the first field. If the message is non-empty
152 * and we're doing an "inc", open the output file.
155 m_getfld_state_reset (&gstate
);
156 if ((state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
)) == FILEEOF
) {
158 advise("read", "unable to"); /* "read error" */
167 scnmsg
= m_name (outnum
);
168 if (*scnmsg
== '?') /* msg num out of range */
171 scnmsg
= "/dev/null";
173 if ((scnout
= fopen (scnmsg
, "w")) == NULL
)
174 adios (scnmsg
, "unable to write");
177 /* scan - main loop */
179 bufsz
= rlwidth
, state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
)) {
186 if ( putc (':', scnout
) == EOF
) DIEWRERR();
190 * if we're interested in this component, save a pointer
191 * to the component text, then start using our next free
192 * buffer as the component temp buffer (buffer switching
193 * saves an extra copy of the component text).
195 if ((cptr
= fmt_findcasecomp(name
))) {
196 if (! cptr
->c_text
) {
197 cptr
->c_text
= tmpbuf
;
198 for (cp
= tmpbuf
+ strlen (tmpbuf
) - 1;
200 if (isspace ((unsigned char) *cp
))
209 while (state
== FLDPLUS
) {
211 state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
);
219 * A slight hack ... if we have less than rlwidth characters
220 * in the buffer, call m_getfld again.
223 if ((i
= strlen(tmpbuf
)) < rlwidth
) {
225 state
= m_getfld (&gstate
, name
, tmpbuf
+ i
, &bufsz
, inb
);
229 state
= FILEEOF
; /* stop now if scan cmd */
230 if (bodycomp
&& startbody
== NULL
)
234 if (putc ('\n', scnout
) == EOF
) DIEWRERR();
237 * The previous code here used to call m_getfld() using
238 * pointers to the underlying output stdio buffers to
239 * avoid the extra copy. Tests by Markus Schnalke show
240 * no noticeable performance loss on larger mailboxes
241 * if we incur an extra copy, and messing around with
242 * internal stdio buffers is becoming more and more
243 * unportable as times go on. So from now on just deal
244 * with the overhead of an extra copy.
246 * Subtle change - with the previous code tmpbuf wasn't
247 * used, so we could reuse it for the {body} component.
248 * Now since we're using tmpbuf as our read buffer we
249 * need to save the beginning of the body for later.
250 * See the above (and below) use of startbody.
253 if (bodycomp
&& startbody
== NULL
) {
258 while (state
== BODY
) {
260 state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
);
268 fprintf (stderr
, "??Format error (message %d) in ",
269 outnum
? outnum
: innum
);
271 fprintf (stderr
, "??Format error in ");
273 fprintf (stderr
, "component %d\n", compnum
);
276 FPUTS ("\n\nBAD MSG:\n");
278 if (putc ('\n', scnout
) == EOF
) DIEWRERR();
288 adios (NULL
, "getfld() returned %d", state
);
293 * format and output the scan line.
297 advise("read", "unable to"); /* "read error" */
301 /* Save and restore buffer so we don't trash our dynamic pool! */
303 saved_c_text
= bodycomp
->c_text
;
304 bodycomp
->c_text
= startbody
;
311 dat
[2] = ftell(scnout
);
312 if (dat
[2] == EOF
) DIEWRERR();
315 if ((datecomp
&& !datecomp
->c_text
) || (!size
&& !outnum
)) {
318 fstat (fileno(inb
), &st
);
319 if (!size
&& !outnum
)
322 if (! datecomp
->c_text
) {
323 if (datecomp
->c_tws
== NULL
)
324 NEW0(datecomp
->c_tws
);
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
, NULL
);
336 bodycomp
->c_text
= saved_c_text
;
339 fputs (charstring_buffer (*scanl
), stdout
);
341 cptr
= fmt_findcomp ("encrypted");
342 encrypted
= cptr
&& cptr
->c_text
;
344 /* return dynamically allocated buffers to pool */
345 while ((cptr
= *savecomp
++)) {
349 if (outnum
&& (ferror(scnout
) || fclose (scnout
) == EOF
))
352 return (state
!= FILEEOF
? SCNERR
: encrypted
? SCNENC
: SCNMSG
);
357 mh_fputs(char *s
, FILE *stream
)
362 if (putc (c
,stream
) == EOF
)
367 /* The following two functions allow access to the global gstate above. */
370 m_getfld_state_destroy (&gstate
);
374 scan_detect_mbox_style (FILE *f
) {
375 m_unknown (&gstate
, f
);