[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This chapter describes how to interface the GNU History Library with programs that you write. It should be considered a technical guide. For information on the interactive use of GNU History, @pxref{Using History Interactively}.
1.1 Introduction to History | What is the GNU History library for? | |
1.2 History Storage | How information is stored. | |
1.3 History Functions | Functions that you can use. | |
1.4 History Variables | Variables that control behaviour. | |
1.5 History Programming Example | Example of using the GNU History Library. |
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Many programs read input from the user a line at a time. The GNU history library is able to keep track of those lines, associate arbitrary data with each line, and utilize information from previous lines in making up new ones.
The programmer using the History library has available to him functions for remembering lines on a history stack, associating arbitrary data with a line, removing lines from the stack, searching through the stack for a line containing an arbitrary text string, and referencing any line on the stack directly. In addition, a history expansion function is available which provides for a consistent user interface across many different programs.
The end-user using programs written with the History library has the
benifit of a consistent user interface, with a set of well-known
commands for manipulating the text of previous lines and using that text
in new commands. The basic history manipulation commands are similar to
the history substitution used by Csh
.
If the programmer desires, he can use the Readline library, which includes some history manipulation by default, and has the added advantage of Emacs style command line editing.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
typedef struct _hist_entry { char *line; char *data; } HIST_ENTRY;
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This section describes the calling sequence for the various functions present in GNU History.
Begin a session in which the history functions might be used. This just initializes the interactive variables.
Place string at the end of the history list. The associated data
field (if any) is set to NULL
.
Returns the number which says what history element we are now looking at.
Set the position in the history list to pos.
Search for string in the history list, starting at pos, an absolute index into the list. direction, if negative, says to search backwards from pos, else forwards. Returns the absolute index of the history element where string was found, or -1 otherwise.
Remove history element which from the history. The removed element is returned to you so you can free the line, data, and containing structure.
Stifle the history list, remembering only max number of entries.
Stop stifling the history. This returns the previous amount the history was stifled by. The value is positive if the history was stifled, negative if it wasn’t.
Add the contents of filename to the history list, a line at a
time. If filename is NULL
, then read from
‘~/.history’. Returns 0 if successful, or errno if not.
Read a range of lines from filename, adding them to the history list.
Start reading at the from’th line and end at the to’th. If
from is zero, start at the beginning. If to is less than
from, then read until the end of the file. If filename is
NULL
, then read from ‘~/.history’. Returns 0 if successful,
or errno
if not.
Append the current history to filename. If filename is
NULL
, then append the history list to ‘~/.history’. Values
returned are as in read_history ()
.
Append nelement entries to filename. The entries appended are from the end of the list minus nelements up to the end of the list.
Make the history entry at which have line and data.
This returns the old entry so you can dispose of the data. In the case
of an invalid which, a NULL
pointer is returned.
Return the history entry at the current position, as determined by
history_offset
. If there is no entry there, return a NULL
pointer.
Back up history_offset to the previous history entry, and return a
pointer to that entry. If there is no previous entry, return a
NULL
pointer.
Move history_offset
forward to the next history entry, and return
the a pointer to that entry. If there is no next entry, return a
NULL
pointer.
Return a NULL
terminated array of HIST_ENTRY
which is the
current input history. Element 0 of this list is the beginning of time.
If there is no history, return NULL
.
Search the history for string, starting at history_offset
.
If direction < 0, then the search is through previous entries,
else through subsequent. If string is found, then
current_history ()
is the history entry, and the value of this
function is the offset in the line of that history entry that the
string was found in. Otherwise, nothing is changed, and a -1 is
returned.
Expand string, placing the result into output, a pointer to a string. Returns:
0
If no expansions took place (or, if the only change in the text was the de-slashifying of the history expansion character),
1
if expansions did take place, or
-1
if there was an error in expansion.
If an error ocurred in expansion, then output contains a descriptive error message.
Extract a string segment consisting of the first through last arguments present in string. Arguments are broken up as in the GNU Bash shell.
Return the number of bytes that the primary history entries are using.
This just adds up the lengths of the_history->lines
.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This section describes the variables in GNU History that are externally visible.
For convenience only. You set this when interpreting history commands. It is the logical offset of the first history element.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The following snippet of code demonstrates simple use of the GNU History Library.
main () { char line[1024], *t; int done = 0; line[0] = 0; while (!done) { fprintf (stdout, "history%% "); t = gets (line); if (!t) strcpy (line, "quit"); if (line[0]) { char *expansion; int result; using_history (); result = history_expand (line, &expansion); strcpy (line, expansion); free (expansion); if (result) fprintf (stderr, "%s\n", line); if (result < 0) continue; add_history (line); } if (strcmp (line, "quit") == 0) done = 1; if (strcmp (line, "save") == 0) write_history (0); if (strcmp (line, "read") == 0) read_history (0); if (strcmp (line, "list") == 0) { register HIST_ENTRY **the_list = history_list (); register int i; if (the_list) for (i = 0; the_list[i]; i++) fprintf (stdout, "%d: %s\n", i + history_base, the_list[i]->line); } if (strncmp (line, "delete", strlen ("delete")) == 0) { int which; if ((sscanf (line + strlen ("delete"), "%d", &which)) == 1) { HIST_ENTRY *entry = remove_history (which); if (!entry) fprintf (stderr, "No such entry %d\n", which); else { free (entry->line); free (entry); } } else { fprintf (stderr, "non-numeric arg given to `delete'\n"); } } } }
[Top] | [Contents] | [Index] | [ ? ] |
This document was generated on January 15, 2023 using texi2html 5.0.
The buttons in the navigation panels have the following meaning:
Button | Name | Go to | From 1.2.3 go to |
---|---|---|---|
[ << ] | FastBack | Beginning of this chapter or previous chapter | 1 |
[ < ] | Back | Previous section in reading order | 1.2.2 |
[ Up ] | Up | Up section | 1.2 |
[ > ] | Forward | Next section in reading order | 1.2.4 |
[ >> ] | FastForward | Next chapter | 2 |
[Top] | Top | Cover (top) of document | |
[Contents] | Contents | Table of contents | |
[Index] | Index | Index | |
[ ? ] | About | About (help) |
where the Example assumes that the current position is at Subsubsection One-Two-Three of a document of the following structure:
This document was generated on January 15, 2023 using texi2html 5.0.