history.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* history.h -- the names of functions that you can call in history. */
  2. /* Copyright (C) 1989-2009 Free Software Foundation, Inc.
  3. This file contains the GNU History Library (History), a set of
  4. routines for managing the text of previously typed lines.
  5. History is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. History is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with History. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef _HISTORY_H_
  17. #define _HISTORY_H_
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. #include <time.h> /* XXX - for history timestamp code */
  22. #if defined READLINE_LIBRARY
  23. # include "rlstdc.h"
  24. # include "rltypedefs.h"
  25. #else
  26. # include <readline/rlstdc.h>
  27. # include <readline/rltypedefs.h>
  28. #endif
  29. #ifdef __STDC__
  30. typedef void *histdata_t;
  31. #else
  32. typedef char *histdata_t;
  33. #endif
  34. /* The structure used to store a history entry. */
  35. typedef struct _hist_entry {
  36. char *line;
  37. char *timestamp; /* char * rather than time_t for read/write */
  38. histdata_t data;
  39. } HIST_ENTRY;
  40. /* Size of the history-library-managed space in history entry HS. */
  41. #define HISTENT_BYTES(hs) (strlen ((hs)->line) + strlen ((hs)->timestamp))
  42. /* A structure used to pass the current state of the history stuff around. */
  43. typedef struct _hist_state {
  44. HIST_ENTRY **entries; /* Pointer to the entries themselves. */
  45. int offset; /* The location pointer within this array. */
  46. int length; /* Number of elements within this array. */
  47. int size; /* Number of slots allocated to this array. */
  48. int flags;
  49. } HISTORY_STATE;
  50. /* Flag values for the `flags' member of HISTORY_STATE. */
  51. #define HS_STIFLED 0x01
  52. /* Initialization and state management. */
  53. /* Begin a session in which the history functions might be used. This
  54. just initializes the interactive variables. */
  55. extern void using_history PARAMS((void));
  56. /* Return the current HISTORY_STATE of the history. */
  57. extern HISTORY_STATE *history_get_history_state PARAMS((void));
  58. /* Set the state of the current history array to STATE. */
  59. extern void history_set_history_state PARAMS((HISTORY_STATE *));
  60. /* Manage the history list. */
  61. /* Place STRING at the end of the history list.
  62. The associated data field (if any) is set to NULL. */
  63. extern void add_history PARAMS((const char *));
  64. /* Change the timestamp associated with the most recent history entry to
  65. STRING. */
  66. extern void add_history_time PARAMS((const char *));
  67. /* A reasonably useless function, only here for completeness. WHICH
  68. is the magic number that tells us which element to delete. The
  69. elements are numbered from 0. */
  70. extern HIST_ENTRY *remove_history PARAMS((int));
  71. /* Free the history entry H and return any application-specific data
  72. associated with it. */
  73. extern histdata_t free_history_entry PARAMS((HIST_ENTRY *));
  74. /* Make the history entry at WHICH have LINE and DATA. This returns
  75. the old entry so you can dispose of the data. In the case of an
  76. invalid WHICH, a NULL pointer is returned. */
  77. extern HIST_ENTRY *replace_history_entry PARAMS((int, const char *, histdata_t));
  78. /* Clear the history list and start over. */
  79. extern void clear_history PARAMS((void));
  80. /* Stifle the history list, remembering only MAX number of entries. */
  81. extern void stifle_history PARAMS((int));
  82. /* Stop stifling the history. This returns the previous amount the
  83. history was stifled by. The value is positive if the history was
  84. stifled, negative if it wasn't. */
  85. extern int unstifle_history PARAMS((void));
  86. /* Return 1 if the history is stifled, 0 if it is not. */
  87. extern int history_is_stifled PARAMS((void));
  88. /* Information about the history list. */
  89. /* Return a NULL terminated array of HIST_ENTRY which is the current input
  90. history. Element 0 of this list is the beginning of time. If there
  91. is no history, return NULL. */
  92. extern HIST_ENTRY **history_list PARAMS((void));
  93. /* Returns the number which says what history element we are now
  94. looking at. */
  95. extern int where_history PARAMS((void));
  96. /* Return the history entry at the current position, as determined by
  97. history_offset. If there is no entry there, return a NULL pointer. */
  98. extern HIST_ENTRY *current_history PARAMS((void));
  99. /* Return the history entry which is logically at OFFSET in the history
  100. array. OFFSET is relative to history_base. */
  101. extern HIST_ENTRY *history_get PARAMS((int));
  102. /* Return the timestamp associated with the HIST_ENTRY * passed as an
  103. argument */
  104. extern time_t history_get_time PARAMS((HIST_ENTRY *));
  105. /* Return the number of bytes that the primary history entries are using.
  106. This just adds up the lengths of the_history->lines. */
  107. extern int history_total_bytes PARAMS((void));
  108. /* Moving around the history list. */
  109. /* Set the position in the history list to POS. */
  110. extern int history_set_pos PARAMS((int));
  111. /* Back up history_offset to the previous history entry, and return
  112. a pointer to that entry. If there is no previous entry, return
  113. a NULL pointer. */
  114. extern HIST_ENTRY *previous_history PARAMS((void));
  115. /* Move history_offset forward to the next item in the input_history,
  116. and return the a pointer to that entry. If there is no next entry,
  117. return a NULL pointer. */
  118. extern HIST_ENTRY *next_history PARAMS((void));
  119. /* Searching the history list. */
  120. /* Search the history for STRING, starting at history_offset.
  121. If DIRECTION < 0, then the search is through previous entries,
  122. else through subsequent. If the string is found, then
  123. current_history () is the history entry, and the value of this function
  124. is the offset in the line of that history entry that the string was
  125. found in. Otherwise, nothing is changed, and a -1 is returned. */
  126. extern int history_search PARAMS((const char *, int));
  127. /* Search the history for STRING, starting at history_offset.
  128. The search is anchored: matching lines must begin with string.
  129. DIRECTION is as in history_search(). */
  130. extern int history_search_prefix PARAMS((const char *, int));
  131. /* Search for STRING in the history list, starting at POS, an
  132. absolute index into the list. DIR, if negative, says to search
  133. backwards from POS, else forwards.
  134. Returns the absolute index of the history element where STRING
  135. was found, or -1 otherwise. */
  136. extern int history_search_pos PARAMS((const char *, int, int));
  137. /* Managing the history file. */
  138. /* Add the contents of FILENAME to the history list, a line at a time.
  139. If FILENAME is NULL, then read from ~/.history. Returns 0 if
  140. successful, or errno if not. */
  141. extern int read_history PARAMS((const char *));
  142. /* Read a range of lines from FILENAME, adding them to the history list.
  143. Start reading at the FROM'th line and end at the TO'th. If FROM
  144. is zero, start at the beginning. If TO is less than FROM, read
  145. until the end of the file. If FILENAME is NULL, then read from
  146. ~/.history. Returns 0 if successful, or errno if not. */
  147. extern int read_history_range PARAMS((const char *, int, int));
  148. /* Write the current history to FILENAME. If FILENAME is NULL,
  149. then write the history list to ~/.history. Values returned
  150. are as in read_history (). */
  151. extern int write_history PARAMS((const char *));
  152. /* Append NELEMENT entries to FILENAME. The entries appended are from
  153. the end of the list minus NELEMENTs up to the end of the list. */
  154. extern int append_history PARAMS((int, const char *));
  155. /* Truncate the history file, leaving only the last NLINES lines. */
  156. extern int history_truncate_file PARAMS((const char *, int));
  157. /* History expansion. */
  158. /* Expand the string STRING, placing the result into OUTPUT, a pointer
  159. to a string. Returns:
  160. 0) If no expansions took place (or, if the only change in
  161. the text was the de-slashifying of the history expansion
  162. character)
  163. 1) If expansions did take place
  164. -1) If there was an error in expansion.
  165. 2) If the returned line should just be printed.
  166. If an error ocurred in expansion, then OUTPUT contains a descriptive
  167. error message. */
  168. extern int history_expand PARAMS((char *, char **));
  169. /* Extract a string segment consisting of the FIRST through LAST
  170. arguments present in STRING. Arguments are broken up as in
  171. the shell. */
  172. extern char *history_arg_extract PARAMS((int, int, const char *));
  173. /* Return the text of the history event beginning at the current
  174. offset into STRING. Pass STRING with *INDEX equal to the
  175. history_expansion_char that begins this specification.
  176. DELIMITING_QUOTE is a character that is allowed to end the string
  177. specification for what to search for in addition to the normal
  178. characters `:', ` ', `\t', `\n', and sometimes `?'. */
  179. extern char *get_history_event PARAMS((const char *, int *, int));
  180. /* Return an array of tokens, much as the shell might. The tokens are
  181. parsed out of STRING. */
  182. extern char **history_tokenize PARAMS((const char *));
  183. /* Exported history variables. */
  184. extern int history_base;
  185. extern int history_length;
  186. extern int history_max_entries;
  187. extern char history_expansion_char;
  188. extern char history_subst_char;
  189. extern char *history_word_delimiters;
  190. extern char history_comment_char;
  191. extern char *history_no_expand_chars;
  192. extern char *history_search_delimiter_chars;
  193. extern int history_quotes_inhibit_expansion;
  194. extern int history_write_timestamps;
  195. /* Backwards compatibility */
  196. extern int max_input_history;
  197. /* If set, this function is called to decide whether or not a particular
  198. history expansion should be treated as a special case for the calling
  199. application and not expanded. */
  200. extern rl_linebuf_func_t *history_inhibit_expansion_function;
  201. #ifdef __cplusplus
  202. }
  203. #endif
  204. #endif /* !_HISTORY_H_ */