4.4BSD snapshot (revision 8.1); add 1993 to copyright
[unix-history] / usr / src / usr.bin / mail / cmd3.c
index 5746086..987e2d9 100644 (file)
@@ -1,22 +1,16 @@
 /*
 /*
- * Copyright (c) 1980 Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1980, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that this notice is preserved and that due credit is given
- * to the University of California at Berkeley. The name of the University
- * may not be used to endorse or promote products derived from this
- * software without specific prior written permission. This software
- * is provided ``as is'' without express or implied warranty.
+ * %sccs.include.redist.c%
  */
 
  */
 
-#ifdef notdef
-static char sccsid[] = "@(#)cmd3.c     5.12 (Berkeley) %G%";
-#endif /* notdef */
+#ifndef lint
+static char sccsid[] = "@(#)cmd3.c     8.1 (Berkeley) %G%";
+#endif /* not lint */
 
 #include "rcv.h"
 
 #include "rcv.h"
-#include <sys/stat.h>
-#include <sys/wait.h>
+#include "extern.h"
 
 /*
  * Mail -- a mail program
 
 /*
  * Mail -- a mail program
@@ -28,78 +22,42 @@ static char sccsid[] = "@(#)cmd3.c  5.12 (Berkeley) %G%";
  * Process a shell escape by saving signals, ignoring signals,
  * and forking a sh -c
  */
  * Process a shell escape by saving signals, ignoring signals,
  * and forking a sh -c
  */
-
+int
 shell(str)
        char *str;
 {
 shell(str)
        char *str;
 {
-       int (*sigint)(), (*sigquit)();
-       union wait stat;
-       register int t;
-       char *Shell;
+       sig_t sigint = signal(SIGINT, SIG_IGN);
+       char *shell;
        char cmd[BUFSIZ];
 
        char cmd[BUFSIZ];
 
-       strcpy(cmd, str);
+       (void) strcpy(cmd, str);
        if (bangexp(cmd) < 0)
        if (bangexp(cmd) < 0)
-               return(-1);
-       if ((Shell = value("SHELL")) == NOSTR)
-               Shell = SHELL;
-       sigint = signal(SIGINT, SIG_IGN);
-       sigquit = signal(SIGQUIT, SIG_IGN);
-       t = vfork();
-       if (t == 0) {
-               if (sigint != SIG_IGN)
-                       signal(SIGINT, SIG_DFL);
-               if (sigquit != SIG_IGN)
-                       signal(SIGQUIT, SIG_DFL);
-               execl(Shell, Shell, "-c", cmd, (char *)0);
-               perror(Shell);
-               _exit(1);
-       }
-       while (wait(&stat) != t)
-               ;
-       if (t == -1)
-               perror("fork");
-       signal(SIGINT, sigint);
-       signal(SIGQUIT, sigquit);
+               return 1;
+       if ((shell = value("SHELL")) == NOSTR)
+               shell = _PATH_CSHELL;
+       (void) run_command(shell, 0, -1, -1, "-c", cmd, NOSTR);
+       (void) signal(SIGINT, sigint);
        printf("!\n");
        printf("!\n");
-       return(0);
+       return 0;
 }
 
 /*
  * Fork an interactive shell.
  */
 }
 
 /*
  * Fork an interactive shell.
  */
-
 /*ARGSUSED*/
 /*ARGSUSED*/
