]>
diplodocus.org Git - nmh/blob - uip/scansbr.c
28c382fe1180b480ec7fa2c1e8b25f8ed0e8479c
1 /* scansbr.c -- routines to help scan along...
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
9 #include "sbr/m_name.h"
10 #include "sbr/m_gmprot.h"
11 #include "sbr/m_getfld.h"
12 #include "sbr/getcpy.h"
13 #include "sbr/error.h"
14 #include "h/addrsbr.h"
15 #include "h/fmt_scan.h"
16 #include "h/scansbr.h"
19 #include "sbr/terminal.h"
21 static struct format
*fmt
;
22 static struct comp
*datecomp
; /* pntr to "date" comp */
23 static struct comp
*bodycomp
; /* pntr to "body" pseudo-comp *
25 static int ncomps
= 0; /* # of interesting components */
26 static char **compbuffers
= 0; /* buffers for component text */
27 static struct comp
**used_buf
= 0; /* stack for comp that use buffers */
29 static int dat
[5]; /* aux. data for format routine */
31 static m_getfld_state_t gstate
; /* for accessor functions below */
33 #define DIEWRERR() adios (scnmsg, "write error on")
36 if (putc((c), scnout) == EOF) \
40 if (fputs(buf,scnout) == EOF)\
44 /* outnum determines how the input from inb is copied. If positive then
45 * it is the number of the message to create, e.g. for inc(1), and all
46 * of the email is copied into that message, with some tweaks. If 0,
47 * e.g. `scan 42', then reading inb can dubiously stop after a whole
48 * buffer of body, even though this might not be enough to fulfill the
49 * scan format and width. Or if -1 then no copy is being created, but
50 * all of inb must be read because the next message must be found, e.g.
51 * `scan -file foo.mbox'. */
54 scan (FILE *inb
, int innum
, int outnum
, char *nfs
, int width
, int curflg
,
55 int unseen
, char *folder
, long size
, int noisy
, charstring_t
*scanl
)
59 int i
, compnum
, encrypted
, state
;
60 char *cp
, *tmpbuf
, *startbody
, **nxtbuf
;
61 char *saved_c_text
= NULL
;
63 struct comp
**savecomp
;
68 static int rlwidth
, slwidth
;
70 /* first-time only initialization, which will always happen the
71 way the code is now, with callers initializing *scanl to NULL.
72 scanl used to be a global. */
77 tty_width
= sc_width();
80 width
= max(tty_width
, WIDTH
/ 2);
81 } else if (width
== 0) {
82 /* Unlimited width. */
85 dat
[3] = slwidth
= width
;
86 *scanl
= charstring_create (min(width
, NMH_BUFSIZ
));
90 /* Compile format string */
91 ncomps
= fmt_compile (nfs
, &fmt
, 1) + 2;
93 bodycomp
= fmt_findcomp("body");
94 datecomp
= fmt_findcomp("date");
95 cptr
= fmt_findcomp("folder");
97 cptr
->c_text
= mh_xstrdup(folder
);
98 if (fmt_addcompentry("encrypted")) {
101 cptr
= fmt_findcomp("dtimenow");
103 cptr
->c_text
= getcpy(dtimenow (0));
106 * In other programs I got rid of this complicated buffer switching,
107 * but since scan reads lots of messages at once and this complicated
108 * memory management, I decided to keep it; otherwise there was
109 * the potential for a lot of malloc() and free()s, and I could
110 * see the malloc() pool really getting fragmented. Maybe it
111 * wouldn't be an issue in practice; perhaps this will get
114 * So, some notes for what's going on:
116 * nxtbuf is an array of pointers that contains malloc()'d buffers
117 * to hold our component text. used_buf is an array of struct comp
118 * pointers that holds pointers to component structures we found while
119 * processing a message.
121 * We read in the message with m_getfld(), using "tmpbuf" as our
122 * input buffer. tmpbuf is set at the start of message processing
123 * to the first buffer in our buffer pool (nxtbuf).
125 * Every time we find a component we care about, we set that component's
126 * text buffer to the current value of tmpbuf, and then switch tmpbuf
127 * to the next buffer in our pool. We also add that component to
130 * When we're done, we go back and zero out all of the component
131 * text buffer pointers that we saved in used_buf.
133 * Note that this means c_text memory is NOT owned by the fmt_module
134 * and it's our responsibility to free it.
137 nxtbuf
= compbuffers
= mh_xcalloc(ncomps
, sizeof *nxtbuf
);
138 used_buf
= mh_xcalloc(ncomps
+ 1, sizeof *used_buf
);
139 used_buf
+= ncomps
+1; *--used_buf
= 0;
140 rlwidth
= NMH_BUFSIZ
;
141 for (i
= ncomps
; i
--; )
142 *nxtbuf
++ = mh_xmalloc(rlwidth
);
146 * each-message initialization
148 nxtbuf
= compbuffers
;
152 dat
[0] = innum
? innum
: outnum
;
157 * Get the first field. If the message is non-empty
158 * and we're doing an "inc", open the output file.
161 m_getfld_state_reset (&gstate
);
162 if ((state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
)) == FILEEOF
) {
164 advise("read", "unable to"); /* "read error" */
171 scnmsg
= m_name (outnum
);
172 if (*scnmsg
== '?') /* msg num out of range */
174 if ((scnout
= fopen (scnmsg
, "w")) == NULL
)
175 adios (scnmsg
, "unable to write");
178 /* scan - main loop */
180 bufsz
= rlwidth
, state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
)) {
191 * if we're interested in this component, save a pointer
192 * to the component text, then start using our next free
193 * buffer as the component temp buffer (buffer switching
194 * saves an extra copy of the component text).
196 if ((cptr
= fmt_findcasecomp(name
))) {
197 if (! cptr
->c_text
) {
198 cptr
->c_text
= tmpbuf
;
199 for (cp
= tmpbuf
+ strlen (tmpbuf
) - 1;
201 if (isspace ((unsigned char) *cp
))
210 while (state
== FLDPLUS
) {
212 state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
);
220 * A slight hack ... if we have less than rlwidth characters
221 * in the buffer, call m_getfld again.
224 if ((i
= strlen(tmpbuf
)) < rlwidth
) {
226 state
= m_getfld (&gstate
, name
, tmpbuf
+ i
, &bufsz
, inb
);
230 state
= FILEEOF
; /* stop now if scan cmd */
231 if (bodycomp
&& startbody
== NULL
)
240 * The previous code here used to call m_getfld() using
241 * pointers to the underlying output stdio buffers to
242 * avoid the extra copy. Tests by Markus Schnalke show
243 * no noticeable performance loss on larger mailboxes
244 * if we incur an extra copy, and messing around with
245 * internal stdio buffers is becoming more and more
246 * unportable as times go on. So from now on just deal
247 * with the overhead of an extra copy.
249 * Subtle change - with the previous code tmpbuf wasn't
250 * used, so we could reuse it for the {body} component.
251 * Now since we're using tmpbuf as our read buffer we
252 * need to save the beginning of the body for later.
253 * See the above (and below) use of startbody.
256 if (bodycomp
&& startbody
== NULL
) {
261 while (state
== BODY
) {
263 state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
);
272 fprintf (stderr
, "??Format error (message %d) in ",
273 outnum
? outnum
: innum
);
275 fprintf (stderr
, "??Format error in ");
277 fprintf (stderr
, "component %d\n", compnum
);
280 FPUTS ("\n\nBAD MSG:\n");
292 die("getfld() returned %d", state
);
297 * format and output the scan line.
301 advise("read", "unable to"); /* "read error" */
305 /* Save and restore buffer so we don't trash our dynamic pool! */
307 saved_c_text
= bodycomp
->c_text
;
308 bodycomp
->c_text
= startbody
;
315 dat
[2] = ftell(scnout
);
316 if (dat
[2] == EOF
) DIEWRERR();
319 if ((datecomp
&& !datecomp
->c_text
) || (!size
&& !outnum
)) {
322 fstat (fileno(inb
), &st
);
323 if (!size
&& !outnum
)
326 if (! datecomp
->c_text
) {
327 if (datecomp
->c_tws
== NULL
)
328 NEW0(datecomp
->c_tws
);
329 *datecomp
->c_tws
= *dlocaltime ((time_t *) &st
.st_mtime
);
330 datecomp
->c_flags
|= CF_DATEFAB
|CF_TRUE
;
332 datecomp
->c_flags
&= ~CF_DATEFAB
;
337 fmt_scan (fmt
, *scanl
, slwidth
, dat
, NULL
);
340 bodycomp
->c_text
= saved_c_text
;
343 fputs (charstring_buffer (*scanl
), stdout
);
345 cptr
= fmt_findcomp ("encrypted");
346 encrypted
= cptr
&& cptr
->c_text
;
348 /* return dynamically allocated buffers to pool */
349 while ((cptr
= *savecomp
++)) {
353 if (scnout
&& (ferror(scnout
) || fclose (scnout
) == EOF
))
356 return state
!= FILEEOF
? SCNERR
: encrypted
? SCNENC
: SCNMSG
;
360 /* The following two functions allow access to the global gstate above. */
364 m_getfld_state_destroy (&gstate
);
368 scan_detect_mbox_style (FILE *f
)
370 m_unknown (&gstate
, f
);