hstech.texinfo

来自「linux下bash的源码」· TEXINFO 代码 · 共 490 行 · 第 1/2 页

TEXINFO
490
字号
These functions allow searching of the history list for entries containinga specific string.  Searching may be performed both forward and backwardfrom the current history position.  The search may be @dfn{anchored},meaning that the string must match at the beginning of the history entry.@cindex anchored search@deftypefun int history_search (char *string, int direction)Search the history for @var{string}, starting at the current historyoffset.  If @var{direction} < 0, then the search is through previous entries,else through subsequent.  If @var{string} is found, thenthe current history index is set to that history entry, and the valuereturned is the offset in the line of the entry where@var{string} was found.  Otherwise, nothing is changed, and a -1 isreturned.@end deftypefun@deftypefun int history_search_prefix (char *string, int direction)Search the history for @var{string}, starting at the current historyoffset.  The search is anchored: matching lines must begin with@var{string}.  If @var{direction} < 0, then the search is through previousentries, else through subsequent.  If @var{string} is found, then thecurrent history index is set to that entry, and the return value is 0. Otherwise, nothing is changed, and a -1 is returned. @end deftypefun@deftypefun int history_search_pos (char *string, int direction, int pos)Search for @var{string} in the history list, starting at @var{pos}, anabsolute index into the list.  If @var{direction} is negative, the searchproceeds backward from @var{pos}, otherwise forward.  Returns the absoluteindex of the history element where @var{string} was found, or -1 otherwise.@end deftypefun@node Managing the History File@subsection Managing the History FileThe History library can read the history from and write it to a file.This section documents the functions for managing a history file.@deftypefun int read_history (char *filename)Add the contents of @var{filename} to the history list, a line at atime.  If @var{filename} is @code{NULL}, then read from@file{~/.history}.  Returns 0 if successful, or errno if not.@end deftypefun@deftypefun int read_history_range (char *filename, int from, int to)Read a range of lines from @var{filename}, adding them to the history list.Start reading at line @var{from} and end at @var{to}.  If@var{from} is zero, start at the beginning.  If @var{to} is less than@var{from}, then read until the end of the file.  If @var{filename} is@code{NULL}, then read from @file{~/.history}.  Returns 0 if successful,or @code{errno} if not.@end deftypefun@deftypefun int write_history (char *filename)Write the current history to @var{filename}, overwriting @var{filename}if necessary.  If @var{filename} is@code{NULL}, then write the history list to @file{~/.history}.  Valuesreturned are as in @code{read_history ()}.@end deftypefun@deftypefun int append_history (int nelements, char *filename)Append the last @var{nelements} of the history list to @var{filename}.@end deftypefun@deftypefun int history_truncate_file (char *filename, int nlines)Truncate the history file @var{filename}, leaving only the last@var{nlines} lines.@end deftypefun@node History Expansion@subsection History ExpansionThese functions implement @code{csh}-like history expansion.@deftypefun int history_expand (char *string, char **output)Expand @var{string}, placing the result into @var{output}, a pointerto a string (@pxref{History Interaction}).  Returns:@table @code@item 0If no expansions took place (or, if the only change inthe text was the de-slashifying of the history expansioncharacter);@item 1if expansions did take place;@item -1if there was an error in expansion;@item 2if the returned line should only be displayed, but not executed,as with the @code{:p} modifier (@pxref{Modifiers}).@end tableIf an error ocurred in expansion, then @var{output} contains a descriptiveerror message.@end deftypefun@deftypefun {char *} history_arg_extract (int first, int last, char *string)Extract a string segment consisting of the @var{first} through @var{last}arguments present in @var{string}.  Arguments are broken up as in Bash.@end deftypefun@deftypefun {char *} get_history_event (char *string, int *cindex, int qchar)Returns the text of the history event beginning at @var{string} +@var{*cindex}.  @var{*cindex} is modified to point to after the eventspecifier.  At function entry, @var{cindex} points to the index into@var{string} where the history event specification begins.  @var{qchar}is a character that is allowed to end the event specification in additionto the ``normal'' terminating characters.@end deftypefun@deftypefun {char **} history_tokenize (char *string)Return an array of tokens parsed out of @var{string}, much as theshell might.  The tokens are split on white space and on thecharacters @code{()<>;&|$}, and shell quoting conventions areobeyed.@end deftypefun@node History Variables@section History VariablesThis section describes the externally visible variables exported bythe GNU History Library.@deftypevar int history_baseThe logical offset of the first entry in the history list.@end deftypevar@deftypevar int history_lengthThe number of entries currently stored in the history list.@end deftypevar@deftypevar int max_input_historyThe maximum number of history entries.  This must be changed using@code{stifle_history ()}.@end deftypevar@deftypevar char history_expansion_charThe character that starts a history event.  The default is @samp{!}.@end deftypevar@deftypevar char history_subst_charThe character that invokes word substitution if found at the start ofa line.  The default is @samp{^}.@end deftypevar@deftypevar char history_comment_charDuring tokenization, if this character is seen as the first characterof a word, then it and all subsequent characters up to a newline areignored, suppressing history expansion for the remainder of the line.This is disabled by default.@end deftypevar@deftypevar {char *} history_no_expand_charsThe list of characters which inhibit history expansion if found immediatelyfollowing @var{history_expansion_char}.  The default is whitespace and@samp{=}.@end deftypevar@node History Programming Example@section History Programming ExampleThe following program demonstrates simple use of the GNU History Library.@smallexamplemain ()@{  char line[1024], *t;  int len, done = 0;  line[0] = 0;  using_history ();  while (!done)    @{      printf ("history$ ");      fflush (stdout);      t = fgets (line, sizeof (line) - 1, stdin);      if (t && *t)        @{          len = strlen (t);          if (t[len - 1] == '\n')            t[len - 1] = '\0';        @}      if (!t)        strcpy (line, "quit");      if (line[0])        @{          char *expansion;          int result;          result = history_expand (line, &expansion);          if (result)            fprintf (stderr, "%s\n", expansion);          if (result < 0 || result == 2)            @{              free (expansion);              continue;            @}          add_history (expansion);          strncpy (line, expansion, sizeof (line) - 1);          free (expansion);        @}      if (strcmp (line, "quit") == 0)        done = 1;      else if (strcmp (line, "save") == 0)        write_history ("history_file");      else if (strcmp (line, "read") == 0)        read_history ("history_file");      else if (strcmp (line, "list") == 0)        @{          register HIST_ENTRY **the_list;          register int i;          the_list = history_list ();          if (the_list)            for (i = 0; the_list[i]; i++)              printf ("%d: %s\n", i + history_base, the_list[i]->line);        @}      else if (strncmp (line, "delete", 6) == 0)        @{          int which;          if ((sscanf (line + 6, "%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");            @}        @}    @}@}@end smallexample

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?