Removed the -fcc switch from repl(1).
[mmh] / uip / replsbr.c
index add1371..852becd 100644 (file)
@@ -1,10 +1,10 @@
 /*
- * replsbr.c -- routines to help repl along...
- *
- * 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.
- */
+** replsbr.c -- routines to help repl along...
+**
+** 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 <h/mh.h>
 #include <h/addrsbr.h>
@@ -26,12 +26,12 @@ static char *dfhost = NULL;
 static struct mailname mq = { NULL };
 
 /*
- * Buffer size for content part of header fields.
- * We want this to be large enough so that we don't
- * do a lot of extra FLDPLUS calls on m_getfld but
- * small enough so that we don't snarf the entire
- * message body when we're not going to use any of it.
- */
+** Buffer size for content part of header fields.
+** We want this to be large enough so that we don't
+** do a lot of extra FLDPLUS calls on m_getfld but
+** small enough so that we don't snarf the entire
+** message body when we're not going to use any of it.
+*/
 #define SBUFSIZ 256
 
 static struct format *fmt;
@@ -59,15 +59,15 @@ static char *addrcomps[] = {
 };
 
 /*
- * static prototypes
- */
-static int insert (struct mailname *);
-static void replfilter (FILE *, FILE *, char *);
+** static prototypes
+*/
+static int insert(struct mailname *);
+static void replfilter(FILE *, FILE *, char *);
 
 
 void
