1 /* $NetBSD: hist.c,v 1.17 2007/07/16 14:07:01 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.17 2007/07/16 14:07:01 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);
86 enthist(int event
, struct wordent
*lp
, int docopy
)
90 np
= (struct Hist
*)xmalloc((size_t)sizeof(*np
));
91 np
->Hnum
= np
->Href
= event
;
93 copylex(&np
->Hlex
, lp
);
96 np
->Hlex
.next
= lp
->next
;
97 lp
->next
->prev
= &np
->Hlex
;
98 np
->Hlex
.prev
= lp
->prev
;
99 lp
->prev
->next
= &np
->Hlex
;
101 np
->Hnext
= Histlist
.Hnext
;
107 hfree(struct Hist
*hp
)
115 dohist(Char
**v
, struct command
*t
)
123 if (getn(value(STRhistory
)) == 0)
126 sigemptyset(&nsigset
);
127 (void)sigaddset(&nsigset
, SIGINT
);
128 (void)sigprocmask(SIG_UNBLOCK
, &nsigset
, NULL
);
130 while (*++v
&& **v
== '-') {
141 case '-': /* ignore multiple '-'s */
144 stderror(ERR_HISTUS
);
151 n
= getn(value(STRhistory
));
153 dohist1(Histlist
.Hnext
, &n
, rflg
, hflg
);
157 dohist1(struct Hist
*hp
, int *np
, int rflg
, int hflg
)
163 for (; hp
!= 0; hp
= hp
->Hnext
) {
167 dohist1(hp
->Hnext
, np
, rflg
, hflg
);
178 phist(struct Hist
*hp
, int hflg
)
181 (void)fprintf(cshout
, "%6d\t", hp
->Hnum
);
182 prlex(cshout
, &hp
->Hlex
);