added "more" command
[unix-history] / usr / src / usr.bin / mail / lex.c
index 088c8dd..6564095 100644 (file)
@@ -1,6 +1,15 @@
-#
+/*
+ * Copyright (c) 1980 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
+#ifndef lint
+static char sccsid[] = "@(#)lex.c      5.1 (Berkeley) %G%";
+#endif not lint
 
 #include "rcv.h"
 
 #include "rcv.h"
+#include <sys/stat.h>
 
 /*
  * Mail -- a mail program
 
 /*
  * Mail -- a mail program
@@ -8,8 +17,6 @@
  * Lexical processing of commands.
  */
 
  * Lexical processing of commands.
  */
 
-static char *SccsId = "@(#)lex.c       2.6 %G%";
-
 char   *prompt = "& ";
 
 /*
 char   *prompt = "& ";
 
 /*
@@ -24,16 +31,20 @@ setfile(name, isedit)
 {
        FILE *ibuf;
        int i;
 {
        FILE *ibuf;
        int i;
+       struct stat stb;
        static int shudclob;
        static char efile[128];
        extern char tempMesg[];
 
        static int shudclob;
        static char efile[128];
        extern char tempMesg[];
 
-       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);
+       if (!edit) {
+               if (fstat(fileno(ibuf), &stb) < 0) {
+                       perror(name);
+                       exit(1);
+               }
+               if (stb.st_size == 0)
+                       return(-1);
        }
 
        /*
        }
 
        /*
@@ -103,7 +114,9 @@ commands()
        char linebuf[LINESIZE];
        int hangup(), contin();
 
        char linebuf[LINESIZE];
        int hangup(), contin();
 
+# ifdef VMUNIX
        sigset(SIGCONT, SIG_DFL);
        sigset(SIGCONT, SIG_DFL);
+# endif VMUNIX
        if (rcvmode && !sourcing) {
                if (sigset(SIGINT, SIG_IGN) != SIG_IGN)
                        sigset(SIGINT, stop);
        if (rcvmode && !sourcing) {
                if (sigset(SIGINT, SIG_IGN) != SIG_IGN)
                        sigset(SIGINT, stop);
@@ -124,10 +137,13 @@ commands()
                eofloop = 0;
 top:
                if (shudprompt) {
                eofloop = 0;
 top:
                if (shudprompt) {
-                       sigset(SIGCONT, contin);
                        printf(prompt);
                        printf(prompt);
-               }
-               flush();
+                       fflush(stdout);
+# ifdef VMUNIX
+                       sigset(SIGCONT, contin);
+# endif VMUNIX
+               } else
+                       fflush(stdout);
                sreset();
 
                /*
                sreset();
 
                /*
@@ -163,7 +179,9 @@ top:
                                break;
                        linebuf[n++] = ' ';
                }
                                break;
                        linebuf[n++] = ' ';
                }
+# ifdef VMUNIX
                sigset(SIGCONT, SIG_DFL);
                sigset(SIGCONT, SIG_DFL);
+# endif VMUNIX
                if (execute(linebuf, 0))
                        return;
 more:          ;
                if (execute(linebuf, 0))
                        return;
 more:          ;
@@ -487,10 +505,41 @@ 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;
+       }
+}
+
 stop(s)
 {
        register FILE *fp;
 
 stop(s)
 {
        register FILE *fp;
 
+# ifndef VMUNIX
+       s = SIGINT;
+# endif VMUNIX
        noreset = 0;
        if (!inithdr)
                sawcom++;
        noreset = 0;
        if (!inithdr)
                sawcom++;
@@ -498,27 +547,20 @@ stop(s)
        while (sourcing)
                unstack();
        getuserid((char *) -1);
        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");
+# ifndef VMUNIX
+       signal(s, stop);
+# endif
        reset(0);
 }
 
        reset(0);
 }
 
@@ -534,11 +576,11 @@ announce(pr)
        int vec[2], mdot;
        extern char *version;
 
        int vec[2], mdot;
        extern char *version;
 
+       if (pr && value("quiet") == NOSTR)
+               printf(greeting, 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];
        if (msgCount > 0 && !noheader) {
                inithdr++;
        dot = &message[mdot - 1];
        if (msgCount > 0 && !noheader) {
                inithdr++;
@@ -551,11 +593,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)
@@ -568,13 +610,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++;
+       }
+       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\": ", mailname);
+       printf("\"%s\": ", ename);
        if (msgCount == 1)
                printf("1 message");
        else
        if (msgCount == 1)
                printf("1 message");
        else
@@ -583,6 +638,10 @@ 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");
        if (readonly)
                printf(" [Read only]");
        printf("\n");