* rluser.texinfo (Sample Init File): Prevent overfull hboxes.
[deliverable/binutils-gdb.git] / readline / doc / hstech.texinfo
CommitLineData
d60d9f65
SS
1@ignore
2This file documents the user interface to the GNU History library.
3
4Copyright (C) 1988, 1991, 1994, 1996 Free Software Foundation, Inc.
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
30with the GNU History Library.
31It should be considered a technical guide.
32For information on the interactive use of GNU History, @pxref{Using
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
46Many programs read input from the user a line at a time. The GNU History
47library is able to keep track of those lines, associate arbitrary data with
48each line, and utilize information from previous lines in composing new
49ones.
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
1b17e766
EZ
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
d60d9f65
SS
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
83typedef struct _hist_entry @{
84 char *line;
85 char *data;
86@} HIST_ENTRY;
87@end example
88
89The history list itself might therefore be declared as
90
91@example
92HIST_ENTRY **the_history_list;
93@end example
94
95The state of the History library is encapsulated into a single structure:
96
97@example
98/* A structure used to pass the current state of the history stuff around. */
99typedef struct _hist_state @{
100 HIST_ENTRY **entries; /* Pointer to the entries themselves. */
101 int offset; /* The location pointer within this array. */
102 int length; /* Number of elements within this array. */
103 int size; /* Number of slots allocated to this array. */
104 int flags;
105@} HISTORY_STATE;
106@end example
107
108If the flags member includes @code{HS_STIFLED}, the history has been
109stifled.
110
111@node History Functions
112@section History Functions
113
114This section describes the calling sequence for the various functions
115present in GNU History.
116
117@menu
118* Initializing History and State Management:: Functions to call when you
119 want to use history in a
120 program.
121* History List Management:: Functions used to manage the list
122 of history entries.
123* Information About the History List:: Functions returning information about
124 the history list.
125* Moving Around the History List:: Functions used to change the position
126 in the history list.
127* Searching the History List:: Functions to search the history list
128 for entries containing a string.
129* Managing the History File:: Functions that read and write a file
130 containing the history list.
131* History Expansion:: Functions to perform csh-like history
132 expansion.
133@end menu
134
135@node Initializing History and State Management
136@subsection Initializing History and State Management
137
138This section describes functions used to initialize and manage
139the state of the History library when you want to use the history
140functions in your program.
141
142@deftypefun void using_history ()
143Begin a session in which the history functions might be used. This
144initializes the interactive variables.
145@end deftypefun
146
147@deftypefun {HISTORY_STATE *} history_get_history_state ()
148Return a structure describing the current state of the input history.
149@end deftypefun
150
151@deftypefun void history_set_history_state (HISTORY_STATE *state)
152Set the state of the history list according to @var{state}.
153@end deftypefun
154
155@node History List Management
156@subsection History List Management
157
158These functions manage individual entries on the history list, or set
159parameters managing the list itself.
160
161@deftypefun void add_history (char *string)
162Place @var{string} at the end of the history list. The associated data
163field (if any) is set to @code{NULL}.
164@end deftypefun
165
166@deftypefun {HIST_ENTRY *} remove_history (int which)
167Remove history entry at offset @var{which} from the history. The
168removed element is returned so you can free the line, data,
169and containing structure.
170@end deftypefun
171
172@deftypefun {HIST_ENTRY *} replace_history_entry (int which, char *line, char *data)
173Make the history entry at offset @var{which} have @var{line} and @var{data}.
174This returns the old entry so you can dispose of the data. In the case
175of an invalid @var{which}, a @code{NULL} pointer is returned.
176@end deftypefun
177
178@deftypefun void clear_history ()
179Clear the history list by deleting all the entries.
180@end deftypefun
181
182@deftypefun void stifle_history (int max)
183Stifle the history list, remembering only the last @var{max} entries.
184@end deftypefun
185
186@deftypefun int unstifle_history ()
187Stop stifling the history. This returns the previous amount the
188history was stifled. The value is positive if the history was
189stifled, negative if it wasn't.
190@end deftypefun
191
192@deftypefun int history_is_stifled ()
193Returns non-zero if the history is stifled, zero if it is not.
194@end deftypefun
195
196@node Information About the History List
197@subsection Information About the History List
198
199These functions return information about the entire history list or
200individual list entries.
201
202@deftypefun {HIST_ENTRY **} history_list ()
203Return a @code{NULL} terminated array of @code{HIST_ENTRY} which is the
204current input history. Element 0 of this list is the beginning of time.
205If there is no history, return @code{NULL}.
206@end deftypefun
207
208@deftypefun int where_history ()
209Returns the offset of the current history element.
210@end deftypefun
211
212@deftypefun {HIST_ENTRY *} current_history ()
213Return the history entry at the current position, as determined by
214@code{where_history ()}. If there is no entry there, return a @code{NULL}
215pointer.
216@end deftypefun
217
218@deftypefun {HIST_ENTRY *} history_get (int offset)
219Return the history entry at position @var{offset}, starting from
220@code{history_base}. If there is no entry there, or if @var{offset}
221is greater than the history length, return a @code{NULL} pointer.
222@end deftypefun
223
224@deftypefun int history_total_bytes ()
225Return the number of bytes that the primary history entries are using.
226This function returns the sum of the lengths of all the lines in the
227history.
228@end deftypefun
229
230@node Moving Around the History List
231@subsection Moving Around the History List
232
233These functions allow the current index into the history list to be
234set or changed.
235
236@deftypefun int history_set_pos (int pos)
237Set the position in the history list to @var{pos}, an absolute index
238into the list.
239@end deftypefun
240
241@deftypefun {HIST_ENTRY *} previous_history ()
242Back up the current history offset to the previous history entry, and
243return a pointer to that entry. If there is no previous entry, return
244a @code{NULL} pointer.
245@end deftypefun
246
247@deftypefun {HIST_ENTRY *} next_history ()
248Move the current history offset forward to the next history entry, and
249return the a pointer to that entry. If there is no next entry, return
250a @code{NULL} pointer.
251@end deftypefun
252
253@node Searching the History List
254@subsection Searching the History List
255@cindex History Searching
256
257These functions allow searching of the history list for entries containing
258a specific string. Searching may be performed both forward and backward
259from the current history position. The search may be @dfn{anchored},
260meaning that the string must match at the beginning of the history entry.
261@cindex anchored search
262
263@deftypefun int history_search (char *string, int direction)
264Search the history for @var{string}, starting at the current history
265offset. If @var{direction} < 0, then the search is through previous entries,
266else through subsequent. If @var{string} is found, then
267the current history index is set to that history entry, and the value
268returned is the offset in the line of the entry where
269@var{string} was found. Otherwise, nothing is changed, and a -1 is
270returned.
271@end deftypefun
272
273@deftypefun int history_search_prefix (char *string, int direction)
274Search the history for @var{string}, starting at the current history
275offset. The search is anchored: matching lines must begin with
276@var{string}. If @var{direction} < 0, then the search is through previous
277entries, else through subsequent. If @var{string} is found, then the
278current history index is set to that entry, and the return value is 0.
279Otherwise, nothing is changed, and a -1 is returned.
280@end deftypefun
281
282@deftypefun int history_search_pos (char *string, int direction, int pos)
283Search for @var{string} in the history list, starting at @var{pos}, an
284absolute index into the list. If @var{direction} is negative, the search
285proceeds backward from @var{pos}, otherwise forward. Returns the absolute
286index of the history element where @var{string} was found, or -1 otherwise.
287@end deftypefun
288
289@node Managing the History File
290@subsection Managing the History File
291
292The History library can read the history from and write it to a file.
293This section documents the functions for managing a history file.
294
295@deftypefun int read_history (char *filename)
296Add the contents of @var{filename} to the history list, a line at a
297time. If @var{filename} is @code{NULL}, then read from
298@file{~/.history}. Returns 0 if successful, or errno if not.
299@end deftypefun
300
301@deftypefun int read_history_range (char *filename, int from, int to)
302Read a range of lines from @var{filename}, adding them to the history list.
303Start reading at line @var{from} and end at @var{to}. If
304@var{from} is zero, start at the beginning. If @var{to} is less than
305@var{from}, then read until the end of the file. If @var{filename} is
306@code{NULL}, then read from @file{~/.history}. Returns 0 if successful,
307or @code{errno} if not.
308@end deftypefun
309
310@deftypefun int write_history (char *filename)
311Write the current history to @var{filename}, overwriting @var{filename}
312if necessary. If @var{filename} is
313@code{NULL}, then write the history list to @file{~/.history}. Values
314returned are as in @code{read_history ()}.
315@end deftypefun
316
317@deftypefun int append_history (int nelements, char *filename)
318Append the last @var{nelements} of the history list to @var{filename}.
319@end deftypefun
320
321@deftypefun int history_truncate_file (char *filename, int nlines)
322Truncate the history file @var{filename}, leaving only the last
323@var{nlines} lines.
324@end deftypefun
325
326@node History Expansion
327@subsection History Expansion
328
329These functions implement @code{csh}-like history expansion.
330
331@deftypefun int history_expand (char *string, char **output)
332Expand @var{string}, placing the result into @var{output}, a pointer
333to a string (@pxref{History Interaction}). Returns:
334@table @code
335@item 0
336If no expansions took place (or, if the only change in
337the text was the de-slashifying of the history expansion
338character);
339@item 1
340if expansions did take place;
341@item -1
342if there was an error in expansion;
343@item 2
1b17e766 344if the returned line should be displayed, but not executed,
d60d9f65
SS
345as with the @code{:p} modifier (@pxref{Modifiers}).
346@end table
347
348If an error ocurred in expansion, then @var{output} contains a descriptive
349error message.
350@end deftypefun
351
352@deftypefun {char *} history_arg_extract (int first, int last, char *string)
353Extract a string segment consisting of the @var{first} through @var{last}
354arguments present in @var{string}. Arguments are broken up as in Bash.
355@end deftypefun
356
357@deftypefun {char *} get_history_event (char *string, int *cindex, int qchar)
358Returns the text of the history event beginning at @var{string} +
359@var{*cindex}. @var{*cindex} is modified to point to after the event
360specifier. At function entry, @var{cindex} points to the index into
361@var{string} where the history event specification begins. @var{qchar}
362is a character that is allowed to end the event specification in addition
363to the ``normal'' terminating characters.
364@end deftypefun
365
366@deftypefun {char **} history_tokenize (char *string)
367Return an array of tokens parsed out of @var{string}, much as the
368shell might. The tokens are split on white space and on the
369characters @code{()<>;&|$}, and shell quoting conventions are
370obeyed.
371@end deftypefun
372
373@node History Variables
374@section History Variables
375
376This section describes the externally visible variables exported by
377the GNU History Library.
378
379@deftypevar int history_base
380The logical offset of the first entry in the history list.
381@end deftypevar
382
383@deftypevar int history_length
384The number of entries currently stored in the history list.
385@end deftypevar
386
387@deftypevar int max_input_history
388The maximum number of history entries. This must be changed using
389@code{stifle_history ()}.
390@end deftypevar
391
392@deftypevar char history_expansion_char
393The character that starts a history event. The default is @samp{!}.
394@end deftypevar
395
396@deftypevar char history_subst_char
397The character that invokes word substitution if found at the start of
398a line. The default is @samp{^}.
399@end deftypevar
400
401@deftypevar char history_comment_char
402During tokenization, if this character is seen as the first character
403of a word, then it and all subsequent characters up to a newline are
404ignored, suppressing history expansion for the remainder of the line.
405This is disabled by default.
406@end deftypevar
407
408@deftypevar {char *} history_no_expand_chars
409The list of characters which inhibit history expansion if found immediately
410following @var{history_expansion_char}. The default is whitespace and
411@samp{=}.
412@end deftypevar
413
414@deftypevar {char *} history_search_delimiter_chars
415The list of additional characters which can delimit a history search
416string, in addition to whitespace, @samp{:} and @samp{?} in the case of
417a substring search. The default is empty.
418@end deftypevar
419
420@deftypevar int history_quotes_inhibit_expansion
421If non-zero, single-quoted words are not scanned for the history expansion
422character. The default value is 0.
423@end deftypevar
424
425@deftypevar {Function *} history_inhibit_expansion_function
426This should be set to the address of a function that takes two arguments:
427a @code{char *} (@var{string}) and an integer index into that string (@var{i}).
428It should return a non-zero value if the history expansion starting at
429@var{string[i]} should not be performed; zero if the expansion should
430be done.
431It is intended for use by applications like Bash that use the history
432expansion character for additional purposes.
433By default, this variable is set to NULL.
434@end deftypevar
435
436@node History Programming Example
437@section History Programming Example
438
439The following program demonstrates simple use of the GNU History Library.
440
441@smallexample
442main ()
443@{
444 char line[1024], *t;
445 int len, done = 0;
446
447 line[0] = 0;
448
449 using_history ();
450 while (!done)
451 @{
452 printf ("history$ ");
453 fflush (stdout);
454 t = fgets (line, sizeof (line) - 1, stdin);
455 if (t && *t)
456 @{
457 len = strlen (t);
458 if (t[len - 1] == '\n')
459 t[len - 1] = '\0';
460 @}
461
462 if (!t)
463 strcpy (line, "quit");
464
465 if (line[0])
466 @{
467 char *expansion;
468 int result;
469
470 result = history_expand (line, &expansion);
471 if (result)
472 fprintf (stderr, "%s\n", expansion);
473
474 if (result < 0 || result == 2)
475 @{
476 free (expansion);
477 continue;
478 @}
479
480 add_history (expansion);
481 strncpy (line, expansion, sizeof (line) - 1);
482 free (expansion);
483 @}
484
485 if (strcmp (line, "quit") == 0)
486 done = 1;
487 else if (strcmp (line, "save") == 0)
488 write_history ("history_file");
489 else if (strcmp (line, "read") == 0)
490 read_history ("history_file");
491 else if (strcmp (line, "list") == 0)
492 @{
493 register HIST_ENTRY **the_list;
494 register int i;
495
496 the_list = history_list ();
497 if (the_list)
498 for (i = 0; the_list[i]; i++)
499 printf ("%d: %s\n", i + history_base, the_list[i]->line);
500 @}
501 else if (strncmp (line, "delete", 6) == 0)
502 @{
503 int which;
504 if ((sscanf (line + 6, "%d", &which)) == 1)
505 @{
506 HIST_ENTRY *entry = remove_history (which);
507 if (!entry)
508 fprintf (stderr, "No such entry %d\n", which);
509 else
510 @{
511 free (entry->line);
512 free (entry);
513 @}
514 @}
515 else
516 @{
517 fprintf (stderr, "non-numeric arg given to `delete'\n");
518 @}
519 @}
520 @}
521@}
522@end smallexample
This page took 0.132065 seconds and 4 git commands to generate.