Home | History | Annotate | Line # | Download | only in libedit
el.c revision 1.89
      1  1.89  christos /*	$NetBSD: el.c,v 1.89 2016/04/19 19:50:53 christos Exp $	*/
      2   1.2     lukem 
      3   1.1       cgd /*-
      4   1.1       cgd  * Copyright (c) 1992, 1993
      5   1.1       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * This code is derived from software contributed to Berkeley by
      8   1.1       cgd  * Christos Zoulas of Cornell University.
      9   1.1       cgd  *
     10   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1       cgd  * modification, are permitted provided that the following conditions
     12   1.1       cgd  * are met:
     13   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18  1.32       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1       cgd  *    may be used to endorse or promote products derived from this software
     20   1.1       cgd  *    without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1       cgd  * SUCH DAMAGE.
     33   1.1       cgd  */
     34   1.1       cgd 
     35  1.29  christos #include "config.h"
     36   1.1       cgd #if !defined(lint) && !defined(SCCSID)
     37   1.2     lukem #if 0
     38   1.1       cgd static char sccsid[] = "@(#)el.c	8.2 (Berkeley) 1/3/94";
     39   1.2     lukem #else
     40  1.89  christos __RCSID("$NetBSD: el.c,v 1.89 2016/04/19 19:50:53 christos Exp $");
     41   1.2     lukem #endif
     42   1.1       cgd #endif /* not lint && not SCCSID */
     43   1.1       cgd 
     44   1.1       cgd /*
     45   1.1       cgd  * el.c: EditLine interface functions
     46   1.1       cgd  */
     47   1.1       cgd #include <sys/types.h>
     48   1.1       cgd #include <sys/param.h>
     49  1.82  christos #include <ctype.h>
     50  1.84  christos #include <langinfo.h>
     51  1.84  christos #include <locale.h>
     52  1.82  christos #include <stdarg.h>
     53  1.82  christos #include <stdlib.h>
     54   1.1       cgd #include <string.h>
     55  1.75  christos 
     56   1.1       cgd #include "el.h"
     57  1.81  christos #include "parse.h"
     58  1.89  christos #include "read.h"
     59   1.1       cgd 
     60   1.1       cgd /* el_init():
     61   1.1       cgd  *	Initialize editline and set default parameters.
     62   1.1       cgd  */
     63  1.88  christos EditLine *
     64  1.19     lukem el_init(const char *prog, FILE *fin, FILE *fout, FILE *ferr)
     65   1.1       cgd {
     66  1.72  christos     return el_init_fd(prog, fin, fout, ferr, fileno(fin), fileno(fout),
     67  1.72  christos 	fileno(ferr));
     68  1.72  christos }
     69  1.72  christos 
     70  1.88  christos EditLine *
     71  1.72  christos el_init_fd(const char *prog, FILE *fin, FILE *fout, FILE *ferr,
     72  1.72  christos     int fdin, int fdout, int fderr)
     73  1.72  christos {
     74  1.67  christos 	EditLine *el = el_malloc(sizeof(*el));
     75   1.1       cgd 
     76  1.19     lukem 	if (el == NULL)
     77  1.68  christos 		return NULL;
     78   1.1       cgd 
     79  1.19     lukem 	memset(el, 0, sizeof(EditLine));
     80   1.1       cgd 
     81  1.44  christos 	el->el_infile = fin;
     82  1.19     lukem 	el->el_outfile = fout;
     83  1.19     lukem 	el->el_errfile = ferr;
     84  1.44  christos 
     85  1.72  christos 	el->el_infd = fdin;
     86  1.72  christos 	el->el_outfd = fdout;
     87  1.72  christos 	el->el_errfd = fderr;
     88  1.44  christos 
     89  1.86  christos 	el->el_prog = wcsdup(ct_decode_string(prog, &el->el_scratch));
     90  1.56  christos 	if (el->el_prog == NULL) {
     91  1.36  christos 		el_free(el);
     92  1.36  christos 		return NULL;
     93  1.36  christos 	}
     94  1.19     lukem 
     95  1.19     lukem 	/*
     96  1.19     lukem          * Initialize all the modules. Order is important!!!
     97  1.19     lukem          */
     98  1.19     lukem 	el->el_flags = 0;
     99  1.59  christos 	if (setlocale(LC_CTYPE, NULL) != NULL){
    100  1.59  christos 		if (strcmp(nl_langinfo(CODESET), "UTF-8") == 0)
    101  1.56  christos 			el->el_flags |= CHARSET_IS_UTF8;
    102  1.56  christos 	}
    103  1.19     lukem 
    104  1.64  christos 	if (terminal_init(el) == -1) {
    105  1.36  christos 		el_free(el->el_prog);
    106  1.25  christos 		el_free(el);
    107  1.25  christos 		return NULL;
    108  1.25  christos 	}
    109  1.66  christos 	(void) keymacro_init(el);
    110  1.19     lukem 	(void) map_init(el);
    111  1.19     lukem 	if (tty_init(el) == -1)
    112  1.19     lukem 		el->el_flags |= NO_TTY;
    113  1.19     lukem 	(void) ch_init(el);
    114  1.19     lukem 	(void) search_init(el);
    115  1.19     lukem 	(void) hist_init(el);
    116  1.19     lukem 	(void) prompt_init(el);
    117  1.19     lukem 	(void) sig_init(el);
    118  1.89  christos 	if (read_init(el) == -1) {
    119  1.89  christos 		el_end(el);
    120  1.89  christos 		return NULL;
    121  1.89  christos 	}
    122  1.68  christos 	return el;
    123  1.19     lukem }
    124   1.1       cgd 
    125   1.1       cgd 
    126   1.1       cgd /* el_end():
    127   1.1       cgd  *	Clean up.
    128   1.1       cgd  */
    129  1.88  christos void
    130  1.19     lukem el_end(EditLine *el)
    131   1.1       cgd {
    132   1.1       cgd 
    133  1.19     lukem 	if (el == NULL)
    134  1.19     lukem 		return;
    135   1.1       cgd 
    136  1.19     lukem 	el_reset(el);
    137  1.19     lukem 
    138  1.64  christos 	terminal_end(el);
    139  1.66  christos 	keymacro_end(el);
    140  1.19     lukem 	map_end(el);
    141  1.74  christos 	if (!(el->el_flags & NO_TTY))
    142  1.74  christos 		tty_end(el);
    143  1.19     lukem 	ch_end(el);
    144  1.19     lukem 	search_end(el);
    145  1.19     lukem 	hist_end(el);
    146  1.19     lukem 	prompt_end(el);
    147  1.19     lukem 	sig_end(el);
    148  1.19     lukem 
    149  1.67  christos 	el_free(el->el_prog);
    150  1.67  christos 	el_free(el->el_scratch.cbuff);
    151  1.67  christos 	el_free(el->el_scratch.wbuff);
    152  1.67  christos 	el_free(el->el_lgcyconv.cbuff);
    153  1.67  christos 	el_free(el->el_lgcyconv.wbuff);
    154  1.67  christos 	el_free(el);
    155  1.19     lukem }
    156   1.1       cgd 
    157   1.1       cgd 
    158   1.1       cgd /* el_reset():
    159   1.1       cgd  *	Reset the tty and the parser
    160   1.1       cgd  */
    161  1.88  christos void
    162  1.19     lukem el_reset(EditLine *el)
    163   1.1       cgd {
    164  1.19     lukem 
    165  1.19     lukem 	tty_cookedmode(el);
    166  1.40  christos 	ch_reset(el, 0);		/* XXX: Do we want that? */
    167   1.1       cgd }
    168   1.1       cgd 
    169   1.1       cgd 
    170   1.1       cgd /* el_set():
    171   1.1       cgd  *	set the editline parameters
    172   1.1       cgd  */
    173  1.88  christos int
    174  1.86  christos el_wset(EditLine *el, int op, ...)
    175   1.1       cgd {
    176  1.44  christos 	va_list ap;
    177  1.27  christos 	int rv = 0;
    178   1.1       cgd 
    179  1.19     lukem 	if (el == NULL)
    180  1.68  christos 		return -1;
    181  1.44  christos 	va_start(ap, op);
    182  1.22       wiz 
    183  1.19     lukem 	switch (op) {
    184  1.19     lukem 	case EL_PROMPT:
    185  1.51  christos 	case EL_RPROMPT: {
    186  1.51  christos 		el_pfunc_t p = va_arg(ap, el_pfunc_t);
    187  1.52  christos 
    188  1.56  christos 		rv = prompt_set(el, p, 0, op, 1);
    189  1.52  christos 		break;
    190  1.52  christos 	}
    191  1.52  christos 
    192  1.60  christos 	case EL_RESIZE: {
    193  1.60  christos 		el_zfunc_t p = va_arg(ap, el_zfunc_t);
    194  1.60  christos 		void *arg = va_arg(ap, void *);
    195  1.60  christos 		rv = ch_resizefun(el, p, arg);
    196  1.60  christos 		break;
    197  1.60  christos 	}
    198  1.60  christos 
    199  1.73  christos 	case EL_ALIAS_TEXT: {
    200  1.73  christos 		el_afunc_t p = va_arg(ap, el_afunc_t);
    201  1.73  christos 		void *arg = va_arg(ap, void *);
    202  1.73  christos 		rv = ch_aliasfun(el, p, arg);
    203  1.73  christos 		break;
    204  1.73  christos 	}
    205  1.73  christos 
    206  1.52  christos 	case EL_PROMPT_ESC:
    207  1.52  christos 	case EL_RPROMPT_ESC: {
    208  1.52  christos 		el_pfunc_t p = va_arg(ap, el_pfunc_t);
    209  1.56  christos 		int c = va_arg(ap, int);
    210  1.51  christos 
    211  1.87  christos 		rv = prompt_set(el, p, (wchar_t)c, op, 1);
    212  1.50  christos 		break;
    213  1.51  christos 	}
    214  1.13    simonb 
    215  1.19     lukem 	case EL_TERMINAL:
    216  1.64  christos 		rv = terminal_set(el, va_arg(ap, char *));
    217  1.19     lukem 		break;
    218   1.1       cgd 
    219  1.19     lukem 	case EL_EDITOR:
    220  1.87  christos 		rv = map_set_editor(el, va_arg(ap, wchar_t *));
    221   1.1       cgd 		break;
    222   1.1       cgd 
    223  1.19     lukem 	case EL_SIGNAL:
    224  1.44  christos 		if (va_arg(ap, int))
    225  1.19     lukem 			el->el_flags |= HANDLE_SIGNALS;
    226  1.19     lukem 		else
    227  1.19     lukem 			el->el_flags &= ~HANDLE_SIGNALS;
    228   1.1       cgd 		break;
    229   1.1       cgd 
    230  1.19     lukem 	case EL_BIND:
    231  1.19     lukem 	case EL_TELLTC:
    232  1.19     lukem 	case EL_SETTC:
    233  1.19     lukem 	case EL_ECHOTC:
    234  1.19     lukem 	case EL_SETTY:
    235  1.19     lukem 	{
    236  1.87  christos 		const wchar_t *argv[20];
    237  1.19     lukem 		int i;
    238  1.19     lukem 
    239  1.70  christos 		for (i = 1; i < (int)__arraycount(argv); i++)
    240  1.87  christos 			if ((argv[i] = va_arg(ap, wchar_t *)) == NULL)
    241  1.19     lukem 				break;
    242  1.19     lukem 
    243  1.19     lukem 		switch (op) {
    244  1.19     lukem 		case EL_BIND:
    245  1.86  christos 			argv[0] = L"bind";
    246  1.19     lukem 			rv = map_bind(el, i, argv);
    247  1.19     lukem 			break;
    248  1.19     lukem 
    249  1.19     lukem 		case EL_TELLTC:
    250  1.86  christos 			argv[0] = L"telltc";
    251  1.64  christos 			rv = terminal_telltc(el, i, argv);
    252  1.19     lukem 			break;
    253  1.19     lukem 
    254  1.19     lukem 		case EL_SETTC:
    255  1.86  christos 			argv[0] = L"settc";
    256  1.64  christos 			rv = terminal_settc(el, i, argv);
    257  1.19     lukem 			break;
    258  1.19     lukem 
    259  1.19     lukem 		case EL_ECHOTC:
    260  1.86  christos 			argv[0] = L"echotc";
    261  1.64  christos 			rv = terminal_echotc(el, i, argv);
    262  1.19     lukem 			break;
    263  1.19     lukem 
    264  1.19     lukem 		case EL_SETTY:
    265  1.86  christos 			argv[0] = L"setty";
    266  1.19     lukem 			rv = tty_stty(el, i, argv);
    267  1.19     lukem 			break;
    268  1.19     lukem 
    269  1.19     lukem 		default:
    270  1.19     lukem 			rv = -1;
    271  1.20  christos 			EL_ABORT((el->el_errfile, "Bad op %d\n", op));
    272  1.19     lukem 			break;
    273  1.19     lukem 		}
    274   1.1       cgd 		break;
    275  1.19     lukem 	}
    276  1.19     lukem 
    277  1.19     lukem 	case EL_ADDFN:
    278  1.19     lukem 	{
    279  1.87  christos 		wchar_t *name = va_arg(ap, wchar_t *);
    280  1.87  christos 		wchar_t *help = va_arg(ap, wchar_t *);
    281  1.44  christos 		el_func_t func = va_arg(ap, el_func_t);
    282   1.1       cgd 
    283  1.19     lukem 		rv = map_addfunc(el, name, help, func);
    284   1.1       cgd 		break;
    285  1.19     lukem 	}
    286  1.19     lukem 
    287  1.19     lukem 	case EL_HIST:
    288  1.19     lukem 	{
    289  1.44  christos 		hist_fun_t func = va_arg(ap, hist_fun_t);
    290  1.67  christos 		void *ptr = va_arg(ap, void *);
    291   1.1       cgd 
    292  1.19     lukem 		rv = hist_set(el, func, ptr);
    293  1.59  christos 		if (!(el->el_flags & CHARSET_IS_UTF8))
    294  1.59  christos 			el->el_flags &= ~NARROW_HISTORY;
    295   1.1       cgd 		break;
    296  1.19     lukem 	}
    297   1.1       cgd 
    298  1.19     lukem 	case EL_EDITMODE:
    299  1.44  christos 		if (va_arg(ap, int))
    300  1.19     lukem 			el->el_flags &= ~EDIT_DISABLED;
    301  1.19     lukem 		else
    302  1.19     lukem 			el->el_flags |= EDIT_DISABLED;
    303  1.19     lukem 		rv = 0;
    304   1.1       cgd 		break;
    305  1.13    simonb 
    306  1.23  christos 	case EL_GETCFN:
    307  1.23  christos 	{
    308  1.44  christos 		el_rfunc_t rc = va_arg(ap, el_rfunc_t);
    309  1.89  christos 		rv = el_read_setfn(el->el_read, rc);
    310  1.23  christos 		break;
    311  1.23  christos 	}
    312  1.23  christos 
    313  1.24  christos 	case EL_CLIENTDATA:
    314  1.44  christos 		el->el_data = va_arg(ap, void *);
    315  1.24  christos 		break;
    316  1.24  christos 
    317  1.34  christos 	case EL_UNBUFFERED:
    318  1.44  christos 		rv = va_arg(ap, int);
    319  1.34  christos 		if (rv && !(el->el_flags & UNBUFFERED)) {
    320  1.34  christos 			el->el_flags |= UNBUFFERED;
    321  1.34  christos 			read_prepare(el);
    322  1.34  christos 		} else if (!rv && (el->el_flags & UNBUFFERED)) {
    323  1.34  christos 			el->el_flags &= ~UNBUFFERED;
    324  1.34  christos 			read_finish(el);
    325  1.34  christos 		}
    326  1.35  christos 		rv = 0;
    327  1.35  christos 		break;
    328  1.35  christos 
    329  1.35  christos 	case EL_PREP_TERM:
    330  1.44  christos 		rv = va_arg(ap, int);
    331  1.35  christos 		if (rv)
    332  1.38  christos 			(void) tty_rawmode(el);
    333  1.35  christos 		else
    334  1.38  christos 			(void) tty_cookedmode(el);
    335  1.34  christos 		rv = 0;
    336  1.34  christos 		break;
    337  1.34  christos 
    338  1.44  christos 	case EL_SETFP:
    339  1.44  christos 	{
    340  1.44  christos 		FILE *fp;
    341  1.44  christos 		int what;
    342  1.44  christos 
    343  1.44  christos 		what = va_arg(ap, int);
    344  1.44  christos 		fp = va_arg(ap, FILE *);
    345  1.44  christos 
    346  1.44  christos 		rv = 0;
    347  1.44  christos 		switch (what) {
    348  1.44  christos 		case 0:
    349  1.44  christos 			el->el_infile = fp;
    350  1.44  christos 			el->el_infd = fileno(fp);
    351  1.44  christos 			break;
    352  1.44  christos 		case 1:
    353  1.44  christos 			el->el_outfile = fp;
    354  1.61  christos 			el->el_outfd = fileno(fp);
    355  1.44  christos 			break;
    356  1.44  christos 		case 2:
    357  1.44  christos 			el->el_errfile = fp;
    358  1.61  christos 			el->el_errfd = fileno(fp);
    359  1.44  christos 			break;
    360  1.44  christos 		default:
    361  1.44  christos 			rv = -1;
    362  1.44  christos 			break;
    363  1.44  christos 		}
    364  1.44  christos 		break;
    365  1.44  christos 	}
    366  1.44  christos 
    367  1.45  christos 	case EL_REFRESH:
    368  1.45  christos 		re_clear_display(el);
    369  1.45  christos 		re_refresh(el);
    370  1.64  christos 		terminal__flush(el);
    371  1.45  christos 		break;
    372  1.45  christos 
    373  1.19     lukem 	default:
    374  1.19     lukem 		rv = -1;
    375  1.27  christos 		break;
    376   1.1       cgd 	}
    377   1.1       cgd 
    378  1.44  christos 	va_end(ap);
    379  1.68  christos 	return rv;
    380  1.19     lukem }
    381   1.1       cgd 
    382   1.1       cgd 
    383  1.10     lukem /* el_get():
    384  1.10     lukem  *	retrieve the editline parameters
    385  1.10     lukem  */
    386  1.88  christos int
    387  1.86  christos el_wget(EditLine *el, int op, ...)
    388  1.10     lukem {
    389  1.42  christos 	va_list ap;
    390  1.19     lukem 	int rv;
    391  1.10     lukem 
    392  1.42  christos 	if (el == NULL)
    393  1.42  christos 		return -1;
    394  1.42  christos 
    395  1.42  christos 	va_start(ap, op);
    396  1.42  christos 
    397  1.19     lukem 	switch (op) {
    398  1.19     lukem 	case EL_PROMPT:
    399  1.51  christos 	case EL_RPROMPT: {
    400  1.51  christos 		el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
    401  1.56  christos 		rv = prompt_get(el, p, 0, op);
    402  1.56  christos 		break;
    403  1.56  christos 	}
    404  1.56  christos 	case EL_PROMPT_ESC:
    405  1.56  christos 	case EL_RPROMPT_ESC: {
    406  1.56  christos 		el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
    407  1.87  christos 		wchar_t *c = va_arg(ap, wchar_t *);
    408  1.51  christos 
    409  1.51  christos 		rv = prompt_get(el, p, c, op);
    410  1.19     lukem 		break;
    411  1.51  christos 	}
    412  1.10     lukem 
    413  1.19     lukem 	case EL_EDITOR:
    414  1.87  christos 		rv = map_get_editor(el, va_arg(ap, const wchar_t **));
    415  1.10     lukem 		break;
    416  1.10     lukem 
    417  1.19     lukem 	case EL_SIGNAL:
    418  1.42  christos 		*va_arg(ap, int *) = (el->el_flags & HANDLE_SIGNALS);
    419  1.19     lukem 		rv = 0;
    420  1.10     lukem 		break;
    421  1.10     lukem 
    422  1.19     lukem 	case EL_EDITMODE:
    423  1.42  christos 		*va_arg(ap, int *) = !(el->el_flags & EDIT_DISABLED);
    424  1.19     lukem 		rv = 0;
    425  1.10     lukem 		break;
    426  1.10     lukem 
    427  1.19     lukem 	case EL_TERMINAL:
    428  1.64  christos 		terminal_get(el, va_arg(ap, const char **));
    429  1.33  christos 		rv = 0;
    430  1.10     lukem 		break;
    431  1.10     lukem 
    432  1.42  christos 	case EL_GETTC:
    433  1.19     lukem 	{
    434  1.42  christos 		static char name[] = "gettc";
    435  1.42  christos 		char *argv[20];
    436  1.19     lukem 		int i;
    437  1.10     lukem 
    438  1.82  christos 		for (i = 1; i < (int)__arraycount(argv); i++)
    439  1.42  christos 			if ((argv[i] = va_arg(ap, char *)) == NULL)
    440  1.19     lukem 				break;
    441  1.19     lukem 
    442  1.69  christos 		argv[0] = name;
    443  1.69  christos 		rv = terminal_gettc(el, i, argv);
    444  1.10     lukem 		break;
    445  1.10     lukem 	}
    446  1.13    simonb 
    447  1.23  christos 	case EL_GETCFN:
    448  1.89  christos 		*va_arg(ap, el_rfunc_t *) = el_read_getfn(el->el_read);
    449  1.24  christos 		rv = 0;
    450  1.24  christos 		break;
    451  1.24  christos 
    452  1.24  christos 	case EL_CLIENTDATA:
    453  1.42  christos 		*va_arg(ap, void **) = el->el_data;
    454  1.34  christos 		rv = 0;
    455  1.34  christos 		break;
    456  1.34  christos 
    457  1.34  christos 	case EL_UNBUFFERED:
    458  1.71  christos 		*va_arg(ap, int *) = (el->el_flags & UNBUFFERED) != 0;
    459  1.23  christos 		rv = 0;
    460  1.23  christos 		break;
    461  1.19     lukem 
    462  1.44  christos 	case EL_GETFP:
    463  1.44  christos 	{
    464  1.44  christos 		int what;
    465  1.44  christos 		FILE **fpp;
    466  1.44  christos 
    467  1.44  christos 		what = va_arg(ap, int);
    468  1.44  christos 		fpp = va_arg(ap, FILE **);
    469  1.44  christos 		rv = 0;
    470  1.44  christos 		switch (what) {
    471  1.44  christos 		case 0:
    472  1.44  christos 			*fpp = el->el_infile;
    473  1.44  christos 			break;
    474  1.44  christos 		case 1:
    475  1.44  christos 			*fpp = el->el_outfile;
    476  1.44  christos 			break;
    477  1.44  christos 		case 2:
    478  1.44  christos 			*fpp = el->el_errfile;
    479  1.44  christos 			break;
    480  1.44  christos 		default:
    481  1.44  christos 			rv = -1;
    482  1.44  christos 			break;
    483  1.44  christos 		}
    484  1.44  christos 		break;
    485  1.44  christos 	}
    486  1.19     lukem 	default:
    487  1.19     lukem 		rv = -1;
    488  1.44  christos 		break;
    489  1.19     lukem 	}
    490  1.42  christos 	va_end(ap);
    491  1.10     lukem 
    492  1.68  christos 	return rv;
    493  1.19     lukem }
    494  1.10     lukem 
    495  1.10     lukem 
    496   1.1       cgd /* el_line():
    497   1.1       cgd  *	Return editing info
    498   1.1       cgd  */
    499  1.88  christos const LineInfoW *
    500  1.86  christos el_wline(EditLine *el)
    501   1.1       cgd {
    502  1.19     lukem 
    503  1.86  christos 	return (const LineInfoW *)(void *)&el->el_line;
    504   1.1       cgd }
    505   1.1       cgd 
    506   1.1       cgd 
    507   1.1       cgd /* el_source():
    508   1.1       cgd  *	Source a file
    509   1.1       cgd  */
    510  1.88  christos int
    511  1.19     lukem el_source(EditLine *el, const char *fname)
    512   1.1       cgd {
    513  1.19     lukem 	FILE *fp;
    514  1.19     lukem 	size_t len;
    515  1.77  christos 	ssize_t slen;
    516  1.27  christos 	char *ptr;
    517  1.64  christos 	char *path = NULL;
    518  1.87  christos 	const wchar_t *dptr;
    519  1.64  christos 	int error = 0;
    520  1.19     lukem 
    521  1.19     lukem 	fp = NULL;
    522  1.19     lukem 	if (fname == NULL) {
    523  1.29  christos #ifdef HAVE_ISSETUGID
    524  1.27  christos 		static const char elpath[] = "/.editrc";
    525  1.65  christos 		size_t plen = sizeof(elpath);
    526  1.27  christos 
    527  1.19     lukem 		if (issetugid())
    528  1.68  christos 			return -1;
    529  1.19     lukem 		if ((ptr = getenv("HOME")) == NULL)
    530  1.68  christos 			return -1;
    531  1.65  christos 		plen += strlen(ptr);
    532  1.67  christos 		if ((path = el_malloc(plen * sizeof(*path))) == NULL)
    533  1.68  christos 			return -1;
    534  1.65  christos 		(void)snprintf(path, plen, "%s%s", ptr, elpath);
    535  1.19     lukem 		fname = path;
    536  1.27  christos #else
    537  1.27  christos 		/*
    538  1.27  christos 		 * If issetugid() is missing, always return an error, in order
    539  1.27  christos 		 * to keep from inadvertently opening up the user to a security
    540  1.27  christos 		 * hole.
    541  1.27  christos 		 */
    542  1.68  christos 		return -1;
    543  1.27  christos #endif
    544  1.19     lukem 	}
    545  1.19     lukem 	if (fp == NULL)
    546  1.19     lukem 		fp = fopen(fname, "r");
    547  1.64  christos 	if (fp == NULL) {
    548  1.64  christos 		el_free(path);
    549  1.68  christos 		return -1;
    550  1.64  christos 	}
    551  1.19     lukem 
    552  1.77  christos 	ptr = NULL;
    553  1.77  christos 	len = 0;
    554  1.77  christos 	while ((slen = getline(&ptr, &len, fp)) != -1) {
    555  1.63  christos 		if (*ptr == '\n')
    556  1.63  christos 			continue;	/* Empty line. */
    557  1.77  christos 		if (slen > 0 && ptr[--slen] == '\n')
    558  1.77  christos 			ptr[slen] = '\0';
    559  1.77  christos 
    560  1.56  christos 		dptr = ct_decode_string(ptr, &el->el_scratch);
    561  1.56  christos 		if (!dptr)
    562  1.56  christos 			continue;
    563  1.55  christos 		/* loop until first non-space char or EOL */
    564  1.85  christos 		while (*dptr != '\0' && iswspace(*dptr))
    565  1.56  christos 			dptr++;
    566  1.56  christos 		if (*dptr == '#')
    567  1.55  christos 			continue;   /* ignore, this is a comment line */
    568  1.64  christos 		if ((error = parse_line(el, dptr)) == -1)
    569  1.64  christos 			break;
    570   1.1       cgd 	}
    571  1.76  christos 	free(ptr);
    572   1.1       cgd 
    573  1.64  christos 	el_free(path);
    574  1.19     lukem 	(void) fclose(fp);
    575  1.68  christos 	return error;
    576   1.1       cgd }
    577   1.1       cgd 
    578   1.1       cgd 
    579   1.1       cgd /* el_resize():
    580   1.1       cgd  *	Called from program when terminal is resized
    581   1.1       cgd  */
    582  1.88  christos void
    583  1.19     lukem el_resize(EditLine *el)
    584   1.1       cgd {
    585  1.19     lukem 	int lins, cols;
    586  1.19     lukem 	sigset_t oset, nset;
    587  1.19     lukem 
    588  1.19     lukem 	(void) sigemptyset(&nset);
    589  1.19     lukem 	(void) sigaddset(&nset, SIGWINCH);
    590  1.19     lukem 	(void) sigprocmask(SIG_BLOCK, &nset, &oset);
    591  1.19     lukem 
    592  1.19     lukem 	/* get the correct window size */
    593  1.64  christos 	if (terminal_get_size(el, &lins, &cols))
    594  1.64  christos 		terminal_change_size(el, lins, cols);
    595   1.1       cgd 
    596  1.19     lukem 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
    597   1.9  christos }
    598  1.14     lukem 
    599   1.9  christos 
    600   1.9  christos /* el_beep():
    601   1.9  christos  *	Called from the program to beep
    602   1.9  christos  */
    603  1.88  christos void
    604  1.19     lukem el_beep(EditLine *el)
    605   1.9  christos {
    606  1.19     lukem 
    607  1.64  christos 	terminal_beep(el);
    608   1.1       cgd }
    609  1.10     lukem 
    610  1.10     lukem 
    611  1.10     lukem /* el_editmode()
    612  1.10     lukem  *	Set the state of EDIT_DISABLED from the `edit' command.
    613  1.10     lukem  */
    614  1.10     lukem protected int
    615  1.10     lukem /*ARGSUSED*/
    616  1.87  christos el_editmode(EditLine *el, int argc, const wchar_t **argv)
    617  1.10     lukem {
    618  1.87  christos 	const wchar_t *how;
    619  1.10     lukem 
    620  1.19     lukem 	if (argv == NULL || argc != 2 || argv[1] == NULL)
    621  1.68  christos 		return -1;
    622  1.10     lukem 
    623  1.19     lukem 	how = argv[1];
    624  1.86  christos 	if (wcscmp(how, L"on") == 0) {
    625  1.19     lukem 		el->el_flags &= ~EDIT_DISABLED;
    626  1.39  christos 		tty_rawmode(el);
    627  1.86  christos 	} else if (wcscmp(how, L"off") == 0) {
    628  1.39  christos 		tty_cookedmode(el);
    629  1.19     lukem 		el->el_flags |= EDIT_DISABLED;
    630  1.39  christos 	}
    631  1.19     lukem 	else {
    632  1.85  christos 		(void) fprintf(el->el_errfile, "edit: Bad value `%ls'.\n",
    633  1.56  christos 		    how);
    634  1.68  christos 		return -1;
    635  1.19     lukem 	}
    636  1.68  christos 	return 0;
    637  1.19     lukem }
    638