date and time created 91/03/14 12:01:54 by donn
[unix-history] / usr / src / bin / csh / csh.c
index 26ce663..787d169 100644 (file)
@@ -1,15 +1,18 @@
 /*
  * Copyright (c) 1980 Regents of the University of California.
 /*
  * Copyright (c) 1980 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
+ * All rights reserved.  The Berkeley Software License Agreement
  * specifies the terms and conditions for redistribution.
  */
 
 #ifndef lint
  * specifies the terms and conditions for redistribution.
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)csh.c      5.1 (Berkeley) %G%";
-#endif not lint
+static char *sccsid = "@(#)csh.c       5.17 (Berkeley) %G%";
+#endif
 
 #include "sh.h"
 #include <sys/ioctl.h>
 
 #include "sh.h"
 #include <sys/ioctl.h>
+#include <sys/file.h>
+#include "pathnames.h"
+
 /*
  * C Shell
  *
 /*
  * C Shell
  *
@@ -20,11 +23,12 @@ static char sccsid[] = "@(#)csh.c   5.1 (Berkeley) %G%";
  * April 1980
  */
 
  * April 1980
  */
 
-char   *pathlist[] =   { ".", "/usr/ucb", "/bin", "/usr/bin", 0 };
+char   *pathlist[] =   { ".", _PATH_BIN, _PATH_USRBIN, 0 };
 char   *dumphist[] =   { "history", "-h", 0, 0 };
 char   *loadhist[] =   { "source", "-h", "~/.history", 0 };
 char   HIST = '!';
 char   HISTSUB = '^';
 char   *dumphist[] =   { "history", "-h", 0, 0 };
 char   *loadhist[] =   { "source", "-h", "~/.history", 0 };
 char   HIST = '!';
 char   HISTSUB = '^';
+bool   mflag;
 bool   nofile;
 bool   reenter;
 bool   nverbose;
 bool   nofile;
 bool   reenter;
 bool   nverbose;
@@ -35,20 +39,25 @@ bool        batch;
 bool   prompt = 1;
 bool   enterhist = 0;
 
 bool   prompt = 1;
 bool   enterhist = 0;
 
