1204917Sdes/* $OpenBSD: sshtty.c,v 1.14 2010/01/09 05:04:24 djm Exp $ */
276259Sgreen/*
376259Sgreen * Author: Tatu Ylonen <ylo@cs.hut.fi>
476259Sgreen * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
576259Sgreen *                    All rights reserved
676259Sgreen *
776259Sgreen * As far as I am concerned, the code I have written for this software
876259Sgreen * can be used freely for any purpose.  Any derived versions of this
976259Sgreen * software must be clearly marked as such, and if the derived work is
1076259Sgreen * incompatible with the protocol description in the RFC file, it must be
1176259Sgreen * called by a name other than "ssh" or "Secure Shell".
1276259Sgreen */
1376259Sgreen/*
1476259Sgreen * Copyright (c) 2001 Markus Friedl.  All rights reserved.
1576259Sgreen * Copyright (c) 2001 Kevin Steves.  All rights reserved.
1676259Sgreen *
1776259Sgreen * Redistribution and use in source and binary forms, with or without
1876259Sgreen * modification, are permitted provided that the following conditions
1976259Sgreen * are met:
2076259Sgreen * 1. Redistributions of source code must retain the above copyright
2176259Sgreen *    notice, this list of conditions and the following disclaimer.
2276259Sgreen * 2. Redistributions in binary form must reproduce the above copyright
2376259Sgreen *    notice, this list of conditions and the following disclaimer in the
2476259Sgreen *    documentation and/or other materials provided with the distribution.
2576259Sgreen *
2676259Sgreen * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2776259Sgreen * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2876259Sgreen * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2976259Sgreen * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
3076259Sgreen * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
3176259Sgreen * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3276259Sgreen * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3376259Sgreen * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3476259Sgreen * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3576259Sgreen * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3676259Sgreen */
3776259Sgreen
3876259Sgreen#include "includes.h"
3976259Sgreen
40162852Sdes#include <sys/types.h>
41162852Sdes#include <stdio.h>
42162852Sdes#include <termios.h>
43162852Sdes#include <pwd.h>
44162852Sdes
45137015Sdes#include "sshpty.h"
4676259Sgreen
4776259Sgreenstatic struct termios _saved_tio;
4876259Sgreenstatic int _in_raw_mode = 0;
4976259Sgreen
50181111Sdesstruct termios *
5176259Sgreenget_saved_tio(void)
5276259Sgreen{
53181111Sdes	return _in_raw_mode ? &_saved_tio : NULL;
5476259Sgreen}
5576259Sgreen
5676259Sgreenvoid
57204917Sdesleave_raw_mode(int quiet)
5876259Sgreen{
5976259Sgreen	if (!_in_raw_mode)
6076259Sgreen		return;
61204917Sdes	if (tcsetattr(fileno(stdin), TCSADRAIN, &_saved_tio) == -1) {
62204917Sdes		if (!quiet)
63204917Sdes			perror("tcsetattr");
64204917Sdes	} else
6576259Sgreen		_in_raw_mode = 0;
6676259Sgreen}
6776259Sgreen
6876259Sgreenvoid
69204917Sdesenter_raw_mode(int quiet)
7076259Sgreen{
7176259Sgreen	struct termios tio;
7276259Sgreen
7376259Sgreen	if (tcgetattr(fileno(stdin), &tio) == -1) {
74204917Sdes		if (!quiet)
75204917Sdes			perror("tcgetattr");
7676259Sgreen		return;
7776259Sgreen	}
7876259Sgreen	_saved_tio = tio;
7976259Sgreen	tio.c_iflag |= IGNPAR;
8076259Sgreen	tio.c_iflag &= ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF);
81124208Sdes#ifdef IUCLC
82124208Sdes	tio.c_iflag &= ~IUCLC;
83124208Sdes#endif
8476259Sgreen	tio.c_lflag &= ~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL);
8576259Sgreen#ifdef IEXTEN
8676259Sgreen	tio.c_lflag &= ~IEXTEN;
8776259Sgreen#endif
8876259Sgreen	tio.c_oflag &= ~OPOST;
8976259Sgreen	tio.c_cc[VMIN] = 1;
9076259Sgreen	tio.c_cc[VTIME] = 0;
91204917Sdes	if (tcsetattr(fileno(stdin), TCSADRAIN, &tio) == -1) {
92204917Sdes		if (!quiet)
93204917Sdes			perror("tcsetattr");
94204917Sdes	} else
9576259Sgreen		_in_raw_mode = 1;
9676259Sgreen}
97