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