use shell="" instead of shell=none for no shell,
[unix-history] / usr / src / usr.bin / mail / cmd3.c
index c1e70a1..c6c3995 100644 (file)
@@ -1,7 +1,22 @@
-#
+/*
+ * Copyright (c) 1980 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.
+ */
+
+#ifdef notdef
+static char sccsid[] = "@(#)cmd3.c     5.10 (Berkeley) %G%";
+#endif /* notdef */
 
 #include "rcv.h"
 #include <sys/stat.h>
 
 #include "rcv.h"
 #include <sys/stat.h>
+#include <sys/wait.h>
 
 /*
  * Mail -- a mail program
 
 /*
  * Mail -- a mail program
@@ -9,8 +24,6 @@
  * Still more user commands.
  */
 
  * Still more user commands.
  */
 
-static char *SccsId = "@(#)cmd3.c      1.13 %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
@@ -19,7 +32,8 @@ static char *SccsId = "@(#)cmd3.c     1.13 %G%";
 shell(str)
        char *str;
 {
 shell(str)
        char *str;
 {
-       int (*sig[2])(), stat[1];
+       int (*sigint)(), (*sigquit)();
+       union wait stat;
        register int t;
        char *Shell;
        char cmd[BUFSIZ];
        register int t;
        char *Shell;
        char cmd[BUFSIZ];
@@ -29,23 +43,24 @@ shell(str)
                return(-1);
        if ((Shell = value("SHELL")) == NOSTR)
                Shell = SHELL;
                return(-1);
        if ((Shell = value("SHELL")) == NOSTR)
                Shell = SHELL;
-       for (t = 2; t < 4; t++)
-               sig[t-2] = sigset(t, SIG_IGN);
+       sigint = signal(SIGINT, SIG_IGN);
+       sigquit = signal(SIGQUIT, SIG_IGN);
        t = vfork();
        if (t == 0) {
        t = vfork();
        if (t == 0) {
-               for (t = 2; t < 4; t++)
-                       if (sig[t-2] != SIG_IGN)
-                               sigsys(t, SIG_DFL);
-               execl(Shell, Shell, "-c", cmd, 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);
        }
                perror(Shell);
                _exit(1);
        }
-       while (wait(stat) != t)
+       while (wait(&stat) != t)
                ;
        if (t == -1)
                perror("fork");
                ;
        if (t == -1)
                perror("fork");
-       for (t = 2; t < 4; t++)
-               sigset(t, sig[t-2]);
+       signal(SIGINT, sigint);
+       signal(SIGQUIT, sigquit);
        printf("!\n");
        return(0);
 }
        printf("!\n");
        return(0);
 }
@@ -54,31 +69,35 @@ shell(str)
  * Fork an interactive shell.
  */
 
  * Fork an interactive shell.
  */
 
+/*ARGSUSED*/
 dosh(str)
        char *str;
 {
 dosh(str)
        char *str;
 {
-       int (*sig[2])(), stat[1];
+       int (*sigint)(), (*sigquit)();
+       union wait stat;
        register int t;
        char *Shell;
        register int t;
        char *Shell;
+
        if ((Shell = value("SHELL")) == NOSTR)
                Shell = SHELL;
        if ((Shell = value("SHELL")) == NOSTR)
                Shell = SHELL;
-       for (t = 2; t < 4; t++)
-               sig[t-2] = sigset(t, SIG_IGN);
+       sigint = signal(SIGINT, SIG_IGN);
+       sigquit = signal(SIGQUIT, SIG_IGN);
        t = vfork();
        if (t == 0) {
        t = vfork();
        if (t == 0) {
-               for (t = 2; t < 4; t++)
-                       if (sig[t-2] != SIG_IGN)
-                               sigsys(t, SIG_DFL);
-               execl(Shell, Shell, 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);
        }
                perror(Shell);
                _exit(1);
        }
-       while (wait(stat) != t)
+       while (wait(&stat) != t)
                ;
        if (t == -1)
                perror("fork");
                ;
        if (t == -1)
                perror("fork");
-       for (t = 2; t < 4; t++)
-               sigsys(t, sig[t-2]);
+       signal(SIGINT, sigint);
+       signal(SIGQUIT, sigquit);
        putchar('\n');
        return(0);
 }
        putchar('\n');
        return(0);
 }
@@ -147,7 +166,7 @@ help()
        register FILE *f;
 
        if ((f = fopen(HELPFILE, "r")) == NULL) {
        register FILE *f;
 
        if ((f = fopen(HELPFILE, "r")) == NULL) {
-               printf("No help just now.\n");
+               perror(HELPFILE);
                return(1);
        }
        while ((c = getc(f)) != EOF)
                return(1);
        }
        while ((c = getc(f)) != EOF)
