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