histedit.h revision 276881
1189251Ssam/*	$NetBSD: histedit.h,v 1.53 2014/06/18 18:12:28 christos Exp $	*/
2189251Ssam
3189251Ssam/*-
4189251Ssam * Copyright (c) 1992, 1993
5252726Srpaulo *	The Regents of the University of California.  All rights reserved.
6252726Srpaulo *
7189251Ssam * This code is derived from software contributed to Berkeley by
8189251Ssam * Christos Zoulas of Cornell University.
9189251Ssam *
10189251Ssam * Redistribution and use in source and binary forms, with or without
11189251Ssam * modification, are permitted provided that the following conditions
12189251Ssam * are met:
13189251Ssam * 1. Redistributions of source code must retain the above copyright
14189251Ssam *    notice, this list of conditions and the following disclaimer.
15189251Ssam * 2. Redistributions in binary form must reproduce the above copyright
16189251Ssam *    notice, this list of conditions and the following disclaimer in the
17214734Srpaulo *    documentation and/or other materials provided with the distribution.
18189251Ssam * 3. Neither the name of the University nor the names of its contributors
19189251Ssam *    may be used to endorse or promote products derived from this software
20189251Ssam *    without specific prior written permission.
21189251Ssam *
22189251Ssam * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23189251Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24189251Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25189251Ssam * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26189251Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27189251Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28189251Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29189251Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30189251Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31189251Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32189251Ssam * SUCH DAMAGE.
33189251Ssam *
34189251Ssam *	@(#)histedit.h	8.2 (Berkeley) 1/3/94
35189251Ssam * $FreeBSD: head/lib/libedit/histedit.h 276881 2015-01-09 07:40:56Z bapt $
36189251Ssam */
37189251Ssam
38189251Ssam/*
39189251Ssam * histedit.h: Line editor and history interface.
40189251Ssam */
41189251Ssam#ifndef _HISTEDIT_H_
42189251Ssam#define	_HISTEDIT_H_
43189251Ssam
44189251Ssam#define	LIBEDIT_MAJOR 2
45189251Ssam#define	LIBEDIT_MINOR 11
46189251Ssam
47189251Ssam#include <sys/types.h>
48189251Ssam#include <stdio.h>
49189251Ssam
50189251Ssam#ifdef __cplusplus
51189251Ssamextern "C" {
52189251Ssam#endif
53189251Ssam
54189251Ssam/*
55189251Ssam * ==== Editing ====
56189251Ssam */
57189251Ssam
58189251Ssamtypedef struct editline EditLine;
59189251Ssam
60189251Ssam/*
61189251Ssam * For user-defined function interface
62189251Ssam */
63189251Ssamtypedef struct lineinfo {
64189251Ssam	const char	*buffer;
65189251Ssam	const char	*cursor;
66189251Ssam	const char	*lastchar;
67189251Ssam} LineInfo;
68189251Ssam
69189251Ssam/*
70189251Ssam * EditLine editor function return codes.
71189251Ssam * For user-defined function interface
72189251Ssam */
73189251Ssam#define	CC_NORM		0
74189251Ssam#define	CC_NEWLINE	1
75189251Ssam#define	CC_EOF		2
76189251Ssam#define	CC_ARGHACK	3
77189251Ssam#define	CC_REFRESH	4
78189251Ssam#define	CC_CURSOR	5
79189251Ssam#define	CC_ERROR	6
80189251Ssam#define	CC_FATAL	7
81189251Ssam#define	CC_REDISPLAY	8
82189251Ssam#define	CC_REFRESH_BEEP	9
83189251Ssam
84189251Ssam/*
85189251Ssam * Initialization, cleanup, and resetting
86189251Ssam */
87189251SsamEditLine	*el_init(const char *, FILE *, FILE *, FILE *);
88189251SsamEditLine	*el_init_fd(const char *, FILE *, FILE *, FILE *,
89189251Ssam    int, int, int);
90189251Ssamvoid		 el_end(EditLine *);
91189251Ssamvoid		 el_reset(EditLine *);
92189251Ssam
93189251Ssam/*
94189251Ssam * Get a line, a character or push a string back in the input queue
95189251Ssam */
96189251Ssamconst char	*el_gets(EditLine *, int *);
97189251Ssamint		 el_getc(EditLine *, char *);
98189251Ssamvoid		 el_push(EditLine *, const char *);
99189251Ssam
100189251Ssam/*
101189251Ssam * Beep!
102189251Ssam */
103189251Ssamvoid		 el_beep(EditLine *);
104189251Ssam
105189251Ssam/*
106189251Ssam * High level function internals control
107189251Ssam * Parses argc, argv array and executes builtin editline commands
108189251Ssam */
109189251Ssamint		 el_parse(EditLine *, int, const char **);
110189251Ssam
111189251Ssam/*
112189251Ssam * Low level editline access functions
113189251Ssam */
114189251Ssamint		 el_set(EditLine *, int, ...);
115189251Ssamint		 el_get(EditLine *, int, ...);
116189251Ssamunsigned char	_el_fn_complete(EditLine *, int);
117189251Ssamunsigned char	_el_fn_sh_complete(EditLine *, int);
118189251Ssam
119189251Ssam/*
120189251Ssam * el_set/el_get parameters
121189251Ssam *
122189251Ssam * When using el_wset/el_wget (as opposed to el_set/el_get):
123189251Ssam *   Char is wchar_t, otherwise it is char.
124189251Ssam *   prompt_func is el_wpfunc_t, otherwise it is el_pfunc_t .
125189251Ssam
126189251Ssam * Prompt function prototypes are:
127189251Ssam *   typedef char    *(*el_pfunct_t)  (EditLine *);
128189251Ssam *   typedef wchar_t *(*el_wpfunct_t) (EditLine *);
129189251Ssam *
130189251Ssam * For operations that support set or set/get, the argument types listed are for
131189251Ssam * the "set" operation. For "get", each listed type must be a pointer.
132189251Ssam * E.g. EL_EDITMODE takes an int when set, but an int* when get.
133189251Ssam *
134189251Ssam * Operations that only support "get" have the correct argument types listed.
135189251Ssam */
136189251Ssam#define	EL_PROMPT	0	/* , prompt_func);		      set/get */
137189251Ssam#define	EL_TERMINAL	1	/* , const char *);		      set/get */
138189251Ssam#define	EL_EDITOR	2	/* , const Char *);		      set/get */
139189251Ssam#define	EL_SIGNAL	3	/* , int);			      set/get */
140189251Ssam#define	EL_BIND		4	/* , const Char *, ..., NULL);	      set     */
141189251Ssam#define	EL_TELLTC	5	/* , const Char *, ..., NULL);	      set     */
142189251Ssam#define	EL_SETTC	6	/* , const Char *, ..., NULL);	      set     */
143189251Ssam#define	EL_ECHOTC	7	/* , const Char *, ..., NULL);        set     */
144189251Ssam#define	EL_SETTY	8	/* , const Char *, ..., NULL);        set     */
145189251Ssam#define	EL_ADDFN	9	/* , const Char *, const Char,        set     */
146189251Ssam				/*   el_func_t);		 	      */
147189251Ssam#define	EL_HIST		10	/* , hist_fun_t, const void *);	      set     */
148189251Ssam#define	EL_EDITMODE	11	/* , int);			      set/get */
149189251Ssam#define	EL_RPROMPT	12	/* , prompt_func);		      set/get */
150189251Ssam#define	EL_GETCFN	13	/* , el_rfunc_t);		      set/get */
151189251Ssam#define	EL_CLIENTDATA	14	/* , void *);			      set/get */
152189251Ssam#define	EL_UNBUFFERED	15	/* , int);			      set/get */
153189251Ssam#define	EL_PREP_TERM	16	/* , int);			      set     */
154189251Ssam#define	EL_GETTC	17	/* , const Char *, ..., NULL);		  get */
155189251Ssam#define	EL_GETFP	18	/* , int, FILE **);		          get */
156189251Ssam#define	EL_SETFP	19	/* , int, FILE *);		      set     */
157189251Ssam#define	EL_REFRESH	20	/* , void);			      set     */
158189251Ssam#define	EL_PROMPT_ESC	21	/* , prompt_func, Char);	      set/get */
159189251Ssam#define	EL_RPROMPT_ESC	22	/* , prompt_func, Char);	      set/get */
160189251Ssam#define	EL_RESIZE	23	/* , el_zfunc_t, void *);	      set     */
161189251Ssam#define	EL_ALIAS_TEXT	24	/* , el_afunc_t, void *);	      set     */
162189251Ssam
163189251Ssam#define	EL_BUILTIN_GETCFN	(NULL)
164189251Ssam
165189251Ssam/*
166252726Srpaulo * Source named file or $PWD/.editrc or $HOME/.editrc
167252726Srpaulo */
168189251Ssamint		el_source(EditLine *, const char *);
169189251Ssam
170189251Ssam/*
171189251Ssam * Must be called when the terminal changes size; If EL_SIGNAL
172189251Ssam * is set this is done automatically otherwise it is the responsibility
173189251Ssam * of the application
174189251Ssam */
175189251Ssamvoid		 el_resize(EditLine *);
176189251Ssam
177189251Ssam/*
178189251Ssam * User-defined function interface.
179252726Srpaulo */
180252726Srpauloconst LineInfo	*el_line(EditLine *);
181189251Ssamint		 el_insertstr(EditLine *, const char *);
182189251Ssamvoid		 el_deletestr(EditLine *, int);
183189251Ssam
184189251Ssam
185189251Ssam/*
186189251Ssam * ==== History ====
187189251Ssam */
188189251Ssam
189189251Ssamtypedef struct history History;
190189251Ssam
191189251Ssamtypedef struct HistEvent {
192189251Ssam	int		 num;
193189251Ssam	const char	*str;
194189251Ssam} HistEvent;
195189251Ssam
196189251Ssam/*
197189251Ssam * History access functions.
198189251Ssam */
199189251SsamHistory *	history_init(void);
200189251Ssamvoid		history_end(History *);
201189251Ssam
202189251Ssamint		history(History *, HistEvent *, int, ...);
203189251Ssam
204189251Ssam#define	H_FUNC		 0	/* , UTSL		*/
205189251Ssam#define	H_SETSIZE	 1	/* , const int);	*/
206189251Ssam#define	H_GETSIZE	 2	/* , void);		*/
207189251Ssam#define	H_FIRST		 3	/* , void);		*/
208189251Ssam#define	H_LAST		 4	/* , void);		*/
209189251Ssam#define	H_PREV		 5	/* , void);		*/
210189251Ssam#define	H_NEXT		 6	/* , void);		*/
211189251Ssam#define	H_CURR		 8	/* , const int);	*/
212189251Ssam#define	H_SET		 7	/* , int);		*/
213189251Ssam#define	H_ADD		 9	/* , const wchar_t *);	*/
214189251Ssam#define	H_ENTER		10	/* , const wchar_t *);	*/
215189251Ssam#define	H_APPEND	11	/* , const wchar_t *);	*/
216189251Ssam#define	H_END		12	/* , void);		*/
217189251Ssam#define	H_NEXT_STR	13	/* , const wchar_t *);	*/
218189251Ssam#define	H_PREV_STR	14	/* , const wchar_t *);	*/
219189251Ssam#define	H_NEXT_EVENT	15	/* , const int);	*/
220189251Ssam#define	H_PREV_EVENT	16	/* , const int);	*/
221189251Ssam#define	H_LOAD		17	/* , const char *);	*/
222189251Ssam#define	H_SAVE		18	/* , const char *);	*/
223189251Ssam#define	H_CLEAR		19	/* , void);		*/
224189251Ssam#define	H_SETUNIQUE	20	/* , int);		*/
225189251Ssam#define	H_GETUNIQUE	21	/* , void);		*/
226189251Ssam#define	H_DEL		22	/* , int);		*/
227189251Ssam#define	H_NEXT_EVDATA	23	/* , const int, histdata_t *);	*/
228189251Ssam#define	H_DELDATA	24	/* , int, histdata_t *);*/
229189251Ssam#define	H_REPLACE	25	/* , const char *, histdata_t);	*/
230189251Ssam#define	H_SAVE_FP	26	/* , FILE *);		*/
231189251Ssam
232189251Ssam
233189251Ssam
234189251Ssam/*
235189251Ssam * ==== Tokenization ====
236189251Ssam */
237189251Ssam
238189251Ssamtypedef struct tokenizer Tokenizer;
239189251Ssam
240189251Ssam/*
241189251Ssam * String tokenization functions, using simplified sh(1) quoting rules
242189251Ssam */
243189251SsamTokenizer	*tok_init(const char *);
244189251Ssamvoid		 tok_end(Tokenizer *);
245189251Ssamvoid		 tok_reset(Tokenizer *);
246189251Ssamint		 tok_line(Tokenizer *, const LineInfo *,
247189251Ssam		    int *, const char ***, int *, int *);
248189251Ssamint		 tok_str(Tokenizer *, const char *,
249189251Ssam		    int *, const char ***);
250189251Ssam
251189251Ssam/*
252189251Ssam * Begin Wide Character Support
253189251Ssam */
254189251Ssam#ifdef __linux__
255189251Ssam/* Apparently we need _GNU_SOURCE defined to get access to wcsdup on Linux */
256189251Ssam#ifndef _GNU_SOURCE
257189251Ssam#define _GNU_SOURCE
258189251Ssam#endif
259189251Ssam#endif
260189251Ssam
261189251Ssam#include <wchar.h>
262189251Ssam#include <wctype.h>
263189251Ssam
264189251Ssam/*
265189251Ssam * Wide character versions
266189251Ssam */
267189251Ssam
268189251Ssam/*
269189251Ssam * ==== Editing ====
270189251Ssam */
271189251Ssamtypedef struct lineinfow {
272189251Ssam	const wchar_t	*buffer;
273252726Srpaulo	const wchar_t	*cursor;
274189251Ssam	const wchar_t	*lastchar;
275189251Ssam} LineInfoW;
276189251Ssam
277189251Ssamconst wchar_t	*el_wgets(EditLine *, int *);
278189251Ssamint		 el_wgetc(EditLine *, wchar_t *);
279189251Ssamvoid		 el_wpush(EditLine *, const wchar_t *);
280189251Ssam
281189251Ssamint		 el_wparse(EditLine *, int, const wchar_t **);
282189251Ssam
283189251Ssamint		 el_wset(EditLine *, int, ...);
284189251Ssamint		 el_wget(EditLine *, int, ...);
285189251Ssam
286189251Ssamint		 el_cursor(EditLine *, int);
287189251Ssamconst LineInfoW	*el_wline(EditLine *);
288189251Ssamint		 el_winsertstr(EditLine *, const wchar_t *);
289189251Ssam#define          el_wdeletestr  el_deletestr
290189251Ssam
291189251Ssam/*
292189251Ssam * ==== History ====
293252726Srpaulo */
294252726Srpaulotypedef struct histeventW {
295252726Srpaulo	int		 num;
296189251Ssam	const wchar_t	*str;
297252726Srpaulo} HistEventW;
298252726Srpaulo
299252726Srpaulotypedef struct historyW HistoryW;
300189251Ssam
301252726SrpauloHistoryW *	history_winit(void);
302252726Srpaulovoid		history_wend(HistoryW *);
303252726Srpaulo
304189251Ssamint		history_w(HistoryW *, HistEventW *, int, ...);
305189251Ssam
306189251Ssam/*
307189251Ssam * ==== Tokenization ====
308252726Srpaulo */
309252726Srpaulotypedef struct tokenizerW TokenizerW;
310252726Srpaulo
311252726Srpaulo/* Wide character tokenizer support */
312189251SsamTokenizerW	*tok_winit(const wchar_t *);
313189251Ssamvoid		 tok_wend(TokenizerW *);
314189251Ssamvoid		 tok_wreset(TokenizerW *);
315189251Ssamint		 tok_wline(TokenizerW *, const LineInfoW *,
316189251Ssam		    int *, const wchar_t ***, int *, int *);
317189251Ssamint		 tok_wstr(TokenizerW *, const wchar_t *,
318189251Ssam		    int *, const wchar_t ***);
319189251Ssam
320189251Ssam#ifdef __cplusplus
321189251Ssam}
322189251Ssam#endif
323189251Ssam
324189251Ssam#endif /* _HISTEDIT_H_ */
325189251Ssam