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