Home | History | Annotate | Line # | Download | only in libedit
eln.c revision 1.33
      1  1.33  christos /*	$NetBSD: eln.c,v 1.33 2016/04/11 18:56:31 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  *
     16   1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17   1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18   1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19   1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20   1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21   1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22   1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23   1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24   1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25   1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26   1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     27   1.1  christos  */
     28   1.1  christos #include "config.h"
     29   1.1  christos #if !defined(lint) && !defined(SCCSID)
     30  1.33  christos __RCSID("$NetBSD: eln.c,v 1.33 2016/04/11 18:56:31 christos Exp $");
     31   1.1  christos #endif /* not lint && not SCCSID */
     32   1.1  christos 
     33  1.22  christos #include <errno.h>
     34   1.1  christos #include <stdarg.h>
     35   1.1  christos #include <stdio.h>
     36   1.2  christos #include <stdlib.h>
     37   1.1  christos 
     38  1.25  christos #include "el.h"
     39  1.25  christos 
     40  1.33  christos int
     41   1.1  christos el_getc(EditLine *el, char *cp)
     42   1.1  christos {
     43   1.1  christos 	int num_read;
     44   1.1  christos 	wchar_t wc = 0;
     45   1.1  christos 
     46  1.22  christos 	num_read = el_wgetc(el, &wc);
     47  1.22  christos 	*cp = '\0';
     48  1.22  christos 	if (num_read <= 0)
     49  1.22  christos 		return num_read;
     50  1.30  christos 	num_read = wctob(wc);
     51  1.22  christos 	if (num_read == EOF) {
     52  1.22  christos 		errno = ERANGE;
     53  1.22  christos 		return -1;
     54  1.22  christos 	} else {
     55  1.22  christos 		*cp = (char)num_read;
     56  1.22  christos 		return 1;
     57  1.22  christos 	}
     58   1.1  christos }
     59   1.1  christos 
     60   1.1  christos 
     61  1.33  christos void
     62   1.1  christos el_push(EditLine *el, const char *str)
     63   1.1  christos {
     64   1.1  christos 	/* Using multibyte->wide string decoding works fine under single-byte
     65   1.1  christos 	 * character sets too, and Does The Right Thing. */
     66   1.1  christos 	el_wpush(el, ct_decode_string(str, &el->el_lgcyconv));
     67   1.1  christos }
     68   1.1  christos 
     69   1.1  christos 
     70  1.33  christos const char *
     71   1.1  christos el_gets(EditLine *el, int *nread)
     72   1.1  christos {
     73   1.6  christos 	const wchar_t *tmp;
     74   1.6  christos 
     75   1.6  christos 	tmp = el_wgets(el, nread);
     76  1.19  christos 	if (tmp != NULL) {
     77  1.21  christos 	    int i;
     78  1.19  christos 	    size_t nwread = 0;
     79  1.21  christos 
     80  1.21  christos 	    for (i = 0; i < *nread; i++)
     81  1.19  christos 		nwread += ct_enc_width(tmp[i]);
     82  1.19  christos 	    *nread = (int)nwread;
     83  1.19  christos 	}
     84   1.6  christos 	return ct_encode_string(tmp, &el->el_lgcyconv);
     85   1.1  christos }
     86   1.1  christos 
     87   1.1  christos 
     88  1.33  christos int
     89   1.1  christos el_parse(EditLine *el, int argc, const char *argv[])
     90   1.1  christos {
     91   1.1  christos 	int ret;
     92   1.1  christos 	const wchar_t **wargv;
     93   1.1  christos 
     94   1.1  christos 	wargv = (const wchar_t **)
     95   1.1  christos 	    ct_decode_argv(argc, argv, &el->el_lgcyconv);
     96   1.1  christos 	if (!wargv)
     97   1.1  christos 		return -1;
     98   1.1  christos 	ret = el_wparse(el, argc, wargv);
     99  1.30  christos 	el_free(wargv);
    100   1.1  christos 
    101   1.1  christos 	return ret;
    102   1.1  christos }
    103   1.1  christos 
    104   1.1  christos 
    105  1.33  christos int
    106   1.1  christos el_set(EditLine *el, int op, ...)
    107   1.1  christos {
    108   1.1  christos 	va_list ap;
    109   1.1  christos 	int ret;
    110   1.1  christos 
    111   1.1  christos 	if (!el)
    112   1.1  christos 		return -1;
    113   1.1  christos 	va_start(ap, op);
    114   1.1  christos 
    115   1.1  christos 	switch (op) {
    116   1.1  christos 	case EL_PROMPT:         /* el_pfunc_t */
    117   1.1  christos 	case EL_RPROMPT: {
    118   1.1  christos 		el_pfunc_t p = va_arg(ap, el_pfunc_t);
    119   1.1  christos 		ret = prompt_set(el, p, 0, op, 0);
    120   1.1  christos 		break;
    121   1.1  christos 	}
    122   1.1  christos 
    123   1.8  christos 	case EL_RESIZE: {
    124   1.8  christos 		el_zfunc_t p = va_arg(ap, el_zfunc_t);
    125   1.8  christos 		void *arg = va_arg(ap, void *);
    126   1.8  christos 		ret = ch_resizefun(el, p, arg);
    127   1.8  christos 		break;
    128   1.8  christos 	}
    129   1.8  christos 
    130  1.17  christos 	case EL_ALIAS_TEXT: {
    131  1.17  christos 		el_afunc_t p = va_arg(ap, el_afunc_t);
    132  1.17  christos 		void *arg = va_arg(ap, void *);
    133  1.17  christos 		ret = ch_aliasfun(el, p, arg);
    134  1.17  christos 		break;
    135  1.17  christos 	}
    136  1.17  christos 
    137  1.15  christos 	case EL_PROMPT_ESC:
    138  1.15  christos 	case EL_RPROMPT_ESC: {
    139  1.15  christos 		el_pfunc_t p = va_arg(ap, el_pfunc_t);
    140  1.15  christos 		int c = va_arg(ap, int);
    141  1.15  christos 
    142  1.15  christos 		ret = prompt_set(el, p, c, op, 0);
    143  1.15  christos 		break;
    144  1.15  christos 	}
    145  1.15  christos 
    146   1.1  christos 	case EL_TERMINAL:       /* const char * */
    147   1.1  christos 		ret = el_wset(el, op, va_arg(ap, char *));
    148   1.1  christos 		break;
    149   1.1  christos 
    150   1.5  christos 	case EL_EDITOR:		/* const wchar_t * */
    151   1.5  christos 		ret = el_wset(el, op, ct_decode_string(va_arg(ap, char *),
    152   1.5  christos 		    &el->el_lgcyconv));
    153   1.5  christos 		break;
    154   1.5  christos 
    155   1.1  christos 	case EL_SIGNAL:         /* int */
    156   1.1  christos 	case EL_EDITMODE:
    157   1.1  christos 	case EL_UNBUFFERED:
    158   1.1  christos 	case EL_PREP_TERM:
    159   1.1  christos 		ret = el_wset(el, op, va_arg(ap, int));
    160   1.1  christos 		break;
    161   1.1  christos 
    162   1.1  christos 	case EL_BIND:   /* const char * list -> const wchar_t * list */
    163   1.1  christos 	case EL_TELLTC:
    164   1.1  christos 	case EL_SETTC:
    165   1.1  christos 	case EL_ECHOTC:
    166   1.1  christos 	case EL_SETTY: {
    167   1.1  christos 		const char *argv[20];
    168   1.1  christos 		int i;
    169   1.1  christos 		const wchar_t **wargv;
    170  1.16  christos 		for (i = 1; i < (int)__arraycount(argv) - 1; ++i)
    171  1.16  christos 			if ((argv[i] = va_arg(ap, const char *)) == NULL)
    172   1.1  christos 			    break;
    173  1.16  christos 		argv[0] = argv[i] = NULL;
    174   1.1  christos 		wargv = (const wchar_t **)
    175  1.14  christos 		    ct_decode_argv(i + 1, argv, &el->el_lgcyconv);
    176   1.1  christos 		if (!wargv) {
    177   1.1  christos 		    ret = -1;
    178   1.1  christos 		    goto out;
    179   1.1  christos 		}
    180   1.1  christos 		/*
    181   1.1  christos 		 * AFAIK we can't portably pass through our new wargv to
    182   1.1  christos 		 * el_wset(), so we have to reimplement the body of
    183   1.1  christos 		 * el_wset() for these ops.
    184   1.1  christos 		 */
    185   1.1  christos 		switch (op) {
    186   1.1  christos 		case EL_BIND:
    187  1.31  christos 			wargv[0] = L"bind";
    188   1.1  christos 			ret = map_bind(el, i, wargv);
    189   1.1  christos 			break;
    190   1.1  christos 		case EL_TELLTC:
    191  1.31  christos 			wargv[0] = L"telltc";
    192  1.11  christos 			ret = terminal_telltc(el, i, wargv);
    193   1.1  christos 			break;
    194   1.1  christos 		case EL_SETTC:
    195  1.31  christos 			wargv[0] = L"settc";
    196  1.11  christos 			ret = terminal_settc(el, i, wargv);
    197   1.1  christos 			break;
    198   1.1  christos 		case EL_ECHOTC:
    199  1.31  christos 			wargv[0] = L"echotc";
    200  1.11  christos 			ret = terminal_echotc(el, i, wargv);
    201   1.1  christos 			break;
    202   1.1  christos 		case EL_SETTY:
    203  1.31  christos 			wargv[0] = L"setty";
    204   1.1  christos 			ret = tty_stty(el, i, wargv);
    205   1.1  christos 			break;
    206   1.1  christos 		default:
    207   1.1  christos 			ret = -1;
    208   1.1  christos 		}
    209  1.30  christos 		el_free(wargv);
    210   1.1  christos 		break;
    211   1.1  christos 	}
    212   1.1  christos 
    213   1.1  christos 	/* XXX: do we need to change el_func_t too? */
    214   1.1  christos 	case EL_ADDFN: {          /* const char *, const char *, el_func_t */
    215   1.1  christos 		const char *args[2];
    216   1.1  christos 		el_func_t func;
    217   1.1  christos 		wchar_t **wargv;
    218   1.1  christos 
    219   1.1  christos 		args[0] = va_arg(ap, const char *);
    220   1.1  christos 		args[1] = va_arg(ap, const char *);
    221   1.1  christos 		func = va_arg(ap, el_func_t);
    222   1.1  christos 
    223   1.1  christos 		wargv = ct_decode_argv(2, args, &el->el_lgcyconv);
    224   1.1  christos 		if (!wargv) {
    225   1.1  christos 		    ret = -1;
    226   1.1  christos 		    goto out;
    227   1.1  christos 		}
    228  1.23  christos 		/* XXX: The two strdup's leak */
    229  1.31  christos 		ret = map_addfunc(el, wcsdup(wargv[0]), wcsdup(wargv[1]),
    230   1.4  christos 		    func);
    231  1.30  christos 		el_free(wargv);
    232   1.1  christos 		break;
    233   1.1  christos 	}
    234   1.1  christos 	case EL_HIST: {           /* hist_fun_t, const char * */
    235   1.1  christos 		hist_fun_t fun = va_arg(ap, hist_fun_t);
    236  1.12  christos 		void *ptr = va_arg(ap, void *);
    237   1.2  christos 		ret = hist_set(el, fun, ptr);
    238   1.9  christos 		el->el_flags |= NARROW_HISTORY;
    239   1.1  christos 		break;
    240   1.1  christos 	}
    241  1.15  christos 
    242   1.1  christos 	case EL_GETCFN:         /* el_rfunc_t */
    243   1.1  christos 		ret = el_wset(el, op, va_arg(ap, el_rfunc_t));
    244   1.1  christos 		break;
    245  1.15  christos 
    246   1.1  christos 	case EL_CLIENTDATA:     /* void * */
    247   1.1  christos 		ret = el_wset(el, op, va_arg(ap, void *));
    248   1.1  christos 		break;
    249  1.15  christos 
    250   1.1  christos 	case EL_SETFP: {          /* int, FILE * */
    251   1.1  christos 		int what = va_arg(ap, int);
    252   1.1  christos 		FILE *fp = va_arg(ap, FILE *);
    253   1.1  christos 		ret = el_wset(el, op, what, fp);
    254   1.1  christos 		break;
    255   1.1  christos 	}
    256  1.15  christos 
    257  1.15  christos 	case EL_REFRESH:
    258  1.15  christos 		re_clear_display(el);
    259  1.15  christos 		re_refresh(el);
    260  1.15  christos 		terminal__flush(el);
    261  1.15  christos 		ret = 0;
    262   1.1  christos 		break;
    263  1.15  christos 
    264   1.1  christos 	default:
    265   1.1  christos 		ret = -1;
    266   1.1  christos 		break;
    267   1.1  christos 	}
    268   1.1  christos 
    269   1.1  christos out:
    270   1.1  christos 	va_end(ap);
    271   1.1  christos 	return ret;
    272   1.1  christos }
    273   1.1  christos 
    274   1.1  christos 
    275  1.33  christos int
    276   1.1  christos el_get(EditLine *el, int op, ...)
    277   1.1  christos {
    278   1.1  christos 	va_list ap;
    279   1.1  christos 	int ret;
    280   1.1  christos 
    281   1.1  christos 	if (!el)
    282   1.1  christos 		return -1;
    283   1.1  christos 
    284   1.1  christos 	va_start(ap, op);
    285   1.1  christos 
    286   1.1  christos 	switch (op) {
    287   1.1  christos 	case EL_PROMPT:         /* el_pfunc_t * */
    288   1.1  christos 	case EL_RPROMPT: {
    289   1.1  christos 		el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
    290   1.2  christos 		ret = prompt_get(el, p, 0, op);
    291   1.1  christos 		break;
    292   1.1  christos 	}
    293   1.1  christos 
    294   1.1  christos 	case EL_PROMPT_ESC: /* el_pfunc_t *, char **/
    295   1.1  christos 	case EL_RPROMPT_ESC: {
    296   1.1  christos 		el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
    297   1.1  christos 		char *c = va_arg(ap, char *);
    298  1.10       mrg 		wchar_t wc = 0;
    299   1.2  christos 		ret = prompt_get(el, p, &wc, op);
    300  1.13  christos 		*c = (char)wc;
    301   1.1  christos 		break;
    302   1.1  christos 	}
    303   1.1  christos 
    304   1.1  christos 	case EL_EDITOR: {
    305   1.1  christos 		const char **p = va_arg(ap, const char **);
    306   1.1  christos 		const wchar_t *pw;
    307   1.1  christos 		ret = el_wget(el, op, &pw);
    308   1.1  christos 		*p = ct_encode_string(pw, &el->el_lgcyconv);
    309   1.1  christos 		if (!el->el_lgcyconv.csize)
    310   1.1  christos 			ret = -1;
    311   1.1  christos 		break;
    312   1.1  christos 	}
    313   1.1  christos 
    314   1.1  christos 	case EL_TERMINAL:       /* const char ** */
    315   1.1  christos 		ret = el_wget(el, op, va_arg(ap, const char **));
    316   1.1  christos 		break;
    317   1.1  christos 
    318   1.1  christos 	case EL_SIGNAL:         /* int * */
    319   1.1  christos 	case EL_EDITMODE:
    320   1.1  christos 	case EL_UNBUFFERED:
    321   1.1  christos 	case EL_PREP_TERM:
    322   1.1  christos 		ret = el_wget(el, op, va_arg(ap, int *));
    323   1.1  christos 		break;
    324   1.1  christos 
    325   1.1  christos 	case EL_GETTC: {
    326   1.1  christos 		char *argv[20];
    327   1.1  christos 		static char gettc[] = "gettc";
    328   1.1  christos 		int i;
    329   1.1  christos 		for (i = 1; i < (int)__arraycount(argv); ++i)
    330   1.1  christos 			if ((argv[i] = va_arg(ap, char *)) == NULL)
    331   1.1  christos 				break;
    332   1.1  christos 		argv[0] = gettc;
    333  1.11  christos 		ret = terminal_gettc(el, i, argv);
    334   1.1  christos 		break;
    335   1.1  christos 	}
    336   1.1  christos 
    337   1.1  christos 	case EL_GETCFN:         /* el_rfunc_t */
    338   1.1  christos 		ret = el_wget(el, op, va_arg(ap, el_rfunc_t *));
    339   1.1  christos 		break;
    340   1.1  christos 
    341   1.1  christos 	case EL_CLIENTDATA:     /* void ** */
    342   1.1  christos 		ret = el_wget(el, op, va_arg(ap, void **));
    343   1.1  christos 		break;
    344   1.1  christos 
    345   1.1  christos 	case EL_GETFP: {          /* int, FILE ** */
    346   1.1  christos 		int what = va_arg(ap, int);
    347   1.1  christos 		FILE **fpp = va_arg(ap, FILE **);
    348   1.1  christos 		ret = el_wget(el, op, what, fpp);
    349   1.1  christos 		break;
    350   1.1  christos 	}
    351   1.1  christos 
    352   1.1  christos 	default:
    353   1.1  christos 		ret = -1;
    354   1.1  christos 		break;
    355   1.1  christos 	}
    356   1.1  christos 
    357   1.1  christos 	va_end(ap);
    358   1.1  christos 	return ret;
    359   1.1  christos }
    360   1.1  christos 
    361   1.1  christos 
    362   1.1  christos const LineInfo *
    363   1.1  christos el_line(EditLine *el)
    364   1.1  christos {
    365   1.1  christos 	const LineInfoW *winfo = el_wline(el);
    366   1.1  christos 	LineInfo *info = &el->el_lgcylinfo;
    367   1.7  christos 	size_t offset;
    368  1.32  christos 	const wchar_t *p;
    369   1.1  christos 
    370   1.1  christos 	info->buffer   = ct_encode_string(winfo->buffer, &el->el_lgcyconv);
    371   1.7  christos 
    372   1.7  christos 	offset = 0;
    373   1.7  christos 	for (p = winfo->buffer; p < winfo->cursor; p++)
    374   1.7  christos 		offset += ct_enc_width(*p);
    375   1.7  christos 	info->cursor = info->buffer + offset;
    376   1.7  christos 
    377   1.7  christos 	offset = 0;
    378   1.7  christos 	for (p = winfo->buffer; p < winfo->lastchar; p++)
    379   1.7  christos 		offset += ct_enc_width(*p);
    380   1.7  christos 	info->lastchar = info->buffer + offset;
    381   1.7  christos 
    382   1.1  christos 	return info;
    383   1.1  christos }
    384   1.1  christos 
    385   1.1  christos 
    386   1.1  christos int
    387   1.1  christos el_insertstr(EditLine *el, const char *str)
    388   1.1  christos {
    389   1.1  christos 	return el_winsertstr(el, ct_decode_string(str, &el->el_lgcyconv));
    390   1.1  christos }
    391