X-Git-Url: https://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=progs%2Freset_cmd.c;h=a4e44efc0f572b9d8abf572bdbe27684ce78ccfa;hp=94ad75089e436feb4b149d14f0ba6657cb2d0c0b;hb=b60a2772d9f149d8e900c1d5a09a53a56a0837a8;hpb=96592d717179f99c8fe1c5a63fc4a26c24867288;ds=sidebyside diff --git a/progs/reset_cmd.c b/progs/reset_cmd.c index 94ad7508..a4e44efc 100644 --- a/progs/reset_cmd.c +++ b/progs/reset_cmd.c @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright (c) 2016 Free Software Foundation, Inc. * + * Copyright (c) 2016,2017 Free Software Foundation, Inc. * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the * @@ -31,6 +31,7 @@ ****************************************************************************/ #include +#include #include #include @@ -51,7 +52,7 @@ #include #endif -MODULE_ID("$Id: reset_cmd.c,v 1.5 2016/08/06 21:04:54 tom Exp $") +MODULE_ID("$Id: reset_cmd.c,v 1.13 2017/10/07 20:56:03 tom Exp $") /* * SCO defines TIOCGSIZE and the corresponding struct. Other systems (SunOS, @@ -73,47 +74,24 @@ MODULE_ID("$Id: reset_cmd.c,v 1.5 2016/08/06 21:04:54 tom Exp $") # endif #endif -static int my_fd; static FILE *my_file; -static TTY original_settings; -static bool can_restore = FALSE; static bool use_reset = FALSE; /* invoked as reset */ static bool use_init = FALSE; /* invoked as init */ static void -exit_error(void) +failed(const char *msg) { + int code = errno; + + (void) fprintf(stderr, "%s: %s: %s\n", _nc_progname, msg, strerror(code)); restore_tty_settings(); (void) fprintf(my_file, "\n"); fflush(my_file); - ExitProgram(EXIT_FAILURE); - /* NOTREACHED */ -} - -static void -failed(const char *msg) -{ - char temp[BUFSIZ]; - - _nc_STRCPY(temp, _nc_progname, sizeof(temp)); - _nc_STRCAT(temp, ": ", sizeof(temp)); - perror(strncat(temp, msg, sizeof(temp) - strlen(temp) - 2)); - exit_error(); + ExitProgram(ErrSystem(code)); /* NOTREACHED */ } -static bool -get_tty_settings(int fd, TTY * tty_settings) -{ - bool success = TRUE; - my_fd = fd; - if (fd < 0 || GET_TTY(my_fd, tty_settings) < 0) { - success = FALSE; - } - return success; -} - static bool cat_file(char *file) { @@ -214,13 +192,9 @@ out_char(int c) * a child program dies in raw mode. */ void -reset_tty_settings(TTY * tty_settings) +reset_tty_settings(int fd, TTY * tty_settings) { -#ifdef TERMIOS - tcgetattr(my_fd, tty_settings); -#else - stty(my_fd, tty_settings); -#endif + GET_TTY(fd, tty_settings); #ifdef TERMIOS #if defined(VDISCARD) && defined(CDISCARD) @@ -354,7 +328,7 @@ reset_tty_settings(TTY * tty_settings) ); #endif - SET_TTY(my_fd, tty_settings); + SET_TTY(fd, tty_settings); } /* @@ -486,16 +460,17 @@ sent_string(const char *s) /* Output startup string. */ bool -send_init_strings(TTY * old_settings) +send_init_strings(int fd GCC_UNUSED, TTY * old_settings) { int i; bool need_flush = FALSE; + (void) old_settings; #ifdef TAB3 if (old_settings != 0 && old_settings->c_oflag & (TAB3 | ONLCR | OCRNL | ONLRET)) { old_settings->c_oflag &= (TAB3 | ONLCR | OCRNL | ONLRET); - SET_TTY(my_fd, old_settings); + SET_TTY(fd, old_settings); } #endif if (use_reset || use_init) { @@ -511,17 +486,23 @@ send_init_strings(TTY * old_settings) ? reset_2string : init_2string); +#if defined(set_lr_margin) if (set_lr_margin != 0) { need_flush |= sent_string(TPARM_2(set_lr_margin, 0, columns - 1)); - } else if (set_left_margin_parm != 0 - && set_right_margin_parm != 0) { + } else +#endif +#if defined(set_left_margin_parm) && defined(set_right_margin_parm) + if (set_left_margin_parm != 0 + && set_right_margin_parm != 0) { need_flush |= sent_string(TPARM_1(set_left_margin_parm, 0)); need_flush |= sent_string(TPARM_1(set_right_margin_parm, columns - 1)); - } else if (clear_margins != 0 - && set_left_margin != 0 - && set_right_margin != 0) { + } else +#endif + if (clear_margins != 0 + && set_left_margin != 0 + && set_right_margin != 0) { need_flush |= sent_string(clear_margins); if (carriage_return != 0) { need_flush |= sent_string(carriage_return); @@ -624,53 +605,27 @@ print_tty_chars(TTY * old_settings, TTY * new_settings) show_tty_change(old_settings, new_settings, "Interrupt", VINTR, CINTR); } +#if HAVE_SIZECHANGE /* - * Open a file descriptor on the current terminal, to obtain its settings. - * stderr is less likely to be redirected than stdout; try that first. + * Set window size if not set already, but update our copy of the values if the + * size was set. */ -int -save_tty_settings(TTY * tty_settings) -{ - if (!get_tty_settings(STDERR_FILENO, tty_settings) && - !get_tty_settings(STDOUT_FILENO, tty_settings) && - !get_tty_settings(STDIN_FILENO, tty_settings) && - !get_tty_settings(open("/dev/tty", O_RDWR), tty_settings)) { - failed("terminal attributes"); - } - can_restore = TRUE; - original_settings = *tty_settings; - return my_fd; -} - void -restore_tty_settings(void) -{ - if (can_restore) - SET_TTY(my_fd, &original_settings); -} - -/* Set the modes if they've changed. */ -void -update_tty_settings(TTY * old_settings, TTY * new_settings) -{ - if (memcmp(new_settings, old_settings, sizeof(TTY))) { - SET_TTY(my_fd, new_settings); - } -} - -#if HAVE_SIZECHANGE -/* Set window size if not set already */ -void -set_window_size(int fd, int high, int wide) +set_window_size(int fd, short *high, short *wide) { STRUCT_WINSIZE win; (void) ioctl(fd, IOCTL_GET_WINSIZE, &win); if (WINSIZE_ROWS(win) == 0 && - WINSIZE_COLS(win) == 0 && - high > 0 && wide > 0) { - WINSIZE_ROWS(win) = (unsigned short) high; - WINSIZE_COLS(win) = (unsigned short) wide; - (void) ioctl(fd, IOCTL_SET_WINSIZE, &win); + WINSIZE_COLS(win) == 0) { + if (*high > 0 && *wide > 0) { + WINSIZE_ROWS(win) = (unsigned short) *high; + WINSIZE_COLS(win) = (unsigned short) *wide; + (void) ioctl(fd, IOCTL_SET_WINSIZE, &win); + } + } else if (WINSIZE_ROWS(win) > 0 && + WINSIZE_COLS(win) > 0) { + *high = (short) WINSIZE_ROWS(win); + *wide = (short) WINSIZE_COLS(win); } } #endif