stop using global variable needlessly

This commit is contained in:
Denis Vlasenko 2007-01-18 00:53:35 +00:00
parent cbbc043d1b
commit 41aaefce71

View File

@ -911,9 +911,11 @@ int stty_main(int argc, char **argv)
struct termios mode; struct termios mode;
void (*output_func)(const struct termios *, const int); void (*output_func)(const struct termios *, const int);
const char *file_name = NULL; const char *file_name = NULL;
int k;
int display_all = 0; int display_all = 0;
option_mask32 = STTY_noargs; int stty_state;
int k;
stty_state = STTY_noargs;
output_func = do_display; output_func = do_display;
/* First pass: only parse/verify command line params */ /* First pass: only parse/verify command line params */
@ -931,7 +933,7 @@ int stty_main(int argc, char **argv)
if (mp) { if (mp) {
if (!(mp->flags & REV)) if (!(mp->flags & REV))
goto invalid_argument; goto invalid_argument;
option_mask32 &= ~STTY_noargs; stty_state &= ~STTY_noargs;
continue; continue;
} }
/* It is an option - parse it */ /* It is an option - parse it */
@ -939,12 +941,12 @@ int stty_main(int argc, char **argv)
while (arg[++i]) { while (arg[++i]) {
switch (arg[i]) { switch (arg[i]) {
case 'a': case 'a':
option_mask32 |= STTY_verbose_output; stty_state |= STTY_verbose_output;
output_func = do_display; output_func = do_display;
display_all = 1; display_all = 1;
break; break;
case 'g': case 'g':
option_mask32 |= STTY_recoverable_output; stty_state |= STTY_recoverable_output;
output_func = display_recoverable; output_func = display_recoverable;
break; break;
case 'F': case 'F':
@ -971,7 +973,7 @@ end_option:
mp = find_mode(arg); mp = find_mode(arg);
if (mp) { if (mp) {
option_mask32 &= ~STTY_noargs; stty_state &= ~STTY_noargs;
continue; continue;
} }
@ -981,7 +983,7 @@ end_option:
bb_error_msg_and_die(bb_msg_requires_arg, arg); bb_error_msg_and_die(bb_msg_requires_arg, arg);
/* called for the side effect of xfunc death only */ /* called for the side effect of xfunc death only */
set_control_char_or_die(cp, argnext, &mode); set_control_char_or_die(cp, argnext, &mode);
option_mask32 &= ~STTY_noargs; stty_state &= ~STTY_noargs;
++k; ++k;
continue; continue;
} }
@ -1024,16 +1026,16 @@ end_option:
invalid_argument: invalid_argument:
bb_error_msg_and_die("invalid argument '%s'", arg); bb_error_msg_and_die("invalid argument '%s'", arg);
} }
option_mask32 &= ~STTY_noargs; stty_state &= ~STTY_noargs;
} }
/* Specifying both -a and -g is an error */ /* Specifying both -a and -g is an error */
if ((option_mask32 & (STTY_verbose_output | STTY_recoverable_output)) == if ((stty_state & (STTY_verbose_output | STTY_recoverable_output)) ==
(STTY_verbose_output | STTY_recoverable_output)) (STTY_verbose_output | STTY_recoverable_output))
bb_error_msg_and_die("verbose and stty-readable output styles are mutually exclusive"); bb_error_msg_and_die("verbose and stty-readable output styles are mutually exclusive");
/* Specifying -a or -g with non-options is an error */ /* Specifying -a or -g with non-options is an error */
if (!(option_mask32 & STTY_noargs) && if (!(stty_state & STTY_noargs) &&
(option_mask32 & (STTY_verbose_output | STTY_recoverable_output))) (stty_state & (STTY_verbose_output | STTY_recoverable_output)))
bb_error_msg_and_die("modes may not be set when specifying an output style"); bb_error_msg_and_die("modes may not be set when specifying an output style");
/* Now it is safe to start doing things */ /* Now it is safe to start doing things */
@ -1057,7 +1059,7 @@ invalid_argument:
if (tcgetattr(STDIN_FILENO, &mode)) if (tcgetattr(STDIN_FILENO, &mode))
perror_on_device_and_die("%s"); perror_on_device_and_die("%s");
if (option_mask32 & (STTY_verbose_output | STTY_recoverable_output | STTY_noargs)) { if (stty_state & (STTY_verbose_output | STTY_recoverable_output | STTY_noargs)) {
get_terminal_width_height(STDOUT_FILENO, &max_col, NULL); get_terminal_width_height(STDOUT_FILENO, &max_col, NULL);
output_func(&mode, display_all); output_func(&mode, display_all);
return EXIT_SUCCESS; return EXIT_SUCCESS;
@ -1076,7 +1078,7 @@ invalid_argument:
mp = find_mode(arg+1); mp = find_mode(arg+1);
if (mp) { if (mp) {
set_mode(mp, 1 /* reversed */, &mode); set_mode(mp, 1 /* reversed */, &mode);
option_mask32 |= STTY_require_set_attr; stty_state |= STTY_require_set_attr;
} }
/* It is an option - already parsed. Skip it */ /* It is an option - already parsed. Skip it */
continue; continue;
@ -1085,7 +1087,7 @@ invalid_argument:
mp = find_mode(arg); mp = find_mode(arg);
if (mp) { if (mp) {
set_mode(mp, 0 /* non-reversed */, &mode); set_mode(mp, 0 /* non-reversed */, &mode);
option_mask32 |= STTY_require_set_attr; stty_state |= STTY_require_set_attr;
continue; continue;
} }
@ -1093,7 +1095,7 @@ invalid_argument:
if (cp) { if (cp) {
++k; ++k;
set_control_char_or_die(cp, argnext, &mode); set_control_char_or_die(cp, argnext, &mode);
option_mask32 |= STTY_require_set_attr; stty_state |= STTY_require_set_attr;
continue; continue;
} }
@ -1106,7 +1108,7 @@ invalid_argument:
#ifdef HAVE_C_LINE #ifdef HAVE_C_LINE
case param_line: case param_line:
mode.c_line = xatoul_sfx(argnext, stty_suffixes); mode.c_line = xatoul_sfx(argnext, stty_suffixes);
option_mask32 |= STTY_require_set_attr; stty_state |= STTY_require_set_attr;
break; break;
#endif #endif
#ifdef TIOCGWINSZ #ifdef TIOCGWINSZ
@ -1125,24 +1127,24 @@ invalid_argument:
break; break;
case param_ispeed: case param_ispeed:
set_speed_or_die(input_speed, argnext, &mode); set_speed_or_die(input_speed, argnext, &mode);
option_mask32 |= (STTY_require_set_attr | STTY_speed_was_set); stty_state |= (STTY_require_set_attr | STTY_speed_was_set);
break; break;
case param_ospeed: case param_ospeed:
set_speed_or_die(output_speed, argnext, &mode); set_speed_or_die(output_speed, argnext, &mode);
option_mask32 |= (STTY_require_set_attr | STTY_speed_was_set); stty_state |= (STTY_require_set_attr | STTY_speed_was_set);
break; break;
default: default:
if (recover_mode(arg, &mode) == 1) if (recover_mode(arg, &mode) == 1)
option_mask32 |= STTY_require_set_attr; stty_state |= STTY_require_set_attr;
else /* true: if (tty_value_to_baud(xatou(arg)) != (speed_t) -1) */{ else /* true: if (tty_value_to_baud(xatou(arg)) != (speed_t) -1) */{
set_speed_or_die(both_speeds, arg, &mode); set_speed_or_die(both_speeds, arg, &mode);
option_mask32 |= (STTY_require_set_attr | STTY_speed_was_set); stty_state |= (STTY_require_set_attr | STTY_speed_was_set);
} /* else - impossible (caught in the first pass): } /* else - impossible (caught in the first pass):
bb_error_msg_and_die("invalid argument '%s'", arg); */ bb_error_msg_and_die("invalid argument '%s'", arg); */
} }
} }
if (option_mask32 & STTY_require_set_attr) { if (stty_state & STTY_require_set_attr) {
struct termios new_mode; struct termios new_mode;
if (tcsetattr(STDIN_FILENO, TCSADRAIN, &mode)) if (tcsetattr(STDIN_FILENO, TCSADRAIN, &mode))
@ -1173,8 +1175,8 @@ invalid_argument:
error for a true failure to set the baud rate */ error for a true failure to set the baud rate */
new_mode.c_cflag &= (~CIBAUD); new_mode.c_cflag &= (~CIBAUD);
if (option_mask32 & STTY_speed_was_set || if ((stty_state & STTY_speed_was_set)
memcmp(&mode, &new_mode, sizeof(mode)) != 0) || memcmp(&mode, &new_mode, sizeof(mode)) != 0)
#endif #endif
perror_on_device_and_die("%s: cannot perform all requested operations"); perror_on_device_and_die("%s: cannot perform all requested operations");
} }