tc.disc.c revision 167465
1249423Sdim/* $Header: /p/tcsh/cvsroot/tcsh/tc.disc.c,v 3.17 2006/03/02 18:46:45 christos Exp $ */
2234971Sdim/*
3234971Sdim * tc.disc.c: Functions to set/clear line disciplines
4234971Sdim *
5234971Sdim */
6234971Sdim/*-
7234971Sdim * Copyright (c) 1980, 1991 The Regents of the University of California.
8234971Sdim * All rights reserved.
9234971Sdim *
10234971Sdim * Redistribution and use in source and binary forms, with or without
11234971Sdim * modification, are permitted provided that the following conditions
12234971Sdim * are met:
13234971Sdim * 1. Redistributions of source code must retain the above copyright
14234971Sdim *    notice, this list of conditions and the following disclaimer.
15234971Sdim * 2. Redistributions in binary form must reproduce the above copyright
16234971Sdim *    notice, this list of conditions and the following disclaimer in the
17234971Sdim *    documentation and/or other materials provided with the distribution.
18234971Sdim * 3. Neither the name of the University nor the names of its contributors
19234971Sdim *    may be used to endorse or promote products derived from this software
20234971Sdim *    without specific prior written permission.
21234971Sdim *
22234971Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23234971Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24234971Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25234971Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26234971Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27234971Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28234971Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29234971Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30234971Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31234971Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32234971Sdim * SUCH DAMAGE.
33234971Sdim */
34234971Sdim#include "sh.h"
35234971Sdim
36234971SdimRCSID("$tcsh: tc.disc.c,v 3.17 2006/03/02 18:46:45 christos Exp $")
37234971Sdim
38234971Sdim#ifdef OREO
39234971Sdim#include <compat.h>
40234971Sdim#endif	/* OREO */
41234971Sdim
42234971Sdim#include "ed.h"
43234971Sdim
44234971Sdimstatic int add_discipline = 0;	/* Did we add a line discipline	 */
45234971Sdim
46234971Sdim#if defined(IRIS4D) || defined(OREO) || defined(sonyrisc)
47234971Sdim# define HAVE_DISC
48234971Sdim# ifndef POSIX
49234971Sdimstatic struct termio otermiob;
50234971Sdim# else
51234971Sdimstatic struct termios otermiob;
52234971Sdim# endif /* POSIX */
53234971Sdim#endif	/* IRIS4D || OREO */
54234971Sdim
55234971Sdim#ifdef _IBMR2
56234971Sdim# define HAVE_DISC
57234971Sdimchar    strPOSIX[] = "posix";
58234971Sdim#endif	/* _IBMR2 */
59234971Sdim
60234971Sdim#if !defined(HAVE_DISC) && defined(TIOCGETD) && defined(NTTYDISC)
61234971Sdimstatic int oldisc;
62#endif /* !HAVE_DISC && TIOCGETD && NTTYDISC */
63
64int
65/*ARGSUSED*/
66setdisc(int f)
67{
68#ifdef IRIS4D
69# ifndef POSIX
70    struct termio termiob;
71# else
72    struct termios termiob;
73# endif
74
75    if (ioctl(f, TCGETA, (ioctl_t) & termiob) == 0) {
76	otermiob = termiob;
77#if (SYSVREL < 4) || !defined(IRIS4D)
78	if (termiob.c_line != NTTYDISC || termiob.c_cc[VSWTCH] == 0) { /*}*/
79	    termiob.c_line = NTTYDISC;
80#else
81	if (termiob.c_cc[VSWTCH] == 0) {
82#endif
83	    termiob.c_cc[VSWTCH] = CSWTCH;
84	    if (ioctl(f, TCSETA, (ioctl_t) & termiob) != 0)
85		return (-1);
86	}
87    }
88    else
89	return (-1);
90    add_discipline = 1;
91    return (0);
92#endif /* IRIS4D */
93
94
95#ifdef OREO
96# ifndef POSIX
97    struct termio termiob;
98# else
99    struct termios termiob;
100# endif
101
102    struct ltchars ltcbuf;
103
104    if (ioctl(f, TCGETA, (ioctl_t) & termiob) == 0) {
105	int comp = getcompat(COMPAT_BSDTTY);
106	otermiob = termiob;
107	if ((comp & COMPAT_BSDTTY) != COMPAT_BSDTTY) {
108	    (void) setcompat(comp | COMPAT_BSDTTY);
109	    if (ioctl(f, TIOCGLTC, (ioctl_t) & ltcbuf) != 0)
110		xprintf(CGETS(21, 1, "Couldn't get local chars.\n"));
111	    else {
112		ltcbuf.t_suspc = CTL_ESC('\032');        /* ^Z */
113		ltcbuf.t_dsuspc = CTL_ESC('\031');       /* ^Y */
114		ltcbuf.t_rprntc = CTL_ESC('\022');       /* ^R */
115		ltcbuf.t_flushc = CTL_ESC('\017');       /* ^O */
116		ltcbuf.t_werasc = CTL_ESC('\027');       /* ^W */
117		ltcbuf.t_lnextc = CTL_ESC('\026');       /* ^V */
118		if (ioctl(f, TIOCSLTC, (ioctl_t) & ltcbuf) != 0)
119		    xprintf(CGETS(21, 2, "Couldn't set local chars.\n"));
120	    }
121	    termiob.c_cc[VSWTCH] = '\0';
122	    if (ioctl(f, TCSETAF, (ioctl_t) & termiob) != 0)
123		return (-1);
124	}
125    }
126    else
127	return (-1);
128    add_discipline = 1;
129    return (0);
130#endif				/* OREO */
131
132
133#ifdef _IBMR2
134    union txname tx;
135
136    tx.tx_which = 0;
137
138    if (ioctl(f, TXGETLD, (ioctl_t) & tx) == 0) {
139	if (strcmp(tx.tx_name, strPOSIX) != 0)
140	    if (ioctl(f, TXADDCD, (ioctl_t) strPOSIX) == 0) {
141		add_discipline = 1;
142		return (0);
143	    }
144	return (0);
145    }
146    else
147	return (-1);
148#endif	/* _IBMR2 */
149
150#ifndef HAVE_DISC
151# if defined(TIOCGETD) && defined(NTTYDISC)
152    if (ioctl(f, TIOCGETD, (ioctl_t) & oldisc) == 0) {
153	if (oldisc != NTTYDISC) {
154	    int     ldisc = NTTYDISC;
155
156	    if (ioctl(f, TIOCSETD, (ioctl_t) & ldisc) != 0)
157		return (-1);
158	    add_discipline = 1;
159	}
160	else
161	    oldisc = -1;
162	return (0);
163    }
164    else
165	return (-1);
166# else
167    USE(f);
168    return (0);
169# endif	/* TIOCGETD && NTTYDISC */
170#endif	/* !HAVE_DISC */
171} /* end setdisc */
172
173
174int
175/*ARGSUSED*/
176resetdisc(int f)
177{
178    if (add_discipline) {
179	add_discipline = 0;
180#if defined(OREO) || defined(IRIS4D)
181	return (ioctl(f, TCSETAF, (ioctl_t) & otermiob));
182#endif /* OREO || IRIS4D */
183
184#ifdef _IBMR2
185	return (ioctl(f, TXDELCD, (ioctl_t) strPOSIX));
186#endif /* _IBMR2 */
187
188#ifndef HAVE_DISC
189# if defined(TIOCSETD) && defined(NTTYDISC)
190	return (ioctl(f, TIOCSETD, (ioctl_t) & oldisc));
191# endif /* TIOCSETD && NTTYDISC */
192#endif /* !HAVE_DISC */
193    }
194    USE(f);
195    return (0);
196} /* end resetdisc */
197