]> git.ipfire.org Git - thirdparty/bash.git/blame_incremental - lib/readline/history.c
Bash-5.3 distribution sources and documentation
[thirdparty/bash.git] / lib / readline / history.c
... / ...
CommitLineData
1/* history.c -- standalone history library */
2
3/* Copyright (C) 1989-2025 Free Software Foundation, Inc.
4
5 This file contains the GNU History Library (History), a set of
6 routines for managing the text of previously typed lines.
7
8 History is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 History is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with History. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22/* The goal is to make the implementation transparent, so that you
23 don't have to know what data types are used, just what functions
24 you can call. I think I have done that. */
25#define READLINE_LIBRARY
26
27#if defined (HAVE_CONFIG_H)
28# include <config.h>
29#endif
30
31#include <stdio.h>
32
33#if defined (HAVE_STDLIB_H)
34# include <stdlib.h>
35#else
36# include "ansi_stdlib.h"
37#endif /* HAVE_STDLIB_H */
38
39#if defined (HAVE_UNISTD_H)
40# ifdef _MINIX
41# include <sys/types.h>
42# endif
43# include <unistd.h>
44#endif
45#include "posixtime.h"
46
47#include <errno.h>
48
49#include "history.h"
50#include "histlib.h"
51
52#include "xmalloc.h"
53
54#if !defined (errno)
55extern int errno;
56#endif
57
58/* How big to make the_history when we first allocate it. */
59#define DEFAULT_HISTORY_INITIAL_SIZE 502
60
61#define MAX_HISTORY_INITIAL_SIZE 8192
62
63/* The number of slots to increase the_history by. */
64#define DEFAULT_HISTORY_GROW_SIZE 256
65
66static char *hist_inittime (void);
67
68static int history_list_grow_size (void);
69static void history_list_resize (int); /* XXX - size_t? */
70static void advance_history (void);
71
72/* **************************************************************** */
73/* */
74/* History Functions */
75/* */
76/* **************************************************************** */
77
78/* An array of HIST_ENTRY. This is where we store the history. the_history is
79 a roving pointer somewhere into this, so the user-visible history list is
80 a window into real_history starting at the_history and extending
81 history_length entries. */
82static HIST_ENTRY **real_history = (HIST_ENTRY **)NULL;
83
84/* The current number of slots allocated to the input_history. */
85static int real_history_size = 0;
86
87/* A pointer to somewhere in real_history, where the user-visible history
88 starts. */
89static HIST_ENTRY **the_history = (HIST_ENTRY **)NULL;
90
91/* Non-zero means that we have enforced a limit on the amount of
92 history that we save. */
93static int history_stifled;
94
95/* The number of history entries available for user use, starting at the_history.
96 real_history_size - history_size == the_history - real_history */
97static int history_size;
98
99/* If HISTORY_STIFLED is non-zero, then this is the maximum number of
100 entries to remember. */
101int history_max_entries;
102int max_input_history; /* backwards compatibility */
103
104/* The current location of the interactive history pointer. Just makes
105 life easier for outside callers. */
106int history_offset;
107
108/* The number of strings currently stored in the history list. This is
109 always <= real_history_length */
110int history_length;
111
112/* The logical `base' of the history array. It defaults to 1. */
113int history_base = 1;
114
115/* Compute the number of bits required to store a given nonnegative integer.
116
117 NOTE: _bit_length(0) == 0 */
118static inline unsigned
119_bit_length(unsigned n)
120{
121 /* This implementation is for simplicity, not for performance, but it is
122 fast enough for our purposes here. */
123 unsigned count = 0;
124 while (n)
125 {
126 n >>= 1;
127 count++;
128 }
129 return count;
130}
131
132/* Compute a grow size that adjusts to the size of the history.
133
134 We aim to grow by roughly sqrt(history_size) in order to amortize the cost of
135 realloc() and memmove(). This reduces the total cost of the memmoves from
136 O(N^2) to O(N*sqrt(N)). */
137static int
138history_list_grow_size (void)
139{
140 int width, r;
141 /* Handle small positive values and guard against history_length accidentally
142 being negative. */
143 const int MIN_BITS = 10;
144 if (history_length < (1 << MIN_BITS))
145 return DEFAULT_HISTORY_GROW_SIZE;
146
147 /* For other values, we just want something easy to compute that grows roughly
148 as sqrt(N), where N=history_length. We use approximately 2^((k+1)/2),
149 where k is the bit length of N. This bounds the value between sqrt(2N) and
150 2*sqrt(N). */
151 width = MIN_BITS + _bit_length(history_length >> MIN_BITS);
152
153 /* If width is odd then this is 2^((width+1)/2). An even width gives a value
154 of 3*2^((width-2)/2) ~ 1.06*2^((width+1)/2). */
155 r = (1 << (width / 2)) + (1 << ((width - 1) / 2));
156
157 /* Make sure we always expand the list by at least DEFAULT_HISTORY_GROW_SIZE */
158 return ((r < DEFAULT_HISTORY_GROW_SIZE) ? DEFAULT_HISTORY_GROW_SIZE : r);
159}
160
161/* Return the current HISTORY_STATE of the history. */
162HISTORY_STATE *
163history_get_history_state (void)
164{
165 HISTORY_STATE *state;
166
167 state = (HISTORY_STATE *)xmalloc (sizeof (HISTORY_STATE));
168 state->entries = the_history;
169 state->offset = history_offset;
170 state->length = history_length;
171 state->size = history_size;
172 state->flags = 0;
173 if (history_stifled)
174 state->flags |= HS_STIFLED;
175
176 return (state);
177}
178
179/* Set the state of the current history array to STATE. */
180void
181history_set_history_state (HISTORY_STATE *state)
182{
183 the_history = state->entries;
184 history_offset = state->offset;
185 history_length = state->length;
186 history_size = state->size;
187 if (state->flags & HS_STIFLED)
188 history_stifled = 1;
189}
190
191/* Begin a session in which the history functions might be used. This
192 initializes interactive variables. */
193void
194using_history (void)
195{
196 history_offset = history_length;
197}
198
199/* Return the number of bytes that the primary history entries are using.
200 This just adds up the lengths of the_history->lines and the associated
201 timestamps. */
202int
203history_total_bytes (void)
204{
205 int i, result;
206
207 for (i = result = 0; the_history && the_history[i]; i++)
208 result += HISTENT_BYTES (the_history[i]);
209
210 return (result);
211}
212
213/* Returns the magic number which says what history element we are
214 looking at now. In this implementation, it returns history_offset. */
215int
216where_history (void)
217{
218 return (history_offset);
219}
220
221/* Make the current history item be the one at POS, an absolute index.
222 Returns zero if POS is out of range, else non-zero. */
223int
224history_set_pos (int pos)
225{
226 if (pos > history_length || pos < 0 || !the_history)
227 return (0);
228 history_offset = pos;
229 return (1);
230}
231
232/* Are we currently at the end of the history list? */
233int
234_hs_at_end_of_history (void)
235{
236 return (the_history == 0 || history_offset == history_length);
237}
238
239/* Return the current history array. The caller has to be careful, since this
240 is the actual array of data, and could be bashed or made corrupt easily.
241 The array is terminated with a NULL pointer. */
242HIST_ENTRY **
243history_list (void)
244{
245 return (the_history);
246}
247
248/* Return the history entry at the current position, as determined by
249 history_offset. If there is no entry there, return a NULL pointer. */
250HIST_ENTRY *
251current_history (void)
252{
253 return ((history_offset == history_length) || the_history == 0)
254 ? (HIST_ENTRY *)NULL
255 : the_history[history_offset];
256}
257
258/* Back up history_offset to the previous history entry, and return
259 a pointer to that entry. If there is no previous entry then return
260 a NULL pointer. */
261HIST_ENTRY *
262previous_history (void)
263{
264 return history_offset ? the_history[--history_offset] : (HIST_ENTRY *)NULL;
265}
266
267/* Move history_offset forward to the next history entry, and return
268 a pointer to that entry. If there is no next entry then return a
269 NULL pointer. */
270HIST_ENTRY *
271next_history (void)
272{
273 return (history_offset == history_length) ? (HIST_ENTRY *)NULL : the_history[++history_offset];
274}
275
276/* Return the history entry which is logically at OFFSET in the history array.
277 OFFSET is relative to history_base. */
278HIST_ENTRY *
279history_get (int offset)
280{
281 int local_index;
282
283 local_index = offset - history_base;
284 return (local_index >= history_length || local_index < 0 || the_history == 0)
285 ? (HIST_ENTRY *)NULL
286 : the_history[local_index];
287}
288
289HIST_ENTRY *
290alloc_history_entry (char *string, char *ts)
291{
292 HIST_ENTRY *temp;
293
294 temp = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
295
296 temp->line = string ? savestring (string) : string;
297 temp->data = (char *)NULL;
298 temp->timestamp = ts;
299
300 return temp;
301}
302
303time_t
304history_get_time (HIST_ENTRY *hist)
305{
306 char *ts;
307 time_t t;
308
309 if (hist == 0 || hist->timestamp == 0)
310 return 0;
311 ts = hist->timestamp;
312 if (ts[0] != history_comment_char)
313 return 0;
314 errno = 0;
315 t = (time_t) strtol (ts + 1, (char **)NULL, 10); /* XXX - should use strtol() here */
316 if (errno == ERANGE)
317 return (time_t)0;
318 return t;
319}
320
321static char *
322hist_inittime (void)
323{
324 time_t t;
325 char ts[64], *ret;
326
327 t = getnow ();
328#if defined (HAVE_VSNPRINTF) /* assume snprintf if vsnprintf exists */
329 snprintf (ts, sizeof (ts) - 1, "X%lu", (unsigned long) t);
330#else
331 sprintf (ts, "X%lu", (unsigned long) t);
332#endif
333 ret = savestring (ts);
334 ret[0] = history_comment_char;
335
336 return ret;
337}
338
339/* Ensure space for new history entries by resetting the start pointer
340 (the_history) and resizing real_history if necessary. */
341static void
342history_list_resize (int new_size)
343{
344 /* Do nothing there is already enough user space. history_length is always <=
345 real_history_size */
346 if (new_size < history_length)
347 return;
348
349 /* If we need to, reset the_history to the start of real_history and
350 start over. */
351 if (the_history != real_history)
352 memmove (real_history, the_history, history_length * sizeof (HIST_ENTRY *));
353
354 /* Don't bother if real_history_size is already big enough, since at this
355 point the_history == real_history and we will set history_size to
356 real_history_size. We don't shrink the history list. */
357 if (new_size > real_history_size)
358 {
359 real_history = (HIST_ENTRY **) xrealloc (real_history, new_size * sizeof (HIST_ENTRY *));
360 real_history_size = new_size;
361 }
362 the_history = real_history;
363 history_size = real_history_size;
364
365 if (history_size > history_length)
366 memset (real_history + history_length, 0, (history_size - history_length) * sizeof (HIST_ENTRY *));
367}
368
369static void
370advance_history (void)
371{
372 /* Advance 'the_history' pointer to simulate dropping the first entry. */
373 the_history++;
374 history_size--;
375
376 /* If full, move all the entries (and trailing NULL) to the beginning. */
377 if (history_length == history_size)
378 history_list_resize (history_length + history_list_grow_size ());
379}
380
381/* Place STRING at the end of the history list. The data field
382 is set to NULL. */
383void
384add_history (const char *string)
385{
386 HIST_ENTRY *temp;
387 int new_length;
388
389 if (history_stifled && (history_length == history_max_entries))
390 {
391 /* If the history is stifled, and history_length is zero,
392 and it equals history_max_entries, we don't save items. */
393 if (history_length == 0)
394 return;
395
396 /* If there is something in the slot, then remove it. */
397 if (the_history[0])
398 (void) free_history_entry (the_history[0]);
399
400 /* Advance the pointer into real_history, resizing if necessary. */
401 advance_history ();
402
403 new_length = history_length;
404 history_base++;
405 }
406 else
407 {
408 if (history_size == 0)
409 {
410 int initial_size;
411 if (history_stifled && history_max_entries > 0)
412 initial_size = (history_max_entries > MAX_HISTORY_INITIAL_SIZE)
413 ? MAX_HISTORY_INITIAL_SIZE
414 : history_max_entries + 2;
415 else
416 initial_size = DEFAULT_HISTORY_INITIAL_SIZE;
417 history_list_resize (initial_size);
418 new_length = 1;
419 }
420 else
421 {
422 if (history_length == (history_size - 1))
423 history_list_resize (real_history_size + history_list_grow_size ());
424 new_length = history_length + 1;
425 }
426 }
427
428 temp = alloc_history_entry ((char *)string, hist_inittime ());
429
430 the_history[new_length] = (HIST_ENTRY *)NULL;
431 the_history[new_length - 1] = temp;
432 history_length = new_length;
433}
434
435/* Change the time stamp of the most recent history entry to STRING. */
436void
437add_history_time (const char *string)
438{
439 HIST_ENTRY *hs;
440
441 if (string == 0 || history_length < 1)
442 return;
443 hs = the_history[history_length - 1];
444 FREE (hs->timestamp);
445 hs->timestamp = savestring (string);
446}
447
448/* Free HIST and return the data so the calling application can free it
449 if necessary and desired. */
450histdata_t
451free_history_entry (HIST_ENTRY *hist)
452{
453 histdata_t x;
454
455 if (hist == 0)
456 return ((histdata_t) 0);
457 FREE (hist->line);
458 FREE (hist->timestamp);
459 x = hist->data;
460 xfree (hist);
461 return (x);
462}
463
464HIST_ENTRY *
465copy_history_entry (HIST_ENTRY *hist)
466{
467 HIST_ENTRY *ret;
468 char *ts;
469
470 if (hist == 0)
471 return hist;
472
473 ret = alloc_history_entry (hist->line, (char *)NULL);
474
475 ts = hist->timestamp ? savestring (hist->timestamp) : hist->timestamp;
476 ret->timestamp = ts;
477
478 ret->data = hist->data;
479
480 return ret;
481}
482
483/* Make the history entry at WHICH have LINE and DATA. This returns
484 the old entry so you can dispose of the data. In the case of an
485 invalid WHICH, a NULL pointer is returned. */
486HIST_ENTRY *
487replace_history_entry (int which, const char *line, histdata_t data)
488{
489 HIST_ENTRY *temp, *old_value;
490
491 if (which < 0 || which >= history_length)
492 return ((HIST_ENTRY *)NULL);
493
494 temp = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
495 old_value = the_history[which];
496
497 temp->line = savestring (line);
498 temp->data = data;
499 temp->timestamp = old_value->timestamp ? savestring (old_value->timestamp) : 0;
500 the_history[which] = temp;
501
502 return (old_value);
503}
504
505/* Append LINE to the history line at offset WHICH, adding a newline to the
506 end of the current line first. This can be used to construct multi-line
507 history entries while reading lines from the history file. */
508void
509_hs_append_history_line (int which, const char *line)
510{
511 HIST_ENTRY *hent;
512 size_t newlen, curlen, minlen;
513 char *newline;
514
515 hent = the_history[which];
516 curlen = strlen (hent->line);
517 minlen = curlen + strlen (line) + 2; /* min space needed */
518 if (curlen > 256) /* XXX - for now */
519 {
520 newlen = 512; /* now realloc in powers of 2 */
521 /* we recalcluate every time; the operations are cheap */
522 while (newlen < minlen)
523 newlen <<= 1;
524 }
525 else
526 newlen = minlen;
527 /* Assume that realloc returns the same pointer and doesn't try a new
528 alloc/copy if the new size is the same as the one last passed. */
529 newline = realloc (hent->line, newlen);
530 if (newline)
531 {
532 hent->line = newline;
533 hent->line[curlen++] = '\n';
534 strcpy (hent->line + curlen, line);
535 }
536}
537
538/* Replace the DATA in the specified history entries, replacing OLD with
539 NEW. WHICH says which one(s) to replace: WHICH == -1 means to replace
540 all of the history entries where entry->data == OLD; WHICH == -2 means
541 to replace the `newest' history entry where entry->data == OLD; and
542 WHICH >= 0 means to replace that particular history entry's data, as
543 long as it matches OLD. */
544void
545_hs_replace_history_data (int which, histdata_t *old, histdata_t *new)
546{
547 HIST_ENTRY *entry;
548 int i, last;
549
550 if (which < -2 || which >= history_length || history_length == 0 || the_history == 0)
551 return;
552
553 if (which >= 0)
554 {
555 entry = the_history[which];
556 if (entry && entry->data == old)
557 entry->data = new;
558 return;
559 }
560
561 last = -1;
562 for (i = history_length - 1; i >= 0; i--)
563 {
564 entry = the_history[i];
565 if (entry == 0)
566 continue;
567 if (entry->data == old)
568 {
569 last = i;
570 if (which == -1)
571 entry->data = new;
572 }
573 }
574 if (which == -2 && last >= 0)
575 {
576 entry = the_history[last];
577 entry->data = new; /* XXX - we don't check entry->old */
578 }
579}
580
581int
582_hs_search_history_data (histdata_t *needle)
583{
584 int i;
585 HIST_ENTRY *entry;
586
587 if (history_length == 0 || the_history == 0)
588 return -1;
589
590 for (i = history_length - 1; i >= 0; i--)
591 {
592 entry = the_history[i];
593 if (entry == 0)
594 continue;
595 if (entry->data == needle)
596 return i;
597 }
598 return -1;
599}
600
601/* Remove history element WHICH from the history. The removed
602 element is returned to you so you can free the line, data,
603 and containing structure. */
604HIST_ENTRY *
605remove_history (int which)
606{
607 HIST_ENTRY *return_value;
608#if 1
609 int nentries;
610 HIST_ENTRY **start, **end;
611#else
612 int i;
613#endif
614
615 if (which < 0 || which >= history_length || history_length == 0 || the_history == 0)
616 return ((HIST_ENTRY *)NULL);
617
618 return_value = the_history[which];
619
620#if 1
621 /* Copy the rest of the entries, moving down one slot. Copy includes
622 trailing NULL. */
623 nentries = history_length - which;
624 start = the_history + which;
625 end = start + 1;
626 memmove (start, end, nentries * sizeof (HIST_ENTRY *));
627#else
628 for (i = which; i < history_length; i++)
629 the_history[i] = the_history[i + 1];
630#endif
631
632 history_length--;
633
634 return (return_value);
635}
636
637HIST_ENTRY **
638remove_history_range (int first, int last)
639{
640 HIST_ENTRY **return_value;
641 register int i;
642 int nentries;
643 HIST_ENTRY **start, **end;
644
645 if (the_history == 0 || history_length == 0)
646 return ((HIST_ENTRY **)NULL);
647 if (first < 0 || first >= history_length || last < 0 || last >= history_length)
648 return ((HIST_ENTRY **)NULL);
649 if (first > last)
650 return (HIST_ENTRY **)NULL;
651
652 nentries = last - first + 1;
653 return_value = (HIST_ENTRY **)malloc ((nentries + 1) * sizeof (HIST_ENTRY *));
654 if (return_value == 0)
655 return return_value;
656
657 /* Return all the deleted entries in a list */
658 for (i = first ; i <= last; i++)
659 return_value[i - first] = the_history[i];
660 return_value[i - first] = (HIST_ENTRY *)NULL;
661
662 /* Copy the rest of the entries, moving down NENTRIES slots. Copy includes
663 trailing NULL. */
664 start = the_history + first;
665 end = the_history + last + 1;
666 memmove (start, end, (history_length - last) * sizeof (HIST_ENTRY *));
667
668 history_length -= nentries;
669
670 return (return_value);
671}
672
673/* Stifle the history list, remembering only MAX number of lines. */
674void
675stifle_history (int max)
676{
677 register int i, j;
678
679 if (max < 0)
680 max = 0;
681
682 if (history_length > max)
683 {
684 /* This loses because we cannot free the data. */
685 for (i = 0, j = history_length - max; i < j; i++)
686 free_history_entry (the_history[i]);
687
688 history_base = i;
689 for (j = 0, i = history_length - max; j < max; i++, j++)
690 the_history[j] = the_history[i];
691 the_history[j] = (HIST_ENTRY *)NULL;
692 history_length = j;
693 }
694
695 history_stifled = 1;
696 max_input_history = history_max_entries = max;
697}
698
699/* Stop stifling the history. This returns the previous maximum
700 number of history entries. The value is positive if the history
701 was stifled, negative if it wasn't. */
702int
703unstifle_history (void)
704{
705 if (history_stifled)
706 {
707 history_stifled = 0;
708 return (history_max_entries);
709 }
710 else
711 return (-history_max_entries);
712}
713
714int
715history_is_stifled (void)
716{
717 return (history_stifled);
718}
719
720void
721clear_history (void)
722{
723 register int i;
724
725 /* This loses because we cannot free the data. */
726 for (i = 0; i < history_length; i++)
727 {
728 free_history_entry (the_history[i]);
729 the_history[i] = (HIST_ENTRY *)NULL;
730 }
731
732 history_offset = history_length = 0;
733 history_base = 1; /* reset history base to default */
734}