xref: /dflybsd-src/contrib/gdb-7/readline/history.h (revision 16003dcfd2baa152f5dd24794ec9f36e139eaeb8)
1*6b445a62SJohn Marino /* history.h -- the names of functions that you can call in history. */
2*6b445a62SJohn Marino 
3*6b445a62SJohn Marino /* Copyright (C) 1989-2009 Free Software Foundation, Inc.
4*6b445a62SJohn Marino 
5*6b445a62SJohn Marino    This file contains the GNU History Library (History), a set of
6*6b445a62SJohn Marino    routines for managing the text of previously typed lines.
7*6b445a62SJohn Marino 
8*6b445a62SJohn Marino    History is free software: you can redistribute it and/or modify
9*6b445a62SJohn Marino    it under the terms of the GNU General Public License as published by
10*6b445a62SJohn Marino    the Free Software Foundation, either version 3 of the License, or
11*6b445a62SJohn Marino    (at your option) any later version.
12*6b445a62SJohn Marino 
13*6b445a62SJohn Marino    History is distributed in the hope that it will be useful,
14*6b445a62SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
15*6b445a62SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*6b445a62SJohn Marino    GNU General Public License for more details.
17*6b445a62SJohn Marino 
18*6b445a62SJohn Marino    You should have received a copy of the GNU General Public License
19*6b445a62SJohn Marino    along with History.  If not, see <http://www.gnu.org/licenses/>.
20*6b445a62SJohn Marino */
21*6b445a62SJohn Marino 
22*6b445a62SJohn Marino #ifndef _HISTORY_H_
23*6b445a62SJohn Marino #define _HISTORY_H_
24*6b445a62SJohn Marino 
25*6b445a62SJohn Marino #ifdef __cplusplus
26*6b445a62SJohn Marino extern "C" {
27*6b445a62SJohn Marino #endif
28*6b445a62SJohn Marino 
29*6b445a62SJohn Marino #include <time.h>		/* XXX - for history timestamp code */
30*6b445a62SJohn Marino 
31*6b445a62SJohn Marino #if defined READLINE_LIBRARY
32*6b445a62SJohn Marino #  include "rlstdc.h"
33*6b445a62SJohn Marino #  include "rltypedefs.h"
34*6b445a62SJohn Marino #else
35*6b445a62SJohn Marino #  include <readline/rlstdc.h>
36*6b445a62SJohn Marino #  include <readline/rltypedefs.h>
37*6b445a62SJohn Marino #endif
38*6b445a62SJohn Marino 
39*6b445a62SJohn Marino #ifdef __STDC__
40*6b445a62SJohn Marino typedef void *histdata_t;
41*6b445a62SJohn Marino #else
42*6b445a62SJohn Marino typedef char *histdata_t;
43*6b445a62SJohn Marino #endif
44*6b445a62SJohn Marino 
45*6b445a62SJohn Marino /* The structure used to store a history entry. */
46*6b445a62SJohn Marino typedef struct _hist_entry {
47*6b445a62SJohn Marino   char *line;
48*6b445a62SJohn Marino   char *timestamp;		/* char * rather than time_t for read/write */
49*6b445a62SJohn Marino   histdata_t data;
50*6b445a62SJohn Marino } HIST_ENTRY;
51*6b445a62SJohn Marino 
52*6b445a62SJohn Marino /* Size of the history-library-managed space in history entry HS. */
53*6b445a62SJohn Marino #define HISTENT_BYTES(hs)	(strlen ((hs)->line) + strlen ((hs)->timestamp))
54*6b445a62SJohn Marino 
55*6b445a62SJohn Marino /* A structure used to pass the current state of the history stuff around. */
56*6b445a62SJohn Marino typedef struct _hist_state {
57*6b445a62SJohn Marino   HIST_ENTRY **entries;		/* Pointer to the entries themselves. */
58*6b445a62SJohn Marino   int offset;			/* The location pointer within this array. */
59*6b445a62SJohn Marino   int length;			/* Number of elements within this array. */
60*6b445a62SJohn Marino   int size;			/* Number of slots allocated to this array. */
61*6b445a62SJohn Marino   int flags;
62*6b445a62SJohn Marino } HISTORY_STATE;
63*6b445a62SJohn Marino 
64*6b445a62SJohn Marino /* Flag values for the `flags' member of HISTORY_STATE. */
65*6b445a62SJohn Marino #define HS_STIFLED	0x01
66*6b445a62SJohn Marino 
67*6b445a62SJohn Marino /* Initialization and state management. */
68*6b445a62SJohn Marino 
69*6b445a62SJohn Marino /* Begin a session in which the history functions might be used.  This
70*6b445a62SJohn Marino    just initializes the interactive variables. */
71*6b445a62SJohn Marino extern void using_history PARAMS((void));
72*6b445a62SJohn Marino 
73*6b445a62SJohn Marino /* Return the current HISTORY_STATE of the history. */
74*6b445a62SJohn Marino extern HISTORY_STATE *history_get_history_state PARAMS((void));
75*6b445a62SJohn Marino 
76*6b445a62SJohn Marino /* Set the state of the current history array to STATE. */
77*6b445a62SJohn Marino extern void history_set_history_state PARAMS((HISTORY_STATE *));
78*6b445a62SJohn Marino 
79*6b445a62SJohn Marino /* Manage the history list. */
80*6b445a62SJohn Marino 
81*6b445a62SJohn Marino /* Place STRING at the end of the history list.
82*6b445a62SJohn Marino    The associated data field (if any) is set to NULL. */
83*6b445a62SJohn Marino extern void add_history PARAMS((const char *));
84*6b445a62SJohn Marino 
85*6b445a62SJohn Marino /* Change the timestamp associated with the most recent history entry to
86*6b445a62SJohn Marino    STRING. */
87*6b445a62SJohn Marino extern void add_history_time PARAMS((const char *));
88*6b445a62SJohn Marino 
89*6b445a62SJohn Marino /* A reasonably useless function, only here for completeness.  WHICH
90*6b445a62SJohn Marino    is the magic number that tells us which element to delete.  The
91*6b445a62SJohn Marino    elements are numbered from 0. */
92*6b445a62SJohn Marino extern HIST_ENTRY *remove_history PARAMS((int));
93*6b445a62SJohn Marino 
94*6b445a62SJohn Marino /* Free the history entry H and return any application-specific data
95*6b445a62SJohn Marino    associated with it. */
96*6b445a62SJohn Marino extern histdata_t free_history_entry PARAMS((HIST_ENTRY *));
97*6b445a62SJohn Marino 
98*6b445a62SJohn Marino /* Make the history entry at WHICH have LINE and DATA.  This returns
99*6b445a62SJohn Marino    the old entry so you can dispose of the data.  In the case of an
100*6b445a62SJohn Marino    invalid WHICH, a NULL pointer is returned. */
101*6b445a62SJohn Marino extern HIST_ENTRY *replace_history_entry PARAMS((int, const char *, histdata_t));
102*6b445a62SJohn Marino 
103*6b445a62SJohn Marino /* Clear the history list and start over. */
104*6b445a62SJohn Marino extern void clear_history PARAMS((void));
105*6b445a62SJohn Marino 
106*6b445a62SJohn Marino /* Stifle the history list, remembering only MAX number of entries. */
107*6b445a62SJohn Marino extern void stifle_history PARAMS((int));
108*6b445a62SJohn Marino 
109*6b445a62SJohn Marino /* Stop stifling the history.  This returns the previous amount the
110*6b445a62SJohn Marino    history was stifled by.  The value is positive if the history was
111*6b445a62SJohn Marino    stifled, negative if it wasn't. */
112*6b445a62SJohn Marino extern int unstifle_history PARAMS((void));
113*6b445a62SJohn Marino 
114*6b445a62SJohn Marino /* Return 1 if the history is stifled, 0 if it is not. */
115*6b445a62SJohn Marino extern int history_is_stifled PARAMS((void));
116*6b445a62SJohn Marino 
117*6b445a62SJohn Marino /* Information about the history list. */
118*6b445a62SJohn Marino 
119*6b445a62SJohn Marino /* Return a NULL terminated array of HIST_ENTRY which is the current input
120*6b445a62SJohn Marino    history.  Element 0 of this list is the beginning of time.  If there
121*6b445a62SJohn Marino    is no history, return NULL. */
122*6b445a62SJohn Marino extern HIST_ENTRY **history_list PARAMS((void));
123*6b445a62SJohn Marino 
124*6b445a62SJohn Marino /* Returns the number which says what history element we are now
125*6b445a62SJohn Marino    looking at.  */
126*6b445a62SJohn Marino extern int where_history PARAMS((void));
127*6b445a62SJohn Marino 
128*6b445a62SJohn Marino /* Return the history entry at the current position, as determined by
129*6b445a62SJohn Marino    history_offset.  If there is no entry there, return a NULL pointer. */
130*6b445a62SJohn Marino extern HIST_ENTRY *current_history PARAMS((void));
131*6b445a62SJohn Marino 
132*6b445a62SJohn Marino /* Return the history entry which is logically at OFFSET in the history
133*6b445a62SJohn Marino    array.  OFFSET is relative to history_base. */
134*6b445a62SJohn Marino extern HIST_ENTRY *history_get PARAMS((int));
135*6b445a62SJohn Marino 
136*6b445a62SJohn Marino /* Return the timestamp associated with the HIST_ENTRY * passed as an
137*6b445a62SJohn Marino    argument */
138*6b445a62SJohn Marino extern time_t history_get_time PARAMS((HIST_ENTRY *));
139*6b445a62SJohn Marino 
140*6b445a62SJohn Marino /* Return the number of bytes that the primary history entries are using.
141*6b445a62SJohn Marino    This just adds up the lengths of the_history->lines. */
142*6b445a62SJohn Marino extern int history_total_bytes PARAMS((void));
143*6b445a62SJohn Marino 
144*6b445a62SJohn Marino /* Moving around the history list. */
145*6b445a62SJohn Marino 
146*6b445a62SJohn Marino /* Set the position in the history list to POS. */
147*6b445a62SJohn Marino extern int history_set_pos PARAMS((int));
148*6b445a62SJohn Marino 
149*6b445a62SJohn Marino /* Back up history_offset to the previous history entry, and return
150*6b445a62SJohn Marino    a pointer to that entry.  If there is no previous entry, return
151*6b445a62SJohn Marino    a NULL pointer. */
152*6b445a62SJohn Marino extern HIST_ENTRY *previous_history PARAMS((void));
153*6b445a62SJohn Marino 
154*6b445a62SJohn Marino /* Move history_offset forward to the next item in the input_history,
155*6b445a62SJohn Marino    and return the a pointer to that entry.  If there is no next entry,
156*6b445a62SJohn Marino    return a NULL pointer. */
157*6b445a62SJohn Marino extern HIST_ENTRY *next_history PARAMS((void));
158*6b445a62SJohn Marino 
159*6b445a62SJohn Marino /* Searching the history list. */
160*6b445a62SJohn Marino 
161*6b445a62SJohn Marino /* Search the history for STRING, starting at history_offset.
162*6b445a62SJohn Marino    If DIRECTION < 0, then the search is through previous entries,
163*6b445a62SJohn Marino    else through subsequent.  If the string is found, then
164*6b445a62SJohn Marino    current_history () is the history entry, and the value of this function
165*6b445a62SJohn Marino    is the offset in the line of that history entry that the string was
166*6b445a62SJohn Marino    found in.  Otherwise, nothing is changed, and a -1 is returned. */
167*6b445a62SJohn Marino extern int history_search PARAMS((const char *, int));
168*6b445a62SJohn Marino 
169*6b445a62SJohn Marino /* Search the history for STRING, starting at history_offset.
170*6b445a62SJohn Marino    The search is anchored: matching lines must begin with string.
171*6b445a62SJohn Marino    DIRECTION is as in history_search(). */
172*6b445a62SJohn Marino extern int history_search_prefix PARAMS((const char *, int));
173*6b445a62SJohn Marino 
174*6b445a62SJohn Marino /* Search for STRING in the history list, starting at POS, an
175*6b445a62SJohn Marino    absolute index into the list.  DIR, if negative, says to search
176*6b445a62SJohn Marino    backwards from POS, else forwards.
177*6b445a62SJohn Marino    Returns the absolute index of the history element where STRING
178*6b445a62SJohn Marino    was found, or -1 otherwise. */
179*6b445a62SJohn Marino extern int history_search_pos PARAMS((const char *, int, int));
180*6b445a62SJohn Marino 
181*6b445a62SJohn Marino /* Managing the history file. */
182*6b445a62SJohn Marino 
183*6b445a62SJohn Marino /* Add the contents of FILENAME to the history list, a line at a time.
184*6b445a62SJohn Marino    If FILENAME is NULL, then read from ~/.history.  Returns 0 if
185*6b445a62SJohn Marino    successful, or errno if not. */
186*6b445a62SJohn Marino extern int read_history PARAMS((const char *));
187*6b445a62SJohn Marino 
188*6b445a62SJohn Marino /* Read a range of lines from FILENAME, adding them to the history list.
189*6b445a62SJohn Marino    Start reading at the FROM'th line and end at the TO'th.  If FROM
190*6b445a62SJohn Marino    is zero, start at the beginning.  If TO is less than FROM, read
191*6b445a62SJohn Marino    until the end of the file.  If FILENAME is NULL, then read from
192*6b445a62SJohn Marino    ~/.history.  Returns 0 if successful, or errno if not. */
193*6b445a62SJohn Marino extern int read_history_range PARAMS((const char *, int, int));
194*6b445a62SJohn Marino 
195*6b445a62SJohn Marino /* Write the current history to FILENAME.  If FILENAME is NULL,
196*6b445a62SJohn Marino    then write the history list to ~/.history.  Values returned
197*6b445a62SJohn Marino    are as in read_history ().  */
198*6b445a62SJohn Marino extern int write_history PARAMS((const char *));
199*6b445a62SJohn Marino 
200*6b445a62SJohn Marino /* Append NELEMENT entries to FILENAME.  The entries appended are from
201*6b445a62SJohn Marino    the end of the list minus NELEMENTs up to the end of the list. */
202*6b445a62SJohn Marino extern int append_history PARAMS((int, const char *));
203*6b445a62SJohn Marino 
204*6b445a62SJohn Marino /* Truncate the history file, leaving only the last NLINES lines. */
205*6b445a62SJohn Marino extern int history_truncate_file PARAMS((const char *, int));
206*6b445a62SJohn Marino 
207*6b445a62SJohn Marino /* History expansion. */
208*6b445a62SJohn Marino 
209*6b445a62SJohn Marino /* Expand the string STRING, placing the result into OUTPUT, a pointer
210*6b445a62SJohn Marino    to a string.  Returns:
211*6b445a62SJohn Marino 
212*6b445a62SJohn Marino    0) If no expansions took place (or, if the only change in
213*6b445a62SJohn Marino       the text was the de-slashifying of the history expansion
214*6b445a62SJohn Marino       character)
215*6b445a62SJohn Marino    1) If expansions did take place
216*6b445a62SJohn Marino   -1) If there was an error in expansion.
217*6b445a62SJohn Marino    2) If the returned line should just be printed.
218*6b445a62SJohn Marino 
219*6b445a62SJohn Marino   If an error ocurred in expansion, then OUTPUT contains a descriptive
220*6b445a62SJohn Marino   error message. */
221*6b445a62SJohn Marino extern int history_expand PARAMS((char *, char **));
222*6b445a62SJohn Marino 
223*6b445a62SJohn Marino /* Extract a string segment consisting of the FIRST through LAST
224*6b445a62SJohn Marino    arguments present in STRING.  Arguments are broken up as in
225*6b445a62SJohn Marino    the shell. */
226*6b445a62SJohn Marino extern char *history_arg_extract PARAMS((int, int, const char *));
227*6b445a62SJohn Marino 
228*6b445a62SJohn Marino /* Return the text of the history event beginning at the current
229*6b445a62SJohn Marino    offset into STRING.  Pass STRING with *INDEX equal to the
230*6b445a62SJohn Marino    history_expansion_char that begins this specification.
231*6b445a62SJohn Marino    DELIMITING_QUOTE is a character that is allowed to end the string
232*6b445a62SJohn Marino    specification for what to search for in addition to the normal
233*6b445a62SJohn Marino    characters `:', ` ', `\t', `\n', and sometimes `?'. */
234*6b445a62SJohn Marino extern char *get_history_event PARAMS((const char *, int *, int));
235*6b445a62SJohn Marino 
236*6b445a62SJohn Marino /* Return an array of tokens, much as the shell might.  The tokens are
237*6b445a62SJohn Marino    parsed out of STRING. */
238*6b445a62SJohn Marino extern char **history_tokenize PARAMS((const char *));
239*6b445a62SJohn Marino 
240*6b445a62SJohn Marino /* Exported history variables. */
241*6b445a62SJohn Marino extern int history_base;
242*6b445a62SJohn Marino extern int history_length;
243*6b445a62SJohn Marino extern int history_max_entries;
244*6b445a62SJohn Marino extern char history_expansion_char;
245*6b445a62SJohn Marino extern char history_subst_char;
246*6b445a62SJohn Marino extern char *history_word_delimiters;
247*6b445a62SJohn Marino extern char history_comment_char;
248*6b445a62SJohn Marino extern char *history_no_expand_chars;
249*6b445a62SJohn Marino extern char *history_search_delimiter_chars;
250*6b445a62SJohn Marino extern int history_quotes_inhibit_expansion;
251*6b445a62SJohn Marino 
252*6b445a62SJohn Marino extern int history_write_timestamps;
253*6b445a62SJohn Marino 
254*6b445a62SJohn Marino /* Backwards compatibility */
255*6b445a62SJohn Marino extern int max_input_history;
256*6b445a62SJohn Marino 
257*6b445a62SJohn Marino /* If set, this function is called to decide whether or not a particular
258*6b445a62SJohn Marino    history expansion should be treated as a special case for the calling
259*6b445a62SJohn Marino    application and not expanded. */
260*6b445a62SJohn Marino extern rl_linebuf_func_t *history_inhibit_expansion_function;
261*6b445a62SJohn Marino 
262*6b445a62SJohn Marino #ifdef __cplusplus
263*6b445a62SJohn Marino }
264*6b445a62SJohn Marino #endif
265*6b445a62SJohn Marino 
266*6b445a62SJohn Marino #endif /* !_HISTORY_H_ */
267