1136644Sache/* history.h -- the names of functions that you can call in history. */ 2136644Sache/* Copyright (C) 1989-2003 Free Software Foundation, Inc. 321308Sache 421308Sache This file contains the GNU History Library (the Library), a set of 521308Sache routines for managing the text of previously typed lines. 621308Sache 721308Sache The Library is free software; you can redistribute it and/or modify 821308Sache it under the terms of the GNU General Public License as published by 958310Sache the Free Software Foundation; either version 2, or (at your option) 1021308Sache any later version. 1121308Sache 1221308Sache The Library is distributed in the hope that it will be useful, but 1321308Sache WITHOUT ANY WARRANTY; without even the implied warranty of 1421308Sache MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1521308Sache General Public License for more details. 1621308Sache 1721308Sache The GNU General Public License is often shipped with GNU software, and 1821308Sache is generally kept in a file called COPYING or LICENSE. If you do not 1921308Sache have a copy of the license, write to the Free Software Foundation, 2058310Sache 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ 2121308Sache 2221308Sache#ifndef _HISTORY_H_ 2321308Sache#define _HISTORY_H_ 2421308Sache 2547558Sache#ifdef __cplusplus 2647558Sacheextern "C" { 2747558Sache#endif 2847558Sache 29136644Sache#include <time.h> /* XXX - for history timestamp code */ 30136644Sache 3147558Sache#if defined READLINE_LIBRARY 3247558Sache# include "rlstdc.h" 3375406Sache# include "rltypedefs.h" 3447558Sache#else 3547558Sache# include <readline/rlstdc.h> 3675406Sache# include <readline/rltypedefs.h> 3747558Sache#endif 3847558Sache 3947558Sache#ifdef __STDC__ 4047558Sachetypedef void *histdata_t; 4147558Sache#else 4247558Sachetypedef char *histdata_t; 4347558Sache#endif 4447558Sache 4521308Sache/* The structure used to store a history entry. */ 4621308Sachetypedef struct _hist_entry { 4721308Sache char *line; 48136644Sache char *timestamp; /* char * rather than time_t for read/write */ 4947558Sache histdata_t data; 5021308Sache} HIST_ENTRY; 5121308Sache 52136644Sache/* Size of the history-library-managed space in history entry HS. */ 53136644Sache#define HISTENT_BYTES(hs) (strlen ((hs)->line) + strlen ((hs)->timestamp)) 54136644Sache 5521308Sache/* A structure used to pass the current state of the history stuff around. */ 5621308Sachetypedef struct _hist_state { 5721308Sache HIST_ENTRY **entries; /* Pointer to the entries themselves. */ 5821308Sache int offset; /* The location pointer within this array. */ 5921308Sache int length; /* Number of elements within this array. */ 6021308Sache int size; /* Number of slots allocated to this array. */ 6121308Sache int flags; 6221308Sache} HISTORY_STATE; 6321308Sache 6421308Sache/* Flag values for the `flags' member of HISTORY_STATE. */ 6521308Sache#define HS_STIFLED 0x01 6621308Sache 6721308Sache/* Initialization and state management. */ 6821308Sache 6921308Sache/* Begin a session in which the history functions might be used. This 7021308Sache just initializes the interactive variables. */ 71119610Sacheextern void using_history PARAMS((void)); 7221308Sache 7321308Sache/* Return the current HISTORY_STATE of the history. */ 74119610Sacheextern HISTORY_STATE *history_get_history_state PARAMS((void)); 7521308Sache 7621308Sache/* Set the state of the current history array to STATE. */ 77119610Sacheextern void history_set_history_state PARAMS((HISTORY_STATE *)); 7821308Sache 7921308Sache/* Manage the history list. */ 8021308Sache 8121308Sache/* Place STRING at the end of the history list. 8221308Sache The associated data field (if any) is set to NULL. */ 83119610Sacheextern void add_history PARAMS((const char *)); 8421308Sache 85136644Sache/* Change the timestamp associated with the most recent history entry to 86136644Sache STRING. */ 87136644Sacheextern void add_history_time PARAMS((const char *)); 88136644Sache 8921308Sache/* A reasonably useless function, only here for completeness. WHICH 9021308Sache is the magic number that tells us which element to delete. The 9121308Sache elements are numbered from 0. */ 92119610Sacheextern HIST_ENTRY *remove_history PARAMS((int)); 9321308Sache 94136644Sache/* Free the history entry H and return any application-specific data 95136644Sache associated with it. */ 96136644Sacheextern histdata_t free_history_entry PARAMS((HIST_ENTRY *)); 97136644Sache 9821308Sache/* Make the history entry at WHICH have LINE and DATA. This returns 9921308Sache the old entry so you can dispose of the data. In the case of an 10021308Sache invalid WHICH, a NULL pointer is returned. */ 101119610Sacheextern HIST_ENTRY *replace_history_entry PARAMS((int, const char *, histdata_t)); 10221308Sache 10321308Sache/* Clear the history list and start over. */ 104119610Sacheextern void clear_history PARAMS((void)); 10521308Sache 10621308Sache/* Stifle the history list, remembering only MAX number of entries. */ 107119610Sacheextern void stifle_history PARAMS((int)); 10821308Sache 10921308Sache/* Stop stifling the history. This returns the previous amount the 11021308Sache history was stifled by. The value is positive if the history was 11121308Sache stifled, negative if it wasn't. */ 112119610Sacheextern int unstifle_history PARAMS((void)); 11321308Sache 11421308Sache/* Return 1 if the history is stifled, 0 if it is not. */ 115119610Sacheextern int history_is_stifled PARAMS((void)); 11621308Sache 11721308Sache/* Information about the history list. */ 11821308Sache 11921308Sache/* Return a NULL terminated array of HIST_ENTRY which is the current input 12021308Sache history. Element 0 of this list is the beginning of time. If there 12121308Sache is no history, return NULL. */ 122119610Sacheextern HIST_ENTRY **history_list PARAMS((void)); 12321308Sache 12421308Sache/* Returns the number which says what history element we are now 12521308Sache looking at. */ 126119610Sacheextern int where_history PARAMS((void)); 12721308Sache 12821308Sache/* Return the history entry at the current position, as determined by 12921308Sache history_offset. If there is no entry there, return a NULL pointer. */ 130119610Sacheextern HIST_ENTRY *current_history PARAMS((void)); 13121308Sache 13221308Sache/* Return the history entry which is logically at OFFSET in the history 13321308Sache array. OFFSET is relative to history_base. */ 134119610Sacheextern HIST_ENTRY *history_get PARAMS((int)); 13521308Sache 136136644Sache/* Return the timestamp associated with the HIST_ENTRY * passed as an 137136644Sache argument */ 138136644Sacheextern time_t history_get_time PARAMS((HIST_ENTRY *)); 139136644Sache 14021308Sache/* Return the number of bytes that the primary history entries are using. 14121308Sache This just adds up the lengths of the_history->lines. */ 142119610Sacheextern int history_total_bytes PARAMS((void)); 14321308Sache 14421308Sache/* Moving around the history list. */ 14521308Sache 14621308Sache/* Set the position in the history list to POS. */ 147119610Sacheextern int history_set_pos PARAMS((int)); 14821308Sache 14921308Sache/* Back up history_offset to the previous history entry, and return 15021308Sache a pointer to that entry. If there is no previous entry, return 15121308Sache a NULL pointer. */ 152119610Sacheextern HIST_ENTRY *previous_history PARAMS((void)); 15321308Sache 15421308Sache/* Move history_offset forward to the next item in the input_history, 15521308Sache and return the a pointer to that entry. If there is no next entry, 15621308Sache return a NULL pointer. */ 157119610Sacheextern HIST_ENTRY *next_history PARAMS((void)); 15821308Sache 15921308Sache/* Searching the history list. */ 16021308Sache 16121308Sache/* Search the history for STRING, starting at history_offset. 16221308Sache If DIRECTION < 0, then the search is through previous entries, 16321308Sache else through subsequent. If the string is found, then 16421308Sache current_history () is the history entry, and the value of this function 16521308Sache is the offset in the line of that history entry that the string was 16621308Sache found in. Otherwise, nothing is changed, and a -1 is returned. */ 167119610Sacheextern int history_search PARAMS((const char *, int)); 16821308Sache 16921308Sache/* Search the history for STRING, starting at history_offset. 17047558Sache The search is anchored: matching lines must begin with string. 17147558Sache DIRECTION is as in history_search(). */ 172119610Sacheextern int history_search_prefix PARAMS((const char *, int)); 17321308Sache 17421308Sache/* Search for STRING in the history list, starting at POS, an 17521308Sache absolute index into the list. DIR, if negative, says to search 17621308Sache backwards from POS, else forwards. 17721308Sache Returns the absolute index of the history element where STRING 17821308Sache was found, or -1 otherwise. */ 179119610Sacheextern int history_search_pos PARAMS((const char *, int, int)); 18021308Sache 18121308Sache/* Managing the history file. */ 18221308Sache 18321308Sache/* Add the contents of FILENAME to the history list, a line at a time. 18421308Sache If FILENAME is NULL, then read from ~/.history. Returns 0 if 18521308Sache successful, or errno if not. */ 186119610Sacheextern int read_history PARAMS((const char *)); 18721308Sache 18821308Sache/* Read a range of lines from FILENAME, adding them to the history list. 18921308Sache Start reading at the FROM'th line and end at the TO'th. If FROM 19021308Sache is zero, start at the beginning. If TO is less than FROM, read 19121308Sache until the end of the file. If FILENAME is NULL, then read from 19221308Sache ~/.history. Returns 0 if successful, or errno if not. */ 193119610Sacheextern int read_history_range PARAMS((const char *, int, int)); 19421308Sache 19521308Sache/* Write the current history to FILENAME. If FILENAME is NULL, 19621308Sache then write the history list to ~/.history. Values returned 19721308Sache are as in read_history (). */ 198119610Sacheextern int write_history PARAMS((const char *)); 19921308Sache 20021308Sache/* Append NELEMENT entries to FILENAME. The entries appended are from 20121308Sache the end of the list minus NELEMENTs up to the end of the list. */ 202119610Sacheextern int append_history PARAMS((int, const char *)); 20321308Sache 20421308Sache/* Truncate the history file, leaving only the last NLINES lines. */ 205119610Sacheextern int history_truncate_file PARAMS((const char *, int)); 20621308Sache 20721308Sache/* History expansion. */ 20821308Sache 20921308Sache/* Expand the string STRING, placing the result into OUTPUT, a pointer 21021308Sache to a string. Returns: 21121308Sache 21221308Sache 0) If no expansions took place (or, if the only change in 21321308Sache the text was the de-slashifying of the history expansion 21421308Sache character) 21521308Sache 1) If expansions did take place 21621308Sache -1) If there was an error in expansion. 21721308Sache 2) If the returned line should just be printed. 21821308Sache 21921308Sache If an error ocurred in expansion, then OUTPUT contains a descriptive 22021308Sache error message. */ 221119610Sacheextern int history_expand PARAMS((char *, char **)); 22221308Sache 22321308Sache/* Extract a string segment consisting of the FIRST through LAST 22421308Sache arguments present in STRING. Arguments are broken up as in 22521308Sache the shell. */ 226119610Sacheextern char *history_arg_extract PARAMS((int, int, const char *)); 22721308Sache 22821308Sache/* Return the text of the history event beginning at the current 22947558Sache offset into STRING. Pass STRING with *INDEX equal to the 23047558Sache history_expansion_char that begins this specification. 23147558Sache DELIMITING_QUOTE is a character that is allowed to end the string 23247558Sache specification for what to search for in addition to the normal 23347558Sache characters `:', ` ', `\t', `\n', and sometimes `?'. */ 234119610Sacheextern char *get_history_event PARAMS((const char *, int *, int)); 23521308Sache 23621308Sache/* Return an array of tokens, much as the shell might. The tokens are 23721308Sache parsed out of STRING. */ 238119610Sacheextern char **history_tokenize PARAMS((const char *)); 23921308Sache 24021308Sache/* Exported history variables. */ 24121308Sacheextern int history_base; 24221308Sacheextern int history_length; 24375406Sacheextern int history_max_entries; 24421308Sacheextern char history_expansion_char; 24521308Sacheextern char history_subst_char; 24675406Sacheextern char *history_word_delimiters; 24721308Sacheextern char history_comment_char; 24821308Sacheextern char *history_no_expand_chars; 24921308Sacheextern char *history_search_delimiter_chars; 25021308Sacheextern int history_quotes_inhibit_expansion; 25121308Sache 252136644Sacheextern int history_write_timestamps; 253136644Sache 25475406Sache/* Backwards compatibility */ 25575406Sacheextern int max_input_history; 25675406Sache 25726497Sache/* If set, this function is called to decide whether or not a particular 25826497Sache history expansion should be treated as a special case for the calling 25926497Sache application and not expanded. */ 26075406Sacheextern rl_linebuf_func_t *history_inhibit_expansion_function; 26126497Sache 26247558Sache#ifdef __cplusplus 26347558Sache} 26447558Sache#endif 26547558Sache 26621308Sache#endif /* !_HISTORY_H_ */ 267