1 /****************************************************************************
2 * Copyright 2019-2020,2021 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's only
52 #include <sys/stream.h>
56 MODULE_ID("$Id: reset_cmd.c,v 1.25 2021/03/20 18:23:14 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 = 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)
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(VFLUSH) && defined(CFLUSH)
207 reset_char(VFLUSH, CFLUSH);
209 reset_char(VINTR, CINTR);
210 reset_char(VKILL, CKILL);
211 #if defined(VLNEXT) && defined(CLNEXT)
212 reset_char(VLNEXT, CLNEXT);
214 reset_char(VQUIT, CQUIT);
215 #if defined(VREPRINT) && defined(CRPRNT)
216 reset_char(VREPRINT, CRPRNT);
218 #if defined(VSTART) && defined(CSTART)
219 reset_char(VSTART, CSTART);
221 #if defined(VSTOP) && defined(CSTOP)
222 reset_char(VSTOP, CSTOP);
224 #if defined(VSUSP) && defined(CSUSP)
225 reset_char(VSUSP, CSUSP);
227 #if defined(VWERASE) && defined(CWERASE)
228 reset_char(VWERASE, CWERASE);
231 tty_settings->c_iflag &= ~((unsigned) (IGNBRK
245 tty_settings->c_iflag |= (BRKINT
254 tty_settings->c_oflag &= ~((unsigned) (0
293 tty_settings->c_oflag |= (OPOST
299 tty_settings->c_cflag &= ~((unsigned) (CSIZE
304 tty_settings->c_cflag |= (CS8 | CREAD);
305 tty_settings->c_lflag &= ~((unsigned) (ECHONL
318 tty_settings->c_lflag |= (ISIG
332 SET_TTY(fd, tty_settings);
336 * Returns a "good" value for the erase character. This is loosely based on
345 && VALID_STRING(key_backspace)
346 && strlen(key_backspace) == 1) {
347 result = key_backspace[0];
356 * Update the values of the erase, interrupt, and kill characters in the TTY
359 * SVr4 tset (e.g., Solaris 2.5) only modifies the intr, quit or erase
360 * characters if they're unset, or if we specify them as options. This differs
361 * from BSD 4.4 tset, which always sets erase.
364 set_control_chars(TTY * tty_settings, int my_erase, int my_intr, int my_kill)
366 #if defined(EXP_WIN32_DRIVER)
373 if (DISABLED(tty_settings->c_cc[VERASE]) || my_erase >= 0) {
374 tty_settings->c_cc[VERASE] = UChar((my_erase >= 0)
379 if (DISABLED(tty_settings->c_cc[VINTR]) || my_intr >= 0) {
380 tty_settings->c_cc[VINTR] = UChar((my_intr >= 0)
385 if (DISABLED(tty_settings->c_cc[VKILL]) || my_kill >= 0) {
386 tty_settings->c_cc[VKILL] = UChar((my_kill >= 0)
394 * Set up various conversions in the TTY parameter, including parity, tabs,
395 * returns, echo, and case, according to the termcap entry.
398 set_conversions(TTY * tty_settings)
400 #if defined(EXP_WIN32_DRIVER)
404 tty_settings->c_oflag |= ONLCR;
406 tty_settings->c_iflag |= ICRNL;
407 tty_settings->c_lflag |= ECHO;
409 tty_settings->c_oflag |= OXTABS;
412 /* test used to be tgetflag("NL") */
413 if (VALID_STRING(newline) && newline[0] == '\n' && !newline[1]) {
414 /* Newline, not linefeed. */
416 tty_settings->c_oflag &= ~((unsigned) ONLCR);
418 tty_settings->c_iflag &= ~((unsigned) ICRNL);
421 /* test used to be tgetflag("pt") */
422 if (VALID_STRING(set_tab) && VALID_STRING(clear_all_tabs))
423 tty_settings->c_oflag &= ~OXTABS;
425 tty_settings->c_lflag |= (ECHOE | ECHOK);
430 sent_string(const char *s)
433 if (VALID_STRING(s)) {
434 tputs(s, 0, out_char);
443 if (VALID_STRING(carriage_return)) {
444 sent_string(carriage_return);
452 * Set the hardware tabs on the terminal, using the 'ct' (clear all tabs),
453 * 'st' (set one tab) and 'ch' (horizontal cursor addressing) capabilities.
454 * This is done before 'if' and 'is', so they can recover in case of error.
456 * Return TRUE if we set any tab stops, FALSE if not.
459 reset_tabstops(int wide)
462 && VALID_NUMERIC(init_tabs)
463 && VALID_STRING(set_tab)
464 && VALID_STRING(clear_all_tabs)) {
468 tputs(clear_all_tabs, 0, out_char);
470 if (init_tabs > wide)
471 init_tabs = (short) wide;
472 for (c = init_tabs; c < wide; c += init_tabs) {
473 fprintf(my_file, "%*s", init_tabs, " ");
474 tputs(set_tab, 0, out_char);
483 /* Output startup string. */
485 send_init_strings(int fd GCC_UNUSED, TTY * old_settings)
488 bool need_flush = FALSE;
492 if (old_settings != 0 &&
493 old_settings->c_oflag & (TAB3 | ONLCR | OCRNL | ONLRET)) {
494 old_settings->c_oflag &= (TAB3 | ONLCR | OCRNL | ONLRET);
495 SET_TTY(fd, old_settings);
498 if (use_reset || use_init) {
499 if (VALID_STRING(init_prog)) {
500 IGNORE_RC(system(init_prog));
503 need_flush |= sent_string((use_reset && (reset_1string != 0))
507 need_flush |= sent_string((use_reset && (reset_2string != 0))
511 if (VALID_STRING(clear_margins)) {
512 need_flush |= sent_string(clear_margins);
514 #if defined(set_lr_margin)
515 if (VALID_STRING(set_lr_margin)) {
516 need_flush |= sent_string(TIPARM_2(set_lr_margin, 0, columns - 1));
519 #if defined(set_left_margin_parm) && defined(set_right_margin_parm)
520 if (VALID_STRING(set_left_margin_parm)
521 && VALID_STRING(set_right_margin_parm)) {
522 need_flush |= sent_string(TIPARM_1(set_left_margin_parm, 0));
523 need_flush |= sent_string(TIPARM_1(set_right_margin_parm,
527 if (VALID_STRING(set_left_margin)
528 && VALID_STRING(set_right_margin)) {
529 need_flush |= to_left_margin();
530 need_flush |= sent_string(set_left_margin);
531 if (VALID_STRING(parm_right_cursor)) {
532 need_flush |= sent_string(TIPARM_1(parm_right_cursor,
535 for (i = 0; i < columns - 1; i++) {
540 need_flush |= sent_string(set_right_margin);
541 need_flush |= to_left_margin();
544 need_flush |= reset_tabstops(columns);
546 need_flush |= cat_file((use_reset && reset_file) ? reset_file : init_file);
548 need_flush |= sent_string((use_reset && (reset_3string != 0))
557 * Tell the user if a control key has been changed from the default value.
560 show_tty_change(TTY * old_settings,
566 unsigned older = 0, newer = 0;
569 #if defined(EXP_WIN32_DRIVER)
577 newer = new_settings->c_cc[which];
578 older = old_settings->c_cc[which];
580 if (older == newer && older == def)
583 (void) fprintf(stderr, "%s %s ", name, older == newer ? "is" : "set to");
585 if (DISABLED(newer)) {
586 (void) fprintf(stderr, "undef.\n");
588 * Check 'delete' before 'backspace', since the key_backspace value
591 } else if (newer == 0177) {
592 (void) fprintf(stderr, "delete.\n");
593 } else if ((p = key_backspace) != 0
594 && newer == (unsigned char) p[0]
596 (void) fprintf(stderr, "backspace.\n");
597 } else if (newer < 040) {
599 (void) fprintf(stderr, "control-%c (^%c).\n", UChar(newer), UChar(newer));
601 (void) fprintf(stderr, "%c.\n", UChar(newer));
604 /**************************************************************************
606 **************************************************************************/
609 reset_start(FILE *fp, bool is_reset, bool is_init)
612 use_reset = is_reset;
624 print_tty_chars(TTY * old_settings, TTY * new_settings)
626 #if defined(EXP_WIN32_DRIVER)
629 show_tty_change(old_settings, new_settings, "Erase", VERASE, CERASE);
630 show_tty_change(old_settings, new_settings, "Kill", VKILL, CKILL);
631 show_tty_change(old_settings, new_settings, "Interrupt", VINTR, CINTR);
637 * Set window size if not set already, but update our copy of the values if the
641 set_window_size(int fd, short *high, short *wide)
644 (void) ioctl(fd, IOCTL_GET_WINSIZE, &win);
645 if (WINSIZE_ROWS(win) == 0 &&
646 WINSIZE_COLS(win) == 0) {
647 if (*high > 0 && *wide > 0) {
648 WINSIZE_ROWS(win) = (unsigned short) *high;
649 WINSIZE_COLS(win) = (unsigned short) *wide;
650 (void) ioctl(fd, IOCTL_SET_WINSIZE, &win);
652 } else if (WINSIZE_ROWS(win) > 0 &&
653 WINSIZE_COLS(win) > 0) {
654 *high = (short) WINSIZE_ROWS(win);
655 *wide = (short) WINSIZE_COLS(win);