1
/* $NetBSD: term.c,v 1.48 2009/02/06 20:08:13 sketch 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";
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!
53
#if 0 /* TODO: do we need this */
64
/* Don't use Solaris's term.h. */
65
#if (defined(HAVE_TERM_H) && !defined(__SunOS))
68
#include <sys/types.h>
69
#include <sys/ioctl.h>
78
* IMPORTANT NOTE: these routines are allowed to look at the current screen
79
* and the current possition assuming that it is correct. If this is not
80
* true, then the update will be WRONG! This is (should be) a valid
84
#define TC_BUFSIZE 2048
86
#define GoodStr(a) (el->el_term.t_str[a] != NULL && \
87
el->el_term.t_str[a][0] != '\0')
88
#define Str(a) el->el_term.t_str[a]
89
#define Val(a) el->el_term.t_val[a]
92
private const struct {
163
private const struct termcapstr {
165
const char *long_name;
168
{ "al", "add new blank line" },
170
{ "bl", "audible bell" },
172
{ "cd", "clear to bottom" },
174
{ "ce", "clear to end of line" },
176
{ "ch", "cursor to horiz pos" },
178
{ "cl", "clear screen" },
180
{ "dc", "delete a character" },
182
{ "dl", "delete a line" },
184
{ "dm", "start delete mode" },
186
{ "ed", "end delete mode" },
188
{ "ei", "end insert mode" },
190
{ "fs", "cursor from status line" },
192
{ "ho", "home cursor" },
194
{ "ic", "insert character" },
196
{ "im", "start insert mode" },
198
{ "ip", "insert padding" },
200
{ "kd", "sends cursor down" },
202
{ "kl", "sends cursor left" },
204
{ "kr", "sends cursor right" },
206
{ "ku", "sends cursor up" },
208
{ "md", "begin bold" },
210
{ "me", "end attributes" },
212
{ "nd", "non destructive space" },
214
{ "se", "end standout" },
216
{ "so", "begin standout" },
218
{ "ts", "cursor to status line" },
220
{ "up", "cursor up one" },
222
{ "us", "begin underline" },
224
{ "ue", "end underline" },
226
{ "vb", "visible bell" },
228
{ "DC", "delete multiple chars" },
230
{ "DO", "cursor down multiple" },
232
{ "IC", "insert multiple chars" },
234
{ "LE", "cursor left multiple" },
236
{ "RI", "cursor right multiple" },
238
{ "UP", "cursor up multiple" },
240
{ "kh", "send cursor home" },
242
{ "@7", "send cursor end" },
247
private const struct termcapval {
249
const char *long_name;
252
{ "am", "has automatic margins" },
254
{ "pt", "has physical tabs" },
256
{ "li", "Number of lines" },
258
{ "co", "Number of columns" },
260
{ "km", "Has meta key" },
262
{ "xt", "Tab chars destructive" },
264
{ "xn", "newline ignored at right margin" },
266
{ "MT", "Has meta key" }, /* XXX? */
270
/* do two or more of the attributes use me */
272
private void term_setflags(EditLine *);
273
private int term_rebuffer_display(EditLine *);
274
private void term_free_display(EditLine *);
275
private int term_alloc_display(EditLine *);
276
private void term_alloc(EditLine *, const struct termcapstr *, const char *);
277
private void term_init_arrow(EditLine *);
278
private void term_reset_arrow(EditLine *);
279
private int term_putc(int);
280
private void term_tputs(EditLine *, const char *, int);
283
private pthread_mutex_t term_mutex = PTHREAD_MUTEX_INITIALIZER;
285
private FILE *term_outfile = NULL;
289
* Set the terminal capability flags
292
term_setflags(EditLine *el)
295
if (el->el_tty.t_tabs)
296
EL_FLAGS |= (Val(T_pt) && !Val(T_xt)) ? TERM_CAN_TAB : 0;
298
EL_FLAGS |= (Val(T_km) || Val(T_MT)) ? TERM_HAS_META : 0;
299
EL_FLAGS |= GoodStr(T_ce) ? TERM_CAN_CEOL : 0;
300
EL_FLAGS |= (GoodStr(T_dc) || GoodStr(T_DC)) ? TERM_CAN_DELETE : 0;
301
EL_FLAGS |= (GoodStr(T_im) || GoodStr(T_ic) || GoodStr(T_IC)) ?
303
EL_FLAGS |= (GoodStr(T_up) || GoodStr(T_UP)) ? TERM_CAN_UP : 0;
304
EL_FLAGS |= Val(T_am) ? TERM_HAS_AUTO_MARGINS : 0;
305
EL_FLAGS |= Val(T_xn) ? TERM_HAS_MAGIC_MARGINS : 0;
307
if (GoodStr(T_me) && GoodStr(T_ue))
308
EL_FLAGS |= (strcmp(Str(T_me), Str(T_ue)) == 0) ?
311
EL_FLAGS &= ~TERM_CAN_ME;
312
if (GoodStr(T_me) && GoodStr(T_se))
313
EL_FLAGS |= (strcmp(Str(T_me), Str(T_se)) == 0) ?
319
(void) fprintf(el->el_errfile,
320
"WARNING: Your terminal cannot move up.\n");
321
(void) fprintf(el->el_errfile,
322
"Editing may be odd for long lines.\n");
325
(void) fprintf(el->el_errfile, "no clear EOL capability.\n");
327
(void) fprintf(el->el_errfile, "no delete char capability.\n");
329
(void) fprintf(el->el_errfile, "no insert char capability.\n");
330
#endif /* DEBUG_SCREEN */
334
* Initialize the terminal stuff
337
term_init(EditLine *el)
340
el->el_term.t_buf = (char *) el_malloc(TC_BUFSIZE);
341
if (el->el_term.t_buf == NULL)
343
el->el_term.t_cap = (char *) el_malloc(TC_BUFSIZE);
344
if (el->el_term.t_cap == NULL)
346
el->el_term.t_fkey = (fkey_t *) el_malloc(A_K_NKEYS * sizeof(fkey_t));
347
if (el->el_term.t_fkey == NULL)
349
el->el_term.t_loc = 0;
350
el->el_term.t_str = (char **) el_malloc(T_str * sizeof(char *));
351
if (el->el_term.t_str == NULL)
353
(void) memset(el->el_term.t_str, 0, T_str * sizeof(char *));
354
el->el_term.t_val = (int *) el_malloc(T_val * sizeof(int));
355
if (el->el_term.t_val == NULL)
357
(void) memset(el->el_term.t_val, 0, T_val * sizeof(int));
358
(void) term_set(el, NULL);
364
* Clean up the terminal stuff
367
term_end(EditLine *el)
370
el_free((ptr_t) el->el_term.t_buf);
371
el->el_term.t_buf = NULL;
372
el_free((ptr_t) el->el_term.t_cap);
373
el->el_term.t_cap = NULL;
374
el->el_term.t_loc = 0;
375
el_free((ptr_t) el->el_term.t_str);
376
el->el_term.t_str = NULL;
377
el_free((ptr_t) el->el_term.t_val);
378
el->el_term.t_val = NULL;
379
el_free((ptr_t) el->el_term.t_fkey);
380
el->el_term.t_fkey = NULL;
381
term_free_display(el);
386
* Maintain a string pool for termcap strings
389
term_alloc(EditLine *el, const struct termcapstr *t, const char *cap)
391
char termbuf[TC_BUFSIZE];
393
char **tlist = el->el_term.t_str;
394
char **tmp, **str = &tlist[t - tstr];
396
if (cap == NULL || *cap == '\0') {
402
tlen = *str == NULL ? 0 : strlen(*str);
405
* New string is shorter; no need to allocate space
409
(void) strcpy(*str, cap); /* XXX strcpy is safe */
413
* New string is longer; see if we have enough space to append
415
if (el->el_term.t_loc + 3 < TC_BUFSIZE) {
416
/* XXX strcpy is safe */
417
(void) strcpy(*str = &el->el_term.t_buf[el->el_term.t_loc],
419
el->el_term.t_loc += clen + 1; /* one for \0 */
423
* Compact our buffer; no need to check compaction, cause we know it
427
for (tmp = tlist; tmp < &tlist[T_str]; tmp++)
428
if (*tmp != NULL && *tmp != '\0' && *tmp != *str) {
431
for (ptr = *tmp; *ptr != '\0'; termbuf[tlen++] = *ptr++)
433
termbuf[tlen++] = '\0';
435
memcpy(el->el_term.t_buf, termbuf, TC_BUFSIZE);
436
el->el_term.t_loc = tlen;
437
if (el->el_term.t_loc + 3 >= TC_BUFSIZE) {
438
(void) fprintf(el->el_errfile,
439
"Out of termcap string space.\n");
442
/* XXX strcpy is safe */
443
(void) strcpy(*str = &el->el_term.t_buf[el->el_term.t_loc], cap);
444
el->el_term.t_loc += clen + 1; /* one for \0 */
449
/* term_rebuffer_display():
450
* Rebuffer the display after the screen changed size
453
term_rebuffer_display(EditLine *el)
455
coord_t *c = &el->el_term.t_size;
457
term_free_display(el);
462
if (term_alloc_display(el) == -1)
468
/* term_alloc_display():
469
* Allocate a new display.
472
term_alloc_display(EditLine *el)
476
coord_t *c = &el->el_term.t_size;
478
b = (char **) el_malloc((size_t) (sizeof(char *) * (c->v + 1)));
481
for (i = 0; i < c->v; i++) {
482
b[i] = (char *) el_malloc((size_t) (sizeof(char) * (c->h + 1)));
485
el_free((ptr_t) b[i]);
493
b = (char **) el_malloc((size_t) (sizeof(char *) * (c->v + 1)));
496
for (i = 0; i < c->v; i++) {
497
b[i] = (char *) el_malloc((size_t) (sizeof(char) * (c->h + 1)));
500
el_free((ptr_t) b[i]);
511
/* term_free_display():
512
* Free the display buffers
515
term_free_display(EditLine *el)
521
el->el_display = NULL;
523
for (bufp = b; *bufp != NULL; bufp++)
524
el_free((ptr_t) * bufp);
528
el->el_vdisplay = NULL;
530
for (bufp = b; *bufp != NULL; bufp++)
531
el_free((ptr_t) * bufp);
537
/* term_move_to_line():
538
* move to line <where> (first line == 0)
539
* as efficiently as possible
542
term_move_to_line(EditLine *el, int where)
546
if (where == el->el_cursor.v)
549
if (where > el->el_term.t_size.v) {
551
(void) fprintf(el->el_errfile,
552
"term_move_to_line: where is ridiculous: %d\r\n", where);
553
#endif /* DEBUG_SCREEN */
556
if ((del = where - el->el_cursor.v) > 0) {
558
if (EL_HAS_AUTO_MARGINS &&
559
el->el_display[el->el_cursor.v][0] != '\0') {
560
/* move without newline */
561
term_move_to_char(el, el->el_term.t_size.h - 1);
563
&el->el_display[el->el_cursor.v][el->el_cursor.h],
568
if ((del > 1) && GoodStr(T_DO)) {
569
term_tputs(el, tgoto(Str(T_DO), del,
573
for (; del > 0; del--)
574
term__putc(el, '\n');
575
/* because the \n will become \r\n */
580
} else { /* del < 0 */
581
if (GoodStr(T_UP) && (-del > 1 || !GoodStr(T_up)))
582
term_tputs(el, tgoto(Str(T_UP), -del, -del), -del);
585
for (; del < 0; del++)
586
term_tputs(el, Str(T_up), 1);
589
el->el_cursor.v = where;/* now where is here */
593
/* term_move_to_char():
594
* Move to the character position specified
597
term_move_to_char(EditLine *el, int where)
602
if (where == el->el_cursor.h)
605
if (where > el->el_term.t_size.h) {
607
(void) fprintf(el->el_errfile,
608
"term_move_to_char: where is riduculous: %d\r\n", where);
609
#endif /* DEBUG_SCREEN */
612
if (!where) { /* if where is first column */
613
term__putc(el, '\r'); /* do a CR */
617
del = where - el->el_cursor.h;
619
if ((del < -4 || del > 4) && GoodStr(T_ch))
620
/* go there directly */
621
term_tputs(el, tgoto(Str(T_ch), where, where), where);
623
if (del > 0) { /* moving forward */
624
if ((del > 4) && GoodStr(T_RI))
625
term_tputs(el, tgoto(Str(T_RI), del, del), del);
627
/* if I can do tabs, use them */
629
if ((el->el_cursor.h & 0370) !=
631
/* if not within tab stop */
633
(el->el_cursor.h & 0370);
636
term__putc(el, '\t');
638
el->el_cursor.h = where & 0370;
642
* it's usually cheaper to just write the
646
* NOTE THAT term_overwrite() WILL CHANGE
650
&el->el_display[el->el_cursor.v][el->el_cursor.h],
651
where - el->el_cursor.h);
654
} else { /* del < 0 := moving backward */
655
if ((-del > 4) && GoodStr(T_LE))
656
term_tputs(el, tgoto(Str(T_LE), -del, -del),
658
else { /* can't go directly there */
660
* if the "cost" is greater than the "cost"
664
((unsigned int)-del >
665
(((unsigned int) where >> 3) +
668
term__putc(el, '\r'); /* do a CR */
670
goto mc_again; /* and try again */
672
for (i = 0; i < -del; i++)
673
term__putc(el, '\b');
677
el->el_cursor.h = where; /* now where is here */
682
* Overstrike num characters
685
term_overwrite(EditLine *el, const char *cp, int n)
688
return; /* catch bugs */
690
if (n > el->el_term.t_size.h) {
692
(void) fprintf(el->el_errfile,
693
"term_overwrite: n is riduculous: %d\r\n", n);
694
#endif /* DEBUG_SCREEN */
698
term__putc(el, *cp++);
702
if (el->el_cursor.h >= el->el_term.t_size.h) { /* wrap? */
703
if (EL_HAS_AUTO_MARGINS) { /* yes */
706
if (EL_HAS_MAGIC_MARGINS) {
707
/* force the wrap to avoid the "magic"
710
if ((c = el->el_display[el->el_cursor.v][el->el_cursor.h])
712
term_overwrite(el, &c, 1);
717
} else /* no wrap, but cursor stays on screen */
718
el->el_cursor.h = el->el_term.t_size.h;
723
/* term_deletechars():
724
* Delete num characters
727
term_deletechars(EditLine *el, int num)
732
if (!EL_CAN_DELETE) {
734
(void) fprintf(el->el_errfile, " ERROR: cannot delete \n");
735
#endif /* DEBUG_EDIT */
738
if (num > el->el_term.t_size.h) {
740
(void) fprintf(el->el_errfile,
741
"term_deletechars: num is riduculous: %d\r\n", num);
742
#endif /* DEBUG_SCREEN */
745
if (GoodStr(T_DC)) /* if I have multiple delete */
746
if ((num > 1) || !GoodStr(T_dc)) { /* if dc would be more
748
term_tputs(el, tgoto(Str(T_DC), num, num), num);
751
if (GoodStr(T_dm)) /* if I have delete mode */
752
term_tputs(el, Str(T_dm), 1);
754
if (GoodStr(T_dc)) /* else do one at a time */
756
term_tputs(el, Str(T_dc), 1);
758
if (GoodStr(T_ed)) /* if I have delete mode */
759
term_tputs(el, Str(T_ed), 1);
763
/* term_insertwrite():
764
* Puts terminal in insert character mode or inserts num
765
* characters in the line
768
term_insertwrite(EditLine *el, char *cp, int num)
772
if (!EL_CAN_INSERT) {
774
(void) fprintf(el->el_errfile, " ERROR: cannot insert \n");
775
#endif /* DEBUG_EDIT */
778
if (num > el->el_term.t_size.h) {
780
(void) fprintf(el->el_errfile,
781
"StartInsert: num is riduculous: %d\r\n", num);
782
#endif /* DEBUG_SCREEN */
785
if (GoodStr(T_IC)) /* if I have multiple insert */
786
if ((num > 1) || !GoodStr(T_ic)) {
787
/* if ic would be more expensive */
788
term_tputs(el, tgoto(Str(T_IC), num, num), num);
789
term_overwrite(el, cp, num);
790
/* this updates el_cursor.h */
793
if (GoodStr(T_im) && GoodStr(T_ei)) { /* if I have insert mode */
794
term_tputs(el, Str(T_im), 1);
796
el->el_cursor.h += num;
798
term__putc(el, *cp++);
801
if (GoodStr(T_ip)) /* have to make num chars insert */
802
term_tputs(el, Str(T_ip), 1);
804
term_tputs(el, Str(T_ei), 1);
808
if (GoodStr(T_ic)) /* have to make num chars insert */
809
term_tputs(el, Str(T_ic), 1);
811
term__putc(el, *cp++);
815
if (GoodStr(T_ip)) /* have to make num chars insert */
816
term_tputs(el, Str(T_ip), 1);
817
/* pad the inserted char */
824
* clear to end of line. There are num characters to clear
827
term_clear_EOL(EditLine *el, int num)
831
if (EL_CAN_CEOL && GoodStr(T_ce))
832
term_tputs(el, Str(T_ce), 1);
834
for (i = 0; i < num; i++)
836
el->el_cursor.h += num; /* have written num spaces */
841
/* term_clear_screen():
845
term_clear_screen(EditLine *el)
846
{ /* clear the whole screen and home */
849
/* send the clear screen code */
850
term_tputs(el, Str(T_cl), Val(T_li));
851
else if (GoodStr(T_ho) && GoodStr(T_cd)) {
852
term_tputs(el, Str(T_ho), Val(T_li)); /* home */
853
/* clear to bottom of screen */
854
term_tputs(el, Str(T_cd), Val(T_li));
856
term__putc(el, '\r');
857
term__putc(el, '\n');
863
* Beep the way the terminal wants us
866
term_beep(EditLine *el)
869
/* what termcap says we should use */
870
term_tputs(el, Str(T_bl), 1);
872
term__putc(el, '\007'); /* an ASCII bell; ^G */
877
/* term_clear_to_bottom():
878
* Clear to the bottom of the screen
881
term_clear_to_bottom(EditLine *el)
884
term_tputs(el, Str(T_cd), Val(T_li));
885
else if (GoodStr(T_ce))
886
term_tputs(el, Str(T_ce), Val(T_li));
891
term_get(EditLine *el, const char **term)
893
*term = el->el_term.t_name;
898
* Read in the terminal capabilities from the requested terminal
901
term_set(EditLine *el, const char *term)
904
char buf[TC_BUFSIZE];
906
const struct termcapstr *t;
910
(void) sigemptyset(&nset);
911
(void) sigaddset(&nset, SIGWINCH);
912
(void) sigprocmask(SIG_BLOCK, &nset, &oset);
918
term = getenv("TERM");
920
if (!term || !term[0])
923
if (strcmp(term, "emacs") == 0)
924
el->el_flags |= EDIT_DISABLED;
926
memset(el->el_term.t_cap, 0, TC_BUFSIZE);
928
i = tgetent(el->el_term.t_cap, term);
932
(void) fprintf(el->el_errfile,
933
"Cannot read termcap database;\n");
935
(void) fprintf(el->el_errfile,
936
"No entry for terminal type \"%s\";\n", term);
937
(void) fprintf(el->el_errfile,
938
"using dumb terminal settings.\n");
939
Val(T_co) = 80; /* do a dumb terminal */
940
Val(T_pt) = Val(T_km) = Val(T_li) = 0;
941
Val(T_xt) = Val(T_MT);
942
for (t = tstr; t->name != NULL; t++)
943
term_alloc(el, t, NULL);
945
/* auto/magic margins */
946
Val(T_am) = tgetflag("am");
947
Val(T_xn) = tgetflag("xn");
949
Val(T_pt) = tgetflag("pt");
950
Val(T_xt) = tgetflag("xt");
951
/* do we have a meta? */
952
Val(T_km) = tgetflag("km");
953
Val(T_MT) = tgetflag("MT");
955
Val(T_co) = tgetnum("co");
956
Val(T_li) = tgetnum("li");
957
for (t = tstr; t->name != NULL; t++) {
958
/* XXX: some systems' tgetstr needs non const */
959
term_alloc(el, t, tgetstr(strchr(t->name, *t->name),
965
Val(T_co) = 80; /* just in case */
969
el->el_term.t_size.v = Val(T_co);
970
el->el_term.t_size.h = Val(T_li);
974
/* get the correct window size */
975
(void) term_get_size(el, &lins, &cols);
976
if (term_change_size(el, lins, cols) == -1)
978
(void) sigprocmask(SIG_SETMASK, &oset, NULL);
980
el->el_term.t_name = term;
981
return (i <= 0 ? -1 : 0);
986
* Return the new window size in lines and cols, and
987
* true if the size was changed.
990
term_get_size(EditLine *el, int *lins, int *cols)
999
if (ioctl(el->el_infd, TIOCGWINSZ, (ioctl_t) & ws) != -1) {
1010
if (ioctl(el->el_infd, TIOCGSIZE, (ioctl_t) & ts) != -1) {
1014
*lins = ts.ts_lines;
1018
return (Val(T_co) != *cols || Val(T_li) != *lins);
1022
/* term_change_size():
1023
* Change the size of the terminal
1026
term_change_size(EditLine *el, int lins, int cols)
1031
Val(T_co) = (cols < 2) ? 80 : cols;
1032
Val(T_li) = (lins < 1) ? 24 : lins;
1034
/* re-make display buffers */
1035
if (term_rebuffer_display(el) == -1)
1037
re_clear_display(el);
1042
/* term_init_arrow():
1043
* Initialize the arrow key bindings from termcap
1046
term_init_arrow(EditLine *el)
1048
fkey_t *arrow = el->el_term.t_fkey;
1050
arrow[A_K_DN].name = "down";
1051
arrow[A_K_DN].key = T_kd;
1052
arrow[A_K_DN].fun.cmd = ED_NEXT_HISTORY;
1053
arrow[A_K_DN].type = XK_CMD;
1055
arrow[A_K_UP].name = "up";
1056
arrow[A_K_UP].key = T_ku;
1057
arrow[A_K_UP].fun.cmd = ED_PREV_HISTORY;
1058
arrow[A_K_UP].type = XK_CMD;
1060
arrow[A_K_LT].name = "left";
1061
arrow[A_K_LT].key = T_kl;
1062
arrow[A_K_LT].fun.cmd = ED_PREV_CHAR;
1063
arrow[A_K_LT].type = XK_CMD;
1065
arrow[A_K_RT].name = "right";
1066
arrow[A_K_RT].key = T_kr;
1067
arrow[A_K_RT].fun.cmd = ED_NEXT_CHAR;
1068
arrow[A_K_RT].type = XK_CMD;
1070
arrow[A_K_HO].name = "home";
1071
arrow[A_K_HO].key = T_kh;
1072
arrow[A_K_HO].fun.cmd = ED_MOVE_TO_BEG;
1073
arrow[A_K_HO].type = XK_CMD;
1075
arrow[A_K_EN].name = "end";
1076
arrow[A_K_EN].key = T_at7;
1077
arrow[A_K_EN].fun.cmd = ED_MOVE_TO_END;
1078
arrow[A_K_EN].type = XK_CMD;
1082
/* term_reset_arrow():
1083
* Reset arrow key bindings
1086
term_reset_arrow(EditLine *el)
1088
fkey_t *arrow = el->el_term.t_fkey;
1089
static const char strA[] = {033, '[', 'A', '\0'};
1090
static const char strB[] = {033, '[', 'B', '\0'};
1091
static const char strC[] = {033, '[', 'C', '\0'};
1092
static const char strD[] = {033, '[', 'D', '\0'};
1093
static const char strH[] = {033, '[', 'H', '\0'};
1094
static const char strF[] = {033, '[', 'F', '\0'};
1095
static const char stOA[] = {033, 'O', 'A', '\0'};
1096
static const char stOB[] = {033, 'O', 'B', '\0'};
1097
static const char stOC[] = {033, 'O', 'C', '\0'};
1098
static const char stOD[] = {033, 'O', 'D', '\0'};
1099
static const char stOH[] = {033, 'O', 'H', '\0'};
1100
static const char stOF[] = {033, 'O', 'F', '\0'};
1102
key_add(el, strA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1103
key_add(el, strB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1104
key_add(el, strC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1105
key_add(el, strD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1106
key_add(el, strH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1107
key_add(el, strF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1108
key_add(el, stOA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1109
key_add(el, stOB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1110
key_add(el, stOC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1111
key_add(el, stOD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1112
key_add(el, stOH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1113
key_add(el, stOF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1115
if (el->el_map.type == MAP_VI) {
1116
key_add(el, &strA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1117
key_add(el, &strB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1118
key_add(el, &strC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1119
key_add(el, &strD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1120
key_add(el, &strH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1121
key_add(el, &strF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1122
key_add(el, &stOA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1123
key_add(el, &stOB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1124
key_add(el, &stOC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1125
key_add(el, &stOD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1126
key_add(el, &stOH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1127
key_add(el, &stOF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1132
/* term_set_arrow():
1133
* Set an arrow key binding
1136
term_set_arrow(EditLine *el, const char *name, key_value_t *fun, int type)
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].fun = *fun;
1144
arrow[i].type = type;
1151
/* term_clear_arrow():
1152
* Clear an arrow key binding
1155
term_clear_arrow(EditLine *el, const char *name)
1157
fkey_t *arrow = el->el_term.t_fkey;
1160
for (i = 0; i < A_K_NKEYS; i++)
1161
if (strcmp(name, arrow[i].name) == 0) {
1162
arrow[i].type = XK_NOD;
1169
/* term_print_arrow():
1170
* Print the arrow key bindings
1173
term_print_arrow(EditLine *el, const char *name)
1176
fkey_t *arrow = el->el_term.t_fkey;
1178
for (i = 0; i < A_K_NKEYS; i++)
1179
if (*name == '\0' || strcmp(name, arrow[i].name) == 0)
1180
if (arrow[i].type != XK_NOD)
1181
key_kprint(el, arrow[i].name, &arrow[i].fun,
1186
/* term_bind_arrow():
1187
* Bind the arrow keys
1190
term_bind_arrow(EditLine *el)
1193
const el_action_t *dmap;
1196
fkey_t *arrow = el->el_term.t_fkey;
1198
/* Check if the components needed are initialized */
1199
if (el->el_term.t_buf == NULL || el->el_map.key == NULL)
1202
map = el->el_map.type == MAP_VI ? el->el_map.alt : el->el_map.key;
1203
dmap = el->el_map.type == MAP_VI ? el->el_map.vic : el->el_map.emacs;
1205
term_reset_arrow(el);
1207
for (i = 0; i < A_K_NKEYS; i++) {
1208
p = el->el_term.t_str[arrow[i].key];
1210
j = (unsigned char) *p;
1212
* Assign the arrow keys only if:
1214
* 1. They are multi-character arrow keys and the user
1215
* has not re-assigned the leading character, or
1216
* has re-assigned the leading character to be
1217
* ED_SEQUENCE_LEAD_IN
1218
* 2. They are single arrow keys pointing to an
1221
if (arrow[i].type == XK_NOD)
1222
key_clear(el, map, p);
1224
if (p[1] && (dmap[j] == map[j] ||
1225
map[j] == ED_SEQUENCE_LEAD_IN)) {
1226
key_add(el, p, &arrow[i].fun,
1228
map[j] = ED_SEQUENCE_LEAD_IN;
1229
} else if (map[j] == ED_UNASSIGNED) {
1230
key_clear(el, map, p);
1231
if (arrow[i].type == XK_CMD)
1232
map[j] = arrow[i].fun.cmd;
1234
key_add(el, p, &arrow[i].fun,
1249
if (term_outfile == NULL)
1251
return fputc(c, term_outfile);
1255
term_tputs(EditLine *el, const char *cap, int affcnt)
1258
pthread_mutex_lock(&term_mutex);
1260
term_outfile = el->el_outfile;
1261
(void)tputs(cap, affcnt, term_putc);
1263
pthread_mutex_unlock(&term_mutex);
1271
term__putc(EditLine *el, int c)
1274
return fputc(c, el->el_outfile);
1281
term__flush(EditLine *el)
1284
(void) fflush(el->el_outfile);
1288
* Write the given character out, in a human readable form
1291
term_writec(EditLine *el, int c)
1294
int cnt = key__decode_char(buf, sizeof(buf), 0, c);
1296
term_overwrite(el, buf, cnt);
1302
* Print the current termcap characteristics
1306
term_telltc(EditLine *el, int argc __attribute__((__unused__)),
1307
const char **argv __attribute__((__unused__)))
1309
const struct termcapstr *t;
1311
char upbuf[EL_BUFSIZ];
1313
(void) fprintf(el->el_outfile, "\n\tYour terminal has the\n");
1314
(void) fprintf(el->el_outfile, "\tfollowing characteristics:\n\n");
1315
(void) fprintf(el->el_outfile, "\tIt has %d columns and %d lines\n",
1316
Val(T_co), Val(T_li));
1317
(void) fprintf(el->el_outfile,
1318
"\tIt has %s meta key\n", EL_HAS_META ? "a" : "no");
1319
(void) fprintf(el->el_outfile,
1320
"\tIt can%suse tabs\n", EL_CAN_TAB ? " " : "not ");
1321
(void) fprintf(el->el_outfile, "\tIt %s automatic margins\n",
1322
EL_HAS_AUTO_MARGINS ? "has" : "does not have");
1323
if (EL_HAS_AUTO_MARGINS)
1324
(void) fprintf(el->el_outfile, "\tIt %s magic margins\n",
1325
EL_HAS_MAGIC_MARGINS ? "has" : "does not have");
1327
for (t = tstr, ts = el->el_term.t_str; t->name != NULL; t++, ts++) {
1330
(void) key__decode_str(*ts, upbuf, sizeof(upbuf), "");
1335
(void) fprintf(el->el_outfile, "\t%25s (%s) == %s\n",
1336
t->long_name, t->name, ub);
1338
(void) fputc('\n', el->el_outfile);
1344
* Change the current terminal characteristics
1348
term_settc(EditLine *el, int argc __attribute__((__unused__)),
1351
const struct termcapstr *ts;
1352
const struct termcapval *tv;
1353
const char *what, *how;
1355
if (argv == NULL || argv[1] == NULL || argv[2] == NULL)
1362
* Do the strings first
1364
for (ts = tstr; ts->name != NULL; ts++)
1365
if (strcmp(ts->name, what) == 0)
1368
if (ts->name != NULL) {
1369
term_alloc(el, ts, how);
1374
* Do the numeric ones second
1376
for (tv = tval; tv->name != NULL; tv++)
1377
if (strcmp(tv->name, what) == 0)
1380
if (tv->name != NULL)
1383
if (tv == &tval[T_pt] || tv == &tval[T_km] ||
1384
tv == &tval[T_am] || tv == &tval[T_xn]) {
1385
if (strcmp(how, "yes") == 0)
1386
el->el_term.t_val[tv - tval] = 1;
1387
else if (strcmp(how, "no") == 0)
1388
el->el_term.t_val[tv - tval] = 0;
1390
(void) fprintf(el->el_errfile,
1391
"%s: Bad value `%s'.\n", argv[0], how);
1395
if (term_change_size(el, Val(T_li), Val(T_co)) == -1)
1402
i = strtol(how, &ep, 10);
1404
(void) fprintf(el->el_errfile,
1405
"%s: Bad value `%s'.\n", argv[0], how);
1408
el->el_term.t_val[tv - tval] = (int) i;
1409
el->el_term.t_size.v = Val(T_co);
1410
el->el_term.t_size.h = Val(T_li);
1411
if (tv == &tval[T_co] || tv == &tval[T_li])
1412
if (term_change_size(el, Val(T_li), Val(T_co))
1421
* Get the current terminal characteristics
1425
term_gettc(EditLine *el, int argc __attribute__((__unused__)), char **argv)
1427
const struct termcapstr *ts;
1428
const struct termcapval *tv;
1432
if (argv == NULL || argv[1] == NULL || argv[2] == NULL)
1439
* Do the strings first
1441
for (ts = tstr; ts->name != NULL; ts++)
1442
if (strcmp(ts->name, what) == 0)
1445
if (ts->name != NULL) {
1446
*(char **)how = el->el_term.t_str[ts - tstr];
1450
* Do the numeric ones second
1452
for (tv = tval; tv->name != NULL; tv++)
1453
if (strcmp(tv->name, what) == 0)
1456
if (tv->name == NULL)
1459
if (tv == &tval[T_pt] || tv == &tval[T_km] ||
1460
tv == &tval[T_am] || tv == &tval[T_xn]) {
1461
static char yes[] = "yes";
1462
static char no[] = "no";
1463
if (el->el_term.t_val[tv - tval])
1464
*(char **)how = yes;
1469
*(int *)how = el->el_term.t_val[tv - tval];
1475
* Print the termcap string out with variable substitution
1479
term_echotc(EditLine *el, int argc __attribute__((__unused__)),
1482
char *cap, *scap, *ep;
1483
int arg_need, arg_cols, arg_rows;
1484
int verbose = 0, silent = 0;
1486
static const char fmts[] = "%s\n", fmtd[] = "%d\n";
1487
const struct termcapstr *t;
1488
char buf[TC_BUFSIZE];
1493
if (argv == NULL || argv[1] == NULL)
1497
if (argv[0][0] == '-') {
1498
switch (argv[0][1]) {
1506
/* stderror(ERR_NAME | ERR_TCUSAGE); */
1511
if (!*argv || *argv[0] == '\0')
1513
if (strcmp(*argv, "tabs") == 0) {
1514
(void) fprintf(el->el_outfile, fmts, EL_CAN_TAB ? "yes" : "no");
1516
} else if (strcmp(*argv, "meta") == 0) {
1517
(void) fprintf(el->el_outfile, fmts, Val(T_km) ? "yes" : "no");
1519
} else if (strcmp(*argv, "xn") == 0) {
1520
(void) fprintf(el->el_outfile, fmts, EL_HAS_MAGIC_MARGINS ?
1523
} else if (strcmp(*argv, "am") == 0) {
1524
(void) fprintf(el->el_outfile, fmts, EL_HAS_AUTO_MARGINS ?
1527
} else if (strcmp(*argv, "baud") == 0) {
1531
for (i = 0; baud_rate[i].b_name != NULL; i++)
1532
if (el->el_tty.t_speed == baud_rate[i].b_rate) {
1533
(void) fprintf(el->el_outfile, fmts,
1534
baud_rate[i].b_name);
1537
(void) fprintf(el->el_outfile, fmtd, 0);
1539
(void) fprintf(el->el_outfile, fmtd, (int)el->el_tty.t_speed);
1542
} else if (strcmp(*argv, "rows") == 0 || strcmp(*argv, "lines") == 0) {
1543
(void) fprintf(el->el_outfile, fmtd, Val(T_li));
1545
} else if (strcmp(*argv, "cols") == 0) {
1546
(void) fprintf(el->el_outfile, fmtd, Val(T_co));
1550
* Try to use our local definition first
1553
for (t = tstr; t->name != NULL; t++)
1554
if (strcmp(t->name, *argv) == 0) {
1555
scap = el->el_term.t_str[t - tstr];
1558
if (t->name == NULL) {
1559
/* XXX: some systems' tgetstr needs non const */
1560
scap = tgetstr(strchr(*argv, **argv), &area);
1562
if (!scap || scap[0] == '\0') {
1564
(void) fprintf(el->el_errfile,
1565
"echotc: Termcap parameter `%s' not found.\n",
1570
* Count home many values we need for this capability.
1572
for (cap = scap, arg_need = 0; *cap; cap++)
1592
* hpux has lot's of them...
1595
(void) fprintf(el->el_errfile,
1596
"echotc: Warning: unknown termcap %% `%c'.\n",
1598
/* This is bad, but I won't complain */
1605
if (*argv && *argv[0]) {
1607
(void) fprintf(el->el_errfile,
1608
"echotc: Warning: Extra argument `%s'.\n",
1612
term_tputs(el, scap, 1);
1616
if (!*argv || *argv[0] == '\0') {
1618
(void) fprintf(el->el_errfile,
1619
"echotc: Warning: Missing argument.\n");
1623
i = strtol(*argv, &ep, 10);
1624
if (*ep != '\0' || i < 0) {
1626
(void) fprintf(el->el_errfile,
1627
"echotc: Bad value `%s' for rows.\n",
1633
if (*argv && *argv[0]) {
1635
(void) fprintf(el->el_errfile,
1636
"echotc: Warning: Extra argument `%s'.\n",
1640
term_tputs(el, tgoto(scap, arg_cols, arg_rows), 1);
1643
/* This is wrong, but I will ignore it... */
1645
(void) fprintf(el->el_errfile,
1646
"echotc: Warning: Too many required arguments (%d).\n",
1651
if (!*argv || *argv[0] == '\0') {
1653
(void) fprintf(el->el_errfile,
1654
"echotc: Warning: Missing argument.\n");
1657
i = strtol(*argv, &ep, 10);
1658
if (*ep != '\0' || i < 0) {
1660
(void) fprintf(el->el_errfile,
1661
"echotc: Bad value `%s' for cols.\n",
1667
if (!*argv || *argv[0] == '\0') {
1669
(void) fprintf(el->el_errfile,
1670
"echotc: Warning: Missing argument.\n");
1673
i = strtol(*argv, &ep, 10);
1674
if (*ep != '\0' || i < 0) {
1676
(void) fprintf(el->el_errfile,
1677
"echotc: Bad value `%s' for rows.\n",
1684
(void) fprintf(el->el_errfile,
1685
"echotc: Bad value `%s'.\n", *argv);
1689
if (*argv && *argv[0]) {
1691
(void) fprintf(el->el_errfile,
1692
"echotc: Warning: Extra argument `%s'.\n",
1696
term_tputs(el, tgoto(scap, arg_cols, arg_rows), arg_rows);