terminal.c revision 1.28
1/* $NetBSD: terminal.c,v 1.28 2016/04/11 00:50:13 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 38static char sccsid[] = "@(#)term.c 8.2 (Berkeley) 4/30/95"; 39#else 40__RCSID("$NetBSD: terminal.c,v 1.28 2016/04/11 00:50:13 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 77/* 78 * IMPORTANT NOTE: these routines are allowed to look at the current screen 79 * and the current position assuming that it is correct. If this is not 80 * true, then the update will be WRONG! This is (should be) a valid 81 * assumption... 82 */ 83 84#define TC_BUFSIZE ((size_t)2048) 85 86#define GoodStr(a) (el->el_terminal.t_str[a] != NULL && \ 87 el->el_terminal.t_str[a][0] != '\0') 88#define Str(a) el->el_terminal.t_str[a] 89#define Val(a) el->el_terminal.t_val[a] 90 91private const struct termcapstr { 92 const char *name; 93 const char *long_name; 94} tstr[] = { 95#define T_al 0 96 { "al", "add new blank line" }, 97#define T_bl 1 98 { "bl", "audible bell" }, 99#define T_cd 2 100 { "cd", "clear to bottom" }, 101#define T_ce 3 102 { "ce", "clear to end of line" }, 103#define T_ch 4 104 { "ch", "cursor to horiz pos" }, 105#define T_cl 5 106 { "cl", "clear screen" }, 107#define T_dc 6 108 { "dc", "delete a character" }, 109#define T_dl 7 110 { "dl", "delete a line" }, 111#define T_dm 8 112 { "dm", "start delete mode" }, 113#define T_ed 9 114 { "ed", "end delete mode" }, 115#define T_ei 10 116 { "ei", "end insert mode" }, 117#define T_fs 11 118 { "fs", "cursor from status line" }, 119#define T_ho 12 120 { "ho", "home cursor" }, 121#define T_ic 13 122 { "ic", "insert character" }, 123#define T_im 14 124 { "im", "start insert mode" }, 125#define T_ip 15 126 { "ip", "insert padding" }, 127#define T_kd 16 128 { "kd", "sends cursor down" }, 129#define T_kl 17 130 { "kl", "sends cursor left" }, 131#define T_kr 18 132 { "kr", "sends cursor right" }, 133#define T_ku 19 134 { "ku", "sends cursor up" }, 135#define T_md 20 136 { "md", "begin bold" }, 137#define T_me 21 138 { "me", "end attributes" }, 139#define T_nd 22 140 { "nd", "non destructive space" }, 141#define T_se 23 142 { "se", "end standout" }, 143#define T_so 24 144 { "so", "begin standout" }, 145#define T_ts 25 146 { "ts", "cursor to status line" }, 147#define T_up 26 148 { "up", "cursor up one" }, 149#define T_us 27 150 { "us", "begin underline" }, 151#define T_ue 28 152 { "ue", "end underline" }, 153#define T_vb 29 154 { "vb", "visible bell" }, 155#define T_DC 30 156 { "DC", "delete multiple chars" }, 157#define T_DO 31 158 { "DO", "cursor down multiple" }, 159#define T_IC 32 160 { "IC", "insert multiple chars" }, 161#define T_LE 33 162 { "LE", "cursor left multiple" }, 163#define T_RI 34 164 { "RI", "cursor right multiple" }, 165#define T_UP 35 166 { "UP", "cursor up multiple" }, 167#define T_kh 36 168 { "kh", "send cursor home" }, 169#define T_at7 37 170 { "@7", "send cursor end" }, 171#define T_kD 38 172 { "kD", "send cursor delete" }, 173#define T_str 39 174 { NULL, NULL } 175}; 176 177private const struct termcapval { 178 const char *name; 179 const char *long_name; 180} tval[] = { 181#define T_am 0 182 { "am", "has automatic margins" }, 183#define T_pt 1 184 { "pt", "has physical tabs" }, 185#define T_li 2 186 { "li", "Number of lines" }, 187#define T_co 3 188 { "co", "Number of columns" }, 189#define T_km 4 190 { "km", "Has meta key" }, 191#define T_xt 5 192 { "xt", "Tab chars destructive" }, 193#define T_xn 6 194 { "xn", "newline ignored at right margin" }, 195#define T_MT 7 196 { "MT", "Has meta key" }, /* XXX? */ 197#define T_val 8 198 { NULL, NULL, } 199}; 200/* do two or more of the attributes use me */ 201 202private void terminal_setflags(EditLine *); 203private int terminal_rebuffer_display(EditLine *); 204private void terminal_free_display(EditLine *); 205private int terminal_alloc_display(EditLine *); 206private void terminal_alloc(EditLine *, const struct termcapstr *, 207 const char *); 208private void terminal_init_arrow(EditLine *); 209private void terminal_reset_arrow(EditLine *); 210private int terminal_putc(int); 211private void terminal_tputs(EditLine *, const char *, int); 212 213#ifdef _REENTRANT 214private pthread_mutex_t terminal_mutex = PTHREAD_MUTEX_INITIALIZER; 215#endif 216private FILE *terminal_outfile = NULL; 217 218 219/* terminal_setflags(): 220 * Set the terminal capability flags 221 */ 222private void 223terminal_setflags(EditLine *el) 224{ 225 EL_FLAGS = 0; 226 if (el->el_tty.t_tabs) 227 EL_FLAGS |= (Val(T_pt) && !Val(T_xt)) ? TERM_CAN_TAB : 0; 228 229 EL_FLAGS |= (Val(T_km) || Val(T_MT)) ? TERM_HAS_META : 0; 230 EL_FLAGS |= GoodStr(T_ce) ? TERM_CAN_CEOL : 0; 231 EL_FLAGS |= (GoodStr(T_dc) || GoodStr(T_DC)) ? TERM_CAN_DELETE : 0; 232 EL_FLAGS |= (GoodStr(T_im) || GoodStr(T_ic) || GoodStr(T_IC)) ? 233 TERM_CAN_INSERT : 0; 234 EL_FLAGS |= (GoodStr(T_up) || GoodStr(T_UP)) ? TERM_CAN_UP : 0; 235 EL_FLAGS |= Val(T_am) ? TERM_HAS_AUTO_MARGINS : 0; 236 EL_FLAGS |= Val(T_xn) ? TERM_HAS_MAGIC_MARGINS : 0; 237 238 if (GoodStr(T_me) && GoodStr(T_ue)) 239 EL_FLAGS |= (strcmp(Str(T_me), Str(T_ue)) == 0) ? 240 TERM_CAN_ME : 0; 241 else 242 EL_FLAGS &= ~TERM_CAN_ME; 243 if (GoodStr(T_me) && GoodStr(T_se)) 244 EL_FLAGS |= (strcmp(Str(T_me), Str(T_se)) == 0) ? 245 TERM_CAN_ME : 0; 246 247 248#ifdef DEBUG_SCREEN 249 if (!EL_CAN_UP) { 250 (void) fprintf(el->el_errfile, 251 "WARNING: Your terminal cannot move up.\n"); 252 (void) fprintf(el->el_errfile, 253 "Editing may be odd for long lines.\n"); 254 } 255 if (!EL_CAN_CEOL) 256 (void) fprintf(el->el_errfile, "no clear EOL capability.\n"); 257 if (!EL_CAN_DELETE) 258 (void) fprintf(el->el_errfile, "no delete char capability.\n"); 259 if (!EL_CAN_INSERT) 260 (void) fprintf(el->el_errfile, "no insert char capability.\n"); 261#endif /* DEBUG_SCREEN */ 262} 263 264/* terminal_init(): 265 * Initialize the terminal stuff 266 */ 267protected int 268terminal_init(EditLine *el) 269{ 270 271 el->el_terminal.t_buf = el_malloc(TC_BUFSIZE * 272 sizeof(*el->el_terminal.t_buf)); 273 if (el->el_terminal.t_buf == NULL) 274 goto fail1; 275 el->el_terminal.t_cap = el_malloc(TC_BUFSIZE * 276 sizeof(*el->el_terminal.t_cap)); 277 if (el->el_terminal.t_cap == NULL) 278 goto fail2; 279 el->el_terminal.t_fkey = el_malloc(A_K_NKEYS * 280 sizeof(*el->el_terminal.t_fkey)); 281 if (el->el_terminal.t_fkey == NULL) 282 goto fail3; 283 el->el_terminal.t_loc = 0; 284 el->el_terminal.t_str = el_malloc(T_str * 285 sizeof(*el->el_terminal.t_str)); 286 if (el->el_terminal.t_str == NULL) 287 goto fail4; 288 (void) memset(el->el_terminal.t_str, 0, T_str * 289 sizeof(*el->el_terminal.t_str)); 290 el->el_terminal.t_val = el_malloc(T_val * 291 sizeof(*el->el_terminal.t_val)); 292 if (el->el_terminal.t_val == NULL) 293 goto fail5; 294 (void) memset(el->el_terminal.t_val, 0, T_val * 295 sizeof(*el->el_terminal.t_val)); 296 (void) terminal_set(el, NULL); 297 terminal_init_arrow(el); 298 return 0; 299fail5: 300 free(el->el_terminal.t_str); 301 el->el_terminal.t_str = NULL; 302fail4: 303 free(el->el_terminal.t_fkey); 304 el->el_terminal.t_fkey = NULL; 305fail3: 306 free(el->el_terminal.t_cap); 307 el->el_terminal.t_cap = NULL; 308fail2: 309 free(el->el_terminal.t_buf); 310 el->el_terminal.t_buf = NULL; 311fail1: 312 return -1; 313} 314 315/* terminal_end(): 316 * Clean up the terminal stuff 317 */ 318protected void 319terminal_end(EditLine *el) 320{ 321 322 el_free(el->el_terminal.t_buf); 323 el->el_terminal.t_buf = NULL; 324 el_free(el->el_terminal.t_cap); 325 el->el_terminal.t_cap = NULL; 326 el->el_terminal.t_loc = 0; 327 el_free(el->el_terminal.t_str); 328 el->el_terminal.t_str = NULL; 329 el_free(el->el_terminal.t_val); 330 el->el_terminal.t_val = NULL; 331 el_free(el->el_terminal.t_fkey); 332 el->el_terminal.t_fkey = NULL; 333 terminal_free_display(el); 334} 335 336 337/* terminal_alloc(): 338 * Maintain a string pool for termcap strings 339 */ 340private void 341terminal_alloc(EditLine *el, const struct termcapstr *t, const char *cap) 342{ 343 char termbuf[TC_BUFSIZE]; 344 size_t tlen, clen; 345 char **tlist = el->el_terminal.t_str; 346 char **tmp, **str = &tlist[t - tstr]; 347 348 (void) memset(termbuf, 0, sizeof(termbuf)); 349 if (cap == NULL || *cap == '\0') { 350 *str = NULL; 351 return; 352 } else 353 clen = strlen(cap); 354 355 tlen = *str == NULL ? 0 : strlen(*str); 356 357 /* 358 * New string is shorter; no need to allocate space 359 */ 360 if (clen <= tlen) { 361 if (*str) 362 (void) strcpy(*str, cap); /* XXX strcpy is safe */ 363 return; 364 } 365 /* 366 * New string is longer; see if we have enough space to append 367 */ 368 if (el->el_terminal.t_loc + 3 < TC_BUFSIZE) { 369 /* XXX strcpy is safe */ 370 (void) strcpy(*str = &el->el_terminal.t_buf[ 371 el->el_terminal.t_loc], cap); 372 el->el_terminal.t_loc += clen + 1; /* one for \0 */ 373 return; 374 } 375 /* 376 * Compact our buffer; no need to check compaction, cause we know it 377 * fits... 378 */ 379 tlen = 0; 380 for (tmp = tlist; tmp < &tlist[T_str]; tmp++) 381 if (*tmp != NULL && **tmp != '\0' && *tmp != *str) { 382 char *ptr; 383 384 for (ptr = *tmp; *ptr != '\0'; termbuf[tlen++] = *ptr++) 385 continue; 386 termbuf[tlen++] = '\0'; 387 } 388 memcpy(el->el_terminal.t_buf, termbuf, TC_BUFSIZE); 389 el->el_terminal.t_loc = tlen; 390 if (el->el_terminal.t_loc + 3 >= TC_BUFSIZE) { 391 (void) fprintf(el->el_errfile, 392 "Out of termcap string space.\n"); 393 return; 394 } 395 /* XXX strcpy is safe */ 396 (void) strcpy(*str = &el->el_terminal.t_buf[el->el_terminal.t_loc], 397 cap); 398 el->el_terminal.t_loc += (size_t)clen + 1; /* one for \0 */ 399 return; 400} 401 402 403/* terminal_rebuffer_display(): 404 * Rebuffer the display after the screen changed size 405 */ 406private int 407terminal_rebuffer_display(EditLine *el) 408{ 409 coord_t *c = &el->el_terminal.t_size; 410 411 terminal_free_display(el); 412 413 c->h = Val(T_co); 414 c->v = Val(T_li); 415 416 if (terminal_alloc_display(el) == -1) 417 return -1; 418 return 0; 419} 420 421 422/* terminal_alloc_display(): 423 * Allocate a new display. 424 */ 425private int 426terminal_alloc_display(EditLine *el) 427{ 428 int i; 429 wchar_t **b; 430 coord_t *c = &el->el_terminal.t_size; 431 432 b = el_malloc(sizeof(*b) * (size_t)(c->v + 1)); 433 if (b == NULL) 434 goto done; 435 for (i = 0; i < c->v; i++) { 436 b[i] = el_malloc(sizeof(**b) * (size_t)(c->h + 1)); 437 if (b[i] == NULL) { 438 while (--i >= 0) 439 el_free(b[i]); 440 el_free(b); 441 goto done; 442 } 443 } 444 b[c->v] = NULL; 445 el->el_display = b; 446 447 b = el_malloc(sizeof(*b) * (size_t)(c->v + 1)); 448 if (b == NULL) 449 goto done; 450 for (i = 0; i < c->v; i++) { 451 b[i] = el_malloc(sizeof(**b) * (size_t)(c->h + 1)); 452 if (b[i] == NULL) { 453 while (--i >= 0) 454 el_free(b[i]); 455 el_free(b); 456 goto done; 457 } 458 } 459 b[c->v] = NULL; 460 el->el_vdisplay = b; 461 return 0; 462done: 463 terminal_free_display(el); 464 return -1; 465} 466 467 468/* terminal_free_display(): 469 * Free the display buffers 470 */ 471private void 472terminal_free_display(EditLine *el) 473{ 474 wchar_t **b; 475 wchar_t **bufp; 476 477 b = el->el_display; 478 el->el_display = NULL; 479 if (b != NULL) { 480 for (bufp = b; *bufp != NULL; bufp++) 481 el_free(*bufp); 482 el_free(b); 483 } 484 b = el->el_vdisplay; 485 el->el_vdisplay = NULL; 486 if (b != NULL) { 487 for (bufp = b; *bufp != NULL; bufp++) 488 el_free(*bufp); 489 el_free(b); 490 } 491} 492 493 494/* terminal_move_to_line(): 495 * move to line <where> (first line == 0) 496 * as efficiently as possible 497 */ 498protected void 499terminal_move_to_line(EditLine *el, int where) 500{ 501 int del; 502 503 if (where == el->el_cursor.v) 504 return; 505 506 if (where > el->el_terminal.t_size.v) { 507#ifdef DEBUG_SCREEN 508 (void) fprintf(el->el_errfile, 509 "%s: where is ridiculous: %d\r\n", __func__, where); 510#endif /* DEBUG_SCREEN */ 511 return; 512 } 513 if ((del = where - el->el_cursor.v) > 0) { 514 while (del > 0) { 515 if (EL_HAS_AUTO_MARGINS && 516 el->el_display[el->el_cursor.v][0] != '\0') { 517 size_t h = (size_t) 518 (el->el_terminal.t_size.h - 1); 519 for (; h > 0 && 520 el->el_display[el->el_cursor.v][h] == 521 MB_FILL_CHAR; 522 h--) 523 continue; 524 /* move without newline */ 525 terminal_move_to_char(el, (int)h); 526 terminal_overwrite(el, &el->el_display 527 [el->el_cursor.v][el->el_cursor.h], 528 (size_t)(el->el_terminal.t_size.h - 529 el->el_cursor.h)); 530 /* updates Cursor */ 531 del--; 532 } else { 533 if ((del > 1) && GoodStr(T_DO)) { 534 terminal_tputs(el, tgoto(Str(T_DO), del, 535 del), del); 536 del = 0; 537 } else { 538 for (; del > 0; del--) 539 terminal__putc(el, '\n'); 540 /* because the \n will become \r\n */ 541 el->el_cursor.h = 0; 542 } 543 } 544 } 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 */ 561protected void 562terminal_move_to_char(EditLine *el, int where) 563{ 564 int del, i; 565 566mc_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 /* if I can do tabs, use them */ 594 if (EL_CAN_TAB) { 595 if ((el->el_cursor.h & 0370) != 596 (where & ~0x7) 597 && (el->el_display[ 598 el->el_cursor.v][where & 0370] != 599 MB_FILL_CHAR) 600 ) { 601 /* if not within tab stop */ 602 for (i = 603 (el->el_cursor.h & 0370); 604 i < (where & ~0x7); 605 i += 8) 606 terminal__putc(el, 607 '\t'); 608 /* then tab over */ 609 el->el_cursor.h = where & ~0x7; 610 } 611 } 612 /* 613 * it's usually cheaper to just write the 614 * chars, so we do. 615 */ 616 /* 617 * NOTE THAT terminal_overwrite() WILL CHANGE 618 * el->el_cursor.h!!! 619 */ 620 terminal_overwrite(el, &el->el_display[ 621 el->el_cursor.v][el->el_cursor.h], 622 (size_t)(where - el->el_cursor.h)); 623 624 } 625 } else { /* del < 0 := moving backward */ 626 if ((-del > 4) && GoodStr(T_LE)) 627 terminal_tputs(el, tgoto(Str(T_LE), -del, -del), 628 -del); 629 else { /* can't go directly there */ 630 /* 631 * if the "cost" is greater than the "cost" 632 * from col 0 633 */ 634 if (EL_CAN_TAB ? 635 ((unsigned int)-del > 636 (((unsigned int) where >> 3) + 637 (where & 07))) 638 : (-del > where)) { 639 terminal__putc(el, '\r');/* do a CR */ 640 el->el_cursor.h = 0; 641 goto mc_again; /* and try again */ 642 } 643 for (i = 0; i < -del; i++) 644 terminal__putc(el, '\b'); 645 } 646 } 647 } 648 el->el_cursor.h = where; /* now where is here */ 649} 650 651 652/* terminal_overwrite(): 653 * Overstrike num characters 654 * Assumes MB_FILL_CHARs are present to keep the column count correct 655 */ 656protected void 657terminal_overwrite(EditLine *el, const wchar_t *cp, size_t n) 658{ 659 if (n == 0) 660 return; 661 662 if (n > (size_t)el->el_terminal.t_size.h) { 663#ifdef DEBUG_SCREEN 664 (void) fprintf(el->el_errfile, 665 "%s: n is ridiculous: %zu\r\n", __func__, n); 666#endif /* DEBUG_SCREEN */ 667 return; 668 } 669 670 do { 671 /* terminal__putc() ignores any MB_FILL_CHARs */ 672 terminal__putc(el, *cp++); 673 el->el_cursor.h++; 674 } while (--n); 675 676 if (el->el_cursor.h >= el->el_terminal.t_size.h) { /* wrap? */ 677 if (EL_HAS_AUTO_MARGINS) { /* yes */ 678 el->el_cursor.h = 0; 679 el->el_cursor.v++; 680 if (EL_HAS_MAGIC_MARGINS) { 681 /* force the wrap to avoid the "magic" 682 * situation */ 683 wchar_t c; 684 if ((c = el->el_display[el->el_cursor.v] 685 [el->el_cursor.h]) != '\0') { 686 terminal_overwrite(el, &c, (size_t)1); 687 while (el->el_display[el->el_cursor.v] 688 [el->el_cursor.h] == MB_FILL_CHAR) 689 el->el_cursor.h++; 690 } else { 691 terminal__putc(el, ' '); 692 el->el_cursor.h = 1; 693 } 694 } 695 } else /* no wrap, but cursor stays on screen */ 696 el->el_cursor.h = el->el_terminal.t_size.h - 1; 697 } 698} 699 700 701/* terminal_deletechars(): 702 * Delete num characters 703 */ 704protected void 705terminal_deletechars(EditLine *el, int num) 706{ 707 if (num <= 0) 708 return; 709 710 if (!EL_CAN_DELETE) { 711#ifdef DEBUG_EDIT 712 (void) fprintf(el->el_errfile, " ERROR: cannot delete \n"); 713#endif /* DEBUG_EDIT */ 714 return; 715 } 716 if (num > el->el_terminal.t_size.h) { 717#ifdef DEBUG_SCREEN 718 (void) fprintf(el->el_errfile, 719 "%s: num is ridiculous: %d\r\n", __func__, num); 720#endif /* DEBUG_SCREEN */ 721 return; 722 } 723 if (GoodStr(T_DC)) /* if I have multiple delete */ 724 if ((num > 1) || !GoodStr(T_dc)) { /* if dc would be more 725 * expen. */ 726 terminal_tputs(el, tgoto(Str(T_DC), num, num), num); 727 return; 728 } 729 if (GoodStr(T_dm)) /* if I have delete mode */ 730 terminal_tputs(el, Str(T_dm), 1); 731 732 if (GoodStr(T_dc)) /* else do one at a time */ 733 while (num--) 734 terminal_tputs(el, Str(T_dc), 1); 735 736 if (GoodStr(T_ed)) /* if I have delete mode */ 737 terminal_tputs(el, Str(T_ed), 1); 738} 739 740 741/* terminal_insertwrite(): 742 * Puts terminal in insert character mode or inserts num 743 * characters in the line 744 * Assumes MB_FILL_CHARs are present to keep column count correct 745 */ 746protected void 747terminal_insertwrite(EditLine *el, wchar_t *cp, int num) 748{ 749 if (num <= 0) 750 return; 751 if (!EL_CAN_INSERT) { 752#ifdef DEBUG_EDIT 753 (void) fprintf(el->el_errfile, " ERROR: cannot insert \n"); 754#endif /* DEBUG_EDIT */ 755 return; 756 } 757 if (num > el->el_terminal.t_size.h) { 758#ifdef DEBUG_SCREEN 759 (void) fprintf(el->el_errfile, 760 "%s: num is ridiculous: %d\r\n", __func__, num); 761#endif /* DEBUG_SCREEN */ 762 return; 763 } 764 if (GoodStr(T_IC)) /* if I have multiple insert */ 765 if ((num > 1) || !GoodStr(T_ic)) { 766 /* if ic would be more expensive */ 767 terminal_tputs(el, tgoto(Str(T_IC), num, num), num); 768 terminal_overwrite(el, cp, (size_t)num); 769 /* this updates el_cursor.h */ 770 return; 771 } 772 if (GoodStr(T_im) && GoodStr(T_ei)) { /* if I have insert mode */ 773 terminal_tputs(el, Str(T_im), 1); 774 775 el->el_cursor.h += num; 776 do 777 terminal__putc(el, *cp++); 778 while (--num); 779 780 if (GoodStr(T_ip)) /* have to make num chars insert */ 781 terminal_tputs(el, Str(T_ip), 1); 782 783 terminal_tputs(el, Str(T_ei), 1); 784 return; 785 } 786 do { 787 if (GoodStr(T_ic)) /* have to make num chars insert */ 788 terminal_tputs(el, Str(T_ic), 1); 789 790 terminal__putc(el, *cp++); 791 792 el->el_cursor.h++; 793 794 if (GoodStr(T_ip)) /* have to make num chars insert */ 795 terminal_tputs(el, Str(T_ip), 1); 796 /* pad the inserted char */ 797 798 } while (--num); 799} 800 801 802/* terminal_clear_EOL(): 803 * clear to end of line. There are num characters to clear 804 */ 805protected void 806terminal_clear_EOL(EditLine *el, int num) 807{ 808 int i; 809 810 if (EL_CAN_CEOL && GoodStr(T_ce)) 811 terminal_tputs(el, Str(T_ce), 1); 812 else { 813 for (i = 0; i < num; i++) 814 terminal__putc(el, ' '); 815 el->el_cursor.h += num; /* have written num spaces */ 816 } 817} 818 819 820/* terminal_clear_screen(): 821 * Clear the screen 822 */ 823protected void 824terminal_clear_screen(EditLine *el) 825{ /* clear the whole screen and home */ 826 827 if (GoodStr(T_cl)) 828 /* send the clear screen code */ 829 terminal_tputs(el, Str(T_cl), Val(T_li)); 830 else if (GoodStr(T_ho) && GoodStr(T_cd)) { 831 terminal_tputs(el, Str(T_ho), Val(T_li)); /* home */ 832 /* clear to bottom of screen */ 833 terminal_tputs(el, Str(T_cd), Val(T_li)); 834 } else { 835 terminal__putc(el, '\r'); 836 terminal__putc(el, '\n'); 837 } 838} 839 840 841/* terminal_beep(): 842 * Beep the way the terminal wants us 843 */ 844protected void 845terminal_beep(EditLine *el) 846{ 847 if (GoodStr(T_bl)) 848 /* what termcap says we should use */ 849 terminal_tputs(el, Str(T_bl), 1); 850 else 851 terminal__putc(el, '\007'); /* an ASCII bell; ^G */ 852} 853 854 855protected void 856terminal_get(EditLine *el, const char **term) 857{ 858 *term = el->el_terminal.t_name; 859} 860 861 862/* terminal_set(): 863 * Read in the terminal capabilities from the requested terminal 864 */ 865protected int 866terminal_set(EditLine *el, const char *term) 867{ 868 int i; 869 char buf[TC_BUFSIZE]; 870 char *area; 871 const struct termcapstr *t; 872 sigset_t oset, nset; 873 int lins, cols; 874 875 (void) sigemptyset(&nset); 876 (void) sigaddset(&nset, SIGWINCH); 877 (void) sigprocmask(SIG_BLOCK, &nset, &oset); 878 879 area = buf; 880 881 882 if (term == NULL) 883 term = getenv("TERM"); 884 885 if (!term || !term[0]) 886 term = "dumb"; 887 888 if (strcmp(term, "emacs") == 0) 889 el->el_flags |= EDIT_DISABLED; 890 891 (void) memset(el->el_terminal.t_cap, 0, TC_BUFSIZE); 892 893 i = tgetent(el->el_terminal.t_cap, term); 894 895 if (i <= 0) { 896 if (i == -1) 897 (void) fprintf(el->el_errfile, 898 "Cannot read termcap database;\n"); 899 else if (i == 0) 900 (void) fprintf(el->el_errfile, 901 "No entry for terminal type \"%s\";\n", term); 902 (void) fprintf(el->el_errfile, 903 "using dumb terminal settings.\n"); 904 Val(T_co) = 80; /* do a dumb terminal */ 905 Val(T_pt) = Val(T_km) = Val(T_li) = 0; 906 Val(T_xt) = Val(T_MT); 907 for (t = tstr; t->name != NULL; t++) 908 terminal_alloc(el, t, NULL); 909 } else { 910 /* auto/magic margins */ 911 Val(T_am) = tgetflag("am"); 912 Val(T_xn) = tgetflag("xn"); 913 /* Can we tab */ 914 Val(T_pt) = tgetflag("pt"); 915 Val(T_xt) = tgetflag("xt"); 916 /* do we have a meta? */ 917 Val(T_km) = tgetflag("km"); 918 Val(T_MT) = tgetflag("MT"); 919 /* Get the size */ 920 Val(T_co) = tgetnum("co"); 921 Val(T_li) = tgetnum("li"); 922 for (t = tstr; t->name != NULL; t++) { 923 /* XXX: some systems' tgetstr needs non const */ 924 terminal_alloc(el, t, tgetstr(strchr(t->name, *t->name), 925 &area)); 926 } 927 } 928 929 if (Val(T_co) < 2) 930 Val(T_co) = 80; /* just in case */ 931 if (Val(T_li) < 1) 932 Val(T_li) = 24; 933 934 el->el_terminal.t_size.v = Val(T_co); 935 el->el_terminal.t_size.h = Val(T_li); 936 937 terminal_setflags(el); 938 939 /* get the correct window size */ 940 (void) terminal_get_size(el, &lins, &cols); 941 if (terminal_change_size(el, lins, cols) == -1) 942 return -1; 943 (void) sigprocmask(SIG_SETMASK, &oset, NULL); 944 terminal_bind_arrow(el); 945 el->el_terminal.t_name = term; 946 return i <= 0 ? -1 : 0; 947} 948 949 950/* terminal_get_size(): 951 * Return the new window size in lines and cols, and 952 * true if the size was changed. 953 */ 954protected int 955terminal_get_size(EditLine *el, int *lins, int *cols) 956{ 957 958 *cols = Val(T_co); 959 *lins = Val(T_li); 960 961#ifdef TIOCGWINSZ 962 { 963 struct winsize ws; 964 if (ioctl(el->el_infd, TIOCGWINSZ, &ws) != -1) { 965 if (ws.ws_col) 966 *cols = ws.ws_col; 967 if (ws.ws_row) 968 *lins = ws.ws_row; 969 } 970 } 971#endif 972#ifdef TIOCGSIZE 973 { 974 struct ttysize ts; 975 if (ioctl(el->el_infd, TIOCGSIZE, &ts) != -1) { 976 if (ts.ts_cols) 977 *cols = ts.ts_cols; 978 if (ts.ts_lines) 979 *lins = ts.ts_lines; 980 } 981 } 982#endif 983 return Val(T_co) != *cols || Val(T_li) != *lins; 984} 985 986 987/* terminal_change_size(): 988 * Change the size of the terminal 989 */ 990protected int 991terminal_change_size(EditLine *el, int lins, int cols) 992{ 993 /* 994 * Just in case 995 */ 996 Val(T_co) = (cols < 2) ? 80 : cols; 997 Val(T_li) = (lins < 1) ? 24 : lins; 998 999 /* re-make display buffers */ 1000 if (terminal_rebuffer_display(el) == -1) 1001 return -1; 1002 re_clear_display(el); 1003 return 0; 1004} 1005 1006 1007/* terminal_init_arrow(): 1008 * Initialize the arrow key bindings from termcap 1009 */ 1010private void 1011terminal_init_arrow(EditLine *el) 1012{ 1013 funckey_t *arrow = el->el_terminal.t_fkey; 1014 1015 arrow[A_K_DN].name = L"down"; 1016 arrow[A_K_DN].key = T_kd; 1017 arrow[A_K_DN].fun.cmd = ED_NEXT_HISTORY; 1018 arrow[A_K_DN].type = XK_CMD; 1019 1020 arrow[A_K_UP].name = L"up"; 1021 arrow[A_K_UP].key = T_ku; 1022 arrow[A_K_UP].fun.cmd = ED_PREV_HISTORY; 1023 arrow[A_K_UP].type = XK_CMD; 1024 1025 arrow[A_K_LT].name = L"left"; 1026 arrow[A_K_LT].key = T_kl; 1027 arrow[A_K_LT].fun.cmd = ED_PREV_CHAR; 1028 arrow[A_K_LT].type = XK_CMD; 1029 1030 arrow[A_K_RT].name = L"right"; 1031 arrow[A_K_RT].key = T_kr; 1032 arrow[A_K_RT].fun.cmd = ED_NEXT_CHAR; 1033 arrow[A_K_RT].type = XK_CMD; 1034 1035 arrow[A_K_HO].name = L"home"; 1036 arrow[A_K_HO].key = T_kh; 1037 arrow[A_K_HO].fun.cmd = ED_MOVE_TO_BEG; 1038 arrow[A_K_HO].type = XK_CMD; 1039 1040 arrow[A_K_EN].name = L"end"; 1041 arrow[A_K_EN].key = T_at7; 1042 arrow[A_K_EN].fun.cmd = ED_MOVE_TO_END; 1043 arrow[A_K_EN].type = XK_CMD; 1044 1045 arrow[A_K_DE].name = L"delete"; 1046 arrow[A_K_DE].key = T_kD; 1047 arrow[A_K_DE].fun.cmd = ED_DELETE_NEXT_CHAR; 1048 arrow[A_K_DE].type = XK_CMD; 1049} 1050 1051 1052/* terminal_reset_arrow(): 1053 * Reset arrow key bindings 1054 */ 1055private void 1056terminal_reset_arrow(EditLine *el) 1057{ 1058 funckey_t *arrow = el->el_terminal.t_fkey; 1059 static const wchar_t strA[] = L"\033[A"; 1060 static const wchar_t strB[] = L"\033[B"; 1061 static const wchar_t strC[] = L"\033[C"; 1062 static const wchar_t strD[] = L"\033[D"; 1063 static const wchar_t strH[] = L"\033[H"; 1064 static const wchar_t strF[] = L"\033[F"; 1065 static const wchar_t stOA[] = L"\033OA"; 1066 static const wchar_t stOB[] = L"\033OB"; 1067 static const wchar_t stOC[] = L"\033OC"; 1068 static const wchar_t stOD[] = L"\033OD"; 1069 static const wchar_t stOH[] = L"\033OH"; 1070 static const wchar_t stOF[] = L"\033OF"; 1071 1072 keymacro_add(el, strA, &arrow[A_K_UP].fun, arrow[A_K_UP].type); 1073 keymacro_add(el, strB, &arrow[A_K_DN].fun, arrow[A_K_DN].type); 1074 keymacro_add(el, strC, &arrow[A_K_RT].fun, arrow[A_K_RT].type); 1075 keymacro_add(el, strD, &arrow[A_K_LT].fun, arrow[A_K_LT].type); 1076 keymacro_add(el, strH, &arrow[A_K_HO].fun, arrow[A_K_HO].type); 1077 keymacro_add(el, strF, &arrow[A_K_EN].fun, arrow[A_K_EN].type); 1078 keymacro_add(el, stOA, &arrow[A_K_UP].fun, arrow[A_K_UP].type); 1079 keymacro_add(el, stOB, &arrow[A_K_DN].fun, arrow[A_K_DN].type); 1080 keymacro_add(el, stOC, &arrow[A_K_RT].fun, arrow[A_K_RT].type); 1081 keymacro_add(el, stOD, &arrow[A_K_LT].fun, arrow[A_K_LT].type); 1082 keymacro_add(el, stOH, &arrow[A_K_HO].fun, arrow[A_K_HO].type); 1083 keymacro_add(el, stOF, &arrow[A_K_EN].fun, arrow[A_K_EN].type); 1084 1085 if (el->el_map.type != MAP_VI) 1086 return; 1087 keymacro_add(el, &strA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type); 1088 keymacro_add(el, &strB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type); 1089 keymacro_add(el, &strC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type); 1090 keymacro_add(el, &strD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type); 1091 keymacro_add(el, &strH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type); 1092 keymacro_add(el, &strF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type); 1093 keymacro_add(el, &stOA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type); 1094 keymacro_add(el, &stOB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type); 1095 keymacro_add(el, &stOC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type); 1096 keymacro_add(el, &stOD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type); 1097 keymacro_add(el, &stOH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type); 1098 keymacro_add(el, &stOF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type); 1099} 1100 1101 1102/* terminal_set_arrow(): 1103 * Set an arrow key binding 1104 */ 1105protected int 1106terminal_set_arrow(EditLine *el, const wchar_t *name, keymacro_value_t *fun, 1107 int type) 1108{ 1109 funckey_t *arrow = el->el_terminal.t_fkey; 1110 int i; 1111 1112 for (i = 0; i < A_K_NKEYS; i++) 1113 if (wcscmp(name, arrow[i].name) == 0) { 1114 arrow[i].fun = *fun; 1115 arrow[i].type = type; 1116 return 0; 1117 } 1118 return -1; 1119} 1120 1121 1122/* terminal_clear_arrow(): 1123 * Clear an arrow key binding 1124 */ 1125protected int 1126terminal_clear_arrow(EditLine *el, const wchar_t *name) 1127{ 1128 funckey_t *arrow = el->el_terminal.t_fkey; 1129 int i; 1130 1131 for (i = 0; i < A_K_NKEYS; i++) 1132 if (wcscmp(name, arrow[i].name) == 0) { 1133 arrow[i].type = XK_NOD; 1134 return 0; 1135 } 1136 return -1; 1137} 1138 1139 1140/* terminal_print_arrow(): 1141 * Print the arrow key bindings 1142 */ 1143protected void 1144terminal_print_arrow(EditLine *el, const wchar_t *name) 1145{ 1146 int i; 1147 funckey_t *arrow = el->el_terminal.t_fkey; 1148 1149 for (i = 0; i < A_K_NKEYS; i++) 1150 if (*name == '\0' || wcscmp(name, arrow[i].name) == 0) 1151 if (arrow[i].type != XK_NOD) 1152 keymacro_kprint(el, arrow[i].name, 1153 &arrow[i].fun, arrow[i].type); 1154} 1155 1156 1157/* terminal_bind_arrow(): 1158 * Bind the arrow keys 1159 */ 1160protected void 1161terminal_bind_arrow(EditLine *el) 1162{ 1163 el_action_t *map; 1164 const el_action_t *dmap; 1165 int i, j; 1166 char *p; 1167 funckey_t *arrow = el->el_terminal.t_fkey; 1168 1169 /* Check if the components needed are initialized */ 1170 if (el->el_terminal.t_buf == NULL || el->el_map.key == NULL) 1171 return; 1172 1173 map = el->el_map.type == MAP_VI ? el->el_map.alt : el->el_map.key; 1174 dmap = el->el_map.type == MAP_VI ? el->el_map.vic : el->el_map.emacs; 1175 1176 terminal_reset_arrow(el); 1177 1178 for (i = 0; i < A_K_NKEYS; i++) { 1179 wchar_t wt_str[VISUAL_WIDTH_MAX]; 1180 wchar_t *px; 1181 size_t n; 1182 1183 p = el->el_terminal.t_str[arrow[i].key]; 1184 if (!p || !*p) 1185 continue; 1186 for (n = 0; n < VISUAL_WIDTH_MAX && p[n]; ++n) 1187 wt_str[n] = p[n]; 1188 while (n < VISUAL_WIDTH_MAX) 1189 wt_str[n++] = '\0'; 1190 px = wt_str; 1191 j = (unsigned char) *p; 1192 /* 1193 * Assign the arrow keys only if: 1194 * 1195 * 1. They are multi-character arrow keys and the user 1196 * has not re-assigned the leading character, or 1197 * has re-assigned the leading character to be 1198 * ED_SEQUENCE_LEAD_IN 1199 * 2. They are single arrow keys pointing to an 1200 * unassigned key. 1201 */ 1202 if (arrow[i].type == XK_NOD) 1203 keymacro_clear(el, map, px); 1204 else { 1205 if (p[1] && (dmap[j] == map[j] || 1206 map[j] == ED_SEQUENCE_LEAD_IN)) { 1207 keymacro_add(el, px, &arrow[i].fun, 1208 arrow[i].type); 1209 map[j] = ED_SEQUENCE_LEAD_IN; 1210 } else if (map[j] == ED_UNASSIGNED) { 1211 keymacro_clear(el, map, px); 1212 if (arrow[i].type == XK_CMD) 1213 map[j] = arrow[i].fun.cmd; 1214 else 1215 keymacro_add(el, px, &arrow[i].fun, 1216 arrow[i].type); 1217 } 1218 } 1219 } 1220} 1221 1222/* terminal_putc(): 1223 * Add a character 1224 */ 1225private int 1226terminal_putc(int c) 1227{ 1228 if (terminal_outfile == NULL) 1229 return -1; 1230 return fputc(c, terminal_outfile); 1231} 1232 1233private void 1234terminal_tputs(EditLine *el, const char *cap, int affcnt) 1235{ 1236#ifdef _REENTRANT 1237 pthread_mutex_lock(&terminal_mutex); 1238#endif 1239 terminal_outfile = el->el_outfile; 1240 (void)tputs(cap, affcnt, terminal_putc); 1241#ifdef _REENTRANT 1242 pthread_mutex_unlock(&terminal_mutex); 1243#endif 1244} 1245 1246/* terminal__putc(): 1247 * Add a character 1248 */ 1249protected int 1250terminal__putc(EditLine *el, wint_t c) 1251{ 1252 char buf[MB_LEN_MAX +1]; 1253 ssize_t i; 1254 if (c == (wint_t)MB_FILL_CHAR) 1255 return 0; 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 */ 1266protected void 1267terminal__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 */ 1276protected void 1277terminal_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 */ 1292protected int 1293/*ARGSUSED*/ 1294terminal_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_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 */ 1334protected int 1335/*ARGSUSED*/ 1336terminal_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 */ 1413protected int 1414/*ARGSUSED*/ 1415terminal_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 */ 1467protected int 1468/*ARGSUSED*/ 1469terminal_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