1 /* $NetBSD: term.c,v 1.54 2009/08/30 15:41:05 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.54 2009/08/30 15:41:05 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!
62 /* Solaris's term.h does horrid things. */
64 #if defined(HAVE_TERM_H) && !defined(__sun)
67 #include <sys/types.h>
68 #include <sys/ioctl.h>
77 * IMPORTANT NOTE: these routines are allowed to look at the current screen
78 * and the current possition assuming that it is correct. If this is not
79 * true, then the update will be WRONG! This is (should be) a valid
83 #define TC_BUFSIZE 2048
85 #define GoodStr(a) (el->el_term.t_str[a] != NULL && \
86 el->el_term.t_str[a][0] != '\0')
87 #define Str(a) el->el_term.t_str[a]
88 #define Val(a) el->el_term.t_val[a]
91 private const struct {
162 private const struct termcapstr
{
164 const char *long_name
;
167 { "al", "add new blank line" },
169 { "bl", "audible bell" },
171 { "cd", "clear to bottom" },
173 { "ce", "clear to end of line" },
175 { "ch", "cursor to horiz pos" },
177 { "cl", "clear screen" },
179 { "dc", "delete a character" },
181 { "dl", "delete a line" },
183 { "dm", "start delete mode" },
185 { "ed", "end delete mode" },
187 { "ei", "end insert mode" },
189 { "fs", "cursor from status line" },
191 { "ho", "home cursor" },
193 { "ic", "insert character" },
195 { "im", "start insert mode" },
197 { "ip", "insert padding" },
199 { "kd", "sends cursor down" },
201 { "kl", "sends cursor left" },
203 { "kr", "sends cursor right" },
205 { "ku", "sends cursor up" },
207 { "md", "begin bold" },
209 { "me", "end attributes" },
211 { "nd", "non destructive space" },
213 { "se", "end standout" },
215 { "so", "begin standout" },
217 { "ts", "cursor to status line" },
219 { "up", "cursor up one" },
221 { "us", "begin underline" },
223 { "ue", "end underline" },
225 { "vb", "visible bell" },
227 { "DC", "delete multiple chars" },
229 { "DO", "cursor down multiple" },
231 { "IC", "insert multiple chars" },
233 { "LE", "cursor left multiple" },
235 { "RI", "cursor right multiple" },
237 { "UP", "cursor up multiple" },
239 { "kh", "send cursor home" },
241 { "@7", "send cursor end" },
246 private const struct termcapval
{
248 const char *long_name
;
251 { "am", "has automatic margins" },
253 { "pt", "has physical tabs" },
255 { "li", "Number of lines" },
257 { "co", "Number of columns" },
259 { "km", "Has meta key" },
261 { "xt", "Tab chars destructive" },
263 { "xn", "newline ignored at right margin" },
265 { "MT", "Has meta key" }, /* XXX? */
269 /* do two or more of the attributes use me */
271 private void term_setflags(EditLine
*);
272 private int term_rebuffer_display(EditLine
*);
273 private void term_free_display(EditLine
*);
274 private int term_alloc_display(EditLine
*);
275 private void term_alloc(EditLine
*, const struct termcapstr
*, const char *);
276 private void term_init_arrow(EditLine
*);
277 private void term_reset_arrow(EditLine
*);
278 private int term_putc(int);
279 private void term_tputs(EditLine
*, const char *, int);
282 private pthread_mutex_t term_mutex
= PTHREAD_MUTEX_INITIALIZER
;
284 private FILE *term_outfile
= NULL
;
288 * Set the terminal capability flags
291 term_setflags(EditLine
*el
)
294 if (el
->el_tty
.t_tabs
)
295 EL_FLAGS
|= (Val(T_pt
) && !Val(T_xt
)) ? TERM_CAN_TAB
: 0;
297 EL_FLAGS
|= (Val(T_km
) || Val(T_MT
)) ? TERM_HAS_META
: 0;
298 EL_FLAGS
|= GoodStr(T_ce
) ? TERM_CAN_CEOL
: 0;
299 EL_FLAGS
|= (GoodStr(T_dc
) || GoodStr(T_DC
)) ? TERM_CAN_DELETE
: 0;
300 EL_FLAGS
|= (GoodStr(T_im
) || GoodStr(T_ic
) || GoodStr(T_IC
)) ?
302 EL_FLAGS
|= (GoodStr(T_up
) || GoodStr(T_UP
)) ? TERM_CAN_UP
: 0;
303 EL_FLAGS
|= Val(T_am
) ? TERM_HAS_AUTO_MARGINS
: 0;
304 EL_FLAGS
|= Val(T_xn
) ? TERM_HAS_MAGIC_MARGINS
: 0;
306 if (GoodStr(T_me
) && GoodStr(T_ue
))
307 EL_FLAGS
|= (strcmp(Str(T_me
), Str(T_ue
)) == 0) ?
310 EL_FLAGS
&= ~TERM_CAN_ME
;
311 if (GoodStr(T_me
) && GoodStr(T_se
))
312 EL_FLAGS
|= (strcmp(Str(T_me
), Str(T_se
)) == 0) ?
318 (void) fprintf(el
->el_errfile
,
319 "WARNING: Your terminal cannot move up.\n");
320 (void) fprintf(el
->el_errfile
,
321 "Editing may be odd for long lines.\n");
324 (void) fprintf(el
->el_errfile
, "no clear EOL capability.\n");
326 (void) fprintf(el
->el_errfile
, "no delete char capability.\n");
328 (void) fprintf(el
->el_errfile
, "no insert char capability.\n");
329 #endif /* DEBUG_SCREEN */
333 * Initialize the terminal stuff
336 term_init(EditLine
*el
)
339 el
->el_term
.t_buf
= (char *) el_malloc(TC_BUFSIZE
);
340 if (el
->el_term
.t_buf
== NULL
)
342 el
->el_term
.t_cap
= (char *) el_malloc(TC_BUFSIZE
);
343 if (el
->el_term
.t_cap
== NULL
)
345 el
->el_term
.t_fkey
= (fkey_t
*) el_malloc(A_K_NKEYS
* sizeof(fkey_t
));
346 if (el
->el_term
.t_fkey
== NULL
)
348 el
->el_term
.t_loc
= 0;
349 el
->el_term
.t_str
= (char **) el_malloc(T_str
* sizeof(char *));
350 if (el
->el_term
.t_str
== NULL
)
352 (void) memset(el
->el_term
.t_str
, 0, T_str
* sizeof(char *));
353 el
->el_term
.t_val
= (int *) el_malloc(T_val
* sizeof(int));
354 if (el
->el_term
.t_val
== NULL
)
356 (void) memset(el
->el_term
.t_val
, 0, T_val
* sizeof(int));
357 (void) term_set(el
, NULL
);
363 * Clean up the terminal stuff
366 term_end(EditLine
*el
)
369 el_free((ptr_t
) el
->el_term
.t_buf
);
370 el
->el_term
.t_buf
= NULL
;
371 el_free((ptr_t
) el
->el_term
.t_cap
);
372 el
->el_term
.t_cap
= NULL
;
373 el
->el_term
.t_loc
= 0;
374 el_free((ptr_t
) el
->el_term
.t_str
);
375 el
->el_term
.t_str
= NULL
;
376 el_free((ptr_t
) el
->el_term
.t_val
);
377 el
->el_term
.t_val
= NULL
;
378 el_free((ptr_t
) el
->el_term
.t_fkey
);
379 el
->el_term
.t_fkey
= NULL
;
380 term_free_display(el
);
385 * Maintain a string pool for termcap strings
388 term_alloc(EditLine
*el
, const struct termcapstr
*t
, const char *cap
)
390 char termbuf
[TC_BUFSIZE
];
392 char **tlist
= el
->el_term
.t_str
;
393 char **tmp
, **str
= &tlist
[t
- tstr
];
395 if (cap
== NULL
|| *cap
== '\0') {
401 tlen
= *str
== NULL
? 0 : strlen(*str
);
404 * New string is shorter; no need to allocate space
408 (void) strcpy(*str
, cap
); /* XXX strcpy is safe */
412 * New string is longer; see if we have enough space to append
414 if (el
->el_term
.t_loc
+ 3 < TC_BUFSIZE
) {
415 /* XXX strcpy is safe */
416 (void) strcpy(*str
= &el
->el_term
.t_buf
[el
->el_term
.t_loc
],
418 el
->el_term
.t_loc
+= (int)clen
+ 1; /* one for \0 */
422 * Compact our buffer; no need to check compaction, cause we know it
426 for (tmp
= tlist
; tmp
< &tlist
[T_str
]; tmp
++)
427 if (*tmp
!= NULL
&& *tmp
!= '\0' && *tmp
!= *str
) {
430 for (ptr
= *tmp
; *ptr
!= '\0'; termbuf
[tlen
++] = *ptr
++)
432 termbuf
[tlen
++] = '\0';
434 memcpy(el
->el_term
.t_buf
, termbuf
, TC_BUFSIZE
);
435 el
->el_term
.t_loc
= (int)tlen
;
436 if (el
->el_term
.t_loc
+ 3 >= TC_BUFSIZE
) {
437 (void) fprintf(el
->el_errfile
,
438 "Out of termcap string space.\n");
441 /* XXX strcpy is safe */
442 (void) strcpy(*str
= &el
->el_term
.t_buf
[el
->el_term
.t_loc
], cap
);
443 el
->el_term
.t_loc
+= (int)clen
+ 1; /* one for \0 */
448 /* term_rebuffer_display():
449 * Rebuffer the display after the screen changed size
452 term_rebuffer_display(EditLine
*el
)
454 coord_t
*c
= &el
->el_term
.t_size
;
456 term_free_display(el
);
461 if (term_alloc_display(el
) == -1)
467 /* term_alloc_display():
468 * Allocate a new display.
471 term_alloc_display(EditLine
*el
)
475 coord_t
*c
= &el
->el_term
.t_size
;
477 b
= (char **) el_malloc((size_t) (sizeof(char *) * (c
->v
+ 1)));
480 for (i
= 0; i
< c
->v
; i
++) {
481 b
[i
] = (char *) el_malloc((size_t) (sizeof(char) * (c
->h
+ 1)));
484 el_free((ptr_t
) b
[i
]);
492 b
= (char **) el_malloc((size_t) (sizeof(char *) * (c
->v
+ 1)));
495 for (i
= 0; i
< c
->v
; i
++) {
496 b
[i
] = (char *) el_malloc((size_t) (sizeof(char) * (c
->h
+ 1)));
499 el_free((ptr_t
) b
[i
]);
510 /* term_free_display():
511 * Free the display buffers
514 term_free_display(EditLine
*el
)
520 el
->el_display
= NULL
;
522 for (bufp
= b
; *bufp
!= NULL
; bufp
++)
523 el_free((ptr_t
) * bufp
);
527 el
->el_vdisplay
= NULL
;
529 for (bufp
= b
; *bufp
!= NULL
; bufp
++)
530 el_free((ptr_t
) * bufp
);
536 /* term_move_to_line():
537 * move to line <where> (first line == 0)
538 * as efficiently as possible
541 term_move_to_line(EditLine
*el
, int where
)
545 if (where
== el
->el_cursor
.v
)
548 if (where
> el
->el_term
.t_size
.v
) {
550 (void) fprintf(el
->el_errfile
,
551 "term_move_to_line: where is ridiculous: %d\r\n", where
);
552 #endif /* DEBUG_SCREEN */
555 if ((del
= where
- el
->el_cursor
.v
) > 0) {
557 if (EL_HAS_AUTO_MARGINS
&&
558 el
->el_display
[el
->el_cursor
.v
][0] != '\0') {
559 /* move without newline */
560 term_move_to_char(el
, el
->el_term
.t_size
.h
- 1);
561 term_overwrite(el
, &el
->el_display
562 [el
->el_cursor
.v
][el
->el_cursor
.h
],
563 (size_t)(el
->el_term
.t_size
.h
-
568 if ((del
> 1) && GoodStr(T_DO
)) {
569 term_tputs(el
, tgoto(Str(T_DO
), del
,
573 for (; del
> 0; del
--)
574 term__putc(el
, '\n');
575 /* because the \n will become \r\n */
580 } else { /* del < 0 */
581 if (GoodStr(T_UP
) && (-del
> 1 || !GoodStr(T_up
)))
582 term_tputs(el
, tgoto(Str(T_UP
), -del
, -del
), -del
);
585 for (; del
< 0; del
++)
586 term_tputs(el
, Str(T_up
), 1);
589 el
->el_cursor
.v
= where
;/* now where is here */
593 /* term_move_to_char():
594 * Move to the character position specified
597 term_move_to_char(EditLine
*el
, int where
)
602 if (where
== el
->el_cursor
.h
)
605 if (where
> el
->el_term
.t_size
.h
) {
607 (void) fprintf(el
->el_errfile
,
608 "term_move_to_char: where is riduculous: %d\r\n", where
);
609 #endif /* DEBUG_SCREEN */
612 if (!where
) { /* if where is first column */
613 term__putc(el
, '\r'); /* do a CR */
617 del
= where
- el
->el_cursor
.h
;
619 if ((del
< -4 || del
> 4) && GoodStr(T_ch
))
620 /* go there directly */
621 term_tputs(el
, tgoto(Str(T_ch
), where
, where
), where
);
623 if (del
> 0) { /* moving forward */
624 if ((del
> 4) && GoodStr(T_RI
))
625 term_tputs(el
, tgoto(Str(T_RI
), del
, del
), del
);
627 /* if I can do tabs, use them */
629 if ((el
->el_cursor
.h
& 0370) !=
631 /* if not within tab stop */
633 (el
->el_cursor
.h
& 0370);
636 term__putc(el
, '\t');
638 el
->el_cursor
.h
= where
& 0370;
642 * it's usually cheaper to just write the
646 * NOTE THAT term_overwrite() WILL CHANGE
650 &el
->el_display
[el
->el_cursor
.v
][el
->el_cursor
.h
],
651 (size_t)(where
- el
->el_cursor
.h
));
654 } else { /* del < 0 := moving backward */
655 if ((-del
> 4) && GoodStr(T_LE
))
656 term_tputs(el
, tgoto(Str(T_LE
), -del
, -del
),
658 else { /* can't go directly there */
660 * if the "cost" is greater than the "cost"
664 ((unsigned int)-del
>
665 (((unsigned int) where
>> 3) +
668 term__putc(el
, '\r'); /* do a CR */
670 goto mc_again
; /* and try again */
672 for (i
= 0; i
< -del
; i
++)
673 term__putc(el
, '\b');
677 el
->el_cursor
.h
= where
; /* now where is here */
682 * Overstrike num characters
685 term_overwrite(EditLine
*el
, const char *cp
, size_t n
)
690 if (n
> (size_t)el
->el_term
.t_size
.h
) {
692 (void) fprintf(el
->el_errfile
,
693 "term_overwrite: n is riduculous: %d\r\n", n
);
694 #endif /* DEBUG_SCREEN */
699 term__putc(el
, *cp
++);
703 if (el
->el_cursor
.h
>= el
->el_term
.t_size
.h
) { /* wrap? */
704 if (EL_HAS_AUTO_MARGINS
) { /* yes */
707 if (EL_HAS_MAGIC_MARGINS
) {
708 /* force the wrap to avoid the "magic"
711 if ((c
= el
->el_display
[el
->el_cursor
.v
][el
->el_cursor
.h
])
713 term_overwrite(el
, &c
, 1);
718 } else /* no wrap, but cursor stays on screen */
719 el
->el_cursor
.h
= el
->el_term
.t_size
.h
- 1;
724 /* term_deletechars():
725 * Delete num characters
728 term_deletechars(EditLine
*el
, int num
)
733 if (!EL_CAN_DELETE
) {
735 (void) fprintf(el
->el_errfile
, " ERROR: cannot delete \n");
736 #endif /* DEBUG_EDIT */
739 if (num
> el
->el_term
.t_size
.h
) {
741 (void) fprintf(el
->el_errfile
,
742 "term_deletechars: num is riduculous: %d\r\n", num
);
743 #endif /* DEBUG_SCREEN */
746 if (GoodStr(T_DC
)) /* if I have multiple delete */
747 if ((num
> 1) || !GoodStr(T_dc
)) { /* if dc would be more
749 term_tputs(el
, tgoto(Str(T_DC
), num
, num
), num
);
752 if (GoodStr(T_dm
)) /* if I have delete mode */
753 term_tputs(el
, Str(T_dm
), 1);
755 if (GoodStr(T_dc
)) /* else do one at a time */
757 term_tputs(el
, Str(T_dc
), 1);
759 if (GoodStr(T_ed
)) /* if I have delete mode */
760 term_tputs(el
, Str(T_ed
), 1);
764 /* term_insertwrite():
765 * Puts terminal in insert character mode or inserts num
766 * characters in the line
769 term_insertwrite(EditLine
*el
, char *cp
, int num
)
773 if (!EL_CAN_INSERT
) {
775 (void) fprintf(el
->el_errfile
, " ERROR: cannot insert \n");
776 #endif /* DEBUG_EDIT */
779 if (num
> el
->el_term
.t_size
.h
) {
781 (void) fprintf(el
->el_errfile
,
782 "StartInsert: num is riduculous: %d\r\n", num
);
783 #endif /* DEBUG_SCREEN */
786 if (GoodStr(T_IC
)) /* if I have multiple insert */
787 if ((num
> 1) || !GoodStr(T_ic
)) {
788 /* if ic would be more expensive */
789 term_tputs(el
, tgoto(Str(T_IC
), num
, num
), num
);
790 term_overwrite(el
, cp
, (size_t)num
);
791 /* this updates el_cursor.h */
794 if (GoodStr(T_im
) && GoodStr(T_ei
)) { /* if I have insert mode */
795 term_tputs(el
, Str(T_im
), 1);
797 el
->el_cursor
.h
+= num
;
799 term__putc(el
, *cp
++);
802 if (GoodStr(T_ip
)) /* have to make num chars insert */
803 term_tputs(el
, Str(T_ip
), 1);
805 term_tputs(el
, Str(T_ei
), 1);
809 if (GoodStr(T_ic
)) /* have to make num chars insert */
810 term_tputs(el
, Str(T_ic
), 1);
812 term__putc(el
, *cp
++);
816 if (GoodStr(T_ip
)) /* have to make num chars insert */
817 term_tputs(el
, Str(T_ip
), 1);
818 /* pad the inserted char */
825 * clear to end of line. There are num characters to clear
828 term_clear_EOL(EditLine
*el
, int num
)
832 if (EL_CAN_CEOL
&& GoodStr(T_ce
))
833 term_tputs(el
, Str(T_ce
), 1);
835 for (i
= 0; i
< num
; i
++)
837 el
->el_cursor
.h
+= num
; /* have written num spaces */
842 /* term_clear_screen():
846 term_clear_screen(EditLine
*el
)
847 { /* clear the whole screen and home */
850 /* send the clear screen code */
851 term_tputs(el
, Str(T_cl
), Val(T_li
));
852 else if (GoodStr(T_ho
) && GoodStr(T_cd
)) {
853 term_tputs(el
, Str(T_ho
), Val(T_li
)); /* home */
854 /* clear to bottom of screen */
855 term_tputs(el
, Str(T_cd
), Val(T_li
));
857 term__putc(el
, '\r');
858 term__putc(el
, '\n');
864 * Beep the way the terminal wants us
867 term_beep(EditLine
*el
)
870 /* what termcap says we should use */
871 term_tputs(el
, Str(T_bl
), 1);
873 term__putc(el
, '\007'); /* an ASCII bell; ^G */
878 /* term_clear_to_bottom():
879 * Clear to the bottom of the screen
882 term_clear_to_bottom(EditLine
*el
)
885 term_tputs(el
, Str(T_cd
), Val(T_li
));
886 else if (GoodStr(T_ce
))
887 term_tputs(el
, Str(T_ce
), Val(T_li
));
892 term_get(EditLine
*el
, const char **term
)
894 *term
= el
->el_term
.t_name
;
899 * Read in the terminal capabilities from the requested terminal
902 term_set(EditLine
*el
, const char *term
)
905 char buf
[TC_BUFSIZE
];
907 const struct termcapstr
*t
;
911 (void) sigemptyset(&nset
);
912 (void) sigaddset(&nset
, SIGWINCH
);
913 (void) sigprocmask(SIG_BLOCK
, &nset
, &oset
);
919 term
= getenv("TERM");
921 if (!term
|| !term
[0])
924 if (strcmp(term
, "emacs") == 0)
925 el
->el_flags
|= EDIT_DISABLED
;
927 memset(el
->el_term
.t_cap
, 0, TC_BUFSIZE
);
929 i
= tgetent(el
->el_term
.t_cap
, term
);
933 (void) fprintf(el
->el_errfile
,
934 "Cannot read termcap database;\n");
936 (void) fprintf(el
->el_errfile
,
937 "No entry for terminal type \"%s\";\n", term
);
938 (void) fprintf(el
->el_errfile
,
939 "using dumb terminal settings.\n");
940 Val(T_co
) = 80; /* do a dumb terminal */
941 Val(T_pt
) = Val(T_km
) = Val(T_li
) = 0;
942 Val(T_xt
) = Val(T_MT
);
943 for (t
= tstr
; t
->name
!= NULL
; t
++)
944 term_alloc(el
, t
, NULL
);
946 /* auto/magic margins */
947 Val(T_am
) = tgetflag("am");
948 Val(T_xn
) = tgetflag("xn");
950 Val(T_pt
) = tgetflag("pt");
951 Val(T_xt
) = tgetflag("xt");
952 /* do we have a meta? */
953 Val(T_km
) = tgetflag("km");
954 Val(T_MT
) = tgetflag("MT");
956 Val(T_co
) = tgetnum("co");
957 Val(T_li
) = tgetnum("li");
958 for (t
= tstr
; t
->name
!= NULL
; t
++) {
959 /* XXX: some systems' tgetstr needs non const */
960 term_alloc(el
, t
, tgetstr(strchr(t
->name
, *t
->name
),
966 Val(T_co
) = 80; /* just in case */
970 el
->el_term
.t_size
.v
= Val(T_co
);
971 el
->el_term
.t_size
.h
= Val(T_li
);
975 /* get the correct window size */
976 (void) term_get_size(el
, &lins
, &cols
);
977 if (term_change_size(el
, lins
, cols
) == -1)
979 (void) sigprocmask(SIG_SETMASK
, &oset
, NULL
);
981 el
->el_term
.t_name
= term
;
982 return (i
<= 0 ? -1 : 0);
987 * Return the new window size in lines and cols, and
988 * true if the size was changed.
991 term_get_size(EditLine
*el
, int *lins
, int *cols
)
1000 if (ioctl(el
->el_infd
, TIOCGWINSZ
, (ioctl_t
) & ws
) != -1) {
1011 if (ioctl(el
->el_infd
, TIOCGSIZE
, (ioctl_t
) & ts
) != -1) {
1015 *lins
= ts
.ts_lines
;
1019 return (Val(T_co
) != *cols
|| Val(T_li
) != *lins
);
1023 /* term_change_size():
1024 * Change the size of the terminal
1027 term_change_size(EditLine
*el
, int lins
, int cols
)
1032 Val(T_co
) = (cols
< 2) ? 80 : cols
;
1033 Val(T_li
) = (lins
< 1) ? 24 : lins
;
1035 /* re-make display buffers */
1036 if (term_rebuffer_display(el
) == -1)
1038 re_clear_display(el
);
1043 /* term_init_arrow():
1044 * Initialize the arrow key bindings from termcap
1047 term_init_arrow(EditLine
*el
)
1049 fkey_t
*arrow
= el
->el_term
.t_fkey
;
1051 arrow
[A_K_DN
].name
= "down";
1052 arrow
[A_K_DN
].key
= T_kd
;
1053 arrow
[A_K_DN
].fun
.cmd
= ED_NEXT_HISTORY
;
1054 arrow
[A_K_DN
].type
= XK_CMD
;
1056 arrow
[A_K_UP
].name
= "up";
1057 arrow
[A_K_UP
].key
= T_ku
;
1058 arrow
[A_K_UP
].fun
.cmd
= ED_PREV_HISTORY
;
1059 arrow
[A_K_UP
].type
= XK_CMD
;
1061 arrow
[A_K_LT
].name
= "left";
1062 arrow
[A_K_LT
].key
= T_kl
;
1063 arrow
[A_K_LT
].fun
.cmd
= ED_PREV_CHAR
;
1064 arrow
[A_K_LT
].type
= XK_CMD
;
1066 arrow
[A_K_RT
].name
= "right";
1067 arrow
[A_K_RT
].key
= T_kr
;
1068 arrow
[A_K_RT
].fun
.cmd
= ED_NEXT_CHAR
;
1069 arrow
[A_K_RT
].type
= XK_CMD
;
1071 arrow
[A_K_HO
].name
= "home";
1072 arrow
[A_K_HO
].key
= T_kh
;
1073 arrow
[A_K_HO
].fun
.cmd
= ED_MOVE_TO_BEG
;
1074 arrow
[A_K_HO
].type
= XK_CMD
;
1076 arrow
[A_K_EN
].name
= "end";
1077 arrow
[A_K_EN
].key
= T_at7
;
1078 arrow
[A_K_EN
].fun
.cmd
= ED_MOVE_TO_END
;
1079 arrow
[A_K_EN
].type
= XK_CMD
;
1083 /* term_reset_arrow():
1084 * Reset arrow key bindings
1087 term_reset_arrow(EditLine
*el
)
1089 fkey_t
*arrow
= el
->el_term
.t_fkey
;
1090 static const char strA
[] = {033, '[', 'A', '\0'};
1091 static const char strB
[] = {033, '[', 'B', '\0'};
1092 static const char strC
[] = {033, '[', 'C', '\0'};
1093 static const char strD
[] = {033, '[', 'D', '\0'};
1094 static const char strH
[] = {033, '[', 'H', '\0'};
1095 static const char strF
[] = {033, '[', 'F', '\0'};
1096 static const char stOA
[] = {033, 'O', 'A', '\0'};
1097 static const char stOB
[] = {033, 'O', 'B', '\0'};
1098 static const char stOC
[] = {033, 'O', 'C', '\0'};
1099 static const char stOD
[] = {033, 'O', 'D', '\0'};
1100 static const char stOH
[] = {033, 'O', 'H', '\0'};
1101 static const char stOF
[] = {033, 'O', 'F', '\0'};
1103 key_add(el
, strA
, &arrow
[A_K_UP
].fun
, arrow
[A_K_UP
].type
);
1104 key_add(el
, strB
, &arrow
[A_K_DN
].fun
, arrow
[A_K_DN
].type
);
1105 key_add(el
, strC
, &arrow
[A_K_RT
].fun
, arrow
[A_K_RT
].type
);
1106 key_add(el
, strD
, &arrow
[A_K_LT
].fun
, arrow
[A_K_LT
].type
);
1107 key_add(el
, strH
, &arrow
[A_K_HO
].fun
, arrow
[A_K_HO
].type
);
1108 key_add(el
, strF
, &arrow
[A_K_EN
].fun
, arrow
[A_K_EN
].type
);
1109 key_add(el
, stOA
, &arrow
[A_K_UP
].fun
, arrow
[A_K_UP
].type
);
1110 key_add(el
, stOB
, &arrow
[A_K_DN
].fun
, arrow
[A_K_DN
].type
);
1111 key_add(el
, stOC
, &arrow
[A_K_RT
].fun
, arrow
[A_K_RT
].type
);
1112 key_add(el
, stOD
, &arrow
[A_K_LT
].fun
, arrow
[A_K_LT
].type
);
1113 key_add(el
, stOH
, &arrow
[A_K_HO
].fun
, arrow
[A_K_HO
].type
);
1114 key_add(el
, stOF
, &arrow
[A_K_EN
].fun
, arrow
[A_K_EN
].type
);
1116 if (el
->el_map
.type
== MAP_VI
) {
1117 key_add(el
, &strA
[1], &arrow
[A_K_UP
].fun
, arrow
[A_K_UP
].type
);
1118 key_add(el
, &strB
[1], &arrow
[A_K_DN
].fun
, arrow
[A_K_DN
].type
);
1119 key_add(el
, &strC
[1], &arrow
[A_K_RT
].fun
, arrow
[A_K_RT
].type
);
1120 key_add(el
, &strD
[1], &arrow
[A_K_LT
].fun
, arrow
[A_K_LT
].type
);
1121 key_add(el
, &strH
[1], &arrow
[A_K_HO
].fun
, arrow
[A_K_HO
].type
);
1122 key_add(el
, &strF
[1], &arrow
[A_K_EN
].fun
, arrow
[A_K_EN
].type
);
1123 key_add(el
, &stOA
[1], &arrow
[A_K_UP
].fun
, arrow
[A_K_UP
].type
);
1124 key_add(el
, &stOB
[1], &arrow
[A_K_DN
].fun
, arrow
[A_K_DN
].type
);
1125 key_add(el
, &stOC
[1], &arrow
[A_K_RT
].fun
, arrow
[A_K_RT
].type
);
1126 key_add(el
, &stOD
[1], &arrow
[A_K_LT
].fun
, arrow
[A_K_LT
].type
);
1127 key_add(el
, &stOH
[1], &arrow
[A_K_HO
].fun
, arrow
[A_K_HO
].type
);
1128 key_add(el
, &stOF
[1], &arrow
[A_K_EN
].fun
, arrow
[A_K_EN
].type
);
1133 /* term_set_arrow():
1134 * Set an arrow key binding
1137 term_set_arrow(EditLine
*el
, const char *name
, key_value_t
*fun
, int type
)
1139 fkey_t
*arrow
= el
->el_term
.t_fkey
;
1142 for (i
= 0; i
< A_K_NKEYS
; i
++)
1143 if (strcmp(name
, arrow
[i
].name
) == 0) {
1144 arrow
[i
].fun
= *fun
;
1145 arrow
[i
].type
= type
;
1152 /* term_clear_arrow():
1153 * Clear an arrow key binding
1156 term_clear_arrow(EditLine
*el
, const char *name
)
1158 fkey_t
*arrow
= el
->el_term
.t_fkey
;
1161 for (i
= 0; i
< A_K_NKEYS
; i
++)
1162 if (strcmp(name
, arrow
[i
].name
) == 0) {
1163 arrow
[i
].type
= XK_NOD
;
1170 /* term_print_arrow():
1171 * Print the arrow key bindings
1174 term_print_arrow(EditLine
*el
, const char *name
)
1177 fkey_t
*arrow
= el
->el_term
.t_fkey
;
1179 for (i
= 0; i
< A_K_NKEYS
; i
++)
1180 if (*name
== '\0' || strcmp(name
, arrow
[i
].name
) == 0)
1181 if (arrow
[i
].type
!= XK_NOD
)
1182 key_kprint(el
, arrow
[i
].name
, &arrow
[i
].fun
,
1187 /* term_bind_arrow():
1188 * Bind the arrow keys
1191 term_bind_arrow(EditLine
*el
)
1194 const el_action_t
*dmap
;
1197 fkey_t
*arrow
= el
->el_term
.t_fkey
;
1199 /* Check if the components needed are initialized */
1200 if (el
->el_term
.t_buf
== NULL
|| el
->el_map
.key
== NULL
)
1203 map
= el
->el_map
.type
== MAP_VI
? el
->el_map
.alt
: el
->el_map
.key
;
1204 dmap
= el
->el_map
.type
== MAP_VI
? el
->el_map
.vic
: el
->el_map
.emacs
;
1206 term_reset_arrow(el
);
1208 for (i
= 0; i
< A_K_NKEYS
; i
++) {
1209 p
= el
->el_term
.t_str
[arrow
[i
].key
];
1211 j
= (unsigned char) *p
;
1213 * Assign the arrow keys only if:
1215 * 1. They are multi-character arrow keys and the user
1216 * has not re-assigned the leading character, or
1217 * has re-assigned the leading character to be
1218 * ED_SEQUENCE_LEAD_IN
1219 * 2. They are single arrow keys pointing to an
1222 if (arrow
[i
].type
== XK_NOD
)
1223 key_clear(el
, map
, p
);
1225 if (p
[1] && (dmap
[j
] == map
[j
] ||
1226 map
[j
] == ED_SEQUENCE_LEAD_IN
)) {
1227 key_add(el
, p
, &arrow
[i
].fun
,
1229 map
[j
] = ED_SEQUENCE_LEAD_IN
;
1230 } else if (map
[j
] == ED_UNASSIGNED
) {
1231 key_clear(el
, map
, p
);
1232 if (arrow
[i
].type
== XK_CMD
)
1233 map
[j
] = arrow
[i
].fun
.cmd
;
1235 key_add(el
, p
, &arrow
[i
].fun
,
1250 if (term_outfile
== NULL
)
1252 return fputc(c
, term_outfile
);
1256 term_tputs(EditLine
*el
, const char *cap
, int affcnt
)
1259 pthread_mutex_lock(&term_mutex
);
1261 term_outfile
= el
->el_outfile
;
1262 (void)tputs(cap
, affcnt
, term_putc
);
1264 pthread_mutex_unlock(&term_mutex
);
1272 term__putc(EditLine
*el
, int c
)
1275 return fputc(c
, el
->el_outfile
);
1282 term__flush(EditLine
*el
)
1285 (void) fflush(el
->el_outfile
);
1289 * Write the given character out, in a human readable form
1292 term_writec(EditLine
*el
, int c
)
1295 size_t cnt
= key__decode_char(buf
, sizeof(buf
), 0, c
);
1297 term_overwrite(el
, buf
, (size_t)cnt
);
1303 * Print the current termcap characteristics
1307 term_telltc(EditLine
*el
, int argc
__attribute__((__unused__
)),
1308 const char **argv
__attribute__((__unused__
)))
1310 const struct termcapstr
*t
;
1312 char upbuf
[EL_BUFSIZ
];
1314 (void) fprintf(el
->el_outfile
, "\n\tYour terminal has the\n");
1315 (void) fprintf(el
->el_outfile
, "\tfollowing characteristics:\n\n");
1316 (void) fprintf(el
->el_outfile
, "\tIt has %d columns and %d lines\n",
1317 Val(T_co
), Val(T_li
));
1318 (void) fprintf(el
->el_outfile
,
1319 "\tIt has %s meta key\n", EL_HAS_META
? "a" : "no");
1320 (void) fprintf(el
->el_outfile
,
1321 "\tIt can%suse tabs\n", EL_CAN_TAB
? " " : "not ");
1322 (void) fprintf(el
->el_outfile
, "\tIt %s automatic margins\n",
1323 EL_HAS_AUTO_MARGINS
? "has" : "does not have");
1324 if (EL_HAS_AUTO_MARGINS
)
1325 (void) fprintf(el
->el_outfile
, "\tIt %s magic margins\n",
1326 EL_HAS_MAGIC_MARGINS
? "has" : "does not have");
1328 for (t
= tstr
, ts
= el
->el_term
.t_str
; t
->name
!= NULL
; t
++, ts
++) {
1331 (void) key__decode_str(*ts
, upbuf
, sizeof(upbuf
), "");
1336 (void) fprintf(el
->el_outfile
, "\t%25s (%s) == %s\n",
1337 t
->long_name
, t
->name
, ub
);
1339 (void) fputc('\n', el
->el_outfile
);
1345 * Change the current terminal characteristics
1349 term_settc(EditLine
*el
, int argc
__attribute__((__unused__
)),
1352 const struct termcapstr
*ts
;
1353 const struct termcapval
*tv
;
1354 const char *what
, *how
;
1356 if (argv
== NULL
|| argv
[1] == NULL
|| argv
[2] == NULL
)
1363 * Do the strings first
1365 for (ts
= tstr
; ts
->name
!= NULL
; ts
++)
1366 if (strcmp(ts
->name
, what
) == 0)
1369 if (ts
->name
!= NULL
) {
1370 term_alloc(el
, ts
, how
);
1375 * Do the numeric ones second
1377 for (tv
= tval
; tv
->name
!= NULL
; tv
++)
1378 if (strcmp(tv
->name
, what
) == 0)
1381 if (tv
->name
!= NULL
)
1384 if (tv
== &tval
[T_pt
] || tv
== &tval
[T_km
] ||
1385 tv
== &tval
[T_am
] || tv
== &tval
[T_xn
]) {
1386 if (strcmp(how
, "yes") == 0)
1387 el
->el_term
.t_val
[tv
- tval
] = 1;
1388 else if (strcmp(how
, "no") == 0)
1389 el
->el_term
.t_val
[tv
- tval
] = 0;
1391 (void) fprintf(el
->el_errfile
,
1392 "%s: Bad value `%s'.\n", argv
[0], how
);
1396 if (term_change_size(el
, Val(T_li
), Val(T_co
)) == -1)
1403 i
= strtol(how
, &ep
, 10);
1405 (void) fprintf(el
->el_errfile
,
1406 "%s: Bad value `%s'.\n", argv
[0], how
);
1409 el
->el_term
.t_val
[tv
- tval
] = (int) i
;
1410 el
->el_term
.t_size
.v
= Val(T_co
);
1411 el
->el_term
.t_size
.h
= Val(T_li
);
1412 if (tv
== &tval
[T_co
] || tv
== &tval
[T_li
])
1413 if (term_change_size(el
, Val(T_li
), Val(T_co
))
1422 * Get the current terminal characteristics
1426 term_gettc(EditLine
*el
, int argc
__attribute__((__unused__
)), char **argv
)
1428 const struct termcapstr
*ts
;
1429 const struct termcapval
*tv
;
1433 if (argv
== NULL
|| argv
[1] == NULL
|| argv
[2] == NULL
)
1440 * Do the strings first
1442 for (ts
= tstr
; ts
->name
!= NULL
; ts
++)
1443 if (strcmp(ts
->name
, what
) == 0)
1446 if (ts
->name
!= NULL
) {
1447 *(char **)how
= el
->el_term
.t_str
[ts
- tstr
];
1451 * Do the numeric ones second
1453 for (tv
= tval
; tv
->name
!= NULL
; tv
++)
1454 if (strcmp(tv
->name
, what
) == 0)
1457 if (tv
->name
== NULL
)
1460 if (tv
== &tval
[T_pt
] || tv
== &tval
[T_km
] ||
1461 tv
== &tval
[T_am
] || tv
== &tval
[T_xn
]) {
1462 static char yes
[] = "yes";
1463 static char no
[] = "no";
1464 if (el
->el_term
.t_val
[tv
- tval
])
1465 *(char **)how
= yes
;
1470 *(int *)how
= el
->el_term
.t_val
[tv
- tval
];
1476 * Print the termcap string out with variable substitution
1480 term_echotc(EditLine
*el
, int argc
__attribute__((__unused__
)),
1483 char *cap
, *scap
, *ep
;
1484 int arg_need
, arg_cols
, arg_rows
;
1485 int verbose
= 0, silent
= 0;
1487 static const char fmts
[] = "%s\n", fmtd
[] = "%d\n";
1488 const struct termcapstr
*t
;
1489 char buf
[TC_BUFSIZE
];
1494 if (argv
== NULL
|| argv
[1] == NULL
)
1498 if (argv
[0][0] == '-') {
1499 switch (argv
[0][1]) {
1507 /* stderror(ERR_NAME | ERR_TCUSAGE); */
1512 if (!*argv
|| *argv
[0] == '\0')
1514 if (strcmp(*argv
, "tabs") == 0) {
1515 (void) fprintf(el
->el_outfile
, fmts
, EL_CAN_TAB
? "yes" : "no");
1517 } else if (strcmp(*argv
, "meta") == 0) {
1518 (void) fprintf(el
->el_outfile
, fmts
, Val(T_km
) ? "yes" : "no");
1520 } else if (strcmp(*argv
, "xn") == 0) {
1521 (void) fprintf(el
->el_outfile
, fmts
, EL_HAS_MAGIC_MARGINS
?
1524 } else if (strcmp(*argv
, "am") == 0) {
1525 (void) fprintf(el
->el_outfile
, fmts
, EL_HAS_AUTO_MARGINS
?
1528 } else if (strcmp(*argv
, "baud") == 0) {
1532 for (i
= 0; baud_rate
[i
].b_name
!= NULL
; i
++)
1533 if (el
->el_tty
.t_speed
== baud_rate
[i
].b_rate
) {
1534 (void) fprintf(el
->el_outfile
, fmts
,
1535 baud_rate
[i
].b_name
);
1538 (void) fprintf(el
->el_outfile
, fmtd
, 0);
1540 (void) fprintf(el
->el_outfile
, fmtd
, (int)el
->el_tty
.t_speed
);
1543 } else if (strcmp(*argv
, "rows") == 0 || strcmp(*argv
, "lines") == 0) {
1544 (void) fprintf(el
->el_outfile
, fmtd
, Val(T_li
));
1546 } else if (strcmp(*argv
, "cols") == 0) {
1547 (void) fprintf(el
->el_outfile
, fmtd
, Val(T_co
));
1551 * Try to use our local definition first
1554 for (t
= tstr
; t
->name
!= NULL
; t
++)
1555 if (strcmp(t
->name
, *argv
) == 0) {
1556 scap
= el
->el_term
.t_str
[t
- tstr
];
1559 if (t
->name
== NULL
) {
1560 /* XXX: some systems' tgetstr needs non const */
1561 scap
= tgetstr(strchr(*argv
, **argv
), &area
);
1563 if (!scap
|| scap
[0] == '\0') {
1565 (void) fprintf(el
->el_errfile
,
1566 "echotc: Termcap parameter `%s' not found.\n",
1571 * Count home many values we need for this capability.
1573 for (cap
= scap
, arg_need
= 0; *cap
; cap
++)
1593 * hpux has lot's of them...
1596 (void) fprintf(el
->el_errfile
,
1597 "echotc: Warning: unknown termcap %% `%c'.\n",
1599 /* This is bad, but I won't complain */
1606 if (*argv
&& *argv
[0]) {
1608 (void) fprintf(el
->el_errfile
,
1609 "echotc: Warning: Extra argument `%s'.\n",
1613 term_tputs(el
, scap
, 1);
1617 if (!*argv
|| *argv
[0] == '\0') {
1619 (void) fprintf(el
->el_errfile
,
1620 "echotc: Warning: Missing argument.\n");
1624 i
= strtol(*argv
, &ep
, 10);
1625 if (*ep
!= '\0' || i
< 0) {
1627 (void) fprintf(el
->el_errfile
,
1628 "echotc: Bad value `%s' for rows.\n",
1634 if (*argv
&& *argv
[0]) {
1636 (void) fprintf(el
->el_errfile
,
1637 "echotc: Warning: Extra argument `%s'.\n",
1641 term_tputs(el
, tgoto(scap
, arg_cols
, arg_rows
), 1);
1644 /* This is wrong, but I will ignore it... */
1646 (void) fprintf(el
->el_errfile
,
1647 "echotc: Warning: Too many required arguments (%d).\n",
1652 if (!*argv
|| *argv
[0] == '\0') {
1654 (void) fprintf(el
->el_errfile
,
1655 "echotc: Warning: Missing argument.\n");
1658 i
= strtol(*argv
, &ep
, 10);
1659 if (*ep
!= '\0' || i
< 0) {
1661 (void) fprintf(el
->el_errfile
,
1662 "echotc: Bad value `%s' for cols.\n",
1668 if (!*argv
|| *argv
[0] == '\0') {
1670 (void) fprintf(el
->el_errfile
,
1671 "echotc: Warning: Missing argument.\n");
1674 i
= strtol(*argv
, &ep
, 10);
1675 if (*ep
!= '\0' || i
< 0) {
1677 (void) fprintf(el
->el_errfile
,
1678 "echotc: Bad value `%s' for rows.\n",
1685 (void) fprintf(el
->el_errfile
,
1686 "echotc: Bad value `%s'.\n", *argv
);
1690 if (*argv
&& *argv
[0]) {
1692 (void) fprintf(el
->el_errfile
,
1693 "echotc: Warning: Extra argument `%s'.\n",
1697 term_tputs(el
, tgoto(scap
, arg_cols
, arg_rows
), arg_rows
);