home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / AP / TERMNET / READLINE.0 / DOC / HSTECH.TEX < prev    next >
Encoding:
Text File  |  1995-04-20  |  16.9 KB  |  490 lines

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