@@ -179,19 +198,28 @@ schdir(str)
        return(0);
 }
 
        return(0);
 }
 
+respond(msgvec)
+       int *msgvec;
+{
+       if (value("Replyall") == NOSTR)
+               return (_respond(msgvec));
+       else
+               return (_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()
  */
 
-respond(msgvec)
+_respond(msgvec)
        int *msgvec;
 {
        struct message *mp;
        int *msgvec;
 {
        struct message *mp;
-       char *cp, buf[2 * LINESIZE], *rcv, *replyto;
+       char *cp, *rcv, *replyto;
+       char buf[2 * LINESIZE], **ap;
        struct name *np;
        struct header head;
        struct name *np;
        struct header head;
-       char *netmap();
 
        if (msgvec[1] != 0) {
                printf("Sorry, can't reply to multiple messages at once\n");
 
        if (msgvec[1] != 0) {
                printf("Sorry, can't reply to multiple messages at once\n");
@@ -199,20 +227,31 @@ respond(msgvec)
        }
        mp = &message[msgvec[0] - 1];
        dot = mp;
        }
        mp = &message[msgvec[0] - 1];
        dot = mp;
-       rcv = nameof(mp, 1);
+       rcv = NOSTR;
+       cp = skin(nameof(mp, 1));
+       if (cp != NOSTR)
+           rcv = cp;
+       cp = skin(hfield("from", mp));
+       if (cp != NOSTR)
+           rcv = cp;
        replyto = skin(hfield("reply-to", mp));
        strcpy(buf, "");
        if (replyto != NOSTR)
                strcpy(buf, replyto);
        else {
        replyto = skin(hfield("reply-to", mp));
        strcpy(buf, "");
        if (replyto != NOSTR)
                strcpy(buf, replyto);
        else {
-               cp = hfield("to", mp);
+               cp = skin(hfield("to", mp));
                if (cp != NOSTR)
                        strcpy(buf, cp);
        }
        np = elide(extract(buf, GTO));
                if (cp != NOSTR)
                        strcpy(buf, cp);
        }
        np = elide(extract(buf, GTO));
-       /* rcv = rename(rcv); */
-       mapf(np, rcv);
-       np = delname(np, myname);
+       /*
+        * Delete my name from the reply list,
+        * and with it, all my alternate names.
+        */
+       np = delname(np, myname, icequal);
+       if (altnames)
+               for (ap = altnames; *ap; ap++)
+                       np = delname(np, *ap, icequal);
        head.h_seq = 1;
        cp = detract(np, 0);
        if (cp != NOSTR && replyto == NOSTR) {
        head.h_seq = 1;
        cp = detract(np, 0);
        if (cp != NOSTR && replyto == NOSTR) {
@@ -235,15 +274,18 @@ respond(msgvec)
        head.h_subject = reedit(head.h_subject);
        head.h_cc = NOSTR;
        if (replyto == NOSTR) {
        head.h_subject = reedit(head.h_subject);
        head.h_cc = NOSTR;
        if (replyto == NOSTR) {
-               cp = hfield("cc", mp);
+               cp = skin(hfield("cc", mp));
                if (cp != NOSTR) {
                        np = elide(extract(cp, GCC));
                if (cp != NOSTR) {
                        np = elide(extract(cp, GCC));
-                       mapf(np, rcv);
-                       np = delname(np, myname);
+                       np = delname(np, myname, icequal);
+                       if (altnames != 0)
+                               for (ap = altnames; *ap; ap++)
+                                       np = delname(np, *ap, icequal);
                        head.h_cc = detract(np, 0);
                }
        }
        head.h_bcc = NOSTR;
                        head.h_cc = detract(np, 0);
                }
        }
        head.h_bcc = NOSTR;
+       head.h_smopts = NOSTR;
        mail1(&head);
        return(0);
 }
        mail1(&head);
        return(0);
 }
@@ -267,7 +309,7 @@ reedit(subj)
        if (icequal(sbuf, "re:"))
                return(subj);
        newsubj = salloc(strlen(subj) + 6);
        if (icequal(sbuf, "re:"))
                return(subj);
        newsubj = salloc(strlen(subj) + 6);
-       sprintf(newsubj, "Re:  %s", subj);
+       sprintf(newsubj, "Re: %s", subj);
        return(newsubj);
 }
 
        return(newsubj);
 }
 
@@ -296,6 +338,22 @@ preserve(msgvec)
        return(0);
 }
 
        return(0);
 }
 
