add ``history -h >file'' and ``source -h file'' to
authorKirk McKusick <mckusic@ucbvax.Berkeley.EDU>
Fri, 20 Nov 1981 13:04:04 +0000 (05:04 -0800)
committerKirk McKusick <mckusic@ucbvax.Berkeley.EDU>
Fri, 20 Nov 1981 13:04:04 +0000 (05:04 -0800)
implement history save and restore.

SCCS-vsn: bin/csh/csh.c 4.4
SCCS-vsn: bin/csh/csh.h 4.2
SCCS-vsn: bin/csh/hist.c 4.3

usr/src/bin/csh/csh.c
usr/src/bin/csh/csh.h
usr/src/bin/csh/hist.c

index 5552b12..b6a55da 100644 (file)
@@ -13,7 +13,8 @@ static        char *sccsid = "@(#)csh.c 4.4 %G%";
  */
 
 char   *pathlist[] =   { ".", "/usr/ucb", "/bin", "/usr/bin", 0 };
  */
 
 char   *pathlist[] =   { ".", "/usr/ucb", "/bin", "/usr/bin", 0 };
-char   *dumphist[] =   { "history", "-c", "+1000" };
+char   *dumphist[] =   { "history", "-h", 0 };
+char   *loadhist[] =   { "source", "-h", "~/.history", 0 };
 char   HIST = '!';
 char   HISTSUB = '^';
 bool   nofile;
 char   HIST = '!';
 char   HISTSUB = '^';
 bool   nofile;
@@ -113,6 +114,8 @@ main(c, av)
        sigset(SIGINT, parintr);                        /* ... restore */
        parterm = signal(SIGTERM, SIG_IGN);     /* parents terminability */
        signal(SIGTERM, parterm);                       /* ... restore */
        sigset(SIGINT, parintr);                        /* ... restore */
        parterm = signal(SIGTERM, SIG_IGN);     /* parents terminability */
        signal(SIGTERM, parterm);                       /* ... restore */
+       if (loginsh)
+               signal(SIGHUP, phup);           /* exit processing on HUP */
 
        /*
         * Process the arguments.
 
        /*
         * Process the arguments.
@@ -285,7 +288,9 @@ retry:
                                if (ioctl(f, TIOCGETD, &oldisc) != 0) 
                                        goto notty;
                                if (oldisc != NTTYDISC) {
                                if (ioctl(f, TIOCGETD, &oldisc) != 0) 
                                        goto notty;
                                if (oldisc != NTTYDISC) {
-                       printf("Switching to new tty driver...\n");
+#ifdef DEBUG
+                                       printf("Switching to new tty driver...\n");
+#endif DEBUG
                                        ldisc = NTTYDISC;
                                        ioctl(f, TIOCSETD, &ldisc);
                                } else
                                        ldisc = NTTYDISC;
                                        ioctl(f, TIOCSETD, &ldisc);
                                } else
@@ -318,11 +323,9 @@ notty:
                srccat(value("home"), "/.cshrc");
                if (!fast && !arginp && !onelflg)
                        dohash();
                srccat(value("home"), "/.cshrc");
                if (!fast && !arginp && !onelflg)
                        dohash();
+               dosource(loadhist);
                if (loginsh) {
                        srccat(value("home"), "/.login");
                if (loginsh) {
                        srccat(value("home"), "/.login");
-                       enterhist = 1;
-                       srccat(value("home"), "/.history");
-                       enterhist = 0;
                }
        }
 
                }
        }
 
@@ -351,6 +354,7 @@ notty:
                child++;
                goodbye();
        }
                child++;
                goodbye();
        }
+       rechist();
        exitstat();
 }
 
        exitstat();
 }
 
@@ -361,7 +365,9 @@ untty()
                setpgrp(0, opgrp);
                ioctl(FSHTTY, TIOCSPGRP, &opgrp);
                if (oldisc != -1 && oldisc != NTTYDISC) {
                setpgrp(0, opgrp);
                ioctl(FSHTTY, TIOCSPGRP, &opgrp);
                if (oldisc != -1 && oldisc != NTTYDISC) {
+#ifdef DEBUG
                        printf("\nReverting to old tty driver...\n");
                        printf("\nReverting to old tty driver...\n");
+#endif DEBUG
                        ioctl(FSHTTY, TIOCSETD, &oldisc);
                }
        }
                        ioctl(FSHTTY, TIOCSETD, &oldisc);
                }
        }
@@ -416,9 +422,9 @@ srccat(cp, dp)
        /* ioctl(unit, FIOCLEX, NULL); */
        xfree(ep);
 #ifdef INGRES
        /* ioctl(unit, FIOCLEX, NULL); */
        xfree(ep);
 #ifdef INGRES
-       srcunit(unit, 0);
+       srcunit(unit, 0, 0);
 #else
 #else
-       srcunit(unit, 1);
+       srcunit(unit, 1, 0);
 #endif
 }
 
 #endif
 }
 
