Skip to content

Commit

Permalink
Userland/test: Handle '!' being used as a string
Browse files Browse the repository at this point in the history
e.g. `test ! = !`.
Fixes SerenityOS#6465.
  • Loading branch information
alimpfard authored and linusg committed Apr 19, 2021
1 parent 6e2f2cd commit 061aaa3
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions Userland/Utilities/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,15 +356,6 @@ static OwnPtr<Condition> parse_simple_expression(char* argv[])
fatal_error("Unmatched \033[1m(");
}

if (arg == "!") {
if (should_treat_expression_as_single_string(argv[optind]))
return make<StringCompare>(move(arg), "", StringCompare::NotEqual);
auto command = parse_complex_expression(argv);
if (!command)
fatal_error("Expected an expression after \033[1m!");
return make<Not>(command.release_nonnull());
}

// Try to read a unary op.
if (arg.starts_with('-') && arg.length() == 2) {
optind++;
Expand Down Expand Up @@ -464,6 +455,17 @@ static OwnPtr<Condition> parse_simple_expression(char* argv[])
--optind;
return make<StringCompare>("", lhs, StringCompare::NotEqual);
} else {
// Now that we know it's not a well-formed expression, see if it's actually a negation
if (lhs == "!") {
if (should_treat_expression_as_single_string(arg))
return make<StringCompare>(move(lhs), "", StringCompare::NotEqual);

auto command = parse_complex_expression(argv);
if (!command)
fatal_error("Expected an expression after \x1b[1m!");

return make<Not>(command.release_nonnull());
}
--optind;
return make<StringCompare>("", lhs, StringCompare::NotEqual);
}
Expand Down

0 comments on commit 061aaa3

Please sign in to comment.