17c478bd9Sstevel@tonic-gate #ifndef history_h
27c478bd9Sstevel@tonic-gate #define history_h
37c478bd9Sstevel@tonic-gate 
47c478bd9Sstevel@tonic-gate /*
57c478bd9Sstevel@tonic-gate  * Copyright (c) 2000, 2001, 2002, 2003, 2004 by Martin C. Shepherd.
6*1da57d55SToomas Soome  *
77c478bd9Sstevel@tonic-gate  * All rights reserved.
8*1da57d55SToomas Soome  *
97c478bd9Sstevel@tonic-gate  * Permission is hereby granted, free of charge, to any person obtaining a
107c478bd9Sstevel@tonic-gate  * copy of this software and associated documentation files (the
117c478bd9Sstevel@tonic-gate  * "Software"), to deal in the Software without restriction, including
127c478bd9Sstevel@tonic-gate  * without limitation the rights to use, copy, modify, merge, publish,
137c478bd9Sstevel@tonic-gate  * distribute, and/or sell copies of the Software, and to permit persons
147c478bd9Sstevel@tonic-gate  * to whom the Software is furnished to do so, provided that the above
157c478bd9Sstevel@tonic-gate  * copyright notice(s) and this permission notice appear in all copies of
167c478bd9Sstevel@tonic-gate  * the Software and that both the above copyright notice(s) and this
177c478bd9Sstevel@tonic-gate  * permission notice appear in supporting documentation.
18*1da57d55SToomas Soome  *
197c478bd9Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
207c478bd9Sstevel@tonic-gate  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
217c478bd9Sstevel@tonic-gate  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
227c478bd9Sstevel@tonic-gate  * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
237c478bd9Sstevel@tonic-gate  * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
247c478bd9Sstevel@tonic-gate  * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
257c478bd9Sstevel@tonic-gate  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
267c478bd9Sstevel@tonic-gate  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
277c478bd9Sstevel@tonic-gate  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28*1da57d55SToomas Soome  *
297c478bd9Sstevel@tonic-gate  * Except as contained in this notice, the name of a copyright holder
307c478bd9Sstevel@tonic-gate  * shall not be used in advertising or otherwise to promote the sale, use
317c478bd9Sstevel@tonic-gate  * or other dealings in this Software without prior written authorization
327c478bd9Sstevel@tonic-gate  * of the copyright holder.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <stdio.h>    /* FILE * */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /*-----------------------------------------------------------------------
387c478bd9Sstevel@tonic-gate  * This module is used to record and traverse historical lines of user input.
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate typedef struct GlHistory GlHistory;
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  * Create a new history maintenance object.
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate GlHistory *_new_GlHistory(size_t buflen);
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  * Delete a history maintenance object.
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate GlHistory *_del_GlHistory(GlHistory *glh);
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate int _glh_add_history(GlHistory *glh, const char *line, int force);
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate int _glh_search_prefix(GlHistory *glh, const char *line, int prefix_len);
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate char *_glh_find_backwards(GlHistory *glh, char *line, size_t dim);
587c478bd9Sstevel@tonic-gate char *_glh_find_forwards(GlHistory *glh, char *line, size_t dim);
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate int _glh_cancel_search(GlHistory *glh);
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate char *_glh_oldest_line(GlHistory *glh, char *line, size_t dim);
637c478bd9Sstevel@tonic-gate char *_glh_current_line(GlHistory *glh, char *line, size_t dim);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * Whenever a new line is added to the history buffer, it is given
677c478bd9Sstevel@tonic-gate  * a unique ID, recorded in an object of the following type.
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate typedef unsigned long GlhLineID;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate  * Query the id of a history line offset by a given number of lines from
737c478bd9Sstevel@tonic-gate  * the one that is currently being recalled. If a recall session isn't
747c478bd9Sstevel@tonic-gate  * in progress, or the offset points outside the history list, 0 is
757c478bd9Sstevel@tonic-gate  * returned.
767c478bd9Sstevel@tonic-gate  */
777c478bd9Sstevel@tonic-gate GlhLineID _glh_line_id(GlHistory *glh, int offset);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate  * Recall a line by its history buffer ID. If the line is no longer
817c478bd9Sstevel@tonic-gate  * in the buffer, or the specified id is zero, NULL is returned.
827c478bd9Sstevel@tonic-gate  */
837c478bd9Sstevel@tonic-gate char *_glh_recall_line(GlHistory *glh, GlhLineID id, char *line, size_t dim);
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /*
867c478bd9Sstevel@tonic-gate  * Write the contents of the history buffer to a given file. Note that
877c478bd9Sstevel@tonic-gate  * ~ and $ expansion are not performed on the filename.
887c478bd9Sstevel@tonic-gate  */
897c478bd9Sstevel@tonic-gate int _glh_save_history(GlHistory *glh, const char *filename,
907c478bd9Sstevel@tonic-gate 		      const char *comment, int max_lines);
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /*
937c478bd9Sstevel@tonic-gate  * Restore the contents of the history buffer from a given file.
947c478bd9Sstevel@tonic-gate  * Note that ~ and $ expansion are not performed on the filename.
957c478bd9Sstevel@tonic-gate  */
967c478bd9Sstevel@tonic-gate int _glh_load_history(GlHistory *glh, const char *filename, const char *comment,
977c478bd9Sstevel@tonic-gate 		      char *line, size_t dim);
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /*
1007c478bd9Sstevel@tonic-gate  * Set and query the current history group.
1017c478bd9Sstevel@tonic-gate  */
1027c478bd9Sstevel@tonic-gate int _glh_set_group(GlHistory *glh, unsigned group);
1037c478bd9Sstevel@tonic-gate int _glh_get_group(GlHistory *glh);
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * Display the contents of the history list to the specified stdio
1077c478bd9Sstevel@tonic-gate  * output group.
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate int _glh_show_history(GlHistory *glh, GlWriteFn *write_fn, void *data,
1107c478bd9Sstevel@tonic-gate 		      const char *fmt, int all_groups, int max_lines);
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate /*
1137c478bd9Sstevel@tonic-gate  * Change the size of the history buffer.
1147c478bd9Sstevel@tonic-gate  */
1157c478bd9Sstevel@tonic-gate int _glh_resize_history(GlHistory *glh, size_t bufsize);
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate /*
1187c478bd9Sstevel@tonic-gate  * Set an upper limit to the number of lines that can be recorded in the
1197c478bd9Sstevel@tonic-gate  * history list, or remove a previously specified limit.
1207c478bd9Sstevel@tonic-gate  */
1217c478bd9Sstevel@tonic-gate void _glh_limit_history(GlHistory *glh, int max_lines);
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate  * Discard either all history, or the history associated with the current
1257c478bd9Sstevel@tonic-gate  * history group.
1267c478bd9Sstevel@tonic-gate  */
1277c478bd9Sstevel@tonic-gate void _glh_clear_history(GlHistory *glh, int all_groups);
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate /*
1307c478bd9Sstevel@tonic-gate  * Temporarily enable or disable the history facility.
1317c478bd9Sstevel@tonic-gate  */
1327c478bd9Sstevel@tonic-gate void _glh_toggle_history(GlHistory *glh, int enable);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /*
1357c478bd9Sstevel@tonic-gate  * Lookup a history line by its sequential number of entry in the
1367c478bd9Sstevel@tonic-gate  * history buffer.
1377c478bd9Sstevel@tonic-gate  */
1387c478bd9Sstevel@tonic-gate int _glh_lookup_history(GlHistory *glh, GlhLineID id, const char **line,
1397c478bd9Sstevel@tonic-gate 			unsigned *group, time_t *timestamp);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate /*
1427c478bd9Sstevel@tonic-gate  * Query the state of the history list.
1437c478bd9Sstevel@tonic-gate  */
1447c478bd9Sstevel@tonic-gate void _glh_state_of_history(GlHistory *glh, int *enabled, unsigned *group,
1457c478bd9Sstevel@tonic-gate 			   int *max_lines);
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate  * Get the range of lines in the history buffer.
1497c478bd9Sstevel@tonic-gate  */
1507c478bd9Sstevel@tonic-gate void _glh_range_of_history(GlHistory *glh, unsigned long *oldest,
1517c478bd9Sstevel@tonic-gate 			   unsigned long *newest, int *nlines);
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /*
1547c478bd9Sstevel@tonic-gate  * Return the size of the history buffer and the amount of the
1557c478bd9Sstevel@tonic-gate  * buffer that is currently in use.
1567c478bd9Sstevel@tonic-gate  */
1577c478bd9Sstevel@tonic-gate void _glh_size_of_history(GlHistory *glh, size_t *buff_size, size_t *buff_used);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate /*
1607c478bd9Sstevel@tonic-gate  * Get information about the last error in this module.
1617c478bd9Sstevel@tonic-gate  */
1627c478bd9Sstevel@tonic-gate const char *_glh_last_error(GlHistory *glh);
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate /*
1657c478bd9Sstevel@tonic-gate  * Return non-zero if a history search session is currently in progress.
1667c478bd9Sstevel@tonic-gate  */
1677c478bd9Sstevel@tonic-gate int _glh_search_active(GlHistory *glh);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate #endif
170