X-Git-Url: http://git.marmaro.de/?a=blobdiff_plain;f=uip%2Freplsbr.c;h=a6d0f233b54e455a577731596991b199c37ad620;hb=356f49083391bd018b6c5dfed21e59247b4cc74a;hp=7515f1169f8b34e8ad2a127dc434fc94f4635bf2;hpb=794dab6aa5027ffaf024506140c258f6a0a9322d;p=mmh diff --git a/uip/replsbr.c b/uip/replsbr.c index 7515f11..a6d0f23 100644 --- a/uip/replsbr.c +++ b/uip/replsbr.c @@ -2,13 +2,17 @@ /* * replsbr.c -- routines to help repl along... * - * $Id$ + * This code is Copyright (c) 2002, by the authors of nmh. See the + * COPYRIGHT file in the root directory of the nmh distribution for + * complete copyright information. */ #include #include #include +#include #include /* L_SET */ +#include extern short ccto; /* from repl.c */ extern short cccc; @@ -20,7 +24,8 @@ static int dftype=0; static char *badaddrs = NULL; static char *dfhost = NULL; -static struct mailname mq = { NULL }; +static struct mailname mq; +static int nodupcheck = 0; /* If set, no check for duplicates */ /* * Buffer size for content part of header fields. @@ -59,12 +64,12 @@ static char *addrcomps[] = { * static prototypes */ static int insert (struct mailname *); -static void replfilter (FILE *, FILE *, char *); +static void replfilter (FILE *, FILE *, char *, int); void replout (FILE *inb, char *msg, char *drft, struct msgs *mp, int outputlinelen, - int mime, char *form, char *filter, char *fcc) + int mime, char *form, char *filter, char *fcc, int fmtproc) { register int state, i; register struct comp *cptr; @@ -72,14 +77,18 @@ replout (FILE *inb, char *msg, char *drft, struct msgs *mp, int outputlinelen, register char **nxtbuf; register char **ap; register struct comp **savecomp; - int char_read = 0, format_len; - char name[NAMESZ], *scanl, *cp; + int char_read = 0, format_len, mask; + char name[NAMESZ], *scanl; + unsigned char *cp; FILE *out; + NMH_UNUSED (msg); - umask(~m_gmprot()); + mask = umask(~m_gmprot()); if ((out = fopen (drft, "w")) == NULL) adios (drft, "unable to create"); + umask(mask); + /* get new format string */ cp = new_fs (form, NULL, NULL); format_len = strlen (cp); @@ -97,8 +106,7 @@ replout (FILE *inb, char *msg, char *drft, struct msgs *mp, int outputlinelen, *--savecomp = NULL; /* point at zero'd end minus 1 */ for (i = ncomps; i--; ) - if (!(*nxtbuf++ = malloc(SBUFSIZ))) - adios (NULL, "unable to allocate component buffer"); + *nxtbuf++ = mh_xmalloc(SBUFSIZ); nxtbuf = compbuffers; /* point at start */ tmpbuf = *nxtbuf++; @@ -152,7 +160,7 @@ replout (FILE *inb, char *msg, char *drft, struct msgs *mp, int outputlinelen, */ if ((cptr = wantcomp[CHASH(name)])) do { - if (!strcasecmp(name, cptr->c_name)) { + if (!mh_strcasecmp(name, cptr->c_name)) { char_read += msg_count; if (! cptr->c_text) { cptr->c_text = tmpbuf; @@ -223,7 +231,7 @@ finished: } } i = format_len + char_read + 256; - scanl = malloc ((size_t) i + 2); + scanl = mh_xmalloc ((size_t) i + 2); dat[0] = 0; dat[1] = 0; dat[2] = 0; @@ -241,12 +249,17 @@ finished: * or add mhn directives */ if (filter) { - replfilter (inb, out, filter); + fflush(out); + if (ferror (out)) + adios (drft, "error writing"); + + replfilter (inb, out, filter, fmtproc); } else if (mime && mp) { fprintf (out, "#forw [original message] +%s %s\n", mp->foldpath, m_name (mp->lowsel)); } + fflush(out); if (ferror (out)) adios (drft, "error writing"); fclose (out); @@ -279,11 +292,9 @@ static unsigned int bufsiz=0; /* current size of buf */ int i = dst - buf;\ int n = last_dst - buf;\ bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\ - buf = realloc (buf, bufsiz);\ + buf = mh_xrealloc (buf, bufsiz);\ dst = buf + i;\ last_dst = buf + n;\ - if (! buf)\ - adios (NULL, "formataddr: couldn't get buffer space");\ bufend = buf + bufsiz;\ } @@ -312,9 +323,7 @@ formataddr (char *orig, char *str) /* if we don't have a buffer yet, get one */ if (bufsiz == 0) { - buf = malloc (BUFINCR); - if (! buf) - adios (NULL, "formataddr: couldn't allocate buffer space"); + buf = mh_xmalloc (BUFINCR); last_dst = buf; /* XXX */ bufsiz = BUFINCR - 6; /* leave some slop */ bufend = buf + bufsiz; @@ -371,18 +380,44 @@ formataddr (char *orig, char *str) } +/* + * fmt_scan will call this routine if the user includes the function + * "(concataddr {component})" in a format string. This behaves exactly + * like formataddr, except that it does NOT suppress duplicate addresses + * between calls. + * + * As an implementation detail: I thought about splitting out formataddr() + * into the generic part and duplicate-suppressing part, but the call to + * insert() was buried deep within a couple of loops and I didn't see a + * way to do it easily. So instead we simply set a special flag to stop + * the duplicate check and call formataddr(). + */ +char * +concataddr(char *orig, char *str) +{ + char *cp; + + nodupcheck = 1; + cp = formataddr(orig, str); + nodupcheck = 0; + return cp; +} + static int insert (struct mailname *np) { char buffer[BUFSIZ]; register struct mailname *mp; + if (nodupcheck) + return 1; + if (np->m_mbox == NULL) return 0; for (mp = &mq; mp->m_next; mp = mp->m_next) { - if (!strcasecmp (np->m_host, mp->m_next->m_host) - && !strcasecmp (np->m_mbox, mp->m_next->m_mbox)) + if (!mh_strcasecmp (np->m_host, mp->m_next->m_host) + && !mh_strcasecmp (np->m_mbox, mp->m_next->m_mbox)) return 0; } if (!ccme && ismymbox (np)) @@ -395,24 +430,23 @@ insert (struct mailname *np) } mp->m_next = np; -#ifdef ISI - if (ismymbox (np)) - ccme = 0; -#endif - return 1; } /* * Call the mhlproc + * + * This function expects that argument out has been fflushed by the caller. */ static void -replfilter (FILE *in, FILE *out, char *filter) +replfilter (FILE *in, FILE *out, char *filter, int fmtproc) { int pid; char *mhl; + char *errstr; + char *arglist[7]; if (filter == NULL) return; @@ -424,9 +458,8 @@ replfilter (FILE *in, FILE *out, char *filter) rewind (in); lseek (fileno(in), (off_t) 0, SEEK_SET); - fflush (out); - switch (pid = vfork ()) { + switch (pid = vfork()) { case NOTOK: adios ("fork", "unable to"); @@ -435,9 +468,32 @@ replfilter (FILE *in, FILE *out, char *filter) dup2 (fileno (out), fileno (stdout)); closefds (3); - execlp (mhlproc, mhl, "-form", filter, "-noclear", NULL); - fprintf (stderr, "unable to exec "); - perror (mhlproc); + arglist[0] = mhl; + arglist[1] = "-form"; + arglist[2] = filter; + arglist[3] = "-noclear"; + + switch (fmtproc) { + case 1: + arglist[4] = "-fmtproc"; + arglist[5] = formatproc; + arglist[6] = NULL; + break; + case 0: + arglist[4] = "-nofmtproc"; + arglist[5] = NULL; + break; + default: + arglist[4] = NULL; + } + + execvp (mhlproc, arglist); + errstr = strerror(errno); + write(2, "unable to exec ", 15); + write(2, mhlproc, strlen(mhlproc)); + write(2, ": ", 2); + write(2, errstr, strlen(errstr)); + write(2, "\n", 1); _exit (-1); default: