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>
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.11 2016/12/24 23:20:57 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 */
85 restore_tty_settings();
86 (void) fprintf(my_file, "\n");
88 ExitProgram(EXIT_FAILURE);
93 failed(const char *msg)
97 _nc_STRCPY(temp, _nc_progname, sizeof(temp));
98 _nc_STRCAT(temp, ": ", sizeof(temp));
99 _nc_STRNCAT(temp, msg, sizeof(temp), sizeof(temp) - strlen(temp) - 2);
114 if ((fp = fopen(file, "r")) == 0)
117 while ((nr = fread(buf, sizeof(char), sizeof(buf), fp)) != 0) {
118 if (fwrite(buf, sizeof(char), nr, my_file) != nr) {
131 return putc(c, my_file);
134 /**************************************************************************
136 **************************************************************************/
138 /* some BSD systems have these built in, some systems are missing
139 * one or more definitions. The safest solution is to override unless the
140 * commonly-altered ones are defined.
142 #if !(defined(CERASE) && defined(CINTR) && defined(CKILL) && defined(CQUIT))
155 /* control-character defaults */
157 #define CEOF CTRL('D')
160 #define CERASE CTRL('H')
163 #define CINTR 127 /* ^? */
166 #define CKILL CTRL('U')
169 #define CLNEXT CTRL('v')
172 #define CRPRNT CTRL('r')
175 #define CQUIT CTRL('\\')
178 #define CSTART CTRL('Q')
181 #define CSTOP CTRL('S')
184 #define CSUSP CTRL('Z')
187 #if defined(_POSIX_VDISABLE)
188 #define DISABLED(val) (((_POSIX_VDISABLE != -1) \
189 && ((val) == _POSIX_VDISABLE)) \
192 #define DISABLED(val) ((int)(val) <= 0)
195 #define CHK(val, dft) (unsigned char) (DISABLED(val) ? dft : val)
197 #define reset_char(item, value) \
198 tty_settings->c_cc[item] = CHK(tty_settings->c_cc[item], value)
201 * Reset the terminal mode bits to a sensible state. Very useful after
202 * a child program dies in raw mode.
205 reset_tty_settings(int fd, TTY * tty_settings)
207 GET_TTY(fd, tty_settings);
210 #if defined(VDISCARD) && defined(CDISCARD)
211 reset_char(VDISCARD, CDISCARD);
213 reset_char(VEOF, CEOF);
214 reset_char(VERASE, CERASE);
215 #if defined(VFLUSH) && defined(CFLUSH)
216 reset_char(VFLUSH, CFLUSH);
218 reset_char(VINTR, CINTR);
219 reset_char(VKILL, CKILL);
220 #if defined(VLNEXT) && defined(CLNEXT)
221 reset_char(VLNEXT, CLNEXT);
223 reset_char(VQUIT, CQUIT);
224 #if defined(VREPRINT) && defined(CRPRNT)
225 reset_char(VREPRINT, CRPRNT);
227 #if defined(VSTART) && defined(CSTART)
228 reset_char(VSTART, CSTART);
230 #if defined(VSTOP) && defined(CSTOP)
231 reset_char(VSTOP, CSTOP);
233 #if defined(VSUSP) && defined(CSUSP)
234 reset_char(VSUSP, CSUSP);
236 #if defined(VWERASE) && defined(CWERASE)
237 reset_char(VWERASE, CWERASE);
240 tty_settings->c_iflag &= ~((unsigned) (IGNBRK
254 tty_settings->c_iflag |= (BRKINT
263 tty_settings->c_oflag &= ~((unsigned) (0
302 tty_settings->c_oflag |= (OPOST
308 tty_settings->c_cflag &= ~((unsigned) (CSIZE
313 tty_settings->c_cflag |= (CS8 | CREAD);
314 tty_settings->c_lflag &= ~((unsigned) (ECHONL
327 tty_settings->c_lflag |= (ISIG
341 SET_TTY(fd, tty_settings);
345 * Returns a "good" value for the erase character. This is loosely based on
354 && key_backspace != 0
355 && strlen(key_backspace) == 1) {
356 result = key_backspace[0];
365 * Update the values of the erase, interrupt, and kill characters in the TTY
368 * SVr4 tset (e.g., Solaris 2.5) only modifies the intr, quit or erase
369 * characters if they're unset, or if we specify them as options. This differs
370 * from BSD 4.4 tset, which always sets erase.
373 set_control_chars(TTY * tty_settings, int my_erase, int my_intr, int my_kill)
375 if (DISABLED(tty_settings->c_cc[VERASE]) || my_erase >= 0) {
376 tty_settings->c_cc[VERASE] = UChar((my_erase >= 0)
381 if (DISABLED(tty_settings->c_cc[VINTR]) || my_intr >= 0) {
382 tty_settings->c_cc[VINTR] = UChar((my_intr >= 0)
387 if (DISABLED(tty_settings->c_cc[VKILL]) || my_kill >= 0) {
388 tty_settings->c_cc[VKILL] = UChar((my_kill >= 0)
395 * Set up various conversions in the TTY parameter, including parity, tabs,
396 * returns, echo, and case, according to the termcap entry.
399 set_conversions(TTY * tty_settings)
402 tty_settings->c_oflag |= ONLCR;
404 tty_settings->c_iflag |= ICRNL;
405 tty_settings->c_lflag |= ECHO;
407 tty_settings->c_oflag |= OXTABS;
410 /* test used to be tgetflag("NL") */
411 if (newline != (char *) 0 && newline[0] == '\n' && !newline[1]) {
412 /* Newline, not linefeed. */
414 tty_settings->c_oflag &= ~((unsigned) ONLCR);
416 tty_settings->c_iflag &= ~((unsigned) ICRNL);
419 /* test used to be tgetflag("pt") */
420 if (has_hardware_tabs) /* Print tabs. */
421 tty_settings->c_oflag &= ~OXTABS;
423 tty_settings->c_lflag |= (ECHOE | ECHOK);
427 * Set the hardware tabs on the terminal, using the 'ct' (clear all tabs),
428 * 'st' (set one tab) and 'ch' (horizontal cursor addressing) capabilities.
429 * This is done before 'if' and 'is', so they can recover in case of error.
431 * Return TRUE if we set any tab stops, FALSE if not.
434 reset_tabstops(int wide)
436 if ((init_tabs != 8) && (set_tab && clear_all_tabs)) {
439 (void) putc('\r', my_file); /* Force to left margin. */
440 tputs(clear_all_tabs, 0, out_char);
442 for (c = 8; c < wide; c += 8) {
443 /* Get to the right column. In BSD tset, this used to try a bunch
444 * of half-clever things with cup and hpa, for an average saving of
445 * somewhat less than two character times per tab stop, less than
446 * .01 sec at 2400cps. We lost all this cruft because it seemed to
447 * be introducing some odd bugs.
448 * -----------12345678----------- */
449 (void) fputs(" ", my_file);
450 tputs(set_tab, 0, out_char);
459 sent_string(const char *s)
463 tputs(s, 0, out_char);
469 #define PUTCHAR(c) fputc(c, my_file)
471 /* Output startup string. */
473 send_init_strings(int fd GCC_UNUSED, TTY * old_settings)
476 bool need_flush = FALSE;
480 if (old_settings != 0 &&
481 old_settings->c_oflag & (TAB3 | ONLCR | OCRNL | ONLRET)) {
482 old_settings->c_oflag &= (TAB3 | ONLCR | OCRNL | ONLRET);
483 SET_TTY(fd, old_settings);
486 if (use_reset || use_init) {
487 if (init_prog != 0) {
488 IGNORE_RC(system(init_prog));
491 need_flush |= sent_string((use_reset && (reset_1string != 0))
495 need_flush |= sent_string((use_reset && (reset_2string != 0))
499 if (set_lr_margin != 0) {
500 need_flush |= sent_string(TPARM_2(set_lr_margin, 0,
502 } else if (set_left_margin_parm != 0
503 && set_right_margin_parm != 0) {
504 need_flush |= sent_string(TPARM_1(set_left_margin_parm, 0));
505 need_flush |= sent_string(TPARM_1(set_right_margin_parm,
507 } else if (clear_margins != 0
508 && set_left_margin != 0
509 && set_right_margin != 0) {
510 need_flush |= sent_string(clear_margins);
511 if (carriage_return != 0) {
512 need_flush |= sent_string(carriage_return);
516 need_flush |= sent_string(set_left_margin);
517 if (parm_right_cursor) {
518 need_flush |= sent_string(TPARM_1(parm_right_cursor,
521 for (i = 0; i < columns - 1; i++) {
525 need_flush |= sent_string(set_right_margin);
526 if (carriage_return != 0) {
527 need_flush |= sent_string(carriage_return);
533 need_flush |= reset_tabstops(columns);
535 need_flush |= cat_file((use_reset && reset_file) ? reset_file : init_file);
537 need_flush |= sent_string((use_reset && (reset_3string != 0))
546 * Tell the user if a control key has been changed from the default value.
549 show_tty_change(TTY * old_settings,
555 unsigned older, newer;
558 newer = new_settings->c_cc[which];
559 older = old_settings->c_cc[which];
561 if (older == newer && older == def)
564 (void) fprintf(stderr, "%s %s ", name, older == newer ? "is" : "set to");
566 if (DISABLED(newer)) {
567 (void) fprintf(stderr, "undef.\n");
569 * Check 'delete' before 'backspace', since the key_backspace value
572 } else if (newer == 0177) {
573 (void) fprintf(stderr, "delete.\n");
574 } else if ((p = key_backspace) != 0
575 && newer == (unsigned char) p[0]
577 (void) fprintf(stderr, "backspace.\n");
578 } else if (newer < 040) {
580 (void) fprintf(stderr, "control-%c (^%c).\n", UChar(newer), UChar(newer));
582 (void) fprintf(stderr, "%c.\n", UChar(newer));
585 /**************************************************************************
587 **************************************************************************/
590 reset_start(FILE *fp, bool is_reset, bool is_init)
593 use_reset = is_reset;
605 print_tty_chars(TTY * old_settings, TTY * new_settings)
607 show_tty_change(old_settings, new_settings, "Erase", VERASE, CERASE);
608 show_tty_change(old_settings, new_settings, "Kill", VKILL, CKILL);
609 show_tty_change(old_settings, new_settings, "Interrupt", VINTR, CINTR);
614 * Set window size if not set already, but update our copy of the values if the
618 set_window_size(int fd, short *high, short *wide)
621 (void) ioctl(fd, IOCTL_GET_WINSIZE, &win);
622 if (WINSIZE_ROWS(win) == 0 &&
623 WINSIZE_COLS(win) == 0) {
624 if (*high > 0 && *wide > 0) {
625 WINSIZE_ROWS(win) = (unsigned short) *high;
626 WINSIZE_COLS(win) = (unsigned short) *wide;
627 (void) ioctl(fd, IOCTL_SET_WINSIZE, &win);
629 } else if (WINSIZE_ROWS(win) > 0 &&
630 WINSIZE_COLS(win) > 0) {
631 *high = (short) WINSIZE_ROWS(win);
632 *wide = (short) WINSIZE_COLS(win);