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