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