From 72edd888f1f06bc23fa349054fa137e281fcc602 Mon Sep 17 00:00:00 2001 From: Fabian Boehm Date: Wed, 12 Jul 2023 17:59:43 +0200 Subject: [PATCH] Return a falsey status if the last `-c` command has a parse error This makes `fish -c begin` fail with a status of 127 - it already printed a syntax error so that was weird. (127 was the status for syntax errors when piping to fish, so we stay consistent with that) We allow multiple `-c` commands, and this will return the regular status if the last `-c` succeeded. This is fundamentally an extremely weird situation but this is the simple targeted fix - we did nothing, unsuccessfully, so we should fail. Things to consider in future: 1. Return something better than 127 - that's the status for "unknown command"! 2. Fail after a `-c` failed, potentially even checking all of them before executing the first? Fixes #9888 (cherry picked from commit a6c36a014c4a55d96c395ccfa418d6c761bd32da) --- src/fish.cpp | 6 +++++- tests/checks/basic.fish | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/fish.cpp b/src/fish.cpp index 56dba892e..1e2aa7dc5 100644 --- a/src/fish.cpp +++ b/src/fish.cpp @@ -258,6 +258,7 @@ static void read_init(parser_t &parser, const struct config_paths_t &paths) { static int run_command_list(parser_t &parser, const std::vector &cmds, const io_chain_t &io) { + int retval = STATUS_CMD_OK; for (const auto &cmd : cmds) { wcstring cmd_wcs = str2wcstring(cmd); // Parse into an ast and detect errors. @@ -273,14 +274,17 @@ static int run_command_list(parser_t &parser, const std::vector &cm parsed_source_ref_t ps = std::make_shared(std::move(cmd_wcs), std::move(ast)); parser.eval(ps, io); + retval = STATUS_CMD_OK; } else { wcstring sb; parser.get_backtrace(cmd_wcs, errors, sb); std::fwprintf(stderr, L"%ls", sb.c_str()); + // XXX: Why is this the return for "unknown command"? + retval = STATUS_CMD_UNKNOWN; } } - return 0; + return retval; } /// Parse the argument list, return the index of the first non-flag arguments. diff --git a/tests/checks/basic.fish b/tests/checks/basic.fish index 60a4e18a2..f85280e29 100644 --- a/tests/checks/basic.fish +++ b/tests/checks/basic.fish @@ -569,24 +569,46 @@ $fish -c 'echo \utest' # CHECKERR: echo \utest # CHECKERR: ^~~~~^ +echo $status +# CHECK: 127 + $fish -c 'echo \c' # CHECKERR: fish: Incomplete escape sequence '\c' # CHECKERR: echo \c # CHECKERR: ^^ +echo $status +# CHECK: 127 + $fish -c 'echo \C' # CHECK: C +echo $status +# CHECK: 0 $fish -c 'echo \U' # CHECKERR: fish: Incomplete escape sequence '\U' # CHECKERR: echo \U # CHECKERR: ^^ +echo $status +# CHECK: 127 + $fish -c 'echo \x' # CHECKERR: fish: Incomplete escape sequence '\x' # CHECKERR: echo \x # CHECKERR: ^^ +echo $status +# CHECK: 127 + +$fish -c begin +# CHECKERR: fish: Missing end to balance this begin +# CHECKERR: begin +# CHECKERR: ^~~~^ + +echo $status +# CHECK: 127 + printf '%s\n' "#!/bin/sh" 'echo $0' > $tmpdir/argv0.sh chmod +x $tmpdir/argv0.sh cd $tmpdir