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