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