1 /* $NetBSD: hist.c,v 1.20 2013/07/16 17:47:43 christos Exp $ */
4 * Copyright (c) 1980, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)hist.c 8.1 (Berkeley) 5/31/93";
37 __RCSID("$NetBSD: hist.c,v 1.20 2013/07/16 17:47:43 christos Exp $");
41 #include <sys/types.h>
49 static void hfree(struct Hist
*);
50 static void dohist1(struct Hist
*, int *, int, int);
51 static void phist(struct Hist
*, int);
54 savehist(struct wordent
*sp
)
62 /* throw away null lines */
63 if (sp
->next
->word
[0] == '\n')
65 cp
= value(STRhistory
);
74 histlen
= histlen
* 10 + *p
++ - '0';
77 for (hp
= &Histlist
; (np
= hp
->Hnext
) != NULL
;)
78 if (eventno
- np
->Href
>= histlen
|| histlen
== 0)
79 hp
->Hnext
= np
->Hnext
, hfree(np
);
82 (void) enthist(++eventno
, sp
, 1);
87 loadhist(struct Hist
*hp
) {
90 if (hi
== NULL
|| hp
== NULL
)
93 if (sprlex(&h
, &hp
->Hlex
) != -1) {
95 history(hi
, &ev
, H_ENTER
, h
);
101 enthist(int event
, struct wordent
*lp
, int docopy
)
108 if (sprlex(&h
, lp
) != -1) {
110 history(hi
, &ev
, H_ENTER
, h
);
114 np
= xmalloc(sizeof(*np
));
115 np
->Hnum
= np
->Href
= event
;
117 copylex(&np
->Hlex
, lp
);
120 np
->Hlex
.next
= lp
->next
;
121 lp
->next
->prev
= &np
->Hlex
;
122 np
->Hlex
.prev
= lp
->prev
;
123 lp
->prev
->next
= &np
->Hlex
;
125 np
->Hnext
= Histlist
.Hnext
;
131 hfree(struct Hist
*hp
)
139 dohist(Char
**v
, struct command
*t
)
147 if (getn(value(STRhistory
)) == 0)
150 sigemptyset(&nsigset
);
151 (void)sigaddset(&nsigset
, SIGINT
);
152 (void)sigprocmask(SIG_UNBLOCK
, &nsigset
, NULL
);
154 while (*++v
&& **v
== '-') {
165 case '-': /* ignore multiple '-'s */
168 stderror(ERR_HISTUS
);
175 n
= getn(value(STRhistory
));
177 dohist1(Histlist
.Hnext
, &n
, rflg
, hflg
);
181 dohist1(struct Hist
*hp
, int *np
, int rflg
, int hflg
)
187 for (; hp
!= 0; hp
= hp
->Hnext
) {
191 dohist1(hp
->Hnext
, np
, rflg
, hflg
);
202 phist(struct Hist
*hp
, int hflg
)
205 (void)fprintf(cshout
, "%6d\t", hp
->Hnum
);
206 prlex(cshout
, &hp
->Hlex
);