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