1 /* $NetBSD: term.c,v 1.56 2009/12/28 21:54:21 christos Exp $ */
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
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
36 #if !defined(lint) && !defined(SCCSID)
38 static char sccsid
[] = "@(#)term.c 8.2 (Berkeley) 4/30/95";
40 __RCSID("$NetBSD: term.c,v 1.56 2009/12/28 21:54:21 christos Exp $");
42 #endif /* not lint && not SCCSID */
45 * term.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!
63 /* Solaris's term.h does horrid things. */
65 #if defined(HAVE_TERM_H) && !defined(__sun)
68 #include <sys/types.h>
69 #include <sys/ioctl.h>
78 * IMPORTANT NOTE: these routines are allowed to look at the current screen
79 * and the current position assuming that it is correct. If this is not
80 * true, then the update will be WRONG! This is (should be) a valid
84 #define TC_BUFSIZE 2048
86 #define GoodStr(a) (el->el_term.t_str[a] != NULL && \
87 el->el_term.t_str[a][0] != '\0')
88 #define Str(a) el->el_term.t_str[a]
89 #define Val(a) el->el_term.t_val[a]
92 private const struct {
163 private const struct termcapstr
{
165 const char *long_name
;
168 { "al", "add new blank line" },
170 { "bl", "audible bell" },
172 { "cd", "clear to bottom" },
174 { "ce", "clear to end of line" },
176 { "ch", "cursor to horiz pos" },
178 { "cl", "clear screen" },
180 { "dc", "delete a character" },
182 { "dl", "delete a line" },
184 { "dm", "start delete mode" },
186 { "ed", "end delete mode" },
188 { "ei", "end insert mode" },
190 { "fs", "cursor from status line" },
192 { "ho", "home cursor" },
194 { "ic", "insert character" },
196 { "im", "start insert mode" },
198 { "ip", "insert padding" },
200 { "kd", "sends cursor down" },
202 { "kl", "sends cursor left" },
204 { "kr", "sends cursor right" },
206 { "ku", "sends cursor up" },
208 { "md", "begin bold" },
210 { "me", "end attributes" },
212 { "nd", "non destructive space" },
214 { "se", "end standout" },
216 { "so", "begin standout" },
218 { "ts", "cursor to status line" },
220 { "up", "cursor up one" },
222 { "us", "begin underline" },
224 { "ue", "end underline" },
226 { "vb", "visible bell" },
228 { "DC", "delete multiple chars" },
230 { "DO", "cursor down multiple" },
232 { "IC", "insert multiple chars" },
234 { "LE", "cursor left multiple" },
236 { "RI", "cursor right multiple" },
238 { "UP", "cursor up multiple" },
240 { "kh", "send cursor home" },
242 { "@7", "send cursor end" },
247 private const struct termcapval
{
249 const char *long_name
;
252 { "am", "has automatic margins" },
254 { "pt", "has physical tabs" },
256 { "li", "Number of lines" },
258 { "co", "Number of columns" },
260 { "km", "Has meta key" },
262 { "xt", "Tab chars destructive" },
264 { "xn", "newline ignored at right margin" },
266 { "MT", "Has meta key" }, /* XXX? */
270 /* do two or more of the attributes use me */
272 private void term_setflags(EditLine
*);
273 private int term_rebuffer_display(EditLine
*);
274 private void term_free_display(EditLine
*);
275 private int term_alloc_display(EditLine
*);
276 private void term_alloc(EditLine
*, const struct termcapstr
*, const char *);
277 private void term_init_arrow(EditLine
*);
278 private void term_reset_arrow(EditLine
*);
279 private int term_putc(int);
280 private void term_tputs(EditLine
*, const char *, int);
283 private pthread_mutex_t term_mutex
= PTHREAD_MUTEX_INITIALIZER
;
285 private FILE *term_outfile
= NULL
;
289 * Set the terminal capability flags
292 term_setflags(EditLine
*el
)
295 if (el
->el_tty
.t_tabs
)
296 EL_FLAGS
|= (Val(T_pt
) && !Val(T_xt
)) ? TERM_CAN_TAB
: 0;
298 EL_FLAGS
|= (Val(T_km
) || Val(T_MT
)) ? TERM_HAS_META
: 0;
299 EL_FLAGS
|= GoodStr(T_ce
) ? TERM_CAN_CEOL
: 0;
300 EL_FLAGS
|= (GoodStr(T_dc
) || GoodStr(T_DC
)) ? TERM_CAN_DELETE
: 0;
301 EL_FLAGS
|= (GoodStr(T_im
) || GoodStr(T_ic
) || GoodStr(T_IC
)) ?
303 EL_FLAGS
|= (GoodStr(T_up
) || GoodStr(T_UP
)) ? TERM_CAN_UP
: 0;
304 EL_FLAGS
|= Val(T_am
) ? TERM_HAS_AUTO_MARGINS
: 0;
305 EL_FLAGS
|= Val(T_xn
) ? TERM_HAS_MAGIC_MARGINS
: 0;
307 if (GoodStr(T_me
) && GoodStr(T_ue
))
308 EL_FLAGS
|= (strcmp(Str(T_me
), Str(T_ue
)) == 0) ?
311 EL_FLAGS
&= ~TERM_CAN_ME
;
312 if (GoodStr(T_me
) && GoodStr(T_se
))
313 EL_FLAGS
|= (strcmp(Str(T_me
), Str(T_se
)) == 0) ?
319 (void) fprintf(el
->el_errfile
,
320 "WARNING: Your terminal cannot move up.\n");
321 (void) fprintf(el
->el_errfile
,
322 "Editing may be odd for long lines.\n");
325 (void) fprintf(el
->el_errfile
, "no clear EOL capability.\n");
327 (void) fprintf(el
->el_errfile
, "no delete char capability.\n");
329 (void) fprintf(el
->el_errfile
, "no insert char capability.\n");
330 #endif /* DEBUG_SCREEN */
334 * Initialize the terminal stuff
337 term_init(EditLine
*el
)
340 el
->el_term
.t_buf
= (char *) el_malloc(TC_BUFSIZE
);
341 if (el
->el_term
.t_buf
== NULL
)
343 el
->el_term
.t_cap
= (char *) el_malloc(TC_BUFSIZE
);
344 if (el
->el_term
.t_cap
== NULL
)
346 el
->el_term
.t_fkey
= (fkey_t
*) el_malloc(A_K_NKEYS
* sizeof(fkey_t
));
347 if (el
->el_term
.t_fkey
== NULL
)
349 el
->el_term
.t_loc
= 0;
350 el
->el_term
.t_str
= (char **) el_malloc(T_str
* sizeof(char *));
351 if (el
->el_term
.t_str
== NULL
)
353 (void) memset(el
->el_term
.t_str
, 0, T_str
* sizeof(char *));
354 el
->el_term
.t_val
= (int *) el_malloc(T_val
* sizeof(int));
355 if (el
->el_term
.t_val
== NULL
)
357 (void) memset(el
->el_term
.t_val
, 0, T_val
* sizeof(int));
358 (void) term_set(el
, NULL
);
364 * Clean up the terminal stuff
367 term_end(EditLine
*el
)
370 el_free((ptr_t
) el
->el_term
.t_buf
);
371 el
->el_term
.t_buf
= NULL
;
372 el_free((ptr_t
) el
->el_term
.t_cap
);
373 el
->el_term
.t_cap
= NULL
;
374 el
->el_term
.t_loc
= 0;
375 el_free((ptr_t
) el
->el_term
.t_str
);
376 el
->el_term
.t_str
= NULL
;
377 el_free((ptr_t
) el
->el_term
.t_val
);
378 el
->el_term
.t_val
= NULL
;
379 el_free((ptr_t
) el
->el_term
.t_fkey
);
380 el
->el_term
.t_fkey
= NULL
;
381 term_free_display(el
);
386 * Maintain a string pool for termcap strings
389 term_alloc(EditLine
*el
, const struct termcapstr
*t
, const char *cap
)
391 char termbuf
[TC_BUFSIZE
];
393 char **tlist
= el
->el_term
.t_str
;
394 char **tmp
, **str
= &tlist
[t
- tstr
];
396 if (cap
== NULL
|| *cap
== '\0') {
402 tlen
= *str
== NULL
? 0 : strlen(*str
);
405 * New string is shorter; no need to allocate space
409 (void) strcpy(*str
, cap
); /* XXX strcpy is safe */
413 * New string is longer; see if we have enough space to append
415 if (el
->el_term
.t_loc
+ 3 < TC_BUFSIZE
) {
416 /* XXX strcpy is safe */
417 (void) strcpy(*str
= &el
->el_term
.t_buf
[el
->el_term
.t_loc
],
419 el
->el_term
.t_loc
+= (int)clen
+ 1; /* one for \0 */
423 * Compact our buffer; no need to check compaction, cause we know it
427 for (tmp
= tlist
; tmp
< &tlist
[T_str
]; tmp
++)
428 if (*tmp
!= NULL
&& *tmp
!= '\0' && *tmp
!= *str
) {
431 for (ptr
= *tmp
; *ptr
!= '\0'; termbuf
[tlen
++] = *ptr
++)
433 termbuf
[tlen
++] = '\0';
435 memcpy(el
->el_term
.t_buf
, termbuf
, TC_BUFSIZE
);
436 el
->el_term
.t_loc
= (int)tlen
;
437 if (el
->el_term
.t_loc
+ 3 >= TC_BUFSIZE
) {
438 (void) fprintf(el
->el_errfile
,
439 "Out of termcap string space.\n");
442 /* XXX strcpy is safe */
443 (void) strcpy(*str
= &el
->el_term
.t_buf
[el
->el_term
.t_loc
], cap
);
444 el
->el_term
.t_loc
+= (int)clen
+ 1; /* one for \0 */
449 /* term_rebuffer_display():
450 * Rebuffer the display after the screen changed size
453 term_rebuffer_display(EditLine
*el
)
455 coord_t
*c
= &el
->el_term
.t_size
;
457 term_free_display(el
);
462 if (term_alloc_display(el
) == -1)
468 /* term_alloc_display():
469 * Allocate a new display.
472 term_alloc_display(EditLine
*el
)
476 coord_t
*c
= &el
->el_term
.t_size
;
478 b
= el_malloc(sizeof(*b
) * (c
->v
+ 1));
481 for (i
= 0; i
< c
->v
; i
++) {
482 b
[i
] = el_malloc(sizeof(**b
) * (c
->h
+ 1));
485 el_free((ptr_t
) b
[i
]);
493 b
= el_malloc(sizeof(*b
) * (c
->v
+ 1));
496 for (i
= 0; i
< c
->v
; i
++) {
497 b
[i
] = el_malloc(sizeof(**b
) * (c
->h
+ 1));
500 el_free((ptr_t
) b
[i
]);
511 /* term_free_display():
512 * Free the display buffers
515 term_free_display(EditLine
*el
)
521 el
->el_display
= NULL
;
523 for (bufp
= b
; *bufp
!= NULL
; bufp
++)
524 el_free((ptr_t
) *bufp
);
528 el
->el_vdisplay
= NULL
;
530 for (bufp
= b
; *bufp
!= NULL
; bufp
++)
531 el_free((ptr_t
) *bufp
);
537 /* term_move_to_line():
538 * move to line <where> (first line == 0)
539 * as efficiently as possible
542 term_move_to_line(EditLine
*el
, int where
)
546 if (where
== el
->el_cursor
.v
)
549 if (where
> el
->el_term
.t_size
.v
) {
551 (void) fprintf(el
->el_errfile
,
552 "term_move_to_line: where is ridiculous: %d\r\n", where
);
553 #endif /* DEBUG_SCREEN */
556 if ((del
= where
- el
->el_cursor
.v
) > 0) {
558 if (EL_HAS_AUTO_MARGINS
&&
559 el
->el_display
[el
->el_cursor
.v
][0] != '\0') {
560 size_t h
= el
->el_term
.t_size
.h
- 1;
563 el
->el_display
[el
->el_cursor
.v
][h
] ==
568 /* move without newline */
569 term_move_to_char(el
, (int)h
);
570 term_overwrite(el
, &el
->el_display
571 [el
->el_cursor
.v
][el
->el_cursor
.h
],
572 (size_t)(el
->el_term
.t_size
.h
-
577 if ((del
> 1) && GoodStr(T_DO
)) {
578 term_tputs(el
, tgoto(Str(T_DO
), del
,
582 for (; del
> 0; del
--)
583 term__putc(el
, '\n');
584 /* because the \n will become \r\n */
589 } else { /* del < 0 */
590 if (GoodStr(T_UP
) && (-del
> 1 || !GoodStr(T_up
)))
591 term_tputs(el
, tgoto(Str(T_UP
), -del
, -del
), -del
);
594 for (; del
< 0; del
++)
595 term_tputs(el
, Str(T_up
), 1);
598 el
->el_cursor
.v
= where
;/* now where is here */
602 /* term_move_to_char():
603 * Move to the character position specified
606 term_move_to_char(EditLine
*el
, int where
)
611 if (where
== el
->el_cursor
.h
)
614 if (where
> el
->el_term
.t_size
.h
) {
616 (void) fprintf(el
->el_errfile
,
617 "term_move_to_char: where is riduculous: %d\r\n", where
);
618 #endif /* DEBUG_SCREEN */
621 if (!where
) { /* if where is first column */
622 term__putc(el
, '\r'); /* do a CR */
626 del
= where
- el
->el_cursor
.h
;
628 if ((del
< -4 || del
> 4) && GoodStr(T_ch
))
629 /* go there directly */
630 term_tputs(el
, tgoto(Str(T_ch
), where
, where
), where
);
632 if (del
> 0) { /* moving forward */
633 if ((del
> 4) && GoodStr(T_RI
))
634 term_tputs(el
, tgoto(Str(T_RI
), del
, del
), del
);
636 /* if I can do tabs, use them */
638 if ((el
->el_cursor
.h
& 0370) !=
642 el
->el_cursor
.v
][where
& 0370] !=
646 /* if not within tab stop */
648 (el
->el_cursor
.h
& 0370);
651 term__putc(el
, '\t');
653 el
->el_cursor
.h
= where
& ~0x7;
657 * it's usually cheaper to just write the
661 * NOTE THAT term_overwrite() WILL CHANGE
664 term_overwrite(el
, &el
->el_display
[
665 el
->el_cursor
.v
][el
->el_cursor
.h
],
666 (size_t)(where
- el
->el_cursor
.h
));
669 } else { /* del < 0 := moving backward */
670 if ((-del
> 4) && GoodStr(T_LE
))
671 term_tputs(el
, tgoto(Str(T_LE
), -del
, -del
),
673 else { /* can't go directly there */
675 * if the "cost" is greater than the "cost"
679 ((unsigned int)-del
>
680 (((unsigned int) where
>> 3) +
683 term__putc(el
, '\r'); /* do a CR */
685 goto mc_again
; /* and try again */
687 for (i
= 0; i
< -del
; i
++)
688 term__putc(el
, '\b');
692 el
->el_cursor
.h
= where
; /* now where is here */
697 * Overstrike num characters
698 * Assumes MB_FILL_CHARs are present to keep the column count correct
701 term_overwrite(EditLine
*el
, const Char
*cp
, size_t n
)
706 if (n
> (size_t)el
->el_term
.t_size
.h
) {
708 (void) fprintf(el
->el_errfile
,
709 "term_overwrite: n is riduculous: %d\r\n", n
);
710 #endif /* DEBUG_SCREEN */
715 /* term__putc() ignores any MB_FILL_CHARs */
716 term__putc(el
, *cp
++);
720 if (el
->el_cursor
.h
>= el
->el_term
.t_size
.h
) { /* wrap? */
721 if (EL_HAS_AUTO_MARGINS
) { /* yes */
724 if (EL_HAS_MAGIC_MARGINS
) {
725 /* force the wrap to avoid the "magic"
728 if ((c
= el
->el_display
[el
->el_cursor
.v
]
729 [el
->el_cursor
.h
]) != '\0') {
730 term_overwrite(el
, &c
, 1);
732 while (el
->el_display
[el
->el_cursor
.v
]
733 [el
->el_cursor
.h
] == MB_FILL_CHAR
)
741 } else /* no wrap, but cursor stays on screen */
742 el
->el_cursor
.h
= el
->el_term
.t_size
.h
- 1;
747 /* term_deletechars():
748 * Delete num characters
751 term_deletechars(EditLine
*el
, int num
)
756 if (!EL_CAN_DELETE
) {
758 (void) fprintf(el
->el_errfile
, " ERROR: cannot delete \n");
759 #endif /* DEBUG_EDIT */
762 if (num
> el
->el_term
.t_size
.h
) {
764 (void) fprintf(el
->el_errfile
,
765 "term_deletechars: num is riduculous: %d\r\n", num
);
766 #endif /* DEBUG_SCREEN */
769 if (GoodStr(T_DC
)) /* if I have multiple delete */
770 if ((num
> 1) || !GoodStr(T_dc
)) { /* if dc would be more
772 term_tputs(el
, tgoto(Str(T_DC
), num
, num
), num
);
775 if (GoodStr(T_dm
)) /* if I have delete mode */
776 term_tputs(el
, Str(T_dm
), 1);
778 if (GoodStr(T_dc
)) /* else do one at a time */
780 term_tputs(el
, Str(T_dc
), 1);
782 if (GoodStr(T_ed
)) /* if I have delete mode */
783 term_tputs(el
, Str(T_ed
), 1);
787 /* term_insertwrite():
788 * Puts terminal in insert character mode or inserts num
789 * characters in the line
790 * Assumes MB_FILL_CHARs are present to keep column count correct
793 term_insertwrite(EditLine
*el
, Char
*cp
, int num
)
797 if (!EL_CAN_INSERT
) {
799 (void) fprintf(el
->el_errfile
, " ERROR: cannot insert \n");
800 #endif /* DEBUG_EDIT */
803 if (num
> el
->el_term
.t_size
.h
) {
805 (void) fprintf(el
->el_errfile
,
806 "StartInsert: num is riduculous: %d\r\n", num
);
807 #endif /* DEBUG_SCREEN */
810 if (GoodStr(T_IC
)) /* if I have multiple insert */
811 if ((num
> 1) || !GoodStr(T_ic
)) {
812 /* if ic would be more expensive */
813 term_tputs(el
, tgoto(Str(T_IC
), num
, num
), num
);
814 term_overwrite(el
, cp
, (size_t)num
);
815 /* this updates el_cursor.h */
818 if (GoodStr(T_im
) && GoodStr(T_ei
)) { /* if I have insert mode */
819 term_tputs(el
, Str(T_im
), 1);
821 el
->el_cursor
.h
+= num
;
823 term__putc(el
, *cp
++);
826 if (GoodStr(T_ip
)) /* have to make num chars insert */
827 term_tputs(el
, Str(T_ip
), 1);
829 term_tputs(el
, Str(T_ei
), 1);
833 if (GoodStr(T_ic
)) /* have to make num chars insert */
834 term_tputs(el
, Str(T_ic
), 1);
836 term__putc(el
, *cp
++);
840 if (GoodStr(T_ip
)) /* have to make num chars insert */
841 term_tputs(el
, Str(T_ip
), 1);
842 /* pad the inserted char */
849 * clear to end of line. There are num characters to clear
852 term_clear_EOL(EditLine
*el
, int num
)
856 if (EL_CAN_CEOL
&& GoodStr(T_ce
))
857 term_tputs(el
, Str(T_ce
), 1);
859 for (i
= 0; i
< num
; i
++)
861 el
->el_cursor
.h
+= num
; /* have written num spaces */
866 /* term_clear_screen():
870 term_clear_screen(EditLine
*el
)
871 { /* clear the whole screen and home */
874 /* send the clear screen code */
875 term_tputs(el
, Str(T_cl
), Val(T_li
));
876 else if (GoodStr(T_ho
) && GoodStr(T_cd
)) {
877 term_tputs(el
, Str(T_ho
), Val(T_li
)); /* home */
878 /* clear to bottom of screen */
879 term_tputs(el
, Str(T_cd
), Val(T_li
));
881 term__putc(el
, '\r');
882 term__putc(el
, '\n');
888 * Beep the way the terminal wants us
891 term_beep(EditLine
*el
)
894 /* what termcap says we should use */
895 term_tputs(el
, Str(T_bl
), 1);
897 term__putc(el
, '\007'); /* an ASCII bell; ^G */
902 /* term_clear_to_bottom():
903 * Clear to the bottom of the screen
906 term_clear_to_bottom(EditLine
*el
)
909 term_tputs(el
, Str(T_cd
), Val(T_li
));
910 else if (GoodStr(T_ce
))
911 term_tputs(el
, Str(T_ce
), Val(T_li
));
916 term_get(EditLine
*el
, const char **term
)
918 *term
= el
->el_term
.t_name
;
923 * Read in the terminal capabilities from the requested terminal
926 term_set(EditLine
*el
, const char *term
)
929 char buf
[TC_BUFSIZE
];
931 const struct termcapstr
*t
;
935 (void) sigemptyset(&nset
);
936 (void) sigaddset(&nset
, SIGWINCH
);
937 (void) sigprocmask(SIG_BLOCK
, &nset
, &oset
);
943 term
= getenv("TERM");
945 if (!term
|| !term
[0])
948 if (strcmp(term
, "emacs") == 0)
949 el
->el_flags
|= EDIT_DISABLED
;
951 memset(el
->el_term
.t_cap
, 0, TC_BUFSIZE
);
953 i
= tgetent(el
->el_term
.t_cap
, term
);
957 (void) fprintf(el
->el_errfile
,
958 "Cannot read termcap database;\n");
960 (void) fprintf(el
->el_errfile
,
961 "No entry for terminal type \"%s\";\n", term
);
962 (void) fprintf(el
->el_errfile
,
963 "using dumb terminal settings.\n");
964 Val(T_co
) = 80; /* do a dumb terminal */
965 Val(T_pt
) = Val(T_km
) = Val(T_li
) = 0;
966 Val(T_xt
) = Val(T_MT
);
967 for (t
= tstr
; t
->name
!= NULL
; t
++)
968 term_alloc(el
, t
, NULL
);
970 /* auto/magic margins */
971 Val(T_am
) = tgetflag("am");
972 Val(T_xn
) = tgetflag("xn");
974 Val(T_pt
) = tgetflag("pt");
975 Val(T_xt
) = tgetflag("xt");
976 /* do we have a meta? */
977 Val(T_km
) = tgetflag("km");
978 Val(T_MT
) = tgetflag("MT");
980 Val(T_co
) = tgetnum("co");
981 Val(T_li
) = tgetnum("li");
982 for (t
= tstr
; t
->name
!= NULL
; t
++) {
983 /* XXX: some systems' tgetstr needs non const */
984 term_alloc(el
, t
, tgetstr(strchr(t
->name
, *t
->name
),
990 Val(T_co
) = 80; /* just in case */
994 el
->el_term
.t_size
.v
= Val(T_co
);
995 el
->el_term
.t_size
.h
= Val(T_li
);
999 /* get the correct window size */
1000 (void) term_get_size(el
, &lins
, &cols
);
1001 if (term_change_size(el
, lins
, cols
) == -1)
1003 (void) sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1004 term_bind_arrow(el
);
1005 el
->el_term
.t_name
= term
;
1006 return (i
<= 0 ? -1 : 0);
1011 * Return the new window size in lines and cols, and
1012 * true if the size was changed.
1015 term_get_size(EditLine
*el
, int *lins
, int *cols
)
1024 if (ioctl(el
->el_infd
, TIOCGWINSZ
, (ioctl_t
) & ws
) != -1) {
1035 if (ioctl(el
->el_infd
, TIOCGSIZE
, (ioctl_t
) & ts
) != -1) {
1039 *lins
= ts
.ts_lines
;
1043 return (Val(T_co
) != *cols
|| Val(T_li
) != *lins
);
1047 /* term_change_size():
1048 * Change the size of the terminal
1051 term_change_size(EditLine
*el
, int lins
, int cols
)
1056 Val(T_co
) = (cols
< 2) ? 80 : cols
;
1057 Val(T_li
) = (lins
< 1) ? 24 : lins
;
1059 /* re-make display buffers */
1060 if (term_rebuffer_display(el
) == -1)
1062 re_clear_display(el
);
1067 /* term_init_arrow():
1068 * Initialize the arrow key bindings from termcap
1071 term_init_arrow(EditLine
*el
)
1073 fkey_t
*arrow
= el
->el_term
.t_fkey
;
1075 arrow
[A_K_DN
].name
= STR("down");
1076 arrow
[A_K_DN
].key
= T_kd
;
1077 arrow
[A_K_DN
].fun
.cmd
= ED_NEXT_HISTORY
;
1078 arrow
[A_K_DN
].type
= XK_CMD
;
1080 arrow
[A_K_UP
].name
= STR("up");
1081 arrow
[A_K_UP
].key
= T_ku
;
1082 arrow
[A_K_UP
].fun
.cmd
= ED_PREV_HISTORY
;
1083 arrow
[A_K_UP
].type
= XK_CMD
;
1085 arrow
[A_K_LT
].name
= STR("left");
1086 arrow
[A_K_LT
].key
= T_kl
;
1087 arrow
[A_K_LT
].fun
.cmd
= ED_PREV_CHAR
;
1088 arrow
[A_K_LT
].type
= XK_CMD
;
1090 arrow
[A_K_RT
].name
= STR("right");
1091 arrow
[A_K_RT
].key
= T_kr
;
1092 arrow
[A_K_RT
].fun
.cmd
= ED_NEXT_CHAR
;
1093 arrow
[A_K_RT
].type
= XK_CMD
;
1095 arrow
[A_K_HO
].name
= STR("home");
1096 arrow
[A_K_HO
].key
= T_kh
;
1097 arrow
[A_K_HO
].fun
.cmd
= ED_MOVE_TO_BEG
;
1098 arrow
[A_K_HO
].type
= XK_CMD
;
1100 arrow
[A_K_EN
].name
= STR("end");
1101 arrow
[A_K_EN
].key
= T_at7
;
1102 arrow
[A_K_EN
].fun
.cmd
= ED_MOVE_TO_END
;
1103 arrow
[A_K_EN
].type
= XK_CMD
;
1107 /* term_reset_arrow():
1108 * Reset arrow key bindings
1111 term_reset_arrow(EditLine
*el
)
1113 fkey_t
*arrow
= el
->el_term
.t_fkey
;
1114 static const Char strA
[] = {033, '[', 'A', '\0'};
1115 static const Char strB
[] = {033, '[', 'B', '\0'};
1116 static const Char strC
[] = {033, '[', 'C', '\0'};
1117 static const Char strD
[] = {033, '[', 'D', '\0'};
1118 static const Char strH
[] = {033, '[', 'H', '\0'};
1119 static const Char strF
[] = {033, '[', 'F', '\0'};
1120 static const Char stOA
[] = {033, 'O', 'A', '\0'};
1121 static const Char stOB
[] = {033, 'O', 'B', '\0'};
1122 static const Char stOC
[] = {033, 'O', 'C', '\0'};
1123 static const Char stOD
[] = {033, 'O', 'D', '\0'};
1124 static const Char stOH
[] = {033, 'O', 'H', '\0'};
1125 static const Char stOF
[] = {033, 'O', 'F', '\0'};
1127 key_add(el
, strA
, &arrow
[A_K_UP
].fun
, arrow
[A_K_UP
].type
);
1128 key_add(el
, strB
, &arrow
[A_K_DN
].fun
, arrow
[A_K_DN
].type
);
1129 key_add(el
, strC
, &arrow
[A_K_RT
].fun
, arrow
[A_K_RT
].type
);
1130 key_add(el
, strD
, &arrow
[A_K_LT
].fun
, arrow
[A_K_LT
].type
);
1131 key_add(el
, strH
, &arrow
[A_K_HO
].fun
, arrow
[A_K_HO
].type
);
1132 key_add(el
, strF
, &arrow
[A_K_EN
].fun
, arrow
[A_K_EN
].type
);
1133 key_add(el
, stOA
, &arrow
[A_K_UP
].fun
, arrow
[A_K_UP
].type
);
1134 key_add(el
, stOB
, &arrow
[A_K_DN
].fun
, arrow
[A_K_DN
].type
);
1135 key_add(el
, stOC
, &arrow
[A_K_RT
].fun
, arrow
[A_K_RT
].type
);
1136 key_add(el
, stOD
, &arrow
[A_K_LT
].fun
, arrow
[A_K_LT
].type
);
1137 key_add(el
, stOH
, &arrow
[A_K_HO
].fun
, arrow
[A_K_HO
].type
);
1138 key_add(el
, stOF
, &arrow
[A_K_EN
].fun
, arrow
[A_K_EN
].type
);
1140 if (el
->el_map
.type
== MAP_VI
) {
1141 key_add(el
, &strA
[1], &arrow
[A_K_UP
].fun
, arrow
[A_K_UP
].type
);
1142 key_add(el
, &strB
[1], &arrow
[A_K_DN
].fun
, arrow
[A_K_DN
].type
);
1143 key_add(el
, &strC
[1], &arrow
[A_K_RT
].fun
, arrow
[A_K_RT
].type
);
1144 key_add(el
, &strD
[1], &arrow
[A_K_LT
].fun
, arrow
[A_K_LT
].type
);
1145 key_add(el
, &strH
[1], &arrow
[A_K_HO
].fun
, arrow
[A_K_HO
].type
);
1146 key_add(el
, &strF
[1], &arrow
[A_K_EN
].fun
, arrow
[A_K_EN
].type
);
1147 key_add(el
, &stOA
[1], &arrow
[A_K_UP
].fun
, arrow
[A_K_UP
].type
);
1148 key_add(el
, &stOB
[1], &arrow
[A_K_DN
].fun
, arrow
[A_K_DN
].type
);
1149 key_add(el
, &stOC
[1], &arrow
[A_K_RT
].fun
, arrow
[A_K_RT
].type
);
1150 key_add(el
, &stOD
[1], &arrow
[A_K_LT
].fun
, arrow
[A_K_LT
].type
);
1151 key_add(el
, &stOH
[1], &arrow
[A_K_HO
].fun
, arrow
[A_K_HO
].type
);
1152 key_add(el
, &stOF
[1], &arrow
[A_K_EN
].fun
, arrow
[A_K_EN
].type
);
1157 /* term_set_arrow():
1158 * Set an arrow key binding
1161 term_set_arrow(EditLine
*el
, const Char
*name
, key_value_t
*fun
, int type
)
1163 fkey_t
*arrow
= el
->el_term
.t_fkey
;
1166 for (i
= 0; i
< A_K_NKEYS
; i
++)
1167 if (Strcmp(name
, arrow
[i
].name
) == 0) {
1168 arrow
[i
].fun
= *fun
;
1169 arrow
[i
].type
= type
;
1176 /* term_clear_arrow():
1177 * Clear an arrow key binding
1180 term_clear_arrow(EditLine
*el
, const Char
*name
)
1182 fkey_t
*arrow
= el
->el_term
.t_fkey
;
1185 for (i
= 0; i
< A_K_NKEYS
; i
++)
1186 if (Strcmp(name
, arrow
[i
].name
) == 0) {
1187 arrow
[i
].type
= XK_NOD
;
1194 /* term_print_arrow():
1195 * Print the arrow key bindings
1198 term_print_arrow(EditLine
*el
, const Char
*name
)
1201 fkey_t
*arrow
= el
->el_term
.t_fkey
;
1203 for (i
= 0; i
< A_K_NKEYS
; i
++)
1204 if (*name
== '\0' || Strcmp(name
, arrow
[i
].name
) == 0)
1205 if (arrow
[i
].type
!= XK_NOD
)
1206 key_kprint(el
, arrow
[i
].name
, &arrow
[i
].fun
,
1211 /* term_bind_arrow():
1212 * Bind the arrow keys
1215 term_bind_arrow(EditLine
*el
)
1218 const el_action_t
*dmap
;
1221 fkey_t
*arrow
= el
->el_term
.t_fkey
;
1223 /* Check if the components needed are initialized */
1224 if (el
->el_term
.t_buf
== NULL
|| el
->el_map
.key
== NULL
)
1227 map
= el
->el_map
.type
== MAP_VI
? el
->el_map
.alt
: el
->el_map
.key
;
1228 dmap
= el
->el_map
.type
== MAP_VI
? el
->el_map
.vic
: el
->el_map
.emacs
;
1230 term_reset_arrow(el
);
1232 for (i
= 0; i
< A_K_NKEYS
; i
++) {
1233 Char wt_str
[VISUAL_WIDTH_MAX
];
1237 p
= el
->el_term
.t_str
[arrow
[i
].key
];
1240 for (n
= 0; n
< VISUAL_WIDTH_MAX
&& p
[n
]; ++n
)
1242 while (n
< VISUAL_WIDTH_MAX
)
1245 j
= (unsigned char) *p
;
1247 * Assign the arrow keys only if:
1249 * 1. They are multi-character arrow keys and the user
1250 * has not re-assigned the leading character, or
1251 * has re-assigned the leading character to be
1252 * ED_SEQUENCE_LEAD_IN
1253 * 2. They are single arrow keys pointing to an
1256 if (arrow
[i
].type
== XK_NOD
)
1257 key_clear(el
, map
, px
);
1259 if (p
[1] && (dmap
[j
] == map
[j
] ||
1260 map
[j
] == ED_SEQUENCE_LEAD_IN
)) {
1261 key_add(el
, px
, &arrow
[i
].fun
,
1263 map
[j
] = ED_SEQUENCE_LEAD_IN
;
1264 } else if (map
[j
] == ED_UNASSIGNED
) {
1265 key_clear(el
, map
, px
);
1266 if (arrow
[i
].type
== XK_CMD
)
1267 map
[j
] = arrow
[i
].fun
.cmd
;
1269 key_add(el
, px
, &arrow
[i
].fun
,
1282 if (term_outfile
== NULL
)
1284 return fputc(c
, term_outfile
);
1288 term_tputs(EditLine
*el
, const char *cap
, int affcnt
)
1291 pthread_mutex_lock(&term_mutex
);
1293 term_outfile
= el
->el_outfile
;
1294 (void)tputs(cap
, affcnt
, term_putc
);
1296 pthread_mutex_unlock(&term_mutex
);
1304 term__putc(EditLine
*el
, Int c
)
1306 char buf
[MB_LEN_MAX
+1];
1308 if (c
== MB_FILL_CHAR
)
1310 i
= ct_encode_char(buf
, MB_LEN_MAX
, c
);
1314 return fputs(buf
, el
->el_outfile
);
1321 term__flush(EditLine
*el
)
1324 (void) fflush(el
->el_outfile
);
1328 * Write the given character out, in a human readable form
1331 term_writec(EditLine
*el
, Int c
)
1333 Char visbuf
[VISUAL_WIDTH_MAX
+1];
1334 ssize_t vcnt
= ct_visual_char(visbuf
, VISUAL_WIDTH_MAX
, c
);
1335 visbuf
[vcnt
] = '\0';
1336 term_overwrite(el
, visbuf
, (size_t)vcnt
);
1342 * Print the current termcap characteristics
1346 term_telltc(EditLine
*el
, int argc
__attribute__((__unused__
)),
1347 const Char
**argv
__attribute__((__unused__
)))
1349 const struct termcapstr
*t
;
1352 (void) fprintf(el
->el_outfile
, "\n\tYour terminal has the\n");
1353 (void) fprintf(el
->el_outfile
, "\tfollowing characteristics:\n\n");
1354 (void) fprintf(el
->el_outfile
, "\tIt has %d columns and %d lines\n",
1355 Val(T_co
), Val(T_li
));
1356 (void) fprintf(el
->el_outfile
,
1357 "\tIt has %s meta key\n", EL_HAS_META
? "a" : "no");
1358 (void) fprintf(el
->el_outfile
,
1359 "\tIt can%suse tabs\n", EL_CAN_TAB
? " " : "not ");
1360 (void) fprintf(el
->el_outfile
, "\tIt %s automatic margins\n",
1361 EL_HAS_AUTO_MARGINS
? "has" : "does not have");
1362 if (EL_HAS_AUTO_MARGINS
)
1363 (void) fprintf(el
->el_outfile
, "\tIt %s magic margins\n",
1364 EL_HAS_MAGIC_MARGINS
? "has" : "does not have");
1366 for (t
= tstr
, ts
= el
->el_term
.t_str
; t
->name
!= NULL
; t
++, ts
++) {
1369 ub
= ct_encode_string(ct_visual_string(
1370 ct_decode_string(*ts
, &el
->el_scratch
)),
1375 (void) fprintf(el
->el_outfile
, "\t%25s (%s) == %s\n",
1376 t
->long_name
, t
->name
, ub
);
1378 (void) fputc('\n', el
->el_outfile
);
1384 * Change the current terminal characteristics
1388 term_settc(EditLine
*el
, int argc
__attribute__((__unused__
)),
1391 const struct termcapstr
*ts
;
1392 const struct termcapval
*tv
;
1393 char what
[8], how
[8];
1395 if (argv
== NULL
|| argv
[1] == NULL
|| argv
[2] == NULL
)
1398 strncpy(what
, ct_encode_string(argv
[1], &el
->el_scratch
), sizeof(what
));
1399 what
[sizeof(what
) - 1] = '\0';
1400 strncpy(how
, ct_encode_string(argv
[2], &el
->el_scratch
), sizeof(how
));
1401 how
[sizeof(how
) - 1] = '\0';
1404 * Do the strings first
1406 for (ts
= tstr
; ts
->name
!= NULL
; ts
++)
1407 if (strcmp(ts
->name
, what
) == 0)
1410 if (ts
->name
!= NULL
) {
1411 term_alloc(el
, ts
, how
);
1416 * Do the numeric ones second
1418 for (tv
= tval
; tv
->name
!= NULL
; tv
++)
1419 if (strcmp(tv
->name
, what
) == 0)
1422 if (tv
->name
!= NULL
)
1425 if (tv
== &tval
[T_pt
] || tv
== &tval
[T_km
] ||
1426 tv
== &tval
[T_am
] || tv
== &tval
[T_xn
]) {
1427 if (strcmp(how
, "yes") == 0)
1428 el
->el_term
.t_val
[tv
- tval
] = 1;
1429 else if (strcmp(how
, "no") == 0)
1430 el
->el_term
.t_val
[tv
- tval
] = 0;
1432 (void) fprintf(el
->el_errfile
,
1433 "" FSTR
": Bad value `%s'.\n", argv
[0], how
);
1437 if (term_change_size(el
, Val(T_li
), Val(T_co
)) == -1)
1444 i
= strtol(how
, &ep
, 10);
1446 (void) fprintf(el
->el_errfile
,
1447 "" FSTR
": Bad value `%s'.\n", argv
[0], how
);
1450 el
->el_term
.t_val
[tv
- tval
] = (int) i
;
1451 el
->el_term
.t_size
.v
= Val(T_co
);
1452 el
->el_term
.t_size
.h
= Val(T_li
);
1453 if (tv
== &tval
[T_co
] || tv
== &tval
[T_li
])
1454 if (term_change_size(el
, Val(T_li
), Val(T_co
))
1463 * Get the current terminal characteristics
1467 term_gettc(EditLine
*el
, int argc
__attribute__((__unused__
)), char **argv
)
1469 const struct termcapstr
*ts
;
1470 const struct termcapval
*tv
;
1474 if (argv
== NULL
|| argv
[1] == NULL
|| argv
[2] == NULL
)
1481 * Do the strings first
1483 for (ts
= tstr
; ts
->name
!= NULL
; ts
++)
1484 if (strcmp(ts
->name
, what
) == 0)
1487 if (ts
->name
!= NULL
) {
1488 *(char **)how
= el
->el_term
.t_str
[ts
- tstr
];
1492 * Do the numeric ones second
1494 for (tv
= tval
; tv
->name
!= NULL
; tv
++)
1495 if (strcmp(tv
->name
, what
) == 0)
1498 if (tv
->name
== NULL
)
1501 if (tv
== &tval
[T_pt
] || tv
== &tval
[T_km
] ||
1502 tv
== &tval
[T_am
] || tv
== &tval
[T_xn
]) {
1503 static char yes
[] = "yes";
1504 static char no
[] = "no";
1505 if (el
->el_term
.t_val
[tv
- tval
])
1506 *(char **)how
= yes
;
1511 *(int *)how
= el
->el_term
.t_val
[tv
- tval
];
1517 * Print the termcap string out with variable substitution
1521 term_echotc(EditLine
*el
, int argc
__attribute__((__unused__
)),
1526 int arg_need
, arg_cols
, arg_rows
;
1527 int verbose
= 0, silent
= 0;
1529 static const char fmts
[] = "%s\n", fmtd
[] = "%d\n";
1530 const struct termcapstr
*t
;
1531 char buf
[TC_BUFSIZE
];
1536 if (argv
== NULL
|| argv
[1] == NULL
)
1540 if (argv
[0][0] == '-') {
1541 switch (argv
[0][1]) {
1549 /* stderror(ERR_NAME | ERR_TCUSAGE); */
1554 if (!*argv
|| *argv
[0] == '\0')
1556 if (Strcmp(*argv
, STR("tabs")) == 0) {
1557 (void) fprintf(el
->el_outfile
, fmts
, EL_CAN_TAB
? "yes" : "no");
1559 } else if (Strcmp(*argv
, STR("meta")) == 0) {
1560 (void) fprintf(el
->el_outfile
, fmts
, Val(T_km
) ? "yes" : "no");
1562 } else if (Strcmp(*argv
, STR("xn")) == 0) {
1563 (void) fprintf(el
->el_outfile
, fmts
, EL_HAS_MAGIC_MARGINS
?
1566 } else if (Strcmp(*argv
, STR("am")) == 0) {
1567 (void) fprintf(el
->el_outfile
, fmts
, EL_HAS_AUTO_MARGINS
?
1570 } else if (Strcmp(*argv
, STR("baud")) == 0) {
1574 for (i
= 0; baud_rate
[i
].b_name
!= NULL
; i
++)
1575 if (el
->el_tty
.t_speed
== baud_rate
[i
].b_rate
) {
1576 (void) fprintf(el
->el_outfile
, fmts
,
1577 baud_rate
[i
].b_name
);
1580 (void) fprintf(el
->el_outfile
, fmtd
, 0);
1582 (void) fprintf(el
->el_outfile
, fmtd
, (int)el
->el_tty
.t_speed
);
1585 } else if (Strcmp(*argv
, STR("rows")) == 0 ||
1586 Strcmp(*argv
, STR("lines")) == 0) {
1587 (void) fprintf(el
->el_outfile
, fmtd
, Val(T_li
));
1589 } else if (Strcmp(*argv
, STR("cols")) == 0) {
1590 (void) fprintf(el
->el_outfile
, fmtd
, Val(T_co
));
1594 * Try to use our local definition first
1597 for (t
= tstr
; t
->name
!= NULL
; t
++)
1599 ct_encode_string(*argv
, &el
->el_scratch
)) == 0) {
1600 scap
= el
->el_term
.t_str
[t
- tstr
];
1603 if (t
->name
== NULL
) {
1604 /* XXX: some systems' tgetstr needs non const */
1605 scap
= tgetstr(ct_encode_string(*argv
, &el
->el_scratch
), &area
);
1607 if (!scap
|| scap
[0] == '\0') {
1609 (void) fprintf(el
->el_errfile
,
1610 "echotc: Termcap parameter `" FSTR
"' not found.\n",
1615 * Count home many values we need for this capability.
1617 for (cap
= scap
, arg_need
= 0; *cap
; cap
++)
1637 * hpux has lot's of them...
1640 (void) fprintf(el
->el_errfile
,
1641 "echotc: Warning: unknown termcap %% `%c'.\n",
1643 /* This is bad, but I won't complain */
1650 if (*argv
&& *argv
[0]) {
1652 (void) fprintf(el
->el_errfile
,
1653 "echotc: Warning: Extra argument `" FSTR
"'.\n",
1657 term_tputs(el
, scap
, 1);
1661 if (!*argv
|| *argv
[0] == '\0') {
1663 (void) fprintf(el
->el_errfile
,
1664 "echotc: Warning: Missing argument.\n");
1668 i
= Strtol(*argv
, &ep
, 10);
1669 if (*ep
!= '\0' || i
< 0) {
1671 (void) fprintf(el
->el_errfile
,
1672 "echotc: Bad value `" FSTR
"' for rows.\n",
1678 if (*argv
&& *argv
[0]) {
1680 (void) fprintf(el
->el_errfile
,
1681 "echotc: Warning: Extra argument `" FSTR
"'.\n",
1685 term_tputs(el
, tgoto(scap
, arg_cols
, arg_rows
), 1);
1688 /* This is wrong, but I will ignore it... */
1690 (void) fprintf(el
->el_errfile
,
1691 "echotc: Warning: Too many required arguments (%d).\n",
1696 if (!*argv
|| *argv
[0] == '\0') {
1698 (void) fprintf(el
->el_errfile
,
1699 "echotc: Warning: Missing argument.\n");
1702 i
= Strtol(*argv
, &ep
, 10);
1703 if (*ep
!= '\0' || i
< 0) {
1705 (void) fprintf(el
->el_errfile
,
1706 "echotc: Bad value `" FSTR
"' for cols.\n",
1712 if (!*argv
|| *argv
[0] == '\0') {
1714 (void) fprintf(el
->el_errfile
,
1715 "echotc: Warning: Missing argument.\n");
1718 i
= Strtol(*argv
, &ep
, 10);
1719 if (*ep
!= '\0' || i
< 0) {
1721 (void) fprintf(el
->el_errfile
,
1722 "echotc: Bad value `" FSTR
"' for rows.\n",
1729 (void) fprintf(el
->el_errfile
,
1730 "echotc: Bad value `" FSTR
"'.\n", *argv
);
1734 if (*argv
&& *argv
[0]) {
1736 (void) fprintf(el
->el_errfile
,
1737 "echotc: Warning: Extra argument `" FSTR
"'.\n",
1741 term_tputs(el
, tgoto(scap
, arg_cols
, arg_rows
), arg_rows
);