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