1 /****************************************************************************
2 * Copyright 2019-2021,2023 Thomas E. Dickey *
3 * Copyright 2016,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Thomas E. Dickey *
32 ****************************************************************************/
34 #include <reset_cmd.h>
35 #include <tty_settings.h>
42 # if !defined(sun) || !TERMIOS
44 # include <sys/ioctl.h>
50 /* they neglected to define struct winsize in termios.h -- it is only
52 #include <sys/stream.h>
56 MODULE_ID("$Id: reset_cmd.c,v 1.29 2023/01/07 21:50:34 tom Exp $")
59 * SCO defines TIOCGSIZE and the corresponding struct. Other systems (SunOS,
60 * Solaris, IRIX) define TIOCGWINSZ and struct winsize.
63 # define IOCTL_GET_WINSIZE TIOCGSIZE
64 # define IOCTL_SET_WINSIZE TIOCSSIZE
65 # define STRUCT_WINSIZE struct ttysize
66 # define WINSIZE_ROWS(n) n.ts_lines
67 # define WINSIZE_COLS(n) n.ts_cols
70 # define IOCTL_GET_WINSIZE TIOCGWINSZ
71 # define IOCTL_SET_WINSIZE TIOCSWINSZ
72 # define STRUCT_WINSIZE struct winsize
73 # define WINSIZE_ROWS(n) n.ws_row
74 # define WINSIZE_COLS(n) n.ws_col
80 static bool use_reset = FALSE; /* invoked as reset */
81 static bool use_init = FALSE; /* invoked as init */
83 static GCC_NORETURN void
84 failed(const char *msg)
88 (void) fprintf(stderr, "%s: %s: %s\n", _nc_progname, msg, strerror(code));
89 restore_tty_settings();
90 (void) fprintf(my_file, "\n");
92 ExitProgram(ErrSystem(code));
105 if ((fp = safe_fopen(file, "r")) == 0)
108 while ((nr = fread(buf, sizeof(char), sizeof(buf), fp)) != 0) {
109 if (fwrite(buf, sizeof(char), nr, my_file) != nr) {
122 return putc(c, my_file);
125 /**************************************************************************
127 **************************************************************************/
129 /* some BSD systems have these built in, some systems are missing
130 * one or more definitions. The safest solution is to override unless the
131 * commonly-altered ones are defined.
133 #if !(defined(CERASE) && defined(CINTR) && defined(CKILL) && defined(CQUIT))
146 /* control-character defaults */
148 #define CEOF CTRL('D')
151 #define CERASE CTRL('H')
154 #define CINTR 127 /* ^? */
157 #define CKILL CTRL('U')
160 #define CLNEXT CTRL('v')
163 #define CRPRNT CTRL('r')
166 #define CQUIT CTRL('\\')
169 #define CSTART CTRL('Q')
172 #define CSTOP CTRL('S')
175 #define CSUSP CTRL('Z')
178 #if defined(_POSIX_VDISABLE)
179 #define DISABLED(val) (((_POSIX_VDISABLE != -1) \
180 && ((val) == _POSIX_VDISABLE)) \
183 #define DISABLED(val) ((int)(val) <= 0)
186 #define CHK(val, dft) (unsigned char) (DISABLED(val) ? dft : val)
188 #define reset_char(item, value) \
189 tty_settings->c_cc[item] = CHK(tty_settings->c_cc[item], value)
192 * Reset the terminal mode bits to a sensible state. Very useful after
193 * a child program dies in raw mode.
196 reset_tty_settings(int fd, TTY * tty_settings, int noset)
198 GET_TTY(fd, tty_settings);
201 #if defined(VDISCARD) && defined(CDISCARD)
202 reset_char(VDISCARD, CDISCARD);
204 reset_char(VEOF, CEOF);
205 reset_char(VERASE, CERASE);
206 #if defined(VERASE2) && defined(CERASE2)
207 reset_char(VERASE2, CERASE2);
209 #if defined(VFLUSH) && defined(CFLUSH)
210 reset_char(VFLUSH, CFLUSH);
212 reset_char(VINTR, CINTR);
213 reset_char(VKILL, CKILL);
214 #if defined(VLNEXT) && defined(CLNEXT)
215 reset_char(VLNEXT, CLNEXT);
217 reset_char(VQUIT, CQUIT);
218 #if defined(VREPRINT) && defined(CRPRNT)
219 reset_char(VREPRINT, CRPRNT);
221 #if defined(VSTART) && defined(CSTART)
222 reset_char(VSTART, CSTART);
224 #if defined(VSTOP) && defined(CSTOP)
225 reset_char(VSTOP, CSTOP);
227 #if defined(VSUSP) && defined(CSUSP)
228 reset_char(VSUSP, CSUSP);
230 #if defined(VWERASE) && defined(CWERASE)
231 reset_char(VWERASE, CWERASE);
234 tty_settings->c_iflag &= ~((unsigned) (IGNBRK
248 tty_settings->c_iflag |= (BRKINT
257 tty_settings->c_oflag &= ~((unsigned) (0
296 tty_settings->c_oflag |= (OPOST
302 tty_settings->c_cflag &= ~((unsigned) (CSIZE
307 tty_settings->c_cflag |= (CS8 | CREAD);
308 tty_settings->c_lflag &= ~((unsigned) (ECHONL
321 tty_settings->c_lflag |= (ISIG
336 SET_TTY(fd, tty_settings);
341 * Returns a "good" value for the erase character. This is loosely based on
350 && VALID_STRING(key_backspace)
351 && strlen(key_backspace) == 1) {
352 result = key_backspace[0];
361 * Update the values of the erase, interrupt, and kill characters in the TTY
364 * SVr4 tset (e.g., Solaris 2.5) only modifies the intr, quit or erase
365 * characters if they're unset, or if we specify them as options. This differs
366 * from BSD 4.4 tset, which always sets erase.
369 set_control_chars(TTY * tty_settings, int my_erase, int my_intr, int my_kill)
371 #if defined(EXP_WIN32_DRIVER)
378 if (DISABLED(tty_settings->c_cc[VERASE]) || my_erase >= 0) {
379 tty_settings->c_cc[VERASE] = UChar((my_erase >= 0)
384 if (DISABLED(tty_settings->c_cc[VINTR]) || my_intr >= 0) {
385 tty_settings->c_cc[VINTR] = UChar((my_intr >= 0)
390 if (DISABLED(tty_settings->c_cc[VKILL]) || my_kill >= 0) {
391 tty_settings->c_cc[VKILL] = UChar((my_kill >= 0)
399 * Set up various conversions in the TTY parameter, including parity, tabs,
400 * returns, echo, and case, according to the termcap entry.
403 set_conversions(TTY * tty_settings)
405 #if defined(EXP_WIN32_DRIVER)
409 tty_settings->c_oflag |= ONLCR;
411 tty_settings->c_iflag |= ICRNL;
412 tty_settings->c_lflag |= ECHO;
414 tty_settings->c_oflag |= OXTABS;
417 /* test used to be tgetflag("NL") */
418 if (VALID_STRING(newline) && newline[0] == '\n' && !newline[1]) {
419 /* Newline, not linefeed. */
421 tty_settings->c_oflag &= ~((unsigned) ONLCR);
423 tty_settings->c_iflag &= ~((unsigned) ICRNL);
426 /* test used to be tgetflag("pt") */
427 if (VALID_STRING(set_tab) && VALID_STRING(clear_all_tabs))
428 tty_settings->c_oflag &= ~OXTABS;
430 tty_settings->c_lflag |= (ECHOE | ECHOK);
435 sent_string(const char *s)
438 if (VALID_STRING(s)) {
439 tputs(s, 0, out_char);
448 if (VALID_STRING(carriage_return)) {
449 sent_string(carriage_return);
457 * Set the hardware tabs on the terminal, using the 'ct' (clear all tabs),
458 * 'st' (set one tab) and 'ch' (horizontal cursor addressing) capabilities.
459 * This is done before 'if' and 'is', so they can recover in case of error.
461 * Return TRUE if we set any tab stops, FALSE if not.
464 reset_tabstops(int wide)
467 && VALID_NUMERIC(init_tabs)
468 && VALID_STRING(set_tab)
469 && VALID_STRING(clear_all_tabs)) {
473 tputs(clear_all_tabs, 0, out_char);
475 if (init_tabs > wide)
476 init_tabs = (short) wide;
477 for (c = init_tabs; c < wide; c += init_tabs) {
478 fprintf(my_file, "%*s", init_tabs, " ");
479 tputs(set_tab, 0, out_char);
488 /* Output startup string. */
490 send_init_strings(int fd GCC_UNUSED, TTY * old_settings)
493 bool need_flush = FALSE;
497 if (old_settings != 0 &&
498 old_settings->c_oflag & (TAB3 | ONLCR | OCRNL | ONLRET)) {
499 old_settings->c_oflag &= (TAB3 | ONLCR | OCRNL | ONLRET);
500 SET_TTY(fd, old_settings);
503 if (use_reset || use_init) {
504 if (VALID_STRING(init_prog)) {
505 IGNORE_RC(system(init_prog));
508 need_flush |= sent_string((use_reset && (reset_1string != 0))
512 need_flush |= sent_string((use_reset && (reset_2string != 0))
516 if (VALID_STRING(clear_margins)) {
517 need_flush |= sent_string(clear_margins);
519 #if defined(set_lr_margin)
520 if (VALID_STRING(set_lr_margin)) {
521 need_flush |= sent_string(TIPARM_2(set_lr_margin, 0, columns - 1));
524 #if defined(set_left_margin_parm) && defined(set_right_margin_parm)
525 if (VALID_STRING(set_left_margin_parm)
526 && VALID_STRING(set_right_margin_parm)) {
527 need_flush |= sent_string(TIPARM_1(set_left_margin_parm, 0));
528 need_flush |= sent_string(TIPARM_1(set_right_margin_parm,
532 if (VALID_STRING(set_left_margin)
533 && VALID_STRING(set_right_margin)) {
534 need_flush |= to_left_margin();
535 need_flush |= sent_string(set_left_margin);
536 if (VALID_STRING(parm_right_cursor)) {
537 need_flush |= sent_string(TIPARM_1(parm_right_cursor,
540 for (i = 0; i < columns - 1; i++) {
545 need_flush |= sent_string(set_right_margin);
546 need_flush |= to_left_margin();
549 need_flush |= reset_tabstops(columns);
551 need_flush |= cat_file((use_reset && reset_file) ? reset_file : init_file);
553 need_flush |= sent_string((use_reset && (reset_3string != 0))
562 * Tell the user if a control key has been changed from the default value.
565 show_tty_change(TTY * old_settings,
571 unsigned older = 0, newer = 0;
574 #if defined(EXP_WIN32_DRIVER)
582 newer = new_settings->c_cc[which];
583 older = old_settings->c_cc[which];
585 if (older == newer && older == def)
588 (void) fprintf(stderr, "%s %s ", name, older == newer ? "is" : "set to");
590 if (DISABLED(newer)) {
591 (void) fprintf(stderr, "undef.\n");
593 * Check 'delete' before 'backspace', since the key_backspace value
596 } else if (newer == 0177) {
597 (void) fprintf(stderr, "delete.\n");
598 } else if ((p = key_backspace) != 0
599 && newer == (unsigned char) p[0]
601 (void) fprintf(stderr, "backspace.\n");
602 } else if (newer < 040) {
604 (void) fprintf(stderr, "control-%c (^%c).\n", UChar(newer), UChar(newer));
606 (void) fprintf(stderr, "%c.\n", UChar(newer));
609 /**************************************************************************
611 **************************************************************************/
614 reset_start(FILE *fp, bool is_reset, bool is_init)
617 use_reset = is_reset;
629 print_tty_chars(TTY * old_settings, TTY * new_settings)
631 #if defined(EXP_WIN32_DRIVER)
634 show_tty_change(old_settings, new_settings, "Erase", VERASE, CERASE);
635 show_tty_change(old_settings, new_settings, "Kill", VKILL, CKILL);
636 show_tty_change(old_settings, new_settings, "Interrupt", VINTR, CINTR);
642 * Set window size if not set already, but update our copy of the values if the
646 set_window_size(int fd, short *high, short *wide)
649 (void) ioctl(fd, IOCTL_GET_WINSIZE, &win);
650 if (WINSIZE_ROWS(win) == 0 &&
651 WINSIZE_COLS(win) == 0) {
652 if (*high > 0 && *wide > 0) {
653 WINSIZE_ROWS(win) = (unsigned short) *high;
654 WINSIZE_COLS(win) = (unsigned short) *wide;
655 (void) ioctl(fd, IOCTL_SET_WINSIZE, &win);
657 } else if (WINSIZE_ROWS(win) > 0 &&
658 WINSIZE_COLS(win) > 0) {
659 *high = (short) WINSIZE_ROWS(win);
660 *wide = (short) WINSIZE_COLS(win);