X-Git-Url: https://diplodocus.org/git/nmh/blobdiff_plain/1e18bf0ab892a2ae9d3151c248b652898f7d7116..fcaa3047b2a23fc0c36be518d1ca1ea0b0cb3b66:/uip/scansbr.c diff --git a/uip/scansbr.c b/uip/scansbr.c index 569991ec..b6df6830 100644 --- a/uip/scansbr.c +++ b/uip/scansbr.c @@ -5,19 +5,26 @@ * complete copyright information. */ -#include -#include -#include -#include -#include -#include +#include "h/mh.h" +#include "sbr/dtime.h" +#include "scansbr.h" +#include "sbr/m_name.h" +#include "sbr/m_gmprot.h" +#include "sbr/m_getfld.h" +#include "sbr/getcpy.h" +#include "sbr/error.h" +#include "h/addrsbr.h" +#include "h/fmt_scan.h" +#include "h/tws.h" +#include "h/utils.h" +#include "sbr/terminal.h" static struct format *fmt; static struct comp *datecomp; /* pntr to "date" comp */ static struct comp *bodycomp; /* pntr to "body" pseudo-comp * * (if referenced) */ static int ncomps = 0; /* # of interesting components */ -static char **compbuffers = 0; /* buffers for component text */ +static char **compbuffers = 0; /* buffers for component text */ static struct comp **used_buf = 0; /* stack for comp that use buffers */ static int dat[5]; /* aux. data for format routine */ @@ -26,16 +33,31 @@ static m_getfld_state_t gstate; /* for accessor functions below */ #define DIEWRERR() adios (scnmsg, "write error on") +#define PUTC(c) \ + if (putc((c), scnout) == EOF) \ + DIEWRERR(); + #define FPUTS(buf) {\ if (fputs(buf,scnout) == EOF)\ DIEWRERR();\ } +/* outnum determines how the input from inb is copied. If positive then + * it is the number of the message to create, e.g. for inc(1), and all + * of the email is copied into that message, with some tweaks. If 0, + * e.g. `scan 42', then reading inb can dubiously stop after a whole + * buffer of body, even though this might not be enough to fulfill the + * scan format and width. Or if -1 then no copy is being created, but + * all of inb must be read because the next message must be found, e.g. + * `scan -file foo.mbox'. */ + int scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg, int unseen, char *folder, long size, int noisy, charstring_t *scanl) { - int i, compnum, encrypted, state; + static bool deja_vu; + static int tty_width; + int i, compnum, state; char *cp, *tmpbuf, *startbody, **nxtbuf; char *saved_c_text = NULL; struct comp *cptr; @@ -51,9 +73,12 @@ scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg, scanl used to be a global. */ if (! *scanl) { if (width == -1) { - /* Default: width of the terminal, but at least WIDTH/2. */ - if ((width = sc_width ()) < WIDTH/2) - width = WIDTH/2; + if (!deja_vu) { + deja_vu = true; + tty_width = sc_width(); + } + + width = max(tty_width, WIDTH / 2); } else if (width == 0) { /* Unlimited width. */ width = INT_MAX; @@ -71,9 +96,6 @@ scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg, cptr = fmt_findcomp("folder"); if (cptr && folder) cptr->c_text = mh_xstrdup(folder); - if (fmt_addcompentry("encrypted")) { - ncomps++; - } cptr = fmt_findcomp("dtimenow"); if (cptr) cptr->c_text = getcpy(dtimenow (0)); @@ -143,16 +165,12 @@ scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg, return SCNEOF; } - if (outnum) { - if (outnum > 0) { - scnmsg = m_name (outnum); - if (*scnmsg == '?') /* msg num out of range */ - return SCNNUM; - } else { - scnmsg = "/dev/null"; - } - if ((scnout = fopen (scnmsg, "w")) == NULL) - adios (scnmsg, "unable to write"); + if (outnum > 0) { + scnmsg = m_name (outnum); + if (*scnmsg == '?') /* msg num out of range */ + return SCNNUM; + if ((scnout = fopen (scnmsg, "w")) == NULL) + adios (scnmsg, "unable to write"); } /* scan - main loop */ @@ -162,9 +180,9 @@ scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg, case FLD: case FLDPLUS: compnum++; - if (outnum) { + if (scnout) { FPUTS (name); - if ( putc (':', scnout) == EOF) DIEWRERR(); + PUTC(':'); FPUTS (tmpbuf); } /* @@ -190,7 +208,7 @@ scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg, while (state == FLDPLUS) { bufsz = rlwidth; state = m_getfld (&gstate, name, tmpbuf, &bufsz, inb); - if (outnum) + if (scnout) FPUTS (tmpbuf); } break; @@ -206,14 +224,16 @@ scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg, state = m_getfld (&gstate, name, tmpbuf + i, &bufsz, inb); } - if (! outnum) { + if (outnum == 0) { state = FILEEOF; /* stop now if scan cmd */ if (bodycomp && startbody == NULL) - startbody = tmpbuf; + startbody = tmpbuf; goto finished; } - if (putc ('\n', scnout) == EOF) DIEWRERR(); - FPUTS (tmpbuf); + if (scnout) { + PUTC('\n'); + FPUTS (tmpbuf); + } /* * The previous code here used to call m_getfld() using * pointers to the underlying output stdio buffers to @@ -239,13 +259,14 @@ body:; while (state == BODY) { bufsz = rlwidth; state = m_getfld (&gstate, name, tmpbuf, &bufsz, inb); - FPUTS(tmpbuf); + if (scnout) + FPUTS(tmpbuf); } goto finished; case LENERR: case FMTERR: - if (innum) + if (innum) fprintf (stderr, "??Format error (message %d) in ", outnum ? outnum : innum); else @@ -253,10 +274,10 @@ body:; fprintf (stderr, "component %d\n", compnum); - if (outnum) { + if (scnout) { FPUTS ("\n\nBAD MSG:\n"); FPUTS (name); - if (putc ('\n', scnout) == EOF) DIEWRERR(); + PUTC('\n'); state = BODY; goto body; } @@ -266,7 +287,7 @@ body:; goto finished; default: - adios (NULL, "getfld() returned %d", state); + die("getfld() returned %d", state); } } @@ -287,7 +308,7 @@ finished: if (size) dat[2] = size; - else if (outnum > 0) + else if (scnout) { dat[2] = ftell(scnout); if (dat[2] == EOF) DIEWRERR(); @@ -319,28 +340,27 @@ finished: if (noisy) fputs (charstring_buffer (*scanl), stdout); - cptr = fmt_findcomp ("encrypted"); - encrypted = cptr && cptr->c_text; - /* return dynamically allocated buffers to pool */ while ((cptr = *savecomp++)) { cptr->c_text = NULL; } - if (outnum && (ferror(scnout) || fclose (scnout) == EOF)) + if (scnout && (ferror(scnout) || fclose (scnout) == EOF)) DIEWRERR(); - return (state != FILEEOF ? SCNERR : encrypted ? SCNENC : SCNMSG); + return state != FILEEOF ? SCNERR : SCNMSG; } /* The following two functions allow access to the global gstate above. */ void -scan_finished(void) { +scan_finished(void) +{ m_getfld_state_destroy (&gstate); } void -scan_detect_mbox_style (FILE *f) { +scan_detect_mbox_style (FILE *f) +{ m_unknown (&gstate, f); }