2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Christos Zoulas of Cornell University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $NetBSD: term.c,v 1.56 2009/12/28 21:54:21 christos Exp $
35 #if !defined(lint) && !defined(SCCSID)
36 static char sccsid[] = "@(#)term.c 8.2 (Berkeley) 4/30/95";
37 #endif /* not lint && not SCCSID */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * term.c: Editor/termcap-curses interface
43 * We have to declare a static variable here, since the
44 * termcap putchar routine does not take an argument!
55 #include <sys/types.h>
56 #include <sys/ioctl.h>
61 * IMPORTANT NOTE: these routines are allowed to look at the current screen
62 * and the current possition assuming that it is correct. If this is not
63 * true, then the update will be WRONG! This is (should be) a valid
67 #define TC_BUFSIZE 2048
69 #define GoodStr(a) (el->el_term.t_str[a] != NULL && \
70 el->el_term.t_str[a][0] != '\0')
71 #define Str(a) el->el_term.t_str[a]
72 #define Val(a) el->el_term.t_val[a]
75 private const struct {
146 private const struct termcapstr {
148 const char *long_name;
151 { "al", "add new blank line" },
153 { "bl", "audible bell" },
155 { "cd", "clear to bottom" },
157 { "ce", "clear to end of line" },
159 { "ch", "cursor to horiz pos" },
161 { "cl", "clear screen" },
163 { "dc", "delete a character" },
165 { "dl", "delete a line" },
167 { "dm", "start delete mode" },
169 { "ed", "end delete mode" },
171 { "ei", "end insert mode" },
173 { "fs", "cursor from status line" },
175 { "ho", "home cursor" },
177 { "ic", "insert character" },
179 { "im", "start insert mode" },
181 { "ip", "insert padding" },
183 { "kd", "sends cursor down" },
185 { "kl", "sends cursor left" },
187 { "kr", "sends cursor right" },
189 { "ku", "sends cursor up" },
191 { "md", "begin bold" },
193 { "me", "end attributes" },
195 { "nd", "non destructive space" },
197 { "se", "end standout" },
199 { "so", "begin standout" },
201 { "ts", "cursor to status line" },
203 { "up", "cursor up one" },
205 { "us", "begin underline" },
207 { "ue", "end underline" },
209 { "vb", "visible bell" },
211 { "DC", "delete multiple chars" },
213 { "DO", "cursor down multiple" },
215 { "IC", "insert multiple chars" },
217 { "LE", "cursor left multiple" },
219 { "RI", "cursor right multiple" },
221 { "UP", "cursor up multiple" },
223 { "kh", "send cursor home" },
225 { "@7", "send cursor end" },
230 private const struct termcapval {
232 const char *long_name;
235 { "am", "has automatic margins" },
237 { "pt", "has physical tabs" },
239 { "li", "Number of lines" },
241 { "co", "Number of columns" },
243 { "km", "Has meta key" },
245 { "xt", "Tab chars destructive" },
247 { "xn", "newline ignored at right margin" },
249 { "MT", "Has meta key" }, /* XXX? */
253 /* do two or more of the attributes use me */
255 private void term_setflags(EditLine *);
256 private int term_rebuffer_display(EditLine *);
257 private void term_free_display(EditLine *);
258 private int term_alloc_display(EditLine *);
259 private void term_alloc(EditLine *, const struct termcapstr *, const char *);
260 private void term_init_arrow(EditLine *);
261 private void term_reset_arrow(EditLine *);
262 private int term_putc(int);
263 private void term_tputs(EditLine *, const char *, int);
266 private pthread_mutex_t term_mutex = PTHREAD_MUTEX_INITIALIZER;
268 private FILE *term_outfile = NULL;
272 * Set the terminal capability flags
275 term_setflags(EditLine *el)
278 if (el->el_tty.t_tabs)
279 EL_FLAGS |= (Val(T_pt) && !Val(T_xt)) ? TERM_CAN_TAB : 0;
281 EL_FLAGS |= (Val(T_km) || Val(T_MT)) ? TERM_HAS_META : 0;
282 EL_FLAGS |= GoodStr(T_ce) ? TERM_CAN_CEOL : 0;
283 EL_FLAGS |= (GoodStr(T_dc) || GoodStr(T_DC)) ? TERM_CAN_DELETE : 0;
284 EL_FLAGS |= (GoodStr(T_im) || GoodStr(T_ic) || GoodStr(T_IC)) ?
286 EL_FLAGS |= (GoodStr(T_up) || GoodStr(T_UP)) ? TERM_CAN_UP : 0;
287 EL_FLAGS |= Val(T_am) ? TERM_HAS_AUTO_MARGINS : 0;
288 EL_FLAGS |= Val(T_xn) ? TERM_HAS_MAGIC_MARGINS : 0;
290 if (GoodStr(T_me) && GoodStr(T_ue))
291 EL_FLAGS |= (strcmp(Str(T_me), Str(T_ue)) == 0) ?
294 EL_FLAGS &= ~TERM_CAN_ME;
295 if (GoodStr(T_me) && GoodStr(T_se))
296 EL_FLAGS |= (strcmp(Str(T_me), Str(T_se)) == 0) ?
302 (void) fprintf(el->el_errfile,
303 "WARNING: Your terminal cannot move up.\n");
304 (void) fprintf(el->el_errfile,
305 "Editing may be odd for long lines.\n");
308 (void) fprintf(el->el_errfile, "no clear EOL capability.\n");
310 (void) fprintf(el->el_errfile, "no delete char capability.\n");
312 (void) fprintf(el->el_errfile, "no insert char capability.\n");
313 #endif /* DEBUG_SCREEN */
317 * Initialize the terminal stuff
320 term_init(EditLine *el)
323 el->el_term.t_buf = (char *) el_malloc(TC_BUFSIZE);
324 if (el->el_term.t_buf == NULL)
326 el->el_term.t_cap = (char *) el_malloc(TC_BUFSIZE);
327 if (el->el_term.t_cap == NULL)
329 el->el_term.t_fkey = (fkey_t *) el_malloc(A_K_NKEYS * sizeof(fkey_t));
330 if (el->el_term.t_fkey == NULL)
332 el->el_term.t_loc = 0;
333 el->el_term.t_str = (char **) el_malloc(T_str * sizeof(char *));
334 if (el->el_term.t_str == NULL)
336 (void) memset(el->el_term.t_str, 0, T_str * sizeof(char *));
337 el->el_term.t_val = (int *) el_malloc(T_val * sizeof(int));
338 if (el->el_term.t_val == NULL)
340 (void) memset(el->el_term.t_val, 0, T_val * sizeof(int));
342 (void) term_set(el, NULL);
347 * Clean up the terminal stuff
350 term_end(EditLine *el)
353 el_free((ptr_t) el->el_term.t_buf);
354 el->el_term.t_buf = NULL;
355 el_free((ptr_t) el->el_term.t_cap);
356 el->el_term.t_cap = NULL;
357 el->el_term.t_loc = 0;
358 el_free((ptr_t) el->el_term.t_str);
359 el->el_term.t_str = NULL;
360 el_free((ptr_t) el->el_term.t_val);
361 el->el_term.t_val = NULL;
362 el_free((ptr_t) el->el_term.t_fkey);
363 el->el_term.t_fkey = NULL;
364 term_free_display(el);
369 * Maintain a string pool for termcap strings
372 term_alloc(EditLine *el, const struct termcapstr *t, const char *cap)
374 char termbuf[TC_BUFSIZE];
376 char **tlist = el->el_term.t_str;
377 char **tmp, **str = &tlist[t - tstr];
379 if (cap == NULL || *cap == '\0') {
385 tlen = *str == NULL ? 0 : strlen(*str);
388 * New string is shorter; no need to allocate space
392 (void) strcpy(*str, cap); /* XXX strcpy is safe */
396 * New string is longer; see if we have enough space to append
398 if (el->el_term.t_loc + 3 < TC_BUFSIZE) {
399 /* XXX strcpy is safe */
400 (void) strcpy(*str = &el->el_term.t_buf[el->el_term.t_loc],
402 el->el_term.t_loc += (int)clen + 1; /* one for \0 */
406 * Compact our buffer; no need to check compaction, cause we know it
410 for (tmp = tlist; tmp < &tlist[T_str]; tmp++)
411 if (*tmp != NULL && *tmp != '\0' && *tmp != *str) {
414 for (ptr = *tmp; *ptr != '\0'; termbuf[tlen++] = *ptr++)
416 termbuf[tlen++] = '\0';
418 memcpy(el->el_term.t_buf, termbuf, TC_BUFSIZE);
419 el->el_term.t_loc = (int)tlen;
420 if (el->el_term.t_loc + 3 >= TC_BUFSIZE) {
421 (void) fprintf(el->el_errfile,
422 "Out of termcap string space.\n");
425 /* XXX strcpy is safe */
426 (void) strcpy(*str = &el->el_term.t_buf[el->el_term.t_loc], cap);
427 el->el_term.t_loc += (int)clen + 1; /* one for \0 */
432 /* term_rebuffer_display():
433 * Rebuffer the display after the screen changed size
436 term_rebuffer_display(EditLine *el)
438 coord_t *c = &el->el_term.t_size;
440 term_free_display(el);
445 if (term_alloc_display(el) == -1)
451 /* term_alloc_display():
452 * Allocate a new display.
455 term_alloc_display(EditLine *el)
459 coord_t *c = &el->el_term.t_size;
461 b = (char **) el_malloc((size_t) (sizeof(char *) * (c->v + 1)));
464 for (i = 0; i < c->v; i++) {
465 b[i] = (char *) el_malloc((size_t) (sizeof(char) * (c->h + 1)));
468 el_free((ptr_t) b[i]);
476 b = (char **) el_malloc((size_t) (sizeof(char *) * (c->v + 1)));
479 for (i = 0; i < c->v; i++) {
480 b[i] = (char *) el_malloc((size_t) (sizeof(char) * (c->h + 1)));
483 el_free((ptr_t) b[i]);
494 /* term_free_display():
495 * Free the display buffers
498 term_free_display(EditLine *el)
504 el->el_display = NULL;
506 for (bufp = b; *bufp != NULL; bufp++)
507 el_free((ptr_t) * bufp);
511 el->el_vdisplay = NULL;
513 for (bufp = b; *bufp != NULL; bufp++)
514 el_free((ptr_t) * bufp);
520 /* term_move_to_line():
521 * move to line <where> (first line == 0)
522 * as efficiently as possible
525 term_move_to_line(EditLine *el, int where)
529 if (where == el->el_cursor.v)
532 if (where > el->el_term.t_size.v) {
534 (void) fprintf(el->el_errfile,
535 "term_move_to_line: where is ridiculous: %d\r\n", where);
536 #endif /* DEBUG_SCREEN */
539 if ((del = where - el->el_cursor.v) > 0) {
541 if (EL_HAS_AUTO_MARGINS &&
542 el->el_display[el->el_cursor.v][0] != '\0') {
543 /* move without newline */
544 term_move_to_char(el, el->el_term.t_size.h - 1);
545 term_overwrite(el, &el->el_display
546 [el->el_cursor.v][el->el_cursor.h],
547 (size_t)(el->el_term.t_size.h -
552 if ((del > 1) && GoodStr(T_DO)) {
553 term_tputs(el, tgoto(Str(T_DO), del,
557 for (; del > 0; del--)
558 term__putc(el, '\n');
559 /* because the \n will become \r\n */
564 } else { /* del < 0 */
565 if (GoodStr(T_UP) && (-del > 1 || !GoodStr(T_up)))
566 term_tputs(el, tgoto(Str(T_UP), -del, -del), -del);
569 for (; del < 0; del++)
570 term_tputs(el, Str(T_up), 1);
573 el->el_cursor.v = where;/* now where is here */
577 /* term_move_to_char():
578 * Move to the character position specified
581 term_move_to_char(EditLine *el, int where)
586 if (where == el->el_cursor.h)
589 if (where > el->el_term.t_size.h) {
591 (void) fprintf(el->el_errfile,
592 "term_move_to_char: where is riduculous: %d\r\n", where);
593 #endif /* DEBUG_SCREEN */
596 if (!where) { /* if where is first column */
597 term__putc(el, '\r'); /* do a CR */
601 del = where - el->el_cursor.h;
603 if ((del < -4 || del > 4) && GoodStr(T_ch))
604 /* go there directly */
605 term_tputs(el, tgoto(Str(T_ch), where, where), where);
607 if (del > 0) { /* moving forward */
608 if ((del > 4) && GoodStr(T_RI))
609 term_tputs(el, tgoto(Str(T_RI), del, del), del);
611 /* if I can do tabs, use them */
613 if ((el->el_cursor.h & 0370) !=
615 /* if not within tab stop */
617 (el->el_cursor.h & 0370);
620 term__putc(el, '\t');
622 el->el_cursor.h = where & ~0x7;
626 * it's usually cheaper to just write the
630 * NOTE THAT term_overwrite() WILL CHANGE
633 term_overwrite(el, &el->el_display[
634 el->el_cursor.v][el->el_cursor.h],
635 (size_t)(where - el->el_cursor.h));
638 } else { /* del < 0 := moving backward */
639 if ((-del > 4) && GoodStr(T_LE))
640 term_tputs(el, tgoto(Str(T_LE), -del, -del),
642 else { /* can't go directly there */
644 * if the "cost" is greater than the "cost"
648 ((unsigned int)-del >
649 (((unsigned int) where >> 3) +
652 term__putc(el, '\r'); /* do a CR */
654 goto mc_again; /* and try again */
656 for (i = 0; i < -del; i++)
657 term__putc(el, '\b');
661 el->el_cursor.h = where; /* now where is here */
666 * Overstrike num characters
669 term_overwrite(EditLine *el, const char *cp, size_t n)
674 if (n > (size_t)el->el_term.t_size.h) {
676 (void) fprintf(el->el_errfile,
677 "term_overwrite: n is riduculous: %d\r\n", n);
678 #endif /* DEBUG_SCREEN */
683 term__putc(el, *cp++);
687 if (el->el_cursor.h >= el->el_term.t_size.h) { /* wrap? */
688 if (EL_HAS_AUTO_MARGINS) { /* yes */
691 if (EL_HAS_MAGIC_MARGINS) {
692 /* force the wrap to avoid the "magic"
695 if ((c = el->el_display[el->el_cursor.v][el->el_cursor.h])
697 term_overwrite(el, &c, 1);
703 } else /* no wrap, but cursor stays on screen */
704 el->el_cursor.h = el->el_term.t_size.h - 1;
709 /* term_deletechars():
710 * Delete num characters
713 term_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_term.t_size.h) {
726 (void) fprintf(el->el_errfile,
727 "term_deletechars: num is riduculous: %d\r\n", 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 term_tputs(el, tgoto(Str(T_DC), num, num), num);
737 if (GoodStr(T_dm)) /* if I have delete mode */
738 term_tputs(el, Str(T_dm), 1);
740 if (GoodStr(T_dc)) /* else do one at a time */
742 term_tputs(el, Str(T_dc), 1);
744 if (GoodStr(T_ed)) /* if I have delete mode */
745 term_tputs(el, Str(T_ed), 1);
749 /* term_insertwrite():
750 * Puts terminal in insert character mode or inserts num
751 * characters in the line
754 term_insertwrite(EditLine *el, char *cp, int num)
758 if (!EL_CAN_INSERT) {
760 (void) fprintf(el->el_errfile, " ERROR: cannot insert \n");
761 #endif /* DEBUG_EDIT */
764 if (num > el->el_term.t_size.h) {
766 (void) fprintf(el->el_errfile,
767 "StartInsert: num is riduculous: %d\r\n", num);
768 #endif /* DEBUG_SCREEN */
771 if (GoodStr(T_IC)) /* if I have multiple insert */
772 if ((num > 1) || !GoodStr(T_ic)) {
773 /* if ic would be more expensive */
774 term_tputs(el, tgoto(Str(T_IC), num, num), num);
775 term_overwrite(el, cp, (size_t)num);
776 /* this updates el_cursor.h */
779 if (GoodStr(T_im) && GoodStr(T_ei)) { /* if I have insert mode */
780 term_tputs(el, Str(T_im), 1);
782 el->el_cursor.h += num;
784 term__putc(el, *cp++);
787 if (GoodStr(T_ip)) /* have to make num chars insert */
788 term_tputs(el, Str(T_ip), 1);
790 term_tputs(el, Str(T_ei), 1);
794 if (GoodStr(T_ic)) /* have to make num chars insert */
795 term_tputs(el, Str(T_ic), 1);
797 term__putc(el, *cp++);
801 if (GoodStr(T_ip)) /* have to make num chars insert */
802 term_tputs(el, Str(T_ip), 1);
803 /* pad the inserted char */
810 * clear to end of line. There are num characters to clear
813 term_clear_EOL(EditLine *el, int num)
817 if (EL_CAN_CEOL && GoodStr(T_ce))
818 term_tputs(el, Str(T_ce), 1);
820 for (i = 0; i < num; i++)
822 el->el_cursor.h += num; /* have written num spaces */
827 /* term_clear_screen():
831 term_clear_screen(EditLine *el)
832 { /* clear the whole screen and home */
835 /* send the clear screen code */
836 term_tputs(el, Str(T_cl), Val(T_li));
837 else if (GoodStr(T_ho) && GoodStr(T_cd)) {
838 term_tputs(el, Str(T_ho), Val(T_li)); /* home */
839 /* clear to bottom of screen */
840 term_tputs(el, Str(T_cd), Val(T_li));
842 term__putc(el, '\r');
843 term__putc(el, '\n');
849 * Beep the way the terminal wants us
852 term_beep(EditLine *el)
855 /* what termcap says we should use */
856 term_tputs(el, Str(T_bl), 1);
858 term__putc(el, '\007'); /* an ASCII bell; ^G */
863 /* term_clear_to_bottom():
864 * Clear to the bottom of the screen
867 term_clear_to_bottom(EditLine *el)
870 term_tputs(el, Str(T_cd), Val(T_li));
871 else if (GoodStr(T_ce))
872 term_tputs(el, Str(T_ce), Val(T_li));
877 term_get(EditLine *el, const char **term)
879 *term = el->el_term.t_name;
884 * Read in the terminal capabilities from the requested terminal
887 term_set(EditLine *el, const char *term)
890 char buf[TC_BUFSIZE];
892 const struct termcapstr *t;
896 (void) sigemptyset(&nset);
897 (void) sigaddset(&nset, SIGWINCH);
898 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
904 term = getenv("TERM");
906 if (!term || !term[0])
909 if (strcmp(term, "emacs") == 0)
910 el->el_flags |= EDIT_DISABLED;
912 memset(el->el_term.t_cap, 0, TC_BUFSIZE);
914 i = tgetent(el->el_term.t_cap, term);
918 (void) fprintf(el->el_errfile,
919 "Cannot read termcap database;\n");
921 (void) fprintf(el->el_errfile,
922 "No entry for terminal type \"%s\";\n", term);
923 (void) fprintf(el->el_errfile,
924 "using dumb terminal settings.\n");
925 Val(T_co) = 80; /* do a dumb terminal */
926 Val(T_pt) = Val(T_km) = Val(T_li) = 0;
927 Val(T_xt) = Val(T_MT);
928 for (t = tstr; t->name != NULL; t++)
929 term_alloc(el, t, NULL);
931 /* auto/magic margins */
932 Val(T_am) = tgetflag("am");
933 Val(T_xn) = tgetflag("xn");
935 Val(T_pt) = tgetflag("pt");
936 Val(T_xt) = tgetflag("xt");
937 /* do we have a meta? */
938 Val(T_km) = tgetflag("km");
939 Val(T_MT) = tgetflag("MT");
941 Val(T_co) = tgetnum("co");
942 Val(T_li) = tgetnum("li");
943 for (t = tstr; t->name != NULL; t++) {
944 /* XXX: some systems' tgetstr needs non const */
945 term_alloc(el, t, tgetstr(strchr(t->name, *t->name),
951 Val(T_co) = 80; /* just in case */
955 el->el_term.t_size.v = Val(T_co);
956 el->el_term.t_size.h = Val(T_li);
960 /* get the correct window size */
961 (void) term_get_size(el, &lins, &cols);
962 if (term_change_size(el, lins, cols) == -1)
964 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
966 el->el_term.t_name = term;
967 return (i <= 0 ? -1 : 0);
972 * Return the new window size in lines and cols, and
973 * true if the size was changed.
976 term_get_size(EditLine *el, int *lins, int *cols)
985 if (ioctl(el->el_infd, TIOCGWINSZ, (ioctl_t) & ws) != -1) {
996 if (ioctl(el->el_infd, TIOCGSIZE, (ioctl_t) & ts) != -1) {
1000 *lins = ts.ts_lines;
1004 return (Val(T_co) != *cols || Val(T_li) != *lins);
1008 /* term_change_size():
1009 * Change the size of the terminal
1012 term_change_size(EditLine *el, int lins, int cols)
1017 Val(T_co) = (cols < 2) ? 80 : cols;
1018 Val(T_li) = (lins < 1) ? 24 : lins;
1020 /* re-make display buffers */
1021 if (term_rebuffer_display(el) == -1)
1023 re_clear_display(el);
1028 /* term_init_arrow():
1029 * Initialize the arrow key bindings from termcap
1032 term_init_arrow(EditLine *el)
1034 fkey_t *arrow = el->el_term.t_fkey;
1036 arrow[A_K_DN].name = "down";
1037 arrow[A_K_DN].key = T_kd;
1038 arrow[A_K_DN].fun.cmd = ED_NEXT_HISTORY;
1039 arrow[A_K_DN].type = XK_CMD;
1041 arrow[A_K_UP].name = "up";
1042 arrow[A_K_UP].key = T_ku;
1043 arrow[A_K_UP].fun.cmd = ED_PREV_HISTORY;
1044 arrow[A_K_UP].type = XK_CMD;
1046 arrow[A_K_LT].name = "left";
1047 arrow[A_K_LT].key = T_kl;
1048 arrow[A_K_LT].fun.cmd = ED_PREV_CHAR;
1049 arrow[A_K_LT].type = XK_CMD;
1051 arrow[A_K_RT].name = "right";
1052 arrow[A_K_RT].key = T_kr;
1053 arrow[A_K_RT].fun.cmd = ED_NEXT_CHAR;
1054 arrow[A_K_RT].type = XK_CMD;
1056 arrow[A_K_HO].name = "home";
1057 arrow[A_K_HO].key = T_kh;
1058 arrow[A_K_HO].fun.cmd = ED_MOVE_TO_BEG;
1059 arrow[A_K_HO].type = XK_CMD;
1061 arrow[A_K_EN].name = "end";
1062 arrow[A_K_EN].key = T_at7;
1063 arrow[A_K_EN].fun.cmd = ED_MOVE_TO_END;
1064 arrow[A_K_EN].type = XK_CMD;
1068 /* term_reset_arrow():
1069 * Reset arrow key bindings
1072 term_reset_arrow(EditLine *el)
1074 fkey_t *arrow = el->el_term.t_fkey;
1075 static const char strA[] = {033, '[', 'A', '\0'};
1076 static const char strB[] = {033, '[', 'B', '\0'};
1077 static const char strC[] = {033, '[', 'C', '\0'};
1078 static const char strD[] = {033, '[', 'D', '\0'};
1079 static const char strH[] = {033, '[', 'H', '\0'};
1080 static const char strF[] = {033, '[', 'F', '\0'};
1081 static const char str1[] = {033, '[', '1', '~', '\0'};
1082 static const char str4[] = {033, '[', '4', '~', '\0'};
1083 static const char stOA[] = {033, 'O', 'A', '\0'};
1084 static const char stOB[] = {033, 'O', 'B', '\0'};
1085 static const char stOC[] = {033, 'O', 'C', '\0'};
1086 static const char stOD[] = {033, 'O', 'D', '\0'};
1087 static const char stOH[] = {033, 'O', 'H', '\0'};
1088 static const char stOF[] = {033, 'O', 'F', '\0'};
1090 key_add(el, strA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1091 key_add(el, strB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1092 key_add(el, strC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1093 key_add(el, strD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1094 key_add(el, strH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1095 key_add(el, strF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1096 key_add(el, str1, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1097 key_add(el, str4, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1098 key_add(el, stOA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1099 key_add(el, stOB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1100 key_add(el, stOC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1101 key_add(el, stOD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1102 key_add(el, stOH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1103 key_add(el, stOF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1105 if (el->el_map.type == MAP_VI) {
1106 key_add(el, &strA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1107 key_add(el, &strB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1108 key_add(el, &strC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1109 key_add(el, &strD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1110 key_add(el, &strH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1111 key_add(el, &strF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1112 key_add(el, &str1[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1113 key_add(el, &str4[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1114 key_add(el, &stOA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1115 key_add(el, &stOB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1116 key_add(el, &stOC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1117 key_add(el, &stOD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1118 key_add(el, &stOH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1119 key_add(el, &stOF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1124 /* term_set_arrow():
1125 * Set an arrow key binding
1128 term_set_arrow(EditLine *el, const char *name, key_value_t *fun, int type)
1130 fkey_t *arrow = el->el_term.t_fkey;
1133 for (i = 0; i < A_K_NKEYS; i++)
1134 if (strcmp(name, arrow[i].name) == 0) {
1135 arrow[i].fun = *fun;
1136 arrow[i].type = type;
1143 /* term_clear_arrow():
1144 * Clear an arrow key binding
1147 term_clear_arrow(EditLine *el, const char *name)
1149 fkey_t *arrow = el->el_term.t_fkey;
1152 for (i = 0; i < A_K_NKEYS; i++)
1153 if (strcmp(name, arrow[i].name) == 0) {
1154 arrow[i].type = XK_NOD;
1161 /* term_print_arrow():
1162 * Print the arrow key bindings
1165 term_print_arrow(EditLine *el, const char *name)
1168 fkey_t *arrow = el->el_term.t_fkey;
1170 for (i = 0; i < A_K_NKEYS; i++)
1171 if (*name == '\0' || strcmp(name, arrow[i].name) == 0)
1172 if (arrow[i].type != XK_NOD)
1173 key_kprint(el, arrow[i].name, &arrow[i].fun,
1178 /* term_bind_arrow():
1179 * Bind the arrow keys
1182 term_bind_arrow(EditLine *el)
1185 const el_action_t *dmap;
1188 fkey_t *arrow = el->el_term.t_fkey;
1190 /* Check if the components needed are initialized */
1191 if (el->el_term.t_buf == NULL || el->el_map.key == NULL)
1194 map = el->el_map.type == MAP_VI ? el->el_map.alt : el->el_map.key;
1195 dmap = el->el_map.type == MAP_VI ? el->el_map.vic : el->el_map.emacs;
1197 term_reset_arrow(el);
1199 for (i = 0; i < A_K_NKEYS; i++) {
1200 p = el->el_term.t_str[arrow[i].key];
1202 j = (unsigned char) *p;
1204 * Assign the arrow keys only if:
1206 * 1. They are multi-character arrow keys and the user
1207 * has not re-assigned the leading character, or
1208 * has re-assigned the leading character to be
1209 * ED_SEQUENCE_LEAD_IN
1210 * 2. They are single arrow keys pointing to an
1213 if (arrow[i].type == XK_NOD)
1214 key_clear(el, map, p);
1216 if (p[1] && (dmap[j] == map[j] ||
1217 map[j] == ED_SEQUENCE_LEAD_IN)) {
1218 key_add(el, p, &arrow[i].fun,
1220 map[j] = ED_SEQUENCE_LEAD_IN;
1221 } else if (map[j] == ED_UNASSIGNED) {
1222 key_clear(el, map, p);
1223 if (arrow[i].type == XK_CMD)
1224 map[j] = arrow[i].fun.cmd;
1226 key_add(el, p, &arrow[i].fun,
1241 if (term_outfile == NULL)
1243 return fputc(c, term_outfile);
1247 term_tputs(EditLine *el, const char *cap, int affcnt)
1250 pthread_mutex_lock(&term_mutex);
1252 term_outfile = el->el_outfile;
1253 (void)tputs(cap, affcnt, term_putc);
1255 pthread_mutex_unlock(&term_mutex);
1263 term__putc(EditLine *el, int c)
1266 return fputc(c, el->el_outfile);
1273 term__flush(EditLine *el)
1276 (void) fflush(el->el_outfile);
1280 * Write the given character out, in a human readable form
1283 term_writec(EditLine *el, int c)
1286 size_t cnt = key__decode_char(buf, sizeof(buf), 0, c);
1288 term_overwrite(el, buf, (size_t)cnt);
1294 * Print the current termcap characteristics
1298 term_telltc(EditLine *el, int argc __unused,
1299 const char **argv __unused)
1301 const struct termcapstr *t;
1303 char upbuf[EL_BUFSIZ];
1305 (void) fprintf(el->el_outfile, "\n\tYour terminal has the\n");
1306 (void) fprintf(el->el_outfile, "\tfollowing characteristics:\n\n");
1307 (void) fprintf(el->el_outfile, "\tIt has %d columns and %d lines\n",
1308 Val(T_co), Val(T_li));
1309 (void) fprintf(el->el_outfile,
1310 "\tIt has %s meta key\n", EL_HAS_META ? "a" : "no");
1311 (void) fprintf(el->el_outfile,
1312 "\tIt can%suse tabs\n", EL_CAN_TAB ? " " : "not ");
1313 (void) fprintf(el->el_outfile, "\tIt %s automatic margins\n",
1314 EL_HAS_AUTO_MARGINS ? "has" : "does not have");
1315 if (EL_HAS_AUTO_MARGINS)
1316 (void) fprintf(el->el_outfile, "\tIt %s magic margins\n",
1317 EL_HAS_MAGIC_MARGINS ? "has" : "does not have");
1319 for (t = tstr, ts = el->el_term.t_str; t->name != NULL; t++, ts++) {
1322 (void) key__decode_str(*ts, upbuf, sizeof(upbuf), "");
1327 (void) fprintf(el->el_outfile, "\t%25s (%s) == %s\n",
1328 t->long_name, t->name, ub);
1330 (void) fputc('\n', el->el_outfile);
1336 * Change the current terminal characteristics
1340 term_settc(EditLine *el, int argc __unused,
1343 const struct termcapstr *ts;
1344 const struct termcapval *tv;
1345 const char *what, *how;
1347 if (argv == NULL || argv[1] == NULL || argv[2] == NULL)
1354 * Do the strings first
1356 for (ts = tstr; ts->name != NULL; ts++)
1357 if (strcmp(ts->name, what) == 0)
1360 if (ts->name != NULL) {
1361 term_alloc(el, ts, how);
1366 * Do the numeric ones second
1368 for (tv = tval; tv->name != NULL; tv++)
1369 if (strcmp(tv->name, what) == 0)
1372 if (tv->name != NULL)
1375 if (tv == &tval[T_pt] || tv == &tval[T_km] ||
1376 tv == &tval[T_am] || tv == &tval[T_xn]) {
1377 if (strcmp(how, "yes") == 0)
1378 el->el_term.t_val[tv - tval] = 1;
1379 else if (strcmp(how, "no") == 0)
1380 el->el_term.t_val[tv - tval] = 0;
1382 (void) fprintf(el->el_errfile,
1383 "%s: Bad value `%s'.\n", argv[0], how);
1387 if (term_change_size(el, Val(T_li), Val(T_co)) == -1)
1394 i = strtol(how, &ep, 10);
1396 (void) fprintf(el->el_errfile,
1397 "%s: Bad value `%s'.\n", argv[0], how);
1400 el->el_term.t_val[tv - tval] = (int) i;
1401 el->el_term.t_size.v = Val(T_co);
1402 el->el_term.t_size.h = Val(T_li);
1403 if (tv == &tval[T_co] || tv == &tval[T_li])
1404 if (term_change_size(el, Val(T_li), Val(T_co))
1413 * Get the current terminal characteristics
1417 term_gettc(EditLine *el, int argc __unused, char **argv)
1419 const struct termcapstr *ts;
1420 const struct termcapval *tv;
1424 if (argv == NULL || argv[1] == NULL || argv[2] == NULL)
1431 * Do the strings first
1433 for (ts = tstr; ts->name != NULL; ts++)
1434 if (strcmp(ts->name, what) == 0)
1437 if (ts->name != NULL) {
1438 *(char **)how = el->el_term.t_str[ts - tstr];
1442 * Do the numeric ones second
1444 for (tv = tval; tv->name != NULL; tv++)
1445 if (strcmp(tv->name, what) == 0)
1448 if (tv->name == NULL)
1451 if (tv == &tval[T_pt] || tv == &tval[T_km] ||
1452 tv == &tval[T_am] || tv == &tval[T_xn]) {
1453 static char yes[] = "yes";
1454 static char no[] = "no";
1455 if (el->el_term.t_val[tv - tval])
1456 *(char **)how = yes;
1461 *(int *)how = el->el_term.t_val[tv - tval];
1467 * Print the termcap string out with variable substitution
1471 term_echotc(EditLine *el, int argc __unused,
1474 char *cap, *scap, *ep;
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 (strcmp(*argv, "tabs") == 0) {
1506 (void) fprintf(el->el_outfile, fmts, EL_CAN_TAB ? "yes" : "no");
1508 } else if (strcmp(*argv, "meta") == 0) {
1509 (void) fprintf(el->el_outfile, fmts, Val(T_km) ? "yes" : "no");
1511 } else if (strcmp(*argv, "xn") == 0) {
1512 (void) fprintf(el->el_outfile, fmts, EL_HAS_MAGIC_MARGINS ?
1515 } else if (strcmp(*argv, "am") == 0) {
1516 (void) fprintf(el->el_outfile, fmts, EL_HAS_AUTO_MARGINS ?
1519 } else if (strcmp(*argv, "baud") == 0) {
1523 for (i = 0; baud_rate[i].b_name != NULL; i++)
1524 if (el->el_tty.t_speed == baud_rate[i].b_rate) {
1525 (void) fprintf(el->el_outfile, fmts,
1526 baud_rate[i].b_name);
1529 (void) fprintf(el->el_outfile, fmtd, 0);
1531 (void) fprintf(el->el_outfile, fmtd, (int)el->el_tty.t_speed);
1534 } else if (strcmp(*argv, "rows") == 0 || strcmp(*argv, "lines") == 0) {
1535 (void) fprintf(el->el_outfile, fmtd, Val(T_li));
1537 } else if (strcmp(*argv, "cols") == 0) {
1538 (void) fprintf(el->el_outfile, fmtd, Val(T_co));
1542 * Try to use our local definition first
1545 for (t = tstr; t->name != NULL; t++)
1546 if (strcmp(t->name, *argv) == 0) {
1547 scap = el->el_term.t_str[t - tstr];
1550 if (t->name == NULL) {
1551 /* XXX: some systems' tgetstr needs non const */
1552 scap = tgetstr(strchr(*argv, **argv), &area);
1554 if (!scap || scap[0] == '\0') {
1556 (void) fprintf(el->el_errfile,
1557 "echotc: Termcap parameter `%s' not found.\n",
1562 * Count home many values we need for this capability.
1564 for (cap = scap, arg_need = 0; *cap; cap++)
1584 * hpux has lot's of them...
1587 (void) fprintf(el->el_errfile,
1588 "echotc: Warning: unknown termcap %% `%c'.\n",
1590 /* This is bad, but I won't complain */
1597 if (*argv && *argv[0]) {
1599 (void) fprintf(el->el_errfile,
1600 "echotc: Warning: Extra argument `%s'.\n",
1604 term_tputs(el, scap, 1);
1608 if (!*argv || *argv[0] == '\0') {
1610 (void) fprintf(el->el_errfile,
1611 "echotc: Warning: Missing argument.\n");
1615 i = strtol(*argv, &ep, 10);
1616 if (*ep != '\0' || i < 0) {
1618 (void) fprintf(el->el_errfile,
1619 "echotc: Bad value `%s' for rows.\n",
1625 if (*argv && *argv[0]) {
1627 (void) fprintf(el->el_errfile,
1628 "echotc: Warning: Extra argument `%s'.\n",
1632 term_tputs(el, tgoto(scap, arg_cols, arg_rows), 1);
1635 /* This is wrong, but I will ignore it... */
1637 (void) fprintf(el->el_errfile,
1638 "echotc: Warning: Too many required arguments (%d).\n",
1643 if (!*argv || *argv[0] == '\0') {
1645 (void) fprintf(el->el_errfile,
1646 "echotc: Warning: Missing argument.\n");
1649 i = strtol(*argv, &ep, 10);
1650 if (*ep != '\0' || i < 0) {
1652 (void) fprintf(el->el_errfile,
1653 "echotc: Bad value `%s' for cols.\n",
1659 if (!*argv || *argv[0] == '\0') {
1661 (void) fprintf(el->el_errfile,
1662 "echotc: Warning: Missing argument.\n");
1665 i = strtol(*argv, &ep, 10);
1666 if (*ep != '\0' || i < 0) {
1668 (void) fprintf(el->el_errfile,
1669 "echotc: Bad value `%s' for rows.\n",
1676 (void) fprintf(el->el_errfile,
1677 "echotc: Bad value `%s'.\n", *argv);
1681 if (*argv && *argv[0]) {
1683 (void) fprintf(el->el_errfile,
1684 "echotc: Warning: Extra argument `%s'.\n",
1688 term_tputs(el, tgoto(scap, arg_cols, arg_rows), arg_rows);