sshtty.c revision 137015
190075Sobrien/*
290075Sobrien * Author: Tatu Ylonen <ylo@cs.hut.fi>
3117395Skan * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
490075Sobrien *                    All rights reserved
590075Sobrien *
690075Sobrien * As far as I am concerned, the code I have written for this software
790075Sobrien * can be used freely for any purpose.  Any derived versions of this
890075Sobrien * software must be clearly marked as such, and if the derived work is
990075Sobrien * incompatible with the protocol description in the RFC file, it must be
1090075Sobrien * called by a name other than "ssh" or "Secure Shell".
1190075Sobrien */
1290075Sobrien/*
13132718Skan * Copyright (c) 2001 Markus Friedl.  All rights reserved.
1490075Sobrien * Copyright (c) 2001 Kevin Steves.  All rights reserved.
1590075Sobrien *
16146895Skan * Redistribution and use in source and binary forms, with or without
1790075Sobrien * modification, are permitted provided that the following conditions
18146895Skan * are met:
19146895Skan * 1. Redistributions of source code must retain the above copyright
2090075Sobrien *    notice, this list of conditions and the following disclaimer.
21146895Skan * 2. Redistributions in binary form must reproduce the above copyright
22146895Skan *    notice, this list of conditions and the following disclaimer in the
23146895Skan *    documentation and/or other materials provided with the distribution.
24146895Skan *
2590075Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2690075Sobrien * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2790075Sobrien * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2890075Sobrien * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2990075Sobrien * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30117395Skan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31117395Skan * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3290075Sobrien * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3390075Sobrien * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3490075Sobrien * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3590075Sobrien */
3690075Sobrien
3790075Sobrien#include "includes.h"
3890075SobrienRCSID("$OpenBSD: sshtty.c,v 1.6 2004/05/08 00:01:37 deraadt Exp $");
3996263Sobrien
4090075Sobrien#include "sshpty.h"
4190075Sobrien#include "log.h"
4290075Sobrien
43104752Skanstatic struct termios _saved_tio;
4490075Sobrienstatic int _in_raw_mode = 0;
4590075Sobrien
4690075Sobrienstruct termios
4790075Sobrienget_saved_tio(void)
4890075Sobrien{
4990075Sobrien	return _saved_tio;
5090075Sobrien}
5190075Sobrien
5290075Sobrienvoid
53132718Skanleave_raw_mode(void)
54132718Skan{
55132718Skan	if (!_in_raw_mode)
56132718Skan		return;
57132718Skan	if (tcsetattr(fileno(stdin), TCSADRAIN, &_saved_tio) == -1)
58132718Skan		perror("tcsetattr");
59132718Skan	else
60132718Skan		_in_raw_mode = 0;
61132718Skan}
62132718Skan
6390075Sobrienvoid
6490075Sobrienenter_raw_mode(void)
6590075Sobrien{
6690075Sobrien	struct termios tio;
6790075Sobrien
6890075Sobrien	if (tcgetattr(fileno(stdin), &tio) == -1) {
6990075Sobrien		perror("tcgetattr");
7090075Sobrien		return;
7190075Sobrien	}
7290075Sobrien	_saved_tio = tio;
7390075Sobrien	tio.c_iflag |= IGNPAR;
7490075Sobrien	tio.c_iflag &= ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF);
7590075Sobrien#ifdef IUCLC
7690075Sobrien	tio.c_iflag &= ~IUCLC;
77117395Skan#endif
78117395Skan	tio.c_lflag &= ~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL);
7990075Sobrien#ifdef IEXTEN
8090075Sobrien	tio.c_lflag &= ~IEXTEN;
8190075Sobrien#endif
82132718Skan	tio.c_oflag &= ~OPOST;
8390075Sobrien	tio.c_cc[VMIN] = 1;
84132718Skan	tio.c_cc[VTIME] = 0;
85132718Skan	if (tcsetattr(fileno(stdin), TCSADRAIN, &tio) == -1)
86132718Skan		perror("tcsetattr");
8790075Sobrien	else
8890075Sobrien		_in_raw_mode = 1;
8990075Sobrien}
9090075Sobrien