1 #ifndef lint 2 static char *sccsid = "@(#)hist.c 4.10 (Berkeley) 03/19/85"; 3 #endif 4 5 #include "sh.h" 6 7 /* 8 * C shell 9 */ 10 11 savehist(sp) 12 struct wordent *sp; 13 { 14 register struct Hist *hp, *np; 15 register int histlen = 0; 16 char *cp; 17 18 /* throw away null lines */ 19 if (sp->next->word[0] == '\n') 20 return; 21 cp = value("history"); 22 if (*cp) { 23 register char *p = cp; 24 25 while (*p) { 26 if (!digit(*p)) { 27 histlen = 0; 28 break; 29 } 30 histlen = histlen * 10 + *p++ - '0'; 31 } 32 } 33 for (hp = &Histlist; np = hp->Hnext;) 34 if (eventno - np->Href >= histlen || histlen == 0) 35 hp->Hnext = np->Hnext, hfree(np); 36 else 37 hp = np; 38 (void) enthist(++eventno, sp, 1); 39 } 40 41 struct Hist * 42 enthist(event, lp, docopy) 43 int event; 44 register struct wordent *lp; 45 bool docopy; 46 { 47 register struct Hist *np; 48 49 np = (struct Hist *) xalloc(sizeof *np); 50 np->Hnum = np->Href = event; 51 if (docopy) 52 copylex(&np->Hlex, lp); 53 else { 54 np->Hlex.next = lp->next; 55 lp->next->prev = &np->Hlex; 56 np->Hlex.prev = lp->prev; 57 lp->prev->next = &np->Hlex; 58 } 59 np->Hnext = Histlist.Hnext; 60 Histlist.Hnext = np; 61 return (np); 62 } 63 64 hfree(hp) 65 register struct Hist *hp; 66 { 67 68 freelex(&hp->Hlex); 69 xfree((char *)hp); 70 } 71 72 dohist(vp) 73 char **vp; 74 { 75 int n, rflg = 0, hflg = 0; 76 if (getn(value("history")) == 0) 77 return; 78 if (setintr) 79 (void) sigsetmask(sigblock(0) & ~sigmask(SIGINT)); 80 while (*++vp && **vp == '-') { 81 char *vp2 = *vp; 82 83 while (*++vp2) 84 switch (*vp2) { 85 case 'h': 86 hflg++; 87 break; 88 case 'r': 89 rflg++; 90 break; 91 case '-': /* ignore multiple '-'s */ 92 break; 93 default: 94 printf("Unknown flag: -%c\n", *vp2); 95 error("Usage: history [-rh] [# number of events]"); 96 } 97 } 98 if (*vp) 99 n = getn(*vp); 100 else { 101 n = getn(value("history")); 102 } 103 dohist1(Histlist.Hnext, &n, rflg, hflg); 104 } 105 106 dohist1(hp, np, rflg, hflg) 107 struct Hist *hp; 108 int *np, rflg, hflg; 109 { 110 bool print = (*np) > 0; 111 top: 112 if (hp == 0) 113 return; 114 (*np)--; 115 hp->Href++; 116 if (rflg == 0) { 117 dohist1(hp->Hnext, np, rflg, hflg); 118 if (print) 119 phist(hp, hflg); 120 return; 121 } 122 if (*np >= 0) 123 phist(hp, hflg); 124 hp = hp->Hnext; 125 goto top; 126 } 127 128 phist(hp, hflg) 129 register struct Hist *hp; 130 int hflg; 131 { 132 133 if (hflg == 0) 134 printf("%6d\t", hp->Hnum); 135 prlex(&hp->Hlex); 136 } 137