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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * $FreeBSD: src/lib/libedit/term.c,v 1.11.6.1 2000/08/16 14:43:40 ache Exp $
39 #if !defined(lint) && !defined(SCCSID)
40 static char sccsid[] = "@(#)term.c 8.2 (Berkeley) 4/30/95";
41 #endif /* not lint && not SCCSID */
44 * term.c: Editor/termcap-curses interface
45 * We have to declare a static variable here, since the
46 * 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]
74 private struct termcapstr {
80 { "al", "add new blank line" },
82 { "bl", "audible bell" },
84 { "cd", "clear to bottom" },
86 { "ce", "clear to end of line" },
88 { "ch", "cursor to horiz pos" },
90 { "cl", "clear screen" },
92 { "dc", "delete a character" },
94 { "dl", "delete a line" },
96 { "dm", "start delete mode" },
98 { "ed", "end delete mode" },
100 { "ei", "end insert mode" },
102 { "fs", "cursor from status line" },
104 { "ho", "home cursor" },
106 { "ic", "insert character" },
108 { "im", "start insert mode" },
110 { "ip", "insert padding" },
112 { "kd", "sends cursor down" },
114 { "kl", "sends cursor left" },
116 { "kr", "sends cursor right" },
118 { "ku", "sends cursor up" },
120 { "md", "begin bold" },
122 { "me", "end attributes" },
124 { "nd", "non destructive space" },
126 { "se", "end standout" },
128 { "so", "begin standout" },
130 { "ts", "cursor to status line" },
132 { "up", "cursor up one" },
134 { "us", "begin underline" },
136 { "ue", "end underline" },
138 { "vb", "visible bell" },
140 { "DC", "delete multiple chars" },
142 { "DO", "cursor down multiple" },
144 { "IC", "insert multiple chars" },
146 { "LE", "cursor left multiple" },
148 { "RI", "cursor right multiple" },
150 { "UP", "cursor up multiple" },
152 { "kh", "sends cursor home" },
154 { "@7", "sends cursor end" },
159 private struct termcapval {
164 { "pt", "has physical tabs" },
166 { "li", "Number of lines" },
168 { "co", "Number of columns" },
170 { "km", "Has meta key" },
172 { "xt", "Tab chars destructive" },
174 { "MT", "Has meta key" }, /* XXX? */
179 /* do two or more of the attributes use me */
181 private void term_rebuffer_display __P((EditLine *));
182 private void term_free_display __P((EditLine *));
183 private void term_alloc_display __P((EditLine *));
184 private void term_alloc __P((EditLine *,
185 struct termcapstr *, char *));
186 private void term_init_arrow __P((EditLine *));
187 private void term_reset_arrow __P((EditLine *));
190 private FILE *term_outfile = NULL; /* XXX: How do we fix that? */
194 * Set the terminal capability flags
201 if (el->el_tty.t_tabs)
202 EL_FLAGS |= (Val(T_pt) && !Val(T_xt)) ? TERM_CAN_TAB : 0;
204 EL_FLAGS |= (Val(T_km) || Val(T_MT)) ? TERM_HAS_META : 0;
205 EL_FLAGS |= GoodStr(T_ce) ? TERM_CAN_CEOL : 0;
206 EL_FLAGS |= (GoodStr(T_dc) || GoodStr(T_DC)) ? TERM_CAN_DELETE : 0;
207 EL_FLAGS |= (GoodStr(T_im) || GoodStr(T_ic) || GoodStr(T_IC)) ?
209 EL_FLAGS |= (GoodStr(T_up) || GoodStr(T_UP)) ? TERM_CAN_UP : 0;
211 if (GoodStr(T_me) && GoodStr(T_ue))
212 EL_FLAGS |= (strcmp(Str(T_me), Str(T_ue)) == 0) ? TERM_CAN_ME : 0;
214 EL_FLAGS &= ~TERM_CAN_ME;
215 if (GoodStr(T_me) && GoodStr(T_se))
216 EL_FLAGS |= (strcmp(Str(T_me), Str(T_se)) == 0) ? TERM_CAN_ME : 0;
221 (void) fprintf(el->el_errfile, "WARNING: Your terminal cannot move up.\n");
222 (void) fprintf(el->el_errfile, "Editing may be odd for long lines.\n");
225 (void) fprintf(el->el_errfile, "no clear EOL capability.\n");
227 (void) fprintf(el->el_errfile, "no delete char capability.\n");
229 (void) fprintf(el->el_errfile, "no insert char capability.\n");
230 #endif /* DEBUG_SCREEN */
235 * Initialize the terminal stuff
241 el->el_term.t_buf = (char *) el_malloc(TC_BUFSIZE);
242 el->el_term.t_cap = (char *) el_malloc(TC_BUFSIZE);
243 el->el_term.t_fkey = (fkey_t *) el_malloc(A_K_NKEYS * sizeof(fkey_t));
244 (void) memset(el->el_term.t_fkey, 0, A_K_NKEYS * sizeof(fkey_t));
245 el->el_term.t_loc = 0;
246 el->el_term.t_str = (char **) el_malloc(T_str * sizeof(char*));
247 (void) memset(el->el_term.t_str, 0, T_str * sizeof(char*));
248 el->el_term.t_val = (int *) el_malloc(T_val * sizeof(int));
249 (void) memset(el->el_term.t_val, 0, T_val * sizeof(int));
250 term_outfile = el->el_outfile;
251 (void) term_set(el, NULL);
257 * Clean up the terminal stuff
263 el_free((ptr_t) el->el_term.t_buf);
264 el->el_term.t_buf = NULL;
265 el_free((ptr_t) el->el_term.t_cap);
266 el->el_term.t_cap = NULL;
267 el->el_term.t_loc = 0;
268 el_free((ptr_t) el->el_term.t_str);
269 el->el_term.t_str = NULL;
270 el_free((ptr_t) el->el_term.t_val);
271 el->el_term.t_val = NULL;
272 term_free_display(el);
277 * Maintain a string pool for termcap strings
280 term_alloc(el, t, cap)
282 struct termcapstr *t;
285 char termbuf[TC_BUFSIZE];
287 char **tlist = el->el_term.t_str;
288 char **tmp, **str = &tlist[t - tstr];
290 if (cap == NULL || *cap == '\0') {
297 tlen = *str == NULL ? 0 : strlen(*str);
300 * New string is shorter; no need to allocate space
303 (void)strcpy(*str, cap); /* XXX strcpy is safe */
308 * New string is longer; see if we have enough space to append
310 if (el->el_term.t_loc + 3 < TC_BUFSIZE) {
311 /* XXX strcpy is safe */
312 (void)strcpy(*str = &el->el_term.t_buf[el->el_term.t_loc], cap);
313 el->el_term.t_loc += clen + 1; /* one for \0 */
318 * Compact our buffer; no need to check compaction, cause we know it
322 for (tmp = tlist; tmp < &tlist[T_str]; tmp++)
323 if (*tmp != NULL && *tmp != '\0' && *tmp != *str) {
326 for (ptr = *tmp; *ptr != '\0'; termbuf[tlen++] = *ptr++)
328 termbuf[tlen++] = '\0';
330 memcpy(el->el_term.t_buf, termbuf, TC_BUFSIZE);
331 el->el_term.t_loc = tlen;
332 if (el->el_term.t_loc + 3 >= TC_BUFSIZE) {
333 (void) fprintf(el->el_errfile, "Out of termcap string space.\n");
336 /* XXX strcpy is safe */
337 (void)strcpy(*str = &el->el_term.t_buf[el->el_term.t_loc], cap);
338 el->el_term.t_loc += clen + 1; /* one for \0 */
340 } /* end term_alloc */
343 /* term_rebuffer_display():
344 * Rebuffer the display after the screen changed size
347 term_rebuffer_display(el)
350 coord_t *c = &el->el_term.t_size;
352 term_free_display(el);
354 /* make this public, -1 to avoid wraps */
355 c->h = Val(T_co) - 1;
356 c->v = (EL_BUFSIZ * 4) / c->h + 1;
358 term_alloc_display(el);
359 } /* end term_rebuffer_display */
362 /* term_alloc_display():
363 * Allocate a new display.
366 term_alloc_display(el)
371 coord_t *c = &el->el_term.t_size;
373 b = (char **) el_malloc((size_t) (sizeof(char *) * (c->v + 1)));
374 for (i = 0; i < c->v; i++)
375 b[i] = (char *) el_malloc((size_t) (sizeof(char) * (c->h + 1)));
379 b = (char **) el_malloc((size_t) (sizeof(char *) * (c->v + 1)));
380 for (i = 0; i < c->v; i++)
381 b[i] = (char *) el_malloc((size_t) (sizeof(char) * (c->h + 1)));
385 } /* end term_alloc_display */
388 /* term_free_display():
389 * Free the display buffers
392 term_free_display(el)
399 el->el_display = NULL;
401 for (bufp = b; *bufp != NULL; bufp++)
402 el_free((ptr_t) *bufp);
406 el->el_vdisplay = NULL;
408 for (bufp = b; *bufp != NULL; bufp++)
409 el_free((ptr_t) * bufp);
412 } /* end term_free_display */
415 /* term_move_to_line():
416 * move to line <where> (first line == 0)
417 * as efficiently as possible
420 term_move_to_line(el, where)
426 if (where == el->el_cursor.v)
429 if (where > el->el_term.t_size.v) {
431 (void) fprintf(el->el_errfile,
432 "term_move_to_line: where is ridiculous: %d\r\n", where);
433 #endif /* DEBUG_SCREEN */
437 if ((del = where - el->el_cursor.v) > 0) {
438 if ((del > 1) && GoodStr(T_DO))
439 (void) tputs(tgoto(Str(T_DO), del, del), del, term__putc);
441 for (i = 0; i < del; i++)
443 el->el_cursor.h = 0; /* because the \n will become \r\n */
447 if (GoodStr(T_UP) && (-del > 1 || !GoodStr(T_up)))
448 (void) tputs(tgoto(Str(T_UP), -del, -del), -del, term__putc);
451 for (i = 0; i < -del; i++)
452 (void) tputs(Str(T_up), 1, term__putc);
455 el->el_cursor.v = where; /* now where is here */
456 } /* end term_move_to_line */
459 /* term_move_to_char():
460 * Move to the character position specified
463 term_move_to_char(el, where)
470 if (where == el->el_cursor.h)
473 if (where > (el->el_term.t_size.h + 1)) {
475 (void) fprintf(el->el_errfile,
476 "term_move_to_char: where is riduculous: %d\r\n", where);
477 #endif /* DEBUG_SCREEN */
481 if (!where) { /* if where is first column */
482 term__putc('\r'); /* do a CR */
487 del = where - el->el_cursor.h;
489 if ((del < -4 || del > 4) && GoodStr(T_ch))
490 /* go there directly */
491 (void) tputs(tgoto(Str(T_ch), where, where), where, term__putc);
493 if (del > 0) { /* moving forward */
494 if ((del > 4) && GoodStr(T_RI))
495 (void) tputs(tgoto(Str(T_RI), del, del), del, term__putc);
497 if (EL_CAN_TAB) { /* if I can do tabs, use them */
498 if ((el->el_cursor.h & 0370) != (where & 0370)) {
499 /* if not within tab stop */
500 for (i = (el->el_cursor.h & 0370);
501 i < (where & 0370); i += 8)
502 term__putc('\t'); /* then tab over */
503 el->el_cursor.h = where & 0370;
506 /* it's usually cheaper to just write the chars, so we do. */
508 /* NOTE THAT term_overwrite() WILL CHANGE el->el_cursor.h!!! */
510 &el->el_display[el->el_cursor.v][el->el_cursor.h],
511 where - el->el_cursor.h);
515 else { /* del < 0 := moving backward */
516 if ((-del > 4) && GoodStr(T_LE))
517 (void) tputs(tgoto(Str(T_LE), -del, -del), -del, term__putc);
518 else { /* can't go directly there */
519 /* if the "cost" is greater than the "cost" from col 0 */
520 if (EL_CAN_TAB ? (-del > ((where >> 3) + (where & 07)))
522 term__putc('\r'); /* do a CR */
524 goto mc_again; /* and try again */
526 for (i = 0; i < -del; i++)
531 el->el_cursor.h = where; /* now where is here */
532 } /* end term_move_to_char */
536 * Overstrike num characters
539 term_overwrite(el, cp, n)
545 return; /* catch bugs */
547 if (n > (el->el_term.t_size.h + 1)) {
549 (void) fprintf(el->el_errfile, "term_overwrite: n is riduculous: %d\r\n", n);
550 #endif /* DEBUG_SCREEN */
558 } /* end term_overwrite */
561 /* term_deletechars():
562 * Delete num characters
565 term_deletechars(el, num)
572 if (!EL_CAN_DELETE) {
574 (void) fprintf(el->el_errfile, " ERROR: cannot delete \n");
575 #endif /* DEBUG_EDIT */
579 if (num > el->el_term.t_size.h) {
581 (void) fprintf(el->el_errfile,
582 "term_deletechars: num is riduculous: %d\r\n", num);
583 #endif /* DEBUG_SCREEN */
587 if (GoodStr(T_DC)) /* if I have multiple delete */
588 if ((num > 1) || !GoodStr(T_dc)) { /* if dc would be more expen. */
589 (void) tputs(tgoto(Str(T_DC), num, num), num, term__putc);
593 if (GoodStr(T_dm)) /* if I have delete mode */
594 (void) tputs(Str(T_dm), 1, term__putc);
596 if (GoodStr(T_dc)) /* else do one at a time */
598 (void) tputs(Str(T_dc), 1, term__putc);
600 if (GoodStr(T_ed)) /* if I have delete mode */
601 (void) tputs(Str(T_ed), 1, term__putc);
602 } /* end term_deletechars */
605 /* term_insertwrite():
606 * Puts terminal in insert character mode or inserts num
607 * characters in the line
610 term_insertwrite(el, cp, num)
617 if (!EL_CAN_INSERT) {
619 (void) fprintf(el->el_errfile, " ERROR: cannot insert \n");
620 #endif /* DEBUG_EDIT */
624 if (num > el->el_term.t_size.h) {
626 (void) fprintf(el->el_errfile, "StartInsert: num is riduculous: %d\r\n", num);
627 #endif /* DEBUG_SCREEN */
631 if (GoodStr(T_IC)) /* if I have multiple insert */
632 if ((num > 1) || !GoodStr(T_ic)) { /* if ic would be more expen. */
633 (void) tputs(tgoto(Str(T_IC), num, num), num, term__putc);
634 term_overwrite(el, cp, num); /* this updates el_cursor.h */
638 if (GoodStr(T_im) && GoodStr(T_ei)) { /* if I have insert mode */
639 (void) tputs(Str(T_im), 1, term__putc);
641 el->el_cursor.h += num;
646 if (GoodStr(T_ip)) /* have to make num chars insert */
647 (void) tputs(Str(T_ip), 1, term__putc);
649 (void) tputs(Str(T_ei), 1, term__putc);
654 if (GoodStr(T_ic)) /* have to make num chars insert */
655 (void) tputs(Str(T_ic), 1, term__putc); /* insert a char */
661 if (GoodStr(T_ip)) /* have to make num chars insert */
662 (void) tputs(Str(T_ip), 1, term__putc);/* pad the inserted char */
665 } /* end term_insertwrite */
669 * clear to end of line. There are num characters to clear
672 term_clear_EOL(el, num)
678 if (EL_CAN_CEOL && GoodStr(T_ce))
679 (void) tputs(Str(T_ce), 1, term__putc);
681 for (i = 0; i < num; i++)
683 el->el_cursor.h += num; /* have written num spaces */
685 } /* end term_clear_EOL */
688 /* term_clear_screen():
692 term_clear_screen(el)
694 { /* clear the whole screen and home */
696 /* send the clear screen code */
697 (void) tputs(Str(T_cl), Val(T_li), term__putc);
698 else if (GoodStr(T_ho) && GoodStr(T_cd)) {
699 (void) tputs(Str(T_ho), Val(T_li), term__putc); /* home */
700 /* clear to bottom of screen */
701 (void) tputs(Str(T_cd), Val(T_li), term__putc);
707 } /* end term_clear_screen */
711 * Beep the way the terminal wants us
718 (void) tputs(Str(T_vb), 1, term__putc); /* visible bell */
719 else if (GoodStr(T_bl))
720 /* what termcap says we should use */
721 (void) tputs(Str(T_bl), 1, term__putc);
723 term__putc('\007'); /* an ASCII bell; ^G */
724 } /* end term_beep */
728 /* term_clear_to_bottom():
729 * Clear to the bottom of the screen
732 term_clear_to_bottom(el)
736 (void) tputs(Str(T_cd), Val(T_li), term__putc);
737 else if (GoodStr(T_ce))
738 (void) tputs(Str(T_ce), Val(T_li), term__putc);
739 } /* end term_clear_to_bottom */
744 * Read in the terminal capabilities from the requested terminal
752 char buf[TC_BUFSIZE];
754 struct termcapstr *t;
758 (void) sigemptyset(&nset);
759 (void) sigaddset(&nset, SIGWINCH);
760 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
766 term = getenv("TERM");
768 if (!term || !term[0])
771 memset(el->el_term.t_cap, 0, TC_BUFSIZE);
773 i = tgetent(el->el_term.t_cap, term);
777 (void) fprintf(el->el_errfile, "Cannot read termcap database;\n");
779 (void) fprintf(el->el_errfile,
780 "No entry for terminal type \"%s\";\n", term);
781 (void) fprintf(el->el_errfile, "using dumb terminal settings.\n");
782 Val(T_co) = 80; /* do a dumb terminal */
783 Val(T_pt) = Val(T_km) = Val(T_li) = 0;
784 Val(T_xt) = Val(T_MT);
785 for (t = tstr; t->name != NULL; t++)
786 term_alloc(el, t, NULL);
790 Val(T_pt) = tgetflag("pt");
791 Val(T_xt) = tgetflag("xt");
792 /* do we have a meta? */
793 Val(T_km) = tgetflag("km");
794 Val(T_MT) = tgetflag("MT");
796 Val(T_co) = tgetnum("co");
797 Val(T_li) = tgetnum("li");
798 for (t = tstr; t->name != NULL; t++)
799 term_alloc(el, t, tgetstr(t->name, &area));
803 Val(T_co) = 80; /* just in case */
807 el->el_term.t_size.v = Val(T_co);
808 el->el_term.t_size.h = Val(T_li);
812 (void) term_get_size(el, &lins, &cols);/* get the correct window size */
813 term_change_size(el, lins, cols);
814 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
816 return i <= 0 ? -1 : 0;
821 * Return the new window size in lines and cols, and
822 * true if the size was changed.
825 term_get_size(el, lins, cols)
836 if (ioctl(el->el_infd, TIOCGWINSZ, (ioctl_t) &ws) != -1) {
847 if (ioctl(el->el_infd, TIOCGSIZE, (ioctl_t) &ts) != -1) {
855 return (Val(T_co) != *cols || Val(T_li) != *lins);
856 } /* end term_get_size */
859 /* term_change_size():
860 * Change the size of the terminal
863 term_change_size(el, lins, cols)
870 Val(T_co) = (cols < 2) ? 80 : cols;
871 Val(T_li) = (lins < 1) ? 24 : lins;
873 term_rebuffer_display(el); /* re-make display buffers */
874 re_clear_display(el);
875 } /* end term_change_size */
878 /* term_init_arrow():
879 * Initialize the arrow key bindings from termcap
885 fkey_t *arrow = el->el_term.t_fkey;
887 arrow[A_K_DN].name = "down";
888 arrow[A_K_DN].key = T_kd;
889 arrow[A_K_DN].fun.cmd = ED_NEXT_HISTORY;
890 arrow[A_K_DN].type = XK_CMD;
892 arrow[A_K_UP].name = "up";
893 arrow[A_K_UP].key = T_ku;
894 arrow[A_K_UP].fun.cmd = ED_PREV_HISTORY;
895 arrow[A_K_UP].type = XK_CMD;
897 arrow[A_K_LT].name = "left";
898 arrow[A_K_LT].key = T_kl;
899 arrow[A_K_LT].fun.cmd = ED_PREV_CHAR;
900 arrow[A_K_LT].type = XK_CMD;
902 arrow[A_K_RT].name = "right";
903 arrow[A_K_RT].key = T_kr;
904 arrow[A_K_RT].fun.cmd = ED_NEXT_CHAR;
905 arrow[A_K_RT].type = XK_CMD;
907 arrow[A_K_HO].name = "home";
908 arrow[A_K_HO].key = T_kh;
909 arrow[A_K_HO].fun.cmd = ED_MOVE_TO_BEG;
910 arrow[A_K_HO].type = XK_CMD;
912 arrow[A_K_EN].name = "end";
913 arrow[A_K_EN].key = T_at7;
914 arrow[A_K_EN].fun.cmd = ED_MOVE_TO_END;
915 arrow[A_K_EN].type = XK_CMD;
919 /* term_reset_arrow():
920 * Reset arrow key bindings
926 fkey_t *arrow = el->el_term.t_fkey;
927 static char strA[] = {033, '[', 'A', '\0'};
928 static char strB[] = {033, '[', 'B', '\0'};
929 static char strC[] = {033, '[', 'C', '\0'};
930 static char strD[] = {033, '[', 'D', '\0'};
931 static char str1[] = {033, '[', '1', '~', '\0'};
932 static char str4[] = {033, '[', '4', '~', '\0'};
933 static char stOA[] = {033, 'O', 'A', '\0'};
934 static char stOB[] = {033, 'O', 'B', '\0'};
935 static char stOC[] = {033, 'O', 'C', '\0'};
936 static char stOD[] = {033, 'O', 'D', '\0'};
938 key_add(el, strA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
939 key_add(el, strB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
940 key_add(el, strC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
941 key_add(el, strD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
942 key_add(el, str1, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
943 key_add(el, str4, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
944 key_add(el, stOA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
945 key_add(el, stOB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
946 key_add(el, stOC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
947 key_add(el, stOD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
949 if (el->el_map.type == MAP_VI) {
950 key_add(el, &strA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
951 key_add(el, &strB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
952 key_add(el, &strC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
953 key_add(el, &strD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
954 key_add(el, &str1[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
955 key_add(el, &str4[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
956 key_add(el, &stOA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
957 key_add(el, &stOB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
958 key_add(el, &stOC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
959 key_add(el, &stOD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
965 * Set an arrow key binding
968 term_set_arrow(el, name, fun, type)
974 fkey_t *arrow = el->el_term.t_fkey;
977 for (i = 0; i < A_K_NKEYS; i++)
978 if (strcmp(name, arrow[i].name) == 0) {
980 arrow[i].type = type;
987 /* term_clear_arrow():
988 * Clear an arrow key binding
991 term_clear_arrow(el, name)
995 fkey_t *arrow = el->el_term.t_fkey;
998 for (i = 0; i < A_K_NKEYS; i++)
999 if (strcmp(name, arrow[i].name) == 0) {
1000 arrow[i].type = XK_NOD;
1007 /* term_print_arrow():
1008 * Print the arrow key bindings
1011 term_print_arrow(el, name)
1016 fkey_t *arrow = el->el_term.t_fkey;
1018 for (i = 0; i < A_K_NKEYS; i++)
1019 if (*name == '\0' || strcmp(name, arrow[i].name) == 0)
1020 if (arrow[i].type != XK_NOD)
1021 key_kprint(el, arrow[i].name, &arrow[i].fun, arrow[i].type);
1025 /* term_bind_arrow():
1026 * Bind the arrow keys
1032 el_action_t *map, *dmap;
1035 fkey_t *arrow = el->el_term.t_fkey;
1037 /* Check if the components needed are initialized */
1038 if (el->el_term.t_buf == NULL || el->el_map.key == NULL)
1041 map = el->el_map.type == MAP_VI ? el->el_map.alt : el->el_map.key;
1042 dmap = el->el_map.type == MAP_VI ? el->el_map.vic : el->el_map.emacs;
1044 term_reset_arrow(el);
1046 for (i = 0; i < A_K_NKEYS; i++) {
1047 p = el->el_term.t_str[arrow[i].key];
1049 j = (unsigned char) *p;
1051 * Assign the arrow keys only if:
1053 * 1. They are multi-character arrow keys and the user
1054 * has not re-assigned the leading character, or
1055 * has re-assigned the leading character to be
1056 * ED_SEQUENCE_LEAD_IN
1057 * 2. They are single arrow keys pointing to an unassigned key.
1059 if (arrow[i].type == XK_NOD)
1060 key_clear(el, map, p);
1062 if (p[1] && (dmap[j] == map[j] ||
1063 map[j] == ED_SEQUENCE_LEAD_IN)) {
1064 key_add(el, p, &arrow[i].fun, arrow[i].type);
1065 map[j] = ED_SEQUENCE_LEAD_IN;
1067 else if (map[j] == ED_UNASSIGNED) {
1068 key_clear(el, map, p);
1069 if (arrow[i].type == XK_CMD)
1070 map[j] = arrow[i].fun.cmd;
1072 key_add(el, p, &arrow[i].fun, arrow[i].type);
1087 return fputc(c, term_outfile);
1088 } /* end term__putc */
1097 (void) fflush(term_outfile);
1098 } /* end term__flush */
1102 * Print the current termcap characteristics
1106 term_telltc(el, argc, argv)
1111 struct termcapstr *t;
1113 char upbuf[EL_BUFSIZ];
1115 (void) fprintf(el->el_outfile, "\n\tYour terminal has the\n");
1116 (void) fprintf(el->el_outfile, "\tfollowing characteristics:\n\n");
1117 (void) fprintf(el->el_outfile, "\tIt has %d columns and %d lines\n",
1118 Val(T_co), Val(T_li));
1119 (void) fprintf(el->el_outfile,
1120 "\tIt has %s meta key\n", EL_HAS_META ? "a" : "no");
1121 (void) fprintf(el->el_outfile,
1122 "\tIt can%suse tabs\n", EL_CAN_TAB ? " " : "not ");
1124 (void) fprintf(el->el_outfile, "\tIt %s automatic margins\n",
1125 (T_Margin&MARGIN_AUTO)? "has": "does not have");
1126 if (T_Margin & MARGIN_AUTO)
1127 (void) fprintf(el->el_outfile, "\tIt %s magic margins\n",
1128 (T_Margin&MARGIN_MAGIC)?"has":"does not have");
1131 for (t = tstr, ts = el->el_term.t_str; t->name != NULL; t++, ts++)
1132 (void) fprintf(el->el_outfile, "\t%25s (%s) == %s\n", t->long_name,
1133 t->name, *ts && **ts ?
1134 key__decode_str(*ts, upbuf, "") : "(empty)");
1135 (void) fputc('\n', el->el_outfile);
1141 * Change the current terminal characteristics
1145 term_settc(el, argc, argv)
1150 struct termcapstr *ts;
1151 struct termcapval *tv;
1154 if (argv == NULL || argv[1] == NULL || argv[2] == NULL)
1161 * Do the strings first
1163 for (ts = tstr; ts->name != NULL; ts++)
1164 if (strcmp(ts->name, what) == 0)
1167 if (ts->name != NULL) {
1168 term_alloc(el, ts, how);
1174 * Do the numeric ones second
1176 for (tv = tval; tv->name != NULL; tv++)
1177 if (strcmp(tv->name, what) == 0)
1180 if (tv->name != NULL) {
1181 if (tv == &tval[T_pt] || tv == &tval[T_km]
1183 || tv == &tval[T_am] || tv == &tval[T_xn]
1186 if (strcmp(how, "yes") == 0)
1187 el->el_term.t_val[tv - tval] = 1;
1188 else if (strcmp(how, "no") == 0)
1189 el->el_term.t_val[tv - tval] = 0;
1191 (void) fprintf(el->el_errfile, "settc: Bad value `%s'.\n", how);
1195 term_change_size(el, Val(T_li), Val(T_co));
1199 el->el_term.t_val[tv - tval] = atoi(how);
1200 el->el_term.t_size.v = Val(T_co);
1201 el->el_term.t_size.h = Val(T_li);
1202 if (tv == &tval[T_co] || tv == &tval[T_li])
1203 term_change_size(el, Val(T_li), Val(T_co));
1212 * Print the termcap string out with variable substitution
1216 term_echotc(el, argc, argv)
1222 int arg_need, arg_cols, arg_rows;
1223 int verbose = 0, silent = 0;
1225 static char *fmts = "%s\n", *fmtd = "%d\n";
1226 struct termcapstr *t;
1227 char buf[TC_BUFSIZE];
1231 if (argv == NULL || argv[1] == NULL)
1235 if (argv[0][0] == '-') {
1236 switch (argv[0][1]) {
1244 /* stderror(ERR_NAME | ERR_TCUSAGE); */
1249 if (!*argv || *argv[0] == '\0')
1251 if (strcmp(*argv, "tabs") == 0) {
1252 (void) fprintf(el->el_outfile, fmts, EL_CAN_TAB ? "yes" : "no");
1255 else if (strcmp(*argv, "meta") == 0) {
1256 (void) fprintf(el->el_outfile, fmts, Val(T_km) ? "yes" : "no");
1260 else if (strcmp(*argv, "xn") == 0) {
1261 (void) fprintf(el->el_outfile, fmts, T_Margin & MARGIN_MAGIC ?
1265 else if (strcmp(*argv, "am") == 0) {
1266 (void) fprintf(el->el_outfile, fmts, T_Margin & MARGIN_AUTO ?
1271 else if (strcmp(*argv, "baud") == 0) {
1272 (void) fprintf(el->el_outfile, "%lu\n", (u_long)el->el_tty.t_speed);
1275 else if (strcmp(*argv, "rows") == 0 || strcmp(*argv, "lines") == 0) {
1276 (void) fprintf(el->el_outfile, fmtd, Val(T_li));
1279 else if (strcmp(*argv, "cols") == 0) {
1280 (void) fprintf(el->el_outfile, fmtd, Val(T_co));
1285 * Try to use our local definition first
1288 for (t = tstr; t->name != NULL; t++)
1289 if (strcmp(t->name, *argv) == 0) {
1290 scap = el->el_term.t_str[t - tstr];
1293 if (t->name == NULL)
1294 scap = tgetstr(*argv, &area);
1295 if (!scap || scap[0] == '\0') {
1297 (void) fprintf(el->el_errfile,
1298 "echotc: Termcap parameter `%s' not found.\n", *argv);
1303 * Count home many values we need for this capability.
1305 for (cap = scap, arg_need = 0; *cap; cap++)
1325 * hpux has lot's of them...
1328 (void) fprintf(el->el_errfile,
1329 "echotc: Warning: unknown termcap %% `%c'.\n", *cap);
1330 /* This is bad, but I won't complain */
1337 if (*argv && *argv[0]) {
1339 (void) fprintf(el->el_errfile,
1340 "echotc: Warning: Extra argument `%s'.\n", *argv);
1343 (void) tputs(scap, 1, term__putc);
1347 if (!*argv || *argv[0] == '\0') {
1349 (void) fprintf(el->el_errfile,
1350 "echotc: Warning: Missing argument.\n");
1354 arg_rows = atoi(*argv);
1356 if (*argv && *argv[0]) {
1358 (void) fprintf(el->el_errfile,
1359 "echotc: Warning: Extra argument `%s'.\n", *argv);
1362 (void) tputs(tgoto(scap, arg_cols, arg_rows), 1, term__putc);
1365 /* This is wrong, but I will ignore it... */
1367 (void) fprintf(el->el_errfile,
1368 "echotc: Warning: Too many required arguments (%d).\n",
1373 if (!*argv || *argv[0] == '\0') {
1375 (void) fprintf(el->el_errfile,
1376 "echotc: Warning: Missing argument.\n");
1379 arg_cols = atoi(*argv);
1381 if (!*argv || *argv[0] == '\0') {
1383 (void) fprintf(el->el_errfile,
1384 "echotc: Warning: Missing argument.\n");
1387 arg_rows = atoi(*argv);
1389 if (*argv && *argv[0]) {
1391 (void) fprintf(el->el_errfile,
1392 "echotc: Warning: Extra argument `%s'.\n", *argv);
1395 (void) tputs(tgoto(scap, arg_cols, arg_rows), arg_rows, term__putc);