]>
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.
10 #include "h/addrsbr.h"
11 #include "h/fmt_scan.h"
12 #include "h/scansbr.h"
15 #include "sbr/terminal.h"
17 static struct format
*fmt
;
18 static struct comp
*datecomp
; /* pntr to "date" comp */
19 static struct comp
*bodycomp
; /* pntr to "body" pseudo-comp *
21 static int ncomps
= 0; /* # of interesting components */
22 static char **compbuffers
= 0; /* buffers for component text */
23 static struct comp
**used_buf
= 0; /* stack for comp that use buffers */
25 static int dat
[5]; /* aux. data for format routine */
27 static m_getfld_state_t gstate
; /* for accessor functions below */
29 #define DIEWRERR() adios (scnmsg, "write error on")
32 if (putc((c), scnout) == EOF) \
36 if (fputs(buf,scnout) == EOF)\
40 /* outnum determines how the input from inb is copied. If positive then
41 * it is the number of the message to create, e.g. for inc(1), and all
42 * of the email is copied into that message, with some tweaks. If 0,
43 * e.g. `scan 42', then reading inb can dubiously stop after a whole
44 * buffer of body, even though this might not be enough to fulfill the
45 * scan format and width. Or if -1 then no copy is being created, but
46 * all of inb must be read because the next message must be found, e.g.
47 * `scan -file foo.mbox'. */
50 scan (FILE *inb
, int innum
, int outnum
, char *nfs
, int width
, int curflg
,
51 int unseen
, char *folder
, long size
, int noisy
, charstring_t
*scanl
)
55 int i
, compnum
, encrypted
, state
;
56 char *cp
, *tmpbuf
, *startbody
, **nxtbuf
;
57 char *saved_c_text
= NULL
;
59 struct comp
**savecomp
;
64 static int rlwidth
, slwidth
;
66 /* first-time only initialization, which will always happen the
67 way the code is now, with callers initializing *scanl to NULL.
68 scanl used to be a global. */
73 tty_width
= sc_width();
76 width
= max(tty_width
, WIDTH
/ 2);
77 } else if (width
== 0) {
78 /* Unlimited width. */
81 dat
[3] = slwidth
= width
;
82 *scanl
= charstring_create (min(width
, NMH_BUFSIZ
));
86 /* Compile format string */
87 ncomps
= fmt_compile (nfs
, &fmt
, 1) + 2;
89 bodycomp
= fmt_findcomp("body");
90 datecomp
= fmt_findcomp("date");
91 cptr
= fmt_findcomp("folder");
93 cptr
->c_text
= mh_xstrdup(folder
);
94 if (fmt_addcompentry("encrypted")) {
97 cptr
= fmt_findcomp("dtimenow");
99 cptr
->c_text
= getcpy(dtimenow (0));
102 * In other programs I got rid of this complicated buffer switching,
103 * but since scan reads lots of messages at once and this complicated
104 * memory management, I decided to keep it; otherwise there was
105 * the potential for a lot of malloc() and free()s, and I could
106 * see the malloc() pool really getting fragmented. Maybe it
107 * wouldn't be an issue in practice; perhaps this will get
110 * So, some notes for what's going on:
112 * nxtbuf is an array of pointers that contains malloc()'d buffers
113 * to hold our component text. used_buf is an array of struct comp
114 * pointers that holds pointers to component structures we found while
115 * processing a message.
117 * We read in the message with m_getfld(), using "tmpbuf" as our
118 * input buffer. tmpbuf is set at the start of message processing
119 * to the first buffer in our buffer pool (nxtbuf).
121 * Every time we find a component we care about, we set that component's
122 * text buffer to the current value of tmpbuf, and then switch tmpbuf
123 * to the next buffer in our pool. We also add that component to
126 * When we're done, we go back and zero out all of the component
127 * text buffer pointers that we saved in used_buf.
129 * Note that this means c_text memory is NOT owned by the fmt_module
130 * and it's our responsibility to free it.
133 nxtbuf
= compbuffers
= mh_xcalloc(ncomps
, sizeof *nxtbuf
);
134 used_buf
= mh_xcalloc(ncomps
+ 1, sizeof *used_buf
);
135 used_buf
+= ncomps
+1; *--used_buf
= 0;
136 rlwidth
= NMH_BUFSIZ
;
137 for (i
= ncomps
; i
--; )
138 *nxtbuf
++ = mh_xmalloc(rlwidth
);
142 * each-message initialization
144 nxtbuf
= compbuffers
;
148 dat
[0] = innum
? innum
: outnum
;
153 * Get the first field. If the message is non-empty
154 * and we're doing an "inc", open the output file.
157 m_getfld_state_reset (&gstate
);
158 if ((state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
)) == FILEEOF
) {
160 advise("read", "unable to"); /* "read error" */
167 scnmsg
= m_name (outnum
);
168 if (*scnmsg
== '?') /* msg num out of range */
170 if ((scnout
= fopen (scnmsg
, "w")) == NULL
)
171 adios (scnmsg
, "unable to write");
174 /* scan - main loop */
176 bufsz
= rlwidth
, state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
)) {
187 * if we're interested in this component, save a pointer
188 * to the component text, then start using our next free
189 * buffer as the component temp buffer (buffer switching
190 * saves an extra copy of the component text).
192 if ((cptr
= fmt_findcasecomp(name
))) {
193 if (! cptr
->c_text
) {
194 cptr
->c_text
= tmpbuf
;
195 for (cp
= tmpbuf
+ strlen (tmpbuf
) - 1;
197 if (isspace ((unsigned char) *cp
))
206 while (state
== FLDPLUS
) {
208 state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
);
216 * A slight hack ... if we have less than rlwidth characters
217 * in the buffer, call m_getfld again.
220 if ((i
= strlen(tmpbuf
)) < rlwidth
) {
222 state
= m_getfld (&gstate
, name
, tmpbuf
+ i
, &bufsz
, inb
);
226 state
= FILEEOF
; /* stop now if scan cmd */
227 if (bodycomp
&& startbody
== NULL
)
236 * The previous code here used to call m_getfld() using
237 * pointers to the underlying output stdio buffers to
238 * avoid the extra copy. Tests by Markus Schnalke show
239 * no noticeable performance loss on larger mailboxes
240 * if we incur an extra copy, and messing around with
241 * internal stdio buffers is becoming more and more
242 * unportable as times go on. So from now on just deal
243 * with the overhead of an extra copy.
245 * Subtle change - with the previous code tmpbuf wasn't
246 * used, so we could reuse it for the {body} component.
247 * Now since we're using tmpbuf as our read buffer we
248 * need to save the beginning of the body for later.
249 * See the above (and below) use of startbody.
252 if (bodycomp
&& startbody
== NULL
) {
257 while (state
== BODY
) {
259 state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
);
268 fprintf (stderr
, "??Format error (message %d) in ",
269 outnum
? outnum
: innum
);
271 fprintf (stderr
, "??Format error in ");
273 fprintf (stderr
, "component %d\n", compnum
);
276 FPUTS ("\n\nBAD MSG:\n");
288 die("getfld() returned %d", state
);
293 * format and output the scan line.
297 advise("read", "unable to"); /* "read error" */
301 /* Save and restore buffer so we don't trash our dynamic pool! */
303 saved_c_text
= bodycomp
->c_text
;
304 bodycomp
->c_text
= startbody
;
311 dat
[2] = ftell(scnout
);
312 if (dat
[2] == EOF
) DIEWRERR();
315 if ((datecomp
&& !datecomp
->c_text
) || (!size
&& !outnum
)) {
318 fstat (fileno(inb
), &st
);
319 if (!size
&& !outnum
)
322 if (! datecomp
->c_text
) {
323 if (datecomp
->c_tws
== NULL
)
324 NEW0(datecomp
->c_tws
);
325 *datecomp
->c_tws
= *dlocaltime ((time_t *) &st
.st_mtime
);
326 datecomp
->c_flags
|= CF_DATEFAB
|CF_TRUE
;
328 datecomp
->c_flags
&= ~CF_DATEFAB
;
333 fmt_scan (fmt
, *scanl
, slwidth
, dat
, NULL
);
336 bodycomp
->c_text
= saved_c_text
;
339 fputs (charstring_buffer (*scanl
), stdout
);
341 cptr
= fmt_findcomp ("encrypted");
342 encrypted
= cptr
&& cptr
->c_text
;
344 /* return dynamically allocated buffers to pool */
345 while ((cptr
= *savecomp
++)) {
349 if (scnout
&& (ferror(scnout
) || fclose (scnout
) == EOF
))
352 return state
!= FILEEOF
? SCNERR
: encrypted
? SCNENC
: SCNMSG
;
356 /* The following two functions allow access to the global gstate above. */
360 m_getfld_state_destroy (&gstate
);
364 scan_detect_mbox_style (FILE *f
)
366 m_unknown (&gstate
, f
);