history.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /* history.h -- the names of functions that you can call in history. */
  2. /* Copyright (C) 1989-2022 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. /* Let's not step on anyone else's define for now, since we don't use this yet. */
  35. #ifndef HS_HISTORY_VERSION
  36. # define HS_HISTORY_VERSION 0x0802 /* History 8.2 */
  37. #endif
  38. /* The structure used to store a history entry. */
  39. typedef struct _hist_entry {
  40. char *line;
  41. char *timestamp; /* char * rather than time_t for read/write */
  42. histdata_t data;
  43. } HIST_ENTRY;
  44. /* Size of the history-library-managed space in history entry HS. */
  45. #define HISTENT_BYTES(hs) (strlen ((hs)->line) + strlen ((hs)->timestamp))
  46. /* A structure used to pass the current state of the history stuff around. */
  47. typedef struct _hist_state {
  48. HIST_ENTRY **entries; /* Pointer to the entries themselves. */
  49. int offset; /* The location pointer within this array. */
  50. int length; /* Number of elements within this array. */
  51. int size; /* Number of slots allocated to this array. */
  52. int flags;
  53. } HISTORY_STATE;
  54. /* Flag values for the `flags' member of HISTORY_STATE. */
  55. #define HS_STIFLED 0x01
  56. /* Initialization and state management. */
  57. /* Begin a session in which the history functions might be used. This
  58. just initializes the interactive variables. */
  59. extern void using_history (void);
  60. /* Return the current HISTORY_STATE of the history. */
  61. extern HISTORY_STATE *history_get_history_state (void);
  62. /* Set the state of the current history array to STATE. */
  63. extern void history_set_history_state (HISTORY_STATE *);
  64. /* Manage the history list. */
  65. /* Place STRING at the end of the history list.
  66. The associated data field (if any) is set to NULL. */
  67. extern void add_history (const char *);
  68. /* Change the timestamp associated with the most recent history entry to
  69. STRING. */
  70. extern void add_history_time (const char *);
  71. /* Remove an entry from the history list. WHICH is the magic number that
  72. tells us which element to delete. The elements are numbered from 0. */
  73. extern HIST_ENTRY *remove_history (int);
  74. /* Remove a set of entries from the history list: FIRST to LAST, inclusive */
  75. extern HIST_ENTRY **remove_history_range (int, int);
  76. /* Allocate a history entry consisting of STRING and TIMESTAMP and return
  77. a pointer to it. */
  78. extern HIST_ENTRY *alloc_history_entry (char *, char *);
  79. /* Copy the history entry H, but not the (opaque) data pointer */
  80. extern HIST_ENTRY *copy_history_entry (HIST_ENTRY *);
  81. /* Free the history entry H and return any application-specific data
  82. associated with it. */
  83. extern histdata_t free_history_entry (HIST_ENTRY *);
  84. /* Make the history entry at WHICH have LINE and DATA. This returns
  85. the old entry so you can dispose of the data. In the case of an
  86. invalid WHICH, a NULL pointer is returned. */
  87. extern HIST_ENTRY *replace_history_entry (int, const char *, histdata_t);
  88. /* Clear the history list and start over. */
  89. extern void clear_history (void);
  90. /* Stifle the history list, remembering only MAX number of entries. */
  91. extern void stifle_history (int);
  92. /* Stop stifling the history. This returns the previous amount the
  93. history was stifled by. The value is positive if the history was
  94. stifled, negative if it wasn't. */
  95. extern int unstifle_history (void);
  96. /* Return 1 if the history is stifled, 0 if it is not. */
  97. extern int history_is_stifled (void);
  98. /* Information about the history list. */
  99. /* Return a NULL terminated array of HIST_ENTRY which is the current input
  100. history. Element 0 of this list is the beginning of time. If there
  101. is no history, return NULL. */
  102. extern HIST_ENTRY **history_list (void);
  103. /* Returns the number which says what history element we are now
  104. looking at. */
  105. extern int where_history (void);
  106. /* Return the history entry at the current position, as determined by
  107. history_offset. If there is no entry there, return a NULL pointer. */
  108. extern HIST_ENTRY *current_history (void);
  109. /* Return the history entry which is logically at OFFSET in the history
  110. array. OFFSET is relative to history_base. */
  111. extern HIST_ENTRY *history_get (int);
  112. /* Return the timestamp associated with the HIST_ENTRY * passed as an
  113. argument */
  114. extern time_t history_get_time (HIST_ENTRY *);
  115. /* Return the number of bytes that the primary history entries are using.
  116. This just adds up the lengths of the_history->lines. */
  117. extern int history_total_bytes (void);
  118. /* Moving around the history list. */
  119. /* Set the position in the history list to POS. */
  120. extern int history_set_pos (int);
  121. /* Back up history_offset to the previous history entry, and return
  122. a pointer to that entry. If there is no previous entry, return
  123. a NULL pointer. */
  124. extern HIST_ENTRY *previous_history (void);
  125. /* Move history_offset forward to the next item in the input_history,
  126. and return the a pointer to that entry. If there is no next entry,
  127. return a NULL pointer. */
  128. extern HIST_ENTRY *next_history (void);
  129. /* Searching the history list. */
  130. /* Search the history for STRING, starting at history_offset.
  131. If DIRECTION < 0, then the search is through previous entries,
  132. else through subsequent. If the string is found, then
  133. current_history () is the history entry, and the value of this function
  134. is the offset in the line of that history entry that the string was
  135. found in. Otherwise, nothing is changed, and a -1 is returned. */
  136. extern int history_search (const char *, int);
  137. /* Search the history for STRING, starting at history_offset.
  138. The search is anchored: matching lines must begin with string.
  139. DIRECTION is as in history_search(). */
  140. extern int history_search_prefix (const char *, int);
  141. /* Search for STRING in the history list, starting at POS, an
  142. absolute index into the list. DIR, if negative, says to search
  143. backwards from POS, else forwards.
  144. Returns the absolute index of the history element where STRING
  145. was found, or -1 otherwise. */
  146. extern int history_search_pos (const char *, int, int);
  147. /* Managing the history file. */
  148. /* Add the contents of FILENAME to the history list, a line at a time.
  149. If FILENAME is NULL, then read from ~/.history. Returns 0 if
  150. successful, or errno if not. */
  151. extern int read_history (const char *);
  152. /* Read a range of lines from FILENAME, adding them to the history list.
  153. Start reading at the FROM'th line and end at the TO'th. If FROM
  154. is zero, start at the beginning. If TO is less than FROM, read
  155. until the end of the file. If FILENAME is NULL, then read from
  156. ~/.history. Returns 0 if successful, or errno if not. */
  157. extern int read_history_range (const char *, int, int);
  158. /* Write the current history to FILENAME. If FILENAME is NULL,
  159. then write the history list to ~/.history. Values returned
  160. are as in read_history (). */
  161. extern int write_history (const char *);
  162. /* Append NELEMENT entries to FILENAME. The entries appended are from
  163. the end of the list minus NELEMENTs up to the end of the list. */
  164. extern int append_history (int, const char *);
  165. /* Truncate the history file, leaving only the last NLINES lines. */
  166. extern int history_truncate_file (const char *, int);
  167. /* History expansion. */
  168. /* Expand the string STRING, placing the result into OUTPUT, a pointer
  169. to a string. Returns:
  170. 0) If no expansions took place (or, if the only change in
  171. the text was the de-slashifying of the history expansion
  172. character)
  173. 1) If expansions did take place
  174. -1) If there was an error in expansion.
  175. 2) If the returned line should just be printed.
  176. If an error occurred in expansion, then OUTPUT contains a descriptive
  177. error message. */
  178. extern int history_expand (char *, char **);
  179. /* Extract a string segment consisting of the FIRST through LAST
  180. arguments present in STRING. Arguments are broken up as in
  181. the shell. */
  182. extern char *history_arg_extract (int, int, const char *);
  183. /* Return the text of the history event beginning at the current
  184. offset into STRING. Pass STRING with *INDEX equal to the
  185. history_expansion_char that begins this specification.
  186. DELIMITING_QUOTE is a character that is allowed to end the string
  187. specification for what to search for in addition to the normal
  188. characters `:', ` ', `\t', `\n', and sometimes `?'. */
  189. extern char *get_history_event (const char *, int *, int);
  190. /* Return an array of tokens, much as the shell might. The tokens are
  191. parsed out of STRING. */
  192. extern char **history_tokenize (const char *);
  193. /* Exported history variables. */
  194. extern int history_base;
  195. extern int history_length;
  196. extern int history_max_entries;
  197. extern int history_offset;
  198. extern int history_lines_read_from_file;
  199. extern int history_lines_written_to_file;
  200. extern char history_expansion_char;
  201. extern char history_subst_char;
  202. extern char *history_word_delimiters;
  203. extern char history_comment_char;
  204. extern char *history_no_expand_chars;
  205. extern char *history_search_delimiter_chars;
  206. extern int history_quotes_inhibit_expansion;
  207. extern int history_quoting_state;
  208. extern int history_write_timestamps;
  209. /* These two are undocumented; the second is reserved for future use */
  210. extern int history_multiline_entries;
  211. extern int history_file_version;
  212. /* Backwards compatibility */
  213. extern int max_input_history;
  214. /* If set, this function is called to decide whether or not a particular
  215. history expansion should be treated as a special case for the calling
  216. application and not expanded. */
  217. extern rl_linebuf_func_t *history_inhibit_expansion_function;
  218. #ifdef __cplusplus
  219. }
  220. #endif
  221. #endif /* !_HISTORY_H_ */