1 /* $OpenBSD: terminal.c,v 1.18 2017/04/12 18:24:37 tb Exp $ */
2 /* $NetBSD: terminal.c,v 1.17 2016/02/15 15:35:03 christos Exp $ */
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Christos Zoulas of Cornell University.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * terminal.c: Editor/termcap-curses interface
40 * We have to declare a static variable here, since the
41 * termcap putchar routine does not take an argument!
43 #include <sys/types.h>
44 #include <sys/ioctl.h>
59 /* Solaris's term.h does horrid things. */
61 #if defined(HAVE_TERM_H) && !defined(__sun)
73 * IMPORTANT NOTE: these routines are allowed to look at the current screen
74 * and the current position assuming that it is correct. If this is not
75 * true, then the update will be WRONG! This is (should be) a valid
79 #define TC_BUFSIZE 2048
81 #define GoodStr(a) (el->el_terminal.t_str[a] != NULL && \
82 el->el_terminal.t_str[a][0] != '\0')
83 #define Str(a) el->el_terminal.t_str[a]
84 #define Val(a) el->el_terminal.t_val[a]
86 static const struct termcapstr
{
88 const char *long_name
;
91 { "al", "add new blank line" },
93 { "bl", "audible bell" },
95 { "cd", "clear to bottom" },
97 { "ce", "clear to end of line" },
99 { "ch", "cursor to horiz pos" },
101 { "cl", "clear screen" },
103 { "dc", "delete a character" },
105 { "dl", "delete a line" },
107 { "dm", "start delete mode" },
109 { "ed", "end delete mode" },
111 { "ei", "end insert mode" },
113 { "fs", "cursor from status line" },
115 { "ho", "home cursor" },
117 { "ic", "insert character" },
119 { "im", "start insert mode" },
121 { "ip", "insert padding" },
123 { "kd", "sends cursor down" },
125 { "kl", "sends cursor left" },
127 { "kr", "sends cursor right" },
129 { "ku", "sends cursor up" },
131 { "md", "begin bold" },
133 { "me", "end attributes" },
135 { "nd", "non destructive space" },
137 { "se", "end standout" },
139 { "so", "begin standout" },
141 { "ts", "cursor to status line" },
143 { "up", "cursor up one" },
145 { "us", "begin underline" },
147 { "ue", "end underline" },
149 { "vb", "visible bell" },
151 { "DC", "delete multiple chars" },
153 { "DO", "cursor down multiple" },
155 { "IC", "insert multiple chars" },
157 { "LE", "cursor left multiple" },
159 { "RI", "cursor right multiple" },
161 { "UP", "cursor up multiple" },
163 { "kh", "send cursor home" },
165 { "@7", "send cursor end" },
170 static const struct termcapval
{
172 const char *long_name
;
175 { "am", "has automatic margins" },
177 { "pt", "has physical tabs" },
179 { "li", "Number of lines" },
181 { "co", "Number of columns" },
183 { "km", "Has meta key" },
185 { "xt", "Tab chars destructive" },
187 { "xn", "newline ignored at right margin" },
189 { "MT", "Has meta key" }, /* XXX? */
193 /* do two or more of the attributes use me */
195 static void terminal_setflags(EditLine
*);
196 static int terminal_rebuffer_display(EditLine
*);
197 static void terminal_free_display(EditLine
*);
198 static int terminal_alloc_display(EditLine
*);
199 static void terminal_alloc(EditLine
*, const struct termcapstr
*,
201 static void terminal_init_arrow(EditLine
*);
202 static void terminal_reset_arrow(EditLine
*);
203 static int terminal_putc(int);
204 static void terminal_tputs(EditLine
*, const char *, int);
207 static pthread_mutex_t terminal_mutex
= PTHREAD_MUTEX_INITIALIZER
;
209 static FILE *terminal_outfile
= NULL
;
212 /* terminal_setflags():
213 * Set the terminal capability flags
216 terminal_setflags(EditLine
*el
)
219 if (el
->el_tty
.t_tabs
)
220 EL_FLAGS
|= (Val(T_pt
) && !Val(T_xt
)) ? TERM_CAN_TAB
: 0;
222 EL_FLAGS
|= (Val(T_km
) || Val(T_MT
)) ? TERM_HAS_META
: 0;
223 EL_FLAGS
|= GoodStr(T_ce
) ? TERM_CAN_CEOL
: 0;
224 EL_FLAGS
|= (GoodStr(T_dc
) || GoodStr(T_DC
)) ? TERM_CAN_DELETE
: 0;
225 EL_FLAGS
|= (GoodStr(T_im
) || GoodStr(T_ic
) || GoodStr(T_IC
)) ?
227 EL_FLAGS
|= (GoodStr(T_up
) || GoodStr(T_UP
)) ? TERM_CAN_UP
: 0;
228 EL_FLAGS
|= Val(T_am
) ? TERM_HAS_AUTO_MARGINS
: 0;
229 EL_FLAGS
|= Val(T_xn
) ? TERM_HAS_MAGIC_MARGINS
: 0;
231 if (GoodStr(T_me
) && GoodStr(T_ue
))
232 EL_FLAGS
|= (strcmp(Str(T_me
), Str(T_ue
)) == 0) ?
235 EL_FLAGS
&= ~TERM_CAN_ME
;
236 if (GoodStr(T_me
) && GoodStr(T_se
))
237 EL_FLAGS
|= (strcmp(Str(T_me
), Str(T_se
)) == 0) ?
243 (void) fprintf(el
->el_errfile
,
244 "WARNING: Your terminal cannot move up.\n");
245 (void) fprintf(el
->el_errfile
,
246 "Editing may be odd for long lines.\n");
249 (void) fprintf(el
->el_errfile
, "no clear EOL capability.\n");
251 (void) fprintf(el
->el_errfile
, "no delete char capability.\n");
253 (void) fprintf(el
->el_errfile
, "no insert char capability.\n");
254 #endif /* DEBUG_SCREEN */
258 * Initialize the terminal stuff
261 terminal_init(EditLine
*el
)
264 el
->el_terminal
.t_buf
= (char *)malloc(TC_BUFSIZE
);
265 if (el
->el_terminal
.t_buf
== NULL
)
267 el
->el_terminal
.t_cap
= (char *)malloc(TC_BUFSIZE
);
268 if (el
->el_terminal
.t_cap
== NULL
)
270 el
->el_terminal
.t_fkey
= reallocarray(NULL
, A_K_NKEYS
,
271 sizeof(*el
->el_terminal
.t_fkey
));
272 if (el
->el_terminal
.t_fkey
== NULL
)
274 el
->el_terminal
.t_loc
= 0;
275 el
->el_terminal
.t_str
= calloc(T_str
, sizeof(char *));
276 if (el
->el_terminal
.t_str
== NULL
)
278 el
->el_terminal
.t_val
= calloc(T_val
, sizeof(int));
279 if (el
->el_terminal
.t_val
== NULL
)
281 (void) terminal_set(el
, NULL
);
282 terminal_init_arrow(el
);
285 free(el
->el_terminal
.t_str
);
286 el
->el_terminal
.t_str
= NULL
;
288 free(el
->el_terminal
.t_fkey
);
289 el
->el_terminal
.t_fkey
= NULL
;
291 free(el
->el_terminal
.t_cap
);
292 el
->el_terminal
.t_cap
= NULL
;
294 free(el
->el_terminal
.t_buf
);
295 el
->el_terminal
.t_buf
= NULL
;
301 * Clean up the terminal stuff
304 terminal_end(EditLine
*el
)
307 free(el
->el_terminal
.t_buf
);
308 el
->el_terminal
.t_buf
= NULL
;
309 free(el
->el_terminal
.t_cap
);
310 el
->el_terminal
.t_cap
= NULL
;
311 el
->el_terminal
.t_loc
= 0;
312 free(el
->el_terminal
.t_str
);
313 el
->el_terminal
.t_str
= NULL
;
314 free(el
->el_terminal
.t_val
);
315 el
->el_terminal
.t_val
= NULL
;
316 free(el
->el_terminal
.t_fkey
);
317 el
->el_terminal
.t_fkey
= NULL
;
318 terminal_free_display(el
);
323 * Maintain a string pool for termcap strings
326 terminal_alloc(EditLine
*el
, const struct termcapstr
*t
, const char *cap
)
328 char termbuf
[TC_BUFSIZE
];
330 char **tlist
= el
->el_terminal
.t_str
;
331 char **tmp
, **str
= &tlist
[t
- tstr
];
333 if (cap
== NULL
|| *cap
== '\0') {
339 tlen
= *str
== NULL
? 0 : strlen(*str
);
342 * New string is shorter; no need to allocate space
346 (void) strlcpy(*str
, cap
, tlen
+ 1);
350 * New string is longer; see if we have enough space to append
352 if (el
->el_terminal
.t_loc
+ 3 < TC_BUFSIZE
) {
353 tlen
= TC_BUFSIZE
- el
->el_terminal
.t_loc
;
354 (void) strlcpy(*str
= &el
->el_terminal
.t_buf
[
355 el
->el_terminal
.t_loc
], cap
, tlen
);
356 el
->el_terminal
.t_loc
+= (int)clen
+ 1; /* one for \0 */
360 * Compact our buffer; no need to check compaction, cause we know it
364 for (tmp
= tlist
; tmp
< &tlist
[T_str
]; tmp
++)
365 if (*tmp
!= NULL
&& **tmp
!= '\0' && *tmp
!= *str
) {
368 for (ptr
= *tmp
; *ptr
!= '\0'; termbuf
[tlen
++] = *ptr
++)
370 termbuf
[tlen
++] = '\0';
372 memcpy(el
->el_terminal
.t_buf
, termbuf
, TC_BUFSIZE
);
373 el
->el_terminal
.t_loc
= (int)tlen
;
374 if (el
->el_terminal
.t_loc
+ 3 >= TC_BUFSIZE
) {
375 (void) fprintf(el
->el_errfile
,
376 "Out of termcap string space.\n");
379 tlen
= TC_BUFSIZE
- el
->el_terminal
.t_loc
;
380 (void) strlcpy(*str
= &el
->el_terminal
.t_buf
[el
->el_terminal
.t_loc
],
382 el
->el_terminal
.t_loc
+= (int)clen
+ 1; /* one for \0 */
387 /* terminal_rebuffer_display():
388 * Rebuffer the display after the screen changed size
391 terminal_rebuffer_display(EditLine
*el
)
393 coord_t
*c
= &el
->el_terminal
.t_size
;
395 terminal_free_display(el
);
400 if (terminal_alloc_display(el
) == -1)
406 /* terminal_alloc_display():
407 * Allocate a new display.
410 terminal_alloc_display(EditLine
*el
)
414 coord_t
*c
= &el
->el_terminal
.t_size
;
416 b
= reallocarray(NULL
, c
->v
+ 1, sizeof(*b
));
419 for (i
= 0; i
< c
->v
; i
++) {
420 b
[i
] = reallocarray(NULL
, c
->h
+ 1, sizeof(**b
));
431 b
= reallocarray(NULL
, c
->v
+ 1, sizeof(*b
));
434 for (i
= 0; i
< c
->v
; i
++) {
435 b
[i
] = reallocarray(NULL
, c
->h
+ 1, sizeof(**b
));
447 terminal_free_display(el
);
452 /* terminal_free_display():
453 * Free the display buffers
456 terminal_free_display(EditLine
*el
)
462 el
->el_display
= NULL
;
464 for (bufp
= b
; *bufp
!= NULL
; bufp
++)
469 el
->el_vdisplay
= NULL
;
471 for (bufp
= b
; *bufp
!= NULL
; bufp
++)
478 /* terminal_move_to_line():
479 * move to line <where> (first line == 0)
480 * as efficiently as possible
483 terminal_move_to_line(EditLine
*el
, int where
)
487 if (where
== el
->el_cursor
.v
)
490 if (where
> el
->el_terminal
.t_size
.v
) {
492 (void) fprintf(el
->el_errfile
,
493 "terminal_move_to_line: where is ridiculous: %d\r\n",
495 #endif /* DEBUG_SCREEN */
498 if ((del
= where
- el
->el_cursor
.v
) > 0) {
500 if (EL_HAS_AUTO_MARGINS
&&
501 el
->el_display
[el
->el_cursor
.v
][0] != '\0') {
502 size_t h
= el
->el_terminal
.t_size
.h
- 1;
504 el
->el_display
[el
->el_cursor
.v
][h
] ==
508 /* move without newline */
509 terminal_move_to_char(el
, (int)h
);
510 terminal_overwrite(el
, &el
->el_display
511 [el
->el_cursor
.v
][el
->el_cursor
.h
],
512 (size_t)(el
->el_terminal
.t_size
.h
-
517 if ((del
> 1) && GoodStr(T_DO
)) {
518 terminal_tputs(el
, tgoto(Str(T_DO
), del
,
522 for (; del
> 0; del
--)
523 terminal__putc(el
, '\n');
524 /* because the \n will become \r\n */
529 } else { /* del < 0 */
530 if (GoodStr(T_UP
) && (-del
> 1 || !GoodStr(T_up
)))
531 terminal_tputs(el
, tgoto(Str(T_UP
), -del
, -del
), -del
);
534 for (; del
< 0; del
++)
535 terminal_tputs(el
, Str(T_up
), 1);
538 el
->el_cursor
.v
= where
;/* now where is here */
542 /* terminal_move_to_char():
543 * Move to the character position specified
546 terminal_move_to_char(EditLine
*el
, int where
)
551 if (where
== el
->el_cursor
.h
)
554 if (where
> el
->el_terminal
.t_size
.h
) {
556 (void) fprintf(el
->el_errfile
,
557 "terminal_move_to_char: where is riduculous: %d\r\n",
559 #endif /* DEBUG_SCREEN */
562 if (!where
) { /* if where is first column */
563 terminal__putc(el
, '\r'); /* do a CR */
567 del
= where
- el
->el_cursor
.h
;
569 if ((del
< -4 || del
> 4) && GoodStr(T_ch
))
570 /* go there directly */
571 terminal_tputs(el
, tgoto(Str(T_ch
), where
, where
), where
);
573 if (del
> 0) { /* moving forward */
574 if ((del
> 4) && GoodStr(T_RI
))
575 terminal_tputs(el
, tgoto(Str(T_RI
), del
, del
),
578 /* if I can do tabs, use them */
580 if ((el
->el_cursor
.h
& 0370) !=
583 el
->el_cursor
.v
][where
& 0370] !=
586 /* if not within tab stop */
588 (el
->el_cursor
.h
& 0370);
594 el
->el_cursor
.h
= where
& ~0x7;
598 * it's usually cheaper to just write the
602 * NOTE THAT terminal_overwrite() WILL CHANGE
605 terminal_overwrite(el
, &el
->el_display
[
606 el
->el_cursor
.v
][el
->el_cursor
.h
],
607 (size_t)(where
- el
->el_cursor
.h
));
610 } else { /* del < 0 := moving backward */
611 if ((-del
> 4) && GoodStr(T_LE
))
612 terminal_tputs(el
, tgoto(Str(T_LE
), -del
, -del
),
614 else { /* can't go directly there */
616 * if the "cost" is greater than the "cost"
620 ((unsigned int)-del
>
621 (((unsigned int) where
>> 3) +
624 terminal__putc(el
, '\r');/* do a CR */
626 goto mc_again
; /* and try again */
628 for (i
= 0; i
< -del
; i
++)
629 terminal__putc(el
, '\b');
633 el
->el_cursor
.h
= where
; /* now where is here */
637 /* terminal_overwrite():
638 * Overstrike num characters
639 * Assumes MB_FILL_CHARs are present to keep the column count correct
642 terminal_overwrite(EditLine
*el
, const wchar_t *cp
, size_t n
)
647 if (n
> (size_t)el
->el_terminal
.t_size
.h
) {
649 (void) fprintf(el
->el_errfile
,
650 "terminal_overwrite: n is riduculous: %zu\r\n", n
);
651 #endif /* DEBUG_SCREEN */
656 /* terminal__putc() ignores any MB_FILL_CHARs */
657 terminal__putc(el
, *cp
++);
661 if (el
->el_cursor
.h
>= el
->el_terminal
.t_size
.h
) { /* wrap? */
662 if (EL_HAS_AUTO_MARGINS
) { /* yes */
665 if (EL_HAS_MAGIC_MARGINS
) {
666 /* force the wrap to avoid the "magic"
669 if ((c
= el
->el_display
[el
->el_cursor
.v
]
670 [el
->el_cursor
.h
]) != '\0') {
671 terminal_overwrite(el
, &c
, 1);
672 while (el
->el_display
[el
->el_cursor
.v
]
673 [el
->el_cursor
.h
] == MB_FILL_CHAR
)
676 terminal__putc(el
, ' ');
680 } else /* no wrap, but cursor stays on screen */
681 el
->el_cursor
.h
= el
->el_terminal
.t_size
.h
- 1;
686 /* terminal_deletechars():
687 * Delete num characters
690 terminal_deletechars(EditLine
*el
, int num
)
695 if (!EL_CAN_DELETE
) {
697 (void) fprintf(el
->el_errfile
, " ERROR: cannot delete \n");
698 #endif /* DEBUG_EDIT */
701 if (num
> el
->el_terminal
.t_size
.h
) {
703 (void) fprintf(el
->el_errfile
,
704 "terminal_deletechars: num is riduculous: %d\r\n", num
);
705 #endif /* DEBUG_SCREEN */
708 if (GoodStr(T_DC
)) /* if I have multiple delete */
709 if ((num
> 1) || !GoodStr(T_dc
)) { /* if dc would be more
711 terminal_tputs(el
, tgoto(Str(T_DC
), num
, num
), num
);
714 if (GoodStr(T_dm
)) /* if I have delete mode */
715 terminal_tputs(el
, Str(T_dm
), 1);
717 if (GoodStr(T_dc
)) /* else do one at a time */
719 terminal_tputs(el
, Str(T_dc
), 1);
721 if (GoodStr(T_ed
)) /* if I have delete mode */
722 terminal_tputs(el
, Str(T_ed
), 1);
726 /* terminal_insertwrite():
727 * Puts terminal in insert character mode or inserts num
728 * characters in the line
729 * Assumes MB_FILL_CHARs are present to keep column count correct
732 terminal_insertwrite(EditLine
*el
, wchar_t *cp
, int num
)
736 if (!EL_CAN_INSERT
) {
738 (void) fprintf(el
->el_errfile
, " ERROR: cannot insert \n");
739 #endif /* DEBUG_EDIT */
742 if (num
> el
->el_terminal
.t_size
.h
) {
744 (void) fprintf(el
->el_errfile
,
745 "StartInsert: num is riduculous: %d\r\n", num
);
746 #endif /* DEBUG_SCREEN */
749 if (GoodStr(T_IC
)) /* if I have multiple insert */
750 if ((num
> 1) || !GoodStr(T_ic
)) {
751 /* if ic would be more expensive */
752 terminal_tputs(el
, tgoto(Str(T_IC
), num
, num
), num
);
753 terminal_overwrite(el
, cp
, (size_t)num
);
754 /* this updates el_cursor.h */
757 if (GoodStr(T_im
) && GoodStr(T_ei
)) { /* if I have insert mode */
758 terminal_tputs(el
, Str(T_im
), 1);
760 el
->el_cursor
.h
+= num
;
762 terminal__putc(el
, *cp
++);
765 if (GoodStr(T_ip
)) /* have to make num chars insert */
766 terminal_tputs(el
, Str(T_ip
), 1);
768 terminal_tputs(el
, Str(T_ei
), 1);
772 if (GoodStr(T_ic
)) /* have to make num chars insert */
773 terminal_tputs(el
, Str(T_ic
), 1);
775 terminal__putc(el
, *cp
++);
779 if (GoodStr(T_ip
)) /* have to make num chars insert */
780 terminal_tputs(el
, Str(T_ip
), 1);
781 /* pad the inserted char */
787 /* terminal_clear_EOL():
788 * clear to end of line. There are num characters to clear
791 terminal_clear_EOL(EditLine
*el
, int num
)
795 if (EL_CAN_CEOL
&& GoodStr(T_ce
))
796 terminal_tputs(el
, Str(T_ce
), 1);
798 for (i
= 0; i
< num
; i
++)
799 terminal__putc(el
, ' ');
800 el
->el_cursor
.h
+= num
; /* have written num spaces */
805 /* terminal_clear_screen():
809 terminal_clear_screen(EditLine
*el
)
810 { /* clear the whole screen and home */
813 /* send the clear screen code */
814 terminal_tputs(el
, Str(T_cl
), Val(T_li
));
815 else if (GoodStr(T_ho
) && GoodStr(T_cd
)) {
816 terminal_tputs(el
, Str(T_ho
), Val(T_li
)); /* home */
817 /* clear to bottom of screen */
818 terminal_tputs(el
, Str(T_cd
), Val(T_li
));
820 terminal__putc(el
, '\r');
821 terminal__putc(el
, '\n');
827 * Beep the way the terminal wants us
830 terminal_beep(EditLine
*el
)
833 /* what termcap says we should use */
834 terminal_tputs(el
, Str(T_bl
), 1);
836 terminal__putc(el
, '\007'); /* an ASCII bell; ^G */
841 terminal_get(EditLine
*el
, const char **term
)
843 *term
= el
->el_terminal
.t_name
;
848 * Read in the terminal capabilities from the requested terminal
851 terminal_set(EditLine
*el
, const char *term
)
854 char buf
[TC_BUFSIZE
];
856 const struct termcapstr
*t
;
860 (void) sigemptyset(&nset
);
861 (void) sigaddset(&nset
, SIGWINCH
);
862 (void) sigprocmask(SIG_BLOCK
, &nset
, &oset
);
868 term
= getenv("TERM");
870 if (!term
|| !term
[0])
873 if (strcmp(term
, "emacs") == 0)
874 el
->el_flags
|= EDIT_DISABLED
;
876 (void) memset(el
->el_terminal
.t_cap
, 0, TC_BUFSIZE
);
878 i
= tgetent(el
->el_terminal
.t_cap
, term
);
882 (void) fprintf(el
->el_errfile
,
883 "Cannot read termcap database;\n");
885 (void) fprintf(el
->el_errfile
,
886 "No entry for terminal type \"%s\";\n", term
);
887 (void) fprintf(el
->el_errfile
,
888 "using dumb terminal settings.\n");
889 Val(T_co
) = 80; /* do a dumb terminal */
890 Val(T_pt
) = Val(T_km
) = Val(T_li
) = 0;
891 Val(T_xt
) = Val(T_MT
);
892 for (t
= tstr
; t
->name
!= NULL
; t
++)
893 terminal_alloc(el
, t
, NULL
);
895 /* auto/magic margins */
896 Val(T_am
) = tgetflag("am");
897 Val(T_xn
) = tgetflag("xn");
899 Val(T_pt
) = tgetflag("pt");
900 Val(T_xt
) = tgetflag("xt");
901 /* do we have a meta? */
902 Val(T_km
) = tgetflag("km");
903 Val(T_MT
) = tgetflag("MT");
905 Val(T_co
) = tgetnum("co");
906 Val(T_li
) = tgetnum("li");
907 for (t
= tstr
; t
->name
!= NULL
; t
++) {
908 /* XXX: some systems' tgetstr needs non const */
909 terminal_alloc(el
, t
, tgetstr(strchr(t
->name
, *t
->name
),
915 Val(T_co
) = 80; /* just in case */
919 el
->el_terminal
.t_size
.v
= Val(T_co
);
920 el
->el_terminal
.t_size
.h
= Val(T_li
);
922 terminal_setflags(el
);
924 /* get the correct window size */
925 (void) terminal_get_size(el
, &lins
, &cols
);
926 if (terminal_change_size(el
, lins
, cols
) == -1)
928 (void) sigprocmask(SIG_SETMASK
, &oset
, NULL
);
929 terminal_bind_arrow(el
);
930 el
->el_terminal
.t_name
= term
;
931 return i
<= 0 ? -1 : 0;
935 /* terminal_get_size():
936 * Return the new window size in lines and cols, and
937 * true if the size was changed.
940 terminal_get_size(EditLine
*el
, int *lins
, int *cols
)
949 if (ioctl(el
->el_infd
, TIOCGWINSZ
, &ws
) != -1) {
960 if (ioctl(el
->el_infd
, TIOCGSIZE
, &ts
) != -1) {
968 return Val(T_co
) != *cols
|| Val(T_li
) != *lins
;
972 /* terminal_change_size():
973 * Change the size of the terminal
976 terminal_change_size(EditLine
*el
, int lins
, int cols
)
981 Val(T_co
) = (cols
< 2) ? 80 : cols
;
982 Val(T_li
) = (lins
< 1) ? 24 : lins
;
984 /* re-make display buffers */
985 if (terminal_rebuffer_display(el
) == -1)
987 re_clear_display(el
);
992 /* terminal_init_arrow():
993 * Initialize the arrow key bindings from termcap
996 terminal_init_arrow(EditLine
*el
)
998 funckey_t
*arrow
= el
->el_terminal
.t_fkey
;
1000 arrow
[A_K_DN
].name
= L
"down";
1001 arrow
[A_K_DN
].key
= T_kd
;
1002 arrow
[A_K_DN
].fun
.cmd
= ED_NEXT_HISTORY
;
1003 arrow
[A_K_DN
].type
= XK_CMD
;
1005 arrow
[A_K_UP
].name
= L
"up";
1006 arrow
[A_K_UP
].key
= T_ku
;
1007 arrow
[A_K_UP
].fun
.cmd
= ED_PREV_HISTORY
;
1008 arrow
[A_K_UP
].type
= XK_CMD
;
1010 arrow
[A_K_LT
].name
= L
"left";
1011 arrow
[A_K_LT
].key
= T_kl
;
1012 arrow
[A_K_LT
].fun
.cmd
= ED_PREV_CHAR
;
1013 arrow
[A_K_LT
].type
= XK_CMD
;
1015 arrow
[A_K_RT
].name
= L
"right";
1016 arrow
[A_K_RT
].key
= T_kr
;
1017 arrow
[A_K_RT
].fun
.cmd
= ED_NEXT_CHAR
;
1018 arrow
[A_K_RT
].type
= XK_CMD
;
1020 arrow
[A_K_HO
].name
= L
"home";
1021 arrow
[A_K_HO
].key
= T_kh
;
1022 arrow
[A_K_HO
].fun
.cmd
= ED_MOVE_TO_BEG
;
1023 arrow
[A_K_HO
].type
= XK_CMD
;
1025 arrow
[A_K_EN
].name
= L
"end";
1026 arrow
[A_K_EN
].key
= T_at7
;
1027 arrow
[A_K_EN
].fun
.cmd
= ED_MOVE_TO_END
;
1028 arrow
[A_K_EN
].type
= XK_CMD
;
1032 /* terminal_reset_arrow():
1033 * Reset arrow key bindings
1036 terminal_reset_arrow(EditLine
*el
)
1038 funckey_t
*arrow
= el
->el_terminal
.t_fkey
;
1039 static const wchar_t strA
[] = L
"\033[A";
1040 static const wchar_t strB
[] = L
"\033[B";
1041 static const wchar_t strC
[] = L
"\033[C";
1042 static const wchar_t strD
[] = L
"\033[D";
1043 static const wchar_t strH
[] = L
"\033[H";
1044 static const wchar_t strF
[] = L
"\033[F";
1045 static const wchar_t stOA
[] = L
"\033OA";
1046 static const wchar_t stOB
[] = L
"\033OB";
1047 static const wchar_t stOC
[] = L
"\033OC";
1048 static const wchar_t stOD
[] = L
"\033OD";
1049 static const wchar_t stOH
[] = L
"\033OH";
1050 static const wchar_t stOF
[] = L
"\033OF";
1052 keymacro_add(el
, strA
, &arrow
[A_K_UP
].fun
, arrow
[A_K_UP
].type
);
1053 keymacro_add(el
, strB
, &arrow
[A_K_DN
].fun
, arrow
[A_K_DN
].type
);
1054 keymacro_add(el
, strC
, &arrow
[A_K_RT
].fun
, arrow
[A_K_RT
].type
);
1055 keymacro_add(el
, strD
, &arrow
[A_K_LT
].fun
, arrow
[A_K_LT
].type
);
1056 keymacro_add(el
, strH
, &arrow
[A_K_HO
].fun
, arrow
[A_K_HO
].type
);
1057 keymacro_add(el
, strF
, &arrow
[A_K_EN
].fun
, arrow
[A_K_EN
].type
);
1058 keymacro_add(el
, stOA
, &arrow
[A_K_UP
].fun
, arrow
[A_K_UP
].type
);
1059 keymacro_add(el
, stOB
, &arrow
[A_K_DN
].fun
, arrow
[A_K_DN
].type
);
1060 keymacro_add(el
, stOC
, &arrow
[A_K_RT
].fun
, arrow
[A_K_RT
].type
);
1061 keymacro_add(el
, stOD
, &arrow
[A_K_LT
].fun
, arrow
[A_K_LT
].type
);
1062 keymacro_add(el
, stOH
, &arrow
[A_K_HO
].fun
, arrow
[A_K_HO
].type
);
1063 keymacro_add(el
, stOF
, &arrow
[A_K_EN
].fun
, arrow
[A_K_EN
].type
);
1065 if (el
->el_map
.type
!= MAP_VI
)
1067 keymacro_add(el
, &strA
[1], &arrow
[A_K_UP
].fun
, arrow
[A_K_UP
].type
);
1068 keymacro_add(el
, &strB
[1], &arrow
[A_K_DN
].fun
, arrow
[A_K_DN
].type
);
1069 keymacro_add(el
, &strC
[1], &arrow
[A_K_RT
].fun
, arrow
[A_K_RT
].type
);
1070 keymacro_add(el
, &strD
[1], &arrow
[A_K_LT
].fun
, arrow
[A_K_LT
].type
);
1071 keymacro_add(el
, &strH
[1], &arrow
[A_K_HO
].fun
, arrow
[A_K_HO
].type
);
1072 keymacro_add(el
, &strF
[1], &arrow
[A_K_EN
].fun
, arrow
[A_K_EN
].type
);
1073 keymacro_add(el
, &stOA
[1], &arrow
[A_K_UP
].fun
, arrow
[A_K_UP
].type
);
1074 keymacro_add(el
, &stOB
[1], &arrow
[A_K_DN
].fun
, arrow
[A_K_DN
].type
);
1075 keymacro_add(el
, &stOC
[1], &arrow
[A_K_RT
].fun
, arrow
[A_K_RT
].type
);
1076 keymacro_add(el
, &stOD
[1], &arrow
[A_K_LT
].fun
, arrow
[A_K_LT
].type
);
1077 keymacro_add(el
, &stOH
[1], &arrow
[A_K_HO
].fun
, arrow
[A_K_HO
].type
);
1078 keymacro_add(el
, &stOF
[1], &arrow
[A_K_EN
].fun
, arrow
[A_K_EN
].type
);
1082 /* terminal_set_arrow():
1083 * Set an arrow key binding
1086 terminal_set_arrow(EditLine
*el
, const wchar_t *name
, keymacro_value_t
*fun
,
1089 funckey_t
*arrow
= el
->el_terminal
.t_fkey
;
1092 for (i
= 0; i
< A_K_NKEYS
; i
++)
1093 if (wcscmp(name
, arrow
[i
].name
) == 0) {
1094 arrow
[i
].fun
= *fun
;
1095 arrow
[i
].type
= type
;
1102 /* terminal_clear_arrow():
1103 * Clear an arrow key binding
1106 terminal_clear_arrow(EditLine
*el
, const wchar_t *name
)
1108 funckey_t
*arrow
= el
->el_terminal
.t_fkey
;
1111 for (i
= 0; i
< A_K_NKEYS
; i
++)
1112 if (wcscmp(name
, arrow
[i
].name
) == 0) {
1113 arrow
[i
].type
= XK_NOD
;
1120 /* terminal_print_arrow():
1121 * Print the arrow key bindings
1124 terminal_print_arrow(EditLine
*el
, const wchar_t *name
)
1127 funckey_t
*arrow
= el
->el_terminal
.t_fkey
;
1129 for (i
= 0; i
< A_K_NKEYS
; i
++)
1130 if (*name
== '\0' || wcscmp(name
, arrow
[i
].name
) == 0)
1131 if (arrow
[i
].type
!= XK_NOD
)
1132 keymacro_kprint(el
, arrow
[i
].name
,
1133 &arrow
[i
].fun
, arrow
[i
].type
);
1137 /* terminal_bind_arrow():
1138 * Bind the arrow keys
1141 terminal_bind_arrow(EditLine
*el
)
1144 const el_action_t
*dmap
;
1147 funckey_t
*arrow
= el
->el_terminal
.t_fkey
;
1149 /* Check if the components needed are initialized */
1150 if (el
->el_terminal
.t_buf
== NULL
|| el
->el_map
.key
== NULL
)
1153 map
= el
->el_map
.type
== MAP_VI
? el
->el_map
.alt
: el
->el_map
.key
;
1154 dmap
= el
->el_map
.type
== MAP_VI
? el
->el_map
.vic
: el
->el_map
.emacs
;
1156 terminal_reset_arrow(el
);
1158 for (i
= 0; i
< A_K_NKEYS
; i
++) {
1159 wchar_t wt_str
[VISUAL_WIDTH_MAX
];
1163 p
= el
->el_terminal
.t_str
[arrow
[i
].key
];
1166 for (n
= 0; n
< VISUAL_WIDTH_MAX
&& p
[n
]; ++n
)
1168 while (n
< VISUAL_WIDTH_MAX
)
1171 j
= (unsigned char) *p
;
1173 * Assign the arrow keys only if:
1175 * 1. They are multi-character arrow keys and the user
1176 * has not re-assigned the leading character, or
1177 * has re-assigned the leading character to be
1178 * ED_SEQUENCE_LEAD_IN
1179 * 2. They are single arrow keys pointing to an
1182 if (arrow
[i
].type
== XK_NOD
)
1183 keymacro_clear(el
, map
, px
);
1185 if (p
[1] && (dmap
[j
] == map
[j
] ||
1186 map
[j
] == ED_SEQUENCE_LEAD_IN
)) {
1187 keymacro_add(el
, px
, &arrow
[i
].fun
,
1189 map
[j
] = ED_SEQUENCE_LEAD_IN
;
1190 } else if (map
[j
] == ED_UNASSIGNED
) {
1191 keymacro_clear(el
, map
, px
);
1192 if (arrow
[i
].type
== XK_CMD
)
1193 map
[j
] = arrow
[i
].fun
.cmd
;
1195 keymacro_add(el
, px
, &arrow
[i
].fun
,
1206 terminal_putc(int c
)
1208 if (terminal_outfile
== NULL
)
1210 return fputc(c
, terminal_outfile
);
1214 terminal_tputs(EditLine
*el
, const char *cap
, int affcnt
)
1217 pthread_mutex_lock(&terminal_mutex
);
1219 terminal_outfile
= el
->el_outfile
;
1220 (void)tputs(cap
, affcnt
, terminal_putc
);
1222 pthread_mutex_unlock(&terminal_mutex
);
1226 /* terminal__putc():
1230 terminal__putc(EditLine
*el
, wint_t c
)
1232 char buf
[MB_LEN_MAX
+1];
1234 if (c
== (wint_t)MB_FILL_CHAR
)
1236 i
= ct_encode_char(buf
, MB_LEN_MAX
, c
);
1240 return fputs(buf
, el
->el_outfile
);
1243 /* terminal__flush():
1247 terminal__flush(EditLine
*el
)
1250 (void) fflush(el
->el_outfile
);
1253 /* terminal_writec():
1254 * Write the given character out, in a human readable form
1257 terminal_writec(EditLine
*el
, wint_t c
)
1259 wchar_t visbuf
[VISUAL_WIDTH_MAX
+1];
1260 ssize_t vcnt
= ct_visual_char(visbuf
, VISUAL_WIDTH_MAX
, c
);
1261 visbuf
[vcnt
] = '\0';
1262 terminal_overwrite(el
, visbuf
, (size_t)vcnt
);
1263 terminal__flush(el
);
1267 /* terminal_telltc():
1268 * Print the current termcap characteristics
1272 terminal_telltc(EditLine
*el
, int argc
__attribute__((__unused__
)),
1273 const wchar_t **argv
__attribute__((__unused__
)))
1275 const struct termcapstr
*t
;
1278 (void) fprintf(el
->el_outfile
, "\n\tYour terminal has the\n");
1279 (void) fprintf(el
->el_outfile
, "\tfollowing characteristics:\n\n");
1280 (void) fprintf(el
->el_outfile
, "\tIt has %d columns and %d lines\n",
1281 Val(T_co
), Val(T_li
));
1282 (void) fprintf(el
->el_outfile
,
1283 "\tIt has %s meta key\n", EL_HAS_META
? "a" : "no");
1284 (void) fprintf(el
->el_outfile
,
1285 "\tIt can%suse tabs\n", EL_CAN_TAB
? " " : "not ");
1286 (void) fprintf(el
->el_outfile
, "\tIt %s automatic margins\n",
1287 EL_HAS_AUTO_MARGINS
? "has" : "does not have");
1288 if (EL_HAS_AUTO_MARGINS
)
1289 (void) fprintf(el
->el_outfile
, "\tIt %s magic margins\n",
1290 EL_HAS_MAGIC_MARGINS
? "has" : "does not have");
1292 for (t
= tstr
, ts
= el
->el_terminal
.t_str
; t
->name
!= NULL
; t
++, ts
++) {
1295 ub
= ct_encode_string(ct_visual_string(
1296 ct_decode_string(*ts
, &el
->el_scratch
)),
1301 (void) fprintf(el
->el_outfile
, "\t%25s (%s) == %s\n",
1302 t
->long_name
, t
->name
, ub
);
1304 (void) fputc('\n', el
->el_outfile
);
1309 /* terminal_settc():
1310 * Change the current terminal characteristics
1314 terminal_settc(EditLine
*el
, int argc
__attribute__((__unused__
)),
1315 const wchar_t **argv
)
1317 const struct termcapstr
*ts
;
1318 const struct termcapval
*tv
;
1319 char what
[8], how
[8];
1321 if (argv
== NULL
|| argv
[1] == NULL
|| argv
[2] == NULL
)
1324 strncpy(what
, ct_encode_string(argv
[1], &el
->el_scratch
), sizeof(what
));
1325 what
[sizeof(what
) - 1] = '\0';
1326 strncpy(how
, ct_encode_string(argv
[2], &el
->el_scratch
), sizeof(how
));
1327 how
[sizeof(how
) - 1] = '\0';
1330 * Do the strings first
1332 for (ts
= tstr
; ts
->name
!= NULL
; ts
++)
1333 if (strcmp(ts
->name
, what
) == 0)
1336 if (ts
->name
!= NULL
) {
1337 terminal_alloc(el
, ts
, how
);
1338 terminal_setflags(el
);
1342 * Do the numeric ones second
1344 for (tv
= tval
; tv
->name
!= NULL
; tv
++)
1345 if (strcmp(tv
->name
, what
) == 0)
1348 if (tv
->name
!= NULL
)
1351 if (tv
== &tval
[T_pt
] || tv
== &tval
[T_km
] ||
1352 tv
== &tval
[T_am
] || tv
== &tval
[T_xn
]) {
1353 if (strcmp(how
, "yes") == 0)
1354 el
->el_terminal
.t_val
[tv
- tval
] = 1;
1355 else if (strcmp(how
, "no") == 0)
1356 el
->el_terminal
.t_val
[tv
- tval
] = 0;
1358 (void) fprintf(el
->el_errfile
,
1359 "%ls: Bad value `%s'.\n", argv
[0], how
);
1362 terminal_setflags(el
);
1363 if (terminal_change_size(el
, Val(T_li
), Val(T_co
)) == -1)
1370 i
= strtol(how
, &ep
, 10);
1372 (void) fprintf(el
->el_errfile
,
1373 "%ls: Bad value `%s'.\n", argv
[0], how
);
1376 el
->el_terminal
.t_val
[tv
- tval
] = (int) i
;
1377 el
->el_terminal
.t_size
.v
= Val(T_co
);
1378 el
->el_terminal
.t_size
.h
= Val(T_li
);
1379 if (tv
== &tval
[T_co
] || tv
== &tval
[T_li
])
1380 if (terminal_change_size(el
, Val(T_li
), Val(T_co
))
1388 /* terminal_gettc():
1389 * Get the current terminal characteristics
1393 terminal_gettc(EditLine
*el
, int argc
__attribute__((__unused__
)), char **argv
)
1395 const struct termcapstr
*ts
;
1396 const struct termcapval
*tv
;
1400 if (argv
== NULL
|| argv
[1] == NULL
|| argv
[2] == NULL
)
1407 * Do the strings first
1409 for (ts
= tstr
; ts
->name
!= NULL
; ts
++)
1410 if (strcmp(ts
->name
, what
) == 0)
1413 if (ts
->name
!= NULL
) {
1414 *(char **)how
= el
->el_terminal
.t_str
[ts
- tstr
];
1418 * Do the numeric ones second
1420 for (tv
= tval
; tv
->name
!= NULL
; tv
++)
1421 if (strcmp(tv
->name
, what
) == 0)
1424 if (tv
->name
== NULL
)
1427 if (tv
== &tval
[T_pt
] || tv
== &tval
[T_km
] ||
1428 tv
== &tval
[T_am
] || tv
== &tval
[T_xn
]) {
1429 static char yes
[] = "yes";
1430 static char no
[] = "no";
1431 if (el
->el_terminal
.t_val
[tv
- tval
])
1432 *(char **)how
= yes
;
1437 *(int *)how
= el
->el_terminal
.t_val
[tv
- tval
];
1442 /* terminal_echotc():
1443 * Print the termcap string out with variable substitution
1447 terminal_echotc(EditLine
*el
, int argc
__attribute__((__unused__
)),
1448 const wchar_t **argv
)
1452 int arg_need
, arg_cols
, arg_rows
;
1453 int verbose
= 0, silent
= 0;
1455 static const char fmts
[] = "%s\n", fmtd
[] = "%d\n";
1456 const struct termcapstr
*t
;
1457 char buf
[TC_BUFSIZE
];
1462 if (argv
== NULL
|| argv
[1] == NULL
)
1466 if (argv
[0][0] == '-') {
1467 switch (argv
[0][1]) {
1475 /* stderror(ERR_NAME | ERR_TCUSAGE); */
1480 if (!*argv
|| *argv
[0] == '\0')
1482 if (wcscmp(*argv
, L
"tabs") == 0) {
1483 (void) fprintf(el
->el_outfile
, fmts
, EL_CAN_TAB
? "yes" : "no");
1485 } else if (wcscmp(*argv
, L
"meta") == 0) {
1486 (void) fprintf(el
->el_outfile
, fmts
, Val(T_km
) ? "yes" : "no");
1488 } else if (wcscmp(*argv
, L
"xn") == 0) {
1489 (void) fprintf(el
->el_outfile
, fmts
, EL_HAS_MAGIC_MARGINS
?
1492 } else if (wcscmp(*argv
, L
"am") == 0) {
1493 (void) fprintf(el
->el_outfile
, fmts
, EL_HAS_AUTO_MARGINS
?
1496 } else if (wcscmp(*argv
, L
"baud") == 0) {
1497 (void) fprintf(el
->el_outfile
, fmtd
, (int)el
->el_tty
.t_speed
);
1499 } else if (wcscmp(*argv
, L
"rows") == 0 ||
1500 wcscmp(*argv
, L
"lines") == 0) {
1501 (void) fprintf(el
->el_outfile
, fmtd
, Val(T_li
));
1503 } else if (wcscmp(*argv
, L
"cols") == 0) {
1504 (void) fprintf(el
->el_outfile
, fmtd
, Val(T_co
));
1508 * Try to use our local definition first
1511 for (t
= tstr
; t
->name
!= NULL
; t
++)
1513 ct_encode_string(*argv
, &el
->el_scratch
)) == 0) {
1514 scap
= el
->el_terminal
.t_str
[t
- tstr
];
1517 if (t
->name
== NULL
) {
1518 /* XXX: some systems' tgetstr needs non const */
1519 scap
= tgetstr(ct_encode_string(*argv
, &el
->el_scratch
), &area
);
1521 if (!scap
|| scap
[0] == '\0') {
1523 (void) fprintf(el
->el_errfile
,
1524 "echotc: Termcap parameter `%ls' not found.\n",
1529 * Count home many values we need for this capability.
1531 for (cap
= scap
, arg_need
= 0; *cap
; cap
++)
1551 * hpux has lot's of them...
1554 (void) fprintf(el
->el_errfile
,
1555 "echotc: Warning: unknown termcap %% `%c'.\n",
1557 /* This is bad, but I won't complain */
1564 if (*argv
&& *argv
[0]) {
1566 (void) fprintf(el
->el_errfile
,
1567 "echotc: Warning: Extra argument `%ls'.\n",
1571 terminal_tputs(el
, scap
, 1);
1575 if (!*argv
|| *argv
[0] == '\0') {
1577 (void) fprintf(el
->el_errfile
,
1578 "echotc: Warning: Missing argument.\n");
1582 i
= wcstol(*argv
, &ep
, 10);
1583 if (*ep
!= '\0' || i
< 0) {
1585 (void) fprintf(el
->el_errfile
,
1586 "echotc: Bad value `%ls' for rows.\n",
1592 if (*argv
&& *argv
[0]) {
1594 (void) fprintf(el
->el_errfile
,
1595 "echotc: Warning: Extra argument `%ls"
1599 terminal_tputs(el
, tgoto(scap
, arg_cols
, arg_rows
), 1);
1602 /* This is wrong, but I will ignore it... */
1604 (void) fprintf(el
->el_errfile
,
1605 "echotc: Warning: Too many required arguments (%d).\n",
1610 if (!*argv
|| *argv
[0] == '\0') {
1612 (void) fprintf(el
->el_errfile
,
1613 "echotc: Warning: Missing argument.\n");
1616 i
= wcstol(*argv
, &ep
, 10);
1617 if (*ep
!= '\0' || i
< 0) {
1619 (void) fprintf(el
->el_errfile
,
1620 "echotc: Bad value `%ls' for cols.\n",
1626 if (!*argv
|| *argv
[0] == '\0') {
1628 (void) fprintf(el
->el_errfile
,
1629 "echotc: Warning: Missing argument.\n");
1632 i
= wcstol(*argv
, &ep
, 10);
1633 if (*ep
!= '\0' || i
< 0) {
1635 (void) fprintf(el
->el_errfile
,
1636 "echotc: Bad value `%ls' for rows.\n",
1643 (void) fprintf(el
->el_errfile
,
1644 "echotc: Bad value `%ls'.\n", *argv
);
1648 if (*argv
&& *argv
[0]) {
1650 (void) fprintf(el
->el_errfile
,
1651 "echotc: Warning: Extra argument `%ls"
1655 terminal_tputs(el
, tgoto(scap
, arg_cols
, arg_rows
), arg_rows
);