BSD 4_4 release
[unix-history] / usr / src / usr.bin / mail / lex.c
index c56951d..3428203 100644 (file)
@@ -1,27 +1,44 @@
 /*
 /*
- * Copyright (c) 1980 Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1980, 1993
+ *     The 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.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. 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 BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)lex.c      5.17 (Berkeley) %G%";
+static char sccsid[] = "@(#)lex.c      8.1 (Berkeley) 6/6/93";
 #endif /* not lint */
 
 #include "rcv.h"
 #endif /* not lint */
 
 #include "rcv.h"
-#include <sys/stat.h>
 #include <errno.h>
 #include <errno.h>
+#include <fcntl.h>
+#include "extern.h"
 
 /*
  * Mail -- a mail program
 
 /*
  * Mail -- a mail program
@@ -33,35 +50,42 @@ 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)
+int
+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;
        extern char tempMesg[];
        extern int errno;
 
        static int shudclob;
        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);
        }
 
        if (fstat(fileno(ibuf), &stb) < 0) {
                perror("fstat");
                perror(name);
                return(-1);
        }
 
        if (fstat(fileno(ibuf), &stb) < 0) {
                perror("fstat");
-               fclose(ibuf);
+               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;
                perror(name);
                return (-1);
                errno = EISDIR;
                perror(name);
                return (-1);
@@ -70,7 +94,7 @@ setfile(name, isedit)
                break;
 
        default:
                break;
 
        default:
-               fclose(ibuf);
+               Fclose(ibuf);
                errno = EINVAL;
                perror(name);
                return (-1);
                errno = EINVAL;
                perror(name);
                return (-1);
@@ -103,6 +127,7 @@ setfile(name, isedit)
        }
        shudclob = 1;
        edit = isedit;
        }
        shudclob = 1;
        edit = isedit;
+       strcpy(prevfile, mailname);
        if (name != mailname)
                strcpy(mailname, name);
        mailsize = fsize(ibuf);
        if (name != mailname)
                strcpy(mailname, name);
        mailsize = fsize(ibuf);
@@ -110,16 +135,23 @@ setfile(name, isedit)
                perror(tempMesg);
                exit(1);
        }
                perror(tempMesg);
                exit(1);
        }
+       (void) fcntl(fileno(otf), F_SETFD, 1);
        if ((itf = fopen(tempMesg, "r")) == NULL) {
                perror(tempMesg);
                exit(1);
        }
        if ((itf = fopen(tempMesg, "r")) == NULL) {
                perror(tempMesg);
                exit(1);
        }
-       remove(tempMesg);
+       (void) fcntl(fileno(itf), F_SETFD, 1);
+       rm(tempMesg);
        setptr(ibuf);
        setmsize(msgCount);
        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);
 }
 
@@ -130,12 +162,13 @@ 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.
  */
+void
 commands()
 {
        int eofloop = 0;
        register int n;
        char linebuf[LINESIZE];
 commands()
 {
        int eofloop = 0;
        register int n;
        char linebuf[LINESIZE];
-       int intr(), stop(), hangup(), contin();
+       void intr(), stop(), hangup();
 
        if (!sourcing) {
                if (signal(SIGINT, SIG_IGN) != SIG_IGN)
 
        if (!sourcing) {
                if (signal(SIGINT, SIG_IGN) != SIG_IGN)
@@ -206,8 +239,10 @@ commands()
  * the interactive command loop.
  * Contxt is non-zero if called while composing mail.
  */
  * the interactive command loop.
  * Contxt is non-zero if called while composing mail.
  */
+int
 execute(linebuf, contxt)
        char linebuf[];
 execute(linebuf, contxt)
        char linebuf[];
+       int contxt;
 {
        char word[LINESIZE];
        char *arglist[MAXARGC];
 {
        char word[LINESIZE];
        char *arglist[MAXARGC];
@@ -401,12 +436,13 @@ out:
  * 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.
  */
+void
 setmsize(sz)
 setmsize(sz)
+       int sz;
 {
 
        if (msgvec != 0)
 {
 
        if (msgvec != 0)
-               cfree((char *) msgvec);
+               free((char *) msgvec);
        msgvec = (int *) calloc((unsigned) (sz + 1), sizeof *msgvec);
 }
 
        msgvec = (int *) calloc((unsigned) (sz + 1), sizeof *msgvec);
 }
 
@@ -432,7 +468,7 @@ lex(word)
  * Determine if as1 is a valid prefix of as2.
  * Return true if yep.
  */
  * Determine if as1 is a valid prefix of as2.
  * Return true if yep.
  */
-
+int
 isprefix(as1, as2)
        char *as1, *as2;
 {
 isprefix(as1, as2)
        char *as1, *as2;
 {
@@ -456,36 +492,10 @@ 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*/
+void
 intr(s)
 intr(s)
+       int s;
 {
 
        noreset = 0;
 {
 
        noreset = 0;
@@ -495,10 +505,7 @@ intr(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);
@@ -511,9 +518,11 @@ intr(s)
 /*
  * When we wake up after ^Z, reprint the prompt.
  */
 /*
  * When we wake up after ^Z, reprint the prompt.
  */
+void
 stop(s)
 stop(s)
+       int s;
 {
 {
-       int (*old_action)() = signal(s, SIG_DFL);
+       sig_t old_action = signal(s, SIG_DFL);
 
        sigsetmask(sigblock(0) & ~sigmask(s));
        kill(0, s);
 
        sigsetmask(sigblock(0) & ~sigmask(s));
        kill(0, s);
@@ -529,7 +538,9 @@ stop(s)
  * Branch here on hangup signal and simulate "exit".
  */
 /*ARGSUSED*/
  * Branch here on hangup signal and simulate "exit".
  */
 /*ARGSUSED*/
+void
 hangup(s)
 hangup(s)
+       int s;
 {
 
        /* nothing to do? */
 {
 
        /* nothing to do? */
@@ -540,7 +551,7 @@ hangup(s)
  * 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.
  */
-
+void
 announce()
 {
        int vec[2], mdot;
 announce()
 {
        int vec[2], mdot;
@@ -560,6 +571,7 @@ announce()
  * 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.
  */
+int
 newfileinfo()
 {
        register struct message *mp;
 newfileinfo()
 {
        register struct message *mp;
@@ -620,7 +632,9 @@ newfileinfo()
  */
 
 /*ARGSUSED*/
  */
 
 /*ARGSUSED*/
+int
 pversion(e)
 pversion(e)
+       int e;
 {
        extern char *version;
 
 {
        extern char *version;
 
@@ -631,12 +645,13 @@ pversion(e)
 /*
  * Load a file of user definitions.
  */
 /*
  * Load a file of user definitions.
  */
+void
 load(name)
        char *name;
 {
        register FILE *in, *oldin;
 
 load(name)
        char *name;
 {
        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;
@@ -646,5 +661,5 @@ load(name)
        loading = 0;
        sourcing = 0;
        input = oldin;
        loading = 0;
        sourcing = 0;
        input = oldin;
-       fclose(in);
+       Fclose(in);
 }
 }