+/*
+ * Mark all given messages as unread.
+ */
+unread(msgvec)
+       int     msgvec[];
+{
+       register int *ip;
+
+       for (ip = msgvec; *ip != NULL; ip++) {
+               dot = &message[*ip-1];
+               dot->m_flag &= ~(MREAD|MTOUCH);
+               dot->m_flag |= MSTATUS;
+       }
+       return(0);
+}
+
 /*
  * Print the size of each message.
  */
 /*
  * Print the size of each message.
  */
@@ -309,7 +367,7 @@ messize(msgvec)
        for (ip = msgvec; *ip != NULL; ip++) {
                mesg = *ip;
                mp = &message[mesg-1];
        for (ip = msgvec; *ip != NULL; ip++) {
                mesg = *ip;
                mp = &message[mesg-1];
-               printf("%d: %d\n", mesg, msize(mp));
+               printf("%d: %d/%ld\n", mesg, mp->m_lines, mp->m_size);
        }
        return(0);
 }
        }
        return(0);
 }
@@ -324,6 +382,7 @@ rexit(e)
        if (sourcing)
                return(1);
        exit(e);
        if (sourcing)
                return(1);
        exit(e);
+       /*NOTREACHED*/
 }
 
 /*
 }
 
 /*
@@ -382,7 +441,6 @@ unset(arglist)
        char **arglist;
 {
        register struct var *vp, *vp2;
        char **arglist;
 {
        register struct var *vp, *vp2;
-       register char *cp;
        int errs, h;
        char **ap;
 
        int errs, h;
        char **ap;
 
@@ -400,7 +458,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(vp2);
+                       cfree((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)
@@ -408,7 +466,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(vp2);
+               cfree((char *) vp2);
        }
        return(errs);
 }
        }
        return(errs);
 }
@@ -484,7 +542,7 @@ sort(list)
                ;
        if (ap-list < 2)
                return;
                ;
        if (ap-list < 2)
                return;
-       qsort(list, ap-list, sizeof *list, diction);
+       qsort((char *)list, ap-list, sizeof *list, diction);
 }
 
 /*
 }
 
 /*
@@ -502,9 +560,10 @@ diction(a, b)
  * The do nothing command for comments.
  */
 
  * The do nothing command for comments.
  */
 
