From d7929fa799f7aa58afd6475adb18d39cd488f90f Mon Sep 17 00:00:00 2001 From: Kirk McKusick Date: Fri, 20 Nov 1981 05:04:04 -0800 Subject: [PATCH] add ``history -h >file'' and ``source -h file'' to 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 | 71 ++++++++++++++++++++++++++++++++---------- usr/src/bin/csh/csh.h | 3 +- usr/src/bin/csh/hist.c | 30 +++++++++--------- 3 files changed, 72 insertions(+), 32 deletions(-) diff --git a/usr/src/bin/csh/csh.c b/usr/src/bin/csh/csh.c index 5552b127f3..b6a55da727 100644 --- a/usr/src/bin/csh/csh.c +++ b/usr/src/bin/csh/csh.c @@ -13,7 +13,8 @@ static char *sccsid = "@(#)csh.c 4.4 %G%"; */ 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; @@ -113,6 +114,8 @@ main(c, av) 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. @@ -285,7 +288,9 @@ retry: 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 @@ -318,11 +323,9 @@ notty: srccat(value("home"), "/.cshrc"); if (!fast && !arginp && !onelflg) dohash(); + dosource(loadhist); if (loginsh) { srccat(value("home"), "/.login"); - enterhist = 1; - srccat(value("home"), "/.history"); - enterhist = 0; } } @@ -351,6 +354,7 @@ notty: child++; goodbye(); } + rechist(); exitstat(); } @@ -361,7 +365,9 @@ untty() setpgrp(0, opgrp); ioctl(FSHTTY, TIOCSPGRP, &opgrp); if (oldisc != -1 && oldisc != NTTYDISC) { +#ifdef DEBUG printf("\nReverting to old tty driver...\n"); +#endif DEBUG ioctl(FSHTTY, TIOCSETD, &oldisc); } } @@ -416,9 +422,9 @@ srccat(cp, dp) /* ioctl(unit, FIOCLEX, NULL); */ xfree(ep); #ifdef INGRES - srcunit(unit, 0); + srcunit(unit, 0, 0); #else - srcunit(unit, 1); + srcunit(unit, 1, 0); #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. */ -srcunit(unit, onlyown) +srcunit(unit, onlyown, hflg) register int unit; bool onlyown; + bool hflg; { /* 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; + bool oenterhist = enterhist; + char OHIST = HIST; #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; + enterhist = hflg; + if (enterhist) + HIST = '\0'; /* * 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; + enterhist = oenterhist; + HIST = OHIST; #ifdef TELL cantell = otell; #endif @@ -527,12 +541,12 @@ srcunit(unit, onlyown) error(NOSTR); } -goodbye() +rechist() { char buf[BUFSIZ]; int fp, ftmp, oldidfds; - if (loginsh) { + if (!fast) { strcpy(buf, value("home")); strcat(buf, "/.history"); fp = creat(buf, 0777); @@ -546,6 +560,12 @@ goodbye() SHOUT = ftmp; didfds = oldidfds; } + } +} + +goodbye() +{ + if (loginsh) { signal(SIGQUIT, SIG_IGN); sigset(SIGINT, SIG_IGN); signal(SIGTERM, SIG_IGN); @@ -553,6 +573,7 @@ goodbye() if (adrof("home")) srccat(value("home"), "/.logout"); } + rechist(); exitstat(); } @@ -568,6 +589,15 @@ exitstat() 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. @@ -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. */ - if (lex(¶ml) && !err && intty || adrof("verbose")) { + if (lex(¶ml) && !err && intty || + adrof("verbose")) { haderr = 1; prlex(¶ml); haderr = 0; @@ -741,9 +772,10 @@ process(catch) savehist(¶ml); /* - * Print lexical error messages. + * Print lexical error messages, except when sourcing + * history lists. */ - if (err) + if (!enterhist && err) error(err); /* @@ -780,14 +812,21 @@ dosource(t) { register char *f; register int u; + bool hflg = 0; + char buf[BUFSIZ]; 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); - if (u < 0) + if (u < 0 && !hflg) Perror(f); - srcunit(u, 0); + srcunit(u, 0, hflg); } /* diff --git a/usr/src/bin/csh/csh.h b/usr/src/bin/csh/csh.h index feb4e062f2..49d8cef569 100644 --- a/usr/src/bin/csh/csh.h +++ b/usr/src/bin/csh/csh.h @@ -1,4 +1,4 @@ -/* csh.h 4.1 %G% */ +/* csh.h 4.2 %G% */ #include "sh.local.h" #ifdef VMUNIX @@ -397,6 +397,7 @@ char *globone(); struct biltins *isbfunc(); char **glob(); char *operate(); +int phup(); int pintr(); int pchild(); char *putn(); diff --git a/usr/src/bin/csh/hist.c b/usr/src/bin/csh/hist.c index 881b9e6319..5d8a379217 100644 --- a/usr/src/bin/csh/hist.c +++ b/usr/src/bin/csh/hist.c @@ -69,16 +69,15 @@ hfree(hp) 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 (*vp && eq(*vp, "-c")) { - cflg++; + if (*vp && eq(*vp, "-h")) { + hflg++; vp++; } if (*vp && eq(*vp, "-r")) { @@ -88,14 +87,15 @@ dohist(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; - int *np; + int *np, rflg, hflg; { bool print = (*np) > 0; top: @@ -104,23 +104,23 @@ top: (*np)--; hp->Href++; if (rflg == 0) { - dohist1(hp->Hnext, np, rflg, cflg); + dohist1(hp->Hnext, np, rflg, hflg); if (print) - phist(hp, cflg); + phist(hp, hflg); return; } if (*np >= 0) - phist(hp, cflg); + phist(hp, hflg); hp = hp->Hnext; goto top; } -phist(hp, cflg) +phist(hp, hflg) register struct Hist *hp; - int cflg; + int hflg; { - if (cflg == 0) + if (hflg == 0) printf("%6d\t", hp->Hnum); prlex(&hp->Hlex); } -- 2.20.1