cleaned up edit1, use a file in /tmp, not in current directory, and more
[unix-history] / usr / src / usr.bin / mail / lex.c
index 63a711f..55c6754 100644 (file)
@@ -1,6 +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[] = "@(#)lex.c      5.13 (Berkeley) %G%";
+#endif /* notdef */
 
 #include "rcv.h"
 
 #include "rcv.h"
+#include <sys/stat.h>
+#include <errno.h>
 
 /*
  * Mail -- a mail program
 
 /*
  * Mail -- a mail program
@@ -8,7 +24,7 @@
  * Lexical processing of commands.
  */
 
  * Lexical processing of commands.
  */
 
-static char *SccsId = "@(#)lex.c       2.2 %G%";
+char   *prompt = "& ";
 
 /*
  * Set up editing on the given file name.
 
 /*
  * Set up editing on the given file name.
@@ -22,15 +38,36 @@ setfile(name, isedit)
 {
        FILE *ibuf;
        int i;
 {
        FILE *ibuf;
        int i;
+       struct stat stb;
        static int shudclob;
        static int shudclob;
-       static char efile[128];
        extern char tempMesg[];
        extern char tempMesg[];
+       extern int errno;
 
 
-       if ((ibuf = fopen(name, "r")) == NULL) {
-               if (isedit)
-                       perror(name);
-               else
-                       printf("No mail for %s\n", myname);
+       if ((ibuf = fopen(name, "r")) == NULL)
+               return(-1);
+
+       if (fstat(fileno(ibuf), &stb) < 0) {
+               fclose(ibuf);
+               return (-1);
+       }
+
+       switch (stb.st_mode & S_IFMT) {
+       case S_IFDIR:
+               fclose(ibuf);
+               errno = EISDIR;
+               return (-1);
+
+       case S_IFREG:
+               break;
+
+       default:
+               fclose(ibuf);
+               errno = EINVAL;
+               return (-1);
+       }
+
+       if (!edit && stb.st_size == 0) {
+               fclose(ibuf);
                return(-1);
        }
 
                return(-1);
        }
 
@@ -65,8 +102,6 @@ setfile(name, isedit)
        }
        shudclob = 1;
        edit = isedit;
        }
        shudclob = 1;
        edit = isedit;
-       strncpy(efile, name, 128);
-       editfile = efile;
        if (name != mailname)
                strcpy(mailname, name);
        mailsize = fsize(ibuf);
        if (name != mailname)
                strcpy(mailname, name);
        mailsize = fsize(ibuf);
@@ -83,7 +118,6 @@ setfile(name, isedit)
        setmsize(msgCount);
        fclose(ibuf);
        relsesigs();
        setmsize(msgCount);
        fclose(ibuf);
        relsesigs();
-       shudann = 1;
        sawcom = 0;
        return(0);
 }
        sawcom = 0;
        return(0);
 }
@@ -94,47 +128,24 @@ setfile(name, isedit)
  */
 
 int    *msgvec;
  */
 
 int    *msgvec;