@@ -426,9 +432,10 @@ srccat(cp, dp)
  * 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.  If onlyown it must be our file or our group or
  * we don't chance it. This occurs on ".cshrc"s and the like.
  */
-srcunit(unit, onlyown)
+srcunit(unit, onlyown, hflg)
        register int unit;
        bool onlyown;
        register int unit;
        bool onlyown;
+       bool 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 */
@@ -437,6 +444,8 @@ srcunit(unit, onlyown)
        char *ogointr = gointr, *oarginp = arginp;
        char *oevalp = evalp, **oevalvec = evalvec;
        int oonelflg = onelflg;
        char *ogointr = gointr, *oarginp = arginp;
        char *oevalp = evalp, **oevalvec = evalvec;
        int oonelflg = onelflg;
+       bool oenterhist = enterhist;
+       char OHIST = HIST;
 #ifdef TELL
        bool otell = cantell;
 #endif
 #ifdef TELL
        bool otell = cantell;
 #endif
@@ -484,6 +493,9 @@ srcunit(unit, onlyown)
                oSHIN = SHIN, SHIN = unit, arginp = 0, onelflg = 0;
                intty = isatty(SHIN), whyles = 0, gointr = 0;
                evalvec = 0; evalp = 0;
                oSHIN = SHIN, SHIN = unit, arginp = 0, onelflg = 0;
                intty = isatty(SHIN), whyles = 0, gointr = 0;
                evalvec = 0; evalp = 0;
+               enterhist = hflg;
+               if (enterhist)
+                       HIST = '\0';
                /*
                 * Now if we are allowing commands to be interrupted,
                 * we let ourselves be interrupted.
                /*
                 * Now if we are allowing commands to be interrupted,
                 * we let ourselves be interrupted.
@@ -513,6 +525,8 @@ srcunit(unit, onlyown)
                arginp = oarginp, onelflg = oonelflg;
                evalp = oevalp, evalvec = oevalvec;
                intty = oldintty, whyles = oldwhyl, gointr = ogointr;
                arginp = oarginp, onelflg = oonelflg;
                evalp = oevalp, evalvec = oevalvec;
                intty = oldintty, whyles = oldwhyl, gointr = ogointr;
+               enterhist = oenterhist;
+               HIST = OHIST;
 #ifdef TELL
                cantell = otell;
 #endif
 #ifdef TELL
                cantell = otell;
 #endif
@@ -527,12 +541,12 @@ srcunit(unit, onlyown)
                error(NOSTR);
 }
 
                error(NOSTR);
 }
 
-goodbye()
+rechist()
 {
        char buf[BUFSIZ];
        int fp, ftmp, oldidfds;
 
 {
        char buf[BUFSIZ];
        int fp, ftmp, oldidfds;
 
-       if (loginsh) {
+       if (!fast) {
                strcpy(buf, value("home"));
                strcat(buf, "/.history");
                fp = creat(buf, 0777);
                strcpy(buf, value("home"));
                strcat(buf, "/.history");
                fp = creat(buf, 0777);
@@ -546,6 +560,12 @@ goodbye()
                        SHOUT = ftmp;
                        didfds = oldidfds;
                }
                        SHOUT = ftmp;
                        didfds = oldidfds;
                }
+       }
+}
+
+goodbye()
+{
+       if (loginsh) {
                signal(SIGQUIT, SIG_IGN);
                sigset(SIGINT, SIG_IGN);
                signal(SIGTERM, SIG_IGN);
                signal(SIGQUIT, SIG_IGN);
                sigset(SIGINT, SIG_IGN);
                signal(SIGTERM, SIG_IGN);
@@ -553,6 +573,7 @@ goodbye()
                if (adrof("home"))
                        srccat(value("home"), "/.logout");
        }
                if (adrof("home"))
                        srccat(value("home"), "/.logout");
        }
+       rechist();
        exitstat();
 }
 
        exitstat();
 }
 
@@ -568,6 +589,15 @@ exitstat()
        exit(getn(value("status")));
 }
 
        exit(getn(value("status")));
 }
 
+/*
+ * in the event of a HUP we want to save the history
+ */
+phup()
+{
+       rechist();
+       exit(1);
+}
+
 char   *jobargv[2] = { "jobs", 0 };
 /*
  * Catch an interrupt, e.g. during lexical input.
 char   *jobargv[2] = { "jobs", 0 };
 /*
  * Catch an interrupt, e.g. during lexical input.
@@ -719,7 +749,8 @@ process(catch)
                 * Echo not only on VERBOSE, but also with history expansion.
                 * If there is a lexical error then we forego history echo.
                 */
                 * Echo not only on VERBOSE, but also with history expansion.
                 * If there is a lexical error then we forego history echo.
                 */
