xref: /minix3/lib/libedit/eln.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: eln.c,v 1.19 2015/05/18 15:07:04 christos Exp $	*/
23e1db26aSLionel Sambuc 
33e1db26aSLionel Sambuc /*-
43e1db26aSLionel Sambuc  * Copyright (c) 2009 The NetBSD Foundation, Inc.
53e1db26aSLionel Sambuc  * All rights reserved.
63e1db26aSLionel Sambuc  *
73e1db26aSLionel Sambuc  * Redistribution and use in source and binary forms, with or without
83e1db26aSLionel Sambuc  * modification, are permitted provided that the following conditions
93e1db26aSLionel Sambuc  * are met:
103e1db26aSLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
113e1db26aSLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
123e1db26aSLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
133e1db26aSLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
143e1db26aSLionel Sambuc  *    documentation and/or other materials provided with the distribution.
153e1db26aSLionel Sambuc  * 3. All advertising materials mentioning features or use of this software
163e1db26aSLionel Sambuc  *    must display the following acknowledgement:
173e1db26aSLionel Sambuc  *        This product includes software developed by the NetBSD
183e1db26aSLionel Sambuc  *        Foundation, Inc. and its contributors.
193e1db26aSLionel Sambuc  * 4. Neither the name of The NetBSD Foundation nor the names of its
203e1db26aSLionel Sambuc  *    contributors may be used to endorse or promote products derived
213e1db26aSLionel Sambuc  *    from this software without specific prior written permission.
223e1db26aSLionel Sambuc  *
233e1db26aSLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
243e1db26aSLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
253e1db26aSLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
263e1db26aSLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
273e1db26aSLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
283e1db26aSLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
293e1db26aSLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
303e1db26aSLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
313e1db26aSLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
323e1db26aSLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
333e1db26aSLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
343e1db26aSLionel Sambuc  */
353e1db26aSLionel Sambuc #include "config.h"
363e1db26aSLionel Sambuc #if !defined(lint) && !defined(SCCSID)
37*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: eln.c,v 1.19 2015/05/18 15:07:04 christos Exp $");
383e1db26aSLionel Sambuc #endif /* not lint && not SCCSID */
393e1db26aSLionel Sambuc 
403e1db26aSLionel Sambuc #include "histedit.h"
413e1db26aSLionel Sambuc #include "el.h"
423e1db26aSLionel Sambuc #include "read.h"
433e1db26aSLionel Sambuc #include <stdarg.h>
443e1db26aSLionel Sambuc #include <stdio.h>
453e1db26aSLionel Sambuc #include <stdlib.h>
463e1db26aSLionel Sambuc 
473e1db26aSLionel Sambuc public int
el_getc(EditLine * el,char * cp)483e1db26aSLionel Sambuc el_getc(EditLine *el, char *cp)
493e1db26aSLionel Sambuc {
503e1db26aSLionel Sambuc 	int num_read;
513e1db26aSLionel Sambuc 	wchar_t wc = 0;
523e1db26aSLionel Sambuc 
533e1db26aSLionel Sambuc 	if (!(el->el_flags & CHARSET_IS_UTF8))
543e1db26aSLionel Sambuc 		el->el_flags |= IGNORE_EXTCHARS;
553e1db26aSLionel Sambuc 	num_read = el_wgetc (el, &wc);
563e1db26aSLionel Sambuc 	if (!(el->el_flags & CHARSET_IS_UTF8))
573e1db26aSLionel Sambuc 		el->el_flags &= ~IGNORE_EXTCHARS;
583e1db26aSLionel Sambuc 
593e1db26aSLionel Sambuc 	if (num_read > 0)
603e1db26aSLionel Sambuc 		*cp = (char)wc;
613e1db26aSLionel Sambuc 	return num_read;
623e1db26aSLionel Sambuc }
633e1db26aSLionel Sambuc 
643e1db26aSLionel Sambuc 
653e1db26aSLionel Sambuc public void
el_push(EditLine * el,const char * str)663e1db26aSLionel Sambuc el_push(EditLine *el, const char *str)
673e1db26aSLionel Sambuc {
683e1db26aSLionel Sambuc 	/* Using multibyte->wide string decoding works fine under single-byte
693e1db26aSLionel Sambuc 	 * character sets too, and Does The Right Thing. */
703e1db26aSLionel Sambuc 	el_wpush(el, ct_decode_string(str, &el->el_lgcyconv));
713e1db26aSLionel Sambuc }
723e1db26aSLionel Sambuc 
733e1db26aSLionel Sambuc 
743e1db26aSLionel Sambuc public const char *
el_gets(EditLine * el,int * nread)753e1db26aSLionel Sambuc el_gets(EditLine *el, int *nread)
763e1db26aSLionel Sambuc {
773e1db26aSLionel Sambuc 	const wchar_t *tmp;
783e1db26aSLionel Sambuc 
79*0a6a1f1dSLionel Sambuc 	if (!(el->el_flags & CHARSET_IS_UTF8))
803e1db26aSLionel Sambuc 		el->el_flags |= IGNORE_EXTCHARS;
813e1db26aSLionel Sambuc 	tmp = el_wgets(el, nread);
82*0a6a1f1dSLionel Sambuc 	if (tmp != NULL) {
83*0a6a1f1dSLionel Sambuc 	    size_t nwread = 0;
84*0a6a1f1dSLionel Sambuc 	    for (int i = 0; i < *nread; i++)
85*0a6a1f1dSLionel Sambuc 		nwread += ct_enc_width(tmp[i]);
86*0a6a1f1dSLionel Sambuc 	    *nread = (int)nwread;
87*0a6a1f1dSLionel Sambuc 	}
88*0a6a1f1dSLionel Sambuc 	if (!(el->el_flags & CHARSET_IS_UTF8))
893e1db26aSLionel Sambuc 		el->el_flags &= ~IGNORE_EXTCHARS;
903e1db26aSLionel Sambuc 	return ct_encode_string(tmp, &el->el_lgcyconv);
913e1db26aSLionel Sambuc }
923e1db26aSLionel Sambuc 
933e1db26aSLionel Sambuc 
943e1db26aSLionel Sambuc public int
el_parse(EditLine * el,int argc,const char * argv[])953e1db26aSLionel Sambuc el_parse(EditLine *el, int argc, const char *argv[])
963e1db26aSLionel Sambuc {
973e1db26aSLionel Sambuc 	int ret;
983e1db26aSLionel Sambuc 	const wchar_t **wargv;
993e1db26aSLionel Sambuc 
1003e1db26aSLionel Sambuc 	wargv = (const wchar_t **)
1013e1db26aSLionel Sambuc 	    ct_decode_argv(argc, argv, &el->el_lgcyconv);
1023e1db26aSLionel Sambuc 	if (!wargv)
1033e1db26aSLionel Sambuc 		return -1;
1043e1db26aSLionel Sambuc 	ret = el_wparse(el, argc, wargv);
1053e1db26aSLionel Sambuc 	ct_free_argv(wargv);
1063e1db26aSLionel Sambuc 
1073e1db26aSLionel Sambuc 	return ret;
1083e1db26aSLionel Sambuc }
1093e1db26aSLionel Sambuc 
1103e1db26aSLionel Sambuc 
1113e1db26aSLionel Sambuc public int
el_set(EditLine * el,int op,...)1123e1db26aSLionel Sambuc el_set(EditLine *el, int op, ...)
1133e1db26aSLionel Sambuc {
1143e1db26aSLionel Sambuc 	va_list ap;
1153e1db26aSLionel Sambuc 	int ret;
1163e1db26aSLionel Sambuc 
1173e1db26aSLionel Sambuc 	if (!el)
1183e1db26aSLionel Sambuc 		return -1;
1193e1db26aSLionel Sambuc 	va_start(ap, op);
1203e1db26aSLionel Sambuc 
1213e1db26aSLionel Sambuc 	switch (op) {
1223e1db26aSLionel Sambuc 	case EL_PROMPT:         /* el_pfunc_t */
1233e1db26aSLionel Sambuc 	case EL_RPROMPT: {
1243e1db26aSLionel Sambuc 		el_pfunc_t p = va_arg(ap, el_pfunc_t);
1253e1db26aSLionel Sambuc 		ret = prompt_set(el, p, 0, op, 0);
1263e1db26aSLionel Sambuc 		break;
1273e1db26aSLionel Sambuc 	}
1283e1db26aSLionel Sambuc 
1293e1db26aSLionel Sambuc 	case EL_RESIZE: {
1303e1db26aSLionel Sambuc 		el_zfunc_t p = va_arg(ap, el_zfunc_t);
1313e1db26aSLionel Sambuc 		void *arg = va_arg(ap, void *);
1323e1db26aSLionel Sambuc 		ret = ch_resizefun(el, p, arg);
1333e1db26aSLionel Sambuc 		break;
1343e1db26aSLionel Sambuc 	}
1353e1db26aSLionel Sambuc 
136*0a6a1f1dSLionel Sambuc 	case EL_ALIAS_TEXT: {
137*0a6a1f1dSLionel Sambuc 		el_afunc_t p = va_arg(ap, el_afunc_t);
138*0a6a1f1dSLionel Sambuc 		void *arg = va_arg(ap, void *);
139*0a6a1f1dSLionel Sambuc 		ret = ch_aliasfun(el, p, arg);
140*0a6a1f1dSLionel Sambuc 		break;
141*0a6a1f1dSLionel Sambuc 	}
142*0a6a1f1dSLionel Sambuc 
143*0a6a1f1dSLionel Sambuc 	case EL_PROMPT_ESC:
144*0a6a1f1dSLionel Sambuc 	case EL_RPROMPT_ESC: {
145*0a6a1f1dSLionel Sambuc 		el_pfunc_t p = va_arg(ap, el_pfunc_t);
146*0a6a1f1dSLionel Sambuc 		int c = va_arg(ap, int);
147*0a6a1f1dSLionel Sambuc 
148*0a6a1f1dSLionel Sambuc 		ret = prompt_set(el, p, c, op, 0);
149*0a6a1f1dSLionel Sambuc 		break;
150*0a6a1f1dSLionel Sambuc 	}
151*0a6a1f1dSLionel Sambuc 
1523e1db26aSLionel Sambuc 	case EL_TERMINAL:       /* const char * */
1533e1db26aSLionel Sambuc 		ret = el_wset(el, op, va_arg(ap, char *));
1543e1db26aSLionel Sambuc 		break;
1553e1db26aSLionel Sambuc 
1563e1db26aSLionel Sambuc 	case EL_EDITOR:		/* const wchar_t * */
1573e1db26aSLionel Sambuc 		ret = el_wset(el, op, ct_decode_string(va_arg(ap, char *),
1583e1db26aSLionel Sambuc 		    &el->el_lgcyconv));
1593e1db26aSLionel Sambuc 		break;
1603e1db26aSLionel Sambuc 
1613e1db26aSLionel Sambuc 	case EL_SIGNAL:         /* int */
1623e1db26aSLionel Sambuc 	case EL_EDITMODE:
1633e1db26aSLionel Sambuc 	case EL_UNBUFFERED:
1643e1db26aSLionel Sambuc 	case EL_PREP_TERM:
1653e1db26aSLionel Sambuc 		ret = el_wset(el, op, va_arg(ap, int));
1663e1db26aSLionel Sambuc 		break;
1673e1db26aSLionel Sambuc 
1683e1db26aSLionel Sambuc 	case EL_BIND:   /* const char * list -> const wchar_t * list */
1693e1db26aSLionel Sambuc 	case EL_TELLTC:
1703e1db26aSLionel Sambuc 	case EL_SETTC:
1713e1db26aSLionel Sambuc 	case EL_ECHOTC:
1723e1db26aSLionel Sambuc 	case EL_SETTY: {
1733e1db26aSLionel Sambuc 		const char *argv[20];
1743e1db26aSLionel Sambuc 		int i;
1753e1db26aSLionel Sambuc 		const wchar_t **wargv;
176*0a6a1f1dSLionel Sambuc 		for (i = 1; i < (int)__arraycount(argv) - 1; ++i)
177*0a6a1f1dSLionel Sambuc 			if ((argv[i] = va_arg(ap, const char *)) == NULL)
1783e1db26aSLionel Sambuc 			    break;
179*0a6a1f1dSLionel Sambuc 		argv[0] = argv[i] = NULL;
1803e1db26aSLionel Sambuc 		wargv = (const wchar_t **)
1813e1db26aSLionel Sambuc 		    ct_decode_argv(i + 1, argv, &el->el_lgcyconv);
1823e1db26aSLionel Sambuc 		if (!wargv) {
1833e1db26aSLionel Sambuc 		    ret = -1;
1843e1db26aSLionel Sambuc 		    goto out;
1853e1db26aSLionel Sambuc 		}
1863e1db26aSLionel Sambuc 		/*
1873e1db26aSLionel Sambuc 		 * AFAIK we can't portably pass through our new wargv to
1883e1db26aSLionel Sambuc 		 * el_wset(), so we have to reimplement the body of
1893e1db26aSLionel Sambuc 		 * el_wset() for these ops.
1903e1db26aSLionel Sambuc 		 */
1913e1db26aSLionel Sambuc 		switch (op) {
1923e1db26aSLionel Sambuc 		case EL_BIND:
1933e1db26aSLionel Sambuc 			wargv[0] = STR("bind");
1943e1db26aSLionel Sambuc 			ret = map_bind(el, i, wargv);
1953e1db26aSLionel Sambuc 			break;
1963e1db26aSLionel Sambuc 		case EL_TELLTC:
1973e1db26aSLionel Sambuc 			wargv[0] = STR("telltc");
1983e1db26aSLionel Sambuc 			ret = terminal_telltc(el, i, wargv);
1993e1db26aSLionel Sambuc 			break;
2003e1db26aSLionel Sambuc 		case EL_SETTC:
2013e1db26aSLionel Sambuc 			wargv[0] = STR("settc");
2023e1db26aSLionel Sambuc 			ret = terminal_settc(el, i, wargv);
2033e1db26aSLionel Sambuc 			break;
2043e1db26aSLionel Sambuc 		case EL_ECHOTC:
2053e1db26aSLionel Sambuc 			wargv[0] = STR("echotc");
2063e1db26aSLionel Sambuc 			ret = terminal_echotc(el, i, wargv);
2073e1db26aSLionel Sambuc 			break;
2083e1db26aSLionel Sambuc 		case EL_SETTY:
2093e1db26aSLionel Sambuc 			wargv[0] = STR("setty");
2103e1db26aSLionel Sambuc 			ret = tty_stty(el, i, wargv);
2113e1db26aSLionel Sambuc 			break;
2123e1db26aSLionel Sambuc 		default:
2133e1db26aSLionel Sambuc 			ret = -1;
2143e1db26aSLionel Sambuc 		}
2153e1db26aSLionel Sambuc 		ct_free_argv(wargv);
2163e1db26aSLionel Sambuc 		break;
2173e1db26aSLionel Sambuc 	}
2183e1db26aSLionel Sambuc 
2193e1db26aSLionel Sambuc 	/* XXX: do we need to change el_func_t too? */
2203e1db26aSLionel Sambuc 	case EL_ADDFN: {          /* const char *, const char *, el_func_t */
2213e1db26aSLionel Sambuc 		const char *args[2];
2223e1db26aSLionel Sambuc 		el_func_t func;
2233e1db26aSLionel Sambuc 		wchar_t **wargv;
2243e1db26aSLionel Sambuc 
2253e1db26aSLionel Sambuc 		args[0] = va_arg(ap, const char *);
2263e1db26aSLionel Sambuc 		args[1] = va_arg(ap, const char *);
2273e1db26aSLionel Sambuc 		func = va_arg(ap, el_func_t);
2283e1db26aSLionel Sambuc 
2293e1db26aSLionel Sambuc 		wargv = ct_decode_argv(2, args, &el->el_lgcyconv);
2303e1db26aSLionel Sambuc 		if (!wargv) {
2313e1db26aSLionel Sambuc 		    ret = -1;
2323e1db26aSLionel Sambuc 		    goto out;
2333e1db26aSLionel Sambuc 		}
2343e1db26aSLionel Sambuc 		// XXX: The two strdup's leak
2353e1db26aSLionel Sambuc 		ret = map_addfunc(el, Strdup(wargv[0]), Strdup(wargv[1]),
2363e1db26aSLionel Sambuc 		    func);
2373e1db26aSLionel Sambuc 		ct_free_argv(wargv);
2383e1db26aSLionel Sambuc 		break;
2393e1db26aSLionel Sambuc 	}
2403e1db26aSLionel Sambuc 	case EL_HIST: {           /* hist_fun_t, const char * */
2413e1db26aSLionel Sambuc 		hist_fun_t fun = va_arg(ap, hist_fun_t);
2423e1db26aSLionel Sambuc 		void *ptr = va_arg(ap, void *);
2433e1db26aSLionel Sambuc 		ret = hist_set(el, fun, ptr);
2443e1db26aSLionel Sambuc 		el->el_flags |= NARROW_HISTORY;
2453e1db26aSLionel Sambuc 		break;
2463e1db26aSLionel Sambuc 	}
247*0a6a1f1dSLionel Sambuc 
2483e1db26aSLionel Sambuc 	/* XXX: do we need to change el_rfunc_t? */
2493e1db26aSLionel Sambuc 	case EL_GETCFN:         /* el_rfunc_t */
2503e1db26aSLionel Sambuc 		ret = el_wset(el, op, va_arg(ap, el_rfunc_t));
2513e1db26aSLionel Sambuc 		el->el_flags |= NARROW_READ;
2523e1db26aSLionel Sambuc 		break;
253*0a6a1f1dSLionel Sambuc 
2543e1db26aSLionel Sambuc 	case EL_CLIENTDATA:     /* void * */
2553e1db26aSLionel Sambuc 		ret = el_wset(el, op, va_arg(ap, void *));
2563e1db26aSLionel Sambuc 		break;
257*0a6a1f1dSLionel Sambuc 
2583e1db26aSLionel Sambuc 	case EL_SETFP: {          /* int, FILE * */
2593e1db26aSLionel Sambuc 		int what = va_arg(ap, int);
2603e1db26aSLionel Sambuc 		FILE *fp = va_arg(ap, FILE *);
2613e1db26aSLionel Sambuc 		ret = el_wset(el, op, what, fp);
2623e1db26aSLionel Sambuc 		break;
2633e1db26aSLionel Sambuc 	}
264*0a6a1f1dSLionel Sambuc 
265*0a6a1f1dSLionel Sambuc 	case EL_REFRESH:
266*0a6a1f1dSLionel Sambuc 		re_clear_display(el);
267*0a6a1f1dSLionel Sambuc 		re_refresh(el);
268*0a6a1f1dSLionel Sambuc 		terminal__flush(el);
269*0a6a1f1dSLionel Sambuc 		ret = 0;
2703e1db26aSLionel Sambuc 		break;
271*0a6a1f1dSLionel Sambuc 
2723e1db26aSLionel Sambuc 	default:
2733e1db26aSLionel Sambuc 		ret = -1;
2743e1db26aSLionel Sambuc 		break;
2753e1db26aSLionel Sambuc 	}
2763e1db26aSLionel Sambuc 
2773e1db26aSLionel Sambuc out:
2783e1db26aSLionel Sambuc 	va_end(ap);
2793e1db26aSLionel Sambuc 	return ret;
2803e1db26aSLionel Sambuc }
2813e1db26aSLionel Sambuc 
2823e1db26aSLionel Sambuc 
2833e1db26aSLionel Sambuc public int
el_get(EditLine * el,int op,...)2843e1db26aSLionel Sambuc el_get(EditLine *el, int op, ...)
2853e1db26aSLionel Sambuc {
2863e1db26aSLionel Sambuc 	va_list ap;
2873e1db26aSLionel Sambuc 	int ret;
2883e1db26aSLionel Sambuc 
2893e1db26aSLionel Sambuc 	if (!el)
2903e1db26aSLionel Sambuc 		return -1;
2913e1db26aSLionel Sambuc 
2923e1db26aSLionel Sambuc 	va_start(ap, op);
2933e1db26aSLionel Sambuc 
2943e1db26aSLionel Sambuc 	switch (op) {
2953e1db26aSLionel Sambuc 	case EL_PROMPT:         /* el_pfunc_t * */
2963e1db26aSLionel Sambuc 	case EL_RPROMPT: {
2973e1db26aSLionel Sambuc 		el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
2983e1db26aSLionel Sambuc 		ret = prompt_get(el, p, 0, op);
2993e1db26aSLionel Sambuc 		break;
3003e1db26aSLionel Sambuc 	}
3013e1db26aSLionel Sambuc 
3023e1db26aSLionel Sambuc 	case EL_PROMPT_ESC: /* el_pfunc_t *, char **/
3033e1db26aSLionel Sambuc 	case EL_RPROMPT_ESC: {
3043e1db26aSLionel Sambuc 		el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
3053e1db26aSLionel Sambuc 		char *c = va_arg(ap, char *);
3063e1db26aSLionel Sambuc 		wchar_t wc = 0;
3073e1db26aSLionel Sambuc 		ret = prompt_get(el, p, &wc, op);
3083e1db26aSLionel Sambuc 		*c = (char)wc;
3093e1db26aSLionel Sambuc 		break;
3103e1db26aSLionel Sambuc 	}
3113e1db26aSLionel Sambuc 
3123e1db26aSLionel Sambuc 	case EL_EDITOR: {
3133e1db26aSLionel Sambuc 		const char **p = va_arg(ap, const char **);
3143e1db26aSLionel Sambuc 		const wchar_t *pw;
3153e1db26aSLionel Sambuc 		ret = el_wget(el, op, &pw);
3163e1db26aSLionel Sambuc 		*p = ct_encode_string(pw, &el->el_lgcyconv);
3173e1db26aSLionel Sambuc 		if (!el->el_lgcyconv.csize)
3183e1db26aSLionel Sambuc 			ret = -1;
3193e1db26aSLionel Sambuc 		break;
3203e1db26aSLionel Sambuc 	}
3213e1db26aSLionel Sambuc 
3223e1db26aSLionel Sambuc 	case EL_TERMINAL:       /* const char ** */
3233e1db26aSLionel Sambuc 		ret = el_wget(el, op, va_arg(ap, const char **));
3243e1db26aSLionel Sambuc 		break;
3253e1db26aSLionel Sambuc 
3263e1db26aSLionel Sambuc 	case EL_SIGNAL:         /* int * */
3273e1db26aSLionel Sambuc 	case EL_EDITMODE:
3283e1db26aSLionel Sambuc 	case EL_UNBUFFERED:
3293e1db26aSLionel Sambuc 	case EL_PREP_TERM:
3303e1db26aSLionel Sambuc 		ret = el_wget(el, op, va_arg(ap, int *));
3313e1db26aSLionel Sambuc 		break;
3323e1db26aSLionel Sambuc 
3333e1db26aSLionel Sambuc 	case EL_GETTC: {
3343e1db26aSLionel Sambuc 		char *argv[20];
3353e1db26aSLionel Sambuc 		static char gettc[] = "gettc";
3363e1db26aSLionel Sambuc 		int i;
3373e1db26aSLionel Sambuc 		for (i = 1; i < (int)__arraycount(argv); ++i)
3383e1db26aSLionel Sambuc 			if ((argv[i] = va_arg(ap, char *)) == NULL)
3393e1db26aSLionel Sambuc 				break;
3403e1db26aSLionel Sambuc 		argv[0] = gettc;
3413e1db26aSLionel Sambuc 		ret = terminal_gettc(el, i, argv);
3423e1db26aSLionel Sambuc 		break;
3433e1db26aSLionel Sambuc 	}
3443e1db26aSLionel Sambuc 
3453e1db26aSLionel Sambuc 	/* XXX: do we need to change el_rfunc_t? */
3463e1db26aSLionel Sambuc 	case EL_GETCFN:         /* el_rfunc_t */
3473e1db26aSLionel Sambuc 		ret = el_wget(el, op, va_arg(ap, el_rfunc_t *));
3483e1db26aSLionel Sambuc 		break;
3493e1db26aSLionel Sambuc 
3503e1db26aSLionel Sambuc 	case EL_CLIENTDATA:     /* void ** */
3513e1db26aSLionel Sambuc 		ret = el_wget(el, op, va_arg(ap, void **));
3523e1db26aSLionel Sambuc 		break;
3533e1db26aSLionel Sambuc 
3543e1db26aSLionel Sambuc 	case EL_GETFP: {          /* int, FILE ** */
3553e1db26aSLionel Sambuc 		int what = va_arg(ap, int);
3563e1db26aSLionel Sambuc 		FILE **fpp = va_arg(ap, FILE **);
3573e1db26aSLionel Sambuc 		ret = el_wget(el, op, what, fpp);
3583e1db26aSLionel Sambuc 		break;
3593e1db26aSLionel Sambuc 	}
3603e1db26aSLionel Sambuc 
3613e1db26aSLionel Sambuc 	default:
3623e1db26aSLionel Sambuc 		ret = -1;
3633e1db26aSLionel Sambuc 		break;
3643e1db26aSLionel Sambuc 	}
3653e1db26aSLionel Sambuc 
3663e1db26aSLionel Sambuc 	va_end(ap);
3673e1db26aSLionel Sambuc 	return ret;
3683e1db26aSLionel Sambuc }
3693e1db26aSLionel Sambuc 
3703e1db26aSLionel Sambuc 
3713e1db26aSLionel Sambuc const LineInfo *
el_line(EditLine * el)3723e1db26aSLionel Sambuc el_line(EditLine *el)
3733e1db26aSLionel Sambuc {
3743e1db26aSLionel Sambuc 	const LineInfoW *winfo = el_wline(el);
3753e1db26aSLionel Sambuc 	LineInfo *info = &el->el_lgcylinfo;
3763e1db26aSLionel Sambuc 	size_t offset;
3773e1db26aSLionel Sambuc 	const Char *p;
3783e1db26aSLionel Sambuc 
3793e1db26aSLionel Sambuc 	info->buffer   = ct_encode_string(winfo->buffer, &el->el_lgcyconv);
3803e1db26aSLionel Sambuc 
3813e1db26aSLionel Sambuc 	offset = 0;
3823e1db26aSLionel Sambuc 	for (p = winfo->buffer; p < winfo->cursor; p++)
3833e1db26aSLionel Sambuc 		offset += ct_enc_width(*p);
3843e1db26aSLionel Sambuc 	info->cursor = info->buffer + offset;
3853e1db26aSLionel Sambuc 
3863e1db26aSLionel Sambuc 	offset = 0;
3873e1db26aSLionel Sambuc 	for (p = winfo->buffer; p < winfo->lastchar; p++)
3883e1db26aSLionel Sambuc 		offset += ct_enc_width(*p);
3893e1db26aSLionel Sambuc 	info->lastchar = info->buffer + offset;
3903e1db26aSLionel Sambuc 
3913e1db26aSLionel Sambuc 	return info;
3923e1db26aSLionel Sambuc }
3933e1db26aSLionel Sambuc 
3943e1db26aSLionel Sambuc 
3953e1db26aSLionel Sambuc int
el_insertstr(EditLine * el,const char * str)3963e1db26aSLionel Sambuc el_insertstr(EditLine *el, const char *str)
3973e1db26aSLionel Sambuc {
3983e1db26aSLionel Sambuc 	return el_winsertstr(el, ct_decode_string(str, &el->el_lgcyconv));
3993e1db26aSLionel Sambuc }
400