1 /****************************************************************************
2 * Copyright (c) 2016 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>
40 # if !defined(sun) || !TERMIOS
42 # include <sys/ioctl.h>
48 /* they neglected to define struct winsize in termios.h -- it's only
50 #include <sys/stream.h>
54 MODULE_ID("$Id: reset_cmd.c,v 1.9 2016/10/23 01:08:11 tom Exp $")
57 * SCO defines TIOCGSIZE and the corresponding struct. Other systems (SunOS,
58 * Solaris, IRIX) define TIOCGWINSZ and struct winsize.
61 # define IOCTL_GET_WINSIZE TIOCGSIZE
62 # define IOCTL_SET_WINSIZE TIOCSSIZE
63 # define STRUCT_WINSIZE struct ttysize
64 # define WINSIZE_ROWS(n) n.ts_lines
65 # define WINSIZE_COLS(n) n.ts_cols
68 # define IOCTL_GET_WINSIZE TIOCGWINSZ
69 # define IOCTL_SET_WINSIZE TIOCSWINSZ
70 # define STRUCT_WINSIZE struct winsize
71 # define WINSIZE_ROWS(n) n.ws_row
72 # define WINSIZE_COLS(n) n.ws_col
78 static TTY original_settings;
80 static bool can_restore = FALSE;
81 static bool use_reset = FALSE; /* invoked as reset */
82 static bool use_init = FALSE; /* invoked as init */
87 restore_tty_settings();
88 (void) fprintf(my_file, "\n");
90 ExitProgram(EXIT_FAILURE);
95 failed(const char *msg)
99 _nc_STRCPY(temp, _nc_progname, sizeof(temp));
100 _nc_STRCAT(temp, ": ", sizeof(temp));
101 _nc_STRNCAT(temp, msg, sizeof(temp), sizeof(temp) - strlen(temp) - 2);
108 get_tty_settings(int fd, TTY * tty_settings)
112 if (fd < 0 || GET_TTY(my_fd, tty_settings) < 0) {
127 if ((fp = fopen(file, "r")) == 0)
130 while ((nr = fread(buf, sizeof(char), sizeof(buf), fp)) != 0) {
131 if (fwrite(buf, sizeof(char), nr, my_file) != nr) {
144 return putc(c, my_file);
147 /**************************************************************************
149 **************************************************************************/
151 /* some BSD systems have these built in, some systems are missing
152 * one or more definitions. The safest solution is to override unless the
153 * commonly-altered ones are defined.
155 #if !(defined(CERASE) && defined(CINTR) && defined(CKILL) && defined(CQUIT))
168 /* control-character defaults */
170 #define CEOF CTRL('D')
173 #define CERASE CTRL('H')
176 #define CINTR 127 /* ^? */
179 #define CKILL CTRL('U')
182 #define CLNEXT CTRL('v')
185 #define CRPRNT CTRL('r')
188 #define CQUIT CTRL('\\')
191 #define CSTART CTRL('Q')
194 #define CSTOP CTRL('S')
197 #define CSUSP CTRL('Z')
200 #if defined(_POSIX_VDISABLE)
201 #define DISABLED(val) (((_POSIX_VDISABLE != -1) \
202 && ((val) == _POSIX_VDISABLE)) \
205 #define DISABLED(val) ((int)(val) <= 0)
208 #define CHK(val, dft) (unsigned char) (DISABLED(val) ? dft : val)
210 #define reset_char(item, value) \
211 tty_settings->c_cc[item] = CHK(tty_settings->c_cc[item], value)
214 * Reset the terminal mode bits to a sensible state. Very useful after
215 * a child program dies in raw mode.
218 reset_tty_settings(TTY * tty_settings)
221 tcgetattr(my_fd, tty_settings);
223 stty(my_fd, tty_settings);
227 #if defined(VDISCARD) && defined(CDISCARD)
228 reset_char(VDISCARD, CDISCARD);
230 reset_char(VEOF, CEOF);
231 reset_char(VERASE, CERASE);
232 #if defined(VFLUSH) && defined(CFLUSH)
233 reset_char(VFLUSH, CFLUSH);
235 reset_char(VINTR, CINTR);
236 reset_char(VKILL, CKILL);
237 #if defined(VLNEXT) && defined(CLNEXT)
238 reset_char(VLNEXT, CLNEXT);
240 reset_char(VQUIT, CQUIT);
241 #if defined(VREPRINT) && defined(CRPRNT)
242 reset_char(VREPRINT, CRPRNT);
244 #if defined(VSTART) && defined(CSTART)
245 reset_char(VSTART, CSTART);
247 #if defined(VSTOP) && defined(CSTOP)
248 reset_char(VSTOP, CSTOP);
250 #if defined(VSUSP) && defined(CSUSP)
251 reset_char(VSUSP, CSUSP);
253 #if defined(VWERASE) && defined(CWERASE)
254 reset_char(VWERASE, CWERASE);
257 tty_settings->c_iflag &= ~((unsigned) (IGNBRK
271 tty_settings->c_iflag |= (BRKINT
280 tty_settings->c_oflag &= ~((unsigned) (0
319 tty_settings->c_oflag |= (OPOST
325 tty_settings->c_cflag &= ~((unsigned) (CSIZE
330 tty_settings->c_cflag |= (CS8 | CREAD);
331 tty_settings->c_lflag &= ~((unsigned) (ECHONL
344 tty_settings->c_lflag |= (ISIG
358 SET_TTY(my_fd, tty_settings);
362 * Returns a "good" value for the erase character. This is loosely based on
371 && key_backspace != 0
372 && strlen(key_backspace) == 1) {
373 result = key_backspace[0];
382 * Update the values of the erase, interrupt, and kill characters in the TTY
385 * SVr4 tset (e.g., Solaris 2.5) only modifies the intr, quit or erase
386 * characters if they're unset, or if we specify them as options. This differs
387 * from BSD 4.4 tset, which always sets erase.
390 set_control_chars(TTY * tty_settings, int my_erase, int my_intr, int my_kill)
392 if (DISABLED(tty_settings->c_cc[VERASE]) || my_erase >= 0) {
393 tty_settings->c_cc[VERASE] = UChar((my_erase >= 0)
398 if (DISABLED(tty_settings->c_cc[VINTR]) || my_intr >= 0) {
399 tty_settings->c_cc[VINTR] = UChar((my_intr >= 0)
404 if (DISABLED(tty_settings->c_cc[VKILL]) || my_kill >= 0) {
405 tty_settings->c_cc[VKILL] = UChar((my_kill >= 0)
412 * Set up various conversions in the TTY parameter, including parity, tabs,
413 * returns, echo, and case, according to the termcap entry.
416 set_conversions(TTY * tty_settings)
419 tty_settings->c_oflag |= ONLCR;
421 tty_settings->c_iflag |= ICRNL;
422 tty_settings->c_lflag |= ECHO;
424 tty_settings->c_oflag |= OXTABS;
427 /* test used to be tgetflag("NL") */
428 if (newline != (char *) 0 && newline[0] == '\n' && !newline[1]) {
429 /* Newline, not linefeed. */
431 tty_settings->c_oflag &= ~((unsigned) ONLCR);
433 tty_settings->c_iflag &= ~((unsigned) ICRNL);
436 /* test used to be tgetflag("pt") */
437 if (has_hardware_tabs) /* Print tabs. */
438 tty_settings->c_oflag &= ~OXTABS;
440 tty_settings->c_lflag |= (ECHOE | ECHOK);
444 * Set the hardware tabs on the terminal, using the 'ct' (clear all tabs),
445 * 'st' (set one tab) and 'ch' (horizontal cursor addressing) capabilities.
446 * This is done before 'if' and 'is', so they can recover in case of error.
448 * Return TRUE if we set any tab stops, FALSE if not.
451 reset_tabstops(int wide)
453 if ((init_tabs != 8) && (set_tab && clear_all_tabs)) {
456 (void) putc('\r', my_file); /* Force to left margin. */
457 tputs(clear_all_tabs, 0, out_char);
459 for (c = 8; c < wide; c += 8) {
460 /* Get to the right column. In BSD tset, this used to try a bunch
461 * of half-clever things with cup and hpa, for an average saving of
462 * somewhat less than two character times per tab stop, less than
463 * .01 sec at 2400cps. We lost all this cruft because it seemed to
464 * be introducing some odd bugs.
465 * -----------12345678----------- */
466 (void) fputs(" ", my_file);
467 tputs(set_tab, 0, out_char);
476 sent_string(const char *s)
480 tputs(s, 0, out_char);
486 #define PUTCHAR(c) fputc(c, my_file)
488 /* Output startup string. */
490 send_init_strings(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(my_fd, old_settings);
503 if (use_reset || use_init) {
504 if (init_prog != 0) {
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 (set_lr_margin != 0) {
517 need_flush |= sent_string(TPARM_2(set_lr_margin, 0,
519 } else if (set_left_margin_parm != 0
520 && set_right_margin_parm != 0) {
521 need_flush |= sent_string(TPARM_1(set_left_margin_parm, 0));
522 need_flush |= sent_string(TPARM_1(set_right_margin_parm,
524 } else if (clear_margins != 0
525 && set_left_margin != 0
526 && set_right_margin != 0) {
527 need_flush |= sent_string(clear_margins);
528 if (carriage_return != 0) {
529 need_flush |= sent_string(carriage_return);
533 need_flush |= sent_string(set_left_margin);
534 if (parm_right_cursor) {
535 need_flush |= sent_string(TPARM_1(parm_right_cursor,
538 for (i = 0; i < columns - 1; i++) {
542 need_flush |= sent_string(set_right_margin);
543 if (carriage_return != 0) {
544 need_flush |= sent_string(carriage_return);
550 need_flush |= reset_tabstops(columns);
552 need_flush |= cat_file((use_reset && reset_file) ? reset_file : init_file);
554 need_flush |= sent_string((use_reset && (reset_3string != 0))
563 * Tell the user if a control key has been changed from the default value.
566 show_tty_change(TTY * old_settings,
572 unsigned older, newer;
575 newer = new_settings->c_cc[which];
576 older = old_settings->c_cc[which];
578 if (older == newer && older == def)
581 (void) fprintf(stderr, "%s %s ", name, older == newer ? "is" : "set to");
583 if (DISABLED(newer)) {
584 (void) fprintf(stderr, "undef.\n");
586 * Check 'delete' before 'backspace', since the key_backspace value
589 } else if (newer == 0177) {
590 (void) fprintf(stderr, "delete.\n");
591 } else if ((p = key_backspace) != 0
592 && newer == (unsigned char) p[0]
594 (void) fprintf(stderr, "backspace.\n");
595 } else if (newer < 040) {
597 (void) fprintf(stderr, "control-%c (^%c).\n", UChar(newer), UChar(newer));
599 (void) fprintf(stderr, "%c.\n", UChar(newer));
602 /**************************************************************************
604 **************************************************************************/
607 reset_start(FILE *fp, bool is_reset, bool is_init)
610 use_reset = is_reset;
622 print_tty_chars(TTY * old_settings, TTY * new_settings)
624 show_tty_change(old_settings, new_settings, "Erase", VERASE, CERASE);
625 show_tty_change(old_settings, new_settings, "Kill", VKILL, CKILL);
626 show_tty_change(old_settings, new_settings, "Interrupt", VINTR, CINTR);
630 * Open a file descriptor on the current terminal, to obtain its settings.
631 * stderr is less likely to be redirected than stdout; try that first.
634 save_tty_settings(TTY * tty_settings)
636 if (!get_tty_settings(STDERR_FILENO, tty_settings) &&
637 !get_tty_settings(STDOUT_FILENO, tty_settings) &&
638 !get_tty_settings(STDIN_FILENO, tty_settings) &&
639 !get_tty_settings(open("/dev/tty", O_RDWR), tty_settings)) {
640 failed("terminal attributes");
643 original_settings = *tty_settings;
648 restore_tty_settings(void)
651 SET_TTY(my_fd, &original_settings);
654 /* Set the modes if they've changed. */
656 update_tty_settings(TTY * old_settings, TTY * new_settings)
658 if (memcmp(new_settings, old_settings, sizeof(TTY))) {
659 SET_TTY(my_fd, new_settings);
665 * Set window size if not set already, but update our copy of the values if the
669 set_window_size(int fd, short *high, short *wide)
672 (void) ioctl(fd, IOCTL_GET_WINSIZE, &win);
673 if (WINSIZE_ROWS(win) == 0 &&
674 WINSIZE_COLS(win) == 0) {
675 if (*high > 0 && *wide > 0) {
676 WINSIZE_ROWS(win) = (unsigned short) *high;
677 WINSIZE_COLS(win) = (unsigned short) *wide;
678 (void) ioctl(fd, IOCTL_SET_WINSIZE, &win);
680 } else if (WINSIZE_ROWS(win) > 0 &&
681 WINSIZE_COLS(win) > 0) {
682 *high = (short) WINSIZE_ROWS(win);
683 *wide = (short) WINSIZE_COLS(win);