remove trailing newlines from components to fix bug with spaces at the end of
[mmh] / uip / replsbr.c
index 57eb6cd..b8e8e92 100644 (file)
@@ -3,12 +3,18 @@
  * 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 <h/mh.h>
 #include <h/addrsbr.h>
 #include <h/fmt_scan.h>
+#include <h/utils.h>
 #include <sys/file.h>          /* L_SET */
+#include <errno.h>
 
 extern short ccto;             /* from repl.c */
 extern short cccc;
@@ -72,14 +78,16 @@ 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;
+    int        char_read = 0, format_len, mask;
     char name[NAMESZ], *scanl, *cp;
     FILE *out;
 
-    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 +105,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++;
@@ -155,18 +162,21 @@ replout (FILE *inb, char *msg, char *drft, struct msgs *mp, int outputlinelen,
                        if (!strcasecmp(name, cptr->c_name)) {
                            char_read += msg_count;
                            if (! cptr->c_text) {
-                               cptr->c_text = tmpbuf;
+                               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 (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) {
@@ -222,7 +232,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;
@@ -240,19 +250,24 @@ finished:
      * or add mhn directives
      */
     if (filter) {
+       fflush(out);
+       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));
     }
 
+    fflush(out);
     if (ferror (out))
        adios (drft, "error writing");
     fclose (out);
 
     /* return dynamically allocated buffers */
     free (scanl);
-    for (nxtbuf = compbuffers, i = ncomps; cptr = *savecomp++; nxtbuf++, i--)
+    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 */
@@ -278,11 +293,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;\
            }
 
@@ -311,9 +324,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;
@@ -334,7 +345,7 @@ formataddr (char *orig, char *str)
     }
 
     /* concatenate all the new addresses onto 'buf' */
-    for (isgroup = 0; cp = getname (str); ) {
+    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);
@@ -405,6 +416,8 @@ insert (struct mailname *np)
 
 /*
  * Call the mhlproc
+ *
+ * This function expects that argument out has been fflushed by the caller.
  */
 
 static void
@@ -412,6 +425,7 @@ replfilter (FILE *in, FILE *out, char *filter)
 {
     int        pid;
     char *mhl;
+    char *errstr;
 
     if (filter == NULL)
        return;
@@ -423,7 +437,6 @@ replfilter (FILE *in, FILE *out, char *filter)
 
     rewind (in);
     lseek (fileno(in), (off_t) 0, SEEK_SET);
-    fflush (out);
 
     switch (pid = vfork ()) {
        case NOTOK: 
@@ -435,8 +448,12 @@ replfilter (FILE *in, FILE *out, char *filter)
            closefds (3);
 
            execlp (mhlproc, mhl, "-form", filter, "-noclear", NULL);
-           fprintf (stderr, "unable to exec ");
-           perror (mhlproc);
+           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: