BSD 4_3_Reno release
[unix-history] / usr / src / usr.bin / mail / lex.c
index 1ed9ca2..880c99a 100644 (file)
@@ -2,17 +2,24 @@
  * Copyright (c) 1980 Regents of the University of California.
  * All rights reserved.
  *
  * 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.
+ * Redistribution and use in source and binary forms are permitted provided
+ * that: (1) source distributions retain this entire copyright notice and
+ * comment, and (2) distributions including binaries display the following
+ * acknowledgement:  ``This product includes software developed by the
+ * University of California, Berkeley and its contributors'' in the
+ * documentation or other materials provided with the distribution and in
+ * all advertising materials mentioning features or use of this software.
+ * Neither the name of the University nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
  */
 
-#ifdef notdef
-static char sccsid[] = "@(#)lex.c      5.9 (Berkeley) %G%";
-#endif /* notdef */
+#ifndef lint
+static char sccsid[] = "@(#)lex.c      5.21 (Berkeley) 6/25/90";
+#endif /* not lint */
 
 #include "rcv.h"
 #include <sys/stat.h>
 
 #include "rcv.h"
 #include <sys/stat.h>
@@ -28,50 +35,55 @@ char        *prompt = "& ";
 
 /*
  * Set up editing on the given file name.
 
 /*
  * Set up editing on the given file name.
- * If isedit is true, we are considered to be editing the file,
- * otherwise we are reading our mail which has signficance for
- * mbox and so forth.
+ * If the first character of name is %, we are considered to be
+ * editing the file, otherwise we are reading our mail which has
+ * signficance for mbox and so forth.
  */
  */
