xref: /illumos-gate/usr/src/cmd/csh/sh.hist.c (revision 7c478bd9)
1 /*
2  * Copyright 1990 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
7 /*	  All Rights Reserved  	*/
8 
9 /*
10  * Copyright (c) 1980 Regents of the University of California.
11  * All rights reserved.  The Berkeley Software License Agreement
12  * specifies the terms and conditions for redistribution.
13  */
14 
15 #pragma ident	"%Z%%M%	%I%	%E% SMI"
16 
17 #include "sh.h"
18 #include "sh.tconst.h"
19 
20 /*
21  * C shell
22  */
23 
24 savehist(sp)
25 	struct wordent *sp;
26 {
27 	register struct Hist *hp, *np;
28 	register int histlen = 0;
29 	tchar *cp;
30 
31 #ifdef TRACE
32 	tprintf("TRACE- savehist()\n");
33 #endif
34 	/* throw away null lines */
35 	if (sp->next->word[0] == '\n')
36 		return;
37 	cp = value(S_history /*"history"*/);
38 	if (*cp) {
39 		register tchar *p = cp;
40 
41 		while (*p) {
42 			if (!digit(*p)) {
43 				histlen = 0;
44 				break;
45 			}
46 			histlen = histlen * 10 + *p++ - '0';
47 		}
48 	}
49 	for (hp = &Histlist; np = hp->Hnext;)
50 		if (eventno - np->Href >= histlen || histlen == 0)
51 			hp->Hnext = np->Hnext, hfree(np);
52 		else
53 			hp = np;
54 	(void) enthist(++eventno, sp, 1);
55 }
56 
57 struct Hist *
58 enthist(event, lp, docopy)
59 	int event;
60 	register struct wordent *lp;
61 	bool docopy;
62 {
63 	register struct Hist *np;
64 
65 #ifdef TRACE
66 	tprintf("TRACE- enthist()\n");
67 #endif
68 	np = (struct Hist *) xalloc(sizeof *np);
69 	np->Hnum = np->Href = event;
70 	if (docopy)
71 		copylex(&np->Hlex, lp);
72 	else {
73 		np->Hlex.next = lp->next;
74 		lp->next->prev = &np->Hlex;
75 		np->Hlex.prev = lp->prev;
76 		lp->prev->next = &np->Hlex;
77 	}
78 	np->Hnext = Histlist.Hnext;
79 	Histlist.Hnext = np;
80 	return (np);
81 }
82 
83 hfree(hp)
84 	register struct Hist *hp;
85 {
86 #ifdef TRACE
87 	tprintf("TRACE- hfree()\n");
88 #endif
89 
90 	freelex(&hp->Hlex);
91 	xfree( (tchar *)hp);
92 }
93 
94 dohist(vp)
95 	tchar **vp;
96 {
97 	int n, rflg = 0, hflg = 0;
98 #ifdef TRACE
99 	tprintf("TRACE- dohist()\n");
100 #endif
101 	if (getn(value(S_history /*"history"*/)) == 0)
102 		return;
103 	if (setintr)
104 		(void) sigsetmask(sigblock(0) & ~sigmask(SIGINT));
105 	while (*++vp && **vp == '-') {
106 		tchar *vp2 = *vp;
107 
108 		while (*++vp2)
109 			switch (*vp2) {
110 			case 'h':
111 				hflg++;
112 				break;
113 			case 'r':
114 				rflg++;
115 				break;
116 			case '-':	/* ignore multiple '-'s */
117 				break;
118 			default:
119 				printf("Unknown flag: -%c\n", *vp2);
120 				error("Usage: history [-rh] [# number of events]");
121 			}
122 	}
123 	if (*vp)
124 		n = getn(*vp);
125 	else {
126 		n = getn(value(S_history /*"history"*/));
127 	}
128 	dohist1(Histlist.Hnext, &n, rflg, hflg);
129 }
130 
131 dohist1(hp, np, rflg, hflg)
132 	struct Hist *hp;
133 	int *np, rflg, hflg;
134 {
135 	bool print = (*np) > 0;
136 #ifdef TRACE
137 	tprintf("TRACE- dohist1()\n");
138 #endif
139 top:
140 	if (hp == 0)
141 		return;
142 	(*np)--;
143 	hp->Href++;
144 	if (rflg == 0) {
145 		dohist1(hp->Hnext, np, rflg, hflg);
146 		if (print)
147 			phist(hp, hflg);
148 		return;
149 	}
150 	if (*np >= 0)
151 		phist(hp, hflg);
152 	hp = hp->Hnext;
153 	goto top;
154 }
155 
156 phist(hp, hflg)
157 	register struct Hist *hp;
158 	int hflg;
159 {
160 #ifdef TRACE
161 	tprintf("TRACE- phist()\n");
162 #endif
163 
164 	if (hflg == 0)
165 		printf("%6d\t", hp->Hnum);
166 	prlex(&hp->Hlex);
167 }
168