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