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