Fixed directory printing in case there was both a ~user and a ~user1.
[unix-history] / usr / src / bin / csh / hist.c
CommitLineData
ecc449eb
KB
1/*-
2 * Copyright (c) 1980, 1991 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * %sccs.include.redist.c%
b79f4fa9
DF
6 */
7
35371dec 8#ifndef lint
454c2aa3 9static char sccsid[] = "@(#)hist.c 5.10 (Berkeley) %G%";
ecc449eb 10#endif /* not lint */
91a455c6 11
b9c4f741
KB
12#include <sys/types.h>
13#include <stdlib.h>
4df6491c
CZ
14#if __STDC__
15# include <stdarg.h>
16#else
17# include <varargs.h>
18#endif
19
4d7b2685
KB
20#include "csh.h"
21#include "extern.h"
91a455c6 22
0aec749d 23static void hfree __P((struct Hist *));
454c2aa3
CZ
24static void dohist1 __P((struct Hist *, int *, int, int));
25static void phist __P((struct Hist *, int));
91a455c6 26
6e37afca 27void
91a455c6 28savehist(sp)
6e37afca 29 struct wordent *sp;
91a455c6 30{
6e37afca
KB
31 register struct Hist *hp, *np;
32 register int histlen = 0;
33 Char *cp;
34
35 /* throw away null lines */
36 if (sp->next->word[0] == '\n')
37 return;
38 cp = value(STRhistory);
39 if (*cp) {
40 register Char *p = cp;
41
42 while (*p) {
43 if (!Isdigit(*p)) {
44 histlen = 0;
45 break;
46 }
47 histlen = histlen * 10 + *p++ - '0';
35371dec 48 }
6e37afca
KB
49 }
50 for (hp = &Histlist; np = hp->Hnext;)
51 if (eventno - np->Href >= histlen || histlen == 0)
52 hp->Hnext = np->Hnext, hfree(np);
53 else
54 hp = np;
55 (void) enthist(++eventno, sp, 1);
91a455c6
BJ
56}
57
58struct Hist *
59enthist(event, lp, docopy)
6e37afca
KB
60 int event;
61 register struct wordent *lp;
62 bool docopy;
91a455c6 63{
6e37afca
KB
64 register struct Hist *np;
65
66 np = (struct Hist *) xmalloc((size_t) sizeof(*np));
6e37afca
KB
67 np->Hnum = np->Href = event;
68 if (docopy) {
69 copylex(&np->Hlex, lp);
70 }
71 else {
72 np->Hlex.next = lp->next;
73 lp->next->prev = &np->Hlex;
74 np->Hlex.prev = lp->prev;
75 lp->prev->next = &np->Hlex;
76 }
77 np->Hnext = Histlist.Hnext;
78 Histlist.Hnext = np;
79 return (np);
91a455c6
BJ
80}
81
6e37afca 82static void
91a455c6 83hfree(hp)
6e37afca 84 register struct Hist *hp;
91a455c6
BJ
85{
86
6e37afca
KB
87 freelex(&hp->Hlex);
88 xfree((ptr_t) hp);
91a455c6
BJ
89}
90
6e37afca 91void
454c2aa3
CZ
92/*ARGSUSED*/
93dohist(v, t)
94 Char **v;
95 struct command *t;
91a455c6 96{
454c2aa3 97 int n, rflg = 0, hflg = 0;
6e37afca
KB
98
99 if (getn(value(STRhistory)) == 0)
100 return;
101 if (setintr)
b9c4f741 102 (void) sigsetmask(sigblock((sigset_t) 0) & ~sigmask(SIGINT));
454c2aa3
CZ
103 while (*++v && **v == '-') {
104 Char *vp = *v;
6e37afca 105
454c2aa3
CZ
106 while (*++vp)
107 switch (*vp) {
6e37afca
KB
108 case 'h':
109 hflg++;
110 break;
111 case 'r':
112 rflg++;
113 break;
6e37afca
KB
114 case '-': /* ignore multiple '-'s */
115 break;
116 default:
117 stderror(ERR_HISTUS);
118 break;
119 }
120 }
454c2aa3
CZ
121 if (*v)
122 n = getn(*v);
6e37afca
KB
123 else {
124 n = getn(value(STRhistory));
125 }
454c2aa3 126 dohist1(Histlist.Hnext, &n, rflg, hflg);
91a455c6
BJ
127}
128
6e37afca 129static void
454c2aa3 130dohist1(hp, np, rflg, hflg)
6e37afca 131 struct Hist *hp;
454c2aa3 132 int *np, rflg, hflg;
91a455c6 133{
6e37afca
KB
134 bool print = (*np) > 0;
135
136 for (; hp != 0; hp = hp->Hnext) {
91a455c6
BJ
137 (*np)--;
138 hp->Href++;
139 if (rflg == 0) {
454c2aa3 140 dohist1(hp->Hnext, np, rflg, hflg);
6e37afca 141 if (print)
454c2aa3 142 phist(hp, hflg);
6e37afca 143 return;
91a455c6
BJ
144 }
145 if (*np >= 0)
454c2aa3 146 phist(hp, hflg);
6e37afca 147 }
91a455c6
BJ
148}
149
6e37afca 150static void
454c2aa3 151phist(hp, hflg)
6e37afca 152 register struct Hist *hp;
454c2aa3 153 int hflg;
91a455c6 154{
454c2aa3
CZ
155 if (hflg == 0)
156 (void) fprintf(cshout, "%6d\t", hp->Hnum);
157 prlex(cshout, &hp->Hlex);
91a455c6 158}