xref: /openbsd/usr.bin/bc/tty.c (revision 5b133f3f)
1 /*      $OpenBSD: tty.c,v 1.5 2023/03/08 04:43:10 guenther 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
settty(struct termios * t)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
gettty(struct termios * t)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 void
tstpcont(int sig)48 tstpcont(int sig)
49 {
50 	int save_errno = errno;
51 
52 	if (sig == SIGTSTP) {
53 		signal(SIGCONT, tstpcont);
54 		gettty(&ttyedit);
55 		settty(&ttysaved);
56 	} else {
57 		signal(SIGTSTP, tstpcont);
58 		settty(&ttyedit);
59 	}
60 	signal(sig, SIG_DFL);
61 	kill(0, sig);
62 	errno = save_errno;
63 }
64