aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-01-18 00:53:35 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-01-18 00:53:35 +0000
commit41aaefce71bfdfb7d788c29e9df90971eecc8193 (patch)
tree7ac93671972cfb0029cc50e00de6413433b1767c
parentcbbc043d1bce3bf48097520dbb91d84a423386a6 (diff)
downloadbusybox-w32-41aaefce71bfdfb7d788c29e9df90971eecc8193.tar.gz
busybox-w32-41aaefce71bfdfb7d788c29e9df90971eecc8193.tar.bz2
busybox-w32-41aaefce71bfdfb7d788c29e9df90971eecc8193.zip
stop using global variable needlessly
-rw-r--r--coreutils/stty.c58
1 files changed, 30 insertions, 28 deletions
diff --git a/coreutils/stty.c b/coreutils/stty.c
index 7206f2d11..3ce8bf312 100644
--- a/coreutils/stty.c
+++ b/coreutils/stty.c
@@ -901,19 +901,21 @@ static void set_control_char_or_die(const struct control_info *info,
901 mode->c_cc[info->offset] = value; 901 mode->c_cc[info->offset] = value;
902} 902}
903 903
904#define STTY_require_set_attr (1<<0) 904#define STTY_require_set_attr (1<<0)
905#define STTY_speed_was_set (1<<1) 905#define STTY_speed_was_set (1<<1)
906#define STTY_verbose_output (1<<2) 906#define STTY_verbose_output (1<<2)
907#define STTY_recoverable_output (1<<3) 907#define STTY_recoverable_output (1<<3)
908#define STTY_noargs (1<<4) 908#define STTY_noargs (1<<4)
909int stty_main(int argc, char **argv) 909int stty_main(int argc, char **argv)
910{ 910{
911 struct termios mode; 911 struct termios mode;
912 void (*output_func)(const struct termios *, const int); 912 void (*output_func)(const struct termios *, const int);
913 const char *file_name = NULL; 913 const char *file_name = NULL;
914 int k;
915 int display_all = 0; 914 int display_all = 0;
916 option_mask32 = STTY_noargs; 915 int stty_state;
916 int k;
917
918 stty_state = STTY_noargs;
917 output_func = do_display; 919 output_func = do_display;
918 920
919 /* First pass: only parse/verify command line params */ 921 /* First pass: only parse/verify command line params */
@@ -931,7 +933,7 @@ int stty_main(int argc, char **argv)
931 if (mp) { 933 if (mp) {
932 if (!(mp->flags & REV)) 934 if (!(mp->flags & REV))
933 goto invalid_argument; 935 goto invalid_argument;
934 option_mask32 &= ~STTY_noargs; 936 stty_state &= ~STTY_noargs;
935 continue; 937 continue;
936 } 938 }
937 /* It is an option - parse it */ 939 /* It is an option - parse it */
@@ -939,12 +941,12 @@ int stty_main(int argc, char **argv)
939 while (arg[++i]) { 941 while (arg[++i]) {
940 switch (arg[i]) { 942 switch (arg[i]) {
941 case 'a': 943 case 'a':
942 option_mask32 |= STTY_verbose_output; 944 stty_state |= STTY_verbose_output;
943 output_func = do_display; 945 output_func = do_display;
944 display_all = 1; 946 display_all = 1;
945 break; 947 break;
946 case 'g': 948 case 'g':
947 option_mask32 |= STTY_recoverable_output; 949 stty_state |= STTY_recoverable_output;
948 output_func = display_recoverable; 950 output_func = display_recoverable;
949 break; 951 break;
950 case 'F': 952 case 'F':
@@ -971,7 +973,7 @@ end_option:
971 973
972 mp = find_mode(arg); 974 mp = find_mode(arg);
973 if (mp) { 975 if (mp) {
974 option_mask32 &= ~STTY_noargs; 976 stty_state &= ~STTY_noargs;
975 continue; 977 continue;
976 } 978 }
977 979
@@ -981,7 +983,7 @@ end_option:
981 bb_error_msg_and_die(bb_msg_requires_arg, arg); 983 bb_error_msg_and_die(bb_msg_requires_arg, arg);
982 /* called for the side effect of xfunc death only */ 984 /* called for the side effect of xfunc death only */
983 set_control_char_or_die(cp, argnext, &mode); 985 set_control_char_or_die(cp, argnext, &mode);
984 option_mask32 &= ~STTY_noargs; 986 stty_state &= ~STTY_noargs;
985 ++k; 987 ++k;
986 continue; 988 continue;
987 } 989 }
@@ -1024,16 +1026,16 @@ end_option:
1024invalid_argument: 1026invalid_argument:
1025 bb_error_msg_and_die("invalid argument '%s'", arg); 1027 bb_error_msg_and_die("invalid argument '%s'", arg);
1026 } 1028 }
1027 option_mask32 &= ~STTY_noargs; 1029 stty_state &= ~STTY_noargs;
1028 } 1030 }
1029 1031
1030 /* Specifying both -a and -g is an error */ 1032 /* Specifying both -a and -g is an error */
1031 if ((option_mask32 & (STTY_verbose_output | STTY_recoverable_output)) == 1033 if ((stty_state & (STTY_verbose_output | STTY_recoverable_output)) ==
1032 (STTY_verbose_output | STTY_recoverable_output)) 1034 (STTY_verbose_output | STTY_recoverable_output))
1033 bb_error_msg_and_die("verbose and stty-readable output styles are mutually exclusive"); 1035 bb_error_msg_and_die("verbose and stty-readable output styles are mutually exclusive");
1034 /* Specifying -a or -g with non-options is an error */ 1036 /* Specifying -a or -g with non-options is an error */
1035 if (!(option_mask32 & STTY_noargs) && 1037 if (!(stty_state & STTY_noargs) &&
1036 (option_mask32 & (STTY_verbose_output | STTY_recoverable_output))) 1038 (stty_state & (STTY_verbose_output | STTY_recoverable_output)))
1037 bb_error_msg_and_die("modes may not be set when specifying an output style"); 1039 bb_error_msg_and_die("modes may not be set when specifying an output style");
1038 1040
1039 /* Now it is safe to start doing things */ 1041 /* Now it is safe to start doing things */
@@ -1057,7 +1059,7 @@ invalid_argument:
1057 if (tcgetattr(STDIN_FILENO, &mode)) 1059 if (tcgetattr(STDIN_FILENO, &mode))
1058 perror_on_device_and_die("%s"); 1060 perror_on_device_and_die("%s");
1059 1061
1060 if (option_mask32 & (STTY_verbose_output | STTY_recoverable_output | STTY_noargs)) { 1062 if (stty_state & (STTY_verbose_output | STTY_recoverable_output | STTY_noargs)) {
1061 get_terminal_width_height(STDOUT_FILENO, &max_col, NULL); 1063 get_terminal_width_height(STDOUT_FILENO, &max_col, NULL);
1062 output_func(&mode, display_all); 1064 output_func(&mode, display_all);
1063 return EXIT_SUCCESS; 1065 return EXIT_SUCCESS;
@@ -1076,7 +1078,7 @@ invalid_argument:
1076 mp = find_mode(arg+1); 1078 mp = find_mode(arg+1);
1077 if (mp) { 1079 if (mp) {
1078 set_mode(mp, 1 /* reversed */, &mode); 1080 set_mode(mp, 1 /* reversed */, &mode);
1079 option_mask32 |= STTY_require_set_attr; 1081 stty_state |= STTY_require_set_attr;
1080 } 1082 }
1081 /* It is an option - already parsed. Skip it */ 1083 /* It is an option - already parsed. Skip it */
1082 continue; 1084 continue;
@@ -1085,7 +1087,7 @@ invalid_argument:
1085 mp = find_mode(arg); 1087 mp = find_mode(arg);
1086 if (mp) { 1088 if (mp) {
1087 set_mode(mp, 0 /* non-reversed */, &mode); 1089 set_mode(mp, 0 /* non-reversed */, &mode);
1088 option_mask32 |= STTY_require_set_attr; 1090 stty_state |= STTY_require_set_attr;
1089 continue; 1091 continue;
1090 } 1092 }
1091 1093
@@ -1093,7 +1095,7 @@ invalid_argument:
1093 if (cp) { 1095 if (cp) {
1094 ++k; 1096 ++k;
1095 set_control_char_or_die(cp, argnext, &mode); 1097 set_control_char_or_die(cp, argnext, &mode);
1096 option_mask32 |= STTY_require_set_attr; 1098 stty_state |= STTY_require_set_attr;
1097 continue; 1099 continue;
1098 } 1100 }
1099 1101
@@ -1106,7 +1108,7 @@ invalid_argument:
1106#ifdef HAVE_C_LINE 1108#ifdef HAVE_C_LINE
1107 case param_line: 1109 case param_line:
1108 mode.c_line = xatoul_sfx(argnext, stty_suffixes); 1110 mode.c_line = xatoul_sfx(argnext, stty_suffixes);
1109 option_mask32 |= STTY_require_set_attr; 1111 stty_state |= STTY_require_set_attr;
1110 break; 1112 break;
1111#endif 1113#endif
1112#ifdef TIOCGWINSZ 1114#ifdef TIOCGWINSZ
@@ -1125,24 +1127,24 @@ invalid_argument:
1125 break; 1127 break;
1126 case param_ispeed: 1128 case param_ispeed:
1127 set_speed_or_die(input_speed, argnext, &mode); 1129 set_speed_or_die(input_speed, argnext, &mode);
1128 option_mask32 |= (STTY_require_set_attr | STTY_speed_was_set); 1130 stty_state |= (STTY_require_set_attr | STTY_speed_was_set);
1129 break; 1131 break;
1130 case param_ospeed: 1132 case param_ospeed:
1131 set_speed_or_die(output_speed, argnext, &mode); 1133 set_speed_or_die(output_speed, argnext, &mode);
1132 option_mask32 |= (STTY_require_set_attr | STTY_speed_was_set); 1134 stty_state |= (STTY_require_set_attr | STTY_speed_was_set);
1133 break; 1135 break;
1134 default: 1136 default:
1135 if (recover_mode(arg, &mode) == 1) 1137 if (recover_mode(arg, &mode) == 1)
1136 option_mask32 |= STTY_require_set_attr; 1138 stty_state |= STTY_require_set_attr;
1137 else /* true: if (tty_value_to_baud(xatou(arg)) != (speed_t) -1) */{ 1139 else /* true: if (tty_value_to_baud(xatou(arg)) != (speed_t) -1) */{
1138 set_speed_or_die(both_speeds, arg, &mode); 1140 set_speed_or_die(both_speeds, arg, &mode);
1139 option_mask32 |= (STTY_require_set_attr | STTY_speed_was_set); 1141 stty_state |= (STTY_require_set_attr | STTY_speed_was_set);
1140 } /* else - impossible (caught in the first pass): 1142 } /* else - impossible (caught in the first pass):
1141 bb_error_msg_and_die("invalid argument '%s'", arg); */ 1143 bb_error_msg_and_die("invalid argument '%s'", arg); */
1142 } 1144 }
1143 } 1145 }
1144 1146
1145 if (option_mask32 & STTY_require_set_attr) { 1147 if (stty_state & STTY_require_set_attr) {
1146 struct termios new_mode; 1148 struct termios new_mode;
1147 1149
1148 if (tcsetattr(STDIN_FILENO, TCSADRAIN, &mode)) 1150 if (tcsetattr(STDIN_FILENO, TCSADRAIN, &mode))
@@ -1173,8 +1175,8 @@ invalid_argument:
1173 error for a true failure to set the baud rate */ 1175 error for a true failure to set the baud rate */
1174 1176
1175 new_mode.c_cflag &= (~CIBAUD); 1177 new_mode.c_cflag &= (~CIBAUD);
1176 if (option_mask32 & STTY_speed_was_set || 1178 if ((stty_state & STTY_speed_was_set)
1177 memcmp(&mode, &new_mode, sizeof(mode)) != 0) 1179 || memcmp(&mode, &new_mode, sizeof(mode)) != 0)
1178#endif 1180#endif
1179 perror_on_device_and_die("%s: cannot perform all requested operations"); 1181 perror_on_device_and_die("%s: cannot perform all requested operations");
1180 } 1182 }