1*c7ef0cfcSnicm /****************************************************************************
2*c7ef0cfcSnicm * Copyright 2020,2021 Thomas E. Dickey *
3*c7ef0cfcSnicm * Copyright 2016,2017 Free Software Foundation, Inc. *
4*c7ef0cfcSnicm * *
5*c7ef0cfcSnicm * Permission is hereby granted, free of charge, to any person obtaining a *
6*c7ef0cfcSnicm * copy of this software and associated documentation files (the *
7*c7ef0cfcSnicm * "Software"), to deal in the Software without restriction, including *
8*c7ef0cfcSnicm * without limitation the rights to use, copy, modify, merge, publish, *
9*c7ef0cfcSnicm * distribute, distribute with modifications, sublicense, and/or sell *
10*c7ef0cfcSnicm * copies of the Software, and to permit persons to whom the Software is *
11*c7ef0cfcSnicm * furnished to do so, subject to the following conditions: *
12*c7ef0cfcSnicm * *
13*c7ef0cfcSnicm * The above copyright notice and this permission notice shall be included *
14*c7ef0cfcSnicm * in all copies or substantial portions of the Software. *
15*c7ef0cfcSnicm * *
16*c7ef0cfcSnicm * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17*c7ef0cfcSnicm * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18*c7ef0cfcSnicm * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19*c7ef0cfcSnicm * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20*c7ef0cfcSnicm * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21*c7ef0cfcSnicm * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22*c7ef0cfcSnicm * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23*c7ef0cfcSnicm * *
24*c7ef0cfcSnicm * Except as contained in this notice, the name(s) of the above copyright *
25*c7ef0cfcSnicm * holders shall not be used in advertising or otherwise to promote the *
26*c7ef0cfcSnicm * sale, use or other dealings in this Software without prior written *
27*c7ef0cfcSnicm * authorization. *
28*c7ef0cfcSnicm ****************************************************************************/
29*c7ef0cfcSnicm
30*c7ef0cfcSnicm /****************************************************************************
31*c7ef0cfcSnicm * Author: Thomas E. Dickey *
32*c7ef0cfcSnicm ****************************************************************************/
33*c7ef0cfcSnicm
34*c7ef0cfcSnicm #define USE_LIBTINFO
35*c7ef0cfcSnicm #include <tty_settings.h>
36*c7ef0cfcSnicm
37*c7ef0cfcSnicm #include <fcntl.h>
38*c7ef0cfcSnicm
39*c7ef0cfcSnicm MODULE_ID("$Id: tty_settings.c,v 1.1 2023/10/17 09:52:10 nicm Exp $")
40*c7ef0cfcSnicm
41*c7ef0cfcSnicm static int my_fd;
42*c7ef0cfcSnicm static TTY original_settings;
43*c7ef0cfcSnicm static bool can_restore = FALSE;
44*c7ef0cfcSnicm
45*c7ef0cfcSnicm static void
failed(const char * msg)46*c7ef0cfcSnicm failed(const char *msg)
47*c7ef0cfcSnicm {
48*c7ef0cfcSnicm int code = errno;
49*c7ef0cfcSnicm
50*c7ef0cfcSnicm (void) fprintf(stderr, "%s: %s: %s\n", _nc_progname, msg, strerror(code));
51*c7ef0cfcSnicm restore_tty_settings();
52*c7ef0cfcSnicm (void) fprintf(stderr, "\n");
53*c7ef0cfcSnicm ExitProgram(ErrSystem(code));
54*c7ef0cfcSnicm /* NOTREACHED */
55*c7ef0cfcSnicm }
56*c7ef0cfcSnicm
57*c7ef0cfcSnicm static bool
get_tty_settings(int fd,TTY * tty_settings)58*c7ef0cfcSnicm get_tty_settings(int fd, TTY * tty_settings)
59*c7ef0cfcSnicm {
60*c7ef0cfcSnicm bool success = TRUE;
61*c7ef0cfcSnicm my_fd = fd;
62*c7ef0cfcSnicm if (fd < 0 || GET_TTY(my_fd, tty_settings) < 0) {
63*c7ef0cfcSnicm success = FALSE;
64*c7ef0cfcSnicm }
65*c7ef0cfcSnicm return success;
66*c7ef0cfcSnicm }
67*c7ef0cfcSnicm
68*c7ef0cfcSnicm /*
69*c7ef0cfcSnicm * Open a file descriptor on the current terminal, to obtain its settings.
70*c7ef0cfcSnicm * stderr is less likely to be redirected than stdout; try that first.
71*c7ef0cfcSnicm */
72*c7ef0cfcSnicm int
save_tty_settings(TTY * tty_settings,bool need_tty)73*c7ef0cfcSnicm save_tty_settings(TTY * tty_settings, bool need_tty)
74*c7ef0cfcSnicm {
75*c7ef0cfcSnicm if (!get_tty_settings(STDERR_FILENO, tty_settings) &&
76*c7ef0cfcSnicm !get_tty_settings(STDOUT_FILENO, tty_settings) &&
77*c7ef0cfcSnicm !get_tty_settings(STDIN_FILENO, tty_settings)) {
78*c7ef0cfcSnicm if (need_tty) {
79*c7ef0cfcSnicm int fd = open("/dev/tty", O_RDWR);
80*c7ef0cfcSnicm if (!get_tty_settings(fd, tty_settings)) {
81*c7ef0cfcSnicm failed("terminal attributes");
82*c7ef0cfcSnicm }
83*c7ef0cfcSnicm } else {
84*c7ef0cfcSnicm my_fd = fileno(stdout);
85*c7ef0cfcSnicm }
86*c7ef0cfcSnicm } else {
87*c7ef0cfcSnicm can_restore = TRUE;
88*c7ef0cfcSnicm original_settings = *tty_settings;
89*c7ef0cfcSnicm }
90*c7ef0cfcSnicm return my_fd;
91*c7ef0cfcSnicm }
92*c7ef0cfcSnicm
93*c7ef0cfcSnicm void
restore_tty_settings(void)94*c7ef0cfcSnicm restore_tty_settings(void)
95*c7ef0cfcSnicm {
96*c7ef0cfcSnicm if (can_restore)
97*c7ef0cfcSnicm SET_TTY(my_fd, &original_settings);
98*c7ef0cfcSnicm }
99*c7ef0cfcSnicm
100*c7ef0cfcSnicm /* Set the modes if they've changed. */
101*c7ef0cfcSnicm void
update_tty_settings(TTY * old_settings,TTY * new_settings)102*c7ef0cfcSnicm update_tty_settings(TTY * old_settings, TTY * new_settings)
103*c7ef0cfcSnicm {
104*c7ef0cfcSnicm if (memcmp(new_settings, old_settings, sizeof(TTY))) {
105*c7ef0cfcSnicm SET_TTY(my_fd, new_settings);
106*c7ef0cfcSnicm }
107*c7ef0cfcSnicm }
108