-               if (lex(&paraml) && !err && intty || adrof("verbose")) {
+               if (lex(&paraml) && !err && intty ||
+                   adrof("verbose")) {
                        haderr = 1;
                        prlex(&paraml);
                        haderr = 0;
                        haderr = 1;
                        prlex(&paraml);
                        haderr = 0;
@@ -741,9 +772,10 @@ process(catch)
                        savehist(&paraml);
 
                /*
                        savehist(&paraml);
 
                /*
-                * Print lexical error messages.
+                * Print lexical error messages, except when sourcing
+                * history lists.
                 */
                 */
-               if (err)
+               if (!enterhist && err)
                        error(err);
 
                /*
                        error(err);
 
                /*
@@ -780,14 +812,21 @@ dosource(t)
 {
        register char *f;
        register int u;
 {
        register char *f;
        register int u;
+       bool hflg = 0;
+       char buf[BUFSIZ];
 
        t++;
 
        t++;
-       f = globone(*t);
+       if (*t && eq(*t, "-h")) {
+               t++;
+               hflg++;
+       }
+       strcpy(buf, *t);
+       f = globone(buf);
        u = dmove(open(f, 0), -1);
        xfree(f);
        u = dmove(open(f, 0), -1);
        xfree(f);
-       if (u < 0)
+       if (u < 0 && !hflg)
                Perror(f);
                Perror(f);
-       srcunit(u, 0);
+       srcunit(u, 0, hflg);
 }
 
 /*
 }
 
 /*
index feb4e06..49d8cef 100644 (file)
@@ -1,4 +1,4 @@
-/* csh.h 4.1 %G% */
+/* csh.h 4.2 %G% */
 
 #include "sh.local.h"
 #ifdef VMUNIX
 
 #include "sh.local.h"
 #ifdef VMUNIX
@@ -397,6 +397,7 @@ char        *globone();
 struct biltins *isbfunc();
 char   **glob();
 char   *operate();
 struct biltins *isbfunc();
 char   **glob();
 char   *operate();
+int    phup();
 int    pintr();
 int    pchild();
 char   *putn();
 int    pintr();
 int    pchild();
 char   *putn();
index 881b9e6..5d8a379 100644 (file)
@@ -69,16 +69,15 @@ hfree(hp)
 dohist(vp)
        char **vp;
 {
 dohist(vp)
        char **vp;
 {
-       int n, rflg = 0, cflg = 0;
-
+       int n, rflg = 0, hflg = 0;
        if (getn(value("history")) == 0)
                return;
        if (setintr)
                sigrelse(SIGINT);
        vp++;
        while (*vp[0] == '-') {
        if (getn(value("history")) == 0)
                return;
        if (setintr)
                sigrelse(SIGINT);
        vp++;
        while (*vp[0] == '-') {
-               if (*vp && eq(*vp, "-c")) {
-                       cflg++;
+               if (*vp && eq(*vp, "-h")) {
+                       hflg++;
                        vp++;
                }
                if (*vp && eq(*vp, "-r")) {
                        vp++;
                }
                if (*vp && eq(*vp, "-r")) {
@@ -88,14 +87,15 @@ dohist(vp)
        }
        if (*vp)
                n = getn(*vp);
        }
        if (*vp)
                n = getn(*vp);
-       else
-               n = 1000;
-       dohist1(Histlist.Hnext, &n, rflg, cflg);
+       else {
+               n = getn(value("history"));
+       }
+       dohist1(Histlist.Hnext, &n, rflg, hflg);
 }
 
 }
 
-dohist1(hp, np, rflg, cflg)
+dohist1(hp, np, rflg, hflg)
        struct Hist *hp;
        struct Hist *hp;
-       int *np;
+       int *np, rflg, hflg;
 {
        bool print = (*np) > 0;
 top:
 {
        bool print = (*np) > 0;
 top:
@@ -104,23 +104,23 @@ top:
        (*np)--;
        hp->Href++;
        if (rflg == 0) {
        (*np)--;
        hp->Href++;
        if (rflg == 0) {
-               dohist1(hp->Hnext, np, rflg, cflg);
+               dohist1(hp->Hnext, np, rflg, hflg);
                if (print)
                if (print)
-                       phist(hp, cflg);
+                       phist(hp, hflg);
                return;
        }
        if (*np >= 0)
                return;
        }
        if (*np >= 0)
-               phist(hp, cflg);
+               phist(hp, hflg);
        hp = hp->Hnext;
        goto top;
 }
 
        hp = hp->Hnext;
        goto top;
 }
 
-phist(hp, cflg)
+phist(hp, hflg)
        register struct Hist *hp;
        register struct Hist *hp;
-       int cflg;
+       int hflg;
 {
 
 {
 
-       if (cflg == 0)
+       if (hflg == 0)
                printf("%6d\t", hp->Hnum);
        prlex(&hp->Hlex);
 }
                printf("%6d\t", hp->Hnum);
        prlex(&hp->Hlex);
 }