1 /*	$NetBSD: histedit.h,v 1.6 2020/07/04 13:43:21 lukem Exp $	*/
2 /*	from	NetBSD: histedit.h,v 1.57 2017/09/01 10:19:10 christos Exp	*/
3 
4 /*-
5  * Copyright (c) 1992, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Christos Zoulas of Cornell University.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)histedit.h	8.2 (Berkeley) 1/3/94
36  */
37 
38 /*
39  * histedit.h: Line editor and history interface.
40  */
41 #ifndef _HISTEDIT_H_
42 #define	_HISTEDIT_H_
43 
44 #define	LIBEDIT_MAJOR 2
45 #define	LIBEDIT_MINOR 11
46 
47 #if 0 /* tnftp */
48 #include <sys/types.h>
49 #include <stdio.h>
50 #endif /* tnftp */
51 
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55 
56 /*
57  * ==== Editing ====
58  */
59 
60 typedef struct editline EditLine;
61 
62 /*
63  * For user-defined function interface
64  */
65 typedef struct lineinfo {
66 	const char	*buffer;
67 	const char	*cursor;
68 	const char	*lastchar;
69 } LineInfo;
70 
71 /*
72  * EditLine editor function return codes.
73  * For user-defined function interface
74  */
75 #define	CC_NORM		0
76 #define	CC_NEWLINE	1
77 #define	CC_EOF		2
78 #define	CC_ARGHACK	3
79 #define	CC_REFRESH	4
80 #define	CC_CURSOR	5
81 #define	CC_ERROR	6
82 #define	CC_FATAL	7
83 #define	CC_REDISPLAY	8
84 #define	CC_REFRESH_BEEP	9
85 
86 /*
87  * Initialization, cleanup, and resetting
88  */
89 EditLine	*el_init(const char *, FILE *, FILE *, FILE *);
90 EditLine	*el_init_fd(const char *, FILE *, FILE *, FILE *,
91     int, int, int);
92 void		 el_end(EditLine *);
93 void		 el_reset(EditLine *);
94 
95 /*
96  * Get a line, a character or push a string back in the input queue
97  */
98 const char	*el_gets(EditLine *, int *);
99 int		 el_getc(EditLine *, char *);
100 void		 el_push(EditLine *, const char *);
101 
102 /*
103  * Beep!
104  */
105 void		 el_beep(EditLine *);
106 
107 /*
108  * High level function internals control
109  * Parses argc, argv array and executes builtin editline commands
110  */
111 int		 el_parse(EditLine *, int, const char **);
112 
113 /*
114  * Low level editline access functions
115  */
116 int		 el_set(EditLine *, int, ...);
117 int		 el_get(EditLine *, int, ...);
118 unsigned char	_el_fn_complete(EditLine *, int);
119 
120 /*
121  * el_set/el_get parameters
122  *
123  * When using el_wset/el_wget (as opposed to el_set/el_get):
124  *   Char is wchar_t, otherwise it is char.
125  *   prompt_func is el_wpfunc_t, otherwise it is el_pfunc_t .
126 
127  * Prompt function prototypes are:
128  *   typedef char    *(*el_pfunct_t)  (EditLine *);
129  *   typedef wchar_t *(*el_wpfunct_t) (EditLine *);
130  *
131  * For operations that support set or set/get, the argument types listed are for
132  * the "set" operation. For "get", each listed type must be a pointer.
133  * E.g. EL_EDITMODE takes an int when set, but an int* when get.
134  *
135  * Operations that only support "get" have the correct argument types listed.
136  */
137 #define	EL_PROMPT	0	/* , prompt_func);		      set/get */
138 #define	EL_TERMINAL	1	/* , const char *);		      set/get */
139 #define	EL_EDITOR	2	/* , const Char *);		      set/get */
140 #define	EL_SIGNAL	3	/* , int);			      set/get */
141 #define	EL_BIND		4	/* , const Char *, ..., NULL);	      set     */
142 #define	EL_TELLTC	5	/* , const Char *, ..., NULL);	      set     */
143 #define	EL_SETTC	6	/* , const Char *, ..., NULL);	      set     */
144 #define	EL_ECHOTC	7	/* , const Char *, ..., NULL);        set     */
145 #define	EL_SETTY	8	/* , const Char *, ..., NULL);        set     */
146 #define	EL_ADDFN	9	/* , const Char *, const Char,        set     */
147 				/*   el_func_t);			      */
148 #define	EL_HIST		10	/* , hist_fun_t, const void *);	      set     */
149 #define	EL_EDITMODE	11	/* , int);			      set/get */
150 #define	EL_RPROMPT	12	/* , prompt_func);		      set/get */
151 #define	EL_GETCFN	13	/* , el_rfunc_t);		      set/get */
152 #define	EL_CLIENTDATA	14	/* , void *);			      set/get */
153 #define	EL_UNBUFFERED	15	/* , int);			      set/get */
154 #define	EL_PREP_TERM	16	/* , int);			      set     */
155 #define	EL_GETTC	17	/* , const Char *, ..., NULL);		  get */
156 #define	EL_GETFP	18	/* , int, FILE **);		          get */
157 #define	EL_SETFP	19	/* , int, FILE *);		      set     */
158 #define	EL_REFRESH	20	/* , void);			      set     */
159 #define	EL_PROMPT_ESC	21	/* , prompt_func, Char);	      set/get */
160 #define	EL_RPROMPT_ESC	22	/* , prompt_func, Char);	      set/get */
161 #define	EL_RESIZE	23	/* , el_zfunc_t, void *);	      set     */
162 #define	EL_ALIAS_TEXT	24	/* , el_afunc_t, void *);	      set     */
163 
164 #define	EL_BUILTIN_GETCFN	(NULL)
165 
166 /*
167  * Source named file or $PWD/.editrc or $HOME/.editrc
168  */
169 int		el_source(EditLine *, const char *);
170 
171 /*
172  * Must be called when the terminal changes size; If EL_SIGNAL
173  * is set this is done automatically otherwise it is the responsibility
174  * of the application
175  */
176 void		 el_resize(EditLine *);
177 
178 /*
179  * User-defined function interface.
180  */
181 const LineInfo	*el_line(EditLine *);
182 int		 el_insertstr(EditLine *, const char *);
183 void		 el_deletestr(EditLine *, int);
184 
185 
186 /*
187  * ==== History ====
188  */
189 
190 typedef struct history History;
191 
192 typedef struct HistEvent {
193 	int		 num;
194 	const char	*str;
195 } HistEvent;
196 
197 /*
198  * History access functions.
199  */
200 History *	history_init(void);
201 void		history_end(History *);
202 
203 int		history(History *, HistEvent *, int, ...);
204 
205 #define	H_FUNC		 0	/* , UTSL		*/
206 #define	H_SETSIZE	 1	/* , const int);	*/
207 #define	H_GETSIZE	 2	/* , void);		*/
208 #define	H_FIRST		 3	/* , void);		*/
209 #define	H_LAST		 4	/* , void);		*/
210 #define	H_PREV		 5	/* , void);		*/
211 #define	H_NEXT		 6	/* , void);		*/
212 #define	H_CURR		 8	/* , const int);	*/
213 #define	H_SET		 7	/* , int);		*/
214 #define	H_ADD		 9	/* , const wchar_t *);	*/
215 #define	H_ENTER		10	/* , const wchar_t *);	*/
216 #define	H_APPEND	11	/* , const wchar_t *);	*/
217 #define	H_END		12	/* , void);		*/
218 #define	H_NEXT_STR	13	/* , const wchar_t *);	*/
219 #define	H_PREV_STR	14	/* , const wchar_t *);	*/
220 #define	H_NEXT_EVENT	15	/* , const int);	*/
221 #define	H_PREV_EVENT	16	/* , const int);	*/
222 #define	H_LOAD		17	/* , const char *);	*/
223 #define	H_SAVE		18	/* , const char *);	*/
224 #define	H_CLEAR		19	/* , void);		*/
225 #define	H_SETUNIQUE	20	/* , int);		*/
226 #define	H_GETUNIQUE	21	/* , void);		*/
227 #define	H_DEL		22	/* , int);		*/
228 #define	H_NEXT_EVDATA	23	/* , const int, histdata_t *);	*/
229 #define	H_DELDATA	24	/* , int, histdata_t *);*/
230 #define	H_REPLACE	25	/* , const char *, histdata_t);	*/
231 #define	H_SAVE_FP	26	/* , FILE *);		*/
232 #define	H_NSAVE_FP	27	/* , size_t, FILE *);	*/
233 
234 
235 
236 /*
237  * ==== Tokenization ====
238  */
239 
240 typedef struct tokenizer Tokenizer;
241 
242 /*
243  * String tokenization functions, using simplified sh(1) quoting rules
244  */
245 Tokenizer	*tok_init(const char *);
246 void		 tok_end(Tokenizer *);
247 void		 tok_reset(Tokenizer *);
248 int		 tok_line(Tokenizer *, const LineInfo *,
249 		    int *, const char ***, int *, int *);
250 int		 tok_str(Tokenizer *, const char *,
251 		    int *, const char ***);
252 
253 /*
254  * Begin Wide Character Support
255  */
256 #include <wchar.h>
257 #include <wctype.h>
258 
259 /*
260  * ==== Editing ====
261  */
262 typedef struct lineinfow {
263 	const wchar_t	*buffer;
264 	const wchar_t	*cursor;
265 	const wchar_t	*lastchar;
266 } LineInfoW;
267 
268 typedef int	(*el_rfunc_t)(EditLine *, wchar_t *);
269 
270 const wchar_t	*el_wgets(EditLine *, int *);
271 int		 el_wgetc(EditLine *, wchar_t *);
272 void		 el_wpush(EditLine *, const wchar_t *);
273 
274 int		 el_wparse(EditLine *, int, const wchar_t **);
275 
276 int		 el_wset(EditLine *, int, ...);
277 int		 el_wget(EditLine *, int, ...);
278 
279 int		 el_cursor(EditLine *, int);
280 const LineInfoW	*el_wline(EditLine *);
281 int		 el_winsertstr(EditLine *, const wchar_t *);
282 #define          el_wdeletestr  el_deletestr
283 
284 /*
285  * ==== History ====
286  */
287 typedef struct histeventW {
288 	int		 num;
289 	const wchar_t	*str;
290 } HistEventW;
291 
292 typedef struct historyW HistoryW;
293 
294 HistoryW *	history_winit(void);
295 void		history_wend(HistoryW *);
296 
297 int		history_w(HistoryW *, HistEventW *, int, ...);
298 
299 /*
300  * ==== Tokenization ====
301  */
302 typedef struct tokenizerW TokenizerW;
303 
304 /* Wide character tokenizer support */
305 TokenizerW	*tok_winit(const wchar_t *);
306 void		 tok_wend(TokenizerW *);
307 void		 tok_wreset(TokenizerW *);
308 int		 tok_wline(TokenizerW *, const LineInfoW *,
309 		    int *, const wchar_t ***, int *, int *);
310 int		 tok_wstr(TokenizerW *, const wchar_t *,
311 		    int *, const wchar_t ***);
312 
313 #ifdef __cplusplus
314 }
315 #endif
316 
317 #endif /* _HISTEDIT_H_ */
318