-main(c, av)
-       int c;
-       char **av;
+extern gid_t getegid(), getgid();
+extern uid_t geteuid(), getuid();
+
+void pintr(), pchild(), srcunit();
+
+main(argc, argv)
+       int argc;
+       char **argv;
 {
        register char **v, *cp;
        register int f;
        struct sigvec osv;
 
        settimes();                     /* Immed. estab. timing base */
 {
        register char **v, *cp;
        register int f;
        struct sigvec osv;
 
        settimes();                     /* Immed. estab. timing base */
-       v = av;
+       v = argv;
        if (eq(v[0], "a.out"))          /* A.out's are quittable */
                quitit = 1;
        uid = getuid();
        if (eq(v[0], "a.out"))          /* A.out's are quittable */
                quitit = 1;
        uid = getuid();
-       loginsh = **v == '-' && c == 1;
+       loginsh = **v == '-' && argc == 1;
        if (loginsh)
                (void) time(&chktim);
 
        if (loginsh)
                (void) time(&chktim);
 
@@ -88,7 +97,7 @@ main(c, av)
                set1("path", saveblk(pathlist), &shvhed);
        else
                importpath(cp);
                set1("path", saveblk(pathlist), &shvhed);
        else
                importpath(cp);
-       set("shell", SHELLPATH);
+       set("shell", _PATH_CSHELL);
 
        doldol = putn(getpid());                /* For $$ */
        shtemp = strspl("/tmp/sh", doldol);     /* For << */
 
        doldol = putn(getpid());                /* For $$ */
        shtemp = strspl("/tmp/sh", doldol);     /* For << */
@@ -107,6 +116,8 @@ main(c, av)
        (void) sigvec(SIGTERM, (struct sigvec *)0, &osv);
        parterm = osv.sv_handler;
        if (loginsh) {
        (void) sigvec(SIGTERM, (struct sigvec *)0, &osv);
        parterm = osv.sv_handler;
        if (loginsh) {
+               sig_t phup;
+
                (void) signal(SIGHUP, phup);    /* exit processing on HUP */
                (void) signal(SIGXCPU, phup);   /* ...and on XCPU */
                (void) signal(SIGXFSZ, phup);   /* ...and on XFSZ */
                (void) signal(SIGHUP, phup);    /* exit processing on HUP */
                (void) signal(SIGXCPU, phup);   /* ...and on XCPU */
                (void) signal(SIGXFSZ, phup);   /* ...and on XFSZ */
@@ -118,8 +129,8 @@ main(c, av)
         * Note that processing of -v/-x is actually delayed till after
         * script processing.
         */
         * Note that processing of -v/-x is actually delayed till after
         * script processing.
         */
-       c--, v++;
-       while (c > 0 && (cp = v[0])[0] == '-' && *++cp != '\0' && !batch) {
+       argc--, v++;
+       while (argc > 0 && (cp = v[0])[0] == '-' && *++cp != '\0' && !batch) {
                do switch (*cp++) {
 
                case 'b':               /* -b   Next arg is input file */
                do switch (*cp++) {
 
                case 'b':               /* -b   Next arg is input file */
@@ -127,9 +138,9 @@ main(c, av)
                        break;
 
                case 'c':               /* -c   Command input from arg */
                        break;
 
                case 'c':               /* -c   Command input from arg */
-                       if (c == 1)
+                       if (argc == 1)
                                exit(0);
                                exit(0);
-                       c--, v++;
+                       argc--, v++;
                        arginp = v[0];
                        prompt = 0;
                        nofile++;
                        arginp = v[0];
                        prompt = 0;
                        nofile++;
@@ -148,6 +159,10 @@ main(c, av)
                        nofile++;
                        break;
 
                        nofile++;
                        break;
 
+               case 'm':               /* -m   read .cshrc (from su) */
+                       mflag++;
+                       break;
+
                case 'n':               /* -n   Don't execute */
                        noexec++;
                        break;
                case 'n':               /* -n   Don't execute */
                        noexec++;
                        break;
@@ -183,7 +198,7 @@ main(c, av)
                        break;
 
                } while (*cp);
                        break;
 
                } while (*cp);
-               v++, c--;
+               v++, argc--;
        }
 
        if (quitit)                     /* With all due haste, for debugging */
        }
 
        if (quitit)                     /* With all due haste, for debugging */
@@ -194,7 +209,7 @@ main(c, av)
         * are remaining arguments the first of them is the name
         * of a shell file from which to read commands.
         */
         * are remaining arguments the first of them is the name
         * of a shell file from which to read commands.
         */
-       if (nofile == 0 && c > 0) {
+       if (nofile == 0 && argc > 0) {
                nofile = open(v[0], 0);
                if (nofile < 0) {
                        child++;                /* So this ... */
                nofile = open(v[0], 0);
                if (nofile < 0) {
                        child++;                /* So this ... */
@@ -204,7 +219,7 @@ main(c, av)
                SHIN = dmove(nofile, FSHIN);    /* Replace FSHIN */
                (void) ioctl(SHIN, FIOCLEX, (char *)0);
                prompt = 0;
                SHIN = dmove(nofile, FSHIN);    /* Replace FSHIN */
                (void) ioctl(SHIN, FIOCLEX, (char *)0);
                prompt = 0;
-               c--, v++;
+               argc--, v++;
        }
        if (!batch && (uid != geteuid() || getgid() != getegid())) {
                errno = EACCES;
        }
        if (!batch && (uid != geteuid() || getgid() != getegid())) {
                errno = EACCES;
@@ -245,9 +260,8 @@ main(c, av)
         */
        shpgrp = getpgrp(0);
        opgrp = tpgrp = -1;
         */
        shpgrp = getpgrp(0);
        opgrp = tpgrp = -1;
-       oldisc = -1;
        if (setintr) {
        if (setintr) {
-               **av = '-';
+               **argv = '-';
                if (!quitit)            /* Wary! */
                        (void) signal(SIGQUIT, SIG_IGN);
                (void) signal(SIGINT, pintr);
                if (!quitit)            /* Wary! */
                        (void) signal(SIGQUIT, SIG_IGN);
                (void) signal(SIGINT, pintr);
@@ -276,27 +290,16 @@ retry:
                            tpgrp != -1) {
                                int ldisc;
                                if (tpgrp != shpgrp) {
                            tpgrp != -1) {
                                int ldisc;
                                if (tpgrp != shpgrp) {
-                                       int (*old)() = signal(SIGTTIN, SIG_DFL);
+                                       sig_t old = signal(SIGTTIN, SIG_DFL);
                                        (void) kill(0, SIGTTIN);
                                        (void) signal(SIGTTIN, old);
                                        goto retry;
                                }
                                        (void) kill(0, SIGTTIN);
                                        (void) signal(SIGTTIN, old);
                                        goto retry;
                                }
-                               if (ioctl(f, TIOCGETD, (char *)&oldisc) != 0) 
-                                       goto notty;
-                               if (oldisc != NTTYDISC) {
-#ifdef DEBUG
-                                       printf("Switching to new tty driver...\n");
-#endif DEBUG
-                                       ldisc = NTTYDISC;
-                                       (void) ioctl(f, TIOCSETD,
-                                               (char *)&ldisc);
-                               } else
-                                       oldisc = -1;
                                opgrp = shpgrp;
                                shpgrp = getpid();
                                tpgrp = shpgrp;
                                opgrp = shpgrp;
                                shpgrp = getpid();
                                tpgrp = shpgrp;
-                               (void) ioctl(f, TIOCSPGRP, (char *)&shpgrp);
                                (void) setpgrp(0, shpgrp);
                                (void) setpgrp(0, shpgrp);
+                               (void) ioctl(f, TIOCSPGRP, (char *)&shpgrp);
                                (void) ioctl(dcopy(f, FSHTTY), FIOCLEX,
                                        (char *)0);
                        } else {
                                (void) ioctl(dcopy(f, FSHTTY), FIOCLEX,
                                        (char *)0);
                        } else {
@@ -318,6 +321,19 @@ notty:
        haderr = 0;             /* In case second time through */
        if (!fast && reenter == 0) {
                reenter++;
        haderr = 0;             /* In case second time through */
        if (!fast && reenter == 0) {
                reenter++;
+               {
+               int osetintr, omask;
+                       osetintr = setintr;
+                       omask = sigblock(sigmask(SIGINT));
+                       setintr = 0;
+                       srcunit(open(_PATH_DOTCSHRC, O_RDONLY), 0, 0);
+                       if (!fast && !arginp && !onelflg)
+                               dohash();
+                       if (loginsh)
+                               srcunit(open(_PATH_DOTLOGIN, O_RDONLY), 0, 0);
+                       (void)sigsetmask(omask);
+                       setintr = osetintr;
+               }
                /* Will have value("home") here because set fast if don't */
                srccat(value("home"), "/.cshrc");
                if (!fast && !arginp && !onelflg && !havhash)
                /* Will have value("home") here because set fast if don't */
                srccat(value("home"), "/.cshrc");
                if (!fast && !arginp && !onelflg && !havhash)
@@ -363,12 +379,6 @@ untty()
        if (tpgrp > 0) {
                (void) setpgrp(0, opgrp);
                (void) ioctl(FSHTTY, TIOCSPGRP, (char *)&opgrp);
        if (tpgrp > 0) {
                (void) setpgrp(0, opgrp);
                (void) ioctl(FSHTTY, TIOCSPGRP, (char *)&opgrp);
-               if (oldisc != -1 && oldisc != NTTYDISC) {
-#ifdef DEBUG
-                       printf("\nReverting to old tty driver...\n");
-#endif DEBUG
-                       (void) ioctl(FSHTTY, TIOCSETD, (char *)&oldisc);
-               }
        }
 }
 
        }
 }
 
@@ -420,21 +430,18 @@ srccat(cp, dp)
 
        (void) ioctl(unit, FIOCLEX, (char *)0);
        xfree(ep);
 
        (void) ioctl(unit, FIOCLEX, (char *)0);
        xfree(ep);
-#ifdef INGRES
-       srcunit(unit, 0, 0);
-#else
-       srcunit(unit, 1, 0);
-#endif
+       srcunit(unit, mflag ? 0 : 1, 0);
 }
 
 /*
 }
 
 /*
- * Source to a unit.  If onlyown it must be our file or our group or
- * we don't chance it. This occurs on ".cshrc"s and the like.
+ * Source to a unit.
+ * This occurs on ".cshrc"s and the like.
  */
  */
+int    insource;
+static void
 srcunit(unit, onlyown, hflg)
        register int unit;
 srcunit(unit, onlyown, hflg)
        register int unit;
-       bool onlyown;
-       bool hflg;
+       bool onlyown, hflg;
 {
        /* We have to push down a lot of state here */
        /* All this could go into a structure */
 {
        /* We have to push down a lot of state here */
        /* All this could go into a structure */
@@ -452,7 +459,8 @@ srcunit(unit, onlyown, hflg)
 
        /* The (few) real local variables */
        jmp_buf oldexit;
 
        /* The (few) real local variables */
        jmp_buf oldexit;
-       int reenter, omask;
+       int reenter;
+       long omask;
 
        if (unit < 0)
                return;
 
        if (unit < 0)
                return;
@@ -479,6 +487,7 @@ srcunit(unit, onlyown, hflg)
         * once.  This is less efficient globally on many variable references
         * however.
         */
         * once.  This is less efficient globally on many variable references
         * however.
         */
+       insource = 1;
        getexit(oldexit);
        reenter = 0;
        if (setintr)
        getexit(oldexit);
        reenter = 0;
        if (setintr)
@@ -540,6 +549,7 @@ srcunit(unit, onlyown, hflg)
         */
        if (reenter >= 2)
                error(NOSTR);
         */
        if (reenter >= 2)
                error(NOSTR);
+       insource = 0;
 }
 
 rechist()
 }
 
 rechist()
@@ -575,6 +585,7 @@ goodbye()
                (void) signal(SIGINT, SIG_IGN);
                (void) signal(SIGTERM, SIG_IGN);
                setintr = 0;            /* No interrupts after "logout" */
                (void) signal(SIGINT, SIG_IGN);
                (void) signal(SIGTERM, SIG_IGN);
                setintr = 0;            /* No interrupts after "logout" */
+               srcunit(open(_PATH_DOTLOGOUT, O_RDONLY), 0, 0);
                if (adrof("home"))
                        srccat(value("home"), "/.logout");
        }
                if (adrof("home"))
                        srccat(value("home"), "/.logout");
        }
@@ -614,6 +625,7 @@ char        *jobargv[2] = { "jobs", 0 };
  * and finally go through the normal error mechanism, which
  * gets a chance to make the shell go away.
  */
  * and finally go through the normal error mechanism, which
  * gets a chance to make the shell go away.
  */
+void
 pintr()
 {
        pintr1(1);
 pintr()
 {
        pintr1(1);
@@ -623,9 +635,9 @@ pintr1(wantnl)
        bool wantnl;
 {
        register char **v;
        bool wantnl;
 {
        register char **v;
-       int omask;
+       long omask;
 
 
-       omask = sigblock(0);
+       omask = sigblock(0L);
        if (setintr) {
                (void) sigsetmask(omask & ~sigmask(SIGINT));
                if (pjobs) {
        if (setintr) {
                (void) sigsetmask(omask & ~sigmask(SIGINT));
                if (pjobs) {
@@ -689,7 +701,7 @@ process(catch)
                 * Interruptible during interactive reads
                 */
                if (setintr)
                 * Interruptible during interactive reads
                 */
                if (setintr)
-                       (void) sigsetmask(sigblock(0) & ~sigmask(SIGINT));
+                       (void) sigsetmask(sigblock(0L) & ~sigmask(SIGINT));
 
                /*
                 * For the sake of reset()
 
                /*
                 * For the sake of reset()
@@ -923,7 +935,7 @@ printprompt()
                        else {
                                if (*cp == '\\' && cp[1] == HIST)
                                        cp++;
                        else {
                                if (*cp == '\\' && cp[1] == HIST)
                                        cp++;
-                               putchar(*cp | QUOTE);
+                               cshputchar(*cp | QUOTE);
                        }
        } else
                /* 
                        }
        } else
                /*