1 /* $NetBSD: terminal.c,v 1.14 2012/05/30 18:21:14 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Christos Zoulas of Cornell University. 9 * 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. 21 * 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. 33 */ 34 35 #include "config.h" 36 #if !defined(lint) && !defined(SCCSID) 37 #if 0 38 static char sccsid[] = "@(#)term.c 8.2 (Berkeley) 4/30/95"; 39 #else 40 __RCSID("$NetBSD: terminal.c,v 1.14 2012/05/30 18:21:14 christos Exp $"); 41 #endif 42 #endif /* not lint && not SCCSID */ 43 44 /* 45 * terminal.c: Editor/termcap-curses interface 46 * We have to declare a static variable here, since the 47 * termcap putchar routine does not take an argument! 48 */ 49 #include <stdio.h> 50 #include <signal.h> 51 #include <string.h> 52 #include <stdlib.h> 53 #include <unistd.h> 54 #include <limits.h> 55 #ifdef HAVE_TERMCAP_H 56 #include <termcap.h> 57 #endif 58 #ifdef HAVE_CURSES_H 59 #include <curses.h> 60 #elif HAVE_NCURSES_H 61 #include <ncurses.h> 62 #endif 63 64 /* Solaris's term.h does horrid things. */ 65 #if defined(HAVE_TERM_H) && !defined(__sun) && !defined(HAVE_TERMCAP_H) 66 #include <term.h> 67 #endif 68 69 #include <sys/types.h> 70 #include <sys/ioctl.h> 71 72 #ifdef _REENTRANT 73 #include <pthread.h> 74 #endif 75 76 #include "el.h" 77 78 /* 79 * IMPORTANT NOTE: these routines are allowed to look at the current screen 80 * and the current position assuming that it is correct. If this is not 81 * true, then the update will be WRONG! This is (should be) a valid 82 * assumption... 83 */ 84 85 #define TC_BUFSIZE ((size_t)2048) 86 87 #define GoodStr(a) (el->el_terminal.t_str[a] != NULL && \ 88 el->el_terminal.t_str[a][0] != '\0') 89 #define Str(a) el->el_terminal.t_str[a] 90 #define Val(a) el->el_terminal.t_val[a] 91 92 private const struct termcapstr { 93 const char *name; 94 const char *long_name; 95 } tstr[] = { 96 #define T_al 0 97 { "al", "add new blank line" }, 98 #define T_bl 1 99 { "bl", "audible bell" }, 100 #define T_cd 2 101 { "cd", "clear to bottom" }, 102 #define T_ce 3 103 { "ce", "clear to end of line" }, 104 #define T_ch 4 105 { "ch", "cursor to horiz pos" }, 106 #define T_cl 5 107 { "cl", "clear screen" }, 108 #define T_dc 6 109 { "dc", "delete a character" }, 110 #define T_dl 7 111 { "dl", "delete a line" }, 112 #define T_dm 8 113 { "dm", "start delete mode" }, 114 #define T_ed 9 115 { "ed", "end delete mode" }, 116 #define T_ei 10 117 { "ei", "end insert mode" }, 118 #define T_fs 11 119 { "fs", "cursor from status line" }, 120 #define T_ho 12 121 { "ho", "home cursor" }, 122 #define T_ic 13 123 { "ic", "insert character" }, 124 #define T_im 14 125 { "im", "start insert mode" }, 126 #define T_ip 15 127 { "ip", "insert padding" }, 128 #define T_kd 16 129 { "kd", "sends cursor down" }, 130 #define T_kl 17 131 { "kl", "sends cursor left" }, 132 #define T_kr 18 133 { "kr", "sends cursor right" }, 134 #define T_ku 19 135 { "ku", "sends cursor up" }, 136 #define T_md 20 137 { "md", "begin bold" }, 138 #define T_me 21 139 { "me", "end attributes" }, 140 #define T_nd 22 141 { "nd", "non destructive space" }, 142 #define T_se 23 143 { "se", "end standout" }, 144 #define T_so 24 145 { "so", "begin standout" }, 146 #define T_ts 25 147 { "ts", "cursor to status line" }, 148 #define T_up 26 149 { "up", "cursor up one" }, 150 #define T_us 27 151 { "us", "begin underline" }, 152 #define T_ue 28 153 { "ue", "end underline" }, 154 #define T_vb 29 155 { "vb", "visible bell" }, 156 #define T_DC 30 157 { "DC", "delete multiple chars" }, 158 #define T_DO 31 159 { "DO", "cursor down multiple" }, 160 #define T_IC 32 161 { "IC", "insert multiple chars" }, 162 #define T_LE 33 163 { "LE", "cursor left multiple" }, 164 #define T_RI 34 165 { "RI", "cursor right multiple" }, 166 #define T_UP 35 167 { "UP", "cursor up multiple" }, 168 #define T_kh 36 169 { "kh", "send cursor home" }, 170 #define T_at7 37 171 { "@7", "send cursor end" }, 172 #define T_kD 38 173 { "kD", "send cursor delete" }, 174 #define T_str 39 175 { NULL, NULL } 176 }; 177 178 private const struct termcapval { 179 const char *name; 180 const char *long_name; 181 } tval[] = { 182 #define T_am 0 183 { "am", "has automatic margins" }, 184 #define T_pt 1 185 { "pt", "has physical tabs" }, 186 #define T_li 2 187 { "li", "Number of lines" }, 188 #define T_co 3 189 { "co", "Number of columns" }, 190 #define T_km 4 191 { "km", "Has meta key" }, 192 #define T_xt 5 193 { "xt", "Tab chars destructive" }, 194 #define T_xn 6 195 { "xn", "newline ignored at right margin" }, 196 #define T_MT 7 197 { "MT", "Has meta key" }, /* XXX? */ 198 #define T_val 8 199 { NULL, NULL, } 200 }; 201 /* do two or more of the attributes use me */ 202 203 private void terminal_setflags(EditLine *); 204 private int terminal_rebuffer_display(EditLine *); 205 private void terminal_free_display(EditLine *); 206 private int terminal_alloc_display(EditLine *); 207 private void terminal_alloc(EditLine *, const struct termcapstr *, 208 const char *); 209 private void terminal_init_arrow(EditLine *); 210 private void terminal_reset_arrow(EditLine *); 211 private int terminal_putc(int); 212 private void terminal_tputs(EditLine *, const char *, int); 213 214 #ifdef _REENTRANT 215 private pthread_mutex_t terminal_mutex = PTHREAD_MUTEX_INITIALIZER; 216 #endif 217 private FILE *terminal_outfile = NULL; 218 219 220 /* terminal_setflags(): 221 * Set the terminal capability flags 222 */ 223 private void 224 terminal_setflags(EditLine *el) 225 { 226 EL_FLAGS = 0; 227 if (el->el_tty.t_tabs) 228 EL_FLAGS |= (Val(T_pt) && !Val(T_xt)) ? TERM_CAN_TAB : 0; 229 230 EL_FLAGS |= (Val(T_km) || Val(T_MT)) ? TERM_HAS_META : 0; 231 EL_FLAGS |= GoodStr(T_ce) ? TERM_CAN_CEOL : 0; 232 EL_FLAGS |= (GoodStr(T_dc) || GoodStr(T_DC)) ? TERM_CAN_DELETE : 0; 233 EL_FLAGS |= (GoodStr(T_im) || GoodStr(T_ic) || GoodStr(T_IC)) ? 234 TERM_CAN_INSERT : 0; 235 EL_FLAGS |= (GoodStr(T_up) || GoodStr(T_UP)) ? TERM_CAN_UP : 0; 236 EL_FLAGS |= Val(T_am) ? TERM_HAS_AUTO_MARGINS : 0; 237 EL_FLAGS |= Val(T_xn) ? TERM_HAS_MAGIC_MARGINS : 0; 238 239 if (GoodStr(T_me) && GoodStr(T_ue)) 240 EL_FLAGS |= (strcmp(Str(T_me), Str(T_ue)) == 0) ? 241 TERM_CAN_ME : 0; 242 else 243 EL_FLAGS &= ~TERM_CAN_ME; 244 if (GoodStr(T_me) && GoodStr(T_se)) 245 EL_FLAGS |= (strcmp(Str(T_me), Str(T_se)) == 0) ? 246 TERM_CAN_ME : 0; 247 248 249 #ifdef DEBUG_SCREEN 250 if (!EL_CAN_UP) { 251 (void) fprintf(el->el_errfile, 252 "WARNING: Your terminal cannot move up.\n"); 253 (void) fprintf(el->el_errfile, 254 "Editing may be odd for long lines.\n"); 255 } 256 if (!EL_CAN_CEOL) 257 (void) fprintf(el->el_errfile, "no clear EOL capability.\n"); 258 if (!EL_CAN_DELETE) 259 (void) fprintf(el->el_errfile, "no delete char capability.\n"); 260 if (!EL_CAN_INSERT) 261 (void) fprintf(el->el_errfile, "no insert char capability.\n"); 262 #endif /* DEBUG_SCREEN */ 263 } 264 265 /* terminal_init(): 266 * Initialize the terminal stuff 267 */ 268 protected int 269 terminal_init(EditLine *el) 270 { 271 272 el->el_terminal.t_buf = el_malloc(TC_BUFSIZE * 273 sizeof(*el->el_terminal.t_buf)); 274 if (el->el_terminal.t_buf == NULL) 275 return -1; 276 el->el_terminal.t_cap = el_malloc(TC_BUFSIZE * 277 sizeof(*el->el_terminal.t_cap)); 278 if (el->el_terminal.t_cap == NULL) 279 return -1; 280 el->el_terminal.t_fkey = el_malloc(A_K_NKEYS * 281 sizeof(*el->el_terminal.t_fkey)); 282 if (el->el_terminal.t_fkey == NULL) 283 return -1; 284 el->el_terminal.t_loc = 0; 285 el->el_terminal.t_str = el_malloc(T_str * 286 sizeof(*el->el_terminal.t_str)); 287 if (el->el_terminal.t_str == NULL) 288 return -1; 289 (void) memset(el->el_terminal.t_str, 0, T_str * 290 sizeof(*el->el_terminal.t_str)); 291 el->el_terminal.t_val = el_malloc(T_val * 292 sizeof(*el->el_terminal.t_val)); 293 if (el->el_terminal.t_val == NULL) 294 return -1; 295 (void) memset(el->el_terminal.t_val, 0, T_val * 296 sizeof(*el->el_terminal.t_val)); 297 (void) terminal_set(el, NULL); 298 terminal_init_arrow(el); 299 return 0; 300 } 301 302 /* terminal_end(): 303 * Clean up the terminal stuff 304 */ 305 protected void 306 terminal_end(EditLine *el) 307 { 308 309 el_free(el->el_terminal.t_buf); 310 el->el_terminal.t_buf = NULL; 311 el_free(el->el_terminal.t_cap); 312 el->el_terminal.t_cap = NULL; 313 el->el_terminal.t_loc = 0; 314 el_free(el->el_terminal.t_str); 315 el->el_terminal.t_str = NULL; 316 el_free(el->el_terminal.t_val); 317 el->el_terminal.t_val = NULL; 318 el_free(el->el_terminal.t_fkey); 319 el->el_terminal.t_fkey = NULL; 320 terminal_free_display(el); 321 } 322 323 324 /* terminal_alloc(): 325 * Maintain a string pool for termcap strings 326 */ 327 private void 328 terminal_alloc(EditLine *el, const struct termcapstr *t, const char *cap) 329 { 330 char termbuf[TC_BUFSIZE]; 331 size_t tlen, clen; 332 char **tlist = el->el_terminal.t_str; 333 char **tmp, **str = &tlist[t - tstr]; 334 335 (void) memset(termbuf, 0, sizeof(termbuf)); 336 if (cap == NULL || *cap == '\0') { 337 *str = NULL; 338 return; 339 } else 340 clen = strlen(cap); 341 342 tlen = *str == NULL ? 0 : strlen(*str); 343 344 /* 345 * New string is shorter; no need to allocate space 346 */ 347 if (clen <= tlen) { 348 if (*str) 349 (void) strcpy(*str, cap); /* XXX strcpy is safe */ 350 return; 351 } 352 /* 353 * New string is longer; see if we have enough space to append 354 */ 355 if (el->el_terminal.t_loc + 3 < TC_BUFSIZE) { 356 /* XXX strcpy is safe */ 357 (void) strcpy(*str = &el->el_terminal.t_buf[ 358 el->el_terminal.t_loc], cap); 359 el->el_terminal.t_loc += clen + 1; /* one for \0 */ 360 return; 361 } 362 /* 363 * Compact our buffer; no need to check compaction, cause we know it 364 * fits... 365 */ 366 tlen = 0; 367 for (tmp = tlist; tmp < &tlist[T_str]; tmp++) 368 if (*tmp != NULL && *tmp != '\0' && *tmp != *str) { 369 char *ptr; 370 371 for (ptr = *tmp; *ptr != '\0'; termbuf[tlen++] = *ptr++) 372 continue; 373 termbuf[tlen++] = '\0'; 374 } 375 memcpy(el->el_terminal.t_buf, termbuf, TC_BUFSIZE); 376 el->el_terminal.t_loc = tlen; 377 if (el->el_terminal.t_loc + 3 >= TC_BUFSIZE) { 378 (void) fprintf(el->el_errfile, 379 "Out of termcap string space.\n"); 380 return; 381 } 382 /* XXX strcpy is safe */ 383 (void) strcpy(*str = &el->el_terminal.t_buf[el->el_terminal.t_loc], 384 cap); 385 el->el_terminal.t_loc += (size_t)clen + 1; /* one for \0 */ 386 return; 387 } 388 389 390 /* terminal_rebuffer_display(): 391 * Rebuffer the display after the screen changed size 392 */ 393 private int 394 terminal_rebuffer_display(EditLine *el) 395 { 396 coord_t *c = &el->el_terminal.t_size; 397 398 terminal_free_display(el); 399 400 c->h = Val(T_co); 401 c->v = Val(T_li); 402 403 if (terminal_alloc_display(el) == -1) 404 return -1; 405 return 0; 406 } 407 408 409 /* terminal_alloc_display(): 410 * Allocate a new display. 411 */ 412 private int 413 terminal_alloc_display(EditLine *el) 414 { 415 int i; 416 Char **b; 417 coord_t *c = &el->el_terminal.t_size; 418 419 b = el_malloc(sizeof(*b) * (size_t)(c->v + 1)); 420 if (b == NULL) 421 return -1; 422 for (i = 0; i < c->v; i++) { 423 b[i] = el_malloc(sizeof(**b) * (size_t)(c->h + 1)); 424 if (b[i] == NULL) { 425 while (--i >= 0) 426 el_free(b[i]); 427 el_free(b); 428 return -1; 429 } 430 } 431 b[c->v] = NULL; 432 el->el_display = b; 433 434 b = el_malloc(sizeof(*b) * (size_t)(c->v + 1)); 435 if (b == NULL) 436 return -1; 437 for (i = 0; i < c->v; i++) { 438 b[i] = el_malloc(sizeof(**b) * (size_t)(c->h + 1)); 439 if (b[i] == NULL) { 440 while (--i >= 0) 441 el_free(b[i]); 442 el_free(b); 443 return -1; 444 } 445 } 446 b[c->v] = NULL; 447 el->el_vdisplay = b; 448 return 0; 449 } 450 451 452 /* terminal_free_display(): 453 * Free the display buffers 454 */ 455 private void 456 terminal_free_display(EditLine *el) 457 { 458 Char **b; 459 Char **bufp; 460 461 b = el->el_display; 462 el->el_display = NULL; 463 if (b != NULL) { 464 for (bufp = b; *bufp != NULL; bufp++) 465 el_free(*bufp); 466 el_free(b); 467 } 468 b = el->el_vdisplay; 469 el->el_vdisplay = NULL; 470 if (b != NULL) { 471 for (bufp = b; *bufp != NULL; bufp++) 472 el_free(*bufp); 473 el_free(b); 474 } 475 } 476 477 478 /* terminal_move_to_line(): 479 * move to line <where> (first line == 0) 480 * as efficiently as possible 481 */ 482 protected void 483 terminal_move_to_line(EditLine *el, int where) 484 { 485 int del; 486 487 if (where == el->el_cursor.v) 488 return; 489 490 if (where > el->el_terminal.t_size.v) { 491 #ifdef DEBUG_SCREEN 492 (void) fprintf(el->el_errfile, 493 "terminal_move_to_line: where is ridiculous: %d\r\n", 494 where); 495 #endif /* DEBUG_SCREEN */ 496 return; 497 } 498 if ((del = where - el->el_cursor.v) > 0) { 499 while (del > 0) { 500 if (EL_HAS_AUTO_MARGINS && 501 el->el_display[el->el_cursor.v][0] != '\0') { 502 size_t h = (size_t) 503 (el->el_terminal.t_size.h - 1); 504 #ifdef WIDECHAR 505 for (; h > 0 && 506 el->el_display[el->el_cursor.v][h] == 507 MB_FILL_CHAR; 508 h--) 509 continue; 510 #endif 511 /* move without newline */ 512 terminal_move_to_char(el, (int)h); 513 terminal_overwrite(el, &el->el_display 514 [el->el_cursor.v][el->el_cursor.h], 515 (size_t)(el->el_terminal.t_size.h - 516 el->el_cursor.h)); 517 /* updates Cursor */ 518 del--; 519 } else { 520 if ((del > 1) && GoodStr(T_DO)) { 521 terminal_tputs(el, tgoto(Str(T_DO), del, 522 del), del); 523 del = 0; 524 } else { 525 for (; del > 0; del--) 526 terminal__putc(el, '\n'); 527 /* because the \n will become \r\n */ 528 el->el_cursor.h = 0; 529 } 530 } 531 } 532 } else { /* del < 0 */ 533 if (GoodStr(T_UP) && (-del > 1 || !GoodStr(T_up))) 534 terminal_tputs(el, tgoto(Str(T_UP), -del, -del), -del); 535 else { 536 if (GoodStr(T_up)) 537 for (; del < 0; del++) 538 terminal_tputs(el, Str(T_up), 1); 539 } 540 } 541 el->el_cursor.v = where;/* now where is here */ 542 } 543 544 545 /* terminal_move_to_char(): 546 * Move to the character position specified 547 */ 548 protected void 549 terminal_move_to_char(EditLine *el, int where) 550 { 551 int del, i; 552 553 mc_again: 554 if (where == el->el_cursor.h) 555 return; 556 557 if (where > el->el_terminal.t_size.h) { 558 #ifdef DEBUG_SCREEN 559 (void) fprintf(el->el_errfile, 560 "terminal_move_to_char: where is riduculous: %d\r\n", 561 where); 562 #endif /* DEBUG_SCREEN */ 563 return; 564 } 565 if (!where) { /* if where is first column */ 566 terminal__putc(el, '\r'); /* do a CR */ 567 el->el_cursor.h = 0; 568 return; 569 } 570 del = where - el->el_cursor.h; 571 572 if ((del < -4 || del > 4) && GoodStr(T_ch)) 573 /* go there directly */ 574 terminal_tputs(el, tgoto(Str(T_ch), where, where), where); 575 else { 576 if (del > 0) { /* moving forward */ 577 if ((del > 4) && GoodStr(T_RI)) 578 terminal_tputs(el, tgoto(Str(T_RI), del, del), 579 del); 580 else { 581 /* if I can do tabs, use them */ 582 if (EL_CAN_TAB) { 583 if ((el->el_cursor.h & 0370) != 584 (where & ~0x7) 585 #ifdef WIDECHAR 586 && (el->el_display[ 587 el->el_cursor.v][where & 0370] != 588 MB_FILL_CHAR) 589 #endif 590 ) { 591 /* if not within tab stop */ 592 for (i = 593 (el->el_cursor.h & 0370); 594 i < (where & ~0x7); 595 i += 8) 596 terminal__putc(el, 597 '\t'); 598 /* then tab over */ 599 el->el_cursor.h = where & ~0x7; 600 } 601 } 602 /* 603 * it's usually cheaper to just write the 604 * chars, so we do. 605 */ 606 /* 607 * NOTE THAT terminal_overwrite() WILL CHANGE 608 * el->el_cursor.h!!! 609 */ 610 terminal_overwrite(el, &el->el_display[ 611 el->el_cursor.v][el->el_cursor.h], 612 (size_t)(where - el->el_cursor.h)); 613 614 } 615 } else { /* del < 0 := moving backward */ 616 if ((-del > 4) && GoodStr(T_LE)) 617 terminal_tputs(el, tgoto(Str(T_LE), -del, -del), 618 -del); 619 else { /* can't go directly there */ 620 /* 621 * if the "cost" is greater than the "cost" 622 * from col 0 623 */ 624 if (EL_CAN_TAB ? 625 ((unsigned int)-del > 626 (((unsigned int) where >> 3) + 627 (where & 07))) 628 : (-del > where)) { 629 terminal__putc(el, '\r');/* do a CR */ 630 el->el_cursor.h = 0; 631 goto mc_again; /* and try again */ 632 } 633 for (i = 0; i < -del; i++) 634 terminal__putc(el, '\b'); 635 } 636 } 637 } 638 el->el_cursor.h = where; /* now where is here */ 639 } 640 641 642 /* terminal_overwrite(): 643 * Overstrike num characters 644 * Assumes MB_FILL_CHARs are present to keep the column count correct 645 */ 646 protected void 647 terminal_overwrite(EditLine *el, const Char *cp, size_t n) 648 { 649 if (n == 0) 650 return; 651 652 if (n > (size_t)el->el_terminal.t_size.h) { 653 #ifdef DEBUG_SCREEN 654 (void) fprintf(el->el_errfile, 655 "terminal_overwrite: n is riduculous: %d\r\n", n); 656 #endif /* DEBUG_SCREEN */ 657 return; 658 } 659 660 do { 661 /* terminal__putc() ignores any MB_FILL_CHARs */ 662 terminal__putc(el, *cp++); 663 el->el_cursor.h++; 664 } while (--n); 665 666 if (el->el_cursor.h >= el->el_terminal.t_size.h) { /* wrap? */ 667 if (EL_HAS_AUTO_MARGINS) { /* yes */ 668 el->el_cursor.h = 0; 669 el->el_cursor.v++; 670 if (EL_HAS_MAGIC_MARGINS) { 671 /* force the wrap to avoid the "magic" 672 * situation */ 673 Char c; 674 if ((c = el->el_display[el->el_cursor.v] 675 [el->el_cursor.h]) != '\0') { 676 terminal_overwrite(el, &c, (size_t)1); 677 #ifdef WIDECHAR 678 while (el->el_display[el->el_cursor.v] 679 [el->el_cursor.h] == MB_FILL_CHAR) 680 el->el_cursor.h++; 681 #endif 682 } else { 683 terminal__putc(el, ' '); 684 el->el_cursor.h = 1; 685 } 686 } 687 } else /* no wrap, but cursor stays on screen */ 688 el->el_cursor.h = el->el_terminal.t_size.h - 1; 689 } 690 } 691 692 693 /* terminal_deletechars(): 694 * Delete num characters 695 */ 696 protected void 697 terminal_deletechars(EditLine *el, int num) 698 { 699 if (num <= 0) 700 return; 701 702 if (!EL_CAN_DELETE) { 703 #ifdef DEBUG_EDIT 704 (void) fprintf(el->el_errfile, " ERROR: cannot delete \n"); 705 #endif /* DEBUG_EDIT */ 706 return; 707 } 708 if (num > el->el_terminal.t_size.h) { 709 #ifdef DEBUG_SCREEN 710 (void) fprintf(el->el_errfile, 711 "terminal_deletechars: num is riduculous: %d\r\n", num); 712 #endif /* DEBUG_SCREEN */ 713 return; 714 } 715 if (GoodStr(T_DC)) /* if I have multiple delete */ 716 if ((num > 1) || !GoodStr(T_dc)) { /* if dc would be more 717 * expen. */ 718 terminal_tputs(el, tgoto(Str(T_DC), num, num), num); 719 return; 720 } 721 if (GoodStr(T_dm)) /* if I have delete mode */ 722 terminal_tputs(el, Str(T_dm), 1); 723 724 if (GoodStr(T_dc)) /* else do one at a time */ 725 while (num--) 726 terminal_tputs(el, Str(T_dc), 1); 727 728 if (GoodStr(T_ed)) /* if I have delete mode */ 729 terminal_tputs(el, Str(T_ed), 1); 730 } 731 732 733 /* terminal_insertwrite(): 734 * Puts terminal in insert character mode or inserts num 735 * characters in the line 736 * Assumes MB_FILL_CHARs are present to keep column count correct 737 */ 738 protected void 739 terminal_insertwrite(EditLine *el, Char *cp, int num) 740 { 741 if (num <= 0) 742 return; 743 if (!EL_CAN_INSERT) { 744 #ifdef DEBUG_EDIT 745 (void) fprintf(el->el_errfile, " ERROR: cannot insert \n"); 746 #endif /* DEBUG_EDIT */ 747 return; 748 } 749 if (num > el->el_terminal.t_size.h) { 750 #ifdef DEBUG_SCREEN 751 (void) fprintf(el->el_errfile, 752 "StartInsert: num is riduculous: %d\r\n", num); 753 #endif /* DEBUG_SCREEN */ 754 return; 755 } 756 if (GoodStr(T_IC)) /* if I have multiple insert */ 757 if ((num > 1) || !GoodStr(T_ic)) { 758 /* if ic would be more expensive */ 759 terminal_tputs(el, tgoto(Str(T_IC), num, num), num); 760 terminal_overwrite(el, cp, (size_t)num); 761 /* this updates el_cursor.h */ 762 return; 763 } 764 if (GoodStr(T_im) && GoodStr(T_ei)) { /* if I have insert mode */ 765 terminal_tputs(el, Str(T_im), 1); 766 767 el->el_cursor.h += num; 768 do 769 terminal__putc(el, *cp++); 770 while (--num); 771 772 if (GoodStr(T_ip)) /* have to make num chars insert */ 773 terminal_tputs(el, Str(T_ip), 1); 774 775 terminal_tputs(el, Str(T_ei), 1); 776 return; 777 } 778 do { 779 if (GoodStr(T_ic)) /* have to make num chars insert */ 780 terminal_tputs(el, Str(T_ic), 1); 781 782 terminal__putc(el, *cp++); 783 784 el->el_cursor.h++; 785 786 if (GoodStr(T_ip)) /* have to make num chars insert */ 787 terminal_tputs(el, Str(T_ip), 1); 788 /* pad the inserted char */ 789 790 } while (--num); 791 } 792 793 794 /* terminal_clear_EOL(): 795 * clear to end of line. There are num characters to clear 796 */ 797 protected void 798 terminal_clear_EOL(EditLine *el, int num) 799 { 800 int i; 801 802 if (EL_CAN_CEOL && GoodStr(T_ce)) 803 terminal_tputs(el, Str(T_ce), 1); 804 else { 805 for (i = 0; i < num; i++) 806 terminal__putc(el, ' '); 807 el->el_cursor.h += num; /* have written num spaces */ 808 } 809 } 810 811 812 /* terminal_clear_screen(): 813 * Clear the screen 814 */ 815 protected void 816 terminal_clear_screen(EditLine *el) 817 { /* clear the whole screen and home */ 818 819 if (GoodStr(T_cl)) 820 /* send the clear screen code */ 821 terminal_tputs(el, Str(T_cl), Val(T_li)); 822 else if (GoodStr(T_ho) && GoodStr(T_cd)) { 823 terminal_tputs(el, Str(T_ho), Val(T_li)); /* home */ 824 /* clear to bottom of screen */ 825 terminal_tputs(el, Str(T_cd), Val(T_li)); 826 } else { 827 terminal__putc(el, '\r'); 828 terminal__putc(el, '\n'); 829 } 830 } 831 832 833 /* terminal_beep(): 834 * Beep the way the terminal wants us 835 */ 836 protected void 837 terminal_beep(EditLine *el) 838 { 839 if (GoodStr(T_bl)) 840 /* what termcap says we should use */ 841 terminal_tputs(el, Str(T_bl), 1); 842 else 843 terminal__putc(el, '\007'); /* an ASCII bell; ^G */ 844 } 845 846 847 protected void 848 terminal_get(EditLine *el, const char **term) 849 { 850 *term = el->el_terminal.t_name; 851 } 852 853 854 /* terminal_set(): 855 * Read in the terminal capabilities from the requested terminal 856 */ 857 protected int 858 terminal_set(EditLine *el, const char *term) 859 { 860 int i; 861 char buf[TC_BUFSIZE]; 862 char *area; 863 const struct termcapstr *t; 864 sigset_t oset, nset; 865 int lins, cols; 866 867 (void) sigemptyset(&nset); 868 (void) sigaddset(&nset, SIGWINCH); 869 (void) sigprocmask(SIG_BLOCK, &nset, &oset); 870 871 area = buf; 872 873 874 if (term == NULL) 875 term = getenv("TERM"); 876 877 if (!term || !term[0]) 878 term = "dumb"; 879 880 if (strcmp(term, "emacs") == 0) 881 el->el_flags |= EDIT_DISABLED; 882 883 (void) memset(el->el_terminal.t_cap, 0, TC_BUFSIZE); 884 885 i = tgetent(el->el_terminal.t_cap, term); 886 887 if (i <= 0) { 888 if (i == -1) 889 (void) fprintf(el->el_errfile, 890 "Cannot read termcap database;\n"); 891 else if (i == 0) 892 (void) fprintf(el->el_errfile, 893 "No entry for terminal type \"%s\";\n", term); 894 (void) fprintf(el->el_errfile, 895 "using dumb terminal settings.\n"); 896 Val(T_co) = 80; /* do a dumb terminal */ 897 Val(T_pt) = Val(T_km) = Val(T_li) = 0; 898 Val(T_xt) = Val(T_MT); 899 for (t = tstr; t->name != NULL; t++) 900 terminal_alloc(el, t, NULL); 901 } else { 902 /* auto/magic margins */ 903 Val(T_am) = tgetflag("am"); 904 Val(T_xn) = tgetflag("xn"); 905 /* Can we tab */ 906 Val(T_pt) = tgetflag("pt"); 907 Val(T_xt) = tgetflag("xt"); 908 /* do we have a meta? */ 909 Val(T_km) = tgetflag("km"); 910 Val(T_MT) = tgetflag("MT"); 911 /* Get the size */ 912 Val(T_co) = tgetnum("co"); 913 Val(T_li) = tgetnum("li"); 914 for (t = tstr; t->name != NULL; t++) { 915 /* XXX: some systems' tgetstr needs non const */ 916 terminal_alloc(el, t, tgetstr(strchr(t->name, *t->name), 917 &area)); 918 } 919 } 920 921 if (Val(T_co) < 2) 922 Val(T_co) = 80; /* just in case */ 923 if (Val(T_li) < 1) 924 Val(T_li) = 24; 925 926 el->el_terminal.t_size.v = Val(T_co); 927 el->el_terminal.t_size.h = Val(T_li); 928 929 terminal_setflags(el); 930 931 /* get the correct window size */ 932 (void) terminal_get_size(el, &lins, &cols); 933 if (terminal_change_size(el, lins, cols) == -1) 934 return -1; 935 (void) sigprocmask(SIG_SETMASK, &oset, NULL); 936 terminal_bind_arrow(el); 937 el->el_terminal.t_name = term; 938 return i <= 0 ? -1 : 0; 939 } 940 941 942 /* terminal_get_size(): 943 * Return the new window size in lines and cols, and 944 * true if the size was changed. 945 */ 946 protected int 947 terminal_get_size(EditLine *el, int *lins, int *cols) 948 { 949 950 *cols = Val(T_co); 951 *lins = Val(T_li); 952 953 #ifdef TIOCGWINSZ 954 { 955 struct winsize ws; 956 if (ioctl(el->el_infd, TIOCGWINSZ, &ws) != -1) { 957 if (ws.ws_col) 958 *cols = ws.ws_col; 959 if (ws.ws_row) 960 *lins = ws.ws_row; 961 } 962 } 963 #endif 964 #ifndef __minix 965 #ifdef TIOCGSIZE 966 { 967 struct ttysize ts; 968 if (ioctl(el->el_infd, TIOCGSIZE, &ts) != -1) { 969 if (ts.ts_cols) 970 *cols = ts.ts_cols; 971 if (ts.ts_lines) 972 *lins = ts.ts_lines; 973 } 974 } 975 #endif 976 #endif 977 return Val(T_co) != *cols || Val(T_li) != *lins; 978 } 979 980 981 /* terminal_change_size(): 982 * Change the size of the terminal 983 */ 984 protected int 985 terminal_change_size(EditLine *el, int lins, int cols) 986 { 987 /* 988 * Just in case 989 */ 990 Val(T_co) = (cols < 2) ? 80 : cols; 991 Val(T_li) = (lins < 1) ? 24 : lins; 992 993 /* re-make display buffers */ 994 if (terminal_rebuffer_display(el) == -1) 995 return -1; 996 re_clear_display(el); 997 return 0; 998 } 999 1000 1001 /* terminal_init_arrow(): 1002 * Initialize the arrow key bindings from termcap 1003 */ 1004 private void 1005 terminal_init_arrow(EditLine *el) 1006 { 1007 funckey_t *arrow = el->el_terminal.t_fkey; 1008 1009 arrow[A_K_DN].name = STR("down"); 1010 arrow[A_K_DN].key = T_kd; 1011 arrow[A_K_DN].fun.cmd = ED_NEXT_HISTORY; 1012 arrow[A_K_DN].type = XK_CMD; 1013 1014 arrow[A_K_UP].name = STR("up"); 1015 arrow[A_K_UP].key = T_ku; 1016 arrow[A_K_UP].fun.cmd = ED_PREV_HISTORY; 1017 arrow[A_K_UP].type = XK_CMD; 1018 1019 arrow[A_K_LT].name = STR("left"); 1020 arrow[A_K_LT].key = T_kl; 1021 arrow[A_K_LT].fun.cmd = ED_PREV_CHAR; 1022 arrow[A_K_LT].type = XK_CMD; 1023 1024 arrow[A_K_RT].name = STR("right"); 1025 arrow[A_K_RT].key = T_kr; 1026 arrow[A_K_RT].fun.cmd = ED_NEXT_CHAR; 1027 arrow[A_K_RT].type = XK_CMD; 1028 1029 arrow[A_K_HO].name = STR("home"); 1030 arrow[A_K_HO].key = T_kh; 1031 arrow[A_K_HO].fun.cmd = ED_MOVE_TO_BEG; 1032 arrow[A_K_HO].type = XK_CMD; 1033 1034 arrow[A_K_EN].name = STR("end"); 1035 arrow[A_K_EN].key = T_at7; 1036 arrow[A_K_EN].fun.cmd = ED_MOVE_TO_END; 1037 arrow[A_K_EN].type = XK_CMD; 1038 1039 arrow[A_K_DE].name = STR("delete"); 1040 arrow[A_K_DE].key = T_kD; 1041 arrow[A_K_DE].fun.cmd = ED_DELETE_NEXT_CHAR; 1042 arrow[A_K_DE].type = XK_CMD; 1043 } 1044 1045 1046 /* terminal_reset_arrow(): 1047 * Reset arrow key bindings 1048 */ 1049 private void 1050 terminal_reset_arrow(EditLine *el) 1051 { 1052 funckey_t *arrow = el->el_terminal.t_fkey; 1053 static const Char strA[] = {033, '[', 'A', '\0'}; 1054 static const Char strB[] = {033, '[', 'B', '\0'}; 1055 static const Char strC[] = {033, '[', 'C', '\0'}; 1056 static const Char strD[] = {033, '[', 'D', '\0'}; 1057 static const Char strH[] = {033, '[', 'H', '\0'}; 1058 static const Char strF[] = {033, '[', 'F', '\0'}; 1059 static const Char stOA[] = {033, 'O', 'A', '\0'}; 1060 static const Char stOB[] = {033, 'O', 'B', '\0'}; 1061 static const Char stOC[] = {033, 'O', 'C', '\0'}; 1062 static const Char stOD[] = {033, 'O', 'D', '\0'}; 1063 static const Char stOH[] = {033, 'O', 'H', '\0'}; 1064 static const Char stOF[] = {033, 'O', 'F', '\0'}; 1065 1066 keymacro_add(el, strA, &arrow[A_K_UP].fun, arrow[A_K_UP].type); 1067 keymacro_add(el, strB, &arrow[A_K_DN].fun, arrow[A_K_DN].type); 1068 keymacro_add(el, strC, &arrow[A_K_RT].fun, arrow[A_K_RT].type); 1069 keymacro_add(el, strD, &arrow[A_K_LT].fun, arrow[A_K_LT].type); 1070 keymacro_add(el, strH, &arrow[A_K_HO].fun, arrow[A_K_HO].type); 1071 keymacro_add(el, strF, &arrow[A_K_EN].fun, arrow[A_K_EN].type); 1072 keymacro_add(el, stOA, &arrow[A_K_UP].fun, arrow[A_K_UP].type); 1073 keymacro_add(el, stOB, &arrow[A_K_DN].fun, arrow[A_K_DN].type); 1074 keymacro_add(el, stOC, &arrow[A_K_RT].fun, arrow[A_K_RT].type); 1075 keymacro_add(el, stOD, &arrow[A_K_LT].fun, arrow[A_K_LT].type); 1076 keymacro_add(el, stOH, &arrow[A_K_HO].fun, arrow[A_K_HO].type); 1077 keymacro_add(el, stOF, &arrow[A_K_EN].fun, arrow[A_K_EN].type); 1078 1079 if (el->el_map.type != MAP_VI) 1080 return; 1081 keymacro_add(el, &strA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type); 1082 keymacro_add(el, &strB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type); 1083 keymacro_add(el, &strC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type); 1084 keymacro_add(el, &strD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type); 1085 keymacro_add(el, &strH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type); 1086 keymacro_add(el, &strF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type); 1087 keymacro_add(el, &stOA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type); 1088 keymacro_add(el, &stOB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type); 1089 keymacro_add(el, &stOC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type); 1090 keymacro_add(el, &stOD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type); 1091 keymacro_add(el, &stOH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type); 1092 keymacro_add(el, &stOF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type); 1093 } 1094 1095 1096 /* terminal_set_arrow(): 1097 * Set an arrow key binding 1098 */ 1099 protected int 1100 terminal_set_arrow(EditLine *el, const Char *name, keymacro_value_t *fun, 1101 int type) 1102 { 1103 funckey_t *arrow = el->el_terminal.t_fkey; 1104 int i; 1105 1106 for (i = 0; i < A_K_NKEYS; i++) 1107 if (Strcmp(name, arrow[i].name) == 0) { 1108 arrow[i].fun = *fun; 1109 arrow[i].type = type; 1110 return 0; 1111 } 1112 return -1; 1113 } 1114 1115 1116 /* terminal_clear_arrow(): 1117 * Clear an arrow key binding 1118 */ 1119 protected int 1120 terminal_clear_arrow(EditLine *el, const Char *name) 1121 { 1122 funckey_t *arrow = el->el_terminal.t_fkey; 1123 int i; 1124 1125 for (i = 0; i < A_K_NKEYS; i++) 1126 if (Strcmp(name, arrow[i].name) == 0) { 1127 arrow[i].type = XK_NOD; 1128 return 0; 1129 } 1130 return -1; 1131 } 1132 1133 1134 /* terminal_print_arrow(): 1135 * Print the arrow key bindings 1136 */ 1137 protected void 1138 terminal_print_arrow(EditLine *el, const Char *name) 1139 { 1140 int i; 1141 funckey_t *arrow = el->el_terminal.t_fkey; 1142 1143 for (i = 0; i < A_K_NKEYS; i++) 1144 if (*name == '\0' || Strcmp(name, arrow[i].name) == 0) 1145 if (arrow[i].type != XK_NOD) 1146 keymacro_kprint(el, arrow[i].name, 1147 &arrow[i].fun, arrow[i].type); 1148 } 1149 1150 1151 /* terminal_bind_arrow(): 1152 * Bind the arrow keys 1153 */ 1154 protected void 1155 terminal_bind_arrow(EditLine *el) 1156 { 1157 el_action_t *map; 1158 const el_action_t *dmap; 1159 int i, j; 1160 char *p; 1161 funckey_t *arrow = el->el_terminal.t_fkey; 1162 1163 /* Check if the components needed are initialized */ 1164 if (el->el_terminal.t_buf == NULL || el->el_map.key == NULL) 1165 return; 1166 1167 map = el->el_map.type == MAP_VI ? el->el_map.alt : el->el_map.key; 1168 dmap = el->el_map.type == MAP_VI ? el->el_map.vic : el->el_map.emacs; 1169 1170 terminal_reset_arrow(el); 1171 1172 for (i = 0; i < A_K_NKEYS; i++) { 1173 Char wt_str[VISUAL_WIDTH_MAX]; 1174 Char *px; 1175 size_t n; 1176 1177 p = el->el_terminal.t_str[arrow[i].key]; 1178 if (!p || !*p) 1179 continue; 1180 for (n = 0; n < VISUAL_WIDTH_MAX && p[n]; ++n) 1181 wt_str[n] = p[n]; 1182 while (n < VISUAL_WIDTH_MAX) 1183 wt_str[n++] = '\0'; 1184 px = wt_str; 1185 j = (unsigned char) *p; 1186 /* 1187 * Assign the arrow keys only if: 1188 * 1189 * 1. They are multi-character arrow keys and the user 1190 * has not re-assigned the leading character, or 1191 * has re-assigned the leading character to be 1192 * ED_SEQUENCE_LEAD_IN 1193 * 2. They are single arrow keys pointing to an 1194 * unassigned key. 1195 */ 1196 if (arrow[i].type == XK_NOD) 1197 keymacro_clear(el, map, px); 1198 else { 1199 if (p[1] && (dmap[j] == map[j] || 1200 map[j] == ED_SEQUENCE_LEAD_IN)) { 1201 keymacro_add(el, px, &arrow[i].fun, 1202 arrow[i].type); 1203 map[j] = ED_SEQUENCE_LEAD_IN; 1204 } else if (map[j] == ED_UNASSIGNED) { 1205 keymacro_clear(el, map, px); 1206 if (arrow[i].type == XK_CMD) 1207 map[j] = arrow[i].fun.cmd; 1208 else 1209 keymacro_add(el, px, &arrow[i].fun, 1210 arrow[i].type); 1211 } 1212 } 1213 } 1214 } 1215 1216 /* terminal_putc(): 1217 * Add a character 1218 */ 1219 private int 1220 terminal_putc(int c) 1221 { 1222 if (terminal_outfile == NULL) 1223 return -1; 1224 return fputc(c, terminal_outfile); 1225 } 1226 1227 private void 1228 terminal_tputs(EditLine *el, const char *cap, int affcnt) 1229 { 1230 #ifdef _REENTRANT 1231 pthread_mutex_lock(&terminal_mutex); 1232 #endif 1233 terminal_outfile = el->el_outfile; 1234 (void)tputs(cap, affcnt, terminal_putc); 1235 #ifdef _REENTRANT 1236 pthread_mutex_unlock(&terminal_mutex); 1237 #endif 1238 } 1239 1240 /* terminal__putc(): 1241 * Add a character 1242 */ 1243 protected int 1244 terminal__putc(EditLine *el, Int c) 1245 { 1246 char buf[MB_LEN_MAX +1]; 1247 ssize_t i; 1248 if (c == (Int)MB_FILL_CHAR) 1249 return 0; 1250 i = ct_encode_char(buf, (size_t)MB_LEN_MAX, c); 1251 if (i <= 0) 1252 return (int)i; 1253 buf[i] = '\0'; 1254 return fputs(buf, el->el_outfile); 1255 } 1256 1257 /* terminal__flush(): 1258 * Flush output 1259 */ 1260 protected void 1261 terminal__flush(EditLine *el) 1262 { 1263 1264 (void) fflush(el->el_outfile); 1265 } 1266 1267 /* terminal_writec(): 1268 * Write the given character out, in a human readable form 1269 */ 1270 protected void 1271 terminal_writec(EditLine *el, Int c) 1272 { 1273 Char visbuf[VISUAL_WIDTH_MAX +1]; 1274 ssize_t vcnt = ct_visual_char(visbuf, VISUAL_WIDTH_MAX, c); 1275 if (vcnt < 0) 1276 vcnt = 0; 1277 visbuf[vcnt] = '\0'; 1278 terminal_overwrite(el, visbuf, (size_t)vcnt); 1279 terminal__flush(el); 1280 } 1281 1282 1283 /* terminal_telltc(): 1284 * Print the current termcap characteristics 1285 */ 1286 protected int 1287 /*ARGSUSED*/ 1288 terminal_telltc(EditLine *el, int argc __attribute__((__unused__)), 1289 const Char **argv __attribute__((__unused__))) 1290 { 1291 const struct termcapstr *t; 1292 char **ts; 1293 1294 (void) fprintf(el->el_outfile, "\n\tYour terminal has the\n"); 1295 (void) fprintf(el->el_outfile, "\tfollowing characteristics:\n\n"); 1296 (void) fprintf(el->el_outfile, "\tIt has %d columns and %d lines\n", 1297 Val(T_co), Val(T_li)); 1298 (void) fprintf(el->el_outfile, 1299 "\tIt has %s meta key\n", EL_HAS_META ? "a" : "no"); 1300 (void) fprintf(el->el_outfile, 1301 "\tIt can%suse tabs\n", EL_CAN_TAB ? " " : "not "); 1302 (void) fprintf(el->el_outfile, "\tIt %s automatic margins\n", 1303 EL_HAS_AUTO_MARGINS ? "has" : "does not have"); 1304 if (EL_HAS_AUTO_MARGINS) 1305 (void) fprintf(el->el_outfile, "\tIt %s magic margins\n", 1306 EL_HAS_MAGIC_MARGINS ? "has" : "does not have"); 1307 1308 for (t = tstr, ts = el->el_terminal.t_str; t->name != NULL; t++, ts++) { 1309 const char *ub; 1310 if (*ts && **ts) { 1311 ub = ct_encode_string(ct_visual_string( 1312 ct_decode_string(*ts, &el->el_scratch)), 1313 &el->el_scratch); 1314 } else { 1315 ub = "(empty)"; 1316 } 1317 (void) fprintf(el->el_outfile, "\t%25s (%s) == %s\n", 1318 t->long_name, t->name, ub); 1319 } 1320 (void) fputc('\n', el->el_outfile); 1321 return 0; 1322 } 1323 1324 1325 /* terminal_settc(): 1326 * Change the current terminal characteristics 1327 */ 1328 protected int 1329 /*ARGSUSED*/ 1330 terminal_settc(EditLine *el, int argc __attribute__((__unused__)), 1331 const Char **argv) 1332 { 1333 const struct termcapstr *ts; 1334 const struct termcapval *tv; 1335 char what[8], how[8]; 1336 1337 if (argv == NULL || argv[1] == NULL || argv[2] == NULL) 1338 return -1; 1339 1340 strncpy(what, ct_encode_string(argv[1], &el->el_scratch), sizeof(what)); 1341 what[sizeof(what) - 1] = '\0'; 1342 strncpy(how, ct_encode_string(argv[2], &el->el_scratch), sizeof(how)); 1343 how[sizeof(how) - 1] = '\0'; 1344 1345 /* 1346 * Do the strings first 1347 */ 1348 for (ts = tstr; ts->name != NULL; ts++) 1349 if (strcmp(ts->name, what) == 0) 1350 break; 1351 1352 if (ts->name != NULL) { 1353 terminal_alloc(el, ts, how); 1354 terminal_setflags(el); 1355 return 0; 1356 } 1357 /* 1358 * Do the numeric ones second 1359 */ 1360 for (tv = tval; tv->name != NULL; tv++) 1361 if (strcmp(tv->name, what) == 0) 1362 break; 1363 1364 if (tv->name != NULL) 1365 return -1; 1366 1367 if (tv == &tval[T_pt] || tv == &tval[T_km] || 1368 tv == &tval[T_am] || tv == &tval[T_xn]) { 1369 if (strcmp(how, "yes") == 0) 1370 el->el_terminal.t_val[tv - tval] = 1; 1371 else if (strcmp(how, "no") == 0) 1372 el->el_terminal.t_val[tv - tval] = 0; 1373 else { 1374 (void) fprintf(el->el_errfile, 1375 "" FSTR ": Bad value `%s'.\n", argv[0], how); 1376 return -1; 1377 } 1378 terminal_setflags(el); 1379 if (terminal_change_size(el, Val(T_li), Val(T_co)) == -1) 1380 return -1; 1381 return 0; 1382 } else { 1383 long i; 1384 char *ep; 1385 1386 i = strtol(how, &ep, 10); 1387 if (*ep != '\0') { 1388 (void) fprintf(el->el_errfile, 1389 "" FSTR ": Bad value `%s'.\n", argv[0], how); 1390 return -1; 1391 } 1392 el->el_terminal.t_val[tv - tval] = (int) i; 1393 el->el_terminal.t_size.v = Val(T_co); 1394 el->el_terminal.t_size.h = Val(T_li); 1395 if (tv == &tval[T_co] || tv == &tval[T_li]) 1396 if (terminal_change_size(el, Val(T_li), Val(T_co)) 1397 == -1) 1398 return -1; 1399 return 0; 1400 } 1401 } 1402 1403 1404 /* terminal_gettc(): 1405 * Get the current terminal characteristics 1406 */ 1407 protected int 1408 /*ARGSUSED*/ 1409 terminal_gettc(EditLine *el, int argc __attribute__((__unused__)), char **argv) 1410 { 1411 const struct termcapstr *ts; 1412 const struct termcapval *tv; 1413 char *what; 1414 void *how; 1415 1416 if (argv == NULL || argv[1] == NULL || argv[2] == NULL) 1417 return -1; 1418 1419 what = argv[1]; 1420 how = argv[2]; 1421 1422 /* 1423 * Do the strings first 1424 */ 1425 for (ts = tstr; ts->name != NULL; ts++) 1426 if (strcmp(ts->name, what) == 0) 1427 break; 1428 1429 if (ts->name != NULL) { 1430 *(char **)how = el->el_terminal.t_str[ts - tstr]; 1431 return 0; 1432 } 1433 /* 1434 * Do the numeric ones second 1435 */ 1436 for (tv = tval; tv->name != NULL; tv++) 1437 if (strcmp(tv->name, what) == 0) 1438 break; 1439 1440 if (tv->name == NULL) 1441 return -1; 1442 1443 if (tv == &tval[T_pt] || tv == &tval[T_km] || 1444 tv == &tval[T_am] || tv == &tval[T_xn]) { 1445 static char yes[] = "yes"; 1446 static char no[] = "no"; 1447 if (el->el_terminal.t_val[tv - tval]) 1448 *(char **)how = yes; 1449 else 1450 *(char **)how = no; 1451 return 0; 1452 } else { 1453 *(int *)how = el->el_terminal.t_val[tv - tval]; 1454 return 0; 1455 } 1456 } 1457 1458 /* terminal_echotc(): 1459 * Print the termcap string out with variable substitution 1460 */ 1461 protected int 1462 /*ARGSUSED*/ 1463 terminal_echotc(EditLine *el, int argc __attribute__((__unused__)), 1464 const Char **argv) 1465 { 1466 char *cap, *scap; 1467 Char *ep; 1468 int arg_need, arg_cols, arg_rows; 1469 int verbose = 0, silent = 0; 1470 char *area; 1471 static const char fmts[] = "%s\n", fmtd[] = "%d\n"; 1472 const struct termcapstr *t; 1473 char buf[TC_BUFSIZE]; 1474 long i; 1475 1476 area = buf; 1477 1478 if (argv == NULL || argv[1] == NULL) 1479 return -1; 1480 argv++; 1481 1482 if (argv[0][0] == '-') { 1483 switch (argv[0][1]) { 1484 case 'v': 1485 verbose = 1; 1486 break; 1487 case 's': 1488 silent = 1; 1489 break; 1490 default: 1491 /* stderror(ERR_NAME | ERR_TCUSAGE); */ 1492 break; 1493 } 1494 argv++; 1495 } 1496 if (!*argv || *argv[0] == '\0') 1497 return 0; 1498 if (Strcmp(*argv, STR("tabs")) == 0) { 1499 (void) fprintf(el->el_outfile, fmts, EL_CAN_TAB ? "yes" : "no"); 1500 return 0; 1501 } else if (Strcmp(*argv, STR("meta")) == 0) { 1502 (void) fprintf(el->el_outfile, fmts, Val(T_km) ? "yes" : "no"); 1503 return 0; 1504 } else if (Strcmp(*argv, STR("xn")) == 0) { 1505 (void) fprintf(el->el_outfile, fmts, EL_HAS_MAGIC_MARGINS ? 1506 "yes" : "no"); 1507 return 0; 1508 } else if (Strcmp(*argv, STR("am")) == 0) { 1509 (void) fprintf(el->el_outfile, fmts, EL_HAS_AUTO_MARGINS ? 1510 "yes" : "no"); 1511 return 0; 1512 } else if (Strcmp(*argv, STR("baud")) == 0) { 1513 (void) fprintf(el->el_outfile, fmtd, (int)el->el_tty.t_speed); 1514 return 0; 1515 } else if (Strcmp(*argv, STR("rows")) == 0 || 1516 Strcmp(*argv, STR("lines")) == 0) { 1517 (void) fprintf(el->el_outfile, fmtd, Val(T_li)); 1518 return 0; 1519 } else if (Strcmp(*argv, STR("cols")) == 0) { 1520 (void) fprintf(el->el_outfile, fmtd, Val(T_co)); 1521 return 0; 1522 } 1523 /* 1524 * Try to use our local definition first 1525 */ 1526 scap = NULL; 1527 for (t = tstr; t->name != NULL; t++) 1528 if (strcmp(t->name, 1529 ct_encode_string(*argv, &el->el_scratch)) == 0) { 1530 scap = el->el_terminal.t_str[t - tstr]; 1531 break; 1532 } 1533 if (t->name == NULL) { 1534 /* XXX: some systems' tgetstr needs non const */ 1535 scap = tgetstr(ct_encode_string(*argv, &el->el_scratch), &area); 1536 } 1537 if (!scap || scap[0] == '\0') { 1538 if (!silent) 1539 (void) fprintf(el->el_errfile, 1540 "echotc: Termcap parameter `" FSTR "' not found.\n", 1541 *argv); 1542 return -1; 1543 } 1544 /* 1545 * Count home many values we need for this capability. 1546 */ 1547 for (cap = scap, arg_need = 0; *cap; cap++) 1548 if (*cap == '%') 1549 switch (*++cap) { 1550 case 'd': 1551 case '2': 1552 case '3': 1553 case '.': 1554 case '+': 1555 arg_need++; 1556 break; 1557 case '%': 1558 case '>': 1559 case 'i': 1560 case 'r': 1561 case 'n': 1562 case 'B': 1563 case 'D': 1564 break; 1565 default: 1566 /* 1567 * hpux has lot's of them... 1568 */ 1569 if (verbose) 1570 (void) fprintf(el->el_errfile, 1571 "echotc: Warning: unknown termcap %% `%c'.\n", 1572 *cap); 1573 /* This is bad, but I won't complain */ 1574 break; 1575 } 1576 1577 switch (arg_need) { 1578 case 0: 1579 argv++; 1580 if (*argv && *argv[0]) { 1581 if (!silent) 1582 (void) fprintf(el->el_errfile, 1583 "echotc: Warning: Extra argument `" FSTR "'.\n", 1584 *argv); 1585 return -1; 1586 } 1587 terminal_tputs(el, scap, 1); 1588 break; 1589 case 1: 1590 argv++; 1591 if (!*argv || *argv[0] == '\0') { 1592 if (!silent) 1593 (void) fprintf(el->el_errfile, 1594 "echotc: Warning: Missing argument.\n"); 1595 return -1; 1596 } 1597 arg_cols = 0; 1598 i = Strtol(*argv, &ep, 10); 1599 if (*ep != '\0' || i < 0) { 1600 if (!silent) 1601 (void) fprintf(el->el_errfile, 1602 "echotc: Bad value `" FSTR "' for rows.\n", 1603 *argv); 1604 return -1; 1605 } 1606 arg_rows = (int) i; 1607 argv++; 1608 if (*argv && *argv[0]) { 1609 if (!silent) 1610 (void) fprintf(el->el_errfile, 1611 "echotc: Warning: Extra argument `" FSTR 1612 "'.\n", *argv); 1613 return -1; 1614 } 1615 terminal_tputs(el, tgoto(scap, arg_cols, arg_rows), 1); 1616 break; 1617 default: 1618 /* This is wrong, but I will ignore it... */ 1619 if (verbose) 1620 (void) fprintf(el->el_errfile, 1621 "echotc: Warning: Too many required arguments (%d).\n", 1622 arg_need); 1623 /* FALLTHROUGH */ 1624 case 2: 1625 argv++; 1626 if (!*argv || *argv[0] == '\0') { 1627 if (!silent) 1628 (void) fprintf(el->el_errfile, 1629 "echotc: Warning: Missing argument.\n"); 1630 return -1; 1631 } 1632 i = Strtol(*argv, &ep, 10); 1633 if (*ep != '\0' || i < 0) { 1634 if (!silent) 1635 (void) fprintf(el->el_errfile, 1636 "echotc: Bad value `" FSTR "' for cols.\n", 1637 *argv); 1638 return -1; 1639 } 1640 arg_cols = (int) i; 1641 argv++; 1642 if (!*argv || *argv[0] == '\0') { 1643 if (!silent) 1644 (void) fprintf(el->el_errfile, 1645 "echotc: Warning: Missing argument.\n"); 1646 return -1; 1647 } 1648 i = Strtol(*argv, &ep, 10); 1649 if (*ep != '\0' || i < 0) { 1650 if (!silent) 1651 (void) fprintf(el->el_errfile, 1652 "echotc: Bad value `" FSTR "' for rows.\n", 1653 *argv); 1654 return -1; 1655 } 1656 arg_rows = (int) i; 1657 if (*ep != '\0') { 1658 if (!silent) 1659 (void) fprintf(el->el_errfile, 1660 "echotc: Bad value `" FSTR "'.\n", *argv); 1661 return -1; 1662 } 1663 argv++; 1664 if (*argv && *argv[0]) { 1665 if (!silent) 1666 (void) fprintf(el->el_errfile, 1667 "echotc: Warning: Extra argument `" FSTR 1668 "'.\n", *argv); 1669 return -1; 1670 } 1671 terminal_tputs(el, tgoto(scap, arg_cols, arg_rows), arg_rows); 1672 break; 1673 } 1674 return 0; 1675 } 1676