-replout (FILE *inb, char *msg, char *drft, struct msgs *mp, int outputlinelen,
-       int mime, char *form, char *filter, char *fcc)
+replout(FILE *inb, char *msg, char *drft, struct msgs *mp,
+       int mime, char *form, char *filter)
 {
        register int state, i;
        register struct comp *cptr;
@@ -81,24 +81,24 @@ replout (FILE *inb, char *msg, char *drft, struct msgs *mp, int outputlinelen,
        FILE *out;
 
        mask = umask(~m_gmprot());
-       if ((out = fopen (drft, "w")) == NULL)
-               adios (drft, "unable to create");
+       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);
+       cp = new_fs(form, NULL, NULL);
+       format_len = strlen(cp);
 
        /* compile format string */
-       ncomps = fmt_compile (cp, &fmt) + 1;
+       ncomps = fmt_compile(cp, &fmt) + 1;
 
        if (!(nxtbuf = compbuffers = (char **)
                        calloc((size_t) ncomps, sizeof(char *))))
-               adios (NULL, "unable to allocate component buffers");
+               adios(NULL, "unable to allocate component buffers");
        if (!(savecomp = used_buf = (struct comp **)
                        calloc((size_t) (ncomps+1), sizeof(struct comp *))))
-               adios (NULL, "unable to allocate component buffer stack");
+               adios(NULL, "unable to allocate component buffer stack");
        savecomp += ncomps + 1;
        *--savecomp = NULL;  /* point at zero'd end minus 1 */
 
@@ -109,110 +109,104 @@ replout (FILE *inb, char *msg, char *drft, struct msgs *mp, int outputlinelen,
        tmpbuf = *nxtbuf++;
 
        for (ap = addrcomps; *ap; ap++) {
-               FINDCOMP (cptr, *ap);
+               FINDCOMP(cptr, *ap);
                if (cptr)
                        cptr->c_type |= CT_ADDR;
        }
 
        /*
-        * ignore any components killed by command line switches
-        */
+       ** ignore any components killed by command line switches
+       */
        if (!ccto) {
-               FINDCOMP (cptr, "to");
+               FINDCOMP(cptr, "to");
                if (cptr)
                        cptr->c_name = "";
        }
        if (!cccc) {
-               FINDCOMP (cptr, "cc");
+               FINDCOMP(cptr, "cc");
                if (cptr)
                        cptr->c_name = "";
        }
-       /* set up the "fcc" pseudo-component */
-       if (fcc) {
-               FINDCOMP (cptr, "fcc");
-               if (cptr)
-                       cptr->c_text = getcpy (fcc);
-       }
        if ((cp = getenv("USER"))) {
-               FINDCOMP (cptr, "user");
+               FINDCOMP(cptr, "user");
                if (cptr)
                        cptr->c_text = getcpy(cp);
        }
        if (!ccme)
-               ismymbox (NULL);
+               ismymbox(NULL);
 
        /*
-        * pick any interesting stuff out of msg "inb"
-        */
+       ** pick any interesting stuff out of msg "inb"
+       */
        for (state = FLD;;) {
-               state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
+               state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb);
                switch (state) {
-                       case FLD:
-                       case FLDPLUS:
-                               /*
-                                * if we're interested in this component,
-                                * save a pointer to the component text,
-                                * then start using our next free 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)) {
-                                                       char_read += msg_count;
-                                                       if (! cptr->c_text) {
-                                                               i = strlen(cptr->c_text = tmpbuf) - 1;
-                                                               if (tmpbuf[i] == '\n')
-                                                                       tmpbuf[i] = '\0';
-                                                               *--savecomp = cptr;
-                                                               tmpbuf = *nxtbuf++;
-                                                       } else {
-                                                               i = strlen (cp = cptr->c_text) - 1;
-                                                               if (cp[i] == '\n') {
-                                                                       if (cptr->c_type & CT_ADDR) {
-                                                                               cp[i] = '\0';
-                                                                               cp = add (",\n\t", cp);
-                                                                       } else {
-                                                                               cp = add ("\t", cp);
-                                                                       }
+               case FLD:
+               case FLDPLUS:
+                       /*
+                       ** if we're interested in this component, save
+                       ** a pointer to the component text, then start
+                       ** using our next free 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)) {
+                                               char_read += msg_count;
+                                               if (! cptr->c_text) {
+                                                       i = strlen(cptr->c_text = tmpbuf) - 1;
+                                                       if (tmpbuf[i] == '\n')
+                                                               tmpbuf[i] = '\0';
+                                                       *--savecomp = cptr;
+                                                       tmpbuf = *nxtbuf++;
+                                               } else {
+                                                       i = strlen(cp = cptr->c_text) - 1;
+                                                       if (cp[i] == '\n') {
+                                                               if (cptr->c_type & CT_ADDR) {
+                                                                       cp[i] = '\0';
+                                                                       cp = add(",\n\t", cp);
+                                                               } else {
+                                                                       cp = add("\t", cp);
                                                                }
-                                                               cptr->c_text = add (tmpbuf, cp);
                                                        }
-                                                       while (state == FLDPLUS) {
-                                                               state = m_getfld (state, name, tmpbuf,
-                                                                                                 SBUFSIZ, inb);
-                                                               cptr->c_text = add (tmpbuf, cptr->c_text);
-                                                               char_read += msg_count;
-                                                       }
-                                                       break;
+                                                       cptr->c_text = add(tmpbuf, cp);
+                                               }
+                                               while (state == FLDPLUS) {
+                                                       state = m_getfld(state, name, tmpbuf,
+                                                                                         SBUFSIZ, inb);
+                                                       cptr->c_text = add(tmpbuf, cptr->c_text);
+                                                       char_read += msg_count;
                                                }
-                                       } while ((cptr = cptr->c_next));
+                                               break;
+                                       }
+                               } while ((cptr = cptr->c_next));
 
-                               while (state == FLDPLUS)
-                                       state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
-                               break;
+                       while (state == FLDPLUS)
+                               state = m_getfld(state, name, tmpbuf,
+                                               SBUFSIZ, inb);
+                       break;
 
-                       case LENERR:
-                       case FMTERR:
-                       case BODY:
-                       case FILEEOF:
-                               goto finished;
+               case LENERR:
+               case FMTERR:
+               case BODY:
+               case FILEEOF:
+                       goto finished;
 
-                       default:
-                               adios (NULL, "m_getfld() returned %d", state);
+               default:
+                       adios(NULL, "m_getfld() returned %d", state);
                }
        }
 
        /*
-        * format and output the header lines.
-        */
+       ** format and output the header lines.
+       */
 finished:
 
        /*
-        * if there's a "Subject" component, strip any "Re:"s off it
-        */
-       FINDCOMP (cptr, "subject")
+       ** if there's a "Subject" component, strip any "Re:"s off it
+       */
+       FINDCOMP(cptr, "subject")
        if (cptr && (cp = cptr->c_text)) {
                register char *sp = cp;
 
@@ -227,52 +221,56 @@ finished:
                }
                if (sp != cptr->c_text) {
                        cp = cptr->c_text;
-                       cptr->c_text = getcpy (sp);
-                       free (cp);
+                       cptr->c_text = getcpy(sp);
+                       free(cp);
                }
        }
        i = format_len + char_read + 256;
-       scanl = mh_xmalloc ((size_t) i + 2);
+       scanl = mh_xmalloc((size_t) i + 2);
        dat[0] = 0;
        dat[1] = 0;
        dat[2] = 0;
-       dat[3] = outputlinelen;
+       dat[3] = OUTPUTLINELEN;
        dat[4] = 0;
-       fmt_scan (fmt, scanl, i, dat);
-       fputs (scanl, out);
+       fmt_scan(fmt, scanl, i, dat);
+       fputs(scanl, out);
        if (badaddrs) {
-               fputs ("\nrepl: bad addresses:\n", out);
-               fputs ( badaddrs, out);
+               fputs("\nrepl: bad addresses:\n", out);
+               fputs( badaddrs, out);
        }
 
-       /*
-        * Check if we should filter the message
-        * or add mhbuild directives
-        */
+       /* Check if we should filter the message */
        if (filter) {
                fflush(out);
-               if (ferror (out))
-                       adios (drft, "error writing");
+               if (ferror(out))
+                       adios(drft, "error writing");
 
-               replfilter (inb, out, filter);
-       } else if (mime && mp) {
-                       fprintf (out, "#forw [original message] +%s %s\n",
-                                        mp->foldpath, m_name (mp->lowsel));
+               replfilter(inb, out, filter);
        }
 
        fflush(out);
-       if (ferror (out))
-               adios (drft, "error writing");
-       fclose (out);
+       if (ferror(out))
+               adios(drft, "error writing");
+       fclose(out);
+
+       /* if we want mime, then add an attachment header */
+       if (!filter && mime && mp) {
+               char buffer[BUFSIZ];
+
+               snprintf(buffer, sizeof buffer, "+%s %s",
+                               mp->foldpath, m_name(mp->lowsel));
+               annotate(drft, attach_hdr, buffer, 0, -2, 1);
+       }
 
        /* return dynamically allocated buffers */
-       free (scanl);
-       for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++); nxtbuf++, i--)
-               free (cptr->c_text);  /* if not nxtbuf, nxtbuf already freed */
+       free(scanl);
+       for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++);
+                       nxtbuf++, i--)
+               free(cptr->c_text);  /* if not nxtbuf, nxtbuf already freed */
        while ( i-- > 0)