+/*ARGSUSED*/
 null(e)
 {
 null(e)
 {
-       return(0);
+       return 0;
 }
 
 /*
 }
 
 /*
@@ -514,96 +573,23 @@ null(e)
  */
 
 file(argv)
  */
 
 file(argv)
-       char **argv;
+       register char **argv;
 {
        register char *cp;
 {
        register char *cp;
-       char fname[BUFSIZ];
 
        if (argv[0] == NOSTR) {
                newfileinfo();
 
        if (argv[0] == NOSTR) {
                newfileinfo();
-               return(0);
+               return 0;
        }
        }
-
-       /*
-        * Acker's!  Must switch to the new file.
-        * We use a funny interpretation --
-        *      # -- gets the previous file
-        *      % -- gets the invoker's post office box
-        *      %user -- gets someone else's post office box
-        *      & -- gets invoker's mbox file
-        *      string -- reads the given file
-        */
-
-       cp = getfilename(argv[0]);
-       if (cp == NOSTR)
-               return(-1);
-       return(setfile(cp, 1));
-}
-
-/*
- * Evaluate the string given as a new mailbox name.
- * Ultimately, we want this to support a number of meta characters.
- * Possibly:
- *     % -- for my system mail box
- *     %user -- for user's system mail box
- *     # -- for previous file
- *     & -- get's invoker's mbox file
- *     file name -- for any other file
- */
-
-char   prevfile[PATHSIZE];
-
-char *
-getfilename(name)
-       char *name;
-{
-       register char *cp;
-       char savename[BUFSIZ];
-       char oldmailname[BUFSIZ];
-
-       switch (*name) {
-       case '%':
-               strcpy(prevfile, mailname);
-               if (name[1] != 0) {
-                       strcpy(savename, myname);
-                       strcpy(oldmailname, mailname);
-                       strncpy(myname, name+1, PATHSIZE-1);
-                       myname[PATHSIZE-1] = 0;
-                       findmail();
-                       cp = savestr(mailname);
-                       strcpy(myname, savename);
-                       strcpy(mailname, oldmailname);
-                       return(cp);
-               }
-               strcpy(oldmailname, mailname);
-               findmail();
-               cp = savestr(mailname);
-               strcpy(mailname, oldmailname);
-               return(cp);
-
-       case '#':
-               if (name[1] != 0)
-                       goto regular;
-               if (prevfile[0] == 0) {
-                       printf("No previous file\n");
-                       return(NOSTR);
-               }
-               cp = savestr(prevfile);
-               strcpy(prevfile, mailname);
-               return(cp);
-
-       case '&':
-               strcpy(prevfile, mailname);
-               if (name[1] == 0)
-                       return(mbox);
-               /* Fall into . . . */
-
-       default:
-regular:
-               strcpy(prevfile, mailname);
-               cp = expand(name);
-               return(cp);
+       if ((cp = expand(*argv)) == NOSTR)
+               return -1;
+       strcpy(prevfile, mailname);
+       if (setfile(cp, **argv != '%')) {
+               perror(cp);
+               return -1;
        }
        }
+       announce();
+       return 0;
 }
 
 /*
 }
 
 /*
@@ -618,30 +604,47 @@ echo(argv)
 
        for (ap = argv; *ap != NOSTR; ap++) {
                cp = *ap;
 
        for (ap = argv; *ap != NOSTR; ap++) {
                cp = *ap;
-               if ((cp = expand(cp)) != NOSTR)
-                       printf("%s ", cp);
+               if ((cp = expand(cp)) != NOSTR) {
+                       if (ap != argv)
+                               putchar(' ');
+                       printf("%s", cp);
+               }
        }
        }
+       putchar('\n');
+
        return(0);
 }
 
        return(0);
 }
 
+Respond(msgvec)
+       int *msgvec;
+{
+       if (value("Replyall") == NOSTR)
+               return (_Respond(msgvec));
+       else
+               return (_respond(msgvec));
+}
+
 /*
  * Reply to a series of messages by simply mailing to the senders
  * and not messing around with the To: and Cc: lists as in normal
  * reply.
  */
 
 /*
  * Reply to a series of messages by simply mailing to the senders
  * and not messing around with the To: and Cc: lists as in normal
  * reply.
  */
 
-Respond(msgvec)
+_Respond(msgvec)
        int msgvec[];
 {
        struct header head;
        struct message *mp;
        register int s, *ap;
        int msgvec[];
 {
        struct header head;
        struct message *mp;
        register int s, *ap;
-       register char *cp, *subject;
+       register char *cp, *cp2, *subject;
 
        for (s = 0, ap = msgvec; *ap != 0; ap++) {
                mp = &message[*ap - 1];
                dot = mp;
 
        for (s = 0, ap = msgvec; *ap != 0; ap++) {
                mp = &message[*ap - 1];
                dot = mp;
-               s += strlen(nameof(mp, 2)) + 1;
+               if ((cp = skin(hfield("from", mp))) != NOSTR)
+                   s+= strlen(cp) + 1;
+               else
+                   s += strlen(skin(nameof(mp, 2))) + 1;
        }
        if (s == 0)
                return(0);
        }
        if (s == 0)
                return(0);
@@ -649,7 +652,9 @@ Respond(msgvec)
        head.h_to = cp;
        for (ap = msgvec; *ap != 0; ap++) {
                mp = &message[*ap - 1];
        head.h_to = cp;
        for (ap = msgvec; *ap != 0; ap++) {
                mp = &message[*ap - 1];
-               cp = copy(nameof(mp, 2), cp);
+               if ((cp2 = skin(hfield("from", mp))) == NOSTR)
+                   cp2 = skin(nameof(mp, 2));
+               cp = copy(cp2, cp);
                *cp++ = ' ';
        }
        *--cp = 0;
                *cp++ = ' ';
        }
        *--cp = 0;
@@ -663,6 +668,7 @@ Respond(msgvec)
                head.h_seq++;
        head.h_cc = NOSTR;
        head.h_bcc = NOSTR;
                head.h_seq++;
        head.h_cc = NOSTR;
        head.h_bcc = NOSTR;
+       head.h_smopts = NOSTR;
        mail1(&head);
        return(0);
 }
        mail1(&head);
        return(0);
 }
@@ -742,3 +748,33 @@ endifcmd()
        cond = CANY;
        return(0);
 }
        cond = CANY;
        return(0);
 }
+
+/*
+ * Set the list of alternate names.
+ */
+alternates(namelist)
+       char **namelist;
+{
+       register int c;
+       register char **ap, **ap2, *cp;
+
+       c = argcount(namelist) + 1;
+       if (c == 1) {
+               if (altnames == 0)
+                       return(0);
+               for (ap = altnames; *ap; ap++)
+                       printf("%s ", *ap);
+               printf("\n");
+               return(0);
+       }
+       if (altnames != 0)
+               cfree((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));
+               strcpy(cp, *ap);
+               *ap2 = cp;
+       }
+       *ap2 = 0;
+       return(0);
+}