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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $NetBSD: tty.c,v 1.25 2006/03/18 09:09:41 christos Exp $
35 #if !defined(lint) && !defined(SCCSID)
36 static char sccsid[] = "@(#)tty.c 8.1 (Berkeley) 6/4/93";
37 #endif /* not lint && not SCCSID */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * tty.c: tty interface stuff
49 typedef struct ttymodes_t {
55 typedef struct ttymap_t {
56 int nch, och; /* Internal and termio rep of chars */
57 el_action_t bind[3]; /* emacs, vi, and vi-cmd */
61 private const ttyperm_t ttyperm = {
63 {"iflag:", ICRNL, (INLCR | IGNCR)},
64 {"oflag:", (OPOST | ONLCR), ONLRET},
66 {"lflag:", (ISIG | ICANON | ECHO | ECHOE | ECHOCTL | IEXTEN),
67 (NOFLSH | ECHONL | EXTPROC | FLUSHO)},
71 {"iflag:", (INLCR | ICRNL), IGNCR},
72 {"oflag:", (OPOST | ONLCR), ONLRET},
75 (NOFLSH | ICANON | ECHO | ECHOK | ECHONL | EXTPROC | IEXTEN | FLUSHO)},
76 {"chars:", (C_SH(C_MIN) | C_SH(C_TIME) | C_SH(C_SWTCH) | C_SH(C_DSWTCH) |
77 C_SH(C_SUSP) | C_SH(C_DSUSP) | C_SH(C_EOL) | C_SH(C_DISCARD) |
78 C_SH(C_PGOFF) | C_SH(C_PAGE) | C_SH(C_STATUS)), 0}
81 {"iflag:", 0, IXON | IXOFF | INLCR | ICRNL},
84 {"lflag:", 0, ISIG | IEXTEN},
89 private const ttychar_t ttychar = {
91 CINTR, CQUIT, CERASE, CKILL,
92 CEOF, CEOL, CEOL2, CSWTCH,
93 CDSWTCH, CERASE2, CSTART, CSTOP,
94 CWERASE, CSUSP, CDSUSP, CREPRINT,
95 CDISCARD, CLNEXT, CSTATUS, CPAGE,
96 CPGOFF, CKILL2, CBRK, CMIN,
100 CINTR, CQUIT, CERASE, CKILL,
101 _POSIX_VDISABLE, _POSIX_VDISABLE, _POSIX_VDISABLE, _POSIX_VDISABLE,
102 _POSIX_VDISABLE, CERASE2, CSTART, CSTOP,
103 _POSIX_VDISABLE, CSUSP, _POSIX_VDISABLE, _POSIX_VDISABLE,
104 CDISCARD, _POSIX_VDISABLE, _POSIX_VDISABLE, _POSIX_VDISABLE,
105 _POSIX_VDISABLE, _POSIX_VDISABLE, _POSIX_VDISABLE, 1,
119 private const ttymap_t tty_map[] = {
122 {EM_DELETE_PREV_CHAR, VI_DELETE_PREV_CHAR, ED_PREV_CHAR}},
126 {EM_DELETE_PREV_CHAR, VI_DELETE_PREV_CHAR, ED_PREV_CHAR}},
130 {EM_KILL_LINE, VI_KILL_LINE_PREV, ED_UNASSIGNED}},
134 {EM_KILL_LINE, VI_KILL_LINE_PREV, ED_UNASSIGNED}},
138 {EM_DELETE_OR_LIST, VI_LIST_OR_EOF, ED_UNASSIGNED}},
142 {ED_DELETE_PREV_WORD, ED_DELETE_PREV_WORD, ED_PREV_WORD}},
145 {C_REPRINT, VREPRINT,
146 {ED_REDISPLAY, ED_INSERT, ED_REDISPLAY}},
147 #endif /* VREPRINT */
150 {ED_QUOTED_INSERT, ED_QUOTED_INSERT, ED_UNASSIGNED}},
153 {ED_UNASSIGNED, ED_UNASSIGNED, ED_UNASSIGNED}}
156 private const ttymodes_t ttymodes[] = {
158 {"ignbrk", IGNBRK, MD_INP},
161 {"brkint", BRKINT, MD_INP},
164 {"ignpar", IGNPAR, MD_INP},
167 {"parmrk", PARMRK, MD_INP},
170 {"inpck", INPCK, MD_INP},
173 {"istrip", ISTRIP, MD_INP},
176 {"inlcr", INLCR, MD_INP},
179 {"igncr", IGNCR, MD_INP},
182 {"icrnl", ICRNL, MD_INP},
185 {"iuclc", IUCLC, MD_INP},
188 {"ixon", IXON, MD_INP},
191 {"ixany", IXANY, MD_INP},
194 {"ixoff", IXOFF, MD_INP},
197 {"imaxbel", IMAXBEL, MD_INP},
201 {"opost", OPOST, MD_OUT},
204 {"olcuc", OLCUC, MD_OUT},
207 {"onlcr", ONLCR, MD_OUT},
210 {"ocrnl", OCRNL, MD_OUT},
213 {"onocr", ONOCR, MD_OUT},
216 {"onoeot", ONOEOT, MD_OUT},
219 {"onlret", ONLRET, MD_OUT},
222 {"ofill", OFILL, MD_OUT},
225 {"ofdel", OFDEL, MD_OUT},
228 {"nldly", NLDLY, MD_OUT},
231 {"crdly", CRDLY, MD_OUT},
234 {"tabdly", TABDLY, MD_OUT},
237 {"xtabs", XTABS, MD_OUT},
240 {"bsdly", BSDLY, MD_OUT},
243 {"vtdly", VTDLY, MD_OUT},
246 {"ffdly", FFDLY, MD_OUT},
249 {"pageout", PAGEOUT, MD_OUT},
252 {"wrap", WRAP, MD_OUT},
256 {"cignore", CIGNORE, MD_CTL},
259 {"cbaud", CBAUD, MD_CTL},
262 {"cstopb", CSTOPB, MD_CTL},
265 {"cread", CREAD, MD_CTL},
268 {"parenb", PARENB, MD_CTL},
271 {"parodd", PARODD, MD_CTL},
274 {"hupcl", HUPCL, MD_CTL},
277 {"clocal", CLOCAL, MD_CTL},
280 {"loblk", LOBLK, MD_CTL},
283 {"cibaud", CIBAUD, MD_CTL},
287 {"ccts_oflow", CCTS_OFLOW, MD_CTL},
289 {"crtscts", CRTSCTS, MD_CTL},
290 #endif /* CCTS_OFLOW */
293 {"crts_iflow", CRTS_IFLOW, MD_CTL},
294 #endif /* CRTS_IFLOW */
296 {"cdtrcts", CDTRCTS, MD_CTL},
299 {"mdmbuf", MDMBUF, MD_CTL},
302 {"rcv1en", RCV1EN, MD_CTL},
305 {"xmt1en", XMT1EN, MD_CTL},
309 {"isig", ISIG, MD_LIN},
312 {"icanon", ICANON, MD_LIN},
315 {"xcase", XCASE, MD_LIN},
318 {"echo", ECHO, MD_LIN},
321 {"echoe", ECHOE, MD_LIN},
324 {"echok", ECHOK, MD_LIN},
327 {"echonl", ECHONL, MD_LIN},
330 {"noflsh", NOFLSH, MD_LIN},
333 {"tostop", TOSTOP, MD_LIN},
336 {"echoctl", ECHOCTL, MD_LIN},
339 {"echoprt", ECHOPRT, MD_LIN},
342 {"echoke", ECHOKE, MD_LIN},
345 {"defecho", DEFECHO, MD_LIN},
348 {"flusho", FLUSHO, MD_LIN},
351 {"pendin", PENDIN, MD_LIN},
354 {"iexten", IEXTEN, MD_LIN},
357 {"nokerninfo", NOKERNINFO, MD_LIN},
358 #endif /* NOKERNINFO */
360 {"altwerase", ALTWERASE, MD_LIN},
361 #endif /* ALTWERASE */
363 {"extproc", EXTPROC, MD_LIN},
367 {"intr", C_SH(C_INTR), MD_CHAR},
370 {"quit", C_SH(C_QUIT), MD_CHAR},
373 {"erase", C_SH(C_ERASE), MD_CHAR},
376 {"kill", C_SH(C_KILL), MD_CHAR},
379 {"eof", C_SH(C_EOF), MD_CHAR},
382 {"eol", C_SH(C_EOL), MD_CHAR},
385 {"eol2", C_SH(C_EOL2), MD_CHAR},
388 {"swtch", C_SH(C_SWTCH), MD_CHAR},
391 {"dswtch", C_SH(C_DSWTCH), MD_CHAR},
394 {"erase2", C_SH(C_ERASE2), MD_CHAR},
397 {"start", C_SH(C_START), MD_CHAR},
400 {"stop", C_SH(C_STOP), MD_CHAR},
403 {"werase", C_SH(C_WERASE), MD_CHAR},
406 {"susp", C_SH(C_SUSP), MD_CHAR},
409 {"dsusp", C_SH(C_DSUSP), MD_CHAR},
411 #if defined(VREPRINT)
412 {"reprint", C_SH(C_REPRINT), MD_CHAR},
413 #endif /* VREPRINT */
414 #if defined(VDISCARD)
415 {"discard", C_SH(C_DISCARD), MD_CHAR},
416 #endif /* VDISCARD */
418 {"lnext", C_SH(C_LNEXT), MD_CHAR},
421 {"status", C_SH(C_STATUS), MD_CHAR},
424 {"page", C_SH(C_PAGE), MD_CHAR},
427 {"pgoff", C_SH(C_PGOFF), MD_CHAR},
430 {"kill2", C_SH(C_KILL2), MD_CHAR},
433 {"brk", C_SH(C_BRK), MD_CHAR},
436 {"min", C_SH(C_MIN), MD_CHAR},
439 {"time", C_SH(C_TIME), MD_CHAR},
446 #define tty_getty(el, td) tcgetattr((el)->el_infd, (td))
447 #define tty_setty(el, td) tcsetattr((el)->el_infd, TCSADRAIN, (td))
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__getcharindex(int);
454 private void tty__getchar(struct termios *, unsigned char *);
455 private void tty__setchar(struct termios *, unsigned char *);
456 private speed_t tty__getspeed(struct termios *);
457 private int tty_setup(EditLine *);
463 * Get the tty parameters and initialize the editing state
466 tty_setup(EditLine *el)
470 if (el->el_flags & EDIT_DISABLED)
473 if (tty_getty(el, &el->el_tty.t_ed) == -1) {
475 (void) fprintf(el->el_errfile,
476 "tty_setup: tty_getty: %s\n", strerror(errno));
477 #endif /* DEBUG_TTY */
480 el->el_tty.t_ts = el->el_tty.t_ex = el->el_tty.t_ed;
482 el->el_tty.t_speed = tty__getspeed(&el->el_tty.t_ex);
483 el->el_tty.t_tabs = tty__gettabs(&el->el_tty.t_ex);
484 el->el_tty.t_eight = tty__geteightbit(&el->el_tty.t_ex);
487 * Reset the tty chars to reasonable defaults
488 * If they are disabled, then enable them.
491 if (tty__cooked_mode(&el->el_tty.t_ts)) {
492 tty__getchar(&el->el_tty.t_ts, el->el_tty.t_c[TS_IO]);
494 * Don't affect CMIN and CTIME for the editor mode
496 for (rst = 0; rst < C_NCC - 2; rst++)
497 if (el->el_tty.t_c[TS_IO][rst] !=
498 el->el_tty.t_vdisable
499 && el->el_tty.t_c[ED_IO][rst] !=
500 el->el_tty.t_vdisable)
501 el->el_tty.t_c[ED_IO][rst] =
502 el->el_tty.t_c[TS_IO][rst];
503 for (rst = 0; rst < C_NCC; rst++)
504 if (el->el_tty.t_c[TS_IO][rst] !=
505 el->el_tty.t_vdisable)
506 el->el_tty.t_c[EX_IO][rst] =
507 el->el_tty.t_c[TS_IO][rst];
511 el->el_tty.t_ed.c_iflag &= ~el->el_tty.t_t[ED_IO][MD_INP].t_clrmask;
512 el->el_tty.t_ed.c_iflag |= el->el_tty.t_t[ED_IO][MD_INP].t_setmask;
514 el->el_tty.t_ed.c_oflag &= ~el->el_tty.t_t[ED_IO][MD_OUT].t_clrmask;
515 el->el_tty.t_ed.c_oflag |= el->el_tty.t_t[ED_IO][MD_OUT].t_setmask;
517 el->el_tty.t_ed.c_cflag &= ~el->el_tty.t_t[ED_IO][MD_CTL].t_clrmask;
518 el->el_tty.t_ed.c_cflag |= el->el_tty.t_t[ED_IO][MD_CTL].t_setmask;
520 el->el_tty.t_ed.c_lflag &= ~el->el_tty.t_t[ED_IO][MD_LIN].t_clrmask;
521 el->el_tty.t_ed.c_lflag |= el->el_tty.t_t[ED_IO][MD_LIN].t_setmask;
523 tty__setchar(&el->el_tty.t_ed, el->el_tty.t_c[ED_IO]);
524 tty_bind_char(el, 1);
529 tty_init(EditLine *el)
532 el->el_tty.t_mode = EX_IO;
533 el->el_tty.t_vdisable = _POSIX_VDISABLE;
534 (void) memcpy(el->el_tty.t_t, ttyperm, sizeof(ttyperm_t));
535 (void) memcpy(el->el_tty.t_c, ttychar, sizeof(ttychar_t));
536 return (tty_setup(el));
541 * Restore the tty to its original settings
545 tty_end(EditLine *el __unused)
548 /* XXX: Maybe reset to an initial state? */
556 tty__getspeed(struct termios *td)
560 if ((spd = cfgetispeed(td)) == 0)
561 spd = cfgetospeed(td);
566 * Return the index of the asked char in the c_cc array
569 tty__getcharindex(int i)
635 #endif /* VREPRINT */
639 #endif /* VDISCARD */
674 * Get the tty characters
677 tty__getchar(struct termios *td, unsigned char *s)
681 s[C_INTR] = td->c_cc[VINTR];
684 s[C_QUIT] = td->c_cc[VQUIT];
687 s[C_ERASE] = td->c_cc[VERASE];
690 s[C_KILL] = td->c_cc[VKILL];
693 s[C_EOF] = td->c_cc[VEOF];
696 s[C_EOL] = td->c_cc[VEOL];
699 s[C_EOL2] = td->c_cc[VEOL2];
702 s[C_SWTCH] = td->c_cc[VSWTCH];
705 s[C_DSWTCH] = td->c_cc[VDSWTCH];
708 s[C_ERASE2] = td->c_cc[VERASE2];
711 s[C_START] = td->c_cc[VSTART];
714 s[C_STOP] = td->c_cc[VSTOP];
717 s[C_WERASE] = td->c_cc[VWERASE];
720 s[C_SUSP] = td->c_cc[VSUSP];
723 s[C_DSUSP] = td->c_cc[VDSUSP];
726 s[C_REPRINT] = td->c_cc[VREPRINT];
727 #endif /* VREPRINT */
729 s[C_DISCARD] = td->c_cc[VDISCARD];
730 #endif /* VDISCARD */
732 s[C_LNEXT] = td->c_cc[VLNEXT];
735 s[C_STATUS] = td->c_cc[VSTATUS];
738 s[C_PAGE] = td->c_cc[VPAGE];
741 s[C_PGOFF] = td->c_cc[VPGOFF];
744 s[C_KILL2] = td->c_cc[VKILL2];
747 s[C_MIN] = td->c_cc[VMIN];
750 s[C_TIME] = td->c_cc[VTIME];
756 * Set the tty characters
759 tty__setchar(struct termios *td, unsigned char *s)
763 td->c_cc[VINTR] = s[C_INTR];
766 td->c_cc[VQUIT] = s[C_QUIT];
769 td->c_cc[VERASE] = s[C_ERASE];
772 td->c_cc[VKILL] = s[C_KILL];
775 td->c_cc[VEOF] = s[C_EOF];
778 td->c_cc[VEOL] = s[C_EOL];
781 td->c_cc[VEOL2] = s[C_EOL2];
784 td->c_cc[VSWTCH] = s[C_SWTCH];
787 td->c_cc[VDSWTCH] = s[C_DSWTCH];
790 td->c_cc[VERASE2] = s[C_ERASE2];
793 td->c_cc[VSTART] = s[C_START];
796 td->c_cc[VSTOP] = s[C_STOP];
799 td->c_cc[VWERASE] = s[C_WERASE];
802 td->c_cc[VSUSP] = s[C_SUSP];
805 td->c_cc[VDSUSP] = s[C_DSUSP];
808 td->c_cc[VREPRINT] = s[C_REPRINT];
809 #endif /* VREPRINT */
811 td->c_cc[VDISCARD] = s[C_DISCARD];
812 #endif /* VDISCARD */
814 td->c_cc[VLNEXT] = s[C_LNEXT];
817 td->c_cc[VSTATUS] = s[C_STATUS];
820 td->c_cc[VPAGE] = s[C_PAGE];
823 td->c_cc[VPGOFF] = s[C_PGOFF];
826 td->c_cc[VKILL2] = s[C_KILL2];
829 td->c_cc[VMIN] = s[C_MIN];
832 td->c_cc[VTIME] = s[C_TIME];
838 * Rebind the editline functions
841 tty_bind_char(EditLine *el, int force)
844 unsigned char *t_n = el->el_tty.t_c[ED_IO];
845 unsigned char *t_o = el->el_tty.t_ed.c_cc;
846 unsigned char new[2], old[2];
848 el_action_t *map, *alt;
849 const el_action_t *dmap, *dalt;
850 new[1] = old[1] = '\0';
852 map = el->el_map.key;
853 alt = el->el_map.alt;
854 if (el->el_map.type == MAP_VI) {
855 dmap = el->el_map.vii;
856 dalt = el->el_map.vic;
858 dmap = el->el_map.emacs;
862 for (tp = tty_map; tp->nch != -1; tp++) {
863 new[0] = t_n[tp->nch];
864 old[0] = t_o[tp->och];
865 if (new[0] == old[0] && !force)
867 /* Put the old default binding back, and set the new binding */
868 key_clear(el, map, (char *)old);
869 map[old[0]] = dmap[old[0]];
870 key_clear(el, map, (char *)new);
871 /* MAP_VI == 1, MAP_EMACS == 0... */
872 map[new[0]] = tp->bind[el->el_map.type];
874 key_clear(el, alt, (char *)old);
875 alt[old[0]] = dalt[old[0]];
876 key_clear(el, alt, (char *)new);
877 alt[new[0]] = tp->bind[el->el_map.type + 1];
884 * Set terminal into 1 character at a time mode.
887 tty_rawmode(EditLine *el)
890 if (el->el_tty.t_mode == ED_IO || el->el_tty.t_mode == QU_IO)
893 if (el->el_flags & EDIT_DISABLED)
896 if (tty_getty(el, &el->el_tty.t_ts) == -1) {
898 (void) fprintf(el->el_errfile, "tty_rawmode: tty_getty: %s\n",
900 #endif /* DEBUG_TTY */
904 * We always keep up with the eight bit setting and the speed of the
905 * tty. But only we only believe changes that are made to cooked mode!
907 el->el_tty.t_eight = tty__geteightbit(&el->el_tty.t_ts);
908 el->el_tty.t_speed = tty__getspeed(&el->el_tty.t_ts);
910 if (tty__getspeed(&el->el_tty.t_ed) != el->el_tty.t_speed) {
911 (void) cfsetispeed(&el->el_tty.t_ed, el->el_tty.t_speed);
912 (void) cfsetospeed(&el->el_tty.t_ed, el->el_tty.t_speed);
914 if (tty__cooked_mode(&el->el_tty.t_ts)) {
915 if ((el->el_tty.t_ts.c_cflag != el->el_tty.t_ex.c_cflag) &&
916 (el->el_tty.t_ts.c_cflag != el->el_tty.t_ed.c_cflag)) {
917 el->el_tty.t_ed.c_cflag =
918 el->el_tty.t_ts.c_cflag;
919 el->el_tty.t_ed.c_cflag &=
920 ~el->el_tty.t_t[ED_IO][MD_CTL].t_clrmask;
921 el->el_tty.t_ed.c_cflag |=
922 el->el_tty.t_t[ED_IO][MD_CTL].t_setmask;
924 if ((el->el_tty.t_ts.c_lflag != el->el_tty.t_ex.c_lflag) &&
925 (el->el_tty.t_ts.c_lflag != el->el_tty.t_ed.c_lflag)) {
926 el->el_tty.t_ed.c_lflag =
927 el->el_tty.t_ts.c_lflag;
928 el->el_tty.t_ed.c_lflag &=
929 ~el->el_tty.t_t[ED_IO][MD_LIN].t_clrmask;
930 el->el_tty.t_ed.c_lflag |=
931 el->el_tty.t_t[ED_IO][MD_LIN].t_setmask;
933 if ((el->el_tty.t_ts.c_iflag != el->el_tty.t_ex.c_iflag) &&
934 (el->el_tty.t_ts.c_iflag != el->el_tty.t_ed.c_iflag)) {
935 el->el_tty.t_ed.c_iflag =
936 el->el_tty.t_ts.c_iflag;
937 el->el_tty.t_ed.c_iflag &=
938 ~el->el_tty.t_t[ED_IO][MD_INP].t_clrmask;
939 el->el_tty.t_ed.c_iflag |=
940 el->el_tty.t_t[ED_IO][MD_INP].t_setmask;
942 if ((el->el_tty.t_ts.c_oflag != el->el_tty.t_ex.c_oflag) &&
943 (el->el_tty.t_ts.c_oflag != el->el_tty.t_ed.c_oflag)) {
944 el->el_tty.t_ed.c_oflag =
945 el->el_tty.t_ts.c_oflag;
946 el->el_tty.t_ed.c_oflag &=
947 ~el->el_tty.t_t[ED_IO][MD_OUT].t_clrmask;
948 el->el_tty.t_ed.c_oflag |=
949 el->el_tty.t_t[ED_IO][MD_OUT].t_setmask;
951 if (tty__gettabs(&el->el_tty.t_ex) == 0)
952 el->el_tty.t_tabs = 0;
954 el->el_tty.t_tabs = EL_CAN_TAB ? 1 : 0;
959 tty__getchar(&el->el_tty.t_ts, el->el_tty.t_c[TS_IO]);
961 * Check if the user made any changes.
962 * If he did, then propagate the changes to the
963 * edit and execute data structures.
965 for (i = 0; i < C_NCC; i++)
966 if (el->el_tty.t_c[TS_IO][i] !=
967 el->el_tty.t_c[EX_IO][i])
972 * Propagate changes only to the unprotected
973 * chars that have been modified just now.
975 for (i = 0; i < C_NCC; i++) {
976 if (!((el->el_tty.t_t[ED_IO][MD_CHAR].t_setmask & C_SH(i)))
977 && (el->el_tty.t_c[TS_IO][i] != el->el_tty.t_c[EX_IO][i]))
978 el->el_tty.t_c[ED_IO][i] = el->el_tty.t_c[TS_IO][i];
979 if (el->el_tty.t_t[ED_IO][MD_CHAR].t_clrmask & C_SH(i))
980 el->el_tty.t_c[ED_IO][i] = el->el_tty.t_vdisable;
982 tty_bind_char(el, 0);
983 tty__setchar(&el->el_tty.t_ed, el->el_tty.t_c[ED_IO]);
985 for (i = 0; i < C_NCC; i++) {
986 if (!((el->el_tty.t_t[EX_IO][MD_CHAR].t_setmask & C_SH(i)))
987 && (el->el_tty.t_c[TS_IO][i] != el->el_tty.t_c[EX_IO][i]))
988 el->el_tty.t_c[EX_IO][i] = el->el_tty.t_c[TS_IO][i];
989 if (el->el_tty.t_t[EX_IO][MD_CHAR].t_clrmask & C_SH(i))
990 el->el_tty.t_c[EX_IO][i] = el->el_tty.t_vdisable;
996 if (el->el_tty.t_mode == EX_IO)
997 el->el_tty.t_ex = el->el_tty.t_ts;
999 if (tty_setty(el, &el->el_tty.t_ed) == -1) {
1001 (void) fprintf(el->el_errfile, "tty_rawmode: tty_setty: %s\n",
1003 #endif /* DEBUG_TTY */
1006 el->el_tty.t_mode = ED_IO;
1011 /* tty_cookedmode():
1012 * Set the tty back to normal mode
1015 tty_cookedmode(EditLine *el)
1016 { /* set tty in normal setup */
1018 if (el->el_tty.t_mode == EX_IO)
1021 if (el->el_flags & EDIT_DISABLED)
1024 if (tty_setty(el, &el->el_tty.t_ex) == -1) {
1026 (void) fprintf(el->el_errfile,
1027 "tty_cookedmode: tty_setty: %s\n",
1029 #endif /* DEBUG_TTY */
1032 el->el_tty.t_mode = EX_IO;
1038 * Turn on quote mode
1041 tty_quotemode(EditLine *el)
1043 if (el->el_tty.t_mode == QU_IO)
1046 el->el_tty.t_qu = el->el_tty.t_ed;
1048 el->el_tty.t_qu.c_iflag &= ~el->el_tty.t_t[QU_IO][MD_INP].t_clrmask;
1049 el->el_tty.t_qu.c_iflag |= el->el_tty.t_t[QU_IO][MD_INP].t_setmask;
1051 el->el_tty.t_qu.c_oflag &= ~el->el_tty.t_t[QU_IO][MD_OUT].t_clrmask;
1052 el->el_tty.t_qu.c_oflag |= el->el_tty.t_t[QU_IO][MD_OUT].t_setmask;
1054 el->el_tty.t_qu.c_cflag &= ~el->el_tty.t_t[QU_IO][MD_CTL].t_clrmask;
1055 el->el_tty.t_qu.c_cflag |= el->el_tty.t_t[QU_IO][MD_CTL].t_setmask;
1057 el->el_tty.t_qu.c_lflag &= ~el->el_tty.t_t[QU_IO][MD_LIN].t_clrmask;
1058 el->el_tty.t_qu.c_lflag |= el->el_tty.t_t[QU_IO][MD_LIN].t_setmask;
1060 if (tty_setty(el, &el->el_tty.t_qu) == -1) {
1062 (void) fprintf(el->el_errfile, "QuoteModeOn: tty_setty: %s\n",
1064 #endif /* DEBUG_TTY */
1067 el->el_tty.t_mode = QU_IO;
1072 /* tty_noquotemode():
1073 * Turn off quote mode
1076 tty_noquotemode(EditLine *el)
1079 if (el->el_tty.t_mode != QU_IO)
1081 if (tty_setty(el, &el->el_tty.t_ed) == -1) {
1083 (void) fprintf(el->el_errfile, "QuoteModeOff: tty_setty: %s\n",
1085 #endif /* DEBUG_TTY */
1088 el->el_tty.t_mode = ED_IO;
1098 tty_stty(EditLine *el, int argc __unused, const char **argv)
1100 const ttymodes_t *m;
1105 struct termios *tios = &el->el_tty.t_ex;
1112 while (argv && *argv && argv[0][0] == '-' && argv[0][2] == '\0')
1113 switch (argv[0][1]) {
1120 tios = &el->el_tty.t_ed;
1125 tios = &el->el_tty.t_ex;
1130 tios = &el->el_tty.t_ts;
1134 (void) fprintf(el->el_errfile,
1135 "%s: Unknown switch `%c'.\n",
1140 if (!argv || !*argv) {
1142 int len = 0, st = 0, cu;
1143 for (m = ttymodes; m->m_name; m++) {
1144 if (m->m_type != i) {
1145 (void) fprintf(el->el_outfile, "%s%s",
1146 i != -1 ? "\n" : "",
1147 el->el_tty.t_t[z][m->m_type].t_name);
1150 strlen(el->el_tty.t_t[z][m->m_type].t_name);
1153 x = (el->el_tty.t_t[z][i].t_setmask & m->m_value)
1155 x = (el->el_tty.t_t[z][i].t_clrmask & m->m_value)
1161 if (x != '\0' || aflag) {
1163 cu = strlen(m->m_name) + (x != '\0') + 1;
1165 if (len + cu >= el->el_term.t_size.h) {
1166 (void) fprintf(el->el_outfile, "\n%*s",
1173 (void) fprintf(el->el_outfile, "%c%s ",
1176 (void) fprintf(el->el_outfile, "%s ",
1180 (void) fprintf(el->el_outfile, "\n");
1183 while (argv && (s = *argv++)) {
1196 for (m = ttymodes; m->m_name; m++)
1197 if ((p ? strncmp(m->m_name, d, (size_t)(p - d)) :
1198 strcmp(m->m_name, d)) == 0 &&
1199 (p == NULL || m->m_type == MD_CHAR))
1203 (void) fprintf(el->el_errfile,
1204 "%s: Invalid argument `%s'.\n", name, d);
1208 int c = ffs((int)m->m_value);
1209 int v = *++p ? parse__escape((const char **) &p) :
1210 el->el_tty.t_vdisable;
1212 c = tty__getcharindex(c);
1219 el->el_tty.t_t[z][m->m_type].t_setmask |= m->m_value;
1220 el->el_tty.t_t[z][m->m_type].t_clrmask &= ~m->m_value;
1223 el->el_tty.t_t[z][m->m_type].t_setmask &= ~m->m_value;
1224 el->el_tty.t_t[z][m->m_type].t_clrmask |= m->m_value;
1227 el->el_tty.t_t[z][m->m_type].t_setmask &= ~m->m_value;
1228 el->el_tty.t_t[z][m->m_type].t_clrmask &= ~m->m_value;
1238 * DEbugging routine to print the tty characters
1241 tty_printchar(EditLine *el, unsigned char *s)
1246 for (i = 0; i < C_NCC; i++) {
1247 for (m = el->el_tty.t_t; m->m_name; m++)
1248 if (m->m_type == MD_CHAR && C_SH(i) == m->m_value)
1251 (void) fprintf(el->el_errfile, "%s ^%c ",
1252 m->m_name, s[i] + 'A' - 1);
1254 (void) fprintf(el->el_errfile, "\n");
1256 (void) fprintf(el->el_errfile, "\n");