lint: too few branches in switch statement

Someone was way too enamored of the `switch` statement. Using it in
places where a simple `if...else if...else` was clearer and shorter.
This commit is contained in:
Kurtis Rader 2016-10-22 20:32:25 -07:00
parent 42458ff7ab
commit 21521b2953
13 changed files with 339 additions and 467 deletions

View file

@ -345,23 +345,16 @@ static int get_terminfo_sequence(const wchar_t *seq, wcstring *out_seq, io_strea
if (input_terminfo_get_sequence(seq, out_seq)) { if (input_terminfo_get_sequence(seq, out_seq)) {
return 1; return 1;
} }
wcstring eseq = escape_string(seq, 0); wcstring eseq = escape_string(seq, 0);
switch (errno) { if (errno == ENOENT) {
case ENOENT: { streams.err.append_format(_(L"%ls: No key with name '%ls' found\n"), L"bind", eseq.c_str());
streams.err.append_format(_(L"%ls: No key with name '%ls' found\n"), L"bind", } else if (errno == EILSEQ) {
eseq.c_str()); streams.err.append_format(_(L"%ls: Key with name '%ls' does not have any mapping\n"),
break; L"bind", eseq.c_str());
} } else {
case EILSEQ: { streams.err.append_format(_(L"%ls: Unknown error trying to bind to key named '%ls'\n"),
streams.err.append_format(_(L"%ls: Key with name '%ls' does not have any mapping\n"), L"bind", eseq.c_str());
L"bind", eseq.c_str());
break;
}
default: {
streams.err.append_format(_(L"%ls: Unknown error trying to bind to key named '%ls'\n"),
L"bind", eseq.c_str());
break;
}
} }
return 0; return 0;
} }
@ -525,43 +518,37 @@ static int builtin_bind(parser_t &parser, io_streams_t &streams, wchar_t **argv)
break; break;
} }
case BIND_INSERT: { case BIND_INSERT: {
switch (argc - w.woptind) { int arg_count = argc - w.woptind;
case 0: { if (arg_count == 0) {
builtin_bind_list(bind_mode_given ? bind_mode : NULL, streams); builtin_bind_list(bind_mode_given ? bind_mode : NULL, streams);
break; } else if (arg_count == 1) {
wcstring seq;
if (use_terminfo) {
if (!get_terminfo_sequence(argv[w.woptind], &seq, streams)) {
res = STATUS_BUILTIN_ERROR;
// get_terminfo_sequence already printed the error.
break;
}
} else {
seq = argv[w.woptind];
} }
case 1: { if (!builtin_bind_list_one(seq, bind_mode, streams)) {
wcstring seq; res = STATUS_BUILTIN_ERROR;
wcstring eseq = escape_string(argv[w.woptind], 0);
if (use_terminfo) { if (use_terminfo) {
if (!get_terminfo_sequence(argv[w.woptind], &seq, streams)) { streams.err.append_format(_(L"%ls: No binding found for key '%ls'\n"),
res = STATUS_BUILTIN_ERROR; argv[0], eseq.c_str());
// get_terminfo_sequence already printed the error.
break;
}
} else { } else {
seq = argv[w.woptind]; streams.err.append_format(_(L"%ls: No binding found for sequence '%ls'\n"),
argv[0], eseq.c_str());
} }
if (!builtin_bind_list_one(seq, bind_mode, streams)) {
res = STATUS_BUILTIN_ERROR;
wcstring eseq = escape_string(argv[w.woptind], 0);
if (use_terminfo) {
streams.err.append_format(_(L"%ls: No binding found for key '%ls'\n"),
argv[0], eseq.c_str());
} else {
streams.err.append_format(
_(L"%ls: No binding found for sequence '%ls'\n"), argv[0],
eseq.c_str());
}
}
break;
} }
default: { break;
if (builtin_bind_add(argv[w.woptind], argv + (w.woptind + 1), } else {
argc - (w.woptind + 1), bind_mode, sets_bind_mode, if (builtin_bind_add(argv[w.woptind], argv + (w.woptind + 1),
use_terminfo, streams)) { argc - (w.woptind + 1), bind_mode, sets_bind_mode,
res = STATUS_BUILTIN_ERROR; use_terminfo, streams)) {
} res = STATUS_BUILTIN_ERROR;
break;
} }
} }
break; break;
@ -1776,39 +1763,33 @@ static int builtin_random(parser_t &parser, io_streams_t &streams, wchar_t **arg
} }
} }
switch (argc - w.woptind) { int arg_count = argc - w.woptind;
case 0: { if (arg_count == 0) {
long res; long res;
if (!seeded) { if (!seeded) {
seeded = 1;
srand48_r(time(0), &seed_buffer);
}
lrand48_r(&seed_buffer, &res);
streams.out.append_format(L"%ld\n", res % 32768);
break;
}
case 1: {
long foo;
wchar_t *end = 0;
errno = 0;
foo = wcstol(argv[w.woptind], &end, 10);
if (errno || *end) {
streams.err.append_format(_(L"%ls: Seed value '%ls' is not a valid number\n"),
argv[0], argv[w.woptind]);
return STATUS_BUILTIN_ERROR;
}
seeded = 1; seeded = 1;
srand48_r(foo, &seed_buffer); srand48_r(time(0), &seed_buffer);
break;
} }
default: { lrand48_r(&seed_buffer, &res);
streams.err.append_format(_(L"%ls: Expected zero or one argument, got %d\n"), argv[0], streams.out.append_format(L"%ld\n", res % 32768);
argc - w.woptind); } else if (arg_count == 1) {
builtin_print_help(parser, streams, argv[0], streams.err); long foo;
wchar_t *end = 0;
errno = 0;
foo = wcstol(argv[w.woptind], &end, 10);
if (errno || *end) {
streams.err.append_format(_(L"%ls: Seed value '%ls' is not a valid number\n"), argv[0],
argv[w.woptind]);
return STATUS_BUILTIN_ERROR; return STATUS_BUILTIN_ERROR;
} }
seeded = 1;
srand48_r(foo, &seed_buffer);
} else {
streams.err.append_format(_(L"%ls: Expected zero or one argument, got %d\n"), argv[0],
argc - w.woptind);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
} }
return STATUS_BUILTIN_OK; return STATUS_BUILTIN_OK;
} }
@ -1900,19 +1881,16 @@ static int builtin_read(parser_t &parser, io_streams_t &streams, wchar_t **argv)
errno = 0; errno = 0;
nchars = fish_wcstoi(w.woptarg, &end, 10); nchars = fish_wcstoi(w.woptarg, &end, 10);
if (errno || *end != 0) { if (errno || *end != 0) {
switch (errno) { if (errno == ERANGE) {
case ERANGE: { streams.err.append_format(_(L"%ls: Argument '%ls' is out of range\n"),
streams.err.append_format(_(L"%ls: Argument '%ls' is out of range\n"), argv[0], w.woptarg);
argv[0], w.woptarg); builtin_print_help(parser, streams, argv[0], streams.err);
builtin_print_help(parser, streams, argv[0], streams.err); return STATUS_BUILTIN_ERROR;
return STATUS_BUILTIN_ERROR; } else {
} streams.err.append_format(_(L"%ls: Argument '%ls' must be an integer\n"),
default: { argv[0], w.woptarg);
streams.err.append_format( builtin_print_help(parser, streams, argv[0], streams.err);
_(L"%ls: Argument '%ls' must be an integer\n"), argv[0], w.woptarg); return STATUS_BUILTIN_ERROR;
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
}
} }
} }
break; break;
@ -2045,18 +2023,10 @@ static int builtin_read(parser_t &parser, io_streams_t &streams, wchar_t **argv)
finished = 1; finished = 1;
} else { } else {
size_t sz = mbrtowc(&res, &b, 1, &state); size_t sz = mbrtowc(&res, &b, 1, &state);
switch (sz) { if (sz == (size_t)-1) {
case (size_t)-1: { memset(&state, 0, sizeof(state));
memset(&state, 0, sizeof(state)); } else if (sz != (size_t)-2) {
break; finished = 1;
}
case (size_t)-2: {
break;
}
default: {
finished = 1;
break;
}
} }
} }
} }
@ -2310,27 +2280,22 @@ static int builtin_status(parser_t &parser, io_streams_t &streams, wchar_t **arg
static int builtin_exit(parser_t &parser, io_streams_t &streams, wchar_t **argv) { static int builtin_exit(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
int argc = builtin_count_args(argv); int argc = builtin_count_args(argv);
long ec = 0; if (argc > 2) {
switch (argc) { streams.err.append_format(BUILTIN_ERR_TOO_MANY_ARGUMENTS, argv[0]);
case 1: { builtin_print_help(parser, streams, argv[0], streams.err);
ec = proc_get_last_status(); return STATUS_BUILTIN_ERROR;
break; }
}
case 2: {
wchar_t *end;
errno = 0;
ec = wcstol(argv[1], &end, 10);
if (errno || *end != 0) {
streams.err.append_format(_(L"%ls: Argument '%ls' must be an integer\n"), argv[0],
argv[1]);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
}
break;
}
default: {
streams.err.append_format(BUILTIN_ERR_TOO_MANY_ARGUMENTS, argv[0]);
long ec;
if (argc == 1) {
ec = proc_get_last_status();
} else {
wchar_t *end;
errno = 0;
ec = wcstol(argv[1], &end, 10);
if (errno || *end != 0) {
streams.err.append_format(_(L"%ls: Argument '%ls' must be an integer\n"), argv[0],
argv[1]);
builtin_print_help(parser, streams, argv[0], streams.err); builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR; return STATUS_BUILTIN_ERROR;
} }
@ -2752,29 +2717,26 @@ static int builtin_breakpoint(parser_t &parser, io_streams_t &streams, wchar_t *
/// Function for handling the \c return builtin. /// Function for handling the \c return builtin.
static int builtin_return(parser_t &parser, io_streams_t &streams, wchar_t **argv) { static int builtin_return(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
int argc = builtin_count_args(argv); int argc = builtin_count_args(argv);
int status = proc_get_last_status();
switch (argc) { if (argc > 2) {
case 1: { streams.err.append_format(_(L"%ls: Too many arguments\n"), argv[0]);
break; builtin_print_help(parser, streams, argv[0], streams.err);
} return STATUS_BUILTIN_ERROR;
case 2: { }
wchar_t *end;
errno = 0; int status;
status = fish_wcstoi(argv[1], &end, 10); if (argc == 2) {
if (errno || *end != 0) { wchar_t *end;
streams.err.append_format(_(L"%ls: Argument '%ls' must be an integer\n"), argv[0], errno = 0;
argv[1]); status = fish_wcstoi(argv[1], &end, 10);
builtin_print_help(parser, streams, argv[0], streams.err); if (errno || *end != 0) {
return STATUS_BUILTIN_ERROR; streams.err.append_format(_(L"%ls: Argument '%ls' must be an integer\n"), argv[0],
} argv[1]);
break;
}
default: {
streams.err.append_format(_(L"%ls: Too many arguments\n"), argv[0]);
builtin_print_help(parser, streams, argv[0], streams.err); builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR; return STATUS_BUILTIN_ERROR;
} }
} else {
status = proc_get_last_status();
} }
// Find the function block. // Find the function block.

View file

@ -152,15 +152,11 @@ static void write_part(const wchar_t *begin, const wchar_t *end, int cut_at_curs
while (tok.next(&token)) { while (tok.next(&token)) {
if ((cut_at_cursor) && (token.offset + token.text.size() >= pos)) break; if ((cut_at_cursor) && (token.offset + token.text.size() >= pos)) break;
switch (token.type) { if (token.type == TOK_STRING) {
case TOK_STRING: { wcstring tmp = token.text;
wcstring tmp = token.text; unescape_string_in_place(&tmp, UNESCAPE_INCOMPLETE);
unescape_string_in_place(&tmp, UNESCAPE_INCOMPLETE); out.append(tmp);
out.append(tmp); out.push_back(L'\n');
out.push_back(L'\n');
break;
}
default: { break; }
} }
} }
@ -476,24 +472,18 @@ int builtin_commandline(parser_t &parser, io_streams_t &streams, wchar_t **argv)
} }
} }
switch (argc - w.woptind) { int arg_count = argc - w.woptind;
case 0: { if (arg_count == 0) {
write_part(begin, end, cut_at_cursor, tokenize, streams); write_part(begin, end, cut_at_cursor, tokenize, streams);
break; } else if (arg_count == 1) {
} replace_part(begin, end, argv[w.woptind], append_mode);
case 1: { } else {
replace_part(begin, end, argv[w.woptind], append_mode); wcstring sb = argv[w.woptind];
break; for (int i = w.woptind + 1; i < argc; i++) {
} sb.push_back(L'\n');
default: { sb.append(argv[i]);
wcstring sb = argv[w.woptind];
for (int i = w.woptind + 1; i < argc; i++) {
sb.push_back(L'\n');
sb.append(argv[i]);
}
replace_part(begin, end, sb.c_str(), append_mode);
break;
} }
replace_part(begin, end, sb.c_str(), append_mode);
} }
return 0; return 0;

View file

@ -579,62 +579,55 @@ bool binary_primary::evaluate(wcstring_list_t &errors) {
} }
bool unary_operator::evaluate(wcstring_list_t &errors) { bool unary_operator::evaluate(wcstring_list_t &errors) {
switch (token) { if (token == test_bang) {
case test_bang: { assert(subject.get());
assert(subject.get()); return !subject->evaluate(errors);
return !subject->evaluate(errors);
}
default: {
errors.push_back(format_string(L"Unknown token type in %s", __func__));
return false;
}
} }
errors.push_back(format_string(L"Unknown token type in %s", __func__));
return false;
} }
bool combining_expression::evaluate(wcstring_list_t &errors) { bool combining_expression::evaluate(wcstring_list_t &errors) {
switch (token) { if (token == test_combine_and || token == test_combine_or) {
case test_combine_and: assert(!subjects.empty());
case test_combine_or: { assert(combiners.size() + 1 == subjects.size());
assert(!subjects.empty());
assert(combiners.size() + 1 == subjects.size());
// One-element case. // One-element case.
if (subjects.size() == 1) return subjects.at(0)->evaluate(errors); if (subjects.size() == 1) return subjects.at(0)->evaluate(errors);
// Evaluate our lists, remembering that AND has higher precedence than OR. We can // Evaluate our lists, remembering that AND has higher precedence than OR. We can
// visualize this as a sequence of OR expressions of AND expressions. // visualize this as a sequence of OR expressions of AND expressions.
size_t idx = 0, max = subjects.size(); size_t idx = 0, max = subjects.size();
bool or_result = false; bool or_result = false;
while (idx < max) { while (idx < max) {
if (or_result) { // short circuit if (or_result) { // short circuit
break;
}
// Evaluate a stream of AND starting at given subject index. It may only have one
// element.
bool and_result = true;
for (; idx < max; idx++) {
// Evaluate it, short-circuiting.
and_result = and_result && subjects.at(idx)->evaluate(errors);
// If the combiner at this index (which corresponding to how we combine with the
// next subject) is not AND, then exit the loop.
if (idx + 1 < max && combiners.at(idx) != test_combine_and) {
idx++;
break; break;
} }
// Evaluate a stream of AND starting at given subject index. It may only have one
// element.
bool and_result = true;
for (; idx < max; idx++) {
// Evaluate it, short-circuiting.
and_result = and_result && subjects.at(idx)->evaluate(errors);
// If the combiner at this index (which corresponding to how we combine with the
// next subject) is not AND, then exit the loop.
if (idx + 1 < max && combiners.at(idx) != test_combine_and) {
idx++;
break;
}
}
// OR it in.
or_result = or_result || and_result;
} }
return or_result;
} // OR it in.
default: { or_result = or_result || and_result;
errors.push_back(format_string(L"Unknown token type in %s", __func__));
return BUILTIN_TEST_FAIL;
} }
return or_result;
} }
errors.push_back(format_string(L"Unknown token type in %s", __func__));
return BUILTIN_TEST_FAIL;
} }
bool parenthetical_expression::evaluate(wcstring_list_t &errors) { bool parenthetical_expression::evaluate(wcstring_list_t &errors) {
@ -798,42 +791,36 @@ int builtin_test(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
// Collect the arguments into a list. // Collect the arguments into a list.
const wcstring_list_t args(argv + 1, argv + 1 + argc); const wcstring_list_t args(argv + 1, argv + 1 + argc);
switch (argc) { if (argc == 0) {
case 0: { return BUILTIN_TEST_FAIL; // Per 1003.1, exit false.
// Per 1003.1, exit false. } else if (argc == 1) {
return BUILTIN_TEST_FAIL; // Per 1003.1, exit true if the arg is non-empty.
} return args.at(0).empty() ? BUILTIN_TEST_FAIL : BUILTIN_TEST_SUCCESS;
case 1: { }
// Per 1003.1, exit true if the arg is non-empty.
return args.at(0).empty() ? BUILTIN_TEST_FAIL : BUILTIN_TEST_SUCCESS;
}
default: {
// Try parsing. If expr is not nil, we are responsible for deleting it.
wcstring err;
expression *expr = test_parser::parse_args(args, err);
if (!expr) {
#if 0
printf("Oops! test was given args:\n");
for (size_t i=0; i < argc; i++) {
printf("\t%ls\n", args.at(i).c_str());
}
printf("and returned parse error: %ls\n", err.c_str());
#endif
streams.err.append(err);
return BUILTIN_TEST_FAIL;
}
wcstring_list_t eval_errors; // Try parsing. If expr is not nil, we are responsible for deleting it.
bool result = expr->evaluate(eval_errors); wcstring err;
if (!eval_errors.empty()) { expression *expr = test_parser::parse_args(args, err);
printf("test returned eval errors:\n"); if (!expr) {
for (size_t i = 0; i < eval_errors.size(); i++) { #if 0
printf("\t%ls\n", eval_errors.at(i).c_str()); printf("Oops! test was given args:\n");
} for (size_t i=0; i < argc; i++) {
} printf("\t%ls\n", args.at(i).c_str());
delete expr; }
return result ? BUILTIN_TEST_SUCCESS : BUILTIN_TEST_FAIL; printf("and returned parse error: %ls\n", err.c_str());
#endif
streams.err.append(err);
return BUILTIN_TEST_FAIL;
}
wcstring_list_t eval_errors;
bool result = expr->evaluate(eval_errors);
if (!eval_errors.empty()) {
printf("test returned eval errors:\n");
for (size_t i = 0; i < eval_errors.size(); i++) {
printf("\t%ls\n", eval_errors.at(i).c_str());
} }
} }
return 1; delete expr;
return result ? BUILTIN_TEST_SUCCESS : BUILTIN_TEST_FAIL;
} }

View file

@ -278,46 +278,42 @@ int builtin_ulimit(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
return 0; return 0;
} }
switch (argc - w.woptind) { int arg_count = argc - w.woptind;
case 0: { // show current limit value if (arg_count == 0) {
print(what, hard, streams); // Show current limit value.
break; print(what, hard, streams);
} } else if (arg_count == 1) {
case 1: { // change current limit value // Change current limit value.
rlim_t new_limit; rlim_t new_limit;
wchar_t *end; wchar_t *end;
// Set both hard and soft limits if nothing else was specified. // Set both hard and soft limits if nothing else was specified.
if (!(hard + soft)) { if (!(hard + soft)) {
hard = soft = 1; hard = soft = 1;
}
if (wcscasecmp(argv[w.woptind], L"unlimited") == 0) {
new_limit = RLIM_INFINITY;
} else if (wcscasecmp(argv[w.woptind], L"hard") == 0) {
new_limit = get(what, 1);
} else if (wcscasecmp(argv[w.woptind], L"soft") == 0) {
new_limit = get(what, soft);
} else {
errno = 0;
new_limit = wcstol(argv[w.woptind], &end, 10);
if (errno || *end) {
streams.err.append_format(L"%ls: Invalid limit '%ls'\n", argv[0], argv[w.woptind]);
builtin_print_help(parser, streams, argv[0], streams.err);
return 1;
} }
new_limit *= get_multiplier(what);
if (wcscasecmp(argv[w.woptind], L"unlimited") == 0) {
new_limit = RLIM_INFINITY;
} else if (wcscasecmp(argv[w.woptind], L"hard") == 0) {
new_limit = get(what, 1);
} else if (wcscasecmp(argv[w.woptind], L"soft") == 0) {
new_limit = get(what, soft);
} else {
errno = 0;
new_limit = wcstol(argv[w.woptind], &end, 10);
if (errno || *end) {
streams.err.append_format(L"%ls: Invalid limit '%ls'\n", argv[0],
argv[w.woptind]);
builtin_print_help(parser, streams, argv[0], streams.err);
return 1;
}
new_limit *= get_multiplier(what);
}
return set(what, hard, soft, new_limit, streams);
}
default: {
streams.err.append(argv[0]);
streams.err.append(L": Too many arguments\n");
builtin_print_help(parser, streams, argv[0], streams.err);
return 1;
} }
return set(what, hard, soft, new_limit, streams);
} }
return 0;
streams.err.append(argv[0]);
streams.err.append(L": Too many arguments\n");
builtin_print_help(parser, streams, argv[0], streams.err);
return 1;
} }

