]>
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>
17 #define MAXSCANL 256 /* longest possible scan line */
20 * Buffer size for content part of header fields. We want this
21 * to be large enough so that we don't do a lot of extra FLDPLUS
22 * calls on m_getfld but small enough so that we don't snarf
23 * the entire message body when we're only going to display 30
28 static struct format
*fmt
;
29 static struct comp
*datecomp
; /* pntr to "date" comp */
30 static struct comp
*bodycomp
; /* pntr to "body" pseudo-comp *
32 static int ncomps
= 0; /* # of interesting components */
33 static char **compbuffers
= 0; /* buffers for component text */
34 static struct comp
**used_buf
= 0; /* stack for comp that use buffers */
36 static int dat
[5]; /* aux. data for format routine */
38 char *scanl
= 0; /* text of most recent scanline */
39 m_getfld_state_t gstate
; /* for access by msh */
41 #define DIEWRERR() adios (scnmsg, "write error on")
44 if (mh_fputs(buf,scnout) == EOF)\
51 static int mh_fputs(char *, FILE *);
53 #ifdef MULTIBYTE_SUPPORT
54 #define SCAN_CHARWIDTH MB_CUR_MAX
56 #define SCAN_CHARWIDTH 1
60 scan (FILE *inb
, int innum
, int outnum
, char *nfs
, int width
, int curflg
,
61 int unseen
, char *folder
, long size
, int noisy
)
63 int i
, compnum
, encrypted
, state
;
64 unsigned char *cp
, *tmpbuf
, *startbody
;
66 char *saved_c_text
= NULL
;
68 struct comp
**savecomp
;
73 static int rlwidth
, slwidth
;
74 static size_t scanl_size
;
76 /* first-time only initialization */
79 if ((width
= sc_width ()) < WIDTH
/2)
81 else if (width
> MAXSCANL
)
84 dat
[3] = slwidth
= width
;
85 /* Arbitrarily allocate 20 * slwidth to provide room for lots
86 of escape sequences. */
87 scanl_size
= SCAN_CHARWIDTH
* (20 * slwidth
+ 2);
88 scanl
= (char *) mh_xmalloc (scanl_size
);
92 /* Compile format string */
93 ncomps
= fmt_compile (nfs
, &fmt
, 1) + 2;
95 bodycomp
= fmt_findcomp("body");
96 datecomp
= fmt_findcomp("date");
97 cptr
= fmt_findcomp("folder");
99 cptr
->c_text
= getcpy(folder
);
100 if (fmt_addcompentry("encrypted")) {
103 cptr
= fmt_findcomp("dtimenow");
105 cptr
->c_text
= getcpy(dtimenow (0));
108 * In other programs I got rid of this complicated buffer switching,
109 * but since scan reads lots of messages at once and this complicated
110 * memory management, I decided to keep it; otherwise there was
111 * the potential for a lot of malloc() and free()s, and I could
112 * see the malloc() pool really getting fragmented. Maybe it
113 * wouldn't be an issue in practice; perhaps this will get
116 * So, some notes for what's going on:
118 * nxtbuf is an array of pointers that contains malloc()'d buffers
119 * to hold our component text. used_buf is an array of struct comp
120 * pointers that holds pointers to component structures we found while
121 * processing a message.
123 * We read in the message with m_getfld(), using "tmpbuf" as our
124 * input buffer. tmpbuf is set at the start of message processing
125 * to the first buffer in our buffer pool (nxtbuf).
127 * Every time we find a component we care about, we set that component's
128 * text buffer to the current value of tmpbuf, and then switch tmpbuf
129 * to the next buffer in our pool. We also add that component to
132 * When we're done, we go back and zero out all of the component
133 * text buffer pointers that we saved in used_buf.
135 * Note that this means c_text memory is NOT owned by the fmt_module
136 * and it's our responsibility to free it.
139 nxtbuf
= compbuffers
= (char **) calloc((size_t) ncomps
, sizeof(char *));
141 adios (NULL
, "unable to allocate component buffers");
142 used_buf
= (struct comp
**) calloc((size_t) (ncomps
+1),
143 sizeof(struct comp
*));
144 if (used_buf
== NULL
)
145 adios (NULL
, "unable to allocate component buffer stack");
146 used_buf
+= ncomps
+1; *--used_buf
= 0;
147 rlwidth
= bodycomp
&& (width
> SBUFSIZ
) ? width
: SBUFSIZ
;
148 for (i
= ncomps
; i
--; )
149 *nxtbuf
++ = mh_xmalloc(rlwidth
);
153 * each-message initialization
155 nxtbuf
= compbuffers
;
159 dat
[0] = innum
? innum
: outnum
;
164 * Get the first field. If the message is non-empty
165 * and we're doing an "inc", open the output file.
168 m_getfld_state_reset (&gstate
);
169 if ((state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
)) == FILEEOF
) {
171 advise("read", "unable to"); /* "read error" */
180 scnmsg
= m_name (outnum
);
181 if (*scnmsg
== '?') /* msg num out of range */
184 scnmsg
= "/dev/null";
186 if ((scnout
= fopen (scnmsg
, "w")) == NULL
)
187 adios (scnmsg
, "unable to write");
190 /* scan - main loop */
192 bufsz
= rlwidth
, state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
)) {
199 if ( putc (':', scnout
) == EOF
) DIEWRERR();
203 * if we're interested in this component, save a pointer
204 * to the component text, then start using our next free
205 * buffer as the component temp buffer (buffer switching
206 * saves an extra copy of the component text).
208 if ((cptr
= fmt_findcasecomp(name
))) {
209 if (! cptr
->c_text
) {
210 cptr
->c_text
= tmpbuf
;
211 for (cp
= tmpbuf
+ strlen (tmpbuf
) - 1;
222 while (state
== FLDPLUS
) {
224 state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
);
233 * A slight hack ... if we have less than rlwidth characters
234 * in the buffer, call m_getfld again.
237 if ((i
= strlen(tmpbuf
)) < rlwidth
) {
239 state
= m_getfld (&gstate
, name
, tmpbuf
+ i
, &bufsz
, inb
);
243 state
= FILEEOF
; /* stop now if scan cmd */
244 if (bodycomp
&& startbody
== NULL
)
248 if (putc ('\n', scnout
) == EOF
) DIEWRERR();
251 * The previous code here used to call m_getfld() using
252 * pointers to the underlying output stdio buffers to
253 * avoid the extra copy. Tests by Markus Schnalke show
254 * no noticable performance loss on larger mailboxes
255 * if we incur an extra copy, and messing around with
256 * internal stdio buffers is becoming more and more
257 * unportable as times go on. So from now on just deal
258 * with the overhead of an extra copy.
260 * Subtle change - with the previous code tmpbuf wasn't
261 * used, so we could reuse it for the {body} component.
262 * Now since we're using tmpbuf as our read buffer we
263 * need to save the beginning of the body for later.
264 * See the above (and below) use of startbody.
267 if (bodycomp
&& startbody
== NULL
) {
272 while (state
== BODY
) {
274 state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
);
282 innum
? "??Format error (message %d) in "
283 : "??Format error in ",
284 outnum
? outnum
: innum
);
285 fprintf (stderr
, "component %d\n", compnum
);
288 FPUTS ("\n\nBAD MSG:\n");
290 if (putc ('\n', scnout
) == EOF
) DIEWRERR();
300 adios (NULL
, "getfld() returned %d", state
);
305 * format and output the scan line.
309 advise("read", "unable to"); /* "read error" */
313 /* Save and restore buffer so we don't trash our dynamic pool! */
315 saved_c_text
= bodycomp
->c_text
;
316 bodycomp
->c_text
= startbody
;
323 dat
[2] = ftell(scnout
);
324 if (dat
[2] == EOF
) DIEWRERR();
327 if ((datecomp
&& !datecomp
->c_text
) || (!size
&& !outnum
)) {
330 fstat (fileno(inb
), &st
);
331 if (!size
&& !outnum
)
334 if (! datecomp
->c_text
) {
335 if (datecomp
->c_tws
== NULL
)
336 datecomp
->c_tws
= (struct tws
*)
337 calloc((size_t) 1, sizeof(*datecomp
->c_tws
));
338 if (datecomp
->c_tws
== NULL
)
339 adios (NULL
, "unable to allocate tws buffer");
340 *datecomp
->c_tws
= *dlocaltime ((time_t *) &st
.st_mtime
);
341 datecomp
->c_flags
|= CF_DATEFAB
|CF_TRUE
;
343 datecomp
->c_flags
&= ~CF_DATEFAB
;
348 fmt_scan (fmt
, scanl
, scanl_size
, slwidth
, dat
);
351 bodycomp
->c_text
= saved_c_text
;
354 fputs (scanl
, stdout
);
356 cptr
= fmt_findcomp ("encrypted");
357 encrypted
= cptr
&& cptr
->c_text
;
359 /* return dynamically allocated buffers to pool */
360 while ((cptr
= *savecomp
++)) {
364 if (outnum
&& (ferror(scnout
) || fclose (scnout
) == EOF
))
367 return (state
!= FILEEOF
? SCNERR
: encrypted
? SCNENC
: SCNMSG
);
372 mh_fputs(char *s
, FILE *stream
)
377 if (putc (c
,stream
) == EOF
)
382 /* The following three functions allow access to the global gstate above. */
385 m_getfld_state_destroy (&gstate
);
389 scan_detect_mbox_style (FILE *f
) {
390 m_unknown (&gstate
, f
);
394 scan_eom_action (int (*action
)()) {
395 m_eomsbr (gstate
, action
);
399 scan_reset_m_getfld_state () {
400 m_getfld_state_reset (&gstate
);