]> diplodocus.org Git - nmh/blobdiff - uip/replsbr.c
Added cpytrim() function. Unlike trimcpy(), it does not modify
[nmh] / uip / replsbr.c
index 3dbc3031ee614874f3a941ea45dd048321b4590c..a7a214bf538ad69ac735b7e420510148818d4e89 100644 (file)
@@ -57,6 +57,8 @@ static char *addrcomps[] = {
  */
 static int insert (struct mailname *);
 static void replfilter (FILE *, FILE *, char *, int);
+static char *replformataddr(char *, char *);
+static char *replconcataddr(char *, char *);
 
 
 void
@@ -69,9 +71,10 @@ replout (FILE *inb, char *msg, char *drft, struct msgs *mp, int outputlinelen,
     struct format *fmt;
     register char **ap;
     int        char_read = 0, format_len, mask;
-    char name[NAMESZ], *scanl;
-    unsigned char *cp;
+    char name[NAMESZ], *scanl, *cp;
     static int dat[5];                 /* aux. data for format routine */
+    m_getfld_state_t gstate = 0;
+    struct fmt_callbacks cb;
 
     FILE *out;
     NMH_UNUSED (msg);
@@ -131,8 +134,9 @@ replout (FILE *inb, char *msg, char *drft, struct msgs *mp, int outputlinelen,
     /*
      * pick any interesting stuff out of msg "inb"
      */
-    for (state = FLD;;) {
-       state = m_getfld (state, name, tmpbuf, sizeof(tmpbuf), inb);
+    for (;;) {
+       int msg_count = sizeof tmpbuf;
+       state = m_getfld (&gstate, name, tmpbuf, &msg_count, inb);
        switch (state) {
            case FLD: 
            case FLDPLUS: 
@@ -147,15 +151,17 @@ replout (FILE *inb, char *msg, char *drft, struct msgs *mp, int outputlinelen,
                if (i != -1) {
                    char_read += msg_count;
                    while (state == FLDPLUS) {
-                       state = m_getfld(state, name, tmpbuf,
-                                        sizeof(tmpbuf), inb);
+                       msg_count= sizeof tmpbuf;
+                       state = m_getfld (&gstate, name, tmpbuf, &msg_count, inb);
                        fmt_appendcomp(i, name, tmpbuf);
                        char_read += msg_count;
                    }
                }
 
-               while (state == FLDPLUS)
-                   state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
+               while (state == FLDPLUS) {
+                   msg_count= sizeof tmpbuf;
+                   state = m_getfld (&gstate, name, tmpbuf, &msg_count, inb);
+               }
                break;
 
            case LENERR: 
@@ -173,6 +179,7 @@ replout (FILE *inb, char *msg, char *drft, struct msgs *mp, int outputlinelen,
      * format and output the header lines.
      */
 finished:
+    m_getfld_state_destroy (&gstate);
 
     /*
      * if there's a "Subject" component, strip any "Re:"s off it
@@ -182,7 +189,7 @@ finished:
        register char *sp = cp;
 
        for (;;) {
-           while (isspace(*cp))
+           while (isspace((unsigned char) *cp))
                cp++;
            if(uprf(cp, "re:"))
                cp += 3;
@@ -203,7 +210,10 @@ finished:
     dat[2] = 0;
     dat[3] = outputlinelen;
     dat[4] = 0;
-    fmt_scan (fmt, scanl, i + 1, i, dat);
+    memset(&cb, 0, sizeof(cb));
+    cb.formataddr = replformataddr;
+    cb.concataddr = replconcataddr;
+    fmt_scan (fmt, scanl, i + 1, i, dat, &cb);
     fputs (scanl, out);
     if (badaddrs) {
        fputs ("\nrepl: bad addresses:\n", out);
@@ -271,8 +281,8 @@ static unsigned int bufsiz=0;       /* current size of buf */
  * don't call "getcpy") but still place no upper limit on the
  * length of the result string.
  */
-char *
-formataddr (char *orig, char *str)
+static char *
+replformataddr (char *orig, char *str)
 {
     register int len;
     char baddr[BUFSIZ], error[BUFSIZ];
@@ -347,19 +357,19 @@ formataddr (char *orig, char *str)
  * like formataddr, except that it does NOT suppress duplicate addresses
  * between calls.
  *
- * As an implementation detail: I thought about splitting out formataddr()
+ * As an implementation detail: I thought about splitting out replformataddr()
  * 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().
+ * the duplicate check and call replformataddr().
  */
-char *
-concataddr(char *orig, char *str)
+static char *
+replconcataddr(char *orig, char *str)
 {
     char *cp;
 
     nodupcheck = 1;
-    cp = formataddr(orig, str);
+    cp = replformataddr(orig, str);
     nodupcheck = 0;
     return cp;
 }
@@ -407,7 +417,8 @@ replfilter (FILE *in, FILE *out, char *filter, int fmtproc)
     int        pid;
     char *mhl;
     char *errstr;
-    char *arglist[7];
+    char **arglist;
+    int argnum;
 
     if (filter == NULL)
        return;
@@ -415,12 +426,10 @@ replfilter (FILE *in, FILE *out, char *filter, int fmtproc)
     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()) {
+    switch (pid = fork()) {
        case NOTOK: 
            adios ("fork", "unable to");
 
@@ -429,26 +438,29 @@ replfilter (FILE *in, FILE *out, char *filter, int fmtproc)
            dup2 (fileno (out), fileno (stdout));
            closefds (3);
 
-           arglist[0] = mhl;
-           arglist[1] = "-form";
-           arglist[2] = filter;
-           arglist[3] = "-noclear";
+           /*
+            * We're not allocating the memory for the extra arguments,
+            * because we never call arglist_free().  But if we ever change
+            * that be sure to use getcpy() for the extra arguments.
+            */
+           arglist = argsplit(mhlproc, &mhl, &argnum);
+           arglist[argnum++] = "-form";
+           arglist[argnum++] = filter;
+           arglist[argnum++] = "-noclear";
 
            switch (fmtproc) {
            case 1:
-               arglist[4] = "-fmtproc";
-               arglist[5] = formatproc;
-               arglist[6] = NULL;
+               arglist[argnum++] = "-fmtproc";
+               arglist[argnum++] = formatproc;
                break;
            case 0:
-               arglist[4] = "-nofmtproc";
-               arglist[5] = NULL;
+               arglist[argnum++] = "-nofmtproc";
                break;
-           default:
-               arglist[4] = NULL;
            }
 
-           execvp (mhlproc, arglist);
+           arglist[argnum++] = NULL;
+
+           execvp (mhl, arglist);
            errstr = strerror(errno);
            write(2, "unable to exec ", 15);
            write(2, mhlproc, strlen(mhlproc));