]>
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.
11 #include "sbr/m_name.h"
12 #include "sbr/m_gmprot.h"
13 #include "sbr/m_getfld.h"
14 #include "sbr/getcpy.h"
15 #include "sbr/error.h"
16 #include "h/addrsbr.h"
17 #include "h/fmt_scan.h"
20 #include "sbr/terminal.h"
22 static struct format
*fmt
;
23 static struct comp
*datecomp
; /* pntr to "date" comp */
24 static struct comp
*bodycomp
; /* pntr to "body" pseudo-comp *
26 static int ncomps
= 0; /* # of interesting components */
27 static char **compbuffers
= 0; /* buffers for component text */
28 static struct comp
**used_buf
= 0; /* stack for comp that use buffers */
30 static int dat
[5]; /* aux. data for format routine */
32 static m_getfld_state_t gstate
; /* for accessor functions below */
34 #define DIEWRERR() adios (scnmsg, "write error on")
37 if (putc((c), scnout) == EOF) \
41 if (fputs(buf,scnout) == EOF)\
45 /* outnum determines how the input from inb is copied. If positive then
46 * it is the number of the message to create, e.g. for inc(1), and all
47 * of the email is copied into that message, with some tweaks. If 0,
48 * e.g. `scan 42', then reading inb can dubiously stop after a whole
49 * buffer of body, even though this might not be enough to fulfill the
50 * scan format and width. Or if -1 then no copy is being created, but
51 * all of inb must be read because the next message must be found, e.g.
52 * `scan -file foo.mbox'. */
55 scan (FILE *inb
, int innum
, int outnum
, char *nfs
, int width
, int curflg
,
56 int unseen
, char *folder
, long size
, int noisy
, charstring_t
*scanl
)
60 int i
, compnum
, encrypted
, state
;
61 char *cp
, *tmpbuf
, *startbody
, **nxtbuf
;
62 char *saved_c_text
= NULL
;
64 struct comp
**savecomp
;
69 static int rlwidth
, slwidth
;
71 /* first-time only initialization, which will always happen the
72 way the code is now, with callers initializing *scanl to NULL.
73 scanl used to be a global. */
78 tty_width
= sc_width();
81 width
= max(tty_width
, WIDTH
/ 2);
82 } else if (width
== 0) {
83 /* Unlimited width. */
86 dat
[3] = slwidth
= width
;
87 *scanl
= charstring_create (min(width
, NMH_BUFSIZ
));
91 /* Compile format string */
92 ncomps
= fmt_compile (nfs
, &fmt
, 1) + 2;
94 bodycomp
= fmt_findcomp("body");
95 datecomp
= fmt_findcomp("date");
96 cptr
= fmt_findcomp("folder");
98 cptr
->c_text
= mh_xstrdup(folder
);
99 if (fmt_addcompentry("encrypted")) {
102 cptr
= fmt_findcomp("dtimenow");
104 cptr
->c_text
= getcpy(dtimenow (0));
107 * In other programs I got rid of this complicated buffer switching,
108 * but since scan reads lots of messages at once and this complicated
109 * memory management, I decided to keep it; otherwise there was
110 * the potential for a lot of malloc() and free()s, and I could
111 * see the malloc() pool really getting fragmented. Maybe it
112 * wouldn't be an issue in practice; perhaps this will get
115 * So, some notes for what's going on:
117 * nxtbuf is an array of pointers that contains malloc()'d buffers
118 * to hold our component text. used_buf is an array of struct comp
119 * pointers that holds pointers to component structures we found while
120 * processing a message.
122 * We read in the message with m_getfld(), using "tmpbuf" as our
123 * input buffer. tmpbuf is set at the start of message processing
124 * to the first buffer in our buffer pool (nxtbuf).
126 * Every time we find a component we care about, we set that component's
127 * text buffer to the current value of tmpbuf, and then switch tmpbuf
128 * to the next buffer in our pool. We also add that component to
131 * When we're done, we go back and zero out all of the component
132 * text buffer pointers that we saved in used_buf.
134 * Note that this means c_text memory is NOT owned by the fmt_module
135 * and it's our responsibility to free it.
138 nxtbuf
= compbuffers
= mh_xcalloc(ncomps
, sizeof *nxtbuf
);
139 used_buf
= mh_xcalloc(ncomps
+ 1, sizeof *used_buf
);
140 used_buf
+= ncomps
+1; *--used_buf
= 0;
141 rlwidth
= NMH_BUFSIZ
;
142 for (i
= ncomps
; i
--; )
143 *nxtbuf
++ = mh_xmalloc(rlwidth
);
147 * each-message initialization
149 nxtbuf
= compbuffers
;
153 dat
[0] = innum
? innum
: outnum
;
158 * Get the first field. If the message is non-empty
159 * and we're doing an "inc", open the output file.
162 m_getfld_state_reset (&gstate
);
163 if ((state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
)) == FILEEOF
) {
165 advise("read", "unable to"); /* "read error" */
172 scnmsg
= m_name (outnum
);
173 if (*scnmsg
== '?') /* msg num out of range */
175 if ((scnout
= fopen (scnmsg
, "w")) == NULL
)
176 adios (scnmsg
, "unable to write");
179 /* scan - main loop */
181 bufsz
= rlwidth
, state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
)) {
192 * if we're interested in this component, save a pointer
193 * to the component text, then start using our next free
194 * buffer as the component temp buffer (buffer switching
195 * saves an extra copy of the component text).
197 if ((cptr
= fmt_findcasecomp(name
))) {
198 if (! cptr
->c_text
) {
199 cptr
->c_text
= tmpbuf
;
200 for (cp
= tmpbuf
+ strlen (tmpbuf
) - 1;
202 if (isspace ((unsigned char) *cp
))
211 while (state
== FLDPLUS
) {
213 state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
);
221 * A slight hack ... if we have less than rlwidth characters
222 * in the buffer, call m_getfld again.
225 if ((i
= strlen(tmpbuf
)) < rlwidth
) {
227 state
= m_getfld (&gstate
, name
, tmpbuf
+ i
, &bufsz
, inb
);
231 state
= FILEEOF
; /* stop now if scan cmd */
232 if (bodycomp
&& startbody
== NULL
)
241 * The previous code here used to call m_getfld() using
242 * pointers to the underlying output stdio buffers to
243 * avoid the extra copy. Tests by Markus Schnalke show
244 * no noticeable performance loss on larger mailboxes
245 * if we incur an extra copy, and messing around with
246 * internal stdio buffers is becoming more and more
247 * unportable as times go on. So from now on just deal
248 * with the overhead of an extra copy.
250 * Subtle change - with the previous code tmpbuf wasn't
251 * used, so we could reuse it for the {body} component.
252 * Now since we're using tmpbuf as our read buffer we
253 * need to save the beginning of the body for later.
254 * See the above (and below) use of startbody.
257 if (bodycomp
&& startbody
== NULL
) {
262 while (state
== BODY
) {
264 state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
);
273 fprintf (stderr
, "??Format error (message %d) in ",
274 outnum
? outnum
: innum
);
276 fprintf (stderr
, "??Format error in ");
278 fprintf (stderr
, "component %d\n", compnum
);
281 FPUTS ("\n\nBAD MSG:\n");
293 die("getfld() returned %d", state
);
298 * format and output the scan line.
302 advise("read", "unable to"); /* "read error" */
306 /* Save and restore buffer so we don't trash our dynamic pool! */
308 saved_c_text
= bodycomp
->c_text
;
309 bodycomp
->c_text
= startbody
;
316 dat
[2] = ftell(scnout
);
317 if (dat
[2] == EOF
) DIEWRERR();
320 if ((datecomp
&& !datecomp
->c_text
) || (!size
&& !outnum
)) {
323 fstat (fileno(inb
), &st
);
324 if (!size
&& !outnum
)
327 if (! datecomp
->c_text
) {
328 if (datecomp
->c_tws
== NULL
)
329 NEW0(datecomp
->c_tws
);
330 *datecomp
->c_tws
= *dlocaltime ((time_t *) &st
.st_mtime
);
331 datecomp
->c_flags
|= CF_DATEFAB
|CF_TRUE
;
333 datecomp
->c_flags
&= ~CF_DATEFAB
;
338 fmt_scan (fmt
, *scanl
, slwidth
, dat
, NULL
);
341 bodycomp
->c_text
= saved_c_text
;
344 fputs (charstring_buffer (*scanl
), stdout
);
346 cptr
= fmt_findcomp ("encrypted");
347 encrypted
= cptr
&& cptr
->c_text
;
349 /* return dynamically allocated buffers to pool */
350 while ((cptr
= *savecomp
++)) {
354 if (scnout
&& (ferror(scnout
) || fclose (scnout
) == EOF
))
357 return state
!= FILEEOF
? SCNERR
: encrypted
? SCNENC
: SCNMSG
;
361 /* The following two functions allow access to the global gstate above. */
365 m_getfld_state_destroy (&gstate
);
369 scan_detect_mbox_style (FILE *f
)
371 m_unknown (&gstate
, f
);