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