cleanups, add manual page
[unix-history] / usr / src / usr.bin / mail / lex.c
index 73a25df..64a8f05 100644 (file)
@@ -1,6 +1,27 @@
-#
+/*
+ * Copyright (c) 1980 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, 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'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
+#ifndef lint
+static char sccsid[] = "@(#)lex.c      5.16 (Berkeley) %G%";
+#endif /* not lint */
 
 #include "rcv.h"
 
 #include "rcv.h"
+#include <sys/stat.h>
+#include <errno.h>
 
 /*
  * Mail -- a mail program
 
 /*
  * Mail -- a mail program
  * Lexical processing of commands.
  */
 
  * Lexical processing of commands.
  */
 
-static char *SccsId = "@(#)lex.c       1.1 %G%";
+char   *prompt = "& ";
+
+/*
+ * 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.
+ */
+
+setfile(name, isedit)
+       char *name;
+{
+       FILE *ibuf;
+       int i;
+       struct stat stb;
+       static int shudclob;
+       extern char tempMesg[];
+       extern int errno;
+
+       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);
+       }
+
+       /*
+        * Looks like all will be well.  We must now relinquish our
+        * hold on the current set of stuff.  Must hold signals
+        * while we are reading the new file, else we will ruin
+        * the message[] data structure.
+        */
+
+       holdsigs();
+       if (shudclob) {
+               if (edit)
+                       edstop();
+               else
+                       quit();
+       }
+
+       /*
+        * Copy the messages into /tmp
+        * and set pointers.
+        */
+
+       readonly = 0;
+       if ((i = open(name, 1)) < 0)
+               readonly++;
+       else
+               close(i);
+       if (shudclob) {
+               fclose(itf);
+               fclose(otf);
+       }
+       shudclob = 1;
+       edit = isedit;
+       if (name != mailname)
+               strcpy(mailname, name);
+       mailsize = fsize(ibuf);
+       if ((otf = fopen(tempMesg, "w")) == NULL) {
+               perror(tempMesg);
+               exit(1);
+       }
+       if ((itf = fopen(tempMesg, "r")) == NULL) {
+               perror(tempMesg);
+               exit(1);
+       }
+       remove(tempMesg);
+       setptr(ibuf);
+       setmsize(msgCount);
+       fclose(ibuf);
+       relsesigs();
+       sawcom = 0;
+       return(0);
+}
 
 /*
  * Interpret user commands one by one.  If standard input is not a tty,
 
 /*
  * Interpret user commands one by one.  If standard input is not a tty,
@@ -16,42 +133,24 @@ static char *SccsId = "@(#)lex.c   1.1 %G%";
  */
 
 int    *msgvec;
  */
 
 int    *msgvec;
