X-Git-Url: http://git.marmaro.de/?a=blobdiff_plain;f=uip%2Fscansbr.c;h=d1868451fdf45dca900d99519ec3038e49197531;hb=a97c3e637eaf2e2f4ecd8cb93b11795afb980988;hp=7eef85f97ecfa378b116cd27df52834cd2ca905c;hpb=bd4fca3a8a15b35a04bdc333503c901d677c12eb;p=mmh diff --git a/uip/scansbr.c b/uip/scansbr.c index 7eef85f..d186845 100644 --- a/uip/scansbr.c +++ b/uip/scansbr.c @@ -31,10 +31,6 @@ #define SBUFSIZ 512 static struct format *fmt; -#ifdef JLR -static struct format *fmt_top; -#endif /* JLR */ - static struct comp *datecomp; /* pntr to "date" comp */ static struct comp *bodycomp; /* pntr to "body" pseudo-comp * * (if referenced) */ @@ -58,7 +54,11 @@ char *scanl = 0; /* text of most recent scanline */ */ static int mh_fputs(char *, FILE *); +#ifdef MULTIBYTE_SUPPORT #define SCAN_CHARWIDTH MB_CUR_MAX +#else +#define SCAN_CHARWIDTH 1 +#endif int scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg, @@ -74,6 +74,7 @@ scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg, FILE *scnout = NULL; char name[NAMESZ]; static int rlwidth, slwidth; + static size_t scanl_size; /* first-time only initialization */ if (!scanl) { @@ -84,32 +85,60 @@ scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg, width = MAXSCANL; } dat[3] = slwidth = width; - scanl = (char *) mh_xmalloc((size_t) SCAN_CHARWIDTH * (slwidth + 2) ); + /* Arbitrarily allocate 20 * slwidth to provide room for lots + of escape sequences. */ + scanl_size = SCAN_CHARWIDTH * (20 * slwidth + 2); + scanl = (char *) mh_xmalloc (scanl_size); if (outnum) umask(~m_gmprot()); /* Compile format string */ - ncomps = fmt_compile (nfs, &fmt) + 1; - -#ifdef JLR - fmt_top = fmt; -#endif /* JLR */ - FINDCOMP(bodycomp, "body"); - FINDCOMP(datecomp, "date"); - FINDCOMP(cptr, "folder"); + ncomps = fmt_compile (nfs, &fmt, 1) + 1; + + bodycomp = fmt_findcomp("body"); + datecomp = fmt_findcomp("date"); + cptr = fmt_findcomp("folder"); if (cptr && folder) - cptr->c_text = folder; - FINDCOMP(cptr, "encrypted"); - if (!cptr) - if ((cptr = (struct comp *) calloc (1, sizeof(*cptr)))) { - cptr->c_name = "encrypted"; - cptr->c_next = wantcomp[i = CHASH (cptr->c_name)]; - wantcomp[i] = cptr; + cptr->c_text = getcpy(folder); + if (fmt_addcompentry("encrypted")) { ncomps++; } - FINDCOMP (cptr, "dtimenow"); + cptr = fmt_findcomp("dtimenow"); if (cptr) cptr->c_text = getcpy(dtimenow (0)); + + /* + * In other programs I got rid of this complicated buffer switching, + * but since scan reads lots of messages at once and this complicated + * memory management, I decided to keep it; otherwise there was + * the potential for a lot of malloc() and free()s, and I could + * see the malloc() pool really getting fragmented. Maybe it + * wouldn't be an issue in practice; perhaps this will get + * revisited someday. + * + * So, some notes for what's going on: + * + * nxtbuf is an array of pointers that contains malloc()'d buffers + * to hold our component text. used_buf is an array of struct comp + * pointers that holds pointers to component structures we found while + * processing a message. + * + * We read in the message with m_getfld(), using "tmpbuf" as our + * input buffer. tmpbuf is set at the start of message processing + * to the first buffer in our buffer pool (nxtbuf). + * + * Every time we find a component we care about, we set that component's + * text buffer to the current value of tmpbuf, and then switch tmpbuf + * to the next buffer in our pool. We also add that component to + * our used_buf pool. + * + * When we're done, we go back and zero out all of the component + * text buffer pointers that we saved in used_buf. + * + * Note that this means c_text memory is NOT owned by the fmt_module + * and it's our responsibility to free it. + */ + nxtbuf = compbuffers = (char **) calloc((size_t) ncomps, sizeof(char *)); if (nxtbuf == NULL) adios (NULL, "unable to allocate component buffers"); @@ -175,23 +204,18 @@ scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg, * buffer as the component temp buffer (buffer switching * saves an extra copy of the component text). */ - if ((cptr = wantcomp[CHASH(name)])) { - do { - if (!mh_strcasecmp(name, cptr->c_name)) { - if (! cptr->c_text) { - cptr->c_text = tmpbuf; - for (cp = tmpbuf + strlen (tmpbuf) - 1; + if ((cptr = fmt_findcasecomp(name))) { + if (! cptr->c_text) { + cptr->c_text = tmpbuf; + for (cp = tmpbuf + strlen (tmpbuf) - 1; cp >= tmpbuf; cp--) - if (isspace (*cp)) - *cp = 0; - else - break; - *--savecomp = cptr; - tmpbuf = *nxtbuf++; - } - break; - } - } while ((cptr = cptr->c_next)); + if (isspace (*cp)) + *cp = 0; + else + break; + *--savecomp = cptr; + tmpbuf = *nxtbuf++; + } } while (state == FLDPLUS) { @@ -326,13 +350,7 @@ finished: } } - fmt_scan (fmt, scanl, slwidth, dat); - -#if 0 - fmt = fmt_scan (fmt, scanl, slwidth, dat); - if (!fmt) - fmt = fmt_top; /* reset for old format files */ -#endif + fmt_scan (fmt, scanl, scanl_size, slwidth, dat); if (bodycomp) bodycomp->c_text = saved_c_text; @@ -340,7 +358,7 @@ finished: if (noisy) fputs (scanl, stdout); - FINDCOMP (cptr, "encrypted"); + cptr = fmt_findcomp ("encrypted"); encrypted = cptr && cptr->c_text; /* return dynamically allocated buffers to pool */ @@ -357,19 +375,6 @@ finished: } -/* - * Cheat: we are loaded with adrparse, which wants a routine called - * OfficialName(). We call adrparse:getm() with the correct arguments - * to prevent OfficialName() from being called. Hence, the following - * is to keep the loader happy. - */ -char * -OfficialName (char *name) -{ - return name; -} - - static int mh_fputs(char *s, FILE *stream) {