/*
 *  File: tty.c
 *  Author: Claes M. Nyberg <md0claes@mdstud.chalmers.se>
 *  Description: TeleType routines
 *  Version: 1.0
 *  Date: Mon Mar 17 20:11:03 CET 2003
 *
 *  Copyright (c) 2003 Claes M. Nyberg <md0claes@mdstud.chalmers.se>
 *  All rights reserved, all wrongs reversed.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *  1. Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *  3. The name of author may not be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 *  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 *  THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 *  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 *  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 *  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 *  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */   

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <termios.h>
#include "sash.h"

extern struct sashopt opt;

/*
 * Set terminal in raw mode, and save the current settings into
 * termios structure pointed to by save if save is not NULL;
 * Returns -1 on error and zero on success.
 */
int
tty_raw(int fd, struct termios *save)
{
    struct termios saved;
    struct termios raw;
	static int first = 1;

    if (isatty(fd) == 0) {
        fprintf(stderr, "tty_raw(): Error, fd is not a tty\n");
        return(-1);
    }

    if (tcgetattr(fd, &saved) < 0)
        return (-1);
		
	/* Make sure that ^H is the backspace character 
	 * (For file transfer mode) */
	saved.c_cc[VERASE] = CONTROL('H');

    /* Save current settings */
    if (save != NULL)
        memcpy(save, &saved, sizeof(struct termios));

    /* Copy current */
    memcpy(&raw, &saved, sizeof(struct termios));

	/* Temporary Windows mode */
	if (strstr(opt.sa_tver, "-WIN")) {

		if (first && opt.sa_verbose > 1) {
			printf("Ignoring to set tty to raw mode (target is Windows)\n");
			printf("Remember to press enter after escape character"
				   "to enter file transfer mode\n");
			first = 0;
		}
		raw.c_lflag &= ~(ISIG | ECHONL);
		raw.c_lflag |= ECHOE;
		raw.c_cc[VERASE] = CONTROL('H');
	}
	/* Raw mode */
	else {
		raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
		raw.c_oflag &= ~(OPOST | ONLCR);
		raw.c_cflag |= CS8;
		raw.c_cflag &= ~(CSIZE | PARENB);
		raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
		raw.c_cc[VMIN] = 1;
		raw.c_cc[VTIME] = 0;
	}

    if (tcsetattr(fd, TCSAFLUSH, &raw) < 0)
        return(-1);

    return (0);
}

