2016-05-03 03:31:33 +08:00
|
|
|
// Provides the "linkage" between a parse_node_tree_t and actual execution structures (job_t, etc.)
|
|
|
|
//
|
|
|
|
// A note on error handling: fish has two kind of errors, fatal parse errors non-fatal runtime
|
|
|
|
// errors. A fatal error prevents execution of the entire file, while a non-fatal error skips that
|
|
|
|
// job.
|
|
|
|
//
|
|
|
|
// Non-fatal errors are printed as soon as they are encountered; otherwise you would have to wait
|
|
|
|
// for the execution to finish to see them.
|
2016-05-19 06:30:21 +08:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
#include <wctype.h>
|
2016-07-06 13:33:32 +08:00
|
|
|
|
|
|
|
#include <algorithm>
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <memory>
|
2016-05-03 03:31:33 +08:00
|
|
|
#include <string>
|
2017-02-11 10:47:02 +08:00
|
|
|
#include <type_traits>
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <vector>
|
2016-04-21 14:00:54 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
#include "builtin.h"
|
2017-06-16 08:57:37 +08:00
|
|
|
#include "builtin_function.h"
|
2016-05-03 03:31:33 +08:00
|
|
|
#include "common.h"
|
|
|
|
#include "complete.h"
|
2015-07-25 23:14:25 +08:00
|
|
|
#include "env.h"
|
|
|
|
#include "event.h"
|
2016-05-03 03:31:33 +08:00
|
|
|
#include "exec.h"
|
2013-12-25 05:17:24 +08:00
|
|
|
#include "expand.h"
|
2015-07-25 23:14:25 +08:00
|
|
|
#include "function.h"
|
2016-04-21 14:00:54 +08:00
|
|
|
#include "io.h"
|
2017-09-09 12:14:26 +08:00
|
|
|
#include "maybe.h"
|
2016-04-21 14:00:54 +08:00
|
|
|
#include "parse_constants.h"
|
2016-05-03 03:31:33 +08:00
|
|
|
#include "parse_execution.h"
|
|
|
|
#include "parse_util.h"
|
|
|
|
#include "parser.h"
|
|
|
|
#include "path.h"
|
2016-04-21 14:00:54 +08:00
|
|
|
#include "proc.h"
|
2016-05-03 03:31:33 +08:00
|
|
|
#include "reader.h"
|
2018-01-21 03:58:57 +08:00
|
|
|
#include "tnode.h"
|
2018-01-21 05:14:29 +08:00
|
|
|
#include "tokenizer.h"
|
2016-05-03 03:31:33 +08:00
|
|
|
#include "util.h"
|
|
|
|
#include "wildcard.h"
|
|
|
|
#include "wutil.h"
|
2013-12-25 05:17:24 +08:00
|
|
|
|
2018-01-14 17:17:57 +08:00
|
|
|
namespace g = grammar;
|
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
/// These are the specific statement types that support redirections.
|
2018-01-23 04:22:42 +08:00
|
|
|
static constexpr bool type_is_redirectable_block(parse_token_type_t type) {
|
|
|
|
return type == symbol_block_statement || type == symbol_if_statement ||
|
|
|
|
type == symbol_switch_statement;
|
|
|
|
}
|
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
static bool specific_statement_type_is_redirectable_block(const parse_node_t &node) {
|
2018-01-23 04:22:42 +08:00
|
|
|
return type_is_redirectable_block(node.type);
|
2014-01-08 02:45:36 +08:00
|
|
|
}
|
2013-12-25 05:17:24 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
/// Get the name of a redirectable block, for profiling purposes.
|
|
|
|
static wcstring profiling_cmd_name_for_redirectable_block(const parse_node_t &node,
|
|
|
|
const parse_node_tree_t &tree,
|
|
|
|
const wcstring &src) {
|
2014-02-10 06:04:43 +08:00
|
|
|
assert(specific_statement_type_is_redirectable_block(node));
|
|
|
|
assert(node.has_source());
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Get the source for the block, and cut it at the next statement terminator.
|
2014-02-10 06:04:43 +08:00
|
|
|
const size_t src_start = node.source_start;
|
|
|
|
size_t src_len = node.source_length;
|
|
|
|
|
2018-01-16 14:13:37 +08:00
|
|
|
auto term = tree.find_child<g::end_command>(node);
|
|
|
|
assert(term.has_source() && term.source_range()->start >= src_start);
|
|
|
|
src_len = term.source_range()->start - src_start;
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2014-02-10 06:04:43 +08:00
|
|
|
wcstring result = wcstring(src, src_start, src_len);
|
|
|
|
result.append(L"...");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-11-04 15:58:44 +08:00
|
|
|
parse_execution_context_t::parse_execution_context_t(parsed_source_ref_t pstree, parser_t *p,
|
|
|
|
std::shared_ptr<job_t> parent)
|
|
|
|
: pstree(std::move(pstree)), parser(p), parent_job(std::move(parent)) {}
|
2013-12-25 05:17:24 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Utilities
|
2013-12-25 05:17:24 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
wcstring parse_execution_context_t::get_source(const parse_node_t &node) const {
|
2017-12-23 06:40:15 +08:00
|
|
|
return node.get_source(pstree->src);
|
2013-12-25 05:17:24 +08:00
|
|
|
}
|
|
|
|
|
2018-01-14 17:26:28 +08:00
|
|
|
tnode_t<g::plain_statement> parse_execution_context_t::infinite_recursive_statement_in_job_list(
|
|
|
|
tnode_t<g::job_list> job_list, wcstring *out_func_name) const {
|
2016-05-03 03:31:33 +08:00
|
|
|
// This is a bit fragile. It is a test to see if we are inside of function call, but not inside
|
|
|
|
// a block in that function call. If, in the future, the rules for what block scopes are pushed
|
|
|
|
// on function invocation changes, then this check will break.
|
2014-01-02 07:29:56 +08:00
|
|
|
const block_t *current = parser->block_at_index(0), *parent = parser->block_at_index(1);
|
2016-05-03 03:31:33 +08:00
|
|
|
bool is_within_function_call =
|
|
|
|
(current && parent && current->type() == TOP && parent->type() == FUNCTION_CALL);
|
|
|
|
if (!is_within_function_call) {
|
2018-01-14 07:36:14 +08:00
|
|
|
return {};
|
2014-01-02 07:29:56 +08:00
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Check to see which function call is forbidden.
|
|
|
|
if (parser->forbidden_function.empty()) {
|
2018-01-14 07:36:14 +08:00
|
|
|
return {};
|
2014-01-02 07:29:56 +08:00
|
|
|
}
|
|
|
|
const wcstring &forbidden_function_name = parser->forbidden_function.back();
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Get the first job in the job list.
|
2018-03-03 10:09:16 +08:00
|
|
|
tnode_t<g::job> first_job = job_list.try_get_child<g::job_conjunction, 1>().child<0>();
|
2018-01-14 17:26:28 +08:00
|
|
|
if (!first_job) {
|
2018-01-14 07:36:14 +08:00
|
|
|
return {};
|
2014-01-02 07:29:56 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Here's the statement node we find that's infinite recursive.
|
2018-01-14 07:36:14 +08:00
|
|
|
tnode_t<grammar::plain_statement> infinite_recursive_statement;
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2018-01-16 08:33:36 +08:00
|
|
|
// Get the list of plain statements.
|
|
|
|
// Ignore statements with decorations like 'builtin' or 'command', since those
|
|
|
|
// are not infinite recursion. In particular that is what enables 'wrapper functions'.
|
|
|
|
tnode_t<g::statement> statement = first_job.child<0>();
|
|
|
|
tnode_t<g::job_continuation> continuation = first_job.child<1>();
|
2018-09-11 13:29:52 +08:00
|
|
|
const null_environment_t nullenv{};
|
2018-01-16 08:33:36 +08:00
|
|
|
while (statement) {
|
|
|
|
tnode_t<g::plain_statement> plain_statement =
|
|
|
|
statement.try_get_child<g::decorated_statement, 0>()
|
|
|
|
.try_get_child<g::plain_statement, 0>();
|
|
|
|
if (plain_statement) {
|
|
|
|
maybe_t<wcstring> cmd = command_for_plain_statement(plain_statement, pstree->src);
|
2018-09-11 13:29:52 +08:00
|
|
|
if (cmd && expand_one(*cmd, EXPAND_SKIP_CMDSUBST | EXPAND_SKIP_VARIABLES, nullenv) &&
|
2018-01-16 08:33:36 +08:00
|
|
|
cmd == forbidden_function_name) {
|
|
|
|
// This is it.
|
|
|
|
infinite_recursive_statement = plain_statement;
|
|
|
|
if (out_func_name != NULL) {
|
|
|
|
*out_func_name = forbidden_function_name;
|
|
|
|
}
|
|
|
|
break;
|
2014-01-02 07:29:56 +08:00
|
|
|
}
|
|
|
|
}
|
2018-01-16 08:33:36 +08:00
|
|
|
statement = continuation.next_in_list<g::statement>();
|
2014-01-02 07:29:56 +08:00
|
|
|
}
|
2018-01-16 08:33:36 +08:00
|
|
|
|
2014-01-02 07:29:56 +08:00
|
|
|
return infinite_recursive_statement;
|
|
|
|
}
|
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
enum process_type_t parse_execution_context_t::process_type_for_command(
|
2018-01-16 07:15:45 +08:00
|
|
|
tnode_t<grammar::plain_statement> statement, const wcstring &cmd) const {
|
2014-01-01 06:37:37 +08:00
|
|
|
enum process_type_t process_type = EXTERNAL;
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Determine the process type, which depends on the statement decoration (command, builtin,
|
|
|
|
// etc).
|
2018-01-16 07:15:45 +08:00
|
|
|
enum parse_statement_decoration_t decoration = get_decoration(statement);
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
if (decoration == parse_statement_decoration_exec) {
|
|
|
|
// Always exec.
|
2014-01-01 06:37:37 +08:00
|
|
|
process_type = INTERNAL_EXEC;
|
2016-05-03 03:31:33 +08:00
|
|
|
} else if (decoration == parse_statement_decoration_command) {
|
|
|
|
// Always a command.
|
2014-01-01 06:37:37 +08:00
|
|
|
process_type = EXTERNAL;
|
2016-05-03 03:31:33 +08:00
|
|
|
} else if (decoration == parse_statement_decoration_builtin) {
|
|
|
|
// What happens if this builtin is not valid?
|
2014-01-01 06:37:37 +08:00
|
|
|
process_type = INTERNAL_BUILTIN;
|
2016-05-03 03:31:33 +08:00
|
|
|
} else if (function_exists(cmd)) {
|
2014-01-01 06:37:37 +08:00
|
|
|
process_type = INTERNAL_FUNCTION;
|
2016-05-03 03:31:33 +08:00
|
|
|
} else if (builtin_exists(cmd)) {
|
2014-01-01 06:37:37 +08:00
|
|
|
process_type = INTERNAL_BUILTIN;
|
2016-05-03 03:31:33 +08:00
|
|
|
} else {
|
2014-01-01 06:37:37 +08:00
|
|
|
process_type = EXTERNAL;
|
|
|
|
}
|
|
|
|
return process_type;
|
|
|
|
}
|
2013-12-25 05:17:24 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
bool parse_execution_context_t::should_cancel_execution(const block_t *block) const {
|
2013-12-30 08:23:26 +08:00
|
|
|
return cancellation_reason(block) != execution_cancellation_none;
|
|
|
|
}
|
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
parse_execution_context_t::execution_cancellation_reason_t
|
|
|
|
parse_execution_context_t::cancellation_reason(const block_t *block) const {
|
|
|
|
if (shell_is_exiting()) {
|
2013-12-30 08:23:26 +08:00
|
|
|
return execution_cancellation_exit;
|
2016-05-05 06:19:47 +08:00
|
|
|
}
|
|
|
|
if (parser && parser->cancellation_requested) {
|
2014-01-03 04:37:50 +08:00
|
|
|
return execution_cancellation_skip;
|
2016-05-05 06:19:47 +08:00
|
|
|
}
|
|
|
|
if (block && block->skip) {
|
2013-12-30 08:23:26 +08:00
|
|
|
return execution_cancellation_skip;
|
|
|
|
}
|
2017-01-22 06:15:03 +08:00
|
|
|
if (block && block->loop_status != LOOP_NORMAL) {
|
|
|
|
return execution_cancellation_loop_control;
|
|
|
|
}
|
2016-05-05 06:19:47 +08:00
|
|
|
return execution_cancellation_none;
|
2013-12-25 05:17:24 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
/// Return whether the job contains a single statement, of block type, with no redirections.
|
2018-01-16 08:00:19 +08:00
|
|
|
bool parse_execution_context_t::job_is_simple_block(tnode_t<g::job> job_node) const {
|
|
|
|
tnode_t<g::statement> statement = job_node.child<0>();
|
2014-01-08 02:45:36 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Must be no pipes.
|
2018-01-16 08:00:19 +08:00
|
|
|
if (job_node.child<1>().try_get_child<g::tok_pipe, 0>()) {
|
2014-01-08 02:45:36 +08:00
|
|
|
return false;
|
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2018-01-23 04:22:42 +08:00
|
|
|
// Helper to check if an argument or redirection list has no redirections.
|
|
|
|
auto is_empty = [](tnode_t<g::arguments_or_redirections_list> lst) -> bool {
|
2018-01-23 05:18:34 +08:00
|
|
|
return !lst.next_in_list<g::redirection>();
|
2018-01-23 04:22:42 +08:00
|
|
|
};
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2018-01-23 04:22:42 +08:00
|
|
|
// Check if we're a block statement with redirections. We do it this obnoxious way to preserve
|
|
|
|
// type safety (in case we add more specific statement types).
|
|
|
|
const parse_node_t &specific_statement = statement.get_child_node<0>();
|
|
|
|
switch (specific_statement.type) {
|
|
|
|
case symbol_block_statement:
|
|
|
|
return is_empty(statement.require_get_child<g::block_statement, 0>().child<3>());
|
|
|
|
case symbol_switch_statement:
|
|
|
|
return is_empty(statement.require_get_child<g::switch_statement, 0>().child<5>());
|
|
|
|
case symbol_if_statement:
|
|
|
|
return is_empty(statement.require_get_child<g::if_statement, 0>().child<3>());
|
2018-03-03 10:09:16 +08:00
|
|
|
case symbol_not_statement:
|
2018-01-23 04:22:42 +08:00
|
|
|
case symbol_decorated_statement:
|
|
|
|
// not block statements
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
assert(0 && "Unexpected child block type");
|
|
|
|
return false;
|
|
|
|
}
|
2014-01-08 02:45:36 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
parse_execution_result_t parse_execution_context_t::run_if_statement(
|
2018-04-01 05:57:24 +08:00
|
|
|
tnode_t<g::if_statement> statement, const block_t *associated_block) {
|
2014-01-01 06:37:37 +08:00
|
|
|
parse_execution_result_t result = parse_execution_success;
|
2013-12-27 17:38:43 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// We have a sequence of if clauses, with a final else, resulting in a single job list that we
|
|
|
|
// execute.
|
2018-01-14 18:02:02 +08:00
|
|
|
tnode_t<g::job_list> job_list_to_execute;
|
|
|
|
tnode_t<g::if_clause> if_clause = statement.child<0>();
|
|
|
|
tnode_t<g::else_clause> else_clause = statement.child<1>();
|
2016-05-03 03:31:33 +08:00
|
|
|
for (;;) {
|
2018-04-01 05:57:24 +08:00
|
|
|
if (should_cancel_execution(associated_block)) {
|
2014-01-01 06:37:37 +08:00
|
|
|
result = parse_execution_cancelled;
|
|
|
|
break;
|
|
|
|
}
|
2016-05-03 03:31:33 +08:00
|
|
|
|
|
|
|
// An if condition has a job and a "tail" of andor jobs, e.g. "foo ; and bar; or baz".
|
2018-03-03 02:23:57 +08:00
|
|
|
tnode_t<g::job_conjunction> condition_head = if_clause.child<1>();
|
2018-01-14 18:02:02 +08:00
|
|
|
tnode_t<g::andor_job_list> condition_boolean_tail = if_clause.child<3>();
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Check the condition and the tail. We treat parse_execution_errored here as failure, in
|
|
|
|
// accordance with historic behavior.
|
2018-04-01 05:57:24 +08:00
|
|
|
parse_execution_result_t cond_ret = run_job_conjunction(condition_head, associated_block);
|
2016-05-03 03:31:33 +08:00
|
|
|
if (cond_ret == parse_execution_success) {
|
2018-04-01 05:57:24 +08:00
|
|
|
cond_ret = run_job_list(condition_boolean_tail, associated_block);
|
2015-12-20 06:45:45 +08:00
|
|
|
}
|
2016-05-03 03:31:33 +08:00
|
|
|
const bool take_branch =
|
|
|
|
(cond_ret == parse_execution_success) && proc_get_last_status() == EXIT_SUCCESS;
|
2014-01-01 06:37:37 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
if (take_branch) {
|
|
|
|
// Condition succeeded.
|
2018-01-14 18:02:02 +08:00
|
|
|
job_list_to_execute = if_clause.child<4>();
|
2013-12-27 17:38:43 +08:00
|
|
|
break;
|
2018-01-14 18:02:02 +08:00
|
|
|
}
|
|
|
|
auto else_cont = else_clause.try_get_child<g::else_continuation, 1>();
|
|
|
|
if (!else_cont) {
|
2016-05-03 03:31:33 +08:00
|
|
|
// 'if' condition failed, no else clause, return 0, we're done.
|
2017-05-04 15:18:02 +08:00
|
|
|
proc_set_last_status(STATUS_CMD_OK);
|
2013-12-27 17:38:43 +08:00
|
|
|
break;
|
2016-05-03 03:31:33 +08:00
|
|
|
} else {
|
|
|
|
// We have an 'else continuation' (either else-if or else).
|
2018-01-14 18:02:02 +08:00
|
|
|
if (auto maybe_if_clause = else_cont.try_get_child<g::if_clause, 0>()) {
|
2016-05-03 03:31:33 +08:00
|
|
|
// it's an 'else if', go to the next one.
|
2015-12-16 06:59:03 +08:00
|
|
|
if_clause = maybe_if_clause;
|
2018-01-14 18:02:02 +08:00
|
|
|
else_clause = else_cont.try_get_child<g::else_clause, 1>();
|
|
|
|
assert(else_clause && "Expected to have an else clause");
|
2016-05-03 03:31:33 +08:00
|
|
|
} else {
|
|
|
|
// It's the final 'else', we're done.
|
2018-01-14 18:02:02 +08:00
|
|
|
job_list_to_execute = else_cont.try_get_child<g::job_list, 1>();
|
|
|
|
assert(job_list_to_execute && "Should have a job list");
|
2013-12-27 17:38:43 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Execute any job list we got.
|
2018-01-16 08:08:06 +08:00
|
|
|
if (job_list_to_execute) {
|
2018-04-01 05:57:24 +08:00
|
|
|
if_block_t *ib = parser->push_block<if_block_t>();
|
2018-01-16 08:08:06 +08:00
|
|
|
run_job_list(job_list_to_execute, ib);
|
2018-04-01 05:57:24 +08:00
|
|
|
if (should_cancel_execution(ib)) {
|
|
|
|
result = parse_execution_cancelled;
|
|
|
|
}
|
|
|
|
parser->pop_block(ib);
|
2016-05-03 03:31:33 +08:00
|
|
|
} else {
|
|
|
|
// No job list means no sucessful conditions, so return 0 (issue #1443).
|
2017-05-04 15:18:02 +08:00
|
|
|
proc_set_last_status(STATUS_CMD_OK);
|
2015-01-21 15:44:49 +08:00
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// It's possible there's a last-minute cancellation (issue #1297).
|
2018-04-01 05:57:24 +08:00
|
|
|
if (should_cancel_execution(associated_block)) {
|
2014-02-12 17:39:06 +08:00
|
|
|
result = parse_execution_cancelled;
|
|
|
|
}
|
2013-12-27 17:38:43 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Otherwise, take the exit status of the job list. Reversal of issue #1061.
|
2014-01-01 06:37:37 +08:00
|
|
|
return result;
|
2013-12-27 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2018-01-23 04:22:42 +08:00
|
|
|
parse_execution_result_t parse_execution_context_t::run_begin_statement(
|
|
|
|
tnode_t<g::job_list> contents) {
|
2017-01-22 07:35:35 +08:00
|
|
|
// Basic begin/end block. Push a scope block, run jobs, pop it
|
|
|
|
scope_block_t *sb = parser->push_block<scope_block_t>(BEGIN);
|
2014-01-01 06:37:37 +08:00
|
|
|
parse_execution_result_t ret = run_job_list(contents, sb);
|
2013-12-27 17:38:43 +08:00
|
|
|
parser->pop_block(sb);
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2014-01-01 06:37:37 +08:00
|
|
|
return ret;
|
2014-01-15 17:40:40 +08:00
|
|
|
}
|
2013-12-27 17:38:43 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Define a function.
|
|
|
|
parse_execution_result_t parse_execution_context_t::run_function_statement(
|
2018-02-10 13:53:06 +08:00
|
|
|
tnode_t<g::function_header> header, tnode_t<g::job_list> body) {
|
2016-05-03 03:31:33 +08:00
|
|
|
// Get arguments.
|
2018-01-16 07:15:45 +08:00
|
|
|
wcstring_list_t arguments;
|
|
|
|
argument_node_list_t arg_nodes = header.descendants<g::argument>();
|
|
|
|
parse_execution_result_t result =
|
|
|
|
this->expand_arguments_from_nodes(arg_nodes, &arguments, failglob);
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-10-31 11:26:10 +08:00
|
|
|
if (result != parse_execution_success) {
|
|
|
|
return result;
|
|
|
|
}
|
2017-07-27 11:17:04 +08:00
|
|
|
io_streams_t streams(0); // no limit on the amount of output from builtin_function()
|
2018-02-10 13:53:06 +08:00
|
|
|
int err = builtin_function(*parser, streams, arguments, pstree, body);
|
2016-10-31 11:26:10 +08:00
|
|
|
proc_set_last_status(err);
|
|
|
|
|
2017-06-18 13:36:56 +08:00
|
|
|
if (!streams.err.empty()) {
|
2018-05-30 12:11:34 +08:00
|
|
|
this->report_error(header, L"%ls", streams.err.contents().c_str());
|
2016-10-31 11:26:10 +08:00
|
|
|
result = parse_execution_errored;
|
2013-12-27 17:38:43 +08:00
|
|
|
}
|
2016-10-31 11:26:10 +08:00
|
|
|
|
2014-01-01 06:37:37 +08:00
|
|
|
return result;
|
2013-12-27 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
parse_execution_result_t parse_execution_context_t::run_block_statement(
|
2018-04-01 05:57:24 +08:00
|
|
|
tnode_t<g::block_statement> statement, const block_t *associated_block) {
|
2018-01-14 17:42:58 +08:00
|
|
|
tnode_t<g::block_header> bheader = statement.child<0>();
|
|
|
|
tnode_t<g::job_list> contents = statement.child<1>();
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2014-01-01 06:37:37 +08:00
|
|
|
parse_execution_result_t ret = parse_execution_success;
|
2018-01-14 17:42:58 +08:00
|
|
|
if (auto header = bheader.try_get_child<g::for_header, 0>()) {
|
|
|
|
ret = run_for_statement(header, contents);
|
|
|
|
} else if (auto header = bheader.try_get_child<g::while_header, 0>()) {
|
2018-04-01 05:57:24 +08:00
|
|
|
ret = run_while_statement(header, contents, associated_block);
|
2018-01-14 17:42:58 +08:00
|
|
|
} else if (auto header = bheader.try_get_child<g::function_header, 0>()) {
|
2018-02-10 13:53:06 +08:00
|
|
|
ret = run_function_statement(header, contents);
|
2018-01-14 17:42:58 +08:00
|
|
|
} else if (auto header = bheader.try_get_child<g::begin_header, 0>()) {
|
2018-01-21 06:05:34 +08:00
|
|
|
ret = run_begin_statement(contents);
|
2018-01-14 17:42:58 +08:00
|
|
|
} else {
|
|
|
|
debug(0, L"Unexpected block header: %ls\n", bheader.node()->describe().c_str());
|
|
|
|
PARSER_DIE();
|
2013-12-27 17:38:43 +08:00
|
|
|
}
|
2014-01-01 06:37:37 +08:00
|
|
|
return ret;
|
2013-12-27 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2017-09-09 12:14:26 +08:00
|
|
|
/// Return true if the current execution context is within a function block, else false.
|
|
|
|
bool parse_execution_context_t::is_function_context() const {
|
|
|
|
const block_t *current = parser->block_at_index(0);
|
|
|
|
const block_t *parent = parser->block_at_index(1);
|
|
|
|
bool is_within_function_call =
|
|
|
|
(current && parent && current->type() == TOP && parent->type() == FUNCTION_CALL);
|
|
|
|
return is_within_function_call;
|
|
|
|
}
|
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
parse_execution_result_t parse_execution_context_t::run_for_statement(
|
2018-01-14 17:47:09 +08:00
|
|
|
tnode_t<grammar::for_header> header, tnode_t<grammar::job_list> block_contents) {
|
2016-05-03 03:31:33 +08:00
|
|
|
// Get the variable name: `for var_name in ...`. We expand the variable name. It better result
|
|
|
|
// in just one.
|
2018-01-14 17:47:09 +08:00
|
|
|
tnode_t<g::tok_string> var_name_node = header.child<1>();
|
2014-02-22 10:20:51 +08:00
|
|
|
wcstring for_var_name = get_source(var_name_node);
|
2018-09-11 13:29:52 +08:00
|
|
|
if (!expand_one(for_var_name, 0, parser->vars())) {
|
2014-02-22 10:20:51 +08:00
|
|
|
report_error(var_name_node, FAILED_EXPANSION_VARIABLE_NAME_ERR_MSG, for_var_name.c_str());
|
|
|
|
return parse_execution_errored;
|
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Get the contents to iterate over.
|
2018-01-16 07:15:45 +08:00
|
|
|
wcstring_list_t arguments;
|
|
|
|
parse_execution_result_t ret = this->expand_arguments_from_nodes(
|
|
|
|
get_argument_nodes(header.child<3>()), &arguments, nullglob);
|
2016-05-03 03:31:33 +08:00
|
|
|
if (ret != parse_execution_success) {
|
2014-06-01 03:41:27 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2018-09-25 10:26:46 +08:00
|
|
|
auto &vars = parser->vars();
|
|
|
|
auto var = vars.get(for_var_name, ENV_LOCAL);
|
|
|
|
if (!var && !is_function_context()) var = vars.get(for_var_name, ENV_DEFAULT);
|
2017-09-09 12:14:26 +08:00
|
|
|
if (!var || var->read_only()) {
|
2018-09-11 12:27:25 +08:00
|
|
|
int retval = parser->vars().set_empty(for_var_name, ENV_LOCAL | ENV_USER);
|
2017-09-09 12:14:26 +08:00
|
|
|
if (retval != ENV_OK) {
|
|
|
|
report_error(var_name_node, L"You cannot use read-only variable '%ls' in a for loop",
|
|
|
|
for_var_name.c_str());
|
|
|
|
return parse_execution_errored;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-22 07:35:35 +08:00
|
|
|
for_block_t *fb = parser->push_block<for_block_t>();
|
2013-12-27 17:38:43 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Now drive the for loop.
|
2018-01-16 07:15:45 +08:00
|
|
|
for (const wcstring &val : arguments) {
|
2016-05-03 03:31:33 +08:00
|
|
|
if (should_cancel_execution(fb)) {
|
2014-01-01 06:37:37 +08:00
|
|
|
ret = parse_execution_cancelled;
|
2014-01-01 16:04:02 +08:00
|
|
|
break;
|
2014-01-01 06:37:37 +08:00
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2018-09-14 15:36:26 +08:00
|
|
|
int retval = parser->vars().set_one(for_var_name, ENV_DEFAULT | ENV_USER, val);
|
2017-12-22 05:53:18 +08:00
|
|
|
assert(retval == ENV_OK && "for loop variable should have been successfully set");
|
|
|
|
(void)retval;
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2017-08-21 03:02:45 +08:00
|
|
|
fb->loop_status = LOOP_NORMAL;
|
2013-12-29 08:18:38 +08:00
|
|
|
this->run_job_list(block_contents, fb);
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
if (this->cancellation_reason(fb) == execution_cancellation_loop_control) {
|
|
|
|
// Handle break or continue.
|
|
|
|
if (fb->loop_status == LOOP_CONTINUE) {
|
|
|
|
// Reset the loop state.
|
2013-12-30 08:23:26 +08:00
|
|
|
fb->loop_status = LOOP_NORMAL;
|
|
|
|
continue;
|
2016-05-03 03:31:33 +08:00
|
|
|
} else if (fb->loop_status == LOOP_BREAK) {
|
2013-12-30 08:23:26 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-12-29 14:52:06 +08:00
|
|
|
}
|
2013-12-27 17:38:43 +08:00
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2014-01-08 02:45:36 +08:00
|
|
|
parser->pop_block(fb);
|
2014-01-01 06:37:37 +08:00
|
|
|
return ret;
|
2013-12-27 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
parse_execution_result_t parse_execution_context_t::run_switch_statement(
|
2018-01-14 18:30:18 +08:00
|
|
|
tnode_t<grammar::switch_statement> statement) {
|
2014-01-01 06:37:37 +08:00
|
|
|
parse_execution_result_t result = parse_execution_success;
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Get the switch variable.
|
2018-01-14 18:30:18 +08:00
|
|
|
tnode_t<grammar::argument> switch_value_n = statement.child<1>();
|
|
|
|
const wcstring switch_value = get_source(switch_value_n);
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Expand it. We need to offset any errors by the position of the string.
|
2013-12-27 19:58:42 +08:00
|
|
|
std::vector<completion_t> switch_values_expanded;
|
2014-03-22 08:13:33 +08:00
|
|
|
parse_error_list_t errors;
|
2018-09-11 13:29:52 +08:00
|
|
|
int expand_ret = expand_string(switch_value, &switch_values_expanded, EXPAND_NO_DESCRIPTIONS,
|
|
|
|
parser->vars(), &errors);
|
2018-01-14 18:30:18 +08:00
|
|
|
parse_error_offset_source_start(&errors, switch_value_n.source_range()->start);
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
switch (expand_ret) {
|
|
|
|
case EXPAND_ERROR: {
|
2014-03-22 08:13:33 +08:00
|
|
|
result = report_errors(errors);
|
2013-12-27 19:58:42 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-05-03 03:31:33 +08:00
|
|
|
case EXPAND_WILDCARD_NO_MATCH: {
|
2018-01-14 18:30:18 +08:00
|
|
|
result = report_unmatched_wildcard_error(switch_value_n);
|
2013-12-27 19:58:42 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case EXPAND_WILDCARD_MATCH:
|
2016-05-03 03:31:33 +08:00
|
|
|
case EXPAND_OK: {
|
2013-12-27 19:58:42 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-10-30 08:25:48 +08:00
|
|
|
default: {
|
|
|
|
DIE("unexpected expand_string() return value");
|
|
|
|
break;
|
|
|
|
}
|
2013-12-27 19:58:42 +08:00
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
if (result == parse_execution_success && switch_values_expanded.size() != 1) {
|
|
|
|
result =
|
2018-01-14 18:30:18 +08:00
|
|
|
report_error(switch_value_n, _(L"switch: Expected exactly one argument, got %lu\n"),
|
2016-05-03 03:31:33 +08:00
|
|
|
switch_values_expanded.size());
|
2013-12-27 19:58:42 +08:00
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-10-31 11:26:10 +08:00
|
|
|
if (result != parse_execution_success) {
|
|
|
|
return result;
|
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-10-31 11:26:10 +08:00
|
|
|
const wcstring &switch_value_expanded = switch_values_expanded.at(0).completion;
|
2014-03-29 07:56:44 +08:00
|
|
|
|
2017-01-22 07:35:35 +08:00
|
|
|
switch_block_t *sb = parser->push_block<switch_block_t>();
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-10-31 11:26:10 +08:00
|
|
|
// Expand case statements.
|
2018-01-14 18:30:18 +08:00
|
|
|
tnode_t<g::case_item_list> case_item_list = statement.child<3>();
|
|
|
|
tnode_t<g::case_item> matching_case_item{};
|
|
|
|
while (auto case_item = case_item_list.next_in_list<g::case_item>()) {
|
2016-10-31 11:26:10 +08:00
|
|
|
if (should_cancel_execution(sb)) {
|
|
|
|
result = parse_execution_cancelled;
|
|
|
|
break;
|
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-10-31 11:26:10 +08:00
|
|
|
// Expand arguments. A case item list may have a wildcard that fails to expand to
|
|
|
|
// anything. We also report case errors, but don't stop execution; i.e. a case item that
|
|
|
|
// contains an unexpandable process will report and then fail to match.
|
2018-01-16 07:15:45 +08:00
|
|
|
auto arg_nodes = get_argument_nodes(case_item.child<1>());
|
2016-10-31 11:26:10 +08:00
|
|
|
wcstring_list_t case_args;
|
|
|
|
parse_execution_result_t case_result =
|
2018-01-16 07:15:45 +08:00
|
|
|
this->expand_arguments_from_nodes(arg_nodes, &case_args, failglob);
|
2016-10-31 11:26:10 +08:00
|
|
|
if (case_result == parse_execution_success) {
|
2018-01-14 18:30:18 +08:00
|
|
|
for (const wcstring &arg : case_args) {
|
2016-10-31 11:26:10 +08:00
|
|
|
// Unescape wildcards so they can be expanded again.
|
|
|
|
wcstring unescaped_arg = parse_util_unescape_wildcards(arg);
|
|
|
|
bool match = wildcard_match(switch_value_expanded, unescaped_arg);
|
|
|
|
|
|
|
|
// If this matched, we're done.
|
|
|
|
if (match) {
|
|
|
|
matching_case_item = case_item;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-03-29 07:56:44 +08:00
|
|
|
}
|
2018-01-14 18:30:18 +08:00
|
|
|
if (matching_case_item) break;
|
2016-10-31 11:26:10 +08:00
|
|
|
}
|
2013-12-27 19:58:42 +08:00
|
|
|
|
2018-01-14 18:30:18 +08:00
|
|
|
if (matching_case_item) {
|
2016-10-31 11:26:10 +08:00
|
|
|
// Success, evaluate the job list.
|
2018-01-14 18:30:18 +08:00
|
|
|
assert(result == parse_execution_success && "Expected success");
|
|
|
|
auto job_list = matching_case_item.child<3>();
|
|
|
|
result = this->run_job_list(job_list, sb);
|
2014-03-29 07:56:44 +08:00
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-10-31 11:26:10 +08:00
|
|
|
parser->pop_block(sb);
|
2014-01-01 06:37:37 +08:00
|
|
|
return result;
|
2013-12-27 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
parse_execution_result_t parse_execution_context_t::run_while_statement(
|
2018-04-01 05:57:24 +08:00
|
|
|
tnode_t<grammar::while_header> header, tnode_t<grammar::job_list> contents,
|
|
|
|
const block_t *associated_block) {
|
2014-01-01 06:37:37 +08:00
|
|
|
parse_execution_result_t ret = parse_execution_success;
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// The conditions of the while loop.
|
2018-03-03 02:23:57 +08:00
|
|
|
tnode_t<g::job_conjunction> condition_head = header.child<1>();
|
2018-01-14 18:41:37 +08:00
|
|
|
tnode_t<g::andor_job_list> condition_boolean_tail = header.child<3>();
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Run while the condition is true.
|
2018-09-27 21:13:55 +08:00
|
|
|
bool loop_executed = false;
|
2016-05-03 03:31:33 +08:00
|
|
|
for (;;) {
|
|
|
|
// Check the condition.
|
2018-04-01 05:57:24 +08:00
|
|
|
parse_execution_result_t cond_ret =
|
|
|
|
this->run_job_conjunction(condition_head, associated_block);
|
2016-05-03 03:31:33 +08:00
|
|
|
if (cond_ret == parse_execution_success) {
|
2018-04-01 05:57:24 +08:00
|
|
|
cond_ret = run_job_list(condition_boolean_tail, associated_block);
|
2015-12-20 06:45:45 +08:00
|
|
|
}
|
2016-05-03 03:31:33 +08:00
|
|
|
|
|
|
|
// We only continue on successful execution and EXIT_SUCCESS.
|
|
|
|
if (cond_ret != parse_execution_success || proc_get_last_status() != EXIT_SUCCESS) {
|
2014-01-01 06:37:37 +08:00
|
|
|
break;
|
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Check cancellation.
|
2018-04-01 05:57:24 +08:00
|
|
|
if (this->should_cancel_execution(associated_block)) {
|
2014-01-01 06:37:37 +08:00
|
|
|
ret = parse_execution_cancelled;
|
|
|
|
break;
|
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2018-09-27 21:13:55 +08:00
|
|
|
loop_executed = true;
|
|
|
|
|
2018-04-01 05:57:24 +08:00
|
|
|
// Push a while block and then check its cancellation reason.
|
|
|
|
while_block_t *wb = parser->push_block<while_block_t>();
|
2018-01-14 18:41:37 +08:00
|
|
|
this->run_job_list(contents, wb);
|
2018-04-01 05:57:24 +08:00
|
|
|
auto loop_status = wb->loop_status;
|
|
|
|
auto cancel_reason = this->cancellation_reason(wb);
|
|
|
|
parser->pop_block(wb);
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2018-04-01 05:57:24 +08:00
|
|
|
if (cancel_reason == execution_cancellation_loop_control) {
|
2016-05-03 03:31:33 +08:00
|
|
|
// Handle break or continue.
|
2018-04-01 05:57:24 +08:00
|
|
|
if (loop_status == LOOP_CONTINUE) {
|
2013-12-30 08:23:26 +08:00
|
|
|
continue;
|
2018-04-01 05:57:24 +08:00
|
|
|
} else if (loop_status == LOOP_BREAK) {
|
2013-12-30 08:23:26 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-12-29 14:52:06 +08:00
|
|
|
}
|
2016-05-03 03:31:33 +08:00
|
|
|
|
|
|
|
// no_exec means that fish was invoked with -n or --no-execute. If set, we allow the loop to
|
|
|
|
// not-execute once so its contents can be checked, and then break.
|
|
|
|
if (no_exec) {
|
2014-07-12 02:28:10 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-12-27 04:24:00 +08:00
|
|
|
}
|
2018-09-27 21:13:55 +08:00
|
|
|
|
|
|
|
if (loop_executed) {
|
|
|
|
proc_set_last_status(STATUS_CMD_OK);
|
|
|
|
}
|
|
|
|
|
2014-01-01 06:37:37 +08:00
|
|
|
return ret;
|
2013-12-25 05:17:24 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Reports an error. Always returns parse_execution_errored, so you can assign the result to an
|
|
|
|
// 'errored' variable.
|
|
|
|
parse_execution_result_t parse_execution_context_t::report_error(const parse_node_t &node,
|
|
|
|
const wchar_t *fmt, ...) const {
|
|
|
|
// Create an error.
|
2016-02-28 10:37:59 +08:00
|
|
|
parse_error_list_t error_list = parse_error_list_t(1);
|
|
|
|
parse_error_t *error = &error_list.at(0);
|
|
|
|
error->source_start = node.source_start;
|
|
|
|
error->source_length = node.source_length;
|
2016-05-03 03:31:33 +08:00
|
|
|
error->code = parse_error_syntax; // hackish
|
2016-02-28 10:37:59 +08:00
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start(va, fmt);
|
|
|
|
error->text = vformat_string(fmt, va);
|
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
this->report_errors(error_list);
|
2014-03-22 08:13:33 +08:00
|
|
|
return parse_execution_errored;
|
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
parse_execution_result_t parse_execution_context_t::report_errors(
|
|
|
|
const parse_error_list_t &error_list) const {
|
|
|
|
if (!parser->cancellation_requested) {
|
|
|
|
if (error_list.empty()) {
|
2017-01-03 13:11:53 +08:00
|
|
|
debug(0, "Error reported but no error text found.");
|
2014-03-22 08:13:33 +08:00
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Get a backtrace.
|
2014-01-09 10:20:55 +08:00
|
|
|
wcstring backtrace_and_desc;
|
2017-12-23 06:40:15 +08:00
|
|
|
parser->get_backtrace(pstree->src, error_list, backtrace_and_desc);
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Print it.
|
2017-01-03 13:11:53 +08:00
|
|
|
if (!should_suppress_stderr_for_tests()) {
|
|
|
|
fwprintf(stderr, L"%ls", backtrace_and_desc.c_str());
|
|
|
|
}
|
2014-01-09 10:20:55 +08:00
|
|
|
}
|
2014-01-01 06:37:37 +08:00
|
|
|
return parse_execution_errored;
|
2013-12-25 05:17:24 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
/// Reports an unmatched wildcard error and returns parse_execution_errored.
|
|
|
|
parse_execution_result_t parse_execution_context_t::report_unmatched_wildcard_error(
|
2018-08-26 16:41:45 +08:00
|
|
|
const parse_node_t &unmatched_wildcard) const {
|
2013-12-27 17:38:43 +08:00
|
|
|
proc_set_last_status(STATUS_UNMATCHED_WILDCARD);
|
2016-02-09 02:49:26 +08:00
|
|
|
report_error(unmatched_wildcard, WILDCARD_ERR_MSG, get_source(unmatched_wildcard).c_str());
|
2014-06-01 03:41:27 +08:00
|
|
|
return parse_execution_errored;
|
2013-12-27 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2016-07-06 13:33:32 +08:00
|
|
|
// Given a command string that might contain fish special tokens return a string without those
|
|
|
|
// tokens.
|
2017-03-29 10:15:08 +08:00
|
|
|
//
|
|
|
|
// TODO(krader1961): Figure out what VARIABLE_EXPAND means in this context. After looking at the
|
|
|
|
// code and doing various tests I couldn't figure out why that token would be present when this
|
|
|
|
// code is run. I was therefore unable to determine how to substitute its presence in the error
|
|
|
|
// message.
|
|
|
|
static wcstring reconstruct_orig_str(wcstring tokenized_str) {
|
|
|
|
wcstring orig_str = tokenized_str;
|
|
|
|
|
|
|
|
if (tokenized_str.find(VARIABLE_EXPAND_SINGLE) != std::string::npos) {
|
2016-07-06 13:33:32 +08:00
|
|
|
// Variable was quoted to force expansion of multiple elements into a single element.
|
|
|
|
//
|
|
|
|
// The following isn't entirely correct. For example, $abc"$def" will become "$abc$def".
|
|
|
|
// However, anyone writing the former is asking for trouble so I don't feel bad about not
|
|
|
|
// accurately reconstructing what they typed.
|
2017-03-29 10:15:08 +08:00
|
|
|
wcstring new_str = wcstring(tokenized_str);
|
|
|
|
std::replace(new_str.begin(), new_str.end(), (wchar_t)VARIABLE_EXPAND_SINGLE, L'$');
|
|
|
|
orig_str = L"\"" + new_str + L"\"";
|
2016-07-06 13:33:32 +08:00
|
|
|
}
|
|
|
|
|
2017-03-29 10:15:08 +08:00
|
|
|
return orig_str;
|
2016-07-06 13:33:32 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
/// Handle the case of command not found.
|
|
|
|
parse_execution_result_t parse_execution_context_t::handle_command_not_found(
|
2018-01-16 04:41:54 +08:00
|
|
|
const wcstring &cmd_str, tnode_t<g::plain_statement> statement, int err_code) {
|
2016-05-03 03:31:33 +08:00
|
|
|
// We couldn't find the specified command. This is a non-fatal error. We want to set the exit
|
|
|
|
// status to 127, which is the standard number used by other shells like bash and zsh.
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
const wchar_t *const cmd = cmd_str.c_str();
|
|
|
|
const wchar_t *const equals_ptr = wcschr(cmd, L'=');
|
|
|
|
if (equals_ptr != NULL) {
|
|
|
|
// Try to figure out if this is a pure variable assignment (foo=bar), or if this appears to
|
|
|
|
// be running a command (foo=bar ruby...).
|
|
|
|
const wcstring name_str = wcstring(cmd, equals_ptr - cmd); // variable name, up to the =
|
|
|
|
const wcstring val_str = wcstring(equals_ptr + 1); // variable value, past the =
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2018-01-16 04:41:54 +08:00
|
|
|
auto args = statement.descendants<g::argument>(1);
|
2016-05-03 03:31:33 +08:00
|
|
|
if (!args.empty()) {
|
2018-01-16 04:41:54 +08:00
|
|
|
const wcstring argument = get_source(args.at(0));
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Looks like a command.
|
2018-01-16 04:41:54 +08:00
|
|
|
this->report_error(statement, ERROR_BAD_EQUALS_IN_COMMAND5, argument.c_str(),
|
2016-05-03 03:31:33 +08:00
|
|
|
name_str.c_str(), val_str.c_str(), argument.c_str(),
|
2018-03-10 04:40:35 +08:00
|
|
|
ellipsis_str);
|
2016-05-03 03:31:33 +08:00
|
|
|
} else {
|
2017-03-29 10:15:08 +08:00
|
|
|
wcstring assigned_val = reconstruct_orig_str(val_str);
|
2018-01-16 04:41:54 +08:00
|
|
|
this->report_error(statement, ERROR_BAD_COMMAND_ASSIGN_ERR_MSG, name_str.c_str(),
|
2017-03-29 10:15:08 +08:00
|
|
|
assigned_val.c_str());
|
2013-12-30 08:23:26 +08:00
|
|
|
}
|
2016-05-03 03:31:33 +08:00
|
|
|
} else if (err_code != ENOENT) {
|
2018-01-16 04:41:54 +08:00
|
|
|
this->report_error(statement, _(L"The file '%ls' is not executable by this user"), cmd);
|
2016-05-03 03:31:33 +08:00
|
|
|
} else {
|
|
|
|
// Handle unrecognized commands with standard command not found handler that can make better
|
|
|
|
// error messages.
|
2013-12-30 08:23:26 +08:00
|
|
|
wcstring_list_t event_args;
|
2015-04-19 03:53:43 +08:00
|
|
|
{
|
2018-01-16 07:15:45 +08:00
|
|
|
auto args = get_argument_nodes(statement.child<1>());
|
2016-05-03 03:31:33 +08:00
|
|
|
parse_execution_result_t arg_result =
|
2018-01-16 07:15:45 +08:00
|
|
|
this->expand_arguments_from_nodes(args, &event_args, failglob);
|
2015-04-19 03:53:43 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
if (arg_result != parse_execution_success) {
|
2015-04-19 03:53:43 +08:00
|
|
|
return arg_result;
|
|
|
|
}
|
|
|
|
|
|
|
|
event_args.insert(event_args.begin(), cmd_str);
|
|
|
|
}
|
|
|
|
|
2013-12-30 08:23:26 +08:00
|
|
|
event_fire_generic(L"fish_command_not_found", &event_args);
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Here we want to report an error (so it shows a backtrace), but with no text.
|
2018-01-16 04:41:54 +08:00
|
|
|
this->report_error(statement, L"");
|
2013-12-30 08:23:26 +08:00
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Set the last proc status appropriately.
|
2017-05-04 15:18:02 +08:00
|
|
|
proc_set_last_status(err_code == ENOENT ? STATUS_CMD_UNKNOWN : STATUS_NOT_EXECUTABLE);
|
2015-04-19 03:53:43 +08:00
|
|
|
|
|
|
|
return parse_execution_errored;
|
2013-12-30 08:23:26 +08:00
|
|
|
}
|
2013-12-29 08:18:38 +08:00
|
|
|
|
2018-08-26 16:41:45 +08:00
|
|
|
parse_execution_result_t parse_execution_context_t::expand_command(
|
|
|
|
tnode_t<grammar::plain_statement> statement, wcstring *out_cmd,
|
|
|
|
wcstring_list_t *out_args) const {
|
|
|
|
// Here we're expanding a command, for example $HOME/bin/stuff or $randomthing. The first
|
|
|
|
// completion becomes the command itself, everything after becomes arguments. Command
|
|
|
|
// substitutions are not supported.
|
|
|
|
parse_error_list_t errors;
|
|
|
|
|
|
|
|
// Get the unexpanded command string. We expect to always get it here.
|
|
|
|
wcstring unexp_cmd = *command_for_plain_statement(statement, pstree->src);
|
|
|
|
wcstring cmd;
|
|
|
|
wcstring_list_t args;
|
|
|
|
|
|
|
|
// Expand the string to produce completions, and report errors.
|
2018-09-11 13:29:52 +08:00
|
|
|
expand_error_t expand_err =
|
|
|
|
expand_to_command_and_args(unexp_cmd, parser->vars(), out_cmd, out_args, &errors);
|
2018-08-26 16:41:45 +08:00
|
|
|
if (expand_err == EXPAND_ERROR) {
|
|
|
|
proc_set_last_status(STATUS_ILLEGAL_CMD);
|
|
|
|
return report_errors(errors);
|
|
|
|
} else if (expand_err == EXPAND_WILDCARD_NO_MATCH) {
|
|
|
|
return report_unmatched_wildcard_error(statement);
|
|
|
|
}
|
|
|
|
assert(expand_err == EXPAND_OK || expand_err == EXPAND_WILDCARD_MATCH);
|
|
|
|
|
|
|
|
// Complain if the resulting expansion was empty, or expanded to an empty string.
|
|
|
|
if (out_cmd->empty()) {
|
|
|
|
return this->report_error(statement, _(L"The expanded command was empty."));
|
|
|
|
}
|
|
|
|
return parse_execution_success;
|
|
|
|
}
|
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
/// Creates a 'normal' (non-block) process.
|
|
|
|
parse_execution_result_t parse_execution_context_t::populate_plain_process(
|
2018-01-16 04:12:42 +08:00
|
|
|
job_t *job, process_t *proc, tnode_t<grammar::plain_statement> statement) {
|
2014-01-01 06:37:37 +08:00
|
|
|
assert(job != NULL);
|
|
|
|
assert(proc != NULL);
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// We may decide that a command should be an implicit cd.
|
2013-12-30 08:23:26 +08:00
|
|
|
bool use_implicit_cd = false;
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2018-08-26 16:41:45 +08:00
|
|
|
// Get the command and any arguments due to expanding the command.
|
|
|
|
wcstring cmd;
|
|
|
|
wcstring_list_t args_from_cmd_expansion;
|
|
|
|
auto ret = expand_command(statement, &cmd, &args_from_cmd_expansion);
|
|
|
|
if (ret != parse_execution_success) {
|
|
|
|
return ret;
|
2013-12-25 05:17:24 +08:00
|
|
|
}
|
2018-08-26 16:41:45 +08:00
|
|
|
assert(!cmd.empty() && "expand_command should not produce an empty command");
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Determine the process type.
|
2014-01-01 06:37:37 +08:00
|
|
|
enum process_type_t process_type = process_type_for_command(statement, cmd);
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Check for stack overflow.
|
|
|
|
if (process_type == INTERNAL_FUNCTION &&
|
|
|
|
parser->forbidden_function.size() > FISH_MAX_STACK_DEPTH) {
|
2014-01-02 07:29:56 +08:00
|
|
|
this->report_error(statement, CALL_STACK_LIMIT_EXCEEDED_ERR_MSG);
|
|
|
|
return parse_execution_errored;
|
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2018-08-21 11:50:27 +08:00
|
|
|
// Protect against exec with background processes running
|
|
|
|
static uint32_t last_exec_run_counter = -1;
|
|
|
|
if (process_type == INTERNAL_EXEC) {
|
|
|
|
job_iterator_t jobs;
|
|
|
|
bool have_bg = false;
|
|
|
|
const job_t *bg = nullptr;
|
|
|
|
while ((bg = jobs.next())) {
|
2018-12-31 05:32:07 +08:00
|
|
|
// The assumption here is that if it is a foreground job,
|
|
|
|
// it's related to us.
|
|
|
|
// This stops us from asking if we're doing `exec` inside a function.
|
|
|
|
if (!bg->is_completed() && !bg->is_foreground()) {
|
2018-08-21 11:50:27 +08:00
|
|
|
have_bg = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (have_bg) {
|
|
|
|
/* debug(1, "Background jobs remain! run_counter: %u, last_exec_run_count: %u", reader_run_count(), last_exec_run_counter); */
|
|
|
|
if (isatty(STDIN_FILENO) && reader_run_count() - 1 != last_exec_run_counter) {
|
|
|
|
reader_bg_job_warning();
|
|
|
|
last_exec_run_counter = reader_run_count();
|
|
|
|
return parse_execution_errored;
|
|
|
|
}
|
|
|
|
else {
|
2018-10-21 02:58:51 +08:00
|
|
|
hup_background_jobs();
|
2018-08-21 11:50:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-08 06:57:58 +08:00
|
|
|
wcstring path_to_external_command;
|
2016-05-03 03:31:33 +08:00
|
|
|
if (process_type == EXTERNAL || process_type == INTERNAL_EXEC) {
|
|
|
|
// Determine the actual command. This may be an implicit cd.
|
2018-09-17 03:48:50 +08:00
|
|
|
bool has_command = path_get_path(cmd, &path_to_external_command, parser->vars());
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// If there was no command, then we care about the value of errno after checking for it, to
|
|
|
|
// distinguish between e.g. no file vs permissions problem.
|
2013-12-30 08:23:26 +08:00
|
|
|
const int no_cmd_err_code = errno;
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// If the specified command does not exist, and is undecorated, try using an implicit cd.
|
2018-01-16 04:12:42 +08:00
|
|
|
if (!has_command && get_decoration(statement) == parse_statement_decoration_none) {
|
2016-05-03 03:31:33 +08:00
|
|
|
// Implicit cd requires an empty argument and redirection list.
|
2018-01-16 04:12:42 +08:00
|
|
|
tnode_t<g::arguments_or_redirections_list> args = statement.child<1>();
|
2018-08-26 16:41:45 +08:00
|
|
|
if (args_from_cmd_expansion.empty() && !args.try_get_child<g::argument, 0>() &&
|
|
|
|
!args.try_get_child<g::redirection, 0>()) {
|
|
|
|
// Ok, no arguments or redirections; check to see if the command is a directory.
|
2018-11-18 10:02:28 +08:00
|
|
|
use_implicit_cd =
|
2018-09-16 19:05:17 +08:00
|
|
|
path_as_implicit_cd(cmd, parser->vars().get_pwd_slash(), parser->vars())
|
|
|
|
.has_value();
|
2013-12-30 08:23:26 +08:00
|
|
|
}
|
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
if (!has_command && !use_implicit_cd) {
|
|
|
|
// No command.
|
2015-04-19 03:53:43 +08:00
|
|
|
return this->handle_command_not_found(cmd, statement, no_cmd_err_code);
|
2013-12-25 05:17:24 +08:00
|
|
|
}
|
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2018-08-26 16:41:45 +08:00
|
|
|
// Produce the full argument list and the set of IO redirections.
|
|
|
|
wcstring_list_t cmd_args;
|
2013-12-30 08:23:26 +08:00
|
|
|
io_chain_t process_io_chain;
|
2016-05-03 03:31:33 +08:00
|
|
|
if (use_implicit_cd) {
|
2018-08-26 16:41:45 +08:00
|
|
|
// Implicit cd is simple.
|
|
|
|
cmd_args = {L"cd", cmd};
|
2014-01-08 06:57:58 +08:00
|
|
|
path_to_external_command.clear();
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// If we have defined a wrapper around cd, use it, otherwise use the cd builtin.
|
2013-12-30 08:23:26 +08:00
|
|
|
process_type = function_exists(L"cd") ? INTERNAL_FUNCTION : INTERNAL_BUILTIN;
|
2016-05-03 03:31:33 +08:00
|
|
|
} else {
|
2018-08-26 16:41:45 +08:00
|
|
|
// Not implicit cd.
|
2017-04-05 12:28:57 +08:00
|
|
|
const globspec_t glob_behavior = (cmd == L"set" || cmd == L"count") ? nullglob : failglob;
|
2018-08-26 16:41:45 +08:00
|
|
|
// Form the list of arguments. The command is the first argument, followed by any arguments
|
|
|
|
// from expanding the command, followed by the argument nodes themselves. E.g. if the
|
|
|
|
// command is '$gco foo' and $gco is git checkout.
|
|
|
|
cmd_args.push_back(cmd);
|
|
|
|
cmd_args.insert(cmd_args.end(), args_from_cmd_expansion.begin(),
|
|
|
|
args_from_cmd_expansion.end());
|
2018-01-16 07:15:45 +08:00
|
|
|
argument_node_list_t arg_nodes = statement.descendants<g::argument>();
|
2016-05-03 03:31:33 +08:00
|
|
|
parse_execution_result_t arg_result =
|
2018-08-26 16:41:45 +08:00
|
|
|
this->expand_arguments_from_nodes(arg_nodes, &cmd_args, glob_behavior);
|
2016-05-03 03:31:33 +08:00
|
|
|
if (arg_result != parse_execution_success) {
|
2014-06-01 03:41:27 +08:00
|
|
|
return arg_result;
|
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// The set of IO redirections that we construct for the process.
|
2018-01-16 07:37:13 +08:00
|
|
|
if (!this->determine_io_chain(statement.child<1>(), &process_io_chain)) {
|
2014-01-01 06:37:37 +08:00
|
|
|
return parse_execution_errored;
|
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Determine the process type.
|
2014-01-01 06:37:37 +08:00
|
|
|
process_type = process_type_for_command(statement, cmd);
|
2013-12-30 08:23:26 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Populate the process.
|
2014-01-01 06:37:37 +08:00
|
|
|
proc->type = process_type;
|
2018-08-26 16:41:45 +08:00
|
|
|
proc->set_argv(cmd_args);
|
2014-01-01 06:37:37 +08:00
|
|
|
proc->set_io_chain(process_io_chain);
|
2014-01-08 06:57:58 +08:00
|
|
|
proc->actual_cmd = path_to_external_command;
|
2014-01-01 06:37:37 +08:00
|
|
|
return parse_execution_success;
|
2013-12-25 05:17:24 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Determine the list of arguments, expanding stuff. Reports any errors caused by expansion. If we
|
|
|
|
// have a wildcard that could not be expanded, report the error and continue.
|
2018-01-16 07:15:45 +08:00
|
|
|
parse_execution_result_t parse_execution_context_t::expand_arguments_from_nodes(
|
|
|
|
const argument_node_list_t &argument_nodes, wcstring_list_t *out_arguments,
|
|
|
|
globspec_t glob_behavior) {
|
2016-05-03 03:31:33 +08:00
|
|
|
// Get all argument nodes underneath the statement. We guess we'll have that many arguments (but
|
|
|
|
// may have more or fewer, if there are wildcards involved).
|
2014-06-01 03:41:27 +08:00
|
|
|
out_arguments->reserve(out_arguments->size() + argument_nodes.size());
|
2016-02-28 08:14:05 +08:00
|
|
|
std::vector<completion_t> arg_expanded;
|
2018-01-16 07:15:45 +08:00
|
|
|
for (const auto arg_node : argument_nodes) {
|
2016-05-03 03:31:33 +08:00
|
|
|
// Expect all arguments to have source.
|
2013-12-25 05:17:24 +08:00
|
|
|
assert(arg_node.has_source());
|
2017-12-23 06:40:15 +08:00
|
|
|
const wcstring arg_str = arg_node.get_source(pstree->src);
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Expand this string.
|
2014-03-22 08:13:33 +08:00
|
|
|
parse_error_list_t errors;
|
2016-02-28 08:14:05 +08:00
|
|
|
arg_expanded.clear();
|
2018-09-11 13:29:52 +08:00
|
|
|
int expand_ret =
|
|
|
|
expand_string(arg_str, &arg_expanded, EXPAND_NO_DESCRIPTIONS, parser->vars(), &errors);
|
2018-01-16 07:15:45 +08:00
|
|
|
parse_error_offset_source_start(&errors, arg_node.source_range()->start);
|
2016-05-03 03:31:33 +08:00
|
|
|
switch (expand_ret) {
|
|
|
|
case EXPAND_ERROR: {
|
2014-03-22 08:13:33 +08:00
|
|
|
this->report_errors(errors);
|
2016-02-09 02:49:26 +08:00
|
|
|
return parse_execution_errored;
|
2013-12-25 05:17:24 +08:00
|
|
|
}
|
2016-05-03 03:31:33 +08:00
|
|
|
case EXPAND_WILDCARD_NO_MATCH: {
|
|
|
|
if (glob_behavior == failglob) {
|
|
|
|
// Report the unmatched wildcard error and stop processing.
|
2016-02-09 02:49:26 +08:00
|
|
|
report_unmatched_wildcard_error(arg_node);
|
|
|
|
return parse_execution_errored;
|
|
|
|
}
|
2013-12-25 05:17:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case EXPAND_WILDCARD_MATCH:
|
2016-05-03 03:31:33 +08:00
|
|
|
case EXPAND_OK: {
|
2013-12-25 05:17:24 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-10-30 08:25:48 +08:00
|
|
|
default: {
|
|
|
|
DIE("unexpected expand_string() return value");
|
|
|
|
break;
|
|
|
|
}
|
2013-12-25 05:17:24 +08:00
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2017-01-27 08:14:50 +08:00
|
|
|
// Now copy over any expanded arguments. Use std::move() to avoid extra allocations; this
|
2016-05-03 03:31:33 +08:00
|
|
|
// is called very frequently.
|
2017-01-27 08:14:50 +08:00
|
|
|
out_arguments->reserve(out_arguments->size() + arg_expanded.size());
|
|
|
|
for (completion_t &new_arg : arg_expanded) {
|
|
|
|
out_arguments->push_back(std::move(new_arg.completion));
|
2013-12-25 05:17:24 +08:00
|
|
|
}
|
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-02-09 02:49:26 +08:00
|
|
|
return parse_execution_success;
|
2013-12-25 05:17:24 +08:00
|
|
|
}
|
|
|
|
|
2018-01-16 07:37:13 +08:00
|
|
|
bool parse_execution_context_t::determine_io_chain(tnode_t<g::arguments_or_redirections_list> node,
|
2016-05-03 03:31:33 +08:00
|
|
|
io_chain_t *out_chain) {
|
2013-12-25 05:17:24 +08:00
|
|
|
io_chain_t result;
|
2013-12-27 04:55:10 +08:00
|
|
|
bool errored = false;
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Get all redirection nodes underneath the statement.
|
2018-01-23 05:18:34 +08:00
|
|
|
while (auto redirect_node = node.next_in_list<g::redirection>()) {
|
2016-05-03 03:31:33 +08:00
|
|
|
int source_fd = -1; // source fd
|
|
|
|
wcstring target; // file path or target fd
|
2018-02-24 07:19:58 +08:00
|
|
|
auto redirect_type = redirection_type(redirect_node, pstree->src, &source_fd, &target);
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// PCA: I can't justify this EXPAND_SKIP_VARIABLES flag. It was like this when I got here.
|
2018-09-11 13:29:52 +08:00
|
|
|
bool target_expanded =
|
|
|
|
expand_one(target, no_exec ? EXPAND_SKIP_VARIABLES : 0, parser->vars());
|
2016-05-03 03:31:33 +08:00
|
|
|
if (!target_expanded || target.empty()) {
|
|
|
|
// TODO: Improve this error message.
|
|
|
|
errored =
|
|
|
|
report_error(redirect_node, _(L"Invalid redirection target: %ls"), target.c_str());
|
2013-12-25 05:17:24 +08:00
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Generate the actual IO redirection.
|
2013-12-25 05:17:24 +08:00
|
|
|
shared_ptr<io_data_t> new_io;
|
2018-02-24 07:19:58 +08:00
|
|
|
assert(redirect_type && "expected to have a valid redirection");
|
|
|
|
switch (*redirect_type) {
|
|
|
|
case redirection_type_t::fd: {
|
2016-05-03 03:31:33 +08:00
|
|
|
if (target == L"-") {
|
2013-12-25 05:17:24 +08:00
|
|
|
new_io.reset(new io_close_t(source_fd));
|
2016-05-03 03:31:33 +08:00
|
|
|
} else {
|
2016-11-23 12:24:03 +08:00
|
|
|
int old_fd = fish_wcstoi(target.c_str());
|
|
|
|
if (errno || old_fd < 0) {
|
2017-05-02 12:44:30 +08:00
|
|
|
const wchar_t *fmt =
|
|
|
|
_(L"Requested redirection to '%ls', "
|
|
|
|
L"which is not a valid file descriptor");
|
|
|
|
errored = report_error(redirect_node, fmt, target.c_str());
|
2016-05-03 03:31:33 +08:00
|
|
|
} else {
|
2015-01-09 02:44:05 +08:00
|
|
|
new_io.reset(new io_fd_t(source_fd, old_fd, true));
|
2013-12-25 05:17:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-02-24 07:19:58 +08:00
|
|
|
default: {
|
|
|
|
int oflags = oflags_for_redirection_type(*redirect_type);
|
2013-12-25 05:17:24 +08:00
|
|
|
io_file_t *new_io_file = new io_file_t(source_fd, target, oflags);
|
|
|
|
new_io.reset(new_io_file);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Append the new_io if we got one.
|
|
|
|
if (new_io.get() != NULL) {
|
2013-12-25 05:17:24 +08:00
|
|
|
result.push_back(new_io);
|
|
|
|
}
|
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
if (out_chain && !errored) {
|
2017-01-27 08:14:50 +08:00
|
|
|
*out_chain = std::move(result);
|
2013-12-27 04:55:10 +08:00
|
|
|
}
|
2016-05-03 03:31:33 +08:00
|
|
|
return !errored;
|
2013-12-25 05:17:24 +08:00
|
|
|
}
|
|
|
|
|
2018-03-03 10:09:16 +08:00
|
|
|
parse_execution_result_t parse_execution_context_t::populate_not_process(
|
|
|
|
job_t *job, process_t *proc, tnode_t<g::not_statement> not_statement) {
|
2018-10-03 01:30:23 +08:00
|
|
|
job->set_flag(job_flag_t::NEGATE, !job->get_flag(job_flag_t::NEGATE));
|
2018-03-05 08:06:32 +08:00
|
|
|
return this->populate_job_process(job, proc,
|
|
|
|
not_statement.require_get_child<g::statement, 1>());
|
2013-12-25 05:17:24 +08:00
|
|
|
}
|
|
|
|
|
2018-01-16 07:37:13 +08:00
|
|
|
template <typename Type>
|
2018-02-11 11:16:35 +08:00
|
|
|
parse_execution_result_t parse_execution_context_t::populate_block_process(
|
|
|
|
job_t *job, process_t *proc, tnode_t<g::statement> statement,
|
|
|
|
tnode_t<Type> specific_statement) {
|
2016-05-03 03:31:33 +08:00
|
|
|
// We handle block statements by creating INTERNAL_BLOCK_NODE, that will bounce back to us when
|
|
|
|
// it's time to execute them.
|
2016-10-10 05:38:26 +08:00
|
|
|
UNUSED(job);
|
2018-01-16 07:37:13 +08:00
|
|
|
static_assert(Type::token == symbol_block_statement || Type::token == symbol_if_statement ||
|
|
|
|
Type::token == symbol_switch_statement,
|
|
|
|
"Invalid block process");
|
2018-02-11 11:16:35 +08:00
|
|
|
assert(statement && "statement missing");
|
|
|
|
assert(specific_statement && "specific_statement missing");
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// The set of IO redirections that we construct for the process.
|
2018-01-16 07:37:13 +08:00
|
|
|
// TODO: fix this ugly find_child.
|
2018-02-11 11:16:35 +08:00
|
|
|
auto arguments = specific_statement.template find_child<g::arguments_or_redirections_list>();
|
2013-12-29 08:18:38 +08:00
|
|
|
io_chain_t process_io_chain;
|
2018-01-16 07:37:13 +08:00
|
|
|
bool errored = !this->determine_io_chain(arguments, &process_io_chain);
|
2016-05-03 03:31:33 +08:00
|
|
|
if (errored) return parse_execution_errored;
|
2013-12-29 08:18:38 +08:00
|
|
|
|
2014-01-01 06:37:37 +08:00
|
|
|
proc->type = INTERNAL_BLOCK_NODE;
|
2018-02-12 11:34:12 +08:00
|
|
|
proc->block_node_source = pstree;
|
2018-02-11 11:16:35 +08:00
|
|
|
proc->internal_block_node = statement;
|
2014-01-01 06:37:37 +08:00
|
|
|
proc->set_io_chain(process_io_chain);
|
|
|
|
return parse_execution_success;
|
2013-12-27 04:24:00 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
parse_execution_result_t parse_execution_context_t::populate_job_process(
|
2018-01-16 03:45:47 +08:00
|
|
|
job_t *job, process_t *proc, tnode_t<grammar::statement> statement) {
|
2016-05-03 03:31:33 +08:00
|
|
|
// Get the "specific statement" which is boolean / block / if / switch / decorated.
|
2018-01-16 08:18:03 +08:00
|
|
|
const parse_node_t &specific_statement = statement.get_child_node<0>();
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2014-01-01 06:37:37 +08:00
|
|
|
parse_execution_result_t result = parse_execution_success;
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
switch (specific_statement.type) {
|
2018-03-03 10:09:16 +08:00
|
|
|
case symbol_not_statement: {
|
|
|
|
result = this->populate_not_process(job, proc, {&tree(), &specific_statement});
|
2013-12-25 05:17:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case symbol_block_statement:
|
2018-01-16 07:37:13 +08:00
|
|
|
result = this->populate_block_process(
|
2018-02-11 11:16:35 +08:00
|
|
|
job, proc, statement, tnode_t<g::block_statement>(&tree(), &specific_statement));
|
2018-01-16 07:37:13 +08:00
|
|
|
break;
|
2013-12-27 04:24:00 +08:00
|
|
|
case symbol_if_statement:
|
2018-01-16 07:37:13 +08:00
|
|
|
result = this->populate_block_process(
|
2018-02-11 11:16:35 +08:00
|
|
|
job, proc, statement, tnode_t<g::if_statement>(&tree(), &specific_statement));
|
2018-01-16 07:37:13 +08:00
|
|
|
break;
|
|
|
|
case symbol_switch_statement:
|
|
|
|
result = this->populate_block_process(
|
2018-02-11 11:16:35 +08:00
|
|
|
job, proc, statement, tnode_t<g::switch_statement>(&tree(), &specific_statement));
|
2013-12-25 05:17:24 +08:00
|
|
|
break;
|
2016-05-03 03:31:33 +08:00
|
|
|
case symbol_decorated_statement: {
|
|
|
|
// Get the plain statement. It will pull out the decoration itself.
|
2018-01-16 04:12:42 +08:00
|
|
|
tnode_t<g::decorated_statement> dec_stat{&tree(), &specific_statement};
|
|
|
|
auto plain_statement = dec_stat.find_child<g::plain_statement>();
|
2014-01-01 06:37:37 +08:00
|
|
|
result = this->populate_plain_process(job, proc, plain_statement);
|
2013-12-25 05:17:24 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-05-03 03:31:33 +08:00
|
|
|
default: {
|
2017-01-03 13:11:53 +08:00
|
|
|
debug(0, L"'%ls' not handled by new parser yet.",
|
|
|
|
specific_statement.describe().c_str());
|
2013-12-27 04:24:00 +08:00
|
|
|
PARSER_DIE();
|
|
|
|
break;
|
2016-05-03 03:31:33 +08:00
|
|
|
}
|
2013-12-25 05:17:24 +08:00
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2013-12-25 05:17:24 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
parse_execution_result_t parse_execution_context_t::populate_job_from_job_node(
|
2018-01-14 17:17:57 +08:00
|
|
|
job_t *j, tnode_t<grammar::job> job_node, const block_t *associated_block) {
|
2016-10-10 05:38:26 +08:00
|
|
|
UNUSED(associated_block);
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Tell the job what its command is.
|
2013-12-25 05:17:24 +08:00
|
|
|
j->set_command(get_source(job_node));
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// We are going to construct process_t structures for every statement in the job. Get the first
|
|
|
|
// statement.
|
2018-01-16 03:45:47 +08:00
|
|
|
tnode_t<g::statement> statement = job_node.child<0>();
|
|
|
|
assert(statement);
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2014-01-01 06:37:37 +08:00
|
|
|
parse_execution_result_t result = parse_execution_success;
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Create processes. Each one may fail.
|
2017-01-24 01:28:34 +08:00
|
|
|
process_list_t processes;
|
|
|
|
processes.emplace_back(new process_t());
|
2018-01-16 03:45:47 +08:00
|
|
|
result = this->populate_job_process(j, processes.back().get(), statement);
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Construct process_ts for job continuations (pipelines), by walking the list until we hit the
|
|
|
|
// terminal (empty) job continuation.
|
2018-01-14 17:17:57 +08:00
|
|
|
tnode_t<g::job_continuation> job_cont = job_node.child<1>();
|
|
|
|
assert(job_cont);
|
|
|
|
while (auto pipe = job_cont.try_get_child<g::tok_pipe, 0>()) {
|
|
|
|
if (result != parse_execution_success) {
|
|
|
|
break;
|
|
|
|
}
|
2018-02-19 06:37:44 +08:00
|
|
|
tnode_t<g::statement> statement = job_cont.require_get_child<g::statement, 2>();
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Handle the pipe, whose fd may not be the obvious stdout.
|
2018-01-14 17:17:57 +08:00
|
|
|
int pipe_write_fd = fd_redirected_by_pipe(get_source(pipe));
|
2016-05-03 03:31:33 +08:00
|
|
|
if (pipe_write_fd == -1) {
|
2018-01-14 17:17:57 +08:00
|
|
|
result = report_error(pipe, ILLEGAL_FD_ERR_MSG, get_source(pipe).c_str());
|
2014-01-13 19:57:59 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
processes.back()->pipe_write_fd = pipe_write_fd;
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Store the new process (and maybe with an error).
|
2017-01-24 01:28:34 +08:00
|
|
|
processes.emplace_back(new process_t());
|
2018-01-14 17:17:57 +08:00
|
|
|
result = this->populate_job_process(j, processes.back().get(), statement);
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Get the next continuation.
|
2018-02-19 06:37:44 +08:00
|
|
|
job_cont = job_cont.require_get_child<g::job_continuation, 3>();
|
2018-01-14 17:17:57 +08:00
|
|
|
assert(job_cont);
|
2013-12-25 05:17:24 +08:00
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2017-01-24 01:28:34 +08:00
|
|
|
// Inform our processes of who is first and last
|
|
|
|
processes.front()->is_first_in_job = true;
|
|
|
|
processes.back()->is_last_in_job = true;
|
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Return what happened.
|
|
|
|
if (result == parse_execution_success) {
|
|
|
|
// Link up the processes.
|
2016-10-24 04:58:12 +08:00
|
|
|
assert(!processes.empty()); //!OCLINT(multiple unary operator)
|
2017-01-24 01:28:34 +08:00
|
|
|
j->processes = std::move(processes);
|
2014-01-01 06:37:37 +08:00
|
|
|
}
|
|
|
|
return result;
|
2013-12-25 05:17:24 +08:00
|
|
|
}
|
|
|
|
|
2018-01-16 08:00:19 +08:00
|
|
|
parse_execution_result_t parse_execution_context_t::run_1_job(tnode_t<g::job> job_node,
|
2016-05-03 03:31:33 +08:00
|
|
|
const block_t *associated_block) {
|
|
|
|
if (should_cancel_execution(associated_block)) {
|
2014-01-01 06:37:37 +08:00
|
|
|
return parse_execution_cancelled;
|
2013-12-29 08:18:38 +08:00
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Get terminal modes.
|
2013-12-25 05:17:24 +08:00
|
|
|
struct termios tmodes = {};
|
2016-10-23 02:21:13 +08:00
|
|
|
if (shell_is_interactive() && tcgetattr(STDIN_FILENO, &tmodes)) {
|
|
|
|
// Need real error handling here.
|
|
|
|
wperror(L"tcgetattr");
|
|
|
|
return parse_execution_errored;
|
2013-12-25 05:17:24 +08:00
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Increment the eval_level for the duration of this command.
|
2018-02-12 13:42:23 +08:00
|
|
|
scoped_push<int> saved_eval_level(&parser->eval_level, parser->eval_level + 1);
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Save the node index.
|
2018-02-12 12:08:40 +08:00
|
|
|
scoped_push<tnode_t<grammar::job>> saved_node(&executing_job_node, job_node);
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Profiling support.
|
2014-02-10 06:04:43 +08:00
|
|
|
long long start_time = 0, parse_time = 0, exec_time = 0;
|
|
|
|
profile_item_t *profile_item = this->parser->create_profile_item();
|
2016-05-03 03:31:33 +08:00
|
|
|
if (profile_item != NULL) {
|
2014-02-10 06:04:43 +08:00
|
|
|
start_time = get_time();
|
|
|
|
}
|
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// When we encounter a block construct (e.g. while loop) in the general case, we create a "block
|
2018-02-11 11:16:35 +08:00
|
|
|
// process" containing its node. This allows us to handle block-level redirections.
|
2016-05-03 03:31:33 +08:00
|
|
|
// However, if there are no redirections, then we can just jump into the block directly, which
|
|
|
|
// is significantly faster.
|
|
|
|
if (job_is_simple_block(job_node)) {
|
2015-07-20 17:34:57 +08:00
|
|
|
parse_execution_result_t result = parse_execution_success;
|
2016-05-03 03:31:33 +08:00
|
|
|
|
2018-01-16 08:00:19 +08:00
|
|
|
tnode_t<g::statement> statement = job_node.child<0>();
|
2018-01-16 08:18:03 +08:00
|
|
|
const parse_node_t &specific_statement = statement.get_child_node<0>();
|
2014-01-08 02:45:36 +08:00
|
|
|
assert(specific_statement_type_is_redirectable_block(specific_statement));
|
2016-05-03 03:31:33 +08:00
|
|
|
switch (specific_statement.type) {
|
|
|
|
case symbol_block_statement: {
|
2018-04-01 05:57:24 +08:00
|
|
|
result =
|
|
|
|
this->run_block_statement({&tree(), &specific_statement}, associated_block);
|
2014-02-10 06:04:43 +08:00
|
|
|
break;
|
2016-05-03 03:31:33 +08:00
|
|
|
}
|
|
|
|
case symbol_if_statement: {
|
2018-04-01 05:57:24 +08:00
|
|
|
result = this->run_if_statement({&tree(), &specific_statement}, associated_block);
|
2014-02-10 06:04:43 +08:00
|
|
|
break;
|
2016-05-03 03:31:33 +08:00
|
|
|
}
|
|
|
|
case symbol_switch_statement: {
|
2018-01-14 18:30:18 +08:00
|
|
|
result = this->run_switch_statement({&tree(), &specific_statement});
|
2014-02-10 06:04:43 +08:00
|
|
|
break;
|
2016-05-03 03:31:33 +08:00
|
|
|
}
|
|
|
|
default: {
|
|
|
|
// Other types should be impossible due to the
|
|
|
|
// specific_statement_type_is_redirectable_block check.
|
2014-01-08 02:45:36 +08:00
|
|
|
PARSER_DIE();
|
|
|
|
break;
|
2016-05-03 03:31:33 +08:00
|
|
|
}
|
2014-01-08 02:45:36 +08:00
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
if (profile_item != NULL) {
|
|
|
|
// Block-types profile a little weird. They have no 'parse' time, and their command is
|
|
|
|
// just the block type.
|
2014-02-10 06:04:43 +08:00
|
|
|
exec_time = get_time();
|
2018-02-12 13:42:23 +08:00
|
|
|
profile_item->level = parser->eval_level;
|
2014-02-10 06:04:43 +08:00
|
|
|
profile_item->parse = 0;
|
2016-05-03 03:31:33 +08:00
|
|
|
profile_item->exec = (int)(exec_time - start_time);
|
2017-12-23 06:40:15 +08:00
|
|
|
profile_item->cmd = profiling_cmd_name_for_redirectable_block(
|
|
|
|
specific_statement, this->tree(), this->pstree->src);
|
2015-07-20 17:34:57 +08:00
|
|
|
profile_item->skipped = false;
|
2014-02-10 06:04:43 +08:00
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2014-02-10 06:04:43 +08:00
|
|
|
return result;
|
2013-12-25 05:17:24 +08:00
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2018-11-04 15:58:44 +08:00
|
|
|
shared_ptr<job_t> job = std::make_shared<job_t>(acquire_job_id(), block_io, parent_job);
|
2017-01-27 06:47:32 +08:00
|
|
|
job->tmodes = tmodes;
|
2018-10-03 01:30:23 +08:00
|
|
|
job->set_flag(job_flag_t::JOB_CONTROL,
|
2017-01-27 12:00:43 +08:00
|
|
|
(job_control_mode == JOB_CONTROL_ALL) ||
|
|
|
|
((job_control_mode == JOB_CONTROL_INTERACTIVE) && shell_is_interactive()));
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2018-10-03 01:30:23 +08:00
|
|
|
job->set_flag(job_flag_t::FOREGROUND, !job_node_is_background(job_node));
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2018-10-03 01:30:23 +08:00
|
|
|
job->set_flag(job_flag_t::TERMINAL, job->get_flag(job_flag_t::JOB_CONTROL) && !is_event);
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2018-10-03 01:30:23 +08:00
|
|
|
job->set_flag(job_flag_t::SKIP_NOTIFICATION,
|
2017-01-27 12:00:43 +08:00
|
|
|
is_subshell || is_block || is_event || !shell_is_interactive());
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Tell the current block what its job is. This has to happen before we populate it (#1394).
|
2017-01-27 06:47:32 +08:00
|
|
|
parser->current_block()->job = job;
|
2014-04-02 15:32:08 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Populate the job. This may fail for reasons like command_not_found. If this fails, an error
|
|
|
|
// will have been printed.
|
|
|
|
parse_execution_result_t pop_result =
|
2018-01-16 08:00:19 +08:00
|
|
|
this->populate_job_from_job_node(job.get(), job_node, associated_block);
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Clean up the job on failure or cancellation.
|
2014-01-01 06:37:37 +08:00
|
|
|
bool populated_job = (pop_result == parse_execution_success);
|
2016-05-03 03:31:33 +08:00
|
|
|
if (!populated_job || this->should_cancel_execution(associated_block)) {
|
2017-01-27 06:47:32 +08:00
|
|
|
assert(parser->current_block()->job == job);
|
2014-04-02 15:32:08 +08:00
|
|
|
parser->current_block()->job = NULL;
|
2014-01-03 04:37:50 +08:00
|
|
|
populated_job = false;
|
2013-12-27 05:36:43 +08:00
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Store time it took to 'parse' the command.
|
|
|
|
if (profile_item != NULL) {
|
2013-12-27 04:24:00 +08:00
|
|
|
parse_time = get_time();
|
2013-12-25 05:17:24 +08:00
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
if (populated_job) {
|
|
|
|
// Success. Give the job to the parser - it will clean it up.
|
2017-01-27 06:47:32 +08:00
|
|
|
parser->job_add(job);
|
2013-12-27 17:38:43 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Check to see if this contained any external commands.
|
2013-12-27 04:55:10 +08:00
|
|
|
bool job_contained_external_command = false;
|
2017-01-27 06:47:32 +08:00
|
|
|
for (const auto &proc : job->processes) {
|
2016-05-03 03:31:33 +08:00
|
|
|
if (proc->type == EXTERNAL) {
|
2013-12-27 04:55:10 +08:00
|
|
|
job_contained_external_command = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Actually execute the job.
|
2018-11-19 07:14:08 +08:00
|
|
|
if (!exec_job(*this->parser, job)) {
|
|
|
|
parser->job_remove(job.get());
|
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Only external commands require a new fishd barrier.
|
|
|
|
if (job_contained_external_command) {
|
2013-12-27 04:55:10 +08:00
|
|
|
set_proc_had_barrier(false);
|
2013-12-25 05:17:24 +08:00
|
|
|
}
|
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
if (profile_item != NULL) {
|
2013-12-27 04:24:00 +08:00
|
|
|
exec_time = get_time();
|
2018-02-12 13:42:23 +08:00
|
|
|
profile_item->level = parser->eval_level;
|
2016-05-03 03:31:33 +08:00
|
|
|
profile_item->parse = (int)(parse_time - start_time);
|
|
|
|
profile_item->exec = (int)(exec_time - parse_time);
|
2017-01-27 06:47:32 +08:00
|
|
|
profile_item->cmd = job ? job->command() : wcstring();
|
2016-05-03 03:31:33 +08:00
|
|
|
profile_item->skipped = !populated_job;
|
2013-12-25 05:17:24 +08:00
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2018-11-19 07:37:54 +08:00
|
|
|
job_reap(false); // clean up jobs
|
2015-07-20 17:34:57 +08:00
|
|
|
return parse_execution_success;
|
2013-12-25 05:17:24 +08:00
|
|
|
}
|
|
|
|
|
2018-03-02 10:30:48 +08:00
|
|
|
parse_execution_result_t parse_execution_context_t::run_job_conjunction(
|
|
|
|
tnode_t<grammar::job_conjunction> job_expr, const block_t *associated_block) {
|
|
|
|
parse_execution_result_t result = parse_execution_success;
|
|
|
|
tnode_t<g::job_conjunction> cursor = job_expr;
|
2018-03-03 10:09:16 +08:00
|
|
|
// continuation is the parent of the cursor
|
2018-03-02 10:30:48 +08:00
|
|
|
tnode_t<g::job_conjunction_continuation> continuation;
|
|
|
|
while (cursor) {
|
|
|
|
if (should_cancel_execution(associated_block)) break;
|
|
|
|
bool skip = false;
|
|
|
|
if (continuation) {
|
|
|
|
// Check the conjunction type.
|
|
|
|
parse_bool_statement_type_t conj = bool_statement_type(continuation);
|
|
|
|
assert((conj == parse_bool_and || conj == parse_bool_or) && "Unexpected conjunction");
|
2018-03-03 10:09:16 +08:00
|
|
|
skip = should_skip(conj);
|
2018-03-02 10:30:48 +08:00
|
|
|
}
|
|
|
|
if (! skip) {
|
|
|
|
result = run_1_job(cursor.child<0>(), associated_block);
|
|
|
|
}
|
|
|
|
continuation = cursor.child<1>();
|
|
|
|
cursor = continuation.try_get_child<g::job_conjunction, 2>();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-03-03 10:09:16 +08:00
|
|
|
bool parse_execution_context_t::should_skip(parse_bool_statement_type_t type) const {
|
|
|
|
switch (type) {
|
|
|
|
case parse_bool_and:
|
|
|
|
// AND. Skip if the last job failed.
|
|
|
|
return proc_get_last_status() != 0;
|
|
|
|
case parse_bool_or:
|
|
|
|
// OR. Skip if the last job succeeded.
|
|
|
|
return proc_get_last_status() == 0;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-16 08:08:06 +08:00
|
|
|
template <typename Type>
|
|
|
|
parse_execution_result_t parse_execution_context_t::run_job_list(tnode_t<Type> job_list,
|
2016-05-03 03:31:33 +08:00
|
|
|
const block_t *associated_block) {
|
2018-03-03 10:09:16 +08:00
|
|
|
// We handle both job_list and andor_job_list uniformly.
|
2018-01-16 08:08:06 +08:00
|
|
|
static_assert(Type::token == symbol_job_list || Type::token == symbol_andor_job_list,
|
|
|
|
"Not a job list");
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2014-01-01 06:37:37 +08:00
|
|
|
parse_execution_result_t result = parse_execution_success;
|
2018-03-03 10:09:16 +08:00
|
|
|
while (auto job_conj = job_list.template next_in_list<g::job_conjunction>()) {
|
2018-01-16 08:08:06 +08:00
|
|
|
if (should_cancel_execution(associated_block)) break;
|
2018-03-03 10:09:16 +08:00
|
|
|
|
|
|
|
// Maybe skip the job if it has a leading and/or.
|
|
|
|
// Skipping is treated as success.
|
|
|
|
if (should_skip(get_decorator(job_conj))) {
|
|
|
|
result = parse_execution_success;
|
|
|
|
} else {
|
|
|
|
result = this->run_job_conjunction(job_conj, associated_block);
|
|
|
|
}
|
2013-12-25 05:17:24 +08:00
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2018-03-03 10:09:16 +08:00
|
|
|
// Returns the result of the last job executed or skipped.
|
2013-12-27 05:24:10 +08:00
|
|
|
return result;
|
2013-12-27 04:24:00 +08:00
|
|
|
}
|
|
|
|
|
2018-02-11 11:16:35 +08:00
|
|
|
parse_execution_result_t parse_execution_context_t::eval_node(tnode_t<g::statement> statement,
|
|
|
|
const block_t *associated_block,
|
|
|
|
const io_chain_t &io) {
|
|
|
|
assert(statement && "Empty node in eval_node");
|
|
|
|
assert(statement.matches_node_tree(tree()) && "statement has unexpected tree");
|
2016-05-03 03:31:33 +08:00
|
|
|
// Apply this block IO for the duration of this function.
|
2013-12-29 08:18:38 +08:00
|
|
|
scoped_push<io_chain_t> block_io_push(&block_io, io);
|
2014-01-01 06:37:37 +08:00
|
|
|
enum parse_execution_result_t status = parse_execution_success;
|
2018-02-11 11:16:35 +08:00
|
|
|
if (auto block = statement.try_get_child<g::block_statement, 0>()) {
|
2018-04-01 05:57:24 +08:00
|
|
|
status = this->run_block_statement(block, associated_block);
|
2018-02-11 11:16:35 +08:00
|
|
|
} else if (auto ifstat = statement.try_get_child<g::if_statement, 0>()) {
|
2018-04-01 05:57:24 +08:00
|
|
|
status = this->run_if_statement(ifstat, associated_block);
|
2018-02-11 11:16:35 +08:00
|
|
|
} else if (auto switchstat = statement.try_get_child<g::switch_statement, 0>()) {
|
|
|
|
status = this->run_switch_statement(switchstat);
|
|
|
|
} else {
|
|
|
|
debug(0, "Unexpected node %ls found in %s", statement.node()->describe().c_str(),
|
|
|
|
__FUNCTION__);
|
|
|
|
abort();
|
2013-12-27 17:38:43 +08:00
|
|
|
}
|
2018-02-11 11:16:35 +08:00
|
|
|
return status;
|
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2018-02-11 11:16:35 +08:00
|
|
|
parse_execution_result_t parse_execution_context_t::eval_node(tnode_t<g::job_list> job_list,
|
|
|
|
const block_t *associated_block,
|
|
|
|
const io_chain_t &io) {
|
|
|
|
// Apply this block IO for the duration of this function.
|
|
|
|
assert(job_list && "Empty node in eval_node");
|
|
|
|
assert(job_list.matches_node_tree(tree()) && "job_list has unexpected tree");
|
|
|
|
scoped_push<io_chain_t> block_io_push(&block_io, io);
|
|
|
|
enum parse_execution_result_t status = parse_execution_success;
|
|
|
|
wcstring func_name;
|
|
|
|
auto infinite_recursive_node =
|
|
|
|
this->infinite_recursive_statement_in_job_list(job_list, &func_name);
|
|
|
|
if (infinite_recursive_node) {
|
|
|
|
// We have an infinite recursion.
|
|
|
|
this->report_error(infinite_recursive_node, INFINITE_FUNC_RECURSION_ERR_MSG,
|
|
|
|
func_name.c_str());
|
|
|
|
status = parse_execution_errored;
|
|
|
|
} else {
|
|
|
|
// No infinite recursion.
|
|
|
|
status = this->run_job_list(job_list, associated_block);
|
|
|
|
}
|
2014-01-01 06:37:37 +08:00
|
|
|
return status;
|
2013-12-25 05:17:24 +08:00
|
|
|
}
|
2014-03-02 08:04:13 +08:00
|
|
|
|
2018-02-12 12:08:40 +08:00
|
|
|
int parse_execution_context_t::line_offset_of_node(tnode_t<g::job> node) {
|
2016-05-03 03:31:33 +08:00
|
|
|
// If we're not executing anything, return -1.
|
2018-02-12 12:08:40 +08:00
|
|
|
if (!node) {
|
2014-03-02 08:04:13 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// If for some reason we're executing a node without source, return -1.
|
2018-02-12 12:08:40 +08:00
|
|
|
auto range = node.source_range();
|
|
|
|
if (!range) {
|
2014-03-02 08:04:13 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2016-05-03 03:31:33 +08:00
|
|
|
|
2018-02-12 12:08:40 +08:00
|
|
|
return this->line_offset_of_character_at_offset(range->start);
|
2014-09-28 08:32:54 +08:00
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
int parse_execution_context_t::line_offset_of_character_at_offset(size_t offset) {
|
|
|
|
// Count the number of newlines, leveraging our cache.
|
2017-12-23 06:40:15 +08:00
|
|
|
assert(offset <= pstree->src.size());
|
2014-03-02 08:04:13 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Easy hack to handle 0.
|
|
|
|
if (offset == 0) {
|
2014-03-17 07:45:00 +08:00
|
|
|
return 0;
|
2014-03-02 08:04:13 +08:00
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// We want to return (one plus) the number of newlines at offsets less than the given offset.
|
|
|
|
// cached_lineno_count is the number of newlines at indexes less than cached_lineno_offset.
|
2017-12-23 06:40:15 +08:00
|
|
|
const wchar_t *str = pstree->src.c_str();
|
2016-05-03 03:31:33 +08:00
|
|
|
if (offset > cached_lineno_offset) {
|
2014-03-02 08:04:13 +08:00
|
|
|
size_t i;
|
2018-11-14 18:24:55 +08:00
|
|
|
for (i = cached_lineno_offset; i < offset && str[i] != L'\0'; i++) {
|
2016-05-03 03:31:33 +08:00
|
|
|
// Add one for every newline we find in the range [cached_lineno_offset, offset).
|
|
|
|
if (str[i] == L'\n') {
|
2014-03-02 08:04:13 +08:00
|
|
|
cached_lineno_count++;
|
|
|
|
}
|
|
|
|
}
|
2016-05-03 03:31:33 +08:00
|
|
|
cached_lineno_offset =
|
|
|
|
i; // note: i, not offset, in case offset is beyond the length of the string
|
|
|
|
} else if (offset < cached_lineno_offset) {
|
|
|
|
// Subtract one for every newline we find in the range [offset, cached_lineno_offset).
|
|
|
|
for (size_t i = offset; i < cached_lineno_offset; i++) {
|
|
|
|
if (str[i] == L'\n') {
|
2014-03-02 08:04:13 +08:00
|
|
|
cached_lineno_count--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cached_lineno_offset = offset;
|
|
|
|
}
|
2014-03-17 07:45:00 +08:00
|
|
|
return cached_lineno_count;
|
|
|
|
}
|
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
int parse_execution_context_t::get_current_line_number() {
|
2014-03-17 07:45:00 +08:00
|
|
|
int line_number = -1;
|
2018-02-12 12:08:40 +08:00
|
|
|
int line_offset = this->line_offset_of_node(this->executing_job_node);
|
2016-05-03 03:31:33 +08:00
|
|
|
if (line_offset >= 0) {
|
|
|
|
// The offset is 0 based; the number is 1 based.
|
2014-03-17 07:45:00 +08:00
|
|
|
line_number = line_offset + 1;
|
|
|
|
}
|
|
|
|
return line_number;
|
2014-03-02 08:04:13 +08:00
|
|
|
}
|
2014-03-17 13:06:32 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
int parse_execution_context_t::get_current_source_offset() const {
|
2014-03-17 13:06:32 +08:00
|
|
|
int result = -1;
|
2018-02-12 12:08:40 +08:00
|
|
|
if (executing_job_node) {
|
|
|
|
if (auto range = executing_job_node.source_range()) {
|
|
|
|
result = static_cast<int>(range->start);
|
2014-03-17 13:06:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|