]>
| Commit | Line | Data |
|---|---|---|
| b80f6443 | 1 | /* history.c -- standalone history library */ |
| 726f6388 | 2 | |
| b8c60bc9 | 3 | /* Copyright (C) 1989-2025 Free Software Foundation, Inc. |
| 726f6388 | 4 | |
| 3185942a | 5 | This file contains the GNU History Library (History), a set of |
| 726f6388 JA |
6 | routines for managing the text of previously typed lines. |
| 7 | ||
| 3185942a | 8 | History is free software: you can redistribute it and/or modify |
| 726f6388 | 9 | it under the terms of the GNU General Public License as published by |
| 3185942a JA |
10 | the Free Software Foundation, either version 3 of the License, or |
| 11 | (at your option) any later version. | |
| 726f6388 | 12 | |
| 3185942a JA |
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. | |
| 726f6388 | 17 | |
| 3185942a JA |
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 | */ | |
| 726f6388 JA |
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 | ||
| ccc6cda3 JA |
27 | #if defined (HAVE_CONFIG_H) |
| 28 | # include <config.h> | |
| 29 | #endif | |
| 30 | ||
| 726f6388 | 31 | #include <stdio.h> |
| ccc6cda3 | 32 | |
| 726f6388 JA |
33 | #if defined (HAVE_STDLIB_H) |
| 34 | # include <stdlib.h> | |
| 35 | #else | |
| 36 | # include "ansi_stdlib.h" | |
| 37 | #endif /* HAVE_STDLIB_H */ | |
| ccc6cda3 | 38 | |
| 726f6388 | 39 | #if defined (HAVE_UNISTD_H) |
| cce855bc JA |
40 | # ifdef _MINIX |
| 41 | # include <sys/types.h> | |
| 42 | # endif | |
| 726f6388 JA |
43 | # include <unistd.h> |
| 44 | #endif | |
| b8c60bc9 | 45 | #include "posixtime.h" |
| ccc6cda3 | 46 | |
| a0c0a00f CR |
47 | #include <errno.h> |
| 48 | ||
| 726f6388 | 49 | #include "history.h" |
| ccc6cda3 | 50 | #include "histlib.h" |
| 726f6388 | 51 | |
| bb70624e | 52 | #include "xmalloc.h" |
| 726f6388 | 53 | |
| a0c0a00f CR |
54 | #if !defined (errno) |
| 55 | extern 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 | ||
| 8ddc8d6e CR |
61 | #define MAX_HISTORY_INITIAL_SIZE 8192 |
| 62 | ||
| ccc6cda3 | 63 | /* The number of slots to increase the_history by. */ |
| b8c60bc9 | 64 | #define DEFAULT_HISTORY_GROW_SIZE 256 |
| 726f6388 | 65 | |
| 74091dd4 | 66 | static char *hist_inittime (void); |
| b80f6443 | 67 | |
| b8c60bc9 CR |
68 | static int history_list_grow_size (void); |
| 69 | static void history_list_resize (int); /* XXX - size_t? */ | |
| 70 | static void advance_history (void); | |
| 71 | ||
| 726f6388 JA |
72 | /* **************************************************************** */ |
| 73 | /* */ | |
| 74 | /* History Functions */ | |
| 75 | /* */ | |
| 76 | /* **************************************************************** */ | |
| 77 | ||
| b8c60bc9 CR |
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. */ | |
| 82 | static HIST_ENTRY **real_history = (HIST_ENTRY **)NULL; | |
| 83 | ||
| 84 | /* The current number of slots allocated to the input_history. */ | |
| 85 | static int real_history_size = 0; | |
| 86 | ||
| 87 | /* A pointer to somewhere in real_history, where the user-visible history | |
| 88 | starts. */ | |
| 726f6388 JA |
89 | static 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. */ | |
| ccc6cda3 | 93 | static int history_stifled; |
| 726f6388 | 94 | |
| b8c60bc9 CR |
95 | /* The number of history entries available for user use, starting at the_history. |
| 96 | real_history_size - history_size == the_history - real_history */ | |
| f73dda09 JA |
97 | static int history_size; |
| 98 | ||
| 726f6388 JA |
99 | /* If HISTORY_STIFLED is non-zero, then this is the maximum number of |
| 100 | entries to remember. */ | |
| 28ef6c31 JA |
101 | int history_max_entries; |
| 102 | int max_input_history; /* backwards compatibility */ | |
| 726f6388 JA |
103 | |
| 104 | /* The current location of the interactive history pointer. Just makes | |
| 105 | life easier for outside callers. */ | |
| ccc6cda3 | 106 | int history_offset; |
| 726f6388 | 107 | |
| b8c60bc9 CR |
108 | /* The number of strings currently stored in the history list. This is |
| 109 | always <= real_history_length */ | |
| ccc6cda3 | 110 | int history_length; |
| 726f6388 | 111 | |
| 726f6388 JA |
112 | /* The logical `base' of the history array. It defaults to 1. */ |
| 113 | int history_base = 1; | |
| 114 | ||
| b8c60bc9 CR |
115 | /* Compute the number of bits required to store a given nonnegative integer. |
| 116 | ||
| 117 | NOTE: _bit_length(0) == 0 */ | |
| 118 | static 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)). */ | |
| 137 | static int | |
| 138 | history_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 | ||
| 726f6388 JA |
161 | /* Return the current HISTORY_STATE of the history. */ |
| 162 | HISTORY_STATE * | |
| d233b485 | 163 | history_get_history_state (void) |
| 726f6388 JA |
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. */ | |
| 180 | void | |
| d233b485 | 181 | history_set_history_state (HISTORY_STATE *state) |
| 726f6388 JA |
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. */ | |
| 193 | void | |
| d233b485 | 194 | using_history (void) |
| 726f6388 JA |
195 | { |
| 196 | history_offset = history_length; | |
| 197 | } | |
| 198 | ||
| 199 | /* Return the number of bytes that the primary history entries are using. | |
| b80f6443 JA |
200 | This just adds up the lengths of the_history->lines and the associated |
| 201 | timestamps. */ | |
| 726f6388 | 202 | int |
| d233b485 | 203 | history_total_bytes (void) |
| 726f6388 | 204 | { |
| b8c60bc9 | 205 | int i, result; |
| 726f6388 | 206 | |
| 28ef6c31 | 207 | for (i = result = 0; the_history && the_history[i]; i++) |
| b80f6443 | 208 | result += HISTENT_BYTES (the_history[i]); |
| 726f6388 JA |
209 | |
| 210 | return (result); | |
| 211 | } | |
| 212 | ||
| ccc6cda3 JA |
213 | /* Returns the magic number which says what history element we are |
| 214 | looking at now. In this implementation, it returns history_offset. */ | |
| 215 | int | |
| d233b485 | 216 | where_history (void) |
| ccc6cda3 JA |
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. */ | |
| 223 | int | |
| d233b485 | 224 | history_set_pos (int pos) |
| ccc6cda3 JA |
225 | { |
| 226 | if (pos > history_length || pos < 0 || !the_history) | |
| 227 | return (0); | |
| 228 | history_offset = pos; | |
| 229 | return (1); | |
| 230 | } | |
| 74091dd4 CR |
231 | |
| 232 | /* Are we currently at the end of the history list? */ | |
| 233 | int | |
| 234 | _hs_at_end_of_history (void) | |
| 235 | { | |
| 236 | return (the_history == 0 || history_offset == history_length); | |
| 237 | } | |
| ccc6cda3 | 238 | |
| 3185942a | 239 | /* Return the current history array. The caller has to be careful, since this |
| ccc6cda3 JA |
240 | is the actual array of data, and could be bashed or made corrupt easily. |
| 241 | The array is terminated with a NULL pointer. */ | |
| 242 | HIST_ENTRY ** | |
| d233b485 | 243 | history_list (void) |
| ccc6cda3 JA |
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. */ | |
| 250 | HIST_ENTRY * | |
| d233b485 | 251 | current_history (void) |
| ccc6cda3 JA |
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. */ | |
| 261 | HIST_ENTRY * | |
| d233b485 | 262 | previous_history (void) |
| ccc6cda3 JA |
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. */ | |
| 270 | HIST_ENTRY * | |
| d233b485 | 271 | next_history (void) |
| ccc6cda3 JA |
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. */ | |
| 278 | HIST_ENTRY * | |
| d233b485 | 279 | history_get (int offset) |
| ccc6cda3 JA |
280 | { |
| 281 | int local_index; | |
| 282 | ||
| 283 | local_index = offset - history_base; | |
| 95732b49 | 284 | return (local_index >= history_length || local_index < 0 || the_history == 0) |
| ccc6cda3 JA |
285 | ? (HIST_ENTRY *)NULL |
| 286 | : the_history[local_index]; | |
| 287 | } | |
| 288 | ||
| 0628567a | 289 | HIST_ENTRY * |
| d233b485 | 290 | alloc_history_entry (char *string, char *ts) |
| 0628567a JA |
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 | ||
| b80f6443 | 303 | time_t |
| d233b485 | 304 | history_get_time (HIST_ENTRY *hist) |
| b80f6443 JA |
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; | |
| a0c0a00f | 314 | errno = 0; |
| ac50fbac | 315 | t = (time_t) strtol (ts + 1, (char **)NULL, 10); /* XXX - should use strtol() here */ |
| a0c0a00f CR |
316 | if (errno == ERANGE) |
| 317 | return (time_t)0; | |
| b80f6443 JA |
318 | return t; |
| 319 | } | |
| 320 | ||
| 321 | static char * | |
| d233b485 | 322 | hist_inittime (void) |
| b80f6443 JA |
323 | { |
| 324 | time_t t; | |
| 325 | char ts[64], *ret; | |
| 326 | ||
| b8c60bc9 | 327 | t = getnow (); |
| b80f6443 JA |
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 | ||
| b8c60bc9 CR |
339 | /* Ensure space for new history entries by resetting the start pointer |
| 340 | (the_history) and resizing real_history if necessary. */ | |
| 341 | static void | |
| 342 | history_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 | ||
| 369 | static void | |
| 370 | advance_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 | ||
| 726f6388 JA |
381 | /* Place STRING at the end of the history list. The data field |
| 382 | is set to NULL. */ | |
| 383 | void | |
| d233b485 | 384 | add_history (const char *string) |
| 726f6388 JA |
385 | { |
| 386 | HIST_ENTRY *temp; | |
| e59fb114 | 387 | int new_length; |
| 726f6388 | 388 | |
| 28ef6c31 | 389 | if (history_stifled && (history_length == history_max_entries)) |
| 726f6388 | 390 | { |
| 726f6388 | 391 | /* If the history is stifled, and history_length is zero, |
| 28ef6c31 | 392 | and it equals history_max_entries, we don't save items. */ |
| 726f6388 JA |
393 | if (history_length == 0) |
| 394 | return; | |
| 395 | ||
| 396 | /* If there is something in the slot, then remove it. */ | |
| 397 | if (the_history[0]) | |
| b80f6443 | 398 | (void) free_history_entry (the_history[0]); |
| 726f6388 | 399 | |
| b8c60bc9 CR |
400 | /* Advance the pointer into real_history, resizing if necessary. */ |
| 401 | advance_history (); | |
| 726f6388 | 402 | |
| e59fb114 | 403 | new_length = history_length; |
| 726f6388 | 404 | history_base++; |
| 726f6388 JA |
405 | } |
| 406 | else | |
| 407 | { | |
| ccc6cda3 | 408 | if (history_size == 0) |
| 726f6388 | 409 | { |
| b8c60bc9 | 410 | int initial_size; |
| a0c0a00f | 411 | if (history_stifled && history_max_entries > 0) |
| b8c60bc9 | 412 | initial_size = (history_max_entries > MAX_HISTORY_INITIAL_SIZE) |
| 8ddc8d6e CR |
413 | ? MAX_HISTORY_INITIAL_SIZE |
| 414 | : history_max_entries + 2; | |
| a0c0a00f | 415 | else |
| b8c60bc9 CR |
416 | initial_size = DEFAULT_HISTORY_INITIAL_SIZE; |
| 417 | history_list_resize (initial_size); | |
| e59fb114 | 418 | new_length = 1; |
| 726f6388 JA |
419 | } |
| 420 | else | |
| 421 | { | |
| 422 | if (history_length == (history_size - 1)) | |
| b8c60bc9 | 423 | history_list_resize (real_history_size + history_list_grow_size ()); |
| e59fb114 | 424 | new_length = history_length + 1; |
| 726f6388 JA |
425 | } |
| 426 | } | |
| 427 | ||
| a0c0a00f | 428 | temp = alloc_history_entry ((char *)string, hist_inittime ()); |
| b80f6443 | 429 | |
| e59fb114 CR |
430 | the_history[new_length] = (HIST_ENTRY *)NULL; |
| 431 | the_history[new_length - 1] = temp; | |
| 432 | history_length = new_length; | |
| 726f6388 JA |
433 | } |
| 434 | ||
| b80f6443 JA |
435 | /* Change the time stamp of the most recent history entry to STRING. */ |
| 436 | void | |
| d233b485 | 437 | add_history_time (const char *string) |
| b80f6443 JA |
438 | { |
| 439 | HIST_ENTRY *hs; | |
| 440 | ||
| ac50fbac | 441 | if (string == 0 || history_length < 1) |
| 3185942a | 442 | return; |
| b80f6443 JA |
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. */ | |
| 450 | histdata_t | |
| d233b485 | 451 | free_history_entry (HIST_ENTRY *hist) |
| b80f6443 JA |
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; | |
| 495aee44 | 460 | xfree (hist); |
| b80f6443 JA |
461 | return (x); |
| 462 | } | |
| 0628567a JA |
463 | |
| 464 | HIST_ENTRY * | |
| d233b485 | 465 | copy_history_entry (HIST_ENTRY *hist) |
| 0628567a JA |
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 | } | |
| b80f6443 | 482 | |
| 726f6388 JA |
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. */ | |
| 486 | HIST_ENTRY * | |
| d233b485 | 487 | replace_history_entry (int which, const char *line, histdata_t data) |
| 726f6388 | 488 | { |
| 28ef6c31 | 489 | HIST_ENTRY *temp, *old_value; |
| 726f6388 | 490 | |
| 95732b49 | 491 | if (which < 0 || which >= history_length) |
| 726f6388 JA |
492 | return ((HIST_ENTRY *)NULL); |
| 493 | ||
| 28ef6c31 | 494 | temp = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY)); |
| 726f6388 JA |
495 | old_value = the_history[which]; |
| 496 | ||
| 497 | temp->line = savestring (line); | |
| 498 | temp->data = data; | |
| 74091dd4 | 499 | temp->timestamp = old_value->timestamp ? savestring (old_value->timestamp) : 0; |
| 726f6388 JA |
500 | the_history[which] = temp; |
| 501 | ||
| 502 | return (old_value); | |
| 503 | } | |
| 504 | ||
| a0c0a00f CR |
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. */ | |
| 508 | void | |
| d233b485 | 509 | _hs_append_history_line (int which, const char *line) |
| a0c0a00f CR |
510 | { |
| 511 | HIST_ENTRY *hent; | |
| d233b485 | 512 | size_t newlen, curlen, minlen; |
| a0c0a00f CR |
513 | char *newline; |
| 514 | ||
| 515 | hent = the_history[which]; | |
| 516 | curlen = strlen (hent->line); | |
| d233b485 CR |
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. */ | |
| a0c0a00f CR |
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 | ||
| 0628567a JA |
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. */ | |
| 544 | void | |
| d233b485 | 545 | _hs_replace_history_data (int which, histdata_t *old, histdata_t *new) |
| 0628567a JA |
546 | { |
| 547 | HIST_ENTRY *entry; | |
| b8c60bc9 | 548 | int i, last; |
| 0628567a JA |
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; | |
| b8c60bc9 | 562 | for (i = history_length - 1; i >= 0; i--) |
| 0628567a JA |
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 | } | |
| b8c60bc9 CR |
579 | } |
| 580 | ||
| 581 | int | |
| 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 | } | |
| 0628567a | 600 | |
| 726f6388 JA |
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. */ | |
| 604 | HIST_ENTRY * | |
| d233b485 | 605 | remove_history (int which) |
| 726f6388 JA |
606 | { |
| 607 | HIST_ENTRY *return_value; | |
| d233b485 CR |
608 | #if 1 |
| 609 | int nentries; | |
| 610 | HIST_ENTRY **start, **end; | |
| b8c60bc9 CR |
611 | #else |
| 612 | int i; | |
| d233b485 | 613 | #endif |
| 726f6388 | 614 | |
| 95732b49 JA |
615 | if (which < 0 || which >= history_length || history_length == 0 || the_history == 0) |
| 616 | return ((HIST_ENTRY *)NULL); | |
| 726f6388 | 617 | |
| 95732b49 | 618 | return_value = the_history[which]; |
| 726f6388 | 619 | |
| d233b485 CR |
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 | |
| 95732b49 JA |
628 | for (i = which; i < history_length; i++) |
| 629 | the_history[i] = the_history[i + 1]; | |
| d233b485 | 630 | #endif |
| 95732b49 JA |
631 | |
| 632 | history_length--; | |
| 726f6388 JA |
633 | |
| 634 | return (return_value); | |
| 635 | } | |
| 636 | ||
| d233b485 CR |
637 | HIST_ENTRY ** |
| 638 | remove_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 | ||
| 726f6388 JA |
673 | /* Stifle the history list, remembering only MAX number of lines. */ |
| 674 | void | |
| d233b485 | 675 | stifle_history (int max) |
| 726f6388 | 676 | { |
| 28ef6c31 JA |
677 | register int i, j; |
| 678 | ||
| 726f6388 JA |
679 | if (max < 0) |
| 680 | max = 0; | |
| 681 | ||
| 682 | if (history_length > max) | |
| 683 | { | |
| 726f6388 | 684 | /* This loses because we cannot free the data. */ |
| ccc6cda3 | 685 | for (i = 0, j = history_length - max; i < j; i++) |
| b80f6443 | 686 | free_history_entry (the_history[i]); |
| 726f6388 JA |
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; | |
| 28ef6c31 | 696 | max_input_history = history_max_entries = max; |
| 726f6388 JA |
697 | } |
| 698 | ||
| 7117c2d2 JA |
699 | /* Stop stifling the history. This returns the previous maximum |
| 700 | number of history entries. The value is positive if the history | |
| 3185942a | 701 | was stifled, negative if it wasn't. */ |
| 726f6388 | 702 | int |
| d233b485 | 703 | unstifle_history (void) |
| 726f6388 | 704 | { |
| 726f6388 JA |
705 | if (history_stifled) |
| 706 | { | |
| 726f6388 | 707 | history_stifled = 0; |
| 7117c2d2 | 708 | return (history_max_entries); |
| 726f6388 | 709 | } |
| 7117c2d2 JA |
710 | else |
| 711 | return (-history_max_entries); | |
| 726f6388 JA |
712 | } |
| 713 | ||
| 714 | int | |
| d233b485 | 715 | history_is_stifled (void) |
| 726f6388 JA |
716 | { |
| 717 | return (history_stifled); | |
| 718 | } | |
| 719 | ||
| ccc6cda3 | 720 | void |
| d233b485 | 721 | clear_history (void) |
| 726f6388 JA |
722 | { |
| 723 | register int i; | |
| 726f6388 | 724 | |
| ccc6cda3 JA |
725 | /* This loses because we cannot free the data. */ |
| 726 | for (i = 0; i < history_length; i++) | |
| 726f6388 | 727 | { |
| b80f6443 | 728 | free_history_entry (the_history[i]); |
| ccc6cda3 | 729 | the_history[i] = (HIST_ENTRY *)NULL; |
| 726f6388 JA |
730 | } |
| 731 | ||
| ccc6cda3 | 732 | history_offset = history_length = 0; |
| d233b485 | 733 | history_base = 1; /* reset history base to default */ |
| 726f6388 | 734 | } |