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 * @(#)term.c 8.2 (Berkeley) 4/30/95
33 * $NetBSD: term.c,v 1.40 2004/05/22 23:21:28 christos Exp $
34 * $DragonFly: src/lib/libedit/term.c,v 1.4 2005/11/13 11:58:30 corecode Exp $
40 * term.c: Editor/termcap-curses interface
41 * We have to declare a static variable here, since the
42 * termcap putchar routine does not take an argument!
58 /* Solaris's term.h does horrid things. */
59 #if (defined(HAVE_TERM_H) && !defined(SUNOS))
62 #include <sys/types.h>
63 #include <sys/ioctl.h>
68 * IMPORTANT NOTE: these routines are allowed to look at the current screen
69 * and the current possition assuming that it is correct. If this is not
70 * true, then the update will be WRONG! This is (should be) a valid
74 #define TC_BUFSIZE 2048
76 #define GoodStr(a) (el->el_term.t_str[a] != NULL && \
77 el->el_term.t_str[a][0] != '\0')
78 #define Str(a) el->el_term.t_str[a]
79 #define Val(a) el->el_term.t_val[a]
82 private const struct {
153 private const struct termcapstr {
155 const char *long_name;
158 { "al", "add new blank line" },
160 { "bl", "audible bell" },
162 { "cd", "clear to bottom" },
164 { "ce", "clear to end of line" },
166 { "ch", "cursor to horiz pos" },
168 { "cl", "clear screen" },
170 { "dc", "delete a character" },
172 { "dl", "delete a line" },
174 { "dm", "start delete mode" },
176 { "ed", "end delete mode" },
178 { "ei", "end insert mode" },
180 { "fs", "cursor from status line" },
182 { "ho", "home cursor" },
184 { "ic", "insert character" },
186 { "im", "start insert mode" },
188 { "ip", "insert padding" },
190 { "kd", "sends cursor down" },
192 { "kl", "sends cursor left" },
194 { "kr", "sends cursor right" },
196 { "ku", "sends cursor up" },
198 { "md", "begin bold" },
200 { "me", "end attributes" },
202 { "nd", "non destructive space" },
204 { "se", "end standout" },
206 { "so", "begin standout" },
208 { "ts", "cursor to status line" },
210 { "up", "cursor up one" },
212 { "us", "begin underline" },
214 { "ue", "end underline" },
216 { "vb", "visible bell" },
218 { "DC", "delete multiple chars" },
220 { "DO", "cursor down multiple" },
222 { "IC", "insert multiple chars" },
224 { "LE", "cursor left multiple" },
226 { "RI", "cursor right multiple" },
228 { "UP", "cursor up multiple" },
230 { "kh", "send cursor home" },
232 { "@7", "send cursor end" },
237 private const struct termcapval {
239 const char *long_name;
242 { "am", "has automatic margins" },
244 { "pt", "has physical tabs" },
246 { "li", "Number of lines" },
248 { "co", "Number of columns" },
250 { "km", "Has meta key" },
252 { "xt", "Tab chars destructive" },
254 { "xn", "newline ignored at right margin" },
256 { "MT", "Has meta key" }, /* XXX? */
260 /* do two or more of the attributes use me */
262 private void term_setflags(EditLine *);
263 private int term_rebuffer_display(EditLine *);
264 private void term_free_display(EditLine *);
265 private int term_alloc_display(EditLine *);
266 private void term_alloc(EditLine *, const struct termcapstr *, const char *);
267 private void term_init_arrow(EditLine *);
268 private void term_reset_arrow(EditLine *);
271 private FILE *term_outfile = NULL; /* XXX: How do we fix that? */
275 * Set the terminal capability flags
278 term_setflags(EditLine *el)
281 if (el->el_tty.t_tabs)
282 EL_FLAGS |= (Val(T_pt) && !Val(T_xt)) ? TERM_CAN_TAB : 0;
284 EL_FLAGS |= (Val(T_km) || Val(T_MT)) ? TERM_HAS_META : 0;
285 EL_FLAGS |= GoodStr(T_ce) ? TERM_CAN_CEOL : 0;
286 EL_FLAGS |= (GoodStr(T_dc) || GoodStr(T_DC)) ? TERM_CAN_DELETE : 0;
287 EL_FLAGS |= (GoodStr(T_im) || GoodStr(T_ic) || GoodStr(T_IC)) ?
289 EL_FLAGS |= (GoodStr(T_up) || GoodStr(T_UP)) ? TERM_CAN_UP : 0;
290 EL_FLAGS |= Val(T_am) ? TERM_HAS_AUTO_MARGINS : 0;
291 EL_FLAGS |= Val(T_xn) ? TERM_HAS_MAGIC_MARGINS : 0;
293 if (GoodStr(T_me) && GoodStr(T_ue))
294 EL_FLAGS |= (strcmp(Str(T_me), Str(T_ue)) == 0) ?
297 EL_FLAGS &= ~TERM_CAN_ME;
298 if (GoodStr(T_me) && GoodStr(T_se))
299 EL_FLAGS |= (strcmp(Str(T_me), Str(T_se)) == 0) ?
305 (void) fprintf(el->el_errfile,
306 "WARNING: Your terminal cannot move up.\n");
307 (void) fprintf(el->el_errfile,
308 "Editing may be odd for long lines.\n");
311 (void) fprintf(el->el_errfile, "no clear EOL capability.\n");
313 (void) fprintf(el->el_errfile, "no delete char capability.\n");
315 (void) fprintf(el->el_errfile, "no insert char capability.\n");
316 #endif /* DEBUG_SCREEN */
321 * Initialize the terminal stuff
324 term_init(EditLine *el)
327 el->el_term.t_buf = (char *) el_malloc(TC_BUFSIZE);
328 if (el->el_term.t_buf == NULL)
330 el->el_term.t_cap = (char *) el_malloc(TC_BUFSIZE);
331 if (el->el_term.t_cap == NULL)
333 el->el_term.t_fkey = (fkey_t *) el_malloc(A_K_NKEYS * sizeof(fkey_t));
334 if (el->el_term.t_fkey == NULL)
336 el->el_term.t_loc = 0;
337 el->el_term.t_str = (char **) el_malloc(T_str * sizeof(char *));
338 if (el->el_term.t_str == NULL)
340 (void) memset(el->el_term.t_str, 0, T_str * sizeof(char *));
341 el->el_term.t_val = (int *) el_malloc(T_val * sizeof(int));
342 if (el->el_term.t_val == NULL)
344 (void) memset(el->el_term.t_val, 0, T_val * sizeof(int));
345 term_outfile = el->el_outfile;
346 (void) term_set(el, NULL);
352 * Clean up the terminal stuff
355 term_end(EditLine *el)
358 el_free((ptr_t) el->el_term.t_buf);
359 el->el_term.t_buf = NULL;
360 el_free((ptr_t) el->el_term.t_cap);
361 el->el_term.t_cap = NULL;
362 el->el_term.t_loc = 0;
363 el_free((ptr_t) el->el_term.t_str);
364 el->el_term.t_str = NULL;
365 el_free((ptr_t) el->el_term.t_val);
366 el->el_term.t_val = NULL;
367 el_free((ptr_t) el->el_term.t_fkey);
368 el->el_term.t_fkey = NULL;
369 term_free_display(el);
374 * Maintain a string pool for termcap strings
377 term_alloc(EditLine *el, const struct termcapstr *t, const char *cap)
379 char termbuf[TC_BUFSIZE];
381 char **tlist = el->el_term.t_str;
382 char **tmp, **str = &tlist[t - tstr];
384 if (cap == NULL || *cap == '\0') {
390 tlen = *str == NULL ? 0 : strlen(*str);
393 * New string is shorter; no need to allocate space
396 (void) strcpy(*str, cap); /* XXX strcpy is safe */
400 * New string is longer; see if we have enough space to append
402 if (el->el_term.t_loc + 3 < TC_BUFSIZE) {
403 /* XXX strcpy is safe */
404 (void) strcpy(*str = &el->el_term.t_buf[el->el_term.t_loc],
406 el->el_term.t_loc += clen + 1; /* one for \0 */
410 * Compact our buffer; no need to check compaction, cause we know it
414 for (tmp = tlist; tmp < &tlist[T_str]; tmp++)
415 if (*tmp != NULL && *tmp != '\0' && *tmp != *str) {
418 for (ptr = *tmp; *ptr != '\0'; termbuf[tlen++] = *ptr++)
420 termbuf[tlen++] = '\0';
422 memcpy(el->el_term.t_buf, termbuf, TC_BUFSIZE);
423 el->el_term.t_loc = tlen;
424 if (el->el_term.t_loc + 3 >= TC_BUFSIZE) {
425 (void) fprintf(el->el_errfile,
426 "Out of termcap string space.\n");
429 /* XXX strcpy is safe */
430 (void) strcpy(*str = &el->el_term.t_buf[el->el_term.t_loc], cap);
431 el->el_term.t_loc += clen + 1; /* one for \0 */
436 /* term_rebuffer_display():
437 * Rebuffer the display after the screen changed size
440 term_rebuffer_display(EditLine *el)
442 coord_t *c = &el->el_term.t_size;
444 term_free_display(el);
449 if (term_alloc_display(el) == -1)
455 /* term_alloc_display():
456 * Allocate a new display.
459 term_alloc_display(EditLine *el)
463 coord_t *c = &el->el_term.t_size;
465 b = (char **) el_malloc((size_t) (sizeof(char *) * (c->v + 1)));
468 for (i = 0; i < c->v; i++) {
469 b[i] = (char *) el_malloc((size_t) (sizeof(char) * (c->h + 1)));
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)));
490 /* term_free_display():
491 * Free the display buffers
494 term_free_display(EditLine *el)
500 el->el_display = NULL;
502 for (bufp = b; *bufp != NULL; bufp++)
503 el_free((ptr_t) * bufp);
507 el->el_vdisplay = NULL;
509 for (bufp = b; *bufp != NULL; bufp++)
510 el_free((ptr_t) * bufp);
516 /* term_move_to_line():
517 * move to line <where> (first line == 0)
518 * as efficiently as possible
521 term_move_to_line(EditLine *el, int where)
525 if (where == el->el_cursor.v)
528 if (where > el->el_term.t_size.v) {
530 (void) fprintf(el->el_errfile,
531 "term_move_to_line: where is ridiculous: %d\r\n", where);
532 #endif /* DEBUG_SCREEN */
535 if ((del = where - el->el_cursor.v) > 0) {
537 if (EL_HAS_AUTO_MARGINS &&
538 el->el_display[el->el_cursor.v][0] != '\0') {
539 /* move without newline */
540 term_move_to_char(el, el->el_term.t_size.h - 1);
542 &el->el_display[el->el_cursor.v][el->el_cursor.h],
547 if ((del > 1) && GoodStr(T_DO)) {
548 (void) tputs(tgoto(Str(T_DO), del, del),
552 for (; del > 0; del--)
554 /* because the \n will become \r\n */
559 } else { /* del < 0 */
560 if (GoodStr(T_UP) && (-del > 1 || !GoodStr(T_up)))
561 (void) tputs(tgoto(Str(T_UP), -del, -del), -del,
565 for (; del < 0; del++)
566 (void) tputs(Str(T_up), 1, term__putc);
569 el->el_cursor.v = where;/* now where is here */
573 /* term_move_to_char():
574 * Move to the character position specified
577 term_move_to_char(EditLine *el, int where)
582 if (where == el->el_cursor.h)
585 if (where > el->el_term.t_size.h) {
587 (void) fprintf(el->el_errfile,
588 "term_move_to_char: where is riduculous: %d\r\n", where);
589 #endif /* DEBUG_SCREEN */
592 if (!where) { /* if where is first column */
593 term__putc('\r'); /* do a CR */
597 del = where - el->el_cursor.h;
599 if ((del < -4 || del > 4) && GoodStr(T_ch))
600 /* go there directly */
601 (void) tputs(tgoto(Str(T_ch), where, where), where, term__putc);
603 if (del > 0) { /* moving forward */
604 if ((del > 4) && GoodStr(T_RI))
605 (void) tputs(tgoto(Str(T_RI), del, del),
608 /* if I can do tabs, use them */
610 if ((el->el_cursor.h & 0370) !=
612 /* if not within tab stop */
614 (el->el_cursor.h & 0370);
619 el->el_cursor.h = where & 0370;
623 * it's usually cheaper to just write the
627 * NOTE THAT term_overwrite() WILL CHANGE
631 &el->el_display[el->el_cursor.v][el->el_cursor.h],
632 where - el->el_cursor.h);
635 } else { /* del < 0 := moving backward */
636 if ((-del > 4) && GoodStr(T_LE))
637 (void) tputs(tgoto(Str(T_LE), -del, -del),
639 else { /* can't go directly there */
641 * if the "cost" is greater than the "cost"
645 ((unsigned int)-del >
646 (((unsigned int) where >> 3) +
649 term__putc('\r'); /* do a CR */
651 goto mc_again; /* and try again */
653 for (i = 0; i < -del; i++)
658 el->el_cursor.h = where; /* now where is here */
663 * Overstrike num characters
666 term_overwrite(EditLine *el, const char *cp, int n)
669 return; /* catch bugs */
671 if (n > el->el_term.t_size.h) {
673 (void) fprintf(el->el_errfile,
674 "term_overwrite: n is riduculous: %d\r\n", n);
675 #endif /* DEBUG_SCREEN */
683 if (el->el_cursor.h >= el->el_term.t_size.h) { /* wrap? */
684 if (EL_HAS_AUTO_MARGINS) { /* yes */
687 if (EL_HAS_MAGIC_MARGINS) {
688 /* force the wrap to avoid the "magic"
691 if ((c = el->el_display[el->el_cursor.v][el->el_cursor.h])
693 term_overwrite(el, &c, 1);
698 } else /* no wrap, but cursor stays on screen */
699 el->el_cursor.h = el->el_term.t_size.h;
704 /* term_deletechars():
705 * Delete num characters
708 term_deletechars(EditLine *el, int num)
713 if (!EL_CAN_DELETE) {
715 (void) fprintf(el->el_errfile, " ERROR: cannot delete \n");
716 #endif /* DEBUG_EDIT */
719 if (num > el->el_term.t_size.h) {
721 (void) fprintf(el->el_errfile,
722 "term_deletechars: num is riduculous: %d\r\n", num);
723 #endif /* DEBUG_SCREEN */
726 if (GoodStr(T_DC)) /* if I have multiple delete */
727 if ((num > 1) || !GoodStr(T_dc)) { /* if dc would be more
729 (void) tputs(tgoto(Str(T_DC), num, num),
733 if (GoodStr(T_dm)) /* if I have delete mode */
734 (void) tputs(Str(T_dm), 1, term__putc);
736 if (GoodStr(T_dc)) /* else do one at a time */
738 (void) tputs(Str(T_dc), 1, term__putc);
740 if (GoodStr(T_ed)) /* if I have delete mode */
741 (void) tputs(Str(T_ed), 1, term__putc);
745 /* term_insertwrite():
746 * Puts terminal in insert character mode or inserts num
747 * characters in the line
750 term_insertwrite(EditLine *el, char *cp, int num)
754 if (!EL_CAN_INSERT) {
756 (void) fprintf(el->el_errfile, " ERROR: cannot insert \n");
757 #endif /* DEBUG_EDIT */
760 if (num > el->el_term.t_size.h) {
762 (void) fprintf(el->el_errfile,
763 "StartInsert: num is riduculous: %d\r\n", num);
764 #endif /* DEBUG_SCREEN */
767 if (GoodStr(T_IC)) /* if I have multiple insert */
768 if ((num > 1) || !GoodStr(T_ic)) {
769 /* if ic would be more expensive */
770 (void) tputs(tgoto(Str(T_IC), num, num),
772 term_overwrite(el, cp, num);
773 /* this updates el_cursor.h */
776 if (GoodStr(T_im) && GoodStr(T_ei)) { /* if I have insert mode */
777 (void) tputs(Str(T_im), 1, term__putc);
779 el->el_cursor.h += num;
784 if (GoodStr(T_ip)) /* have to make num chars insert */
785 (void) tputs(Str(T_ip), 1, term__putc);
787 (void) tputs(Str(T_ei), 1, term__putc);
791 if (GoodStr(T_ic)) /* have to make num chars insert */
792 (void) tputs(Str(T_ic), 1, term__putc);
799 if (GoodStr(T_ip)) /* have to make num chars insert */
800 (void) tputs(Str(T_ip), 1, term__putc);
801 /* pad the inserted char */
808 * clear to end of line. There are num characters to clear
811 term_clear_EOL(EditLine *el, int num)
815 if (EL_CAN_CEOL && GoodStr(T_ce))
816 (void) tputs(Str(T_ce), 1, term__putc);
818 for (i = 0; i < num; i++)
820 el->el_cursor.h += num; /* have written num spaces */
825 /* term_clear_screen():
829 term_clear_screen(EditLine *el)
830 { /* clear the whole screen and home */
833 /* send the clear screen code */
834 (void) tputs(Str(T_cl), Val(T_li), term__putc);
835 else if (GoodStr(T_ho) && GoodStr(T_cd)) {
836 (void) tputs(Str(T_ho), Val(T_li), term__putc); /* home */
837 /* clear to bottom of screen */
838 (void) tputs(Str(T_cd), Val(T_li), term__putc);
847 * Beep the way the terminal wants us
850 term_beep(EditLine *el)
853 /* what termcap says we should use */
854 (void) tputs(Str(T_bl), 1, term__putc);
856 term__putc('\007'); /* an ASCII bell; ^G */
861 /* term_clear_to_bottom():
862 * Clear to the bottom of the screen
865 term_clear_to_bottom(EditLine *el)
868 (void) tputs(Str(T_cd), Val(T_li), term__putc);
869 else if (GoodStr(T_ce))
870 (void) tputs(Str(T_ce), Val(T_li), term__putc);
875 term_get(EditLine *el, const char **term)
877 *term = el->el_term.t_name;
882 * Read in the terminal capabilities from the requested terminal
885 term_set(EditLine *el, const char *term)
888 char buf[TC_BUFSIZE];
890 const struct termcapstr *t;
894 (void) sigemptyset(&nset);
895 (void) sigaddset(&nset, SIGWINCH);
896 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
902 term = getenv("TERM");
904 if (!term || !term[0])
907 if (strcmp(term, "emacs") == 0)
908 el->el_flags |= EDIT_DISABLED;
910 memset(el->el_term.t_cap, 0, TC_BUFSIZE);
912 i = tgetent(el->el_term.t_cap, term);
916 (void) fprintf(el->el_errfile,
917 "Cannot read termcap database;\n");
919 (void) fprintf(el->el_errfile,
920 "No entry for terminal type \"%s\";\n", term);
921 (void) fprintf(el->el_errfile,
922 "using dumb terminal settings.\n");
923 Val(T_co) = 80; /* do a dumb terminal */
924 Val(T_pt) = Val(T_km) = Val(T_li) = 0;
925 Val(T_xt) = Val(T_MT);
926 for (t = tstr; t->name != NULL; t++)
927 term_alloc(el, t, NULL);
929 /* auto/magic margins */
930 Val(T_am) = tgetflag("am");
931 Val(T_xn) = tgetflag("xn");
933 Val(T_pt) = tgetflag("pt");
934 Val(T_xt) = tgetflag("xt");
935 /* do we have a meta? */
936 Val(T_km) = tgetflag("km");
937 Val(T_MT) = tgetflag("MT");
939 Val(T_co) = tgetnum("co");
940 Val(T_li) = tgetnum("li");
941 for (t = tstr; t->name != NULL; t++) {
942 /* XXX: some systems tgetstr needs non const */
943 term_alloc(el, t, tgetstr(strchr(t->name, *t->name),
949 Val(T_co) = 80; /* just in case */
953 el->el_term.t_size.v = Val(T_co);
954 el->el_term.t_size.h = Val(T_li);
958 /* get the correct window size */
959 (void) term_get_size(el, &lins, &cols);
960 if (term_change_size(el, lins, cols) == -1)
962 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
964 el->el_term.t_name = term;
965 return (i <= 0 ? -1 : 0);
970 * Return the new window size in lines and cols, and
971 * true if the size was changed.
974 term_get_size(EditLine *el, int *lins, int *cols)
983 if (ioctl(el->el_infd, TIOCGWINSZ, (ioctl_t) & ws) != -1) {
994 if (ioctl(el->el_infd, TIOCGSIZE, (ioctl_t) & ts) != -1) {
1002 return (Val(T_co) != *cols || Val(T_li) != *lins);
1006 /* term_change_size():
1007 * Change the size of the terminal
1010 term_change_size(EditLine *el, int lins, int cols)
1015 Val(T_co) = (cols < 2) ? 80 : cols;
1016 Val(T_li) = (lins < 1) ? 24 : lins;
1018 /* re-make display buffers */
1019 if (term_rebuffer_display(el) == -1)
1021 re_clear_display(el);
1026 /* term_init_arrow():
1027 * Initialize the arrow key bindings from termcap
1030 term_init_arrow(EditLine *el)
1032 fkey_t *arrow = el->el_term.t_fkey;
1034 arrow[A_K_DN].name = "down";
1035 arrow[A_K_DN].key = T_kd;
1036 arrow[A_K_DN].fun.cmd = ED_NEXT_HISTORY;
1037 arrow[A_K_DN].type = XK_CMD;
1039 arrow[A_K_UP].name = "up";
1040 arrow[A_K_UP].key = T_ku;
1041 arrow[A_K_UP].fun.cmd = ED_PREV_HISTORY;
1042 arrow[A_K_UP].type = XK_CMD;
1044 arrow[A_K_LT].name = "left";
1045 arrow[A_K_LT].key = T_kl;
1046 arrow[A_K_LT].fun.cmd = ED_PREV_CHAR;
1047 arrow[A_K_LT].type = XK_CMD;
1049 arrow[A_K_RT].name = "right";
1050 arrow[A_K_RT].key = T_kr;
1051 arrow[A_K_RT].fun.cmd = ED_NEXT_CHAR;
1052 arrow[A_K_RT].type = XK_CMD;
1054 arrow[A_K_HO].name = "home";
1055 arrow[A_K_HO].key = T_kh;
1056 arrow[A_K_HO].fun.cmd = ED_MOVE_TO_BEG;
1057 arrow[A_K_HO].type = XK_CMD;
1059 arrow[A_K_EN].name = "end";
1060 arrow[A_K_EN].key = T_at7;
1061 arrow[A_K_EN].fun.cmd = ED_MOVE_TO_END;
1062 arrow[A_K_EN].type = XK_CMD;
1066 /* term_reset_arrow():
1067 * Reset arrow key bindings
1070 term_reset_arrow(EditLine *el)
1072 fkey_t *arrow = el->el_term.t_fkey;
1073 static const char strA[] = {033, '[', 'A', '\0'};
1074 static const char strB[] = {033, '[', 'B', '\0'};
1075 static const char strC[] = {033, '[', 'C', '\0'};
1076 static const char strD[] = {033, '[', 'D', '\0'};
1077 static const char strH[] = {033, '[', 'H', '\0'};
1078 static const char strF[] = {033, '[', 'F', '\0'};
1079 static const char stOA[] = {033, 'O', 'A', '\0'};
1080 static const char stOB[] = {033, 'O', 'B', '\0'};
1081 static const char stOC[] = {033, 'O', 'C', '\0'};
1082 static const char stOD[] = {033, 'O', 'D', '\0'};
1083 static const char stOH[] = {033, 'O', 'H', '\0'};
1084 static const char stOF[] = {033, 'O', 'F', '\0'};
1086 key_add(el, strA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1087 key_add(el, strB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1088 key_add(el, strC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1089 key_add(el, strD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1090 key_add(el, strH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1091 key_add(el, strF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1092 key_add(el, stOA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1093 key_add(el, stOB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1094 key_add(el, stOC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1095 key_add(el, stOD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1096 key_add(el, stOH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1097 key_add(el, stOF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1099 if (el->el_map.type == MAP_VI) {
1100 key_add(el, &strA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1101 key_add(el, &strB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1102 key_add(el, &strC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1103 key_add(el, &strD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1104 key_add(el, &strH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1105 key_add(el, &strF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1106 key_add(el, &stOA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1107 key_add(el, &stOB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1108 key_add(el, &stOC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1109 key_add(el, &stOD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1110 key_add(el, &stOH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1111 key_add(el, &stOF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1116 /* term_set_arrow():
1117 * Set an arrow key binding
1120 term_set_arrow(EditLine *el, const char *name, key_value_t *fun, int type)
1122 fkey_t *arrow = el->el_term.t_fkey;
1125 for (i = 0; i < A_K_NKEYS; i++)
1126 if (strcmp(name, arrow[i].name) == 0) {
1127 arrow[i].fun = *fun;
1128 arrow[i].type = type;
1135 /* term_clear_arrow():
1136 * Clear an arrow key binding
1139 term_clear_arrow(EditLine *el, const char *name)
1141 fkey_t *arrow = el->el_term.t_fkey;
1144 for (i = 0; i < A_K_NKEYS; i++)
1145 if (strcmp(name, arrow[i].name) == 0) {
1146 arrow[i].type = XK_NOD;
1153 /* term_print_arrow():
1154 * Print the arrow key bindings
1157 term_print_arrow(EditLine *el, const char *name)
1160 fkey_t *arrow = el->el_term.t_fkey;
1162 for (i = 0; i < A_K_NKEYS; i++)
1163 if (*name == '\0' || strcmp(name, arrow[i].name) == 0)
1164 if (arrow[i].type != XK_NOD)
1165 key_kprint(el, arrow[i].name, &arrow[i].fun,
1170 /* term_bind_arrow():
1171 * Bind the arrow keys
1174 term_bind_arrow(EditLine *el)
1177 const el_action_t *dmap;
1180 fkey_t *arrow = el->el_term.t_fkey;
1182 /* Check if the components needed are initialized */
1183 if (el->el_term.t_buf == NULL || el->el_map.key == NULL)
1186 map = el->el_map.type == MAP_VI ? el->el_map.alt : el->el_map.key;
1187 dmap = el->el_map.type == MAP_VI ? el->el_map.vic : el->el_map.emacs;
1189 term_reset_arrow(el);
1191 for (i = 0; i < A_K_NKEYS; i++) {
1192 p = el->el_term.t_str[arrow[i].key];
1194 j = (unsigned char) *p;
1196 * Assign the arrow keys only if:
1198 * 1. They are multi-character arrow keys and the user
1199 * has not re-assigned the leading character, or
1200 * has re-assigned the leading character to be
1201 * ED_SEQUENCE_LEAD_IN
1202 * 2. They are single arrow keys pointing to an
1205 if (arrow[i].type == XK_NOD)
1206 key_clear(el, map, p);
1208 if (p[1] && (dmap[j] == map[j] ||
1209 map[j] == ED_SEQUENCE_LEAD_IN)) {
1210 key_add(el, p, &arrow[i].fun,
1212 map[j] = ED_SEQUENCE_LEAD_IN;
1213 } else if (map[j] == ED_UNASSIGNED) {
1214 key_clear(el, map, p);
1215 if (arrow[i].type == XK_CMD)
1216 map[j] = arrow[i].fun.cmd;
1218 key_add(el, p, &arrow[i].fun,
1234 return (fputc(c, term_outfile));
1245 (void) fflush(term_outfile);
1250 * Print the current termcap characteristics
1254 term_telltc(EditLine *el, int argc __attribute__((__unused__)),
1255 const char **argv __attribute__((__unused__)))
1257 const struct termcapstr *t;
1259 char upbuf[EL_BUFSIZ];
1261 (void) fprintf(el->el_outfile, "\n\tYour terminal has the\n");
1262 (void) fprintf(el->el_outfile, "\tfollowing characteristics:\n\n");
1263 (void) fprintf(el->el_outfile, "\tIt has %d columns and %d lines\n",
1264 Val(T_co), Val(T_li));
1265 (void) fprintf(el->el_outfile,
1266 "\tIt has %s meta key\n", EL_HAS_META ? "a" : "no");
1267 (void) fprintf(el->el_outfile,
1268 "\tIt can%suse tabs\n", EL_CAN_TAB ? " " : "not ");
1269 (void) fprintf(el->el_outfile, "\tIt %s automatic margins\n",
1270 EL_HAS_AUTO_MARGINS ? "has" : "does not have");
1271 if (EL_HAS_AUTO_MARGINS)
1272 (void) fprintf(el->el_outfile, "\tIt %s magic margins\n",
1273 EL_HAS_MAGIC_MARGINS ? "has" : "does not have");
1275 for (t = tstr, ts = el->el_term.t_str; t->name != NULL; t++, ts++)
1276 (void) fprintf(el->el_outfile, "\t%25s (%s) == %s\n",
1278 t->name, *ts && **ts ?
1279 key__decode_str(*ts, upbuf, "") : "(empty)");
1280 (void) fputc('\n', el->el_outfile);
1286 * Change the current terminal characteristics
1290 term_settc(EditLine *el, int argc __attribute__((__unused__)),
1293 const struct termcapstr *ts;
1294 const struct termcapval *tv;
1295 const char *what, *how;
1297 if (argv == NULL || argv[1] == NULL || argv[2] == NULL)
1304 * Do the strings first
1306 for (ts = tstr; ts->name != NULL; ts++)
1307 if (strcmp(ts->name, what) == 0)
1310 if (ts->name != NULL) {
1311 term_alloc(el, ts, how);
1316 * Do the numeric ones second
1318 for (tv = tval; tv->name != NULL; tv++)
1319 if (strcmp(tv->name, what) == 0)
1322 if (tv->name != NULL) {
1323 if (tv == &tval[T_pt] || tv == &tval[T_km] ||
1324 tv == &tval[T_am] || tv == &tval[T_xn]) {
1325 if (strcmp(how, "yes") == 0)
1326 el->el_term.t_val[tv - tval] = 1;
1327 else if (strcmp(how, "no") == 0)
1328 el->el_term.t_val[tv - tval] = 0;
1330 (void) fprintf(el->el_errfile,
1331 "settc: Bad value `%s'.\n", how);
1335 if (term_change_size(el, Val(T_li), Val(T_co)) == -1)
1342 i = strtol(how, &ep, 10);
1344 (void) fprintf(el->el_errfile,
1345 "settc: Bad value `%s'.\n", how);
1348 el->el_term.t_val[tv - tval] = (int) i;
1349 el->el_term.t_size.v = Val(T_co);
1350 el->el_term.t_size.h = Val(T_li);
1351 if (tv == &tval[T_co] || tv == &tval[T_li])
1352 if (term_change_size(el, Val(T_li), Val(T_co))
1363 * Print the termcap string out with variable substitution
1367 term_echotc(EditLine *el, int argc __attribute__((__unused__)),
1370 char *cap, *scap, *ep;
1371 int arg_need, arg_cols, arg_rows;
1372 int verbose = 0, silent = 0;
1374 static const char fmts[] = "%s\n", fmtd[] = "%d\n";
1375 const struct termcapstr *t;
1376 char buf[TC_BUFSIZE];
1381 if (argv == NULL || argv[1] == NULL)
1385 if (argv[0][0] == '-') {
1386 switch (argv[0][1]) {
1394 /* stderror(ERR_NAME | ERR_TCUSAGE); */
1399 if (!*argv || *argv[0] == '\0')
1401 if (strcmp(*argv, "tabs") == 0) {
1402 (void) fprintf(el->el_outfile, fmts, EL_CAN_TAB ? "yes" : "no");
1404 } else if (strcmp(*argv, "meta") == 0) {
1405 (void) fprintf(el->el_outfile, fmts, Val(T_km) ? "yes" : "no");
1407 } else if (strcmp(*argv, "xn") == 0) {
1408 (void) fprintf(el->el_outfile, fmts, EL_HAS_MAGIC_MARGINS ?
1411 } else if (strcmp(*argv, "am") == 0) {
1412 (void) fprintf(el->el_outfile, fmts, EL_HAS_AUTO_MARGINS ?
1415 } else if (strcmp(*argv, "baud") == 0) {
1419 for (i = 0; baud_rate[i].b_name != NULL; i++)
1420 if (el->el_tty.t_speed == baud_rate[i].b_rate) {
1421 (void) fprintf(el->el_outfile, fmts,
1422 baud_rate[i].b_name);
1425 (void) fprintf(el->el_outfile, fmtd, 0);
1427 (void) fprintf(el->el_outfile, fmtd, (int)el->el_tty.t_speed);
1430 } else if (strcmp(*argv, "rows") == 0 || strcmp(*argv, "lines") == 0) {
1431 (void) fprintf(el->el_outfile, fmtd, Val(T_li));
1433 } else if (strcmp(*argv, "cols") == 0) {
1434 (void) fprintf(el->el_outfile, fmtd, Val(T_co));
1438 * Try to use our local definition first
1441 for (t = tstr; t->name != NULL; t++)
1442 if (strcmp(t->name, *argv) == 0) {
1443 scap = el->el_term.t_str[t - tstr];
1446 if (t->name == NULL) {
1447 /* XXX: some systems tgetstr needs non const */
1448 scap = tgetstr(strchr(*argv, **argv), &area);
1450 if (!scap || scap[0] == '\0') {
1452 (void) fprintf(el->el_errfile,
1453 "echotc: Termcap parameter `%s' not found.\n",
1458 * Count home many values we need for this capability.
1460 for (cap = scap, arg_need = 0; *cap; cap++)
1480 * hpux has lot's of them...
1483 (void) fprintf(el->el_errfile,
1484 "echotc: Warning: unknown termcap %% `%c'.\n",
1486 /* This is bad, but I won't complain */
1493 if (*argv && *argv[0]) {
1495 (void) fprintf(el->el_errfile,
1496 "echotc: Warning: Extra argument `%s'.\n",
1500 (void) tputs(scap, 1, term__putc);
1504 if (!*argv || *argv[0] == '\0') {
1506 (void) fprintf(el->el_errfile,
1507 "echotc: Warning: Missing argument.\n");
1511 i = strtol(*argv, &ep, 10);
1512 if (*ep != '\0' || i < 0) {
1514 (void) fprintf(el->el_errfile,
1515 "echotc: Bad value `%s' for rows.\n",
1521 if (*argv && *argv[0]) {
1523 (void) fprintf(el->el_errfile,
1524 "echotc: Warning: Extra argument `%s'.\n",
1528 (void) tputs(tgoto(scap, arg_cols, arg_rows), 1, term__putc);
1531 /* This is wrong, but I will ignore it... */
1533 (void) fprintf(el->el_errfile,
1534 "echotc: Warning: Too many required arguments (%d).\n",
1539 if (!*argv || *argv[0] == '\0') {
1541 (void) fprintf(el->el_errfile,
1542 "echotc: Warning: Missing argument.\n");
1545 i = strtol(*argv, &ep, 10);
1546 if (*ep != '\0' || i < 0) {
1548 (void) fprintf(el->el_errfile,
1549 "echotc: Bad value `%s' for cols.\n",
1555 if (!*argv || *argv[0] == '\0') {
1557 (void) fprintf(el->el_errfile,
1558 "echotc: Warning: Missing argument.\n");
1561 i = strtol(*argv, &ep, 10);
1562 if (*ep != '\0' || i < 0) {
1564 (void) fprintf(el->el_errfile,
1565 "echotc: Bad value `%s' for rows.\n",
1572 (void) fprintf(el->el_errfile,
1573 "echotc: Bad value `%s'.\n", *argv);
1577 if (*argv && *argv[0]) {
1579 (void) fprintf(el->el_errfile,
1580 "echotc: Warning: Extra argument `%s'.\n",
1584 (void) tputs(tgoto(scap, arg_cols, arg_rows), arg_rows,