Remove gdb workaround from readline/emacs_keymap.c
[deliverable/binutils-gdb.git] / readline / history.c
CommitLineData
5bdf8622 1/* history.c -- standalone history library */
d60d9f65 2
775e241e 3/* Copyright (C) 1989-2015 Free Software Foundation, Inc.
d60d9f65 4
cc88a640 5 This file contains the GNU History Library (History), a set of
d60d9f65
SS
6 routines for managing the text of previously typed lines.
7
cc88a640 8 History is free software: you can redistribute it and/or modify
d60d9f65 9 it under the terms of the GNU General Public License as published by
cc88a640
JK
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
d60d9f65 12
cc88a640
JK
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.
d60d9f65 17
cc88a640
JK
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*/
d60d9f65
SS
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
775e241e
TT
46#include <errno.h>
47
d60d9f65
SS
48#include "history.h"
49#include "histlib.h"
50
1b17e766 51#include "xmalloc.h"
d60d9f65 52
775e241e
TT
53#if !defined (errno)
54extern int errno;
55#endif
56
57/* How big to make the_history when we first allocate it. */
58#define DEFAULT_HISTORY_INITIAL_SIZE 502
59
60#define MAX_HISTORY_INITIAL_SIZE 8192
61
d60d9f65
SS
62/* The number of slots to increase the_history by. */
63#define DEFAULT_HISTORY_GROW_SIZE 50
64
5bdf8622
DJ
65static char *hist_inittime PARAMS((void));
66
d60d9f65
SS
67/* **************************************************************** */
68/* */
69/* History Functions */
70/* */
71/* **************************************************************** */
72
73/* An array of HIST_ENTRY. This is where we store the history. */
74static HIST_ENTRY **the_history = (HIST_ENTRY **)NULL;
75
76/* Non-zero means that we have enforced a limit on the amount of
77 history that we save. */
78static int history_stifled;
79
9255ee31
EZ
80/* The current number of slots allocated to the input_history. */
81static int history_size;
82
d60d9f65
SS
83/* If HISTORY_STIFLED is non-zero, then this is the maximum number of
84 entries to remember. */
9255ee31
EZ
85int history_max_entries;
86int max_input_history; /* backwards compatibility */
d60d9f65
SS
87
88/* The current location of the interactive history pointer. Just makes
89 life easier for outside callers. */
90int history_offset;
91
92/* The number of strings currently stored in the history list. */
93int history_length;
94
d60d9f65
SS
95/* The logical `base' of the history array. It defaults to 1. */
96int history_base = 1;
97
98/* Return the current HISTORY_STATE of the history. */
99HISTORY_STATE *
100history_get_history_state ()
101{
102 HISTORY_STATE *state;
103
104 state = (HISTORY_STATE *)xmalloc (sizeof (HISTORY_STATE));
105 state->entries = the_history;
106 state->offset = history_offset;
107 state->length = history_length;
108 state->size = history_size;
109 state->flags = 0;
110 if (history_stifled)
111 state->flags |= HS_STIFLED;
112
113 return (state);
114}
115
116/* Set the state of the current history array to STATE. */
117void
118history_set_history_state (state)
119 HISTORY_STATE *state;
120{
121 the_history = state->entries;
122 history_offset = state->offset;
123 history_length = state->length;
124 history_size = state->size;
125 if (state->flags & HS_STIFLED)
126 history_stifled = 1;
127}
128
129/* Begin a session in which the history functions might be used. This
130 initializes interactive variables. */
131void
132using_history ()
133{
134 history_offset = history_length;
135}
136
137/* Return the number of bytes that the primary history entries are using.
5bdf8622
DJ
138 This just adds up the lengths of the_history->lines and the associated
139 timestamps. */
d60d9f65
SS
140int
141history_total_bytes ()
142{
143 register int i, result;
144
9255ee31 145 for (i = result = 0; the_history && the_history[i]; i++)
5bdf8622 146 result += HISTENT_BYTES (the_history[i]);
d60d9f65
SS
147
148 return (result);
149}
150
151/* Returns the magic number which says what history element we are
152 looking at now. In this implementation, it returns history_offset. */
153int
154where_history ()
155{
156 return (history_offset);
157}
158
159/* Make the current history item be the one at POS, an absolute index.
160 Returns zero if POS is out of range, else non-zero. */
161int
162history_set_pos (pos)
163 int pos;
164{
165 if (pos > history_length || pos < 0 || !the_history)
166 return (0);
167 history_offset = pos;
168 return (1);
169}
170
cc88a640 171/* Return the current history array. The caller has to be careful, since this
d60d9f65
SS
172 is the actual array of data, and could be bashed or made corrupt easily.
173 The array is terminated with a NULL pointer. */
174HIST_ENTRY **
175history_list ()
176{
177 return (the_history);
178}
179
180/* Return the history entry at the current position, as determined by
181 history_offset. If there is no entry there, return a NULL pointer. */
182HIST_ENTRY *
183current_history ()
184{
185 return ((history_offset == history_length) || the_history == 0)
186 ? (HIST_ENTRY *)NULL
187 : the_history[history_offset];
188}
189
190/* Back up history_offset to the previous history entry, and return
191 a pointer to that entry. If there is no previous entry then return
192 a NULL pointer. */
193HIST_ENTRY *
194previous_history ()
195{
196 return history_offset ? the_history[--history_offset] : (HIST_ENTRY *)NULL;
197}
198
199/* Move history_offset forward to the next history entry, and return
200 a pointer to that entry. If there is no next entry then return a
201 NULL pointer. */
202HIST_ENTRY *
203next_history ()
204{
205 return (history_offset == history_length) ? (HIST_ENTRY *)NULL : the_history[++history_offset];
206}
207
208/* Return the history entry which is logically at OFFSET in the history array.
209 OFFSET is relative to history_base. */
210HIST_ENTRY *
211history_get (offset)
212 int offset;
213{
214 int local_index;
215
216 local_index = offset - history_base;
5bdf8622 217 return (local_index >= history_length || local_index < 0 || the_history == 0)
d60d9f65
SS
218 ? (HIST_ENTRY *)NULL
219 : the_history[local_index];
220}
221
cc88a640
JK
222HIST_ENTRY *
223alloc_history_entry (string, ts)
224 char *string;
225 char *ts;
226{
227 HIST_ENTRY *temp;
228
229 temp = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
230
231 temp->line = string ? savestring (string) : string;
232 temp->data = (char *)NULL;
233 temp->timestamp = ts;
234
235 return temp;
236}
237
5bdf8622
DJ
238time_t
239history_get_time (hist)
240 HIST_ENTRY *hist;
241{
242 char *ts;
243 time_t t;
244
245 if (hist == 0 || hist->timestamp == 0)
246 return 0;
247 ts = hist->timestamp;
248 if (ts[0] != history_comment_char)
249 return 0;
775e241e
TT
250 errno = 0;
251 t = (time_t) strtol (ts + 1, (char **)NULL, 10); /* XXX - should use strtol() here */
252 if (errno == ERANGE)
253 return (time_t)0;
5bdf8622
DJ
254 return t;
255}
256
257static char *
258hist_inittime ()
259{
260 time_t t;
261 char ts[64], *ret;
262
263 t = (time_t) time ((time_t *)0);
264#if defined (HAVE_VSNPRINTF) /* assume snprintf if vsnprintf exists */
265 snprintf (ts, sizeof (ts) - 1, "X%lu", (unsigned long) t);
266#else
267 sprintf (ts, "X%lu", (unsigned long) t);
268#endif
269 ret = savestring (ts);
270 ret[0] = history_comment_char;
271
272 return ret;
273}
274
d60d9f65
SS
275/* Place STRING at the end of the history list. The data field
276 is set to NULL. */
277void
278add_history (string)
9255ee31 279 const char *string;
d60d9f65
SS
280{
281 HIST_ENTRY *temp;
775e241e 282 int new_length;
d60d9f65 283
9255ee31 284 if (history_stifled && (history_length == history_max_entries))
d60d9f65
SS
285 {
286 register int i;
287
288 /* If the history is stifled, and history_length is zero,
9255ee31 289 and it equals history_max_entries, we don't save items. */
d60d9f65
SS
290 if (history_length == 0)
291 return;
292
293 /* If there is something in the slot, then remove it. */
294 if (the_history[0])
5bdf8622 295 (void) free_history_entry (the_history[0]);
d60d9f65 296
775e241e
TT
297 /* Copy the rest of the entries, moving down one slot. Copy includes
298 trailing NULL. */
299 memmove (the_history, the_history + 1, history_length * sizeof (HIST_ENTRY *));
d60d9f65 300
775e241e 301 new_length = history_length;
d60d9f65
SS
302 history_base++;
303 }
304 else
305 {
306 if (history_size == 0)
307 {
775e241e
TT
308 if (history_stifled && history_max_entries > 0)
309 history_size = (history_max_entries > MAX_HISTORY_INITIAL_SIZE)
310 ? MAX_HISTORY_INITIAL_SIZE
311 : history_max_entries + 2;
312 else
313 history_size = DEFAULT_HISTORY_INITIAL_SIZE;
d60d9f65 314 the_history = (HIST_ENTRY **)xmalloc (history_size * sizeof (HIST_ENTRY *));
775e241e 315 new_length = 1;
d60d9f65
SS
316 }
317 else
318 {
319 if (history_length == (history_size - 1))
320 {
321 history_size += DEFAULT_HISTORY_GROW_SIZE;
322 the_history = (HIST_ENTRY **)
323 xrealloc (the_history, history_size * sizeof (HIST_ENTRY *));
324 }
775e241e 325 new_length = history_length + 1;
d60d9f65
SS
326 }
327 }
328
775e241e 329 temp = alloc_history_entry ((char *)string, hist_inittime ());
5bdf8622 330
775e241e
TT
331 the_history[new_length] = (HIST_ENTRY *)NULL;
332 the_history[new_length - 1] = temp;
333 history_length = new_length;
d60d9f65
SS
334}
335
5bdf8622
DJ
336/* Change the time stamp of the most recent history entry to STRING. */
337void
338add_history_time (string)
339 const char *string;
340{
341 HIST_ENTRY *hs;
342
775e241e 343 if (string == 0 || history_length < 1)
cc88a640 344 return;
5bdf8622
DJ
345 hs = the_history[history_length - 1];
346 FREE (hs->timestamp);
347 hs->timestamp = savestring (string);
348}
349
350/* Free HIST and return the data so the calling application can free it
351 if necessary and desired. */
352histdata_t
353free_history_entry (hist)
354 HIST_ENTRY *hist;
355{
356 histdata_t x;
357
358 if (hist == 0)
359 return ((histdata_t) 0);
360 FREE (hist->line);
361 FREE (hist->timestamp);
362 x = hist->data;
cc88a640 363 xfree (hist);
5bdf8622
DJ
364 return (x);
365}
cc88a640
JK
366
367HIST_ENTRY *
368copy_history_entry (hist)
369 HIST_ENTRY *hist;
370{
371 HIST_ENTRY *ret;
372 char *ts;
373
374 if (hist == 0)
375 return hist;
376
377 ret = alloc_history_entry (hist->line, (char *)NULL);
378
379 ts = hist->timestamp ? savestring (hist->timestamp) : hist->timestamp;
380 ret->timestamp = ts;
381
382 ret->data = hist->data;
383
384 return ret;
385}
5bdf8622 386
d60d9f65
SS
387/* Make the history entry at WHICH have LINE and DATA. This returns
388 the old entry so you can dispose of the data. In the case of an
389 invalid WHICH, a NULL pointer is returned. */
390HIST_ENTRY *
391replace_history_entry (which, line, data)
392 int which;
9255ee31 393 const char *line;
c862e87b 394 histdata_t data;
d60d9f65 395{
9255ee31 396 HIST_ENTRY *temp, *old_value;
d60d9f65 397
5bdf8622 398 if (which < 0 || which >= history_length)
d60d9f65
SS
399 return ((HIST_ENTRY *)NULL);
400
9255ee31 401 temp = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
d60d9f65
SS
402 old_value = the_history[which];
403
404 temp->line = savestring (line);
405 temp->data = data;
5bdf8622 406 temp->timestamp = savestring (old_value->timestamp);
d60d9f65
SS
407 the_history[which] = temp;
408
409 return (old_value);
410}
411
775e241e
TT
412/* Append LINE to the history line at offset WHICH, adding a newline to the
413 end of the current line first. This can be used to construct multi-line
414 history entries while reading lines from the history file. */
415void
416_hs_append_history_line (which, line)
417 int which;
418 const char *line;
419{
420 HIST_ENTRY *hent;
421 size_t newlen, curlen;
422 char *newline;
423
424 hent = the_history[which];
425 curlen = strlen (hent->line);
426 newlen = curlen + strlen (line) + 2;
427 newline = realloc (hent->line, newlen);
428 if (newline)
429 {
430 hent->line = newline;
431 hent->line[curlen++] = '\n';
432 strcpy (hent->line + curlen, line);
433 }
434}
435
cc88a640
JK
436/* Replace the DATA in the specified history entries, replacing OLD with
437 NEW. WHICH says which one(s) to replace: WHICH == -1 means to replace
438 all of the history entries where entry->data == OLD; WHICH == -2 means
439 to replace the `newest' history entry where entry->data == OLD; and
440 WHICH >= 0 means to replace that particular history entry's data, as
441 long as it matches OLD. */
442void
775e241e 443_hs_replace_history_data (which, old, new)
cc88a640
JK
444 int which;
445 histdata_t *old, *new;
446{
447 HIST_ENTRY *entry;
448 register int i, last;
449
450 if (which < -2 || which >= history_length || history_length == 0 || the_history == 0)
451 return;
452
453 if (which >= 0)
454 {
455 entry = the_history[which];
456 if (entry && entry->data == old)
457 entry->data = new;
458 return;
459 }
460
461 last = -1;
462 for (i = 0; i < history_length; i++)
463 {
464 entry = the_history[i];
465 if (entry == 0)
466 continue;
467 if (entry->data == old)
468 {
469 last = i;
470 if (which == -1)
471 entry->data = new;
472 }
473 }
474 if (which == -2 && last >= 0)
475 {
476 entry = the_history[last];
477 entry->data = new; /* XXX - we don't check entry->old */
478 }
479}
480
d60d9f65
SS
481/* Remove history element WHICH from the history. The removed
482 element is returned to you so you can free the line, data,
483 and containing structure. */
484HIST_ENTRY *
485remove_history (which)
486 int which;
487{
488 HIST_ENTRY *return_value;
9255ee31 489 register int i;
d60d9f65 490
5bdf8622
DJ
491 if (which < 0 || which >= history_length || history_length == 0 || the_history == 0)
492 return ((HIST_ENTRY *)NULL);
d60d9f65 493
5bdf8622 494 return_value = the_history[which];
d60d9f65 495
5bdf8622
DJ
496 for (i = which; i < history_length; i++)
497 the_history[i] = the_history[i + 1];
498
499 history_length--;
d60d9f65
SS
500
501 return (return_value);
502}
503
504/* Stifle the history list, remembering only MAX number of lines. */
505void
506stifle_history (max)
507 int max;
508{
9255ee31
EZ
509 register int i, j;
510
d60d9f65
SS
511 if (max < 0)
512 max = 0;
513
514 if (history_length > max)
515 {
d60d9f65
SS
516 /* This loses because we cannot free the data. */
517 for (i = 0, j = history_length - max; i < j; i++)
5bdf8622 518 free_history_entry (the_history[i]);
d60d9f65
SS
519
520 history_base = i;
521 for (j = 0, i = history_length - max; j < max; i++, j++)
522 the_history[j] = the_history[i];
523 the_history[j] = (HIST_ENTRY *)NULL;
524 history_length = j;
525 }
526
527 history_stifled = 1;
9255ee31 528 max_input_history = history_max_entries = max;
d60d9f65
SS
529}
530
9255ee31
EZ
531/* Stop stifling the history. This returns the previous maximum
532 number of history entries. The value is positive if the history
cc88a640 533 was stifled, negative if it wasn't. */
d60d9f65
SS
534int
535unstifle_history ()
536{
537 if (history_stifled)
538 {
539 history_stifled = 0;
9255ee31 540 return (history_max_entries);
d60d9f65 541 }
9255ee31
EZ
542 else
543 return (-history_max_entries);
d60d9f65
SS
544}
545
546int
547history_is_stifled ()
548{
549 return (history_stifled);
550}
551
552void
553clear_history ()
554{
555 register int i;
556
557 /* This loses because we cannot free the data. */
558 for (i = 0; i < history_length; i++)
559 {
5bdf8622 560 free_history_entry (the_history[i]);
d60d9f65
SS
561 the_history[i] = (HIST_ENTRY *)NULL;
562 }
563
564 history_offset = history_length = 0;
565}
This page took 1.441272 seconds and 4 git commands to generate.