stty: code shrink

function                                             old     new   delta
set_mode                                             759     725     -34

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2013-05-17 18:06:49 +02:00
parent b443a3780d
commit e9581b673c

View File

@ -246,10 +246,21 @@ enum speed_setting {
/* Which member(s) of 'struct termios' a mode uses */ /* Which member(s) of 'struct termios' a mode uses */
enum { enum {
/* Do NOT change the order or values, as mode_type_flag()
* depends on them */
control, input, output, local, combination control, input, output, local, combination
}; };
static tcflag_t *get_ptr_to_tcflag(unsigned type, const struct termios *mode)
{
static const uint8_t tcflag_offsets[] ALIGN1 = {
offsetof(struct termios, c_cflag), /* control */
offsetof(struct termios, c_iflag), /* input */
offsetof(struct termios, c_oflag), /* output */
offsetof(struct termios, c_lflag) /* local */
};
if (type <= local) {
return (tcflag_t*) (((char*)mode) + tcflag_offsets[type]);
}
return NULL;
}
/* Flags for 'struct mode_info' */ /* Flags for 'struct mode_info' */
#define SANE_SET 1 /* Set in 'sane' mode */ #define SANE_SET 1 /* Set in 'sane' mode */
@ -800,21 +811,6 @@ static const char *visible(unsigned ch)
return G.buf; return G.buf;
} }
static tcflag_t *mode_type_flag(unsigned type, const struct termios *mode)
{
static const uint8_t tcflag_offsets[] ALIGN1 = {
offsetof(struct termios, c_cflag), /* control */
offsetof(struct termios, c_iflag), /* input */
offsetof(struct termios, c_oflag), /* output */
offsetof(struct termios, c_lflag) /* local */
};
if (type <= local) {
return (tcflag_t*) (((char*)mode) + tcflag_offsets[type]);
}
return NULL;
}
static void set_speed_or_die(enum speed_setting type, const char *arg, static void set_speed_or_die(enum speed_setting type, const char *arg,
struct termios *mode) struct termios *mode)
{ {
@ -1072,7 +1068,7 @@ static void do_display(const struct termios *mode, int all)
prev_type = mode_info[i].type; prev_type = mode_info[i].type;
} }
bitsp = mode_type_flag(mode_info[i].type, mode); bitsp = get_ptr_to_tcflag(mode_info[i].type, mode);
mask = mode_info[i].mask ? mode_info[i].mask : mode_info[i].bits; mask = mode_info[i].mask ? mode_info[i].mask : mode_info[i].bits;
if ((*bitsp & mask) == mode_info[i].bits) { if ((*bitsp & mask) == mode_info[i].bits) {
if (all || (mode_info[i].flags & SANE_UNSET)) if (all || (mode_info[i].flags & SANE_UNSET))
@ -1091,7 +1087,6 @@ static void do_display(const struct termios *mode, int all)
static void sane_mode(struct termios *mode) static void sane_mode(struct termios *mode)
{ {
int i; int i;
tcflag_t *bitsp;
for (i = 0; i < NUM_control_info; ++i) { for (i = 0; i < NUM_control_info; ++i) {
#if VMIN == VEOF #if VMIN == VEOF
@ -1102,14 +1097,17 @@ static void sane_mode(struct termios *mode)
} }
for (i = 0; i < NUM_mode_info; ++i) { for (i = 0; i < NUM_mode_info; ++i) {
tcflag_t val;
tcflag_t *bitsp = get_ptr_to_tcflag(mode_info[i].type, mode);
if (!bitsp)
continue;
val = *bitsp & ~((unsigned long)mode_info[i].mask);
if (mode_info[i].flags & SANE_SET) { if (mode_info[i].flags & SANE_SET) {
bitsp = mode_type_flag(mode_info[i].type, mode); *bitsp = val | mode_info[i].bits;
*bitsp = (*bitsp & ~((unsigned long)mode_info[i].mask)) } else
| mode_info[i].bits; if (mode_info[i].flags & SANE_UNSET) {
} else if (mode_info[i].flags & SANE_UNSET) { *bitsp = val & ~mode_info[i].bits;
bitsp = mode_type_flag(mode_info[i].type, mode);
*bitsp = *bitsp & ~((unsigned long)mode_info[i].mask)
& ~mode_info[i].bits;
} }
} }
} }
@ -1119,17 +1117,18 @@ static void set_mode(const struct mode_info *info, int reversed,
{ {
tcflag_t *bitsp; tcflag_t *bitsp;
bitsp = mode_type_flag(info->type, mode); bitsp = get_ptr_to_tcflag(info->type, mode);
if (bitsp) { if (bitsp) {
tcflag_t val = *bitsp & ~info->mask;
if (reversed) if (reversed)
*bitsp = *bitsp & ~info->mask & ~info->bits; *bitsp = val & ~info->bits;
else else
*bitsp = (*bitsp & ~info->mask) | info->bits; *bitsp = val | info->bits;
return; return;
} }
/* Combination mode */ /* !bitsp - it's a "combination" mode */
if (info == &mode_info[IDX_evenp] || info == &mode_info[IDX_parity]) { if (info == &mode_info[IDX_evenp] || info == &mode_info[IDX_parity]) {
if (reversed) if (reversed)
mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8; mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;