Changed various fopens to open read/write to avoid trashing files
[unix-history] / usr / src / usr.bin / mail / lex.c
index 8d890dc..6c7f331 100644 (file)
@@ -8,7 +8,9 @@
  * Lexical processing of commands.
  */
 
  * Lexical processing of commands.
  */
 
-static char *SccsId = "@(#)lex.c       1.3 %G%";
+static char *SccsId = "@(#)lex.c       2.11 %G%";
+
+char   *prompt = "& ";
 
 /*
  * Set up editing on the given file name.
 
 /*
  * Set up editing on the given file name.
@@ -25,25 +27,18 @@ setfile(name, isedit)
        static int shudclob;
        static char efile[128];
        extern char tempMesg[];
        static int shudclob;
        static char efile[128];
        extern char tempMesg[];
-       int (*sigs[2])();
 
 
-       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);
                return(-1);
-       }
 
        /*
         * Looks like all will be well.  We must now relinquish our
 
        /*
         * Looks like all will be well.  We must now relinquish our
-        * hold on the current set of stuff.  Must ignore signals
+        * 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.
         */
 
         * while we are reading the new file, else we will ruin
         * the message[] data structure.
         */
 
-       for (i = SIGINT; i <= SIGQUIT; i++)
-               sigs[i - SIGINT] = signal(i, SIG_IGN);
+       holdsigs();
        if (shudclob) {
                if (edit)
                        edstop();
        if (shudclob) {
                if (edit)
                        edstop();
@@ -69,6 +64,8 @@ setfile(name, isedit)
        edit = isedit;
        strncpy(efile, name, 128);
        editfile = efile;
        edit = isedit;
        strncpy(efile, name, 128);
        editfile = efile;
+       if (name != mailname)
+               strcpy(mailname, name);
        mailsize = fsize(ibuf);
        if ((otf = fopen(tempMesg, "w")) == NULL) {
                perror(tempMesg);
        mailsize = fsize(ibuf);
        if ((otf = fopen(tempMesg, "w")) == NULL) {
                perror(tempMesg);
@@ -82,10 +79,8 @@ setfile(name, isedit)
        setptr(ibuf);
        setmsize(msgCount);
        fclose(ibuf);
        setptr(ibuf);
        setmsize(msgCount);
        fclose(ibuf);
-       for (i = SIGINT; i <= SIGQUIT; i++)
-               signal(i, sigs[i - SIGINT]);
-       printf("%s: ", name);
-       announce(!edit);
+       relsesigs();
+       sawcom = 0;
        return(0);
 }
 
        return(0);
 }
 
@@ -98,40 +93,23 @@ int *msgvec;
 
 commands()
 {
 
 commands()
 {
-       int prompt, firstsw, stop();
+       int eofloop, shudprompt, stop();
        register int n;
        char linebuf[LINESIZE];
        register int n;
        char linebuf[LINESIZE];
-
-       if (rcvmode)
-               if (signal(SIGINT, SIG_IGN) == SIG_DFL)
-                       signal(SIGINT, stop);
-       input = stdin;
-       prompt = 1;
-       if (!intty)
-               prompt = 0;
-       firstsw = 1;
+       int hangup(), contin();
+
+# ifdef VMUNIX
+       sigset(SIGCONT, SIG_DFL);
+# endif VMUNIX
+       if (rcvmode && !sourcing) {
+               if (sigset(SIGINT, SIG_IGN) != SIG_IGN)
+                       sigset(SIGINT, stop);
+               if (sigset(SIGHUP, SIG_IGN) != SIG_IGN)
+                       sigset(SIGHUP, hangup);
+       }
+       shudprompt = intty && !sourcing;
        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!
-                */
-
-#ifdef CRAZYWOW
-               if (firstsw == 0 && !sourcing) {
-                       firstsw = -1;
-                       if (rcvmode)
-                               announce(1);
-               }
-#endif
 
                /*
                 * Print the prompt, if needed.  Clear out
 
                /*
                 * Print the prompt, if needed.  Clear out
@@ -140,9 +118,14 @@ commands()
 
                if (!rcvmode && !sourcing)
                        return;
 
                if (!rcvmode && !sourcing)
                        return;
+               eofloop = 0;
 top:
 top:
-               if (prompt && !sourcing)
-                       printf("_\r");
+               if (shudprompt) {
+# ifdef VMUNIX
+                       sigset(SIGCONT, contin);
+# endif VMUNIX
+                       printf(prompt);
+               }
                flush();
                sreset();
 
                flush();
                sreset();
 
@@ -156,19 +139,20 @@ top:
                        if (readline(input, &linebuf[n]) <= 0) {
                                if (n != 0)
                                        break;
                        if (readline(input, &linebuf[n]) <= 0) {
                                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 (!edit) {
-                                       signal(SIGINT, SIG_IGN);
-                                       return;
+                               if (value("ignoreeof") != NOSTR && shudprompt) {
+                                       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)
@@ -178,7 +162,10 @@ top:
                                break;
                        linebuf[n++] = ' ';
                }
                                break;
                        linebuf[n++] = ' ';
                }
-               if (execute(linebuf))
+# ifdef VMUNIX
+               sigset(SIGCONT, SIG_DFL);
+# endif VMUNIX
+               if (execute(linebuf, 0))
                        return;
 more:          ;
        }
                        return;
 more:          ;
        }
@@ -188,9 +175,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];
@@ -223,7 +211,7 @@ execute(linebuf)
                return(0);
        }
        cp2 = word;
                return(0);
        }
        cp2 = word;
-       while (*cp && !any(*cp, " \t0123456789$^./-+*'\""))
+       while (*cp && !any(*cp, " \t0123456789$^.:/-+*'\""))
                *cp2++ = *cp++;
        *cp2 = '\0';
 
                *cp2++ = *cp++;
        *cp2 = '\0';
 
@@ -239,12 +227,23 @@ execute(linebuf)
                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.
@@ -252,11 +251,13 @@ execute(linebuf)
         */
 
        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) {
-               signal(SIGINT, SIG_IGN);
+               sigset(SIGINT, SIG_IGN);
                return(1);
        }
 
                return(1);
        }
 
@@ -270,29 +271,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);
        }
        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);
        }
