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