]>
diplodocus.org Git - nmh/blob - uip/scansbr.c
cd615d1e32b56305ae92c02c596657243d53ede6
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 * Buffer size for content part of header fields. We want this
18 * to be large enough so that we don't do a lot of extra FLDPLUS
19 * calls on m_getfld but small enough so that we don't snarf
20 * the entire message body when we're only going to display 30
25 static struct format
*fmt
;
26 static struct comp
*datecomp
; /* pntr to "date" comp */
27 static struct comp
*bodycomp
; /* pntr to "body" pseudo-comp *
29 static int ncomps
= 0; /* # of interesting components */
30 static char **compbuffers
= 0; /* buffers for component text */
31 static struct comp
**used_buf
= 0; /* stack for comp that use buffers */
33 static int dat
[5]; /* aux. data for format routine */
35 static m_getfld_state_t gstate
; /* for accessor functions below */
37 #define DIEWRERR() adios (scnmsg, "write error on")
40 if (fputs(buf,scnout) == EOF)\
45 scan (FILE *inb
, int innum
, int outnum
, char *nfs
, int width
, int curflg
,
46 int unseen
, char *folder
, long size
, int noisy
, charstring_t
*scanl
)
48 int i
, compnum
, encrypted
, state
;
49 char *cp
, *tmpbuf
, *startbody
, **nxtbuf
;
50 char *saved_c_text
= NULL
;
52 struct comp
**savecomp
;
57 static int rlwidth
, slwidth
;
59 /* first-time only initialization, which will always happen the
60 way the code is now, with callers initializing *scanl to NULL.
61 scanl used to be a global. */
64 /* Default: width of the terminal, but at least WIDTH/2. */
65 if ((width
= sc_width ()) < WIDTH
/2)
67 } else if (width
== 0) {
68 /* Unlimited width. */
71 dat
[3] = slwidth
= width
;
72 *scanl
= charstring_create (width
< NMH_BUFSIZ
? width
: NMH_BUFSIZ
);
76 /* Compile format string */
77 ncomps
= fmt_compile (nfs
, &fmt
, 1) + 2;
79 bodycomp
= fmt_findcomp("body");
80 datecomp
= fmt_findcomp("date");
81 cptr
= fmt_findcomp("folder");
83 cptr
->c_text
= mh_xstrdup(folder
);
84 if (fmt_addcompentry("encrypted")) {
87 cptr
= fmt_findcomp("dtimenow");
89 cptr
->c_text
= getcpy(dtimenow (0));
92 * In other programs I got rid of this complicated buffer switching,
93 * but since scan reads lots of messages at once and this complicated
94 * memory management, I decided to keep it; otherwise there was
95 * the potential for a lot of malloc() and free()s, and I could
96 * see the malloc() pool really getting fragmented. Maybe it
97 * wouldn't be an issue in practice; perhaps this will get
100 * So, some notes for what's going on:
102 * nxtbuf is an array of pointers that contains malloc()'d buffers
103 * to hold our component text. used_buf is an array of struct comp
104 * pointers that holds pointers to component structures we found while
105 * processing a message.
107 * We read in the message with m_getfld(), using "tmpbuf" as our
108 * input buffer. tmpbuf is set at the start of message processing
109 * to the first buffer in our buffer pool (nxtbuf).
111 * Every time we find a component we care about, we set that component's
112 * text buffer to the current value of tmpbuf, and then switch tmpbuf
113 * to the next buffer in our pool. We also add that component to
116 * When we're done, we go back and zero out all of the component
117 * text buffer pointers that we saved in used_buf.
119 * Note that this means c_text memory is NOT owned by the fmt_module
120 * and it's our responsibility to free it.
123 nxtbuf
= compbuffers
= mh_xcalloc(ncomps
, sizeof *nxtbuf
);
124 used_buf
= mh_xcalloc(ncomps
+ 1, sizeof *used_buf
);
125 used_buf
+= ncomps
+1; *--used_buf
= 0;
126 rlwidth
= bodycomp
&& (width
> SBUFSIZ
)
127 ? min (width
, NMH_BUFSIZ
)
129 for (i
= ncomps
; i
--; )
130 *nxtbuf
++ = mh_xmalloc(rlwidth
);
134 * each-message initialization
136 nxtbuf
= compbuffers
;
140 dat
[0] = innum
? innum
: outnum
;
145 * Get the first field. If the message is non-empty
146 * and we're doing an "inc", open the output file.
149 m_getfld_state_reset (&gstate
);
150 if ((state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
)) == FILEEOF
) {
152 advise("read", "unable to"); /* "read error" */
160 scnmsg
= m_name (outnum
);
161 if (*scnmsg
== '?') /* msg num out of range */
164 scnmsg
= "/dev/null";
166 if ((scnout
= fopen (scnmsg
, "w")) == NULL
)
167 adios (scnmsg
, "unable to write");
170 /* scan - main loop */
172 bufsz
= rlwidth
, state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
)) {
179 if ( putc (':', scnout
) == EOF
) DIEWRERR();
183 * if we're interested in this component, save a pointer
184 * to the component text, then start using our next free
185 * buffer as the component temp buffer (buffer switching
186 * saves an extra copy of the component text).
188 if ((cptr
= fmt_findcasecomp(name
))) {
189 if (! cptr
->c_text
) {
190 cptr
->c_text
= tmpbuf
;
191 for (cp
= tmpbuf
+ strlen (tmpbuf
) - 1;
193 if (isspace ((unsigned char) *cp
))
202 while (state
== FLDPLUS
) {
204 state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
);
212 * A slight hack ... if we have less than rlwidth characters
213 * in the buffer, call m_getfld again.
216 if ((i
= strlen(tmpbuf
)) < rlwidth
) {
218 state
= m_getfld (&gstate
, name
, tmpbuf
+ i
, &bufsz
, inb
);
222 state
= FILEEOF
; /* stop now if scan cmd */
223 if (bodycomp
&& startbody
== NULL
)
227 if (putc ('\n', scnout
) == EOF
) DIEWRERR();
230 * The previous code here used to call m_getfld() using
231 * pointers to the underlying output stdio buffers to
232 * avoid the extra copy. Tests by Markus Schnalke show
233 * no noticeable performance loss on larger mailboxes
234 * if we incur an extra copy, and messing around with
235 * internal stdio buffers is becoming more and more
236 * unportable as times go on. So from now on just deal
237 * with the overhead of an extra copy.
239 * Subtle change - with the previous code tmpbuf wasn't
240 * used, so we could reuse it for the {body} component.
241 * Now since we're using tmpbuf as our read buffer we
242 * need to save the beginning of the body for later.
243 * See the above (and below) use of startbody.
246 if (bodycomp
&& startbody
== NULL
) {
251 while (state
== BODY
) {
253 state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
);
261 fprintf (stderr
, "??Format error (message %d) in ",
262 outnum
? outnum
: innum
);
264 fprintf (stderr
, "??Format error in ");
266 fprintf (stderr
, "component %d\n", compnum
);
269 FPUTS ("\n\nBAD MSG:\n");
271 if (putc ('\n', scnout
) == EOF
) DIEWRERR();
281 adios (NULL
, "getfld() returned %d", state
);
286 * format and output the scan line.
290 advise("read", "unable to"); /* "read error" */
294 /* Save and restore buffer so we don't trash our dynamic pool! */
296 saved_c_text
= bodycomp
->c_text
;
297 bodycomp
->c_text
= startbody
;
304 dat
[2] = ftell(scnout
);
305 if (dat
[2] == EOF
) DIEWRERR();
308 if ((datecomp
&& !datecomp
->c_text
) || (!size
&& !outnum
)) {
311 fstat (fileno(inb
), &st
);
312 if (!size
&& !outnum
)
315 if (! datecomp
->c_text
) {
316 if (datecomp
->c_tws
== NULL
)
317 NEW0(datecomp
->c_tws
);
318 *datecomp
->c_tws
= *dlocaltime ((time_t *) &st
.st_mtime
);
319 datecomp
->c_flags
|= CF_DATEFAB
|CF_TRUE
;
321 datecomp
->c_flags
&= ~CF_DATEFAB
;
326 fmt_scan (fmt
, *scanl
, slwidth
, dat
, NULL
);
329 bodycomp
->c_text
= saved_c_text
;
332 fputs (charstring_buffer (*scanl
), stdout
);
334 cptr
= fmt_findcomp ("encrypted");
335 encrypted
= cptr
&& cptr
->c_text
;
337 /* return dynamically allocated buffers to pool */
338 while ((cptr
= *savecomp
++)) {
342 if (outnum
&& (ferror(scnout
) || fclose (scnout
) == EOF
))
345 return (state
!= FILEEOF
? SCNERR
: encrypted
? SCNENC
: SCNMSG
);
349 /* The following two functions allow access to the global gstate above. */
352 m_getfld_state_destroy (&gstate
);
356 scan_detect_mbox_style (FILE *f
) {
357 m_unknown (&gstate
, f
);