1*7c478bd9Sstevel@tonic-gate #ifndef history_h 2*7c478bd9Sstevel@tonic-gate #define history_h 3*7c478bd9Sstevel@tonic-gate 4*7c478bd9Sstevel@tonic-gate /* 5*7c478bd9Sstevel@tonic-gate * Copyright (c) 2000, 2001, 2002, 2003, 2004 by Martin C. Shepherd. 6*7c478bd9Sstevel@tonic-gate * 7*7c478bd9Sstevel@tonic-gate * All rights reserved. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * Permission is hereby granted, free of charge, to any person obtaining a 10*7c478bd9Sstevel@tonic-gate * copy of this software and associated documentation files (the 11*7c478bd9Sstevel@tonic-gate * "Software"), to deal in the Software without restriction, including 12*7c478bd9Sstevel@tonic-gate * without limitation the rights to use, copy, modify, merge, publish, 13*7c478bd9Sstevel@tonic-gate * distribute, and/or sell copies of the Software, and to permit persons 14*7c478bd9Sstevel@tonic-gate * to whom the Software is furnished to do so, provided that the above 15*7c478bd9Sstevel@tonic-gate * copyright notice(s) and this permission notice appear in all copies of 16*7c478bd9Sstevel@tonic-gate * the Software and that both the above copyright notice(s) and this 17*7c478bd9Sstevel@tonic-gate * permission notice appear in supporting documentation. 18*7c478bd9Sstevel@tonic-gate * 19*7c478bd9Sstevel@tonic-gate * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 20*7c478bd9Sstevel@tonic-gate * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21*7c478bd9Sstevel@tonic-gate * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT 22*7c478bd9Sstevel@tonic-gate * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 23*7c478bd9Sstevel@tonic-gate * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL 24*7c478bd9Sstevel@tonic-gate * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING 25*7c478bd9Sstevel@tonic-gate * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 26*7c478bd9Sstevel@tonic-gate * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 27*7c478bd9Sstevel@tonic-gate * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 28*7c478bd9Sstevel@tonic-gate * 29*7c478bd9Sstevel@tonic-gate * Except as contained in this notice, the name of a copyright holder 30*7c478bd9Sstevel@tonic-gate * shall not be used in advertising or otherwise to promote the sale, use 31*7c478bd9Sstevel@tonic-gate * or other dealings in this Software without prior written authorization 32*7c478bd9Sstevel@tonic-gate * of the copyright holder. 33*7c478bd9Sstevel@tonic-gate */ 34*7c478bd9Sstevel@tonic-gate 35*7c478bd9Sstevel@tonic-gate #include <stdio.h> /* FILE * */ 36*7c478bd9Sstevel@tonic-gate 37*7c478bd9Sstevel@tonic-gate /*----------------------------------------------------------------------- 38*7c478bd9Sstevel@tonic-gate * This module is used to record and traverse historical lines of user input. 39*7c478bd9Sstevel@tonic-gate */ 40*7c478bd9Sstevel@tonic-gate 41*7c478bd9Sstevel@tonic-gate typedef struct GlHistory GlHistory; 42*7c478bd9Sstevel@tonic-gate 43*7c478bd9Sstevel@tonic-gate /* 44*7c478bd9Sstevel@tonic-gate * Create a new history maintenance object. 45*7c478bd9Sstevel@tonic-gate */ 46*7c478bd9Sstevel@tonic-gate GlHistory *_new_GlHistory(size_t buflen); 47*7c478bd9Sstevel@tonic-gate 48*7c478bd9Sstevel@tonic-gate /* 49*7c478bd9Sstevel@tonic-gate * Delete a history maintenance object. 50*7c478bd9Sstevel@tonic-gate */ 51*7c478bd9Sstevel@tonic-gate GlHistory *_del_GlHistory(GlHistory *glh); 52*7c478bd9Sstevel@tonic-gate 53*7c478bd9Sstevel@tonic-gate int _glh_add_history(GlHistory *glh, const char *line, int force); 54*7c478bd9Sstevel@tonic-gate 55*7c478bd9Sstevel@tonic-gate int _glh_search_prefix(GlHistory *glh, const char *line, int prefix_len); 56*7c478bd9Sstevel@tonic-gate 57*7c478bd9Sstevel@tonic-gate char *_glh_find_backwards(GlHistory *glh, char *line, size_t dim); 58*7c478bd9Sstevel@tonic-gate char *_glh_find_forwards(GlHistory *glh, char *line, size_t dim); 59*7c478bd9Sstevel@tonic-gate 60*7c478bd9Sstevel@tonic-gate int _glh_cancel_search(GlHistory *glh); 61*7c478bd9Sstevel@tonic-gate 62*7c478bd9Sstevel@tonic-gate char *_glh_oldest_line(GlHistory *glh, char *line, size_t dim); 63*7c478bd9Sstevel@tonic-gate char *_glh_current_line(GlHistory *glh, char *line, size_t dim); 64*7c478bd9Sstevel@tonic-gate 65*7c478bd9Sstevel@tonic-gate /* 66*7c478bd9Sstevel@tonic-gate * Whenever a new line is added to the history buffer, it is given 67*7c478bd9Sstevel@tonic-gate * a unique ID, recorded in an object of the following type. 68*7c478bd9Sstevel@tonic-gate */ 69*7c478bd9Sstevel@tonic-gate typedef unsigned long GlhLineID; 70*7c478bd9Sstevel@tonic-gate 71*7c478bd9Sstevel@tonic-gate /* 72*7c478bd9Sstevel@tonic-gate * Query the id of a history line offset by a given number of lines from 73*7c478bd9Sstevel@tonic-gate * the one that is currently being recalled. If a recall session isn't 74*7c478bd9Sstevel@tonic-gate * in progress, or the offset points outside the history list, 0 is 75*7c478bd9Sstevel@tonic-gate * returned. 76*7c478bd9Sstevel@tonic-gate */ 77*7c478bd9Sstevel@tonic-gate GlhLineID _glh_line_id(GlHistory *glh, int offset); 78*7c478bd9Sstevel@tonic-gate 79*7c478bd9Sstevel@tonic-gate /* 80*7c478bd9Sstevel@tonic-gate * Recall a line by its history buffer ID. If the line is no longer 81*7c478bd9Sstevel@tonic-gate * in the buffer, or the specified id is zero, NULL is returned. 82*7c478bd9Sstevel@tonic-gate */ 83*7c478bd9Sstevel@tonic-gate char *_glh_recall_line(GlHistory *glh, GlhLineID id, char *line, size_t dim); 84*7c478bd9Sstevel@tonic-gate 85*7c478bd9Sstevel@tonic-gate /* 86*7c478bd9Sstevel@tonic-gate * Write the contents of the history buffer to a given file. Note that 87*7c478bd9Sstevel@tonic-gate * ~ and $ expansion are not performed on the filename. 88*7c478bd9Sstevel@tonic-gate */ 89*7c478bd9Sstevel@tonic-gate int _glh_save_history(GlHistory *glh, const char *filename, 90*7c478bd9Sstevel@tonic-gate const char *comment, int max_lines); 91*7c478bd9Sstevel@tonic-gate 92*7c478bd9Sstevel@tonic-gate /* 93*7c478bd9Sstevel@tonic-gate * Restore the contents of the history buffer from a given file. 94*7c478bd9Sstevel@tonic-gate * Note that ~ and $ expansion are not performed on the filename. 95*7c478bd9Sstevel@tonic-gate */ 96*7c478bd9Sstevel@tonic-gate int _glh_load_history(GlHistory *glh, const char *filename, const char *comment, 97*7c478bd9Sstevel@tonic-gate char *line, size_t dim); 98*7c478bd9Sstevel@tonic-gate 99*7c478bd9Sstevel@tonic-gate /* 100*7c478bd9Sstevel@tonic-gate * Set and query the current history group. 101*7c478bd9Sstevel@tonic-gate */ 102*7c478bd9Sstevel@tonic-gate int _glh_set_group(GlHistory *glh, unsigned group); 103*7c478bd9Sstevel@tonic-gate int _glh_get_group(GlHistory *glh); 104*7c478bd9Sstevel@tonic-gate 105*7c478bd9Sstevel@tonic-gate /* 106*7c478bd9Sstevel@tonic-gate * Display the contents of the history list to the specified stdio 107*7c478bd9Sstevel@tonic-gate * output group. 108*7c478bd9Sstevel@tonic-gate */ 109*7c478bd9Sstevel@tonic-gate int _glh_show_history(GlHistory *glh, GlWriteFn *write_fn, void *data, 110*7c478bd9Sstevel@tonic-gate const char *fmt, int all_groups, int max_lines); 111*7c478bd9Sstevel@tonic-gate 112*7c478bd9Sstevel@tonic-gate /* 113*7c478bd9Sstevel@tonic-gate * Change the size of the history buffer. 114*7c478bd9Sstevel@tonic-gate */ 115*7c478bd9Sstevel@tonic-gate int _glh_resize_history(GlHistory *glh, size_t bufsize); 116*7c478bd9Sstevel@tonic-gate 117*7c478bd9Sstevel@tonic-gate /* 118*7c478bd9Sstevel@tonic-gate * Set an upper limit to the number of lines that can be recorded in the 119*7c478bd9Sstevel@tonic-gate * history list, or remove a previously specified limit. 120*7c478bd9Sstevel@tonic-gate */ 121*7c478bd9Sstevel@tonic-gate void _glh_limit_history(GlHistory *glh, int max_lines); 122*7c478bd9Sstevel@tonic-gate 123*7c478bd9Sstevel@tonic-gate /* 124*7c478bd9Sstevel@tonic-gate * Discard either all history, or the history associated with the current 125*7c478bd9Sstevel@tonic-gate * history group. 126*7c478bd9Sstevel@tonic-gate */ 127*7c478bd9Sstevel@tonic-gate void _glh_clear_history(GlHistory *glh, int all_groups); 128*7c478bd9Sstevel@tonic-gate 129*7c478bd9Sstevel@tonic-gate /* 130*7c478bd9Sstevel@tonic-gate * Temporarily enable or disable the history facility. 131*7c478bd9Sstevel@tonic-gate */ 132*7c478bd9Sstevel@tonic-gate void _glh_toggle_history(GlHistory *glh, int enable); 133*7c478bd9Sstevel@tonic-gate 134*7c478bd9Sstevel@tonic-gate /* 135*7c478bd9Sstevel@tonic-gate * Lookup a history line by its sequential number of entry in the 136*7c478bd9Sstevel@tonic-gate * history buffer. 137*7c478bd9Sstevel@tonic-gate */ 138*7c478bd9Sstevel@tonic-gate int _glh_lookup_history(GlHistory *glh, GlhLineID id, const char **line, 139*7c478bd9Sstevel@tonic-gate unsigned *group, time_t *timestamp); 140*7c478bd9Sstevel@tonic-gate 141*7c478bd9Sstevel@tonic-gate /* 142*7c478bd9Sstevel@tonic-gate * Query the state of the history list. 143*7c478bd9Sstevel@tonic-gate */ 144*7c478bd9Sstevel@tonic-gate void _glh_state_of_history(GlHistory *glh, int *enabled, unsigned *group, 145*7c478bd9Sstevel@tonic-gate int *max_lines); 146*7c478bd9Sstevel@tonic-gate 147*7c478bd9Sstevel@tonic-gate /* 148*7c478bd9Sstevel@tonic-gate * Get the range of lines in the history buffer. 149*7c478bd9Sstevel@tonic-gate */ 150*7c478bd9Sstevel@tonic-gate void _glh_range_of_history(GlHistory *glh, unsigned long *oldest, 151*7c478bd9Sstevel@tonic-gate unsigned long *newest, int *nlines); 152*7c478bd9Sstevel@tonic-gate 153*7c478bd9Sstevel@tonic-gate /* 154*7c478bd9Sstevel@tonic-gate * Return the size of the history buffer and the amount of the 155*7c478bd9Sstevel@tonic-gate * buffer that is currently in use. 156*7c478bd9Sstevel@tonic-gate */ 157*7c478bd9Sstevel@tonic-gate void _glh_size_of_history(GlHistory *glh, size_t *buff_size, size_t *buff_used); 158*7c478bd9Sstevel@tonic-gate 159*7c478bd9Sstevel@tonic-gate /* 160*7c478bd9Sstevel@tonic-gate * Get information about the last error in this module. 161*7c478bd9Sstevel@tonic-gate */ 162*7c478bd9Sstevel@tonic-gate const char *_glh_last_error(GlHistory *glh); 163*7c478bd9Sstevel@tonic-gate 164*7c478bd9Sstevel@tonic-gate /* 165*7c478bd9Sstevel@tonic-gate * Return non-zero if a history search session is currently in progress. 166*7c478bd9Sstevel@tonic-gate */ 167*7c478bd9Sstevel@tonic-gate int _glh_search_active(GlHistory *glh); 168*7c478bd9Sstevel@tonic-gate 169*7c478bd9Sstevel@tonic-gate #endif 170