/* * Copyright (c) 1980 Regents of the University of California. * All rights reserved. The Berkeley Software License Agreement * specifies the terms and conditions for redistribution. */ #if !defined(lint) && defined(DOSCCS) static char *sccsid = "@(#)sh.hist.c 5.2 (Berkeley) 6/6/85"; #endif #include "sh.h" /* * C shell */ savehist(sp) struct wordent *sp; { register struct Hist *hp, *np; register int histlen = 0; char *cp; /* throw away null lines */ if (sp->next->word[0] == '\n') return; cp = value("history"); if (*cp) { register char *p = cp; while (*p) { if (!digit(*p)) { histlen = 0; break; } histlen = histlen * 10 + *p++ - '0'; } } for (hp = &Histlist; np = hp->Hnext;) if (eventno - np->Href >= histlen || histlen == 0) hp->Hnext = np->Hnext, hfree(np); else hp = np; (void) enthist(++eventno, sp, 1); } struct Hist * enthist(event, lp, docopy) int event; register struct wordent *lp; bool docopy; { register struct Hist *np; np = (struct Hist *) xalloc(sizeof *np); np->Hnum = np->Href = event; if (docopy) copylex(&np->Hlex, lp); else { np->Hlex.next = lp->next; lp->next->prev = &np->Hlex; np->Hlex.prev = lp->prev; lp->prev->next = &np->Hlex; } np->Hnext = Histlist.Hnext; Histlist.Hnext = np; return (np); } hfree(hp) register struct Hist *hp; { freelex(&hp->Hlex); xfree((char *)hp); } dohist(vp) char **vp; { int n, rflg = 0, hflg = 0; if (getn(value("history")) == 0) return; if (setintr) (void) sigsetmask(sigblock(0L) & ~sigmask(SIGINT)); while (*++vp && **vp == '-') { char *vp2 = *vp; while (*++vp2) switch (*vp2) { case 'h': hflg++; break; case 'r': rflg++; break; case '-': /* ignore multiple '-'s */ break; default: printf("Unknown flag: -%c\n", *vp2); error("Usage: history [-rh] [# number of events]"); } } if (*vp) n = getn(*vp); else { n = getn(value("history")); } dohist1(Histlist.Hnext, &n, rflg, hflg); } dohist1(hp, np, rflg, hflg) struct Hist *hp; int *np, rflg, hflg; { bool print = (*np) > 0; top: if (hp == 0) return; (*np)--; hp->Href++; if (rflg == 0) { dohist1(hp->Hnext, np, rflg, hflg); if (print) phist(hp, hflg); return; } if (*np >= 0) phist(hp, hflg); hp = hp->Hnext; goto top; } phist(hp, hflg) register struct Hist *hp; int hflg; { if (hflg == 0) printf("%6d\t", hp->Hnum); prlex(&hp->Hlex); }