xref: /original-bsd/bin/csh/hist.c (revision ba72ef4c)
1 static	char *sccsid = "@(#)hist.c 4.1 10/09/80";
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 {
72 	int n, rflg = 0;
73 
74 	if (getn(value("history")) == 0)
75 		return;
76 	if (setintr)
77 		sigrelse(SIGINT);
78 	vp++;
79 	if (*vp && eq(*vp, "-r")) {
80 		rflg++;
81 		vp++;
82 	}
83 	if (*vp)
84 		n = getn(*vp);
85 	else
86 		n = 1000;
87 	dohist1(Histlist.Hnext, &n, rflg);
88 }
89 
90 dohist1(hp, np, rflg)
91 	struct Hist *hp;
92 	int *np;
93 {
94 	bool print = (*np) > 0;
95 top:
96 	if (hp == 0)
97 		return;
98 	(*np)--;
99 	hp->Href++;
100 	if (rflg == 0) {
101 		dohist1(hp->Hnext, np, rflg);
102 		if (print)
103 			phist(hp);
104 		return;
105 	}
106 	if (*np >= 0)
107 		phist(hp);
108 	hp = hp->Hnext;
109 	goto top;
110 }
111 
112 phist(hp)
113 	register struct Hist *hp;
114 {
115 
116 	printf("%6d\t", hp->Hnum);
117 	prlex(&hp->Hlex);
118 }
119