-
-setfile(name, isedit)
+setfile(name)
        char *name;
 {
        FILE *ibuf;
        int i;
        struct stat stb;
        char *name;
 {
        FILE *ibuf;
        int i;
        struct stat stb;
+       char isedit = *name != '%';
+       char *who = name[1] ? name + 1 : myname;
        static int shudclob;
        static int shudclob;
-       static char efile[128];
        extern char tempMesg[];
        extern int errno;
 
        extern char tempMesg[];
        extern int errno;
 
-       if ((ibuf = fopen(name, "r")) == NULL)
+       if ((name = expand(name)) == NOSTR)
+               return -1;
+
+       if ((ibuf = Fopen(name, "r")) == NULL) {
+               if (!isedit && errno == ENOENT)
+                       goto nomail;
+               perror(name);
                return(-1);
                return(-1);
+       }
 
        if (fstat(fileno(ibuf), &stb) < 0) {
 
        if (fstat(fileno(ibuf), &stb) < 0) {
-               fclose(ibuf);
+               perror("fstat");
+               Fclose(ibuf);
                return (-1);
        }
 
        switch (stb.st_mode & S_IFMT) {
        case S_IFDIR:
                return (-1);
        }
 
        switch (stb.st_mode & S_IFMT) {
        case S_IFDIR:
-               fclose(ibuf);
+               Fclose(ibuf);
                errno = EISDIR;
                errno = EISDIR;
+               perror(name);
                return (-1);
 
        case S_IFREG:
                break;
 
        default:
                return (-1);
 
        case S_IFREG:
                break;
 
        default:
-               fclose(ibuf);
+               Fclose(ibuf);
                errno = EINVAL;
                errno = EINVAL;
+               perror(name);
                return (-1);
        }
 
                return (-1);
        }
 
-       if (!edit && stb.st_size == 0) {
-               fclose(ibuf);
-               return(-1);
-       }
-
        /*
         * Looks like all will be well.  We must now relinquish our
         * hold on the current set of stuff.  Must hold signals
        /*
         * Looks like all will be well.  We must now relinquish our
         * hold on the current set of stuff.  Must hold signals
@@ -80,12 +92,8 @@ setfile(name, isedit)
         */
 
        holdsigs();
         */
 
        holdsigs();
-       if (shudclob) {
-               if (edit)
-                       edstop();
-               else
-                       quit();
-       }
+       if (shudclob)
+               quit();
 
        /*
         * Copy the messages into /tmp
 
        /*
         * Copy the messages into /tmp
@@ -103,8 +111,7 @@ setfile(name, isedit)
        }
        shudclob = 1;
        edit = isedit;
        }
        shudclob = 1;
        edit = isedit;
-       strncpy(efile, name, 128);
-       editfile = efile;
+       strcpy(prevfile, mailname);
        if (name != mailname)
                strcpy(mailname, name);
        mailsize = fsize(ibuf);
        if (name != mailname)
                strcpy(mailname, name);
        mailsize = fsize(ibuf);
@@ -119,81 +126,62 @@ setfile(name, isedit)
        remove(tempMesg);
        setptr(ibuf);
        setmsize(msgCount);
        remove(tempMesg);
        setptr(ibuf);
        setmsize(msgCount);
-       fclose(ibuf);
+       Fclose(ibuf);
        relsesigs();
        sawcom = 0;
        relsesigs();
        sawcom = 0;
+       if (!edit && msgCount == 0) {
+nomail:
+               fprintf(stderr, "No mail for %s\n", who);
+               return -1;
+       }
        return(0);
 }
 
        return(0);
 }
 
+int    *msgvec;
+int    reset_on_stop;                  /* do a reset() if stopped */
+
 /*
  * Interpret user commands one by one.  If standard input is not a tty,
  * print no prompt.
  */
 /*
  * Interpret user commands one by one.  If standard input is not a tty,
  * print no prompt.
  */
-
-int    *msgvec;
-jmp_buf        commjmp;
-
 commands()
 {
 commands()
 {
-       int eofloop, shudprompt, stop();
+       int eofloop = 0;
        register int n;
        char linebuf[LINESIZE];
        register int n;
        char linebuf[LINESIZE];
-       int hangup(), contin();
+       int intr(), stop(), hangup(), contin();
 
 
-       signal(SIGCONT, SIG_DFL);
-       if (rcvmode && !sourcing) {
+       if (!sourcing) {
                if (signal(SIGINT, SIG_IGN) != SIG_IGN)
                if (signal(SIGINT, SIG_IGN) != SIG_IGN)
-                       signal(SIGINT, stop);
+                       signal(SIGINT, intr);
                if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
                        signal(SIGHUP, hangup);
                if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
                        signal(SIGHUP, hangup);
+               signal(SIGTSTP, stop);
+               signal(SIGTTOU, stop);
+               signal(SIGTTIN, stop);
        }
        }
-       shudprompt = intty && !sourcing;
+       setexit();
        for (;;) {
        for (;;) {
-               setexit();
-
                /*
                 * Print the prompt, if needed.  Clear out
                 * string space, and flush the output.
                 */
                /*
                 * Print the prompt, if needed.  Clear out
                 * string space, and flush the output.
                 */
-
-               if (!rcvmode && !sourcing)
-                       return;
-               eofloop = 0;
-top:
-               if (shudprompt) {
-                       setjmp(commjmp);
-                       signal(SIGCONT, contin);
+               if (!sourcing && value("interactive") != NOSTR) {
+                       reset_on_stop = 1;
                        printf(prompt);
                }
                fflush(stdout);
                        printf(prompt);
                }
                fflush(stdout);
-               if (!sourcing)
-                       sreset();
-
+               sreset();
                /*
                 * Read a line of commands from the current input
                 * and handle end of file specially.
                 */
                /*
                 * Read a line of commands from the current input
                 * and handle end of file specially.
                 */
-
                n = 0;
                for (;;) {
                n = 0;
                for (;;) {
-                       if (readline(input, &linebuf[n]) < 0) {
-                               if (n != 0)
-                                       break;
-                               if (loading)
-                                       return;
-                               if (sourcing) {
-                                       unstack();
-                                       goto more;
-                               }
-                               if (value("ignoreeof") != NOSTR && shudprompt) {
-                                       if (++eofloop < 25) {
-                                               printf("Use \"quit\" to quit.\n");
-                                               goto top;
-                                       }
-                               }
-                               if (edit)
-                                       edstop();
-                               return;
+                       if (readline(input, &linebuf[n], LINESIZE - n) < 0) {
+                               if (n == 0)
+                                       n = -1;
+                               break;
                        }
                        if ((n = strlen(linebuf)) == 0)
                                break;
                        }
                        if ((n = strlen(linebuf)) == 0)
                                break;
@@ -202,20 +190,36 @@ top:
                                break;
                        linebuf[n++] = ' ';
                }
                                break;
                        linebuf[n++] = ' ';
                }
-               signal(SIGCONT, SIG_DFL);
+               reset_on_stop = 0;
+               if (n < 0) {
+                               /* eof */
+                       if (loading)
+                               break;
+                       if (sourcing) {
+                               unstack();
+                               continue;
+                       }
+                       if (value("interactive") != NOSTR &&
+                           value("ignoreeof") != NOSTR &&
+                           ++eofloop < 25) {
+                               printf("Use \"quit\" to quit.\n");
+                               continue;
+                       }
+                       break;
+               }
+               eofloop = 0;
                if (execute(linebuf, 0))
                if (execute(linebuf, 0))
-                       return;
-more:          ;
+                       break;
        }
 }
 
 /*
        }
 }
 
 /*
- * Execute a single command.  If the command executed
- * is "quit," then return non-zero so that the caller
- * will know to return back to main, if he cares.
+ * Execute a single command.
+ * Command functions return 0 for success, 1 for error, and -1
+ * for abort.  A 1 or -1 aborts a load or source.  A -1 aborts
+ * the interactive command loop.
  * Contxt is non-zero if called while composing mail.
  */
  * Contxt is non-zero if called while composing mail.
  */
-
 execute(linebuf, contxt)
        char linebuf[];
 {
 execute(linebuf, contxt)
        char linebuf[];
 {
@@ -225,7 +229,7 @@ execute(linebuf, contxt)
        register char *cp, *cp2;
        register int c;
        int muvec[2];
        register char *cp, *cp2;
        register int c;
        int muvec[2];
-       int edstop(), e;
+       int e = 1;
 
        /*
         * Strip the white space away from the beginning
 
        /*
         * Strip the white space away from the beginning
@@ -241,14 +245,13 @@ execute(linebuf, contxt)
        if (*cp == '!') {
                if (sourcing) {
                        printf("Can't \"!\" while sourcing\n");
        if (*cp == '!') {
                if (sourcing) {
                        printf("Can't \"!\" while sourcing\n");
-                       unstack();
-                       return(0);
+                       goto out;
                }
                shell(cp+1);
                return(0);
        }
        cp2 = word;
                }
                shell(cp+1);
                return(0);
        }
        cp2 = word;
-       while (*cp && !any(*cp, " \t0123456789$^.:/-+*'\""))
+       while (*cp && index(" \t0123456789$^.:/-+*'\"", *cp) == NOSTR)
                *cp2++ = *cp++;
        *cp2 = '\0';
 
                *cp2++ = *cp++;
        *cp2 = '\0';
 
@@ -265,11 +268,7 @@ execute(linebuf, contxt)
        com = lex(word);
        if (com == NONE) {
                printf("Unknown command: \"%s\"\n", word);
        com = lex(word);
        if (com == NONE) {
                printf("Unknown command: \"%s\"\n", word);
-               if (loading)
-                       return(1);
-               if (sourcing)
-                       unstack();
-               return(0);
+               goto out;
        }
 
        /*
        }
 
        /*
@@ -281,23 +280,6 @@ execute(linebuf, contxt)
                if (cond == CRCV && !rcvmode || cond == CSEND && rcvmode)
                        return(0);
 
                if (cond == CRCV && !rcvmode || cond == CSEND && rcvmode)
                        return(0);
 
-       /*
-        * Special case so that quit causes a return to
-        * main, who will call the quit code directly.
-        * If we are in a source file, just unstack.
-        */
-
-       if (com->c_func == edstop && sourcing) {
-               if (loading)
-                       return(1);
-               unstack();
-               return(0);
-       }
-       if (!edit && com->c_func == edstop) {
-               signal(SIGINT, SIG_IGN);
-               return(1);
-       }
-
        /*
         * Process the arguments to the command, depending
         * on the type he expects.  Default to an error.
        /*
         * Process the arguments to the command, depending
         * on the type he expects.  Default to an error.
@@ -308,34 +290,22 @@ 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);
+               goto out;
        }
        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);
+               goto out;
        }
        if (readonly && com->c_argtype & W) {
                printf("May not execute \"%s\" -- message file is read only\n",
                   com->c_name);
        }
        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);
+               goto out;
        }
        if (contxt && com->c_argtype & R) {
                printf("Cannot recursively invoke \"%s\"\n", com->c_name);
        }
        if (contxt && com->c_argtype & R) {
                printf("Cannot recursively invoke \"%s\"\n", com->c_name);
-               return(0);
+               goto out;
        }
        }
-       e = 1;
        switch (com->c_argtype & ~(F|P|I|M|T|W|R)) {
        case MSGLIST:
                /*
        switch (com->c_argtype & ~(F|P|I|M|T|W|R)) {
        case MSGLIST:
                /*
@@ -344,7 +314,7 @@ execute(linebuf, contxt)
                 */
                if (msgvec == 0) {
                        printf("Illegal use of \"message list\"\n");
                 */
                if (msgvec == 0) {
                        printf("Illegal use of \"message list\"\n");
-                       return(-1);
+                       break;
                }
                if ((c = getmsglist(cp, msgvec, com->c_msgflag)) < 0)
                        break;
                }
                if ((c = getmsglist(cp, msgvec, com->c_msgflag)) < 0)
                        break;
@@ -367,7 +337,7 @@ execute(linebuf, contxt)
                 */
                if (msgvec == 0) {
                        printf("Illegal use of \"message list\"\n");
                 */
                if (msgvec == 0) {
                        printf("Illegal use of \"message list\"\n");
-                       return(-1);
+                       break;
                }
                if (getmsglist(cp, msgvec, com->c_msgflag) < 0)
                        break;
                }
                if (getmsglist(cp, msgvec, com->c_msgflag) < 0)
                        break;
@@ -416,17 +386,20 @@ execute(linebuf, contxt)
                panic("Unknown argtype");
        }
 
                panic("Unknown argtype");
        }
 
+out:
        /*
         * Exit the current source file on
         * error.
         */
        /*
         * Exit the current source file on
         * error.
         */
-
-       if (e && loading)
-               return(1);
-       if (e && sourcing)
-               unstack();
-       if (com->c_func == edstop)
-               return(1);
+       if (e) {
+               if (e < 0)
+                       return 1;
+               if (loading)
+                       return 1;
+               if (sourcing)
+                       unstack();
+               return 0;
+       }
        if (value("autoprint") != NOSTR && com->c_argtype & P)
                if ((dot->m_flag & MDELETED) == 0) {
                        muvec[0] = dot - &message[0] + 1;
        if (value("autoprint") != NOSTR && com->c_argtype & P)
                if ((dot->m_flag & MDELETED) == 0) {
                        muvec[0] = dot - &message[0] + 1;
@@ -438,27 +411,6 @@ execute(linebuf, contxt)
        return(0);
 }
 
        return(0);
 }
 
-/*
- * When we wake up after ^Z, reprint the prompt.
- */
-/*ARGSUSED*/
-contin(s)
-{
-
-       longjmp(commjmp, 1);
-}
-
-/*
- * Branch here on hangup signal and simulate "exit".
- */
-/*ARGSUSED*/
-hangup(s)
-{
-
-       /* nothing to do? */
-       exit(0);
-}
-
 /*
  * Set the size of the message vector used to construct argument
  * lists to message list functions.
 /*
  * Set the size of the message vector used to construct argument
  * lists to message list functions.
@@ -518,36 +470,8 @@ isprefix(as1, as2)
 
 int    inithdr;                        /* am printing startup headers */
 
 
 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*/
 /*ARGSUSED*/
-stop(s)
+intr(s)
 {
 
        noreset = 0;
 {
 
        noreset = 0;
@@ -557,10 +481,7 @@ stop(s)
        while (sourcing)
                unstack();
 
        while (sourcing)
                unstack();
 
-       /*
-        * Walk through all the open FILEs, applying xclose() to them
-        */
-       _fwalk(xclose);
+       close_all_files();
 
        if (image >= 0) {
                close(image);
 
        if (image >= 0) {
                close(image);
@@ -570,25 +491,48 @@ stop(s)
        reset(0);
 }
 
        reset(0);
 }
 
+/*
+ * When we wake up after ^Z, reprint the prompt.
+ */
+stop(s)
+{
+       sig_t old_action = signal(s, SIG_DFL);
+
+       sigsetmask(sigblock(0) & ~sigmask(s));
+       kill(0, s);
+       sigblock(sigmask(s));
+       signal(s, old_action);
+       if (reset_on_stop) {
+               reset_on_stop = 0;
+               reset(0);
+       }
+}
+
+/*
+ * Branch here on hangup signal and simulate "exit".
+ */
+/*ARGSUSED*/
+hangup(s)
+{
+
+       /* nothing to do? */
+       exit(1);
+}
+
 /*
  * Announce the presence of the current Mail version,
  * give the message count, and print a header listing.
  */
 
 /*
  * Announce the presence of the current Mail version,
  * 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;
 
 
-       if (pr && value("quiet") == NOSTR)
-               printf(greeting, version);
        mdot = newfileinfo();
        vec[0] = mdot;
        vec[1] = 0;
        dot = &message[mdot - 1];
        mdot = newfileinfo();
        vec[0] = mdot;
        vec[1] = 0;
        dot = &message[mdot - 1];
-       if (msgCount > 0 && !noheader) {
+       if (msgCount > 0 && value("noheader") == NOSTR) {
                inithdr++;
                headers(vec);
                inithdr = 0;
                inithdr++;
                headers(vec);
                inithdr = 0;
@@ -675,7 +619,7 @@ load(name)
 {
        register FILE *in, *oldin;
 
 {
        register FILE *in, *oldin;
 
-       if ((in = fopen(name, "r")) == NULL)
+       if ((in = Fopen(name, "r")) == NULL)
                return;
        oldin = input;
        input = in;
                return;
        oldin = input;
        input = in;
@@ -685,5 +629,5 @@ load(name)
        loading = 0;
        sourcing = 0;
        input = oldin;
        loading = 0;
        sourcing = 0;
        input = oldin;
-       fclose(in);
+       Fclose(in);
 }
 }