diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2013-05-17 18:06:49 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2013-05-17 18:06:49 +0200 |
commit | e9581b673cb731490dac0db597296047acf9b828 (patch) | |
tree | 857fbb72af5c366ab7f17df1394beb204c2c38f8 | |
parent | b443a3780d5a9f8c65c5690b7f5f4bfc2ea8134b (diff) | |
download | busybox-w32-e9581b673cb731490dac0db597296047acf9b828.tar.gz busybox-w32-e9581b673cb731490dac0db597296047acf9b828.tar.bz2 busybox-w32-e9581b673cb731490dac0db597296047acf9b828.zip |
stty: code shrink
function old new delta
set_mode 759 725 -34
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | coreutils/stty.c | 59 |
1 files changed, 29 insertions, 30 deletions
diff --git a/coreutils/stty.c b/coreutils/stty.c index 96754dd84..c52296871 100644 --- a/coreutils/stty.c +++ b/coreutils/stty.c | |||
@@ -246,10 +246,21 @@ enum speed_setting { | |||
246 | 246 | ||
247 | /* Which member(s) of 'struct termios' a mode uses */ | 247 | /* Which member(s) of 'struct termios' a mode uses */ |
248 | enum { | 248 | enum { |
249 | /* Do NOT change the order or values, as mode_type_flag() | ||
250 | * depends on them */ | ||
251 | control, input, output, local, combination | 249 | control, input, output, local, combination |
252 | }; | 250 | }; |
251 | static tcflag_t *get_ptr_to_tcflag(unsigned type, const struct termios *mode) | ||
252 | { | ||
253 | static const uint8_t tcflag_offsets[] ALIGN1 = { | ||
254 | offsetof(struct termios, c_cflag), /* control */ | ||
255 | offsetof(struct termios, c_iflag), /* input */ | ||
256 | offsetof(struct termios, c_oflag), /* output */ | ||
257 | offsetof(struct termios, c_lflag) /* local */ | ||
258 | }; | ||
259 | if (type <= local) { | ||
260 | return (tcflag_t*) (((char*)mode) + tcflag_offsets[type]); | ||
261 | } | ||
262 | return NULL; | ||
263 | } | ||
253 | 264 | ||
254 | /* Flags for 'struct mode_info' */ | 265 | /* Flags for 'struct mode_info' */ |
255 | #define SANE_SET 1 /* Set in 'sane' mode */ | 266 | #define SANE_SET 1 /* Set in 'sane' mode */ |
@@ -800,21 +811,6 @@ static const char *visible(unsigned ch) | |||
800 | return G.buf; | 811 | return G.buf; |
801 | } | 812 | } |
802 | 813 | ||
803 | static tcflag_t *mode_type_flag(unsigned type, const struct termios *mode) | ||
804 | { | ||
805 | static const uint8_t tcflag_offsets[] ALIGN1 = { | ||
806 | offsetof(struct termios, c_cflag), /* control */ | ||
807 | offsetof(struct termios, c_iflag), /* input */ | ||
808 | offsetof(struct termios, c_oflag), /* output */ | ||
809 | offsetof(struct termios, c_lflag) /* local */ | ||
810 | }; | ||
811 | |||
812 | if (type <= local) { | ||
813 | return (tcflag_t*) (((char*)mode) + tcflag_offsets[type]); | ||
814 | } | ||
815 | return NULL; | ||
816 | } | ||
817 | |||
818 | static void set_speed_or_die(enum speed_setting type, const char *arg, | 814 | static void set_speed_or_die(enum speed_setting type, const char *arg, |
819 | struct termios *mode) | 815 | struct termios *mode) |
820 | { | 816 | { |
@@ -1072,7 +1068,7 @@ static void do_display(const struct termios *mode, int all) | |||
1072 | prev_type = mode_info[i].type; | 1068 | prev_type = mode_info[i].type; |
1073 | } | 1069 | } |
1074 | 1070 | ||
1075 | bitsp = mode_type_flag(mode_info[i].type, mode); | 1071 | bitsp = get_ptr_to_tcflag(mode_info[i].type, mode); |
1076 | mask = mode_info[i].mask ? mode_info[i].mask : mode_info[i].bits; | 1072 | mask = mode_info[i].mask ? mode_info[i].mask : mode_info[i].bits; |
1077 | if ((*bitsp & mask) == mode_info[i].bits) { | 1073 | if ((*bitsp & mask) == mode_info[i].bits) { |
1078 | if (all || (mode_info[i].flags & SANE_UNSET)) | 1074 | if (all || (mode_info[i].flags & SANE_UNSET)) |
@@ -1091,7 +1087,6 @@ static void do_display(const struct termios *mode, int all) | |||
1091 | static void sane_mode(struct termios *mode) | 1087 | static void sane_mode(struct termios *mode) |
1092 | { | 1088 | { |
1093 | int i; | 1089 | int i; |
1094 | tcflag_t *bitsp; | ||
1095 | 1090 | ||
1096 | for (i = 0; i < NUM_control_info; ++i) { | 1091 | for (i = 0; i < NUM_control_info; ++i) { |
1097 | #if VMIN == VEOF | 1092 | #if VMIN == VEOF |
@@ -1102,14 +1097,17 @@ static void sane_mode(struct termios *mode) | |||
1102 | } | 1097 | } |
1103 | 1098 | ||
1104 | for (i = 0; i < NUM_mode_info; ++i) { | 1099 | for (i = 0; i < NUM_mode_info; ++i) { |
1100 | tcflag_t val; | ||
1101 | tcflag_t *bitsp = get_ptr_to_tcflag(mode_info[i].type, mode); | ||
1102 | |||
1103 | if (!bitsp) | ||
1104 | continue; | ||
1105 | val = *bitsp & ~((unsigned long)mode_info[i].mask); | ||
1105 | if (mode_info[i].flags & SANE_SET) { | 1106 | if (mode_info[i].flags & SANE_SET) { |
1106 | bitsp = mode_type_flag(mode_info[i].type, mode); | 1107 | *bitsp = val | mode_info[i].bits; |
1107 | *bitsp = (*bitsp & ~((unsigned long)mode_info[i].mask)) | 1108 | } else |
1108 | | mode_info[i].bits; | 1109 | if (mode_info[i].flags & SANE_UNSET) { |
1109 | } else if (mode_info[i].flags & SANE_UNSET) { | 1110 | *bitsp = val & ~mode_info[i].bits; |
1110 | bitsp = mode_type_flag(mode_info[i].type, mode); | ||
1111 | *bitsp = *bitsp & ~((unsigned long)mode_info[i].mask) | ||
1112 | & ~mode_info[i].bits; | ||
1113 | } | 1111 | } |
1114 | } | 1112 | } |
1115 | } | 1113 | } |
@@ -1119,17 +1117,18 @@ static void set_mode(const struct mode_info *info, int reversed, | |||
1119 | { | 1117 | { |
1120 | tcflag_t *bitsp; | 1118 | tcflag_t *bitsp; |
1121 | 1119 | ||
1122 | bitsp = mode_type_flag(info->type, mode); | 1120 | bitsp = get_ptr_to_tcflag(info->type, mode); |
1123 | 1121 | ||
1124 | if (bitsp) { | 1122 | if (bitsp) { |
1123 | tcflag_t val = *bitsp & ~info->mask; | ||
1125 | if (reversed) | 1124 | if (reversed) |
1126 | *bitsp = *bitsp & ~info->mask & ~info->bits; | 1125 | *bitsp = val & ~info->bits; |
1127 | else | 1126 | else |
1128 | *bitsp = (*bitsp & ~info->mask) | info->bits; | 1127 | *bitsp = val | info->bits; |
1129 | return; | 1128 | return; |
1130 | } | 1129 | } |
1131 | 1130 | ||
1132 | /* Combination mode */ | 1131 | /* !bitsp - it's a "combination" mode */ |
1133 | if (info == &mode_info[IDX_evenp] || info == &mode_info[IDX_parity]) { | 1132 | if (info == &mode_info[IDX_evenp] || info == &mode_info[IDX_parity]) { |
1134 | if (reversed) | 1133 | if (reversed) |
1135 | mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8; | 1134 | mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8; |