xref: /freebsd/usr.bin/bc/tty.c (revision 3c4ba5f55438f7afd4f4b0b56f88f2bb505fd6a6)
1 /*	$FreeBSD$	*/
2 /*      $OpenBSD: tty.c,v 1.3 2015/09/05 09:49:24 jsg 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 
26 struct termios ttysaved, ttyedit;
27 
28 static int
29 settty(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 
38 int
39 gettty(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 */
49 void
50 tstpcont(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