hist.c revision 1.8 1 /* $NetBSD: hist.c,v 1.8 1997/01/13 17:53:24 tls Exp $ */
2
3 /*-
4 * Copyright (c) 1980, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)hist.c 8.1 (Berkeley) 5/31/93";
39 #else
40 static char rcsid[] = "$NetBSD: hist.c,v 1.8 1997/01/13 17:53:24 tls Exp $";
41 #endif
42 #endif /* not lint */
43
44 #include <sys/types.h>
45 #include <stdlib.h>
46 #if __STDC__
47 # include <stdarg.h>
48 #else
49 # include <varargs.h>
50 #endif
51
52 #include "csh.h"
53 #include "extern.h"
54
55 static void hfree __P((struct Hist *));
56 static void dohist1 __P((struct Hist *, int *, int, int));
57 static void phist __P((struct Hist *, int));
58
59 void
60 savehist(sp)
61 struct wordent *sp;
62 {
63 struct Hist *hp, *np;
64 int histlen = 0;
65 Char *cp;
66
67 /* throw away null lines */
68 if (sp->next->word[0] == '\n')
69 return;
70 cp = value(STRhistory);
71 if (*cp) {
72 Char *p = cp;
73
74 while (*p) {
75 if (!Isdigit(*p)) {
76 histlen = 0;
77 break;
78 }
79 histlen = histlen * 10 + *p++ - '0';
80 }
81 }
82 for (hp = &Histlist; (np = hp->Hnext) != NULL;)
83 if (eventno - np->Href >= histlen || histlen == 0)
84 hp->Hnext = np->Hnext, hfree(np);
85 else
86 hp = np;
87 (void) enthist(++eventno, sp, 1);
88 }
89
90 struct Hist *
91 enthist(event, lp, docopy)
92 int event;
93 struct wordent *lp;
94 bool docopy;
95 {
96 struct Hist *np;
97
98 np = (struct Hist *) xmalloc((size_t) sizeof(*np));
99 np->Hnum = np->Href = event;
100 if (docopy) {
101 copylex(&np->Hlex, lp);
102 }
103 else {
104 np->Hlex.next = lp->next;
105 lp->next->prev = &np->Hlex;
106 np->Hlex.prev = lp->prev;
107 lp->prev->next = &np->Hlex;
108 }
109 np->Hnext = Histlist.Hnext;
110 Histlist.Hnext = np;
111 return (np);
112 }
113
114 static void
115 hfree(hp)
116 struct Hist *hp;
117 {
118
119 freelex(&hp->Hlex);
120 xfree((ptr_t) hp);
121 }
122
123 void
124 /*ARGSUSED*/
125 dohist(v, t)
126 Char **v;
127 struct command *t;
128 {
129 int n, rflg = 0, hflg = 0;
130 sigset_t sigset;
131
132 if (getn(value(STRhistory)) == 0)
133 return;
134 if (setintr) {
135 sigemptyset(&sigset);
136 sigaddset(&sigset, SIGINT);
137 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
138 }
139 while (*++v && **v == '-') {
140 Char *vp = *v;
141
142 while (*++vp)
143 switch (*vp) {
144 case 'h':
145 hflg++;
146 break;
147 case 'r':
148 rflg++;
149 break;
150 case '-': /* ignore multiple '-'s */
151 break;
152 default:
153 stderror(ERR_HISTUS);
154 break;
155 }
156 }
157 if (*v)
158 n = getn(*v);
159 else {
160 n = getn(value(STRhistory));
161 }
162 dohist1(Histlist.Hnext, &n, rflg, hflg);
163 }
164
165 static void
166 dohist1(hp, np, rflg, hflg)
167 struct Hist *hp;
168 int *np, rflg, hflg;
169 {
170 bool print = (*np) > 0;
171
172 for (; hp != 0; hp = hp->Hnext) {
173 (*np)--;
174 hp->Href++;
175 if (rflg == 0) {
176 dohist1(hp->Hnext, np, rflg, hflg);
177 if (print)
178 phist(hp, hflg);
179 return;
180 }
181 if (*np >= 0)
182 phist(hp, hflg);
183 }
184 }
185
186 static void
187 phist(hp, hflg)
188 struct Hist *hp;
189 int hflg;
190 {
191 if (hflg == 0)
192 (void) fprintf(cshout, "%6d\t", hp->Hnum);
193 prlex(cshout, &hp->Hlex);
194 }
195