Home | History | Annotate | Line # | Download | only in libedit
eln.c revision 1.1
      1  1.1  christos /*	$NetBSD: eln.c,v 1.1 2009/12/30 22:37:40 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 2009 The NetBSD Foundation, Inc.
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * Redistribution and use in source and binary forms, with or without
      8  1.1  christos  * modification, are permitted provided that the following conditions
      9  1.1  christos  * are met:
     10  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     11  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     12  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  christos  *    documentation and/or other materials provided with the distribution.
     15  1.1  christos  * 3. All advertising materials mentioning features or use of this software
     16  1.1  christos  *    must display the following acknowledgement:
     17  1.1  christos  *        This product includes software developed by the NetBSD
     18  1.1  christos  *        Foundation, Inc. and its contributors.
     19  1.1  christos  * 4. Neither the name of The NetBSD Foundation nor the names of its
     20  1.1  christos  *    contributors may be used to endorse or promote products derived
     21  1.1  christos  *    from this software without specific prior written permission.
     22  1.1  christos  *
     23  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     24  1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     27  1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     34  1.1  christos  */
     35  1.1  christos #include "config.h"
     36  1.1  christos #if !defined(lint) && !defined(SCCSID)
     37  1.1  christos __RCSID("$NetBSD: eln.c,v 1.1 2009/12/30 22:37:40 christos Exp $");
     38  1.1  christos #endif /* not lint && not SCCSID */
     39  1.1  christos 
     40  1.1  christos #include "histedit.h"
     41  1.1  christos #include "el.h"
     42  1.1  christos #include "read.h"
     43  1.1  christos #include <stdarg.h>
     44  1.1  christos #include <stdio.h>
     45  1.1  christos 
     46  1.1  christos public int
     47  1.1  christos el_getc(EditLine *el, char *cp)
     48  1.1  christos {
     49  1.1  christos 	int num_read;
     50  1.1  christos 	wchar_t wc = 0;
     51  1.1  christos 
     52  1.1  christos 	el->el_flags |= IGNORE_EXTCHARS;
     53  1.1  christos 	num_read = el_wgetc (el, &wc);
     54  1.1  christos 	el->el_flags &= ~IGNORE_EXTCHARS;
     55  1.1  christos 
     56  1.1  christos 	if (num_read > 0)
     57  1.1  christos 		*cp = (unsigned char)wc;
     58  1.1  christos 	return num_read;
     59  1.1  christos }
     60  1.1  christos 
     61  1.1  christos 
     62  1.1  christos public void
     63  1.1  christos el_push(EditLine *el, const char *str)
     64  1.1  christos {
     65  1.1  christos 	/* Using multibyte->wide string decoding works fine under single-byte
     66  1.1  christos 	 * character sets too, and Does The Right Thing. */
     67  1.1  christos 	el_wpush(el, ct_decode_string(str, &el->el_lgcyconv));
     68  1.1  christos }
     69  1.1  christos 
     70  1.1  christos 
     71  1.1  christos public const char *
     72  1.1  christos el_gets(EditLine *el, int *nread)
     73  1.1  christos {
     74  1.1  christos 	el->el_flags |= IGNORE_EXTCHARS;
     75  1.1  christos 	el_wgets(el, nread);
     76  1.1  christos 	el->el_flags &= ~IGNORE_EXTCHARS;
     77  1.1  christos 	return ct_encode_string(el->el_line.buffer, &el->el_lgcyconv);
     78  1.1  christos }
     79  1.1  christos 
     80  1.1  christos 
     81  1.1  christos public int
     82  1.1  christos el_parse(EditLine *el, int argc, const char *argv[])
     83  1.1  christos {
     84  1.1  christos 	int ret;
     85  1.1  christos 	const wchar_t **wargv;
     86  1.1  christos 
     87  1.1  christos 	wargv = (const wchar_t **)
     88  1.1  christos 	    ct_decode_argv(argc, argv, &el->el_lgcyconv);
     89  1.1  christos 	if (!wargv)
     90  1.1  christos 		return -1;
     91  1.1  christos 	ret = el_wparse(el, argc, wargv);
     92  1.1  christos 	free(wargv);
     93  1.1  christos 
     94  1.1  christos 	return ret;
     95  1.1  christos }
     96  1.1  christos 
     97  1.1  christos 
     98  1.1  christos public int
     99  1.1  christos el_set(EditLine *el, int op, ...)
    100  1.1  christos {
    101  1.1  christos 	va_list ap;
    102  1.1  christos 	int ret;
    103  1.1  christos 
    104  1.1  christos 	if (!el)
    105  1.1  christos 		return -1;
    106  1.1  christos 	va_start(ap, op);
    107  1.1  christos 
    108  1.1  christos 	switch (op) {
    109  1.1  christos 	case EL_PROMPT:         /* el_pfunc_t */
    110  1.1  christos 	case EL_RPROMPT: {
    111  1.1  christos 		el_pfunc_t p = va_arg(ap, el_pfunc_t);
    112  1.1  christos 		ret = prompt_set(el, p, 0, op, 0);
    113  1.1  christos 		break;
    114  1.1  christos 	}
    115  1.1  christos 
    116  1.1  christos 	case EL_TERMINAL:       /* const char * */
    117  1.1  christos 		ret = el_wset(el, op, va_arg(ap, char *));
    118  1.1  christos 		break;
    119  1.1  christos 
    120  1.1  christos 	case EL_SIGNAL:         /* int */
    121  1.1  christos 	case EL_EDITMODE:
    122  1.1  christos 	case EL_UNBUFFERED:
    123  1.1  christos 	case EL_PREP_TERM:
    124  1.1  christos 		ret = el_wset(el, op, va_arg(ap, int));
    125  1.1  christos 		break;
    126  1.1  christos 
    127  1.1  christos 	case EL_BIND:   /* const char * list -> const wchar_t * list */
    128  1.1  christos 	case EL_TELLTC:
    129  1.1  christos 	case EL_SETTC:
    130  1.1  christos 	case EL_ECHOTC:
    131  1.1  christos 	case EL_SETTY: {
    132  1.1  christos 		const char *argv[20];
    133  1.1  christos 		int i;
    134  1.1  christos 		const wchar_t **wargv;
    135  1.1  christos 		for (i = 1; i < (int)__arraycount(argv); ++i)
    136  1.1  christos 			if ((argv[i] = va_arg(ap, char *)) == NULL)
    137  1.1  christos 			    break;
    138  1.1  christos 		argv[0] = NULL;
    139  1.1  christos 		wargv = (const wchar_t **)
    140  1.1  christos 		    ct_decode_argv(i, argv, &el->el_lgcyconv);
    141  1.1  christos 		if (!wargv) {
    142  1.1  christos 		    ret = -1;
    143  1.1  christos 		    goto out;
    144  1.1  christos 		}
    145  1.1  christos 		/*
    146  1.1  christos 		 * AFAIK we can't portably pass through our new wargv to
    147  1.1  christos 		 * el_wset(), so we have to reimplement the body of
    148  1.1  christos 		 * el_wset() for these ops.
    149  1.1  christos 		 */
    150  1.1  christos 		switch (op) {
    151  1.1  christos 		case EL_BIND:
    152  1.1  christos 			wargv[0] = STR("bind");
    153  1.1  christos 			ret = map_bind(el, i, wargv);
    154  1.1  christos 			break;
    155  1.1  christos 		case EL_TELLTC:
    156  1.1  christos 			wargv[0] = STR("telltc");
    157  1.1  christos 			ret = term_telltc(el, i, wargv);
    158  1.1  christos 			break;
    159  1.1  christos 		case EL_SETTC:
    160  1.1  christos 			wargv[0] = STR("settc");
    161  1.1  christos 			ret = term_settc(el, i, wargv);
    162  1.1  christos 			break;
    163  1.1  christos 		case EL_ECHOTC:
    164  1.1  christos 			wargv[0] = STR("echotc");
    165  1.1  christos 			ret = term_echotc(el, i, wargv);
    166  1.1  christos 			break;
    167  1.1  christos 		case EL_SETTY:
    168  1.1  christos 			wargv[0] = STR("setty");
    169  1.1  christos 			ret = tty_stty(el, i, wargv);
    170  1.1  christos 			break;
    171  1.1  christos 		default:
    172  1.1  christos 			ret = -1;
    173  1.1  christos 		}
    174  1.1  christos 		free(wargv);
    175  1.1  christos 		break;
    176  1.1  christos 	}
    177  1.1  christos 
    178  1.1  christos 	/* XXX: do we need to change el_func_t too? */
    179  1.1  christos 	case EL_ADDFN: {          /* const char *, const char *, el_func_t */
    180  1.1  christos 		const char *args[2];
    181  1.1  christos 		el_func_t func;
    182  1.1  christos 		wchar_t **wargv;
    183  1.1  christos 
    184  1.1  christos 		args[0] = va_arg(ap, const char *);
    185  1.1  christos 		args[1] = va_arg(ap, const char *);
    186  1.1  christos 		func = va_arg(ap, el_func_t);
    187  1.1  christos 
    188  1.1  christos 		wargv = ct_decode_argv(2, args, &el->el_lgcyconv);
    189  1.1  christos 		if (!wargv) {
    190  1.1  christos 		    ret = -1;
    191  1.1  christos 		    goto out;
    192  1.1  christos 		}
    193  1.1  christos 		ret = el_wset(el, op, wargv[0], wargv[1], func);
    194  1.1  christos 		free(wargv);
    195  1.1  christos 		break;
    196  1.1  christos 	}
    197  1.1  christos 	case EL_HIST: {           /* hist_fun_t, const char * */
    198  1.1  christos 		hist_fun_t fun = va_arg(ap, hist_fun_t);
    199  1.1  christos 		ptr_t ptr = va_arg(ap, ptr_t);
    200  1.1  christos 		ret = hist_set(el, fun,
    201  1.1  christos 		    ct_decode_string(ptr, &el->el_lgcyconv));
    202  1.1  christos 		break;
    203  1.1  christos 	}
    204  1.1  christos 	/* XXX: do we need to change el_rfunc_t? */
    205  1.1  christos 	case EL_GETCFN:         /* el_rfunc_t */
    206  1.1  christos 		ret = el_wset(el, op, va_arg(ap, el_rfunc_t));
    207  1.1  christos 		break;
    208  1.1  christos 	case EL_CLIENTDATA:     /* void * */
    209  1.1  christos 		ret = el_wset(el, op, va_arg(ap, void *));
    210  1.1  christos 		break;
    211  1.1  christos 	case EL_SETFP: {          /* int, FILE * */
    212  1.1  christos 		int what = va_arg(ap, int);
    213  1.1  christos 		FILE *fp = va_arg(ap, FILE *);
    214  1.1  christos 		ret = el_wset(el, op, what, fp);
    215  1.1  christos 		break;
    216  1.1  christos 	}
    217  1.1  christos 	case EL_PROMPT_ESC: /* el_pfunc_t, char */
    218  1.1  christos 	case EL_RPROMPT_ESC: {
    219  1.1  christos 		el_pfunc_t p = va_arg(ap, el_pfunc_t);
    220  1.1  christos 		char c = va_arg(ap, int);
    221  1.1  christos 		ret = prompt_set(el, p, c, op, 0);
    222  1.1  christos 		break;
    223  1.1  christos 	}
    224  1.1  christos 	default:
    225  1.1  christos 		ret = -1;
    226  1.1  christos 		break;
    227  1.1  christos 	}
    228  1.1  christos 
    229  1.1  christos out:
    230  1.1  christos 	va_end(ap);
    231  1.1  christos 	return ret;
    232  1.1  christos }
    233  1.1  christos 
    234  1.1  christos 
    235  1.1  christos public int
    236  1.1  christos el_get(EditLine *el, int op, ...)
    237  1.1  christos {
    238  1.1  christos 	va_list ap;
    239  1.1  christos 	int ret;
    240  1.1  christos 
    241  1.1  christos 	if (!el)
    242  1.1  christos 		return -1;
    243  1.1  christos 
    244  1.1  christos 	va_start(ap, op);
    245  1.1  christos 
    246  1.1  christos 	switch (op) {
    247  1.1  christos 	case EL_PROMPT:         /* el_pfunc_t * */
    248  1.1  christos 	case EL_RPROMPT: {
    249  1.1  christos 		el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
    250  1.1  christos 		ret = prompt_get(el, p, 0, 0, op);
    251  1.1  christos 		break;
    252  1.1  christos 	}
    253  1.1  christos 
    254  1.1  christos 	case EL_PROMPT_ESC: /* el_pfunc_t *, char **/
    255  1.1  christos 	case EL_RPROMPT_ESC: {
    256  1.1  christos 		el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
    257  1.1  christos 		char *c = va_arg(ap, char *);
    258  1.1  christos 		wchar_t wc;
    259  1.1  christos 		ret = prompt_get(el, p, 0, &wc, op);
    260  1.1  christos 		*c = (unsigned char)wc;
    261  1.1  christos 		break;
    262  1.1  christos 	}
    263  1.1  christos 
    264  1.1  christos 	case EL_EDITOR: {
    265  1.1  christos 		const char **p = va_arg(ap, const char **);
    266  1.1  christos 		const wchar_t *pw;
    267  1.1  christos 		ret = el_wget(el, op, &pw);
    268  1.1  christos 		*p = ct_encode_string(pw, &el->el_lgcyconv);
    269  1.1  christos 		if (!el->el_lgcyconv.csize)
    270  1.1  christos 			ret = -1;
    271  1.1  christos 		break;
    272  1.1  christos 	}
    273  1.1  christos 
    274  1.1  christos 	case EL_TERMINAL:       /* const char ** */
    275  1.1  christos 		ret = el_wget(el, op, va_arg(ap, const char **));
    276  1.1  christos 		break;
    277  1.1  christos 
    278  1.1  christos 	case EL_SIGNAL:         /* int * */
    279  1.1  christos 	case EL_EDITMODE:
    280  1.1  christos 	case EL_UNBUFFERED:
    281  1.1  christos 	case EL_PREP_TERM:
    282  1.1  christos 		ret = el_wget(el, op, va_arg(ap, int *));
    283  1.1  christos 		break;
    284  1.1  christos 
    285  1.1  christos 	case EL_GETTC: {
    286  1.1  christos 		char *argv[20];
    287  1.1  christos 		static char gettc[] = "gettc";
    288  1.1  christos 		int i;
    289  1.1  christos 		for (i = 1; i < (int)__arraycount(argv); ++i)
    290  1.1  christos 			if ((argv[i] = va_arg(ap, char *)) == NULL)
    291  1.1  christos 				break;
    292  1.1  christos 		argv[0] = gettc;
    293  1.1  christos 		ret = term_gettc(el, i, argv);
    294  1.1  christos 		break;
    295  1.1  christos 	}
    296  1.1  christos 
    297  1.1  christos 	/* XXX: do we need to change el_rfunc_t? */
    298  1.1  christos 	case EL_GETCFN:         /* el_rfunc_t */
    299  1.1  christos 		ret = el_wget(el, op, va_arg(ap, el_rfunc_t *));
    300  1.1  christos 		break;
    301  1.1  christos 
    302  1.1  christos 	case EL_CLIENTDATA:     /* void ** */
    303  1.1  christos 		ret = el_wget(el, op, va_arg(ap, void **));
    304  1.1  christos 		break;
    305  1.1  christos 
    306  1.1  christos 	case EL_GETFP: {          /* int, FILE ** */
    307  1.1  christos 		int what = va_arg(ap, int);
    308  1.1  christos 		FILE **fpp = va_arg(ap, FILE **);
    309  1.1  christos 		ret = el_wget(el, op, what, fpp);
    310  1.1  christos 		break;
    311  1.1  christos 	}
    312  1.1  christos 
    313  1.1  christos 	default:
    314  1.1  christos 		ret = -1;
    315  1.1  christos 		break;
    316  1.1  christos 	}
    317  1.1  christos 
    318  1.1  christos 	va_end(ap);
    319  1.1  christos 	return ret;
    320  1.1  christos }
    321  1.1  christos 
    322  1.1  christos 
    323  1.1  christos const LineInfo *
    324  1.1  christos el_line(EditLine *el)
    325  1.1  christos {
    326  1.1  christos 	const LineInfoW *winfo = el_wline(el);
    327  1.1  christos 	LineInfo *info = &el->el_lgcylinfo;
    328  1.1  christos 
    329  1.1  christos 	info->buffer   = ct_encode_string(winfo->buffer, &el->el_lgcyconv);
    330  1.1  christos 	info->cursor   = info->buffer + (winfo->cursor   - winfo->buffer);
    331  1.1  christos 	info->lastchar = info->buffer + (winfo->lastchar - winfo->buffer);
    332  1.1  christos 	return info;
    333  1.1  christos }
    334  1.1  christos 
    335  1.1  christos 
    336  1.1  christos int
    337  1.1  christos el_insertstr(EditLine *el, const char *str)
    338  1.1  christos {
    339  1.1  christos 	return el_winsertstr(el, ct_decode_string(str, &el->el_lgcyconv));
    340  1.1  christos }
    341