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