history.h 9.97 KB
Newer Older
1 2
/* history.h -- the names of functions that you can call in history. */
/* Copyright (C) 1989-2003 Free Software Foundation, Inc.
unknown's avatar
unknown committed
3 4 5 6 7 8

   This file contains the GNU History Library (the Library), a set of
   routines for managing the text of previously typed lines.

   The Library is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
unknown's avatar
unknown committed
9
   the Free Software Foundation; either version 2, or (at your option)
unknown's avatar
unknown committed
10 11 12 13 14 15 16 17 18 19
   any later version.

   The Library is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   The GNU General Public License is often shipped with GNU software, and
   is generally kept in a file called COPYING or LICENSE.  If you do not
   have a copy of the license, write to the Free Software Foundation,
unknown's avatar
unknown committed
20
   59 Temple Place, Suite 330, Boston, MA 02111 USA. */
unknown's avatar
unknown committed
21 22 23 24 25 26 27 28

#ifndef _HISTORY_H_
#define _HISTORY_H_

#ifdef __cplusplus
extern "C" {
#endif

29 30
#include <time.h>		/* XXX - for history timestamp code */

unknown's avatar
unknown committed
31 32
#if defined READLINE_LIBRARY
#  include "rlstdc.h"
unknown's avatar
unknown committed
33
#  include "rltypedefs.h"
unknown's avatar
unknown committed
34 35
#else
#  include <readline/rlstdc.h>
unknown's avatar
unknown committed
36
#  include <readline/rltypedefs.h>
unknown's avatar
unknown committed
37 38 39 40 41 42 43 44 45 46 47
#endif

#ifdef __STDC__
typedef void *histdata_t;
#else
typedef char *histdata_t;
#endif

/* The structure used to store a history entry. */
typedef struct _hist_entry {
  char *line;
48
  char *timestamp;		/* char * rather than time_t for read/write */
unknown's avatar
unknown committed
49 50 51
  histdata_t data;
} HIST_ENTRY;

52 53 54
/* Size of the history-library-managed space in history entry HS. */
#define HISTENT_BYTES(hs)	(strlen ((hs)->line) + strlen ((hs)->timestamp))

unknown's avatar
unknown committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
/* A structure used to pass the current state of the history stuff around. */
typedef struct _hist_state {
  HIST_ENTRY **entries;		/* Pointer to the entries themselves. */
  int offset;			/* The location pointer within this array. */
  int length;			/* Number of elements within this array. */
  int size;			/* Number of slots allocated to this array. */
  int flags;
} HISTORY_STATE;

/* Flag values for the `flags' member of HISTORY_STATE. */
#define HS_STIFLED	0x01

/* Initialization and state management. */

/* Begin a session in which the history functions might be used.  This
   just initializes the interactive variables. */
unknown's avatar
unknown committed
71
extern void using_history PARAMS((void));
unknown's avatar
unknown committed
72 73

/* Return the current HISTORY_STATE of the history. */
unknown's avatar
unknown committed
74
extern HISTORY_STATE *history_get_history_state PARAMS((void));
unknown's avatar
unknown committed
75 76

/* Set the state of the current history array to STATE. */
unknown's avatar
unknown committed
77
extern void history_set_history_state PARAMS((HISTORY_STATE *));
unknown's avatar
unknown committed
78 79 80 81 82

/* Manage the history list. */

/* Place STRING at the end of the history list.
   The associated data field (if any) is set to NULL. */
unknown's avatar
unknown committed
83
extern void add_history PARAMS((const char *));
unknown's avatar
unknown committed
84

85 86 87 88
/* Change the timestamp associated with the most recent history entry to
   STRING. */
extern void add_history_time PARAMS((const char *));

unknown's avatar
unknown committed
89 90 91
/* A reasonably useless function, only here for completeness.  WHICH
   is the magic number that tells us which element to delete.  The
   elements are numbered from 0. */
unknown's avatar
unknown committed
92
extern HIST_ENTRY *remove_history PARAMS((int));
unknown's avatar
unknown committed
93

94 95 96 97
/* Free the history entry H and return any application-specific data
   associated with it. */
extern histdata_t free_history_entry PARAMS((HIST_ENTRY *));

unknown's avatar
unknown committed
98 99 100
/* 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. */
unknown's avatar
unknown committed
101
extern HIST_ENTRY *replace_history_entry PARAMS((int, const char *, histdata_t));
unknown's avatar
unknown committed
102 103

/* Clear the history list and start over. */
unknown's avatar
unknown committed
104
extern void clear_history PARAMS((void));
unknown's avatar
unknown committed
105 106

/* Stifle the history list, remembering only MAX number of entries. */
unknown's avatar
unknown committed
107
extern void stifle_history PARAMS((int));
unknown's avatar
unknown committed
108 109 110 111

/* 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. */
unknown's avatar
unknown committed
112
extern int unstifle_history PARAMS((void));
unknown's avatar
unknown committed
113 114

/* Return 1 if the history is stifled, 0 if it is not. */
unknown's avatar
unknown committed
115
extern int history_is_stifled PARAMS((void));
unknown's avatar
unknown committed
116 117 118 119 120 121

/* Information about the history list. */

/* 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. */
unknown's avatar
unknown committed
122
extern HIST_ENTRY **history_list PARAMS((void));
unknown's avatar
unknown committed
123 124 125

/* Returns the number which says what history element we are now
   looking at.  */
unknown's avatar
unknown committed
126 127
extern int where_history PARAMS((void));
  
unknown's avatar
unknown committed
128 129
/* Return the history entry at the current position, as determined by
   history_offset.  If there is no entry there, return a NULL pointer. */
unknown's avatar
unknown committed
130
extern HIST_ENTRY *current_history PARAMS((void));
unknown's avatar
unknown committed
131 132 133

/* Return the history entry which is logically at OFFSET in the history
   array.  OFFSET is relative to history_base. */
unknown's avatar
unknown committed
134
extern HIST_ENTRY *history_get PARAMS((int));
unknown's avatar
unknown committed
135

136 137 138 139
/* Return the timestamp associated with the HIST_ENTRY * passed as an
   argument */
extern time_t history_get_time PARAMS((HIST_ENTRY *));

unknown's avatar
unknown committed
140 141
/* Return the number of bytes that the primary history entries are using.
   This just adds up the lengths of the_history->lines. */
unknown's avatar
unknown committed
142
extern int history_total_bytes PARAMS((void));
unknown's avatar
unknown committed
143 144 145 146

/* Moving around the history list. */

/* Set the position in the history list to POS. */
unknown's avatar
unknown committed
147
extern int history_set_pos PARAMS((int));
unknown's avatar
unknown committed
148 149 150 151

/* 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. */
unknown's avatar
unknown committed
152
extern HIST_ENTRY *previous_history PARAMS((void));
unknown's avatar
unknown committed
153 154 155 156

/* Move history_offset forward to the next item in the input_history,
   and return the a pointer to that entry.  If there is no next entry,
   return a NULL pointer. */
unknown's avatar
unknown committed
157
extern HIST_ENTRY *next_history PARAMS((void));
unknown's avatar
unknown committed
158 159 160 161 162 163 164 165 166

/* Searching the history list. */

/* Search the history for STRING, starting at history_offset.
   If DIRECTION < 0, then the search is through previous entries,
   else through subsequent.  If the 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. */
unknown's avatar
unknown committed
167
extern int history_search PARAMS((const char *, int));
unknown's avatar
unknown committed
168 169 170 171

/* Search the history for STRING, starting at history_offset.
   The search is anchored: matching lines must begin with string.
   DIRECTION is as in history_search(). */
unknown's avatar
unknown committed
172
extern int history_search_prefix PARAMS((const char *, int));
unknown's avatar
unknown committed
173 174 175 176 177 178

/* Search for STRING in the history list, starting at POS, an
   absolute index into the list.  DIR, 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. */
unknown's avatar
unknown committed
179
extern int history_search_pos PARAMS((const char *, int, int));
unknown's avatar
unknown committed
180 181 182 183 184 185

/* Managing the history file. */

/* 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. */
unknown's avatar
unknown committed
186
extern int read_history PARAMS((const char *));
unknown's avatar
unknown committed
187 188 189 190 191 192

/* 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, read
   until the end of the file.  If FILENAME is NULL, then read from
   ~/.history.  Returns 0 if successful, or errno if not. */
unknown's avatar
unknown committed
193
extern int read_history_range PARAMS((const char *, int, int));
unknown's avatar
unknown committed
194 195 196 197

/* Write the current history to FILENAME.  If FILENAME is NULL,
   then write the history list to ~/.history.  Values returned
   are as in read_history ().  */
unknown's avatar
unknown committed
198
extern int write_history PARAMS((const char *));
unknown's avatar
unknown committed
199 200 201

/* Append NELEMENT entries to FILENAME.  The entries appended are from
   the end of the list minus NELEMENTs up to the end of the list. */
unknown's avatar
unknown committed
202
extern int append_history PARAMS((int, const char *));
unknown's avatar
unknown committed
203 204

/* Truncate the history file, leaving only the last NLINES lines. */
unknown's avatar
unknown committed
205
extern int history_truncate_file PARAMS((const char *, int));
unknown's avatar
unknown committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

/* History expansion. */

/* Expand the string 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
  -1) If there was an error in expansion.
   2) If the returned line should just be printed.

  If an error ocurred in expansion, then OUTPUT contains a descriptive
  error message. */
unknown's avatar
unknown committed
221
extern int history_expand PARAMS((char *, char **));
unknown's avatar
unknown committed
222 223 224 225

/* Extract a string segment consisting of the FIRST through LAST
   arguments present in STRING.  Arguments are broken up as in
   the shell. */
unknown's avatar
unknown committed
226
extern char *history_arg_extract PARAMS((int, int, const char *));
unknown's avatar
unknown committed
227 228 229 230 231 232 233

/* Return the text of the history event beginning at the current
   offset into STRING.  Pass STRING with *INDEX equal to the
   history_expansion_char that begins this specification.
   DELIMITING_QUOTE is a character that is allowed to end the string
   specification for what to search for in addition to the normal
   characters `:', ` ', `\t', `\n', and sometimes `?'. */
unknown's avatar
unknown committed
234
extern char *get_history_event PARAMS((const char *, int *, int));
unknown's avatar
unknown committed
235 236 237

/* Return an array of tokens, much as the shell might.  The tokens are
   parsed out of STRING. */
unknown's avatar
unknown committed
238
extern char **history_tokenize PARAMS((const char *));
unknown's avatar
unknown committed
239 240 241 242

/* Exported history variables. */
extern int history_base;
extern int history_length;
unknown's avatar
unknown committed
243
extern int history_max_entries;
unknown's avatar
unknown committed
244 245
extern char history_expansion_char;
extern char history_subst_char;
246
extern char *history_word_delimiters;
unknown's avatar
unknown committed
247
extern char history_comment_char;
248
extern char *history_no_expand_chars;
unknown's avatar
unknown committed
249
extern char *history_search_delimiter_chars;
unknown's avatar
unknown committed
250 251
extern int history_quotes_inhibit_expansion;

252 253
extern int history_write_timestamps;

unknown's avatar
unknown committed
254 255 256
/* Backwards compatibility */
extern int max_input_history;

unknown's avatar
unknown committed
257 258 259
/* If set, this function is called to decide whether or not a particular
   history expansion should be treated as a special case for the calling
   application and not expanded. */
unknown's avatar
unknown committed
260
extern rl_linebuf_func_t *history_inhibit_expansion_function;
unknown's avatar
unknown committed
261 262 263 264 265 266

#ifdef __cplusplus
}
#endif

#endif /* !_HISTORY_H_ */