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 * $NetBSD: term.c,v 1.31 2001/01/10 22:42:56 jdolecek 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 */
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
46 * term.c: Editor/termcap-curses interface
47 * We have to declare a static variable here, since the
48 * termcap putchar routine does not take an argument!
57 #include <sys/types.h>
58 #include <sys/ioctl.h>
63 * IMPORTANT NOTE: these routines are allowed to look at the current screen
64 * and the current possition assuming that it is correct. If this is not
65 * true, then the update will be WRONG! This is (should be) a valid
69 #define TC_BUFSIZE 2048
71 #define GoodStr(a) (el->el_term.t_str[a] != NULL && \
72 el->el_term.t_str[a][0] != '\0')
73 #define Str(a) el->el_term.t_str[a]
74 #define Val(a) el->el_term.t_val[a]
77 private const struct {
148 private const struct termcapstr {
150 const char *long_name;
153 { "al", "add new blank line" },
155 { "bl", "audible bell" },
157 { "cd", "clear to bottom" },
159 { "ce", "clear to end of line" },
161 { "ch", "cursor to horiz pos" },
163 { "cl", "clear screen" },
165 { "dc", "delete a character" },
167 { "dl", "delete a line" },
169 { "dm", "start delete mode" },
171 { "ed", "end delete mode" },
173 { "ei", "end insert mode" },
175 { "fs", "cursor from status line" },
177 { "ho", "home cursor" },
179 { "ic", "insert character" },
181 { "im", "start insert mode" },
183 { "ip", "insert padding" },
185 { "kd", "sends cursor down" },
187 { "kl", "sends cursor left" },
189 { "kr", "sends cursor right" },
191 { "ku", "sends cursor up" },
193 { "md", "begin bold" },
195 { "me", "end attributes" },
197 { "nd", "non destructive space" },
199 { "se", "end standout" },
201 { "so", "begin standout" },
203 { "ts", "cursor to status line" },
205 { "up", "cursor up one" },
207 { "us", "begin underline" },
209 { "ue", "end underline" },
211 { "vb", "visible bell" },
213 { "DC", "delete multiple chars" },
215 { "DO", "cursor down multiple" },
217 { "IC", "insert multiple chars" },
219 { "LE", "cursor left multiple" },
221 { "RI", "cursor right multiple" },
223 { "UP", "cursor up multiple" },
225 { "kh", "send cursor home" },
227 { "@7", "send cursor end" },
232 private const struct termcapval {
234 const char *long_name;
237 { "am", "has automatic margins" },
239 { "pt", "has physical tabs" },
241 { "li", "Number of lines" },
243 { "co", "Number of columns" },
245 { "km", "Has meta key" },
247 { "xt", "Tab chars destructive" },
249 { "xn", "newline ignored at right margin" },
251 { "MT", "Has meta key" }, /* XXX? */
255 /* do two or more of the attributes use me */
257 private void term_setflags(EditLine *);
258 private int term_rebuffer_display(EditLine *);
259 private void term_free_display(EditLine *);
260 private int term_alloc_display(EditLine *);
261 private void term_alloc(EditLine *, const struct termcapstr *, char *);
262 private void term_init_arrow(EditLine *);
263 private void term_reset_arrow(EditLine *);
266 private FILE *term_outfile = NULL; /* XXX: How do we fix that? */
270 * Set the terminal capability flags
273 term_setflags(EditLine *el)
276 if (el->el_tty.t_tabs)
277 EL_FLAGS |= (Val(T_pt) && !Val(T_xt)) ? TERM_CAN_TAB : 0;
279 EL_FLAGS |= (Val(T_km) || Val(T_MT)) ? TERM_HAS_META : 0;
280 EL_FLAGS |= GoodStr(T_ce) ? TERM_CAN_CEOL : 0;
281 EL_FLAGS |= (GoodStr(T_dc) || GoodStr(T_DC)) ? TERM_CAN_DELETE : 0;
282 EL_FLAGS |= (GoodStr(T_im) || GoodStr(T_ic) || GoodStr(T_IC)) ?
284 EL_FLAGS |= (GoodStr(T_up) || GoodStr(T_UP)) ? TERM_CAN_UP : 0;
285 EL_FLAGS |= Val(T_am) ? TERM_HAS_AUTO_MARGINS : 0;
286 EL_FLAGS |= Val(T_xn) ? TERM_HAS_MAGIC_MARGINS : 0;
288 if (GoodStr(T_me) && GoodStr(T_ue))
289 EL_FLAGS |= (strcmp(Str(T_me), Str(T_ue)) == 0) ?
292 EL_FLAGS &= ~TERM_CAN_ME;
293 if (GoodStr(T_me) && GoodStr(T_se))
294 EL_FLAGS |= (strcmp(Str(T_me), Str(T_se)) == 0) ?
300 (void) fprintf(el->el_errfile,
301 "WARNING: Your terminal cannot move up.\n");
302 (void) fprintf(el->el_errfile,
303 "Editing may be odd for long lines.\n");
306 (void) fprintf(el->el_errfile, "no clear EOL capability.\n");
308 (void) fprintf(el->el_errfile, "no delete char capability.\n");
310 (void) fprintf(el->el_errfile, "no insert char capability.\n");
311 #endif /* DEBUG_SCREEN */
316 * Initialize the terminal stuff
319 term_init(EditLine *el)
322 el->el_term.t_buf = (char *) el_malloc(TC_BUFSIZE);
323 if (el->el_term.t_buf == NULL)
325 el->el_term.t_cap = (char *) el_malloc(TC_BUFSIZE);
326 if (el->el_term.t_cap == NULL)
328 el->el_term.t_fkey = (fkey_t *) el_malloc(A_K_NKEYS * sizeof(fkey_t));
329 if (el->el_term.t_fkey == NULL)
331 el->el_term.t_loc = 0;
332 el->el_term.t_str = (char **) el_malloc(T_str * sizeof(char *));
333 if (el->el_term.t_str == NULL)
335 (void) memset(el->el_term.t_str, 0, T_str * sizeof(char *));
336 el->el_term.t_val = (int *) el_malloc(T_val * sizeof(int));
337 if (el->el_term.t_val == NULL)
339 (void) memset(el->el_term.t_val, 0, T_val * sizeof(int));
340 term_outfile = el->el_outfile;
342 if (term_set(el, NULL) == -1)
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 term_free_display(el);
367 * Maintain a string pool for termcap strings
370 term_alloc(EditLine *el, const struct termcapstr *t, char *cap)
372 char termbuf[TC_BUFSIZE];
374 char **tlist = el->el_term.t_str;
375 char **tmp, **str = &tlist[t - tstr];
377 if (cap == NULL || *cap == '\0') {
383 tlen = *str == NULL ? 0 : strlen(*str);
386 * New string is shorter; no need to allocate space
389 (void) strcpy(*str, cap); /* XXX strcpy is safe */
393 * New string is longer; see if we have enough space to append
395 if (el->el_term.t_loc + 3 < TC_BUFSIZE) {
396 /* XXX strcpy is safe */
397 (void) strcpy(*str = &el->el_term.t_buf[el->el_term.t_loc],
399 el->el_term.t_loc += clen + 1; /* one for \0 */
403 * Compact our buffer; no need to check compaction, cause we know it
407 for (tmp = tlist; tmp < &tlist[T_str]; tmp++)
408 if (*tmp != NULL && *tmp != '\0' && *tmp != *str) {
411 for (ptr = *tmp; *ptr != '\0'; termbuf[tlen++] = *ptr++)
413 termbuf[tlen++] = '\0';
415 memcpy(el->el_term.t_buf, termbuf, TC_BUFSIZE);
416 el->el_term.t_loc = tlen;
417 if (el->el_term.t_loc + 3 >= TC_BUFSIZE) {
418 (void) fprintf(el->el_errfile,
419 "Out of termcap string space.\n");
422 /* XXX strcpy is safe */
423 (void) strcpy(*str = &el->el_term.t_buf[el->el_term.t_loc], cap);
424 el->el_term.t_loc += clen + 1; /* one for \0 */
429 /* term_rebuffer_display():
430 * Rebuffer the display after the screen changed size
433 term_rebuffer_display(EditLine *el)
435 coord_t *c = &el->el_term.t_size;
437 term_free_display(el);
442 if (term_alloc_display(el) == -1)
448 /* term_alloc_display():
449 * Allocate a new display.
452 term_alloc_display(EditLine *el)
456 coord_t *c = &el->el_term.t_size;
458 b = (char **) el_malloc((size_t) (sizeof(char *) * (c->v + 1)));
461 for (i = 0; i < c->v; i++) {
462 b[i] = (char *) el_malloc((size_t) (sizeof(char) * (c->h + 1)));
469 b = (char **) el_malloc((size_t) (sizeof(char *) * (c->v + 1)));
472 for (i = 0; i < c->v; i++) {
473 b[i] = (char *) el_malloc((size_t) (sizeof(char) * (c->h + 1)));
483 /* term_free_display():
484 * Free the display buffers
487 term_free_display(EditLine *el)
493 el->el_display = NULL;
495 for (bufp = b; *bufp != NULL; bufp++)
496 el_free((ptr_t) * bufp);
500 el->el_vdisplay = NULL;
502 for (bufp = b; *bufp != NULL; bufp++)
503 el_free((ptr_t) * bufp);
509 /* term_move_to_line():
510 * move to line <where> (first line == 0)
511 * as efficiently as possible
514 term_move_to_line(EditLine *el, int where)
518 if (where == el->el_cursor.v)
521 if (where > el->el_term.t_size.v) {
523 (void) fprintf(el->el_errfile,
524 "term_move_to_line: where is ridiculous: %d\r\n", where);
525 #endif /* DEBUG_SCREEN */
528 if ((del = where - el->el_cursor.v) > 0) {
530 if (EL_HAS_AUTO_MARGINS &&
531 el->el_display[el->el_cursor.v][0] != '\0') {
532 /* move without newline */
533 term_move_to_char(el, el->el_term.t_size.h - 1);
535 &el->el_display[el->el_cursor.v][el->el_cursor.h],
540 if ((del > 1) && GoodStr(T_DO)) {
541 (void) tputs(tgoto(Str(T_DO), del, del),
545 for (; del > 0; del--)
547 /* because the \n will become \r\n */
552 } else { /* del < 0 */
553 if (GoodStr(T_UP) && (-del > 1 || !GoodStr(T_up)))
554 (void) tputs(tgoto(Str(T_UP), -del, -del), -del,
558 for (; del < 0; del++)
559 (void) tputs(Str(T_up), 1, term__putc);
562 el->el_cursor.v = where;/* now where is here */
566 /* term_move_to_char():
567 * Move to the character position specified
570 term_move_to_char(EditLine *el, int where)
575 if (where == el->el_cursor.h)
578 if (where > el->el_term.t_size.h) {
580 (void) fprintf(el->el_errfile,
581 "term_move_to_char: where is riduculous: %d\r\n", where);
582 #endif /* DEBUG_SCREEN */
585 if (!where) { /* if where is first column */
586 term__putc('\r'); /* do a CR */
590 del = where - el->el_cursor.h;
592 if ((del < -4 || del > 4) && GoodStr(T_ch))
593 /* go there directly */
594 (void) tputs(tgoto(Str(T_ch), where, where), where, term__putc);
596 if (del > 0) { /* moving forward */
597 if ((del > 4) && GoodStr(T_RI))
598 (void) tputs(tgoto(Str(T_RI), del, del),
601 /* if I can do tabs, use them */
603 if ((el->el_cursor.h & 0370) !=
605 /* if not within tab stop */
607 (el->el_cursor.h & 0370);
612 el->el_cursor.h = where & 0370;
616 * it's usually cheaper to just write the
620 * NOTE THAT term_overwrite() WILL CHANGE
624 &el->el_display[el->el_cursor.v][el->el_cursor.h],
625 where - el->el_cursor.h);
628 } else { /* del < 0 := moving backward */
629 if ((-del > 4) && GoodStr(T_LE))
630 (void) tputs(tgoto(Str(T_LE), -del, -del),
632 else { /* can't go directly there */
634 * if the "cost" is greater than the "cost"
638 (-del > (((unsigned int) where >> 3) +
641 term__putc('\r'); /* do a CR */
643 goto mc_again; /* and try again */
645 for (i = 0; i < -del; i++)
650 el->el_cursor.h = where; /* now where is here */
655 * Overstrike num characters
658 term_overwrite(EditLine *el, char *cp, int n)
661 return; /* catch bugs */
663 if (n > el->el_term.t_size.h) {
665 (void) fprintf(el->el_errfile,
666 "term_overwrite: n is riduculous: %d\r\n", n);
667 #endif /* DEBUG_SCREEN */
675 if (el->el_cursor.h >= el->el_term.t_size.h) { /* wrap? */
676 if (EL_HAS_AUTO_MARGINS) { /* yes */
679 if (EL_HAS_MAGIC_MARGINS) {
680 /* force the wrap to avoid the "magic"
683 if ((c = el->el_display[el->el_cursor.v][el->el_cursor.h])
685 term_overwrite(el, &c, 1);
690 } else /* no wrap, but cursor stays on screen */
691 el->el_cursor.h = el->el_term.t_size.h;
696 /* term_deletechars():
697 * Delete num characters
700 term_deletechars(EditLine *el, int num)
705 if (!EL_CAN_DELETE) {
707 (void) fprintf(el->el_errfile, " ERROR: cannot delete \n");
708 #endif /* DEBUG_EDIT */
711 if (num > el->el_term.t_size.h) {
713 (void) fprintf(el->el_errfile,
714 "term_deletechars: num is riduculous: %d\r\n", num);
715 #endif /* DEBUG_SCREEN */
718 if (GoodStr(T_DC)) /* if I have multiple delete */
719 if ((num > 1) || !GoodStr(T_dc)) { /* if dc would be more
721 (void) tputs(tgoto(Str(T_DC), num, num),
725 if (GoodStr(T_dm)) /* if I have delete mode */
726 (void) tputs(Str(T_dm), 1, term__putc);
728 if (GoodStr(T_dc)) /* else do one at a time */
730 (void) tputs(Str(T_dc), 1, term__putc);
732 if (GoodStr(T_ed)) /* if I have delete mode */
733 (void) tputs(Str(T_ed), 1, term__putc);
737 /* term_insertwrite():
738 * Puts terminal in insert character mode or inserts num
739 * characters in the line
742 term_insertwrite(EditLine *el, char *cp, int num)
746 if (!EL_CAN_INSERT) {
748 (void) fprintf(el->el_errfile, " ERROR: cannot insert \n");
749 #endif /* DEBUG_EDIT */
752 if (num > el->el_term.t_size.h) {
754 (void) fprintf(el->el_errfile,
755 "StartInsert: num is riduculous: %d\r\n", num);
756 #endif /* DEBUG_SCREEN */
759 if (GoodStr(T_IC)) /* if I have multiple insert */
760 if ((num > 1) || !GoodStr(T_ic)) {
761 /* if ic would be more expensive */
762 (void) tputs(tgoto(Str(T_IC), num, num),
764 term_overwrite(el, cp, num);
765 /* this updates el_cursor.h */
768 if (GoodStr(T_im) && GoodStr(T_ei)) { /* if I have insert mode */
769 (void) tputs(Str(T_im), 1, term__putc);
771 el->el_cursor.h += num;
776 if (GoodStr(T_ip)) /* have to make num chars insert */
777 (void) tputs(Str(T_ip), 1, term__putc);
779 (void) tputs(Str(T_ei), 1, term__putc);
783 if (GoodStr(T_ic)) /* have to make num chars insert */
784 (void) tputs(Str(T_ic), 1, term__putc);
791 if (GoodStr(T_ip)) /* have to make num chars insert */
792 (void) tputs(Str(T_ip), 1, term__putc);
793 /* pad the inserted char */
800 * clear to end of line. There are num characters to clear
803 term_clear_EOL(EditLine *el, int num)
807 if (EL_CAN_CEOL && GoodStr(T_ce))
808 (void) tputs(Str(T_ce), 1, term__putc);
810 for (i = 0; i < num; i++)
812 el->el_cursor.h += num; /* have written num spaces */
817 /* term_clear_screen():
821 term_clear_screen(EditLine *el)
822 { /* clear the whole screen and home */
825 /* send the clear screen code */
826 (void) tputs(Str(T_cl), Val(T_li), term__putc);
827 else if (GoodStr(T_ho) && GoodStr(T_cd)) {
828 (void) tputs(Str(T_ho), Val(T_li), term__putc); /* home */
829 /* clear to bottom of screen */
830 (void) tputs(Str(T_cd), Val(T_li), term__putc);
839 * Beep the way the terminal wants us
842 term_beep(EditLine *el)
845 /* what termcap says we should use */
846 (void) tputs(Str(T_bl), 1, term__putc);
848 term__putc('\007'); /* an ASCII bell; ^G */
853 /* term_clear_to_bottom():
854 * Clear to the bottom of the screen
857 term_clear_to_bottom(EditLine *el)
860 (void) tputs(Str(T_cd), Val(T_li), term__putc);
861 else if (GoodStr(T_ce))
862 (void) tputs(Str(T_ce), Val(T_li), term__putc);
868 * Read in the terminal capabilities from the requested terminal
871 term_set(EditLine *el, char *term)
874 char buf[TC_BUFSIZE];
876 const struct termcapstr *t;
880 (void) sigemptyset(&nset);
881 (void) sigaddset(&nset, SIGWINCH);
882 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
888 term = getenv("TERM");
890 if (!term || !term[0])
893 if (strcmp(term, "emacs") == 0)
894 el->el_flags |= EDIT_DISABLED;
896 memset(el->el_term.t_cap, 0, TC_BUFSIZE);
898 i = tgetent(el->el_term.t_cap, term);
902 (void) fprintf(el->el_errfile,
903 "Cannot read termcap database;\n");
905 (void) fprintf(el->el_errfile,
906 "No entry for terminal type \"%s\";\n", term);
907 (void) fprintf(el->el_errfile,
908 "using dumb terminal settings.\n");
909 Val(T_co) = 80; /* do a dumb terminal */
910 Val(T_pt) = Val(T_km) = Val(T_li) = 0;
911 Val(T_xt) = Val(T_MT);
912 for (t = tstr; t->name != NULL; t++)
913 term_alloc(el, t, NULL);
915 /* auto/magic margins */
916 Val(T_am) = tgetflag("am");
917 Val(T_xn) = tgetflag("xn");
919 Val(T_pt) = tgetflag("pt");
920 Val(T_xt) = tgetflag("xt");
921 /* do we have a meta? */
922 Val(T_km) = tgetflag("km");
923 Val(T_MT) = tgetflag("MT");
925 Val(T_co) = tgetnum("co");
926 Val(T_li) = tgetnum("li");
927 for (t = tstr; t->name != NULL; t++)
928 term_alloc(el, t, tgetstr(t->name, &area));
932 Val(T_co) = 80; /* just in case */
936 el->el_term.t_size.v = Val(T_co);
937 el->el_term.t_size.h = Val(T_li);
941 /* get the correct window size */
942 (void) term_get_size(el, &lins, &cols);
943 if (term_change_size(el, lins, cols) == -1)
945 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
947 return (i <= 0 ? -1 : 0);
952 * Return the new window size in lines and cols, and
953 * true if the size was changed.
956 term_get_size(EditLine *el, int *lins, int *cols)
965 if (ioctl(el->el_infd, TIOCGWINSZ, (ioctl_t) & ws) != -1) {
976 if (ioctl(el->el_infd, TIOCGSIZE, (ioctl_t) & ts) != -1) {
984 return (Val(T_co) != *cols || Val(T_li) != *lins);
988 /* term_change_size():
989 * Change the size of the terminal
992 term_change_size(EditLine *el, int lins, int cols)
997 Val(T_co) = (cols < 2) ? 80 : cols;
998 Val(T_li) = (lins < 1) ? 24 : lins;
1000 /* re-make display buffers */
1001 if (term_rebuffer_display(el) == -1)
1003 re_clear_display(el);
1008 /* term_init_arrow():
1009 * Initialize the arrow key bindings from termcap
1012 term_init_arrow(EditLine *el)
1014 fkey_t *arrow = el->el_term.t_fkey;
1016 arrow[A_K_DN].name = "down";
1017 arrow[A_K_DN].key = T_kd;
1018 arrow[A_K_DN].fun.cmd = ED_NEXT_HISTORY;
1019 arrow[A_K_DN].type = XK_CMD;
1021 arrow[A_K_UP].name = "up";
1022 arrow[A_K_UP].key = T_ku;
1023 arrow[A_K_UP].fun.cmd = ED_PREV_HISTORY;
1024 arrow[A_K_UP].type = XK_CMD;
1026 arrow[A_K_LT].name = "left";
1027 arrow[A_K_LT].key = T_kl;
1028 arrow[A_K_LT].fun.cmd = ED_PREV_CHAR;
1029 arrow[A_K_LT].type = XK_CMD;
1031 arrow[A_K_RT].name = "right";
1032 arrow[A_K_RT].key = T_kr;
1033 arrow[A_K_RT].fun.cmd = ED_NEXT_CHAR;
1034 arrow[A_K_RT].type = XK_CMD;
1036 arrow[A_K_HO].name = "home";
1037 arrow[A_K_HO].key = T_kh;
1038 arrow[A_K_HO].fun.cmd = ED_MOVE_TO_BEG;
1039 arrow[A_K_HO].type = XK_CMD;
1041 arrow[A_K_EN].name = "end";
1042 arrow[A_K_EN].key = T_at7;
1043 arrow[A_K_EN].fun.cmd = ED_MOVE_TO_END;
1044 arrow[A_K_EN].type = XK_CMD;
1048 /* term_reset_arrow():
1049 * Reset arrow key bindings
1052 term_reset_arrow(EditLine *el)
1054 fkey_t *arrow = el->el_term.t_fkey;
1055 static const char strA[] = {033, '[', 'A', '\0'};
1056 static const char strB[] = {033, '[', 'B', '\0'};
1057 static const char strC[] = {033, '[', 'C', '\0'};
1058 static const char strD[] = {033, '[', 'D', '\0'};
1059 static const char strH[] = {033, '[', 'H', '\0'};
1060 static const char strF[] = {033, '[', 'F', '\0'};
1061 static const char str1[] = {033, '[', '1', '~', '\0'};
1062 static const char str4[] = {033, '[', '4', '~', '\0'};
1063 static const char stOA[] = {033, 'O', 'A', '\0'};
1064 static const char stOB[] = {033, 'O', 'B', '\0'};
1065 static const char stOC[] = {033, 'O', 'C', '\0'};
1066 static const char stOD[] = {033, 'O', 'D', '\0'};
1067 static const char stOH[] = {033, 'O', 'H', '\0'};
1068 static const char stOF[] = {033, 'O', 'F', '\0'};
1070 key_add(el, strA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1071 key_add(el, strB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1072 key_add(el, strC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1073 key_add(el, strD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1074 key_add(el, strH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1075 key_add(el, strF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1076 key_add(el, str1, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1077 key_add(el, str4, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1078 key_add(el, stOA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1079 key_add(el, stOB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1080 key_add(el, stOC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1081 key_add(el, stOD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1082 key_add(el, stOH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1083 key_add(el, stOF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1085 if (el->el_map.type == MAP_VI) {
1086 key_add(el, &strA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1087 key_add(el, &strB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1088 key_add(el, &strC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1089 key_add(el, &strD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1090 key_add(el, &strH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1091 key_add(el, &strF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1092 key_add(el, &str1[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1093 key_add(el, &str4[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1094 key_add(el, &stOA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1095 key_add(el, &stOB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1096 key_add(el, &stOC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1097 key_add(el, &stOD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1098 key_add(el, &stOH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1099 key_add(el, &stOF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1104 /* term_set_arrow():
1105 * Set an arrow key binding
1108 term_set_arrow(EditLine *el, char *name, key_value_t *fun, int type)
1110 fkey_t *arrow = el->el_term.t_fkey;
1113 for (i = 0; i < A_K_NKEYS; i++)
1114 if (strcmp(name, arrow[i].name) == 0) {
1115 arrow[i].fun = *fun;
1116 arrow[i].type = type;
1123 /* term_clear_arrow():
1124 * Clear an arrow key binding
1127 term_clear_arrow(EditLine *el, char *name)
1129 fkey_t *arrow = el->el_term.t_fkey;
1132 for (i = 0; i < A_K_NKEYS; i++)
1133 if (strcmp(name, arrow[i].name) == 0) {
1134 arrow[i].type = XK_NOD;
1141 /* term_print_arrow():
1142 * Print the arrow key bindings
1145 term_print_arrow(EditLine *el, char *name)
1148 fkey_t *arrow = el->el_term.t_fkey;
1150 for (i = 0; i < A_K_NKEYS; i++)
1151 if (*name == '\0' || strcmp(name, arrow[i].name) == 0)
1152 if (arrow[i].type != XK_NOD)
1153 key_kprint(el, arrow[i].name, &arrow[i].fun,
1158 /* term_bind_arrow():
1159 * Bind the arrow keys
1162 term_bind_arrow(EditLine *el)
1165 const el_action_t *dmap;
1168 fkey_t *arrow = el->el_term.t_fkey;
1170 /* Check if the components needed are initialized */
1171 if (el->el_term.t_buf == NULL || el->el_map.key == NULL)
1174 map = el->el_map.type == MAP_VI ? el->el_map.alt : el->el_map.key;
1175 dmap = el->el_map.type == MAP_VI ? el->el_map.vic : el->el_map.emacs;
1177 term_reset_arrow(el);
1179 for (i = 0; i < A_K_NKEYS; i++) {
1180 p = el->el_term.t_str[arrow[i].key];
1182 j = (unsigned char) *p;
1184 * Assign the arrow keys only if:
1186 * 1. They are multi-character arrow keys and the user
1187 * has not re-assigned the leading character, or
1188 * has re-assigned the leading character to be
1189 * ED_SEQUENCE_LEAD_IN
1190 * 2. They are single arrow keys pointing to an
1193 if (arrow[i].type == XK_NOD)
1194 key_clear(el, map, p);
1196 if (p[1] && (dmap[j] == map[j] ||
1197 map[j] == ED_SEQUENCE_LEAD_IN)) {
1198 key_add(el, p, &arrow[i].fun,
1200 map[j] = ED_SEQUENCE_LEAD_IN;
1201 } else if (map[j] == ED_UNASSIGNED) {
1202 key_clear(el, map, p);
1203 if (arrow[i].type == XK_CMD)
1204 map[j] = arrow[i].fun.cmd;
1206 key_add(el, p, &arrow[i].fun,
1222 return (fputc(c, term_outfile));
1233 (void) fflush(term_outfile);
1238 * Print the current termcap characteristics
1242 term_telltc(EditLine *el, int argc, char **argv)
1244 const struct termcapstr *t;
1246 char upbuf[EL_BUFSIZ];
1248 (void) fprintf(el->el_outfile, "\n\tYour terminal has the\n");
1249 (void) fprintf(el->el_outfile, "\tfollowing characteristics:\n\n");
1250 (void) fprintf(el->el_outfile, "\tIt has %d columns and %d lines\n",
1251 Val(T_co), Val(T_li));
1252 (void) fprintf(el->el_outfile,
1253 "\tIt has %s meta key\n", EL_HAS_META ? "a" : "no");
1254 (void) fprintf(el->el_outfile,
1255 "\tIt can%suse tabs\n", EL_CAN_TAB ? " " : "not ");
1256 (void) fprintf(el->el_outfile, "\tIt %s automatic margins\n",
1257 EL_HAS_AUTO_MARGINS ? "has" : "does not have");
1258 if (EL_HAS_AUTO_MARGINS)
1259 (void) fprintf(el->el_outfile, "\tIt %s magic margins\n",
1260 EL_HAS_MAGIC_MARGINS ? "has" : "does not have");
1262 for (t = tstr, ts = el->el_term.t_str; t->name != NULL; t++, ts++)
1263 (void) fprintf(el->el_outfile, "\t%25s (%s) == %s\n",
1265 t->name, *ts && **ts ?
1266 key__decode_str(*ts, upbuf, "") : "(empty)");
1267 (void) fputc('\n', el->el_outfile);
1273 * Change the current terminal characteristics
1277 term_settc(EditLine *el, int argc, char **argv)
1279 const struct termcapstr *ts;
1280 const struct termcapval *tv;
1283 if (argv == NULL || argv[1] == NULL || argv[2] == NULL)
1290 * Do the strings first
1292 for (ts = tstr; ts->name != NULL; ts++)
1293 if (strcmp(ts->name, what) == 0)
1296 if (ts->name != NULL) {
1297 term_alloc(el, ts, how);
1302 * Do the numeric ones second
1304 for (tv = tval; tv->name != NULL; tv++)
1305 if (strcmp(tv->name, what) == 0)
1308 if (tv->name != NULL) {
1309 if (tv == &tval[T_pt] || tv == &tval[T_km] ||
1310 tv == &tval[T_am] || tv == &tval[T_xn]) {
1311 if (strcmp(how, "yes") == 0)
1312 el->el_term.t_val[tv - tval] = 1;
1313 else if (strcmp(how, "no") == 0)
1314 el->el_term.t_val[tv - tval] = 0;
1316 (void) fprintf(el->el_errfile,
1317 "settc: Bad value `%s'.\n", how);
1321 if (term_change_size(el, Val(T_li), Val(T_co)) == -1)
1328 i = strtol(how, &ep, 10);
1330 (void) fprintf(el->el_errfile,
1331 "settc: Bad value `%s'.\n", how);
1334 el->el_term.t_val[tv - tval] = (int) i;
1335 el->el_term.t_size.v = Val(T_co);
1336 el->el_term.t_size.h = Val(T_li);
1337 if (tv == &tval[T_co] || tv == &tval[T_li])
1338 if (term_change_size(el, Val(T_li), Val(T_co))
1349 * Print the termcap string out with variable substitution
1353 term_echotc(EditLine *el, int argc, char **argv)
1355 char *cap, *scap, *ep;
1356 int arg_need, arg_cols, arg_rows;
1357 int verbose = 0, silent = 0;
1359 static const char fmts[] = "%s\n", fmtd[] = "%d\n";
1360 const struct termcapstr *t;
1361 char buf[TC_BUFSIZE];
1366 if (argv == NULL || argv[1] == NULL)
1370 if (argv[0][0] == '-') {
1371 switch (argv[0][1]) {
1379 /* stderror(ERR_NAME | ERR_TCUSAGE); */
1384 if (!*argv || *argv[0] == '\0')
1386 if (strcmp(*argv, "tabs") == 0) {
1387 (void) fprintf(el->el_outfile, fmts, EL_CAN_TAB ? "yes" : "no");
1389 } else if (strcmp(*argv, "meta") == 0) {
1390 (void) fprintf(el->el_outfile, fmts, Val(T_km) ? "yes" : "no");
1392 } else if (strcmp(*argv, "xn") == 0) {
1393 (void) fprintf(el->el_outfile, fmts, EL_HAS_MAGIC_MARGINS ?
1396 } else if (strcmp(*argv, "am") == 0) {
1397 (void) fprintf(el->el_outfile, fmts, EL_HAS_AUTO_MARGINS ?
1400 } else if (strcmp(*argv, "baud") == 0) {
1404 for (i = 0; baud_rate[i].b_name != NULL; i++)
1405 if (el->el_tty.t_speed == baud_rate[i].b_rate) {
1406 (void) fprintf(el->el_outfile, fmts,
1407 baud_rate[i].b_name);
1410 (void) fprintf(el->el_outfile, fmtd, 0);
1412 (void) fprintf(el->el_outfile, fmtd, el->el_tty.t_speed);
1415 } else if (strcmp(*argv, "rows") == 0 || strcmp(*argv, "lines") == 0) {
1416 (void) fprintf(el->el_outfile, fmtd, Val(T_li));
1418 } else if (strcmp(*argv, "cols") == 0) {
1419 (void) fprintf(el->el_outfile, fmtd, Val(T_co));
1423 * Try to use our local definition first
1426 for (t = tstr; t->name != NULL; t++)
1427 if (strcmp(t->name, *argv) == 0) {
1428 scap = el->el_term.t_str[t - tstr];
1431 if (t->name == NULL)
1432 scap = tgetstr(*argv, &area);
1433 if (!scap || scap[0] == '\0') {
1435 (void) fprintf(el->el_errfile,
1436 "echotc: Termcap parameter `%s' not found.\n",
1441 * Count home many values we need for this capability.
1443 for (cap = scap, arg_need = 0; *cap; cap++)
1463 * hpux has lot's of them...
1466 (void) fprintf(el->el_errfile,
1467 "echotc: Warning: unknown termcap %% `%c'.\n",
1469 /* This is bad, but I won't complain */
1476 if (*argv && *argv[0]) {
1478 (void) fprintf(el->el_errfile,
1479 "echotc: Warning: Extra argument `%s'.\n",
1483 (void) tputs(scap, 1, term__putc);
1487 if (!*argv || *argv[0] == '\0') {
1489 (void) fprintf(el->el_errfile,
1490 "echotc: Warning: Missing argument.\n");
1494 i = strtol(*argv, &ep, 10);
1495 if (*ep != '\0' || i < 0) {
1497 (void) fprintf(el->el_errfile,
1498 "echotc: Bad value `%s' for rows.\n",
1504 if (*argv && *argv[0]) {
1506 (void) fprintf(el->el_errfile,
1507 "echotc: Warning: Extra argument `%s'.\n",
1511 (void) tputs(tgoto(scap, arg_cols, arg_rows), 1, term__putc);
1514 /* This is wrong, but I will ignore it... */
1516 (void) fprintf(el->el_errfile,
1517 "echotc: Warning: Too many required arguments (%d).\n",
1522 if (!*argv || *argv[0] == '\0') {
1524 (void) fprintf(el->el_errfile,
1525 "echotc: Warning: Missing argument.\n");
1528 i = strtol(*argv, &ep, 10);
1529 if (*ep != '\0' || i < 0) {
1531 (void) fprintf(el->el_errfile,
1532 "echotc: Bad value `%s' for cols.\n",
1538 if (!*argv || *argv[0] == '\0') {
1540 (void) fprintf(el->el_errfile,
1541 "echotc: Warning: Missing argument.\n");
1544 i = strtol(*argv, &ep, 10);
1545 if (*ep != '\0' || i < 0) {
1547 (void) fprintf(el->el_errfile,
1548 "echotc: Bad value `%s' for rows.\n",
1555 (void) fprintf(el->el_errfile,
1556 "echotc: Bad value `%s'.\n", *argv);
1560 if (*argv && *argv[0]) {
1562 (void) fprintf(el->el_errfile,
1563 "echotc: Warning: Extra argument `%s'.\n",
1567 (void) tputs(tgoto(scap, arg_cols, arg_rows), arg_rows,