Commit | Line | Data |
---|---|---|
d60d9f65 SS |
1 | @ignore |
2 | This file documents the user interface to the GNU History library. | |
3 | ||
4 | Copyright (C) 1988, 1991, 1994, 1996 Free Software Foundation, Inc. | |
5 | Authored by Brian Fox and Chet Ramey. | |
6 | ||
7 | Permission is granted to make and distribute verbatim copies of this manual | |
8 | provided the copyright notice and this permission notice are preserved on | |
9 | all copies. | |
10 | ||
11 | Permission is granted to process this file through Tex and print the | |
12 | results, provided the printed document carries copying permission notice | |
13 | identical to this one except for the removal of this paragraph (this | |
14 | paragraph not being relevant to the printed manual). | |
15 | ||
16 | Permission is granted to copy and distribute modified versions of this | |
17 | manual under the conditions for verbatim copying, provided also that the | |
18 | GNU Copyright statement is available to the distributee, and provided that | |
19 | the entire resulting derived work is distributed under the terms of a | |
20 | permission notice identical to this one. | |
21 | ||
22 | Permission is granted to copy and distribute translations of this manual | |
23 | into 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 | ||
29 | This chapter describes how to interface programs that you write | |
30 | with the GNU History Library. | |
31 | It should be considered a technical guide. | |
32 | For information on the interactive use of GNU History, @pxref{Using | |
33 | History 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 | ||
46 | Many programs read input from the user a line at a time. The GNU History | |
47 | library is able to keep track of those lines, associate arbitrary data with | |
48 | each line, and utilize information from previous lines in composing new | |
49 | ones. | |
50 | ||
51 | The programmer using the History library has available functions | |
52 | for remembering lines on a history list, associating arbitrary data | |
53 | with a line, removing lines from the list, searching through the list | |
54 | for a line containing an arbitrary text string, and referencing any line | |
55 | in the list directly. In addition, a history @dfn{expansion} function | |
56 | is available which provides for a consistent user interface across | |
57 | different programs. | |
58 | ||
59 | The user using programs written with the History library has the | |
60 | benefit of a consistent user interface with a set of well-known | |
61 | commands for manipulating the text of previous lines and using that text | |
62 | in new commands. The basic history manipulation commands are similar to | |
63 | the history substitution provided by @code{csh}. | |
64 | ||
65 | If the programmer desires, he can use the Readline library, which | |
66 | includes some history manipulation by default, and has the added | |
67 | advantage of command line editing. | |
68 | ||
1b17e766 EZ |
69 | Before declaring any functions using any functionality the History |
70 | library provides in other code, an application writer should include | |
71 | the file @code{<readline/history.h>} in any file that uses the | |
72 | History library's features. It supplies extern declarations for all | |
73 | of the library's public functions and variables, and declares all of | |
74 | the public data structures. | |
75 | ||
d60d9f65 SS |
76 | @node History Storage |
77 | @section History Storage | |
78 | ||
79 | The history list is an array of history entries. A history entry is | |
80 | declared as follows: | |
81 | ||
82 | @example | |
83 | typedef struct _hist_entry @{ | |
84 | char *line; | |
85 | char *data; | |
86 | @} HIST_ENTRY; | |
87 | @end example | |
88 | ||
89 | The history list itself might therefore be declared as | |
90 | ||
91 | @example | |
92 | HIST_ENTRY **the_history_list; | |
93 | @end example | |
94 | ||
95 | The 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. */ | |
99 | typedef 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 | ||
108 | If the flags member includes @code{HS_STIFLED}, the history has been | |
109 | stifled. | |
110 | ||
111 | @node History Functions | |
112 | @section History Functions | |
113 | ||
114 | This section describes the calling sequence for the various functions | |
115 | present 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 | ||
138 | This section describes functions used to initialize and manage | |
139 | the state of the History library when you want to use the history | |
140 | functions in your program. | |
141 | ||
142 | @deftypefun void using_history () | |
143 | Begin a session in which the history functions might be used. This | |
144 | initializes the interactive variables. | |
145 | @end deftypefun | |
146 | ||
147 | @deftypefun {HISTORY_STATE *} history_get_history_state () | |
148 | Return a structure describing the current state of the input history. | |
149 | @end deftypefun | |
150 | ||
151 | @deftypefun void history_set_history_state (HISTORY_STATE *state) | |
152 | Set 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 | ||
158 | These functions manage individual entries on the history list, or set | |
159 | parameters managing the list itself. | |
160 | ||
161 | @deftypefun void add_history (char *string) | |
162 | Place @var{string} at the end of the history list. The associated data | |
163 | field (if any) is set to @code{NULL}. | |
164 | @end deftypefun | |
165 | ||
166 | @deftypefun {HIST_ENTRY *} remove_history (int which) | |
167 | Remove history entry at offset @var{which} from the history. The | |
168 | removed element is returned so you can free the line, data, | |
169 | and containing structure. | |
170 | @end deftypefun | |
171 | ||
172 | @deftypefun {HIST_ENTRY *} replace_history_entry (int which, char *line, char *data) | |
173 | Make the history entry at offset @var{which} have @var{line} and @var{data}. | |
174 | This returns the old entry so you can dispose of the data. In the case | |
175 | of an invalid @var{which}, a @code{NULL} pointer is returned. | |
176 | @end deftypefun | |
177 | ||
178 | @deftypefun void clear_history () | |
179 | Clear the history list by deleting all the entries. | |
180 | @end deftypefun | |
181 | ||
182 | @deftypefun void stifle_history (int max) | |
183 | Stifle the history list, remembering only the last @var{max} entries. | |
184 | @end deftypefun | |
185 | ||
186 | @deftypefun int unstifle_history () | |
187 | Stop stifling the history. This returns the previous amount the | |
188 | history was stifled. The value is positive if the history was | |
189 | stifled, negative if it wasn't. | |
190 | @end deftypefun | |
191 | ||
192 | @deftypefun int history_is_stifled () | |
193 | Returns 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 | ||
199 | These functions return information about the entire history list or | |
200 | individual list entries. | |
201 | ||
202 | @deftypefun {HIST_ENTRY **} history_list () | |
203 | Return a @code{NULL} terminated array of @code{HIST_ENTRY} which is the | |
204 | current input history. Element 0 of this list is the beginning of time. | |
205 | If there is no history, return @code{NULL}. | |
206 | @end deftypefun | |
207 | ||
208 | @deftypefun int where_history () | |
209 | Returns the offset of the current history element. | |
210 | @end deftypefun | |
211 | ||
212 | @deftypefun {HIST_ENTRY *} current_history () | |
213 | Return the history entry at the current position, as determined by | |
214 | @code{where_history ()}. If there is no entry there, return a @code{NULL} | |
215 | pointer. | |
216 | @end deftypefun | |
217 | ||
218 | @deftypefun {HIST_ENTRY *} history_get (int offset) | |
219 | Return the history entry at position @var{offset}, starting from | |
220 | @code{history_base}. If there is no entry there, or if @var{offset} | |
221 | is greater than the history length, return a @code{NULL} pointer. | |
222 | @end deftypefun | |
223 | ||
224 | @deftypefun int history_total_bytes () | |
225 | Return the number of bytes that the primary history entries are using. | |
226 | This function returns the sum of the lengths of all the lines in the | |
227 | history. | |
228 | @end deftypefun | |
229 | ||
230 | @node Moving Around the History List | |
231 | @subsection Moving Around the History List | |
232 | ||
233 | These functions allow the current index into the history list to be | |
234 | set or changed. | |
235 | ||
236 | @deftypefun int history_set_pos (int pos) | |
237 | Set the position in the history list to @var{pos}, an absolute index | |
238 | into the list. | |
239 | @end deftypefun | |
240 | ||
241 | @deftypefun {HIST_ENTRY *} previous_history () | |
242 | Back up the current history offset to the previous history entry, and | |
243 | return a pointer to that entry. If there is no previous entry, return | |
244 | a @code{NULL} pointer. | |
245 | @end deftypefun | |
246 | ||
247 | @deftypefun {HIST_ENTRY *} next_history () | |
248 | Move the current history offset forward to the next history entry, and | |
249 | return the a pointer to that entry. If there is no next entry, return | |
250 | a @code{NULL} pointer. | |
251 | @end deftypefun | |
252 | ||
253 | @node Searching the History List | |
254 | @subsection Searching the History List | |
255 | @cindex History Searching | |
256 | ||
257 | These functions allow searching of the history list for entries containing | |
258 | a specific string. Searching may be performed both forward and backward | |
259 | from the current history position. The search may be @dfn{anchored}, | |
260 | meaning 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) | |
264 | Search the history for @var{string}, starting at the current history | |
265 | offset. If @var{direction} < 0, then the search is through previous entries, | |
266 | else through subsequent. If @var{string} is found, then | |
267 | the current history index is set to that history entry, and the value | |
268 | returned is the offset in the line of the entry where | |
269 | @var{string} was found. Otherwise, nothing is changed, and a -1 is | |
270 | returned. | |
271 | @end deftypefun | |
272 | ||
273 | @deftypefun int history_search_prefix (char *string, int direction) | |
274 | Search the history for @var{string}, starting at the current history | |
275 | offset. The search is anchored: matching lines must begin with | |
276 | @var{string}. If @var{direction} < 0, then the search is through previous | |
277 | entries, else through subsequent. If @var{string} is found, then the | |
278 | current history index is set to that entry, and the return value is 0. | |
279 | Otherwise, nothing is changed, and a -1 is returned. | |
280 | @end deftypefun | |
281 | ||
282 | @deftypefun int history_search_pos (char *string, int direction, int pos) | |
283 | Search for @var{string} in the history list, starting at @var{pos}, an | |
284 | absolute index into the list. If @var{direction} is negative, the search | |
285 | proceeds backward from @var{pos}, otherwise forward. Returns the absolute | |
286 | index 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 | ||
292 | The History library can read the history from and write it to a file. | |
293 | This section documents the functions for managing a history file. | |
294 | ||
295 | @deftypefun int read_history (char *filename) | |
296 | Add the contents of @var{filename} to the history list, a line at a | |
297 | time. 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) | |
302 | Read a range of lines from @var{filename}, adding them to the history list. | |
303 | Start 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, | |
307 | or @code{errno} if not. | |
308 | @end deftypefun | |
309 | ||
310 | @deftypefun int write_history (char *filename) | |
311 | Write the current history to @var{filename}, overwriting @var{filename} | |
312 | if necessary. If @var{filename} is | |
313 | @code{NULL}, then write the history list to @file{~/.history}. Values | |
314 | returned are as in @code{read_history ()}. | |
315 | @end deftypefun | |
316 | ||
317 | @deftypefun int append_history (int nelements, char *filename) | |
318 | Append 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) | |
322 | Truncate 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 | ||
329 | These functions implement @code{csh}-like history expansion. | |
330 | ||
331 | @deftypefun int history_expand (char *string, char **output) | |
332 | Expand @var{string}, placing the result into @var{output}, a pointer | |
333 | to a string (@pxref{History Interaction}). Returns: | |
334 | @table @code | |
335 | @item 0 | |
336 | If no expansions took place (or, if the only change in | |
337 | the text was the de-slashifying of the history expansion | |
338 | character); | |
339 | @item 1 | |
340 | if expansions did take place; | |
341 | @item -1 | |
342 | if there was an error in expansion; | |
343 | @item 2 | |
1b17e766 | 344 | if the returned line should be displayed, but not executed, |
d60d9f65 SS |
345 | as with the @code{:p} modifier (@pxref{Modifiers}). |
346 | @end table | |
347 | ||
348 | If an error ocurred in expansion, then @var{output} contains a descriptive | |
349 | error message. | |
350 | @end deftypefun | |
351 | ||
352 | @deftypefun {char *} history_arg_extract (int first, int last, char *string) | |
353 | Extract a string segment consisting of the @var{first} through @var{last} | |
354 | arguments 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) | |
358 | Returns the text of the history event beginning at @var{string} + | |
359 | @var{*cindex}. @var{*cindex} is modified to point to after the event | |
360 | specifier. At function entry, @var{cindex} points to the index into | |
361 | @var{string} where the history event specification begins. @var{qchar} | |
362 | is a character that is allowed to end the event specification in addition | |
363 | to the ``normal'' terminating characters. | |
364 | @end deftypefun | |
365 | ||
366 | @deftypefun {char **} history_tokenize (char *string) | |
367 | Return an array of tokens parsed out of @var{string}, much as the | |
368 | shell might. The tokens are split on white space and on the | |
369 | characters @code{()<>;&|$}, and shell quoting conventions are | |
370 | obeyed. | |
371 | @end deftypefun | |
372 | ||
373 | @node History Variables | |
374 | @section History Variables | |
375 | ||
376 | This section describes the externally visible variables exported by | |
377 | the GNU History Library. | |
378 | ||
379 | @deftypevar int history_base | |
380 | The logical offset of the first entry in the history list. | |
381 | @end deftypevar | |
382 | ||
383 | @deftypevar int history_length | |
384 | The number of entries currently stored in the history list. | |
385 | @end deftypevar | |
386 | ||
387 | @deftypevar int max_input_history | |
388 | The maximum number of history entries. This must be changed using | |
389 | @code{stifle_history ()}. | |
390 | @end deftypevar | |
391 | ||
392 | @deftypevar char history_expansion_char | |
393 | The character that starts a history event. The default is @samp{!}. | |
394 | @end deftypevar | |
395 | ||
396 | @deftypevar char history_subst_char | |
397 | The character that invokes word substitution if found at the start of | |
398 | a line. The default is @samp{^}. | |
399 | @end deftypevar | |
400 | ||
401 | @deftypevar char history_comment_char | |
402 | During tokenization, if this character is seen as the first character | |
403 | of a word, then it and all subsequent characters up to a newline are | |
404 | ignored, suppressing history expansion for the remainder of the line. | |
405 | This is disabled by default. | |
406 | @end deftypevar | |
407 | ||
408 | @deftypevar {char *} history_no_expand_chars | |
409 | The list of characters which inhibit history expansion if found immediately | |
410 | following @var{history_expansion_char}. The default is whitespace and | |
411 | @samp{=}. | |
412 | @end deftypevar | |
413 | ||
414 | @deftypevar {char *} history_search_delimiter_chars | |
415 | The list of additional characters which can delimit a history search | |
416 | string, in addition to whitespace, @samp{:} and @samp{?} in the case of | |
417 | a substring search. The default is empty. | |
418 | @end deftypevar | |
419 | ||
420 | @deftypevar int history_quotes_inhibit_expansion | |
421 | If non-zero, single-quoted words are not scanned for the history expansion | |
422 | character. The default value is 0. | |
423 | @end deftypevar | |
424 | ||
425 | @deftypevar {Function *} history_inhibit_expansion_function | |
426 | This should be set to the address of a function that takes two arguments: | |
427 | a @code{char *} (@var{string}) and an integer index into that string (@var{i}). | |
428 | It 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 | |
430 | be done. | |
431 | It is intended for use by applications like Bash that use the history | |
432 | expansion character for additional purposes. | |
433 | By default, this variable is set to NULL. | |
434 | @end deftypevar | |
435 | ||
436 | @node History Programming Example | |
437 | @section History Programming Example | |
438 | ||
439 | The following program demonstrates simple use of the GNU History Library. | |
440 | ||
441 | @smallexample | |
442 | main () | |
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 |