1 /* $NetBSD: terminal.c,v 1.43 2020/07/10 20:34:24 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.43 2020/07/10 20:34:24 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_calloc(TC_BUFSIZE,
273 sizeof(*el->el_terminal.t_buf));
274 if (el->el_terminal.t_buf == NULL)
276 el->el_terminal.t_cap = el_calloc(TC_BUFSIZE,
277 sizeof(*el->el_terminal.t_cap));
278 if (el->el_terminal.t_cap == NULL)
280 el->el_terminal.t_fkey = el_calloc(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_calloc(T_str,
286 sizeof(*el->el_terminal.t_str));
287 if (el->el_terminal.t_str == NULL)
289 el->el_terminal.t_val = el_calloc(T_val,
290 sizeof(*el->el_terminal.t_val));
291 if (el->el_terminal.t_val == NULL)
293 (void) terminal_set(el, NULL);
294 terminal_init_arrow(el);
297 free(el->el_terminal.t_str);
298 el->el_terminal.t_str = NULL;
300 free(el->el_terminal.t_fkey);
301 el->el_terminal.t_fkey = NULL;
303 free(el->el_terminal.t_cap);
304 el->el_terminal.t_cap = NULL;
306 free(el->el_terminal.t_buf);
307 el->el_terminal.t_buf = NULL;
313 * Clean up the terminal stuff
316 terminal_end(EditLine *el)
319 el_free(el->el_terminal.t_buf);
320 el->el_terminal.t_buf = NULL;
321 el_free(el->el_terminal.t_cap);
322 el->el_terminal.t_cap = NULL;
323 el->el_terminal.t_loc = 0;
324 el_free(el->el_terminal.t_str);
325 el->el_terminal.t_str = NULL;
326 el_free(el->el_terminal.t_val);
327 el->el_terminal.t_val = NULL;
328 el_free(el->el_terminal.t_fkey);
329 el->el_terminal.t_fkey = NULL;
330 terminal_free_display(el);
335 * Maintain a string pool for termcap strings
338 terminal_alloc(EditLine *el, const struct termcapstr *t, const char *cap)
340 char termbuf[TC_BUFSIZE];
342 char **tlist = el->el_terminal.t_str;
343 char **tmp, **str = &tlist[t - tstr];
345 (void) memset(termbuf, 0, sizeof(termbuf));
346 if (cap == NULL || *cap == '\0') {
352 tlen = *str == NULL ? 0 : strlen(*str);
355 * New string is shorter; no need to allocate space
359 (void) strcpy(*str, cap); /* XXX strcpy is safe */
363 * New string is longer; see if we have enough space to append
365 if (el->el_terminal.t_loc + 3 < TC_BUFSIZE) {
366 /* XXX strcpy is safe */
367 (void) strcpy(*str = &el->el_terminal.t_buf[
368 el->el_terminal.t_loc], cap);
369 el->el_terminal.t_loc += clen + 1; /* one for \0 */
373 * Compact our buffer; no need to check compaction, cause we know it
377 for (tmp = tlist; tmp < &tlist[T_str]; tmp++)
378 if (*tmp != NULL && **tmp != '\0' && *tmp != *str) {
381 for (ptr = *tmp; *ptr != '\0'; termbuf[tlen++] = *ptr++)
383 termbuf[tlen++] = '\0';
385 memcpy(el->el_terminal.t_buf, termbuf, TC_BUFSIZE);
386 el->el_terminal.t_loc = tlen;
387 if (el->el_terminal.t_loc + 3 >= TC_BUFSIZE) {
388 (void) fprintf(el->el_errfile,
389 "Out of termcap string space.\n");
392 /* XXX strcpy is safe */
393 (void) strcpy(*str = &el->el_terminal.t_buf[el->el_terminal.t_loc],
395 el->el_terminal.t_loc += (size_t)clen + 1; /* one for \0 */
400 /* terminal_rebuffer_display():
401 * Rebuffer the display after the screen changed size
404 terminal_rebuffer_display(EditLine *el)
406 coord_t *c = &el->el_terminal.t_size;
408 terminal_free_display(el);
413 if (terminal_alloc_display(el) == -1)
419 terminal_alloc_buffer(EditLine *el)
422 coord_t *c = &el->el_terminal.t_size;
425 b = el_calloc((size_t)(c->v + 1), sizeof(*b));
428 for (i = 0; i < c->v; i++) {
429 b[i] = el_calloc((size_t)(c->h + 1), sizeof(**b));
442 terminal_free_buffer(wint_t ***bp)
453 for (bufp = b; *bufp != NULL; bufp++)
458 /* terminal_alloc_display():
459 * Allocate a new display.
462 terminal_alloc_display(EditLine *el)
464 el->el_display = terminal_alloc_buffer(el);
465 if (el->el_display == NULL)
467 el->el_vdisplay = terminal_alloc_buffer(el);
468 if (el->el_vdisplay == NULL)
472 terminal_free_display(el);
477 /* terminal_free_display():
478 * Free the display buffers
481 terminal_free_display(EditLine *el)
483 terminal_free_buffer(&el->el_display);
484 terminal_free_buffer(&el->el_vdisplay);
488 /* terminal_move_to_line():
489 * move to line <where> (first line == 0)
490 * as efficiently as possible
493 terminal_move_to_line(EditLine *el, int where)
497 if (where == el->el_cursor.v)
500 if (where >= el->el_terminal.t_size.v) {
502 (void) fprintf(el->el_errfile,
503 "%s: where is ridiculous: %d\r\n", __func__, where);
504 #endif /* DEBUG_SCREEN */
507 if ((del = where - el->el_cursor.v) > 0) {
509 * We don't use DO here because some terminals are buggy
510 * if the destination is beyond bottom of the screen.
512 for (; del > 0; del--)
513 terminal__putc(el, '\n');
514 /* because the \n will become \r\n */
516 } else { /* del < 0 */
517 if (GoodStr(T_UP) && (-del > 1 || !GoodStr(T_up)))
518 terminal_tputs(el, tgoto(Str(T_UP), -del, -del), -del);
521 for (; del < 0; del++)
522 terminal_tputs(el, Str(T_up), 1);
525 el->el_cursor.v = where;/* now where is here */
529 /* terminal_move_to_char():
530 * Move to the character position specified
533 terminal_move_to_char(EditLine *el, int where)
538 if (where == el->el_cursor.h)
541 if (where > el->el_terminal.t_size.h) {
543 (void) fprintf(el->el_errfile,
544 "%s: where is ridiculous: %d\r\n", __func__, where);
545 #endif /* DEBUG_SCREEN */
548 if (!where) { /* if where is first column */
549 terminal__putc(el, '\r'); /* do a CR */
553 del = where - el->el_cursor.h;
555 if ((del < -4 || del > 4) && GoodStr(T_ch))
556 /* go there directly */
557 terminal_tputs(el, tgoto(Str(T_ch), where, where), where);
559 if (del > 0) { /* moving forward */
560 if ((del > 4) && GoodStr(T_RI))
561 terminal_tputs(el, tgoto(Str(T_RI), del, del),
564 /* if I can do tabs, use them */
566 if ((el->el_cursor.h & 0370) !=
569 el->el_cursor.v][where & 0370] !=
572 /* if not within tab stop */
574 (el->el_cursor.h & 0370);
580 el->el_cursor.h = where & ~0x7;
584 * it's usually cheaper to just write the
588 * NOTE THAT terminal_overwrite() WILL CHANGE
591 terminal_overwrite(el, &el->el_display[
592 el->el_cursor.v][el->el_cursor.h],
593 (size_t)(where - el->el_cursor.h));
596 } else { /* del < 0 := moving backward */
597 if ((-del > 4) && GoodStr(T_LE))
598 terminal_tputs(el, tgoto(Str(T_LE), -del, -del),
600 else { /* can't go directly there */
602 * if the "cost" is greater than the "cost"
606 ((unsigned int)-del >
607 (((unsigned int) where >> 3) +
610 terminal__putc(el, '\r');/* do a CR */
612 goto mc_again; /* and try again */
614 for (i = 0; i < -del; i++)
615 terminal__putc(el, '\b');
619 el->el_cursor.h = where; /* now where is here */
623 /* terminal_overwrite():
624 * Overstrike num characters
625 * Assumes MB_FILL_CHARs are present to keep the column count correct
628 terminal_overwrite(EditLine *el, const wchar_t *cp, size_t n)
633 if (n > (size_t)el->el_terminal.t_size.h) {
635 (void) fprintf(el->el_errfile,
636 "%s: n is ridiculous: %zu\r\n", __func__, n);
637 #endif /* DEBUG_SCREEN */
642 /* terminal__putc() ignores any MB_FILL_CHARs */
643 terminal__putc(el, *cp++);
647 if (el->el_cursor.h >= el->el_terminal.t_size.h) { /* wrap? */
648 if (EL_HAS_AUTO_MARGINS) { /* yes */
650 if (el->el_cursor.v + 1 < el->el_terminal.t_size.v)
652 if (EL_HAS_MAGIC_MARGINS) {
653 /* force the wrap to avoid the "magic"
656 if ((c = el->el_display[el->el_cursor.v]
657 [el->el_cursor.h]) != '\0') {
658 terminal_overwrite(el, &c, (size_t)1);
659 while (el->el_display[el->el_cursor.v]
660 [el->el_cursor.h] == MB_FILL_CHAR)
663 terminal__putc(el, ' ');
667 } else /* no wrap, but cursor stays on screen */
668 el->el_cursor.h = el->el_terminal.t_size.h - 1;
673 /* terminal_deletechars():
674 * Delete num characters
677 terminal_deletechars(EditLine *el, int num)
682 if (!EL_CAN_DELETE) {
684 (void) fprintf(el->el_errfile, " ERROR: cannot delete \n");
685 #endif /* DEBUG_EDIT */
688 if (num > el->el_terminal.t_size.h) {
690 (void) fprintf(el->el_errfile,
691 "%s: num is ridiculous: %d\r\n", __func__, num);
692 #endif /* DEBUG_SCREEN */
695 if (GoodStr(T_DC)) /* if I have multiple delete */
696 if ((num > 1) || !GoodStr(T_dc)) { /* if dc would be more
698 terminal_tputs(el, tgoto(Str(T_DC), num, num), num);
701 if (GoodStr(T_dm)) /* if I have delete mode */
702 terminal_tputs(el, Str(T_dm), 1);
704 if (GoodStr(T_dc)) /* else do one at a time */
706 terminal_tputs(el, Str(T_dc), 1);
708 if (GoodStr(T_ed)) /* if I have delete mode */
709 terminal_tputs(el, Str(T_ed), 1);
713 /* terminal_insertwrite():
714 * Puts terminal in insert character mode or inserts num
715 * characters in the line
716 * Assumes MB_FILL_CHARs are present to keep column count correct
719 terminal_insertwrite(EditLine *el, wchar_t *cp, int num)
723 if (!EL_CAN_INSERT) {
725 (void) fprintf(el->el_errfile, " ERROR: cannot insert \n");
726 #endif /* DEBUG_EDIT */
729 if (num > el->el_terminal.t_size.h) {
731 (void) fprintf(el->el_errfile,
732 "%s: num is ridiculous: %d\r\n", __func__, num);
733 #endif /* DEBUG_SCREEN */
736 if (GoodStr(T_IC)) /* if I have multiple insert */
737 if ((num > 1) || !GoodStr(T_ic)) {
738 /* if ic would be more expensive */
739 terminal_tputs(el, tgoto(Str(T_IC), num, num), num);
740 terminal_overwrite(el, cp, (size_t)num);
741 /* this updates el_cursor.h */
744 if (GoodStr(T_im) && GoodStr(T_ei)) { /* if I have insert mode */
745 terminal_tputs(el, Str(T_im), 1);
747 el->el_cursor.h += num;
749 terminal__putc(el, *cp++);
752 if (GoodStr(T_ip)) /* have to make num chars insert */
753 terminal_tputs(el, Str(T_ip), 1);
755 terminal_tputs(el, Str(T_ei), 1);
759 if (GoodStr(T_ic)) /* have to make num chars insert */
760 terminal_tputs(el, Str(T_ic), 1);
762 terminal__putc(el, *cp++);
766 if (GoodStr(T_ip)) /* have to make num chars insert */
767 terminal_tputs(el, Str(T_ip), 1);
768 /* pad the inserted char */
774 /* terminal_clear_EOL():
775 * clear to end of line. There are num characters to clear
778 terminal_clear_EOL(EditLine *el, int num)
782 if (EL_CAN_CEOL && GoodStr(T_ce))
783 terminal_tputs(el, Str(T_ce), 1);
785 for (i = 0; i < num; i++)
786 terminal__putc(el, ' ');
787 el->el_cursor.h += num; /* have written num spaces */
792 /* terminal_clear_screen():
796 terminal_clear_screen(EditLine *el)
797 { /* clear the whole screen and home */
800 /* send the clear screen code */
801 terminal_tputs(el, Str(T_cl), Val(T_li));
802 else if (GoodStr(T_ho) && GoodStr(T_cd)) {
803 terminal_tputs(el, Str(T_ho), Val(T_li)); /* home */
804 /* clear to bottom of screen */
805 terminal_tputs(el, Str(T_cd), Val(T_li));
807 terminal__putc(el, '\r');
808 terminal__putc(el, '\n');
814 * Beep the way the terminal wants us
817 terminal_beep(EditLine *el)
820 /* what termcap says we should use */
821 terminal_tputs(el, Str(T_bl), 1);
823 terminal__putc(el, '\007'); /* an ASCII bell; ^G */
828 terminal_get(EditLine *el, const char **term)
830 *term = el->el_terminal.t_name;
835 * Read in the terminal capabilities from the requested terminal
838 terminal_set(EditLine *el, const char *term)
841 char buf[TC_BUFSIZE];
843 const struct termcapstr *t;
847 (void) sigemptyset(&nset);
848 (void) sigaddset(&nset, SIGWINCH);
849 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
855 term = getenv("TERM");
857 if (!term || !term[0])
860 if (strcmp(term, "emacs") == 0)
861 el->el_flags |= EDIT_DISABLED;
863 (void) memset(el->el_terminal.t_cap, 0, TC_BUFSIZE);
865 i = tgetent(el->el_terminal.t_cap, term);
869 (void) fprintf(el->el_errfile,
870 "Cannot read termcap database;\n");
872 (void) fprintf(el->el_errfile,
873 "No entry for terminal type \"%s\";\n", term);
874 (void) fprintf(el->el_errfile,
875 "using dumb terminal settings.\n");
876 Val(T_co) = 80; /* do a dumb terminal */
877 Val(T_pt) = Val(T_km) = Val(T_li) = 0;
878 Val(T_xt) = Val(T_MT);
879 for (t = tstr; t->name != NULL; t++)
880 terminal_alloc(el, t, NULL);
882 /* auto/magic margins */
883 Val(T_am) = tgetflag("am");
884 Val(T_xn) = tgetflag("xn");
886 Val(T_pt) = tgetflag("pt");
887 Val(T_xt) = tgetflag("xt");
888 /* do we have a meta? */
889 Val(T_km) = tgetflag("km");
890 Val(T_MT) = tgetflag("MT");
892 Val(T_co) = tgetnum("co");
893 Val(T_li) = tgetnum("li");
894 for (t = tstr; t->name != NULL; t++) {
895 /* XXX: some systems' tgetstr needs non const */
896 terminal_alloc(el, t, tgetstr(strchr(t->name, *t->name),
902 Val(T_co) = 80; /* just in case */
906 el->el_terminal.t_size.v = Val(T_co);
907 el->el_terminal.t_size.h = Val(T_li);
909 terminal_setflags(el);
911 /* get the correct window size */
912 (void) terminal_get_size(el, &lins, &cols);
913 if (terminal_change_size(el, lins, cols) == -1)
915 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
916 terminal_bind_arrow(el);
917 el->el_terminal.t_name = term;
918 return i <= 0 ? -1 : 0;
922 /* terminal_get_size():
923 * Return the new window size in lines and cols, and
924 * true if the size was changed.
927 terminal_get_size(EditLine *el, int *lins, int *cols)
936 if (ioctl(el->el_infd, TIOCGWINSZ, &ws) != -1) {
947 if (ioctl(el->el_infd, TIOCGSIZE, &ts) != -1) {
955 return Val(T_co) != *cols || Val(T_li) != *lins;
959 /* terminal_change_size():
960 * Change the size of the terminal
963 terminal_change_size(EditLine *el, int lins, int cols)
965 coord_t cur = el->el_cursor;
969 Val(T_co) = (cols < 2) ? 80 : cols;
970 Val(T_li) = (lins < 1) ? 24 : lins;
972 /* re-make display buffers */
973 if (terminal_rebuffer_display(el) == -1)
975 re_clear_display(el);
981 /* terminal_init_arrow():
982 * Initialize the arrow key bindings from termcap
985 terminal_init_arrow(EditLine *el)
987 funckey_t *arrow = el->el_terminal.t_fkey;
989 arrow[A_K_DN].name = L"down";
990 arrow[A_K_DN].key = T_kd;
991 arrow[A_K_DN].fun.cmd = ED_NEXT_HISTORY;
992 arrow[A_K_DN].type = XK_CMD;
994 arrow[A_K_UP].name = L"up";
995 arrow[A_K_UP].key = T_ku;
996 arrow[A_K_UP].fun.cmd = ED_PREV_HISTORY;
997 arrow[A_K_UP].type = XK_CMD;
999 arrow[A_K_LT].name = L"left";
1000 arrow[A_K_LT].key = T_kl;
1001 arrow[A_K_LT].fun.cmd = ED_PREV_CHAR;
1002 arrow[A_K_LT].type = XK_CMD;
1004 arrow[A_K_RT].name = L"right";
1005 arrow[A_K_RT].key = T_kr;
1006 arrow[A_K_RT].fun.cmd = ED_NEXT_CHAR;
1007 arrow[A_K_RT].type = XK_CMD;
1009 arrow[A_K_HO].name = L"home";
1010 arrow[A_K_HO].key = T_kh;
1011 arrow[A_K_HO].fun.cmd = ED_MOVE_TO_BEG;
1012 arrow[A_K_HO].type = XK_CMD;
1014 arrow[A_K_EN].name = L"end";
1015 arrow[A_K_EN].key = T_at7;
1016 arrow[A_K_EN].fun.cmd = ED_MOVE_TO_END;
1017 arrow[A_K_EN].type = XK_CMD;
1019 arrow[A_K_DE].name = L"delete";
1020 arrow[A_K_DE].key = T_kD;
1021 arrow[A_K_DE].fun.cmd = ED_DELETE_NEXT_CHAR;
1022 arrow[A_K_DE].type = XK_CMD;
1026 /* terminal_reset_arrow():
1027 * Reset arrow key bindings
1030 terminal_reset_arrow(EditLine *el)
1032 funckey_t *arrow = el->el_terminal.t_fkey;
1033 static const wchar_t strA[] = L"\033[A";
1034 static const wchar_t strB[] = L"\033[B";
1035 static const wchar_t strC[] = L"\033[C";
1036 static const wchar_t strD[] = L"\033[D";
1037 static const wchar_t strH[] = L"\033[H";
1038 static const wchar_t strF[] = L"\033[F";
1039 static const wchar_t stOA[] = L"\033OA";
1040 static const wchar_t stOB[] = L"\033OB";
1041 static const wchar_t stOC[] = L"\033OC";
1042 static const wchar_t stOD[] = L"\033OD";
1043 static const wchar_t stOH[] = L"\033OH";
1044 static const wchar_t stOF[] = L"\033OF";
1046 keymacro_add(el, strA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1047 keymacro_add(el, strB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1048 keymacro_add(el, strC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1049 keymacro_add(el, strD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1050 keymacro_add(el, strH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1051 keymacro_add(el, strF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1052 keymacro_add(el, stOA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1053 keymacro_add(el, stOB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1054 keymacro_add(el, stOC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1055 keymacro_add(el, stOD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1056 keymacro_add(el, stOH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1057 keymacro_add(el, stOF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1059 if (el->el_map.type != MAP_VI)
1061 keymacro_add(el, &strA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1062 keymacro_add(el, &strB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1063 keymacro_add(el, &strC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1064 keymacro_add(el, &strD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1065 keymacro_add(el, &strH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1066 keymacro_add(el, &strF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1067 keymacro_add(el, &stOA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1068 keymacro_add(el, &stOB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1069 keymacro_add(el, &stOC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1070 keymacro_add(el, &stOD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1071 keymacro_add(el, &stOH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1072 keymacro_add(el, &stOF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1076 /* terminal_set_arrow():
1077 * Set an arrow key binding
1080 terminal_set_arrow(EditLine *el, const wchar_t *name, keymacro_value_t *fun,
1083 funckey_t *arrow = el->el_terminal.t_fkey;
1086 for (i = 0; i < A_K_NKEYS; i++)
1087 if (wcscmp(name, arrow[i].name) == 0) {
1088 arrow[i].fun = *fun;
1089 arrow[i].type = type;
1096 /* terminal_clear_arrow():
1097 * Clear an arrow key binding
1100 terminal_clear_arrow(EditLine *el, const wchar_t *name)
1102 funckey_t *arrow = el->el_terminal.t_fkey;
1105 for (i = 0; i < A_K_NKEYS; i++)
1106 if (wcscmp(name, arrow[i].name) == 0) {
1107 arrow[i].type = XK_NOD;
1114 /* terminal_print_arrow():
1115 * Print the arrow key bindings
1117 libedit_private void
1118 terminal_print_arrow(EditLine *el, const wchar_t *name)
1121 funckey_t *arrow = el->el_terminal.t_fkey;
1123 for (i = 0; i < A_K_NKEYS; i++)
1124 if (*name == '\0' || wcscmp(name, arrow[i].name) == 0)
1125 if (arrow[i].type != XK_NOD)
1126 keymacro_kprint(el, arrow[i].name,
1127 &arrow[i].fun, arrow[i].type);
1131 /* terminal_bind_arrow():
1132 * Bind the arrow keys
1134 libedit_private void
1135 terminal_bind_arrow(EditLine *el)
1138 const el_action_t *dmap;
1141 funckey_t *arrow = el->el_terminal.t_fkey;
1143 /* Check if the components needed are initialized */
1144 if (el->el_terminal.t_buf == NULL || el->el_map.key == NULL)
1147 map = el->el_map.type == MAP_VI ? el->el_map.alt : el->el_map.key;
1148 dmap = el->el_map.type == MAP_VI ? el->el_map.vic : el->el_map.emacs;
1150 terminal_reset_arrow(el);
1152 for (i = 0; i < A_K_NKEYS; i++) {
1153 wchar_t wt_str[VISUAL_WIDTH_MAX];
1157 p = el->el_terminal.t_str[arrow[i].key];
1160 for (n = 0; n < VISUAL_WIDTH_MAX && p[n]; ++n)
1162 while (n < VISUAL_WIDTH_MAX)
1165 j = (unsigned char) *p;
1167 * Assign the arrow keys only if:
1169 * 1. They are multi-character arrow keys and the user
1170 * has not re-assigned the leading character, or
1171 * has re-assigned the leading character to be
1172 * ED_SEQUENCE_LEAD_IN
1173 * 2. They are single arrow keys pointing to an
1176 if (arrow[i].type == XK_NOD)
1177 keymacro_clear(el, map, px);
1179 if (p[1] && (dmap[j] == map[j] ||
1180 map[j] == ED_SEQUENCE_LEAD_IN)) {
1181 keymacro_add(el, px, &arrow[i].fun,
1183 map[j] = ED_SEQUENCE_LEAD_IN;
1184 } else if (map[j] == ED_UNASSIGNED) {
1185 keymacro_clear(el, map, px);
1186 if (arrow[i].type == XK_CMD)
1187 map[j] = arrow[i].fun.cmd;
1189 keymacro_add(el, px, &arrow[i].fun,
1200 terminal_putc(int c)
1202 if (terminal_outfile == NULL)
1204 return fputc(c, terminal_outfile);
1208 terminal_tputs(EditLine *el, const char *cap, int affcnt)
1211 pthread_mutex_lock(&terminal_mutex);
1213 terminal_outfile = el->el_outfile;
1214 (void)tputs(cap, affcnt, terminal_putc);
1216 pthread_mutex_unlock(&terminal_mutex);
1220 /* terminal__putc():
1224 terminal__putc(EditLine *el, wint_t c)
1226 char buf[MB_LEN_MAX +1];
1228 if (c == MB_FILL_CHAR)
1231 return fputs(literal_get(el, c), el->el_outfile);
1232 i = ct_encode_char(buf, (size_t)MB_LEN_MAX, c);
1236 return fputs(buf, el->el_outfile);
1239 /* terminal__flush():
1242 libedit_private void
1243 terminal__flush(EditLine *el)
1246 (void) fflush(el->el_outfile);
1249 /* terminal_writec():
1250 * Write the given character out, in a human readable form
1252 libedit_private void
1253 terminal_writec(EditLine *el, wint_t c)
1255 wchar_t visbuf[VISUAL_WIDTH_MAX +1];
1256 ssize_t vcnt = ct_visual_char(visbuf, VISUAL_WIDTH_MAX, c);
1259 visbuf[vcnt] = '\0';
1260 terminal_overwrite(el, visbuf, (size_t)vcnt);
1261 terminal__flush(el);
1265 /* terminal_telltc():
1266 * Print the current termcap characteristics
1270 terminal_telltc(EditLine *el, int argc __attribute__((__unused__)),
1271 const wchar_t **argv __attribute__((__unused__)))
1273 const struct termcapstr *t;
1276 (void) fprintf(el->el_outfile, "\n\tYour terminal has the\n");
1277 (void) fprintf(el->el_outfile, "\tfollowing characteristics:\n\n");
1278 (void) fprintf(el->el_outfile, "\tIt has %d columns and %d lines\n",
1279 Val(T_co), Val(T_li));
1280 (void) fprintf(el->el_outfile,
1281 "\tIt has %s meta key\n", EL_HAS_META ? "a" : "no");
1282 (void) fprintf(el->el_outfile,
1283 "\tIt can%suse tabs\n", EL_CAN_TAB ? " " : "not ");
1284 (void) fprintf(el->el_outfile, "\tIt %s automatic margins\n",
1285 EL_HAS_AUTO_MARGINS ? "has" : "does not have");
1286 if (EL_HAS_AUTO_MARGINS)
1287 (void) fprintf(el->el_outfile, "\tIt %s magic margins\n",
1288 EL_HAS_MAGIC_MARGINS ? "has" : "does not have");
1290 for (t = tstr, ts = el->el_terminal.t_str; t->name != NULL; t++, ts++) {
1293 ub = ct_encode_string(ct_visual_string(
1294 ct_decode_string(*ts, &el->el_scratch),
1295 &el->el_visual), &el->el_scratch);
1299 (void) fprintf(el->el_outfile, "\t%25s (%s) == %s\n",
1300 t->long_name, t->name, ub);
1302 (void) fputc('\n', el->el_outfile);
1307 /* terminal_settc():
1308 * Change the current terminal characteristics
1312 terminal_settc(EditLine *el, int argc __attribute__((__unused__)),
1313 const wchar_t **argv)
1315 const struct termcapstr *ts;
1316 const struct termcapval *tv;
1317 char what[8], how[8];
1321 if (argv == NULL || argv[1] == NULL || argv[2] == NULL)
1324 strlcpy(what, ct_encode_string(argv[1], &el->el_scratch), sizeof(what));
1325 strlcpy(how, ct_encode_string(argv[2], &el->el_scratch), sizeof(how));
1328 * Do the strings first
1330 for (ts = tstr; ts->name != NULL; ts++)
1331 if (strcmp(ts->name, what) == 0)
1334 if (ts->name != NULL) {
1335 terminal_alloc(el, ts, how);
1336 terminal_setflags(el);
1340 * Do the numeric ones second
1342 for (tv = tval; tv->name != NULL; tv++)
1343 if (strcmp(tv->name, what) == 0)
1346 if (tv->name == NULL) {
1347 (void) fprintf(el->el_errfile,
1348 "%ls: Bad capability `%s'.\n", argv[0], what);
1352 if (tv == &tval[T_pt] || tv == &tval[T_km] ||
1353 tv == &tval[T_am] || tv == &tval[T_xn]) {
1357 if (strcmp(how, "yes") == 0)
1358 el->el_terminal.t_val[tv - tval] = 1;
1359 else if (strcmp(how, "no") == 0)
1360 el->el_terminal.t_val[tv - tval] = 0;
1362 (void) fprintf(el->el_errfile,
1363 "%ls: Bad value `%s'.\n", argv[0], how);
1366 terminal_setflags(el);
1373 i = strtol(how, &ep, 10);
1375 (void) fprintf(el->el_errfile,
1376 "%ls: Bad value `%s'.\n", argv[0], how);
1379 el->el_terminal.t_val[tv - tval] = (int) i;
1381 if (tv == &tval[T_co]) {
1382 el->el_terminal.t_size.v = Val(T_co);
1384 } else if (tv == &tval[T_li]) {
1385 el->el_terminal.t_size.h = Val(T_li);
1388 if (i && terminal_change_size(el, Val(T_li), Val(T_co)) == -1)
1394 /* terminal_gettc():
1395 * Get the current terminal characteristics
1399 terminal_gettc(EditLine *el, int argc __attribute__((__unused__)), char **argv)
1401 const struct termcapstr *ts;
1402 const struct termcapval *tv;
1406 if (argv == NULL || argv[1] == NULL || argv[2] == NULL)
1413 * Do the strings first
1415 for (ts = tstr; ts->name != NULL; ts++)
1416 if (strcmp(ts->name, what) == 0)
1419 if (ts->name != NULL) {
1420 *(char **)how = el->el_terminal.t_str[ts - tstr];
1424 * Do the numeric ones second
1426 for (tv = tval; tv->name != NULL; tv++)
1427 if (strcmp(tv->name, what) == 0)
1430 if (tv->name == NULL)
1433 if (tv == &tval[T_pt] || tv == &tval[T_km] ||
1434 tv == &tval[T_am] || tv == &tval[T_xn]) {
1435 static char yes[] = "yes";
1436 static char no[] = "no";
1437 if (el->el_terminal.t_val[tv - tval])
1438 *(char **)how = yes;
1443 *(int *)how = el->el_terminal.t_val[tv - tval];
1448 /* terminal_echotc():
1449 * Print the termcap string out with variable substitution
1453 terminal_echotc(EditLine *el, int argc __attribute__((__unused__)),
1454 const wchar_t **argv)
1458 int arg_need, arg_cols, arg_rows;
1459 int verbose = 0, silent = 0;
1461 static const char fmts[] = "%s\n", fmtd[] = "%d\n";
1462 const struct termcapstr *t;
1463 char buf[TC_BUFSIZE];
1468 if (argv == NULL || argv[1] == NULL)
1472 if (argv[0][0] == '-') {
1473 switch (argv[0][1]) {
1481 /* stderror(ERR_NAME | ERR_TCUSAGE); */
1486 if (!*argv || *argv[0] == '\0')
1488 if (wcscmp(*argv, L"tabs") == 0) {
1489 (void) fprintf(el->el_outfile, fmts, EL_CAN_TAB ? "yes" : "no");
1491 } else if (wcscmp(*argv, L"meta") == 0) {
1492 (void) fprintf(el->el_outfile, fmts, Val(T_km) ? "yes" : "no");
1494 } else if (wcscmp(*argv, L"xn") == 0) {
1495 (void) fprintf(el->el_outfile, fmts, EL_HAS_MAGIC_MARGINS ?
1498 } else if (wcscmp(*argv, L"am") == 0) {
1499 (void) fprintf(el->el_outfile, fmts, EL_HAS_AUTO_MARGINS ?
1502 } else if (wcscmp(*argv, L"baud") == 0) {
1503 (void) fprintf(el->el_outfile, fmtd, (int)el->el_tty.t_speed);
1505 } else if (wcscmp(*argv, L"rows") == 0 ||
1506 wcscmp(*argv, L"lines") == 0) {
1507 (void) fprintf(el->el_outfile, fmtd, Val(T_li));
1509 } else if (wcscmp(*argv, L"cols") == 0) {
1510 (void) fprintf(el->el_outfile, fmtd, Val(T_co));
1514 * Try to use our local definition first
1517 for (t = tstr; t->name != NULL; t++)
1519 ct_encode_string(*argv, &el->el_scratch)) == 0) {
1520 scap = el->el_terminal.t_str[t - tstr];
1523 if (t->name == NULL) {
1524 /* XXX: some systems' tgetstr needs non const */
1525 scap = tgetstr(ct_encode_string(*argv, &el->el_scratch), &area);
1527 if (!scap || scap[0] == '\0') {
1529 (void) fprintf(el->el_errfile,
1530 "echotc: Termcap parameter `%ls' not found.\n",
1535 * Count home many values we need for this capability.
1537 for (cap = scap, arg_need = 0; *cap; cap++)
1557 * hpux has lot's of them...
1560 (void) fprintf(el->el_errfile,
1561 "echotc: Warning: unknown termcap %% `%c'.\n",
1563 /* This is bad, but I won't complain */
1570 if (*argv && *argv[0]) {
1572 (void) fprintf(el->el_errfile,
1573 "echotc: Warning: Extra argument `%ls'.\n",
1577 terminal_tputs(el, scap, 1);
1581 if (!*argv || *argv[0] == '\0') {
1583 (void) fprintf(el->el_errfile,
1584 "echotc: Warning: Missing argument.\n");
1588 i = wcstol(*argv, &ep, 10);
1589 if (*ep != '\0' || i < 0) {
1591 (void) fprintf(el->el_errfile,
1592 "echotc: Bad value `%ls' for rows.\n",
1598 if (*argv && *argv[0]) {
1600 (void) fprintf(el->el_errfile,
1601 "echotc: Warning: Extra argument `%ls"
1605 terminal_tputs(el, tgoto(scap, arg_cols, arg_rows), 1);
1608 /* This is wrong, but I will ignore it... */
1610 (void) fprintf(el->el_errfile,
1611 "echotc: Warning: Too many required arguments (%d).\n",
1616 if (!*argv || *argv[0] == '\0') {
1618 (void) fprintf(el->el_errfile,
1619 "echotc: Warning: Missing argument.\n");
1622 i = wcstol(*argv, &ep, 10);
1623 if (*ep != '\0' || i < 0) {
1625 (void) fprintf(el->el_errfile,
1626 "echotc: Bad value `%ls' for cols.\n",
1632 if (!*argv || *argv[0] == '\0') {
1634 (void) fprintf(el->el_errfile,
1635 "echotc: Warning: Missing argument.\n");
1638 i = wcstol(*argv, &ep, 10);
1639 if (*ep != '\0' || i < 0) {
1641 (void) fprintf(el->el_errfile,
1642 "echotc: Bad value `%ls' for rows.\n",
1649 (void) fprintf(el->el_errfile,
1650 "echotc: Bad value `%ls'.\n", *argv);
1654 if (*argv && *argv[0]) {
1656 (void) fprintf(el->el_errfile,
1657 "echotc: Warning: Extra argument `%ls"
1661 terminal_tputs(el, tgoto(scap, arg_cols, arg_rows), arg_rows);