159243Sobrien/*
259243Sobrien * tc.disc.c: Functions to set/clear line disciplines
359243Sobrien *
459243Sobrien */
559243Sobrien/*-
659243Sobrien * Copyright (c) 1980, 1991 The Regents of the University of California.
759243Sobrien * All rights reserved.
859243Sobrien *
959243Sobrien * Redistribution and use in source and binary forms, with or without
1059243Sobrien * modification, are permitted provided that the following conditions
1159243Sobrien * are met:
1259243Sobrien * 1. Redistributions of source code must retain the above copyright
1359243Sobrien *    notice, this list of conditions and the following disclaimer.
1459243Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1559243Sobrien *    notice, this list of conditions and the following disclaimer in the
1659243Sobrien *    documentation and/or other materials provided with the distribution.
17100616Smp * 3. Neither the name of the University nor the names of its contributors
1859243Sobrien *    may be used to endorse or promote products derived from this software
1959243Sobrien *    without specific prior written permission.
2059243Sobrien *
2159243Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2259243Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2359243Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2459243Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2559243Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2659243Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2759243Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2859243Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2959243Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3059243Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3159243Sobrien * SUCH DAMAGE.
3259243Sobrien */
3359243Sobrien#include "sh.h"
3459243Sobrien#ifdef OREO
3559243Sobrien#include <compat.h>
3659243Sobrien#endif	/* OREO */
3759243Sobrien
38100616Smp#include "ed.h"
3959243Sobrien
40145479Smpstatic int add_discipline = 0;	/* Did we add a line discipline	 */
4159243Sobrien
42231990Smp#if defined(IRIS4D) || defined(OREO) || defined(sonyrisc) || defined(__ANDROID__)
4359243Sobrien# define HAVE_DISC
4459243Sobrien# ifndef POSIX
4559243Sobrienstatic struct termio otermiob;
4659243Sobrien# else
4759243Sobrienstatic struct termios otermiob;
4859243Sobrien# endif /* POSIX */
4959243Sobrien#endif	/* IRIS4D || OREO */
5059243Sobrien
5159243Sobrien#ifdef _IBMR2
5259243Sobrien# define HAVE_DISC
5359243Sobrienchar    strPOSIX[] = "posix";
5459243Sobrien#endif	/* _IBMR2 */
5559243Sobrien
5659243Sobrien#if !defined(HAVE_DISC) && defined(TIOCGETD) && defined(NTTYDISC)
5759243Sobrienstatic int oldisc;
5859243Sobrien#endif /* !HAVE_DISC && TIOCGETD && NTTYDISC */
5959243Sobrien
6059243Sobrienint
6159243Sobrien/*ARGSUSED*/
62167465Smpsetdisc(int f)
6359243Sobrien{
6459243Sobrien#ifdef IRIS4D
6559243Sobrien# ifndef POSIX
6659243Sobrien    struct termio termiob;
6759243Sobrien# else
6859243Sobrien    struct termios termiob;
6959243Sobrien# endif
7059243Sobrien
7159243Sobrien    if (ioctl(f, TCGETA, (ioctl_t) & termiob) == 0) {
7259243Sobrien	otermiob = termiob;
7359243Sobrien#if (SYSVREL < 4) || !defined(IRIS4D)
7459243Sobrien	if (termiob.c_line != NTTYDISC || termiob.c_cc[VSWTCH] == 0) { /*}*/
7559243Sobrien	    termiob.c_line = NTTYDISC;
7659243Sobrien#else
7759243Sobrien	if (termiob.c_cc[VSWTCH] == 0) {
7859243Sobrien#endif
7959243Sobrien	    termiob.c_cc[VSWTCH] = CSWTCH;
8059243Sobrien	    if (ioctl(f, TCSETA, (ioctl_t) & termiob) != 0)
8159243Sobrien		return (-1);
8259243Sobrien	}
8359243Sobrien    }
8459243Sobrien    else
8559243Sobrien	return (-1);
8659243Sobrien    add_discipline = 1;
8759243Sobrien    return (0);
8859243Sobrien#endif /* IRIS4D */
8959243Sobrien
9059243Sobrien
9159243Sobrien#ifdef OREO
9259243Sobrien# ifndef POSIX
9359243Sobrien    struct termio termiob;
9459243Sobrien# else
9559243Sobrien    struct termios termiob;
9659243Sobrien# endif
9759243Sobrien
9859243Sobrien    struct ltchars ltcbuf;
9959243Sobrien
10059243Sobrien    if (ioctl(f, TCGETA, (ioctl_t) & termiob) == 0) {
10159243Sobrien	int comp = getcompat(COMPAT_BSDTTY);
10259243Sobrien	otermiob = termiob;
10359243Sobrien	if ((comp & COMPAT_BSDTTY) != COMPAT_BSDTTY) {
10459243Sobrien	    (void) setcompat(comp | COMPAT_BSDTTY);
10559243Sobrien	    if (ioctl(f, TIOCGLTC, (ioctl_t) & ltcbuf) != 0)
10659243Sobrien		xprintf(CGETS(21, 1, "Couldn't get local chars.\n"));
10759243Sobrien	    else {
10859243Sobrien		ltcbuf.t_suspc = CTL_ESC('\032');        /* ^Z */
10959243Sobrien		ltcbuf.t_dsuspc = CTL_ESC('\031');       /* ^Y */
11059243Sobrien		ltcbuf.t_rprntc = CTL_ESC('\022');       /* ^R */
11159243Sobrien		ltcbuf.t_flushc = CTL_ESC('\017');       /* ^O */
11259243Sobrien		ltcbuf.t_werasc = CTL_ESC('\027');       /* ^W */
11359243Sobrien		ltcbuf.t_lnextc = CTL_ESC('\026');       /* ^V */
11459243Sobrien		if (ioctl(f, TIOCSLTC, (ioctl_t) & ltcbuf) != 0)
11559243Sobrien		    xprintf(CGETS(21, 2, "Couldn't set local chars.\n"));
11659243Sobrien	    }
11759243Sobrien	    termiob.c_cc[VSWTCH] = '\0';
11859243Sobrien	    if (ioctl(f, TCSETAF, (ioctl_t) & termiob) != 0)
11959243Sobrien		return (-1);
12059243Sobrien	}
12159243Sobrien    }
12259243Sobrien    else
12359243Sobrien	return (-1);
12459243Sobrien    add_discipline = 1;
12559243Sobrien    return (0);
12659243Sobrien#endif				/* OREO */
12759243Sobrien
12859243Sobrien
12959243Sobrien#ifdef _IBMR2
13059243Sobrien    union txname tx;
13159243Sobrien
13259243Sobrien    tx.tx_which = 0;
13359243Sobrien
13459243Sobrien    if (ioctl(f, TXGETLD, (ioctl_t) & tx) == 0) {
13559243Sobrien	if (strcmp(tx.tx_name, strPOSIX) != 0)
13659243Sobrien	    if (ioctl(f, TXADDCD, (ioctl_t) strPOSIX) == 0) {
13759243Sobrien		add_discipline = 1;
13859243Sobrien		return (0);
13959243Sobrien	    }
14059243Sobrien	return (0);
14159243Sobrien    }
14259243Sobrien    else
14359243Sobrien	return (-1);
14459243Sobrien#endif	/* _IBMR2 */
14559243Sobrien
14659243Sobrien#ifndef HAVE_DISC
14759243Sobrien# if defined(TIOCGETD) && defined(NTTYDISC)
14859243Sobrien    if (ioctl(f, TIOCGETD, (ioctl_t) & oldisc) == 0) {
14959243Sobrien	if (oldisc != NTTYDISC) {
15059243Sobrien	    int     ldisc = NTTYDISC;
15159243Sobrien
15259243Sobrien	    if (ioctl(f, TIOCSETD, (ioctl_t) & ldisc) != 0)
15359243Sobrien		return (-1);
15459243Sobrien	    add_discipline = 1;
15559243Sobrien	}
15659243Sobrien	else
15759243Sobrien	    oldisc = -1;
15859243Sobrien	return (0);
15959243Sobrien    }
16059243Sobrien    else
16159243Sobrien	return (-1);
16259243Sobrien# else
163145479Smp    USE(f);
16459243Sobrien    return (0);
16559243Sobrien# endif	/* TIOCGETD && NTTYDISC */
16659243Sobrien#endif	/* !HAVE_DISC */
16759243Sobrien} /* end setdisc */
16859243Sobrien
16959243Sobrien
17059243Sobrienint
17159243Sobrien/*ARGSUSED*/
172167465Smpresetdisc(int f)
17359243Sobrien{
17459243Sobrien    if (add_discipline) {
17559243Sobrien	add_discipline = 0;
17659243Sobrien#if defined(OREO) || defined(IRIS4D)
17759243Sobrien	return (ioctl(f, TCSETAF, (ioctl_t) & otermiob));
17859243Sobrien#endif /* OREO || IRIS4D */
17959243Sobrien
18059243Sobrien#ifdef _IBMR2
18159243Sobrien	return (ioctl(f, TXDELCD, (ioctl_t) strPOSIX));
18259243Sobrien#endif /* _IBMR2 */
18359243Sobrien
18459243Sobrien#ifndef HAVE_DISC
18559243Sobrien# if defined(TIOCSETD) && defined(NTTYDISC)
18659243Sobrien	return (ioctl(f, TIOCSETD, (ioctl_t) & oldisc));
18759243Sobrien# endif /* TIOCSETD && NTTYDISC */
18859243Sobrien#endif /* !HAVE_DISC */
18959243Sobrien    }
190145479Smp    USE(f);
19159243Sobrien    return (0);
19259243Sobrien} /* end resetdisc */
193