Commit | Line | Data |
---|---|---|
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 |
4d7b2685 | 9 | static char sccsid[] = "@(#)hist.c 5.6 (Berkeley) %G%"; |
ecc449eb | 10 | #endif /* not lint */ |
91a455c6 | 11 | |
4d7b2685 KB |
12 | #include "csh.h" |
13 | #include "extern.h" | |
91a455c6 | 14 | |
6e37afca KB |
15 | static void hfree(); |
16 | static void dohist1(); | |
17 | static void phist(); | |
91a455c6 | 18 | |
6e37afca | 19 | void |
91a455c6 | 20 | savehist(sp) |
6e37afca | 21 | struct wordent *sp; |
91a455c6 | 22 | { |
6e37afca KB |
23 | register struct Hist *hp, *np; |
24 | register int histlen = 0; | |
25 | Char *cp; | |
26 | ||
27 | /* throw away null lines */ | |
28 | if (sp->next->word[0] == '\n') | |
29 | return; | |
30 | cp = value(STRhistory); | |
31 | if (*cp) { | |
32 | register Char *p = cp; | |
33 | ||
34 | while (*p) { | |
35 | if (!Isdigit(*p)) { | |
36 | histlen = 0; | |
37 | break; | |
38 | } | |
39 | histlen = histlen * 10 + *p++ - '0'; | |
35371dec | 40 | } |
6e37afca KB |
41 | } |
42 | for (hp = &Histlist; np = hp->Hnext;) | |
43 | if (eventno - np->Href >= histlen || histlen == 0) | |
44 | hp->Hnext = np->Hnext, hfree(np); | |
45 | else | |
46 | hp = np; | |
47 | (void) enthist(++eventno, sp, 1); | |
91a455c6 BJ |
48 | } |
49 | ||
50 | struct Hist * | |
51 | enthist(event, lp, docopy) | |
6e37afca KB |
52 | int event; |
53 | register struct wordent *lp; | |
54 | bool docopy; | |
91a455c6 | 55 | { |
6e37afca KB |
56 | register struct Hist *np; |
57 | ||
58 | np = (struct Hist *) xmalloc((size_t) sizeof(*np)); | |
59 | (void) time(&(np->Htime)); | |
60 | np->Hnum = np->Href = event; | |
61 | if (docopy) { | |
62 | copylex(&np->Hlex, lp); | |
63 | } | |
64 | else { | |
65 | np->Hlex.next = lp->next; | |
66 | lp->next->prev = &np->Hlex; | |
67 | np->Hlex.prev = lp->prev; | |
68 | lp->prev->next = &np->Hlex; | |
69 | } | |
70 | np->Hnext = Histlist.Hnext; | |
71 | Histlist.Hnext = np; | |
72 | return (np); | |
91a455c6 BJ |
73 | } |
74 | ||
6e37afca | 75 | static void |
91a455c6 | 76 | hfree(hp) |
6e37afca | 77 | register struct Hist *hp; |
91a455c6 BJ |
78 | { |
79 | ||
6e37afca KB |
80 | freelex(&hp->Hlex); |
81 | xfree((ptr_t) hp); | |
91a455c6 BJ |
82 | } |
83 | ||
6e37afca | 84 | void |
91a455c6 | 85 | dohist(vp) |
6e37afca | 86 | Char **vp; |
91a455c6 | 87 | { |
6e37afca KB |
88 | int n, rflg = 0, hflg = 0, tflg = 0; |
89 | ||
90 | if (getn(value(STRhistory)) == 0) | |
91 | return; | |
92 | if (setintr) | |
93 | (void) sigsetmask(sigblock((sigmask_t) 0) & ~sigmask(SIGINT)); | |
94 | while (*++vp && **vp == '-') { | |
95 | Char *vp2 = *vp; | |
96 | ||
97 | while (*++vp2) | |
98 | switch (*vp2) { | |
99 | case 'h': | |
100 | hflg++; | |
101 | break; | |
102 | case 'r': | |
103 | rflg++; | |
104 | break; | |
105 | case 't': | |
106 | tflg++; | |
107 | break; | |
108 | case '-': /* ignore multiple '-'s */ | |
109 | break; | |
110 | default: | |
111 | stderror(ERR_HISTUS); | |
112 | break; | |
113 | } | |
114 | } | |
115 | if (*vp) | |
116 | n = getn(*vp); | |
117 | else { | |
118 | n = getn(value(STRhistory)); | |
119 | } | |
120 | dohist1(Histlist.Hnext, &n, rflg, hflg, tflg); | |
91a455c6 BJ |
121 | } |
122 | ||
6e37afca KB |
123 | static void |
124 | dohist1(hp, np, rflg, hflg, tflg) | |
125 | struct Hist *hp; | |
126 | int *np, rflg, hflg, tflg; | |
91a455c6 | 127 | { |
6e37afca KB |
128 | bool print = (*np) > 0; |
129 | ||
130 | for (; hp != 0; hp = hp->Hnext) { | |
91a455c6 BJ |
131 | (*np)--; |
132 | hp->Href++; | |
133 | if (rflg == 0) { | |
6e37afca KB |
134 | dohist1(hp->Hnext, np, rflg, hflg, tflg); |
135 | if (print) | |
136 | phist(hp, hflg, tflg); | |
137 | return; | |
91a455c6 BJ |
138 | } |
139 | if (*np >= 0) | |
6e37afca KB |
140 | phist(hp, hflg, tflg); |
141 | } | |
91a455c6 BJ |
142 | } |
143 | ||
6e37afca KB |
144 | static void |
145 | phist(hp, hflg, tflg) | |
146 | register struct Hist *hp; | |
147 | int hflg, tflg; | |
91a455c6 | 148 | { |
6e37afca KB |
149 | struct tm *t; |
150 | char ampm = 'a'; | |
91a455c6 | 151 | |
6e37afca KB |
152 | if (hflg == 0) { |
153 | xprintf("%6d\t", hp->Hnum); | |
154 | if (tflg == 0) { | |
155 | t = localtime(&hp->Htime); | |
156 | if (adrof(STRampm)) { /* addition by Hans J. Albertsson */ | |
157 | if (t->tm_hour >= 12) { | |
158 | if (t->tm_hour > 12) | |
159 | t->tm_hour -= 12; | |
160 | ampm = 'p'; | |
161 | } | |
162 | else if (t->tm_hour == 0) | |
163 | t->tm_hour = 12; | |
164 | xprintf("%2d:%02d%cm\t", t->tm_hour, t->tm_min, ampm); | |
165 | } | |
166 | else { | |
167 | xprintf("%2d:%02d\t", t->tm_hour, t->tm_min); | |
168 | } | |
169 | } | |
170 | } | |
171 | prlex(&hp->Hlex); | |
91a455c6 | 172 | } |