1 /* $NetBSD: tty.c,v 1.31 2009/07/22 15:58:09 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
[] = "@(#)tty.c 8.1 (Berkeley) 6/4/93";
40 __RCSID("$NetBSD: tty.c,v 1.31 2009/07/22 15:58:09 christos Exp $");
42 #endif /* not lint && not SCCSID */
45 * tty.c: tty interface stuff
52 typedef struct ttymodes_t
{
58 typedef struct ttymap_t
{
59 Int nch
, och
; /* Internal and termio rep of chars */
60 el_action_t bind
[3]; /* emacs, vi, and vi-cmd */
64 private const ttyperm_t ttyperm
= {
66 {"iflag:", ICRNL
, (INLCR
| IGNCR
)},
67 {"oflag:", (OPOST
| ONLCR
), ONLRET
},
69 {"lflag:", (ISIG
| ICANON
| ECHO
| ECHOE
| ECHOCTL
| IEXTEN
),
70 (NOFLSH
| ECHONL
| EXTPROC
| FLUSHO
)},
74 {"iflag:", (INLCR
| ICRNL
), IGNCR
},
75 {"oflag:", (OPOST
| ONLCR
), ONLRET
},
78 (NOFLSH
| ICANON
| ECHO
| ECHOK
| ECHONL
| EXTPROC
| IEXTEN
| FLUSHO
)},
79 {"chars:", (C_SH(C_MIN
) | C_SH(C_TIME
) | C_SH(C_SWTCH
) | C_SH(C_DSWTCH
) |
80 C_SH(C_SUSP
) | C_SH(C_DSUSP
) | C_SH(C_EOL
) | C_SH(C_DISCARD
) |
81 C_SH(C_PGOFF
) | C_SH(C_PAGE
) | C_SH(C_STATUS
)), 0}
84 {"iflag:", 0, IXON
| IXOFF
| INLCR
| ICRNL
},
87 {"lflag:", 0, ISIG
| IEXTEN
},
92 private const ttychar_t ttychar
= {
94 CINTR
, CQUIT
, CERASE
, CKILL
,
95 CEOF
, CEOL
, CEOL2
, CSWTCH
,
96 CDSWTCH
, CERASE2
, CSTART
, CSTOP
,
97 CWERASE
, CSUSP
, CDSUSP
, CREPRINT
,
98 CDISCARD
, CLNEXT
, CSTATUS
, CPAGE
,
99 CPGOFF
, CKILL2
, CBRK
, CMIN
,
103 CINTR
, CQUIT
, CERASE
, CKILL
,
104 _POSIX_VDISABLE
, _POSIX_VDISABLE
, _POSIX_VDISABLE
, _POSIX_VDISABLE
,
105 _POSIX_VDISABLE
, CERASE2
, CSTART
, CSTOP
,
106 _POSIX_VDISABLE
, CSUSP
, _POSIX_VDISABLE
, _POSIX_VDISABLE
,
107 CDISCARD
, _POSIX_VDISABLE
, _POSIX_VDISABLE
, _POSIX_VDISABLE
,
108 _POSIX_VDISABLE
, _POSIX_VDISABLE
, _POSIX_VDISABLE
, 1,
122 private const ttymap_t tty_map
[] = {
125 {EM_DELETE_PREV_CHAR
, VI_DELETE_PREV_CHAR
, ED_PREV_CHAR
}},
129 {EM_DELETE_PREV_CHAR
, VI_DELETE_PREV_CHAR
, ED_PREV_CHAR
}},
133 {EM_KILL_LINE
, VI_KILL_LINE_PREV
, ED_UNASSIGNED
}},
137 {EM_KILL_LINE
, VI_KILL_LINE_PREV
, ED_UNASSIGNED
}},
141 {EM_DELETE_OR_LIST
, VI_LIST_OR_EOF
, ED_UNASSIGNED
}},
145 {ED_DELETE_PREV_WORD
, ED_DELETE_PREV_WORD
, ED_PREV_WORD
}},
148 {C_REPRINT
, VREPRINT
,
149 {ED_REDISPLAY
, ED_INSERT
, ED_REDISPLAY
}},
150 #endif /* VREPRINT */
153 {ED_QUOTED_INSERT
, ED_QUOTED_INSERT
, ED_UNASSIGNED
}},
156 {ED_UNASSIGNED
, ED_UNASSIGNED
, ED_UNASSIGNED
}}
159 private const ttymodes_t ttymodes
[] = {
161 {"ignbrk", IGNBRK
, MD_INP
},
164 {"brkint", BRKINT
, MD_INP
},
167 {"ignpar", IGNPAR
, MD_INP
},
170 {"parmrk", PARMRK
, MD_INP
},
173 {"inpck", INPCK
, MD_INP
},
176 {"istrip", ISTRIP
, MD_INP
},
179 {"inlcr", INLCR
, MD_INP
},
182 {"igncr", IGNCR
, MD_INP
},
185 {"icrnl", ICRNL
, MD_INP
},
188 {"iuclc", IUCLC
, MD_INP
},
191 {"ixon", IXON
, MD_INP
},
194 {"ixany", IXANY
, MD_INP
},
197 {"ixoff", IXOFF
, MD_INP
},
200 {"imaxbel", IMAXBEL
, MD_INP
},
204 {"opost", OPOST
, MD_OUT
},
207 {"olcuc", OLCUC
, MD_OUT
},
210 {"onlcr", ONLCR
, MD_OUT
},
213 {"ocrnl", OCRNL
, MD_OUT
},
216 {"onocr", ONOCR
, MD_OUT
},
219 {"onoeot", ONOEOT
, MD_OUT
},
222 {"onlret", ONLRET
, MD_OUT
},
225 {"ofill", OFILL
, MD_OUT
},
228 {"ofdel", OFDEL
, MD_OUT
},
231 {"nldly", NLDLY
, MD_OUT
},
234 {"crdly", CRDLY
, MD_OUT
},
237 {"tabdly", TABDLY
, MD_OUT
},
240 {"xtabs", XTABS
, MD_OUT
},
243 {"bsdly", BSDLY
, MD_OUT
},
246 {"vtdly", VTDLY
, MD_OUT
},
249 {"ffdly", FFDLY
, MD_OUT
},
252 {"pageout", PAGEOUT
, MD_OUT
},
255 {"wrap", WRAP
, MD_OUT
},
259 {"cignore", CIGNORE
, MD_CTL
},
262 {"cbaud", CBAUD
, MD_CTL
},
265 {"cstopb", CSTOPB
, MD_CTL
},
268 {"cread", CREAD
, MD_CTL
},
271 {"parenb", PARENB
, MD_CTL
},
274 {"parodd", PARODD
, MD_CTL
},
277 {"hupcl", HUPCL
, MD_CTL
},
280 {"clocal", CLOCAL
, MD_CTL
},
283 {"loblk", LOBLK
, MD_CTL
},
286 {"cibaud", CIBAUD
, MD_CTL
},
290 {"ccts_oflow", CCTS_OFLOW
, MD_CTL
},
292 {"crtscts", CRTSCTS
, MD_CTL
},
293 #endif /* CCTS_OFLOW */
296 {"crts_iflow", CRTS_IFLOW
, MD_CTL
},
297 #endif /* CRTS_IFLOW */
299 {"cdtrcts", CDTRCTS
, MD_CTL
},
302 {"mdmbuf", MDMBUF
, MD_CTL
},
305 {"rcv1en", RCV1EN
, MD_CTL
},
308 {"xmt1en", XMT1EN
, MD_CTL
},
312 {"isig", ISIG
, MD_LIN
},
315 {"icanon", ICANON
, MD_LIN
},
318 {"xcase", XCASE
, MD_LIN
},
321 {"echo", ECHO
, MD_LIN
},
324 {"echoe", ECHOE
, MD_LIN
},
327 {"echok", ECHOK
, MD_LIN
},
330 {"echonl", ECHONL
, MD_LIN
},
333 {"noflsh", NOFLSH
, MD_LIN
},
336 {"tostop", TOSTOP
, MD_LIN
},
339 {"echoctl", ECHOCTL
, MD_LIN
},
342 {"echoprt", ECHOPRT
, MD_LIN
},
345 {"echoke", ECHOKE
, MD_LIN
},
348 {"defecho", DEFECHO
, MD_LIN
},
351 {"flusho", FLUSHO
, MD_LIN
},
354 {"pendin", PENDIN
, MD_LIN
},
357 {"iexten", IEXTEN
, MD_LIN
},
360 {"nokerninfo", NOKERNINFO
, MD_LIN
},
361 #endif /* NOKERNINFO */
363 {"altwerase", ALTWERASE
, MD_LIN
},
364 #endif /* ALTWERASE */
366 {"extproc", EXTPROC
, MD_LIN
},
370 {"intr", C_SH(C_INTR
), MD_CHAR
},
373 {"quit", C_SH(C_QUIT
), MD_CHAR
},
376 {"erase", C_SH(C_ERASE
), MD_CHAR
},
379 {"kill", C_SH(C_KILL
), MD_CHAR
},
382 {"eof", C_SH(C_EOF
), MD_CHAR
},
385 {"eol", C_SH(C_EOL
), MD_CHAR
},
388 {"eol2", C_SH(C_EOL2
), MD_CHAR
},
391 {"swtch", C_SH(C_SWTCH
), MD_CHAR
},
394 {"dswtch", C_SH(C_DSWTCH
), MD_CHAR
},
397 {"erase2", C_SH(C_ERASE2
), MD_CHAR
},
400 {"start", C_SH(C_START
), MD_CHAR
},
403 {"stop", C_SH(C_STOP
), MD_CHAR
},
406 {"werase", C_SH(C_WERASE
), MD_CHAR
},
409 {"susp", C_SH(C_SUSP
), MD_CHAR
},
412 {"dsusp", C_SH(C_DSUSP
), MD_CHAR
},
414 #if defined(VREPRINT)
415 {"reprint", C_SH(C_REPRINT
), MD_CHAR
},
416 #endif /* VREPRINT */
417 #if defined(VDISCARD)
418 {"discard", C_SH(C_DISCARD
), MD_CHAR
},
419 #endif /* VDISCARD */
421 {"lnext", C_SH(C_LNEXT
), MD_CHAR
},
424 {"status", C_SH(C_STATUS
), MD_CHAR
},
427 {"page", C_SH(C_PAGE
), MD_CHAR
},
430 {"pgoff", C_SH(C_PGOFF
), MD_CHAR
},
433 {"kill2", C_SH(C_KILL2
), MD_CHAR
},
436 {"brk", C_SH(C_BRK
), MD_CHAR
},
439 {"min", C_SH(C_MIN
), MD_CHAR
},
442 {"time", C_SH(C_TIME
), MD_CHAR
},
449 #define tty__gettabs(td) ((((td)->c_oflag & TAB3) == TAB3) ? 0 : 1)
450 #define tty__geteightbit(td) (((td)->c_cflag & CSIZE) == CS8)
451 #define tty__cooked_mode(td) ((td)->c_lflag & ICANON)
453 private int tty_getty(EditLine
*, struct termios
*);
454 private int tty_setty(EditLine
*, int, const struct termios
*);
455 private int tty__getcharindex(int);
456 private void tty__getchar(struct termios
*, unsigned char *);
457 private void tty__setchar(struct termios
*, unsigned char *);
458 private speed_t
tty__getspeed(struct termios
*);
459 private int tty_setup(EditLine
*);
464 * Wrapper for tcgetattr to handle EINTR
467 tty_getty(EditLine
*el
, struct termios
*t
)
470 while ((rv
= tcgetattr(el
->el_infd
, t
)) == -1 && errno
== EINTR
)
476 * Wrapper for tcsetattr to handle EINTR
479 tty_setty(EditLine
*el
, int action
, const struct termios
*t
)
482 while ((rv
= tcsetattr(el
->el_infd
, action
, t
)) == -1 && errno
== EINTR
)
488 * Get the tty parameters and initialize the editing state
491 tty_setup(EditLine
*el
)
495 if (el
->el_flags
& EDIT_DISABLED
)
498 if (tty_getty(el
, &el
->el_tty
.t_ed
) == -1) {
500 (void) fprintf(el
->el_errfile
,
501 "tty_setup: tty_getty: %s\n", strerror(errno
));
502 #endif /* DEBUG_TTY */
505 el
->el_tty
.t_ts
= el
->el_tty
.t_ex
= el
->el_tty
.t_ed
;
507 el
->el_tty
.t_speed
= tty__getspeed(&el
->el_tty
.t_ex
);
508 el
->el_tty
.t_tabs
= tty__gettabs(&el
->el_tty
.t_ex
);
509 el
->el_tty
.t_eight
= tty__geteightbit(&el
->el_tty
.t_ex
);
511 el
->el_tty
.t_ex
.c_iflag
&= ~el
->el_tty
.t_t
[EX_IO
][MD_INP
].t_clrmask
;
512 el
->el_tty
.t_ex
.c_iflag
|= el
->el_tty
.t_t
[EX_IO
][MD_INP
].t_setmask
;
514 el
->el_tty
.t_ex
.c_oflag
&= ~el
->el_tty
.t_t
[EX_IO
][MD_OUT
].t_clrmask
;
515 el
->el_tty
.t_ex
.c_oflag
|= el
->el_tty
.t_t
[EX_IO
][MD_OUT
].t_setmask
;
517 el
->el_tty
.t_ex
.c_cflag
&= ~el
->el_tty
.t_t
[EX_IO
][MD_CTL
].t_clrmask
;
518 el
->el_tty
.t_ex
.c_cflag
|= el
->el_tty
.t_t
[EX_IO
][MD_CTL
].t_setmask
;
520 el
->el_tty
.t_ex
.c_lflag
&= ~el
->el_tty
.t_t
[EX_IO
][MD_LIN
].t_clrmask
;
521 el
->el_tty
.t_ex
.c_lflag
|= el
->el_tty
.t_t
[EX_IO
][MD_LIN
].t_setmask
;
524 * Reset the tty chars to reasonable defaults
525 * If they are disabled, then enable them.
528 if (tty__cooked_mode(&el
->el_tty
.t_ts
)) {
529 tty__getchar(&el
->el_tty
.t_ts
, el
->el_tty
.t_c
[TS_IO
]);
531 * Don't affect CMIN and CTIME for the editor mode
533 for (rst
= 0; rst
< C_NCC
- 2; rst
++)
534 if (el
->el_tty
.t_c
[TS_IO
][rst
] !=
535 el
->el_tty
.t_vdisable
536 && el
->el_tty
.t_c
[ED_IO
][rst
] !=
537 el
->el_tty
.t_vdisable
)
538 el
->el_tty
.t_c
[ED_IO
][rst
] =
539 el
->el_tty
.t_c
[TS_IO
][rst
];
540 for (rst
= 0; rst
< C_NCC
; rst
++)
541 if (el
->el_tty
.t_c
[TS_IO
][rst
] !=
542 el
->el_tty
.t_vdisable
)
543 el
->el_tty
.t_c
[EX_IO
][rst
] =
544 el
->el_tty
.t_c
[TS_IO
][rst
];
546 tty__setchar(&el
->el_tty
.t_ex
, el
->el_tty
.t_c
[EX_IO
]);
547 if (tty_setty(el
, TCSADRAIN
, &el
->el_tty
.t_ex
) == -1) {
549 (void) fprintf(el
->el_errfile
,
550 "tty_setup: tty_setty: %s\n",
552 #endif /* DEBUG_TTY */
558 tty__setchar(&el
->el_tty
.t_ex
, el
->el_tty
.t_c
[EX_IO
]);
561 el
->el_tty
.t_ed
.c_iflag
&= ~el
->el_tty
.t_t
[ED_IO
][MD_INP
].t_clrmask
;
562 el
->el_tty
.t_ed
.c_iflag
|= el
->el_tty
.t_t
[ED_IO
][MD_INP
].t_setmask
;
564 el
->el_tty
.t_ed
.c_oflag
&= ~el
->el_tty
.t_t
[ED_IO
][MD_OUT
].t_clrmask
;
565 el
->el_tty
.t_ed
.c_oflag
|= el
->el_tty
.t_t
[ED_IO
][MD_OUT
].t_setmask
;
567 el
->el_tty
.t_ed
.c_cflag
&= ~el
->el_tty
.t_t
[ED_IO
][MD_CTL
].t_clrmask
;
568 el
->el_tty
.t_ed
.c_cflag
|= el
->el_tty
.t_t
[ED_IO
][MD_CTL
].t_setmask
;
570 el
->el_tty
.t_ed
.c_lflag
&= ~el
->el_tty
.t_t
[ED_IO
][MD_LIN
].t_clrmask
;
571 el
->el_tty
.t_ed
.c_lflag
|= el
->el_tty
.t_t
[ED_IO
][MD_LIN
].t_setmask
;
573 tty__setchar(&el
->el_tty
.t_ed
, el
->el_tty
.t_c
[ED_IO
]);
574 tty_bind_char(el
, 1);
579 tty_init(EditLine
*el
)
582 el
->el_tty
.t_mode
= EX_IO
;
583 el
->el_tty
.t_vdisable
= _POSIX_VDISABLE
;
584 (void) memcpy(el
->el_tty
.t_t
, ttyperm
, sizeof(ttyperm_t
));
585 (void) memcpy(el
->el_tty
.t_c
, ttychar
, sizeof(ttychar_t
));
586 return (tty_setup(el
));
591 * Restore the tty to its original settings
595 tty_end(EditLine
*el
__attribute__((__unused__
)))
598 /* XXX: Maybe reset to an initial state? */
606 tty__getspeed(struct termios
*td
)
610 if ((spd
= cfgetispeed(td
)) == 0)
611 spd
= cfgetospeed(td
);
616 * Return the index of the asked char in the c_cc array
619 tty__getcharindex(int i
)
685 #endif /* VREPRINT */
689 #endif /* VDISCARD */
724 * Get the tty characters
727 tty__getchar(struct termios
*td
, unsigned char *s
)
731 s
[C_INTR
] = td
->c_cc
[VINTR
];
734 s
[C_QUIT
] = td
->c_cc
[VQUIT
];
737 s
[C_ERASE
] = td
->c_cc
[VERASE
];
740 s
[C_KILL
] = td
->c_cc
[VKILL
];
743 s
[C_EOF
] = td
->c_cc
[VEOF
];
746 s
[C_EOL
] = td
->c_cc
[VEOL
];
749 s
[C_EOL2
] = td
->c_cc
[VEOL2
];
752 s
[C_SWTCH
] = td
->c_cc
[VSWTCH
];
755 s
[C_DSWTCH
] = td
->c_cc
[VDSWTCH
];
758 s
[C_ERASE2
] = td
->c_cc
[VERASE2
];
761 s
[C_START
] = td
->c_cc
[VSTART
];
764 s
[C_STOP
] = td
->c_cc
[VSTOP
];
767 s
[C_WERASE
] = td
->c_cc
[VWERASE
];
770 s
[C_SUSP
] = td
->c_cc
[VSUSP
];
773 s
[C_DSUSP
] = td
->c_cc
[VDSUSP
];
776 s
[C_REPRINT
] = td
->c_cc
[VREPRINT
];
777 #endif /* VREPRINT */
779 s
[C_DISCARD
] = td
->c_cc
[VDISCARD
];
780 #endif /* VDISCARD */
782 s
[C_LNEXT
] = td
->c_cc
[VLNEXT
];
785 s
[C_STATUS
] = td
->c_cc
[VSTATUS
];
788 s
[C_PAGE
] = td
->c_cc
[VPAGE
];
791 s
[C_PGOFF
] = td
->c_cc
[VPGOFF
];
794 s
[C_KILL2
] = td
->c_cc
[VKILL2
];
797 s
[C_MIN
] = td
->c_cc
[VMIN
];
800 s
[C_TIME
] = td
->c_cc
[VTIME
];
806 * Set the tty characters
809 tty__setchar(struct termios
*td
, unsigned char *s
)
813 td
->c_cc
[VINTR
] = s
[C_INTR
];
816 td
->c_cc
[VQUIT
] = s
[C_QUIT
];
819 td
->c_cc
[VERASE
] = s
[C_ERASE
];
822 td
->c_cc
[VKILL
] = s
[C_KILL
];
825 td
->c_cc
[VEOF
] = s
[C_EOF
];
828 td
->c_cc
[VEOL
] = s
[C_EOL
];
831 td
->c_cc
[VEOL2
] = s
[C_EOL2
];
834 td
->c_cc
[VSWTCH
] = s
[C_SWTCH
];
837 td
->c_cc
[VDSWTCH
] = s
[C_DSWTCH
];
840 td
->c_cc
[VERASE2
] = s
[C_ERASE2
];
843 td
->c_cc
[VSTART
] = s
[C_START
];
846 td
->c_cc
[VSTOP
] = s
[C_STOP
];
849 td
->c_cc
[VWERASE
] = s
[C_WERASE
];
852 td
->c_cc
[VSUSP
] = s
[C_SUSP
];
855 td
->c_cc
[VDSUSP
] = s
[C_DSUSP
];
858 td
->c_cc
[VREPRINT
] = s
[C_REPRINT
];
859 #endif /* VREPRINT */
861 td
->c_cc
[VDISCARD
] = s
[C_DISCARD
];
862 #endif /* VDISCARD */
864 td
->c_cc
[VLNEXT
] = s
[C_LNEXT
];
867 td
->c_cc
[VSTATUS
] = s
[C_STATUS
];
870 td
->c_cc
[VPAGE
] = s
[C_PAGE
];
873 td
->c_cc
[VPGOFF
] = s
[C_PGOFF
];
876 td
->c_cc
[VKILL2
] = s
[C_KILL2
];
879 td
->c_cc
[VMIN
] = s
[C_MIN
];
882 td
->c_cc
[VTIME
] = s
[C_TIME
];
888 * Rebind the editline functions
891 tty_bind_char(EditLine
*el
, int force
)
894 unsigned char *t_n
= el
->el_tty
.t_c
[ED_IO
];
895 unsigned char *t_o
= el
->el_tty
.t_ed
.c_cc
;
898 el_action_t
*map
, *alt
;
899 const el_action_t
*dmap
, *dalt
;
900 new[1] = old
[1] = '\0';
902 map
= el
->el_map
.key
;
903 alt
= el
->el_map
.alt
;
904 if (el
->el_map
.type
== MAP_VI
) {
905 dmap
= el
->el_map
.vii
;
906 dalt
= el
->el_map
.vic
;
908 dmap
= el
->el_map
.emacs
;
912 for (tp
= tty_map
; tp
->nch
!= -1; tp
++) {
913 new[0] = t_n
[tp
->nch
];
914 old
[0] = t_o
[tp
->och
];
915 if (new[0] == old
[0] && !force
)
917 /* Put the old default binding back, and set the new binding */
918 key_clear(el
, map
, old
);
919 map
[UC(old
[0])] = dmap
[UC(old
[0])];
920 key_clear(el
, map
, new);
921 /* MAP_VI == 1, MAP_EMACS == 0... */
922 map
[UC(new[0])] = tp
->bind
[el
->el_map
.type
];
924 key_clear(el
, alt
, old
);
925 alt
[UC(old
[0])] = dalt
[UC(old
[0])];
926 key_clear(el
, alt
, new);
927 alt
[UC(new[0])] = tp
->bind
[el
->el_map
.type
+ 1];
934 * Set terminal into 1 character at a time mode.
937 tty_rawmode(EditLine
*el
)
940 if (el
->el_tty
.t_mode
== ED_IO
|| el
->el_tty
.t_mode
== QU_IO
)
943 if (el
->el_flags
& EDIT_DISABLED
)
946 if (tty_getty(el
, &el
->el_tty
.t_ts
) == -1) {
948 (void) fprintf(el
->el_errfile
, "tty_rawmode: tty_getty: %s\n",
950 #endif /* DEBUG_TTY */
954 * We always keep up with the eight bit setting and the speed of the
955 * tty. But we only believe changes that are made to cooked mode!
957 el
->el_tty
.t_eight
= tty__geteightbit(&el
->el_tty
.t_ts
);
958 el
->el_tty
.t_speed
= tty__getspeed(&el
->el_tty
.t_ts
);
960 if (tty__getspeed(&el
->el_tty
.t_ex
) != el
->el_tty
.t_speed
||
961 tty__getspeed(&el
->el_tty
.t_ed
) != el
->el_tty
.t_speed
) {
962 (void) cfsetispeed(&el
->el_tty
.t_ex
, el
->el_tty
.t_speed
);
963 (void) cfsetospeed(&el
->el_tty
.t_ex
, el
->el_tty
.t_speed
);
964 (void) cfsetispeed(&el
->el_tty
.t_ed
, el
->el_tty
.t_speed
);
965 (void) cfsetospeed(&el
->el_tty
.t_ed
, el
->el_tty
.t_speed
);
967 if (tty__cooked_mode(&el
->el_tty
.t_ts
)) {
968 if (el
->el_tty
.t_ts
.c_cflag
!= el
->el_tty
.t_ex
.c_cflag
) {
969 el
->el_tty
.t_ex
.c_cflag
=
970 el
->el_tty
.t_ts
.c_cflag
;
971 el
->el_tty
.t_ex
.c_cflag
&=
972 ~el
->el_tty
.t_t
[EX_IO
][MD_CTL
].t_clrmask
;
973 el
->el_tty
.t_ex
.c_cflag
|=
974 el
->el_tty
.t_t
[EX_IO
][MD_CTL
].t_setmask
;
976 el
->el_tty
.t_ed
.c_cflag
=
977 el
->el_tty
.t_ts
.c_cflag
;
978 el
->el_tty
.t_ed
.c_cflag
&=
979 ~el
->el_tty
.t_t
[ED_IO
][MD_CTL
].t_clrmask
;
980 el
->el_tty
.t_ed
.c_cflag
|=
981 el
->el_tty
.t_t
[ED_IO
][MD_CTL
].t_setmask
;
983 if ((el
->el_tty
.t_ts
.c_lflag
!= el
->el_tty
.t_ex
.c_lflag
) &&
984 (el
->el_tty
.t_ts
.c_lflag
!= el
->el_tty
.t_ed
.c_lflag
)) {
985 el
->el_tty
.t_ex
.c_lflag
=
986 el
->el_tty
.t_ts
.c_lflag
;
987 el
->el_tty
.t_ex
.c_lflag
&=
988 ~el
->el_tty
.t_t
[EX_IO
][MD_LIN
].t_clrmask
;
989 el
->el_tty
.t_ex
.c_lflag
|=
990 el
->el_tty
.t_t
[EX_IO
][MD_LIN
].t_setmask
;
992 el
->el_tty
.t_ed
.c_lflag
=
993 el
->el_tty
.t_ts
.c_lflag
;
994 el
->el_tty
.t_ed
.c_lflag
&=
995 ~el
->el_tty
.t_t
[ED_IO
][MD_LIN
].t_clrmask
;
996 el
->el_tty
.t_ed
.c_lflag
|=
997 el
->el_tty
.t_t
[ED_IO
][MD_LIN
].t_setmask
;
999 if ((el
->el_tty
.t_ts
.c_iflag
!= el
->el_tty
.t_ex
.c_iflag
) &&
1000 (el
->el_tty
.t_ts
.c_iflag
!= el
->el_tty
.t_ed
.c_iflag
)) {
1001 el
->el_tty
.t_ex
.c_iflag
=
1002 el
->el_tty
.t_ts
.c_iflag
;
1003 el
->el_tty
.t_ex
.c_iflag
&=
1004 ~el
->el_tty
.t_t
[EX_IO
][MD_INP
].t_clrmask
;
1005 el
->el_tty
.t_ex
.c_iflag
|=
1006 el
->el_tty
.t_t
[EX_IO
][MD_INP
].t_setmask
;
1008 el
->el_tty
.t_ed
.c_iflag
=
1009 el
->el_tty
.t_ts
.c_iflag
;
1010 el
->el_tty
.t_ed
.c_iflag
&=
1011 ~el
->el_tty
.t_t
[ED_IO
][MD_INP
].t_clrmask
;
1012 el
->el_tty
.t_ed
.c_iflag
|=
1013 el
->el_tty
.t_t
[ED_IO
][MD_INP
].t_setmask
;
1015 if ((el
->el_tty
.t_ts
.c_oflag
!= el
->el_tty
.t_ex
.c_oflag
) &&
1016 (el
->el_tty
.t_ts
.c_oflag
!= el
->el_tty
.t_ed
.c_oflag
)) {
1017 el
->el_tty
.t_ex
.c_oflag
=
1018 el
->el_tty
.t_ts
.c_oflag
;
1019 el
->el_tty
.t_ex
.c_oflag
&=
1020 ~el
->el_tty
.t_t
[EX_IO
][MD_OUT
].t_clrmask
;
1021 el
->el_tty
.t_ex
.c_oflag
|=
1022 el
->el_tty
.t_t
[EX_IO
][MD_OUT
].t_setmask
;
1024 el
->el_tty
.t_ed
.c_oflag
=
1025 el
->el_tty
.t_ts
.c_oflag
;
1026 el
->el_tty
.t_ed
.c_oflag
&=
1027 ~el
->el_tty
.t_t
[ED_IO
][MD_OUT
].t_clrmask
;
1028 el
->el_tty
.t_ed
.c_oflag
|=
1029 el
->el_tty
.t_t
[ED_IO
][MD_OUT
].t_setmask
;
1031 if (tty__gettabs(&el
->el_tty
.t_ex
) == 0)
1032 el
->el_tty
.t_tabs
= 0;
1034 el
->el_tty
.t_tabs
= EL_CAN_TAB
? 1 : 0;
1039 tty__getchar(&el
->el_tty
.t_ts
, el
->el_tty
.t_c
[TS_IO
]);
1041 * Check if the user made any changes.
1042 * If he did, then propagate the changes to the
1043 * edit and execute data structures.
1045 for (i
= 0; i
< C_NCC
; i
++)
1046 if (el
->el_tty
.t_c
[TS_IO
][i
] !=
1047 el
->el_tty
.t_c
[EX_IO
][i
])
1052 * Propagate changes only to the unprotected
1053 * chars that have been modified just now.
1055 for (i
= 0; i
< C_NCC
; i
++) {
1056 if (!((el
->el_tty
.t_t
[ED_IO
][MD_CHAR
].t_setmask
& C_SH(i
)))
1057 && (el
->el_tty
.t_c
[TS_IO
][i
] != el
->el_tty
.t_c
[EX_IO
][i
]))
1058 el
->el_tty
.t_c
[ED_IO
][i
] = el
->el_tty
.t_c
[TS_IO
][i
];
1059 if (el
->el_tty
.t_t
[ED_IO
][MD_CHAR
].t_clrmask
& C_SH(i
))
1060 el
->el_tty
.t_c
[ED_IO
][i
] = el
->el_tty
.t_vdisable
;
1062 tty_bind_char(el
, 0);
1063 tty__setchar(&el
->el_tty
.t_ed
, el
->el_tty
.t_c
[ED_IO
]);
1065 for (i
= 0; i
< C_NCC
; i
++) {
1066 if (!((el
->el_tty
.t_t
[EX_IO
][MD_CHAR
].t_setmask
& C_SH(i
)))
1067 && (el
->el_tty
.t_c
[TS_IO
][i
] != el
->el_tty
.t_c
[EX_IO
][i
]))
1068 el
->el_tty
.t_c
[EX_IO
][i
] = el
->el_tty
.t_c
[TS_IO
][i
];
1069 if (el
->el_tty
.t_t
[EX_IO
][MD_CHAR
].t_clrmask
& C_SH(i
))
1070 el
->el_tty
.t_c
[EX_IO
][i
] = el
->el_tty
.t_vdisable
;
1072 tty__setchar(&el
->el_tty
.t_ex
, el
->el_tty
.t_c
[EX_IO
]);
1076 if (tty_setty(el
, TCSADRAIN
, &el
->el_tty
.t_ed
) == -1) {
1078 (void) fprintf(el
->el_errfile
, "tty_rawmode: tty_setty: %s\n",
1080 #endif /* DEBUG_TTY */
1083 el
->el_tty
.t_mode
= ED_IO
;
1088 /* tty_cookedmode():
1089 * Set the tty back to normal mode
1092 tty_cookedmode(EditLine
*el
)
1093 { /* set tty in normal setup */
1095 if (el
->el_tty
.t_mode
== EX_IO
)
1098 if (el
->el_flags
& EDIT_DISABLED
)
1101 if (tty_setty(el
, TCSADRAIN
, &el
->el_tty
.t_ex
) == -1) {
1103 (void) fprintf(el
->el_errfile
,
1104 "tty_cookedmode: tty_setty: %s\n",
1106 #endif /* DEBUG_TTY */
1109 el
->el_tty
.t_mode
= EX_IO
;
1115 * Turn on quote mode
1118 tty_quotemode(EditLine
*el
)
1120 if (el
->el_tty
.t_mode
== QU_IO
)
1123 el
->el_tty
.t_qu
= el
->el_tty
.t_ed
;
1125 el
->el_tty
.t_qu
.c_iflag
&= ~el
->el_tty
.t_t
[QU_IO
][MD_INP
].t_clrmask
;
1126 el
->el_tty
.t_qu
.c_iflag
|= el
->el_tty
.t_t
[QU_IO
][MD_INP
].t_setmask
;
1128 el
->el_tty
.t_qu
.c_oflag
&= ~el
->el_tty
.t_t
[QU_IO
][MD_OUT
].t_clrmask
;
1129 el
->el_tty
.t_qu
.c_oflag
|= el
->el_tty
.t_t
[QU_IO
][MD_OUT
].t_setmask
;
1131 el
->el_tty
.t_qu
.c_cflag
&= ~el
->el_tty
.t_t
[QU_IO
][MD_CTL
].t_clrmask
;
1132 el
->el_tty
.t_qu
.c_cflag
|= el
->el_tty
.t_t
[QU_IO
][MD_CTL
].t_setmask
;
1134 el
->el_tty
.t_qu
.c_lflag
&= ~el
->el_tty
.t_t
[QU_IO
][MD_LIN
].t_clrmask
;
1135 el
->el_tty
.t_qu
.c_lflag
|= el
->el_tty
.t_t
[QU_IO
][MD_LIN
].t_setmask
;
1137 if (tty_setty(el
, TCSADRAIN
, &el
->el_tty
.t_qu
) == -1) {
1139 (void) fprintf(el
->el_errfile
, "QuoteModeOn: tty_setty: %s\n",
1141 #endif /* DEBUG_TTY */
1144 el
->el_tty
.t_mode
= QU_IO
;
1149 /* tty_noquotemode():
1150 * Turn off quote mode
1153 tty_noquotemode(EditLine
*el
)
1156 if (el
->el_tty
.t_mode
!= QU_IO
)
1158 if (tty_setty(el
, TCSADRAIN
, &el
->el_tty
.t_ed
) == -1) {
1160 (void) fprintf(el
->el_errfile
, "QuoteModeOff: tty_setty: %s\n",
1162 #endif /* DEBUG_TTY */
1165 el
->el_tty
.t_mode
= ED_IO
;
1175 tty_stty(EditLine
*el
, int argc
__attribute__((__unused__
)), const Char
**argv
)
1177 const ttymodes_t
*m
;
1181 char name
[EL_BUFSIZ
];
1182 struct termios
*tios
= &el
->el_tty
.t_ex
;
1187 strncpy(name
, ct_encode_string(*argv
++, &el
->el_scratch
), sizeof(name
));
1188 name
[sizeof(name
) - 1] = '\0';
1190 while (argv
&& *argv
&& argv
[0][0] == '-' && argv
[0][2] == '\0')
1191 switch (argv
[0][1]) {
1198 tios
= &el
->el_tty
.t_ed
;
1203 tios
= &el
->el_tty
.t_ex
;
1208 tios
= &el
->el_tty
.t_ts
;
1212 (void) fprintf(el
->el_errfile
,
1213 "%s: Unknown switch `%c'.\n",
1218 if (!argv
|| !*argv
) {
1220 size_t len
= 0, st
= 0, cu
;
1221 for (m
= ttymodes
; m
->m_name
; m
++) {
1222 if (m
->m_type
!= i
) {
1223 (void) fprintf(el
->el_outfile
, "%s%s",
1224 i
!= -1 ? "\n" : "",
1225 el
->el_tty
.t_t
[z
][m
->m_type
].t_name
);
1228 strlen(el
->el_tty
.t_t
[z
][m
->m_type
].t_name
);
1231 x
= (el
->el_tty
.t_t
[z
][i
].t_setmask
& m
->m_value
)
1233 x
= (el
->el_tty
.t_t
[z
][i
].t_clrmask
& m
->m_value
)
1239 if (x
!= '\0' || aflag
) {
1241 cu
= strlen(m
->m_name
) + (x
!= '\0') + 1;
1243 if (len
+ cu
>= (size_t)el
->el_term
.t_size
.h
) {
1244 (void) fprintf(el
->el_outfile
, "\n%*s",
1251 (void) fprintf(el
->el_outfile
, "%c%s ",
1254 (void) fprintf(el
->el_outfile
, "%s ",
1258 (void) fprintf(el
->el_outfile
, "\n");
1261 while (argv
&& (s
= *argv
++)) {
1274 for (m
= ttymodes
; m
->m_name
; m
++)
1275 if ((p
? strncmp(m
->m_name
, ct_encode_string(d
, &el
->el_scratch
), (size_t)(p
- d
)) :
1276 strcmp(m
->m_name
, ct_encode_string(d
, &el
->el_scratch
))) == 0 &&
1277 (p
== NULL
|| m
->m_type
== MD_CHAR
))
1281 (void) fprintf(el
->el_errfile
,
1282 "%s: Invalid argument `" FSTR
"'.\n", name
, d
);
1286 int c
= ffs((int)m
->m_value
);
1287 int v
= *++p
? parse__escape(&p
) :
1288 el
->el_tty
.t_vdisable
;
1291 c
= tty__getcharindex(c
);
1298 el
->el_tty
.t_t
[z
][m
->m_type
].t_setmask
|= m
->m_value
;
1299 el
->el_tty
.t_t
[z
][m
->m_type
].t_clrmask
&= ~m
->m_value
;
1302 el
->el_tty
.t_t
[z
][m
->m_type
].t_setmask
&= ~m
->m_value
;
1303 el
->el_tty
.t_t
[z
][m
->m_type
].t_clrmask
|= m
->m_value
;
1306 el
->el_tty
.t_t
[z
][m
->m_type
].t_setmask
&= ~m
->m_value
;
1307 el
->el_tty
.t_t
[z
][m
->m_type
].t_clrmask
&= ~m
->m_value
;
1312 if (el
->el_tty
.t_mode
== z
) {
1313 if (tty_setty(el
, TCSADRAIN
, tios
) == -1) {
1315 (void) fprintf(el
->el_errfile
,
1316 "tty_stty: tty_setty: %s\n", strerror(errno
));
1317 #endif /* DEBUG_TTY */
1328 * DEbugging routine to print the tty characters
1331 tty_printchar(EditLine
*el
, unsigned char *s
)
1336 for (i
= 0; i
< C_NCC
; i
++) {
1337 for (m
= el
->el_tty
.t_t
; m
->m_name
; m
++)
1338 if (m
->m_type
== MD_CHAR
&& C_SH(i
) == m
->m_value
)
1341 (void) fprintf(el
->el_errfile
, "%s ^%c ",
1342 m
->m_name
, s
[i
] + 'A' - 1);
1344 (void) fprintf(el
->el_errfile
, "\n");
1346 (void) fprintf(el
->el_errfile
, "\n");