1 /* $OpenBSD: term.c,v 1.15 2011/07/07 05:40:42 okan Exp $ */
2 /* $NetBSD: term.c,v 1.57 2009/12/30 22:37:40 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 * term.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!
57 /* Solaris's term.h does horrid things. */
59 #if defined(HAVE_TERM_H) && !defined(__sun)
62 #include <sys/types.h>
63 #include <sys/ioctl.h>
72 * IMPORTANT NOTE: these routines are allowed to look at the current screen
73 * and the current position assuming that it is correct. If this is not
74 * true, then the update will be WRONG! This is (should be) a valid
78 #define TC_BUFSIZE 2048
80 #define GoodStr(a) (el->el_term.t_str[a] != NULL && \
81 el->el_term.t_str[a][0] != '\0')
82 #define Str(a) el->el_term.t_str[a]
83 #define Val(a) el->el_term.t_val[a]
86 private const struct {
157 private const struct termcapstr
{
159 const char *long_name
;
162 { "al", "add new blank line" },
164 { "bl", "audible bell" },
166 { "cd", "clear to bottom" },
168 { "ce", "clear to end of line" },
170 { "ch", "cursor to horiz pos" },
172 { "cl", "clear screen" },
174 { "dc", "delete a character" },
176 { "dl", "delete a line" },
178 { "dm", "start delete mode" },
180 { "ed", "end delete mode" },
182 { "ei", "end insert mode" },
184 { "fs", "cursor from status line" },
186 { "ho", "home cursor" },
188 { "ic", "insert character" },
190 { "im", "start insert mode" },
192 { "ip", "insert padding" },
194 { "kd", "sends cursor down" },
196 { "kl", "sends cursor left" },
198 { "kr", "sends cursor right" },
200 { "ku", "sends cursor up" },
202 { "md", "begin bold" },
204 { "me", "end attributes" },
206 { "nd", "non destructive space" },
208 { "se", "end standout" },
210 { "so", "begin standout" },
212 { "ts", "cursor to status line" },
214 { "up", "cursor up one" },
216 { "us", "begin underline" },
218 { "ue", "end underline" },
220 { "vb", "visible bell" },
222 { "DC", "delete multiple chars" },
224 { "DO", "cursor down multiple" },
226 { "IC", "insert multiple chars" },
228 { "LE", "cursor left multiple" },
230 { "RI", "cursor right multiple" },
232 { "UP", "cursor up multiple" },
234 { "kh", "send cursor home" },
236 { "@7", "send cursor end" },
241 private const struct termcapval
{
243 const char *long_name
;
246 { "am", "has automatic margins" },
248 { "pt", "has physical tabs" },
250 { "li", "Number of lines" },
252 { "co", "Number of columns" },
254 { "km", "Has meta key" },
256 { "xt", "Tab chars destructive" },
258 { "xn", "newline ignored at right margin" },
260 { "MT", "Has meta key" }, /* XXX? */
264 /* do two or more of the attributes use me */
266 private void term_setflags(EditLine
*);
267 private int term_rebuffer_display(EditLine
*);
268 private void term_free_display(EditLine
*);
269 private int term_alloc_display(EditLine
*);
270 private void term_alloc(EditLine
*, const struct termcapstr
*, const char *);
271 private void term_init_arrow(EditLine
*);
272 private void term_reset_arrow(EditLine
*);
273 private int term_putc(int);
274 private void term_tputs(EditLine
*, const char *, int);
277 private pthread_mutex_t term_mutex
= PTHREAD_MUTEX_INITIALIZER
;
279 private FILE *term_outfile
= NULL
;
283 * Set the terminal capability flags
286 term_setflags(EditLine
*el
)
289 if (el
->el_tty
.t_tabs
)
290 EL_FLAGS
|= (Val(T_pt
) && !Val(T_xt
)) ? TERM_CAN_TAB
: 0;
292 EL_FLAGS
|= (Val(T_km
) || Val(T_MT
)) ? TERM_HAS_META
: 0;
293 EL_FLAGS
|= GoodStr(T_ce
) ? TERM_CAN_CEOL
: 0;
294 EL_FLAGS
|= (GoodStr(T_dc
) || GoodStr(T_DC
)) ? TERM_CAN_DELETE
: 0;
295 EL_FLAGS
|= (GoodStr(T_im
) || GoodStr(T_ic
) || GoodStr(T_IC
)) ?
297 EL_FLAGS
|= (GoodStr(T_up
) || GoodStr(T_UP
)) ? TERM_CAN_UP
: 0;
298 EL_FLAGS
|= Val(T_am
) ? TERM_HAS_AUTO_MARGINS
: 0;
299 EL_FLAGS
|= Val(T_xn
) ? TERM_HAS_MAGIC_MARGINS
: 0;
301 if (GoodStr(T_me
) && GoodStr(T_ue
))
302 EL_FLAGS
|= (strcmp(Str(T_me
), Str(T_ue
)) == 0) ?
305 EL_FLAGS
&= ~TERM_CAN_ME
;
306 if (GoodStr(T_me
) && GoodStr(T_se
))
307 EL_FLAGS
|= (strcmp(Str(T_me
), Str(T_se
)) == 0) ?
313 (void) fprintf(el
->el_errfile
,
314 "WARNING: Your terminal cannot move up.\n");
315 (void) fprintf(el
->el_errfile
,
316 "Editing may be odd for long lines.\n");
319 (void) fprintf(el
->el_errfile
, "no clear EOL capability.\n");
321 (void) fprintf(el
->el_errfile
, "no delete char capability.\n");
323 (void) fprintf(el
->el_errfile
, "no insert char capability.\n");
324 #endif /* DEBUG_SCREEN */
328 * Initialize the terminal stuff
331 term_init(EditLine
*el
)
334 el
->el_term
.t_buf
= (char *) el_malloc(TC_BUFSIZE
);
335 if (el
->el_term
.t_buf
== NULL
)
337 el
->el_term
.t_cap
= (char *) el_malloc(TC_BUFSIZE
);
338 if (el
->el_term
.t_cap
== NULL
)
340 el
->el_term
.t_fkey
= (fkey_t
*) el_malloc(A_K_NKEYS
* sizeof(fkey_t
));
341 if (el
->el_term
.t_fkey
== NULL
)
343 el
->el_term
.t_loc
= 0;
344 el
->el_term
.t_str
= (char **) el_malloc(T_str
* sizeof(char *));
345 if (el
->el_term
.t_str
== NULL
)
347 (void) memset(el
->el_term
.t_str
, 0, T_str
* sizeof(char *));
348 el
->el_term
.t_val
= (int *) el_malloc(T_val
* sizeof(int));
349 if (el
->el_term
.t_val
== NULL
)
351 (void) memset(el
->el_term
.t_val
, 0, T_val
* sizeof(int));
352 (void) term_set(el
, NULL
);
358 * Clean up the terminal stuff
361 term_end(EditLine
*el
)
364 el_free((ptr_t
) el
->el_term
.t_buf
);
365 el
->el_term
.t_buf
= NULL
;
366 el_free((ptr_t
) el
->el_term
.t_cap
);
367 el
->el_term
.t_cap
= NULL
;
368 el
->el_term
.t_loc
= 0;
369 el_free((ptr_t
) el
->el_term
.t_str
);
370 el
->el_term
.t_str
= NULL
;
371 el_free((ptr_t
) el
->el_term
.t_val
);
372 el
->el_term
.t_val
= NULL
;
373 el_free((ptr_t
) el
->el_term
.t_fkey
);
374 el
->el_term
.t_fkey
= NULL
;
375 term_free_display(el
);
380 * Maintain a string pool for termcap strings
383 term_alloc(EditLine
*el
, const struct termcapstr
*t
, const char *cap
)
385 char termbuf
[TC_BUFSIZE
];
387 char **tlist
= el
->el_term
.t_str
;
388 char **tmp
, **str
= &tlist
[t
- tstr
];
390 if (cap
== NULL
|| *cap
== '\0') {
396 tlen
= *str
== NULL
? 0 : strlen(*str
);
399 * New string is shorter; no need to allocate space
403 (void) strlcpy(*str
, cap
, tlen
+ 1);
407 * New string is longer; see if we have enough space to append
409 if (el
->el_term
.t_loc
+ 3 < TC_BUFSIZE
) {
410 tlen
= TC_BUFSIZE
- el
->el_term
.t_loc
;
411 (void) strlcpy(*str
= &el
->el_term
.t_buf
[el
->el_term
.t_loc
],
413 el
->el_term
.t_loc
+= (int)clen
+ 1; /* one for \0 */
417 * Compact our buffer; no need to check compaction, cause we know it
421 for (tmp
= tlist
; tmp
< &tlist
[T_str
]; tmp
++)
422 if (*tmp
!= NULL
&& *tmp
!= '\0' && *tmp
!= *str
) {
425 for (ptr
= *tmp
; *ptr
!= '\0'; termbuf
[tlen
++] = *ptr
++)
427 termbuf
[tlen
++] = '\0';
429 memcpy(el
->el_term
.t_buf
, termbuf
, TC_BUFSIZE
);
430 el
->el_term
.t_loc
= (int)tlen
;
431 if (el
->el_term
.t_loc
+ 3 >= TC_BUFSIZE
) {
432 (void) fprintf(el
->el_errfile
,
433 "Out of termcap string space.\n");
436 tlen
= TC_BUFSIZE
- el
->el_term
.t_loc
;
437 (void) strlcpy(*str
= &el
->el_term
.t_buf
[el
->el_term
.t_loc
], cap
, tlen
);
438 el
->el_term
.t_loc
+= (int)clen
+ 1; /* one for \0 */
443 /* term_rebuffer_display():
444 * Rebuffer the display after the screen changed size
447 term_rebuffer_display(EditLine
*el
)
449 coord_t
*c
= &el
->el_term
.t_size
;
451 term_free_display(el
);
456 if (term_alloc_display(el
) == -1)
462 /* term_alloc_display():
463 * Allocate a new display.
466 term_alloc_display(EditLine
*el
)
470 coord_t
*c
= &el
->el_term
.t_size
;
472 b
= el_malloc(sizeof(*b
) * (c
->v
+ 1));
475 for (i
= 0; i
< c
->v
; i
++) {
476 b
[i
] = el_malloc(sizeof(**b
) * (c
->h
+ 1));
479 el_free((ptr_t
) b
[i
]);
487 b
= el_malloc(sizeof(*b
) * (c
->v
+ 1));
490 for (i
= 0; i
< c
->v
; i
++) {
491 b
[i
] = el_malloc(sizeof(**b
) * (c
->h
+ 1));
494 el_free((ptr_t
) b
[i
]);
505 /* term_free_display():
506 * Free the display buffers
509 term_free_display(EditLine
*el
)
515 el
->el_display
= NULL
;
517 for (bufp
= b
; *bufp
!= NULL
; bufp
++)
518 el_free((ptr_t
) *bufp
);
522 el
->el_vdisplay
= NULL
;
524 for (bufp
= b
; *bufp
!= NULL
; bufp
++)
525 el_free((ptr_t
) *bufp
);
531 /* term_move_to_line():
532 * move to line <where> (first line == 0)
533 * as efficiently as possible
536 term_move_to_line(EditLine
*el
, int where
)
540 if (where
== el
->el_cursor
.v
)
543 if (where
> el
->el_term
.t_size
.v
) {
545 (void) fprintf(el
->el_errfile
,
546 "term_move_to_line: where is ridiculous: %d\r\n", where
);
547 #endif /* DEBUG_SCREEN */
550 if ((del
= where
- el
->el_cursor
.v
) > 0) {
552 if (EL_HAS_AUTO_MARGINS
&&
553 el
->el_display
[el
->el_cursor
.v
][0] != '\0') {
554 size_t h
= el
->el_term
.t_size
.h
- 1;
557 el
->el_display
[el
->el_cursor
.v
][h
] ==
562 /* move without newline */
563 term_move_to_char(el
, (int)h
);
564 term_overwrite(el
, &el
->el_display
565 [el
->el_cursor
.v
][el
->el_cursor
.h
],
566 (size_t)(el
->el_term
.t_size
.h
-
571 if ((del
> 1) && GoodStr(T_DO
)) {
572 term_tputs(el
, tgoto(Str(T_DO
), del
,
576 for (; del
> 0; del
--)
577 term__putc(el
, '\n');
578 /* because the \n will become \r\n */
583 } else { /* del < 0 */
584 if (GoodStr(T_UP
) && (-del
> 1 || !GoodStr(T_up
)))
585 term_tputs(el
, tgoto(Str(T_UP
), -del
, -del
), -del
);
588 for (; del
< 0; del
++)
589 term_tputs(el
, Str(T_up
), 1);
592 el
->el_cursor
.v
= where
;/* now where is here */
596 /* term_move_to_char():
597 * Move to the character position specified
600 term_move_to_char(EditLine
*el
, int where
)
605 if (where
== el
->el_cursor
.h
)
608 if (where
> el
->el_term
.t_size
.h
) {
610 (void) fprintf(el
->el_errfile
,
611 "term_move_to_char: where is riduculous: %d\r\n", where
);
612 #endif /* DEBUG_SCREEN */
615 if (!where
) { /* if where is first column */
616 term__putc(el
, '\r'); /* do a CR */
620 del
= where
- el
->el_cursor
.h
;
622 if ((del
< -4 || del
> 4) && GoodStr(T_ch
))
623 /* go there directly */
624 term_tputs(el
, tgoto(Str(T_ch
), where
, where
), where
);
626 if (del
> 0) { /* moving forward */
627 if ((del
> 4) && GoodStr(T_RI
))
628 term_tputs(el
, tgoto(Str(T_RI
), del
, del
), del
);
630 /* if I can do tabs, use them */
632 if ((el
->el_cursor
.h
& 0370) !=
636 el
->el_cursor
.v
][where
& 0370] !=
640 /* if not within tab stop */
642 (el
->el_cursor
.h
& 0370);
645 term__putc(el
, '\t');
647 el
->el_cursor
.h
= where
& ~0x7;
651 * it's usually cheaper to just write the
655 * NOTE THAT term_overwrite() WILL CHANGE
658 term_overwrite(el
, &el
->el_display
[
659 el
->el_cursor
.v
][el
->el_cursor
.h
],
660 (size_t)(where
- el
->el_cursor
.h
));
663 } else { /* del < 0 := moving backward */
664 if ((-del
> 4) && GoodStr(T_LE
))
665 term_tputs(el
, tgoto(Str(T_LE
), -del
, -del
),
667 else { /* can't go directly there */
669 * if the "cost" is greater than the "cost"
673 ((unsigned int)-del
>
674 (((unsigned int) where
>> 3) +
677 term__putc(el
, '\r'); /* do a CR */
679 goto mc_again
; /* and try again */
681 for (i
= 0; i
< -del
; i
++)
682 term__putc(el
, '\b');
686 el
->el_cursor
.h
= where
; /* now where is here */
691 * Overstrike num characters
692 * Assumes MB_FILL_CHARs are present to keep the column count correct
695 term_overwrite(EditLine
*el
, const Char
*cp
, size_t n
)
700 if (n
> (size_t)el
->el_term
.t_size
.h
) {
702 (void) fprintf(el
->el_errfile
,
703 "term_overwrite: n is riduculous: %d\r\n", n
);
704 #endif /* DEBUG_SCREEN */
709 /* term__putc() ignores any MB_FILL_CHARs */
710 term__putc(el
, *cp
++);
714 if (el
->el_cursor
.h
>= el
->el_term
.t_size
.h
) { /* wrap? */
715 if (EL_HAS_AUTO_MARGINS
) { /* yes */
718 if (EL_HAS_MAGIC_MARGINS
) {
719 /* force the wrap to avoid the "magic"
722 if ((c
= el
->el_display
[el
->el_cursor
.v
]
723 [el
->el_cursor
.h
]) != '\0') {
724 term_overwrite(el
, &c
, 1);
726 while (el
->el_display
[el
->el_cursor
.v
]
727 [el
->el_cursor
.h
] == MB_FILL_CHAR
)
735 } else /* no wrap, but cursor stays on screen */
736 el
->el_cursor
.h
= el
->el_term
.t_size
.h
- 1;
741 /* term_deletechars():
742 * Delete num characters
745 term_deletechars(EditLine
*el
, int num
)
750 if (!EL_CAN_DELETE
) {
752 (void) fprintf(el
->el_errfile
, " ERROR: cannot delete \n");
753 #endif /* DEBUG_EDIT */
756 if (num
> el
->el_term
.t_size
.h
) {
758 (void) fprintf(el
->el_errfile
,
759 "term_deletechars: num is riduculous: %d\r\n", num
);
760 #endif /* DEBUG_SCREEN */
763 if (GoodStr(T_DC
)) /* if I have multiple delete */
764 if ((num
> 1) || !GoodStr(T_dc
)) { /* if dc would be more
766 term_tputs(el
, tgoto(Str(T_DC
), num
, num
), num
);
769 if (GoodStr(T_dm
)) /* if I have delete mode */
770 term_tputs(el
, Str(T_dm
), 1);
772 if (GoodStr(T_dc
)) /* else do one at a time */
774 term_tputs(el
, Str(T_dc
), 1);
776 if (GoodStr(T_ed
)) /* if I have delete mode */
777 term_tputs(el
, Str(T_ed
), 1);
781 /* term_insertwrite():
782 * Puts terminal in insert character mode or inserts num
783 * characters in the line
784 * Assumes MB_FILL_CHARs are present to keep column count correct
787 term_insertwrite(EditLine
*el
, Char
*cp
, int num
)
791 if (!EL_CAN_INSERT
) {
793 (void) fprintf(el
->el_errfile
, " ERROR: cannot insert \n");
794 #endif /* DEBUG_EDIT */
797 if (num
> el
->el_term
.t_size
.h
) {
799 (void) fprintf(el
->el_errfile
,
800 "StartInsert: num is riduculous: %d\r\n", num
);
801 #endif /* DEBUG_SCREEN */
804 if (GoodStr(T_IC
)) /* if I have multiple insert */
805 if ((num
> 1) || !GoodStr(T_ic
)) {
806 /* if ic would be more expensive */
807 term_tputs(el
, tgoto(Str(T_IC
), num
, num
), num
);
808 term_overwrite(el
, cp
, (size_t)num
);
809 /* this updates el_cursor.h */
812 if (GoodStr(T_im
) && GoodStr(T_ei
)) { /* if I have insert mode */
813 term_tputs(el
, Str(T_im
), 1);
815 el
->el_cursor
.h
+= num
;
817 term__putc(el
, *cp
++);
820 if (GoodStr(T_ip
)) /* have to make num chars insert */
821 term_tputs(el
, Str(T_ip
), 1);
823 term_tputs(el
, Str(T_ei
), 1);
827 if (GoodStr(T_ic
)) /* have to make num chars insert */
828 term_tputs(el
, Str(T_ic
), 1);
830 term__putc(el
, *cp
++);
834 if (GoodStr(T_ip
)) /* have to make num chars insert */
835 term_tputs(el
, Str(T_ip
), 1);
836 /* pad the inserted char */
843 * clear to end of line. There are num characters to clear
846 term_clear_EOL(EditLine
*el
, int num
)
850 if (EL_CAN_CEOL
&& GoodStr(T_ce
))
851 term_tputs(el
, Str(T_ce
), 1);
853 for (i
= 0; i
< num
; i
++)
855 el
->el_cursor
.h
+= num
; /* have written num spaces */
860 /* term_clear_screen():
864 term_clear_screen(EditLine
*el
)
865 { /* clear the whole screen and home */
868 /* send the clear screen code */
869 term_tputs(el
, Str(T_cl
), Val(T_li
));
870 else if (GoodStr(T_ho
) && GoodStr(T_cd
)) {
871 term_tputs(el
, Str(T_ho
), Val(T_li
)); /* home */
872 /* clear to bottom of screen */
873 term_tputs(el
, Str(T_cd
), Val(T_li
));
875 term__putc(el
, '\r');
876 term__putc(el
, '\n');
882 * Beep the way the terminal wants us
885 term_beep(EditLine
*el
)
888 /* what termcap says we should use */
889 term_tputs(el
, Str(T_bl
), 1);
891 term__putc(el
, '\007'); /* an ASCII bell; ^G */
896 /* term_clear_to_bottom():
897 * Clear to the bottom of the screen
900 term_clear_to_bottom(EditLine
*el
)
903 term_tputs(el
, Str(T_cd
), Val(T_li
));
904 else if (GoodStr(T_ce
))
905 term_tputs(el
, Str(T_ce
), Val(T_li
));
910 term_get(EditLine
*el
, const char **term
)
912 *term
= el
->el_term
.t_name
;
917 * Read in the terminal capabilities from the requested terminal
920 term_set(EditLine
*el
, const char *term
)
923 char buf
[TC_BUFSIZE
];
925 const struct termcapstr
*t
;
929 (void) sigemptyset(&nset
);
930 (void) sigaddset(&nset
, SIGWINCH
);
931 (void) sigprocmask(SIG_BLOCK
, &nset
, &oset
);
937 term
= getenv("TERM");
939 if (!term
|| !term
[0])
942 if (strcmp(term
, "emacs") == 0)
943 el
->el_flags
|= EDIT_DISABLED
;
945 memset(el
->el_term
.t_cap
, 0, TC_BUFSIZE
);
947 i
= tgetent(el
->el_term
.t_cap
, term
);
951 (void) fprintf(el
->el_errfile
,
952 "Cannot read termcap database;\n");
954 (void) fprintf(el
->el_errfile
,
955 "No entry for terminal type \"%s\";\n", term
);
956 (void) fprintf(el
->el_errfile
,
957 "using dumb terminal settings.\n");
958 Val(T_co
) = 80; /* do a dumb terminal */
959 Val(T_pt
) = Val(T_km
) = Val(T_li
) = 0;
960 Val(T_xt
) = Val(T_MT
);
961 for (t
= tstr
; t
->name
!= NULL
; t
++)
962 term_alloc(el
, t
, NULL
);
964 /* auto/magic margins */
965 Val(T_am
) = tgetflag("am");
966 Val(T_xn
) = tgetflag("xn");
968 Val(T_pt
) = tgetflag("pt");
969 Val(T_xt
) = tgetflag("xt");
970 /* do we have a meta? */
971 Val(T_km
) = tgetflag("km");
972 Val(T_MT
) = tgetflag("MT");
974 Val(T_co
) = tgetnum("co");
975 Val(T_li
) = tgetnum("li");
976 for (t
= tstr
; t
->name
!= NULL
; t
++) {
977 /* XXX: some systems' tgetstr needs non const */
978 term_alloc(el
, t
, tgetstr(strchr(t
->name
, *t
->name
),
984 Val(T_co
) = 80; /* just in case */
988 el
->el_term
.t_size
.v
= Val(T_co
);
989 el
->el_term
.t_size
.h
= Val(T_li
);
993 /* get the correct window size */
994 (void) term_get_size(el
, &lins
, &cols
);
995 if (term_change_size(el
, lins
, cols
) == -1)
997 (void) sigprocmask(SIG_SETMASK
, &oset
, NULL
);
999 el
->el_term
.t_name
= term
;
1000 return (i
<= 0 ? -1 : 0);
1005 * Return the new window size in lines and cols, and
1006 * true if the size was changed.
1009 term_get_size(EditLine
*el
, int *lins
, int *cols
)
1018 if (ioctl(el
->el_infd
, TIOCGWINSZ
, (ioctl_t
) & ws
) != -1) {
1029 if (ioctl(el
->el_infd
, TIOCGSIZE
, (ioctl_t
) & ts
) != -1) {
1033 *lins
= ts
.ts_lines
;
1037 return (Val(T_co
) != *cols
|| Val(T_li
) != *lins
);
1041 /* term_change_size():
1042 * Change the size of the terminal
1045 term_change_size(EditLine
*el
, int lins
, int cols
)
1050 Val(T_co
) = (cols
< 2) ? 80 : cols
;
1051 Val(T_li
) = (lins
< 1) ? 24 : lins
;
1053 /* re-make display buffers */
1054 if (term_rebuffer_display(el
) == -1)
1056 re_clear_display(el
);
1061 /* term_init_arrow():
1062 * Initialize the arrow key bindings from termcap
1065 term_init_arrow(EditLine
*el
)
1067 fkey_t
*arrow
= el
->el_term
.t_fkey
;
1069 arrow
[A_K_DN
].name
= STR("down");
1070 arrow
[A_K_DN
].key
= T_kd
;
1071 arrow
[A_K_DN
].fun
.cmd
= ED_NEXT_HISTORY
;
1072 arrow
[A_K_DN
].type
= XK_CMD
;
1074 arrow
[A_K_UP
].name
= STR("up");
1075 arrow
[A_K_UP
].key
= T_ku
;
1076 arrow
[A_K_UP
].fun
.cmd
= ED_PREV_HISTORY
;
1077 arrow
[A_K_UP
].type
= XK_CMD
;
1079 arrow
[A_K_LT
].name
= STR("left");
1080 arrow
[A_K_LT
].key
= T_kl
;
1081 arrow
[A_K_LT
].fun
.cmd
= ED_PREV_CHAR
;
1082 arrow
[A_K_LT
].type
= XK_CMD
;
1084 arrow
[A_K_RT
].name
= STR("right");
1085 arrow
[A_K_RT
].key
= T_kr
;
1086 arrow
[A_K_RT
].fun
.cmd
= ED_NEXT_CHAR
;
1087 arrow
[A_K_RT
].type
= XK_CMD
;
1089 arrow
[A_K_HO
].name
= STR("home");
1090 arrow
[A_K_HO
].key
= T_kh
;
1091 arrow
[A_K_HO
].fun
.cmd
= ED_MOVE_TO_BEG
;
1092 arrow
[A_K_HO
].type
= XK_CMD
;
1094 arrow
[A_K_EN
].name
= STR("end");
1095 arrow
[A_K_EN
].key
= T_at7
;
1096 arrow
[A_K_EN
].fun
.cmd
= ED_MOVE_TO_END
;
1097 arrow
[A_K_EN
].type
= XK_CMD
;
1101 /* term_reset_arrow():
1102 * Reset arrow key bindings
1105 term_reset_arrow(EditLine
*el
)
1107 fkey_t
*arrow
= el
->el_term
.t_fkey
;
1108 static const Char strA
[] = {033, '[', 'A', '\0'};
1109 static const Char strB
[] = {033, '[', 'B', '\0'};
1110 static const Char strC
[] = {033, '[', 'C', '\0'};
1111 static const Char strD
[] = {033, '[', 'D', '\0'};
1112 static const Char strH
[] = {033, '[', 'H', '\0'};
1113 static const Char strF
[] = {033, '[', 'F', '\0'};
1114 static const Char stOA
[] = {033, 'O', 'A', '\0'};
1115 static const Char stOB
[] = {033, 'O', 'B', '\0'};
1116 static const Char stOC
[] = {033, 'O', 'C', '\0'};
1117 static const Char stOD
[] = {033, 'O', 'D', '\0'};
1118 static const Char stOH
[] = {033, 'O', 'H', '\0'};
1119 static const Char stOF
[] = {033, 'O', 'F', '\0'};
1121 key_add(el
, strA
, &arrow
[A_K_UP
].fun
, arrow
[A_K_UP
].type
);
1122 key_add(el
, strB
, &arrow
[A_K_DN
].fun
, arrow
[A_K_DN
].type
);
1123 key_add(el
, strC
, &arrow
[A_K_RT
].fun
, arrow
[A_K_RT
].type
);
1124 key_add(el
, strD
, &arrow
[A_K_LT
].fun
, arrow
[A_K_LT
].type
);
1125 key_add(el
, strH
, &arrow
[A_K_HO
].fun
, arrow
[A_K_HO
].type
);
1126 key_add(el
, strF
, &arrow
[A_K_EN
].fun
, arrow
[A_K_EN
].type
);
1127 key_add(el
, stOA
, &arrow
[A_K_UP
].fun
, arrow
[A_K_UP
].type
);
1128 key_add(el
, stOB
, &arrow
[A_K_DN
].fun
, arrow
[A_K_DN
].type
);
1129 key_add(el
, stOC
, &arrow
[A_K_RT
].fun
, arrow
[A_K_RT
].type
);
1130 key_add(el
, stOD
, &arrow
[A_K_LT
].fun
, arrow
[A_K_LT
].type
);
1131 key_add(el
, stOH
, &arrow
[A_K_HO
].fun
, arrow
[A_K_HO
].type
);
1132 key_add(el
, stOF
, &arrow
[A_K_EN
].fun
, arrow
[A_K_EN
].type
);
1134 if (el
->el_map
.type
== MAP_VI
) {
1135 key_add(el
, &strA
[1], &arrow
[A_K_UP
].fun
, arrow
[A_K_UP
].type
);
1136 key_add(el
, &strB
[1], &arrow
[A_K_DN
].fun
, arrow
[A_K_DN
].type
);
1137 key_add(el
, &strC
[1], &arrow
[A_K_RT
].fun
, arrow
[A_K_RT
].type
);
1138 key_add(el
, &strD
[1], &arrow
[A_K_LT
].fun
, arrow
[A_K_LT
].type
);
1139 key_add(el
, &strH
[1], &arrow
[A_K_HO
].fun
, arrow
[A_K_HO
].type
);
1140 key_add(el
, &strF
[1], &arrow
[A_K_EN
].fun
, arrow
[A_K_EN
].type
);
1141 key_add(el
, &stOA
[1], &arrow
[A_K_UP
].fun
, arrow
[A_K_UP
].type
);
1142 key_add(el
, &stOB
[1], &arrow
[A_K_DN
].fun
, arrow
[A_K_DN
].type
);
1143 key_add(el
, &stOC
[1], &arrow
[A_K_RT
].fun
, arrow
[A_K_RT
].type
);
1144 key_add(el
, &stOD
[1], &arrow
[A_K_LT
].fun
, arrow
[A_K_LT
].type
);
1145 key_add(el
, &stOH
[1], &arrow
[A_K_HO
].fun
, arrow
[A_K_HO
].type
);
1146 key_add(el
, &stOF
[1], &arrow
[A_K_EN
].fun
, arrow
[A_K_EN
].type
);
1151 /* term_set_arrow():
1152 * Set an arrow key binding
1155 term_set_arrow(EditLine
*el
, const Char
*name
, key_value_t
*fun
, int type
)
1157 fkey_t
*arrow
= el
->el_term
.t_fkey
;
1160 for (i
= 0; i
< A_K_NKEYS
; i
++)
1161 if (Strcmp(name
, arrow
[i
].name
) == 0) {
1162 arrow
[i
].fun
= *fun
;
1163 arrow
[i
].type
= type
;
1170 /* term_clear_arrow():
1171 * Clear an arrow key binding
1174 term_clear_arrow(EditLine
*el
, const Char
*name
)
1176 fkey_t
*arrow
= el
->el_term
.t_fkey
;
1179 for (i
= 0; i
< A_K_NKEYS
; i
++)
1180 if (Strcmp(name
, arrow
[i
].name
) == 0) {
1181 arrow
[i
].type
= XK_NOD
;
1188 /* term_print_arrow():
1189 * Print the arrow key bindings
1192 term_print_arrow(EditLine
*el
, const Char
*name
)
1195 fkey_t
*arrow
= el
->el_term
.t_fkey
;
1197 for (i
= 0; i
< A_K_NKEYS
; i
++)
1198 if (*name
== '\0' || Strcmp(name
, arrow
[i
].name
) == 0)
1199 if (arrow
[i
].type
!= XK_NOD
)
1200 key_kprint(el
, arrow
[i
].name
, &arrow
[i
].fun
,
1205 /* term_bind_arrow():
1206 * Bind the arrow keys
1209 term_bind_arrow(EditLine
*el
)
1212 const el_action_t
*dmap
;
1215 fkey_t
*arrow
= el
->el_term
.t_fkey
;
1217 /* Check if the components needed are initialized */
1218 if (el
->el_term
.t_buf
== NULL
|| el
->el_map
.key
== NULL
)
1221 map
= el
->el_map
.type
== MAP_VI
? el
->el_map
.alt
: el
->el_map
.key
;
1222 dmap
= el
->el_map
.type
== MAP_VI
? el
->el_map
.vic
: el
->el_map
.emacs
;
1224 term_reset_arrow(el
);
1226 for (i
= 0; i
< A_K_NKEYS
; i
++) {
1227 Char wt_str
[VISUAL_WIDTH_MAX
];
1231 p
= el
->el_term
.t_str
[arrow
[i
].key
];
1234 for (n
= 0; n
< VISUAL_WIDTH_MAX
&& p
[n
]; ++n
)
1236 while (n
< VISUAL_WIDTH_MAX
)
1239 j
= (unsigned char) *p
;
1241 * Assign the arrow keys only if:
1243 * 1. They are multi-character arrow keys and the user
1244 * has not re-assigned the leading character, or
1245 * has re-assigned the leading character to be
1246 * ED_SEQUENCE_LEAD_IN
1247 * 2. They are single arrow keys pointing to an
1250 if (arrow
[i
].type
== XK_NOD
)
1251 key_clear(el
, map
, px
);
1253 if (p
[1] && (dmap
[j
] == map
[j
] ||
1254 map
[j
] == ED_SEQUENCE_LEAD_IN
)) {
1255 key_add(el
, px
, &arrow
[i
].fun
,
1257 map
[j
] = ED_SEQUENCE_LEAD_IN
;
1258 } else if (map
[j
] == ED_UNASSIGNED
) {
1259 key_clear(el
, map
, px
);
1260 if (arrow
[i
].type
== XK_CMD
)
1261 map
[j
] = arrow
[i
].fun
.cmd
;
1263 key_add(el
, px
, &arrow
[i
].fun
,
1276 if (term_outfile
== NULL
)
1278 return fputc(c
, term_outfile
);
1282 term_tputs(EditLine
*el
, const char *cap
, int affcnt
)
1285 pthread_mutex_lock(&term_mutex
);
1287 term_outfile
= el
->el_outfile
;
1288 (void)tputs(cap
, affcnt
, term_putc
);
1290 pthread_mutex_unlock(&term_mutex
);
1298 term__putc(EditLine
*el
, Int c
)
1300 char buf
[MB_LEN_MAX
+1];
1302 if (c
== MB_FILL_CHAR
)
1304 i
= ct_encode_char(buf
, MB_LEN_MAX
, c
);
1308 return fputs(buf
, el
->el_outfile
);
1315 term__flush(EditLine
*el
)
1318 (void) fflush(el
->el_outfile
);
1322 * Write the given character out, in a human readable form
1325 term_writec(EditLine
*el
, Int c
)
1327 Char visbuf
[VISUAL_WIDTH_MAX
+1];
1328 ssize_t vcnt
= ct_visual_char(visbuf
, VISUAL_WIDTH_MAX
, c
);
1329 visbuf
[vcnt
] = '\0';
1330 term_overwrite(el
, visbuf
, (size_t)vcnt
);
1336 * Print the current termcap characteristics
1340 term_telltc(EditLine
*el
, int argc
__attribute__((__unused__
)),
1341 const Char
**argv
__attribute__((__unused__
)))
1343 const struct termcapstr
*t
;
1346 (void) fprintf(el
->el_outfile
, "\n\tYour terminal has the\n");
1347 (void) fprintf(el
->el_outfile
, "\tfollowing characteristics:\n\n");
1348 (void) fprintf(el
->el_outfile
, "\tIt has %d columns and %d lines\n",
1349 Val(T_co
), Val(T_li
));
1350 (void) fprintf(el
->el_outfile
,
1351 "\tIt has %s meta key\n", EL_HAS_META
? "a" : "no");
1352 (void) fprintf(el
->el_outfile
,
1353 "\tIt can%suse tabs\n", EL_CAN_TAB
? " " : "not ");
1354 (void) fprintf(el
->el_outfile
, "\tIt %s automatic margins\n",
1355 EL_HAS_AUTO_MARGINS
? "has" : "does not have");
1356 if (EL_HAS_AUTO_MARGINS
)
1357 (void) fprintf(el
->el_outfile
, "\tIt %s magic margins\n",
1358 EL_HAS_MAGIC_MARGINS
? "has" : "does not have");
1360 for (t
= tstr
, ts
= el
->el_term
.t_str
; t
->name
!= NULL
; t
++, ts
++) {
1363 ub
= ct_encode_string(ct_visual_string(
1364 ct_decode_string(*ts
, &el
->el_scratch
)),
1369 (void) fprintf(el
->el_outfile
, "\t%25s (%s) == %s\n",
1370 t
->long_name
, t
->name
, ub
);
1372 (void) fputc('\n', el
->el_outfile
);
1378 * Change the current terminal characteristics
1382 term_settc(EditLine
*el
, int argc
__attribute__((__unused__
)),
1385 const struct termcapstr
*ts
;
1386 const struct termcapval
*tv
;
1387 char what
[8], how
[8];
1389 if (argv
== NULL
|| argv
[1] == NULL
|| argv
[2] == NULL
)
1392 strncpy(what
, ct_encode_string(argv
[1], &el
->el_scratch
), sizeof(what
));
1393 what
[sizeof(what
) - 1] = '\0';
1394 strncpy(how
, ct_encode_string(argv
[2], &el
->el_scratch
), sizeof(how
));
1395 how
[sizeof(how
) - 1] = '\0';
1398 * Do the strings first
1400 for (ts
= tstr
; ts
->name
!= NULL
; ts
++)
1401 if (strcmp(ts
->name
, what
) == 0)
1404 if (ts
->name
!= NULL
) {
1405 term_alloc(el
, ts
, how
);
1410 * Do the numeric ones second
1412 for (tv
= tval
; tv
->name
!= NULL
; tv
++)
1413 if (strcmp(tv
->name
, what
) == 0)
1416 if (tv
->name
!= NULL
)
1419 if (tv
== &tval
[T_pt
] || tv
== &tval
[T_km
] ||
1420 tv
== &tval
[T_am
] || tv
== &tval
[T_xn
]) {
1421 if (strcmp(how
, "yes") == 0)
1422 el
->el_term
.t_val
[tv
- tval
] = 1;
1423 else if (strcmp(how
, "no") == 0)
1424 el
->el_term
.t_val
[tv
- tval
] = 0;
1426 (void) fprintf(el
->el_errfile
,
1427 "" FSTR
": Bad value `%s'.\n", argv
[0], how
);
1431 if (term_change_size(el
, Val(T_li
), Val(T_co
)) == -1)
1438 i
= strtol(how
, &ep
, 10);
1440 (void) fprintf(el
->el_errfile
,
1441 "" FSTR
": Bad value `%s'.\n", argv
[0], how
);
1444 el
->el_term
.t_val
[tv
- tval
] = (int) i
;
1445 el
->el_term
.t_size
.v
= Val(T_co
);
1446 el
->el_term
.t_size
.h
= Val(T_li
);
1447 if (tv
== &tval
[T_co
] || tv
== &tval
[T_li
])
1448 if (term_change_size(el
, Val(T_li
), Val(T_co
))
1457 * Get the current terminal characteristics
1461 term_gettc(EditLine
*el
, int argc
__attribute__((__unused__
)), char **argv
)
1463 const struct termcapstr
*ts
;
1464 const struct termcapval
*tv
;
1468 if (argv
== NULL
|| argv
[1] == NULL
|| argv
[2] == NULL
)
1475 * Do the strings first
1477 for (ts
= tstr
; ts
->name
!= NULL
; ts
++)
1478 if (strcmp(ts
->name
, what
) == 0)
1481 if (ts
->name
!= NULL
) {
1482 *(char **)how
= el
->el_term
.t_str
[ts
- tstr
];
1486 * Do the numeric ones second
1488 for (tv
= tval
; tv
->name
!= NULL
; tv
++)
1489 if (strcmp(tv
->name
, what
) == 0)
1492 if (tv
->name
== NULL
)
1495 if (tv
== &tval
[T_pt
] || tv
== &tval
[T_km
] ||
1496 tv
== &tval
[T_am
] || tv
== &tval
[T_xn
]) {
1497 static char yes
[] = "yes";
1498 static char no
[] = "no";
1499 if (el
->el_term
.t_val
[tv
- tval
])
1500 *(char **)how
= yes
;
1505 *(int *)how
= el
->el_term
.t_val
[tv
- tval
];
1511 * Print the termcap string out with variable substitution
1515 term_echotc(EditLine
*el
, int argc
__attribute__((__unused__
)),
1520 int arg_need
, arg_cols
, arg_rows
;
1521 int verbose
= 0, silent
= 0;
1523 static const char fmts
[] = "%s\n", fmtd
[] = "%d\n";
1524 const struct termcapstr
*t
;
1525 char buf
[TC_BUFSIZE
];
1530 if (argv
== NULL
|| argv
[1] == NULL
)
1534 if (argv
[0][0] == '-') {
1535 switch (argv
[0][1]) {
1543 /* stderror(ERR_NAME | ERR_TCUSAGE); */
1548 if (!*argv
|| *argv
[0] == '\0')
1550 if (Strcmp(*argv
, STR("tabs")) == 0) {
1551 (void) fprintf(el
->el_outfile
, fmts
, EL_CAN_TAB
? "yes" : "no");
1553 } else if (Strcmp(*argv
, STR("meta")) == 0) {
1554 (void) fprintf(el
->el_outfile
, fmts
, Val(T_km
) ? "yes" : "no");
1556 } else if (Strcmp(*argv
, STR("xn")) == 0) {
1557 (void) fprintf(el
->el_outfile
, fmts
, EL_HAS_MAGIC_MARGINS
?
1560 } else if (Strcmp(*argv
, STR("am")) == 0) {
1561 (void) fprintf(el
->el_outfile
, fmts
, EL_HAS_AUTO_MARGINS
?
1564 } else if (Strcmp(*argv
, STR("baud")) == 0) {
1568 for (i
= 0; baud_rate
[i
].b_name
!= NULL
; i
++)
1569 if (el
->el_tty
.t_speed
== baud_rate
[i
].b_rate
) {
1570 (void) fprintf(el
->el_outfile
, fmts
,
1571 baud_rate
[i
].b_name
);
1574 (void) fprintf(el
->el_outfile
, fmtd
, 0);
1576 (void) fprintf(el
->el_outfile
, fmtd
, (int)el
->el_tty
.t_speed
);
1579 } else if (Strcmp(*argv
, STR("rows")) == 0 ||
1580 Strcmp(*argv
, STR("lines")) == 0) {
1581 (void) fprintf(el
->el_outfile
, fmtd
, Val(T_li
));
1583 } else if (Strcmp(*argv
, STR("cols")) == 0) {
1584 (void) fprintf(el
->el_outfile
, fmtd
, Val(T_co
));
1588 * Try to use our local definition first
1591 for (t
= tstr
; t
->name
!= NULL
; t
++)
1593 ct_encode_string(*argv
, &el
->el_scratch
)) == 0) {
1594 scap
= el
->el_term
.t_str
[t
- tstr
];
1597 if (t
->name
== NULL
) {
1598 /* XXX: some systems' tgetstr needs non const */
1599 scap
= tgetstr(ct_encode_string(*argv
, &el
->el_scratch
), &area
);
1601 if (!scap
|| scap
[0] == '\0') {
1603 (void) fprintf(el
->el_errfile
,
1604 "echotc: Termcap parameter `" FSTR
"' not found.\n",
1609 * Count home many values we need for this capability.
1611 for (cap
= scap
, arg_need
= 0; *cap
; cap
++)
1631 * hpux has lot's of them...
1634 (void) fprintf(el
->el_errfile
,
1635 "echotc: Warning: unknown termcap %% `%c'.\n",
1637 /* This is bad, but I won't complain */
1644 if (*argv
&& *argv
[0]) {
1646 (void) fprintf(el
->el_errfile
,
1647 "echotc: Warning: Extra argument `" FSTR
"'.\n",
1651 term_tputs(el
, scap
, 1);
1655 if (!*argv
|| *argv
[0] == '\0') {
1657 (void) fprintf(el
->el_errfile
,
1658 "echotc: Warning: Missing argument.\n");
1662 i
= Strtol(*argv
, &ep
, 10);
1663 if (*ep
!= '\0' || i
< 0) {
1665 (void) fprintf(el
->el_errfile
,
1666 "echotc: Bad value `" FSTR
"' for rows.\n",
1672 if (*argv
&& *argv
[0]) {
1674 (void) fprintf(el
->el_errfile
,
1675 "echotc: Warning: Extra argument `" FSTR
"'.\n",
1679 term_tputs(el
, tgoto(scap
, arg_cols
, arg_rows
), 1);
1682 /* This is wrong, but I will ignore it... */
1684 (void) fprintf(el
->el_errfile
,
1685 "echotc: Warning: Too many required arguments (%d).\n",
1690 if (!*argv
|| *argv
[0] == '\0') {
1692 (void) fprintf(el
->el_errfile
,
1693 "echotc: Warning: Missing argument.\n");
1696 i
= Strtol(*argv
, &ep
, 10);
1697 if (*ep
!= '\0' || i
< 0) {
1699 (void) fprintf(el
->el_errfile
,
1700 "echotc: Bad value `" FSTR
"' for cols.\n",
1706 if (!*argv
|| *argv
[0] == '\0') {
1708 (void) fprintf(el
->el_errfile
,
1709 "echotc: Warning: Missing argument.\n");
1712 i
= Strtol(*argv
, &ep
, 10);
1713 if (*ep
!= '\0' || i
< 0) {
1715 (void) fprintf(el
->el_errfile
,
1716 "echotc: Bad value `" FSTR
"' for rows.\n",
1723 (void) fprintf(el
->el_errfile
,
1724 "echotc: Bad value `" FSTR
"'.\n", *argv
);
1728 if (*argv
&& *argv
[0]) {
1730 (void) fprintf(el
->el_errfile
,
1731 "echotc: Warning: Extra argument `" FSTR
"'.\n",
1735 term_tputs(el
, tgoto(scap
, arg_cols
, arg_rows
), arg_rows
);