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