Remove building with NOCRYPTO option
[minix.git] / lib / libedit / el.c
blob864193d5971938e4d78a2124dbfbb43a6b42b7fb
1 /* $NetBSD: el.c,v 1.73 2014/06/18 18:12:28 christos Exp $ */
3 /*-
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Christos Zoulas of Cornell University.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #include "config.h"
36 #if !defined(lint) && !defined(SCCSID)
37 #if 0
38 static char sccsid[] = "@(#)el.c 8.2 (Berkeley) 1/3/94";
39 #else
40 __RCSID("$NetBSD: el.c,v 1.73 2014/06/18 18:12:28 christos Exp $");
41 #endif
42 #endif /* not lint && not SCCSID */
45 * el.c: EditLine interface functions
47 #include <sys/types.h>
48 #include <sys/param.h>
49 #include <string.h>
50 #include <stdlib.h>
51 #include <stdarg.h>
52 #include <ctype.h>
53 #include <locale.h>
54 #include <langinfo.h>
55 #include "el.h"
57 /* el_init():
58 * Initialize editline and set default parameters.
60 public EditLine *
61 el_init(const char *prog, FILE *fin, FILE *fout, FILE *ferr)
63 return el_init_fd(prog, fin, fout, ferr, fileno(fin), fileno(fout),
64 fileno(ferr));
67 public EditLine *
68 el_init_fd(const char *prog, FILE *fin, FILE *fout, FILE *ferr,
69 int fdin, int fdout, int fderr)
71 EditLine *el = el_malloc(sizeof(*el));
73 if (el == NULL)
74 return NULL;
76 memset(el, 0, sizeof(EditLine));
78 el->el_infile = fin;
79 el->el_outfile = fout;
80 el->el_errfile = ferr;
82 el->el_infd = fdin;
83 el->el_outfd = fdout;
84 el->el_errfd = fderr;
86 el->el_prog = Strdup(ct_decode_string(prog, &el->el_scratch));
87 if (el->el_prog == NULL) {
88 el_free(el);
89 return NULL;
93 * Initialize all the modules. Order is important!!!
95 el->el_flags = 0;
96 #ifdef WIDECHAR
97 if (setlocale(LC_CTYPE, NULL) != NULL){
98 if (strcmp(nl_langinfo(CODESET), "UTF-8") == 0)
99 el->el_flags |= CHARSET_IS_UTF8;
101 #endif
103 if (terminal_init(el) == -1) {
104 el_free(el->el_prog);
105 el_free(el);
106 return NULL;
108 (void) keymacro_init(el);
109 (void) map_init(el);
110 if (tty_init(el) == -1)
111 el->el_flags |= NO_TTY;
112 (void) ch_init(el);
113 (void) search_init(el);
114 (void) hist_init(el);
115 (void) prompt_init(el);
116 (void) sig_init(el);
117 (void) read_init(el);
119 return el;
123 /* el_end():
124 * Clean up.
126 public void
127 el_end(EditLine *el)
130 if (el == NULL)
131 return;
133 el_reset(el);
135 terminal_end(el);
136 keymacro_end(el);
137 map_end(el);
138 tty_end(el);
139 ch_end(el);
140 search_end(el);
141 hist_end(el);
142 prompt_end(el);
143 sig_end(el);
145 el_free(el->el_prog);
146 #ifdef WIDECHAR
147 el_free(el->el_scratch.cbuff);
148 el_free(el->el_scratch.wbuff);
149 el_free(el->el_lgcyconv.cbuff);
150 el_free(el->el_lgcyconv.wbuff);
151 #endif
152 el_free(el);
156 /* el_reset():
157 * Reset the tty and the parser
159 public void
160 el_reset(EditLine *el)
163 tty_cookedmode(el);
164 ch_reset(el, 0); /* XXX: Do we want that? */
168 /* el_set():
169 * set the editline parameters
171 public int
172 FUN(el,set)(EditLine *el, int op, ...)
174 va_list ap;
175 int rv = 0;
177 if (el == NULL)
178 return -1;
179 va_start(ap, op);
181 switch (op) {
182 case EL_PROMPT:
183 case EL_RPROMPT: {
184 el_pfunc_t p = va_arg(ap, el_pfunc_t);
186 rv = prompt_set(el, p, 0, op, 1);
187 break;
190 case EL_RESIZE: {
191 el_zfunc_t p = va_arg(ap, el_zfunc_t);
192 void *arg = va_arg(ap, void *);
193 rv = ch_resizefun(el, p, arg);
194 break;
197 case EL_ALIAS_TEXT: {
198 el_afunc_t p = va_arg(ap, el_afunc_t);
199 void *arg = va_arg(ap, void *);
200 rv = ch_aliasfun(el, p, arg);
201 break;
204 case EL_PROMPT_ESC:
205 case EL_RPROMPT_ESC: {
206 el_pfunc_t p = va_arg(ap, el_pfunc_t);
207 int c = va_arg(ap, int);
209 rv = prompt_set(el, p, c, op, 1);
210 break;
213 case EL_TERMINAL:
214 rv = terminal_set(el, va_arg(ap, char *));
215 break;
217 case EL_EDITOR:
218 rv = map_set_editor(el, va_arg(ap, Char *));
219 break;
221 case EL_SIGNAL:
222 if (va_arg(ap, int))
223 el->el_flags |= HANDLE_SIGNALS;
224 else
225 el->el_flags &= ~HANDLE_SIGNALS;
226 break;
228 case EL_BIND:
229 case EL_TELLTC:
230 case EL_SETTC:
231 case EL_ECHOTC:
232 case EL_SETTY:
234 const Char *argv[20];
235 int i;
237 for (i = 1; i < (int)__arraycount(argv); i++)
238 if ((argv[i] = va_arg(ap, Char *)) == NULL)
239 break;
241 switch (op) {
242 case EL_BIND:
243 argv[0] = STR("bind");
244 rv = map_bind(el, i, argv);
245 break;
247 case EL_TELLTC:
248 argv[0] = STR("telltc");
249 rv = terminal_telltc(el, i, argv);
250 break;
252 case EL_SETTC:
253 argv[0] = STR("settc");
254 rv = terminal_settc(el, i, argv);
255 break;
257 case EL_ECHOTC:
258 argv[0] = STR("echotc");
259 rv = terminal_echotc(el, i, argv);
260 break;
262 case EL_SETTY:
263 argv[0] = STR("setty");
264 rv = tty_stty(el, i, argv);
265 break;
267 default:
268 rv = -1;
269 EL_ABORT((el->el_errfile, "Bad op %d\n", op));
270 break;
272 break;
275 case EL_ADDFN:
277 Char *name = va_arg(ap, Char *);
278 Char *help = va_arg(ap, Char *);
279 el_func_t func = va_arg(ap, el_func_t);
281 rv = map_addfunc(el, name, help, func);
282 break;
285 case EL_HIST:
287 hist_fun_t func = va_arg(ap, hist_fun_t);
288 void *ptr = va_arg(ap, void *);
290 rv = hist_set(el, func, ptr);
291 if (!(el->el_flags & CHARSET_IS_UTF8))
292 el->el_flags &= ~NARROW_HISTORY;
293 break;
296 case EL_EDITMODE:
297 if (va_arg(ap, int))
298 el->el_flags &= ~EDIT_DISABLED;
299 else
300 el->el_flags |= EDIT_DISABLED;
301 rv = 0;
302 break;
304 case EL_GETCFN:
306 el_rfunc_t rc = va_arg(ap, el_rfunc_t);
307 rv = el_read_setfn(el, rc);
308 el->el_flags &= ~NARROW_READ;
309 break;
312 case EL_CLIENTDATA:
313 el->el_data = va_arg(ap, void *);
314 break;
316 case EL_UNBUFFERED:
317 rv = va_arg(ap, int);
318 if (rv && !(el->el_flags & UNBUFFERED)) {
319 el->el_flags |= UNBUFFERED;
320 read_prepare(el);
321 } else if (!rv && (el->el_flags & UNBUFFERED)) {
322 el->el_flags &= ~UNBUFFERED;
323 read_finish(el);
325 rv = 0;
326 break;
328 case EL_PREP_TERM:
329 rv = va_arg(ap, int);
330 if (rv)
331 (void) tty_rawmode(el);
332 else
333 (void) tty_cookedmode(el);
334 rv = 0;
335 break;
337 case EL_SETFP:
339 FILE *fp;
340 int what;
342 what = va_arg(ap, int);
343 fp = va_arg(ap, FILE *);
345 rv = 0;
346 switch (what) {
347 case 0:
348 el->el_infile = fp;
349 el->el_infd = fileno(fp);
350 break;
351 case 1:
352 el->el_outfile = fp;
353 el->el_outfd = fileno(fp);
354 break;
355 case 2:
356 el->el_errfile = fp;
357 el->el_errfd = fileno(fp);
358 break;
359 default:
360 rv = -1;
361 break;
363 break;
366 case EL_REFRESH:
367 re_clear_display(el);
368 re_refresh(el);
369 terminal__flush(el);
370 break;
372 default:
373 rv = -1;
374 break;
377 va_end(ap);
378 return rv;
382 /* el_get():
383 * retrieve the editline parameters
385 public int
386 FUN(el,get)(EditLine *el, int op, ...)
388 va_list ap;
389 int rv;
391 if (el == NULL)
392 return -1;
394 va_start(ap, op);
396 switch (op) {
397 case EL_PROMPT:
398 case EL_RPROMPT: {
399 el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
400 rv = prompt_get(el, p, 0, op);
401 break;
403 case EL_PROMPT_ESC:
404 case EL_RPROMPT_ESC: {
405 el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
406 Char *c = va_arg(ap, Char *);
408 rv = prompt_get(el, p, c, op);
409 break;
412 case EL_EDITOR:
413 rv = map_get_editor(el, va_arg(ap, const Char **));
414 break;
416 case EL_SIGNAL:
417 *va_arg(ap, int *) = (el->el_flags & HANDLE_SIGNALS);
418 rv = 0;
419 break;
421 case EL_EDITMODE:
422 *va_arg(ap, int *) = !(el->el_flags & EDIT_DISABLED);
423 rv = 0;
424 break;
426 case EL_TERMINAL:
427 terminal_get(el, va_arg(ap, const char **));
428 rv = 0;
429 break;
431 case EL_GETTC:
433 static char name[] = "gettc";
434 char *argv[20];
435 int i;
437 for (i = 1; i < (int)__arraycount(argv); i++)
438 if ((argv[i] = va_arg(ap, char *)) == NULL)
439 break;
441 argv[0] = name;
442 rv = terminal_gettc(el, i, argv);
443 break;
446 case EL_GETCFN:
447 *va_arg(ap, el_rfunc_t *) = el_read_getfn(el);
448 rv = 0;
449 break;
451 case EL_CLIENTDATA:
452 *va_arg(ap, void **) = el->el_data;
453 rv = 0;
454 break;
456 case EL_UNBUFFERED:
457 *va_arg(ap, int *) = (el->el_flags & UNBUFFERED) != 0;
458 rv = 0;
459 break;
461 case EL_GETFP:
463 int what;
464 FILE **fpp;
466 what = va_arg(ap, int);
467 fpp = va_arg(ap, FILE **);
468 rv = 0;
469 switch (what) {
470 case 0:
471 *fpp = el->el_infile;
472 break;
473 case 1:
474 *fpp = el->el_outfile;
475 break;
476 case 2:
477 *fpp = el->el_errfile;
478 break;
479 default:
480 rv = -1;
481 break;
483 break;
485 default:
486 rv = -1;
487 break;
489 va_end(ap);
491 return rv;
495 /* el_line():
496 * Return editing info
498 public const TYPE(LineInfo) *
499 FUN(el,line)(EditLine *el)
502 return (const TYPE(LineInfo) *)(void *)&el->el_line;
506 /* el_source():
507 * Source a file
509 public int
510 el_source(EditLine *el, const char *fname)
512 FILE *fp;
513 size_t len;
514 char *ptr;
515 char *path = NULL;
516 const Char *dptr;
517 int error = 0;
519 fp = NULL;
520 if (fname == NULL) {
521 #ifdef HAVE_ISSETUGID
522 static const char elpath[] = "/.editrc";
523 size_t plen = sizeof(elpath);
525 if (issetugid())
526 return -1;
527 if ((ptr = getenv("HOME")) == NULL)
528 return -1;
529 plen += strlen(ptr);
530 if ((path = el_malloc(plen * sizeof(*path))) == NULL)
531 return -1;
532 (void)snprintf(path, plen, "%s%s", ptr, elpath);
533 fname = path;
534 #else
536 * If issetugid() is missing, always return an error, in order
537 * to keep from inadvertently opening up the user to a security
538 * hole.
540 return -1;
541 #endif
543 if (fp == NULL)
544 fp = fopen(fname, "r");
545 if (fp == NULL) {
546 el_free(path);
547 return -1;
550 while ((ptr = fgetln(fp, &len)) != NULL) {
551 if (*ptr == '\n')
552 continue; /* Empty line. */
553 dptr = ct_decode_string(ptr, &el->el_scratch);
554 if (!dptr)
555 continue;
556 if (len > 0 && dptr[len - 1] == '\n')
557 --len;
559 /* loop until first non-space char or EOL */
560 while (*dptr != '\0' && Isspace(*dptr))
561 dptr++;
562 if (*dptr == '#')
563 continue; /* ignore, this is a comment line */
564 if ((error = parse_line(el, dptr)) == -1)
565 break;
568 el_free(path);
569 (void) fclose(fp);
570 return error;
574 /* el_resize():
575 * Called from program when terminal is resized
577 public void
578 el_resize(EditLine *el)
580 int lins, cols;
581 sigset_t oset, nset;
583 (void) sigemptyset(&nset);
584 (void) sigaddset(&nset, SIGWINCH);
585 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
587 /* get the correct window size */
588 if (terminal_get_size(el, &lins, &cols))
589 terminal_change_size(el, lins, cols);
591 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
595 /* el_beep():
596 * Called from the program to beep
598 public void
599 el_beep(EditLine *el)
602 terminal_beep(el);
606 /* el_editmode()
607 * Set the state of EDIT_DISABLED from the `edit' command.
609 protected int
610 /*ARGSUSED*/
611 el_editmode(EditLine *el, int argc, const Char **argv)
613 const Char *how;
615 if (argv == NULL || argc != 2 || argv[1] == NULL)
616 return -1;
618 how = argv[1];
619 if (Strcmp(how, STR("on")) == 0) {
620 el->el_flags &= ~EDIT_DISABLED;
621 tty_rawmode(el);
622 } else if (Strcmp(how, STR("off")) == 0) {
623 tty_cookedmode(el);
624 el->el_flags |= EDIT_DISABLED;
626 else {
627 (void) fprintf(el->el_errfile, "edit: Bad value `" FSTR "'.\n",
628 how);
629 return -1;
631 return 0;