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