Rework in dist tools: removed -file switch; don't fork(); refactoring.
authormarkus schnalke <meillo@marmaro.de>
Wed, 21 Mar 2012 19:27:03 +0000 (20:27 +0100)
committermarkus schnalke <meillo@marmaro.de>
Wed, 21 Mar 2012 19:27:03 +0000 (20:27 +0100)
man/dist.man1
man/rcvdist.man1
uip/dist.c
uip/distsbr.c
uip/rcvdist.c

index 0bb3566..e48525f 100644 (file)
@@ -135,7 +135,6 @@ will prevent any edit from occurring.)
 ^Current\-Folder:~^To find the default current folder
 ^Draft\-Folder:~^To set the default draft\-folder
 ^Editor:~^To override the default editor
-^fileproc:~^Program to refile the message
 ^whatnowproc:~^Program to ask the \*(lqWhat now?\*(rq questions
 .fi
 
@@ -162,11 +161,12 @@ instead of
 In order to conform with the ARPA Internet standard, RFC\-822, the
 .RI \*(lq Resent\-xxx: \*(rq
 form is now used.
-.B Dist
-will recognize
+Since
+.B mmh
+.B dist
+will no longer recognize and convert
 .RI \*(lq Distribute\-xxx: \*(rq
-type headers and automatically convert them to
-.RI \*(lq Resent\-xxx: \*(rq.
+type headers.
 
 .SH BUGS
 .B Dist
@@ -180,6 +180,3 @@ does.  The
 program will balk (and rightly so) at poorly formatted messages, and
 .B dist
 won't correct things for you.
-.PP
-If your current working directory is not writable, the link named
-\*(lq@\*(rq is not available.
index 9edfc49..29cf84f 100644 (file)
@@ -12,8 +12,7 @@ rcvdist \- asynchronously redistribute new mail
 .IR formfile ]
 [switches\ for
 .IR postproc ]
-.I address1
-\&...
+.IR RECIPIENT ...
 .RB [ \-version ]
 .RB [ \-help ]
 .ad
@@ -70,8 +69,6 @@ profile component.
 .ta \w'%etcdir%/ExtraBigFileName  'u
 ^%etcdir%/rcvdistcomps~^Default message skeleton
 ^or $HOME/.mmh/rcvdistcomps~^Rather than standard message skeleton
-^$HOME/\&.maildelivery~^The file controlling local delivery
-^%etcdir%/maildelivery~^Rather than the standard file
 .fi
 
 .SH "SEE ALSO"
index 869a906..4eba2eb 100644 (file)
@@ -29,8 +29,6 @@ static struct swit switches[] = {
        { "version", 0 },
 #define HELPSW  8
        { "help", 0 },
-#define FILESW  9
-       { "file file", -4 },  /* interface from msh */
        { NULL, 0 }
 };
 
@@ -41,7 +39,7 @@ main(int argc, char **argv)
        int anot = 0, nedit = 0;
        int nwhat = 0, in, out;
        char *cp, *cwd, *maildir, *msgnam;
-       char *ed = NULL, *file = NULL, *folder = NULL;
+       char *ed = NULL, *folder = NULL;
        char *form = NULL, *msg = NULL, buf[BUFSIZ], drft[BUFSIZ];
        char **argp, **arguments;
        struct msgs *mp = NULL;
@@ -102,14 +100,6 @@ main(int argc, char **argv)
                                nwhat++;
                                continue;
 
-                       case FILESW:
-                               if (file)
-                                       adios(NULL, "only one file at a time!");
-                               if (!(cp = *argp++) || *cp == '-')
-                                       adios(NULL, "missing argument to %s",
-                                                       argp[-2]);
-                               file = getcpy(expanddir(cp));
-                               continue;
                        case FORMSW:
                                if (!(form = *argp++) || *form == '-')
                                        adios(NULL, "missing argument to %s",
@@ -132,9 +122,6 @@ main(int argc, char **argv)
 
        cwd = getcpy(pwd());
 
-       if (file && (msg || folder))
-               adios(NULL, "can't mix files and folders/msgs");
-
        strncpy(drft, m_draft(seq_beyond), sizeof(drft));
        if ((out = creat(drft, m_gmprot())) == NOTOK)
                adios(drft, "unable to create");
@@ -145,56 +132,43 @@ main(int argc, char **argv)
        }
        close(out);
 
-       if (file) {
-               /*
-               ** Dist a file
-               */
-               anot = 0;  /* don't want to annotate a file */
-       } else {
-               /*
-               ** Dist a message
-               */
-               if (!msg)
-                       msg = seq_cur;
-               if (!folder)
-                       folder = getcurfol();
-               maildir = toabsdir(folder);
-
-               if (chdir(maildir) == NOTOK)
-                       adios(maildir, "unable to change directory to");
-
-               /* read folder and create message structure */
-               if (!(mp = folder_read(folder)))
-                       adios(NULL, "unable to read folder %s", folder);
-
-               /* check for empty folder */
-               if (mp->nummsg == 0)
-                       adios(NULL, "no messages in %s", folder);
-
-               /* parse the message range/sequence/name and set SELECTED */
-               if (!m_convert(mp, msg))
-                       done(1);
-               seq_setprev(mp);  /* set the previous-sequence */
-
-               if (mp->numsel > 1)
-                       adios(NULL, "only one message at a time!");
-       }
+       if (!msg)
+               msg = seq_cur;
+       if (!folder)
+               folder = getcurfol();
+       maildir = toabsdir(folder);
+
+       if (chdir(maildir) == NOTOK)
+               adios(maildir, "unable to change directory to");
 
-       msgnam = file ? file : getcpy(m_name(mp->lowsel));
+       if (!(mp = folder_read(folder)))
+               adios(NULL, "unable to read folder %s", folder);
+
+       /* check for empty folder */
+       if (mp->nummsg == 0)
+               adios(NULL, "no messages in %s", folder);
+
+       /* parse the message range/sequence/name and set SELECTED */
+       if (!m_convert(mp, msg))
+               done(1);
+       seq_setprev(mp);
+
+       if (mp->numsel > 1)
+               adios(NULL, "only one message at a time!");
+
+       msgnam = getcpy(m_name(mp->lowsel));
        if ((in = open(msgnam, O_RDONLY)) == NOTOK)
                adios(msgnam, "unable to open message");
 
-       if (!file) {
-               context_replace(curfolder, folder); /* update current folder */
-               seq_setcur(mp, mp->lowsel);  /* update current message */
-               seq_save(mp);  /* synchronize sequences  */
-               context_save();  /* save the context file  */
-       }
+       context_replace(curfolder, folder);
+       seq_setcur(mp, mp->lowsel);
+       seq_save(mp);  /* synchronize sequences  */
+       context_save();
 
        if (nwhat)
                done(0);
-       what_now(ed, nedit, NOUSE, drft, msgnam, 1, mp, anot ? "Resent" : NULL,
-                       cwd);
+       what_now(ed, nedit, NOUSE, drft, msgnam, 1, mp,
+                       anot ? "Resent" : NULL, cwd);
        done(1);
        return 1;
 }
index 396aca8..1ad8e42 100644 (file)
 static int  hdrfd = NOTOK;
 static int  txtfd = NOTOK;
 
-#define BADHDR  "please re-edit %s to remove the ``%s'' header!"
-#define BADTXT  "please re-edit %s to consist of headers only!"
-#define BADMSG  "please re-edit %s to include a ``Resent-To:''!"
-#define BADRFT  "please re-edit %s and fix that header!"
-
 /*
 ** static prototypes
 */
@@ -32,37 +27,37 @@ distout(char *drft, char *msgnam, char *backup)
        char name[NAMESZ], buffer[BUFSIZ];
        register FILE *ifp, *ofp;
 
-       if (rename(drft, strcpy(backup, m_backup(drft))) == NOTOK)
+       if (rename(drft, strcpy(backup, m_backup(drft))) == NOTOK) {
                adios(backup, "unable to rename %s to",drft);
-       if ((ifp = fopen(backup, "r")) == NULL)
+       }
+       if (!(ifp = fopen(backup, "r"))) {
                adios(backup, "unable to read");
+       }
 
-       if ((ofp = fopen(drft, "w")) == NULL)
+       if (!(ofp = fopen(drft, "w"))) {
                adios(drft, "unable to create temporary file");
+       }
        chmod(drft, m_gmprot());
 
        ready_msg(msgnam);
        lseek(hdrfd, (off_t) 0, SEEK_SET); /* msgnam not accurate */
        cpydata(hdrfd, fileno(ofp), msgnam, drft);
 
-       for (state = FLD, resent = NULL;;)
-               switch (state = m_getfld (state, name, buffer,
-                               sizeof buffer, ifp)) {
+       state = FLD;
+       resent = NULL;
+       while (1) {
+               switch (state = m_getfld(state, name, buffer, sizeof buffer,
+                               ifp)) {
                case FLD:
                case FLDPLUS:
                case FLDEOF:
-                       if (uprf(name, "distribute-"))
-                               snprintf(name, sizeof(name), "%s%s",
-                                               "Resent", &name[10]);
-                       if (uprf(name, "distribution-"))
-                               snprintf(name, sizeof(name), "%s%s",
-                                               "Resent", &name[12]);
                        if (!uprf(name, "resent")) {
-                               advise(NULL, BADHDR, "draft", name);
+                               advise(NULL, "Please re-edit draft to remove the ``%s'' header.", name);
                                goto leave_bad;
                        }
-                       if (state == FLD)
+                       if (state == FLD) {
                                resent = add(":", add(name, resent));
+                       }
                        resent = add(buffer, resent);
                        fprintf(ofp, "%s: %s", name, buffer);
                        while (state == FLDPLUS) {
@@ -71,51 +66,57 @@ distout(char *drft, char *msgnam, char *backup)
                                resent = add(buffer, resent);
                                fputs(buffer, ofp);
                        }
-                       if (state == FLDEOF)
+                       if (state == FLDEOF) {
                                goto process;
+                       }
                        break;
 
                case BODY:
                case BODYEOF:
-                       for (dp = buffer; *dp; dp++)
+                       for (dp = buffer; *dp; dp++) {
                                if (!isspace(*dp)) {
-                                       advise(NULL, BADTXT, "draft");
+                                       advise(NULL, "Please re-edit draft to consist of headers only.");
                                        goto leave_bad;
                                }
+                       }
 
                case FILEEOF:
                        goto process;
 
                case LENERR:
                case FMTERR:
-                       advise(NULL, BADRFT, "draft");
+                       advise(NULL, "Please re-edit draft and fix that header.");
 leave_bad: ;
                        fclose(ifp);
                        fclose(ofp);
                        unlink(drft);
-                       if (rename(backup, drft) == NOTOK)
+                       if (rename(backup, drft) == NOTOK) {
                                adios(drft, "unable to rename %s to", backup);
+                       }
                        return NOTOK;
 
                default:
                        adios(NULL, "getfld() returned %d", state);
                }
+       }
+
 process: ;
        fclose(ifp);
        fflush(ofp);
 
        if (!resent) {
-               advise(NULL, BADMSG, "draft");
+               advise(NULL, "Please re-edit draft to include a ``Resent-To:'' header.");
                fclose(ofp);
                unlink(drft);
-               if (rename(backup, drft) == NOTOK)
+               if (rename(backup, drft) == NOTOK) {
                        adios(drft, "unable to rename %s to", backup);
+               }
                return NOTOK;
        }
        free(resent);
 
        if (txtfd != NOTOK) {
-               lseek(txtfd, (off_t) 0, SEEK_SET); /* msgnam not accurate */
+               lseek(txtfd, (off_t) 0, SEEK_SET);  /* msgnam not accurate */
                cpydata(txtfd, fileno(ofp), msgnam, drft);
        }
 
@@ -133,41 +134,48 @@ ready_msg(char *msgnam)
        register FILE *ifp, *ofp;
        char *cp = NULL;
 
-       if (hdrfd != NOTOK)
-               close(hdrfd), hdrfd = NOTOK;
-       if (txtfd != NOTOK)
-               close(txtfd), txtfd = NOTOK;
-
-       if ((ifp = fopen(msgnam, "r")) == NULL)
+       if (hdrfd != NOTOK) {
+               close(hdrfd);
+               hdrfd = NOTOK;
+       }
+       if (txtfd != NOTOK) {
+               close(txtfd);
+               txtfd = NOTOK;
+       }
+       if (!(ifp = fopen(msgnam, "r"))) {
                adios(msgnam, "unable to open message");
+       }
 
        cp = m_mktemp2(NULL, "dist", &hdrfd, NULL);
-       if (cp == NULL) {
+       if (!cp) {
                adios("distsbr", "unable to create temporary file");
        }
        fchmod(hdrfd, 0600);
        strncpy(tmpfil, cp, sizeof(tmpfil));
-       if ((out = dup(hdrfd)) == NOTOK
-                       || (ofp = fdopen(out, "w")) == NULL)
+       if ((out = dup(hdrfd)) == NOTOK || !(ofp = fdopen(out, "w"))) {
                adios(NULL, "no file descriptors -- you lose big");
+       }
        unlink(tmpfil);
 
-       for (state = FLD;;) {
+       state = FLD;
+       while (1) {
                state = m_getfld(state, name, buffer, sizeof buffer, ifp);
                switch (state) {
                case FLD:
                case FLDPLUS:
                case FLDEOF:
-                       if (uprf(name, "resent"))
+                       if (uprf(name, "resent")) {
                                fprintf(ofp, "Prev-");
+                       }
                        fprintf(ofp, "%s: %s", name, buffer);
                        while (state == FLDPLUS) {
                                state = m_getfld(state, name, buffer,
                                                sizeof buffer, ifp);
                                fputs(buffer, ofp);
                        }
-                       if (state == FLDEOF)
+                       if (state == FLDEOF) {
                                goto process;
+                       }
                        break;
 
                case BODY:
@@ -175,14 +183,15 @@ ready_msg(char *msgnam)
                        fclose(ofp);
 
                        cp = m_mktemp2(NULL, "dist", &txtfd, NULL);
-                       if (cp == NULL) {
-                               adios("distsbr", "unable to create temporary file");
+                       if (!cp) {
+                               adios("distsbr", "unable to create temp file");
                        }
                        fchmod(txtfd, 0600);
                        strncpy(tmpfil, cp, sizeof(tmpfil));
-                       if ((out = dup(txtfd)) == NOTOK
-                                       || (ofp = fdopen(out, "w")) == NULL)
-                               adios(NULL, "no file descriptors -- you lose big");
+                       if ((out = dup(txtfd)) == NOTOK ||
+                                       !(ofp = fdopen(out, "w"))) {
+                               adios(NULL, "no file descriptors -- you lose");
+                       }
                        unlink(tmpfil);
                        fprintf(ofp, "\n%s", buffer);
                        while (state == BODY) {
index 625bb4c..58bf62d 100644 (file)
@@ -36,8 +36,7 @@ static void unlink_done(int) NORETURN;
 int
 main(int argc, char **argv)
 {
-       pid_t child_id;
-       int i, vecp = 1;
+       int vecp = 1;
        char *addrs = NULL, *cp, *form = NULL, buf[BUFSIZ];
        char **argp, **arguments, *vec[MAXARGS];
        FILE *fp;
@@ -75,17 +74,19 @@ main(int argc, char **argv)
                                done(1);
 
                        case FORMSW:
-                               if (!(form = *argp++) || *form == '-')
+                               if (!(form = *argp++) || *form == '-') {
                                        adios(NULL, "missing argument to %s",
                                                        argp[-2]);
+                               }
                                continue;
                        }
                }
                addrs = addrs ? add(cp, add(", ", addrs)) : getcpy(cp);
        }
 
-       if (addrs == NULL)
+       if (!addrs) {
                adios(NULL, "usage: %s [switches] [switches for postproc] address ...", invo_name);
+       }
 
        umask(~m_gmprot());
 
@@ -103,29 +104,19 @@ main(int argc, char **argv)
        rcvdistout(fp, form, addrs);
        fclose(fp);
 
-       if (distout(drft, tmpfil, backup) == NOTOK)
+       if (distout(drft, tmpfil, backup) == NOTOK) {
                done(1);
+       }
 
        vec[0] = mhbasename(postproc);
        vec[vecp++] = "-dist";
        vec[vecp++] = drft;
        vec[vecp] = NULL;
 
-       for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
-               sleep(5);
-       switch (child_id) {
-       case NOTOK:
-               admonish(NULL, "unable to fork");/* fall */
-       case OK:
-               execvp(postproc, vec);
-               fprintf(stderr, "unable to exec ");
-               perror(postproc);
-               _exit(1);
-
-       default:
-               done(pidXwait(child_id, postproc));
-       }
-
+       execvp(postproc, vec);
+       fprintf(stderr, "unable to exec ");
+       perror(postproc);
+       _exit(1);
        return 0;  /* dead code to satisfy the compiler */
 }
 
@@ -167,72 +158,85 @@ rcvdistout(FILE *inb, char *form, char *addrs)
        register struct comp *cptr, **savecomp;
        FILE *out;
 
-       if (!(out = fopen(drft, "w")))
+       if (!(out = fopen(drft, "w"))) {
                adios(drft, "unable to create");
+       }
 
        /* get new format string */
        cp = new_fs(form ? form : rcvdistcomps, NULL);
        format_len = strlen(cp);
        ncomps = fmt_compile(cp, &fmt) + 1;
        if (!(nxtbuf = compbuffers =
-                       (char **) calloc((size_t) ncomps, sizeof(char *))))
+                       (char **) calloc((size_t) ncomps, sizeof(char *)))) {
                adios(NULL, "unable to allocate component buffers");
+       }
        if (!(savecomp = used_buf =
                        (struct comp **) calloc((size_t) (ncomps + 1),
-                       sizeof(struct comp *))))
+                       sizeof(struct comp *)))) {
                adios(NULL, "unable to allocate component buffer stack");
+       }
        savecomp += ncomps + 1;
        *--savecomp = 0;
 
-       for (i = ncomps; i--;)
+       for (i = ncomps; i--;) {
                *nxtbuf++ = mh_xmalloc(SBUFSIZ);
+       }
        nxtbuf = compbuffers;
        tmpbuf = *nxtbuf++;
 
        for (ap = addrcomps; *ap; ap++) {
                FINDCOMP(cptr, *ap);
-               if (cptr)
+               if (cptr) {
                        cptr->c_type |= CT_ADDR;
+               }
        }
 
        FINDCOMP(cptr, "addresses");
-       if (cptr)
+       if (cptr) {
                cptr->c_text = addrs;
-
-       for (state = FLD;;) {
-               switch (state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb)) {
+       }
+       state = FLD;
+       while (1) {
+               state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb);
+               switch (state) {
                case FLD:
                case FLDPLUS:
-                       if ((cptr = wantcomp[CHASH(name)]))
+                       if ((cptr = wantcomp[CHASH(name)])) {
                                do {
-                                       if (!mh_strcasecmp(name, cptr->c_name)) {
-                                               char_read += msg_count;
-                                               if (!cptr->c_text) {
-                                                       cptr->c_text = tmpbuf;
-                                                       *--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);
+                                       if (mh_strcasecmp(name, cptr->c_name)!=0) {
+                                               continue;
+                                       }
+                                       char_read += msg_count;
+                                       if (!cptr->c_text) {
+                                               cptr->c_text = tmpbuf;
+                                               *--savecomp = cptr;
+                                               tmpbuf = *nxtbuf++;
+                                       } else {
+                                               cp = cptr->c_text;
+                                               i = strlen(cp) - 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;
                                        }
+                                       break;
                                } while ((cptr = cptr->c_next));
+                       }
 
-                       while (state == FLDPLUS)
-                               state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb);
+                       while (state == FLDPLUS) {
+                               state = m_getfld(state, name, tmpbuf,
+                                               SBUFSIZ, inb);
+                       }
                        break;
 
                case LENERR:
@@ -254,30 +258,35 @@ finished: ;
        fmt_scan(fmt, scanl, i, dat);
        fputs(scanl, out);
 
-       if (ferror(out))
+       if (ferror(out)) {
                adios(drft, "error writing");
+       }
        fclose(out);
 
        free(scanl);
        for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++);
-                       nxtbuf++, i--)
+                       nxtbuf++, i--) {
                free(cptr->c_text);
-       while (i-- > 0)
+       }
+       while (i-- > 0) {
                free(*nxtbuf++);
-       free((char *) compbuffers);
-       free((char *) used_buf);
+       }
+       free(compbuffers);
+       free(used_buf);
 }
 
 
 static void
 unlink_done(int status)
 {
-       if (backup[0])
+       if (*backup) {
                unlink(backup);
-       if (drft[0])
+       }
+       if (*drft) {
                unlink(drft);
-       if (tmpfil[0])
+       }
+       if (*tmpfil) {
                unlink(tmpfil);
-
+       }
        exit(status ? RCV_MBX : RCV_MOK);
 }