1 /****************************************************************************
2 * Copyright 2019-2023,2024 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.37 2024/04/08 17:29: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
78 #define set_flags(target, mask) target |= mask
79 #define clear_flags(target, mask) target &= ~((unsigned)(mask))
83 static bool use_reset = FALSE; /* invoked as reset */
84 static bool use_init = FALSE; /* invoked as init */
86 static GCC_NORETURN void
87 failed(const char *msg)
91 (void) fprintf(stderr, "%s: %s: %s\n", _nc_progname, msg, strerror(code));
92 restore_tty_settings();
93 (void) fprintf(my_file, "\n");
95 ExitProgram(ErrSystem(code));
108 if ((fp = safe_fopen(file, "r")) == 0)
111 while ((nr = fread(buf, sizeof(char), sizeof(buf), fp)) != 0) {
112 if (fwrite(buf, sizeof(char), nr, my_file) != nr) {
125 return putc(c, my_file);
128 /**************************************************************************
130 **************************************************************************/
132 /* some BSD systems have these built in, some systems are missing
133 * one or more definitions. The safest solution is to override unless the
134 * commonly-altered ones are defined.
136 #if !(defined(CERASE) && defined(CINTR) && defined(CKILL) && defined(CQUIT))
149 /* control-character defaults */
151 #define CEOF CTRL('D')
154 #define CERASE CTRL('H')
157 #define CINTR 127 /* ^? */
160 #define CKILL CTRL('U')
163 #define CLNEXT CTRL('v')
166 #define CRPRNT CTRL('r')
169 #define CQUIT CTRL('\\')
172 #define CSTART CTRL('Q')
175 #define CSTOP CTRL('S')
178 #define CSUSP CTRL('Z')
181 #if defined(_POSIX_VDISABLE)
182 #define DISABLED(val) (((_POSIX_VDISABLE != -1) \
183 && ((val) == _POSIX_VDISABLE)) \
186 #define DISABLED(val) ((int)(val) <= 0)
189 #define CHK(val, dft) (unsigned char) (DISABLED(val) ? dft : val)
191 #define reset_char(item, value) \
192 tty_settings->c_cc[item] = CHK(tty_settings->c_cc[item], value)
268 * Reset the terminal mode bits to a sensible state. Very useful after
269 * a child program dies in raw mode.
272 reset_tty_settings(int fd, TTY * tty_settings, int noset)
279 GET_TTY(fd, tty_settings);
282 #if defined(VDISCARD) && defined(CDISCARD)
283 reset_char(VDISCARD, CDISCARD);
285 reset_char(VEOF, CEOF);
286 reset_char(VERASE, CERASE);
287 #if defined(VERASE2) && defined(CERASE2)
288 reset_char(VERASE2, CERASE2);
290 #if defined(VFLUSH) && defined(CFLUSH)
291 reset_char(VFLUSH, CFLUSH);
293 reset_char(VINTR, CINTR);
294 reset_char(VKILL, CKILL);
295 #if defined(VLNEXT) && defined(CLNEXT)
296 reset_char(VLNEXT, CLNEXT);
298 reset_char(VQUIT, CQUIT);
299 #if defined(VREPRINT) && defined(CRPRNT)
300 reset_char(VREPRINT, CRPRNT);
302 #if defined(VSTART) && defined(CSTART)
303 reset_char(VSTART, CSTART);
305 #if defined(VSTOP) && defined(CSTOP)
306 reset_char(VSTOP, CSTOP);
308 #if defined(VSUSP) && defined(CSUSP)
309 reset_char(VSUSP, CSUSP);
311 #if defined(VWERASE) && defined(CWERASE)
312 reset_char(VWERASE, CWERASE);
315 clear_flags(tty_settings->c_iflag, (IGNBRK
325 set_flags(tty_settings->c_iflag, (BRKINT
331 clear_flags(tty_settings->c_oflag, (0
345 set_flags(tty_settings->c_oflag, (OPOST
348 mask = (CSIZE | CSTOPB | PARENB | PARODD);
350 /* leave clocal alone if this appears to use a modem */
351 if (ioctl(fd, TIOCMGET, &modem_bits) == -1)
354 /* cannot check - use the behavior from tset */
357 clear_flags(tty_settings->c_cflag, mask);
359 set_flags(tty_settings->c_cflag, (CS8 | CREAD));
360 clear_flags(tty_settings->c_lflag, (ECHONL
366 set_flags(tty_settings->c_lflag, (ISIG
376 SET_TTY(fd, tty_settings);
381 * Returns a "good" value for the erase character. This is loosely based on
390 && VALID_STRING(key_backspace)
391 && strlen(key_backspace) == 1) {
392 result = key_backspace[0];
401 * Update the values of the erase, interrupt, and kill characters in the TTY
404 * SVr4 tset (e.g., Solaris 2.5) only modifies the intr, quit or erase
405 * characters if they're unset, or if we specify them as options. This differs
406 * from BSD 4.4 tset, which always sets erase.
409 set_control_chars(TTY * tty_settings, int my_erase, int my_intr, int my_kill)
411 #if defined(EXP_WIN32_DRIVER)
418 if (DISABLED(tty_settings->c_cc[VERASE]) || my_erase >= 0) {
419 tty_settings->c_cc[VERASE] = UChar((my_erase >= 0)
424 if (DISABLED(tty_settings->c_cc[VINTR]) || my_intr >= 0) {
425 tty_settings->c_cc[VINTR] = UChar((my_intr >= 0)
430 if (DISABLED(tty_settings->c_cc[VKILL]) || my_kill >= 0) {
431 tty_settings->c_cc[VKILL] = UChar((my_kill >= 0)
439 * Set up various conversions in the TTY parameter, including parity, tabs,
440 * returns, echo, and case, according to the termcap entry.
443 set_conversions(TTY * tty_settings)
445 #if defined(EXP_WIN32_DRIVER)
448 set_flags(tty_settings->c_oflag, ONLCR);
449 set_flags(tty_settings->c_iflag, ICRNL);
450 set_flags(tty_settings->c_lflag, ECHO);
451 set_flags(tty_settings->c_oflag, OXTABS);
453 /* test used to be tgetflag("NL") */
454 if (VALID_STRING(newline) && newline[0] == '\n' && !newline[1]) {
455 /* Newline, not linefeed. */
456 clear_flags(tty_settings->c_oflag, ONLCR);
457 clear_flags(tty_settings->c_iflag, ICRNL);
460 /* test used to be tgetflag("pt") */
461 if (VALID_STRING(set_tab) && VALID_STRING(clear_all_tabs))
462 clear_flags(tty_settings->c_oflag, OXTABS);
464 set_flags(tty_settings->c_lflag, (ECHOE | ECHOK));
469 sent_string(const char *s)
472 if (VALID_STRING(s)) {
473 tputs(s, 0, out_char);
482 if (VALID_STRING(carriage_return)) {
483 sent_string(carriage_return);
491 * Set the hardware tabs on the terminal, using the 'ct' (clear all tabs),
492 * 'st' (set one tab) and 'ch' (horizontal cursor addressing) capabilities.
493 * This is done before 'if' and 'is', so they can recover in case of error.
495 * Return TRUE if we set any tab stops, FALSE if not.
498 reset_tabstops(int wide)
501 && VALID_NUMERIC(init_tabs)
502 && VALID_STRING(set_tab)
503 && VALID_STRING(clear_all_tabs)) {
507 tputs(clear_all_tabs, 0, out_char);
509 if (init_tabs > wide)
510 init_tabs = (short) wide;
511 for (c = init_tabs; c < wide; c += init_tabs) {
512 fprintf(my_file, "%*s", init_tabs, " ");
513 tputs(set_tab, 0, out_char);
522 /* Output startup string. */
524 send_init_strings(int fd GCC_UNUSED, TTY * old_settings)
527 bool need_flush = FALSE;
531 if (old_settings != 0 &&
532 old_settings->c_oflag & (TAB3 | ONLCR | OCRNL | ONLRET)) {
533 old_settings->c_oflag &= (TAB3 | ONLCR | OCRNL | ONLRET);
534 SET_TTY(fd, old_settings);
537 if (use_reset || use_init) {
538 if (VALID_STRING(init_prog)) {
539 IGNORE_RC(system(init_prog));
542 need_flush |= sent_string((use_reset && (reset_1string != 0))
546 need_flush |= sent_string((use_reset && (reset_2string != 0))
550 if (VALID_STRING(clear_margins)) {
551 need_flush |= sent_string(clear_margins);
553 #if defined(set_lr_margin)
554 else if (VALID_STRING(set_lr_margin)) {
555 need_flush |= sent_string(TIPARM_2(set_lr_margin, 0, columns - 1));
558 #if defined(set_left_margin_parm) && defined(set_right_margin_parm)
559 else if (VALID_STRING(set_left_margin_parm)
560 && VALID_STRING(set_right_margin_parm)) {
561 need_flush |= sent_string(TIPARM_1(set_left_margin_parm, 0));
562 need_flush |= sent_string(TIPARM_1(set_right_margin_parm,
566 else if (VALID_STRING(set_left_margin)
567 && VALID_STRING(set_right_margin)) {
568 need_flush |= to_left_margin();
569 need_flush |= sent_string(set_left_margin);
570 if (VALID_STRING(parm_right_cursor)) {
571 need_flush |= sent_string(TIPARM_1(parm_right_cursor,
574 for (i = 0; i < columns - 1; i++) {
579 need_flush |= sent_string(set_right_margin);
580 need_flush |= to_left_margin();
583 need_flush |= reset_tabstops(columns);
585 need_flush |= cat_file((use_reset && reset_file) ? reset_file : init_file);
587 need_flush |= sent_string((use_reset && (reset_3string != 0))
596 * Tell the user if a control key has been changed from the default value.
599 show_tty_change(TTY * old_settings,
605 unsigned older = 0, newer = 0;
608 #if defined(EXP_WIN32_DRIVER)
616 newer = new_settings->c_cc[which];
617 older = old_settings->c_cc[which];
619 if (older == newer && older == def)
622 (void) fprintf(stderr, "%s %s ", name, older == newer ? "is" : "set to");
624 if (DISABLED(newer)) {
625 (void) fprintf(stderr, "undef.\n");
627 * Check 'delete' before 'backspace', since the key_backspace value
630 } else if (newer == 0177) {
631 (void) fprintf(stderr, "delete.\n");
632 } else if ((p = key_backspace) != 0
633 && newer == (unsigned char) p[0]
635 (void) fprintf(stderr, "backspace.\n");
636 } else if (newer < 040) {
638 (void) fprintf(stderr, "control-%c (^%c).\n", UChar(newer), UChar(newer));
640 (void) fprintf(stderr, "%c.\n", UChar(newer));
643 /**************************************************************************
645 **************************************************************************/
648 reset_start(FILE *fp, bool is_reset, bool is_init)
651 use_reset = is_reset;
663 print_tty_chars(TTY * old_settings, TTY * new_settings)
665 #if defined(EXP_WIN32_DRIVER)
668 show_tty_change(old_settings, new_settings, "Erase", VERASE, CERASE);
669 show_tty_change(old_settings, new_settings, "Kill", VKILL, CKILL);
670 show_tty_change(old_settings, new_settings, "Interrupt", VINTR, CINTR);
676 * Set window size if not set already, but update our copy of the values if the
680 set_window_size(int fd, NCURSES_INT2 *high, NCURSES_INT2 *wide)
683 (void) ioctl(fd, IOCTL_GET_WINSIZE, &win);
684 if (WINSIZE_ROWS(win) == 0 &&
685 WINSIZE_COLS(win) == 0) {
686 if (*high > 0 && *wide > 0) {
687 WINSIZE_ROWS(win) = (unsigned short) *high;
688 WINSIZE_COLS(win) = (unsigned short) *wide;
689 (void) ioctl(fd, IOCTL_SET_WINSIZE, &win);
691 } else if (WINSIZE_ROWS(win) > 0 &&
692 WINSIZE_COLS(win) > 0) {
693 *high = (short) WINSIZE_ROWS(win);
694 *wide = (short) WINSIZE_COLS(win);