+       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|W)) {
+       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) {
@@ -312,6 +328,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);
@@ -363,6 +383,8 @@ execute(linebuf)
         * 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)
@@ -373,11 +395,38 @@ execute(linebuf)
                        muvec[1] = 0;
                        type(muvec);
                }
                        muvec[1] = 0;
                        type(muvec);
                }
-       if (!sourcing)
+       if (!sourcing && (com->c_argtype & T) == 0)
                sawcom = 1;
        return(0);
 }
 
                sawcom = 1;
        return(0);
 }
 
+/*
+ * When we wake up after ^Z, reprint the prompt.
+ */
+contin(s)
+{
+
+       printf(prompt);
+       fflush(stdout);
+}
+
+/*
+ * Branch here on hangup signal and simulate quit.
+ */
+hangup()
+{
+
+       holdsigs();
+       if (edit) {
+               if (setexit())
+                       exit(0);
+               edstop();
+       }
+       else
+               quit();
+       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.
@@ -437,13 +486,19 @@ isprefix(as1, as2)
  * Also, unstack all source files.
  */
 
  * Also, unstack all source files.
  */
 
-stop()
+int    inithdr;                        /* am printing startup headers */
+
+stop(s)
 {
        register FILE *fp;
 
 {
        register FILE *fp;
 
+# ifndef VMUNIX
+       s = SIGINT;
+# endif VMUNIX
        noreset = 0;
        noreset = 0;
-       signal(SIGINT, SIG_IGN);
-       sawcom++;
+       if (!inithdr)
+               sawcom++;
+       inithdr = 0;
        while (sourcing)
                unstack();
        getuserid((char *) -1);
        while (sourcing)
                unstack();
        getuserid((char *) -1);
@@ -467,7 +522,11 @@ stop()
        }
        clrbuf(stdout);
        printf("Interrupt\n");
        }
        clrbuf(stdout);
        printf("Interrupt\n");
-       signal(SIGINT, stop);
+# ifdef VMUNIX
+       sigrelse(s);
+# else
+       signal(s, stop);
+# endif
        reset(0);
 }
 
        reset(0);
 }
 
@@ -476,29 +535,83 @@ 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";
+char   *greeting       = "Mail version %s.  Type ? for help.\n";
 
 announce(pr)
 {
 
 announce(pr)
 {
-       int vec[2];
+       int vec[2], mdot;
        extern char *version;
        extern char *version;
-       register struct message *mp;
 
 
-       if (value("hold") != NOSTR)
-               for (mp = &message[0]; mp < &message[msgCount]; mp++)
-                       mp->m_flag |= MPRESERVE;
-       vec[0] = 1;
-       vec[1] = 0;
        if (pr && value("quiet") == NOSTR)
                printf(greeting, version);
        if (pr && value("quiet") == NOSTR)
                printf(greeting, version);
+       mdot = newfileinfo();
+       vec[0] = mdot;
+       vec[1] = 0;
+       dot = &message[mdot - 1];
+       if (msgCount > 0 && !noheader) {
+               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 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)
+                       break;
+       if (mp >= &message[msgCount])
+               for (mp = &message[0]; mp < &message[msgCount]; mp++)
+                       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)
                printf("1 message");
        else
                printf("%d messages", msgCount);
        if (msgCount == 1)
                printf("1 message");
        else
                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");
        if (readonly)
                printf(" [Read only]");
        printf("\n");
-       headers(vec);
+       return(mdot);
 }
 
 strace() {}
 }
 
 strace() {}
@@ -509,6 +622,27 @@ strace() {}
 
 pversion(e)
 {
 
 pversion(e)
 {
-       printf(greeting, 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);
+}