xref: /dflybsd-src/contrib/libedit/src/histedit.h (revision 5f31815c11a07f5ccf614455a765a1ff2a6fa0c3)
1cdf8408cSAntonio Huete Jimenez /*	$NetBSD: histedit.h,v 1.61 2022/02/08 21:13:22 rillig Exp $	*/
232fe07f8SJohn Marino 
332fe07f8SJohn Marino /*-
432fe07f8SJohn Marino  * Copyright (c) 1992, 1993
532fe07f8SJohn Marino  *	The Regents of the University of California.  All rights reserved.
632fe07f8SJohn Marino  *
732fe07f8SJohn Marino  * This code is derived from software contributed to Berkeley by
832fe07f8SJohn Marino  * Christos Zoulas of Cornell University.
932fe07f8SJohn Marino  *
1032fe07f8SJohn Marino  * Redistribution and use in source and binary forms, with or without
1132fe07f8SJohn Marino  * modification, are permitted provided that the following conditions
1232fe07f8SJohn Marino  * are met:
1332fe07f8SJohn Marino  * 1. Redistributions of source code must retain the above copyright
1432fe07f8SJohn Marino  *    notice, this list of conditions and the following disclaimer.
1532fe07f8SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
1632fe07f8SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
1732fe07f8SJohn Marino  *    documentation and/or other materials provided with the distribution.
1832fe07f8SJohn Marino  * 3. Neither the name of the University nor the names of its contributors
1932fe07f8SJohn Marino  *    may be used to endorse or promote products derived from this software
2032fe07f8SJohn Marino  *    without specific prior written permission.
2132fe07f8SJohn Marino  *
2232fe07f8SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2332fe07f8SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2432fe07f8SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2532fe07f8SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2632fe07f8SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2732fe07f8SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2832fe07f8SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2932fe07f8SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3032fe07f8SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3132fe07f8SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3232fe07f8SJohn Marino  * SUCH DAMAGE.
3332fe07f8SJohn Marino  *
3432fe07f8SJohn Marino  *	@(#)histedit.h	8.2 (Berkeley) 1/3/94
3532fe07f8SJohn Marino  */
3632fe07f8SJohn Marino 
3732fe07f8SJohn Marino /*
3832fe07f8SJohn Marino  * histedit.h: Line editor and history interface.
3932fe07f8SJohn Marino  */
4032fe07f8SJohn Marino #ifndef _HISTEDIT_H_
4132fe07f8SJohn Marino #define	_HISTEDIT_H_
4232fe07f8SJohn Marino 
4332fe07f8SJohn Marino #define	LIBEDIT_MAJOR 2
4432fe07f8SJohn Marino #define	LIBEDIT_MINOR 11
4532fe07f8SJohn Marino 
4632fe07f8SJohn Marino #include <sys/types.h>
4732fe07f8SJohn Marino #include <stdio.h>
4832fe07f8SJohn Marino 
4932fe07f8SJohn Marino #ifdef __cplusplus
5032fe07f8SJohn Marino extern "C" {
5132fe07f8SJohn Marino #endif
5232fe07f8SJohn Marino 
5332fe07f8SJohn Marino /*
5432fe07f8SJohn Marino  * ==== Editing ====
5532fe07f8SJohn Marino  */
5632fe07f8SJohn Marino 
5732fe07f8SJohn Marino typedef struct editline EditLine;
5832fe07f8SJohn Marino 
5932fe07f8SJohn Marino /*
6032fe07f8SJohn Marino  * For user-defined function interface
6132fe07f8SJohn Marino  */
6232fe07f8SJohn Marino typedef struct lineinfo {
6332fe07f8SJohn Marino 	const char	*buffer;
6432fe07f8SJohn Marino 	const char	*cursor;
6532fe07f8SJohn Marino 	const char	*lastchar;
6632fe07f8SJohn Marino } LineInfo;
6732fe07f8SJohn Marino 
6832fe07f8SJohn Marino /*
6932fe07f8SJohn Marino  * EditLine editor function return codes.
7032fe07f8SJohn Marino  * For user-defined function interface
7132fe07f8SJohn Marino  */
7232fe07f8SJohn Marino #define	CC_NORM		0
7332fe07f8SJohn Marino #define	CC_NEWLINE	1
7432fe07f8SJohn Marino #define	CC_EOF		2
7532fe07f8SJohn Marino #define	CC_ARGHACK	3
7632fe07f8SJohn Marino #define	CC_REFRESH	4
7732fe07f8SJohn Marino #define	CC_CURSOR	5
7832fe07f8SJohn Marino #define	CC_ERROR	6
7932fe07f8SJohn Marino #define	CC_FATAL	7
8032fe07f8SJohn Marino #define	CC_REDISPLAY	8
8132fe07f8SJohn Marino #define	CC_REFRESH_BEEP	9
8232fe07f8SJohn Marino 
8332fe07f8SJohn Marino /*
8432fe07f8SJohn Marino  * Initialization, cleanup, and resetting
8532fe07f8SJohn Marino  */
8632fe07f8SJohn Marino EditLine	*el_init(const char *, FILE *, FILE *, FILE *);
8784b940c1SJohn Marino EditLine	*el_init_fd(const char *, FILE *, FILE *, FILE *,
8884b940c1SJohn Marino     int, int, int);
8932fe07f8SJohn Marino void		 el_end(EditLine *);
9032fe07f8SJohn Marino void		 el_reset(EditLine *);
9132fe07f8SJohn Marino 
9232fe07f8SJohn Marino /*
9332fe07f8SJohn Marino  * Get a line, a character or push a string back in the input queue
9432fe07f8SJohn Marino  */
9532fe07f8SJohn Marino const char	*el_gets(EditLine *, int *);
9632fe07f8SJohn Marino int		 el_getc(EditLine *, char *);
9732fe07f8SJohn Marino void		 el_push(EditLine *, const char *);
9832fe07f8SJohn Marino 
9932fe07f8SJohn Marino /*
10032fe07f8SJohn Marino  * Beep!
10132fe07f8SJohn Marino  */
10232fe07f8SJohn Marino void		 el_beep(EditLine *);
10332fe07f8SJohn Marino 
10432fe07f8SJohn Marino /*
10532fe07f8SJohn Marino  * High level function internals control
10632fe07f8SJohn Marino  * Parses argc, argv array and executes builtin editline commands
10732fe07f8SJohn Marino  */
10832fe07f8SJohn Marino int		 el_parse(EditLine *, int, const char **);
10932fe07f8SJohn Marino 
11032fe07f8SJohn Marino /*
11132fe07f8SJohn Marino  * Low level editline access functions
11232fe07f8SJohn Marino  */
11332fe07f8SJohn Marino int		 el_set(EditLine *, int, ...);
11432fe07f8SJohn Marino int		 el_get(EditLine *, int, ...);
11532fe07f8SJohn Marino unsigned char	_el_fn_complete(EditLine *, int);
11632fe07f8SJohn Marino 
11732fe07f8SJohn Marino /*
11832fe07f8SJohn Marino  * el_set/el_get parameters
11932fe07f8SJohn Marino  *
12032fe07f8SJohn Marino  * When using el_wset/el_wget (as opposed to el_set/el_get):
12132fe07f8SJohn Marino  *   Char is wchar_t, otherwise it is char.
12232fe07f8SJohn Marino  *   prompt_func is el_wpfunc_t, otherwise it is el_pfunc_t .
12332fe07f8SJohn Marino 
12432fe07f8SJohn Marino  * Prompt function prototypes are:
12532fe07f8SJohn Marino  *   typedef char    *(*el_pfunct_t)  (EditLine *);
12632fe07f8SJohn Marino  *   typedef wchar_t *(*el_wpfunct_t) (EditLine *);
12732fe07f8SJohn Marino  *
12832fe07f8SJohn Marino  * For operations that support set or set/get, the argument types listed are for
12932fe07f8SJohn Marino  * the "set" operation. For "get", each listed type must be a pointer.
13032fe07f8SJohn Marino  * E.g. EL_EDITMODE takes an int when set, but an int* when get.
13132fe07f8SJohn Marino  *
13232fe07f8SJohn Marino  * Operations that only support "get" have the correct argument types listed.
13332fe07f8SJohn Marino  */
13432fe07f8SJohn Marino #define	EL_PROMPT	0	/* , prompt_func);		      set/get */
13532fe07f8SJohn Marino #define	EL_TERMINAL	1	/* , const char *);		      set/get */
13632fe07f8SJohn Marino #define	EL_EDITOR	2	/* , const Char *);		      set/get */
13732fe07f8SJohn Marino #define	EL_SIGNAL	3	/* , int);			      set/get */
13832fe07f8SJohn Marino #define	EL_BIND		4	/* , const Char *, ..., NULL);	      set     */
13932fe07f8SJohn Marino #define	EL_TELLTC	5	/* , const Char *, ..., NULL);	      set     */
14032fe07f8SJohn Marino #define	EL_SETTC	6	/* , const Char *, ..., NULL);	      set     */
14132fe07f8SJohn Marino #define	EL_ECHOTC	7	/* , const Char *, ..., NULL);        set     */
14232fe07f8SJohn Marino #define	EL_SETTY	8	/* , const Char *, ..., NULL);        set     */
14332fe07f8SJohn Marino #define	EL_ADDFN	9	/* , const Char *, const Char,        set     */
14432fe07f8SJohn Marino 				/*   el_func_t);			      */
14532fe07f8SJohn Marino #define	EL_HIST		10	/* , hist_fun_t, const void *);	      set     */
14632fe07f8SJohn Marino #define	EL_EDITMODE	11	/* , int);			      set/get */
14732fe07f8SJohn Marino #define	EL_RPROMPT	12	/* , prompt_func);		      set/get */
14832fe07f8SJohn Marino #define	EL_GETCFN	13	/* , el_rfunc_t);		      set/get */
14932fe07f8SJohn Marino #define	EL_CLIENTDATA	14	/* , void *);			      set/get */
15032fe07f8SJohn Marino #define	EL_UNBUFFERED	15	/* , int);			      set/get */
15132fe07f8SJohn Marino #define	EL_PREP_TERM	16	/* , int);			      set     */
15232fe07f8SJohn Marino #define	EL_GETTC	17	/* , const Char *, ..., NULL);		  get */
15332fe07f8SJohn Marino #define	EL_GETFP	18	/* , int, FILE **);		          get */
15432fe07f8SJohn Marino #define	EL_SETFP	19	/* , int, FILE *);		      set     */
15532fe07f8SJohn Marino #define	EL_REFRESH	20	/* , void);			      set     */
15632fe07f8SJohn Marino #define	EL_PROMPT_ESC	21	/* , prompt_func, Char);	      set/get */
15732fe07f8SJohn Marino #define	EL_RPROMPT_ESC	22	/* , prompt_func, Char);	      set/get */
15832fe07f8SJohn Marino #define	EL_RESIZE	23	/* , el_zfunc_t, void *);	      set     */
15984b940c1SJohn Marino #define	EL_ALIAS_TEXT	24	/* , el_afunc_t, void *);	      set     */
160cdf8408cSAntonio Huete Jimenez #define	EL_SAFEREAD	25	/* , int);			      set/get */
16132fe07f8SJohn Marino 
16232fe07f8SJohn Marino #define	EL_BUILTIN_GETCFN	(NULL)
16332fe07f8SJohn Marino 
16432fe07f8SJohn Marino /*
16532fe07f8SJohn Marino  * Source named file or $PWD/.editrc or $HOME/.editrc
16632fe07f8SJohn Marino  */
16732fe07f8SJohn Marino int		el_source(EditLine *, const char *);
16832fe07f8SJohn Marino 
16932fe07f8SJohn Marino /*
17032fe07f8SJohn Marino  * Must be called when the terminal changes size; If EL_SIGNAL
17132fe07f8SJohn Marino  * is set this is done automatically otherwise it is the responsibility
17232fe07f8SJohn Marino  * of the application
17332fe07f8SJohn Marino  */
17432fe07f8SJohn Marino void		 el_resize(EditLine *);
17532fe07f8SJohn Marino 
17632fe07f8SJohn Marino /*
17732fe07f8SJohn Marino  * User-defined function interface.
17832fe07f8SJohn Marino  */
17932fe07f8SJohn Marino const LineInfo	*el_line(EditLine *);
18032fe07f8SJohn Marino int		 el_insertstr(EditLine *, const char *);
18132fe07f8SJohn Marino void		 el_deletestr(EditLine *, int);
182cdf8408cSAntonio Huete Jimenez int		 el_replacestr(EditLine *, const char *);
183cdf8408cSAntonio Huete Jimenez int		 el_deletestr1(EditLine *, int, int);
18432fe07f8SJohn Marino 
18532fe07f8SJohn Marino /*
18632fe07f8SJohn Marino  * ==== History ====
18732fe07f8SJohn Marino  */
18832fe07f8SJohn Marino 
18932fe07f8SJohn Marino typedef struct history History;
19032fe07f8SJohn Marino 
19132fe07f8SJohn Marino typedef struct HistEvent {
19232fe07f8SJohn Marino 	int		 num;
19332fe07f8SJohn Marino 	const char	*str;
19432fe07f8SJohn Marino } HistEvent;
19532fe07f8SJohn Marino 
19632fe07f8SJohn Marino /*
19732fe07f8SJohn Marino  * History access functions.
19832fe07f8SJohn Marino  */
19932fe07f8SJohn Marino History *	history_init(void);
20032fe07f8SJohn Marino void		history_end(History *);
20132fe07f8SJohn Marino 
20232fe07f8SJohn Marino int		history(History *, HistEvent *, int, ...);
20332fe07f8SJohn Marino 
20432fe07f8SJohn Marino #define	H_FUNC		 0	/* , UTSL		*/
20532fe07f8SJohn Marino #define	H_SETSIZE	 1	/* , const int);	*/
20632fe07f8SJohn Marino #define	H_GETSIZE	 2	/* , void);		*/
20732fe07f8SJohn Marino #define	H_FIRST		 3	/* , void);		*/
20832fe07f8SJohn Marino #define	H_LAST		 4	/* , void);		*/
20932fe07f8SJohn Marino #define	H_PREV		 5	/* , void);		*/
21032fe07f8SJohn Marino #define	H_NEXT		 6	/* , void);		*/
21132fe07f8SJohn Marino #define	H_CURR		 8	/* , const int);	*/
21232fe07f8SJohn Marino #define	H_SET		 7	/* , int);		*/
21332fe07f8SJohn Marino #define	H_ADD		 9	/* , const wchar_t *);	*/
21432fe07f8SJohn Marino #define	H_ENTER		10	/* , const wchar_t *);	*/
21532fe07f8SJohn Marino #define	H_APPEND	11	/* , const wchar_t *);	*/
21632fe07f8SJohn Marino #define	H_END		12	/* , void);		*/
21732fe07f8SJohn Marino #define	H_NEXT_STR	13	/* , const wchar_t *);	*/
21832fe07f8SJohn Marino #define	H_PREV_STR	14	/* , const wchar_t *);	*/
21932fe07f8SJohn Marino #define	H_NEXT_EVENT	15	/* , const int);	*/
22032fe07f8SJohn Marino #define	H_PREV_EVENT	16	/* , const int);	*/
22132fe07f8SJohn Marino #define	H_LOAD		17	/* , const char *);	*/
22232fe07f8SJohn Marino #define	H_SAVE		18	/* , const char *);	*/
22332fe07f8SJohn Marino #define	H_CLEAR		19	/* , void);		*/
22432fe07f8SJohn Marino #define	H_SETUNIQUE	20	/* , int);		*/
22532fe07f8SJohn Marino #define	H_GETUNIQUE	21	/* , void);		*/
22632fe07f8SJohn Marino #define	H_DEL		22	/* , int);		*/
22732fe07f8SJohn Marino #define	H_NEXT_EVDATA	23	/* , const int, histdata_t *);	*/
22832fe07f8SJohn Marino #define	H_DELDATA	24	/* , int, histdata_t *);*/
22932fe07f8SJohn Marino #define	H_REPLACE	25	/* , const char *, histdata_t);	*/
23084b940c1SJohn Marino #define	H_SAVE_FP	26	/* , FILE *);		*/
231ae19eda8Szrj #define	H_NSAVE_FP	27	/* , size_t, FILE *);	*/
23232fe07f8SJohn Marino 
23332fe07f8SJohn Marino 
23432fe07f8SJohn Marino 
23532fe07f8SJohn Marino /*
23632fe07f8SJohn Marino  * ==== Tokenization ====
23732fe07f8SJohn Marino  */
23832fe07f8SJohn Marino 
23932fe07f8SJohn Marino typedef struct tokenizer Tokenizer;
24032fe07f8SJohn Marino 
24132fe07f8SJohn Marino /*
24232fe07f8SJohn Marino  * String tokenization functions, using simplified sh(1) quoting rules
24332fe07f8SJohn Marino  */
24432fe07f8SJohn Marino Tokenizer	*tok_init(const char *);
24532fe07f8SJohn Marino void		 tok_end(Tokenizer *);
24632fe07f8SJohn Marino void		 tok_reset(Tokenizer *);
24732fe07f8SJohn Marino int		 tok_line(Tokenizer *, const LineInfo *,
24832fe07f8SJohn Marino 		    int *, const char ***, int *, int *);
24932fe07f8SJohn Marino int		 tok_str(Tokenizer *, const char *,
25032fe07f8SJohn Marino 		    int *, const char ***);
25132fe07f8SJohn Marino 
25232fe07f8SJohn Marino /*
25332fe07f8SJohn Marino  * Begin Wide Character Support
25432fe07f8SJohn Marino  */
25532fe07f8SJohn Marino #include <wchar.h>
25632fe07f8SJohn Marino #include <wctype.h>
25732fe07f8SJohn Marino 
258*5f31815cSAntonio Huete Jimenez /* This is going to fail to compile in any OS that has wcsdup defined and
259*5f31815cSAntonio Huete Jimenez  * includes histedit.h wihtout including libedit's config.h
260*5f31815cSAntonio Huete Jimenez  */
261*5f31815cSAntonio Huete Jimenez #if !defined(HAVE_WCSDUP) && !defined(__DragonFly__)
262cdf8408cSAntonio Huete Jimenez wchar_t * wcsdup(const wchar_t *str);
263cdf8408cSAntonio Huete Jimenez #endif
264cdf8408cSAntonio Huete Jimenez 
265*5f31815cSAntonio Huete Jimenez 
26632fe07f8SJohn Marino /*
26732fe07f8SJohn Marino  * ==== Editing ====
26832fe07f8SJohn Marino  */
26932fe07f8SJohn Marino typedef struct lineinfow {
27032fe07f8SJohn Marino 	const wchar_t	*buffer;
27132fe07f8SJohn Marino 	const wchar_t	*cursor;
27232fe07f8SJohn Marino 	const wchar_t	*lastchar;
27332fe07f8SJohn Marino } LineInfoW;
27432fe07f8SJohn Marino 
27512db70c8Szrj typedef int	(*el_rfunc_t)(EditLine *, wchar_t *);
27612db70c8Szrj 
27732fe07f8SJohn Marino const wchar_t	*el_wgets(EditLine *, int *);
27832fe07f8SJohn Marino int		 el_wgetc(EditLine *, wchar_t *);
27932fe07f8SJohn Marino void		 el_wpush(EditLine *, const wchar_t *);
28032fe07f8SJohn Marino 
28132fe07f8SJohn Marino int		 el_wparse(EditLine *, int, const wchar_t **);
28232fe07f8SJohn Marino 
28332fe07f8SJohn Marino int		 el_wset(EditLine *, int, ...);
28432fe07f8SJohn Marino int		 el_wget(EditLine *, int, ...);
28532fe07f8SJohn Marino 
28684b940c1SJohn Marino int		 el_cursor(EditLine *, int);
28732fe07f8SJohn Marino const LineInfoW	*el_wline(EditLine *);
28832fe07f8SJohn Marino int		 el_winsertstr(EditLine *, const wchar_t *);
28932fe07f8SJohn Marino #define          el_wdeletestr  el_deletestr
290cdf8408cSAntonio Huete Jimenez int		 el_wreplacestr(EditLine *, const wchar_t *);
29132fe07f8SJohn Marino 
29232fe07f8SJohn Marino /*
29332fe07f8SJohn Marino  * ==== History ====
29432fe07f8SJohn Marino  */
29532fe07f8SJohn Marino typedef struct histeventW {
29632fe07f8SJohn Marino 	int		 num;
29732fe07f8SJohn Marino 	const wchar_t	*str;
29832fe07f8SJohn Marino } HistEventW;
29932fe07f8SJohn Marino 
30032fe07f8SJohn Marino typedef struct historyW HistoryW;
30132fe07f8SJohn Marino 
30232fe07f8SJohn Marino HistoryW *	history_winit(void);
30332fe07f8SJohn Marino void		history_wend(HistoryW *);
30432fe07f8SJohn Marino 
30532fe07f8SJohn Marino int		history_w(HistoryW *, HistEventW *, int, ...);
30632fe07f8SJohn Marino 
30732fe07f8SJohn Marino /*
30832fe07f8SJohn Marino  * ==== Tokenization ====
30932fe07f8SJohn Marino  */
31032fe07f8SJohn Marino typedef struct tokenizerW TokenizerW;
31132fe07f8SJohn Marino 
31232fe07f8SJohn Marino /* Wide character tokenizer support */
31332fe07f8SJohn Marino TokenizerW	*tok_winit(const wchar_t *);
31432fe07f8SJohn Marino void		 tok_wend(TokenizerW *);
31532fe07f8SJohn Marino void		 tok_wreset(TokenizerW *);
31632fe07f8SJohn Marino int		 tok_wline(TokenizerW *, const LineInfoW *,
31732fe07f8SJohn Marino 		    int *, const wchar_t ***, int *, int *);
31832fe07f8SJohn Marino int		 tok_wstr(TokenizerW *, const wchar_t *,
31932fe07f8SJohn Marino 		    int *, const wchar_t ***);
32032fe07f8SJohn Marino 
32132fe07f8SJohn Marino #ifdef __cplusplus
32232fe07f8SJohn Marino }
32332fe07f8SJohn Marino #endif
32432fe07f8SJohn Marino 
32532fe07f8SJohn Marino #endif /* _HISTEDIT_H_ */
326