]>
diplodocus.org Git - nmh/blob - uip/scansbr.c
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/getcpy.h"
10 #include "sbr/error.h"
11 #include "h/addrsbr.h"
12 #include "h/fmt_scan.h"
13 #include "h/scansbr.h"
16 #include "sbr/terminal.h"
18 static struct format
*fmt
;
19 static struct comp
*datecomp
; /* pntr to "date" comp */
20 static struct comp
*bodycomp
; /* pntr to "body" pseudo-comp *
22 static int ncomps
= 0; /* # of interesting components */
23 static char **compbuffers
= 0; /* buffers for component text */
24 static struct comp
**used_buf
= 0; /* stack for comp that use buffers */
26 static int dat
[5]; /* aux. data for format routine */
28 static m_getfld_state_t gstate
; /* for accessor functions below */
30 #define DIEWRERR() adios (scnmsg, "write error on")
33 if (putc((c), scnout) == EOF) \
37 if (fputs(buf,scnout) == EOF)\
41 /* outnum determines how the input from inb is copied. If positive then
42 * it is the number of the message to create, e.g. for inc(1), and all
43 * of the email is copied into that message, with some tweaks. If 0,
44 * e.g. `scan 42', then reading inb can dubiously stop after a whole
45 * buffer of body, even though this might not be enough to fulfill the
46 * scan format and width. Or if -1 then no copy is being created, but
47 * all of inb must be read because the next message must be found, e.g.
48 * `scan -file foo.mbox'. */
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
)
56 int i
, compnum
, encrypted
, state
;
57 char *cp
, *tmpbuf
, *startbody
, **nxtbuf
;
58 char *saved_c_text
= NULL
;
60 struct comp
**savecomp
;
65 static int rlwidth
, slwidth
;
67 /* first-time only initialization, which will always happen the
68 way the code is now, with callers initializing *scanl to NULL.
69 scanl used to be a global. */
74 tty_width
= sc_width();
77 width
= max(tty_width
, WIDTH
/ 2);
78 } else if (width
== 0) {
79 /* Unlimited width. */
82 dat
[3] = slwidth
= width
;
83 *scanl
= charstring_create (min(width
, NMH_BUFSIZ
));
87 /* Compile format string */
88 ncomps
= fmt_compile (nfs
, &fmt
, 1) + 2;
90 bodycomp
= fmt_findcomp("body");
91 datecomp
= fmt_findcomp("date");
92 cptr
= fmt_findcomp("folder");
94 cptr
->c_text
= mh_xstrdup(folder
);
95 if (fmt_addcompentry("encrypted")) {
98 cptr
= fmt_findcomp("dtimenow");
100 cptr
->c_text
= getcpy(dtimenow (0));
103 * In other programs I got rid of this complicated buffer switching,
104 * but since scan reads lots of messages at once and this complicated
105 * memory management, I decided to keep it; otherwise there was
106 * the potential for a lot of malloc() and free()s, and I could
107 * see the malloc() pool really getting fragmented. Maybe it
108 * wouldn't be an issue in practice; perhaps this will get
111 * So, some notes for what's going on:
113 * nxtbuf is an array of pointers that contains malloc()'d buffers
114 * to hold our component text. used_buf is an array of struct comp
115 * pointers that holds pointers to component structures we found while
116 * processing a message.
118 * We read in the message with m_getfld(), using "tmpbuf" as our
119 * input buffer. tmpbuf is set at the start of message processing
120 * to the first buffer in our buffer pool (nxtbuf).
122 * Every time we find a component we care about, we set that component's
123 * text buffer to the current value of tmpbuf, and then switch tmpbuf
124 * to the next buffer in our pool. We also add that component to
127 * When we're done, we go back and zero out all of the component
128 * text buffer pointers that we saved in used_buf.
130 * Note that this means c_text memory is NOT owned by the fmt_module
131 * and it's our responsibility to free it.
134 nxtbuf
= compbuffers
= mh_xcalloc(ncomps
, sizeof *nxtbuf
);
135 used_buf
= mh_xcalloc(ncomps
+ 1, sizeof *used_buf
);
136 used_buf
+= ncomps
+1; *--used_buf
= 0;
137 rlwidth
= NMH_BUFSIZ
;
138 for (i
= ncomps
; i
--; )
139 *nxtbuf
++ = mh_xmalloc(rlwidth
);
143 * each-message initialization
145 nxtbuf
= compbuffers
;
149 dat
[0] = innum
? innum
: outnum
;
154 * Get the first field. If the message is non-empty
155 * and we're doing an "inc", open the output file.
158 m_getfld_state_reset (&gstate
);
159 if ((state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
)) == FILEEOF
) {
161 advise("read", "unable to"); /* "read error" */
168 scnmsg
= m_name (outnum
);
169 if (*scnmsg
== '?') /* msg num out of range */
171 if ((scnout
= fopen (scnmsg
, "w")) == NULL
)
172 adios (scnmsg
, "unable to write");
175 /* scan - main loop */
177 bufsz
= rlwidth
, state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
)) {
188 * if we're interested in this component, save a pointer
189 * to the component text, then start using our next free
190 * buffer as the component temp buffer (buffer switching
191 * saves an extra copy of the component text).
193 if ((cptr
= fmt_findcasecomp(name
))) {
194 if (! cptr
->c_text
) {
195 cptr
->c_text
= tmpbuf
;
196 for (cp
= tmpbuf
+ strlen (tmpbuf
) - 1;
198 if (isspace ((unsigned char) *cp
))
207 while (state
== FLDPLUS
) {
209 state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
);
217 * A slight hack ... if we have less than rlwidth characters
218 * in the buffer, call m_getfld again.
221 if ((i
= strlen(tmpbuf
)) < rlwidth
) {
223 state
= m_getfld (&gstate
, name
, tmpbuf
+ i
, &bufsz
, inb
);
227 state
= FILEEOF
; /* stop now if scan cmd */
228 if (bodycomp
&& startbody
== NULL
)
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
);
269 fprintf (stderr
, "??Format error (message %d) in ",
270 outnum
? outnum
: innum
);
272 fprintf (stderr
, "??Format error in ");
274 fprintf (stderr
, "component %d\n", compnum
);
277 FPUTS ("\n\nBAD MSG:\n");
289 die("getfld() returned %d", state
);
294 * format and output the scan line.
298 advise("read", "unable to"); /* "read error" */
302 /* Save and restore buffer so we don't trash our dynamic pool! */
304 saved_c_text
= bodycomp
->c_text
;
305 bodycomp
->c_text
= startbody
;
312 dat
[2] = ftell(scnout
);
313 if (dat
[2] == EOF
) DIEWRERR();
316 if ((datecomp
&& !datecomp
->c_text
) || (!size
&& !outnum
)) {
319 fstat (fileno(inb
), &st
);
320 if (!size
&& !outnum
)
323 if (! datecomp
->c_text
) {
324 if (datecomp
->c_tws
== NULL
)
325 NEW0(datecomp
->c_tws
);
326 *datecomp
->c_tws
= *dlocaltime ((time_t *) &st
.st_mtime
);
327 datecomp
->c_flags
|= CF_DATEFAB
|CF_TRUE
;
329 datecomp
->c_flags
&= ~CF_DATEFAB
;
334 fmt_scan (fmt
, *scanl
, slwidth
, dat
, NULL
);
337 bodycomp
->c_text
= saved_c_text
;
340 fputs (charstring_buffer (*scanl
), stdout
);
342 cptr
= fmt_findcomp ("encrypted");
343 encrypted
= cptr
&& cptr
->c_text
;
345 /* return dynamically allocated buffers to pool */
346 while ((cptr
= *savecomp
++)) {
350 if (scnout
&& (ferror(scnout
) || fclose (scnout
) == EOF
))
353 return state
!= FILEEOF
? SCNERR
: encrypted
? SCNENC
: SCNMSG
;
357 /* The following two functions allow access to the global gstate above. */
361 m_getfld_state_destroy (&gstate
);
365 scan_detect_mbox_style (FILE *f
)
367 m_unknown (&gstate
, f
);