]> git.ipfire.org Git - thirdparty/bash.git/blame - lib/readline/doc/hstech.texinfo
Imported from ../bash-2.05.tar.gz.
[thirdparty/bash.git] / lib / readline / doc / hstech.texinfo
CommitLineData
726f6388
JA
1@ignore
2This file documents the user interface to the GNU History library.
3
28ef6c31 4Copyright (C) 1988-2001 Free Software Foundation, Inc.
726f6388
JA
5Authored by Brian Fox and Chet Ramey.
6
7Permission is granted to make and distribute verbatim copies of this manual
8provided the copyright notice and this permission notice are preserved on
9all copies.
10
11Permission is granted to process this file through Tex and print the
12results, provided the printed document carries copying permission notice
13identical to this one except for the removal of this paragraph (this
14paragraph not being relevant to the printed manual).
15
16Permission is granted to copy and distribute modified versions of this
17manual under the conditions for verbatim copying, provided also that the
18GNU Copyright statement is available to the distributee, and provided that
19the entire resulting derived work is distributed under the terms of a
20permission notice identical to this one.
21
22Permission is granted to copy and distribute translations of this manual
23into another language, under the above conditions for modified versions.
24@end ignore
25
26@node Programming with GNU History
27@chapter Programming with GNU History
28
29This chapter describes how to interface programs that you write
28ef6c31 30with the @sc{gnu} History Library.
726f6388 31It should be considered a technical guide.
28ef6c31 32For information on the interactive use of @sc{gnu} History, @pxref{Using
726f6388
JA
33History Interactively}.
34
35@menu
36* Introduction to History:: What is the GNU History library for?
37* History Storage:: How information is stored.
38* History Functions:: Functions that you can use.
39* History Variables:: Variables that control behaviour.
40* History Programming Example:: Example of using the GNU History Library.
41@end menu
42
43@node Introduction to History
44@section Introduction to History
45
28ef6c31
JA
46Many programs read input from the user a line at a time. The @sc{gnu}
47History library is able to keep track of those lines, associate arbitrary
48data with each line, and utilize information from previous lines in
49composing new ones.
726f6388
JA
50
51The programmer using the History library has available functions
52for remembering lines on a history list, associating arbitrary data
53with a line, removing lines from the list, searching through the list
54for a line containing an arbitrary text string, and referencing any line
55in the list directly. In addition, a history @dfn{expansion} function
56is available which provides for a consistent user interface across
57different programs.
58
59The user using programs written with the History library has the
60benefit of a consistent user interface with a set of well-known
61commands for manipulating the text of previous lines and using that text
62in new commands. The basic history manipulation commands are similar to
63the history substitution provided by @code{csh}.
64
65If the programmer desires, he can use the Readline library, which
66includes some history manipulation by default, and has the added
67advantage of command line editing.
68
bb70624e
JA
69Before declaring any functions using any functionality the History
70library provides in other code, an application writer should include
71the file @code{<readline/history.h>} in any file that uses the
72History library's features. It supplies extern declarations for all
73of the library's public functions and variables, and declares all of
74the public data structures.
75
726f6388
JA
76@node History Storage
77@section History Storage
78
79The history list is an array of history entries. A history entry is
80declared as follows:
81
82@example
28ef6c31
JA
83typedef void *histdata_t;
84
726f6388
JA
85typedef struct _hist_entry @{
86 char *line;
28ef6c31 87 histdata_t data;
726f6388
JA
88@} HIST_ENTRY;
89@end example
90
91The history list itself might therefore be declared as
92
93@example
94HIST_ENTRY **the_history_list;
95@end example
96
97The state of the History library is encapsulated into a single structure:
98
99@example
28ef6c31
JA
100/*
101 * A structure used to pass around the current state of the history.
102 */
726f6388 103typedef struct _hist_state @{
28ef6c31
JA
104 HIST_ENTRY **entries; /* Pointer to the entries themselves. */
105 int offset; /* The location pointer within this array. */
106 int length; /* Number of elements within this array. */
107 int size; /* Number of slots allocated to this array. */
726f6388
JA
108 int flags;
109@} HISTORY_STATE;
110@end example
111
112If the flags member includes @code{HS_STIFLED}, the history has been
113stifled.
114
115@node History Functions
116@section History Functions
117
118This section describes the calling sequence for the various functions
28ef6c31 119exported by the @sc{gnu} History library.
726f6388
JA
120
121@menu
122* Initializing History and State Management:: Functions to call when you
123 want to use history in a
124 program.
125* History List Management:: Functions used to manage the list
126 of history entries.
127* Information About the History List:: Functions returning information about
128 the history list.
129* Moving Around the History List:: Functions used to change the position
130 in the history list.
131* Searching the History List:: Functions to search the history list
132 for entries containing a string.
133* Managing the History File:: Functions that read and write a file
134 containing the history list.
135* History Expansion:: Functions to perform csh-like history
136 expansion.
137@end menu
138
139@node Initializing History and State Management
140@subsection Initializing History and State Management
141
142This section describes functions used to initialize and manage
143the state of the History library when you want to use the history
144functions in your program.
145
28ef6c31 146@deftypefun void using_history (void)
726f6388
JA
147Begin a session in which the history functions might be used. This
148initializes the interactive variables.
149@end deftypefun
150
28ef6c31 151@deftypefun {HISTORY_STATE *} history_get_history_state (void)
726f6388
JA
152Return a structure describing the current state of the input history.
153@end deftypefun
154
155@deftypefun void history_set_history_state (HISTORY_STATE *state)
156Set the state of the history list according to @var{state}.
157@end deftypefun
158
159@node History List Management
160@subsection History List Management
161
162These functions manage individual entries on the history list, or set
163parameters managing the list itself.
164
28ef6c31 165@deftypefun void add_history (const char *string)
726f6388
JA
166Place @var{string} at the end of the history list. The associated data
167field (if any) is set to @code{NULL}.
168@end deftypefun
169
170@deftypefun {HIST_ENTRY *} remove_history (int which)
171Remove history entry at offset @var{which} from the history. The
172removed element is returned so you can free the line, data,
173and containing structure.
174@end deftypefun
175
28ef6c31 176@deftypefun {HIST_ENTRY *} replace_history_entry (int which, const char *line, histdata_t data)
726f6388
JA
177Make the history entry at offset @var{which} have @var{line} and @var{data}.
178This returns the old entry so you can dispose of the data. In the case
179of an invalid @var{which}, a @code{NULL} pointer is returned.
180@end deftypefun
181
28ef6c31 182@deftypefun void clear_history (void)
ccc6cda3
JA
183Clear the history list by deleting all the entries.
184@end deftypefun
185
726f6388
JA
186@deftypefun void stifle_history (int max)
187Stifle the history list, remembering only the last @var{max} entries.
188@end deftypefun
189
28ef6c31 190@deftypefun int unstifle_history (void)
726f6388
JA
191Stop stifling the history. This returns the previous amount the
192history was stifled. The value is positive if the history was
193stifled, negative if it wasn't.
194@end deftypefun
195
28ef6c31 196@deftypefun int history_is_stifled (void)
726f6388
JA
197Returns non-zero if the history is stifled, zero if it is not.
198@end deftypefun
199
200@node Information About the History List
201@subsection Information About the History List
202
203These functions return information about the entire history list or
204individual list entries.
205
28ef6c31
JA
206@deftypefun {HIST_ENTRY **} history_list (void)
207Return a @code{NULL} terminated array of @code{HIST_ENTRY *} which is the
726f6388
JA
208current input history. Element 0 of this list is the beginning of time.
209If there is no history, return @code{NULL}.
210@end deftypefun
211
28ef6c31 212@deftypefun int where_history (void)
726f6388
JA
213Returns the offset of the current history element.
214@end deftypefun
215
28ef6c31 216@deftypefun {HIST_ENTRY *} current_history (void)
726f6388 217Return the history entry at the current position, as determined by
28ef6c31 218@code{where_history()}. If there is no entry there, return a @code{NULL}
726f6388
JA
219pointer.
220@end deftypefun
221
222@deftypefun {HIST_ENTRY *} history_get (int offset)
223Return the history entry at position @var{offset}, starting from
28ef6c31
JA
224@code{history_base} (@pxref{History Variables}).
225If there is no entry there, or if @var{offset}
726f6388
JA
226is greater than the history length, return a @code{NULL} pointer.
227@end deftypefun
228
28ef6c31 229@deftypefun int history_total_bytes (void)
726f6388
JA
230Return the number of bytes that the primary history entries are using.
231This function returns the sum of the lengths of all the lines in the
232history.
233@end deftypefun
234
235@node Moving Around the History List
236@subsection Moving Around the History List
237
238These functions allow the current index into the history list to be
239set or changed.
240
241@deftypefun int history_set_pos (int pos)
28ef6c31 242Set the current history offset to @var{pos}, an absolute index
726f6388 243into the list.
28ef6c31
JA
244Returns 1 on success, 0 if @var{pos} is less than zero or greater
245than the number of history entries.
726f6388
JA
246@end deftypefun
247
28ef6c31 248@deftypefun {HIST_ENTRY *} previous_history (void)
726f6388
JA
249Back up the current history offset to the previous history entry, and
250return a pointer to that entry. If there is no previous entry, return
251a @code{NULL} pointer.
252@end deftypefun
253
28ef6c31 254@deftypefun {HIST_ENTRY *} next_history (void)
726f6388
JA
255Move the current history offset forward to the next history entry, and
256return the a pointer to that entry. If there is no next entry, return
257a @code{NULL} pointer.
258@end deftypefun
259
260@node Searching the History List
261@subsection Searching the History List
262@cindex History Searching
263
264These functions allow searching of the history list for entries containing
265a specific string. Searching may be performed both forward and backward
266from the current history position. The search may be @dfn{anchored},
267meaning that the string must match at the beginning of the history entry.
268@cindex anchored search
269
28ef6c31
JA
270@deftypefun int history_search (const char *string, int direction)
271Search the history for @var{string}, starting at the current history offset.
272If @var{direction} is less than 0, then the search is through
273previous entries, otherwise through subsequent entries.
274If @var{string} is found, then
726f6388
JA
275the current history index is set to that history entry, and the value
276returned is the offset in the line of the entry where
277@var{string} was found. Otherwise, nothing is changed, and a -1 is
278returned.
279@end deftypefun
280
28ef6c31 281@deftypefun int history_search_prefix (const char *string, int direction)
726f6388
JA
282Search the history for @var{string}, starting at the current history
283offset. The search is anchored: matching lines must begin with
28ef6c31
JA
284@var{string}. If @var{direction} is less than 0, then the search is
285through previous entries, otherwise through subsequent entries.
286If @var{string} is found, then the
726f6388
JA
287current history index is set to that entry, and the return value is 0.
288Otherwise, nothing is changed, and a -1 is returned.
289@end deftypefun
290
28ef6c31 291@deftypefun int history_search_pos (const char *string, int direction, int pos)
726f6388
JA
292Search for @var{string} in the history list, starting at @var{pos}, an
293absolute index into the list. If @var{direction} is negative, the search
294proceeds backward from @var{pos}, otherwise forward. Returns the absolute
295index of the history element where @var{string} was found, or -1 otherwise.
296@end deftypefun
297
298@node Managing the History File
299@subsection Managing the History File
300
301The History library can read the history from and write it to a file.
302This section documents the functions for managing a history file.
303
28ef6c31
JA
304@deftypefun int read_history (const char *filename)
305Add the contents of @var{filename} to the history list, a line at a time.
306If @var{filename} is @code{NULL}, then read from @file{~/.history}.
307Returns 0 if successful, or @code{errno} if not.
726f6388
JA
308@end deftypefun
309
28ef6c31 310@deftypefun int read_history_range (const char *filename, int from, int to)
726f6388 311Read a range of lines from @var{filename}, adding them to the history list.
28ef6c31
JA
312Start reading at line @var{from} and end at @var{to}.
313If @var{from} is zero, start at the beginning. If @var{to} is less than
726f6388
JA
314@var{from}, then read until the end of the file. If @var{filename} is
315@code{NULL}, then read from @file{~/.history}. Returns 0 if successful,
316or @code{errno} if not.
317@end deftypefun
318
28ef6c31 319@deftypefun int write_history (const char *filename)
726f6388 320Write the current history to @var{filename}, overwriting @var{filename}
28ef6c31
JA
321if necessary.
322If @var{filename} is @code{NULL}, then write the history list to
323@file{~/.history}.
324Returns 0 on success, or @code{errno} on a read or write error.
726f6388
JA
325@end deftypefun
326
28ef6c31 327@deftypefun int append_history (int nelements, const char *filename)
726f6388 328Append the last @var{nelements} of the history list to @var{filename}.
28ef6c31
JA
329If @var{filename} is @code{NULL}, then append to @file{~/.history}.
330Returns 0 on success, or @code{errno} on a read or write error.
726f6388
JA
331@end deftypefun
332
28ef6c31 333@deftypefun int history_truncate_file (const char *filename, int nlines)
726f6388
JA
334Truncate the history file @var{filename}, leaving only the last
335@var{nlines} lines.
28ef6c31
JA
336If @var{filename} is @code{NULL}, then @file{~/.history} is truncated.
337Returns 0 on success, or @code{errno} on failure.
726f6388
JA
338@end deftypefun
339
340@node History Expansion
341@subsection History Expansion
342
28ef6c31 343These functions implement history expansion.
726f6388
JA
344
345@deftypefun int history_expand (char *string, char **output)
346Expand @var{string}, placing the result into @var{output}, a pointer
347to a string (@pxref{History Interaction}). Returns:
348@table @code
349@item 0
350If no expansions took place (or, if the only change in
28ef6c31 351the text was the removal of escape characters preceding the history expansion
726f6388
JA
352character);
353@item 1
354if expansions did take place;
355@item -1
356if there was an error in expansion;
357@item 2
bb70624e 358if the returned line should be displayed, but not executed,
726f6388
JA
359as with the @code{:p} modifier (@pxref{Modifiers}).
360@end table
361
362If an error ocurred in expansion, then @var{output} contains a descriptive
363error message.
364@end deftypefun
365
28ef6c31 366@deftypefun {char *} get_history_event (const char *string, int *cindex, int qchar)
726f6388
JA
367Returns the text of the history event beginning at @var{string} +
368@var{*cindex}. @var{*cindex} is modified to point to after the event
369specifier. At function entry, @var{cindex} points to the index into
370@var{string} where the history event specification begins. @var{qchar}
371is a character that is allowed to end the event specification in addition
372to the ``normal'' terminating characters.
373@end deftypefun
374
28ef6c31 375@deftypefun {char **} history_tokenize (const char *string)
726f6388 376Return an array of tokens parsed out of @var{string}, much as the
28ef6c31
JA
377shell might. The tokens are split on the characters in the
378@var{history_word_delimiters} variable,
379and shell quoting conventions are obeyed.
380@end deftypefun
381
382@deftypefun {char *} history_arg_extract (int first, int last, const char *string)
383Extract a string segment consisting of the @var{first} through @var{last}
384arguments present in @var{string}. Arguments are split using
385@code{history_tokenize}.
726f6388
JA
386@end deftypefun
387
388@node History Variables
389@section History Variables
390
28ef6c31
JA
391This section describes the externally-visible variables exported by
392the @sc{gnu} History Library.
726f6388
JA
393
394@deftypevar int history_base
395The logical offset of the first entry in the history list.
396@end deftypevar
397
398@deftypevar int history_length
399The number of entries currently stored in the history list.
400@end deftypevar
401
28ef6c31 402@deftypevar int history_max_entries
726f6388 403The maximum number of history entries. This must be changed using
28ef6c31 404@code{stifle_history()}.
726f6388
JA
405@end deftypevar
406
407@deftypevar char history_expansion_char
28ef6c31
JA
408The character that introduces a history event. The default is @samp{!}.
409Setting this to 0 inhibits history expansion.
726f6388
JA
410@end deftypevar
411
412@deftypevar char history_subst_char
413The character that invokes word substitution if found at the start of
414a line. The default is @samp{^}.
415@end deftypevar
416
417@deftypevar char history_comment_char
418During tokenization, if this character is seen as the first character
419of a word, then it and all subsequent characters up to a newline are
420ignored, suppressing history expansion for the remainder of the line.
421This is disabled by default.
422@end deftypevar
423
28ef6c31
JA
424@deftypevar {char *} history_word_delimiters
425The characters that separate tokens for \fBhistory_tokenize()\fP.
426The default value is @code{" \t\n()<>;&|"}.
427@end deftypevar
428
726f6388
JA
429@deftypevar {char *} history_no_expand_chars
430The list of characters which inhibit history expansion if found immediately
28ef6c31
JA
431following @var{history_expansion_char}. The default is space, tab, newline,
432carriage return, and @samp{=}.
726f6388
JA
433@end deftypevar
434
ccc6cda3
JA
435@deftypevar {char *} history_search_delimiter_chars
436The list of additional characters which can delimit a history search
28ef6c31 437string, in addition to space, TAB, @samp{:} and @samp{?} in the case of
ccc6cda3
JA
438a substring search. The default is empty.
439@end deftypevar
440
441@deftypevar int history_quotes_inhibit_expansion
442If non-zero, single-quoted words are not scanned for the history expansion
443character. The default value is 0.
444@end deftypevar
445
28ef6c31 446@deftypevar {rl_linebuf_func_t *} history_inhibit_expansion_function
d166f048 447This should be set to the address of a function that takes two arguments:
28ef6c31
JA
448a @code{char *} (@var{string})
449and an @code{int} index into that string (@var{i}).
d166f048
JA
450It should return a non-zero value if the history expansion starting at
451@var{string[i]} should not be performed; zero if the expansion should
452be done.
453It is intended for use by applications like Bash that use the history
454expansion character for additional purposes.
28ef6c31 455By default, this variable is set to @code{NULL}.
d166f048
JA
456@end deftypevar
457
726f6388
JA
458@node History Programming Example
459@section History Programming Example
460
28ef6c31 461The following program demonstrates simple use of the @sc{gnu} History Library.
726f6388
JA
462
463@smallexample
28ef6c31
JA
464#include <stdio.h>
465#include <readline/history.h>
466
467main (argc, argv)
468 int argc;
469 char **argv;
726f6388
JA
470@{
471 char line[1024], *t;
472 int len, done = 0;
473
474 line[0] = 0;
475
476 using_history ();
477 while (!done)
478 @{
479 printf ("history$ ");
480 fflush (stdout);
481 t = fgets (line, sizeof (line) - 1, stdin);
482 if (t && *t)
483 @{
484 len = strlen (t);
485 if (t[len - 1] == '\n')
486 t[len - 1] = '\0';
487 @}
488
489 if (!t)
490 strcpy (line, "quit");
491
492 if (line[0])
493 @{
494 char *expansion;
495 int result;
496
497 result = history_expand (line, &expansion);
498 if (result)
499 fprintf (stderr, "%s\n", expansion);
500
501 if (result < 0 || result == 2)
502 @{
503 free (expansion);
504 continue;
505 @}
506
507 add_history (expansion);
508 strncpy (line, expansion, sizeof (line) - 1);
509 free (expansion);
510 @}
511
512 if (strcmp (line, "quit") == 0)
513 done = 1;
514 else if (strcmp (line, "save") == 0)
515 write_history ("history_file");
516 else if (strcmp (line, "read") == 0)
517 read_history ("history_file");
518 else if (strcmp (line, "list") == 0)
519 @{
520 register HIST_ENTRY **the_list;
521 register int i;
522
523 the_list = history_list ();
524 if (the_list)
525 for (i = 0; the_list[i]; i++)
526 printf ("%d: %s\n", i + history_base, the_list[i]->line);
527 @}
528 else if (strncmp (line, "delete", 6) == 0)
529 @{
530 int which;
531 if ((sscanf (line + 6, "%d", &which)) == 1)
532 @{
533 HIST_ENTRY *entry = remove_history (which);
534 if (!entry)
535 fprintf (stderr, "No such entry %d\n", which);
536 else
537 @{
538 free (entry->line);
539 free (entry);
540 @}
541 @}
542 else
543 @{
544 fprintf (stderr, "non-numeric arg given to `delete'\n");
545 @}
546 @}
547 @}
548@}
549@end smallexample