xref: /freebsd/contrib/tcsh/tc.disc.c (revision 2f513db72b034fd5ef7f080b11be5c711c15186a)
1 /*
2  * tc.disc.c: Functions to set/clear line disciplines
3  *
4  */
5 /*-
6  * Copyright (c) 1980, 1991 The Regents of the University of California.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 #include "sh.h"
34 #ifdef OREO
35 #include <compat.h>
36 #endif	/* OREO */
37 
38 #include "ed.h"
39 
40 static int add_discipline = 0;	/* Did we add a line discipline	 */
41 
42 #if defined(IRIS4D) || defined(OREO) || defined(sonyrisc) || defined(__ANDROID__)
43 # define HAVE_DISC
44 # ifndef POSIX
45 static struct termio otermiob;
46 # else
47 static struct termios otermiob;
48 # endif /* POSIX */
49 #endif	/* IRIS4D || OREO */
50 
51 #ifdef _IBMR2
52 # define HAVE_DISC
53 char    strPOSIX[] = "posix";
54 #endif	/* _IBMR2 */
55 
56 #if !defined(HAVE_DISC) && defined(TIOCGETD) && defined(NTTYDISC)
57 static int oldisc;
58 #endif /* !HAVE_DISC && TIOCGETD && NTTYDISC */
59 
60 int
61 /*ARGSUSED*/
62 setdisc(int f)
63 {
64 #ifdef IRIS4D
65 # ifndef POSIX
66     struct termio termiob;
67 # else
68     struct termios termiob;
69 # endif
70 
71     if (ioctl(f, TCGETA, (ioctl_t) & termiob) == 0) {
72 	otermiob = termiob;
73 #if (SYSVREL < 4) || !defined(IRIS4D)
74 	if (termiob.c_line != NTTYDISC || termiob.c_cc[VSWTCH] == 0) { /*}*/
75 	    termiob.c_line = NTTYDISC;
76 #else
77 	if (termiob.c_cc[VSWTCH] == 0) {
78 #endif
79 	    termiob.c_cc[VSWTCH] = CSWTCH;
80 	    if (ioctl(f, TCSETA, (ioctl_t) & termiob) != 0)
81 		return (-1);
82 	}
83     }
84     else
85 	return (-1);
86     add_discipline = 1;
87     return (0);
88 #endif /* IRIS4D */
89 
90 
91 #ifdef OREO
92 # ifndef POSIX
93     struct termio termiob;
94 # else
95     struct termios termiob;
96 # endif
97 
98     struct ltchars ltcbuf;
99 
100     if (ioctl(f, TCGETA, (ioctl_t) & termiob) == 0) {
101 	int comp = getcompat(COMPAT_BSDTTY);
102 	otermiob = termiob;
103 	if ((comp & COMPAT_BSDTTY) != COMPAT_BSDTTY) {
104 	    (void) setcompat(comp | COMPAT_BSDTTY);
105 	    if (ioctl(f, TIOCGLTC, (ioctl_t) & ltcbuf) != 0)
106 		xprintf(CGETS(21, 1, "Couldn't get local chars.\n"));
107 	    else {
108 		ltcbuf.t_suspc = CTL_ESC('\032');        /* ^Z */
109 		ltcbuf.t_dsuspc = CTL_ESC('\031');       /* ^Y */
110 		ltcbuf.t_rprntc = CTL_ESC('\022');       /* ^R */
111 		ltcbuf.t_flushc = CTL_ESC('\017');       /* ^O */
112 		ltcbuf.t_werasc = CTL_ESC('\027');       /* ^W */
113 		ltcbuf.t_lnextc = CTL_ESC('\026');       /* ^V */
114 		if (ioctl(f, TIOCSLTC, (ioctl_t) & ltcbuf) != 0)
115 		    xprintf(CGETS(21, 2, "Couldn't set local chars.\n"));
116 	    }
117 	    termiob.c_cc[VSWTCH] = '\0';
118 	    if (ioctl(f, TCSETAF, (ioctl_t) & termiob) != 0)
119 		return (-1);
120 	}
121     }
122     else
123 	return (-1);
124     add_discipline = 1;
125     return (0);
126 #endif				/* OREO */
127 
128 
129 #ifdef _IBMR2
130     union txname tx;
131 
132     tx.tx_which = 0;
133 
134     if (ioctl(f, TXGETLD, (ioctl_t) & tx) == 0) {
135 	if (strcmp(tx.tx_name, strPOSIX) != 0)
136 	    if (ioctl(f, TXADDCD, (ioctl_t) strPOSIX) == 0) {
137 		add_discipline = 1;
138 		return (0);
139 	    }
140 	return (0);
141     }
142     else
143 	return (-1);
144 #endif	/* _IBMR2 */
145 
146 #ifndef HAVE_DISC
147 # if defined(TIOCGETD) && defined(NTTYDISC)
148     if (ioctl(f, TIOCGETD, (ioctl_t) & oldisc) == 0) {
149 	if (oldisc != NTTYDISC) {
150 	    int     ldisc = NTTYDISC;
151 
152 	    if (ioctl(f, TIOCSETD, (ioctl_t) & ldisc) != 0)
153 		return (-1);
154 	    add_discipline = 1;
155 	}
156 	else
157 	    oldisc = -1;
158 	return (0);
159     }
160     else
161 	return (-1);
162 # else
163     USE(f);
164     return (0);
165 # endif	/* TIOCGETD && NTTYDISC */
166 #endif	/* !HAVE_DISC */
167 } /* end setdisc */
168 
169 
170 int
171 /*ARGSUSED*/
172 resetdisc(int f)
173 {
174     if (add_discipline) {
175 	add_discipline = 0;
176 #if defined(OREO) || defined(IRIS4D)
177 	return (ioctl(f, TCSETAF, (ioctl_t) & otermiob));
178 #endif /* OREO || IRIS4D */
179 
180 #ifdef _IBMR2
181 	return (ioctl(f, TXDELCD, (ioctl_t) strPOSIX));
182 #endif /* _IBMR2 */
183 
184 #ifndef HAVE_DISC
185 # if defined(TIOCSETD) && defined(NTTYDISC)
186 	return (ioctl(f, TIOCSETD, (ioctl_t) & oldisc));
187 # endif /* TIOCSETD && NTTYDISC */
188 #endif /* !HAVE_DISC */
189     }
190     USE(f);
191     return (0);
192 } /* end resetdisc */
193