tty.c revision 265533
1/*	$FreeBSD: stable/10/usr.bin/bc/tty.c 265533 2014-05-07 08:06:54Z delphij $	*/
2/*      $OpenBSD: tty.c,v 1.2 2013/11/12 13:54:51 deraadt Exp $	*/
3
4/*
5 * Copyright (c) 2013, Otto Moerbeek <otto@drijf.net>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include <errno.h>
21#include <signal.h>
22#include <histedit.h>
23#include <termios.h>
24#include "extern.h"
25
26struct termios ttysaved, ttyedit;
27
28static int
29settty(struct termios *t)
30{
31	int ret;
32
33	while ((ret = tcsetattr(0, TCSADRAIN,  t) == -1) && errno == EINTR)
34		continue;
35	return ret;
36}
37
38int
39gettty(struct termios *t)
40{
41	int ret;
42
43	while ((ret = tcgetattr(0, t) == -1) && errno == EINTR)
44		continue;
45	return ret;
46}
47
48/* ARGSUSED */
49void
50tstpcont(int sig)
51{
52	int save_errno = errno;
53
54	if (sig == SIGTSTP) {
55		signal(SIGCONT, tstpcont);
56		gettty(&ttyedit);
57		settty(&ttysaved);
58	} else {
59		signal(SIGTSTP, tstpcont);
60		settty(&ttyedit);
61	}
62	signal(sig, SIG_DFL);
63	kill(0, sig);
64	errno = save_errno;
65}
66