View file

@ -166,36 +166,31 @@ wcstring expand_escape_variable(const wcstring &in) {
tokenize_variable_array(in, lst); tokenize_variable_array(in, lst);
switch (lst.size()) { int size = lst.size();
case 0: { if (size == 0) {
buff.append(L"''"); buff.append(L"''");
break; } else if (size == 1) {
} const wcstring &el = lst.at(0);
case 1: {
const wcstring &el = lst.at(0);
if (el.find(L' ') != wcstring::npos && is_quotable(el)) { if (el.find(L' ') != wcstring::npos && is_quotable(el)) {
buff.append(L"'");
buff.append(el);
buff.append(L"'");
} else {
buff.append(escape_string(el, 1));
}
} else {
for (size_t j = 0; j < lst.size(); j++) {
const wcstring &el = lst.at(j);
if (j) buff.append(L" ");
if (is_quotable(el)) {
buff.append(L"'"); buff.append(L"'");
buff.append(el); buff.append(el);
buff.append(L"'"); buff.append(L"'");
} else { } else {
buff.append(escape_string(el, 1)); buff.append(escape_string(el, 1));
} }
break;
}
default: {
for (size_t j = 0; j < lst.size(); j++) {
const wcstring &el = lst.at(j);
if (j) buff.append(L" ");
if (is_quotable(el)) {
buff.append(L"'");
buff.append(el);
buff.append(L"'");
} else {
buff.append(escape_string(el, 1));
}
}
} }
} }
return buff; return buff;

View file

@ -223,14 +223,8 @@ void input_set_bind_mode(const wcstring &bm) {
} }
/// Returns the arity of a given input function. /// Returns the arity of a given input function.
int input_function_arity(int function) { static int input_function_arity(int function) {
switch (function) { return (function == R_FORWARD_JUMP || function == R_BACKWARD_JUMP) ? 1 : 0;
case R_FORWARD_JUMP:
case R_BACKWARD_JUMP: {
return 1;
}
default: { return 0; }
}
} }
/// Sets the return status of the most recently executed input function. /// Sets the return status of the most recently executed input function.

