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