+jmp_buf        commjmp;
 
 commands()
 {
 
 commands()
 {
-       int prompt, firstsw, stop();
+       int eofloop, stop();
        register int n;
        char linebuf[LINESIZE];
        int hangup(), contin();
 
        register int n;
        char linebuf[LINESIZE];
        int hangup(), contin();
 
-       sigset(SIGCONT, contin);
-       sighold(SIGCONT);
-       if (rcvmode) {
-               if (sigset(SIGINT, SIG_IGN) != SIG_IGN)
-                       sigset(SIGINT, stop);
-               if (sigset(SIGHUP, SIG_IGN) != SIG_IGN)
-                       sigset(SIGHUP, hangup);
+       signal(SIGCONT, SIG_DFL);
+       if (rcvmode && !sourcing) {
+               if (signal(SIGINT, SIG_IGN) != SIG_IGN)
+                       signal(SIGINT, stop);
+               if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
+                       signal(SIGHUP, hangup);
        }
        }
-       input = stdin;
-       prompt = 1;
-       if (!intty)
-               prompt = 0;
-       firstsw = 1;
        for (;;) {
                setexit();
        for (;;) {
                setexit();
-               if (firstsw > 0) {
-                       firstsw = 0;
-                       source1(mailrc);
-                       if (!nosrc)
-                               source1(MASTER);
-               }
-
-               /*
-                * How's this for obscure:  after we
-                * finish sourcing for the first time,
-                * go off and print the headers!
-                */
-
-               if (shudann && !sourcing) {
-                       shudann = 0;
-                       if (rcvmode)
-                               announce(edit);
-               }
 
                /*
                 * Print the prompt, if needed.  Clear out
 
                /*
                 * Print the prompt, if needed.  Clear out
@@ -143,13 +154,16 @@ commands()
 
                if (!rcvmode && !sourcing)
                        return;
 
                if (!rcvmode && !sourcing)
                        return;
+               eofloop = 0;
 top:
 top:
-               if (prompt && !sourcing) {
-                       sigrelse(SIGCONT);
-                       printf("_\r");
+               if (!sourcing && value("interactive") != NOSTR) {
+                       setjmp(commjmp);
+                       signal(SIGCONT, contin);
+                       printf(prompt);
                }
                }
-               flush();
-               sreset();
+               fflush(stdout);
+               if (!sourcing)
+                       sreset();
 
                /*
                 * Read a line of commands from the current input
 
                /*
                 * Read a line of commands from the current input
@@ -158,20 +172,24 @@ top:
 
                n = 0;
                for (;;) {
 
                n = 0;
                for (;;) {
-                       if (readline(input, &linebuf[n]) <= 0) {
+                       if (readline(input, &linebuf[n]) < 0) {
                                if (n != 0)
                                        break;
                                if (n != 0)
                                        break;
+                               if (loading)
+                                       return;
                                if (sourcing) {
                                        unstack();
                                        goto more;
                                }
                                if (sourcing) {
                                        unstack();
                                        goto more;
                                }
-                               if (value("ignoreeof") != NOSTR && prompt) {
-                                       printf("Use \"quit\" to quit.\n");
-                                       goto top;
+                               if (value("interactive") != NOSTR &&
+                                   value("ignoreeof") != NOSTR) {
+                                       if (++eofloop < 25) {
+                                               printf("Use \"quit\" to quit.\n");
+                                               goto top;
+                                       }
                                }
                                }
-                               if (!edit)
-                                       return;
-                               edstop();
+                               if (edit)
+                                       edstop();
                                return;
                        }
                        if ((n = strlen(linebuf)) == 0)
                                return;
                        }
                        if ((n = strlen(linebuf)) == 0)
@@ -181,7 +199,7 @@ top:
                                break;
                        linebuf[n++] = ' ';
                }
                                break;
                        linebuf[n++] = ' ';
                }
-               sighold(SIGCONT);
+               signal(SIGCONT, SIG_DFL);
                if (execute(linebuf, 0))
                        return;
 more:          ;
                if (execute(linebuf, 0))
                        return;
 more:          ;
@@ -215,9 +233,8 @@ execute(linebuf, contxt)
         * lexical conventions.
         */
 
         * lexical conventions.
         */
 
-       cp = linebuf;
-       while (any(*cp, " \t"))
-               cp++;
+       for (cp = linebuf; isspace(*cp); cp++)
+               ;
        if (*cp == '!') {
                if (sourcing) {
                        printf("Can't \"!\" while sourcing\n");
        if (*cp == '!') {
                if (sourcing) {
                        printf("Can't \"!\" while sourcing\n");
@@ -240,11 +257,13 @@ execute(linebuf, contxt)
         * confusion.
         */
 
         * confusion.
         */
 
-       if (sourcing && equal(word, ""))
+       if (sourcing && *word == '\0')
                return(0);
        com = lex(word);
        if (com == NONE) {
                return(0);
        com = lex(word);
        if (com == NONE) {
-               printf("What?\n");
+               printf("Unknown command: \"%s\"\n", word);
+               if (loading)
+                       return(1);
                if (sourcing)
                        unstack();
                return(0);
                if (sourcing)
                        unstack();
                return(0);
@@ -266,11 +285,13 @@ execute(linebuf, contxt)
         */
 
        if (com->c_func == edstop && sourcing) {
         */
 
        if (com->c_func == edstop && sourcing) {
+               if (loading)
+                       return(1);
                unstack();
                return(0);
        }
        if (!edit && com->c_func == edstop) {
                unstack();
                return(0);
        }
        if (!edit && com->c_func == edstop) {
-               sigset(SIGINT, SIG_IGN);
+               signal(SIGINT, SIG_IGN);
                return(1);
        }
 
                return(1);
        }
 
@@ -284,6 +305,8 @@ execute(linebuf, contxt)
        if (!rcvmode && (com->c_argtype & M) == 0) {
                printf("May not execute \"%s\" while sending\n",
                    com->c_name);
        if (!rcvmode && (com->c_argtype & M) == 0) {
                printf("May not execute \"%s\" while sending\n",
                    com->c_name);
+               if (loading)
+                       return(1);
                if (sourcing)
                        unstack();
                return(0);
                if (sourcing)
                        unstack();
                return(0);
@@ -291,12 +314,16 @@ execute(linebuf, contxt)
        if (sourcing && com->c_argtype & I) {
                printf("May not execute \"%s\" while sourcing\n",
                    com->c_name);
        if (sourcing && com->c_argtype & I) {
                printf("May not execute \"%s\" while sourcing\n",
                    com->c_name);
+               if (loading)
+                       return(1);
                unstack();
                return(0);
        }
        if (readonly && com->c_argtype & W) {
                printf("May not execute \"%s\" -- message file is read only\n",
                   com->c_name);
                unstack();
                return(0);
        }
        if (readonly && com->c_argtype & W) {
                printf("May not execute \"%s\" -- message file is read only\n",
                   com->c_name);
+               if (loading)
+                       return(1);
                if (sourcing)
                        unstack();
                return(0);
                if (sourcing)
                        unstack();
                return(0);
@@ -312,6 +339,10 @@ execute(linebuf, contxt)
                 * A message list defaulting to nearest forward
                 * legal message.
                 */
                 * A message list defaulting to nearest forward
                 * legal message.
                 */
+               if (msgvec == 0) {
+                       printf("Illegal use of \"message list\"\n");
+                       return(-1);
+               }
                if ((c = getmsglist(cp, msgvec, com->c_msgflag)) < 0)
                        break;
                if (c  == 0) {
                if ((c = getmsglist(cp, msgvec, com->c_msgflag)) < 0)
                        break;
                if (c  == 0) {
@@ -331,6 +362,10 @@ execute(linebuf, contxt)
                 * A message list with no defaults, but no error
                 * if none exist.
                 */
                 * A message list with no defaults, but no error
                 * if none exist.
                 */
+               if (msgvec == 0) {
+                       printf("Illegal use of \"message list\"\n");
+                       return(-1);
+               }
                if (getmsglist(cp, msgvec, com->c_msgflag) < 0)
                        break;
                e = (*com->c_func)(msgvec);
                if (getmsglist(cp, msgvec, com->c_msgflag) < 0)
                        break;
                e = (*com->c_func)(msgvec);
@@ -341,7 +376,7 @@ execute(linebuf, contxt)
                 * Just the straight string, with
                 * leading blanks removed.
                 */
                 * Just the straight string, with
                 * leading blanks removed.
                 */
-               while (any(*cp, " \t"))
+               while (isspace(*cp))
                        cp++;
                e = (*com->c_func)(cp);
                break;
                        cp++;
                e = (*com->c_func)(cp);
                break;
@@ -350,7 +385,8 @@ execute(linebuf, contxt)
                /*
                 * A vector of strings, in shell style.
                 */
                /*
                 * A vector of strings, in shell style.
                 */
-               if ((c = getrawlist(cp, arglist)) < 0)
+               if ((c = getrawlist(cp, arglist,
+                               sizeof arglist / sizeof *arglist)) < 0)
                        break;
                if (c < com->c_minargs) {
                        printf("%s requires at least %d arg(s)\n",
                        break;
                if (c < com->c_minargs) {
                        printf("%s requires at least %d arg(s)\n",
@@ -382,6 +418,8 @@ execute(linebuf, contxt)
         * error.
         */
 
         * error.
         */
 
+       if (e && loading)
+               return(1);
        if (e && sourcing)
                unstack();
        if (com->c_func == edstop)
        if (e && sourcing)
                unstack();
        if (com->c_func == edstop)
@@ -400,27 +438,21 @@ execute(linebuf, contxt)
 /*
  * When we wake up after ^Z, reprint the prompt.
  */
 /*
  * When we wake up after ^Z, reprint the prompt.
  */
+/*ARGSUSED*/
 contin(s)
 {
 
 contin(s)
 {
 
-       printf("_\r");
-       fflush(stdout);
+       longjmp(commjmp, 1);
 }
 
 /*
 }
 
 /*
- * Branch here on hangup signal and simulate quit.
+ * Branch here on hangup signal and simulate "exit".
  */
  */
-hangup()
+/*ARGSUSED*/
+hangup(s)
 {
 
 {
 
-       holdsigs();
-       if (edit) {
-               if (setexit())
-                       exit(0);
-               edstop();
-       }
-       else
-               quit();
+       /* nothing to do? */
        exit(0);
 }
 
        exit(0);
 }
 
@@ -432,8 +464,8 @@ hangup()
 setmsize(sz)
 {
 
 setmsize(sz)
 {
 
-       if (msgvec != (int *) 0)
-               cfree(msgvec);
+       if (msgvec != 0)
+               cfree((char *) msgvec);
        msgvec = (int *) calloc((unsigned) (sz + 1), sizeof *msgvec);
 }
 
        msgvec = (int *) calloc((unsigned) (sz + 1), sizeof *msgvec);
 }
 
@@ -474,20 +506,46 @@ isprefix(as1, as2)
 }
 
 /*
 }
 
 /*
- * The following gets called on receipt of a rubout.  This is
+ * The following gets called on receipt of an interrupt.  This is
  * to abort printout of a command, mainly.
  * Dispatching here when command() is inactive crashes rcv.
  * Close all open files except 0, 1, 2, and the temporary.
  * to abort printout of a command, mainly.
  * Dispatching here when command() is inactive crashes rcv.
  * Close all open files except 0, 1, 2, and the temporary.
- * The special call to getuserid() is needed so it won't get
- * annoyed about losing its open file.
  * Also, unstack all source files.
  */
 
 int    inithdr;                        /* am printing startup headers */
 
  * Also, unstack all source files.
  */
 
 int    inithdr;                        /* am printing startup headers */
 
+#ifdef _NFILE
+static
+_fwalk(function)
+       register int (*function)();
+{
+       register FILE *iop;
+
+       for (iop = _iob; iop < _iob + _NFILE; iop++)
+               (*function)(iop);
+}
+#endif
+
+static
+xclose(iop)
+       register FILE *iop;
+{
+       if (iop == stdin || iop == stdout ||
+           iop == stderr || iop == itf || iop == otf)
+               return;
+
+       if (iop != pipef)
+               fclose(iop);
+       else {
+               pclose(pipef);
+               pipef = NULL;
+       }
+}
+
+/*ARGSUSED*/
 stop(s)
 {
 stop(s)
 {
-       register FILE *fp;
 
        noreset = 0;
        if (!inithdr)
 
        noreset = 0;
        if (!inithdr)
@@ -495,28 +553,17 @@ stop(s)
        inithdr = 0;
        while (sourcing)
                unstack();
        inithdr = 0;
        while (sourcing)
                unstack();
-       getuserid((char *) -1);
-       for (fp = &_iob[0]; fp < &_iob[_NFILE]; fp++) {
-               if (fp == stdin || fp == stdout)
-                       continue;
-               if (fp == itf || fp == otf)
-                       continue;
-               if (fp == stderr)
-                       continue;
-               if (fp == pipef) {
-                       pclose(pipef);
-                       pipef = NULL;
-                       continue;
-               }
-               fclose(fp);
-       }
+
+       /*
+        * Walk through all the open FILEs, applying xclose() to them
+        */
+       _fwalk(xclose);
+
        if (image >= 0) {
                close(image);
                image = -1;
        }
        if (image >= 0) {
                close(image);
                image = -1;
        }
-       clrbuf(stdout);
-       printf("Interrupt\n");
-       sigrelse(s);
+       fprintf(stderr, "Interrupt\n");
        reset(0);
 }
 
        reset(0);
 }
 
@@ -525,20 +572,15 @@ stop(s)
  * give the message count, and print a header listing.
  */
 
  * give the message count, and print a header listing.
  */
 
-char   *greeting       = "Mail version %s.  Type ? for help.\n";
-
-announce(pr)
+announce()
 {
        int vec[2], mdot;
 {
        int vec[2], mdot;
-       extern char *version;
 
        mdot = newfileinfo();
        vec[0] = mdot;
        vec[1] = 0;
 
        mdot = newfileinfo();
        vec[0] = mdot;
        vec[1] = 0;
-       if (pr && value("quiet") == NOSTR)
-               printf(greeting, version);
        dot = &message[mdot - 1];
        dot = &message[mdot - 1];
-       if (msgCount > 0 && !noheader) {
+       if (msgCount > 0 && value("noheader") == NOSTR) {
                inithdr++;
                headers(vec);
                inithdr = 0;
                inithdr++;
                headers(vec);
                inithdr = 0;
@@ -549,11 +591,11 @@ announce(pr)
  * Announce information about the file we are editing.
  * Return a likely place to set dot.
  */
  * Announce information about the file we are editing.
  * Return a likely place to set dot.
  */
-
 newfileinfo()
 {
        register struct message *mp;
 newfileinfo()
 {
        register struct message *mp;
-       register int u, n, mdot;
+       register int u, n, mdot, d, s;
+       char fname[BUFSIZ], zname[BUFSIZ], *ename;
 
        for (mp = &message[0]; mp < &message[msgCount]; mp++)
                if (mp->m_flag & MNEW)
 
        for (mp = &message[0]; mp < &message[msgCount]; mp++)
                if (mp->m_flag & MNEW)
@@ -566,13 +608,26 @@ newfileinfo()
                mdot = mp - &message[0] + 1;
        else
                mdot = 1;
                mdot = mp - &message[0] + 1;
        else
                mdot = 1;
+       s = d = 0;
        for (mp = &message[0], n = 0, u = 0; mp < &message[msgCount]; mp++) {
                if (mp->m_flag & MNEW)
                        n++;
                if ((mp->m_flag & MREAD) == 0)
                        u++;
        for (mp = &message[0], n = 0, u = 0; mp < &message[msgCount]; mp++) {
                if (mp->m_flag & MNEW)
                        n++;
                if ((mp->m_flag & MREAD) == 0)
                        u++;
+               if (mp->m_flag & MDELETED)
+                       d++;
+               if (mp->m_flag & MSAVED)
+                       s++;
        }
        }
-       printf("\"%s\": ", mailname);
+       ename = mailname;
+       if (getfold(fname) >= 0) {
+               strcat(fname, "/");
+               if (strncmp(fname, mailname, strlen(fname)) == 0) {
+                       sprintf(zname, "+%s", mailname + strlen(fname));
+                       ename = zname;
+               }
+       }
+       printf("\"%s\": ", ename);
        if (msgCount == 1)
                printf("1 message");
        else
        if (msgCount == 1)
                printf("1 message");
        else
@@ -581,20 +636,46 @@ newfileinfo()
                printf(" %d new", n);
        if (u-n > 0)
                printf(" %d unread", u);
                printf(" %d new", n);
        if (u-n > 0)
                printf(" %d unread", u);
+       if (d > 0)
+               printf(" %d deleted", d);
+       if (s > 0)
+               printf(" %d saved", s);
        if (readonly)
                printf(" [Read only]");
        printf("\n");
        return(mdot);
 }
 
        if (readonly)
                printf(" [Read only]");
        printf("\n");
        return(mdot);
 }
 
-strace() {}
-
 /*
  * Print the current version number.
  */
 
 /*
  * Print the current version number.
  */
 
+/*ARGSUSED*/
 pversion(e)
 {
 pversion(e)
 {
+       extern char *version;
+
        printf("Version %s\n", version);
        return(0);
 }
        printf("Version %s\n", version);
        return(0);
 }
+
+/*
+ * Load a file of user definitions.
+ */
+load(name)
+       char *name;
+{
+       register FILE *in, *oldin;
+
+       if ((in = fopen(name, "r")) == NULL)
+               return;
+       oldin = input;
+       input = in;
+       loading = 1;
+       sourcing = 1;
+       commands();
+       loading = 0;
+       sourcing = 0;
+       input = oldin;
+       fclose(in);
+}