View file

@ -110,26 +110,17 @@ static wint_t readb() {
res = select(fd_max + 1, &fdset, 0, 0, usecs_delay > 0 ? &tv : NULL); res = select(fd_max + 1, &fdset, 0, 0, usecs_delay > 0 ? &tv : NULL);
if (res == -1) { if (res == -1) {
switch (errno) { if (errno == EINTR || errno == EAGAIN) {
case EINTR: if (interrupt_handler) {
case EAGAIN: { int res = interrupt_handler();
if (interrupt_handler) { if (res) return res;
int res = interrupt_handler(); if (has_lookahead()) return lookahead_pop();
if (res) { }
return res;
}
if (has_lookahead()) {
return lookahead_pop();
}
}
do_loop = true; do_loop = true;
break; } else {
} // The terminal has been closed. Save and exit.
default: { return R_EOF;
// The terminal has been closed. Save and exit.
return R_EOF;
}
} }
} else { } else {
// Assume we loop unless we see a character in stdin. // Assume we loop unless we see a character in stdin.

View file

@ -222,21 +222,15 @@ int iothread_port(void) {
void iothread_service_completion(void) { void iothread_service_completion(void) {
ASSERT_IS_MAIN_THREAD(); ASSERT_IS_MAIN_THREAD();
char wakeup_byte = 0; char wakeup_byte;
VOMIT_ON_FAILURE(1 != read_loop(iothread_port(), &wakeup_byte, sizeof wakeup_byte)); VOMIT_ON_FAILURE(1 != read_loop(iothread_port(), &wakeup_byte, sizeof wakeup_byte));
switch (wakeup_byte) { if (wakeup_byte == IO_SERVICE_MAIN_THREAD_REQUEST_QUEUE) {
case IO_SERVICE_MAIN_THREAD_REQUEST_QUEUE: { iothread_service_main_thread_requests();
iothread_service_main_thread_requests(); } else if (wakeup_byte == IO_SERVICE_RESULT_QUEUE) {
break; iothread_service_result_queue();
} } else {
case IO_SERVICE_RESULT_QUEUE: { fprintf(stderr, "Unknown wakeup byte %02x in %s\n", wakeup_byte, __FUNCTION__);
iothread_service_result_queue();
break;
}
default: {
fprintf(stderr, "Unknown wakeup byte %02x in %s\n", wakeup_byte, __FUNCTION__);
break;
}
} }
} }

View file

@ -924,18 +924,13 @@ bool parse_ll_t::top_node_handle_terminal_types(parse_token_t token) {
// Now see if we actually matched // Now see if we actually matched
bool matched = false; bool matched = false;
if (stack_top.type == token.type) { if (stack_top.type == token.type) {
switch (stack_top.type) { if (stack_top.type == parse_token_type_string) {
case parse_token_type_string: { // We matched if the keywords match, or no keyword was required.
// We matched if the keywords match, or no keyword was required. matched =
matched = (stack_top.keyword == parse_keyword_none || (stack_top.keyword == parse_keyword_none || stack_top.keyword == token.keyword);
stack_top.keyword == token.keyword); } else {
break; // For other types, we only require that the types match.
} matched = true;
default: {
// For other types, we only require that the types match.
matched = true;
break;
}
} }
} }
@ -1550,8 +1545,7 @@ enum parse_bool_statement_type_t parse_node_tree_t::statement_boolean_type(
bool parse_node_tree_t::job_should_be_backgrounded(const parse_node_t &job) const { bool parse_node_tree_t::job_should_be_backgrounded(const parse_node_t &job) const {
assert(job.type == symbol_job); assert(job.type == symbol_job);
const parse_node_t *opt_background = get_child(job, 2, symbol_optional_background); const parse_node_t *opt_background = get_child(job, 2, symbol_optional_background);
bool result = opt_background != NULL && opt_background->tag == parse_background; return opt_background != NULL && opt_background->tag == parse_background;
return result;
} }
const parse_node_t *parse_node_tree_t::next_node_in_node_list( const parse_node_t *parse_node_tree_t::next_node_in_node_list(

View file

@ -1019,28 +1019,23 @@ parser_test_error_bits_t parse_util_detect_errors_in_argument(const parse_node_t
// Check for invalid variable expansions. // Check for invalid variable expansions.
const size_t unesc_size = unesc.size(); const size_t unesc_size = unesc.size();
for (size_t idx = 0; idx < unesc_size; idx++) { for (size_t idx = 0; idx < unesc_size; idx++) {
switch (unesc.at(idx)) { if (unesc.at(idx) == VARIABLE_EXPAND || unesc.at(idx) == VARIABLE_EXPAND_SINGLE) {
case VARIABLE_EXPAND: wchar_t next_char = idx + 1 < unesc_size ? unesc.at(idx + 1) : L'\0';
case VARIABLE_EXPAND_SINGLE: {
wchar_t next_char = idx + 1 < unesc_size ? unesc.at(idx + 1) : L'\0';
if (next_char != VARIABLE_EXPAND && next_char != VARIABLE_EXPAND_SINGLE && if (next_char != VARIABLE_EXPAND && next_char != VARIABLE_EXPAND_SINGLE &&
!wcsvarchr(next_char)) { !wcsvarchr(next_char)) {
err = 1; err = 1;
if (out_errors) { if (out_errors) {
// We have something like $$$^.... Back up until we reach the first $. // We have something like $$$^.... Back up until we reach the first $.
size_t first_dollar = idx; size_t first_dollar = idx;
while (first_dollar > 0 && while (first_dollar > 0 &&
(unesc.at(first_dollar - 1) == VARIABLE_EXPAND || (unesc.at(first_dollar - 1) == VARIABLE_EXPAND ||
unesc.at(first_dollar - 1) == VARIABLE_EXPAND_SINGLE)) { unesc.at(first_dollar - 1) == VARIABLE_EXPAND_SINGLE)) {
first_dollar--; first_dollar--;
}
parse_util_expand_variable_error(unesc, node.source_start, first_dollar,
out_errors);
} }
parse_util_expand_variable_error(unesc, node.source_start, first_dollar,
out_errors);
} }
break;
} }
} }
} }

View file

@ -190,16 +190,10 @@ wcstring path_apply_working_directory(const wcstring &path, const wcstring &work
// We're going to make sure that if we want to prepend the wd, that the string has no leading // We're going to make sure that if we want to prepend the wd, that the string has no leading
// "/". // "/".
bool prepend_wd; bool prepend_wd;
switch (path.at(0)) { if (path.at(0) == L'/' || path.at(0) == HOME_DIRECTORY) {
case L'/': prepend_wd = false;
case HOME_DIRECTORY: { } else {
prepend_wd = false; prepend_wd = true;
break;
}
default: {
prepend_wd = true;
break;
}
} }
if (!prepend_wd) { if (!prepend_wd) {

View file

@ -1379,27 +1379,23 @@ static bool handle_completions(const std::vector<completion_t> &comp,
const wcstring tok(begin, end - begin); const wcstring tok(begin, end - begin);
// Check trivial cases. // Check trivial cases.
switch (comp.size()) { int size = comp.size();
case 0: { if (size == 0) {
// No suitable completions found, flash screen and return. // No suitable completions found, flash screen and return.
reader_flash(); reader_flash();
done = true; done = true;
success = false; success = false;
break; } else if (size == 1) {
} // Exactly one suitable completion found - insert it.
case 1: { const completion_t &c = comp.at(0);
// Exactly one suitable completion found - insert it.
const completion_t &c = comp.at(0);
// If this is a replacement completion, check that we know how to replace it, e.g. that // If this is a replacement completion, check that we know how to replace it, e.g. that
// the token doesn't contain evil operators like {}. // the token doesn't contain evil operators like {}.
if (!(c.flags & COMPLETE_REPLACES_TOKEN) || reader_can_replace(tok, c.flags)) { if (!(c.flags & COMPLETE_REPLACES_TOKEN) || reader_can_replace(tok, c.flags)) {
completion_insert(c.completion.c_str(), c.flags); completion_insert(c.completion.c_str(), c.flags);
}
done = true;
success = true;
break;
} }
done = true;
success = true;
} }
if (!done) { if (!done) {
@ -1790,24 +1786,22 @@ static void handle_token_history(int forward, int reset) {
tokenizer_t tok(data->token_history_buff.c_str(), TOK_ACCEPT_UNFINISHED); tokenizer_t tok(data->token_history_buff.c_str(), TOK_ACCEPT_UNFINISHED);
tok_t token; tok_t token;
while (tok.next(&token)) { while (tok.next(&token)) {
switch (token.type) { if (token.type == TOK_STRING) {
case TOK_STRING: { if (token.text.find(data->search_buff) != wcstring::npos) {
if (token.text.find(data->search_buff) != wcstring::npos) { // debug( 3, L"Found token at pos %d\n", tok_get_pos( &tok ) );
// debug( 3, L"Found token at pos %d\n", tok_get_pos( &tok ) ); if (token.offset >= current_pos) {
if (token.offset >= current_pos) { break;
break; }
} // debug( 3, L"ok pos" );
// debug( 3, L"ok pos" );
if (find(data->search_prev.begin(), data->search_prev.end(), token.text) ==
if (find(data->search_prev.begin(), data->search_prev.end(), data->search_prev.end()) {
token.text) == data->search_prev.end()) { data->token_history_pos = token.offset;
data->token_history_pos = token.offset; str = token.text;
str = token.text;
}
} }
break;
} }
default: { break; } } else {
break;
} }
} }
} }
@ -2873,36 +2867,30 @@ const wchar_t *reader_readline(int nchars) {
data->history_search.skip_matches(skip_list); data->history_search.skip_matches(skip_list);
} }
switch (data->search_mode) { if (data->search_mode == LINE_SEARCH) {
case LINE_SEARCH: { if ((c == R_HISTORY_SEARCH_BACKWARD) ||
if ((c == R_HISTORY_SEARCH_BACKWARD) || (c == R_HISTORY_TOKEN_SEARCH_BACKWARD)) {
(c == R_HISTORY_TOKEN_SEARCH_BACKWARD)) { data->history_search.go_backwards();
data->history_search.go_backwards(); } else {
} else { if (!data->history_search.go_forwards()) {
if (!data->history_search.go_forwards()) { // If you try to go forwards past the end, we just go to the end.
// If you try to go forwards past the end, we just go to the end. data->history_search.go_to_end();
data->history_search.go_to_end();
}
} }
wcstring new_text;
if (data->history_search.is_at_end()) {
new_text = data->search_buff;
} else {
new_text = data->history_search.current_string();
}
set_command_line_and_position(&data->command_line, new_text,
new_text.size());
break;
} }
case TOKEN_SEARCH: {
if ((c == R_HISTORY_SEARCH_BACKWARD) || wcstring new_text;
(c == R_HISTORY_TOKEN_SEARCH_BACKWARD)) { if (data->history_search.is_at_end()) {
handle_token_history(SEARCH_BACKWARD, reset); new_text = data->search_buff;
} else { } else {
handle_token_history(SEARCH_FORWARD, reset); new_text = data->history_search.current_string();
} }
break; set_command_line_and_position(&data->command_line, new_text, new_text.size());
} else if (data->search_mode == TOKEN_SEARCH) {
if ((c == R_HISTORY_SEARCH_BACKWARD) ||
(c == R_HISTORY_TOKEN_SEARCH_BACKWARD)) {
handle_token_history(SEARCH_BACKWARD, reset);
} else {
handle_token_history(SEARCH_FORWARD, reset);
} }
} }
break; break;

View file

@ -339,18 +339,10 @@ static wcstring file_get_desc(const wcstring &filename, int lstat_res, const str
return COMPLETE_SYMLINK_DESC; return COMPLETE_SYMLINK_DESC;
} }
switch (err) { if (err == ENOENT) return COMPLETE_ROTTEN_SYMLINK_DESC;
case ENOENT: { if (err == ELOOP) return COMPLETE_LOOP_SYMLINK_DESC;
return COMPLETE_ROTTEN_SYMLINK_DESC; // On unknown errors we do nothing. The file will be given the default 'File'
} // description or one based on the suffix.
case ELOOP: {
return COMPLETE_LOOP_SYMLINK_DESC;
}
default: {
// On unknown errors we do nothing. The file will be given the default 'File'
// description or one based on the suffix.
}
}
} else if (S_ISCHR(buf.st_mode)) { } else if (S_ISCHR(buf.st_mode)) {
return COMPLETE_CHAR_DESC; return COMPLETE_CHAR_DESC;
} else if (S_ISBLK(buf.st_mode)) { } else if (S_ISBLK(buf.st_mode)) {