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