1 /****************************************************************************
2 * Copyright (c) 2016-2017,2019 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Thomas E. Dickey *
31 ****************************************************************************/
33 #include <reset_cmd.h>
34 #include <tty_settings.h>
41 # if !defined(sun) || !TERMIOS
43 # include <sys/ioctl.h>
49 /* they neglected to define struct winsize in termios.h -- it's only
51 #include <sys/stream.h>
55 MODULE_ID("$Id: reset_cmd.c,v 1.18 2019/07/13 21:35:13 tom Exp $")
58 * SCO defines TIOCGSIZE and the corresponding struct. Other systems (SunOS,
59 * Solaris, IRIX) define TIOCGWINSZ and struct winsize.
62 # define IOCTL_GET_WINSIZE TIOCGSIZE
63 # define IOCTL_SET_WINSIZE TIOCSSIZE
64 # define STRUCT_WINSIZE struct ttysize
65 # define WINSIZE_ROWS(n) n.ts_lines
66 # define WINSIZE_COLS(n) n.ts_cols
69 # define IOCTL_GET_WINSIZE TIOCGWINSZ
70 # define IOCTL_SET_WINSIZE TIOCSWINSZ
71 # define STRUCT_WINSIZE struct winsize
72 # define WINSIZE_ROWS(n) n.ws_row
73 # define WINSIZE_COLS(n) n.ws_col
79 static bool use_reset = FALSE; /* invoked as reset */
80 static bool use_init = FALSE; /* invoked as init */
83 failed(const char *msg)
87 (void) fprintf(stderr, "%s: %s: %s\n", _nc_progname, msg, strerror(code));
88 restore_tty_settings();
89 (void) fprintf(my_file, "\n");
91 ExitProgram(ErrSystem(code));
104 if ((fp = fopen(file, "r")) == 0)
107 while ((nr = fread(buf, sizeof(char), sizeof(buf), fp)) != 0) {
108 if (fwrite(buf, sizeof(char), nr, my_file) != nr) {
121 return putc(c, my_file);
124 /**************************************************************************
126 **************************************************************************/
128 /* some BSD systems have these built in, some systems are missing
129 * one or more definitions. The safest solution is to override unless the
130 * commonly-altered ones are defined.
132 #if !(defined(CERASE) && defined(CINTR) && defined(CKILL) && defined(CQUIT))
145 /* control-character defaults */
147 #define CEOF CTRL('D')
150 #define CERASE CTRL('H')
153 #define CINTR 127 /* ^? */
156 #define CKILL CTRL('U')
159 #define CLNEXT CTRL('v')
162 #define CRPRNT CTRL('r')
165 #define CQUIT CTRL('\\')
168 #define CSTART CTRL('Q')
171 #define CSTOP CTRL('S')
174 #define CSUSP CTRL('Z')
177 #if defined(_POSIX_VDISABLE)
178 #define DISABLED(val) (((_POSIX_VDISABLE != -1) \
179 && ((val) == _POSIX_VDISABLE)) \
182 #define DISABLED(val) ((int)(val) <= 0)
185 #define CHK(val, dft) (unsigned char) (DISABLED(val) ? dft : val)
187 #define reset_char(item, value) \
188 tty_settings->c_cc[item] = CHK(tty_settings->c_cc[item], value)
191 * Reset the terminal mode bits to a sensible state. Very useful after
192 * a child program dies in raw mode.
195 reset_tty_settings(int fd, TTY * tty_settings)
197 GET_TTY(fd, tty_settings);
200 #if defined(VDISCARD) && defined(CDISCARD)
201 reset_char(VDISCARD, CDISCARD);
203 reset_char(VEOF, CEOF);
204 reset_char(VERASE, CERASE);
205 #if defined(VFLUSH) && defined(CFLUSH)
206 reset_char(VFLUSH, CFLUSH);
208 reset_char(VINTR, CINTR);
209 reset_char(VKILL, CKILL);
210 #if defined(VLNEXT) && defined(CLNEXT)
211 reset_char(VLNEXT, CLNEXT);
213 reset_char(VQUIT, CQUIT);
214 #if defined(VREPRINT) && defined(CRPRNT)
215 reset_char(VREPRINT, CRPRNT);
217 #if defined(VSTART) && defined(CSTART)
218 reset_char(VSTART, CSTART);
220 #if defined(VSTOP) && defined(CSTOP)
221 reset_char(VSTOP, CSTOP);
223 #if defined(VSUSP) && defined(CSUSP)
224 reset_char(VSUSP, CSUSP);
226 #if defined(VWERASE) && defined(CWERASE)
227 reset_char(VWERASE, CWERASE);
230 tty_settings->c_iflag &= ~((unsigned) (IGNBRK
244 tty_settings->c_iflag |= (BRKINT
253 tty_settings->c_oflag &= ~((unsigned) (0
292 tty_settings->c_oflag |= (OPOST
298 tty_settings->c_cflag &= ~((unsigned) (CSIZE
303 tty_settings->c_cflag |= (CS8 | CREAD);
304 tty_settings->c_lflag &= ~((unsigned) (ECHONL
317 tty_settings->c_lflag |= (ISIG
331 SET_TTY(fd, tty_settings);
335 * Returns a "good" value for the erase character. This is loosely based on
344 && VALID_STRING(key_backspace)
345 && strlen(key_backspace) == 1) {
346 result = key_backspace[0];
355 * Update the values of the erase, interrupt, and kill characters in the TTY
358 * SVr4 tset (e.g., Solaris 2.5) only modifies the intr, quit or erase
359 * characters if they're unset, or if we specify them as options. This differs
360 * from BSD 4.4 tset, which always sets erase.
363 set_control_chars(TTY * tty_settings, int my_erase, int my_intr, int my_kill)
365 if (DISABLED(tty_settings->c_cc[VERASE]) || my_erase >= 0) {
366 tty_settings->c_cc[VERASE] = UChar((my_erase >= 0)
371 if (DISABLED(tty_settings->c_cc[VINTR]) || my_intr >= 0) {
372 tty_settings->c_cc[VINTR] = UChar((my_intr >= 0)
377 if (DISABLED(tty_settings->c_cc[VKILL]) || my_kill >= 0) {
378 tty_settings->c_cc[VKILL] = UChar((my_kill >= 0)
385 * Set up various conversions in the TTY parameter, including parity, tabs,
386 * returns, echo, and case, according to the termcap entry.
389 set_conversions(TTY * tty_settings)
392 tty_settings->c_oflag |= ONLCR;
394 tty_settings->c_iflag |= ICRNL;
395 tty_settings->c_lflag |= ECHO;
397 tty_settings->c_oflag |= OXTABS;
400 /* test used to be tgetflag("NL") */
401 if (VALID_STRING(newline) && newline[0] == '\n' && !newline[1]) {
402 /* Newline, not linefeed. */
404 tty_settings->c_oflag &= ~((unsigned) ONLCR);
406 tty_settings->c_iflag &= ~((unsigned) ICRNL);
409 /* test used to be tgetflag("pt") */
410 if (VALID_STRING(set_tab) && VALID_STRING(clear_all_tabs))
411 tty_settings->c_oflag &= ~OXTABS;
413 tty_settings->c_lflag |= (ECHOE | ECHOK);
417 sent_string(const char *s)
420 if (VALID_STRING(s)) {
421 tputs(s, 0, out_char);
430 if (VALID_STRING(carriage_return)) {
431 sent_string(carriage_return);
439 * Set the hardware tabs on the terminal, using the 'ct' (clear all tabs),
440 * 'st' (set one tab) and 'ch' (horizontal cursor addressing) capabilities.
441 * This is done before 'if' and 'is', so they can recover in case of error.
443 * Return TRUE if we set any tab stops, FALSE if not.
446 reset_tabstops(int wide)
449 && VALID_NUMERIC(init_tabs)
450 && VALID_STRING(set_tab)
451 && VALID_STRING(clear_all_tabs)) {
455 tputs(clear_all_tabs, 0, out_char);
457 if (init_tabs > wide)
458 init_tabs = (short) wide;
459 for (c = init_tabs; c < wide; c += init_tabs) {
460 fprintf(my_file, "%*s", init_tabs, " ");
461 tputs(set_tab, 0, out_char);
470 /* Output startup string. */
472 send_init_strings(int fd GCC_UNUSED, TTY * old_settings)
475 bool need_flush = FALSE;
479 if (old_settings != 0 &&
480 old_settings->c_oflag & (TAB3 | ONLCR | OCRNL | ONLRET)) {
481 old_settings->c_oflag &= (TAB3 | ONLCR | OCRNL | ONLRET);
482 SET_TTY(fd, old_settings);
485 if (use_reset || use_init) {
486 if (VALID_STRING(init_prog)) {
487 IGNORE_RC(system(init_prog));
490 need_flush |= sent_string((use_reset && (reset_1string != 0))
494 need_flush |= sent_string((use_reset && (reset_2string != 0))
498 if (VALID_STRING(clear_margins)) {
499 need_flush |= sent_string(clear_margins);
501 #if defined(set_lr_margin)
502 if (VALID_STRING(set_lr_margin)) {
503 need_flush |= sent_string(TPARM_2(set_lr_margin, 0,
507 #if defined(set_left_margin_parm) && defined(set_right_margin_parm)
508 if (VALID_STRING(set_left_margin_parm)
509 && VALID_STRING(set_right_margin_parm)) {
510 need_flush |= sent_string(TPARM_1(set_left_margin_parm, 0));
511 need_flush |= sent_string(TPARM_1(set_right_margin_parm,
515 if (VALID_STRING(set_left_margin)
516 && VALID_STRING(set_right_margin)) {
517 need_flush |= to_left_margin();
518 need_flush |= sent_string(set_left_margin);
519 if (VALID_STRING(parm_right_cursor)) {
520 need_flush |= sent_string(TPARM_1(parm_right_cursor,
523 for (i = 0; i < columns - 1; i++) {
528 need_flush |= sent_string(set_right_margin);
529 need_flush |= to_left_margin();
532 need_flush |= reset_tabstops(columns);
534 need_flush |= cat_file((use_reset && reset_file) ? reset_file : init_file);
536 need_flush |= sent_string((use_reset && (reset_3string != 0))
545 * Tell the user if a control key has been changed from the default value.
548 show_tty_change(TTY * old_settings,
554 unsigned older, newer;
557 newer = new_settings->c_cc[which];
558 older = old_settings->c_cc[which];
560 if (older == newer && older == def)
563 (void) fprintf(stderr, "%s %s ", name, older == newer ? "is" : "set to");
565 if (DISABLED(newer)) {
566 (void) fprintf(stderr, "undef.\n");
568 * Check 'delete' before 'backspace', since the key_backspace value
571 } else if (newer == 0177) {
572 (void) fprintf(stderr, "delete.\n");
573 } else if ((p = key_backspace) != 0
574 && newer == (unsigned char) p[0]
576 (void) fprintf(stderr, "backspace.\n");
577 } else if (newer < 040) {
579 (void) fprintf(stderr, "control-%c (^%c).\n", UChar(newer), UChar(newer));
581 (void) fprintf(stderr, "%c.\n", UChar(newer));
584 /**************************************************************************
586 **************************************************************************/
589 reset_start(FILE *fp, bool is_reset, bool is_init)
592 use_reset = is_reset;
604 print_tty_chars(TTY * old_settings, TTY * new_settings)
606 show_tty_change(old_settings, new_settings, "Erase", VERASE, CERASE);
607 show_tty_change(old_settings, new_settings, "Kill", VKILL, CKILL);
608 show_tty_change(old_settings, new_settings, "Interrupt", VINTR, CINTR);
613 * Set window size if not set already, but update our copy of the values if the
617 set_window_size(int fd, short *high, short *wide)
620 (void) ioctl(fd, IOCTL_GET_WINSIZE, &win);
621 if (WINSIZE_ROWS(win) == 0 &&
622 WINSIZE_COLS(win) == 0) {
623 if (*high > 0 && *wide > 0) {
624 WINSIZE_ROWS(win) = (unsigned short) *high;
625 WINSIZE_COLS(win) = (unsigned short) *wide;
626 (void) ioctl(fd, IOCTL_SET_WINSIZE, &win);
628 } else if (WINSIZE_ROWS(win) > 0 &&
629 WINSIZE_COLS(win) > 0) {
630 *high = (short) WINSIZE_ROWS(win);
631 *wide = (short) WINSIZE_COLS(win);