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