Commit | Line | Data |
---|---|---|
1249ad5d | 1 | static char *sccsid = "@(#)hist.c 4.8 %G%"; |
91a455c6 BJ |
2 | |
3 | #include "sh.h" | |
4 | ||
5 | /* | |
6 | * C shell | |
7 | */ | |
8 | ||
9 | savehist(sp) | |
10 | struct wordent *sp; | |
11 | { | |
12 | register struct Hist *hp, *np; | |
13 | int histlen; | |
14 | register char *cp; | |
15 | ||
16 | cp = value("history"); | |
17 | if (*cp == 0) | |
18 | histlen = 0; | |
19 | else { | |
20 | while (*cp && digit(*cp)) | |
21 | cp++; | |
22 | /* avoid a looping snafu */ | |
23 | if (*cp) | |
24 | set("history", "10"); | |
25 | histlen = getn(value("history")); | |
26 | } | |
27 | /* throw away null lines */ | |
28 | if (sp->next->word[0] == '\n') | |
29 | return; | |
30 | for (hp = &Histlist; np = hp->Hnext;) | |
31 | if (eventno - np->Href >= histlen || histlen == 0) | |
32 | hp->Hnext = np->Hnext, hfree(np); | |
33 | else | |
34 | hp = np; | |
35 | enthist(++eventno, sp, 1); | |
36 | } | |
37 | ||
38 | struct Hist * | |
39 | enthist(event, lp, docopy) | |
40 | int event; | |
41 | register struct wordent *lp; | |
42 | bool docopy; | |
43 | { | |
44 | register struct Hist *np; | |
45 | ||
46 | np = (struct Hist *) calloc(1, sizeof *np); | |
47 | np->Hnum = np->Href = event; | |
48 | if (docopy) | |
49 | copylex(&np->Hlex, lp); | |
50 | else { | |
51 | np->Hlex.next = lp->next; | |
52 | lp->next->prev = &np->Hlex; | |
53 | np->Hlex.prev = lp->prev; | |
54 | lp->prev->next = &np->Hlex; | |
55 | } | |
56 | np->Hnext = Histlist.Hnext; | |
57 | Histlist.Hnext = np; | |
58 | return (np); | |
59 | } | |
60 | ||
61 | hfree(hp) | |
62 | register struct Hist *hp; | |
63 | { | |
64 | ||
65 | freelex(&hp->Hlex); | |
66 | xfree((char *)hp); | |
67 | } | |
68 | ||
69 | dohist(vp) | |
70 | char **vp; | |
71 | { | |
d7929fa7 | 72 | int n, rflg = 0, hflg = 0; |
91a455c6 BJ |
73 | if (getn(value("history")) == 0) |
74 | return; | |
75 | if (setintr) | |
d33af40e | 76 | sigsetmask(sigblock(0) & ~sigmask(SIGINT)); |
91a455c6 | 77 | vp++; |
ea775389 | 78 | while (*vp && *vp[0] == '-') { |
1249ad5d | 79 | if (*vp && eq(*vp, "-h")) |
d7929fa7 | 80 | hflg++; |
1249ad5d | 81 | else if (*vp && eq(*vp, "-r")) |
63af20f7 | 82 | rflg++; |
1249ad5d | 83 | vp++; |
91a455c6 BJ |
84 | } |
85 | if (*vp) | |
86 | n = getn(*vp); | |
d7929fa7 KM |
87 | else { |
88 | n = getn(value("history")); | |
89 | } | |
90 | dohist1(Histlist.Hnext, &n, rflg, hflg); | |
91a455c6 BJ |
91 | } |
92 | ||
d7929fa7 | 93 | dohist1(hp, np, rflg, hflg) |
91a455c6 | 94 | struct Hist *hp; |
d7929fa7 | 95 | int *np, rflg, hflg; |
91a455c6 BJ |
96 | { |
97 | bool print = (*np) > 0; | |
98 | top: | |
99 | if (hp == 0) | |
100 | return; | |
101 | (*np)--; | |
102 | hp->Href++; | |
103 | if (rflg == 0) { | |
d7929fa7 | 104 | dohist1(hp->Hnext, np, rflg, hflg); |
91a455c6 | 105 | if (print) |
d7929fa7 | 106 | phist(hp, hflg); |
91a455c6 BJ |
107 | return; |
108 | } | |
109 | if (*np >= 0) | |
d7929fa7 | 110 | phist(hp, hflg); |
91a455c6 BJ |
111 | hp = hp->Hnext; |
112 | goto top; | |
113 | } | |
114 | ||
d7929fa7 | 115 | phist(hp, hflg) |
91a455c6 | 116 | register struct Hist *hp; |
d7929fa7 | 117 | int hflg; |
91a455c6 BJ |
118 | { |
119 | ||
d7929fa7 | 120 | if (hflg == 0) |
63af20f7 | 121 | printf("%6d\t", hp->Hnum); |
91a455c6 BJ |
122 | prlex(&hp->Hlex); |
123 | } |