Home | History | Annotate | Line # | Download | only in libedit
el.c revision 1.65
      1  1.65  christos /*	$NetBSD: el.c,v 1.65 2011/07/28 01:04:41 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.65  christos __RCSID("$NetBSD: el.c,v 1.65 2011/07/28 01:04:41 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.1       cgd #include <string.h>
     50   1.1       cgd #include <stdlib.h>
     51  1.19     lukem #include <stdarg.h>
     52  1.55  christos #include <ctype.h>
     53  1.56  christos #include <locale.h>
     54  1.59  christos #include <langinfo.h>
     55   1.1       cgd #include "el.h"
     56   1.1       cgd 
     57   1.1       cgd /* el_init():
     58   1.1       cgd  *	Initialize editline and set default parameters.
     59   1.1       cgd  */
     60   1.1       cgd public EditLine *
     61  1.19     lukem el_init(const char *prog, FILE *fin, FILE *fout, FILE *ferr)
     62   1.1       cgd {
     63  1.19     lukem 	EditLine *el = (EditLine *) el_malloc(sizeof(EditLine));
     64   1.1       cgd 
     65  1.19     lukem 	if (el == NULL)
     66  1.19     lukem 		return (NULL);
     67   1.1       cgd 
     68  1.19     lukem 	memset(el, 0, sizeof(EditLine));
     69   1.1       cgd 
     70  1.44  christos 	el->el_infile = fin;
     71  1.19     lukem 	el->el_outfile = fout;
     72  1.19     lukem 	el->el_errfile = ferr;
     73  1.44  christos 
     74  1.44  christos 	el->el_infd = fileno(fin);
     75  1.61  christos 	el->el_outfd = fileno(fout);
     76  1.61  christos 	el->el_errfd = fileno(ferr);
     77  1.44  christos 
     78  1.56  christos 	el->el_prog = Strdup(ct_decode_string(prog, &el->el_scratch));
     79  1.56  christos 	if (el->el_prog == NULL) {
     80  1.36  christos 		el_free(el);
     81  1.36  christos 		return NULL;
     82  1.36  christos 	}
     83  1.19     lukem 
     84  1.19     lukem 	/*
     85  1.19     lukem          * Initialize all the modules. Order is important!!!
     86  1.19     lukem          */
     87  1.19     lukem 	el->el_flags = 0;
     88  1.56  christos #ifdef WIDECHAR
     89  1.59  christos 	if (setlocale(LC_CTYPE, NULL) != NULL){
     90  1.59  christos 		if (strcmp(nl_langinfo(CODESET), "UTF-8") == 0)
     91  1.56  christos 			el->el_flags |= CHARSET_IS_UTF8;
     92  1.56  christos 	}
     93  1.56  christos #endif
     94  1.19     lukem 
     95  1.64  christos 	if (terminal_init(el) == -1) {
     96  1.36  christos 		el_free(el->el_prog);
     97  1.25  christos 		el_free(el);
     98  1.25  christos 		return NULL;
     99  1.25  christos 	}
    100  1.19     lukem 	(void) key_init(el);
    101  1.19     lukem 	(void) map_init(el);
    102  1.19     lukem 	if (tty_init(el) == -1)
    103  1.19     lukem 		el->el_flags |= NO_TTY;
    104  1.19     lukem 	(void) ch_init(el);
    105  1.19     lukem 	(void) search_init(el);
    106  1.19     lukem 	(void) hist_init(el);
    107  1.19     lukem 	(void) prompt_init(el);
    108  1.19     lukem 	(void) sig_init(el);
    109  1.23  christos 	(void) read_init(el);
    110   1.1       cgd 
    111  1.19     lukem 	return (el);
    112  1.19     lukem }
    113   1.1       cgd 
    114   1.1       cgd 
    115   1.1       cgd /* el_end():
    116   1.1       cgd  *	Clean up.
    117   1.1       cgd  */
    118   1.1       cgd public void
    119  1.19     lukem el_end(EditLine *el)
    120   1.1       cgd {
    121   1.1       cgd 
    122  1.19     lukem 	if (el == NULL)
    123  1.19     lukem 		return;
    124   1.1       cgd 
    125  1.19     lukem 	el_reset(el);
    126  1.19     lukem 
    127  1.64  christos 	terminal_end(el);
    128  1.19     lukem 	key_end(el);
    129  1.19     lukem 	map_end(el);
    130  1.19     lukem 	tty_end(el);
    131  1.19     lukem 	ch_end(el);
    132  1.19     lukem 	search_end(el);
    133  1.19     lukem 	hist_end(el);
    134  1.19     lukem 	prompt_end(el);
    135  1.19     lukem 	sig_end(el);
    136  1.19     lukem 
    137  1.19     lukem 	el_free((ptr_t) el->el_prog);
    138  1.56  christos #ifdef WIDECHAR
    139  1.56  christos 	el_free((ptr_t) el->el_scratch.cbuff);
    140  1.56  christos 	el_free((ptr_t) el->el_scratch.wbuff);
    141  1.56  christos 	el_free((ptr_t) el->el_lgcyconv.cbuff);
    142  1.56  christos 	el_free((ptr_t) el->el_lgcyconv.wbuff);
    143  1.56  christos #endif
    144  1.62    bouyer 	el_free((ptr_t) el);
    145  1.19     lukem }
    146   1.1       cgd 
    147   1.1       cgd 
    148   1.1       cgd /* el_reset():
    149   1.1       cgd  *	Reset the tty and the parser
    150   1.1       cgd  */
    151   1.1       cgd public void
    152  1.19     lukem el_reset(EditLine *el)
    153   1.1       cgd {
    154  1.19     lukem 
    155  1.19     lukem 	tty_cookedmode(el);
    156  1.40  christos 	ch_reset(el, 0);		/* XXX: Do we want that? */
    157   1.1       cgd }
    158   1.1       cgd 
    159   1.1       cgd 
    160   1.1       cgd /* el_set():
    161   1.1       cgd  *	set the editline parameters
    162   1.1       cgd  */
    163   1.1       cgd public int
    164  1.56  christos FUN(el,set)(EditLine *el, int op, ...)
    165   1.1       cgd {
    166  1.44  christos 	va_list ap;
    167  1.27  christos 	int rv = 0;
    168   1.1       cgd 
    169  1.19     lukem 	if (el == NULL)
    170  1.19     lukem 		return (-1);
    171  1.44  christos 	va_start(ap, op);
    172  1.22       wiz 
    173  1.19     lukem 	switch (op) {
    174  1.19     lukem 	case EL_PROMPT:
    175  1.51  christos 	case EL_RPROMPT: {
    176  1.51  christos 		el_pfunc_t p = va_arg(ap, el_pfunc_t);
    177  1.52  christos 
    178  1.56  christos 		rv = prompt_set(el, p, 0, op, 1);
    179  1.52  christos 		break;
    180  1.52  christos 	}
    181  1.52  christos 
    182  1.60  christos 	case EL_RESIZE: {
    183  1.60  christos 		el_zfunc_t p = va_arg(ap, el_zfunc_t);
    184  1.60  christos 		void *arg = va_arg(ap, void *);
    185  1.60  christos 		rv = ch_resizefun(el, p, arg);
    186  1.60  christos 		break;
    187  1.60  christos 	}
    188  1.60  christos 
    189  1.52  christos 	case EL_PROMPT_ESC:
    190  1.52  christos 	case EL_RPROMPT_ESC: {
    191  1.52  christos 		el_pfunc_t p = va_arg(ap, el_pfunc_t);
    192  1.56  christos 		int c = va_arg(ap, int);
    193  1.51  christos 
    194  1.56  christos 		rv = prompt_set(el, p, c, op, 1);
    195  1.50  christos 		break;
    196  1.51  christos 	}
    197  1.13    simonb 
    198  1.19     lukem 	case EL_TERMINAL:
    199  1.64  christos 		rv = terminal_set(el, va_arg(ap, char *));
    200  1.19     lukem 		break;
    201   1.1       cgd 
    202  1.19     lukem 	case EL_EDITOR:
    203  1.56  christos 		rv = map_set_editor(el, va_arg(ap, Char *));
    204   1.1       cgd 		break;
    205   1.1       cgd 
    206  1.19     lukem 	case EL_SIGNAL:
    207  1.44  christos 		if (va_arg(ap, int))
    208  1.19     lukem 			el->el_flags |= HANDLE_SIGNALS;
    209  1.19     lukem 		else
    210  1.19     lukem 			el->el_flags &= ~HANDLE_SIGNALS;
    211   1.1       cgd 		break;
    212   1.1       cgd 
    213  1.19     lukem 	case EL_BIND:
    214  1.19     lukem 	case EL_TELLTC:
    215  1.19     lukem 	case EL_SETTC:
    216  1.19     lukem 	case EL_ECHOTC:
    217  1.19     lukem 	case EL_SETTY:
    218  1.19     lukem 	{
    219  1.56  christos 		const Char *argv[20];
    220  1.19     lukem 		int i;
    221  1.19     lukem 
    222  1.19     lukem 		for (i = 1; i < 20; i++)
    223  1.56  christos 			if ((argv[i] = va_arg(ap, Char *)) == NULL)
    224  1.19     lukem 				break;
    225  1.19     lukem 
    226  1.19     lukem 		switch (op) {
    227  1.19     lukem 		case EL_BIND:
    228  1.56  christos 			argv[0] = STR("bind");
    229  1.19     lukem 			rv = map_bind(el, i, argv);
    230  1.19     lukem 			break;
    231  1.19     lukem 
    232  1.19     lukem 		case EL_TELLTC:
    233  1.56  christos 			argv[0] = STR("telltc");
    234  1.64  christos 			rv = terminal_telltc(el, i, argv);
    235  1.19     lukem 			break;
    236  1.19     lukem 
    237  1.19     lukem 		case EL_SETTC:
    238  1.56  christos 			argv[0] = STR("settc");
    239  1.64  christos 			rv = terminal_settc(el, i, argv);
    240  1.19     lukem 			break;
    241  1.19     lukem 
    242  1.19     lukem 		case EL_ECHOTC:
    243  1.56  christos 			argv[0] = STR("echotc");
    244  1.64  christos 			rv = terminal_echotc(el, i, argv);
    245  1.19     lukem 			break;
    246  1.19     lukem 
    247  1.19     lukem 		case EL_SETTY:
    248  1.56  christos 			argv[0] = STR("setty");
    249  1.19     lukem 			rv = tty_stty(el, i, argv);
    250  1.19     lukem 			break;
    251  1.19     lukem 
    252  1.19     lukem 		default:
    253  1.19     lukem 			rv = -1;
    254  1.20  christos 			EL_ABORT((el->el_errfile, "Bad op %d\n", op));
    255  1.19     lukem 			break;
    256  1.19     lukem 		}
    257   1.1       cgd 		break;
    258  1.19     lukem 	}
    259  1.19     lukem 
    260  1.19     lukem 	case EL_ADDFN:
    261  1.19     lukem 	{
    262  1.56  christos 		Char *name = va_arg(ap, Char *);
    263  1.56  christos 		Char *help = va_arg(ap, Char *);
    264  1.44  christos 		el_func_t func = va_arg(ap, el_func_t);
    265   1.1       cgd 
    266  1.19     lukem 		rv = map_addfunc(el, name, help, func);
    267   1.1       cgd 		break;
    268  1.19     lukem 	}
    269  1.19     lukem 
    270  1.19     lukem 	case EL_HIST:
    271  1.19     lukem 	{
    272  1.44  christos 		hist_fun_t func = va_arg(ap, hist_fun_t);
    273  1.56  christos 		ptr_t ptr = va_arg(ap, ptr_t);
    274   1.1       cgd 
    275  1.19     lukem 		rv = hist_set(el, func, ptr);
    276  1.59  christos 		if (!(el->el_flags & CHARSET_IS_UTF8))
    277  1.59  christos 			el->el_flags &= ~NARROW_HISTORY;
    278   1.1       cgd 		break;
    279  1.19     lukem 	}
    280   1.1       cgd 
    281  1.19     lukem 	case EL_EDITMODE:
    282  1.44  christos 		if (va_arg(ap, int))
    283  1.19     lukem 			el->el_flags &= ~EDIT_DISABLED;
    284  1.19     lukem 		else
    285  1.19     lukem 			el->el_flags |= EDIT_DISABLED;
    286  1.19     lukem 		rv = 0;
    287   1.1       cgd 		break;
    288  1.13    simonb 
    289  1.23  christos 	case EL_GETCFN:
    290  1.23  christos 	{
    291  1.44  christos 		el_rfunc_t rc = va_arg(ap, el_rfunc_t);
    292  1.23  christos 		rv = el_read_setfn(el, rc);
    293  1.58  christos 		el->el_flags &= ~NARROW_READ;
    294  1.23  christos 		break;
    295  1.23  christos 	}
    296  1.23  christos 
    297  1.24  christos 	case EL_CLIENTDATA:
    298  1.44  christos 		el->el_data = va_arg(ap, void *);
    299  1.24  christos 		break;
    300  1.24  christos 
    301  1.34  christos 	case EL_UNBUFFERED:
    302  1.44  christos 		rv = va_arg(ap, int);
    303  1.34  christos 		if (rv && !(el->el_flags & UNBUFFERED)) {
    304  1.34  christos 			el->el_flags |= UNBUFFERED;
    305  1.34  christos 			read_prepare(el);
    306  1.34  christos 		} else if (!rv && (el->el_flags & UNBUFFERED)) {
    307  1.34  christos 			el->el_flags &= ~UNBUFFERED;
    308  1.34  christos 			read_finish(el);
    309  1.34  christos 		}
    310  1.35  christos 		rv = 0;
    311  1.35  christos 		break;
    312  1.35  christos 
    313  1.35  christos 	case EL_PREP_TERM:
    314  1.44  christos 		rv = va_arg(ap, int);
    315  1.35  christos 		if (rv)
    316  1.38  christos 			(void) tty_rawmode(el);
    317  1.35  christos 		else
    318  1.38  christos 			(void) tty_cookedmode(el);
    319  1.34  christos 		rv = 0;
    320  1.34  christos 		break;
    321  1.34  christos 
    322  1.44  christos 	case EL_SETFP:
    323  1.44  christos 	{
    324  1.44  christos 		FILE *fp;
    325  1.44  christos 		int what;
    326  1.44  christos 
    327  1.44  christos 		what = va_arg(ap, int);
    328  1.44  christos 		fp = va_arg(ap, FILE *);
    329  1.44  christos 
    330  1.44  christos 		rv = 0;
    331  1.44  christos 		switch (what) {
    332  1.44  christos 		case 0:
    333  1.44  christos 			el->el_infile = fp;
    334  1.44  christos 			el->el_infd = fileno(fp);
    335  1.44  christos 			break;
    336  1.44  christos 		case 1:
    337  1.44  christos 			el->el_outfile = fp;
    338  1.61  christos 			el->el_outfd = fileno(fp);
    339  1.44  christos 			break;
    340  1.44  christos 		case 2:
    341  1.44  christos 			el->el_errfile = fp;
    342  1.61  christos 			el->el_errfd = fileno(fp);
    343  1.44  christos 			break;
    344  1.44  christos 		default:
    345  1.44  christos 			rv = -1;
    346  1.44  christos 			break;
    347  1.44  christos 		}
    348  1.44  christos 		break;
    349  1.44  christos 	}
    350  1.44  christos 
    351  1.45  christos 	case EL_REFRESH:
    352  1.45  christos 		re_clear_display(el);
    353  1.45  christos 		re_refresh(el);
    354  1.64  christos 		terminal__flush(el);
    355  1.45  christos 		break;
    356  1.45  christos 
    357  1.19     lukem 	default:
    358  1.19     lukem 		rv = -1;
    359  1.27  christos 		break;
    360   1.1       cgd 	}
    361   1.1       cgd 
    362  1.44  christos 	va_end(ap);
    363  1.19     lukem 	return (rv);
    364  1.19     lukem }
    365   1.1       cgd 
    366   1.1       cgd 
    367  1.10     lukem /* el_get():
    368  1.10     lukem  *	retrieve the editline parameters
    369  1.10     lukem  */
    370  1.10     lukem public int
    371  1.56  christos FUN(el,get)(EditLine *el, int op, ...)
    372  1.10     lukem {
    373  1.42  christos 	va_list ap;
    374  1.19     lukem 	int rv;
    375  1.10     lukem 
    376  1.42  christos 	if (el == NULL)
    377  1.42  christos 		return -1;
    378  1.42  christos 
    379  1.42  christos 	va_start(ap, op);
    380  1.42  christos 
    381  1.19     lukem 	switch (op) {
    382  1.19     lukem 	case EL_PROMPT:
    383  1.51  christos 	case EL_RPROMPT: {
    384  1.51  christos 		el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
    385  1.56  christos 		rv = prompt_get(el, p, 0, op);
    386  1.56  christos 		break;
    387  1.56  christos 	}
    388  1.56  christos 	case EL_PROMPT_ESC:
    389  1.56  christos 	case EL_RPROMPT_ESC: {
    390  1.56  christos 		el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
    391  1.56  christos 		Char *c = va_arg(ap, Char *);
    392  1.51  christos 
    393  1.51  christos 		rv = prompt_get(el, p, c, op);
    394  1.19     lukem 		break;
    395  1.51  christos 	}
    396  1.10     lukem 
    397  1.19     lukem 	case EL_EDITOR:
    398  1.56  christos 		rv = map_get_editor(el, va_arg(ap, const Char **));
    399  1.10     lukem 		break;
    400  1.10     lukem 
    401  1.19     lukem 	case EL_SIGNAL:
    402  1.42  christos 		*va_arg(ap, int *) = (el->el_flags & HANDLE_SIGNALS);
    403  1.19     lukem 		rv = 0;
    404  1.10     lukem 		break;
    405  1.10     lukem 
    406  1.19     lukem 	case EL_EDITMODE:
    407  1.42  christos 		*va_arg(ap, int *) = !(el->el_flags & EDIT_DISABLED);
    408  1.19     lukem 		rv = 0;
    409  1.10     lukem 		break;
    410  1.10     lukem 
    411  1.19     lukem 	case EL_TERMINAL:
    412  1.64  christos 		terminal_get(el, va_arg(ap, const char **));
    413  1.33  christos 		rv = 0;
    414  1.10     lukem 		break;
    415  1.10     lukem 
    416  1.42  christos 	case EL_GETTC:
    417  1.19     lukem 	{
    418  1.42  christos 		static char name[] = "gettc";
    419  1.42  christos 		char *argv[20];
    420  1.19     lukem 		int i;
    421  1.10     lukem 
    422  1.47     lukem  		for (i = 1; i < (int)(sizeof(argv) / sizeof(argv[0])); i++)
    423  1.42  christos 			if ((argv[i] = va_arg(ap, char *)) == NULL)
    424  1.19     lukem 				break;
    425  1.19     lukem 
    426  1.19     lukem 		switch (op) {
    427  1.42  christos 		case EL_GETTC:
    428  1.42  christos 			argv[0] = name;
    429  1.64  christos 			rv = terminal_gettc(el, i, argv);
    430  1.19     lukem 			break;
    431  1.19     lukem 
    432  1.19     lukem 		default:
    433  1.19     lukem 			rv = -1;
    434  1.43     freza 			EL_ABORT((el->el_errfile, "Bad op %d\n", op));
    435  1.19     lukem 			break;
    436  1.19     lukem 		}
    437  1.10     lukem 		break;
    438  1.10     lukem 	}
    439  1.13    simonb 
    440  1.23  christos 	case EL_GETCFN:
    441  1.42  christos 		*va_arg(ap, el_rfunc_t *) = el_read_getfn(el);
    442  1.24  christos 		rv = 0;
    443  1.24  christos 		break;
    444  1.24  christos 
    445  1.24  christos 	case EL_CLIENTDATA:
    446  1.42  christos 		*va_arg(ap, void **) = el->el_data;
    447  1.34  christos 		rv = 0;
    448  1.34  christos 		break;
    449  1.34  christos 
    450  1.34  christos 	case EL_UNBUFFERED:
    451  1.42  christos 		*va_arg(ap, int *) = (!(el->el_flags & UNBUFFERED));
    452  1.23  christos 		rv = 0;
    453  1.23  christos 		break;
    454  1.19     lukem 
    455  1.44  christos 	case EL_GETFP:
    456  1.44  christos 	{
    457  1.44  christos 		int what;
    458  1.44  christos 		FILE **fpp;
    459  1.44  christos 
    460  1.44  christos 		what = va_arg(ap, int);
    461  1.44  christos 		fpp = va_arg(ap, FILE **);
    462  1.44  christos 		rv = 0;
    463  1.44  christos 		switch (what) {
    464  1.44  christos 		case 0:
    465  1.44  christos 			*fpp = el->el_infile;
    466  1.44  christos 			break;
    467  1.44  christos 		case 1:
    468  1.44  christos 			*fpp = el->el_outfile;
    469  1.44  christos 			break;
    470  1.44  christos 		case 2:
    471  1.44  christos 			*fpp = el->el_errfile;
    472  1.44  christos 			break;
    473  1.44  christos 		default:
    474  1.44  christos 			rv = -1;
    475  1.44  christos 			break;
    476  1.44  christos 		}
    477  1.44  christos 		break;
    478  1.44  christos 	}
    479  1.19     lukem 	default:
    480  1.19     lukem 		rv = -1;
    481  1.44  christos 		break;
    482  1.19     lukem 	}
    483  1.42  christos 	va_end(ap);
    484  1.10     lukem 
    485  1.19     lukem 	return (rv);
    486  1.19     lukem }
    487  1.10     lukem 
    488  1.10     lukem 
    489   1.1       cgd /* el_line():
    490   1.1       cgd  *	Return editing info
    491   1.1       cgd  */
    492  1.56  christos public const TYPE(LineInfo) *
    493  1.56  christos FUN(el,line)(EditLine *el)
    494   1.1       cgd {
    495  1.19     lukem 
    496  1.56  christos 	return (const TYPE(LineInfo) *) (void *) &el->el_line;
    497   1.1       cgd }
    498   1.1       cgd 
    499   1.1       cgd 
    500   1.1       cgd /* el_source():
    501   1.1       cgd  *	Source a file
    502   1.1       cgd  */
    503   1.1       cgd public int
    504  1.19     lukem el_source(EditLine *el, const char *fname)
    505   1.1       cgd {
    506  1.19     lukem 	FILE *fp;
    507  1.19     lukem 	size_t len;
    508  1.27  christos 	char *ptr;
    509  1.64  christos 	char *path = NULL;
    510  1.56  christos 	const Char *dptr;
    511  1.64  christos 	int error = 0;
    512  1.19     lukem 
    513  1.19     lukem 	fp = NULL;
    514  1.19     lukem 	if (fname == NULL) {
    515  1.29  christos #ifdef HAVE_ISSETUGID
    516  1.27  christos 		static const char elpath[] = "/.editrc";
    517  1.65  christos 		size_t plen = sizeof(elpath);
    518  1.27  christos 
    519  1.19     lukem 		if (issetugid())
    520  1.19     lukem 			return (-1);
    521  1.19     lukem 		if ((ptr = getenv("HOME")) == NULL)
    522  1.19     lukem 			return (-1);
    523  1.65  christos 		plen += strlen(ptr);
    524  1.65  christos 		if ((path = malloc(plen)) == NULL)
    525  1.19     lukem 			return (-1);
    526  1.65  christos 		(void)snprintf(path, plen, "%s%s", ptr, elpath);
    527  1.19     lukem 		fname = path;
    528  1.27  christos #else
    529  1.27  christos 		/*
    530  1.27  christos 		 * If issetugid() is missing, always return an error, in order
    531  1.27  christos 		 * to keep from inadvertently opening up the user to a security
    532  1.27  christos 		 * hole.
    533  1.27  christos 		 */
    534  1.27  christos 		return (-1);
    535  1.27  christos #endif
    536  1.19     lukem 	}
    537  1.19     lukem 	if (fp == NULL)
    538  1.19     lukem 		fp = fopen(fname, "r");
    539  1.64  christos 	if (fp == NULL) {
    540  1.64  christos 		el_free(path);
    541  1.19     lukem 		return (-1);
    542  1.64  christos 	}
    543  1.19     lukem 
    544  1.19     lukem 	while ((ptr = fgetln(fp, &len)) != NULL) {
    545  1.63  christos 		if (*ptr == '\n')
    546  1.63  christos 			continue;	/* Empty line. */
    547  1.56  christos 		dptr = ct_decode_string(ptr, &el->el_scratch);
    548  1.56  christos 		if (!dptr)
    549  1.56  christos 			continue;
    550  1.56  christos 		if (len > 0 && dptr[len - 1] == '\n')
    551  1.19     lukem 			--len;
    552  1.55  christos 
    553  1.55  christos 		/* loop until first non-space char or EOL */
    554  1.56  christos 		while (*dptr != '\0' && Isspace(*dptr))
    555  1.56  christos 			dptr++;
    556  1.56  christos 		if (*dptr == '#')
    557  1.55  christos 			continue;   /* ignore, this is a comment line */
    558  1.64  christos 		if ((error = parse_line(el, dptr)) == -1)
    559  1.64  christos 			break;
    560   1.1       cgd 	}
    561   1.1       cgd 
    562  1.64  christos 	el_free(path);
    563  1.19     lukem 	(void) fclose(fp);
    564  1.64  christos 	return (error);
    565   1.1       cgd }
    566   1.1       cgd 
    567   1.1       cgd 
    568   1.1       cgd /* el_resize():
    569   1.1       cgd  *	Called from program when terminal is resized
    570   1.1       cgd  */
    571   1.1       cgd public void
    572  1.19     lukem el_resize(EditLine *el)
    573   1.1       cgd {
    574  1.19     lukem 	int lins, cols;
    575  1.19     lukem 	sigset_t oset, nset;
    576  1.19     lukem 
    577  1.19     lukem 	(void) sigemptyset(&nset);
    578  1.19     lukem 	(void) sigaddset(&nset, SIGWINCH);
    579  1.19     lukem 	(void) sigprocmask(SIG_BLOCK, &nset, &oset);
    580  1.19     lukem 
    581  1.19     lukem 	/* get the correct window size */
    582  1.64  christos 	if (terminal_get_size(el, &lins, &cols))
    583  1.64  christos 		terminal_change_size(el, lins, cols);
    584   1.1       cgd 
    585  1.19     lukem 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
    586   1.9  christos }
    587  1.14     lukem 
    588   1.9  christos 
    589   1.9  christos /* el_beep():
    590   1.9  christos  *	Called from the program to beep
    591   1.9  christos  */
    592   1.9  christos public void
    593  1.19     lukem el_beep(EditLine *el)
    594   1.9  christos {
    595  1.19     lukem 
    596  1.64  christos 	terminal_beep(el);
    597   1.1       cgd }
    598  1.10     lukem 
    599  1.10     lukem 
    600  1.10     lukem /* el_editmode()
    601  1.10     lukem  *	Set the state of EDIT_DISABLED from the `edit' command.
    602  1.10     lukem  */
    603  1.10     lukem protected int
    604  1.10     lukem /*ARGSUSED*/
    605  1.56  christos el_editmode(EditLine *el, int argc, const Char **argv)
    606  1.10     lukem {
    607  1.56  christos 	const Char *how;
    608  1.10     lukem 
    609  1.19     lukem 	if (argv == NULL || argc != 2 || argv[1] == NULL)
    610  1.19     lukem 		return (-1);
    611  1.10     lukem 
    612  1.19     lukem 	how = argv[1];
    613  1.56  christos 	if (Strcmp(how, STR("on")) == 0) {
    614  1.19     lukem 		el->el_flags &= ~EDIT_DISABLED;
    615  1.39  christos 		tty_rawmode(el);
    616  1.56  christos 	} else if (Strcmp(how, STR("off")) == 0) {
    617  1.39  christos 		tty_cookedmode(el);
    618  1.19     lukem 		el->el_flags |= EDIT_DISABLED;
    619  1.39  christos 	}
    620  1.19     lukem 	else {
    621  1.56  christos 		(void) fprintf(el->el_errfile, "edit: Bad value `" FSTR "'.\n",
    622  1.56  christos 		    how);
    623  1.19     lukem 		return (-1);
    624  1.19     lukem 	}
    625  1.19     lukem 	return (0);
    626  1.19     lukem }
    627