1HISTORY(3) Library Functions Manual HISTORY(3)
2
3
4
6 history - GNU History Library
7
9 The GNU History Library is Copyright (C) 1989-2002 by the Free Software
10 Foundation, Inc.
11
13 Many programs read input from the user a line at a time. The GNU His‐
14 tory library is able to keep track of those lines, associate arbitrary
15 data with each line, and utilize information from previous lines in
16 composing new ones.
17
19 The history library supports a history expansion feature that is iden‐
20 tical to the history expansion in bash. This section describes what
21 syntax features are available.
22
23 History expansions introduce words from the history list into the input
24 stream, making it easy to repeat commands, insert the arguments to a
25 previous command into the current input line, or fix errors in previous
26 commands quickly.
27
28 History expansion is usually performed immediately after a complete
29 line is read. It takes place in two parts. The first is to determine
30 which line from the history list to use during substitution. The sec‐
31 ond is to select portions of that line for inclusion into the current
32 one. The line selected from the history is the event, and the portions
33 of that line that are acted upon are words. Various modifiers are
34 available to manipulate the selected words. The line is broken into
35 words in the same fashion as bash does when reading input, so that sev‐
36 eral words that would otherwise be separated are considered one word
37 when surrounded by quotes (see the description of history_tokenize()
38 below). History expansions are introduced by the appearance of the
39 history expansion character, which is ! by default. Only backslash (\)
40 and single quotes can quote the history expansion character.
41
42 Event Designators
43 An event designator is a reference to a command line entry in the his‐
44 tory list.
45
46 ! Start a history substitution, except when followed by a blank,
47 newline, = or (.
48 !n Refer to command line n.
49 !-n Refer to the current command line minus n.
50 !! Refer to the previous command. This is a synonym for `!-1'.
51 !string
52 Refer to the most recent command starting with string.
53 !?string[?]
54 Refer to the most recent command containing string. The trail‐
55 ing ? may be omitted if string is followed immediately by a new‐
56 line.
57 ^string1^string2^
58 Quick substitution. Repeat the last command, replacing string1
59 with string2. Equivalent to ``!!:s/string1/string2/'' (see Mod‐
60 ifiers below).
61 !# The entire command line typed so far.
62
63 Word Designators
64 Word designators are used to select desired words from the event. A :
65 separates the event specification from the word designator. It may be
66 omitted if the word designator begins with a ^, $, *, -, or %. Words
67 are numbered from the beginning of the line, with the first word being
68 denoted by 0 (zero). Words are inserted into the current line sepa‐
69 rated by single spaces.
70
71 0 (zero)
72 The zeroth word. For the shell, this is the command word.
73 n The nth word.
74 ^ The first argument. That is, word 1.
75 $ The last argument.
76 % The word matched by the most recent `?string?' search.
77 x-y A range of words; `-y' abbreviates `0-y'.
78 * All of the words but the zeroth. This is a synonym for `1-$'.
79 It is not an error to use * if there is just one word in the
80 event; the empty string is returned in that case.
81 x* Abbreviates x-$.
82 x- Abbreviates x-$ like x*, but omits the last word.
83
84 If a word designator is supplied without an event specification, the
85 previous command is used as the event.
86
87 Modifiers
88 After the optional word designator, there may appear a sequence of one
89 or more of the following modifiers, each preceded by a `:'.
90
91 h Remove a trailing file name component, leaving only the head.
92 t Remove all leading file name components, leaving the tail.
93 r Remove a trailing suffix of the form .xxx, leaving the basename.
94 e Remove all but the trailing suffix.
95 p Print the new command but do not execute it.
96 q Quote the substituted words, escaping further substitutions.
97 x Quote the substituted words as with q, but break into words at
98 blanks and newlines.
99 s/old/new/
100 Substitute new for the first occurrence of old in the event
101 line. Any delimiter can be used in place of /. The final
102 delimiter is optional if it is the last character of the event
103 line. The delimiter may be quoted in old and new with a single
104 backslash. If & appears in new, it is replaced by old. A sin‐
105 gle backslash will quote the &. If old is null, it is set to
106 the last old substituted, or, if no previous history substitu‐
107 tions took place, the last string in a !?string[?] search.
108 & Repeat the previous substitution.
109 g Cause changes to be applied over the entire event line. This is
110 used in conjunction with `:s' (e.g., `:gs/old/new/') or `:&'.
111 If used with `:s', any delimiter can be used in place of /, and
112 the final delimiter is optional if it is the last character of
113 the event line. An a may be used as a synonym for g.
114 G Apply the following `s' modifier once to each word in the event
115 line.
116
118 This section describes how to use the History library in other pro‐
119 grams.
120
121 Introduction to History
122 The programmer using the History library has available functions for
123 remembering lines on a history list, associating arbitrary data with a
124 line, removing lines from the list, searching through the list for a
125 line containing an arbitrary text string, and referencing any line in
126 the list directly. In addition, a history expansion function is avail‐
127 able which provides for a consistent user interface across different
128 programs.
129
130 The user using programs written with the History library has the bene‐
131 fit of a consistent user interface with a set of well-known commands
132 for manipulating the text of previous lines and using that text in new
133 commands. The basic history manipulation commands are identical to the
134 history substitution provided by bash.
135
136 If the programmer desires, he can use the Readline library, which
137 includes some history manipulation by default, and has the added advan‐
138 tage of command line editing.
139
140 Before declaring any functions using any functionality the History
141 library provides in other code, an application writer should include
142 the file <readline/history.h> in any file that uses the History
143 library's features. It supplies extern declarations for all of the
144 library's public functions and variables, and declares all of the pub‐
145 lic data structures.
146
147
148 History Storage
149 The history list is an array of history entries. A history entry is
150 declared as follows:
151
152 typedef void * histdata_t;
153
154 typedef struct _hist_entry {
155 char *line;
156 char *timestamp;
157 histdata_t data;
158 } HIST_ENTRY;
159
160 The history list itself might therefore be declared as
161
162 HIST_ENTRY ** the_history_list;
163
164 The state of the History library is encapsulated into a single struc‐
165 ture:
166
167 /*
168 * A structure used to pass around the current state of the history.
169 */
170 typedef struct _hist_state {
171 HIST_ENTRY **entries; /* Pointer to the entries themselves. */
172 int offset; /* The location pointer within this array. */
173 int length; /* Number of elements within this array. */
174 int size; /* Number of slots allocated to this array. */
175 int flags;
176 } HISTORY_STATE;
177
178 If the flags member includes HS_STIFLED, the history has been stifled.
179
181 This section describes the calling sequence for the various functions
182 exported by the GNU History library.
183
184 Initializing History and State Management
185 This section describes functions used to initialize and manage the
186 state of the History library when you want to use the history functions
187 in your program.
188
189 void using_history (void)
190 Begin a session in which the history functions might be used. This
191 initializes the interactive variables.
192
193 HISTORY_STATE * history_get_history_state (void)
194 Return a structure describing the current state of the input history.
195
196 void history_set_history_state (HISTORY_STATE *state)
197 Set the state of the history list according to state.
198
199
200 History List Management
201 These functions manage individual entries on the history list, or set
202 parameters managing the list itself.
203
204 void add_history (const char *string)
205 Place string at the end of the history list. The associated data field
206 (if any) is set to NULL.
207
208 void add_history_time (const char *string)
209 Change the time stamp associated with the most recent history entry to
210 string.
211
212 HIST_ENTRY * remove_history (int which)
213 Remove history entry at offset which from the history. The removed
214 element is returned so you can free the line, data, and containing
215 structure.
216
217 histdata_t free_history_entry (HIST_ENTRY *histent)
218 Free the history entry histent and any history library private data
219 associated with it. Returns the application-specific data so the call‐
220 er can dispose of it.
221
222 HIST_ENTRY * replace_history_entry (int which, const char *line, hist‐
223 data_t data)
224 Make the history entry at offset which have line and data. This
225 returns the old entry so the caller can dispose of any application-spe‐
226 cific data. In the case of an invalid which, a NULL pointer is
227 returned.
228
229 void clear_history (void)
230 Clear the history list by deleting all the entries.
231
232 void stifle_history (int max)
233 Stifle the history list, remembering only the last max entries.
234
235 int unstifle_history (void)
236 Stop stifling the history. This returns the previously-set maximum
237 number of history entries (as set by stifle_history()). history was
238 stifled. The value is positive if the history was stifled, negative if
239 it wasn't.
240
241 int history_is_stifled (void)
242 Returns non-zero if the history is stifled, zero if it is not.
243
244
245 Information About the History List
246 These functions return information about the entire history list or
247 individual list entries.
248
249 HIST_ENTRY ** history_list (void)
250 Return a NULL terminated array of HIST_ENTRY * which is the current
251 input history. Element 0 of this list is the beginning of time. If
252 there is no history, return NULL.
253
254 int where_history (void)
255 Returns the offset of the current history element.
256
257 HIST_ENTRY * current_history (void)
258 Return the history entry at the current position, as determined by
259 where_history(). If there is no entry there, return a NULL pointer.
260
261 HIST_ENTRY * history_get (int offset)
262 Return the history entry at position offset, starting from his‐
263 tory_base. If there is no entry there, or if offset is greater than
264 the history length, return a NULL pointer.
265
266 time_t history_get_time (HIST_ENTRY *)
267 Return the time stamp associated with the history entry passed as the
268 argument.
269
270 int history_total_bytes (void)
271 Return the number of bytes that the primary history entries are using.
272 This function returns the sum of the lengths of all the lines in the
273 history.
274
275
276 Moving Around the History List
277 These functions allow the current index into the history list to be set
278 or changed.
279
280 int history_set_pos (int pos)
281 Set the current history offset to pos, an absolute index into the list.
282 Returns 1 on success, 0 if pos is less than zero or greater than the
283 number of history entries.
284
285 HIST_ENTRY * previous_history (void)
286 Back up the current history offset to the previous history entry, and
287 return a pointer to that entry. If there is no previous entry, return
288 a NULL pointer.
289
290 HIST_ENTRY * next_history (void)
291 Move the current history offset forward to the next history entry, and
292 return the a pointer to that entry. If there is no next entry, return
293 a NULL pointer.
294
295
296 Searching the History List
297 These functions allow searching of the history list for entries con‐
298 taining a specific string. Searching may be performed both forward and
299 backward from the current history position. The search may be
300 anchored, meaning that the string must match at the beginning of the
301 history entry.
302
303 int history_search (const char *string, int direction)
304 Search the history for string, starting at the current history offset.
305 If direction is less than 0, then the search is through previous
306 entries, otherwise through subsequent entries. If string is found,
307 then the current history index is set to that history entry, and the
308 value returned is the offset in the line of the entry where string was
309 found. Otherwise, nothing is changed, and a -1 is returned.
310
311 int history_search_prefix (const char *string, int direction)
312 Search the history for string, starting at the current history offset.
313 The search is anchored: matching lines must begin with string. If
314 direction is less than 0, then the search is through previous entries,
315 otherwise through subsequent entries. If string is found, then the
316 current history index is set to that entry, and the return value is 0.
317 Otherwise, nothing is changed, and a -1 is returned.
318
319 int history_search_pos (const char *string, int direction, int pos)
320 Search for string in the history list, starting at pos, an absolute
321 index into the list. If direction is negative, the search proceeds
322 backward from pos, otherwise forward. Returns the absolute index of
323 the history element where string was found, or -1 otherwise.
324
325
326 Managing the History File
327 The History library can read the history from and write it to a file.
328 This section documents the functions for managing a history file.
329
330 int read_history (const char *filename)
331 Add the contents of filename to the history list, a line at a time. If
332 filename is NULL, then read from ~/.history. Returns 0 if successful,
333 or errno if not.
334
335 int read_history_range (const char *filename, int from, int to)
336 Read a range of lines from filename, adding them to the history list.
337 Start reading at line from and end at to. If from is zero, start at
338 the beginning. If to is less than from, then read until the end of the
339 file. If filename is NULL, then read from ~/.history. Returns 0 if
340 successful, or errno if not.
341
342 int write_history (const char *filename)
343 Write the current history to filename, overwriting filename if neces‐
344 sary. If filename is NULL, then write the history list to ~/.history.
345 Returns 0 on success, or errno on a read or write error.
346
347
348 int append_history (int nelements, const char *filename)
349 Append the last nelements of the history list to filename. If filename
350 is NULL, then append to ~/.history. Returns 0 on success, or errno on
351 a read or write error.
352
353 int history_truncate_file (const char *filename, int nlines)
354 Truncate the history file filename, leaving only the last nlines lines.
355 If filename is NULL, then ~/.history is truncated. Returns 0 on suc‐
356 cess, or errno on failure.
357
358
359 History Expansion
360 These functions implement history expansion.
361
362 int history_expand (char *string, char **output)
363 Expand string, placing the result into output, a pointer to a string.
364 Returns:
365 0 If no expansions took place (or, if the only change in
366 the text was the removal of escape characters preceding
367 the history expansion character);
368 1 if expansions did take place;
369 -1 if there was an error in expansion;
370 2 if the returned line should be displayed, but not exe‐
371 cuted, as with the :p modifier.
372 If an error ocurred in expansion, then output contains a descriptive
373 error message.
374
375 char * get_history_event (const char *string, int *cindex, int qchar)
376 Returns the text of the history event beginning at string + *cindex.
377 *cindex is modified to point to after the event specifier. At function
378 entry, cindex points to the index into string where the history event
379 specification begins. qchar is a character that is allowed to end the
380 event specification in addition to the ``normal'' terminating charac‐
381 ters.
382
383 char ** history_tokenize (const char *string)
384 Return an array of tokens parsed out of string, much as the shell
385 might. The tokens are split on the characters in the his‐
386 tory_word_delimiters variable, and shell quoting conventions are
387 obeyed.
388
389 char * history_arg_extract (int first, int last, const char *string)
390 Extract a string segment consisting of the first through last arguments
391 present in string. Arguments are split using history_tokenize().
392
393
394 History Variables
395 This section describes the externally-visible variables exported by the
396 GNU History Library.
397
398 int history_base
399 The logical offset of the first entry in the history list.
400
401 int history_length
402 The number of entries currently stored in the history list.
403
404 int history_max_entries
405 The maximum number of history entries. This must be changed using sti‐
406 fle_history().
407
408 int history_write_timestamps
409 If non-zero, timestamps are written to the history file, so they can be
410 preserved between sessions. The default value is 0, meaning that time‐
411 stamps are not saved.
412
413 char history_expansion_char
414 The character that introduces a history event. The default is !. Set‐
415 ting this to 0 inhibits history expansion.
416
417 char history_subst_char
418 The character that invokes word substitution if found at the start of a
419 line. The default is ^.
420
421 char history_comment_char
422 During tokenization, if this character is seen as the first character
423 of a word, then it and all subsequent characters up to a newline are
424 ignored, suppressing history expansion for the remainder of the line.
425 This is disabled by default.
426
427 char * history_word_delimiters
428 The characters that separate tokens for history_tokenize(). The
429 default value is " \t\n()<>;&|".
430
431 char * history_no_expand_chars
432 The list of characters which inhibit history expansion if found immedi‐
433 ately following history_expansion_char. The default is space, tab,
434 newline, \r, and =.
435
436 char * history_search_delimiter_chars
437 The list of additional characters which can delimit a history search
438 string, in addition to space, tab, : and ? in the case of a substring
439 search. The default is empty.
440
441 int history_quotes_inhibit_expansion
442 If non-zero, single-quoted words are not scanned for the history expan‐
443 sion character. The default value is 0.
444
445 rl_linebuf_func_t * history_inhibit_expansion_function
446 This should be set to the address of a function that takes two argu‐
447 ments: a char * (string) and an int index into that string (i). It
448 should return a non-zero value if the history expansion starting at
449 string[i] should not be performed; zero if the expansion should be
450 done. It is intended for use by applications like bash that use the
451 history expansion character for additional purposes. By default, this
452 variable is set to NULL.
453
455 ~/.history
456 Default filename for reading and writing saved history
457
459 The Gnu Readline Library, Brian Fox and Chet Ramey
460 The Gnu History Library, Brian Fox and Chet Ramey
461 bash(1)
462 readline(3)
463
465 Brian Fox, Free Software Foundation
466 bfox@gnu.org
467
468 Chet Ramey, Case Western Reserve University
469 chet@ins.CWRU.Edu
470
472 If you find a bug in the history library, you should report it. But
473 first, you should make sure that it really is a bug, and that it
474 appears in the latest version of the history library that you have.
475
476 Once you have determined that a bug actually exists, mail a bug report
477 to bug-readline@gnu.org. If you have a fix, you are welcome to mail
478 that as well! Suggestions and `philosophical' bug reports may be
479 mailed to bug-readline@gnu.org or posted to the Usenet newsgroup
480 gnu.bash.bug.
481
482 Comments and bug reports concerning this manual page should be directed
483 to chet@ins.CWRU.Edu.
484
485
486
487GNU History 5.0 2003 July 31 HISTORY(3)