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 #pragma ident "%Z%%M% %I% %E% SMI" 36*7c478bd9Sstevel@tonic-gate 37*7c478bd9Sstevel@tonic-gate #include <stdio.h> /* FILE * */ 38*7c478bd9Sstevel@tonic-gate 39*7c478bd9Sstevel@tonic-gate /*----------------------------------------------------------------------- 40*7c478bd9Sstevel@tonic-gate * This module is used to record and traverse historical lines of user input. 41*7c478bd9Sstevel@tonic-gate */ 42*7c478bd9Sstevel@tonic-gate 43*7c478bd9Sstevel@tonic-gate typedef struct GlHistory GlHistory; 44*7c478bd9Sstevel@tonic-gate 45*7c478bd9Sstevel@tonic-gate /* 46*7c478bd9Sstevel@tonic-gate * Create a new history maintenance object. 47*7c478bd9Sstevel@tonic-gate */ 48*7c478bd9Sstevel@tonic-gate GlHistory *_new_GlHistory(size_t buflen); 49*7c478bd9Sstevel@tonic-gate 50*7c478bd9Sstevel@tonic-gate /* 51*7c478bd9Sstevel@tonic-gate * Delete a history maintenance object. 52*7c478bd9Sstevel@tonic-gate */ 53*7c478bd9Sstevel@tonic-gate GlHistory *_del_GlHistory(GlHistory *glh); 54*7c478bd9Sstevel@tonic-gate 55*7c478bd9Sstevel@tonic-gate int _glh_add_history(GlHistory *glh, const char *line, int force); 56*7c478bd9Sstevel@tonic-gate 57*7c478bd9Sstevel@tonic-gate int _glh_search_prefix(GlHistory *glh, const char *line, int prefix_len); 58*7c478bd9Sstevel@tonic-gate 59*7c478bd9Sstevel@tonic-gate char *_glh_find_backwards(GlHistory *glh, char *line, size_t dim); 60*7c478bd9Sstevel@tonic-gate char *_glh_find_forwards(GlHistory *glh, char *line, size_t dim); 61*7c478bd9Sstevel@tonic-gate 62*7c478bd9Sstevel@tonic-gate int _glh_cancel_search(GlHistory *glh); 63*7c478bd9Sstevel@tonic-gate 64*7c478bd9Sstevel@tonic-gate char *_glh_oldest_line(GlHistory *glh, char *line, size_t dim); 65*7c478bd9Sstevel@tonic-gate char *_glh_current_line(GlHistory *glh, char *line, size_t dim); 66*7c478bd9Sstevel@tonic-gate 67*7c478bd9Sstevel@tonic-gate /* 68*7c478bd9Sstevel@tonic-gate * Whenever a new line is added to the history buffer, it is given 69*7c478bd9Sstevel@tonic-gate * a unique ID, recorded in an object of the following type. 70*7c478bd9Sstevel@tonic-gate */ 71*7c478bd9Sstevel@tonic-gate typedef unsigned long GlhLineID; 72*7c478bd9Sstevel@tonic-gate 73*7c478bd9Sstevel@tonic-gate /* 74*7c478bd9Sstevel@tonic-gate * Query the id of a history line offset by a given number of lines from 75*7c478bd9Sstevel@tonic-gate * the one that is currently being recalled. If a recall session isn't 76*7c478bd9Sstevel@tonic-gate * in progress, or the offset points outside the history list, 0 is 77*7c478bd9Sstevel@tonic-gate * returned. 78*7c478bd9Sstevel@tonic-gate */ 79*7c478bd9Sstevel@tonic-gate GlhLineID _glh_line_id(GlHistory *glh, int offset); 80*7c478bd9Sstevel@tonic-gate 81*7c478bd9Sstevel@tonic-gate /* 82*7c478bd9Sstevel@tonic-gate * Recall a line by its history buffer ID. If the line is no longer 83*7c478bd9Sstevel@tonic-gate * in the buffer, or the specified id is zero, NULL is returned. 84*7c478bd9Sstevel@tonic-gate */ 85*7c478bd9Sstevel@tonic-gate char *_glh_recall_line(GlHistory *glh, GlhLineID id, char *line, size_t dim); 86*7c478bd9Sstevel@tonic-gate 87*7c478bd9Sstevel@tonic-gate /* 88*7c478bd9Sstevel@tonic-gate * Write the contents of the history buffer to a given file. Note that 89*7c478bd9Sstevel@tonic-gate * ~ and $ expansion are not performed on the filename. 90*7c478bd9Sstevel@tonic-gate */ 91*7c478bd9Sstevel@tonic-gate int _glh_save_history(GlHistory *glh, const char *filename, 92*7c478bd9Sstevel@tonic-gate const char *comment, int max_lines); 93*7c478bd9Sstevel@tonic-gate 94*7c478bd9Sstevel@tonic-gate /* 95*7c478bd9Sstevel@tonic-gate * Restore the contents of the history buffer from a given file. 96*7c478bd9Sstevel@tonic-gate * Note that ~ and $ expansion are not performed on the filename. 97*7c478bd9Sstevel@tonic-gate */ 98*7c478bd9Sstevel@tonic-gate int _glh_load_history(GlHistory *glh, const char *filename, const char *comment, 99*7c478bd9Sstevel@tonic-gate char *line, size_t dim); 100*7c478bd9Sstevel@tonic-gate 101*7c478bd9Sstevel@tonic-gate /* 102*7c478bd9Sstevel@tonic-gate * Set and query the current history group. 103*7c478bd9Sstevel@tonic-gate */ 104*7c478bd9Sstevel@tonic-gate int _glh_set_group(GlHistory *glh, unsigned group); 105*7c478bd9Sstevel@tonic-gate int _glh_get_group(GlHistory *glh); 106*7c478bd9Sstevel@tonic-gate 107*7c478bd9Sstevel@tonic-gate /* 108*7c478bd9Sstevel@tonic-gate * Display the contents of the history list to the specified stdio 109*7c478bd9Sstevel@tonic-gate * output group. 110*7c478bd9Sstevel@tonic-gate */ 111*7c478bd9Sstevel@tonic-gate int _glh_show_history(GlHistory *glh, GlWriteFn *write_fn, void *data, 112*7c478bd9Sstevel@tonic-gate const char *fmt, int all_groups, int max_lines); 113*7c478bd9Sstevel@tonic-gate 114*7c478bd9Sstevel@tonic-gate /* 115*7c478bd9Sstevel@tonic-gate * Change the size of the history buffer. 116*7c478bd9Sstevel@tonic-gate */ 117*7c478bd9Sstevel@tonic-gate int _glh_resize_history(GlHistory *glh, size_t bufsize); 118*7c478bd9Sstevel@tonic-gate 119*7c478bd9Sstevel@tonic-gate /* 120*7c478bd9Sstevel@tonic-gate * Set an upper limit to the number of lines that can be recorded in the 121*7c478bd9Sstevel@tonic-gate * history list, or remove a previously specified limit. 122*7c478bd9Sstevel@tonic-gate */ 123*7c478bd9Sstevel@tonic-gate void _glh_limit_history(GlHistory *glh, int max_lines); 124*7c478bd9Sstevel@tonic-gate 125*7c478bd9Sstevel@tonic-gate /* 126*7c478bd9Sstevel@tonic-gate * Discard either all history, or the history associated with the current 127*7c478bd9Sstevel@tonic-gate * history group. 128*7c478bd9Sstevel@tonic-gate */ 129*7c478bd9Sstevel@tonic-gate void _glh_clear_history(GlHistory *glh, int all_groups); 130*7c478bd9Sstevel@tonic-gate 131*7c478bd9Sstevel@tonic-gate /* 132*7c478bd9Sstevel@tonic-gate * Temporarily enable or disable the history facility. 133*7c478bd9Sstevel@tonic-gate */ 134*7c478bd9Sstevel@tonic-gate void _glh_toggle_history(GlHistory *glh, int enable); 135*7c478bd9Sstevel@tonic-gate 136*7c478bd9Sstevel@tonic-gate /* 137*7c478bd9Sstevel@tonic-gate * Lookup a history line by its sequential number of entry in the 138*7c478bd9Sstevel@tonic-gate * history buffer. 139*7c478bd9Sstevel@tonic-gate */ 140*7c478bd9Sstevel@tonic-gate int _glh_lookup_history(GlHistory *glh, GlhLineID id, const char **line, 141*7c478bd9Sstevel@tonic-gate unsigned *group, time_t *timestamp); 142*7c478bd9Sstevel@tonic-gate 143*7c478bd9Sstevel@tonic-gate /* 144*7c478bd9Sstevel@tonic-gate * Query the state of the history list. 145*7c478bd9Sstevel@tonic-gate */ 146*7c478bd9Sstevel@tonic-gate void _glh_state_of_history(GlHistory *glh, int *enabled, unsigned *group, 147*7c478bd9Sstevel@tonic-gate int *max_lines); 148*7c478bd9Sstevel@tonic-gate 149*7c478bd9Sstevel@tonic-gate /* 150*7c478bd9Sstevel@tonic-gate * Get the range of lines in the history buffer. 151*7c478bd9Sstevel@tonic-gate */ 152*7c478bd9Sstevel@tonic-gate void _glh_range_of_history(GlHistory *glh, unsigned long *oldest, 153*7c478bd9Sstevel@tonic-gate unsigned long *newest, int *nlines); 154*7c478bd9Sstevel@tonic-gate 155*7c478bd9Sstevel@tonic-gate /* 156*7c478bd9Sstevel@tonic-gate * Return the size of the history buffer and the amount of the 157*7c478bd9Sstevel@tonic-gate * buffer that is currently in use. 158*7c478bd9Sstevel@tonic-gate */ 159*7c478bd9Sstevel@tonic-gate void _glh_size_of_history(GlHistory *glh, size_t *buff_size, size_t *buff_used); 160*7c478bd9Sstevel@tonic-gate 161*7c478bd9Sstevel@tonic-gate /* 162*7c478bd9Sstevel@tonic-gate * Get information about the last error in this module. 163*7c478bd9Sstevel@tonic-gate */ 164*7c478bd9Sstevel@tonic-gate const char *_glh_last_error(GlHistory *glh); 165*7c478bd9Sstevel@tonic-gate 166*7c478bd9Sstevel@tonic-gate /* 167*7c478bd9Sstevel@tonic-gate * Return non-zero if a history search session is currently in progress. 168*7c478bd9Sstevel@tonic-gate */ 169*7c478bd9Sstevel@tonic-gate int _glh_search_active(GlHistory *glh); 170*7c478bd9Sstevel@tonic-gate 171*7c478bd9Sstevel@tonic-gate #endif 172