X-Git-Url: https://diplodocus.org/git/nmh/blobdiff_plain/9e1f6d6d4c2e154c7274e7fb3f1931924e458b17..ef1ba39e8dae81091b6c3e73e72825ef6edea3c6:/uip/forwsbr.c?ds=sidebyside diff --git a/uip/forwsbr.c b/uip/forwsbr.c old mode 100755 new mode 100644 index 20879ec7..3943071b --- a/uip/forwsbr.c +++ b/uip/forwsbr.c @@ -19,21 +19,43 @@ */ static char msgbuf[256]; -#define COMPFREE(c) if (c->c_text) free(c->c_text) +#define COMPFREE(c) mh_xfree(c->c_text) + +/* + * A list of components we treat as addresses + */ + +static char *addrcomps[] = { + "from", + "sender", + "reply-to", + "to", + "cc", + "bcc", + "resent-from", + "resent-sender", + "resent-reply-to", + "resent-to", + "resent-cc", + "resent-bcc", + NULL +}; int build_form (char *form, char *digest, int *dat, char *from, char *to, char *cc, char *fcc, char *subject, char *inputfile) { int in; - int fmtsize, state, char_read = 0; - unsigned i; - register char *nfs; - char *line, tmpfil[BUFSIZ], name[NAMESZ]; + int fmtsize, state; + int i; + char *nfs; + char tmpfil[BUFSIZ], name[NAMESZ], **ap; + charstring_t line; FILE *tmp; - register struct comp *cptr; + struct comp *cptr; struct format *fmt; char *cp = NULL; + m_getfld_state_t gstate = 0; /* * Open the message we'll be scanning for components @@ -47,7 +69,17 @@ build_form (char *form, char *digest, int *dat, char *from, char *to, fmtsize = strlen (nfs) + 256; /* Compile format string */ - (void) fmt_compile (nfs, &fmt); + (void) fmt_compile (nfs, &fmt, 1); + + /* + * Mark any components tagged as address components + */ + + for (ap = addrcomps; *ap; ap++) { + cptr = fmt_findcomp (*ap); + if (cptr) + cptr->c_type |= CT_ADDR; + } /* * Process our message and save all relevant components @@ -56,8 +88,9 @@ build_form (char *form, char *digest, int *dat, char *from, char *to, * these routines? */ - for (state = FLD;;) { - state = m_getfld(state, name, msgbuf, sizeof(msgbuf), tmp); + for (;;) { + int msg_count = sizeof msgbuf; + state = m_getfld (&gstate, name, msgbuf, &msg_count, tmp); switch (state) { case FLD: case FLDPLUS: @@ -66,39 +99,19 @@ build_form (char *form, char *digest, int *dat, char *from, char *to, * a copy. We don't do all of that weird buffer switching * that replout does. */ - if ((cptr = wantcomp[CHASH(name)])) - do { - if (mh_strcasecmp(name, cptr->c_name) == 0) { - char_read += msg_count; - if (! cptr->c_text) { - i = strlen(cptr->c_text = strdup(msgbuf)) - 1; - if (cptr->c_text[i] == '\n') - cptr->c_text[i] = '\0'; - } else { - i = strlen(cptr->c_text) - 1; - if (cptr->c_text[i] == '\n') { - if (cptr->c_type & CT_ADDR) { - cptr->c_text[i] = '\0'; - cptr->c_text = add(",\n\t", - cptr->c_text); - } else { - cptr->c_text = add ("\t", cptr->c_text); - } - } - cptr->c_text = add(msgbuf, cptr->c_text); - } - while (state == FLDPLUS) { - state = m_getfld(state, name, msgbuf, - sizeof(msgbuf), tmp); - cptr->c_text = add(msgbuf, cptr->c_text); - char_read += msg_count; - } - break; - } - } while ((cptr = cptr->c_next)); - - while (state == FLDPLUS) - state = m_getfld(state, name, msgbuf, sizeof(msgbuf), tmp); + + i = fmt_addcomptext(name, msgbuf); + if (i != -1) { + while (state == FLDPLUS) { + msg_count = sizeof msgbuf; + state = m_getfld (&gstate, name, msgbuf, &msg_count, tmp); + fmt_appendcomp(i, name, msgbuf); + } + } + while (state == FLDPLUS) { + msg_count = sizeof msgbuf; + state = m_getfld (&gstate, name, msgbuf, &msg_count, tmp); + } break; case LENERR: @@ -115,57 +128,65 @@ build_form (char *form, char *digest, int *dat, char *from, char *to, /* * Override any components just in case they were included in the * input message. Also include command-line components given here + * + * With the memory rework I've changed things so we always get copies + * of these strings; I don't like the idea that the caller of this + * function has to know to pass in already-allocated memory (and that + * it will be free()'d by us). */ finished: + m_getfld_state_destroy (&gstate); - FINDCOMP (cptr, "digest"); + cptr = fmt_findcomp ("digest"); if (cptr) { COMPFREE(cptr); - cptr->c_text = digest; + cptr->c_text = getcpy(digest); } - FINDCOMP (cptr, "nmh-date"); + cptr = fmt_findcomp ("nmh-date"); if (cptr) { COMPFREE(cptr); cptr->c_text = getcpy(dtimenow (0)); } - FINDCOMP (cptr, "nmh-from"); + cptr = fmt_findcomp ("nmh-from"); if (cptr) { COMPFREE(cptr); - cptr->c_text = from; + cptr->c_text = getcpy(from); } - FINDCOMP (cptr, "nmh-to"); + cptr = fmt_findcomp ("nmh-to"); if (cptr) { COMPFREE(cptr); - cptr->c_text = to; + cptr->c_text = getcpy(to); } - FINDCOMP (cptr, "nmh-cc"); + cptr = fmt_findcomp ("nmh-cc"); if (cptr) { COMPFREE(cptr); - cptr->c_text = cc; + cptr->c_text = getcpy(cc); } - FINDCOMP (cptr, "nmh-subject"); + cptr = fmt_findcomp ("nmh-subject"); if (cptr) { COMPFREE(cptr); - cptr->c_text = subject; + cptr->c_text = getcpy(subject); } - FINDCOMP (cptr, "fcc"); + cptr = fmt_findcomp ("fcc"); if (cptr) { COMPFREE(cptr); - cptr->c_text = fcc; + cptr->c_text = getcpy(fcc); } cp = m_mktemp2(NULL, invo_name, NULL, &tmp); - if (cp == NULL) adios("forw", "unable to create temporary file"); + if (cp == NULL) { + adios(NULL, "unable to create temporary file in %s", get_temp_dir()); + } strncpy (tmpfil, cp, sizeof(tmpfil)); - unlink (tmpfil); + (void) m_unlink (tmpfil); if ((in = dup (fileno (tmp))) == NOTOK) adios ("dup", "unable to"); - line = mh_xmalloc ((unsigned) fmtsize); - fmt_scan (fmt, line, fmtsize, dat); - fputs (line, tmp); - free (line); + line = charstring_create (fmtsize); + fmt_scan (fmt, line, fmtsize, dat, NULL); + fputs (charstring_buffer (line), tmp); + charstring_free (line); if (fclose (tmp)) adios (tmpfil, "error writing"); @@ -175,9 +196,7 @@ finished: * Free any component buffers that we allocated */ - for (i = 0; i < (sizeof(wantcomp) / sizeof(struct comp)); i++) - for (cptr = wantcomp[i]; cptr != NULL; cptr = cptr->c_next) - COMPFREE(cptr); + fmt_free(fmt, 1); return in; }