-               free (*nxtbuf++);  /* free unused nxtbufs */
-       free ((char *) compbuffers);
-       free ((char *) used_buf);
+               free(*nxtbuf++);  /* free unused nxtbufs */
+       free((char *) compbuffers);
+       free((char *) used_buf);
 }
 
 static char *buf;  /* our current working buffer */
@@ -285,15 +283,15 @@ static unsigned int bufsiz=0;  /* current size of buf */
 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
 
 /*
- * check if there's enough room in buf for str.
- * add more mem if needed
- */
+** check if there's enough room in buf for str.
+** add more mem if needed
+*/
 #define CHECKMEM(str) \
-       if ((len = strlen (str)) >= bufend - dst) {\
+       if ((len = strlen(str)) >= bufend - dst) {\
                int i = dst - buf;\
                int n = last_dst - buf;\
                bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
-               buf = mh_xrealloc (buf, bufsiz);\
+               buf = mh_xrealloc(buf, bufsiz);\
                dst = buf + i;\
                last_dst = buf + n;\
                bufend = buf + bufsiz;\
@@ -301,18 +299,18 @@ static unsigned int bufsiz=0;  /* current size of buf */
 
 
 /*
- * fmt_scan will call this routine if the user includes the function
- * "(formataddr {component})" in a format string.  "orig" is the
- * original contents of the string register.  "str" is the address
- * string to be formatted and concatenated onto orig.  This routine
- * returns a pointer to the concatenated address string.
- *
- * We try to not do a lot of malloc/copy/free's (which is why we
- * don't call "getcpy") but still place no upper limit on the
- * length of the result string.
- */
+** fmt_scan will call this routine if the user includes the function
+** "(formataddr {component})" in a format string.  "orig" is the
+** original contents of the string register.  "str" is the address
+** string to be formatted and concatenated onto orig.  This routine
+** returns a pointer to the concatenated address string.
+**
+** We try to not do a lot of malloc/copy/free's (which is why we
+** don't call "getcpy") but still place no upper limit on the
+** length of the result string.
+*/
 char *
-formataddr (char *orig, char *str)
+formataddr(char *orig, char *str)
 {
        register int len;
        char baddr[BUFSIZ], error[BUFSIZ];
@@ -324,15 +322,15 @@ formataddr (char *orig, char *str)
 
        /* if we don't have a buffer yet, get one */
        if (bufsiz == 0) {
-               buf = mh_xmalloc (BUFINCR);
+               buf = mh_xmalloc(BUFINCR);
                last_dst = buf;  /* XXX */
                bufsiz = BUFINCR - 6;  /* leave some slop */
                bufend = buf + bufsiz;
        }
        /*
-        * If "orig" points to our buffer we can just pick up where we
-        * left off.  Otherwise we have to copy orig into our buffer.
-        */
+       ** If "orig" points to our buffer we can just pick up where we
+       ** left off.  Otherwise we have to copy orig into our buffer.
+       */
        if (orig == buf)
                dst = last_dst;
        else if (!orig || !*orig) {
@@ -340,35 +338,36 @@ formataddr (char *orig, char *str)
                *dst = '\0';
        } else {
                dst = last_dst;  /* XXX */
-               CHECKMEM (orig);
-               CPY (orig);
+               CHECKMEM(orig);
+               CPY(orig);
        }
 
        /* concatenate all the new addresses onto 'buf' */
-       for (isgroup = 0; (cp = getname (str)); ) {
-               if ((mp = getm (cp, dfhost, dftype, AD_NAME, error)) == NULL) {
-                       snprintf (baddr, sizeof(baddr), "\t%s -- %s\n", cp, error);
-                       badaddrs = add (baddr, badaddrs);
+       for (isgroup = 0; (cp = getname(str)); ) {
+               if ((mp = getm(cp, dfhost, dftype, AD_NAME, error)) == NULL) {
+                       snprintf(baddr, sizeof(baddr), "\t%s -- %s\n",
+                                       cp, error);
+                       badaddrs = add(baddr, badaddrs);
                        continue;
                }
                if (isgroup && (mp->m_gname || !mp->m_ingrp)) {
                        *dst++ = ';';
                        isgroup = 0;
                }
-               if (insert (mp)) {
+               if (insert(mp)) {
                        /* if we get here we're going to add an address */
                        if (dst != buf) {
                                *dst++ = ',';
                                *dst++ = ' ';
                        }
                        if (mp->m_gname) {
-                               CHECKMEM (mp->m_gname);
-                               CPY (mp->m_gname);
+                               CHECKMEM(mp->m_gname);
+                               CPY(mp->m_gname);
                                isgroup++;
                        }
-                       sp = adrformat (mp);
-                       CHECKMEM (sp);
-                       CPY (sp);
+                       sp = adrformat(mp);
+                       CHECKMEM(sp);
+                       CPY(sp);
                }
        }
 
@@ -382,7 +381,7 @@ formataddr (char *orig, char *str)
 
 
 static int
-insert (struct mailname *np)
+insert(struct mailname *np)
 {
        char buffer[BUFSIZ];
        register struct mailname *mp;
@@ -391,22 +390,23 @@ insert (struct mailname *np)
                return 0;
 
        for (mp = &mq; mp->m_next; mp = mp->m_next) {
-               if (!mh_strcasecmp (np->m_host, mp->m_next->m_host)
-                               && !mh_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))
+       if (!ccme && ismymbox(np))
                return 0;
 
        if (querysw) {
-               snprintf (buffer, sizeof(buffer), "Reply to %s? ", adrformat (np));
-               if (!gans (buffer, anoyes))
+               snprintf(buffer, sizeof(buffer), "Reply to %s? ",
+                               adrformat(np));
+               if (!gans(buffer, anoyes))
                return 0;
        }
        mp->m_next = np;
 
 #ifdef ISI
-       if (ismymbox (np))
+       if (ismymbox(np))
                ccme = 0;
 #endif
 
@@ -415,13 +415,13 @@ insert (struct mailname *np)
 
 
 /*
- * Call the mhlproc
- *
- * This function expects that argument out has been fflushed by the caller.
- */
+** 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 pid;
        char *mhl;
@@ -430,36 +430,36 @@ replfilter (FILE *in, FILE *out, char *filter)
        if (filter == NULL)
                return;
 
-       if (access (filter, R_OK) == NOTOK)
-               adios (filter, "unable to read");
-
-       mhl = r1bindex (mhlproc, '/');
-
-       rewind (in);
-       lseek (fileno(in), (off_t) 0, SEEK_SET);
-
-       switch (pid = vfork ()) {
-               case NOTOK:
-                       adios ("fork", "unable to");
-
-               case OK:
-                       dup2 (fileno (in), fileno (stdin));
-                       dup2 (fileno (out), fileno (stdout));
-                       closefds (3);
-
-                       execlp (mhlproc, mhl, "-form", filter, "-noclear", NULL);
-                       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:
-                       if (pidXwait (pid, mhl))
-                               done (1);
-                       fseek (out, 0L, SEEK_END);
-                       break;
+       if (access(filter, R_OK) == NOTOK)
+               adios(filter, "unable to read");
+
+       mhl = mhbasename(mhlproc);
+
+       rewind(in);
+       lseek(fileno(in), (off_t) 0, SEEK_SET);
+
+       switch (pid = fork()) {
+       case NOTOK:
+               adios("fork", "unable to");
+
+       case OK:
+               dup2(fileno(in), fileno(stdin));
+               dup2(fileno(out), fileno(stdout));
+               closefds(3);
+
+               execlp(mhlproc, mhl, "-form", filter, "-noclear", NULL);
+               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:
+               if (pidXwait(pid, mhl))
+                       done(1);
+               fseek(out, 0L, SEEK_END);
+               break;
        }
 }