+int
 dosh(str)
        char *str;
 {
 dosh(str)
        char *str;
 {
-       int (*sigint)(), (*sigquit)();
-       union wait stat;
-       register int t;
-       char *Shell;
-
-       if ((Shell = value("SHELL")) == NOSTR)
-               Shell = SHELL;
-       sigint = signal(SIGINT, SIG_IGN);
-       sigquit = signal(SIGQUIT, SIG_IGN);
-       t = vfork();
-       if (t == 0) {
-               if (sigint != SIG_IGN)
-                       signal(SIGINT, SIG_DFL);
-               if (sigquit != SIG_IGN)
-                       signal(SIGQUIT, SIG_DFL);
-               execl(Shell, Shell, (char *)0);
-               perror(Shell);
-               _exit(1);
-       }
-       while (wait(&stat) != t)
-               ;
-       if (t == -1)
-               perror("fork");
-       signal(SIGINT, sigint);
-       signal(SIGQUIT, sigquit);
+       sig_t sigint = signal(SIGINT, SIG_IGN);
+       char *shell;
+
+       if ((shell = value("SHELL")) == NOSTR)
+               shell = _PATH_CSHELL;
+       (void) run_command(shell, 0, -1, -1, NOSTR, NOSTR, NOSTR);
+       (void) signal(SIGINT, sigint);
        putchar('\n');
        putchar('\n');
-       return(0);
+       return 0;
 }
 
 /*
 }
 
 /*
@@ -109,6 +67,7 @@ dosh(str)
 
 char   lastbang[128];
 
 
 char   lastbang[128];
 
+int
 bangexp(str)
        char *str;
 {
 bangexp(str)
        char *str;
 {
@@ -160,44 +119,44 @@ overf:
  * Print out a nice help message from some file or another.
  */
 
  * Print out a nice help message from some file or another.
  */
 
+int
 help()
 {
        register c;
        register FILE *f;
 
 help()
 {
        register c;
        register FILE *f;
 
-       if ((f = fopen(HELPFILE, "r")) == NULL) {
-               perror(HELPFILE);
+       if ((f = Fopen(_PATH_HELP, "r")) == NULL) {
+               perror(_PATH_HELP);
                return(1);
        }
        while ((c = getc(f)) != EOF)
                putchar(c);
                return(1);
        }
        while ((c = getc(f)) != EOF)
                putchar(c);
-       fclose(f);
+       Fclose(f);
        return(0);
 }
 
 /*
  * Change user's working directory.
  */
        return(0);
 }
 
 /*
  * Change user's working directory.
  */
-
-schdir(str)
-       char *str;
+int
+schdir(arglist)
+       char **arglist;
 {
 {
-       register char *cp;
+       char *cp;
 
 
-       for (cp = str; *cp == ' '; cp++)
-               ;
-       if (*cp == '\0')
+       if (*arglist == NOSTR)
                cp = homedir;
        else
                cp = homedir;
        else
-               if ((cp = expand(cp)) == NOSTR)
+               if ((cp = expand(*arglist)) == NOSTR)
                        return(1);
        if (chdir(cp) < 0) {
                perror(cp);
                return(1);
        }
                        return(1);
        if (chdir(cp) < 0) {
                perror(cp);
                return(1);
        }
-       return(0);
+       return 0;
 }
 
 }
 
+int
 respond(msgvec)
        int *msgvec;
 {
 respond(msgvec)
        int *msgvec;
 {
@@ -211,6 +170,7 @@ respond(msgvec)
  * Reply to a list of messages.  Extract each name from the
  * message header and send them off to mail1()
  */
  * Reply to a list of messages.  Extract each name from the
  * message header and send them off to mail1()
  */
+int
 _respond(msgvec)
        int *msgvec;
 {
 _respond(msgvec)
        int *msgvec;
 {
@@ -225,6 +185,7 @@ _respond(msgvec)
                return(1);
        }
        mp = &message[msgvec[0] - 1];
                return(1);
        }
        mp = &message[msgvec[0] - 1];
+       touch(mp);
        dot = mp;
        if ((rcv = skin(hfield("from", mp))) == NOSTR)
                rcv = skin(nameof(mp, 1));
        dot = mp;
        if ((rcv = skin(hfield("from", mp))) == NOSTR)
                rcv = skin(nameof(mp, 1));
@@ -239,10 +200,10 @@ _respond(msgvec)
         * Delete my name from the reply list,
         * and with it, all my alternate names.
         */
         * Delete my name from the reply list,
         * and with it, all my alternate names.
         */
-       np = delname(np, myname, icequal);
+       np = delname(np, myname);
        if (altnames)
                for (ap = altnames; *ap; ap++)
        if (altnames)
                for (ap = altnames; *ap; ap++)
-                       np = delname(np, *ap, icequal);
+                       np = delname(np, *ap);
        if (np != NIL && replyto == NOSTR)
                np = cat(np, extract(rcv, GTO));
        else if (np == NIL) {
        if (np != NIL && replyto == NOSTR)
                np = cat(np, extract(rcv, GTO));
        else if (np == NIL) {
@@ -256,10 +217,10 @@ _respond(msgvec)
        head.h_subject = reedit(head.h_subject);
        if (replyto == NOSTR && (cp = skin(hfield("cc", mp))) != NOSTR) {
                np = elide(extract(cp, GCC));
        head.h_subject = reedit(head.h_subject);
        if (replyto == NOSTR && (cp = skin(hfield("cc", mp))) != NOSTR) {
                np = elide(extract(cp, GCC));
-               np = delname(np, myname, icequal);
+               np = delname(np, myname);
                if (altnames != 0)
                        for (ap = altnames; *ap; ap++)
                if (altnames != 0)
                        for (ap = altnames; *ap; ap++)
-                               np = delname(np, *ap, icequal);
+                               np = delname(np, *ap);
                head.h_cc = np;
        } else
                head.h_cc = NIL;
                head.h_cc = np;
        } else
                head.h_cc = NIL;
@@ -295,7 +256,7 @@ reedit(subj)
  * Preserve the named messages, so that they will be sent
  * back to the system mailbox.
  */
  * Preserve the named messages, so that they will be sent
  * back to the system mailbox.
  */
-
+int
 preserve(msgvec)
        int *msgvec;
 {
 preserve(msgvec)
        int *msgvec;
 {
@@ -319,6 +280,7 @@ preserve(msgvec)
 /*
  * Mark all given messages as unread.
  */
 /*
  * Mark all given messages as unread.
  */
+int
 unread(msgvec)
        int     msgvec[];
 {
 unread(msgvec)
        int     msgvec[];
 {
@@ -335,7 +297,7 @@ unread(msgvec)
 /*
  * Print the size of each message.
  */
 /*
  * Print the size of each message.
  */
-
+int
 messize(msgvec)
        int *msgvec;
 {
 messize(msgvec)
        int *msgvec;
 {
@@ -354,8 +316,9 @@ messize(msgvec)
  * Quit quickly.  If we are sourcing, just pop the input level
  * by returning an error.
  */
  * Quit quickly.  If we are sourcing, just pop the input level
  * by returning an error.
  */
-
+int
 rexit(e)
 rexit(e)
+       int e;
 {
        if (sourcing)
                return(1);
 {
        if (sourcing)
                return(1);
@@ -367,7 +330,7 @@ rexit(e)
  * Set or display a variable value.  Syntax is similar to that
  * of csh.
  */
  * Set or display a variable value.  Syntax is similar to that
  * of csh.
  */
-
+int
 set(arglist)
        char **arglist;
 {
 set(arglist)
        char **arglist;
 {
@@ -376,7 +339,7 @@ set(arglist)
        char varbuf[BUFSIZ], **ap, **p;
        int errs, h, s;
 
        char varbuf[BUFSIZ], **ap, **p;
        int errs, h, s;
 
-       if (argcount(arglist) == 0) {
+       if (*arglist == NOSTR) {
                for (h = 0, s = 1; h < HSHSIZE; h++)
                        for (vp = variables[h]; vp != NOVAR; vp = vp->v_link)
                                s++;
                for (h = 0, s = 1; h < HSHSIZE; h++)
                        for (vp = variables[h]; vp != NOVAR; vp = vp->v_link)
                                s++;
@@ -414,7 +377,7 @@ set(arglist)
 /*
  * Unset a bunch of variable values.
  */
 /*
  * Unset a bunch of variable values.
  */
-
+int
 unset(arglist)
        char **arglist;
 {
 unset(arglist)
        char **arglist;
 {
@@ -436,7 +399,7 @@ unset(arglist)
                        variables[h] = variables[h]->v_link;
                        vfree(vp2->v_name);
                        vfree(vp2->v_value);
                        variables[h] = variables[h]->v_link;
                        vfree(vp2->v_name);
                        vfree(vp2->v_value);
-                       cfree((char *)vp2);
+                       free((char *)vp2);
                        continue;
                }
                for (vp = variables[h]; vp->v_link != vp2; vp = vp->v_link)
                        continue;
                }
                for (vp = variables[h]; vp->v_link != vp2; vp = vp->v_link)
@@ -444,7 +407,7 @@ unset(arglist)
                vp->v_link = vp2->v_link;
                vfree(vp2->v_name);
                vfree(vp2->v_value);
                vp->v_link = vp2->v_link;
                vfree(vp2->v_name);
                vfree(vp2->v_value);
-               cfree((char *) vp2);
+               free((char *) vp2);
        }
        return(errs);
 }
        }
        return(errs);
 }
@@ -452,7 +415,7 @@ unset(arglist)
 /*
  * Put add users to a group.
  */
 /*
  * Put add users to a group.
  */
-
+int
 group(argv)
        char **argv;
 {
 group(argv)
        char **argv;
 {
@@ -462,7 +425,7 @@ group(argv)
        int s;
        char **ap, *gname, **p;
 
        int s;
        char **ap, *gname, **p;
 
-       if (argcount(argv) == 0) {
+       if (*argv == NOSTR) {
                for (h = 0, s = 1; h < HSHSIZE; h++)
                        for (gh = groups[h]; gh != NOGRP; gh = gh->g_link)
                                s++;
                for (h = 0, s = 1; h < HSHSIZE; h++)
                        for (gh = groups[h]; gh != NOGRP; gh = gh->g_link)
                                s++;
@@ -476,7 +439,7 @@ group(argv)
                        printgroup(*p);
                return(0);
        }
                        printgroup(*p);
                return(0);
        }
-       if (argcount(argv) == 1) {
+       if (argv[1] == NOSTR) {
                printgroup(*argv);
                return(0);
        }
                printgroup(*argv);
                return(0);
        }
@@ -509,7 +472,7 @@ group(argv)
  * Sort the passed string vecotor into ascending dictionary
  * order.
  */
  * Sort the passed string vecotor into ascending dictionary
  * order.
  */
-
+void
 sort(list)
        char **list;
 {
 sort(list)
        char **list;
 {
@@ -520,18 +483,18 @@ sort(list)
                ;
        if (ap-list < 2)
                return;
                ;
        if (ap-list < 2)
                return;
-       qsort((char *)list, ap-list, sizeof *list, diction);
+       qsort(list, ap-list, sizeof(*list), diction);
 }
 
 /*
  * Do a dictionary order comparison of the arguments from
  * qsort.
  */
 }
 
 /*
  * Do a dictionary order comparison of the arguments from
  * qsort.
  */
-
+int
 diction(a, b)
 diction(a, b)
-       register char **a, **b;
+       const void *a, *b;
 {
 {
-       return(strcmp(*a, *b));
+       return(strcmp(*(char **)a, *(char **)b));
 }
 
 /*
 }
 
 /*
@@ -539,33 +502,28 @@ diction(a, b)
  */
 
 /*ARGSUSED*/
  */
 
 /*ARGSUSED*/
+int
 null(e)
 null(e)
+       int e;
 {
        return 0;
 }
 
 /*
 {
        return 0;
 }
 
 /*
- * Print out the current edit file, if we are editing.
- * Otherwise, print the name of the person who's mail
- * we are reading.
+ * Change to another file.  With no argument, print information about
+ * the current file.
  */
  */
-
+int
 file(argv)
        register char **argv;
 {
 file(argv)
        register char **argv;
 {
-       register char *cp;
 
        if (argv[0] == NOSTR) {
                newfileinfo();
                return 0;
        }
 
        if (argv[0] == NOSTR) {
                newfileinfo();
                return 0;
        }
-       if ((cp = expand(*argv)) == NOSTR)
-               return -1;
-       strcpy(prevfile, mailname);
-       if (setfile(cp, **argv != '%')) {
-               perror(cp);
-               return -1;
-       }
+       if (setfile(*argv) < 0)
+               return 1;
        announce();
        return 0;
 }
        announce();
        return 0;
 }
@@ -573,6 +531,7 @@ file(argv)
 /*
  * Expand file names like echo
  */
 /*
  * Expand file names like echo
  */
+int
 echo(argv)
        char **argv;
 {
 echo(argv)
        char **argv;
 {
@@ -591,6 +550,7 @@ echo(argv)
        return 0;
 }
 
        return 0;
 }
 
+int
 Respond(msgvec)
        int *msgvec;
 {
 Respond(msgvec)
        int *msgvec;
 {
@@ -605,6 +565,7 @@ Respond(msgvec)
  * and not messing around with the To: and Cc: lists as in normal
  * reply.
  */
  * and not messing around with the To: and Cc: lists as in normal
  * reply.
  */
+int
 _Respond(msgvec)
        int msgvec[];
 {
 _Respond(msgvec)
        int msgvec[];
 {
@@ -616,6 +577,7 @@ _Respond(msgvec)
        head.h_to = NIL;
        for (ap = msgvec; *ap != 0; ap++) {
                mp = &message[*ap - 1];
        head.h_to = NIL;
        for (ap = msgvec; *ap != 0; ap++) {
                mp = &message[*ap - 1];
+               touch(mp);
                dot = mp;
                if ((cp = skin(hfield("from", mp))) == NOSTR)
                        cp = skin(nameof(mp, 2));
                dot = mp;
                if ((cp = skin(hfield("from", mp))) == NOSTR)
                        cp = skin(nameof(mp, 2));
@@ -638,7 +600,7 @@ _Respond(msgvec)
  * Conditional commands.  These allow one to parameterize one's
  * .mailrc and do some things if sending, others if receiving.
  */
  * Conditional commands.  These allow one to parameterize one's
  * .mailrc and do some things if sending, others if receiving.
  */
-
+int
 ifcmd(argv)
        char **argv;
 {
 ifcmd(argv)
        char **argv;
 {
@@ -670,7 +632,7 @@ ifcmd(argv)
  * Implement 'else'.  This is pretty simple -- we just
  * flip over the conditional flag.
  */
  * Implement 'else'.  This is pretty simple -- we just
  * flip over the conditional flag.
  */
-
+int
 elsecmd()
 {
 
 elsecmd()
 {
 
@@ -698,7 +660,7 @@ elsecmd()
 /*
  * End of if statement.  Just set cond back to anything.
  */
 /*
  * End of if statement.  Just set cond back to anything.
  */
-
+int
 endifcmd()
 {
 
 endifcmd()
 {
 
@@ -713,6 +675,7 @@ endifcmd()
 /*
  * Set the list of alternate names.
  */
 /*
  * Set the list of alternate names.
  */
+int
 alternates(namelist)
        char **namelist;
 {
 alternates(namelist)
        char **namelist;
 {
@@ -729,7 +692,7 @@ alternates(namelist)
                return(0);
        }
        if (altnames != 0)
                return(0);
        }
        if (altnames != 0)
-               cfree((char *) altnames);
+               free((char *) altnames);
        altnames = (char **) calloc((unsigned) c, sizeof (char *));
        for (ap = namelist, ap2 = altnames; *ap; ap++, ap2++) {
                cp = (char *) calloc((unsigned) strlen(*ap) + 1, sizeof (char));
        altnames = (char **) calloc((unsigned) c, sizeof (char *));
        for (ap = namelist, ap2 = altnames; *ap; ap++, ap2++) {
                cp = (char *) calloc((unsigned) strlen(*ap) + 1, sizeof (char));