]>
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 m_getfld_state_t gstate
;
19 #define MAXSCANL 256 /* longest possible scan line */
22 * Buffer size for content part of header fields. We want this
23 * to be large enough so that we don't do a lot of extra FLDPLUS
24 * calls on m_getfld but small enough so that we don't snarf
25 * the entire message body when we're only going to display 30
30 static struct format
*fmt
;
31 static struct comp
*datecomp
; /* pntr to "date" comp */
32 static struct comp
*bodycomp
; /* pntr to "body" pseudo-comp *
34 static int ncomps
= 0; /* # of interesting components */
35 static char **compbuffers
= 0; /* buffers for component text */
36 static struct comp
**used_buf
= 0; /* stack for comp that use buffers */
38 static int dat
[5]; /* aux. data for format routine */
40 char *scanl
= 0; /* text of most recent scanline */
42 #define DIEWRERR() adios (scnmsg, "write error on")
45 if (mh_fputs(buf,scnout) == EOF)\
52 static int mh_fputs(char *, FILE *);
54 #ifdef MULTIBYTE_SUPPORT
55 #define SCAN_CHARWIDTH MB_CUR_MAX
57 #define SCAN_CHARWIDTH 1
61 scan (FILE *inb
, int innum
, int outnum
, char *nfs
, int width
, int curflg
,
62 int unseen
, char *folder
, long size
, int noisy
)
64 int i
, compnum
, encrypted
, state
;
65 unsigned char *cp
, *tmpbuf
, *startbody
;
67 char *saved_c_text
= NULL
;
69 struct comp
**savecomp
;
74 static int rlwidth
, slwidth
;
75 static size_t scanl_size
;
77 /* first-time only initialization */
80 if ((width
= sc_width ()) < WIDTH
/2)
82 else if (width
> MAXSCANL
)
85 dat
[3] = slwidth
= width
;
86 /* Arbitrarily allocate 20 * slwidth to provide room for lots
87 of escape sequences. */
88 scanl_size
= SCAN_CHARWIDTH
* (20 * slwidth
+ 2);
89 scanl
= (char *) mh_xmalloc (scanl_size
);
93 /* Compile format string */
94 ncomps
= fmt_compile (nfs
, &fmt
, 1) + 2;
96 bodycomp
= fmt_findcomp("body");
97 datecomp
= fmt_findcomp("date");
98 cptr
= fmt_findcomp("folder");
100 cptr
->c_text
= getcpy(folder
);
101 if (fmt_addcompentry("encrypted")) {
104 cptr
= fmt_findcomp("dtimenow");
106 cptr
->c_text
= getcpy(dtimenow (0));
109 * In other programs I got rid of this complicated buffer switching,
110 * but since scan reads lots of messages at once and this complicated
111 * memory management, I decided to keep it; otherwise there was
112 * the potential for a lot of malloc() and free()s, and I could
113 * see the malloc() pool really getting fragmented. Maybe it
114 * wouldn't be an issue in practice; perhaps this will get
117 * So, some notes for what's going on:
119 * nxtbuf is an array of pointers that contains malloc()'d buffers
120 * to hold our component text. used_buf is an array of struct comp
121 * pointers that holds pointers to component structures we found while
122 * processing a message.
124 * We read in the message with m_getfld(), using "tmpbuf" as our
125 * input buffer. tmpbuf is set at the start of message processing
126 * to the first buffer in our buffer pool (nxtbuf).
128 * Every time we find a component we care about, we set that component's
129 * text buffer to the current value of tmpbuf, and then switch tmpbuf
130 * to the next buffer in our pool. We also add that component to
133 * When we're done, we go back and zero out all of the component
134 * text buffer pointers that we saved in used_buf.
136 * Note that this means c_text memory is NOT owned by the fmt_module
137 * and it's our responsibility to free it.
140 nxtbuf
= compbuffers
= (char **) calloc((size_t) ncomps
, sizeof(char *));
142 adios (NULL
, "unable to allocate component buffers");
143 used_buf
= (struct comp
**) calloc((size_t) (ncomps
+1),
144 sizeof(struct comp
*));
145 if (used_buf
== NULL
)
146 adios (NULL
, "unable to allocate component buffer stack");
147 used_buf
+= ncomps
+1; *--used_buf
= 0;
148 rlwidth
= bodycomp
&& (width
> SBUFSIZ
) ? width
: SBUFSIZ
;
149 for (i
= ncomps
; i
--; )
150 *nxtbuf
++ = mh_xmalloc(rlwidth
);
154 * each-message initialization
156 nxtbuf
= compbuffers
;
160 dat
[0] = innum
? innum
: outnum
;
165 * Get the first field. If the message is non-empty
166 * and we're doing an "inc", open the output file.
169 m_getfld_state_reset (&gstate
);
170 if ((state
= m_getfld (gstate
, name
, tmpbuf
, &bufsz
, inb
)) == FILEEOF
) {
172 advise("read", "unable to"); /* "read error" */
181 scnmsg
= m_name (outnum
);
182 if (*scnmsg
== '?') /* msg num out of range */
185 scnmsg
= "/dev/null";
187 if ((scnout
= fopen (scnmsg
, "w")) == NULL
)
188 adios (scnmsg
, "unable to write");
191 /* scan - main loop */
193 bufsz
= rlwidth
, state
= m_getfld (gstate
, name
, tmpbuf
, &bufsz
, inb
)) {
200 if ( putc (':', scnout
) == EOF
) DIEWRERR();
204 * if we're interested in this component, save a pointer
205 * to the component text, then start using our next free
206 * buffer as the component temp buffer (buffer switching
207 * saves an extra copy of the component text).
209 if ((cptr
= fmt_findcasecomp(name
))) {
210 if (! cptr
->c_text
) {
211 cptr
->c_text
= tmpbuf
;
212 for (cp
= tmpbuf
+ strlen (tmpbuf
) - 1;
223 while (state
== FLDPLUS
) {
225 state
= m_getfld (gstate
, name
, tmpbuf
, &bufsz
, inb
);
234 * A slight hack ... if we have less than rlwidth characters
235 * in the buffer, call m_getfld again.
238 if ((i
= strlen(tmpbuf
)) < rlwidth
) {
240 state
= m_getfld (gstate
, name
, tmpbuf
+ i
, &bufsz
, inb
);
244 state
= FILEEOF
; /* stop now if scan cmd */
245 if (bodycomp
&& startbody
== NULL
)
249 if (putc ('\n', scnout
) == EOF
) DIEWRERR();
252 * The previous code here used to call m_getfld() using
253 * pointers to the underlying output stdio buffers to
254 * avoid the extra copy. Tests by Markus Schnalke show
255 * no noticable performance loss on larger mailboxes
256 * if we incur an extra copy, and messing around with
257 * internal stdio buffers is becoming more and more
258 * unportable as times go on. So from now on just deal
259 * with the overhead of an extra copy.
261 * Subtle change - with the previous code tmpbuf wasn't
262 * used, so we could reuse it for the {body} component.
263 * Now since we're using tmpbuf as our read buffer we
264 * need to save the beginning of the body for later.
265 * See the above (and below) use of startbody.
268 if (bodycomp
&& startbody
== NULL
) {
273 while (state
== BODY
) {
275 state
= m_getfld (gstate
, name
, tmpbuf
, &bufsz
, inb
);
283 innum
? "??Format error (message %d) in "
284 : "??Format error in ",
285 outnum
? outnum
: innum
);
286 fprintf (stderr
, "component %d\n", compnum
);
289 FPUTS ("\n\nBAD MSG:\n");
291 if (putc ('\n', scnout
) == EOF
) DIEWRERR();
301 adios (NULL
, "getfld() returned %d", state
);
306 * format and output the scan line.
310 advise("read", "unable to"); /* "read error" */
314 /* Save and restore buffer so we don't trash our dynamic pool! */
316 saved_c_text
= bodycomp
->c_text
;
317 bodycomp
->c_text
= startbody
;
324 dat
[2] = ftell(scnout
);
325 if (dat
[2] == EOF
) DIEWRERR();
328 if ((datecomp
&& !datecomp
->c_text
) || (!size
&& !outnum
)) {
331 fstat (fileno(inb
), &st
);
332 if (!size
&& !outnum
)
335 if (! datecomp
->c_text
) {
336 if (datecomp
->c_tws
== NULL
)
337 datecomp
->c_tws
= (struct tws
*)
338 calloc((size_t) 1, sizeof(*datecomp
->c_tws
));
339 if (datecomp
->c_tws
== NULL
)
340 adios (NULL
, "unable to allocate tws buffer");
341 *datecomp
->c_tws
= *dlocaltime ((time_t *) &st
.st_mtime
);
342 datecomp
->c_flags
|= CF_DATEFAB
|CF_TRUE
;
344 datecomp
->c_flags
&= ~CF_DATEFAB
;
349 fmt_scan (fmt
, scanl
, scanl_size
, slwidth
, dat
);
352 bodycomp
->c_text
= saved_c_text
;
355 fputs (scanl
, stdout
);
357 cptr
= fmt_findcomp ("encrypted");
358 encrypted
= cptr
&& cptr
->c_text
;
360 /* return dynamically allocated buffers to pool */
361 while ((cptr
= *savecomp
++)) {
362 *--nxtbuf
= cptr
->c_text
;
367 if (outnum
&& (ferror(scnout
) || fclose (scnout
) == EOF
))
370 return (state
!= FILEEOF
? SCNERR
: encrypted
? SCNENC
: SCNMSG
);
375 mh_fputs(char *s
, FILE *stream
)
380 if (putc (c
,stream
) == EOF
)