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