1 /* $Header: /p/tcsh/cvsroot/tcsh/sh.hist.c,v 3.53 2011/01/24 18:10:26 christos Exp $ */
3 * sh.hist.c: Shell history expansions and substitutions
6 * Copyright (c) 1980, 1991 The Regents of the University of California.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 RCSID("$tcsh: sh.hist.c,v 3.53 2011/01/24 18:10:26 christos Exp $")
41 extern struct Strbuf histline
;
44 static int heq (const struct wordent
*, const struct wordent
*);
45 static void hfree (struct Hist
*);
47 #define HIST_ONLY 0x01
48 #define HIST_SAVE 0x02
49 #define HIST_LOAD 0x04
51 #define HIST_CLEAR 0x10
52 #define HIST_MERGE 0x20
53 #define HIST_TIME 0x40
59 /* Static functions don't show up in gprof summaries. So eliminate "static"
60 * modifier from some frequently called functions. */
64 #define PG_STATIC static
67 /* #define DEBUG_HIST 1 */
69 static const int fastMergeErase
= 1;
70 static unsigned histCount
= 0; /* number elements on history list */
71 static struct Hist
*histTail
= NULL
; /* last element on history list */
72 static struct Hist
*histMerg
= NULL
; /* last element merged by Htime */
74 static void insertHistHashTable(struct Hist
*, unsigned);
77 /* Insert new element (hp) in history list after specified predecessor (pp). */
79 hinsert(struct Hist
*hp
, struct Hist
*pp
)
81 struct Hist
*fp
= pp
->Hnext
; /* following element, if any */
82 hp
->Hnext
= fp
, hp
->Hprev
= pp
;
87 histTail
= hp
; /* meaning hp->Hnext == NULL */
91 /* Remove the entry from the history list. */
93 hremove(struct Hist
*hp
)
95 struct Hist
*pp
= hp
->Hprev
;
96 assert(pp
); /* elements always have a previous */
97 pp
->Hnext
= hp
->Hnext
;
99 hp
->Hnext
->Hprev
= pp
;
101 histTail
= pp
; /* we must have been last */
102 if (hp
== histMerg
) /* deleting this hint from list */
104 assert(histCount
> 0);
108 /* Prune length of history list to specified size by history variable. */
110 discardExcess(int histlen
)
112 struct Hist
*hp
, *np
;
113 if (histTail
== NULL
) {
114 assert(histCount
== 0);
115 return; /* no entries on history list */
117 /* Prune dummy entries from the front, then old entries from the back. If
118 * the list is still too long scan the whole list as before. But only do a
119 * full scan if the list is more than 6% (1/16th) too long. */
120 while (histCount
> (unsigned)histlen
&& (np
= Histlist
.Hnext
)) {
121 if (eventno
- np
->Href
>= histlen
|| histlen
== 0)
122 hremove(np
), hfree(np
);
126 while (histCount
> (unsigned)histlen
&& (np
= histTail
) != &Histlist
) {
127 if (eventno
- np
->Href
>= histlen
|| histlen
== 0)
128 hremove(np
), hfree(np
);
132 if (histCount
- (histlen
>> 4) <= (unsigned)histlen
)
133 return; /* don't bother doing the full scan */
134 for (hp
= &Histlist
; histCount
> (unsigned)histlen
&&
135 (np
= hp
->Hnext
) != NULL
;)
136 if (eventno
- np
->Href
>= histlen
|| histlen
== 0)
137 hremove(np
), hfree(np
);
142 /* Add the command "sp" to the history list. */
146 int mflg
) /* true if -m (merge) specified */
151 /* throw away null lines */
152 if (sp
&& sp
->next
->word
[0] == '\n')
154 cp
= varval(STRhistory
);
160 histlen
= histlen
* 10 + *cp
++ - '0';
163 (void) enthist(++eventno
, sp
, 1, mflg
, histlen
);
164 discardExcess(histlen
);
167 #define USE_JENKINS_HASH 1
168 /* #define USE_ONE_AT_A_TIME 1 */
169 #undef PRIME_LENGTH /* no need for good HTL */
171 #ifdef USE_JENKINS_HASH
172 #define hashFcnName "lookup3"
174 lookup3.c, by Bob Jenkins, May 2006, Public Domain.
175 "... You can use this free for any purpose. It's in
176 the public domain. It has no warranty."
177 http://burtleburtle.net/bob/hash/index.html
180 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
183 a -= c; a ^= rot(c, 4); c += b; \
184 b -= a; b ^= rot(a, 6); a += c; \
185 c -= b; c ^= rot(b, 8); b += a; \
186 a -= c; a ^= rot(c,16); c += b; \
187 b -= a; b ^= rot(a,19); a += c; \
188 c -= b; c ^= rot(b, 4); b += a; \
190 #define final(a,b,c) \
192 c ^= b; c -= rot(b,14); \
193 a ^= c; a -= rot(c,11); \
194 b ^= a; b -= rot(a,25); \
195 c ^= b; c -= rot(b,16); \
196 a ^= c; a -= rot(c, 4); \
197 b ^= a; b -= rot(a,14); \
198 c ^= b; c -= rot(b,24); \
201 struct hashValue
/* State used to hash a wordend word list. */
206 /* Set up the internal state */
208 initializeHash(struct hashValue
*h
)
210 h
->a
= h
->b
= h
->c
= 0xdeadbeef;
213 /* This does a partial hash of the Chars in a single word. For efficiency we
214 * include 3 versions of the code to pack Chars into 32-bit words for the
215 * mixing function. */
217 addWordToHash(struct hashValue
*h
, const Char
*word
)
219 uint32_t a
= h
->a
, b
= h
->b
, c
= h
->c
;
222 assert(sizeof(Char
) >= 4);
225 if ((k
= (uChar
)*word
++) == 0) break; a
+= k
;
226 if ((k
= (uChar
)*word
++) == 0) break; b
+= k
;
227 if ((k
= (uChar
)*word
++) == 0) break; c
+= k
;
231 assert(sizeof(Char
) == 2);
234 if ((k
= (uChar
)*word
++) == 0) break; a
+= k
;
235 if ((k
= (uChar
)*word
++) == 0) break; a
+= k
<< 16;
236 if ((k
= (uChar
)*word
++) == 0) break; b
+= k
;
237 if ((k
= (uChar
)*word
++) == 0) break; b
+= k
<< 16;
238 if ((k
= (uChar
)*word
++) == 0) break; c
+= k
;
239 if ((k
= (uChar
)*word
++) == 0) break; c
+= k
<< 16;
244 assert(sizeof(Char
) == 1);
247 if ((k
= *word
++) == 0) break; a
+= k
;
248 if ((k
= *word
++) == 0) break; a
+= k
<< 8;
249 if ((k
= *word
++) == 0) break; a
+= k
<< 16;
250 if ((k
= *word
++) == 0) break; a
+= k
<< 24;
251 if ((k
= *word
++) == 0) break; b
+= k
;
252 if ((k
= *word
++) == 0) break; b
+= k
<< 8;
253 if ((k
= *word
++) == 0) break; b
+= k
<< 16;
254 if ((k
= *word
++) == 0) break; b
+= k
<< 24;
255 if ((k
= *word
++) == 0) break; c
+= k
;
256 if ((k
= *word
++) == 0) break; c
+= k
<< 8;
257 if ((k
= *word
++) == 0) break; c
+= k
<< 16;
258 if ((k
= *word
++) == 0) break; c
+= k
<< 24;
262 h
->a
= a
, h
->b
= b
, h
->c
= c
;
266 addCharToHash(struct hashValue
*h
, Char ch
)
268 /* The compiler (gcc -O2) seems to do a good job optimizing this without
269 * explicitly extracting into local variables. */
271 mix(h
->a
, h
->b
, h
->c
);
275 finalizeHash(struct hashValue
*h
)
277 uint32_t a
= h
->a
, b
= h
->b
, c
= h
->c
;
282 #elif USE_ONE_AT_A_TIME
283 #define hashFcnName "one-at-a-time"
284 /* This one is also from Bob Jenkins, but is slower but simpler than lookup3.
285 "... The code given here are all public domain."
286 http://burtleburtle.net/bob/hash/doobs.html */
290 one_at_a_time(char *key
, ub4 len
)
293 for (hash
=0, i
=0; i
<len
; ++i
)
296 hash
+= (hash
<< 10);
300 hash
^= (hash
>> 11);
301 hash
+= (hash
<< 15);
302 return (hash
& mask
);
306 struct hashValue
{ uint32_t h
; };
308 initializeHash(struct hashValue
*h
)
314 addWordToHash(struct hashValue
*h
, const Char
*word
)
317 uint32_t hash
= h
->h
;
318 while (k
= (uChar
)*word
++)
319 hash
+= k
, hash
+= hash
<< 10, hash
^= hash
>> 6;
324 addCharToHash(struct hashValue
*h
, Char c
)
326 Char b
[2] = { c
, 0 };
331 finalizeHash(struct hashValue
*h
)
333 unsigned hash
= h
->h
;
335 hash
^= (hash
>> 11);
336 hash
+= (hash
<< 15);
341 #define hashFcnName "add-mul"
342 /* Simple multipy and add hash. */
343 #define PRIME_LENGTH 1 /* need "good" HTL */
344 struct hashValue
{ uint32_t h
; };
346 initializeHash(struct hashValue
*h
)
352 addWordToHash(struct hashValue
*h
, const Char
*word
)
355 uint32_t hash
= h
->h
;
356 while (k
= (uChar
)*word
++)
357 hash
= hash
* 0x9e4167b9 + k
;
362 addCharToHash(struct hashValue
*h
, Char c
)
364 h
->h
= h
->h
* 0x9e4167b9 + (uChar
)c
;
368 finalizeHash(struct hashValue
*h
)
375 hashhist(struct wordent
*h0
)
378 struct wordent
*firstWord
= h0
->next
;
379 struct wordent
*h
= firstWord
;
383 for (; h
!= h0
; h
= h
->next
) {
384 if (h
->word
[0] == '\n')
385 break; /* don't hash newline */
387 addCharToHash(&s
, ' '); /* space between words */
388 addWordToHash(&s
, h
->word
);
390 hash
= finalizeHash(&s
);
391 /* Zero means no hash value, so never return zero as a hash value. */
392 return hash
? hash
: 0x7fffffff; /* prime! */
401 addWordToHash(&s
, str
);
402 return finalizeHash(&s
);
406 #ifdef PRIME_LENGTH /* need good HTL */
407 #define hash2tableIndex(hash, len) ((hash) % len)
409 #define hash2tableIndex(hash, len) ((hash) & (len-1))
412 /* This code can be enabled to test the above hash functions for speed and
413 * collision avoidance. The testing is enabled by "occasional" calls to
414 * displayHistStats(), see which. */
419 doTiming(int start
) {
420 static struct timeval beginTime
;
422 gettimeofday(&beginTime
, NULL
);
426 gettimeofday(&now
, NULL
);
427 return (now
.tv_sec
-beginTime
.tv_sec
) +
428 (now
.tv_usec
-beginTime
.tv_usec
)/1e6
;
433 doTiming(int start
) {
440 generateHashes(int nChars
, unsigned nWords
, unsigned samples
, unsigned *hashes
,
445 nWords
= (nWords
< 1) ? 1 : (nWords
> 4) ? 4 : nWords
;
446 Char
*number
= xmalloc((nChars
+nWords
)*sizeof(Char
));
447 struct wordent word
[4];
448 struct wordent base
= { NULL
, &word
[0], &word
[0] };
449 word
[0].word
= number
, word
[0].next
= &base
, word
[0].prev
= &base
;
450 unsigned w
= 0; /* word number */
451 /* Generate multiple words of length 2, 3, 5, then all the rest. */
452 unsigned wBoundaries
[4] = { 2-1, 2+3-1, 2+3+5-1, 0 };
453 /* Ensure the last word has at least 4 Chars in it. */
454 while (nWords
>= 2 && nChars
< (wBoundaries
[nWords
-2]+1) + 4)
456 wBoundaries
[nWords
-1] = 0xffffffff; /* don't end word past this point */
458 for (i
= 0; i
<nChars
; i
++) {
459 /* In deference to the gawd awful add-mul hash, we won't use the worse
460 * case here (setting all Chars to 1), but assume mostly (or at least
461 * initially) ASCII data. */
462 number
[i
+w
] = '!'; /* 0x21 = 33 */
464 if (i
== wBoundaries
[w
]) { /* end a word here and move to next */
466 number
[i
+w
] = 0; /* terminate */
467 word
[w
].word
= &number
[i
+w
+1];
468 word
[w
].next
= &base
, word
[w
].prev
= &word
[w
-1];
469 word
[w
-1].next
= &word
[w
], base
.prev
= &word
[w
];
472 /* w is the index of the last word actually created. */
473 number
[nChars
+ w
] = 0; /* terminate last word */
474 unsigned timeLimit
= !samples
;
476 samples
= 1000000000;
479 for (i
= 0; i
< samples
; i
++) {
480 /* increment 4 digit base 255 number; last characters vary fastest */
481 unsigned j
= nChars
-1 + w
;
483 if (++number
[j
] != 0)
485 /* else reset this digit and proceed to next one */
487 if (&number
[j
] <= word
[w
].word
)
488 break; /* stop at beginning of last word */
491 if (word
[w
].word
[0] == '\n')
492 word
[w
].word
[0]++; /* suppress newline character */
493 unsigned hash
= hashhist(&base
);
494 hashes
[hash2tableIndex(hash
, length
)]++;
495 if (timeLimit
&& (i
& 0x3ffff) == 0x3ffff) {
504 samples
= i
; /* number we actually did */
506 xprintf("Hash %d (%d Char %u words) with %s: %d nsec/hash, %d mcps\n",
507 samples
, nChars
, w
+1, hashFcnName
, (int)((sec
/samples
)*1e9
),
508 (int)((double)samples
*nChars
/sec
/1e6
));
511 #endif /* DEBUG_HIST */
517 static const Char STRtestHashTimings
[] =
518 { 't','e','s','t','H','a','s','h','T','i','m','i','n','g','s', 0 };
519 struct varent
*vp
= adrof(STRtestHashTimings
);
521 unsigned hashes
[4]; /* dummy place to put hashes */
522 Char
**vals
= vp
->vec
;
524 int length
= getn(*vals
);
526 (length
< 5) ? 1 : (length
< 25) ? 2 : (length
< 75) ? 3 : 4;
528 generateHashes(length
, words
, 0, hashes
, 4);
532 unsigned length
= 1024;
533 #ifdef PRIME_LENGTH /* need good HTL */
536 unsigned *hashes
= xmalloc(length
*sizeof(unsigned));
537 memset(hashes
, 0, length
*sizeof(unsigned));
538 /* Compute collision statistics for half full hashes modulo "length". */
539 generateHashes(4, 1, length
/2, hashes
, length
);
540 /* Evaluate collisions by comparing occupancy rates (mean value 0.5).
541 * One bin for each number of hits. */
543 memset(bins
, 0, sizeof(bins
));
544 unsigned highest
= 0;
546 for (i
= 0; i
<length
; i
++) {
547 unsigned hits
= hashes
[i
];
548 if (hits
>= sizeof(bins
)/sizeof(bins
[0])) /* clip */
549 hits
= highest
= sizeof(bins
)/sizeof(bins
[0]) - 1;
554 xprintf("Occupancy of %d buckets by %d hashes %d Chars %d word with %s\n",
555 length
, length
/2, 4, 1, hashFcnName
);
556 for (i
= 0; i
<= highest
; i
++) {
557 xprintf(" %d buckets (%d%%) with %d hits\n",
558 bins
[i
], bins
[i
]*100/length
, i
);
560 /* Count run lengths to evaluate linear rehashing effectiveness. Estimate
561 * a little corrupted by edge effects. */
562 memset(bins
, 0, sizeof(bins
));
564 for (i
= 0; hashes
[i
] == 0; i
++); /* find first occupied bucket */
566 unsigned rehashed
= 0;
567 for (; i
<length
; i
++) {
568 unsigned hits
= hashes
[i
];
569 if (hits
== 0 && rehashed
> 0)
570 hits
= 1 && rehashed
--;
576 /* a real free slot, count it */
577 if (run
>= sizeof(bins
)/sizeof(bins
[0])) /* clip */
578 run
= highest
= sizeof(bins
)/sizeof(bins
[0]) - 1;
585 /* Ignore the partial run at end as we ignored the beginning. */
586 double merit
= 0.0, entries
= 0;
587 for (i
= 0; i
<= highest
; i
++) {
588 entries
+= bins
[i
]*i
; /* total hashed objects */
589 merit
+= bins
[i
]*i
*i
;
591 xprintf("Rehash collision figure of merit %u (ideal=100), run lengths:\n",
592 (int)(100.0*merit
/entries
));
593 for (i
= 0; i
<= highest
; i
++) {
595 xprintf(" %d runs of length %d buckets\n", bins
[i
], i
);
599 #endif /* DEBUG_HIST */
601 /* Compares two word lists for equality. */
603 heq(const struct wordent
*a0
, const struct wordent
*b0
)
605 const struct wordent
*a
= a0
->next
, *b
= b0
->next
;
608 if (Strcmp(a
->word
, b
->word
) != 0)
613 return (b
== b0
) ? 1 : 0;
619 /* Renumber entries following p, which we will be deleting. */
621 renumberHist(struct Hist
*p
)
624 while ((p
= p
->Hnext
))
628 /* The hash table is implemented as an array of pointers to Hist entries. Each
629 * entry is located in the table using hash2tableIndex() and checking the
630 * following entries in case of a collision (linear rehash). Free entries in
631 * the table are zero (0, NULL, emptyHTE). Deleted entries that cannot yet be
632 * freed are set to one (deletedHTE). The Hist.Hhash member is non-zero iff
633 * the entry is in the hash table. When the hash table get too full, it is
634 * reallocated to be approximately twice the history length (see
635 * getHashTableSize). */
636 static struct Hist
**histHashTable
= NULL
;
637 static unsigned histHashTableLength
= 0; /* number of Hist pointers in table */
639 static struct Hist
* const emptyHTE
= NULL
;
640 static struct Hist
* const deletedHTE
= (struct Hist
*)1;
643 unsigned insertCount
;
644 unsigned removeCount
;
651 checkHistHashTable(int print
)
653 unsigned occupied
= 0;
654 unsigned deleted
= 0;
656 for (i
= 0; i
<histHashTableLength
; i
++)
657 if (histHashTable
[i
] == emptyHTE
)
659 else if (histHashTable
[i
] == deletedHTE
)
664 xprintf(" found len %u occupied %u deleted %u\n",
665 histHashTableLength
, occupied
, deleted
);
666 assert(deleted
== hashStats
.deleted
);
669 static int doneTest
= 0;
671 /* Main entry point for displaying history statistics and hash function
674 displayHistStats(const char *reason
)
676 /* Just hash statistics for now. */
677 xprintf("%s history hash table len %u count %u (deleted %d)\n", reason
,
678 histHashTableLength
, histCount
, hashStats
.deleted
);
679 xprintf(" inserts %u rehashes %u%% each\n",
680 hashStats
.insertCount
,
681 (hashStats
.insertCount
682 ? 100*hashStats
.rehashes
/hashStats
.insertCount
: 0));
683 xprintf(" removes %u net %u\n",
684 hashStats
.removeCount
,
685 hashStats
.insertCount
- hashStats
.removeCount
);
686 assert(hashStats
.insertCount
>= hashStats
.removeCount
);
687 checkHistHashTable(1);
688 memset(&hashStats
, 0, sizeof(hashStats
));
696 displayHistStats(const char *reason
)
703 discardHistHashTable(void)
705 if (histHashTable
== NULL
)
707 displayHistStats("Discarding");
708 xfree(histHashTable
);
709 histHashTable
= NULL
;
712 /* Computes a new hash table size, when the current one is too small. */
714 getHashTableSize(int histlen
)
716 unsigned target
= histlen
* 2;
719 while ((size
= 1<<e
) < target
)
721 #ifdef PRIME_LENGTH /* need good HTL */
722 /* Not all prime, but most are and none have factors smaller than 11. */
725 assert((size
& (size
-1)) == 0); /* must be a power of two */
730 /* Create the hash table or resize, if necessary. */
732 createHistHashTable(int histlen
)
735 discardHistHashTable();
739 histlen
= getn(varval(STRhistory
));
741 return; /* no need for hash table */
744 if (histHashTable
!= NULL
) {
745 if (histCount
< histHashTableLength
* 3 / 4)
746 return; /* good enough for now */
747 discardHistHashTable(); /* too small */
749 histHashTableLength
= getHashTableSize(
750 histlen
> (int)histCount
? histlen
: (int)histCount
);
751 histHashTable
= xmalloc(histHashTableLength
* sizeof(struct Hist
*));
752 memset(histHashTable
, 0, histHashTableLength
* sizeof(struct Hist
*));
753 assert(histHashTable
[0] == emptyHTE
);
755 /* Now insert all the entries on the history list into the hash table. */
758 for (hp
= &Histlist
; (hp
= hp
->Hnext
) != NULL
;) {
759 unsigned lpHash
= hashhist(&hp
->Hlex
);
760 assert(!hp
->Hhash
|| hp
->Hhash
== lpHash
);
761 hp
->Hhash
= 0; /* force insert to new hash table */
762 insertHistHashTable(hp
, lpHash
);
767 /* Insert np into the hash table. We assume that np is already on the
768 * Histlist. The specified hashval matches the new Hist entry but has not yet
769 * been assigned to Hhash (or the element is already on the hash table). */
771 insertHistHashTable(struct Hist
*np
, unsigned hashval
)
773 unsigned rehashes
= 0;
777 if (np
->Hhash
!= 0) {
778 /* already in hash table */
779 assert(hashval
== np
->Hhash
);
782 assert(np
!= deletedHTE
);
783 /* Find a free (empty or deleted) slot, using linear rehash. */
784 assert(histHashTable
);
786 ((hi
= hash2tableIndex(hashval
+ rehashes
, histHashTableLength
)),
787 histHashTable
[hi
] != emptyHTE
&& histHashTable
[hi
] != deletedHTE
);
789 assert(np
!= histHashTable
[hi
]);
790 if (rehashes
>= histHashTableLength
/ 10) {
791 /* Hash table is full, so grow it. We assume the create function
792 * will roughly double the size we give it. Create initializes the
793 * new table with everything on the Histlist, so we are done when
796 xprintf("Growing history hash table from %d ...",
797 histHashTableLength
);
800 discardHistHashTable();
801 createHistHashTable(histHashTableLength
);
803 xprintf("to %d.\n", histHashTableLength
);
808 /* Might be sensible to grow hash table if rehashes is "too big" here. */
809 if (histHashTable
[hi
] == deletedHTE
)
811 histHashTable
[hi
] = np
;
813 hashStats
.insertCount
++;
814 hashStats
.rehashes
+= rehashes
;
817 /* Remove the 'np' entry from the hash table. */
819 removeHistHashTable(struct Hist
*np
)
821 unsigned hi
= np
->Hhash
;
822 if (!histHashTable
|| !hi
)
823 return; /* no hash table or not on it */
824 /* find desired entry */
825 while ((hi
= hash2tableIndex(hi
, histHashTableLength
)),
826 histHashTable
[hi
] != emptyHTE
) {
827 if (np
== histHashTable
[hi
]) {
829 unsigned deletes
= 0;
830 histHashTable
[hi
] = deletedHTE
; /* dummy, but non-zero entry */
831 /* now peek ahead to see if the dummies are really necessary. */
833 while (histHashTable
[hash2tableIndex(hi
+i
, histHashTableLength
)] ==
836 if (histHashTable
[hash2tableIndex(hi
+i
, histHashTableLength
)] ==
838 /* dummies are no longer necessary placeholders. */
841 histHashTable
[hash2tableIndex(hi
+i
, histHashTableLength
)] =
845 hashStats
.deleted
+= 1 - deletes
; /* delta deleted entries */
846 hashStats
.removeCount
++;
849 hi
++; /* linear rehash */
851 assert(!"Hist entry not found in hash table");
854 /* Search the history hash table for a command matching lp, using hashval as
857 findHistHashTable(struct wordent
*lp
, unsigned hashval
)
859 unsigned deleted
= 0; /* number of deleted entries skipped */
860 unsigned hi
= hashval
;
864 while ((hi
= hash2tableIndex(hi
, histHashTableLength
)),
865 (hp
= histHashTable
[hi
]) != emptyHTE
) {
866 if (hp
== deletedHTE
)
868 else if (hp
->Hhash
== hashval
&& heq(lp
, &(hp
->Hlex
)))
870 if (deleted
> (histHashTableLength
>>4)) {
871 /* lots of deletes, so we need a sparser table. */
872 discardHistHashTable();
873 createHistHashTable(histHashTableLength
);
874 return findHistHashTable(lp
, hashval
);
876 hi
++; /* linear rehash */
881 /* When merge semantics are in use, find the approximate predecessor for the
882 * new entry, so that the Htime entries are decreasing. Return the entry just
883 * before the first entry with equal times, so the caller can check for
884 * duplicates. When pTime is not NULL, use it as a starting point for search,
885 * otherwise search from beginning (largest time value) of history list. */
886 PG_STATIC
struct Hist
*
888 struct Hist
*np
, /* new entry to be inserted */
889 struct Hist
*pTime
) /* hint about where to insert */
892 if (histTail
&& histTail
->Htime
>= np
->Htime
)
893 pTime
= histTail
; /* new entry goes at the end */
894 if (histMerg
&& histMerg
!= &Histlist
&& histMerg
!= Histlist
.Hnext
) {
895 /* Check above and below previous insertion point, in case we're adding
896 * sequential times in the middle of the list (e.g. history -M). */
897 if (histMerg
->Htime
>= np
->Htime
)
899 else if (histMerg
->Hprev
->Htime
>= np
->Htime
)
900 pTime
= histMerg
->Hprev
;
903 /* With hint, search up the list until Htime is greater. We skip past
904 * the equal ones, too, so our caller can elide duplicates. */
906 while (pp
!= &Histlist
&& pp
->Htime
<= np
->Htime
)
910 /* Search down the list while current entry's time is too large. */
911 while ((p
= pp
->Hnext
) && (p
->Htime
> np
->Htime
))
912 pp
= p
; /* advance insertion point */
913 /* Remember recent position as hint for next time */
918 /* Bubble Hnum & Href in new entry down to pp through earlier part of list. */
919 PG_STATIC
void bubbleHnumHrefDown(struct Hist
*np
, struct Hist
*pp
)
922 for (p
= Histlist
.Hnext
; p
!= pp
->Hnext
; p
= p
->Hnext
) {
923 /* swap Hnum & Href values of p and np. */
924 int n
= p
->Hnum
, r
= p
->Href
;
925 p
->Hnum
= np
->Hnum
; p
->Href
= np
->Href
;
926 np
->Hnum
= n
; np
->Href
= r
;
930 /* Enter new command into the history list according to current settings. */
933 int event
, /* newly incremented global eventno */
936 int mflg
, /* true if merge requested */
937 int histlen
) /* -1 if unknown */
939 struct Hist
*p
= NULL
, *pp
= &Histlist
, *pTime
= NULL
;
942 unsigned lpHash
= 0; /* non-zero if hashing entries */
944 if ((dp
= varval(STRhistdup
)) != STRNULL
) {
945 if (eq(dp
, STRerase
)) {
946 /* masaoki@akebono.tky.hp.com (Kobayashi Masaoki) */
947 createHistHashTable(histlen
);
948 lpHash
= hashhist(lp
);
950 p
= findHistHashTable(lp
, lpHash
);
952 if (Htime
!= 0 && p
->Htime
> Htime
)
954 /* If we are merging, and the old entry is at the place we want
955 * to insert the new entry, then remember the place. */
956 if (mflg
&& Htime
!= 0 && p
->Hprev
->Htime
>= Htime
)
959 renumberHist(p
); /* Reset Href of subsequent entries */
962 p
= NULL
; /* so new entry is allocated below */
965 else if (eq(dp
, STRall
)) {
966 createHistHashTable(histlen
);
967 lpHash
= hashhist(lp
);
969 p
= findHistHashTable(lp
, lpHash
);
970 if (p
) /* p!=NULL, only update this entry's Htime below */
971 eventno
--; /* not adding a new event */
973 else if (eq(dp
, STRprev
)) {
974 if (pp
->Hnext
&& heq(lp
, &(pp
->Hnext
->Hlex
))) {
981 np
= p
? p
: xmalloc(sizeof(*np
));
983 /* Pick up timestamp set by lex() in Htime if reading saved history */
989 (void) time(&(np
->Htime
));
992 return np
; /* reused existing entry */
994 /* Initialize the new entry. */
995 np
->Hnum
= np
->Href
= event
;
997 copylex(&np
->Hlex
, lp
);
999 np
->histline
= Strsave(histline
.s
);
1001 np
->histline
= NULL
;
1004 np
->Hlex
.next
= lp
->next
;
1005 lp
->next
->prev
= &np
->Hlex
;
1006 np
->Hlex
.prev
= lp
->prev
;
1007 lp
->prev
->next
= &np
->Hlex
;
1008 np
->histline
= NULL
;
1012 /* The head of history list is the default insertion point.
1013 If merging, advance insertion point, in pp, according to Htime. */
1014 /* XXX -- In histdup=all, Htime values can be non-monotonic. */
1015 if (mflg
) { /* merge according to np->Htime */
1016 pp
= mergeInsertionPoint(np
, pTime
);
1017 for (p
= pp
->Hnext
; p
&& p
->Htime
== np
->Htime
; pp
= p
, p
= p
->Hnext
) {
1018 if (heq(&p
->Hlex
, &np
->Hlex
)) {
1019 eventno
--; /* duplicate, so don't add new event */
1024 /* pp is now the last entry with time >= to np. */
1025 if (!fastMergeErase
) { /* renumber at end of loadhist */
1026 /* Before inserting np after pp, bubble its Hnum & Href values down
1027 * through the earlier part of list. */
1028 bubbleHnumHrefDown(np
, pp
);
1032 pp
= &Histlist
; /* insert at beginning of history */
1034 if (lpHash
&& histlen
!= 0) /* erase & all modes use hash table */
1035 insertHistHashTable(np
, lpHash
);
1037 discardHistHashTable();
1042 hfree(struct Hist
*hp
)
1044 assert(hp
!= histMerg
);
1046 removeHistHashTable(hp
);
1049 xfree(hp
->histline
);
1054 phist(struct Hist
*hp
, int hflg
)
1056 if (hflg
& HIST_ONLY
) {
1060 * Control characters have to be written as is (output_raw).
1061 * This way one can preserve special characters (like tab) in
1063 * From: mveksler@vnet.ibm.com (Veksler Michael)
1065 old_output_raw
= output_raw
;
1067 cleanup_push(&old_output_raw
, output_raw_restore
);
1068 if (hflg
& HIST_TIME
)
1070 * Make file entry with history time in format:
1071 * "+NNNNNNNNNN" (10 digits, left padded with ascii '0')
1074 xprintf("#+%010lu\n", (unsigned long)hp
->Htime
);
1076 if (HistLit
&& hp
->histline
)
1077 xprintf("%S\n", hp
->histline
);
1080 cleanup_until(&old_output_raw
);
1083 Char
*cp
= str2short("%h\t%T\t%R\n");
1085 struct varent
*vp
= adrof(STRhistory
);
1087 if (vp
&& vp
->vec
!= NULL
&& vp
->vec
[0] && vp
->vec
[1])
1090 p
= tprintf(FMT_HISTORY
, cp
, NULL
, hp
->Htime
, hp
);
1091 cleanup_push(p
, xfree
);
1099 dophist(int n
, int hflg
)
1103 int old_pintr_disabled
;
1105 pintr_push_enable(&old_pintr_disabled
);
1106 cleanup_until(&old_pintr_disabled
);
1108 if ((hflg
& HIST_REV
) == 0) {
1109 /* Since the history list is stored most recent first, non-reversing
1110 * print needs to print (backwards) up the list. */
1111 if ((unsigned)n
>= histCount
)
1114 for (hp
= Histlist
.Hnext
;
1115 --n
> 0 && hp
->Hnext
!= NULL
;
1120 return; /* nothing to print */
1121 for (; hp
!= &Histlist
; hp
= hp
->Hprev
)
1124 for (hp
= Histlist
.Hnext
; n
-- > 0 && hp
!= NULL
; hp
= hp
->Hnext
)
1131 dohist(Char
**vp
, struct command
*c
)
1136 if (getn(varval(STRhistory
)) == 0)
1138 while (*++vp
&& **vp
== '-') {
1165 stderror(ERR_HISTUS
, "chrSLMT");
1169 if (hflg
& HIST_CLEAR
) {
1170 struct Hist
*np
, *hp
;
1171 for (hp
= &Histlist
; (np
= hp
->Hnext
) != NULL
;)
1172 hremove(np
), hfree(np
);
1175 if (hflg
& (HIST_LOAD
| HIST_MERGE
))
1176 loadhist(*vp
, (hflg
& HIST_MERGE
) ? 1 : 0);
1177 else if (hflg
& HIST_SAVE
)
1183 n
= getn(varval(STRhistory
));
1191 fmthist(int fmt
, ptr_t ptr
)
1193 struct Hist
*hp
= ptr
;
1198 return xasprintf("%6d", hp
->Hnum
);
1200 if (HistLit
&& hp
->histline
)
1201 return xasprintf("%S", hp
->histline
);
1206 istr
= sprlex(&hp
->Hlex
);
1207 buf
= xmalloc(Strlen(istr
) * MB_LEN_MAX
+ 1);
1209 for (p
= buf
, ip
= istr
; *ip
!= '\0'; ip
++)
1210 p
+= one_wctomb(p
, CHAR
& *ip
);
1223 /* Save history before exiting the shell. */
1225 rechist(Char
*fname
, int ref
)
1228 int fp
, ftmp
, oldidfds
;
1229 struct varent
*shist
;
1230 static Char
*dumphist
[] = {STRhistory
, STRmhT
, 0, 0};
1232 if (fname
== NULL
&& !ref
)
1235 * If $savehist is just set, we use the value of $history
1236 * else we use the value in $savehist
1238 if (((snum
= varval(STRsavehist
)) == STRNULL
) &&
1239 ((snum
= varval(STRhistory
)) == STRNULL
))
1243 if (fname
== NULL
) {
1244 if ((fname
= varval(STRhistfile
)) == STRNULL
)
1245 fname
= Strspl(varval(STRhome
), &STRtildothist
[1]);
1247 fname
= Strsave(fname
);
1250 fname
= globone(fname
, G_ERROR
);
1251 cleanup_push(fname
, xfree
);
1254 * The 'savehist merge' feature is intended for an environment
1255 * with numerous shells being in simultaneous use. Imagine
1256 * any kind of window system. All these shells 'share' the same
1257 * ~/.history file for recording their command line history.
1258 * Currently the automatic merge can only succeed when the shells
1259 * nicely quit one after another.
1261 * Users that like to nuke their environment require here an atomic
1262 * loadhist-creat-dohist(dumphist)-close
1268 * We need the didfds stuff before loadhist otherwise
1269 * exec in a script will fail to print if merge is set.
1270 * From: mveksler@iil.intel.com (Veksler Michael)
1274 if ((shist
= adrof(STRsavehist
)) != NULL
&& shist
->vec
!= NULL
)
1275 if (shist
->vec
[1] && eq(shist
->vec
[1], STRmerge
))
1278 fp
= xcreat(short2str(fname
), 0600);
1279 cleanup_until(fname
);
1287 dohist(dumphist
, NULL
);
1294 /* This is the entry point for loading history data from a file. */
1296 loadhist(Char
*fname
, int mflg
)
1298 static Char
*loadhist_cmd
[] = {STRsource
, NULL
, NULL
, NULL
};
1299 loadhist_cmd
[1] = mflg
? STRmm
: STRmh
;
1302 loadhist_cmd
[2] = fname
;
1303 else if ((fname
= varval(STRhistfile
)) != STRNULL
)
1304 loadhist_cmd
[2] = fname
;
1306 loadhist_cmd
[2] = STRtildothist
;
1308 dosource(loadhist_cmd
, NULL
);
1310 /* During history merging (enthist sees mflg set), we disable management of
1311 * Hnum and Href (because fastMergeErase is true). So now reset all the
1312 * values based on the final ordering of the history list. */
1315 struct Hist
*hp
= &Histlist
;
1316 while ((hp
= hp
->Hnext
))
1317 hp
->Hnum
= hp
->Href
= n
--;