1 /* $NetBSD: term.c,v 1.35 2002/03/18 16:00:59 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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #if !defined(lint) && !defined(SCCSID)
42 static char sccsid[] = "@(#)term.c 8.2 (Berkeley) 4/30/95";
44 __RCSID("$NetBSD: term.c,v 1.35 2002/03/18 16:00:59 christos Exp $");
46 #endif /* not lint && not SCCSID */
49 * term.c: Editor/termcap-curses interface
50 * We have to declare a static variable here, since the
51 * termcap putchar routine does not take an argument!
67 /* Solaris's term.h does horrid things. */
68 #if (defined(HAVE_TERM_H) && !defined(SUNOS))
71 #include <sys/types.h>
72 #include <sys/ioctl.h>
77 * IMPORTANT NOTE: these routines are allowed to look at the current screen
78 * and the current possition assuming that it is correct. If this is not
79 * true, then the update will be WRONG! This is (should be) a valid
83 #define TC_BUFSIZE 2048
85 #define GoodStr(a) (el->el_term.t_str[a] != NULL && \
86 el->el_term.t_str[a][0] != '\0')
87 #define Str(a) el->el_term.t_str[a]
88 #define Val(a) el->el_term.t_val[a]
91 private const struct {
162 private const struct termcapstr {
164 const char *long_name;
167 { "al", "add new blank line" },
169 { "bl", "audible bell" },
171 { "cd", "clear to bottom" },
173 { "ce", "clear to end of line" },
175 { "ch", "cursor to horiz pos" },
177 { "cl", "clear screen" },
179 { "dc", "delete a character" },
181 { "dl", "delete a line" },
183 { "dm", "start delete mode" },
185 { "ed", "end delete mode" },
187 { "ei", "end insert mode" },
189 { "fs", "cursor from status line" },
191 { "ho", "home cursor" },
193 { "ic", "insert character" },
195 { "im", "start insert mode" },
197 { "ip", "insert padding" },
199 { "kd", "sends cursor down" },
201 { "kl", "sends cursor left" },
203 { "kr", "sends cursor right" },
205 { "ku", "sends cursor up" },
207 { "md", "begin bold" },
209 { "me", "end attributes" },
211 { "nd", "non destructive space" },
213 { "se", "end standout" },
215 { "so", "begin standout" },
217 { "ts", "cursor to status line" },
219 { "up", "cursor up one" },
221 { "us", "begin underline" },
223 { "ue", "end underline" },
225 { "vb", "visible bell" },
227 { "DC", "delete multiple chars" },
229 { "DO", "cursor down multiple" },
231 { "IC", "insert multiple chars" },
233 { "LE", "cursor left multiple" },
235 { "RI", "cursor right multiple" },
237 { "UP", "cursor up multiple" },
239 { "kh", "send cursor home" },
241 { "@7", "send cursor end" },
246 private const struct termcapval {
248 const char *long_name;
251 { "am", "has automatic margins" },
253 { "pt", "has physical tabs" },
255 { "li", "Number of lines" },
257 { "co", "Number of columns" },
259 { "km", "Has meta key" },
261 { "xt", "Tab chars destructive" },
263 { "xn", "newline ignored at right margin" },
265 { "MT", "Has meta key" }, /* XXX? */
269 /* do two or more of the attributes use me */
271 private void term_setflags(EditLine *);
272 private int term_rebuffer_display(EditLine *);
273 private void term_free_display(EditLine *);
274 private int term_alloc_display(EditLine *);
275 private void term_alloc(EditLine *, const struct termcapstr *, const char *);
276 private void term_init_arrow(EditLine *);
277 private void term_reset_arrow(EditLine *);
280 private FILE *term_outfile = NULL; /* XXX: How do we fix that? */
284 * Set the terminal capability flags
287 term_setflags(EditLine *el)
290 if (el->el_tty.t_tabs)
291 EL_FLAGS |= (Val(T_pt) && !Val(T_xt)) ? TERM_CAN_TAB : 0;
293 EL_FLAGS |= (Val(T_km) || Val(T_MT)) ? TERM_HAS_META : 0;
294 EL_FLAGS |= GoodStr(T_ce) ? TERM_CAN_CEOL : 0;
295 EL_FLAGS |= (GoodStr(T_dc) || GoodStr(T_DC)) ? TERM_CAN_DELETE : 0;
296 EL_FLAGS |= (GoodStr(T_im) || GoodStr(T_ic) || GoodStr(T_IC)) ?
298 EL_FLAGS |= (GoodStr(T_up) || GoodStr(T_UP)) ? TERM_CAN_UP : 0;
299 EL_FLAGS |= Val(T_am) ? TERM_HAS_AUTO_MARGINS : 0;
300 EL_FLAGS |= Val(T_xn) ? TERM_HAS_MAGIC_MARGINS : 0;
302 if (GoodStr(T_me) && GoodStr(T_ue))
303 EL_FLAGS |= (strcmp(Str(T_me), Str(T_ue)) == 0) ?
306 EL_FLAGS &= ~TERM_CAN_ME;
307 if (GoodStr(T_me) && GoodStr(T_se))
308 EL_FLAGS |= (strcmp(Str(T_me), Str(T_se)) == 0) ?
314 (void) fprintf(el->el_errfile,
315 "WARNING: Your terminal cannot move up.\n");
316 (void) fprintf(el->el_errfile,
317 "Editing may be odd for long lines.\n");
320 (void) fprintf(el->el_errfile, "no clear EOL capability.\n");
322 (void) fprintf(el->el_errfile, "no delete char capability.\n");
324 (void) fprintf(el->el_errfile, "no insert char capability.\n");
325 #endif /* DEBUG_SCREEN */
330 * Initialize the terminal stuff
333 term_init(EditLine *el)
336 el->el_term.t_buf = (char *) el_malloc(TC_BUFSIZE);
337 if (el->el_term.t_buf == NULL)
339 el->el_term.t_cap = (char *) el_malloc(TC_BUFSIZE);
340 if (el->el_term.t_cap == NULL)
342 el->el_term.t_fkey = (fkey_t *) el_malloc(A_K_NKEYS * sizeof(fkey_t));
343 if (el->el_term.t_fkey == NULL)
345 el->el_term.t_loc = 0;
346 el->el_term.t_str = (char **) el_malloc(T_str * sizeof(char *));
347 if (el->el_term.t_str == NULL)
349 (void) memset(el->el_term.t_str, 0, T_str * sizeof(char *));
350 el->el_term.t_val = (int *) el_malloc(T_val * sizeof(int));
351 if (el->el_term.t_val == NULL)
353 (void) memset(el->el_term.t_val, 0, T_val * sizeof(int));
354 term_outfile = el->el_outfile;
355 (void) term_set(el, NULL);
360 * Clean up the terminal stuff
363 term_end(EditLine *el)
366 el_free((ptr_t) el->el_term.t_buf);
367 el->el_term.t_buf = NULL;
368 el_free((ptr_t) el->el_term.t_cap);
369 el->el_term.t_cap = NULL;
370 el_free((ptr_t) el->el_term.t_fkey);
371 el->el_term.t_fkey = NULL;
372 el->el_term.t_loc = 0;
373 el_free((ptr_t) el->el_term.t_str);
374 el->el_term.t_str = NULL;
375 el_free((ptr_t) el->el_term.t_val);
376 el->el_term.t_val = NULL;
377 term_free_display(el);
382 * Maintain a string pool for termcap strings
385 term_alloc(EditLine *el, const struct termcapstr *t, const char *cap)
387 char termbuf[TC_BUFSIZE];
389 char **tlist = el->el_term.t_str;
390 char **tmp, **str = &tlist[t - tstr];
392 if (cap == NULL || *cap == '\0') {
398 tlen = *str == NULL ? 0 : strlen(*str);
401 * New string is shorter; no need to allocate space
404 (void) strcpy(*str, cap); /* XXX strcpy is safe */
408 * New string is longer; see if we have enough space to append
410 if (el->el_term.t_loc + 3 < TC_BUFSIZE) {
411 /* XXX strcpy is safe */
412 (void) strcpy(*str = &el->el_term.t_buf[el->el_term.t_loc],
414 el->el_term.t_loc += clen + 1; /* one for \0 */
418 * Compact our buffer; no need to check compaction, cause we know it
422 for (tmp = tlist; tmp < &tlist[T_str]; tmp++)
423 if (*tmp != NULL && *tmp != '\0' && *tmp != *str) {
426 for (ptr = *tmp; *ptr != '\0'; termbuf[tlen++] = *ptr++)
428 termbuf[tlen++] = '\0';
430 memcpy(el->el_term.t_buf, termbuf, TC_BUFSIZE);
431 el->el_term.t_loc = tlen;
432 if (el->el_term.t_loc + 3 >= TC_BUFSIZE) {
433 (void) fprintf(el->el_errfile,
434 "Out of termcap string space.\n");
437 /* XXX strcpy is safe */
438 (void) strcpy(*str = &el->el_term.t_buf[el->el_term.t_loc], cap);
439 el->el_term.t_loc += clen + 1; /* one for \0 */
444 /* term_rebuffer_display():
445 * Rebuffer the display after the screen changed size
448 term_rebuffer_display(EditLine *el)
450 coord_t *c = &el->el_term.t_size;
452 term_free_display(el);
457 if (term_alloc_display(el) == -1)
463 /* term_alloc_display():
464 * Allocate a new display.
467 term_alloc_display(EditLine *el)
471 coord_t *c = &el->el_term.t_size;
473 b = (char **) el_malloc((size_t) (sizeof(char *) * (c->v + 1)));
476 for (i = 0; i < c->v; i++) {
477 b[i] = (char *) el_malloc((size_t) (sizeof(char) * (c->h + 1)));
484 b = (char **) el_malloc((size_t) (sizeof(char *) * (c->v + 1)));
487 for (i = 0; i < c->v; i++) {
488 b[i] = (char *) el_malloc((size_t) (sizeof(char) * (c->h + 1)));
498 /* term_free_display():
499 * Free the display buffers
502 term_free_display(EditLine *el)
508 el->el_display = NULL;
510 for (bufp = b; *bufp != NULL; bufp++)
511 el_free((ptr_t) * bufp);
515 el->el_vdisplay = NULL;
517 for (bufp = b; *bufp != NULL; bufp++)
518 el_free((ptr_t) * bufp);
524 /* term_move_to_line():
525 * move to line <where> (first line == 0)
526 * as efficiently as possible
529 term_move_to_line(EditLine *el, int where)
533 if (where == el->el_cursor.v)
536 if (where > el->el_term.t_size.v) {
538 (void) fprintf(el->el_errfile,
539 "term_move_to_line: where is ridiculous: %d\r\n", where);
540 #endif /* DEBUG_SCREEN */
543 if ((del = where - el->el_cursor.v) > 0) {
545 if (EL_HAS_AUTO_MARGINS &&
546 el->el_display[el->el_cursor.v][0] != '\0') {
547 /* move without newline */
548 term_move_to_char(el, el->el_term.t_size.h - 1);
550 &el->el_display[el->el_cursor.v][el->el_cursor.h],
555 if ((del > 1) && GoodStr(T_DO)) {
556 (void) tputs(tgoto(Str(T_DO), del, del),
560 for (; del > 0; del--)
562 /* because the \n will become \r\n */
567 } else { /* del < 0 */
568 if (GoodStr(T_UP) && (-del > 1 || !GoodStr(T_up)))
569 (void) tputs(tgoto(Str(T_UP), -del, -del), -del,
573 for (; del < 0; del++)
574 (void) tputs(Str(T_up), 1, term__putc);
577 el->el_cursor.v = where;/* now where is here */
581 /* term_move_to_char():
582 * Move to the character position specified
585 term_move_to_char(EditLine *el, int where)
590 if (where == el->el_cursor.h)
593 if (where > el->el_term.t_size.h) {
595 (void) fprintf(el->el_errfile,
596 "term_move_to_char: where is riduculous: %d\r\n", where);
597 #endif /* DEBUG_SCREEN */
600 if (!where) { /* if where is first column */
601 term__putc('\r'); /* do a CR */
605 del = where - el->el_cursor.h;
607 if ((del < -4 || del > 4) && GoodStr(T_ch))
608 /* go there directly */
609 (void) tputs(tgoto(Str(T_ch), where, where), where, term__putc);
611 if (del > 0) { /* moving forward */
612 if ((del > 4) && GoodStr(T_RI))
613 (void) tputs(tgoto(Str(T_RI), del, del),
616 /* if I can do tabs, use them */
618 if ((el->el_cursor.h & 0370) !=
620 /* if not within tab stop */
622 (el->el_cursor.h & 0370);
627 el->el_cursor.h = where & 0370;
631 * it's usually cheaper to just write the
635 * NOTE THAT term_overwrite() WILL CHANGE
639 &el->el_display[el->el_cursor.v][el->el_cursor.h],
640 where - el->el_cursor.h);
643 } else { /* del < 0 := moving backward */
644 if ((-del > 4) && GoodStr(T_LE))
645 (void) tputs(tgoto(Str(T_LE), -del, -del),
647 else { /* can't go directly there */
649 * if the "cost" is greater than the "cost"
653 (-del > (((unsigned int) where >> 3) +
656 term__putc('\r'); /* do a CR */
658 goto mc_again; /* and try again */
660 for (i = 0; i < -del; i++)
665 el->el_cursor.h = where; /* now where is here */
670 * Overstrike num characters
673 term_overwrite(EditLine *el, const char *cp, int n)
676 return; /* catch bugs */
678 if (n > el->el_term.t_size.h) {
680 (void) fprintf(el->el_errfile,
681 "term_overwrite: n is riduculous: %d\r\n", n);
682 #endif /* DEBUG_SCREEN */
690 if (el->el_cursor.h >= el->el_term.t_size.h) { /* wrap? */
691 if (EL_HAS_AUTO_MARGINS) { /* yes */
694 if (EL_HAS_MAGIC_MARGINS) {
695 /* force the wrap to avoid the "magic"
698 if ((c = el->el_display[el->el_cursor.v][el->el_cursor.h])
700 term_overwrite(el, &c, 1);
705 } else /* no wrap, but cursor stays on screen */
706 el->el_cursor.h = el->el_term.t_size.h;
711 /* term_deletechars():
712 * Delete num characters
715 term_deletechars(EditLine *el, int num)
720 if (!EL_CAN_DELETE) {
722 (void) fprintf(el->el_errfile, " ERROR: cannot delete \n");
723 #endif /* DEBUG_EDIT */
726 if (num > el->el_term.t_size.h) {
728 (void) fprintf(el->el_errfile,
729 "term_deletechars: num is riduculous: %d\r\n", num);
730 #endif /* DEBUG_SCREEN */
733 if (GoodStr(T_DC)) /* if I have multiple delete */
734 if ((num > 1) || !GoodStr(T_dc)) { /* if dc would be more
736 (void) tputs(tgoto(Str(T_DC), num, num),
740 if (GoodStr(T_dm)) /* if I have delete mode */
741 (void) tputs(Str(T_dm), 1, term__putc);
743 if (GoodStr(T_dc)) /* else do one at a time */
745 (void) tputs(Str(T_dc), 1, term__putc);
747 if (GoodStr(T_ed)) /* if I have delete mode */
748 (void) tputs(Str(T_ed), 1, term__putc);
752 /* term_insertwrite():
753 * Puts terminal in insert character mode or inserts num
754 * characters in the line
757 term_insertwrite(EditLine *el, char *cp, int num)
761 if (!EL_CAN_INSERT) {
763 (void) fprintf(el->el_errfile, " ERROR: cannot insert \n");
764 #endif /* DEBUG_EDIT */
767 if (num > el->el_term.t_size.h) {
769 (void) fprintf(el->el_errfile,
770 "StartInsert: num is riduculous: %d\r\n", num);
771 #endif /* DEBUG_SCREEN */
774 if (GoodStr(T_IC)) /* if I have multiple insert */
775 if ((num > 1) || !GoodStr(T_ic)) {
776 /* if ic would be more expensive */
777 (void) tputs(tgoto(Str(T_IC), num, num),
779 term_overwrite(el, cp, num);
780 /* this updates el_cursor.h */
783 if (GoodStr(T_im) && GoodStr(T_ei)) { /* if I have insert mode */
784 (void) tputs(Str(T_im), 1, term__putc);
786 el->el_cursor.h += num;
791 if (GoodStr(T_ip)) /* have to make num chars insert */
792 (void) tputs(Str(T_ip), 1, term__putc);
794 (void) tputs(Str(T_ei), 1, term__putc);
798 if (GoodStr(T_ic)) /* have to make num chars insert */
799 (void) tputs(Str(T_ic), 1, term__putc);
806 if (GoodStr(T_ip)) /* have to make num chars insert */
807 (void) tputs(Str(T_ip), 1, term__putc);
808 /* pad the inserted char */
815 * clear to end of line. There are num characters to clear
818 term_clear_EOL(EditLine *el, int num)
822 if (EL_CAN_CEOL && GoodStr(T_ce))
823 (void) tputs(Str(T_ce), 1, term__putc);
825 for (i = 0; i < num; i++)
827 el->el_cursor.h += num; /* have written num spaces */
832 /* term_clear_screen():
836 term_clear_screen(EditLine *el)
837 { /* clear the whole screen and home */
840 /* send the clear screen code */
841 (void) tputs(Str(T_cl), Val(T_li), term__putc);
842 else if (GoodStr(T_ho) && GoodStr(T_cd)) {
843 (void) tputs(Str(T_ho), Val(T_li), term__putc); /* home */
844 /* clear to bottom of screen */
845 (void) tputs(Str(T_cd), Val(T_li), term__putc);
854 * Beep the way the terminal wants us
857 term_beep(EditLine *el)
860 /* what termcap says we should use */
861 (void) tputs(Str(T_bl), 1, term__putc);
863 term__putc('\007'); /* an ASCII bell; ^G */
868 /* term_clear_to_bottom():
869 * Clear to the bottom of the screen
872 term_clear_to_bottom(EditLine *el)
875 (void) tputs(Str(T_cd), Val(T_li), term__putc);
876 else if (GoodStr(T_ce))
877 (void) tputs(Str(T_ce), Val(T_li), term__putc);
883 * Read in the terminal capabilities from the requested terminal
886 term_set(EditLine *el, const char *term)
889 char buf[TC_BUFSIZE];
891 const struct termcapstr *t;
895 (void) sigemptyset(&nset);
896 (void) sigaddset(&nset, SIGWINCH);
897 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
903 term = getenv("TERM");
905 if (!term || !term[0])
908 if (strcmp(term, "emacs") == 0)
909 el->el_flags |= EDIT_DISABLED;
911 memset(el->el_term.t_cap, 0, TC_BUFSIZE);
913 i = tgetent(el->el_term.t_cap, term);
917 (void) fprintf(el->el_errfile,
918 "Cannot read termcap database;\n");
920 (void) fprintf(el->el_errfile,
921 "No entry for terminal type \"%s\";\n", term);
922 (void) fprintf(el->el_errfile,
923 "using dumb terminal settings.\n");
924 Val(T_co) = 80; /* do a dumb terminal */
925 Val(T_pt) = Val(T_km) = Val(T_li) = 0;
926 Val(T_xt) = Val(T_MT);
927 for (t = tstr; t->name != NULL; t++)
928 term_alloc(el, t, NULL);
930 /* auto/magic margins */
931 Val(T_am) = tgetflag("am");
932 Val(T_xn) = tgetflag("xn");
934 Val(T_pt) = tgetflag("pt");
935 Val(T_xt) = tgetflag("xt");
936 /* do we have a meta? */
937 Val(T_km) = tgetflag("km");
938 Val(T_MT) = tgetflag("MT");
940 Val(T_co) = tgetnum("co");
941 Val(T_li) = tgetnum("li");
942 for (t = tstr; t->name != NULL; t++)
943 term_alloc(el, t, tgetstr((char *)t->name, &area));
947 Val(T_co) = 80; /* just in case */
951 el->el_term.t_size.v = Val(T_co);
952 el->el_term.t_size.h = Val(T_li);
956 /* get the correct window size */
957 (void) term_get_size(el, &lins, &cols);
958 if (term_change_size(el, lins, cols) == -1)
960 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
962 return (i <= 0 ? -1 : 0);
967 * Return the new window size in lines and cols, and
968 * true if the size was changed.
971 term_get_size(EditLine *el, int *lins, int *cols)
980 if (ioctl(el->el_infd, TIOCGWINSZ, (ioctl_t) & ws) != -1) {
991 if (ioctl(el->el_infd, TIOCGSIZE, (ioctl_t) & ts) != -1) {
999 return (Val(T_co) != *cols || Val(T_li) != *lins);
1003 /* term_change_size():
1004 * Change the size of the terminal
1007 term_change_size(EditLine *el, int lins, int cols)
1012 Val(T_co) = (cols < 2) ? 80 : cols;
1013 Val(T_li) = (lins < 1) ? 24 : lins;
1015 /* re-make display buffers */
1016 if (term_rebuffer_display(el) == -1)
1018 re_clear_display(el);
1023 /* term_init_arrow():
1024 * Initialize the arrow key bindings from termcap
1027 term_init_arrow(EditLine *el)
1029 fkey_t *arrow = el->el_term.t_fkey;
1031 arrow[A_K_DN].name = "down";
1032 arrow[A_K_DN].key = T_kd;
1033 arrow[A_K_DN].fun.cmd = ED_NEXT_HISTORY;
1034 arrow[A_K_DN].type = XK_CMD;
1036 arrow[A_K_UP].name = "up";
1037 arrow[A_K_UP].key = T_ku;
1038 arrow[A_K_UP].fun.cmd = ED_PREV_HISTORY;
1039 arrow[A_K_UP].type = XK_CMD;
1041 arrow[A_K_LT].name = "left";
1042 arrow[A_K_LT].key = T_kl;
1043 arrow[A_K_LT].fun.cmd = ED_PREV_CHAR;
1044 arrow[A_K_LT].type = XK_CMD;
1046 arrow[A_K_RT].name = "right";
1047 arrow[A_K_RT].key = T_kr;
1048 arrow[A_K_RT].fun.cmd = ED_NEXT_CHAR;
1049 arrow[A_K_RT].type = XK_CMD;
1051 arrow[A_K_HO].name = "home";
1052 arrow[A_K_HO].key = T_kh;
1053 arrow[A_K_HO].fun.cmd = ED_MOVE_TO_BEG;
1054 arrow[A_K_HO].type = XK_CMD;
1056 arrow[A_K_EN].name = "end";
1057 arrow[A_K_EN].key = T_at7;
1058 arrow[A_K_EN].fun.cmd = ED_MOVE_TO_END;
1059 arrow[A_K_EN].type = XK_CMD;
1063 /* term_reset_arrow():
1064 * Reset arrow key bindings
1067 term_reset_arrow(EditLine *el)
1069 fkey_t *arrow = el->el_term.t_fkey;
1070 static const char strA[] = {033, '[', 'A', '\0'};
1071 static const char strB[] = {033, '[', 'B', '\0'};
1072 static const char strC[] = {033, '[', 'C', '\0'};
1073 static const char strD[] = {033, '[', 'D', '\0'};
1074 static const char strH[] = {033, '[', 'H', '\0'};
1075 static const char strF[] = {033, '[', 'F', '\0'};
1076 static const char stOA[] = {033, 'O', 'A', '\0'};
1077 static const char stOB[] = {033, 'O', 'B', '\0'};
1078 static const char stOC[] = {033, 'O', 'C', '\0'};
1079 static const char stOD[] = {033, 'O', 'D', '\0'};
1080 static const char stOH[] = {033, 'O', 'H', '\0'};
1081 static const char stOF[] = {033, 'O', 'F', '\0'};
1083 key_add(el, strA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1084 key_add(el, strB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1085 key_add(el, strC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1086 key_add(el, strD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1087 key_add(el, strH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1088 key_add(el, strF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1089 key_add(el, stOA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1090 key_add(el, stOB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1091 key_add(el, stOC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1092 key_add(el, stOD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1093 key_add(el, stOH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1094 key_add(el, stOF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1096 if (el->el_map.type == MAP_VI) {
1097 key_add(el, &strA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1098 key_add(el, &strB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1099 key_add(el, &strC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1100 key_add(el, &strD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1101 key_add(el, &strH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1102 key_add(el, &strF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1103 key_add(el, &stOA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1104 key_add(el, &stOB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1105 key_add(el, &stOC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1106 key_add(el, &stOD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1107 key_add(el, &stOH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1108 key_add(el, &stOF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1113 /* term_set_arrow():
1114 * Set an arrow key binding
1117 term_set_arrow(EditLine *el, const char *name, key_value_t *fun, int type)
1119 fkey_t *arrow = el->el_term.t_fkey;
1122 for (i = 0; i < A_K_NKEYS; i++)
1123 if (strcmp(name, arrow[i].name) == 0) {
1124 arrow[i].fun = *fun;
1125 arrow[i].type = type;
1132 /* term_clear_arrow():
1133 * Clear an arrow key binding
1136 term_clear_arrow(EditLine *el, const char *name)
1138 fkey_t *arrow = el->el_term.t_fkey;
1141 for (i = 0; i < A_K_NKEYS; i++)
1142 if (strcmp(name, arrow[i].name) == 0) {
1143 arrow[i].type = XK_NOD;
1150 /* term_print_arrow():
1151 * Print the arrow key bindings
1154 term_print_arrow(EditLine *el, const char *name)
1157 fkey_t *arrow = el->el_term.t_fkey;
1159 for (i = 0; i < A_K_NKEYS; i++)
1160 if (*name == '\0' || strcmp(name, arrow[i].name) == 0)
1161 if (arrow[i].type != XK_NOD)
1162 key_kprint(el, arrow[i].name, &arrow[i].fun,
1167 /* term_bind_arrow():
1168 * Bind the arrow keys
1171 term_bind_arrow(EditLine *el)
1174 const el_action_t *dmap;
1177 fkey_t *arrow = el->el_term.t_fkey;
1179 /* Check if the components needed are initialized */
1180 if (el->el_term.t_buf == NULL || el->el_map.key == NULL)
1183 map = el->el_map.type == MAP_VI ? el->el_map.alt : el->el_map.key;
1184 dmap = el->el_map.type == MAP_VI ? el->el_map.vic : el->el_map.emacs;
1186 term_reset_arrow(el);
1188 for (i = 0; i < A_K_NKEYS; i++) {
1189 p = el->el_term.t_str[arrow[i].key];
1191 j = (unsigned char) *p;
1193 * Assign the arrow keys only if:
1195 * 1. They are multi-character arrow keys and the user
1196 * has not re-assigned the leading character, or
1197 * has re-assigned the leading character to be
1198 * ED_SEQUENCE_LEAD_IN
1199 * 2. They are single arrow keys pointing to an
1202 if (arrow[i].type == XK_NOD)
1203 key_clear(el, map, p);
1205 if (p[1] && (dmap[j] == map[j] ||
1206 map[j] == ED_SEQUENCE_LEAD_IN)) {
1207 key_add(el, p, &arrow[i].fun,
1209 map[j] = ED_SEQUENCE_LEAD_IN;
1210 } else if (map[j] == ED_UNASSIGNED) {
1211 key_clear(el, map, p);
1212 if (arrow[i].type == XK_CMD)
1213 map[j] = arrow[i].fun.cmd;
1215 key_add(el, p, &arrow[i].fun,
1231 return (fputc(c, term_outfile));
1242 (void) fflush(term_outfile);
1247 * Print the current termcap characteristics
1251 term_telltc(EditLine *el, int argc, const char **argv)
1253 const struct termcapstr *t;
1255 char upbuf[EL_BUFSIZ];
1257 (void) fprintf(el->el_outfile, "\n\tYour terminal has the\n");
1258 (void) fprintf(el->el_outfile, "\tfollowing characteristics:\n\n");
1259 (void) fprintf(el->el_outfile, "\tIt has %d columns and %d lines\n",
1260 Val(T_co), Val(T_li));
1261 (void) fprintf(el->el_outfile,
1262 "\tIt has %s meta key\n", EL_HAS_META ? "a" : "no");
1263 (void) fprintf(el->el_outfile,
1264 "\tIt can%suse tabs\n", EL_CAN_TAB ? " " : "not ");
1265 (void) fprintf(el->el_outfile, "\tIt %s automatic margins\n",
1266 EL_HAS_AUTO_MARGINS ? "has" : "does not have");
1267 if (EL_HAS_AUTO_MARGINS)
1268 (void) fprintf(el->el_outfile, "\tIt %s magic margins\n",
1269 EL_HAS_MAGIC_MARGINS ? "has" : "does not have");
1271 for (t = tstr, ts = el->el_term.t_str; t->name != NULL; t++, ts++)
1272 (void) fprintf(el->el_outfile, "\t%25s (%s) == %s\n",
1274 t->name, *ts && **ts ?
1275 key__decode_str(*ts, upbuf, "") : "(empty)");
1276 (void) fputc('\n', el->el_outfile);
1282 * Change the current terminal characteristics
1286 term_settc(EditLine *el, int argc, const char **argv)
1288 const struct termcapstr *ts;
1289 const struct termcapval *tv;
1290 const char *what, *how;
1292 if (argv == NULL || argv[1] == NULL || argv[2] == NULL)
1299 * Do the strings first
1301 for (ts = tstr; ts->name != NULL; ts++)
1302 if (strcmp(ts->name, what) == 0)
1305 if (ts->name != NULL) {
1306 term_alloc(el, ts, how);
1311 * Do the numeric ones second
1313 for (tv = tval; tv->name != NULL; tv++)
1314 if (strcmp(tv->name, what) == 0)
1317 if (tv->name != NULL) {
1318 if (tv == &tval[T_pt] || tv == &tval[T_km] ||
1319 tv == &tval[T_am] || tv == &tval[T_xn]) {
1320 if (strcmp(how, "yes") == 0)
1321 el->el_term.t_val[tv - tval] = 1;
1322 else if (strcmp(how, "no") == 0)
1323 el->el_term.t_val[tv - tval] = 0;
1325 (void) fprintf(el->el_errfile,
1326 "settc: Bad value `%s'.\n", how);
1330 if (term_change_size(el, Val(T_li), Val(T_co)) == -1)
1337 i = strtol(how, &ep, 10);
1339 (void) fprintf(el->el_errfile,
1340 "settc: Bad value `%s'.\n", how);
1343 el->el_term.t_val[tv - tval] = (int) i;
1344 el->el_term.t_size.v = Val(T_co);
1345 el->el_term.t_size.h = Val(T_li);
1346 if (tv == &tval[T_co] || tv == &tval[T_li])
1347 if (term_change_size(el, Val(T_li), Val(T_co))
1358 * Print the termcap string out with variable substitution
1362 term_echotc(EditLine *el, int argc, const char **argv)
1364 char *cap, *scap, *ep;
1365 int arg_need, arg_cols, arg_rows;
1366 int verbose = 0, silent = 0;
1368 static const char fmts[] = "%s\n", fmtd[] = "%d\n";
1369 const struct termcapstr *t;
1370 char buf[TC_BUFSIZE];
1375 if (argv == NULL || argv[1] == NULL)
1379 if (argv[0][0] == '-') {
1380 switch (argv[0][1]) {
1388 /* stderror(ERR_NAME | ERR_TCUSAGE); */
1393 if (!*argv || *argv[0] == '\0')
1395 if (strcmp(*argv, "tabs") == 0) {
1396 (void) fprintf(el->el_outfile, fmts, EL_CAN_TAB ? "yes" : "no");
1398 } else if (strcmp(*argv, "meta") == 0) {
1399 (void) fprintf(el->el_outfile, fmts, Val(T_km) ? "yes" : "no");
1401 } else if (strcmp(*argv, "xn") == 0) {
1402 (void) fprintf(el->el_outfile, fmts, EL_HAS_MAGIC_MARGINS ?
1405 } else if (strcmp(*argv, "am") == 0) {
1406 (void) fprintf(el->el_outfile, fmts, EL_HAS_AUTO_MARGINS ?
1409 } else if (strcmp(*argv, "baud") == 0) {
1413 for (i = 0; baud_rate[i].b_name != NULL; i++)
1414 if (el->el_tty.t_speed == baud_rate[i].b_rate) {
1415 (void) fprintf(el->el_outfile, fmts,
1416 baud_rate[i].b_name);
1419 (void) fprintf(el->el_outfile, fmtd, 0);
1421 (void) fprintf(el->el_outfile, fmtd, el->el_tty.t_speed);
1424 } else if (strcmp(*argv, "rows") == 0 || strcmp(*argv, "lines") == 0) {
1425 (void) fprintf(el->el_outfile, fmtd, Val(T_li));
1427 } else if (strcmp(*argv, "cols") == 0) {
1428 (void) fprintf(el->el_outfile, fmtd, Val(T_co));
1432 * Try to use our local definition first
1435 for (t = tstr; t->name != NULL; t++)
1436 if (strcmp(t->name, *argv) == 0) {
1437 scap = el->el_term.t_str[t - tstr];
1440 if (t->name == NULL)
1441 scap = tgetstr((char *)argv, &area);
1442 if (!scap || scap[0] == '\0') {
1444 (void) fprintf(el->el_errfile,
1445 "echotc: Termcap parameter `%s' not found.\n",
1450 * Count home many values we need for this capability.
1452 for (cap = scap, arg_need = 0; *cap; cap++)
1472 * hpux has lot's of them...
1475 (void) fprintf(el->el_errfile,
1476 "echotc: Warning: unknown termcap %% `%c'.\n",
1478 /* This is bad, but I won't complain */
1485 if (*argv && *argv[0]) {
1487 (void) fprintf(el->el_errfile,
1488 "echotc: Warning: Extra argument `%s'.\n",
1492 (void) tputs(scap, 1, term__putc);
1496 if (!*argv || *argv[0] == '\0') {
1498 (void) fprintf(el->el_errfile,
1499 "echotc: Warning: Missing argument.\n");
1503 i = strtol(*argv, &ep, 10);
1504 if (*ep != '\0' || i < 0) {
1506 (void) fprintf(el->el_errfile,
1507 "echotc: Bad value `%s' for rows.\n",
1513 if (*argv && *argv[0]) {
1515 (void) fprintf(el->el_errfile,
1516 "echotc: Warning: Extra argument `%s'.\n",
1520 (void) tputs(tgoto(scap, arg_cols, arg_rows), 1, term__putc);
1523 /* This is wrong, but I will ignore it... */
1525 (void) fprintf(el->el_errfile,
1526 "echotc: Warning: Too many required arguments (%d).\n",
1531 if (!*argv || *argv[0] == '\0') {
1533 (void) fprintf(el->el_errfile,
1534 "echotc: Warning: Missing argument.\n");
1537 i = strtol(*argv, &ep, 10);
1538 if (*ep != '\0' || i < 0) {
1540 (void) fprintf(el->el_errfile,
1541 "echotc: Bad value `%s' for cols.\n",
1547 if (!*argv || *argv[0] == '\0') {
1549 (void) fprintf(el->el_errfile,
1550 "echotc: Warning: Missing argument.\n");
1553 i = strtol(*argv, &ep, 10);
1554 if (*ep != '\0' || i < 0) {
1556 (void) fprintf(el->el_errfile,
1557 "echotc: Bad value `%s' for rows.\n",
1564 (void) fprintf(el->el_errfile,
1565 "echotc: Bad value `%s'.\n", *argv);
1569 if (*argv && *argv[0]) {
1571 (void) fprintf(el->el_errfile,
1572 "echotc: Warning: Extra argument `%s'.\n",
1576 (void) tputs(tgoto(scap, arg_cols, arg_rows), arg_rows,