Thu Jul 28 00:49:18 2011 UTC ()
term -> terminal
eliminate MAXPATHLEN


(christos)
diff -r1.63 -r1.64 src/lib/libedit/el.c

cvs diff -r1.63 -r1.64 src/lib/libedit/el.c (expand / switch to unified diff)

--- src/lib/libedit/el.c 2011/07/26 21:03:17 1.63
+++ src/lib/libedit/el.c 2011/07/28 00:49:18 1.64
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: el.c,v 1.63 2011/07/26 21:03:17 christos Exp $ */ 1/* $NetBSD: el.c,v 1.64 2011/07/28 00:49:18 christos Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1992, 1993 4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to Berkeley by 7 * This code is derived from software contributed to Berkeley by
8 * Christos Zoulas of Cornell University. 8 * Christos Zoulas of Cornell University.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -27,27 +27,27 @@ @@ -27,27 +27,27 @@
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 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 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 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 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE. 32 * SUCH DAMAGE.
33 */ 33 */
34 34
35#include "config.h" 35#include "config.h"
36#if !defined(lint) && !defined(SCCSID) 36#if !defined(lint) && !defined(SCCSID)
37#if 0 37#if 0
38static char sccsid[] = "@(#)el.c 8.2 (Berkeley) 1/3/94"; 38static char sccsid[] = "@(#)el.c 8.2 (Berkeley) 1/3/94";
39#else 39#else
40__RCSID("$NetBSD: el.c,v 1.63 2011/07/26 21:03:17 christos Exp $"); 40__RCSID("$NetBSD: el.c,v 1.64 2011/07/28 00:49:18 christos Exp $");
41#endif 41#endif
42#endif /* not lint && not SCCSID */ 42#endif /* not lint && not SCCSID */
43 43
44/* 44/*
45 * el.c: EditLine interface functions 45 * el.c: EditLine interface functions
46 */ 46 */
47#include <sys/types.h> 47#include <sys/types.h>
48#include <sys/param.h> 48#include <sys/param.h>
49#include <string.h> 49#include <string.h>
50#include <stdlib.h> 50#include <stdlib.h>
51#include <stdarg.h> 51#include <stdarg.h>
52#include <ctype.h> 52#include <ctype.h>
53#include <locale.h> 53#include <locale.h>
@@ -82,27 +82,27 @@ el_init(const char *prog, FILE *fin, FIL @@ -82,27 +82,27 @@ el_init(const char *prog, FILE *fin, FIL
82 } 82 }
83 83
84 /* 84 /*
85 * Initialize all the modules. Order is important!!! 85 * Initialize all the modules. Order is important!!!
86 */ 86 */
87 el->el_flags = 0; 87 el->el_flags = 0;
88#ifdef WIDECHAR 88#ifdef WIDECHAR
89 if (setlocale(LC_CTYPE, NULL) != NULL){ 89 if (setlocale(LC_CTYPE, NULL) != NULL){
90 if (strcmp(nl_langinfo(CODESET), "UTF-8") == 0) 90 if (strcmp(nl_langinfo(CODESET), "UTF-8") == 0)
91 el->el_flags |= CHARSET_IS_UTF8; 91 el->el_flags |= CHARSET_IS_UTF8;
92 } 92 }
93#endif 93#endif
94 94
95 if (term_init(el) == -1) { 95 if (terminal_init(el) == -1) {
96 el_free(el->el_prog); 96 el_free(el->el_prog);
97 el_free(el); 97 el_free(el);
98 return NULL; 98 return NULL;
99 } 99 }
100 (void) key_init(el); 100 (void) key_init(el);
101 (void) map_init(el); 101 (void) map_init(el);
102 if (tty_init(el) == -1) 102 if (tty_init(el) == -1)
103 el->el_flags |= NO_TTY; 103 el->el_flags |= NO_TTY;
104 (void) ch_init(el); 104 (void) ch_init(el);
105 (void) search_init(el); 105 (void) search_init(el);
106 (void) hist_init(el); 106 (void) hist_init(el);
107 (void) prompt_init(el); 107 (void) prompt_init(el);
108 (void) sig_init(el); 108 (void) sig_init(el);
@@ -114,27 +114,27 @@ el_init(const char *prog, FILE *fin, FIL @@ -114,27 +114,27 @@ el_init(const char *prog, FILE *fin, FIL
114 114
115/* el_end(): 115/* el_end():
116 * Clean up. 116 * Clean up.
117 */ 117 */
118public void 118public void
119el_end(EditLine *el) 119el_end(EditLine *el)
120{ 120{
121 121
122 if (el == NULL) 122 if (el == NULL)
123 return; 123 return;
124 124
125 el_reset(el); 125 el_reset(el);
126 126
127 term_end(el); 127 terminal_end(el);
128 key_end(el); 128 key_end(el);
129 map_end(el); 129 map_end(el);
130 tty_end(el); 130 tty_end(el);
131 ch_end(el); 131 ch_end(el);
132 search_end(el); 132 search_end(el);
133 hist_end(el); 133 hist_end(el);
134 prompt_end(el); 134 prompt_end(el);
135 sig_end(el); 135 sig_end(el);
136 136
137 el_free((ptr_t) el->el_prog); 137 el_free((ptr_t) el->el_prog);
138#ifdef WIDECHAR 138#ifdef WIDECHAR
139 el_free((ptr_t) el->el_scratch.cbuff); 139 el_free((ptr_t) el->el_scratch.cbuff);
140 el_free((ptr_t) el->el_scratch.wbuff); 140 el_free((ptr_t) el->el_scratch.wbuff);
@@ -186,27 +186,27 @@ FUN(el,set)(EditLine *el, int op, ...) @@ -186,27 +186,27 @@ FUN(el,set)(EditLine *el, int op, ...)
186 break; 186 break;
187 } 187 }
188 188
189 case EL_PROMPT_ESC: 189 case EL_PROMPT_ESC:
190 case EL_RPROMPT_ESC: { 190 case EL_RPROMPT_ESC: {
191 el_pfunc_t p = va_arg(ap, el_pfunc_t); 191 el_pfunc_t p = va_arg(ap, el_pfunc_t);
192 int c = va_arg(ap, int); 192 int c = va_arg(ap, int);
193 193
194 rv = prompt_set(el, p, c, op, 1); 194 rv = prompt_set(el, p, c, op, 1);
195 break; 195 break;
196 } 196 }
197 197
198 case EL_TERMINAL: 198 case EL_TERMINAL:
199 rv = term_set(el, va_arg(ap, char *)); 199 rv = terminal_set(el, va_arg(ap, char *));
200 break; 200 break;
201 201
202 case EL_EDITOR: 202 case EL_EDITOR:
203 rv = map_set_editor(el, va_arg(ap, Char *)); 203 rv = map_set_editor(el, va_arg(ap, Char *));
204 break; 204 break;
205 205
206 case EL_SIGNAL: 206 case EL_SIGNAL:
207 if (va_arg(ap, int)) 207 if (va_arg(ap, int))
208 el->el_flags |= HANDLE_SIGNALS; 208 el->el_flags |= HANDLE_SIGNALS;
209 else 209 else
210 el->el_flags &= ~HANDLE_SIGNALS; 210 el->el_flags &= ~HANDLE_SIGNALS;
211 break; 211 break;
212 212
@@ -221,37 +221,37 @@ FUN(el,set)(EditLine *el, int op, ...) @@ -221,37 +221,37 @@ FUN(el,set)(EditLine *el, int op, ...)
221 221
222 for (i = 1; i < 20; i++) 222 for (i = 1; i < 20; i++)
223 if ((argv[i] = va_arg(ap, Char *)) == NULL) 223 if ((argv[i] = va_arg(ap, Char *)) == NULL)
224 break; 224 break;
225 225
226 switch (op) { 226 switch (op) {
227 case EL_BIND: 227 case EL_BIND:
228 argv[0] = STR("bind"); 228 argv[0] = STR("bind");
229 rv = map_bind(el, i, argv); 229 rv = map_bind(el, i, argv);
230 break; 230 break;
231 231
232 case EL_TELLTC: 232 case EL_TELLTC:
233 argv[0] = STR("telltc"); 233 argv[0] = STR("telltc");
234 rv = term_telltc(el, i, argv); 234 rv = terminal_telltc(el, i, argv);
235 break; 235 break;
236 236
237 case EL_SETTC: 237 case EL_SETTC:
238 argv[0] = STR("settc"); 238 argv[0] = STR("settc");
239 rv = term_settc(el, i, argv); 239 rv = terminal_settc(el, i, argv);
240 break; 240 break;
241 241
242 case EL_ECHOTC: 242 case EL_ECHOTC:
243 argv[0] = STR("echotc"); 243 argv[0] = STR("echotc");
244 rv = term_echotc(el, i, argv); 244 rv = terminal_echotc(el, i, argv);
245 break; 245 break;
246 246
247 case EL_SETTY: 247 case EL_SETTY:
248 argv[0] = STR("setty"); 248 argv[0] = STR("setty");
249 rv = tty_stty(el, i, argv); 249 rv = tty_stty(el, i, argv);
250 break; 250 break;
251 251
252 default: 252 default:
253 rv = -1; 253 rv = -1;
254 EL_ABORT((el->el_errfile, "Bad op %d\n", op)); 254 EL_ABORT((el->el_errfile, "Bad op %d\n", op));
255 break; 255 break;
256 } 256 }
257 break; 257 break;
@@ -341,27 +341,27 @@ FUN(el,set)(EditLine *el, int op, ...) @@ -341,27 +341,27 @@ FUN(el,set)(EditLine *el, int op, ...)
341 el->el_errfile = fp; 341 el->el_errfile = fp;
342 el->el_errfd = fileno(fp); 342 el->el_errfd = fileno(fp);
343 break; 343 break;
344 default: 344 default:
345 rv = -1; 345 rv = -1;
346 break; 346 break;
347 } 347 }
348 break; 348 break;
349 } 349 }
350 350
351 case EL_REFRESH: 351 case EL_REFRESH:
352 re_clear_display(el); 352 re_clear_display(el);
353 re_refresh(el); 353 re_refresh(el);
354 term__flush(el); 354 terminal__flush(el);
355 break; 355 break;
356 356
357 default: 357 default:
358 rv = -1; 358 rv = -1;
359 break; 359 break;
360 } 360 }
361 361
362 va_end(ap); 362 va_end(ap);
363 return (rv); 363 return (rv);
364} 364}
365 365
366 366
367/* el_get(): 367/* el_get():
@@ -399,44 +399,44 @@ FUN(el,get)(EditLine *el, int op, ...) @@ -399,44 +399,44 @@ FUN(el,get)(EditLine *el, int op, ...)
399 break; 399 break;
400 400
401 case EL_SIGNAL: 401 case EL_SIGNAL:
402 *va_arg(ap, int *) = (el->el_flags & HANDLE_SIGNALS); 402 *va_arg(ap, int *) = (el->el_flags & HANDLE_SIGNALS);
403 rv = 0; 403 rv = 0;
404 break; 404 break;
405 405
406 case EL_EDITMODE: 406 case EL_EDITMODE:
407 *va_arg(ap, int *) = !(el->el_flags & EDIT_DISABLED); 407 *va_arg(ap, int *) = !(el->el_flags & EDIT_DISABLED);
408 rv = 0; 408 rv = 0;
409 break; 409 break;
410 410
411 case EL_TERMINAL: 411 case EL_TERMINAL:
412 term_get(el, va_arg(ap, const char **)); 412 terminal_get(el, va_arg(ap, const char **));
413 rv = 0; 413 rv = 0;
414 break; 414 break;
415 415
416 case EL_GETTC: 416 case EL_GETTC:
417 { 417 {
418 static char name[] = "gettc"; 418 static char name[] = "gettc";
419 char *argv[20]; 419 char *argv[20];
420 int i; 420 int i;
421 421
422 for (i = 1; i < (int)(sizeof(argv) / sizeof(argv[0])); i++) 422 for (i = 1; i < (int)(sizeof(argv) / sizeof(argv[0])); i++)
423 if ((argv[i] = va_arg(ap, char *)) == NULL) 423 if ((argv[i] = va_arg(ap, char *)) == NULL)
424 break; 424 break;
425 425
426 switch (op) { 426 switch (op) {
427 case EL_GETTC: 427 case EL_GETTC:
428 argv[0] = name; 428 argv[0] = name;
429 rv = term_gettc(el, i, argv); 429 rv = terminal_gettc(el, i, argv);
430 break; 430 break;
431 431
432 default: 432 default:
433 rv = -1; 433 rv = -1;
434 EL_ABORT((el->el_errfile, "Bad op %d\n", op)); 434 EL_ABORT((el->el_errfile, "Bad op %d\n", op));
435 break; 435 break;
436 } 436 }
437 break; 437 break;
438 } 438 }
439 439
440 case EL_GETCFN: 440 case EL_GETCFN:
441 *va_arg(ap, el_rfunc_t *) = el_read_getfn(el); 441 *va_arg(ap, el_rfunc_t *) = el_read_getfn(el);
442 rv = 0; 442 rv = 0;
@@ -496,113 +496,114 @@ FUN(el,line)(EditLine *el) @@ -496,113 +496,114 @@ FUN(el,line)(EditLine *el)
496 return (const TYPE(LineInfo) *) (void *) &el->el_line; 496 return (const TYPE(LineInfo) *) (void *) &el->el_line;
497} 497}
498 498
499 499
500/* el_source(): 500/* el_source():
501 * Source a file 501 * Source a file
502 */ 502 */
503public int 503public int
504el_source(EditLine *el, const char *fname) 504el_source(EditLine *el, const char *fname)
505{ 505{
506 FILE *fp; 506 FILE *fp;
507 size_t len; 507 size_t len;
508 char *ptr; 508 char *ptr;
509#ifdef HAVE_ISSETUGID 509 char *path = NULL;
510 char path[MAXPATHLEN]; 
511#endif 
512 const Char *dptr; 510 const Char *dptr;
 511 int error = 0;
513 512
514 fp = NULL; 513 fp = NULL;
515 if (fname == NULL) { 514 if (fname == NULL) {
516#ifdef HAVE_ISSETUGID 515#ifdef HAVE_ISSETUGID
517 static const char elpath[] = "/.editrc"; 516 static const char elpath[] = "/.editrc";
 517 size_t len = sizeof(elpath);
518 518
519 if (issetugid()) 519 if (issetugid())
520 return (-1); 520 return (-1);
521 if ((ptr = getenv("HOME")) == NULL) 521 if ((ptr = getenv("HOME")) == NULL)
522 return (-1); 522 return (-1);
523 if (strlcpy(path, ptr, sizeof(path)) >= sizeof(path)) 523 len += strlen(ptr);
524 return (-1); 524 if ((path = malloc(len)) == NULL)
525 if (strlcat(path, elpath, sizeof(path)) >= sizeof(path)) 
526 return (-1); 525 return (-1);
 526 (void)snprintf(path, len, "%s%s", ptr, elpath);
527 fname = path; 527 fname = path;
528#else 528#else
529 /* 529 /*
530 * If issetugid() is missing, always return an error, in order 530 * If issetugid() is missing, always return an error, in order
531 * to keep from inadvertently opening up the user to a security 531 * to keep from inadvertently opening up the user to a security
532 * hole. 532 * hole.
533 */ 533 */
534 return (-1); 534 return (-1);
535#endif 535#endif
536 } 536 }
537 if (fp == NULL) 537 if (fp == NULL)
538 fp = fopen(fname, "r"); 538 fp = fopen(fname, "r");
539 if (fp == NULL) 539 if (fp == NULL) {
 540 el_free(path);
540 return (-1); 541 return (-1);
 542 }
541 543
542 while ((ptr = fgetln(fp, &len)) != NULL) { 544 while ((ptr = fgetln(fp, &len)) != NULL) {
543 if (*ptr == '\n') 545 if (*ptr == '\n')
544 continue; /* Empty line. */ 546 continue; /* Empty line. */
545 dptr = ct_decode_string(ptr, &el->el_scratch); 547 dptr = ct_decode_string(ptr, &el->el_scratch);
546 if (!dptr) 548 if (!dptr)
547 continue; 549 continue;
548 if (len > 0 && dptr[len - 1] == '\n') 550 if (len > 0 && dptr[len - 1] == '\n')
549 --len; 551 --len;
550 552
551 /* loop until first non-space char or EOL */ 553 /* loop until first non-space char or EOL */
552 while (*dptr != '\0' && Isspace(*dptr)) 554 while (*dptr != '\0' && Isspace(*dptr))
553 dptr++; 555 dptr++;
554 if (*dptr == '#') 556 if (*dptr == '#')
555 continue; /* ignore, this is a comment line */ 557 continue; /* ignore, this is a comment line */
556 if (parse_line(el, dptr) == -1) { 558 if ((error = parse_line(el, dptr)) == -1)
557 (void) fclose(fp); 559 break;
558 return (-1); 
559 } 
560 } 560 }
561 561
 562 el_free(path);
562 (void) fclose(fp); 563 (void) fclose(fp);
563 return (0); 564 return (error);
564} 565}
565 566
566 567
567/* el_resize(): 568/* el_resize():
568 * Called from program when terminal is resized 569 * Called from program when terminal is resized
569 */ 570 */
570public void 571public void
571el_resize(EditLine *el) 572el_resize(EditLine *el)
572{ 573{
573 int lins, cols; 574 int lins, cols;
574 sigset_t oset, nset; 575 sigset_t oset, nset;
575 576
576 (void) sigemptyset(&nset); 577 (void) sigemptyset(&nset);
577 (void) sigaddset(&nset, SIGWINCH); 578 (void) sigaddset(&nset, SIGWINCH);
578 (void) sigprocmask(SIG_BLOCK, &nset, &oset); 579 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
579 580
580 /* get the correct window size */ 581 /* get the correct window size */
581 if (term_get_size(el, &lins, &cols)) 582 if (terminal_get_size(el, &lins, &cols))
582 term_change_size(el, lins, cols); 583 terminal_change_size(el, lins, cols);
583 584
584 (void) sigprocmask(SIG_SETMASK, &oset, NULL); 585 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
585} 586}
586 587
587 588
588/* el_beep(): 589/* el_beep():
589 * Called from the program to beep 590 * Called from the program to beep
590 */ 591 */
591public void 592public void
592el_beep(EditLine *el) 593el_beep(EditLine *el)
593{ 594{
594 595
595 term_beep(el); 596 terminal_beep(el);
596} 597}
597 598
598 599
599/* el_editmode() 600/* el_editmode()
600 * Set the state of EDIT_DISABLED from the `edit' command. 601 * Set the state of EDIT_DISABLED from the `edit' command.
601 */ 602 */
602protected int 603protected int
603/*ARGSUSED*/ 604/*ARGSUSED*/
604el_editmode(EditLine *el, int argc, const Char **argv) 605el_editmode(EditLine *el, int argc, const Char **argv)
605{ 606{
606 const Char *how; 607 const Char *how;
607 608
608 if (argv == NULL || argc != 2 || argv[1] == NULL) 609 if (argv == NULL || argc != 2 || argv[1] == NULL)