1 /* $NetBSD: term.c,v 1.35 2002/03/18 16:00:59 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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #if !defined(lint) && !defined(SCCSID)
42 static char sccsid
[] = "@(#)term.c 8.2 (Berkeley) 4/30/95";
44 __RCSID("$NetBSD: term.c,v 1.35 2002/03/18 16:00:59 christos Exp $");
46 #endif /* not lint && not SCCSID */
49 * term.c: Editor/termcap-curses interface
50 * We have to declare a static variable here, since the
51 * termcap putchar routine does not take an argument!
67 #if defined(HAVE_TERM_H)
69 /* Can not use /usr/include/term.h because of a lot of incompatibilities, so just define some prototypes */
70 extern int tgetent(char *, const char *);
71 extern int tgetflag(const char *);
72 extern int tgetnum(const char *);
73 extern char *tgetstr(const char *, char **);
74 extern int tputs (const char *, int, int (*)(int));
75 extern char *tgoto (const char *, int, int);
76 #endif /* defined(HAVE_TERM_H) */
77 #include <sys/types.h>
78 #include <sys/ioctl.h>
83 * IMPORTANT NOTE: these routines are allowed to look at the current screen
84 * and the current possition assuming that it is correct. If this is not
85 * true, then the update will be WRONG! This is (should be) a valid
89 #define TC_BUFSIZE 2048
91 #define GoodStr(a) (el->el_term.t_str[a] != NULL && \
92 el->el_term.t_str[a][0] != '\0')
93 #define Str(a) el->el_term.t_str[a]
94 #define Val(a) el->el_term.t_val[a]
97 private const struct {
168 private const struct termcapstr
{
170 const char *long_name
;
173 { "al", "add new blank line" },
175 { "bl", "audible bell" },
177 { "cd", "clear to bottom" },
179 { "ce", "clear to end of line" },
181 { "ch", "cursor to horiz pos" },
183 { "cl", "clear screen" },
185 { "dc", "delete a character" },
187 { "dl", "delete a line" },
189 { "dm", "start delete mode" },
191 { "ed", "end delete mode" },
193 { "ei", "end insert mode" },
195 { "fs", "cursor from status line" },
197 { "ho", "home cursor" },
199 { "ic", "insert character" },
201 { "im", "start insert mode" },
203 { "ip", "insert padding" },
205 { "kd", "sends cursor down" },
207 { "kl", "sends cursor left" },
209 { "kr", "sends cursor right" },
211 { "ku", "sends cursor up" },
213 { "md", "begin bold" },
215 { "me", "end attributes" },
217 { "nd", "non destructive space" },
219 { "se", "end standout" },
221 { "so", "begin standout" },
223 { "ts", "cursor to status line" },
225 { "up", "cursor up one" },
227 { "us", "begin underline" },
229 { "ue", "end underline" },
231 { "vb", "visible bell" },
233 { "DC", "delete multiple chars" },
235 { "DO", "cursor down multiple" },
237 { "IC", "insert multiple chars" },
239 { "LE", "cursor left multiple" },
241 { "RI", "cursor right multiple" },
243 { "UP", "cursor up multiple" },
245 { "kh", "send cursor home" },
247 { "@7", "send cursor end" },
252 private const struct termcapval
{
254 const char *long_name
;
257 { "am", "has automatic margins" },
259 { "pt", "has physical tabs" },
261 { "li", "Number of lines" },
263 { "co", "Number of columns" },
265 { "km", "Has meta key" },
267 { "xt", "Tab chars destructive" },
269 { "xn", "newline ignored at right margin" },
271 { "MT", "Has meta key" }, /* XXX? */
275 /* do two or more of the attributes use me */
277 private void term_setflags(EditLine
*);
278 private int term_rebuffer_display(EditLine
*);
279 private void term_free_display(EditLine
*);
280 private int term_alloc_display(EditLine
*);
281 private void term_alloc(EditLine
*, const struct termcapstr
*, const char *);
282 private void term_init_arrow(EditLine
*);
283 private void term_reset_arrow(EditLine
*);
286 private FILE *term_outfile
= NULL
; /* XXX: How do we fix that? */
290 * Set the terminal capability flags
293 term_setflags(EditLine
*el
)
296 if (el
->el_tty
.t_tabs
)
297 EL_FLAGS
|= (Val(T_pt
) && !Val(T_xt
)) ? TERM_CAN_TAB
: 0;
299 EL_FLAGS
|= (Val(T_km
) || Val(T_MT
)) ? TERM_HAS_META
: 0;
300 EL_FLAGS
|= GoodStr(T_ce
) ? TERM_CAN_CEOL
: 0;
301 EL_FLAGS
|= (GoodStr(T_dc
) || GoodStr(T_DC
)) ? TERM_CAN_DELETE
: 0;
302 EL_FLAGS
|= (GoodStr(T_im
) || GoodStr(T_ic
) || GoodStr(T_IC
)) ?
304 EL_FLAGS
|= (GoodStr(T_up
) || GoodStr(T_UP
)) ? TERM_CAN_UP
: 0;
305 EL_FLAGS
|= Val(T_am
) ? TERM_HAS_AUTO_MARGINS
: 0;
306 EL_FLAGS
|= Val(T_xn
) ? TERM_HAS_MAGIC_MARGINS
: 0;
308 if (GoodStr(T_me
) && GoodStr(T_ue
))
309 EL_FLAGS
|= (strcmp(Str(T_me
), Str(T_ue
)) == 0) ?
312 EL_FLAGS
&= ~TERM_CAN_ME
;
313 if (GoodStr(T_me
) && GoodStr(T_se
))
314 EL_FLAGS
|= (strcmp(Str(T_me
), Str(T_se
)) == 0) ?
320 (void) fprintf(el
->el_errfile
,
321 "WARNING: Your terminal cannot move up.\n");
322 (void) fprintf(el
->el_errfile
,
323 "Editing may be odd for long lines.\n");
326 (void) fprintf(el
->el_errfile
, "no clear EOL capability.\n");
328 (void) fprintf(el
->el_errfile
, "no delete char capability.\n");
330 (void) fprintf(el
->el_errfile
, "no insert char capability.\n");
331 #endif /* DEBUG_SCREEN */
336 * Initialize the terminal stuff
339 term_init(EditLine
*el
)
342 el
->el_term
.t_buf
= (char *) el_malloc(TC_BUFSIZE
);
343 if (el
->el_term
.t_buf
== NULL
)
345 el
->el_term
.t_cap
= (char *) el_malloc(TC_BUFSIZE
);
346 if (el
->el_term
.t_cap
== NULL
)
348 el
->el_term
.t_fkey
= (fkey_t
*) el_malloc(A_K_NKEYS
* sizeof(fkey_t
));
349 if (el
->el_term
.t_fkey
== NULL
)
351 el
->el_term
.t_loc
= 0;
352 el
->el_term
.t_str
= (char **) el_malloc(T_str
* sizeof(char *));
353 if (el
->el_term
.t_str
== NULL
)
355 (void) memset(el
->el_term
.t_str
, 0, T_str
* sizeof(char *));
356 el
->el_term
.t_val
= (int *) el_malloc(T_val
* sizeof(int));
357 if (el
->el_term
.t_val
== NULL
)
359 (void) memset(el
->el_term
.t_val
, 0, T_val
* sizeof(int));
360 term_outfile
= el
->el_outfile
;
361 (void) term_set(el
, NULL
);
366 * Clean up the terminal stuff
369 term_end(EditLine
*el
)
372 el_free((ptr_t
) el
->el_term
.t_buf
);
373 el
->el_term
.t_buf
= NULL
;
374 el_free((ptr_t
) el
->el_term
.t_cap
);
375 el
->el_term
.t_cap
= NULL
;
376 el_free((ptr_t
) el
->el_term
.t_fkey
);
377 el
->el_term
.t_fkey
= NULL
;
378 el
->el_term
.t_loc
= 0;
379 el_free((ptr_t
) el
->el_term
.t_str
);
380 el
->el_term
.t_str
= NULL
;
381 el_free((ptr_t
) el
->el_term
.t_val
);
382 el
->el_term
.t_val
= NULL
;
383 term_free_display(el
);
388 * Maintain a string pool for termcap strings
391 term_alloc(EditLine
*el
, const struct termcapstr
*t
, const char *cap
)
393 char termbuf
[TC_BUFSIZE
];
395 char **tlist
= el
->el_term
.t_str
;
396 char **tmp
, **str
= &tlist
[t
- tstr
];
398 if (cap
== NULL
|| *cap
== '\0') {
404 tlen
= *str
== NULL
? 0 : strlen(*str
);
407 * New string is shorter; no need to allocate space
410 (void) strcpy(*str
, cap
); /* XXX strcpy is safe */
414 * New string is longer; see if we have enough space to append
416 if (el
->el_term
.t_loc
+ 3 < TC_BUFSIZE
) {
417 /* XXX strcpy is safe */
418 (void) strcpy(*str
= &el
->el_term
.t_buf
[el
->el_term
.t_loc
],
420 el
->el_term
.t_loc
+= clen
+ 1; /* one for \0 */
424 * Compact our buffer; no need to check compaction, cause we know it
428 for (tmp
= tlist
; tmp
< &tlist
[T_str
]; tmp
++)
429 if (*tmp
!= NULL
&& *tmp
!= '\0' && *tmp
!= *str
) {
432 for (ptr
= *tmp
; *ptr
!= '\0'; termbuf
[tlen
++] = *ptr
++)
434 termbuf
[tlen
++] = '\0';
436 memcpy(el
->el_term
.t_buf
, termbuf
, TC_BUFSIZE
);
437 el
->el_term
.t_loc
= tlen
;
438 if (el
->el_term
.t_loc
+ 3 >= TC_BUFSIZE
) {
439 (void) fprintf(el
->el_errfile
,
440 "Out of termcap string space.\n");
443 /* XXX strcpy is safe */
444 (void) strcpy(*str
= &el
->el_term
.t_buf
[el
->el_term
.t_loc
], cap
);
445 el
->el_term
.t_loc
+= clen
+ 1; /* one for \0 */
450 /* term_rebuffer_display():
451 * Rebuffer the display after the screen changed size
454 term_rebuffer_display(EditLine
*el
)
456 coord_t
*c
= &el
->el_term
.t_size
;
458 term_free_display(el
);
463 if (term_alloc_display(el
) == -1)
469 /* term_alloc_display():
470 * Allocate a new display.
473 term_alloc_display(EditLine
*el
)
477 coord_t
*c
= &el
->el_term
.t_size
;
479 b
= (char **) el_malloc((size_t) (sizeof(char *) * (c
->v
+ 1)));
482 for (i
= 0; i
< c
->v
; i
++) {
483 b
[i
] = (char *) el_malloc((size_t) (sizeof(char) * (c
->h
+ 1)));
490 b
= (char **) el_malloc((size_t) (sizeof(char *) * (c
->v
+ 1)));
493 for (i
= 0; i
< c
->v
; i
++) {
494 b
[i
] = (char *) el_malloc((size_t) (sizeof(char) * (c
->h
+ 1)));
504 /* term_free_display():
505 * Free the display buffers
508 term_free_display(EditLine
*el
)
514 el
->el_display
= NULL
;
516 for (bufp
= b
; *bufp
!= NULL
; bufp
++)
517 el_free((ptr_t
) * bufp
);
521 el
->el_vdisplay
= NULL
;
523 for (bufp
= b
; *bufp
!= NULL
; bufp
++)
524 el_free((ptr_t
) * bufp
);
530 /* term_move_to_line():
531 * move to line <where> (first line == 0)
532 * as efficiently as possible
535 term_move_to_line(EditLine
*el
, int where
)
539 if (where
== el
->el_cursor
.v
)
542 if (where
> el
->el_term
.t_size
.v
) {
544 (void) fprintf(el
->el_errfile
,
545 "term_move_to_line: where is ridiculous: %d\r\n", where
);
546 #endif /* DEBUG_SCREEN */
549 if ((del
= where
- el
->el_cursor
.v
) > 0) {
551 if (EL_HAS_AUTO_MARGINS
&&
552 el
->el_display
[el
->el_cursor
.v
][0] != '\0') {
553 /* move without newline */
554 term_move_to_char(el
, el
->el_term
.t_size
.h
- 1);
556 &el
->el_display
[el
->el_cursor
.v
][el
->el_cursor
.h
],
561 if ((del
> 1) && GoodStr(T_DO
)) {
562 (void) tputs(tgoto(Str(T_DO
), del
, del
),
566 for (; del
> 0; del
--)
568 /* because the \n will become \r\n */
573 } else { /* del < 0 */
574 if (GoodStr(T_UP
) && (-del
> 1 || !GoodStr(T_up
)))
575 (void) tputs(tgoto(Str(T_UP
), -del
, -del
), -del
,
579 for (; del
< 0; del
++)
580 (void) tputs(Str(T_up
), 1, term__putc
);
583 el
->el_cursor
.v
= where
;/* now where is here */
587 /* term_move_to_char():
588 * Move to the character position specified
591 term_move_to_char(EditLine
*el
, int where
)
596 if (where
== el
->el_cursor
.h
)
599 if (where
> el
->el_term
.t_size
.h
) {
601 (void) fprintf(el
->el_errfile
,
602 "term_move_to_char: where is riduculous: %d\r\n", where
);
603 #endif /* DEBUG_SCREEN */
606 if (!where
) { /* if where is first column */
607 term__putc('\r'); /* do a CR */
611 del
= where
- el
->el_cursor
.h
;
613 if ((del
< -4 || del
> 4) && GoodStr(T_ch
))
614 /* go there directly */
615 (void) tputs(tgoto(Str(T_ch
), where
, where
), where
, term__putc
);
617 if (del
> 0) { /* moving forward */
618 if ((del
> 4) && GoodStr(T_RI
))
619 (void) tputs(tgoto(Str(T_RI
), del
, del
),
622 /* if I can do tabs, use them */
624 if ((el
->el_cursor
.h
& 0370) !=
626 /* if not within tab stop */
628 (el
->el_cursor
.h
& 0370);
633 el
->el_cursor
.h
= where
& 0370;
637 * it's usually cheaper to just write the
641 * NOTE THAT term_overwrite() WILL CHANGE
645 &el
->el_display
[el
->el_cursor
.v
][el
->el_cursor
.h
],
646 where
- el
->el_cursor
.h
);
649 } else { /* del < 0 := moving backward */
650 if ((-del
> 4) && GoodStr(T_LE
))
651 (void) tputs(tgoto(Str(T_LE
), -del
, -del
),
653 else { /* can't go directly there */
655 * if the "cost" is greater than the "cost"
659 (-del
> (((unsigned int) where
>> 3) +
662 term__putc('\r'); /* do a CR */
664 goto mc_again
; /* and try again */
666 for (i
= 0; i
< -del
; i
++)
671 el
->el_cursor
.h
= where
; /* now where is here */
676 * Overstrike num characters
679 term_overwrite(EditLine
*el
, const char *cp
, int n
)
682 return; /* catch bugs */
684 if (n
> el
->el_term
.t_size
.h
) {
686 (void) fprintf(el
->el_errfile
,
687 "term_overwrite: n is riduculous: %d\r\n", n
);
688 #endif /* DEBUG_SCREEN */
696 if (el
->el_cursor
.h
>= el
->el_term
.t_size
.h
) { /* wrap? */
697 if (EL_HAS_AUTO_MARGINS
) { /* yes */
700 if (EL_HAS_MAGIC_MARGINS
) {
701 /* force the wrap to avoid the "magic"
704 if ((c
= el
->el_display
[el
->el_cursor
.v
][el
->el_cursor
.h
])
706 term_overwrite(el
, &c
, 1);
711 } else /* no wrap, but cursor stays on screen */
712 el
->el_cursor
.h
= el
->el_term
.t_size
.h
;
717 /* term_deletechars():
718 * Delete num characters
721 term_deletechars(EditLine
*el
, int num
)
726 if (!EL_CAN_DELETE
) {
728 (void) fprintf(el
->el_errfile
, " ERROR: cannot delete \n");
729 #endif /* DEBUG_EDIT */
732 if (num
> el
->el_term
.t_size
.h
) {
734 (void) fprintf(el
->el_errfile
,
735 "term_deletechars: num is riduculous: %d\r\n", num
);
736 #endif /* DEBUG_SCREEN */
739 if (GoodStr(T_DC
)) /* if I have multiple delete */
740 if ((num
> 1) || !GoodStr(T_dc
)) { /* if dc would be more
742 (void) tputs(tgoto(Str(T_DC
), num
, num
),
746 if (GoodStr(T_dm
)) /* if I have delete mode */
747 (void) tputs(Str(T_dm
), 1, term__putc
);
749 if (GoodStr(T_dc
)) /* else do one at a time */
751 (void) tputs(Str(T_dc
), 1, term__putc
);
753 if (GoodStr(T_ed
)) /* if I have delete mode */
754 (void) tputs(Str(T_ed
), 1, term__putc
);
758 /* term_insertwrite():
759 * Puts terminal in insert character mode or inserts num
760 * characters in the line
763 term_insertwrite(EditLine
*el
, char *cp
, int num
)
767 if (!EL_CAN_INSERT
) {
769 (void) fprintf(el
->el_errfile
, " ERROR: cannot insert \n");
770 #endif /* DEBUG_EDIT */
773 if (num
> el
->el_term
.t_size
.h
) {
775 (void) fprintf(el
->el_errfile
,
776 "StartInsert: num is riduculous: %d\r\n", num
);
777 #endif /* DEBUG_SCREEN */
780 if (GoodStr(T_IC
)) /* if I have multiple insert */
781 if ((num
> 1) || !GoodStr(T_ic
)) {
782 /* if ic would be more expensive */
783 (void) tputs(tgoto(Str(T_IC
), num
, num
),
785 term_overwrite(el
, cp
, num
);
786 /* this updates el_cursor.h */
789 if (GoodStr(T_im
) && GoodStr(T_ei
)) { /* if I have insert mode */
790 (void) tputs(Str(T_im
), 1, term__putc
);
792 el
->el_cursor
.h
+= num
;
797 if (GoodStr(T_ip
)) /* have to make num chars insert */
798 (void) tputs(Str(T_ip
), 1, term__putc
);
800 (void) tputs(Str(T_ei
), 1, term__putc
);
804 if (GoodStr(T_ic
)) /* have to make num chars insert */
805 (void) tputs(Str(T_ic
), 1, term__putc
);
812 if (GoodStr(T_ip
)) /* have to make num chars insert */
813 (void) tputs(Str(T_ip
), 1, term__putc
);
814 /* pad the inserted char */
821 * clear to end of line. There are num characters to clear
824 term_clear_EOL(EditLine
*el
, int num
)
828 if (EL_CAN_CEOL
&& GoodStr(T_ce
))
829 (void) tputs(Str(T_ce
), 1, term__putc
);
831 for (i
= 0; i
< num
; i
++)
833 el
->el_cursor
.h
+= num
; /* have written num spaces */
838 /* term_clear_screen():
842 term_clear_screen(EditLine
*el
)
843 { /* clear the whole screen and home */
846 /* send the clear screen code */
847 (void) tputs(Str(T_cl
), Val(T_li
), term__putc
);
848 else if (GoodStr(T_ho
) && GoodStr(T_cd
)) {
849 (void) tputs(Str(T_ho
), Val(T_li
), term__putc
); /* home */
850 /* clear to bottom of screen */
851 (void) tputs(Str(T_cd
), Val(T_li
), term__putc
);
860 * Beep the way the terminal wants us
863 term_beep(EditLine
*el
)
866 /* what termcap says we should use */
867 (void) tputs(Str(T_bl
), 1, term__putc
);
869 term__putc('\007'); /* an ASCII bell; ^G */
874 /* term_clear_to_bottom():
875 * Clear to the bottom of the screen
878 term_clear_to_bottom(EditLine
*el
)
881 (void) tputs(Str(T_cd
), Val(T_li
), term__putc
);
882 else if (GoodStr(T_ce
))
883 (void) tputs(Str(T_ce
), Val(T_li
), term__putc
);
889 * Read in the terminal capabilities from the requested terminal
892 term_set(EditLine
*el
, const char *term
)
895 char buf
[TC_BUFSIZE
];
897 const struct termcapstr
*t
;
901 (void) sigemptyset(&nset
);
902 (void) sigaddset(&nset
, SIGWINCH
);
903 (void) sigprocmask(SIG_BLOCK
, &nset
, &oset
);
909 term
= getenv("TERM");
911 if (!term
|| !term
[0])
914 if (strcmp(term
, "emacs") == 0)
915 el
->el_flags
|= EDIT_DISABLED
;
917 memset(el
->el_term
.t_cap
, 0, TC_BUFSIZE
);
919 i
= tgetent(el
->el_term
.t_cap
, term
);
923 (void) fprintf(el
->el_errfile
,
924 "Cannot read termcap database;\n");
926 (void) fprintf(el
->el_errfile
,
927 "No entry for terminal type \"%s\";\n", term
);
928 (void) fprintf(el
->el_errfile
,
929 "using dumb terminal settings.\n");
930 Val(T_co
) = 80; /* do a dumb terminal */
931 Val(T_pt
) = Val(T_km
) = Val(T_li
) = 0;
932 Val(T_xt
) = Val(T_MT
);
933 for (t
= tstr
; t
->name
!= NULL
; t
++)
934 term_alloc(el
, t
, NULL
);
936 /* auto/magic margins */
937 Val(T_am
) = tgetflag("am");
938 Val(T_xn
) = tgetflag("xn");
940 Val(T_pt
) = tgetflag("pt");
941 Val(T_xt
) = tgetflag("xt");
942 /* do we have a meta? */
943 Val(T_km
) = tgetflag("km");
944 Val(T_MT
) = tgetflag("MT");
946 Val(T_co
) = tgetnum("co");
947 Val(T_li
) = tgetnum("li");
948 for (t
= tstr
; t
->name
!= NULL
; t
++)
949 term_alloc(el
, t
, tgetstr((char *)t
->name
, &area
));
953 Val(T_co
) = 80; /* just in case */
957 el
->el_term
.t_size
.v
= Val(T_co
);
958 el
->el_term
.t_size
.h
= Val(T_li
);
962 /* get the correct window size */
963 (void) term_get_size(el
, &lins
, &cols
);
964 if (term_change_size(el
, lins
, cols
) == -1)
966 (void) sigprocmask(SIG_SETMASK
, &oset
, NULL
);
968 return (i
<= 0 ? -1 : 0);
973 * Return the new window size in lines and cols, and
974 * true if the size was changed.
977 term_get_size(EditLine
*el
, int *lins
, int *cols
)
986 if (ioctl(el
->el_infd
, TIOCGWINSZ
, (ioctl_t
) & ws
) != -1) {
997 if (ioctl(el
->el_infd
, TIOCGSIZE
, (ioctl_t
) & ts
) != -1) {
1001 *lins
= ts
.ts_lines
;
1005 return (Val(T_co
) != *cols
|| Val(T_li
) != *lins
);
1009 /* term_change_size():
1010 * Change the size of the terminal
1013 term_change_size(EditLine
*el
, int lins
, int cols
)
1018 Val(T_co
) = (cols
< 2) ? 80 : cols
;
1019 Val(T_li
) = (lins
< 1) ? 24 : lins
;
1021 /* re-make display buffers */
1022 if (term_rebuffer_display(el
) == -1)
1024 re_clear_display(el
);
1029 /* term_init_arrow():
1030 * Initialize the arrow key bindings from termcap
1033 term_init_arrow(EditLine
*el
)
1035 fkey_t
*arrow
= el
->el_term
.t_fkey
;
1037 arrow
[A_K_DN
].name
= "down";
1038 arrow
[A_K_DN
].key
= T_kd
;
1039 arrow
[A_K_DN
].fun
.cmd
= ED_NEXT_HISTORY
;
1040 arrow
[A_K_DN
].type
= XK_CMD
;
1042 arrow
[A_K_UP
].name
= "up";
1043 arrow
[A_K_UP
].key
= T_ku
;
1044 arrow
[A_K_UP
].fun
.cmd
= ED_PREV_HISTORY
;
1045 arrow
[A_K_UP
].type
= XK_CMD
;
1047 arrow
[A_K_LT
].name
= "left";
1048 arrow
[A_K_LT
].key
= T_kl
;
1049 arrow
[A_K_LT
].fun
.cmd
= ED_PREV_CHAR
;
1050 arrow
[A_K_LT
].type
= XK_CMD
;
1052 arrow
[A_K_RT
].name
= "right";
1053 arrow
[A_K_RT
].key
= T_kr
;
1054 arrow
[A_K_RT
].fun
.cmd
= ED_NEXT_CHAR
;
1055 arrow
[A_K_RT
].type
= XK_CMD
;
1057 arrow
[A_K_HO
].name
= "home";
1058 arrow
[A_K_HO
].key
= T_kh
;
1059 arrow
[A_K_HO
].fun
.cmd
= ED_MOVE_TO_BEG
;
1060 arrow
[A_K_HO
].type
= XK_CMD
;
1062 arrow
[A_K_EN
].name
= "end";
1063 arrow
[A_K_EN
].key
= T_at7
;
1064 arrow
[A_K_EN
].fun
.cmd
= ED_MOVE_TO_END
;
1065 arrow
[A_K_EN
].type
= XK_CMD
;
1069 /* term_reset_arrow():
1070 * Reset arrow key bindings
1073 term_reset_arrow(EditLine
*el
)
1075 fkey_t
*arrow
= el
->el_term
.t_fkey
;
1076 static const char strA
[] = {033, '[', 'A', '\0'};
1077 static const char strB
[] = {033, '[', 'B', '\0'};
1078 static const char strC
[] = {033, '[', 'C', '\0'};
1079 static const char strD
[] = {033, '[', 'D', '\0'};
1080 static const char strH
[] = {033, '[', 'H', '\0'};
1081 static const char strF
[] = {033, '[', 'F', '\0'};
1082 static const char stOA
[] = {033, 'O', 'A', '\0'};
1083 static const char stOB
[] = {033, 'O', 'B', '\0'};
1084 static const char stOC
[] = {033, 'O', 'C', '\0'};
1085 static const char stOD
[] = {033, 'O', 'D', '\0'};
1086 static const char stOH
[] = {033, 'O', 'H', '\0'};
1087 static const char stOF
[] = {033, 'O', 'F', '\0'};
1089 key_add(el
, strA
, &arrow
[A_K_UP
].fun
, arrow
[A_K_UP
].type
);
1090 key_add(el
, strB
, &arrow
[A_K_DN
].fun
, arrow
[A_K_DN
].type
);
1091 key_add(el
, strC
, &arrow
[A_K_RT
].fun
, arrow
[A_K_RT
].type
);
1092 key_add(el
, strD
, &arrow
[A_K_LT
].fun
, arrow
[A_K_LT
].type
);
1093 key_add(el
, strH
, &arrow
[A_K_HO
].fun
, arrow
[A_K_HO
].type
);
1094 key_add(el
, strF
, &arrow
[A_K_EN
].fun
, arrow
[A_K_EN
].type
);
1095 key_add(el
, stOA
, &arrow
[A_K_UP
].fun
, arrow
[A_K_UP
].type
);
1096 key_add(el
, stOB
, &arrow
[A_K_DN
].fun
, arrow
[A_K_DN
].type
);
1097 key_add(el
, stOC
, &arrow
[A_K_RT
].fun
, arrow
[A_K_RT
].type
);
1098 key_add(el
, stOD
, &arrow
[A_K_LT
].fun
, arrow
[A_K_LT
].type
);
1099 key_add(el
, stOH
, &arrow
[A_K_HO
].fun
, arrow
[A_K_HO
].type
);
1100 key_add(el
, stOF
, &arrow
[A_K_EN
].fun
, arrow
[A_K_EN
].type
);
1102 if (el
->el_map
.type
== MAP_VI
) {
1103 key_add(el
, &strA
[1], &arrow
[A_K_UP
].fun
, arrow
[A_K_UP
].type
);
1104 key_add(el
, &strB
[1], &arrow
[A_K_DN
].fun
, arrow
[A_K_DN
].type
);
1105 key_add(el
, &strC
[1], &arrow
[A_K_RT
].fun
, arrow
[A_K_RT
].type
);
1106 key_add(el
, &strD
[1], &arrow
[A_K_LT
].fun
, arrow
[A_K_LT
].type
);
1107 key_add(el
, &strH
[1], &arrow
[A_K_HO
].fun
, arrow
[A_K_HO
].type
);
1108 key_add(el
, &strF
[1], &arrow
[A_K_EN
].fun
, arrow
[A_K_EN
].type
);
1109 key_add(el
, &stOA
[1], &arrow
[A_K_UP
].fun
, arrow
[A_K_UP
].type
);
1110 key_add(el
, &stOB
[1], &arrow
[A_K_DN
].fun
, arrow
[A_K_DN
].type
);
1111 key_add(el
, &stOC
[1], &arrow
[A_K_RT
].fun
, arrow
[A_K_RT
].type
);
1112 key_add(el
, &stOD
[1], &arrow
[A_K_LT
].fun
, arrow
[A_K_LT
].type
);
1113 key_add(el
, &stOH
[1], &arrow
[A_K_HO
].fun
, arrow
[A_K_HO
].type
);
1114 key_add(el
, &stOF
[1], &arrow
[A_K_EN
].fun
, arrow
[A_K_EN
].type
);
1119 /* term_set_arrow():
1120 * Set an arrow key binding
1123 term_set_arrow(EditLine
*el
, const char *name
, key_value_t
*fun
, int type
)
1125 fkey_t
*arrow
= el
->el_term
.t_fkey
;
1128 for (i
= 0; i
< A_K_NKEYS
; i
++)
1129 if (strcmp(name
, arrow
[i
].name
) == 0) {
1130 arrow
[i
].fun
= *fun
;
1131 arrow
[i
].type
= type
;
1138 /* term_clear_arrow():
1139 * Clear an arrow key binding
1142 term_clear_arrow(EditLine
*el
, const char *name
)
1144 fkey_t
*arrow
= el
->el_term
.t_fkey
;
1147 for (i
= 0; i
< A_K_NKEYS
; i
++)
1148 if (strcmp(name
, arrow
[i
].name
) == 0) {
1149 arrow
[i
].type
= XK_NOD
;
1156 /* term_print_arrow():
1157 * Print the arrow key bindings
1160 term_print_arrow(EditLine
*el
, const char *name
)
1163 fkey_t
*arrow
= el
->el_term
.t_fkey
;
1165 for (i
= 0; i
< A_K_NKEYS
; i
++)
1166 if (*name
== '\0' || strcmp(name
, arrow
[i
].name
) == 0)
1167 if (arrow
[i
].type
!= XK_NOD
)
1168 key_kprint(el
, arrow
[i
].name
, &arrow
[i
].fun
,
1173 /* term_bind_arrow():
1174 * Bind the arrow keys
1177 term_bind_arrow(EditLine
*el
)
1180 const el_action_t
*dmap
;
1183 fkey_t
*arrow
= el
->el_term
.t_fkey
;
1185 /* Check if the components needed are initialized */
1186 if (el
->el_term
.t_buf
== NULL
|| el
->el_map
.key
== NULL
)
1189 map
= el
->el_map
.type
== MAP_VI
? el
->el_map
.alt
: el
->el_map
.key
;
1190 dmap
= el
->el_map
.type
== MAP_VI
? el
->el_map
.vic
: el
->el_map
.emacs
;
1192 term_reset_arrow(el
);
1194 for (i
= 0; i
< A_K_NKEYS
; i
++) {
1195 p
= el
->el_term
.t_str
[arrow
[i
].key
];
1197 j
= (unsigned char) *p
;
1199 * Assign the arrow keys only if:
1201 * 1. They are multi-character arrow keys and the user
1202 * has not re-assigned the leading character, or
1203 * has re-assigned the leading character to be
1204 * ED_SEQUENCE_LEAD_IN
1205 * 2. They are single arrow keys pointing to an
1208 if (arrow
[i
].type
== XK_NOD
)
1209 key_clear(el
, map
, p
);
1211 if (p
[1] && (dmap
[j
] == map
[j
] ||
1212 map
[j
] == ED_SEQUENCE_LEAD_IN
)) {
1213 key_add(el
, p
, &arrow
[i
].fun
,
1215 map
[j
] = ED_SEQUENCE_LEAD_IN
;
1216 } else if (map
[j
] == ED_UNASSIGNED
) {
1217 key_clear(el
, map
, p
);
1218 if (arrow
[i
].type
== XK_CMD
)
1219 map
[j
] = arrow
[i
].fun
.cmd
;
1221 key_add(el
, p
, &arrow
[i
].fun
,
1237 return (fputc(c
, term_outfile
));
1248 (void) fflush(term_outfile
);
1253 * Print the current termcap characteristics
1257 term_telltc(EditLine
*el
, int argc
, const char **argv
)
1259 const struct termcapstr
*t
;
1261 char upbuf
[EL_BUFSIZ
];
1263 (void) fprintf(el
->el_outfile
, "\n\tYour terminal has the\n");
1264 (void) fprintf(el
->el_outfile
, "\tfollowing characteristics:\n\n");
1265 (void) fprintf(el
->el_outfile
, "\tIt has %d columns and %d lines\n",
1266 Val(T_co
), Val(T_li
));
1267 (void) fprintf(el
->el_outfile
,
1268 "\tIt has %s meta key\n", EL_HAS_META
? "a" : "no");
1269 (void) fprintf(el
->el_outfile
,
1270 "\tIt can%suse tabs\n", EL_CAN_TAB
? " " : "not ");
1271 (void) fprintf(el
->el_outfile
, "\tIt %s automatic margins\n",
1272 EL_HAS_AUTO_MARGINS
? "has" : "does not have");
1273 if (EL_HAS_AUTO_MARGINS
)
1274 (void) fprintf(el
->el_outfile
, "\tIt %s magic margins\n",
1275 EL_HAS_MAGIC_MARGINS
? "has" : "does not have");
1277 for (t
= tstr
, ts
= el
->el_term
.t_str
; t
->name
!= NULL
; t
++, ts
++)
1278 (void) fprintf(el
->el_outfile
, "\t%25s (%s) == %s\n",
1280 t
->name
, *ts
&& **ts
?
1281 key__decode_str(*ts
, upbuf
, "") : "(empty)");
1282 (void) fputc('\n', el
->el_outfile
);
1288 * Change the current terminal characteristics
1292 term_settc(EditLine
*el
, int argc
, const char **argv
)
1294 const struct termcapstr
*ts
;
1295 const struct termcapval
*tv
;
1296 const char *what
, *how
;
1298 if (argv
== NULL
|| argv
[1] == NULL
|| argv
[2] == NULL
)
1305 * Do the strings first
1307 for (ts
= tstr
; ts
->name
!= NULL
; ts
++)
1308 if (strcmp(ts
->name
, what
) == 0)
1311 if (ts
->name
!= NULL
) {
1312 term_alloc(el
, ts
, how
);
1317 * Do the numeric ones second
1319 for (tv
= tval
; tv
->name
!= NULL
; tv
++)
1320 if (strcmp(tv
->name
, what
) == 0)
1323 if (tv
->name
!= NULL
) {
1324 if (tv
== &tval
[T_pt
] || tv
== &tval
[T_km
] ||
1325 tv
== &tval
[T_am
] || tv
== &tval
[T_xn
]) {
1326 if (strcmp(how
, "yes") == 0)
1327 el
->el_term
.t_val
[tv
- tval
] = 1;
1328 else if (strcmp(how
, "no") == 0)
1329 el
->el_term
.t_val
[tv
- tval
] = 0;
1331 (void) fprintf(el
->el_errfile
,
1332 "settc: Bad value `%s'.\n", how
);
1336 if (term_change_size(el
, Val(T_li
), Val(T_co
)) == -1)
1343 i
= strtol(how
, &ep
, 10);
1345 (void) fprintf(el
->el_errfile
,
1346 "settc: Bad value `%s'.\n", how
);
1349 el
->el_term
.t_val
[tv
- tval
] = (int) i
;
1350 el
->el_term
.t_size
.v
= Val(T_co
);
1351 el
->el_term
.t_size
.h
= Val(T_li
);
1352 if (tv
== &tval
[T_co
] || tv
== &tval
[T_li
])
1353 if (term_change_size(el
, Val(T_li
), Val(T_co
))
1364 * Print the termcap string out with variable substitution
1368 term_echotc(EditLine
*el
, int argc
, const char **argv
)
1370 char *cap
, *scap
, *ep
;
1371 int arg_need
, arg_cols
, arg_rows
;
1372 int verbose
= 0, silent
= 0;
1374 static const char fmts
[] = "%s\n", fmtd
[] = "%d\n";
1375 const struct termcapstr
*t
;
1376 char buf
[TC_BUFSIZE
];
1381 if (argv
== NULL
|| argv
[1] == NULL
)
1385 if (argv
[0][0] == '-') {
1386 switch (argv
[0][1]) {
1394 /* stderror(ERR_NAME | ERR_TCUSAGE); */
1399 if (!*argv
|| *argv
[0] == '\0')
1401 if (strcmp(*argv
, "tabs") == 0) {
1402 (void) fprintf(el
->el_outfile
, fmts
, EL_CAN_TAB
? "yes" : "no");
1404 } else if (strcmp(*argv
, "meta") == 0) {
1405 (void) fprintf(el
->el_outfile
, fmts
, Val(T_km
) ? "yes" : "no");
1407 } else if (strcmp(*argv
, "xn") == 0) {
1408 (void) fprintf(el
->el_outfile
, fmts
, EL_HAS_MAGIC_MARGINS
?
1411 } else if (strcmp(*argv
, "am") == 0) {
1412 (void) fprintf(el
->el_outfile
, fmts
, EL_HAS_AUTO_MARGINS
?
1415 } else if (strcmp(*argv
, "baud") == 0) {
1419 for (i
= 0; baud_rate
[i
].b_name
!= NULL
; i
++)
1420 if (el
->el_tty
.t_speed
== baud_rate
[i
].b_rate
) {
1421 (void) fprintf(el
->el_outfile
, fmts
,
1422 baud_rate
[i
].b_name
);
1425 (void) fprintf(el
->el_outfile
, fmtd
, 0);
1427 (void) fprintf(el
->el_outfile
, fmtd
, (int) el
->el_tty
.t_speed
);
1430 } else if (strcmp(*argv
, "rows") == 0 || strcmp(*argv
, "lines") == 0) {
1431 (void) fprintf(el
->el_outfile
, fmtd
, Val(T_li
));
1433 } else if (strcmp(*argv
, "cols") == 0) {
1434 (void) fprintf(el
->el_outfile
, fmtd
, Val(T_co
));
1438 * Try to use our local definition first
1441 for (t
= tstr
; t
->name
!= NULL
; t
++)
1442 if (strcmp(t
->name
, *argv
) == 0) {
1443 scap
= el
->el_term
.t_str
[t
- tstr
];
1446 if (t
->name
== NULL
)
1447 scap
= tgetstr((char *)argv
, &area
);
1448 if (!scap
|| scap
[0] == '\0') {
1450 (void) fprintf(el
->el_errfile
,
1451 "echotc: Termcap parameter `%s' not found.\n",
1456 * Count home many values we need for this capability.
1458 for (cap
= scap
, arg_need
= 0; *cap
; cap
++)
1478 * hpux has lot's of them...
1481 (void) fprintf(el
->el_errfile
,
1482 "echotc: Warning: unknown termcap %% `%c'.\n",
1484 /* This is bad, but I won't complain */
1491 if (*argv
&& *argv
[0]) {
1493 (void) fprintf(el
->el_errfile
,
1494 "echotc: Warning: Extra argument `%s'.\n",
1498 (void) tputs(scap
, 1, term__putc
);
1502 if (!*argv
|| *argv
[0] == '\0') {
1504 (void) fprintf(el
->el_errfile
,
1505 "echotc: Warning: Missing argument.\n");
1509 i
= strtol(*argv
, &ep
, 10);
1510 if (*ep
!= '\0' || i
< 0) {
1512 (void) fprintf(el
->el_errfile
,
1513 "echotc: Bad value `%s' for rows.\n",
1519 if (*argv
&& *argv
[0]) {
1521 (void) fprintf(el
->el_errfile
,
1522 "echotc: Warning: Extra argument `%s'.\n",
1526 (void) tputs(tgoto(scap
, arg_cols
, arg_rows
), 1, term__putc
);
1529 /* This is wrong, but I will ignore it... */
1531 (void) fprintf(el
->el_errfile
,
1532 "echotc: Warning: Too many required arguments (%d).\n",
1537 if (!*argv
|| *argv
[0] == '\0') {
1539 (void) fprintf(el
->el_errfile
,
1540 "echotc: Warning: Missing argument.\n");
1543 i
= strtol(*argv
, &ep
, 10);
1544 if (*ep
!= '\0' || i
< 0) {
1546 (void) fprintf(el
->el_errfile
,
1547 "echotc: Bad value `%s' for cols.\n",
1553 if (!*argv
|| *argv
[0] == '\0') {
1555 (void) fprintf(el
->el_errfile
,
1556 "echotc: Warning: Missing argument.\n");
1559 i
= strtol(*argv
, &ep
, 10);
1560 if (*ep
!= '\0' || i
< 0) {
1562 (void) fprintf(el
->el_errfile
,
1563 "echotc: Bad value `%s' for rows.\n",
1570 (void) fprintf(el
->el_errfile
,
1571 "echotc: Bad value `%s'.\n", *argv
);
1575 if (*argv
&& *argv
[0]) {
1577 (void) fprintf(el
->el_errfile
,
1578 "echotc: Warning: Extra argument `%s'.\n",
1582 (void) tputs(tgoto(scap
, arg_cols
, arg_rows
), arg_rows
,