1 /* $NetBSD: terminal.c,v 1.32 2016/05/09 21:46:56 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.32 2016/05/09 21:46:56 christos Exp $");
42 #endif /* not lint && not SCCSID */
45 * terminal.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!
49 #include <sys/types.h>
50 #include <sys/ioctl.h>
66 /* Solaris's term.h does horrid things. */
67 #if defined(HAVE_TERM_H) && !defined(__sun) && !defined(HAVE_TERMCAP_H)
79 * IMPORTANT NOTE: these routines are allowed to look at the current screen
80 * and the current position assuming that it is correct. If this is not
81 * true, then the update will be WRONG! This is (should be) a valid
85 #define TC_BUFSIZE ((size_t)2048)
87 #define GoodStr(a) (el->el_terminal.t_str[a] != NULL && \
88 el->el_terminal.t_str[a][0] != '\0')
89 #define Str(a) el->el_terminal.t_str[a]
90 #define Val(a) el->el_terminal.t_val[a]
92 static const struct termcapstr {
94 const char *long_name;
97 { "al", "add new blank line" },
99 { "bl", "audible bell" },
101 { "cd", "clear to bottom" },
103 { "ce", "clear to end of line" },
105 { "ch", "cursor to horiz pos" },
107 { "cl", "clear screen" },
109 { "dc", "delete a character" },
111 { "dl", "delete a line" },
113 { "dm", "start delete mode" },
115 { "ed", "end delete mode" },
117 { "ei", "end insert mode" },
119 { "fs", "cursor from status line" },
121 { "ho", "home cursor" },
123 { "ic", "insert character" },
125 { "im", "start insert mode" },
127 { "ip", "insert padding" },
129 { "kd", "sends cursor down" },
131 { "kl", "sends cursor left" },
133 { "kr", "sends cursor right" },
135 { "ku", "sends cursor up" },
137 { "md", "begin bold" },
139 { "me", "end attributes" },
141 { "nd", "non destructive space" },
143 { "se", "end standout" },
145 { "so", "begin standout" },
147 { "ts", "cursor to status line" },
149 { "up", "cursor up one" },
151 { "us", "begin underline" },
153 { "ue", "end underline" },
155 { "vb", "visible bell" },
157 { "DC", "delete multiple chars" },
159 { "DO", "cursor down multiple" },
161 { "IC", "insert multiple chars" },
163 { "LE", "cursor left multiple" },
165 { "RI", "cursor right multiple" },
167 { "UP", "cursor up multiple" },
169 { "kh", "send cursor home" },
171 { "@7", "send cursor end" },
173 { "kD", "send cursor delete" },
178 static const struct termcapval {
180 const char *long_name;
183 { "am", "has automatic margins" },
185 { "pt", "has physical tabs" },
187 { "li", "Number of lines" },
189 { "co", "Number of columns" },
191 { "km", "Has meta key" },
193 { "xt", "Tab chars destructive" },
195 { "xn", "newline ignored at right margin" },
197 { "MT", "Has meta key" }, /* XXX? */
201 /* do two or more of the attributes use me */
203 static void terminal_setflags(EditLine *);
204 static int terminal_rebuffer_display(EditLine *);
205 static void terminal_free_display(EditLine *);
206 static int terminal_alloc_display(EditLine *);
207 static void terminal_alloc(EditLine *, const struct termcapstr *,
209 static void terminal_init_arrow(EditLine *);
210 static void terminal_reset_arrow(EditLine *);
211 static int terminal_putc(int);
212 static void terminal_tputs(EditLine *, const char *, int);
215 static pthread_mutex_t terminal_mutex = PTHREAD_MUTEX_INITIALIZER;
217 static FILE *terminal_outfile = NULL;
220 /* terminal_setflags():
221 * Set the terminal capability flags
224 terminal_setflags(EditLine *el)
227 if (el->el_tty.t_tabs)
228 EL_FLAGS |= (Val(T_pt) && !Val(T_xt)) ? TERM_CAN_TAB : 0;
230 EL_FLAGS |= (Val(T_km) || Val(T_MT)) ? TERM_HAS_META : 0;
231 EL_FLAGS |= GoodStr(T_ce) ? TERM_CAN_CEOL : 0;
232 EL_FLAGS |= (GoodStr(T_dc) || GoodStr(T_DC)) ? TERM_CAN_DELETE : 0;
233 EL_FLAGS |= (GoodStr(T_im) || GoodStr(T_ic) || GoodStr(T_IC)) ?
235 EL_FLAGS |= (GoodStr(T_up) || GoodStr(T_UP)) ? TERM_CAN_UP : 0;
236 EL_FLAGS |= Val(T_am) ? TERM_HAS_AUTO_MARGINS : 0;
237 EL_FLAGS |= Val(T_xn) ? TERM_HAS_MAGIC_MARGINS : 0;
239 if (GoodStr(T_me) && GoodStr(T_ue))
240 EL_FLAGS |= (strcmp(Str(T_me), Str(T_ue)) == 0) ?
243 EL_FLAGS &= ~TERM_CAN_ME;
244 if (GoodStr(T_me) && GoodStr(T_se))
245 EL_FLAGS |= (strcmp(Str(T_me), Str(T_se)) == 0) ?
251 (void) fprintf(el->el_errfile,
252 "WARNING: Your terminal cannot move up.\n");
253 (void) fprintf(el->el_errfile,
254 "Editing may be odd for long lines.\n");
257 (void) fprintf(el->el_errfile, "no clear EOL capability.\n");
259 (void) fprintf(el->el_errfile, "no delete char capability.\n");
261 (void) fprintf(el->el_errfile, "no insert char capability.\n");
262 #endif /* DEBUG_SCREEN */
266 * Initialize the terminal stuff
269 terminal_init(EditLine *el)
272 el->el_terminal.t_buf = el_malloc(TC_BUFSIZE *
273 sizeof(*el->el_terminal.t_buf));
274 if (el->el_terminal.t_buf == NULL)
276 el->el_terminal.t_cap = el_malloc(TC_BUFSIZE *
277 sizeof(*el->el_terminal.t_cap));
278 if (el->el_terminal.t_cap == NULL)
280 el->el_terminal.t_fkey = el_malloc(A_K_NKEYS *
281 sizeof(*el->el_terminal.t_fkey));
282 if (el->el_terminal.t_fkey == NULL)
284 el->el_terminal.t_loc = 0;
285 el->el_terminal.t_str = el_malloc(T_str *
286 sizeof(*el->el_terminal.t_str));
287 if (el->el_terminal.t_str == NULL)
289 (void) memset(el->el_terminal.t_str, 0, T_str *
290 sizeof(*el->el_terminal.t_str));
291 el->el_terminal.t_val = el_malloc(T_val *
292 sizeof(*el->el_terminal.t_val));
293 if (el->el_terminal.t_val == NULL)
295 (void) memset(el->el_terminal.t_val, 0, T_val *
296 sizeof(*el->el_terminal.t_val));
297 (void) terminal_set(el, NULL);
298 terminal_init_arrow(el);
301 free(el->el_terminal.t_str);
302 el->el_terminal.t_str = NULL;
304 free(el->el_terminal.t_fkey);
305 el->el_terminal.t_fkey = NULL;
307 free(el->el_terminal.t_cap);
308 el->el_terminal.t_cap = NULL;
310 free(el->el_terminal.t_buf);
311 el->el_terminal.t_buf = NULL;
317 * Clean up the terminal stuff
320 terminal_end(EditLine *el)
323 el_free(el->el_terminal.t_buf);
324 el->el_terminal.t_buf = NULL;
325 el_free(el->el_terminal.t_cap);
326 el->el_terminal.t_cap = NULL;
327 el->el_terminal.t_loc = 0;
328 el_free(el->el_terminal.t_str);
329 el->el_terminal.t_str = NULL;
330 el_free(el->el_terminal.t_val);
331 el->el_terminal.t_val = NULL;
332 el_free(el->el_terminal.t_fkey);
333 el->el_terminal.t_fkey = NULL;
334 terminal_free_display(el);
339 * Maintain a string pool for termcap strings
342 terminal_alloc(EditLine *el, const struct termcapstr *t, const char *cap)
344 char termbuf[TC_BUFSIZE];
346 char **tlist = el->el_terminal.t_str;
347 char **tmp, **str = &tlist[t - tstr];
349 (void) memset(termbuf, 0, sizeof(termbuf));
350 if (cap == NULL || *cap == '\0') {
356 tlen = *str == NULL ? 0 : strlen(*str);
359 * New string is shorter; no need to allocate space
363 (void) strcpy(*str, cap); /* XXX strcpy is safe */
367 * New string is longer; see if we have enough space to append
369 if (el->el_terminal.t_loc + 3 < TC_BUFSIZE) {
370 /* XXX strcpy is safe */
371 (void) strcpy(*str = &el->el_terminal.t_buf[
372 el->el_terminal.t_loc], cap);
373 el->el_terminal.t_loc += clen + 1; /* one for \0 */
377 * Compact our buffer; no need to check compaction, cause we know it
381 for (tmp = tlist; tmp < &tlist[T_str]; tmp++)
382 if (*tmp != NULL && **tmp != '\0' && *tmp != *str) {
385 for (ptr = *tmp; *ptr != '\0'; termbuf[tlen++] = *ptr++)
387 termbuf[tlen++] = '\0';
389 memcpy(el->el_terminal.t_buf, termbuf, TC_BUFSIZE);
390 el->el_terminal.t_loc = tlen;
391 if (el->el_terminal.t_loc + 3 >= TC_BUFSIZE) {
392 (void) fprintf(el->el_errfile,
393 "Out of termcap string space.\n");
396 /* XXX strcpy is safe */
397 (void) strcpy(*str = &el->el_terminal.t_buf[el->el_terminal.t_loc],
399 el->el_terminal.t_loc += (size_t)clen + 1; /* one for \0 */
404 /* terminal_rebuffer_display():
405 * Rebuffer the display after the screen changed size
408 terminal_rebuffer_display(EditLine *el)
410 coord_t *c = &el->el_terminal.t_size;
412 terminal_free_display(el);
417 if (terminal_alloc_display(el) == -1)
423 /* terminal_alloc_display():
424 * Allocate a new display.
427 terminal_alloc_display(EditLine *el)
431 coord_t *c = &el->el_terminal.t_size;
433 b = el_malloc(sizeof(*b) * (size_t)(c->v + 1));
436 for (i = 0; i < c->v; i++) {
437 b[i] = el_malloc(sizeof(**b) * (size_t)(c->h + 1));
448 b = el_malloc(sizeof(*b) * (size_t)(c->v + 1));
451 for (i = 0; i < c->v; i++) {
452 b[i] = el_malloc(sizeof(**b) * (size_t)(c->h + 1));
464 terminal_free_display(el);
469 /* terminal_free_display():
470 * Free the display buffers
473 terminal_free_display(EditLine *el)
479 el->el_display = NULL;
481 for (bufp = b; *bufp != NULL; bufp++)
486 el->el_vdisplay = NULL;
488 for (bufp = b; *bufp != NULL; bufp++)
495 /* terminal_move_to_line():
496 * move to line <where> (first line == 0)
497 * as efficiently as possible
500 terminal_move_to_line(EditLine *el, int where)
504 if (where == el->el_cursor.v)
507 if (where > el->el_terminal.t_size.v) {
509 (void) fprintf(el->el_errfile,
510 "%s: where is ridiculous: %d\r\n", __func__, where);
511 #endif /* DEBUG_SCREEN */
514 if ((del = where - el->el_cursor.v) > 0) {
516 if (EL_HAS_AUTO_MARGINS &&
517 el->el_display[el->el_cursor.v][0] != '\0') {
519 (el->el_terminal.t_size.h - 1);
521 el->el_display[el->el_cursor.v][h] ==
525 /* move without newline */
526 terminal_move_to_char(el, (int)h);
527 terminal_overwrite(el, &el->el_display
528 [el->el_cursor.v][el->el_cursor.h],
529 (size_t)(el->el_terminal.t_size.h -
534 if ((del > 1) && GoodStr(T_DO)) {
535 terminal_tputs(el, tgoto(Str(T_DO), del,
539 for (; del > 0; del--)
540 terminal__putc(el, '\n');
541 /* because the \n will become \r\n */
546 } else { /* del < 0 */
547 if (GoodStr(T_UP) && (-del > 1 || !GoodStr(T_up)))
548 terminal_tputs(el, tgoto(Str(T_UP), -del, -del), -del);
551 for (; del < 0; del++)
552 terminal_tputs(el, Str(T_up), 1);
555 el->el_cursor.v = where;/* now where is here */
559 /* terminal_move_to_char():
560 * Move to the character position specified
563 terminal_move_to_char(EditLine *el, int where)
568 if (where == el->el_cursor.h)
571 if (where > el->el_terminal.t_size.h) {
573 (void) fprintf(el->el_errfile,
574 "%s: where is ridiculous: %d\r\n", __func__, where);
575 #endif /* DEBUG_SCREEN */
578 if (!where) { /* if where is first column */
579 terminal__putc(el, '\r'); /* do a CR */
583 del = where - el->el_cursor.h;
585 if ((del < -4 || del > 4) && GoodStr(T_ch))
586 /* go there directly */
587 terminal_tputs(el, tgoto(Str(T_ch), where, where), where);
589 if (del > 0) { /* moving forward */
590 if ((del > 4) && GoodStr(T_RI))
591 terminal_tputs(el, tgoto(Str(T_RI), del, del),
594 /* if I can do tabs, use them */
596 if ((el->el_cursor.h & 0370) !=
599 el->el_cursor.v][where & 0370] !=
602 /* if not within tab stop */
604 (el->el_cursor.h & 0370);
610 el->el_cursor.h = where & ~0x7;
614 * it's usually cheaper to just write the
618 * NOTE THAT terminal_overwrite() WILL CHANGE
621 terminal_overwrite(el, &el->el_display[
622 el->el_cursor.v][el->el_cursor.h],
623 (size_t)(where - el->el_cursor.h));
626 } else { /* del < 0 := moving backward */
627 if ((-del > 4) && GoodStr(T_LE))
628 terminal_tputs(el, tgoto(Str(T_LE), -del, -del),
630 else { /* can't go directly there */
632 * if the "cost" is greater than the "cost"
636 ((unsigned int)-del >
637 (((unsigned int) where >> 3) +
640 terminal__putc(el, '\r');/* do a CR */
642 goto mc_again; /* and try again */
644 for (i = 0; i < -del; i++)
645 terminal__putc(el, '\b');
649 el->el_cursor.h = where; /* now where is here */
653 /* terminal_overwrite():
654 * Overstrike num characters
655 * Assumes MB_FILL_CHARs are present to keep the column count correct
658 terminal_overwrite(EditLine *el, const wchar_t *cp, size_t n)
663 if (n > (size_t)el->el_terminal.t_size.h) {
665 (void) fprintf(el->el_errfile,
666 "%s: n is ridiculous: %zu\r\n", __func__, n);
667 #endif /* DEBUG_SCREEN */
672 /* terminal__putc() ignores any MB_FILL_CHARs */
673 terminal__putc(el, *cp++);
677 if (el->el_cursor.h >= el->el_terminal.t_size.h) { /* wrap? */
678 if (EL_HAS_AUTO_MARGINS) { /* yes */
681 if (EL_HAS_MAGIC_MARGINS) {
682 /* force the wrap to avoid the "magic"
685 if ((c = el->el_display[el->el_cursor.v]
686 [el->el_cursor.h]) != '\0') {
687 terminal_overwrite(el, &c, (size_t)1);
688 while (el->el_display[el->el_cursor.v]
689 [el->el_cursor.h] == MB_FILL_CHAR)
692 terminal__putc(el, ' ');
696 } else /* no wrap, but cursor stays on screen */
697 el->el_cursor.h = el->el_terminal.t_size.h - 1;
702 /* terminal_deletechars():
703 * Delete num characters
706 terminal_deletechars(EditLine *el, int num)
711 if (!EL_CAN_DELETE) {
713 (void) fprintf(el->el_errfile, " ERROR: cannot delete \n");
714 #endif /* DEBUG_EDIT */
717 if (num > el->el_terminal.t_size.h) {
719 (void) fprintf(el->el_errfile,
720 "%s: num is ridiculous: %d\r\n", __func__, num);
721 #endif /* DEBUG_SCREEN */
724 if (GoodStr(T_DC)) /* if I have multiple delete */
725 if ((num > 1) || !GoodStr(T_dc)) { /* if dc would be more
727 terminal_tputs(el, tgoto(Str(T_DC), num, num), num);
730 if (GoodStr(T_dm)) /* if I have delete mode */
731 terminal_tputs(el, Str(T_dm), 1);
733 if (GoodStr(T_dc)) /* else do one at a time */
735 terminal_tputs(el, Str(T_dc), 1);
737 if (GoodStr(T_ed)) /* if I have delete mode */
738 terminal_tputs(el, Str(T_ed), 1);
742 /* terminal_insertwrite():
743 * Puts terminal in insert character mode or inserts num
744 * characters in the line
745 * Assumes MB_FILL_CHARs are present to keep column count correct
748 terminal_insertwrite(EditLine *el, wchar_t *cp, int num)
752 if (!EL_CAN_INSERT) {
754 (void) fprintf(el->el_errfile, " ERROR: cannot insert \n");
755 #endif /* DEBUG_EDIT */
758 if (num > el->el_terminal.t_size.h) {
760 (void) fprintf(el->el_errfile,
761 "%s: num is ridiculous: %d\r\n", __func__, num);
762 #endif /* DEBUG_SCREEN */
765 if (GoodStr(T_IC)) /* if I have multiple insert */
766 if ((num > 1) || !GoodStr(T_ic)) {
767 /* if ic would be more expensive */
768 terminal_tputs(el, tgoto(Str(T_IC), num, num), num);
769 terminal_overwrite(el, cp, (size_t)num);
770 /* this updates el_cursor.h */
773 if (GoodStr(T_im) && GoodStr(T_ei)) { /* if I have insert mode */
774 terminal_tputs(el, Str(T_im), 1);
776 el->el_cursor.h += num;
778 terminal__putc(el, *cp++);
781 if (GoodStr(T_ip)) /* have to make num chars insert */
782 terminal_tputs(el, Str(T_ip), 1);
784 terminal_tputs(el, Str(T_ei), 1);
788 if (GoodStr(T_ic)) /* have to make num chars insert */
789 terminal_tputs(el, Str(T_ic), 1);
791 terminal__putc(el, *cp++);
795 if (GoodStr(T_ip)) /* have to make num chars insert */
796 terminal_tputs(el, Str(T_ip), 1);
797 /* pad the inserted char */
803 /* terminal_clear_EOL():
804 * clear to end of line. There are num characters to clear
807 terminal_clear_EOL(EditLine *el, int num)
811 if (EL_CAN_CEOL && GoodStr(T_ce))
812 terminal_tputs(el, Str(T_ce), 1);
814 for (i = 0; i < num; i++)
815 terminal__putc(el, ' ');
816 el->el_cursor.h += num; /* have written num spaces */
821 /* terminal_clear_screen():
825 terminal_clear_screen(EditLine *el)
826 { /* clear the whole screen and home */
829 /* send the clear screen code */
830 terminal_tputs(el, Str(T_cl), Val(T_li));
831 else if (GoodStr(T_ho) && GoodStr(T_cd)) {
832 terminal_tputs(el, Str(T_ho), Val(T_li)); /* home */
833 /* clear to bottom of screen */
834 terminal_tputs(el, Str(T_cd), Val(T_li));
836 terminal__putc(el, '\r');
837 terminal__putc(el, '\n');
843 * Beep the way the terminal wants us
846 terminal_beep(EditLine *el)
849 /* what termcap says we should use */
850 terminal_tputs(el, Str(T_bl), 1);
852 terminal__putc(el, '\007'); /* an ASCII bell; ^G */
857 terminal_get(EditLine *el, const char **term)
859 *term = el->el_terminal.t_name;
864 * Read in the terminal capabilities from the requested terminal
867 terminal_set(EditLine *el, const char *term)
870 char buf[TC_BUFSIZE];
872 const struct termcapstr *t;
876 (void) sigemptyset(&nset);
877 (void) sigaddset(&nset, SIGWINCH);
878 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
884 term = getenv("TERM");
886 if (!term || !term[0])
889 if (strcmp(term, "emacs") == 0)
890 el->el_flags |= EDIT_DISABLED;
892 (void) memset(el->el_terminal.t_cap, 0, TC_BUFSIZE);
894 i = tgetent(el->el_terminal.t_cap, term);
898 (void) fprintf(el->el_errfile,
899 "Cannot read termcap database;\n");
901 (void) fprintf(el->el_errfile,
902 "No entry for terminal type \"%s\";\n", term);
903 (void) fprintf(el->el_errfile,
904 "using dumb terminal settings.\n");
905 Val(T_co) = 80; /* do a dumb terminal */
906 Val(T_pt) = Val(T_km) = Val(T_li) = 0;
907 Val(T_xt) = Val(T_MT);
908 for (t = tstr; t->name != NULL; t++)
909 terminal_alloc(el, t, NULL);
911 /* auto/magic margins */
912 Val(T_am) = tgetflag("am");
913 Val(T_xn) = tgetflag("xn");
915 Val(T_pt) = tgetflag("pt");
916 Val(T_xt) = tgetflag("xt");
917 /* do we have a meta? */
918 Val(T_km) = tgetflag("km");
919 Val(T_MT) = tgetflag("MT");
921 Val(T_co) = tgetnum("co");
922 Val(T_li) = tgetnum("li");
923 for (t = tstr; t->name != NULL; t++) {
924 /* XXX: some systems' tgetstr needs non const */
925 terminal_alloc(el, t, tgetstr(strchr(t->name, *t->name),
931 Val(T_co) = 80; /* just in case */
935 el->el_terminal.t_size.v = Val(T_co);
936 el->el_terminal.t_size.h = Val(T_li);
938 terminal_setflags(el);
940 /* get the correct window size */
941 (void) terminal_get_size(el, &lins, &cols);
942 if (terminal_change_size(el, lins, cols) == -1)
944 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
945 terminal_bind_arrow(el);
946 el->el_terminal.t_name = term;
947 return i <= 0 ? -1 : 0;
951 /* terminal_get_size():
952 * Return the new window size in lines and cols, and
953 * true if the size was changed.
956 terminal_get_size(EditLine *el, int *lins, int *cols)
965 if (ioctl(el->el_infd, TIOCGWINSZ, &ws) != -1) {
976 if (ioctl(el->el_infd, TIOCGSIZE, &ts) != -1) {
984 return Val(T_co) != *cols || Val(T_li) != *lins;
988 /* terminal_change_size():
989 * Change the size of the terminal
992 terminal_change_size(EditLine *el, int lins, int cols)
997 Val(T_co) = (cols < 2) ? 80 : cols;
998 Val(T_li) = (lins < 1) ? 24 : lins;
1000 /* re-make display buffers */
1001 if (terminal_rebuffer_display(el) == -1)
1003 re_clear_display(el);
1008 /* terminal_init_arrow():
1009 * Initialize the arrow key bindings from termcap
1012 terminal_init_arrow(EditLine *el)
1014 funckey_t *arrow = el->el_terminal.t_fkey;
1016 arrow[A_K_DN].name = L"down";
1017 arrow[A_K_DN].key = T_kd;
1018 arrow[A_K_DN].fun.cmd = ED_NEXT_HISTORY;
1019 arrow[A_K_DN].type = XK_CMD;
1021 arrow[A_K_UP].name = L"up";
1022 arrow[A_K_UP].key = T_ku;
1023 arrow[A_K_UP].fun.cmd = ED_PREV_HISTORY;
1024 arrow[A_K_UP].type = XK_CMD;
1026 arrow[A_K_LT].name = L"left";
1027 arrow[A_K_LT].key = T_kl;
1028 arrow[A_K_LT].fun.cmd = ED_PREV_CHAR;
1029 arrow[A_K_LT].type = XK_CMD;
1031 arrow[A_K_RT].name = L"right";
1032 arrow[A_K_RT].key = T_kr;
1033 arrow[A_K_RT].fun.cmd = ED_NEXT_CHAR;
1034 arrow[A_K_RT].type = XK_CMD;
1036 arrow[A_K_HO].name = L"home";
1037 arrow[A_K_HO].key = T_kh;
1038 arrow[A_K_HO].fun.cmd = ED_MOVE_TO_BEG;
1039 arrow[A_K_HO].type = XK_CMD;
1041 arrow[A_K_EN].name = L"end";
1042 arrow[A_K_EN].key = T_at7;
1043 arrow[A_K_EN].fun.cmd = ED_MOVE_TO_END;
1044 arrow[A_K_EN].type = XK_CMD;
1046 arrow[A_K_DE].name = L"delete";
1047 arrow[A_K_DE].key = T_kD;
1048 arrow[A_K_DE].fun.cmd = ED_DELETE_NEXT_CHAR;
1049 arrow[A_K_DE].type = XK_CMD;
1053 /* terminal_reset_arrow():
1054 * Reset arrow key bindings
1057 terminal_reset_arrow(EditLine *el)
1059 funckey_t *arrow = el->el_terminal.t_fkey;
1060 static const wchar_t strA[] = L"\033[A";
1061 static const wchar_t strB[] = L"\033[B";
1062 static const wchar_t strC[] = L"\033[C";
1063 static const wchar_t strD[] = L"\033[D";
1064 static const wchar_t strH[] = L"\033[H";
1065 static const wchar_t strF[] = L"\033[F";
1066 static const wchar_t stOA[] = L"\033OA";
1067 static const wchar_t stOB[] = L"\033OB";
1068 static const wchar_t stOC[] = L"\033OC";
1069 static const wchar_t stOD[] = L"\033OD";
1070 static const wchar_t stOH[] = L"\033OH";
1071 static const wchar_t stOF[] = L"\033OF";
1073 keymacro_add(el, strA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1074 keymacro_add(el, strB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1075 keymacro_add(el, strC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1076 keymacro_add(el, strD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1077 keymacro_add(el, strH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1078 keymacro_add(el, strF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1079 keymacro_add(el, stOA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1080 keymacro_add(el, stOB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1081 keymacro_add(el, stOC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1082 keymacro_add(el, stOD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1083 keymacro_add(el, stOH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1084 keymacro_add(el, stOF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1086 if (el->el_map.type != MAP_VI)
1088 keymacro_add(el, &strA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1089 keymacro_add(el, &strB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1090 keymacro_add(el, &strC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1091 keymacro_add(el, &strD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1092 keymacro_add(el, &strH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1093 keymacro_add(el, &strF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1094 keymacro_add(el, &stOA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1095 keymacro_add(el, &stOB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1096 keymacro_add(el, &stOC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1097 keymacro_add(el, &stOD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1098 keymacro_add(el, &stOH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1099 keymacro_add(el, &stOF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1103 /* terminal_set_arrow():
1104 * Set an arrow key binding
1107 terminal_set_arrow(EditLine *el, const wchar_t *name, keymacro_value_t *fun,
1110 funckey_t *arrow = el->el_terminal.t_fkey;
1113 for (i = 0; i < A_K_NKEYS; i++)
1114 if (wcscmp(name, arrow[i].name) == 0) {
1115 arrow[i].fun = *fun;
1116 arrow[i].type = type;
1123 /* terminal_clear_arrow():
1124 * Clear an arrow key binding
1127 terminal_clear_arrow(EditLine *el, const wchar_t *name)
1129 funckey_t *arrow = el->el_terminal.t_fkey;
1132 for (i = 0; i < A_K_NKEYS; i++)
1133 if (wcscmp(name, arrow[i].name) == 0) {
1134 arrow[i].type = XK_NOD;
1141 /* terminal_print_arrow():
1142 * Print the arrow key bindings
1144 libedit_private void
1145 terminal_print_arrow(EditLine *el, const wchar_t *name)
1148 funckey_t *arrow = el->el_terminal.t_fkey;
1150 for (i = 0; i < A_K_NKEYS; i++)
1151 if (*name == '\0' || wcscmp(name, arrow[i].name) == 0)
1152 if (arrow[i].type != XK_NOD)
1153 keymacro_kprint(el, arrow[i].name,
1154 &arrow[i].fun, arrow[i].type);
1158 /* terminal_bind_arrow():
1159 * Bind the arrow keys
1161 libedit_private void
1162 terminal_bind_arrow(EditLine *el)
1165 const el_action_t *dmap;
1168 funckey_t *arrow = el->el_terminal.t_fkey;
1170 /* Check if the components needed are initialized */
1171 if (el->el_terminal.t_buf == NULL || el->el_map.key == NULL)
1174 map = el->el_map.type == MAP_VI ? el->el_map.alt : el->el_map.key;
1175 dmap = el->el_map.type == MAP_VI ? el->el_map.vic : el->el_map.emacs;
1177 terminal_reset_arrow(el);
1179 for (i = 0; i < A_K_NKEYS; i++) {
1180 wchar_t wt_str[VISUAL_WIDTH_MAX];
1184 p = el->el_terminal.t_str[arrow[i].key];
1187 for (n = 0; n < VISUAL_WIDTH_MAX && p[n]; ++n)
1189 while (n < VISUAL_WIDTH_MAX)
1192 j = (unsigned char) *p;
1194 * Assign the arrow keys only if:
1196 * 1. They are multi-character arrow keys and the user
1197 * has not re-assigned the leading character, or
1198 * has re-assigned the leading character to be
1199 * ED_SEQUENCE_LEAD_IN
1200 * 2. They are single arrow keys pointing to an
1203 if (arrow[i].type == XK_NOD)
1204 keymacro_clear(el, map, px);
1206 if (p[1] && (dmap[j] == map[j] ||
1207 map[j] == ED_SEQUENCE_LEAD_IN)) {
1208 keymacro_add(el, px, &arrow[i].fun,
1210 map[j] = ED_SEQUENCE_LEAD_IN;
1211 } else if (map[j] == ED_UNASSIGNED) {
1212 keymacro_clear(el, map, px);
1213 if (arrow[i].type == XK_CMD)
1214 map[j] = arrow[i].fun.cmd;
1216 keymacro_add(el, px, &arrow[i].fun,
1227 terminal_putc(int c)
1229 if (terminal_outfile == NULL)
1231 return fputc(c, terminal_outfile);
1235 terminal_tputs(EditLine *el, const char *cap, int affcnt)
1238 pthread_mutex_lock(&terminal_mutex);
1240 terminal_outfile = el->el_outfile;
1241 (void)tputs(cap, affcnt, terminal_putc);
1243 pthread_mutex_unlock(&terminal_mutex);
1247 /* terminal__putc():
1251 terminal__putc(EditLine *el, wint_t c)
1253 char buf[MB_LEN_MAX +1];
1255 if (c == (wint_t)MB_FILL_CHAR)
1257 i = ct_encode_char(buf, (size_t)MB_LEN_MAX, c);
1261 return fputs(buf, el->el_outfile);
1264 /* terminal__flush():
1267 libedit_private void
1268 terminal__flush(EditLine *el)
1271 (void) fflush(el->el_outfile);
1274 /* terminal_writec():
1275 * Write the given character out, in a human readable form
1277 libedit_private void
1278 terminal_writec(EditLine *el, wint_t c)
1280 wchar_t visbuf[VISUAL_WIDTH_MAX +1];
1281 ssize_t vcnt = ct_visual_char(visbuf, VISUAL_WIDTH_MAX, c);
1284 visbuf[vcnt] = '\0';
1285 terminal_overwrite(el, visbuf, (size_t)vcnt);
1286 terminal__flush(el);
1290 /* terminal_telltc():
1291 * Print the current termcap characteristics
1295 terminal_telltc(EditLine *el, int argc __attribute__((__unused__)),
1296 const wchar_t **argv __attribute__((__unused__)))
1298 const struct termcapstr *t;
1301 (void) fprintf(el->el_outfile, "\n\tYour terminal has the\n");
1302 (void) fprintf(el->el_outfile, "\tfollowing characteristics:\n\n");
1303 (void) fprintf(el->el_outfile, "\tIt has %d columns and %d lines\n",
1304 Val(T_co), Val(T_li));
1305 (void) fprintf(el->el_outfile,
1306 "\tIt has %s meta key\n", EL_HAS_META ? "a" : "no");
1307 (void) fprintf(el->el_outfile,
1308 "\tIt can%suse tabs\n", EL_CAN_TAB ? " " : "not ");
1309 (void) fprintf(el->el_outfile, "\tIt %s automatic margins\n",
1310 EL_HAS_AUTO_MARGINS ? "has" : "does not have");
1311 if (EL_HAS_AUTO_MARGINS)
1312 (void) fprintf(el->el_outfile, "\tIt %s magic margins\n",
1313 EL_HAS_MAGIC_MARGINS ? "has" : "does not have");
1315 for (t = tstr, ts = el->el_terminal.t_str; t->name != NULL; t++, ts++) {
1318 ub = ct_encode_string(ct_visual_string(
1319 ct_decode_string(*ts, &el->el_scratch),
1320 &el->el_visual), &el->el_scratch);
1324 (void) fprintf(el->el_outfile, "\t%25s (%s) == %s\n",
1325 t->long_name, t->name, ub);
1327 (void) fputc('\n', el->el_outfile);
1332 /* terminal_settc():
1333 * Change the current terminal characteristics
1337 terminal_settc(EditLine *el, int argc __attribute__((__unused__)),
1338 const wchar_t **argv)
1340 const struct termcapstr *ts;
1341 const struct termcapval *tv;
1342 char what[8], how[8];
1344 if (argv == NULL || argv[1] == NULL || argv[2] == NULL)
1347 strncpy(what, ct_encode_string(argv[1], &el->el_scratch), sizeof(what));
1348 what[sizeof(what) - 1] = '\0';
1349 strncpy(how, ct_encode_string(argv[2], &el->el_scratch), sizeof(how));
1350 how[sizeof(how) - 1] = '\0';
1353 * Do the strings first
1355 for (ts = tstr; ts->name != NULL; ts++)
1356 if (strcmp(ts->name, what) == 0)
1359 if (ts->name != NULL) {
1360 terminal_alloc(el, ts, how);
1361 terminal_setflags(el);
1365 * Do the numeric ones second
1367 for (tv = tval; tv->name != NULL; tv++)
1368 if (strcmp(tv->name, what) == 0)
1371 if (tv->name != NULL)
1374 if (tv == &tval[T_pt] || tv == &tval[T_km] ||
1375 tv == &tval[T_am] || tv == &tval[T_xn]) {
1376 if (strcmp(how, "yes") == 0)
1377 el->el_terminal.t_val[tv - tval] = 1;
1378 else if (strcmp(how, "no") == 0)
1379 el->el_terminal.t_val[tv - tval] = 0;
1381 (void) fprintf(el->el_errfile,
1382 "%ls: Bad value `%s'.\n", argv[0], how);
1385 terminal_setflags(el);
1386 if (terminal_change_size(el, Val(T_li), Val(T_co)) == -1)
1393 i = strtol(how, &ep, 10);
1395 (void) fprintf(el->el_errfile,
1396 "%ls: Bad value `%s'.\n", argv[0], how);
1399 el->el_terminal.t_val[tv - tval] = (int) i;
1400 el->el_terminal.t_size.v = Val(T_co);
1401 el->el_terminal.t_size.h = Val(T_li);
1402 if (tv == &tval[T_co] || tv == &tval[T_li])
1403 if (terminal_change_size(el, Val(T_li), Val(T_co))
1411 /* terminal_gettc():
1412 * Get the current terminal characteristics
1416 terminal_gettc(EditLine *el, int argc __attribute__((__unused__)), char **argv)
1418 const struct termcapstr *ts;
1419 const struct termcapval *tv;
1423 if (argv == NULL || argv[1] == NULL || argv[2] == NULL)
1430 * Do the strings first
1432 for (ts = tstr; ts->name != NULL; ts++)
1433 if (strcmp(ts->name, what) == 0)
1436 if (ts->name != NULL) {
1437 *(char **)how = el->el_terminal.t_str[ts - tstr];
1441 * Do the numeric ones second
1443 for (tv = tval; tv->name != NULL; tv++)
1444 if (strcmp(tv->name, what) == 0)
1447 if (tv->name == NULL)
1450 if (tv == &tval[T_pt] || tv == &tval[T_km] ||
1451 tv == &tval[T_am] || tv == &tval[T_xn]) {
1452 static char yes[] = "yes";
1453 static char no[] = "no";
1454 if (el->el_terminal.t_val[tv - tval])
1455 *(char **)how = yes;
1460 *(int *)how = el->el_terminal.t_val[tv - tval];
1465 /* terminal_echotc():
1466 * Print the termcap string out with variable substitution
1470 terminal_echotc(EditLine *el, int argc __attribute__((__unused__)),
1471 const wchar_t **argv)
1475 int arg_need, arg_cols, arg_rows;
1476 int verbose = 0, silent = 0;
1478 static const char fmts[] = "%s\n", fmtd[] = "%d\n";
1479 const struct termcapstr *t;
1480 char buf[TC_BUFSIZE];
1485 if (argv == NULL || argv[1] == NULL)
1489 if (argv[0][0] == '-') {
1490 switch (argv[0][1]) {
1498 /* stderror(ERR_NAME | ERR_TCUSAGE); */
1503 if (!*argv || *argv[0] == '\0')
1505 if (wcscmp(*argv, L"tabs") == 0) {
1506 (void) fprintf(el->el_outfile, fmts, EL_CAN_TAB ? "yes" : "no");
1508 } else if (wcscmp(*argv, L"meta") == 0) {
1509 (void) fprintf(el->el_outfile, fmts, Val(T_km) ? "yes" : "no");
1511 } else if (wcscmp(*argv, L"xn") == 0) {
1512 (void) fprintf(el->el_outfile, fmts, EL_HAS_MAGIC_MARGINS ?
1515 } else if (wcscmp(*argv, L"am") == 0) {
1516 (void) fprintf(el->el_outfile, fmts, EL_HAS_AUTO_MARGINS ?
1519 } else if (wcscmp(*argv, L"baud") == 0) {
1520 (void) fprintf(el->el_outfile, fmtd, (int)el->el_tty.t_speed);
1522 } else if (wcscmp(*argv, L"rows") == 0 ||
1523 wcscmp(*argv, L"lines") == 0) {
1524 (void) fprintf(el->el_outfile, fmtd, Val(T_li));
1526 } else if (wcscmp(*argv, L"cols") == 0) {
1527 (void) fprintf(el->el_outfile, fmtd, Val(T_co));
1531 * Try to use our local definition first
1534 for (t = tstr; t->name != NULL; t++)
1536 ct_encode_string(*argv, &el->el_scratch)) == 0) {
1537 scap = el->el_terminal.t_str[t - tstr];
1540 if (t->name == NULL) {
1541 /* XXX: some systems' tgetstr needs non const */
1542 scap = tgetstr(ct_encode_string(*argv, &el->el_scratch), &area);
1544 if (!scap || scap[0] == '\0') {
1546 (void) fprintf(el->el_errfile,
1547 "echotc: Termcap parameter `%ls' not found.\n",
1552 * Count home many values we need for this capability.
1554 for (cap = scap, arg_need = 0; *cap; cap++)
1574 * hpux has lot's of them...
1577 (void) fprintf(el->el_errfile,
1578 "echotc: Warning: unknown termcap %% `%c'.\n",
1580 /* This is bad, but I won't complain */
1587 if (*argv && *argv[0]) {
1589 (void) fprintf(el->el_errfile,
1590 "echotc: Warning: Extra argument `%ls'.\n",
1594 terminal_tputs(el, scap, 1);
1598 if (!*argv || *argv[0] == '\0') {
1600 (void) fprintf(el->el_errfile,
1601 "echotc: Warning: Missing argument.\n");
1605 i = wcstol(*argv, &ep, 10);
1606 if (*ep != '\0' || i < 0) {
1608 (void) fprintf(el->el_errfile,
1609 "echotc: Bad value `%ls' for rows.\n",
1615 if (*argv && *argv[0]) {
1617 (void) fprintf(el->el_errfile,
1618 "echotc: Warning: Extra argument `%ls"
1622 terminal_tputs(el, tgoto(scap, arg_cols, arg_rows), 1);
1625 /* This is wrong, but I will ignore it... */
1627 (void) fprintf(el->el_errfile,
1628 "echotc: Warning: Too many required arguments (%d).\n",
1633 if (!*argv || *argv[0] == '\0') {
1635 (void) fprintf(el->el_errfile,
1636 "echotc: Warning: Missing argument.\n");
1639 i = wcstol(*argv, &ep, 10);
1640 if (*ep != '\0' || i < 0) {
1642 (void) fprintf(el->el_errfile,
1643 "echotc: Bad value `%ls' for cols.\n",
1649 if (!*argv || *argv[0] == '\0') {
1651 (void) fprintf(el->el_errfile,
1652 "echotc: Warning: Missing argument.\n");
1655 i = wcstol(*argv, &ep, 10);
1656 if (*ep != '\0' || i < 0) {
1658 (void) fprintf(el->el_errfile,
1659 "echotc: Bad value `%ls' for rows.\n",
1666 (void) fprintf(el->el_errfile,
1667 "echotc: Bad value `%ls'.\n", *argv);
1671 if (*argv && *argv[0]) {
1673 (void) fprintf(el->el_errfile,
1674 "echotc: Warning: Extra argument `%ls"
1678 terminal_tputs(el, tgoto(scap, arg_cols, arg_rows), arg_rows);