1 /* $NetBSD: terminal.c,v 1.24 2016/03/22 01:38:17 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: terminal.c,v 1.24 2016/03/22 01:38:17 christos Exp $");
42 #endif /* not lint && not SCCSID */
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
47 * terminal.c: Editor/termcap-curses interface
48 * We have to declare a static variable here, since the
49 * termcap putchar routine does not take an argument!
51 #include <sys/types.h>
52 #include <sys/ioctl.h>
68 /* Solaris's term.h does horrid things. */
69 #if defined(HAVE_TERM_H) && !defined(__sun) && !defined(HAVE_TERMCAP_H)
80 * IMPORTANT NOTE: these routines are allowed to look at the current screen
81 * and the current position assuming that it is correct. If this is not
82 * true, then the update will be WRONG! This is (should be) a valid
86 #define TC_BUFSIZE ((size_t)2048)
88 #define GoodStr(a) (el->el_terminal.t_str[a] != NULL && \
89 el->el_terminal.t_str[a][0] != '\0')
90 #define Str(a) el->el_terminal.t_str[a]
91 #define Val(a) el->el_terminal.t_val[a]
93 private const struct termcapstr {
95 const char *long_name;
98 { "al", "add new blank line" },
100 { "bl", "audible bell" },
102 { "cd", "clear to bottom" },
104 { "ce", "clear to end of line" },
106 { "ch", "cursor to horiz pos" },
108 { "cl", "clear screen" },
110 { "dc", "delete a character" },
112 { "dl", "delete a line" },
114 { "dm", "start delete mode" },
116 { "ed", "end delete mode" },
118 { "ei", "end insert mode" },
120 { "fs", "cursor from status line" },
122 { "ho", "home cursor" },
124 { "ic", "insert character" },
126 { "im", "start insert mode" },
128 { "ip", "insert padding" },
130 { "kd", "sends cursor down" },
132 { "kl", "sends cursor left" },
134 { "kr", "sends cursor right" },
136 { "ku", "sends cursor up" },
138 { "md", "begin bold" },
140 { "me", "end attributes" },
142 { "nd", "non destructive space" },
144 { "se", "end standout" },
146 { "so", "begin standout" },
148 { "ts", "cursor to status line" },
150 { "up", "cursor up one" },
152 { "us", "begin underline" },
154 { "ue", "end underline" },
156 { "vb", "visible bell" },
158 { "DC", "delete multiple chars" },
160 { "DO", "cursor down multiple" },
162 { "IC", "insert multiple chars" },
164 { "LE", "cursor left multiple" },
166 { "RI", "cursor right multiple" },
168 { "UP", "cursor up multiple" },
170 { "kh", "send cursor home" },
172 { "@7", "send cursor end" },
174 { "kD", "send cursor delete" },
179 private const struct termcapval {
181 const char *long_name;
184 { "am", "has automatic margins" },
186 { "pt", "has physical tabs" },
188 { "li", "Number of lines" },
190 { "co", "Number of columns" },
192 { "km", "Has meta key" },
194 { "xt", "Tab chars destructive" },
196 { "xn", "newline ignored at right margin" },
198 { "MT", "Has meta key" }, /* XXX? */
202 /* do two or more of the attributes use me */
204 private void terminal_setflags(EditLine *);
205 private int terminal_rebuffer_display(EditLine *);
206 private void terminal_free_display(EditLine *);
207 private int terminal_alloc_display(EditLine *);
208 private void terminal_alloc(EditLine *, const struct termcapstr *,
210 private void terminal_init_arrow(EditLine *);
211 private void terminal_reset_arrow(EditLine *);
212 private int terminal_putc(int);
213 private void terminal_tputs(EditLine *, const char *, int);
216 private pthread_mutex_t terminal_mutex = PTHREAD_MUTEX_INITIALIZER;
218 private FILE *terminal_outfile = NULL;
221 /* terminal_setflags():
222 * Set the terminal capability flags
225 terminal_setflags(EditLine *el)
228 if (el->el_tty.t_tabs)
229 EL_FLAGS |= (Val(T_pt) && !Val(T_xt)) ? TERM_CAN_TAB : 0;
231 EL_FLAGS |= (Val(T_km) || Val(T_MT)) ? TERM_HAS_META : 0;
232 EL_FLAGS |= GoodStr(T_ce) ? TERM_CAN_CEOL : 0;
233 EL_FLAGS |= (GoodStr(T_dc) || GoodStr(T_DC)) ? TERM_CAN_DELETE : 0;
234 EL_FLAGS |= (GoodStr(T_im) || GoodStr(T_ic) || GoodStr(T_IC)) ?
236 EL_FLAGS |= (GoodStr(T_up) || GoodStr(T_UP)) ? TERM_CAN_UP : 0;
237 EL_FLAGS |= Val(T_am) ? TERM_HAS_AUTO_MARGINS : 0;
238 EL_FLAGS |= Val(T_xn) ? TERM_HAS_MAGIC_MARGINS : 0;
240 if (GoodStr(T_me) && GoodStr(T_ue))
241 EL_FLAGS |= (strcmp(Str(T_me), Str(T_ue)) == 0) ?
244 EL_FLAGS &= ~TERM_CAN_ME;
245 if (GoodStr(T_me) && GoodStr(T_se))
246 EL_FLAGS |= (strcmp(Str(T_me), Str(T_se)) == 0) ?
252 (void) fprintf(el->el_errfile,
253 "WARNING: Your terminal cannot move up.\n");
254 (void) fprintf(el->el_errfile,
255 "Editing may be odd for long lines.\n");
258 (void) fprintf(el->el_errfile, "no clear EOL capability.\n");
260 (void) fprintf(el->el_errfile, "no delete char capability.\n");
262 (void) fprintf(el->el_errfile, "no insert char capability.\n");
263 #endif /* DEBUG_SCREEN */
267 * Initialize the terminal stuff
270 terminal_init(EditLine *el)
273 el->el_terminal.t_buf = el_malloc(TC_BUFSIZE *
274 sizeof(*el->el_terminal.t_buf));
275 if (el->el_terminal.t_buf == NULL)
277 el->el_terminal.t_cap = el_malloc(TC_BUFSIZE *
278 sizeof(*el->el_terminal.t_cap));
279 if (el->el_terminal.t_cap == NULL)
281 el->el_terminal.t_fkey = el_malloc(A_K_NKEYS *
282 sizeof(*el->el_terminal.t_fkey));
283 if (el->el_terminal.t_fkey == NULL)
285 el->el_terminal.t_loc = 0;
286 el->el_terminal.t_str = el_malloc(T_str *
287 sizeof(*el->el_terminal.t_str));
288 if (el->el_terminal.t_str == NULL)
290 (void) memset(el->el_terminal.t_str, 0, T_str *
291 sizeof(*el->el_terminal.t_str));
292 el->el_terminal.t_val = el_malloc(T_val *
293 sizeof(*el->el_terminal.t_val));
294 if (el->el_terminal.t_val == NULL)
296 (void) memset(el->el_terminal.t_val, 0, T_val *
297 sizeof(*el->el_terminal.t_val));
298 (void) terminal_set(el, NULL);
299 terminal_init_arrow(el);
302 free(el->el_terminal.t_str);
303 el->el_terminal.t_str = NULL;
305 free(el->el_terminal.t_fkey);
306 el->el_terminal.t_fkey = NULL;
308 free(el->el_terminal.t_cap);
309 el->el_terminal.t_cap = NULL;
311 free(el->el_terminal.t_buf);
312 el->el_terminal.t_buf = NULL;
318 * Clean up the terminal stuff
321 terminal_end(EditLine *el)
324 el_free(el->el_terminal.t_buf);
325 el->el_terminal.t_buf = NULL;
326 el_free(el->el_terminal.t_cap);
327 el->el_terminal.t_cap = NULL;
328 el->el_terminal.t_loc = 0;
329 el_free(el->el_terminal.t_str);
330 el->el_terminal.t_str = NULL;
331 el_free(el->el_terminal.t_val);
332 el->el_terminal.t_val = NULL;
333 el_free(el->el_terminal.t_fkey);
334 el->el_terminal.t_fkey = NULL;
335 terminal_free_display(el);
340 * Maintain a string pool for termcap strings
343 terminal_alloc(EditLine *el, const struct termcapstr *t, const char *cap)
345 char termbuf[TC_BUFSIZE];
347 char **tlist = el->el_terminal.t_str;
348 char **tmp, **str = &tlist[t - tstr];
350 (void) memset(termbuf, 0, sizeof(termbuf));
351 if (cap == NULL || *cap == '\0') {
357 tlen = *str == NULL ? 0 : strlen(*str);
360 * New string is shorter; no need to allocate space
364 (void) strcpy(*str, cap); /* XXX strcpy is safe */
368 * New string is longer; see if we have enough space to append
370 if (el->el_terminal.t_loc + 3 < TC_BUFSIZE) {
371 /* XXX strcpy is safe */
372 (void) strcpy(*str = &el->el_terminal.t_buf[
373 el->el_terminal.t_loc], cap);
374 el->el_terminal.t_loc += clen + 1; /* one for \0 */
378 * Compact our buffer; no need to check compaction, cause we know it
382 for (tmp = tlist; tmp < &tlist[T_str]; tmp++)
383 if (*tmp != NULL && **tmp != '\0' && *tmp != *str) {
386 for (ptr = *tmp; *ptr != '\0'; termbuf[tlen++] = *ptr++)
388 termbuf[tlen++] = '\0';
390 memcpy(el->el_terminal.t_buf, termbuf, TC_BUFSIZE);
391 el->el_terminal.t_loc = tlen;
392 if (el->el_terminal.t_loc + 3 >= TC_BUFSIZE) {
393 (void) fprintf(el->el_errfile,
394 "Out of termcap string space.\n");
397 /* XXX strcpy is safe */
398 (void) strcpy(*str = &el->el_terminal.t_buf[el->el_terminal.t_loc],
400 el->el_terminal.t_loc += (size_t)clen + 1; /* one for \0 */
405 /* terminal_rebuffer_display():
406 * Rebuffer the display after the screen changed size
409 terminal_rebuffer_display(EditLine *el)
411 coord_t *c = &el->el_terminal.t_size;
413 terminal_free_display(el);
418 if (terminal_alloc_display(el) == -1)
424 /* terminal_alloc_display():
425 * Allocate a new display.
428 terminal_alloc_display(EditLine *el)
432 coord_t *c = &el->el_terminal.t_size;
434 b = el_malloc(sizeof(*b) * (size_t)(c->v + 1));
437 for (i = 0; i < c->v; i++) {
438 b[i] = el_malloc(sizeof(**b) * (size_t)(c->h + 1));
449 b = el_malloc(sizeof(*b) * (size_t)(c->v + 1));
452 for (i = 0; i < c->v; i++) {
453 b[i] = el_malloc(sizeof(**b) * (size_t)(c->h + 1));
465 terminal_free_display(el);
470 /* terminal_free_display():
471 * Free the display buffers
474 terminal_free_display(EditLine *el)
480 el->el_display = NULL;
482 for (bufp = b; *bufp != NULL; bufp++)
487 el->el_vdisplay = NULL;
489 for (bufp = b; *bufp != NULL; bufp++)
496 /* terminal_move_to_line():
497 * move to line <where> (first line == 0)
498 * as efficiently as possible
501 terminal_move_to_line(EditLine *el, int where)
505 if (where == el->el_cursor.v)
508 if (where > el->el_terminal.t_size.v) {
510 (void) fprintf(el->el_errfile,
511 "%s: where is ridiculous: %d\r\n", __func__, where);
512 #endif /* DEBUG_SCREEN */
515 if ((del = where - el->el_cursor.v) > 0) {
517 if (EL_HAS_AUTO_MARGINS &&
518 el->el_display[el->el_cursor.v][0] != '\0') {
520 (el->el_terminal.t_size.h - 1);
523 el->el_display[el->el_cursor.v][h] ==
528 /* move without newline */
529 terminal_move_to_char(el, (int)h);
530 terminal_overwrite(el, &el->el_display
531 [el->el_cursor.v][el->el_cursor.h],
532 (size_t)(el->el_terminal.t_size.h -
537 if ((del > 1) && GoodStr(T_DO)) {
538 terminal_tputs(el, tgoto(Str(T_DO), del,
542 for (; del > 0; del--)
543 terminal__putc(el, '\n');
544 /* because the \n will become \r\n */
549 } else { /* del < 0 */
550 if (GoodStr(T_UP) && (-del > 1 || !GoodStr(T_up)))
551 terminal_tputs(el, tgoto(Str(T_UP), -del, -del), -del);
554 for (; del < 0; del++)
555 terminal_tputs(el, Str(T_up), 1);
558 el->el_cursor.v = where;/* now where is here */
562 /* terminal_move_to_char():
563 * Move to the character position specified
566 terminal_move_to_char(EditLine *el, int where)
571 if (where == el->el_cursor.h)
574 if (where > el->el_terminal.t_size.h) {
576 (void) fprintf(el->el_errfile,
577 "%s: where is ridiculous: %d\r\n", __func__, where);
578 #endif /* DEBUG_SCREEN */
581 if (!where) { /* if where is first column */
582 terminal__putc(el, '\r'); /* do a CR */
586 del = where - el->el_cursor.h;
588 if ((del < -4 || del > 4) && GoodStr(T_ch))
589 /* go there directly */
590 terminal_tputs(el, tgoto(Str(T_ch), where, where), where);
592 if (del > 0) { /* moving forward */
593 if ((del > 4) && GoodStr(T_RI))
594 terminal_tputs(el, tgoto(Str(T_RI), del, del),
597 /* if I can do tabs, use them */
599 if ((el->el_cursor.h & 0370) !=
603 el->el_cursor.v][where & 0370] !=
607 /* if not within tab stop */
609 (el->el_cursor.h & 0370);
615 el->el_cursor.h = where & ~0x7;
619 * it's usually cheaper to just write the
623 * NOTE THAT terminal_overwrite() WILL CHANGE
626 terminal_overwrite(el, &el->el_display[
627 el->el_cursor.v][el->el_cursor.h],
628 (size_t)(where - el->el_cursor.h));
631 } else { /* del < 0 := moving backward */
632 if ((-del > 4) && GoodStr(T_LE))
633 terminal_tputs(el, tgoto(Str(T_LE), -del, -del),
635 else { /* can't go directly there */
637 * if the "cost" is greater than the "cost"
641 ((unsigned int)-del >
642 (((unsigned int) where >> 3) +
645 terminal__putc(el, '\r');/* do a CR */
647 goto mc_again; /* and try again */
649 for (i = 0; i < -del; i++)
650 terminal__putc(el, '\b');
654 el->el_cursor.h = where; /* now where is here */
658 /* terminal_overwrite():
659 * Overstrike num characters
660 * Assumes MB_FILL_CHARs are present to keep the column count correct
663 terminal_overwrite(EditLine *el, const Char *cp, size_t n)
668 if (n > (size_t)el->el_terminal.t_size.h) {
670 (void) fprintf(el->el_errfile,
671 "%s: n is ridiculous: %zu\r\n", __func__, n);
672 #endif /* DEBUG_SCREEN */
677 /* terminal__putc() ignores any MB_FILL_CHARs */
678 terminal__putc(el, *cp++);
682 if (el->el_cursor.h >= el->el_terminal.t_size.h) { /* wrap? */
683 if (EL_HAS_AUTO_MARGINS) { /* yes */
686 if (EL_HAS_MAGIC_MARGINS) {
687 /* force the wrap to avoid the "magic"
690 if ((c = el->el_display[el->el_cursor.v]
691 [el->el_cursor.h]) != '\0') {
692 terminal_overwrite(el, &c, (size_t)1);
694 while (el->el_display[el->el_cursor.v]
695 [el->el_cursor.h] == MB_FILL_CHAR)
699 terminal__putc(el, ' ');
703 } else /* no wrap, but cursor stays on screen */
704 el->el_cursor.h = el->el_terminal.t_size.h - 1;
709 /* terminal_deletechars():
710 * Delete num characters
713 terminal_deletechars(EditLine *el, int num)
718 if (!EL_CAN_DELETE) {
720 (void) fprintf(el->el_errfile, " ERROR: cannot delete \n");
721 #endif /* DEBUG_EDIT */
724 if (num > el->el_terminal.t_size.h) {
726 (void) fprintf(el->el_errfile,
727 "%s: num is ridiculous: %d\r\n", __func__, num);
728 #endif /* DEBUG_SCREEN */
731 if (GoodStr(T_DC)) /* if I have multiple delete */
732 if ((num > 1) || !GoodStr(T_dc)) { /* if dc would be more
734 terminal_tputs(el, tgoto(Str(T_DC), num, num), num);
737 if (GoodStr(T_dm)) /* if I have delete mode */
738 terminal_tputs(el, Str(T_dm), 1);
740 if (GoodStr(T_dc)) /* else do one at a time */
742 terminal_tputs(el, Str(T_dc), 1);
744 if (GoodStr(T_ed)) /* if I have delete mode */
745 terminal_tputs(el, Str(T_ed), 1);
749 /* terminal_insertwrite():
750 * Puts terminal in insert character mode or inserts num
751 * characters in the line
752 * Assumes MB_FILL_CHARs are present to keep column count correct
755 terminal_insertwrite(EditLine *el, Char *cp, int num)
759 if (!EL_CAN_INSERT) {
761 (void) fprintf(el->el_errfile, " ERROR: cannot insert \n");
762 #endif /* DEBUG_EDIT */
765 if (num > el->el_terminal.t_size.h) {
767 (void) fprintf(el->el_errfile,
768 "%s: num is ridiculous: %d\r\n", __func__, num);
769 #endif /* DEBUG_SCREEN */
772 if (GoodStr(T_IC)) /* if I have multiple insert */
773 if ((num > 1) || !GoodStr(T_ic)) {
774 /* if ic would be more expensive */
775 terminal_tputs(el, tgoto(Str(T_IC), num, num), num);
776 terminal_overwrite(el, cp, (size_t)num);
777 /* this updates el_cursor.h */
780 if (GoodStr(T_im) && GoodStr(T_ei)) { /* if I have insert mode */
781 terminal_tputs(el, Str(T_im), 1);
783 el->el_cursor.h += num;
785 terminal__putc(el, *cp++);
788 if (GoodStr(T_ip)) /* have to make num chars insert */
789 terminal_tputs(el, Str(T_ip), 1);
791 terminal_tputs(el, Str(T_ei), 1);
795 if (GoodStr(T_ic)) /* have to make num chars insert */
796 terminal_tputs(el, Str(T_ic), 1);
798 terminal__putc(el, *cp++);
802 if (GoodStr(T_ip)) /* have to make num chars insert */
803 terminal_tputs(el, Str(T_ip), 1);
804 /* pad the inserted char */
810 /* terminal_clear_EOL():
811 * clear to end of line. There are num characters to clear
814 terminal_clear_EOL(EditLine *el, int num)
818 if (EL_CAN_CEOL && GoodStr(T_ce))
819 terminal_tputs(el, Str(T_ce), 1);
821 for (i = 0; i < num; i++)
822 terminal__putc(el, ' ');
823 el->el_cursor.h += num; /* have written num spaces */
828 /* terminal_clear_screen():
832 terminal_clear_screen(EditLine *el)
833 { /* clear the whole screen and home */
836 /* send the clear screen code */
837 terminal_tputs(el, Str(T_cl), Val(T_li));
838 else if (GoodStr(T_ho) && GoodStr(T_cd)) {
839 terminal_tputs(el, Str(T_ho), Val(T_li)); /* home */
840 /* clear to bottom of screen */
841 terminal_tputs(el, Str(T_cd), Val(T_li));
843 terminal__putc(el, '\r');
844 terminal__putc(el, '\n');
850 * Beep the way the terminal wants us
853 terminal_beep(EditLine *el)
856 /* what termcap says we should use */
857 terminal_tputs(el, Str(T_bl), 1);
859 terminal__putc(el, '\007'); /* an ASCII bell; ^G */
864 terminal_get(EditLine *el, const char **term)
866 *term = el->el_terminal.t_name;
871 * Read in the terminal capabilities from the requested terminal
874 terminal_set(EditLine *el, const char *term)
877 char buf[TC_BUFSIZE];
879 const struct termcapstr *t;
883 (void) sigemptyset(&nset);
884 (void) sigaddset(&nset, SIGWINCH);
885 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
891 term = getenv("TERM");
893 if (!term || !term[0])
896 if (strcmp(term, "emacs") == 0)
897 el->el_flags |= EDIT_DISABLED;
899 (void) memset(el->el_terminal.t_cap, 0, TC_BUFSIZE);
901 i = tgetent(el->el_terminal.t_cap, term);
905 (void) fprintf(el->el_errfile,
906 "Cannot read termcap database;\n");
908 (void) fprintf(el->el_errfile,
909 "No entry for terminal type \"%s\";\n", term);
910 (void) fprintf(el->el_errfile,
911 "using dumb terminal settings.\n");
912 Val(T_co) = 80; /* do a dumb terminal */
913 Val(T_pt) = Val(T_km) = Val(T_li) = 0;
914 Val(T_xt) = Val(T_MT);
915 for (t = tstr; t->name != NULL; t++)
916 terminal_alloc(el, t, NULL);
918 /* auto/magic margins */
919 Val(T_am) = tgetflag("am");
920 Val(T_xn) = tgetflag("xn");
922 Val(T_pt) = tgetflag("pt");
923 Val(T_xt) = tgetflag("xt");
924 /* do we have a meta? */
925 Val(T_km) = tgetflag("km");
926 Val(T_MT) = tgetflag("MT");
928 Val(T_co) = tgetnum("co");
929 Val(T_li) = tgetnum("li");
930 for (t = tstr; t->name != NULL; t++) {
931 /* XXX: some systems' tgetstr needs non const */
932 terminal_alloc(el, t, tgetstr(strchr(t->name, *t->name),
938 Val(T_co) = 80; /* just in case */
942 el->el_terminal.t_size.v = Val(T_co);
943 el->el_terminal.t_size.h = Val(T_li);
945 terminal_setflags(el);
947 /* get the correct window size */
948 (void) terminal_get_size(el, &lins, &cols);
949 if (terminal_change_size(el, lins, cols) == -1)
951 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
952 terminal_bind_arrow(el);
953 el->el_terminal.t_name = term;
954 return i <= 0 ? -1 : 0;
958 /* terminal_get_size():
959 * Return the new window size in lines and cols, and
960 * true if the size was changed.
963 terminal_get_size(EditLine *el, int *lins, int *cols)
972 if (ioctl(el->el_infd, TIOCGWINSZ, &ws) != -1) {
983 if (ioctl(el->el_infd, TIOCGSIZE, &ts) != -1) {
991 return Val(T_co) != *cols || Val(T_li) != *lins;
995 /* terminal_change_size():
996 * Change the size of the terminal
999 terminal_change_size(EditLine *el, int lins, int cols)
1004 Val(T_co) = (cols < 2) ? 80 : cols;
1005 Val(T_li) = (lins < 1) ? 24 : lins;
1007 /* re-make display buffers */
1008 if (terminal_rebuffer_display(el) == -1)
1010 re_clear_display(el);
1015 /* terminal_init_arrow():
1016 * Initialize the arrow key bindings from termcap
1019 terminal_init_arrow(EditLine *el)
1021 funckey_t *arrow = el->el_terminal.t_fkey;
1023 arrow[A_K_DN].name = STR("down");
1024 arrow[A_K_DN].key = T_kd;
1025 arrow[A_K_DN].fun.cmd = ED_NEXT_HISTORY;
1026 arrow[A_K_DN].type = XK_CMD;
1028 arrow[A_K_UP].name = STR("up");
1029 arrow[A_K_UP].key = T_ku;
1030 arrow[A_K_UP].fun.cmd = ED_PREV_HISTORY;
1031 arrow[A_K_UP].type = XK_CMD;
1033 arrow[A_K_LT].name = STR("left");
1034 arrow[A_K_LT].key = T_kl;
1035 arrow[A_K_LT].fun.cmd = ED_PREV_CHAR;
1036 arrow[A_K_LT].type = XK_CMD;
1038 arrow[A_K_RT].name = STR("right");
1039 arrow[A_K_RT].key = T_kr;
1040 arrow[A_K_RT].fun.cmd = ED_NEXT_CHAR;
1041 arrow[A_K_RT].type = XK_CMD;
1043 arrow[A_K_HO].name = STR("home");
1044 arrow[A_K_HO].key = T_kh;
1045 arrow[A_K_HO].fun.cmd = ED_MOVE_TO_BEG;
1046 arrow[A_K_HO].type = XK_CMD;
1048 arrow[A_K_EN].name = STR("end");
1049 arrow[A_K_EN].key = T_at7;
1050 arrow[A_K_EN].fun.cmd = ED_MOVE_TO_END;
1051 arrow[A_K_EN].type = XK_CMD;
1053 arrow[A_K_DE].name = STR("delete");
1054 arrow[A_K_DE].key = T_kD;
1055 arrow[A_K_DE].fun.cmd = ED_DELETE_NEXT_CHAR;
1056 arrow[A_K_DE].type = XK_CMD;
1060 /* terminal_reset_arrow():
1061 * Reset arrow key bindings
1064 terminal_reset_arrow(EditLine *el)
1066 funckey_t *arrow = el->el_terminal.t_fkey;
1067 static const Char strA[] = {033, '[', 'A', '\0'};
1068 static const Char strB[] = {033, '[', 'B', '\0'};
1069 static const Char strC[] = {033, '[', 'C', '\0'};
1070 static const Char strD[] = {033, '[', 'D', '\0'};
1071 static const Char strH[] = {033, '[', 'H', '\0'};
1072 static const Char strF[] = {033, '[', 'F', '\0'};
1073 static const Char stOA[] = {033, 'O', 'A', '\0'};
1074 static const Char stOB[] = {033, 'O', 'B', '\0'};
1075 static const Char stOC[] = {033, 'O', 'C', '\0'};
1076 static const Char stOD[] = {033, 'O', 'D', '\0'};
1077 static const Char stOH[] = {033, 'O', 'H', '\0'};
1078 static const Char stOF[] = {033, 'O', 'F', '\0'};
1080 keymacro_add(el, strA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1081 keymacro_add(el, strB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1082 keymacro_add(el, strC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1083 keymacro_add(el, strD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1084 keymacro_add(el, strH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1085 keymacro_add(el, strF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1086 keymacro_add(el, stOA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1087 keymacro_add(el, stOB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1088 keymacro_add(el, stOC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1089 keymacro_add(el, stOD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1090 keymacro_add(el, stOH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1091 keymacro_add(el, stOF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1093 if (el->el_map.type != MAP_VI)
1095 keymacro_add(el, &strA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1096 keymacro_add(el, &strB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1097 keymacro_add(el, &strC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1098 keymacro_add(el, &strD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1099 keymacro_add(el, &strH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1100 keymacro_add(el, &strF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1101 keymacro_add(el, &stOA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1102 keymacro_add(el, &stOB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1103 keymacro_add(el, &stOC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1104 keymacro_add(el, &stOD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1105 keymacro_add(el, &stOH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1106 keymacro_add(el, &stOF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1110 /* terminal_set_arrow():
1111 * Set an arrow key binding
1114 terminal_set_arrow(EditLine *el, const Char *name, keymacro_value_t *fun,
1117 funckey_t *arrow = el->el_terminal.t_fkey;
1120 for (i = 0; i < A_K_NKEYS; i++)
1121 if (Strcmp(name, arrow[i].name) == 0) {
1122 arrow[i].fun = *fun;
1123 arrow[i].type = type;
1130 /* terminal_clear_arrow():
1131 * Clear an arrow key binding
1134 terminal_clear_arrow(EditLine *el, const Char *name)
1136 funckey_t *arrow = el->el_terminal.t_fkey;
1139 for (i = 0; i < A_K_NKEYS; i++)
1140 if (Strcmp(name, arrow[i].name) == 0) {
1141 arrow[i].type = XK_NOD;
1148 /* terminal_print_arrow():
1149 * Print the arrow key bindings
1152 terminal_print_arrow(EditLine *el, const Char *name)
1155 funckey_t *arrow = el->el_terminal.t_fkey;
1157 for (i = 0; i < A_K_NKEYS; i++)
1158 if (*name == '\0' || Strcmp(name, arrow[i].name) == 0)
1159 if (arrow[i].type != XK_NOD)
1160 keymacro_kprint(el, arrow[i].name,
1161 &arrow[i].fun, arrow[i].type);
1165 /* terminal_bind_arrow():
1166 * Bind the arrow keys
1169 terminal_bind_arrow(EditLine *el)
1172 const el_action_t *dmap;
1175 funckey_t *arrow = el->el_terminal.t_fkey;
1177 /* Check if the components needed are initialized */
1178 if (el->el_terminal.t_buf == NULL || el->el_map.key == NULL)
1181 map = el->el_map.type == MAP_VI ? el->el_map.alt : el->el_map.key;
1182 dmap = el->el_map.type == MAP_VI ? el->el_map.vic : el->el_map.emacs;
1184 terminal_reset_arrow(el);
1186 for (i = 0; i < A_K_NKEYS; i++) {
1187 Char wt_str[VISUAL_WIDTH_MAX];
1191 p = el->el_terminal.t_str[arrow[i].key];
1194 for (n = 0; n < VISUAL_WIDTH_MAX && p[n]; ++n)
1196 while (n < VISUAL_WIDTH_MAX)
1199 j = (unsigned char) *p;
1201 * Assign the arrow keys only if:
1203 * 1. They are multi-character arrow keys and the user
1204 * has not re-assigned the leading character, or
1205 * has re-assigned the leading character to be
1206 * ED_SEQUENCE_LEAD_IN
1207 * 2. They are single arrow keys pointing to an
1210 if (arrow[i].type == XK_NOD)
1211 keymacro_clear(el, map, px);
1213 if (p[1] && (dmap[j] == map[j] ||
1214 map[j] == ED_SEQUENCE_LEAD_IN)) {
1215 keymacro_add(el, px, &arrow[i].fun,
1217 map[j] = ED_SEQUENCE_LEAD_IN;
1218 } else if (map[j] == ED_UNASSIGNED) {
1219 keymacro_clear(el, map, px);
1220 if (arrow[i].type == XK_CMD)
1221 map[j] = arrow[i].fun.cmd;
1223 keymacro_add(el, px, &arrow[i].fun,
1234 terminal_putc(int c)
1236 if (terminal_outfile == NULL)
1238 return fputc(c, terminal_outfile);
1242 terminal_tputs(EditLine *el, const char *cap, int affcnt)
1245 pthread_mutex_lock(&terminal_mutex);
1247 terminal_outfile = el->el_outfile;
1248 (void)tputs(cap, affcnt, terminal_putc);
1250 pthread_mutex_unlock(&terminal_mutex);
1254 /* terminal__putc():
1258 terminal__putc(EditLine *el, wint_t c)
1260 char buf[MB_LEN_MAX +1];
1262 if (c == (wint_t)MB_FILL_CHAR)
1264 i = ct_encode_char(buf, (size_t)MB_LEN_MAX, (Char)c);
1268 return fputs(buf, el->el_outfile);
1271 /* terminal__flush():
1275 terminal__flush(EditLine *el)
1278 (void) fflush(el->el_outfile);
1281 /* terminal_writec():
1282 * Write the given character out, in a human readable form
1285 terminal_writec(EditLine *el, wint_t c)
1287 Char visbuf[VISUAL_WIDTH_MAX +1];
1288 ssize_t vcnt = ct_visual_char(visbuf, VISUAL_WIDTH_MAX, (Char)c);
1291 visbuf[vcnt] = '\0';
1292 terminal_overwrite(el, visbuf, (size_t)vcnt);
1293 terminal__flush(el);
1297 /* terminal_telltc():
1298 * Print the current termcap characteristics
1302 terminal_telltc(EditLine *el, int argc __attribute__((__unused__)),
1303 const Char **argv __attribute__((__unused__)))
1305 const struct termcapstr *t;
1308 (void) fprintf(el->el_outfile, "\n\tYour terminal has the\n");
1309 (void) fprintf(el->el_outfile, "\tfollowing characteristics:\n\n");
1310 (void) fprintf(el->el_outfile, "\tIt has %d columns and %d lines\n",
1311 Val(T_co), Val(T_li));
1312 (void) fprintf(el->el_outfile,
1313 "\tIt has %s meta key\n", EL_HAS_META ? "a" : "no");
1314 (void) fprintf(el->el_outfile,
1315 "\tIt can%suse tabs\n", EL_CAN_TAB ? " " : "not ");
1316 (void) fprintf(el->el_outfile, "\tIt %s automatic margins\n",
1317 EL_HAS_AUTO_MARGINS ? "has" : "does not have");
1318 if (EL_HAS_AUTO_MARGINS)
1319 (void) fprintf(el->el_outfile, "\tIt %s magic margins\n",
1320 EL_HAS_MAGIC_MARGINS ? "has" : "does not have");
1322 for (t = tstr, ts = el->el_terminal.t_str; t->name != NULL; t++, ts++) {
1325 ub = ct_encode_string(ct_visual_string(
1326 ct_decode_string(*ts, &el->el_scratch)),
1331 (void) fprintf(el->el_outfile, "\t%25s (%s) == %s\n",
1332 t->long_name, t->name, ub);
1334 (void) fputc('\n', el->el_outfile);
1339 /* terminal_settc():
1340 * Change the current terminal characteristics
1344 terminal_settc(EditLine *el, int argc __attribute__((__unused__)),
1347 const struct termcapstr *ts;
1348 const struct termcapval *tv;
1349 char what[8], how[8];
1351 if (argv == NULL || argv[1] == NULL || argv[2] == NULL)
1354 strncpy(what, ct_encode_string(argv[1], &el->el_scratch), sizeof(what));
1355 what[sizeof(what) - 1] = '\0';
1356 strncpy(how, ct_encode_string(argv[2], &el->el_scratch), sizeof(how));
1357 how[sizeof(how) - 1] = '\0';
1360 * Do the strings first
1362 for (ts = tstr; ts->name != NULL; ts++)
1363 if (strcmp(ts->name, what) == 0)
1366 if (ts->name != NULL) {
1367 terminal_alloc(el, ts, how);
1368 terminal_setflags(el);
1372 * Do the numeric ones second
1374 for (tv = tval; tv->name != NULL; tv++)
1375 if (strcmp(tv->name, what) == 0)
1378 if (tv->name != NULL)
1381 if (tv == &tval[T_pt] || tv == &tval[T_km] ||
1382 tv == &tval[T_am] || tv == &tval[T_xn]) {
1383 if (strcmp(how, "yes") == 0)
1384 el->el_terminal.t_val[tv - tval] = 1;
1385 else if (strcmp(how, "no") == 0)
1386 el->el_terminal.t_val[tv - tval] = 0;
1388 (void) fprintf(el->el_errfile,
1389 "" FSTR ": Bad value `%s'.\n", argv[0], how);
1392 terminal_setflags(el);
1393 if (terminal_change_size(el, Val(T_li), Val(T_co)) == -1)
1400 i = strtol(how, &ep, 10);
1402 (void) fprintf(el->el_errfile,
1403 "" FSTR ": Bad value `%s'.\n", argv[0], how);
1406 el->el_terminal.t_val[tv - tval] = (int) i;
1407 el->el_terminal.t_size.v = Val(T_co);
1408 el->el_terminal.t_size.h = Val(T_li);
1409 if (tv == &tval[T_co] || tv == &tval[T_li])
1410 if (terminal_change_size(el, Val(T_li), Val(T_co))
1418 /* terminal_gettc():
1419 * Get the current terminal characteristics
1423 terminal_gettc(EditLine *el, int argc __attribute__((__unused__)), char **argv)
1425 const struct termcapstr *ts;
1426 const struct termcapval *tv;
1430 if (argv == NULL || argv[1] == NULL || argv[2] == NULL)
1437 * Do the strings first
1439 for (ts = tstr; ts->name != NULL; ts++)
1440 if (strcmp(ts->name, what) == 0)
1443 if (ts->name != NULL) {
1444 *(char **)how = el->el_terminal.t_str[ts - tstr];
1448 * Do the numeric ones second
1450 for (tv = tval; tv->name != NULL; tv++)
1451 if (strcmp(tv->name, what) == 0)
1454 if (tv->name == NULL)
1457 if (tv == &tval[T_pt] || tv == &tval[T_km] ||
1458 tv == &tval[T_am] || tv == &tval[T_xn]) {
1459 static char yes[] = "yes";
1460 static char no[] = "no";
1461 if (el->el_terminal.t_val[tv - tval])
1462 *(char **)how = yes;
1467 *(int *)how = el->el_terminal.t_val[tv - tval];
1472 /* terminal_echotc():
1473 * Print the termcap string out with variable substitution
1477 terminal_echotc(EditLine *el, int argc __attribute__((__unused__)),
1482 int arg_need, arg_cols, arg_rows;
1483 int verbose = 0, silent = 0;
1485 static const char fmts[] = "%s\n", fmtd[] = "%d\n";
1486 const struct termcapstr *t;
1487 char buf[TC_BUFSIZE];
1492 if (argv == NULL || argv[1] == NULL)
1496 if (argv[0][0] == '-') {
1497 switch (argv[0][1]) {
1505 /* stderror(ERR_NAME | ERR_TCUSAGE); */
1510 if (!*argv || *argv[0] == '\0')
1512 if (Strcmp(*argv, STR("tabs")) == 0) {
1513 (void) fprintf(el->el_outfile, fmts, EL_CAN_TAB ? "yes" : "no");
1515 } else if (Strcmp(*argv, STR("meta")) == 0) {
1516 (void) fprintf(el->el_outfile, fmts, Val(T_km) ? "yes" : "no");
1518 } else if (Strcmp(*argv, STR("xn")) == 0) {
1519 (void) fprintf(el->el_outfile, fmts, EL_HAS_MAGIC_MARGINS ?
1522 } else if (Strcmp(*argv, STR("am")) == 0) {
1523 (void) fprintf(el->el_outfile, fmts, EL_HAS_AUTO_MARGINS ?
1526 } else if (Strcmp(*argv, STR("baud")) == 0) {
1527 (void) fprintf(el->el_outfile, fmtd, (int)el->el_tty.t_speed);
1529 } else if (Strcmp(*argv, STR("rows")) == 0 ||
1530 Strcmp(*argv, STR("lines")) == 0) {
1531 (void) fprintf(el->el_outfile, fmtd, Val(T_li));
1533 } else if (Strcmp(*argv, STR("cols")) == 0) {
1534 (void) fprintf(el->el_outfile, fmtd, Val(T_co));
1538 * Try to use our local definition first
1541 for (t = tstr; t->name != NULL; t++)
1543 ct_encode_string(*argv, &el->el_scratch)) == 0) {
1544 scap = el->el_terminal.t_str[t - tstr];
1547 if (t->name == NULL) {
1548 /* XXX: some systems' tgetstr needs non const */
1549 scap = tgetstr(ct_encode_string(*argv, &el->el_scratch), &area);
1551 if (!scap || scap[0] == '\0') {
1553 (void) fprintf(el->el_errfile,
1554 "echotc: Termcap parameter `" FSTR "' not found.\n",
1559 * Count home many values we need for this capability.
1561 for (cap = scap, arg_need = 0; *cap; cap++)
1581 * hpux has lot's of them...
1584 (void) fprintf(el->el_errfile,
1585 "echotc: Warning: unknown termcap %% `%c'.\n",
1587 /* This is bad, but I won't complain */
1594 if (*argv && *argv[0]) {
1596 (void) fprintf(el->el_errfile,
1597 "echotc: Warning: Extra argument `" FSTR "'.\n",
1601 terminal_tputs(el, scap, 1);
1605 if (!*argv || *argv[0] == '\0') {
1607 (void) fprintf(el->el_errfile,
1608 "echotc: Warning: Missing argument.\n");
1612 i = Strtol(*argv, &ep, 10);
1613 if (*ep != '\0' || i < 0) {
1615 (void) fprintf(el->el_errfile,
1616 "echotc: Bad value `" FSTR "' for rows.\n",
1622 if (*argv && *argv[0]) {
1624 (void) fprintf(el->el_errfile,
1625 "echotc: Warning: Extra argument `" FSTR
1629 terminal_tputs(el, tgoto(scap, arg_cols, arg_rows), 1);
1632 /* This is wrong, but I will ignore it... */
1634 (void) fprintf(el->el_errfile,
1635 "echotc: Warning: Too many required arguments (%d).\n",
1640 if (!*argv || *argv[0] == '\0') {
1642 (void) fprintf(el->el_errfile,
1643 "echotc: Warning: Missing argument.\n");
1646 i = Strtol(*argv, &ep, 10);
1647 if (*ep != '\0' || i < 0) {
1649 (void) fprintf(el->el_errfile,
1650 "echotc: Bad value `" FSTR "' for cols.\n",
1656 if (!*argv || *argv[0] == '\0') {
1658 (void) fprintf(el->el_errfile,
1659 "echotc: Warning: Missing argument.\n");
1662 i = Strtol(*argv, &ep, 10);
1663 if (*ep != '\0' || i < 0) {
1665 (void) fprintf(el->el_errfile,
1666 "echotc: Bad value `" FSTR "' for rows.\n",
1673 (void) fprintf(el->el_errfile,
1674 "echotc: Bad value `" FSTR "'.\n", *argv);
1678 if (*argv && *argv[0]) {
1680 (void) fprintf(el->el_errfile,
1681 "echotc: Warning: Extra argument `" FSTR
1685 terminal_tputs(el, tgoto(scap, arg_cols, arg_rows), arg_rows);