]>
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/fmt_scan.h>
11 #include <h/scansbr.h>
14 #include "sbr/terminal.h"
16 static struct format
*fmt
;
17 static struct comp
*datecomp
; /* pntr to "date" comp */
18 static struct comp
*bodycomp
; /* pntr to "body" pseudo-comp *
20 static int ncomps
= 0; /* # of interesting components */
21 static char **compbuffers
= 0; /* buffers for component text */
22 static struct comp
**used_buf
= 0; /* stack for comp that use buffers */
24 static int dat
[5]; /* aux. data for format routine */
26 static m_getfld_state_t gstate
; /* for accessor functions below */
28 #define DIEWRERR() adios (scnmsg, "write error on")
31 if (putc((c), scnout) == EOF) \
35 if (fputs(buf,scnout) == EOF)\
39 /* outnum determines how the input from inb is copied. If positive then
40 * it is the number of the message to create, e.g. for inc(1), and all
41 * of the email is copied into that message, with some tweaks. If 0,
42 * e.g. `scan 42', then reading inb can dubiously stop after a whole
43 * buffer of body, even though this might not be enough to fulfill the
44 * scan format and width. Or if -1 then no copy is being created, but
45 * all of inb must be read because the next message must be found, e.g.
46 * `scan -file foo.mbox'. */
49 scan (FILE *inb
, int innum
, int outnum
, char *nfs
, int width
, int curflg
,
50 int unseen
, char *folder
, long size
, int noisy
, charstring_t
*scanl
)
54 int i
, compnum
, encrypted
, state
;
55 char *cp
, *tmpbuf
, *startbody
, **nxtbuf
;
56 char *saved_c_text
= NULL
;
58 struct comp
**savecomp
;
63 static int rlwidth
, slwidth
;
65 /* first-time only initialization, which will always happen the
66 way the code is now, with callers initializing *scanl to NULL.
67 scanl used to be a global. */
72 tty_width
= sc_width();
75 width
= max(tty_width
, WIDTH
/ 2);
76 } else if (width
== 0) {
77 /* Unlimited width. */
80 dat
[3] = slwidth
= width
;
81 *scanl
= charstring_create (min(width
, NMH_BUFSIZ
));
85 /* Compile format string */
86 ncomps
= fmt_compile (nfs
, &fmt
, 1) + 2;
88 bodycomp
= fmt_findcomp("body");
89 datecomp
= fmt_findcomp("date");
90 cptr
= fmt_findcomp("folder");
92 cptr
->c_text
= mh_xstrdup(folder
);
93 if (fmt_addcompentry("encrypted")) {
96 cptr
= fmt_findcomp("dtimenow");
98 cptr
->c_text
= getcpy(dtimenow (0));
101 * In other programs I got rid of this complicated buffer switching,
102 * but since scan reads lots of messages at once and this complicated
103 * memory management, I decided to keep it; otherwise there was
104 * the potential for a lot of malloc() and free()s, and I could
105 * see the malloc() pool really getting fragmented. Maybe it
106 * wouldn't be an issue in practice; perhaps this will get
109 * So, some notes for what's going on:
111 * nxtbuf is an array of pointers that contains malloc()'d buffers
112 * to hold our component text. used_buf is an array of struct comp
113 * pointers that holds pointers to component structures we found while
114 * processing a message.
116 * We read in the message with m_getfld(), using "tmpbuf" as our
117 * input buffer. tmpbuf is set at the start of message processing
118 * to the first buffer in our buffer pool (nxtbuf).
120 * Every time we find a component we care about, we set that component's
121 * text buffer to the current value of tmpbuf, and then switch tmpbuf
122 * to the next buffer in our pool. We also add that component to
125 * When we're done, we go back and zero out all of the component
126 * text buffer pointers that we saved in used_buf.
128 * Note that this means c_text memory is NOT owned by the fmt_module
129 * and it's our responsibility to free it.
132 nxtbuf
= compbuffers
= mh_xcalloc(ncomps
, sizeof *nxtbuf
);
133 used_buf
= mh_xcalloc(ncomps
+ 1, sizeof *used_buf
);
134 used_buf
+= ncomps
+1; *--used_buf
= 0;
135 rlwidth
= NMH_BUFSIZ
;
136 for (i
= ncomps
; i
--; )
137 *nxtbuf
++ = mh_xmalloc(rlwidth
);
141 * each-message initialization
143 nxtbuf
= compbuffers
;
147 dat
[0] = innum
? innum
: outnum
;
152 * Get the first field. If the message is non-empty
153 * and we're doing an "inc", open the output file.
156 m_getfld_state_reset (&gstate
);
157 if ((state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
)) == FILEEOF
) {
159 advise("read", "unable to"); /* "read error" */
166 scnmsg
= m_name (outnum
);
167 if (*scnmsg
== '?') /* msg num out of range */
169 if ((scnout
= fopen (scnmsg
, "w")) == NULL
)
170 adios (scnmsg
, "unable to write");
173 /* scan - main loop */
175 bufsz
= rlwidth
, state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
)) {
186 * if we're interested in this component, save a pointer
187 * to the component text, then start using our next free
188 * buffer as the component temp buffer (buffer switching
189 * saves an extra copy of the component text).
191 if ((cptr
= fmt_findcasecomp(name
))) {
192 if (! cptr
->c_text
) {
193 cptr
->c_text
= tmpbuf
;
194 for (cp
= tmpbuf
+ strlen (tmpbuf
) - 1;
196 if (isspace ((unsigned char) *cp
))
205 while (state
== FLDPLUS
) {
207 state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
);
215 * A slight hack ... if we have less than rlwidth characters
216 * in the buffer, call m_getfld again.
219 if ((i
= strlen(tmpbuf
)) < rlwidth
) {
221 state
= m_getfld (&gstate
, name
, tmpbuf
+ i
, &bufsz
, inb
);
225 state
= FILEEOF
; /* stop now if scan cmd */
226 if (bodycomp
&& startbody
== NULL
)
235 * The previous code here used to call m_getfld() using
236 * pointers to the underlying output stdio buffers to
237 * avoid the extra copy. Tests by Markus Schnalke show
238 * no noticeable performance loss on larger mailboxes
239 * if we incur an extra copy, and messing around with
240 * internal stdio buffers is becoming more and more
241 * unportable as times go on. So from now on just deal
242 * with the overhead of an extra copy.
244 * Subtle change - with the previous code tmpbuf wasn't
245 * used, so we could reuse it for the {body} component.
246 * Now since we're using tmpbuf as our read buffer we
247 * need to save the beginning of the body for later.
248 * See the above (and below) use of startbody.
251 if (bodycomp
&& startbody
== NULL
) {
256 while (state
== BODY
) {
258 state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
);
267 fprintf (stderr
, "??Format error (message %d) in ",
268 outnum
? outnum
: innum
);
270 fprintf (stderr
, "??Format error in ");
272 fprintf (stderr
, "component %d\n", compnum
);
275 FPUTS ("\n\nBAD MSG:\n");
287 adios (NULL
, "getfld() returned %d", state
);
292 * format and output the scan line.
296 advise("read", "unable to"); /* "read error" */
300 /* Save and restore buffer so we don't trash our dynamic pool! */
302 saved_c_text
= bodycomp
->c_text
;
303 bodycomp
->c_text
= startbody
;
310 dat
[2] = ftell(scnout
);
311 if (dat
[2] == EOF
) DIEWRERR();
314 if ((datecomp
&& !datecomp
->c_text
) || (!size
&& !outnum
)) {
317 fstat (fileno(inb
), &st
);
318 if (!size
&& !outnum
)
321 if (! datecomp
->c_text
) {
322 if (datecomp
->c_tws
== NULL
)
323 NEW0(datecomp
->c_tws
);
324 *datecomp
->c_tws
= *dlocaltime ((time_t *) &st
.st_mtime
);
325 datecomp
->c_flags
|= CF_DATEFAB
|CF_TRUE
;
327 datecomp
->c_flags
&= ~CF_DATEFAB
;
332 fmt_scan (fmt
, *scanl
, slwidth
, dat
, NULL
);
335 bodycomp
->c_text
= saved_c_text
;
338 fputs (charstring_buffer (*scanl
), stdout
);
340 cptr
= fmt_findcomp ("encrypted");
341 encrypted
= cptr
&& cptr
->c_text
;
343 /* return dynamically allocated buffers to pool */
344 while ((cptr
= *savecomp
++)) {
348 if (scnout
&& (ferror(scnout
) || fclose (scnout
) == EOF
))
351 return (state
!= FILEEOF
? SCNERR
: encrypted
? SCNENC
: SCNMSG
);
355 /* The following two functions allow access to the global gstate above. */
357 scan_finished(void) {
358 m_getfld_state_destroy (&gstate
);
362 scan_detect_mbox_style (FILE *f
) {
363 m_unknown (&gstate
, f
);