+jmp_buf        commjmp;
 
 commands()
 {
 
 commands()
 {
-       int prompt, firstsw, stop();
+       int eofloop, stop();
        register int n;
        char linebuf[LINESIZE];
        register int n;
        char linebuf[LINESIZE];
+       int hangup(), contin();
 
 
-       msgvec = (int *) calloc((unsigned) (msgCount + 1), sizeof *msgvec);
-       if (rcvmode)
-               if (signal(SIGINT, SIG_IGN) == SIG_DFL)
+       signal(SIGCONT, SIG_DFL);
+       if (rcvmode && !sourcing) {
+               if (signal(SIGINT, SIG_IGN) != SIG_IGN)
                        signal(SIGINT, stop);
                        signal(SIGINT, stop);
-       input = stdin;
-       prompt = 1;
-       if (!intty)
-               prompt = 0;
-       firstsw = 1;
+               if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
+                       signal(SIGHUP, hangup);
+       }
        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 (firstsw == 0 && !sourcing) {
-                       firstsw = -1;
-                       if (rcvmode)
-                               announce();
-               }
 
                /*
                 * Print the prompt, if needed.  Clear out
 
                /*
                 * Print the prompt, if needed.  Clear out
@@ -60,9 +159,14 @@ commands()
 
                if (!rcvmode && !sourcing)
                        return;
 
                if (!rcvmode && !sourcing)
                        return;
-               if (prompt && !sourcing)
-                       printf("_\r");
-               flush();
+               eofloop = 0;
+top:
+               if (!sourcing && value("interactive") != NOSTR) {
+                       setjmp(commjmp);
+                       signal(SIGCONT, contin);
+                       printf(prompt);
+               }
+               fflush(stdout);
                sreset();
 
                /*
                sreset();
 
                /*
@@ -72,18 +176,24 @@ commands()
 
                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 (!edit) {
-                                       signal(SIGINT, SIG_IGN);
-                                       return;
+                               if (value("interactive") != NOSTR &&
+                                   value("ignoreeof") != NOSTR) {
+                                       if (++eofloop < 25) {
+                                               printf("Use \"quit\" to quit.\n");
+                                               goto top;
+                                       }
                                }
                                }
-                               edstop();
+                               if (edit)
+                                       edstop();
                                return;
                        }
                        if ((n = strlen(linebuf)) == 0)
                                return;
                        }
                        if ((n = strlen(linebuf)) == 0)
@@ -93,7 +203,8 @@ commands()
                                break;
                        linebuf[n++] = ' ';
                }
                                break;
                        linebuf[n++] = ' ';
                }
-               if (execute(linebuf))
+               signal(SIGCONT, SIG_DFL);
+               if (execute(linebuf, 0))
                        return;
 more:          ;
        }
                        return;
 more:          ;
        }
@@ -103,9 +214,10 @@ more:              ;
  * 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.  If the command executed
  * is "quit," then return non-zero so that the caller
  * will know to return back to main, if he cares.
+ * Contxt is non-zero if called while composing mail.
  */
 
  */
 
-execute(linebuf)
+execute(linebuf, contxt)
        char linebuf[];
 {
        char word[LINESIZE];
        char linebuf[];
 {
        char word[LINESIZE];
@@ -113,6 +225,7 @@ execute(linebuf)
        struct cmd *com;
        register char *cp, *cp2;
        register int c;
        struct cmd *com;
        register char *cp, *cp2;
        register int c;
+       int muvec[2];
        int edstop(), e;
 
        /*
        int edstop(), e;
 
        /*
@@ -124,9 +237,8 @@ execute(linebuf)
         * 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");
@@ -137,7 +249,7 @@ execute(linebuf)
                return(0);
        }
        cp2 = word;
                return(0);
        }
        cp2 = word;
-       while (*cp && !any(*cp, " \t0123456789$^.-+*'\""))
+       while (*cp && index(" \t0123456789$^.:/-+*'\"", *cp) == NOSTR)
                *cp2++ = *cp++;
        *cp2 = '\0';
 
                *cp2++ = *cp++;
        *cp2 = '\0';
 
@@ -149,16 +261,27 @@ execute(linebuf)
         * 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);
        }
 
+       /*
+        * See if we should execute the command -- if a conditional
+        * we always execute it, otherwise, check the state of cond.
+        */
+
+       if ((com->c_argtype & F) == 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.
        /*
         * Special case so that quit causes a return to
         * main, who will call the quit code directly.
@@ -166,6 +289,8 @@ execute(linebuf)
         */
 
        if (com->c_func == edstop && sourcing) {
         */
 
        if (com->c_func == edstop && sourcing) {
+               if (loading)
+                       return(1);
                unstack();
                return(0);
        }
                unstack();
                return(0);
        }
@@ -184,22 +309,44 @@ execute(linebuf)
        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);
-               unstack();
+               if (loading)
+                       return(1);
+               if (sourcing)
+                       unstack();
                return(0);
        }
        if (sourcing && com->c_argtype & I) {
                printf("May not execute \"%s\" while sourcing\n",
                    com->c_name);
                return(0);
        }
        if (sourcing && com->c_argtype & I) {
                printf("May not execute \"%s\" while sourcing\n",
                    com->c_name);
+               if (loading)
+                       return(1);
                unstack();
                return(0);
        }
                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 (contxt && com->c_argtype & R) {
+               printf("Cannot recursively invoke \"%s\"\n", com->c_name);
+               return(0);
+       }
        e = 1;
        e = 1;
-       switch (com->c_argtype & ~(P|I|M)) {
+       switch (com->c_argtype & ~(F|P|I|M|T|W|R)) {
        case MSGLIST:
                /*
                 * A message list defaulting to nearest forward
                 * legal message.
                 */
        case MSGLIST:
                /*
                 * 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) {
@@ -219,6 +366,10 @@ execute(linebuf)
                 * 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);
@@ -229,7 +380,7 @@ execute(linebuf)
                 * 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;
@@ -238,7 +389,8 @@ execute(linebuf)
                /*
                 * 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",
@@ -270,18 +422,57 @@ execute(linebuf)
         * error.
         */
 
         * error.
         */
 
+       if (e && loading)
+               return(1);
        if (e && sourcing)
                unstack();
        if (com->c_func == edstop)
                return(1);
        if (value("autoprint") != NOSTR && com->c_argtype & P)
        if (e && sourcing)
                unstack();
        if (com->c_func == edstop)
                return(1);
        if (value("autoprint") != NOSTR && com->c_argtype & P)
-               if ((dot->m_flag & MDELETED) == 0)
-                       print(dot);
-       if (!sourcing)
+               if ((dot->m_flag & MDELETED) == 0) {
+                       muvec[0] = dot - &message[0] + 1;
+                       muvec[1] = 0;
+                       type(muvec);
+               }
+       if (!sourcing && (com->c_argtype & T) == 0)
                sawcom = 1;
        return(0);
 }
 
                sawcom = 1;
        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.
+ */
+setmsize(sz)
+{
+
+       if (msgvec != 0)
+               cfree((char *) msgvec);
+       msgvec = (int *) calloc((unsigned) (sz + 1), sizeof *msgvec);
+}
+
 /*
  * Find the correct command in the command table corresponding
  * to the passed command "word"
 /*
  * Find the correct command in the command table corresponding
  * to the passed command "word"
@@ -319,41 +510,64 @@ 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.
  */
 
  * Also, unstack all source files.
  */
 
-stop()
+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)
 {
 {
-       register FILE *fp;
 
        noreset = 0;
 
        noreset = 0;
-       signal(SIGINT, SIG_IGN);
-       sawcom++;
+       if (!inithdr)
+               sawcom++;
+       inithdr = 0;
        while (sourcing)
                unstack();
        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;
-               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");
-       signal(SIGINT, stop);
+       fprintf(stderr, "Interrupt\n");
        reset(0);
 }
 
        reset(0);
 }
 
@@ -362,36 +576,110 @@ stop()
  * give the message count, and print a header listing.
  */
 
  * give the message count, and print a header listing.
  */
 
-char   *greeting       = "Mail version 2.0 %s.  Type ? for help.\n";
-
 announce()
 {
 announce()
 {
-       int vec[2];
-       extern char *version;
+       int vec[2], mdot;
+
+       mdot = newfileinfo();
+       vec[0] = mdot;
+       vec[1] = 0;
+       dot = &message[mdot - 1];
+       if (msgCount > 0 && value("noheader") == NOSTR) {
+               inithdr++;
+               headers(vec);
+               inithdr = 0;
+       }
+}
+
+/*
+ * Announce information about the file we are editing.
+ * Return a likely place to set dot.
+ */
+newfileinfo()
+{
        register struct message *mp;
        register struct message *mp;
+       register int u, n, mdot, d, s;
+       char fname[BUFSIZ], zname[BUFSIZ], *ename;
 
 
-       if (value("hold") != NOSTR)
+       for (mp = &message[0]; mp < &message[msgCount]; mp++)
+               if (mp->m_flag & MNEW)
+                       break;
+       if (mp >= &message[msgCount])
                for (mp = &message[0]; mp < &message[msgCount]; mp++)
                for (mp = &message[0]; mp < &message[msgCount]; mp++)
-                       mp->m_flag |= MPRESERVE;
-       vec[0] = 1;
-       vec[1] = 0;
-       if (value("quiet") == NOSTR)
-               printf(greeting, version);
+                       if ((mp->m_flag & MREAD) == 0)
+                               break;
+       if (mp < &message[msgCount])
+               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++;
+               if (mp->m_flag & MDELETED)
+                       d++;
+               if (mp->m_flag & MSAVED)
+                       s++;
+       }
+       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)
        if (msgCount == 1)
-               printf("1 message:\n");
+               printf("1 message");
        else
        else
-               printf("%d messages:\n", msgCount);
-       headers(vec);
+               printf("%d messages", msgCount);
+       if (n > 0)
+               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);
 }
 
 }
 
-strace() {}
-
 /*
  * Print the current version number.
  */
 
 /*
  * Print the current version number.
  */
 
+/*ARGSUSED*/
 pversion(e)
 {
 pversion(e)
 {
-       printf(greeting, version);
+       extern char *version;
+
+       printf("Version %s\n", version);
        return(0);
 }
        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);
+}