1
/* $NetBSD: term.c,v 1.41 2005/08/08 14:05:37 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. Neither the name of the University nor the names of its contributors
19
* may be used to endorse or promote products derived from this software
20
* without specific prior written permission.
22
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36
#if !defined(lint) && !defined(SCCSID)
38
static char sccsid[] = "@(#)term.c 8.2 (Berkeley) 4/30/95";
40
__RCSID("$NetBSD: term.c,v 1.41 2005/08/08 14:05:37 christos Exp $");
42
#endif /* not lint && not SCCSID */
45
* term.c: Editor/termcap-curses interface
46
* We have to declare a static variable here, since the
47
* termcap putchar routine does not take an argument!
61
/* Solaris's term.h does horrid things. */
62
#if (defined(HAVE_TERM_H) && !defined(_SUNOS))
66
#include <sys/types.h>
67
#include <sys/ioctl.h>
72
* IMPORTANT NOTE: these routines are allowed to look at the current screen
73
* and the current possition assuming that it is correct. If this is not
74
* true, then the update will be WRONG! This is (should be) a valid
78
#define TC_BUFSIZE 2048
80
#define GoodStr(a) (el->el_term.t_str[a] != NULL && \
81
el->el_term.t_str[a][0] != '\0')
82
#define Str(a) el->el_term.t_str[a]
83
#define Val(a) el->el_term.t_val[a]
86
private const struct {
157
private const struct termcapstr {
159
const char *long_name;
162
{ "al", "add new blank line" },
164
{ "bl", "audible bell" },
166
{ "cd", "clear to bottom" },
168
{ "ce", "clear to end of line" },
170
{ "ch", "cursor to horiz pos" },
172
{ "cl", "clear screen" },
174
{ "dc", "delete a character" },
176
{ "dl", "delete a line" },
178
{ "dm", "start delete mode" },
180
{ "ed", "end delete mode" },
182
{ "ei", "end insert mode" },
184
{ "fs", "cursor from status line" },
186
{ "ho", "home cursor" },
188
{ "ic", "insert character" },
190
{ "im", "start insert mode" },
192
{ "ip", "insert padding" },
194
{ "kd", "sends cursor down" },
196
{ "kl", "sends cursor left" },
198
{ "kr", "sends cursor right" },
200
{ "ku", "sends cursor up" },
202
{ "md", "begin bold" },
204
{ "me", "end attributes" },
206
{ "nd", "non destructive space" },
208
{ "se", "end standout" },
210
{ "so", "begin standout" },
212
{ "ts", "cursor to status line" },
214
{ "up", "cursor up one" },
216
{ "us", "begin underline" },
218
{ "ue", "end underline" },
220
{ "vb", "visible bell" },
222
{ "DC", "delete multiple chars" },
224
{ "DO", "cursor down multiple" },
226
{ "IC", "insert multiple chars" },
228
{ "LE", "cursor left multiple" },
230
{ "RI", "cursor right multiple" },
232
{ "UP", "cursor up multiple" },
234
{ "kh", "send cursor home" },
236
{ "@7", "send cursor end" },
241
private const struct termcapval {
243
const char *long_name;
246
{ "am", "has automatic margins" },
248
{ "pt", "has physical tabs" },
250
{ "li", "Number of lines" },
252
{ "co", "Number of columns" },
254
{ "km", "Has meta key" },
256
{ "xt", "Tab chars destructive" },
258
{ "xn", "newline ignored at right margin" },
260
{ "MT", "Has meta key" }, /* XXX? */
264
/* do two or more of the attributes use me */
266
private void term_setflags(EditLine *);
267
private int term_rebuffer_display(EditLine *);
268
private void term_free_display(EditLine *);
269
private int term_alloc_display(EditLine *);
270
private void term_alloc(EditLine *, const struct termcapstr *, const char *);
271
private void term_init_arrow(EditLine *);
272
private void term_reset_arrow(EditLine *);
275
private FILE *term_outfile = NULL; /* XXX: How do we fix that? */
279
* Set the terminal capability flags
282
term_setflags(EditLine *el)
285
if (el->el_tty.t_tabs)
286
EL_FLAGS |= (Val(T_pt) && !Val(T_xt)) ? TERM_CAN_TAB : 0;
288
EL_FLAGS |= (Val(T_km) || Val(T_MT)) ? TERM_HAS_META : 0;
289
EL_FLAGS |= GoodStr(T_ce) ? TERM_CAN_CEOL : 0;
290
EL_FLAGS |= (GoodStr(T_dc) || GoodStr(T_DC)) ? TERM_CAN_DELETE : 0;
291
EL_FLAGS |= (GoodStr(T_im) || GoodStr(T_ic) || GoodStr(T_IC)) ?
293
EL_FLAGS |= (GoodStr(T_up) || GoodStr(T_UP)) ? TERM_CAN_UP : 0;
294
EL_FLAGS |= Val(T_am) ? TERM_HAS_AUTO_MARGINS : 0;
295
EL_FLAGS |= Val(T_xn) ? TERM_HAS_MAGIC_MARGINS : 0;
297
if (GoodStr(T_me) && GoodStr(T_ue))
298
EL_FLAGS |= (strcmp(Str(T_me), Str(T_ue)) == 0) ?
301
EL_FLAGS &= ~TERM_CAN_ME;
302
if (GoodStr(T_me) && GoodStr(T_se))
303
EL_FLAGS |= (strcmp(Str(T_me), Str(T_se)) == 0) ?
309
(void) fprintf(el->el_errfile,
310
"WARNING: Your terminal cannot move up.\n");
311
(void) fprintf(el->el_errfile,
312
"Editing may be odd for long lines.\n");
315
(void) fprintf(el->el_errfile, "no clear EOL capability.\n");
317
(void) fprintf(el->el_errfile, "no delete char capability.\n");
319
(void) fprintf(el->el_errfile, "no insert char capability.\n");
320
#endif /* DEBUG_SCREEN */
325
* Initialize the terminal stuff
328
term_init(EditLine *el)
331
el->el_term.t_buf = (char *) el_malloc(TC_BUFSIZE);
332
if (el->el_term.t_buf == NULL)
334
el->el_term.t_cap = (char *) el_malloc(TC_BUFSIZE);
335
if (el->el_term.t_cap == NULL)
337
el->el_term.t_fkey = (fkey_t *) el_malloc(A_K_NKEYS * sizeof(fkey_t));
338
if (el->el_term.t_fkey == NULL)
340
el->el_term.t_loc = 0;
341
el->el_term.t_str = (char **) el_malloc(T_str * sizeof(char *));
342
if (el->el_term.t_str == NULL)
344
(void) memset(el->el_term.t_str, 0, T_str * sizeof(char *));
345
el->el_term.t_val = (int *) el_malloc(T_val * sizeof(int));
346
if (el->el_term.t_val == NULL)
348
(void) memset(el->el_term.t_val, 0, T_val * sizeof(int));
349
term_outfile = el->el_outfile;
350
(void) term_set(el, NULL);
356
* Clean up the terminal stuff
359
term_end(EditLine *el)
362
el_free((ptr_t) el->el_term.t_buf);
363
el->el_term.t_buf = NULL;
364
el_free((ptr_t) el->el_term.t_cap);
365
el->el_term.t_cap = NULL;
366
el->el_term.t_loc = 0;
367
el_free((ptr_t) el->el_term.t_str);
368
el->el_term.t_str = NULL;
369
el_free((ptr_t) el->el_term.t_val);
370
el->el_term.t_val = NULL;
371
el_free((ptr_t) el->el_term.t_fkey);
372
el->el_term.t_fkey = NULL;
373
term_free_display(el);
378
* Maintain a string pool for termcap strings
381
term_alloc(EditLine *el, const struct termcapstr *t, const char *cap)
383
char termbuf[TC_BUFSIZE];
385
char **tlist = el->el_term.t_str;
386
char **tmp, **str = &tlist[t - tstr];
388
if (cap == NULL || *cap == '\0') {
394
tlen = *str == NULL ? 0 : strlen(*str);
397
* New string is shorter; no need to allocate space
400
(void) strcpy(*str, cap); /* XXX strcpy is safe */
404
* New string is longer; see if we have enough space to append
406
if (el->el_term.t_loc + 3 < TC_BUFSIZE) {
407
/* XXX strcpy is safe */
408
(void) strcpy(*str = &el->el_term.t_buf[el->el_term.t_loc],
410
el->el_term.t_loc += clen + 1; /* one for \0 */
414
* Compact our buffer; no need to check compaction, cause we know it
418
for (tmp = tlist; tmp < &tlist[T_str]; tmp++)
419
if (*tmp != NULL && *tmp != '\0' && *tmp != *str) {
422
for (ptr = *tmp; *ptr != '\0'; termbuf[tlen++] = *ptr++)
424
termbuf[tlen++] = '\0';
426
memcpy(el->el_term.t_buf, termbuf, TC_BUFSIZE);
427
el->el_term.t_loc = tlen;
428
if (el->el_term.t_loc + 3 >= TC_BUFSIZE) {
429
(void) fprintf(el->el_errfile,
430
"Out of termcap string space.\n");
433
/* XXX strcpy is safe */
434
(void) strcpy(*str = &el->el_term.t_buf[el->el_term.t_loc], cap);
435
el->el_term.t_loc += clen + 1; /* one for \0 */
440
/* term_rebuffer_display():
441
* Rebuffer the display after the screen changed size
444
term_rebuffer_display(EditLine *el)
446
coord_t *c = &el->el_term.t_size;
448
term_free_display(el);
453
if (term_alloc_display(el) == -1)
459
/* term_alloc_display():
460
* Allocate a new display.
463
term_alloc_display(EditLine *el)
467
coord_t *c = &el->el_term.t_size;
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)));
480
b = (char **) el_malloc((size_t) (sizeof(char *) * (c->v + 1)));
483
for (i = 0; i < c->v; i++) {
484
b[i] = (char *) el_malloc((size_t) (sizeof(char) * (c->h + 1)));
494
/* term_free_display():
495
* Free the display buffers
498
term_free_display(EditLine *el)
504
el->el_display = NULL;
506
for (bufp = b; *bufp != NULL; bufp++)
507
el_free((ptr_t) * bufp);
511
el->el_vdisplay = NULL;
513
for (bufp = b; *bufp != NULL; bufp++)
514
el_free((ptr_t) * bufp);
520
/* term_move_to_line():
521
* move to line <where> (first line == 0)
522
* as efficiently as possible
525
term_move_to_line(EditLine *el, int where)
529
if (where == el->el_cursor.v)
532
if (where > el->el_term.t_size.v) {
534
(void) fprintf(el->el_errfile,
535
"term_move_to_line: where is ridiculous: %d\r\n", where);
536
#endif /* DEBUG_SCREEN */
539
if ((del = where - el->el_cursor.v) > 0) {
541
if (EL_HAS_AUTO_MARGINS &&
542
el->el_display[el->el_cursor.v][0] != '\0') {
543
/* move without newline */
544
term_move_to_char(el, el->el_term.t_size.h - 1);
546
&el->el_display[el->el_cursor.v][el->el_cursor.h],
551
if ((del > 1) && GoodStr(T_DO)) {
552
(void) tputs(tgoto(Str(T_DO), del, del),
556
for (; del > 0; del--)
558
/* because the \n will become \r\n */
563
} else { /* del < 0 */
564
if (GoodStr(T_UP) && (-del > 1 || !GoodStr(T_up)))
565
(void) tputs(tgoto(Str(T_UP), -del, -del), -del,
569
for (; del < 0; del++)
570
(void) tputs(Str(T_up), 1, term__putc);
573
el->el_cursor.v = where;/* now where is here */
577
/* term_move_to_char():
578
* Move to the character position specified
581
term_move_to_char(EditLine *el, int where)
586
if (where == el->el_cursor.h)
589
if (where > el->el_term.t_size.h) {
591
(void) fprintf(el->el_errfile,
592
"term_move_to_char: where is riduculous: %d\r\n", where);
593
#endif /* DEBUG_SCREEN */
596
if (!where) { /* if where is first column */
597
term__putc('\r'); /* do a CR */
601
del = where - el->el_cursor.h;
603
if ((del < -4 || del > 4) && GoodStr(T_ch))
604
/* go there directly */
605
(void) tputs(tgoto(Str(T_ch), where, where), where, term__putc);
607
if (del > 0) { /* moving forward */
608
if ((del > 4) && GoodStr(T_RI))
609
(void) tputs(tgoto(Str(T_RI), del, del),
612
/* if I can do tabs, use them */
614
if ((el->el_cursor.h & 0370) !=
616
/* if not within tab stop */
618
(el->el_cursor.h & 0370);
623
el->el_cursor.h = where & 0370;
627
* it's usually cheaper to just write the
631
* NOTE THAT term_overwrite() WILL CHANGE
635
&el->el_display[el->el_cursor.v][el->el_cursor.h],
636
where - el->el_cursor.h);
639
} else { /* del < 0 := moving backward */
640
if ((-del > 4) && GoodStr(T_LE))
641
(void) tputs(tgoto(Str(T_LE), -del, -del),
643
else { /* can't go directly there */
645
* if the "cost" is greater than the "cost"
649
((unsigned int)-del >
650
(((unsigned int) where >> 3) +
653
term__putc('\r'); /* do a CR */
655
goto mc_again; /* and try again */
657
for (i = 0; i < -del; i++)
662
el->el_cursor.h = where; /* now where is here */
667
* Overstrike num characters
670
term_overwrite(EditLine *el, const char *cp, int n)
673
return; /* catch bugs */
675
if (n > el->el_term.t_size.h) {
677
(void) fprintf(el->el_errfile,
678
"term_overwrite: n is riduculous: %d\r\n", n);
679
#endif /* DEBUG_SCREEN */
687
if (el->el_cursor.h >= el->el_term.t_size.h) { /* wrap? */
688
if (EL_HAS_AUTO_MARGINS) { /* yes */
691
if (EL_HAS_MAGIC_MARGINS) {
692
/* force the wrap to avoid the "magic"
695
if ((c = el->el_display[el->el_cursor.v][el->el_cursor.h])
697
term_overwrite(el, &c, 1);
702
} else /* no wrap, but cursor stays on screen */
703
el->el_cursor.h = el->el_term.t_size.h;
708
/* term_deletechars():
709
* Delete num characters
712
term_deletechars(EditLine *el, int num)
717
if (!EL_CAN_DELETE) {
719
(void) fprintf(el->el_errfile, " ERROR: cannot delete \n");
720
#endif /* DEBUG_EDIT */
723
if (num > el->el_term.t_size.h) {
725
(void) fprintf(el->el_errfile,
726
"term_deletechars: num is riduculous: %d\r\n", num);
727
#endif /* DEBUG_SCREEN */
730
if (GoodStr(T_DC)) /* if I have multiple delete */
731
if ((num > 1) || !GoodStr(T_dc)) { /* if dc would be more
733
(void) tputs(tgoto(Str(T_DC), num, num),
737
if (GoodStr(T_dm)) /* if I have delete mode */
738
(void) tputs(Str(T_dm), 1, term__putc);
740
if (GoodStr(T_dc)) /* else do one at a time */
742
(void) tputs(Str(T_dc), 1, term__putc);
744
if (GoodStr(T_ed)) /* if I have delete mode */
745
(void) tputs(Str(T_ed), 1, term__putc);
749
/* term_insertwrite():
750
* Puts terminal in insert character mode or inserts num
751
* characters in the line
754
term_insertwrite(EditLine *el, char *cp, int num)
758
if (!EL_CAN_INSERT) {
760
(void) fprintf(el->el_errfile, " ERROR: cannot insert \n");
761
#endif /* DEBUG_EDIT */
764
if (num > el->el_term.t_size.h) {
766
(void) fprintf(el->el_errfile,
767
"StartInsert: num is riduculous: %d\r\n", num);
768
#endif /* DEBUG_SCREEN */
771
if (GoodStr(T_IC)) /* if I have multiple insert */
772
if ((num > 1) || !GoodStr(T_ic)) {
773
/* if ic would be more expensive */
774
(void) tputs(tgoto(Str(T_IC), num, num),
776
term_overwrite(el, cp, num);
777
/* this updates el_cursor.h */
780
if (GoodStr(T_im) && GoodStr(T_ei)) { /* if I have insert mode */
781
(void) tputs(Str(T_im), 1, term__putc);
783
el->el_cursor.h += num;
788
if (GoodStr(T_ip)) /* have to make num chars insert */
789
(void) tputs(Str(T_ip), 1, term__putc);
791
(void) tputs(Str(T_ei), 1, term__putc);
795
if (GoodStr(T_ic)) /* have to make num chars insert */
796
(void) tputs(Str(T_ic), 1, term__putc);
803
if (GoodStr(T_ip)) /* have to make num chars insert */
804
(void) tputs(Str(T_ip), 1, term__putc);
805
/* pad the inserted char */
812
* clear to end of line. There are num characters to clear
815
term_clear_EOL(EditLine *el, int num)
819
if (EL_CAN_CEOL && GoodStr(T_ce))
820
(void) tputs(Str(T_ce), 1, term__putc);
822
for (i = 0; i < num; i++)
824
el->el_cursor.h += num; /* have written num spaces */
829
/* term_clear_screen():
833
term_clear_screen(EditLine *el)
834
{ /* clear the whole screen and home */
837
/* send the clear screen code */
838
(void) tputs(Str(T_cl), Val(T_li), term__putc);
839
else if (GoodStr(T_ho) && GoodStr(T_cd)) {
840
(void) tputs(Str(T_ho), Val(T_li), term__putc); /* home */
841
/* clear to bottom of screen */
842
(void) tputs(Str(T_cd), Val(T_li), term__putc);
851
* Beep the way the terminal wants us
854
term_beep(EditLine *el)
857
/* what termcap says we should use */
858
(void) tputs(Str(T_bl), 1, term__putc);
860
term__putc('\007'); /* an ASCII bell; ^G */
865
/* term_clear_to_bottom():
866
* Clear to the bottom of the screen
869
term_clear_to_bottom(EditLine *el)
872
(void) tputs(Str(T_cd), Val(T_li), term__putc);
873
else if (GoodStr(T_ce))
874
(void) tputs(Str(T_ce), Val(T_li), term__putc);
879
term_get(EditLine *el, const char **term)
881
*term = el->el_term.t_name;
886
* Read in the terminal capabilities from the requested terminal
889
term_set(EditLine *el, const char *term)
892
char buf[TC_BUFSIZE];
894
const struct termcapstr *t;
898
(void) sigemptyset(&nset);
899
(void) sigaddset(&nset, SIGWINCH);
900
(void) sigprocmask(SIG_BLOCK, &nset, &oset);
906
term = getenv("TERM");
908
if (!term || !term[0])
911
if (strcmp(term, "emacs") == 0)
912
el->el_flags |= EDIT_DISABLED;
914
memset(el->el_term.t_cap, 0, TC_BUFSIZE);
916
i = tgetent(el->el_term.t_cap, term);
920
(void) fprintf(el->el_errfile,
921
"Cannot read termcap database;\n");
923
(void) fprintf(el->el_errfile,
924
"No entry for terminal type \"%s\";\n", term);
925
(void) fprintf(el->el_errfile,
926
"using dumb terminal settings.\n");
927
Val(T_co) = 80; /* do a dumb terminal */
928
Val(T_pt) = Val(T_km) = Val(T_li) = 0;
929
Val(T_xt) = Val(T_MT);
930
for (t = tstr; t->name != NULL; t++)
931
term_alloc(el, t, NULL);
933
/* auto/magic margins */
934
Val(T_am) = tgetflag("am");
935
Val(T_xn) = tgetflag("xn");
937
Val(T_pt) = tgetflag("pt");
938
Val(T_xt) = tgetflag("xt");
939
/* do we have a meta? */
940
Val(T_km) = tgetflag("km");
941
Val(T_MT) = tgetflag("MT");
943
Val(T_co) = tgetnum("co");
944
Val(T_li) = tgetnum("li");
945
for (t = tstr; t->name != NULL; t++) {
946
/* XXX: some systems' tgetstr needs non const */
947
term_alloc(el, t, tgetstr(strchr(t->name, *t->name),
953
Val(T_co) = 80; /* just in case */
957
el->el_term.t_size.v = Val(T_co);
958
el->el_term.t_size.h = Val(T_li);
962
/* get the correct window size */
963
(void) term_get_size(el, &lins, &cols);
964
if (term_change_size(el, lins, cols) == -1)
966
(void) sigprocmask(SIG_SETMASK, &oset, NULL);
968
el->el_term.t_name = term;
969
return (i <= 0 ? -1 : 0);
974
* Return the new window size in lines and cols, and
975
* true if the size was changed.
978
term_get_size(EditLine *el, int *lins, int *cols)
987
if (ioctl(el->el_infd, TIOCGWINSZ, (ioctl_t) & ws) != -1) {
998
if (ioctl(el->el_infd, TIOCGSIZE, (ioctl_t) & ts) != -1) {
1002
*lins = ts.ts_lines;
1006
return (Val(T_co) != *cols || Val(T_li) != *lins);
1010
/* term_change_size():
1011
* Change the size of the terminal
1014
term_change_size(EditLine *el, int lins, int cols)
1019
Val(T_co) = (cols < 2) ? 80 : cols;
1020
Val(T_li) = (lins < 1) ? 24 : lins;
1022
/* re-make display buffers */
1023
if (term_rebuffer_display(el) == -1)
1025
re_clear_display(el);
1030
/* term_init_arrow():
1031
* Initialize the arrow key bindings from termcap
1034
term_init_arrow(EditLine *el)
1036
fkey_t *arrow = el->el_term.t_fkey;
1038
arrow[A_K_DN].name = "down";
1039
arrow[A_K_DN].key = T_kd;
1040
arrow[A_K_DN].fun.cmd = ED_NEXT_HISTORY;
1041
arrow[A_K_DN].type = XK_CMD;
1043
arrow[A_K_UP].name = "up";
1044
arrow[A_K_UP].key = T_ku;
1045
arrow[A_K_UP].fun.cmd = ED_PREV_HISTORY;
1046
arrow[A_K_UP].type = XK_CMD;
1048
arrow[A_K_LT].name = "left";
1049
arrow[A_K_LT].key = T_kl;
1050
arrow[A_K_LT].fun.cmd = ED_PREV_CHAR;
1051
arrow[A_K_LT].type = XK_CMD;
1053
arrow[A_K_RT].name = "right";
1054
arrow[A_K_RT].key = T_kr;
1055
arrow[A_K_RT].fun.cmd = ED_NEXT_CHAR;
1056
arrow[A_K_RT].type = XK_CMD;
1058
arrow[A_K_HO].name = "home";
1059
arrow[A_K_HO].key = T_kh;
1060
arrow[A_K_HO].fun.cmd = ED_MOVE_TO_BEG;
1061
arrow[A_K_HO].type = XK_CMD;
1063
arrow[A_K_EN].name = "end";
1064
arrow[A_K_EN].key = T_at7;
1065
arrow[A_K_EN].fun.cmd = ED_MOVE_TO_END;
1066
arrow[A_K_EN].type = XK_CMD;
1070
/* term_reset_arrow():
1071
* Reset arrow key bindings
1074
term_reset_arrow(EditLine *el)
1076
fkey_t *arrow = el->el_term.t_fkey;
1077
static const char strA[] = {033, '[', 'A', '\0'};
1078
static const char strB[] = {033, '[', 'B', '\0'};
1079
static const char strC[] = {033, '[', 'C', '\0'};
1080
static const char strD[] = {033, '[', 'D', '\0'};
1081
static const char strH[] = {033, '[', 'H', '\0'};
1082
static const char strF[] = {033, '[', 'F', '\0'};
1083
static const char stOA[] = {033, 'O', 'A', '\0'};
1084
static const char stOB[] = {033, 'O', 'B', '\0'};
1085
static const char stOC[] = {033, 'O', 'C', '\0'};
1086
static const char stOD[] = {033, 'O', 'D', '\0'};
1087
static const char stOH[] = {033, 'O', 'H', '\0'};
1088
static const char stOF[] = {033, 'O', 'F', '\0'};
1090
key_add(el, strA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1091
key_add(el, strB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1092
key_add(el, strC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1093
key_add(el, strD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1094
key_add(el, strH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1095
key_add(el, strF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1096
key_add(el, stOA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1097
key_add(el, stOB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1098
key_add(el, stOC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1099
key_add(el, stOD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1100
key_add(el, stOH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1101
key_add(el, stOF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1103
if (el->el_map.type == MAP_VI) {
1104
key_add(el, &strA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1105
key_add(el, &strB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1106
key_add(el, &strC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1107
key_add(el, &strD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1108
key_add(el, &strH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1109
key_add(el, &strF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1110
key_add(el, &stOA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1111
key_add(el, &stOB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1112
key_add(el, &stOC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1113
key_add(el, &stOD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1114
key_add(el, &stOH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1115
key_add(el, &stOF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1120
/* term_set_arrow():
1121
* Set an arrow key binding
1124
term_set_arrow(EditLine *el, const char *name, key_value_t *fun, int type)
1126
fkey_t *arrow = el->el_term.t_fkey;
1129
for (i = 0; i < A_K_NKEYS; i++)
1130
if (strcmp(name, arrow[i].name) == 0) {
1131
arrow[i].fun = *fun;
1132
arrow[i].type = type;
1139
/* term_clear_arrow():
1140
* Clear an arrow key binding
1143
term_clear_arrow(EditLine *el, const char *name)
1145
fkey_t *arrow = el->el_term.t_fkey;
1148
for (i = 0; i < A_K_NKEYS; i++)
1149
if (strcmp(name, arrow[i].name) == 0) {
1150
arrow[i].type = XK_NOD;
1157
/* term_print_arrow():
1158
* Print the arrow key bindings
1161
term_print_arrow(EditLine *el, const char *name)
1164
fkey_t *arrow = el->el_term.t_fkey;
1166
for (i = 0; i < A_K_NKEYS; i++)
1167
if (*name == '\0' || strcmp(name, arrow[i].name) == 0)
1168
if (arrow[i].type != XK_NOD)
1169
key_kprint(el, arrow[i].name, &arrow[i].fun,
1174
/* term_bind_arrow():
1175
* Bind the arrow keys
1178
term_bind_arrow(EditLine *el)
1181
const el_action_t *dmap;
1184
fkey_t *arrow = el->el_term.t_fkey;
1186
/* Check if the components needed are initialized */
1187
if (el->el_term.t_buf == NULL || el->el_map.key == NULL)
1190
map = el->el_map.type == MAP_VI ? el->el_map.alt : el->el_map.key;
1191
dmap = el->el_map.type == MAP_VI ? el->el_map.vic : el->el_map.emacs;
1193
term_reset_arrow(el);
1195
for (i = 0; i < A_K_NKEYS; i++) {
1196
p = el->el_term.t_str[arrow[i].key];
1198
j = (unsigned char) *p;
1200
* Assign the arrow keys only if:
1202
* 1. They are multi-character arrow keys and the user
1203
* has not re-assigned the leading character, or
1204
* has re-assigned the leading character to be
1205
* ED_SEQUENCE_LEAD_IN
1206
* 2. They are single arrow keys pointing to an
1209
if (arrow[i].type == XK_NOD)
1210
key_clear(el, map, p);
1212
if (p[1] && (dmap[j] == map[j] ||
1213
map[j] == ED_SEQUENCE_LEAD_IN)) {
1214
key_add(el, p, &arrow[i].fun,
1216
map[j] = ED_SEQUENCE_LEAD_IN;
1217
} else if (map[j] == ED_UNASSIGNED) {
1218
key_clear(el, map, p);
1219
if (arrow[i].type == XK_CMD)
1220
map[j] = arrow[i].fun.cmd;
1222
key_add(el, p, &arrow[i].fun,
1238
return (fputc(c, term_outfile));
1249
(void) fflush(term_outfile);
1254
* Print the current termcap characteristics
1258
term_telltc(EditLine *el, int argc __attribute__((__unused__)),
1259
const char **argv __attribute__((__unused__)))
1261
const struct termcapstr *t;
1263
char upbuf[EL_BUFSIZ];
1265
(void) fprintf(el->el_outfile, "\n\tYour terminal has the\n");
1266
(void) fprintf(el->el_outfile, "\tfollowing characteristics:\n\n");
1267
(void) fprintf(el->el_outfile, "\tIt has %d columns and %d lines\n",
1268
Val(T_co), Val(T_li));
1269
(void) fprintf(el->el_outfile,
1270
"\tIt has %s meta key\n", EL_HAS_META ? "a" : "no");
1271
(void) fprintf(el->el_outfile,
1272
"\tIt can%suse tabs\n", EL_CAN_TAB ? " " : "not ");
1273
(void) fprintf(el->el_outfile, "\tIt %s automatic margins\n",
1274
EL_HAS_AUTO_MARGINS ? "has" : "does not have");
1275
if (EL_HAS_AUTO_MARGINS)
1276
(void) fprintf(el->el_outfile, "\tIt %s magic margins\n",
1277
EL_HAS_MAGIC_MARGINS ? "has" : "does not have");
1279
for (t = tstr, ts = el->el_term.t_str; t->name != NULL; t++, ts++)
1280
(void) fprintf(el->el_outfile, "\t%25s (%s) == %s\n",
1282
t->name, *ts && **ts ?
1283
key__decode_str(*ts, upbuf, "") : "(empty)");
1284
(void) fputc('\n', el->el_outfile);
1290
* Change the current terminal characteristics
1294
term_settc(EditLine *el, int argc __attribute__((__unused__)),
1297
const struct termcapstr *ts;
1298
const struct termcapval *tv;
1299
const char *what, *how;
1301
if (argv == NULL || argv[1] == NULL || argv[2] == NULL)
1308
* Do the strings first
1310
for (ts = tstr; ts->name != NULL; ts++)
1311
if (strcmp(ts->name, what) == 0)
1314
if (ts->name != NULL) {
1315
term_alloc(el, ts, how);
1320
* Do the numeric ones second
1322
for (tv = tval; tv->name != NULL; tv++)
1323
if (strcmp(tv->name, what) == 0)
1326
if (tv->name != NULL) {
1327
if (tv == &tval[T_pt] || tv == &tval[T_km] ||
1328
tv == &tval[T_am] || tv == &tval[T_xn]) {
1329
if (strcmp(how, "yes") == 0)
1330
el->el_term.t_val[tv - tval] = 1;
1331
else if (strcmp(how, "no") == 0)
1332
el->el_term.t_val[tv - tval] = 0;
1334
(void) fprintf(el->el_errfile,
1335
"settc: Bad value `%s'.\n", how);
1339
if (term_change_size(el, Val(T_li), Val(T_co)) == -1)
1346
i = strtol(how, &ep, 10);
1348
(void) fprintf(el->el_errfile,
1349
"settc: Bad value `%s'.\n", how);
1352
el->el_term.t_val[tv - tval] = (int) i;
1353
el->el_term.t_size.v = Val(T_co);
1354
el->el_term.t_size.h = Val(T_li);
1355
if (tv == &tval[T_co] || tv == &tval[T_li])
1356
if (term_change_size(el, Val(T_li), Val(T_co))
1367
* Print the termcap string out with variable substitution
1371
term_echotc(EditLine *el, int argc __attribute__((__unused__)),
1374
char *cap, *scap, *ep;
1375
int arg_need, arg_cols, arg_rows;
1376
int verbose = 0, silent = 0;
1378
static const char fmts[] = "%s\n", fmtd[] = "%d\n";
1379
const struct termcapstr *t;
1380
char buf[TC_BUFSIZE];
1385
if (argv == NULL || argv[1] == NULL)
1389
if (argv[0][0] == '-') {
1390
switch (argv[0][1]) {
1398
/* stderror(ERR_NAME | ERR_TCUSAGE); */
1403
if (!*argv || *argv[0] == '\0')
1405
if (strcmp(*argv, "tabs") == 0) {
1406
(void) fprintf(el->el_outfile, fmts, EL_CAN_TAB ? "yes" : "no");
1408
} else if (strcmp(*argv, "meta") == 0) {
1409
(void) fprintf(el->el_outfile, fmts, Val(T_km) ? "yes" : "no");
1411
} else if (strcmp(*argv, "xn") == 0) {
1412
(void) fprintf(el->el_outfile, fmts, EL_HAS_MAGIC_MARGINS ?
1415
} else if (strcmp(*argv, "am") == 0) {
1416
(void) fprintf(el->el_outfile, fmts, EL_HAS_AUTO_MARGINS ?
1419
} else if (strcmp(*argv, "baud") == 0) {
1423
for (i = 0; baud_rate[i].b_name != NULL; i++)
1424
if (el->el_tty.t_speed == baud_rate[i].b_rate) {
1425
(void) fprintf(el->el_outfile, fmts,
1426
baud_rate[i].b_name);
1429
(void) fprintf(el->el_outfile, fmtd, 0);
1431
(void) fprintf(el->el_outfile, fmtd, (int)el->el_tty.t_speed);
1434
} else if (strcmp(*argv, "rows") == 0 || strcmp(*argv, "lines") == 0) {
1435
(void) fprintf(el->el_outfile, fmtd, Val(T_li));
1437
} else if (strcmp(*argv, "cols") == 0) {
1438
(void) fprintf(el->el_outfile, fmtd, Val(T_co));
1442
* Try to use our local definition first
1445
for (t = tstr; t->name != NULL; t++)
1446
if (strcmp(t->name, *argv) == 0) {
1447
scap = el->el_term.t_str[t - tstr];
1450
if (t->name == NULL) {
1451
/* XXX: some systems' tgetstr needs non const */
1452
scap = tgetstr(strchr(*argv, **argv), &area);
1454
if (!scap || scap[0] == '\0') {
1456
(void) fprintf(el->el_errfile,
1457
"echotc: Termcap parameter `%s' not found.\n",
1462
* Count home many values we need for this capability.
1464
for (cap = scap, arg_need = 0; *cap; cap++)
1484
* hpux has lot's of them...
1487
(void) fprintf(el->el_errfile,
1488
"echotc: Warning: unknown termcap %% `%c'.\n",
1490
/* This is bad, but I won't complain */
1497
if (*argv && *argv[0]) {
1499
(void) fprintf(el->el_errfile,
1500
"echotc: Warning: Extra argument `%s'.\n",
1504
(void) tputs(scap, 1, term__putc);
1508
if (!*argv || *argv[0] == '\0') {
1510
(void) fprintf(el->el_errfile,
1511
"echotc: Warning: Missing argument.\n");
1515
i = strtol(*argv, &ep, 10);
1516
if (*ep != '\0' || i < 0) {
1518
(void) fprintf(el->el_errfile,
1519
"echotc: Bad value `%s' for rows.\n",
1525
if (*argv && *argv[0]) {
1527
(void) fprintf(el->el_errfile,
1528
"echotc: Warning: Extra argument `%s'.\n",
1532
(void) tputs(tgoto(scap, arg_cols, arg_rows), 1, term__putc);
1535
/* This is wrong, but I will ignore it... */
1537
(void) fprintf(el->el_errfile,
1538
"echotc: Warning: Too many required arguments (%d).\n",
1543
if (!*argv || *argv[0] == '\0') {
1545
(void) fprintf(el->el_errfile,
1546
"echotc: Warning: Missing argument.\n");
1549
i = strtol(*argv, &ep, 10);
1550
if (*ep != '\0' || i < 0) {
1552
(void) fprintf(el->el_errfile,
1553
"echotc: Bad value `%s' for cols.\n",
1559
if (!*argv || *argv[0] == '\0') {
1561
(void) fprintf(el->el_errfile,
1562
"echotc: Warning: Missing argument.\n");
1565
i = strtol(*argv, &ep, 10);
1566
if (*ep != '\0' || i < 0) {
1568
(void) fprintf(el->el_errfile,
1569
"echotc: Bad value `%s' for rows.\n",
1576
(void) fprintf(el->el_errfile,
1577
"echotc: Bad value `%s'.\n", *argv);
1581
if (*argv && *argv[0]) {
1583
(void) fprintf(el->el_errfile,
1584
"echotc: Warning: Extra argument `%s'.\n",
1588
(void) tputs(tgoto(scap, arg_cols, arg_rows), arg_rows,