static const int NUM_control_info =
(sizeof(control_info) / sizeof(struct control_info));
+#define EMT(t) ((enum mode_type)(t))
static const char * visible(unsigned int ch);
static int recover_mode(char *arg, struct termios *mode);
int reversed, struct termios *mode);
static speed_t string_to_baud(const char *arg);
static tcflag_t* mode_type_flag(enum mode_type type, struct termios *mode);
-static void display_all(struct termios *mode, int fd);
-static void display_changed(struct termios *mode, int fd);
-static void display_recoverable(struct termios *mode, int fd);
+static void display_all(struct termios *mode);
+static void display_changed(struct termios *mode);
+static void display_recoverable(struct termios *mode);
static void display_speed(struct termios *mode, int fancy);
-static void display_window_size(int fancy, int fd);
+static void display_window_size(int fancy);
static void sane_mode(struct termios *mode);
static void set_control_char(const struct control_info *info,
const char *arg, struct termios *mode);
static void set_speed(enum speed_setting type,
const char *arg, struct termios *mode);
-static void set_window_size(int rows, int cols, int fd);
+static void set_window_size(int rows, int cols);
static const char *device_name;
#endif
{
struct termios mode;
- void (*output_func)(struct termios *, int);
+ void (*output_func)(struct termios *);
int optc;
int require_set_attr;
int speed_was_set;
int k;
int noargs = 1;
char * file_name = NULL;
- int fd;
-
output_func = display_changed;
verbose_output = 0;
int fdflags;
device_name = file_name;
- fd = bb_xopen(device_name, O_RDONLY | O_NONBLOCK);
- if ((fdflags = fcntl(fd, F_GETFL)) == -1
- || fcntl(fd, F_SETFL, fdflags & ~O_NONBLOCK) < 0)
+ fclose(stdin);
+ bb_xopen(device_name, O_RDONLY | O_NONBLOCK);
+ if ((fdflags = fcntl(STDIN_FILENO, F_GETFL)) == -1
+ || fcntl(STDIN_FILENO, F_SETFL, fdflags & ~O_NONBLOCK) < 0)
perror_on_device("%s: couldn't reset non-blocking mode");
} else {
- fd = 0;
device_name = bb_msg_standard_input;
}
/* Initialize to all zeroes so there is no risk memcmp will report a
spurious difference in an uninitialized portion of the structure. */
memset(&mode, 0, sizeof(mode));
- if (tcgetattr(fd, &mode))
+ if (tcgetattr(STDIN_FILENO, &mode))
perror_on_device("%s");
if (verbose_output | recoverable_output | noargs) {
max_col = screen_columns();
current_col = 0;
- output_func(&mode, fd);
+ output_func(&mode);
return EXIT_SUCCESS;
}
bb_error_msg_and_die("missing argument to `%s'", argv[k]);
++k;
set_window_size((int) bb_xparse_number(argv[k], stty_suffixes),
- -1, fd);
+ -1);
} else if (STREQ(argv[k], "cols") || STREQ(argv[k], "columns")) {
if (k == argc - 1)
bb_error_msg_and_die("missing argument to `%s'", argv[k]);
++k;
set_window_size(-1,
- (int) bb_xparse_number(argv[k], stty_suffixes),
- fd);
+ (int) bb_xparse_number(argv[k], stty_suffixes));
} else if (STREQ(argv[k], "size")) {
max_col = screen_columns();
current_col = 0;
- display_window_size(0, fd);
+ display_window_size(0);
}
#endif
#ifdef HAVE_C_LINE
if (require_set_attr) {
struct termios new_mode;
- if (tcsetattr(fd, TCSADRAIN, &mode))
+ if (tcsetattr(STDIN_FILENO, TCSADRAIN, &mode))
perror_on_device("%s");
/* POSIX (according to Zlotnick's book) tcsetattr returns zero if
/* Initialize to all zeroes so there is no risk memcmp will report a
spurious difference in an uninitialized portion of the structure. */
memset(&new_mode, 0, sizeof(new_mode));
- if (tcgetattr(fd, &new_mode))
+ if (tcgetattr(STDIN_FILENO, &new_mode))
perror_on_device("%s");
/* Normally, one shouldn't use memcmp to compare structures that
if (reversed && (info->flags & REV) == 0)
return 0;
- bitsp = mode_type_flag(info->type, mode);
+ bitsp = mode_type_flag(EMT(info->type), mode);
if (bitsp == NULL) {
/* Combination mode. */
baud = string_to_baud(arg);
- if (type != output_speed) { /* either input or both */
+ if (type != output_speed) { /* either input or both */
cfsetispeed(mode, baud);
}
- if (type != input_speed) { /* either output or both */
+ if (type != input_speed) { /* either output or both */
cfsetospeed(mode, baud);
}
}
}
static void
-set_window_size(int rows, int cols, int fd)
+set_window_size(int rows, int cols)
{
struct winsize win;
- if (get_win_size(fd, &win)) {
+ if (get_win_size(STDIN_FILENO, &win)) {
if (errno != EINVAL)
perror_on_device("%s");
memset(&win, 0, sizeof(win));
win.ws_row = win.ws_col = 1;
- if ((ioctl(fd, TIOCSWINSZ, (char *) &win) != 0)
- || (ioctl(fd, TIOCSSIZE, (char *) &ttysz) != 0)) {
+ if ((ioctl(STDIN_FILENO, TIOCSWINSZ, (char *) &win) != 0)
+ || (ioctl(STDIN_FILENO, TIOCSSIZE, (char *) &ttysz) != 0)) {
perror_on_device("%s");
}
return;
}
# endif
- if (ioctl(fd, TIOCSWINSZ, (char *) &win))
+ if (ioctl(STDIN_FILENO, TIOCSWINSZ, (char *) &win))
perror_on_device("%s");
}
-static void display_window_size(int fancy, int fd)
+static void display_window_size(int fancy)
{
const char *fmt_str = "%s" "\0" "%s: no size information for this device";
struct winsize win;
- if (get_win_size(fd, &win)) {
+ if (get_win_size(STDIN_FILENO, &win)) {
if ((errno != EINVAL) || ((fmt_str += 2), !fancy)) {
perror_on_device(fmt_str);
}
return NULL;
}
-static void display_changed(struct termios *mode, int fd)
+static void display_changed(struct termios *mode)
{
int i;
int empty_line;
for (i = 0; i < NUM_mode_info; ++i) {
if (mode_info[i].flags & OMIT)
continue;
- if (mode_info[i].type != prev_type) {
+ if (EMT(mode_info[i].type) != prev_type) {
if (empty_line == 0) {
putchar('\n');
current_col = 0;
empty_line = 1;
}
- prev_type = mode_info[i].type;
+ prev_type = EMT(mode_info[i].type);
}
- bitsp = mode_type_flag(mode_info[i].type, mode);
+ bitsp = mode_type_flag(EMT(mode_info[i].type), mode);
mask = mode_info[i].mask ? mode_info[i].mask : mode_info[i].bits;
if ((*bitsp & mask) == mode_info[i].bits) {
if (mode_info[i].flags & SANE_UNSET) {
}
static void
-display_all(struct termios *mode, int fd)
+display_all(struct termios *mode)
{
int i;
tcflag_t *bitsp;
display_speed(mode, 1);
#ifdef TIOCGWINSZ
- display_window_size(1, fd);
+ display_window_size(1);
#endif
#ifdef HAVE_C_LINE
wrapf("line = %d;", mode->c_line);
for (i = 0; i < NUM_mode_info; ++i) {
if (mode_info[i].flags & OMIT)
continue;
- if (mode_info[i].type != prev_type) {
+ if (EMT(mode_info[i].type) != prev_type) {
putchar('\n');
current_col = 0;
- prev_type = mode_info[i].type;
+ prev_type = EMT(mode_info[i].type);
}
- bitsp = mode_type_flag(mode_info[i].type, mode);
+ bitsp = mode_type_flag(EMT(mode_info[i].type), mode);
mask = mode_info[i].mask ? mode_info[i].mask : mode_info[i].bits;
if ((*bitsp & mask) == mode_info[i].bits)
wrapf("%s", mode_info[i].name);
ospeed = ispeed = cfgetispeed(mode);
if (ispeed == 0 || ispeed == (ospeed = cfgetospeed(mode))) {
- ispeed = ospeed; /* in case ispeed was 0 */
+ ispeed = ospeed; /* in case ispeed was 0 */
fmt_str += 43;
}
if (fancy) {
current_col = 0;
}
-static void display_recoverable(struct termios *mode, int fd)
+static void display_recoverable(struct termios *mode)
{
int i;
for (i = 0; i < NUM_mode_info; ++i) {
if (mode_info[i].flags & SANE_SET) {
- bitsp = mode_type_flag(mode_info[i].type, mode);
+ bitsp = mode_type_flag(EMT(mode_info[i].type), mode);
*bitsp = (*bitsp & ~((unsigned long)mode_info[i].mask))
| mode_info[i].bits;
} else if (mode_info[i].flags & SANE_UNSET) {
- bitsp = mode_type_flag(mode_info[i].type, mode);
+ bitsp = mode_type_flag(EMT(mode_info[i].type), mode);
*bitsp = *bitsp & ~((unsigned long)mode_info[i].mask)
& ~mode_info[i].bits;
}