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