xref: /openbsd-src/usr.bin/bc/tty.c (revision 5b133f3f277e80f096764111e64f3a1284acb179)
1*5b133f3fSguenther /*      $OpenBSD: tty.c,v 1.5 2023/03/08 04:43:10 guenther Exp $	*/
29fac9dd6Sotto 
39fac9dd6Sotto /*
49fac9dd6Sotto  * Copyright (c) 2013, Otto Moerbeek <otto@drijf.net>
59fac9dd6Sotto  *
69fac9dd6Sotto  * Permission to use, copy, modify, and distribute this software for any
79fac9dd6Sotto  * purpose with or without fee is hereby granted, provided that the above
89fac9dd6Sotto  * copyright notice and this permission notice appear in all copies.
99fac9dd6Sotto  *
109fac9dd6Sotto  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
119fac9dd6Sotto  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
129fac9dd6Sotto  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
139fac9dd6Sotto  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
149fac9dd6Sotto  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
159fac9dd6Sotto  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
169fac9dd6Sotto  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
179fac9dd6Sotto  */
189fac9dd6Sotto 
199fac9dd6Sotto #include <errno.h>
209fac9dd6Sotto #include <signal.h>
219fac9dd6Sotto #include <histedit.h>
229fac9dd6Sotto #include <termios.h>
239fac9dd6Sotto #include "extern.h"
249fac9dd6Sotto 
259fac9dd6Sotto struct termios ttysaved, ttyedit;
269fac9dd6Sotto 
2743a6699fSderaadt static int
settty(struct termios * t)289fac9dd6Sotto settty(struct termios *t)
299fac9dd6Sotto {
309fac9dd6Sotto 	int ret;
319fac9dd6Sotto 
329ed226c2Sjsg 	while ((ret = tcsetattr(0, TCSADRAIN,  t)) == -1 && errno == EINTR)
339fac9dd6Sotto 		continue;
349fac9dd6Sotto 	return ret;
359fac9dd6Sotto }
369fac9dd6Sotto 
379fac9dd6Sotto int
gettty(struct termios * t)389fac9dd6Sotto gettty(struct termios *t)
399fac9dd6Sotto {
409fac9dd6Sotto 	int ret;
419fac9dd6Sotto 
429ed226c2Sjsg 	while ((ret = tcgetattr(0, t)) == -1 && errno == EINTR)
439fac9dd6Sotto 		continue;
449fac9dd6Sotto 	return ret;
459fac9dd6Sotto }
469fac9dd6Sotto 
479fac9dd6Sotto void
tstpcont(int sig)489fac9dd6Sotto tstpcont(int sig)
499fac9dd6Sotto {
509fac9dd6Sotto 	int save_errno = errno;
519fac9dd6Sotto 
529fac9dd6Sotto 	if (sig == SIGTSTP) {
539fac9dd6Sotto 		signal(SIGCONT, tstpcont);
549fac9dd6Sotto 		gettty(&ttyedit);
559fac9dd6Sotto 		settty(&ttysaved);
569fac9dd6Sotto 	} else {
579fac9dd6Sotto 		signal(SIGTSTP, tstpcont);
589fac9dd6Sotto 		settty(&ttyedit);
599fac9dd6Sotto 	}
609fac9dd6Sotto 	signal(sig, SIG_DFL);
619fac9dd6Sotto 	kill(0, sig);
629fac9dd6Sotto 	errno = save_errno;
639fac9dd6Sotto }
64