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