Skip to content
Snippets Groups Projects
term.c 37.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • Mark Spencer's avatar
    Mark Spencer committed
    /*	$NetBSD: term.c,v 1.35 2002/03/18 16:00:59 christos Exp $	*/
    
    /*-
     * Copyright (c) 1992, 1993
     *	The Regents of the University of California.  All rights reserved.
     *
     * This code is derived from software contributed to Berkeley by
     * Christos Zoulas of Cornell University.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     * 1. Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     * 2. Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     * 3. All advertising materials mentioning features or use of this software
     *    must display the following acknowledgement:
     *	This product includes software developed by the University of
     *	California, Berkeley and its contributors.
     * 4. Neither the name of the University nor the names of its contributors
     *    may be used to endorse or promote products derived from this software
     *    without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     * SUCH DAMAGE.
     */
    
    #include "config.h"
    #if !defined(lint) && !defined(SCCSID)
    #if 0
    static char sccsid[] = "@(#)term.c	8.2 (Berkeley) 4/30/95";
    #else
    __RCSID("$NetBSD: term.c,v 1.35 2002/03/18 16:00:59 christos Exp $");
    #endif
    #endif /* not lint && not SCCSID */
    
    /*
     * term.c: Editor/termcap-curses interface
     *	   We have to declare a static variable here, since the
     *	   termcap putchar routine does not take an argument!
     */
    #include <stdio.h>
    #include <signal.h>
    #include <string.h>
    #include <stdlib.h>
    #include <unistd.h>
    #ifdef HAVE_TERMCAP_H
    #include <termcap.h>
    #endif
    #ifdef HAVE_CURSES_H
    #include <curses.h>
    #endif
    #ifdef HAVE_NCURSES_H
    #include <ncurses.h>
    #endif
    /* Solaris's term.h does horrid things. */
    #if (defined(HAVE_TERM_H) && !defined(SUNOS))
    #include <term.h>
    #endif
    #include <sys/types.h>
    #include <sys/ioctl.h>
    
    #include "el.h"
    
    /*
     * IMPORTANT NOTE: these routines are allowed to look at the current screen
     * and the current possition assuming that it is correct.  If this is not
     * true, then the update will be WRONG!  This is (should be) a valid
     * assumption...
     */
    
    #define	TC_BUFSIZE	2048
    
    #define	GoodStr(a)	(el->el_term.t_str[a] != NULL && \
    			    el->el_term.t_str[a][0] != '\0')
    #define	Str(a)		el->el_term.t_str[a]
    #define	Val(a)		el->el_term.t_val[a]
    
    #ifdef notdef
    private const struct {
    	const char *b_name;
    	int b_rate;
    } baud_rate[] = {
    #ifdef B0
    	{ "0", B0 },
    #endif
    #ifdef B50
    	{ "50", B50 },
    #endif
    #ifdef B75
    	{ "75", B75 },
    #endif
    #ifdef B110
    	{ "110", B110 },
    #endif
    #ifdef B134
    	{ "134", B134 },
    #endif
    #ifdef B150
    	{ "150", B150 },
    #endif
    #ifdef B200
    	{ "200", B200 },
    #endif
    #ifdef B300
    	{ "300", B300 },
    #endif
    #ifdef B600
    	{ "600", B600 },
    #endif
    #ifdef B900
    	{ "900", B900 },
    #endif
    #ifdef B1200
    	{ "1200", B1200 },
    #endif
    #ifdef B1800
    	{ "1800", B1800 },
    #endif
    #ifdef B2400
    	{ "2400", B2400 },
    #endif
    #ifdef B3600
    	{ "3600", B3600 },
    #endif
    #ifdef B4800
    	{ "4800", B4800 },
    #endif
    #ifdef B7200
    	{ "7200", B7200 },
    #endif
    #ifdef B9600
    	{ "9600", B9600 },
    #endif
    #ifdef EXTA
    	{ "19200", EXTA },
    #endif
    #ifdef B19200
    	{ "19200", B19200 },
    #endif
    #ifdef EXTB
    	{ "38400", EXTB },
    #endif
    #ifdef B38400
    	{ "38400", B38400 },
    #endif
    	{ NULL, 0 }
    };
    #endif
    
    private const struct termcapstr {
    	const char *name;
    	const char *long_name;
    } tstr[] = {
    #define	T_al	0
    	{ "al", "add new blank line" },
    #define	T_bl	1
    	{ "bl", "audible bell" },
    #define	T_cd	2
    	{ "cd", "clear to bottom" },
    #define	T_ce	3
    	{ "ce", "clear to end of line" },
    #define	T_ch	4
    	{ "ch", "cursor to horiz pos" },
    #define	T_cl	5
    	{ "cl", "clear screen" },
    #define	T_dc	6
    	{ "dc", "delete a character" },
    #define	T_dl	7
    	{ "dl", "delete a line" },
    #define	T_dm	8
    	{ "dm", "start delete mode" },
    #define	T_ed	9
    	{ "ed", "end delete mode" },
    #define	T_ei	10
    	{ "ei", "end insert mode" },
    #define	T_fs	11
    	{ "fs", "cursor from status line" },
    #define	T_ho	12
    	{ "ho", "home cursor" },
    #define	T_ic	13
    	{ "ic", "insert character" },
    #define	T_im	14
    	{ "im", "start insert mode" },
    #define	T_ip	15
    	{ "ip", "insert padding" },
    #define	T_kd	16
    	{ "kd", "sends cursor down" },
    #define	T_kl	17
    	{ "kl", "sends cursor left" },
    #define	T_kr	18
    	{ "kr", "sends cursor right" },
    #define	T_ku	19
    	{ "ku", "sends cursor up" },
    #define	T_md	20
    	{ "md", "begin bold" },
    #define	T_me	21
    	{ "me", "end attributes" },
    #define	T_nd	22
    	{ "nd", "non destructive space" },
    #define	T_se	23
    	{ "se", "end standout" },
    #define	T_so	24
    	{ "so", "begin standout" },
    #define	T_ts	25
    	{ "ts", "cursor to status line" },
    #define	T_up	26
    	{ "up", "cursor up one" },
    #define	T_us	27
    	{ "us", "begin underline" },
    #define	T_ue	28
    	{ "ue", "end underline" },
    #define	T_vb	29
    	{ "vb", "visible bell" },
    #define	T_DC	30
    	{ "DC", "delete multiple chars" },
    #define	T_DO	31
    	{ "DO", "cursor down multiple" },
    #define	T_IC	32
    	{ "IC", "insert multiple chars" },
    #define	T_LE	33
    	{ "LE", "cursor left multiple" },
    #define	T_RI	34
    	{ "RI", "cursor right multiple" },
    #define	T_UP	35
    	{ "UP", "cursor up multiple" },
    #define	T_kh	36
    	{ "kh", "send cursor home" },
    #define	T_at7	37
    	{ "@7", "send cursor end" },
    #define	T_str	38
    	{ NULL, NULL }
    };
    
    private const struct termcapval {
    	const char *name;
    	const char *long_name;
    } tval[] = {
    #define	T_am	0
    	{ "am", "has automatic margins" },
    #define	T_pt	1
    	{ "pt", "has physical tabs" },
    #define	T_li	2
    	{ "li", "Number of lines" },
    #define	T_co	3
    	{ "co", "Number of columns" },
    #define	T_km	4
    	{ "km", "Has meta key" },
    #define	T_xt	5
    	{ "xt", "Tab chars destructive" },
    #define	T_xn	6
    	{ "xn", "newline ignored at right margin" },
    #define	T_MT	7
    	{ "MT", "Has meta key" },			/* XXX? */
    #define	T_val	8
    	{ NULL, NULL, }
    };
    /* do two or more of the attributes use me */
    
    private void	term_setflags(EditLine *);
    private int	term_rebuffer_display(EditLine *);
    private void	term_free_display(EditLine *);
    private int	term_alloc_display(EditLine *);
    private void	term_alloc(EditLine *, const struct termcapstr *, const char *);
    private void	term_init_arrow(EditLine *);
    private void	term_reset_arrow(EditLine *);
    
    
    private FILE *term_outfile = NULL;	/* XXX: How do we fix that? */
    
    
    /* term_setflags():
     *	Set the terminal capability flags
     */
    private void
    term_setflags(EditLine *el)
    {
    	EL_FLAGS = 0;
    	if (el->el_tty.t_tabs)
    		EL_FLAGS |= (Val(T_pt) && !Val(T_xt)) ? TERM_CAN_TAB : 0;
    
    	EL_FLAGS |= (Val(T_km) || Val(T_MT)) ? TERM_HAS_META : 0;
    	EL_FLAGS |= GoodStr(T_ce) ? TERM_CAN_CEOL : 0;
    	EL_FLAGS |= (GoodStr(T_dc) || GoodStr(T_DC)) ? TERM_CAN_DELETE : 0;
    	EL_FLAGS |= (GoodStr(T_im) || GoodStr(T_ic) || GoodStr(T_IC)) ?
    	    TERM_CAN_INSERT : 0;
    	EL_FLAGS |= (GoodStr(T_up) || GoodStr(T_UP)) ? TERM_CAN_UP : 0;
    	EL_FLAGS |= Val(T_am) ? TERM_HAS_AUTO_MARGINS : 0;
    	EL_FLAGS |= Val(T_xn) ? TERM_HAS_MAGIC_MARGINS : 0;
    
    	if (GoodStr(T_me) && GoodStr(T_ue))
    		EL_FLAGS |= (strcmp(Str(T_me), Str(T_ue)) == 0) ?
    		    TERM_CAN_ME : 0;
    	else
    		EL_FLAGS &= ~TERM_CAN_ME;
    	if (GoodStr(T_me) && GoodStr(T_se))
    		EL_FLAGS |= (strcmp(Str(T_me), Str(T_se)) == 0) ?
    		    TERM_CAN_ME : 0;
    
    
    #ifdef DEBUG_SCREEN
    	if (!EL_CAN_UP) {
    		(void) fprintf(el->el_errfile,
    		    "WARNING: Your terminal cannot move up.\n");
    		(void) fprintf(el->el_errfile,
    		    "Editing may be odd for long lines.\n");
    	}
    	if (!EL_CAN_CEOL)
    		(void) fprintf(el->el_errfile, "no clear EOL capability.\n");
    	if (!EL_CAN_DELETE)
    		(void) fprintf(el->el_errfile, "no delete char capability.\n");
    	if (!EL_CAN_INSERT)
    		(void) fprintf(el->el_errfile, "no insert char capability.\n");
    #endif /* DEBUG_SCREEN */
    }
    
    
    /* term_init():
     *	Initialize the terminal stuff
     */
    protected int
    term_init(EditLine *el)
    {
    
    	el->el_term.t_buf = (char *) el_malloc(TC_BUFSIZE);
    	if (el->el_term.t_buf == NULL)
    		return (-1);
    	el->el_term.t_cap = (char *) el_malloc(TC_BUFSIZE);
    	if (el->el_term.t_cap == NULL)
    		return (-1);
    	el->el_term.t_fkey = (fkey_t *) el_malloc(A_K_NKEYS * sizeof(fkey_t));
    	if (el->el_term.t_fkey == NULL)
    		return (-1);
    	el->el_term.t_loc = 0;
    	el->el_term.t_str = (char **) el_malloc(T_str * sizeof(char *));
    	if (el->el_term.t_str == NULL)
    		return (-1);
    	(void) memset(el->el_term.t_str, 0, T_str * sizeof(char *));
    	el->el_term.t_val = (int *) el_malloc(T_val * sizeof(int));
    	if (el->el_term.t_val == NULL)
    		return (-1);
    	(void) memset(el->el_term.t_val, 0, T_val * sizeof(int));
    	term_outfile = el->el_outfile;
    	(void) term_set(el, NULL);
    	term_init_arrow(el);
    	return (0);
    }
    /* term_end():
     *	Clean up the terminal stuff
     */
    protected void
    term_end(EditLine *el)
    {
    
    	el_free((ptr_t) el->el_term.t_buf);
    	el->el_term.t_buf = NULL;
    	el_free((ptr_t) el->el_term.t_cap);
    	el->el_term.t_cap = NULL;
    
    	el_free((ptr_t) el->el_term.t_fkey);
    	el->el_term.t_fkey = NULL;
    
    Mark Spencer's avatar
    Mark Spencer committed
    372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
    	el->el_term.t_loc = 0;
    	el_free((ptr_t) el->el_term.t_str);
    	el->el_term.t_str = NULL;
    	el_free((ptr_t) el->el_term.t_val);
    	el->el_term.t_val = NULL;
    	term_free_display(el);
    }
    
    
    /* term_alloc():
     *	Maintain a string pool for termcap strings
     */
    private void
    term_alloc(EditLine *el, const struct termcapstr *t, const char *cap)
    {
    	char termbuf[TC_BUFSIZE];
    	int tlen, clen;
    	char **tlist = el->el_term.t_str;
    	char **tmp, **str = &tlist[t - tstr];
    
    	if (cap == NULL || *cap == '\0') {
    		*str = NULL;
    		return;
    	} else
    		clen = strlen(cap);
    
    	tlen = *str == NULL ? 0 : strlen(*str);
    
    	/*
             * New string is shorter; no need to allocate space
             */
    	if (clen <= tlen) {
    		(void) strcpy(*str, cap);	/* XXX strcpy is safe */
    		return;
    	}
    	/*
             * New string is longer; see if we have enough space to append
             */
    	if (el->el_term.t_loc + 3 < TC_BUFSIZE) {
    						/* XXX strcpy is safe */
    		(void) strcpy(*str = &el->el_term.t_buf[el->el_term.t_loc],
    		    cap);
    		el->el_term.t_loc += clen + 1;	/* one for \0 */
    		return;
    	}
    	/*
             * Compact our buffer; no need to check compaction, cause we know it
             * fits...
             */
    	tlen = 0;
    	for (tmp = tlist; tmp < &tlist[T_str]; tmp++)
    		if (*tmp != NULL && *tmp != '\0' && *tmp != *str) {
    			char *ptr;
    
    			for (ptr = *tmp; *ptr != '\0'; termbuf[tlen++] = *ptr++)
    				continue;
    			termbuf[tlen++] = '\0';
    		}
    	memcpy(el->el_term.t_buf, termbuf, TC_BUFSIZE);
    	el->el_term.t_loc = tlen;
    	if (el->el_term.t_loc + 3 >= TC_BUFSIZE) {
    		(void) fprintf(el->el_errfile,
    		    "Out of termcap string space.\n");
    		return;
    	}
    					/* XXX strcpy is safe */
    	(void) strcpy(*str = &el->el_term.t_buf[el->el_term.t_loc], cap);
    	el->el_term.t_loc += clen + 1;	/* one for \0 */
    	return;
    }
    
    
    /* term_rebuffer_display():
     *	Rebuffer the display after the screen changed size
     */
    private int
    term_rebuffer_display(EditLine *el)
    {
    	coord_t *c = &el->el_term.t_size;
    
    	term_free_display(el);
    
    	c->h = Val(T_co);
    	c->v = Val(T_li);
    
    	if (term_alloc_display(el) == -1)
    		return (-1);
    	return (0);
    }
    
    
    /* term_alloc_display():
     *	Allocate a new display.
     */
    private int
    term_alloc_display(EditLine *el)
    {
    	int i;
    	char **b;
    	coord_t *c = &el->el_term.t_size;
    
    	b = (char **) el_malloc((size_t) (sizeof(char *) * (c->v + 1)));
    	if (b == NULL)
    		return (-1);
    	for (i = 0; i < c->v; i++) {
    		b[i] = (char *) el_malloc((size_t) (sizeof(char) * (c->h + 1)));
    		if (b[i] == NULL)
    			return (-1);
    	}
    	b[c->v] = NULL;
    	el->el_display = b;
    
    	b = (char **) el_malloc((size_t) (sizeof(char *) * (c->v + 1)));
    	if (b == NULL)
    		return (-1);
    	for (i = 0; i < c->v; i++) {
    		b[i] = (char *) el_malloc((size_t) (sizeof(char) * (c->h + 1)));
    		if (b[i] == NULL)
    			return (-1);
    	}
    	b[c->v] = NULL;
    	el->el_vdisplay = b;
    	return (0);
    }
    
    
    /* term_free_display():
     *	Free the display buffers
     */
    private void
    term_free_display(EditLine *el)
    {
    	char **b;
    	char **bufp;
    
    	b = el->el_display;
    	el->el_display = NULL;
    	if (b != NULL) {
    		for (bufp = b; *bufp != NULL; bufp++)
    			el_free((ptr_t) * bufp);
    		el_free((ptr_t) b);
    	}
    	b = el->el_vdisplay;
    	el->el_vdisplay = NULL;
    	if (b != NULL) {
    		for (bufp = b; *bufp != NULL; bufp++)
    			el_free((ptr_t) * bufp);
    		el_free((ptr_t) b);
    	}
    }
    
    
    /* term_move_to_line():
     *	move to line <where> (first line == 0)
     * 	as efficiently as possible
     */
    protected void
    term_move_to_line(EditLine *el, int where)
    {
    	int del;
    
    	if (where == el->el_cursor.v)
    		return;
    
    	if (where > el->el_term.t_size.v) {
    #ifdef DEBUG_SCREEN
    		(void) fprintf(el->el_errfile,
    		    "term_move_to_line: where is ridiculous: %d\r\n", where);
    #endif /* DEBUG_SCREEN */
    		return;
    	}
    	if ((del = where - el->el_cursor.v) > 0) {
    		while (del > 0) {
    			if (EL_HAS_AUTO_MARGINS &&
    			    el->el_display[el->el_cursor.v][0] != '\0') {
    				/* move without newline */
    				term_move_to_char(el, el->el_term.t_size.h - 1);
    				term_overwrite(el,
    				    &el->el_display[el->el_cursor.v][el->el_cursor.h],
    				    1);
    				/* updates Cursor */
    				del--;
    			} else {
    				if ((del > 1) && GoodStr(T_DO)) {
    					(void) tputs(tgoto(Str(T_DO), del, del),
    					    del, term__putc);
    					del = 0;
    				} else {
    					for (; del > 0; del--)
    						term__putc('\n');
    					/* because the \n will become \r\n */
    					el->el_cursor.h = 0;
    				}
    			}
    		}
    	} else {		/* del < 0 */
    		if (GoodStr(T_UP) && (-del > 1 || !GoodStr(T_up)))
    			(void) tputs(tgoto(Str(T_UP), -del, -del), -del,
    			    term__putc);
    		else {
    			if (GoodStr(T_up))
    				for (; del < 0; del++)
    					(void) tputs(Str(T_up), 1, term__putc);
    		}
    	}
    	el->el_cursor.v = where;/* now where is here */
    }
    
    
    /* term_move_to_char():
     *	Move to the character position specified
     */
    protected void
    term_move_to_char(EditLine *el, int where)
    {
    	int del, i;
    
    mc_again:
    	if (where == el->el_cursor.h)
    		return;
    
    	if (where > el->el_term.t_size.h) {
    #ifdef DEBUG_SCREEN
    		(void) fprintf(el->el_errfile,
    		    "term_move_to_char: where is riduculous: %d\r\n", where);
    #endif /* DEBUG_SCREEN */
    		return;
    	}
    	if (!where) {		/* if where is first column */
    		term__putc('\r');	/* do a CR */
    		el->el_cursor.h = 0;
    		return;
    	}
    	del = where - el->el_cursor.h;
    
    	if ((del < -4 || del > 4) && GoodStr(T_ch))
    		/* go there directly */
    		(void) tputs(tgoto(Str(T_ch), where, where), where, term__putc);
    	else {
    		if (del > 0) {	/* moving forward */
    			if ((del > 4) && GoodStr(T_RI))
    				(void) tputs(tgoto(Str(T_RI), del, del),
    				    del, term__putc);
    			else {
    					/* if I can do tabs, use them */
    				if (EL_CAN_TAB) {
    					if ((el->el_cursor.h & 0370) !=
    					    (where & 0370)) {
    						/* if not within tab stop */
    						for (i =
    						    (el->el_cursor.h & 0370);
    						    i < (where & 0370);
    						    i += 8)
    							term__putc('\t');	
    							/* then tab over */
    						el->el_cursor.h = where & 0370;
    					}
    				}
    				/*
    				 * it's usually cheaper to just write the
    				 * chars, so we do.
    				 */
    				/*
    				 * NOTE THAT term_overwrite() WILL CHANGE
    				 * el->el_cursor.h!!!
    				 */
    				term_overwrite(el,
    				    &el->el_display[el->el_cursor.v][el->el_cursor.h],
    				    where - el->el_cursor.h);
    
    			}
    		} else {	/* del < 0 := moving backward */
    			if ((-del > 4) && GoodStr(T_LE))
    				(void) tputs(tgoto(Str(T_LE), -del, -del),
    				    -del, term__putc);
    			else {	/* can't go directly there */
    				/*
    				 * if the "cost" is greater than the "cost"
    				 * from col 0
    				 */
    				if (EL_CAN_TAB ?
    				    (-del > (((unsigned int) where >> 3) +
    				     (where & 07)))
    				    : (-del > where)) {
    					term__putc('\r');	/* do a CR */
    					el->el_cursor.h = 0;
    					goto mc_again;	/* and try again */
    				}
    				for (i = 0; i < -del; i++)
    					term__putc('\b');
    			}
    		}
    	}
    	el->el_cursor.h = where;		/* now where is here */
    }
    
    
    /* term_overwrite():
     *	Overstrike num characters
     */
    protected void
    term_overwrite(EditLine *el, const char *cp, int n)
    {
    	if (n <= 0)
    		return;		/* catch bugs */
    
    	if (n > el->el_term.t_size.h) {
    #ifdef DEBUG_SCREEN
    		(void) fprintf(el->el_errfile,
    		    "term_overwrite: n is riduculous: %d\r\n", n);
    #endif /* DEBUG_SCREEN */
    		return;
    	}
    	do {
    		term__putc(*cp++);
    		el->el_cursor.h++;
    	} while (--n);
    
    	if (el->el_cursor.h >= el->el_term.t_size.h) {	/* wrap? */
    		if (EL_HAS_AUTO_MARGINS) {	/* yes */
    			el->el_cursor.h = 0;
    			el->el_cursor.v++;
    			if (EL_HAS_MAGIC_MARGINS) {
    				/* force the wrap to avoid the "magic"
    				 * situation */
    				char c;
    				if ((c = el->el_display[el->el_cursor.v][el->el_cursor.h])
    				    != '\0')
    					term_overwrite(el, &c, 1);
    				else
    					term__putc(' ');
    				el->el_cursor.h = 1;
    			}
    		} else		/* no wrap, but cursor stays on screen */
    			el->el_cursor.h = el->el_term.t_size.h;
    	}
    }
    
    
    /* term_deletechars():
     *	Delete num characters
     */
    protected void
    term_deletechars(EditLine *el, int num)
    {
    	if (num <= 0)
    		return;
    
    	if (!EL_CAN_DELETE) {
    #ifdef DEBUG_EDIT
    		(void) fprintf(el->el_errfile, "   ERROR: cannot delete   \n");
    #endif /* DEBUG_EDIT */
    		return;
    	}
    	if (num > el->el_term.t_size.h) {
    #ifdef DEBUG_SCREEN
    		(void) fprintf(el->el_errfile,
    		    "term_deletechars: num is riduculous: %d\r\n", num);
    #endif /* DEBUG_SCREEN */
    		return;
    	}
    	if (GoodStr(T_DC))	/* if I have multiple delete */
    		if ((num > 1) || !GoodStr(T_dc)) {	/* if dc would be more
    							 * expen. */
    			(void) tputs(tgoto(Str(T_DC), num, num),
    			    num, term__putc);
    			return;
    		}
    	if (GoodStr(T_dm))	/* if I have delete mode */
    		(void) tputs(Str(T_dm), 1, term__putc);
    
    	if (GoodStr(T_dc))	/* else do one at a time */
    		while (num--)
    			(void) tputs(Str(T_dc), 1, term__putc);
    
    	if (GoodStr(T_ed))	/* if I have delete mode */
    		(void) tputs(Str(T_ed), 1, term__putc);
    }
    
    
    /* term_insertwrite():
     *	Puts terminal in insert character mode or inserts num
     *	characters in the line
     */
    protected void
    term_insertwrite(EditLine *el, char *cp, int num)
    {
    	if (num <= 0)
    		return;
    	if (!EL_CAN_INSERT) {
    #ifdef DEBUG_EDIT
    		(void) fprintf(el->el_errfile, "   ERROR: cannot insert   \n");
    #endif /* DEBUG_EDIT */
    		return;
    	}
    	if (num > el->el_term.t_size.h) {
    #ifdef DEBUG_SCREEN
    		(void) fprintf(el->el_errfile,
    		    "StartInsert: num is riduculous: %d\r\n", num);
    #endif /* DEBUG_SCREEN */
    		return;
    	}
    	if (GoodStr(T_IC))	/* if I have multiple insert */
    		if ((num > 1) || !GoodStr(T_ic)) {
    				/* if ic would be more expensive */
    			(void) tputs(tgoto(Str(T_IC), num, num),
    			    num, term__putc);
    			term_overwrite(el, cp, num);
    				/* this updates el_cursor.h */
    			return;
    		}
    	if (GoodStr(T_im) && GoodStr(T_ei)) {	/* if I have insert mode */
    		(void) tputs(Str(T_im), 1, term__putc);
    
    		el->el_cursor.h += num;
    		do
    			term__putc(*cp++);
    		while (--num);
    
    		if (GoodStr(T_ip))	/* have to make num chars insert */
    			(void) tputs(Str(T_ip), 1, term__putc);
    
    		(void) tputs(Str(T_ei), 1, term__putc);
    		return;
    	}
    	do {
    		if (GoodStr(T_ic))	/* have to make num chars insert */
    			(void) tputs(Str(T_ic), 1, term__putc);
    					/* insert a char */
    
    		term__putc(*cp++);
    
    		el->el_cursor.h++;
    
    		if (GoodStr(T_ip))	/* have to make num chars insert */
    			(void) tputs(Str(T_ip), 1, term__putc);
    					/* pad the inserted char */
    
    	} while (--num);
    }
    
    
    /* term_clear_EOL():
     *	clear to end of line.  There are num characters to clear
     */
    protected void
    term_clear_EOL(EditLine *el, int num)
    {
    	int i;
    
    	if (EL_CAN_CEOL && GoodStr(T_ce))
    		(void) tputs(Str(T_ce), 1, term__putc);
    	else {
    		for (i = 0; i < num; i++)
    			term__putc(' ');
    		el->el_cursor.h += num;	/* have written num spaces */
    	}
    }
    
    
    /* term_clear_screen():
     *	Clear the screen
     */
    protected void
    term_clear_screen(EditLine *el)
    {				/* clear the whole screen and home */
    
    	if (GoodStr(T_cl))
    		/* send the clear screen code */
    		(void) tputs(Str(T_cl), Val(T_li), term__putc);
    	else if (GoodStr(T_ho) && GoodStr(T_cd)) {
    		(void) tputs(Str(T_ho), Val(T_li), term__putc);	/* home */
    		/* clear to bottom of screen */
    		(void) tputs(Str(T_cd), Val(T_li), term__putc);
    	} else {
    		term__putc('\r');
    		term__putc('\n');
    	}
    }
    
    
    /* term_beep():
     *	Beep the way the terminal wants us
     */
    protected void
    term_beep(EditLine *el)
    {
    	if (GoodStr(T_bl))
    		/* what termcap says we should use */
    		(void) tputs(Str(T_bl), 1, term__putc);
    	else
    		term__putc('\007');	/* an ASCII bell; ^G */
    }
    
    
    #ifdef notdef
    /* term_clear_to_bottom():
     *	Clear to the bottom of the screen
     */
    protected void
    term_clear_to_bottom(EditLine *el)
    {
    	if (GoodStr(T_cd))
    		(void) tputs(Str(T_cd), Val(T_li), term__putc);
    	else if (GoodStr(T_ce))
    		(void) tputs(Str(T_ce), Val(T_li), term__putc);
    }
    #endif
    
    
    /* term_set():
     *	Read in the terminal capabilities from the requested terminal
     */
    protected int
    term_set(EditLine *el, const char *term)
    {
    	int i;
    	char buf[TC_BUFSIZE];
    	char *area;
    	const struct termcapstr *t;
    	sigset_t oset, nset;
    	int lins, cols;
    
    	(void) sigemptyset(&nset);
    	(void) sigaddset(&nset, SIGWINCH);
    	(void) sigprocmask(SIG_BLOCK, &nset, &oset);
    
    	area = buf;
    
    
    	if (term == NULL)
    		term = getenv("TERM");
    
    	if (!term || !term[0])
    		term = "dumb";
    
    	if (strcmp(term, "emacs") == 0)
    		el->el_flags |= EDIT_DISABLED;
    
    	memset(el->el_term.t_cap, 0, TC_BUFSIZE);
    
    	i = tgetent(el->el_term.t_cap, term);
    
    	if (i <= 0) {
    		if (i == -1)
    			(void) fprintf(el->el_errfile,
    			    "Cannot read termcap database;\n");
    		else if (i == 0)
    			(void) fprintf(el->el_errfile,
    			    "No entry for terminal type \"%s\";\n", term);
    		(void) fprintf(el->el_errfile,
    		    "using dumb terminal settings.\n");
    		Val(T_co) = 80;	/* do a dumb terminal */
    		Val(T_pt) = Val(T_km) = Val(T_li) = 0;
    		Val(T_xt) = Val(T_MT);
    		for (t = tstr; t->name != NULL; t++)
    			term_alloc(el, t, NULL);
    	} else {
    		/* auto/magic margins */
    		Val(T_am) = tgetflag("am");
    		Val(T_xn) = tgetflag("xn");
    		/* Can we tab */
    		Val(T_pt) = tgetflag("pt");
    		Val(T_xt) = tgetflag("xt");
    		/* do we have a meta? */
    		Val(T_km) = tgetflag("km");
    		Val(T_MT) = tgetflag("MT");
    		/* Get the size */
    		Val(T_co) = tgetnum("co");
    		Val(T_li) = tgetnum("li");
    		for (t = tstr; t->name != NULL; t++)
    
    Mark Spencer's avatar
    Mark Spencer committed
    			term_alloc(el, t, tgetstr((char *)t->name, &area));
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
    	if (Val(T_co) < 2)
    		Val(T_co) = 80;	/* just in case */
    	if (Val(T_li) < 1)
    		Val(T_li) = 24;
    
    	el->el_term.t_size.v = Val(T_co);
    	el->el_term.t_size.h = Val(T_li);
    
    	term_setflags(el);
    
    				/* get the correct window size */
    	(void) term_get_size(el, &lins, &cols);
    	if (term_change_size(el, lins, cols) == -1)
    		return (-1);
    	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
    	term_bind_arrow(el);
    	return (i <= 0 ? -1 : 0);
    }
    
    
    /* term_get_size():
     *	Return the new window size in lines and cols, and
     *	true if the size was changed.
     */
    protected int
    term_get_size(EditLine *el, int *lins, int *cols)
    {
    
    	*cols = Val(T_co);
    	*lins = Val(T_li);
    
    #ifdef TIOCGWINSZ
    	{
    		struct winsize ws;
    		if (ioctl(el->el_infd, TIOCGWINSZ, (ioctl_t) & ws) != -1) {
    			if (ws.ws_col)
    				*cols = ws.ws_col;
    			if (ws.ws_row)
    				*lins = ws.ws_row;
    		}
    	}
    #endif
    #ifdef TIOCGSIZE
    	{
    		struct ttysize ts;
    		if (ioctl(el->el_infd, TIOCGSIZE, (ioctl_t) & ts) != -1) {
    			if (ts.ts_cols)
    				*cols = ts.ts_cols;
    			if (ts.ts_lines)
    				*lins = ts.ts_lines;
    		}
    	}
    #endif
    	return (Val(T_co) != *cols || Val(T_li) != *lins);
    }