]>
diplodocus.org Git - nmh/blob - uip/scansbr.c
8030715fab61e5db973e9e0a7fc47852746e5c35
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
, 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 cptr
= fmt_findcomp("dtimenow");
101 cptr
->c_text
= getcpy(dtimenow (0));
104 * In other programs I got rid of this complicated buffer switching,
105 * but since scan reads lots of messages at once and this complicated
106 * memory management, I decided to keep it; otherwise there was
107 * the potential for a lot of malloc() and free()s, and I could
108 * see the malloc() pool really getting fragmented. Maybe it
109 * wouldn't be an issue in practice; perhaps this will get
112 * So, some notes for what's going on:
114 * nxtbuf is an array of pointers that contains malloc()'d buffers
115 * to hold our component text. used_buf is an array of struct comp
116 * pointers that holds pointers to component structures we found while
117 * processing a message.
119 * We read in the message with m_getfld(), using "tmpbuf" as our
120 * input buffer. tmpbuf is set at the start of message processing
121 * to the first buffer in our buffer pool (nxtbuf).
123 * Every time we find a component we care about, we set that component's
124 * text buffer to the current value of tmpbuf, and then switch tmpbuf
125 * to the next buffer in our pool. We also add that component to
128 * When we're done, we go back and zero out all of the component
129 * text buffer pointers that we saved in used_buf.
131 * Note that this means c_text memory is NOT owned by the fmt_module
132 * and it's our responsibility to free it.
135 nxtbuf
= compbuffers
= mh_xcalloc(ncomps
, sizeof *nxtbuf
);
136 used_buf
= mh_xcalloc(ncomps
+ 1, sizeof *used_buf
);
137 used_buf
+= ncomps
+1; *--used_buf
= 0;
138 rlwidth
= NMH_BUFSIZ
;
139 for (i
= ncomps
; i
--; )
140 *nxtbuf
++ = mh_xmalloc(rlwidth
);
144 * each-message initialization
146 nxtbuf
= compbuffers
;
150 dat
[0] = innum
? innum
: outnum
;
155 * Get the first field. If the message is non-empty
156 * and we're doing an "inc", open the output file.
159 m_getfld_state_reset (&gstate
);
160 if ((state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
)) == FILEEOF
) {
162 advise("read", "unable to"); /* "read error" */
169 scnmsg
= m_name (outnum
);
170 if (*scnmsg
== '?') /* msg num out of range */
172 if ((scnout
= fopen (scnmsg
, "w")) == NULL
)
173 adios (scnmsg
, "unable to write");
176 /* scan - main loop */
178 bufsz
= rlwidth
, state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
)) {
189 * if we're interested in this component, save a pointer
190 * to the component text, then start using our next free
191 * buffer as the component temp buffer (buffer switching
192 * saves an extra copy of the component text).
194 if ((cptr
= fmt_findcasecomp(name
))) {
195 if (! cptr
->c_text
) {
196 cptr
->c_text
= tmpbuf
;
197 for (cp
= tmpbuf
+ strlen (tmpbuf
) - 1;
199 if (isspace ((unsigned char) *cp
))
208 while (state
== FLDPLUS
) {
210 state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
);
218 * A slight hack ... if we have less than rlwidth characters
219 * in the buffer, call m_getfld again.
222 if ((i
= strlen(tmpbuf
)) < rlwidth
) {
224 state
= m_getfld (&gstate
, name
, tmpbuf
+ i
, &bufsz
, inb
);
228 state
= FILEEOF
; /* stop now if scan cmd */
229 if (bodycomp
&& startbody
== NULL
)
238 * The previous code here used to call m_getfld() using
239 * pointers to the underlying output stdio buffers to
240 * avoid the extra copy. Tests by Markus Schnalke show
241 * no noticeable performance loss on larger mailboxes
242 * if we incur an extra copy, and messing around with
243 * internal stdio buffers is becoming more and more
244 * unportable as times go on. So from now on just deal
245 * with the overhead of an extra copy.
247 * Subtle change - with the previous code tmpbuf wasn't
248 * used, so we could reuse it for the {body} component.
249 * Now since we're using tmpbuf as our read buffer we
250 * need to save the beginning of the body for later.
251 * See the above (and below) use of startbody.
254 if (bodycomp
&& startbody
== NULL
) {
259 while (state
== BODY
) {
261 state
= m_getfld (&gstate
, name
, tmpbuf
, &bufsz
, inb
);
270 fprintf (stderr
, "??Format error (message %d) in ",
271 outnum
? outnum
: innum
);
273 fprintf (stderr
, "??Format error in ");
275 fprintf (stderr
, "component %d\n", compnum
);
278 FPUTS ("\n\nBAD MSG:\n");
290 die("getfld() returned %d", state
);
295 * format and output the scan line.
299 advise("read", "unable to"); /* "read error" */
303 /* Save and restore buffer so we don't trash our dynamic pool! */
305 saved_c_text
= bodycomp
->c_text
;
306 bodycomp
->c_text
= startbody
;
313 dat
[2] = ftell(scnout
);
314 if (dat
[2] == EOF
) DIEWRERR();
317 if ((datecomp
&& !datecomp
->c_text
) || (!size
&& !outnum
)) {
320 fstat (fileno(inb
), &st
);
321 if (!size
&& !outnum
)
324 if (! datecomp
->c_text
) {
325 if (datecomp
->c_tws
== NULL
)
326 NEW0(datecomp
->c_tws
);
327 *datecomp
->c_tws
= *dlocaltime ((time_t *) &st
.st_mtime
);
328 datecomp
->c_flags
|= CF_DATEFAB
|CF_TRUE
;
330 datecomp
->c_flags
&= ~CF_DATEFAB
;
335 fmt_scan (fmt
, *scanl
, slwidth
, dat
, NULL
);
338 bodycomp
->c_text
= saved_c_text
;
341 fputs (charstring_buffer (*scanl
), stdout
);
343 /* return dynamically allocated buffers to pool */
344 while ((cptr
= *savecomp
++)) {
348 if (scnout
&& (ferror(scnout
) || fclose (scnout
) == EOF
))
351 return state
!= FILEEOF
? SCNERR
: SCNMSG
;
355 /* The following two functions allow access to the global gstate above. */
359 m_getfld_state_destroy (&gstate
);
363 scan_detect_mbox_style (FILE *f
)
365 m_unknown (&gstate
, f
);