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