2020-07-02 21:51:45 +00:00
|
|
|
// Provides the "linkage" between an ast and actual execution structures (job_t, etc.)
|
2016-05-18 22:30:21 +00:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2019-10-13 22:50:48 +00:00
|
|
|
#include "parse_execution.h"
|
|
|
|
|
2015-07-25 15:14:25 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <wctype.h>
|
2016-07-06 05:33:32 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
2019-10-13 22:50:48 +00:00
|
|
|
#include <cwchar>
|
2016-04-21 06:00:54 +00:00
|
|
|
#include <memory>
|
2016-05-02 19:31:33 +00:00
|
|
|
#include <string>
|
2017-02-11 02:47:02 +00:00
|
|
|
#include <type_traits>
|
2015-07-25 15:14:25 +00:00
|
|
|
#include <vector>
|
2016-04-21 06:00:54 +00:00
|
|
|
|
2020-07-03 18:16:51 +00:00
|
|
|
#include "ast.h"
|
2016-05-02 19:31:33 +00:00
|
|
|
#include "builtin.h"
|
2017-06-16 00:57:37 +00:00
|
|
|
#include "builtin_function.h"
|
2016-05-02 19:31:33 +00:00
|
|
|
#include "common.h"
|
|
|
|
#include "complete.h"
|
2015-07-25 15:14:25 +00:00
|
|
|
#include "env.h"
|
|
|
|
#include "event.h"
|
2016-05-02 19:31:33 +00:00
|
|
|
#include "exec.h"
|
2013-12-24 21:17:24 +00:00
|
|
|
#include "expand.h"
|
2019-05-27 22:56:53 +00:00
|
|
|
#include "flog.h"
|
2015-07-25 15:14:25 +00:00
|
|
|
#include "function.h"
|
2016-04-21 06:00:54 +00:00
|
|
|
#include "io.h"
|
2020-07-19 23:41:58 +00:00
|
|
|
#include "job_group.h"
|
2017-09-09 04:14:26 +00:00
|
|
|
#include "maybe.h"
|
2016-04-21 06:00:54 +00:00
|
|
|
#include "parse_constants.h"
|
2016-05-02 19:31:33 +00:00
|
|
|
#include "parse_util.h"
|
|
|
|
#include "parser.h"
|
|
|
|
#include "path.h"
|
2016-04-21 06:00:54 +00:00
|
|
|
#include "proc.h"
|
2016-05-02 19:31:33 +00:00
|
|
|
#include "reader.h"
|
2019-12-20 04:41:53 +00:00
|
|
|
#include "timer.h"
|
2018-01-20 21:14:29 +00:00
|
|
|
#include "tokenizer.h"
|
2019-10-19 01:08:22 +00:00
|
|
|
#include "trace.h"
|
2016-05-02 19:31:33 +00:00
|
|
|
#include "util.h"
|
|
|
|
#include "wildcard.h"
|
|
|
|
#include "wutil.h"
|
2013-12-24 21:17:24 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
/// These are the specific statement types that support redirections.
|
2020-07-03 18:16:51 +00:00
|
|
|
static constexpr bool type_is_redirectable_block(ast::type_t type) {
|
|
|
|
using t = ast::type_t;
|
|
|
|
return type == t::block_statement || type == t::if_statement || type == t::switch_statement;
|
2018-01-22 20:22:42 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 18:16:51 +00:00
|
|
|
static bool specific_statement_type_is_redirectable_block(const ast::node_t &node) {
|
2018-01-22 20:22:42 +00:00
|
|
|
return type_is_redirectable_block(node.type);
|
2014-01-07 18:45:36 +00:00
|
|
|
}
|
2013-12-24 21:17:24 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
/// Get the name of a redirectable block, for profiling purposes.
|
2020-07-03 18:16:51 +00:00
|
|
|
static wcstring profiling_cmd_name_for_redirectable_block(const ast::node_t &node,
|
|
|
|
const parsed_source_t &pstree) {
|
|
|
|
using namespace ast;
|
2014-02-09 22:04:43 +00:00
|
|
|
assert(specific_statement_type_is_redirectable_block(node));
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2020-07-03 18:16:51 +00:00
|
|
|
auto source_range = node.try_source_range();
|
|
|
|
assert(source_range.has_value() && "No source range for block");
|
|
|
|
|
|
|
|
size_t src_end = 0;
|
|
|
|
switch (node.type) {
|
|
|
|
case type_t::block_statement: {
|
|
|
|
const node_t *block_header = node.as<block_statement_t>()->header.get();
|
|
|
|
switch (block_header->type) {
|
|
|
|
case type_t::for_header:
|
|
|
|
src_end = block_header->as<for_header_t>()->semi_nl.source_range().start;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case type_t::while_header:
|
|
|
|
src_end = block_header->as<while_header_t>()->condition.source_range().end();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case type_t::function_header:
|
|
|
|
src_end = block_header->as<function_header_t>()->semi_nl.source_range().start;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case type_t::begin_header:
|
|
|
|
src_end = block_header->as<begin_header_t>()->kw_begin.source_range().end();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
DIE("Unexpected block header type");
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case type_t::if_statement:
|
2020-07-22 22:35:14 +00:00
|
|
|
src_end = node.as<if_statement_t>()->if_clause.condition.job.source_range().end();
|
2020-07-03 18:16:51 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case type_t::switch_statement:
|
|
|
|
src_end = node.as<switch_statement_t>()->semi_nl.source_range().start;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
DIE("Not a redirectable block type");
|
|
|
|
break;
|
|
|
|
}
|
2014-02-09 22:04:43 +00:00
|
|
|
|
2020-07-03 18:16:51 +00:00
|
|
|
assert(src_end >= source_range->start && "Invalid source end");
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2020-07-03 18:16:51 +00:00
|
|
|
// Get the source for the block, and cut it at the next statement terminator.
|
|
|
|
wcstring result = pstree.src.substr(source_range->start, src_end - source_range->start);
|
2014-02-09 22:04:43 +00:00
|
|
|
result.append(L"...");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-10-14 22:45:40 +00:00
|
|
|
/// Get a redirection from stderr to stdout (i.e. 2>&1).
|
2019-12-13 00:44:24 +00:00
|
|
|
static redirection_spec_t get_stderr_merge() {
|
|
|
|
const wchar_t *stdout_fileno_str = L"1";
|
|
|
|
return redirection_spec_t{STDERR_FILENO, redirection_mode_t::fd, stdout_fileno_str};
|
2019-10-14 22:45:40 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 19:10:41 +00:00
|
|
|
parse_execution_context_t::parse_execution_context_t(parsed_source_ref_t pstree,
|
2020-01-16 01:14:47 +00:00
|
|
|
const operation_context_t &ctx,
|
Implement cancel groups
This concerns how "internal job groups" know to stop executing when an
external command receives a "cancel signal" (SIGINT or SIGQUIT). For
example:
while true
sleep 1
end
The intent is that if any 'sleep' exits from a cancel signal, then so would
the while loop. This is why you can hit control-C to end the loop even
if the SIGINT is delivered to sleep and not fish.
Here the 'while' loop is considered an "internal job group" (no separate
pgid, bash would not fork) while each 'sleep' is a separate external
command with its own job group, pgroup, etc. Prior to this change, after
running each 'sleep', parse_execution_context_t would check to see if its
exit status was a cancel signal, and if so, stash it into an int that the
cancel checker would check. But this became unwieldy: now there were three
sources of cancellation signals (that int, the job group, and fish itself).
Introduce the notion of a "cancellation group" which is a set of job
groups that should cancel together. Even though the while loop and sleep
are in different job groups, they are in the same cancellation group. When
any job gets a SIGINT or SIGQUIT, it marks that signal in its cancellation
group, which prevents running new jobs in that group.
This reduces the number of signals to check from 3 to 2; eventually we can
teach cancellation groups how to check fish's own signals and then it will
just be 1.
2020-09-02 22:06:05 +00:00
|
|
|
cancellation_group_ref_t cancel_group,
|
2020-05-29 19:10:41 +00:00
|
|
|
io_chain_t block_io)
|
|
|
|
: pstree(std::move(pstree)),
|
|
|
|
parser(ctx.parser.get()),
|
|
|
|
ctx(ctx),
|
Implement cancel groups
This concerns how "internal job groups" know to stop executing when an
external command receives a "cancel signal" (SIGINT or SIGQUIT). For
example:
while true
sleep 1
end
The intent is that if any 'sleep' exits from a cancel signal, then so would
the while loop. This is why you can hit control-C to end the loop even
if the SIGINT is delivered to sleep and not fish.
Here the 'while' loop is considered an "internal job group" (no separate
pgid, bash would not fork) while each 'sleep' is a separate external
command with its own job group, pgroup, etc. Prior to this change, after
running each 'sleep', parse_execution_context_t would check to see if its
exit status was a cancel signal, and if so, stash it into an int that the
cancel checker would check. But this became unwieldy: now there were three
sources of cancellation signals (that int, the job group, and fish itself).
Introduce the notion of a "cancellation group" which is a set of job
groups that should cancel together. Even though the while loop and sleep
are in different job groups, they are in the same cancellation group. When
any job gets a SIGINT or SIGQUIT, it marks that signal in its cancellation
group, which prevents running new jobs in that group.
This reduces the number of signals to check from 3 to 2; eventually we can
teach cancellation groups how to check fish's own signals and then it will
just be 1.
2020-09-02 22:06:05 +00:00
|
|
|
cancel_group(std::move(cancel_group)),
|
2020-05-29 19:10:41 +00:00
|
|
|
block_io(std::move(block_io)) {}
|
2013-12-24 21:17:24 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// Utilities
|
2013-12-24 21:17:24 +00:00
|
|
|
|
2020-07-03 18:16:51 +00:00
|
|
|
wcstring parse_execution_context_t::get_source(const ast::node_t &node) const {
|
|
|
|
return node.source(pstree->src);
|
2013-12-24 21:17:24 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 18:16:51 +00:00
|
|
|
const ast::decorated_statement_t *
|
|
|
|
parse_execution_context_t::infinite_recursive_statement_in_job_list(const ast::job_list_t &jobs,
|
|
|
|
wcstring *out_func_name) const {
|
2016-05-02 19:31:33 +00: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-01 23:29:56 +00:00
|
|
|
const block_t *current = parser->block_at_index(0), *parent = parser->block_at_index(1);
|
2016-05-02 19:31:33 +00:00
|
|
|
bool is_within_function_call =
|
2019-12-22 23:37:14 +00:00
|
|
|
(current && parent && current->type() == block_type_t::top && parent->is_function_call());
|
2016-05-02 19:31:33 +00:00
|
|
|
if (!is_within_function_call) {
|
2020-07-03 18:16:51 +00:00
|
|
|
return nullptr;
|
2014-01-01 23:29:56 +00:00
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2019-11-10 20:36:46 +00:00
|
|
|
// Get the function name of the immediate block.
|
|
|
|
const wcstring &forbidden_function_name = parent->function_name;
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// Get the first job in the job list.
|
2020-07-03 18:16:51 +00:00
|
|
|
const ast::job_conjunction_t *jc = jobs.at(0);
|
|
|
|
if (!jc) return nullptr;
|
|
|
|
const ast::job_t *job = &jc->job;
|
|
|
|
|
|
|
|
// Helper to return if a statement is infinitely recursive in this function.
|
|
|
|
auto statement_recurses =
|
|
|
|
[&](const ast::statement_t &stat) -> const ast::decorated_statement_t * {
|
|
|
|
// Ignore non-decorated statements like `if`, etc.
|
|
|
|
const ast::decorated_statement_t *dc =
|
|
|
|
stat.contents.contents->try_as<ast::decorated_statement_t>();
|
|
|
|
if (!dc) return nullptr;
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2019-12-21 10:45:07 +00:00
|
|
|
// Ignore statements with decorations like 'builtin' or 'command', since those
|
|
|
|
// are not infinite recursion. In particular that is what enables 'wrapper functions'.
|
2020-07-07 23:28:39 +00:00
|
|
|
if (dc->decoration() != statement_decoration_t::none) return nullptr;
|
2020-07-03 18:16:51 +00:00
|
|
|
|
|
|
|
// Check the command.
|
|
|
|
wcstring cmd = dc->command.source(pstree->src);
|
|
|
|
bool forbidden =
|
|
|
|
!cmd.empty() &&
|
|
|
|
expand_one(cmd, {expand_flag::skip_cmdsubst, expand_flag::skip_variables}, ctx) &&
|
|
|
|
cmd == forbidden_function_name;
|
|
|
|
return forbidden ? dc : nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
const ast::decorated_statement_t *infinite_recursive_statement = nullptr;
|
|
|
|
|
|
|
|
// Check main statement.
|
|
|
|
infinite_recursive_statement = statement_recurses(jc->job.statement);
|
|
|
|
|
|
|
|
// Check piped remainder.
|
|
|
|
if (!infinite_recursive_statement) {
|
|
|
|
for (const ast::job_continuation_t &c : job->continuation) {
|
|
|
|
if (const auto *s = statement_recurses(c.statement)) {
|
|
|
|
infinite_recursive_statement = s;
|
2018-01-16 00:33:36 +00:00
|
|
|
break;
|
2014-01-01 23:29:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-16 00:33:36 +00:00
|
|
|
|
2020-07-03 18:16:51 +00:00
|
|
|
if (infinite_recursive_statement && out_func_name) {
|
|
|
|
*out_func_name = forbidden_function_name;
|
|
|
|
}
|
|
|
|
// may be null
|
2014-01-01 23:29:56 +00:00
|
|
|
return infinite_recursive_statement;
|
|
|
|
}
|
|
|
|
|
2019-03-24 19:29:25 +00:00
|
|
|
process_type_t parse_execution_context_t::process_type_for_command(
|
2020-07-03 18:16:51 +00:00
|
|
|
const ast::decorated_statement_t &statement, const wcstring &cmd) const {
|
2019-03-24 19:29:25 +00:00
|
|
|
enum process_type_t process_type = process_type_t::external;
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// Determine the process type, which depends on the statement decoration (command, builtin,
|
|
|
|
// etc).
|
2020-07-03 18:16:51 +00:00
|
|
|
switch (statement.decoration()) {
|
2020-07-07 23:28:39 +00:00
|
|
|
case statement_decoration_t::exec:
|
2019-04-11 03:38:19 +00:00
|
|
|
process_type = process_type_t::exec;
|
|
|
|
break;
|
2020-07-07 23:28:39 +00:00
|
|
|
case statement_decoration_t::command:
|
2019-04-11 03:38:19 +00:00
|
|
|
process_type = process_type_t::external;
|
|
|
|
break;
|
2020-07-07 23:28:39 +00:00
|
|
|
case statement_decoration_t::builtin:
|
2019-04-11 03:38:19 +00:00
|
|
|
process_type = process_type_t::builtin;
|
|
|
|
break;
|
2020-07-07 23:28:39 +00:00
|
|
|
case statement_decoration_t::none:
|
2019-05-05 03:20:52 +00:00
|
|
|
if (function_exists(cmd, *parser)) {
|
2019-04-11 03:38:19 +00:00
|
|
|
process_type = process_type_t::function;
|
|
|
|
} else if (builtin_exists(cmd)) {
|
|
|
|
process_type = process_type_t::builtin;
|
|
|
|
} else {
|
|
|
|
process_type = process_type_t::external;
|
|
|
|
}
|
|
|
|
break;
|
2013-12-31 22:37:37 +00:00
|
|
|
}
|
2019-04-11 03:38:19 +00:00
|
|
|
|
2013-12-31 22:37:37 +00:00
|
|
|
return process_type;
|
|
|
|
}
|
2013-12-24 21:17:24 +00:00
|
|
|
|
2020-01-24 00:45:00 +00:00
|
|
|
maybe_t<end_execution_reason_t> parse_execution_context_t::check_end_execution() const {
|
Implement cancel groups
This concerns how "internal job groups" know to stop executing when an
external command receives a "cancel signal" (SIGINT or SIGQUIT). For
example:
while true
sleep 1
end
The intent is that if any 'sleep' exits from a cancel signal, then so would
the while loop. This is why you can hit control-C to end the loop even
if the SIGINT is delivered to sleep and not fish.
Here the 'while' loop is considered an "internal job group" (no separate
pgid, bash would not fork) while each 'sleep' is a separate external
command with its own job group, pgroup, etc. Prior to this change, after
running each 'sleep', parse_execution_context_t would check to see if its
exit status was a cancel signal, and if so, stash it into an int that the
cancel checker would check. But this became unwieldy: now there were three
sources of cancellation signals (that int, the job group, and fish itself).
Introduce the notion of a "cancellation group" which is a set of job
groups that should cancel together. Even though the while loop and sleep
are in different job groups, they are in the same cancellation group. When
any job gets a SIGINT or SIGQUIT, it marks that signal in its cancellation
group, which prevents running new jobs in that group.
This reduces the number of signals to check from 3 to 2; eventually we can
teach cancellation groups how to check fish's own signals and then it will
just be 1.
2020-09-02 22:06:05 +00:00
|
|
|
if (ctx.check_cancel() || check_cancel_from_fish_signal()) {
|
2020-01-24 00:45:00 +00:00
|
|
|
return end_execution_reason_t::cancelled;
|
2016-05-04 22:19:47 +00:00
|
|
|
}
|
2019-05-20 02:29:25 +00:00
|
|
|
const auto &ld = parser->libdata();
|
2020-08-15 21:41:11 +00:00
|
|
|
if (ld.exit_current_script) {
|
|
|
|
return end_execution_reason_t::cancelled;
|
|
|
|
}
|
2019-05-20 02:29:25 +00:00
|
|
|
if (ld.returning) {
|
2020-01-24 00:45:00 +00:00
|
|
|
return end_execution_reason_t::control_flow;
|
2013-12-30 00:23:26 +00:00
|
|
|
}
|
2019-05-20 02:29:25 +00:00
|
|
|
if (ld.loop_status != loop_status_t::normals) {
|
2020-01-24 00:45:00 +00:00
|
|
|
return end_execution_reason_t::control_flow;
|
2017-01-21 22:15:03 +00:00
|
|
|
}
|
2019-12-18 02:10:29 +00:00
|
|
|
return none();
|
2013-12-24 21:17:24 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
/// Return whether the job contains a single statement, of block type, with no redirections.
|
2020-07-03 18:16:51 +00:00
|
|
|
bool parse_execution_context_t::job_is_simple_block(const ast::job_t &job) const {
|
|
|
|
using namespace ast;
|
2016-05-02 19:31:33 +00:00
|
|
|
// Must be no pipes.
|
2020-07-03 18:16:51 +00:00
|
|
|
if (!job.continuation.empty()) {
|
2014-01-07 18:45:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2020-07-03 18:16:51 +00:00
|
|
|
// Helper to check if an argument_or_redirection_list_t has no redirections.
|
|
|
|
auto no_redirs = [](const argument_or_redirection_list_t &list) -> bool {
|
|
|
|
for (const argument_or_redirection_t &val : list) {
|
|
|
|
if (val.is_redirection()) return false;
|
|
|
|
}
|
|
|
|
return true;
|
2018-01-22 20:22:42 +00:00
|
|
|
};
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2018-01-22 20:22:42 +00: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).
|
2020-07-03 18:16:51 +00:00
|
|
|
const node_t &ss = *job.statement.contents.contents;
|
|
|
|
switch (ss.type) {
|
|
|
|
case type_t::block_statement:
|
|
|
|
return no_redirs(ss.as<block_statement_t>()->args_or_redirs);
|
|
|
|
case type_t::switch_statement:
|
|
|
|
return no_redirs(ss.as<switch_statement_t>()->args_or_redirs);
|
|
|
|
case type_t::if_statement:
|
|
|
|
return no_redirs(ss.as<if_statement_t>()->args_or_redirs);
|
|
|
|
case type_t::not_statement:
|
|
|
|
case type_t::decorated_statement:
|
2018-01-22 20:22:42 +00:00
|
|
|
// not block statements
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
assert(0 && "Unexpected child block type");
|
|
|
|
return false;
|
|
|
|
}
|
2014-01-07 18:45:36 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 00:45:00 +00:00
|
|
|
end_execution_reason_t parse_execution_context_t::run_if_statement(
|
2020-07-03 18:16:51 +00:00
|
|
|
const ast::if_statement_t &statement, const block_t *associated_block) {
|
|
|
|
using namespace ast;
|
|
|
|
using job_list_t = ast::job_list_t;
|
2020-01-24 00:45:00 +00:00
|
|
|
end_execution_reason_t result = end_execution_reason_t::ok;
|
2013-12-27 09:38:43 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// We have a sequence of if clauses, with a final else, resulting in a single job list that we
|
|
|
|
// execute.
|
2020-07-03 18:16:51 +00:00
|
|
|
const job_list_t *job_list_to_execute = nullptr;
|
|
|
|
const if_clause_t *if_clause = &statement.if_clause;
|
|
|
|
|
|
|
|
// Index of the *next* elseif_clause to test.
|
|
|
|
const elseif_clause_list_t &elseif_clauses = statement.elseif_clauses;
|
|
|
|
size_t next_elseif_idx = 0;
|
2019-10-19 01:08:22 +00:00
|
|
|
|
|
|
|
// We start with the 'if'.
|
|
|
|
trace_if_enabled(*parser, L"if");
|
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
for (;;) {
|
2019-12-18 02:10:29 +00:00
|
|
|
if (auto ret = check_end_execution()) {
|
|
|
|
result = *ret;
|
2013-12-31 22:37:37 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-05-02 19:31:33 +00:00
|
|
|
|
|
|
|
// An if condition has a job and a "tail" of andor jobs, e.g. "foo ; and bar; or baz".
|
2020-01-24 01:34:46 +00:00
|
|
|
// Check the condition and the tail. We treat end_execution_reason_t::error here as failure,
|
|
|
|
// in accordance with historic behavior.
|
2020-07-03 18:16:51 +00:00
|
|
|
end_execution_reason_t cond_ret =
|
|
|
|
run_job_conjunction(if_clause->condition, associated_block);
|
2020-01-24 00:45:00 +00:00
|
|
|
if (cond_ret == end_execution_reason_t::ok) {
|
2020-07-03 18:16:51 +00:00
|
|
|
cond_ret = run_job_list(if_clause->andor_tail, associated_block);
|
2015-12-19 22:45:45 +00:00
|
|
|
}
|
2016-05-02 19:31:33 +00:00
|
|
|
const bool take_branch =
|
2020-01-24 00:45:00 +00:00
|
|
|
(cond_ret == end_execution_reason_t::ok) && parser->get_last_status() == EXIT_SUCCESS;
|
2013-12-31 22:37:37 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
if (take_branch) {
|
|
|
|
// Condition succeeded.
|
2020-07-03 18:16:51 +00:00
|
|
|
job_list_to_execute = &if_clause->body;
|
2013-12-27 09:38:43 +00:00
|
|
|
break;
|
2018-01-14 10:02:02 +00:00
|
|
|
}
|
2020-07-03 18:16:51 +00:00
|
|
|
|
|
|
|
// See if we have an elseif.
|
|
|
|
const auto *elseif_clause = elseif_clauses.at(next_elseif_idx++);
|
|
|
|
if (elseif_clause) {
|
|
|
|
trace_if_enabled(*parser, L"else if");
|
|
|
|
if_clause = &elseif_clause->if_clause;
|
2016-05-02 19:31:33 +00:00
|
|
|
} else {
|
2020-07-03 18:16:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!job_list_to_execute) {
|
|
|
|
// our ifs and elseifs failed.
|
|
|
|
// Check our else body.
|
|
|
|
if (statement.else_clause) {
|
|
|
|
trace_if_enabled(*parser, L"else");
|
|
|
|
job_list_to_execute = &statement.else_clause->body;
|
2013-12-27 09:38:43 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2020-07-03 18:16:51 +00:00
|
|
|
if (!job_list_to_execute) {
|
|
|
|
// 'if' condition failed, no else clause, return 0, we're done.
|
|
|
|
// No job list means no successful conditions, so return 0 (issue #1443).
|
|
|
|
parser->set_last_statuses(statuses_t::just(STATUS_CMD_OK));
|
|
|
|
} else {
|
|
|
|
// Execute the job list we got.
|
2019-05-19 21:40:06 +00:00
|
|
|
block_t *ib = parser->push_block(block_t::if_block());
|
2020-07-03 18:16:51 +00:00
|
|
|
run_job_list(*job_list_to_execute, ib);
|
2019-12-18 02:10:29 +00:00
|
|
|
if (auto ret = check_end_execution()) {
|
|
|
|
result = *ret;
|
2018-03-31 21:57:24 +00:00
|
|
|
}
|
|
|
|
parser->pop_block(ib);
|
2015-01-21 07:44:49 +00:00
|
|
|
}
|
2019-10-19 01:08:22 +00:00
|
|
|
trace_if_enabled(*parser, L"end if");
|
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// It's possible there's a last-minute cancellation (issue #1297).
|
2019-12-18 02:10:29 +00:00
|
|
|
if (auto ret = check_end_execution()) {
|
|
|
|
result = *ret;
|
2014-02-12 09:39:06 +00:00
|
|
|
}
|
2013-12-27 09:38:43 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// Otherwise, take the exit status of the job list. Reversal of issue #1061.
|
2013-12-31 22:37:37 +00:00
|
|
|
return result;
|
2013-12-27 09:38:43 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 00:45:00 +00:00
|
|
|
end_execution_reason_t parse_execution_context_t::run_begin_statement(
|
2020-07-03 18:16:51 +00:00
|
|
|
const ast::job_list_t &contents) {
|
2017-01-21 23:35:35 +00:00
|
|
|
// Basic begin/end block. Push a scope block, run jobs, pop it
|
2019-10-19 01:08:22 +00:00
|
|
|
trace_if_enabled(*parser, L"begin");
|
2019-12-22 23:37:14 +00:00
|
|
|
block_t *sb = parser->push_block(block_t::scope_block(block_type_t::begin));
|
2020-01-24 00:45:00 +00:00
|
|
|
end_execution_reason_t ret = run_job_list(contents, sb);
|
2013-12-27 09:38:43 +00:00
|
|
|
parser->pop_block(sb);
|
2019-10-19 01:08:22 +00:00
|
|
|
trace_if_enabled(*parser, L"end begin");
|
2013-12-31 22:37:37 +00:00
|
|
|
return ret;
|
2014-01-15 09:40:40 +00:00
|
|
|
}
|
2013-12-27 09:38:43 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// Define a function.
|
2020-01-24 00:45:00 +00:00
|
|
|
end_execution_reason_t parse_execution_context_t::run_function_statement(
|
2020-07-03 18:16:51 +00:00
|
|
|
const ast::block_statement_t &statement, const ast::function_header_t &header) {
|
|
|
|
using namespace ast;
|
2016-05-02 19:31:33 +00:00
|
|
|
// Get arguments.
|
2018-01-15 23:15:45 +00:00
|
|
|
wcstring_list_t arguments;
|
2020-07-03 18:16:51 +00:00
|
|
|
ast_args_list_t arg_nodes = get_argument_nodes(header.args);
|
|
|
|
arg_nodes.insert(arg_nodes.begin(), &header.first_arg);
|
2020-01-24 00:45:00 +00:00
|
|
|
end_execution_reason_t result =
|
|
|
|
this->expand_arguments_from_nodes(arg_nodes, &arguments, failglob);
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2020-01-24 00:45:00 +00:00
|
|
|
if (result != end_execution_reason_t::ok) {
|
2016-10-31 03:26:10 +00:00
|
|
|
return result;
|
|
|
|
}
|
2019-10-19 01:08:22 +00:00
|
|
|
trace_if_enabled(*parser, L"function", arguments);
|
2021-02-04 22:12:14 +00:00
|
|
|
null_output_stream_t outs;
|
|
|
|
string_output_stream_t errs;
|
2020-07-29 23:03:29 +00:00
|
|
|
io_streams_t streams(outs, errs);
|
2020-07-18 17:25:43 +00:00
|
|
|
int err_code = 0;
|
|
|
|
maybe_t<int> err = builtin_function(*parser, streams, arguments, pstree, statement);
|
|
|
|
if (err) {
|
|
|
|
err_code = err.value();
|
|
|
|
parser->libdata().status_count++;
|
|
|
|
parser->set_last_statuses(statuses_t::just(err_code));
|
|
|
|
}
|
2016-10-31 03:26:10 +00:00
|
|
|
|
2021-02-04 22:12:14 +00:00
|
|
|
const wcstring &errtext = errs.contents();
|
2019-07-21 20:53:05 +00:00
|
|
|
if (!errtext.empty()) {
|
2020-07-18 17:25:43 +00:00
|
|
|
return this->report_error(err_code, header, L"%ls", errtext.c_str());
|
2013-12-27 09:38:43 +00:00
|
|
|
}
|
2013-12-31 22:37:37 +00:00
|
|
|
return result;
|
2013-12-27 09:38:43 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 00:45:00 +00:00
|
|
|
end_execution_reason_t parse_execution_context_t::run_block_statement(
|
2020-07-03 18:16:51 +00:00
|
|
|
const ast::block_statement_t &statement, const block_t *associated_block) {
|
|
|
|
const ast::node_t &bh = *statement.header.contents;
|
|
|
|
const ast::job_list_t &contents = statement.jobs;
|
2020-01-24 00:45:00 +00:00
|
|
|
end_execution_reason_t ret = end_execution_reason_t::ok;
|
2020-07-03 18:16:51 +00:00
|
|
|
if (const auto *fh = bh.try_as<ast::for_header_t>()) {
|
|
|
|
ret = run_for_statement(*fh, contents);
|
|
|
|
} else if (const auto *wh = bh.try_as<ast::while_header_t>()) {
|
|
|
|
ret = run_while_statement(*wh, contents, associated_block);
|
|
|
|
} else if (const auto *fh = bh.try_as<ast::function_header_t>()) {
|
|
|
|
ret = run_function_statement(statement, *fh);
|
|
|
|
} else if (bh.try_as<ast::begin_header_t>()) {
|
2018-01-20 22:05:34 +00:00
|
|
|
ret = run_begin_statement(contents);
|
2018-01-14 09:42:58 +00:00
|
|
|
} else {
|
2020-07-03 18:16:51 +00:00
|
|
|
FLOGF(error, L"Unexpected block header: %ls\n", bh.describe().c_str());
|
2018-01-14 09:42:58 +00:00
|
|
|
PARSER_DIE();
|
2013-12-27 09:38:43 +00:00
|
|
|
}
|
2013-12-31 22:37:37 +00:00
|
|
|
return ret;
|
2013-12-27 09:38:43 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 00:45:00 +00:00
|
|
|
end_execution_reason_t parse_execution_context_t::run_for_statement(
|
2020-07-03 18:16:51 +00:00
|
|
|
const ast::for_header_t &header, const ast::job_list_t &block_contents) {
|
2016-05-02 19:31:33 +00:00
|
|
|
// Get the variable name: `for var_name in ...`. We expand the variable name. It better result
|
|
|
|
// in just one.
|
2020-07-03 18:16:51 +00:00
|
|
|
wcstring for_var_name = header.var_name.source(get_source());
|
2020-01-16 01:14:47 +00:00
|
|
|
if (!expand_one(for_var_name, expand_flags_t{}, ctx)) {
|
2020-07-03 18:16:51 +00:00
|
|
|
return report_error(STATUS_EXPAND_ERROR, header.var_name,
|
2020-01-24 01:34:46 +00:00
|
|
|
FAILED_EXPANSION_VARIABLE_NAME_ERR_MSG, for_var_name.c_str());
|
2014-02-22 02:20:51 +00:00
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// Get the contents to iterate over.
|
2018-01-15 23:15:45 +00:00
|
|
|
wcstring_list_t arguments;
|
2020-07-03 18:16:51 +00:00
|
|
|
ast_args_list_t arg_nodes = get_argument_nodes(header.args);
|
|
|
|
end_execution_reason_t ret = this->expand_arguments_from_nodes(arg_nodes, &arguments, nullglob);
|
2020-01-24 00:45:00 +00:00
|
|
|
if (ret != end_execution_reason_t::ok) {
|
2014-05-31 19:41:27 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2020-01-08 08:03:34 +00:00
|
|
|
auto var = parser->vars().get(for_var_name, ENV_DEFAULT);
|
|
|
|
if (var && var->read_only()) {
|
2020-07-03 18:16:51 +00:00
|
|
|
return report_error(STATUS_INVALID_ARGS, header.var_name,
|
2020-01-24 01:34:46 +00:00
|
|
|
L"You cannot use read-only variable '%ls' in a for loop",
|
|
|
|
for_var_name.c_str());
|
2020-01-08 08:03:34 +00:00
|
|
|
}
|
|
|
|
int retval;
|
|
|
|
if (var) {
|
2020-03-08 03:44:58 +00:00
|
|
|
retval = parser->set_var_and_fire(for_var_name, ENV_LOCAL | ENV_USER, var->as_list());
|
2020-01-08 08:03:34 +00:00
|
|
|
} else {
|
2020-03-08 03:44:58 +00:00
|
|
|
retval = parser->set_empty_var_and_fire(for_var_name, ENV_LOCAL | ENV_USER);
|
2017-09-09 04:14:26 +00:00
|
|
|
}
|
2020-01-08 08:03:34 +00:00
|
|
|
assert(retval == ENV_OK);
|
2017-09-09 04:14:26 +00:00
|
|
|
|
2019-04-08 18:22:21 +00:00
|
|
|
if (!valid_var_name(for_var_name)) {
|
2020-07-03 18:16:51 +00:00
|
|
|
return report_error(STATUS_INVALID_ARGS, header.var_name, BUILTIN_ERR_VARNAME, L"for",
|
2020-01-24 01:34:46 +00:00
|
|
|
for_var_name.c_str());
|
2019-04-08 18:22:21 +00:00
|
|
|
}
|
|
|
|
|
2019-10-19 01:08:22 +00:00
|
|
|
trace_if_enabled(*parser, L"for", arguments);
|
2019-05-19 21:40:06 +00:00
|
|
|
block_t *fb = parser->push_block(block_t::for_block());
|
2013-12-27 09:38:43 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// Now drive the for loop.
|
2018-01-15 23:15:45 +00:00
|
|
|
for (const wcstring &val : arguments) {
|
2019-12-18 02:10:29 +00:00
|
|
|
if (auto reason = check_end_execution()) {
|
|
|
|
ret = *reason;
|
2014-01-01 08:04:02 +00:00
|
|
|
break;
|
2013-12-31 22:37:37 +00:00
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2020-03-08 03:44:58 +00:00
|
|
|
int retval = parser->set_var_and_fire(for_var_name, ENV_DEFAULT | ENV_USER, val);
|
2017-12-21 21:53:18 +00:00
|
|
|
assert(retval == ENV_OK && "for loop variable should have been successfully set");
|
|
|
|
(void)retval;
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2019-05-19 06:12:34 +00:00
|
|
|
auto &ld = parser->libdata();
|
|
|
|
ld.loop_status = loop_status_t::normals;
|
2013-12-29 00:18:38 +00:00
|
|
|
this->run_job_list(block_contents, fb);
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2020-01-24 00:45:00 +00:00
|
|
|
if (check_end_execution() == end_execution_reason_t::control_flow) {
|
2016-05-02 19:31:33 +00:00
|
|
|
// Handle break or continue.
|
2019-05-19 06:12:34 +00:00
|
|
|
bool do_break = (ld.loop_status == loop_status_t::breaks);
|
|
|
|
ld.loop_status = loop_status_t::normals;
|
|
|
|
if (do_break) {
|
2013-12-30 00:23:26 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-12-29 06:52:06 +00:00
|
|
|
}
|
2013-12-27 09:38:43 +00:00
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2014-01-07 18:45:36 +00:00
|
|
|
parser->pop_block(fb);
|
2019-10-19 01:08:22 +00:00
|
|
|
trace_if_enabled(*parser, L"end for");
|
2013-12-31 22:37:37 +00:00
|
|
|
return ret;
|
2013-12-27 09:38:43 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 00:45:00 +00:00
|
|
|
end_execution_reason_t parse_execution_context_t::run_switch_statement(
|
2020-07-03 18:16:51 +00:00
|
|
|
const ast::switch_statement_t &statement) {
|
2016-05-02 19:31:33 +00:00
|
|
|
// Get the switch variable.
|
2020-07-03 18:16:51 +00:00
|
|
|
const wcstring switch_value = get_source(statement.argument);
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// Expand it. We need to offset any errors by the position of the string.
|
2020-01-16 00:13:41 +00:00
|
|
|
completion_list_t switch_values_expanded;
|
2014-03-22 00:13:33 +00:00
|
|
|
parse_error_list_t errors;
|
2020-09-27 23:49:12 +00:00
|
|
|
auto expand_ret =
|
|
|
|
expand_string(switch_value, &switch_values_expanded, expand_flags_t{}, ctx, &errors);
|
2020-07-03 18:16:51 +00:00
|
|
|
parse_error_offset_source_start(&errors, statement.argument.range.start);
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2020-01-24 01:34:46 +00:00
|
|
|
switch (expand_ret.result) {
|
2020-01-22 19:46:02 +00:00
|
|
|
case expand_result_t::error:
|
2020-01-24 01:34:46 +00:00
|
|
|
return report_errors(expand_ret.status, errors);
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2020-01-22 19:58:10 +00:00
|
|
|
case expand_result_t::cancel:
|
2020-01-24 00:45:00 +00:00
|
|
|
return end_execution_reason_t::cancelled;
|
2020-01-22 19:58:10 +00:00
|
|
|
|
2020-01-22 19:46:02 +00:00
|
|
|
case expand_result_t::wildcard_no_match:
|
2020-07-03 18:16:51 +00:00
|
|
|
return report_error(STATUS_UNMATCHED_WILDCARD, statement.argument, WILDCARD_ERR_MSG,
|
|
|
|
get_source(statement.argument).c_str());
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2020-01-22 19:46:02 +00:00
|
|
|
case expand_result_t::ok:
|
|
|
|
if (switch_values_expanded.size() > 1) {
|
2020-07-03 18:16:51 +00:00
|
|
|
return report_error(STATUS_INVALID_ARGS, statement.argument,
|
2020-01-22 19:46:02 +00:00
|
|
|
_(L"switch: Expected at most one argument, got %lu\n"),
|
|
|
|
switch_values_expanded.size());
|
|
|
|
}
|
|
|
|
break;
|
2016-10-31 03:26:10 +00:00
|
|
|
}
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2020-01-22 19:46:02 +00:00
|
|
|
// If we expanded to nothing, match the empty string.
|
|
|
|
assert(switch_values_expanded.size() <= 1 && "Should have at most one expansion");
|
2020-04-03 02:43:34 +00:00
|
|
|
wcstring switch_value_expanded;
|
2020-01-22 19:46:02 +00:00
|
|
|
if (!switch_values_expanded.empty()) {
|
|
|
|
switch_value_expanded = std::move(switch_values_expanded.front().completion);
|
|
|
|
}
|
2014-03-28 23:56:44 +00:00
|
|
|
|
2020-01-24 00:45:00 +00:00
|
|
|
end_execution_reason_t result = end_execution_reason_t::ok;
|
2020-08-13 21:21:03 +00:00
|
|
|
if (trace_enabled(*parser)) trace_argv(*parser, L"switch", {switch_value_expanded});
|
2019-05-19 21:40:06 +00:00
|
|
|
block_t *sb = parser->push_block(block_t::switch_block());
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-10-31 03:26:10 +00:00
|
|
|
// Expand case statements.
|
2020-07-03 18:16:51 +00:00
|
|
|
const ast::case_item_t *matching_case_item = nullptr;
|
|
|
|
for (const ast::case_item_t &case_item : statement.cases) {
|
2019-12-18 02:10:29 +00:00
|
|
|
if (auto ret = check_end_execution()) {
|
|
|
|
result = *ret;
|
2016-10-31 03:26:10 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-10-31 03:26:10 +00: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.
|
2020-07-03 18:16:51 +00:00
|
|
|
ast_args_list_t arg_nodes = get_argument_nodes(case_item.arguments);
|
2016-10-31 03:26:10 +00:00
|
|
|
wcstring_list_t case_args;
|
2020-01-24 00:45:00 +00:00
|
|
|
end_execution_reason_t case_result =
|
2018-01-15 23:15:45 +00:00
|
|
|
this->expand_arguments_from_nodes(arg_nodes, &case_args, failglob);
|
2020-01-24 00:45:00 +00:00
|
|
|
if (case_result == end_execution_reason_t::ok) {
|
2018-01-14 10:30:18 +00:00
|
|
|
for (const wcstring &arg : case_args) {
|
2016-10-31 03:26:10 +00: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) {
|
2020-07-03 18:16:51 +00:00
|
|
|
matching_case_item = &case_item;
|
2016-10-31 03:26:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-03-28 23:56:44 +00:00
|
|
|
}
|
2018-01-14 10:30:18 +00:00
|
|
|
if (matching_case_item) break;
|
2016-10-31 03:26:10 +00:00
|
|
|
}
|
2013-12-27 11:58:42 +00:00
|
|
|
|
2018-01-14 10:30:18 +00:00
|
|
|
if (matching_case_item) {
|
2016-10-31 03:26:10 +00:00
|
|
|
// Success, evaluate the job list.
|
2020-01-24 00:45:00 +00:00
|
|
|
assert(result == end_execution_reason_t::ok && "Expected success");
|
2020-07-03 18:16:51 +00:00
|
|
|
result = this->run_job_list(matching_case_item->body, sb);
|
2014-03-28 23:56:44 +00:00
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-10-31 03:26:10 +00:00
|
|
|
parser->pop_block(sb);
|
2020-08-13 21:21:03 +00:00
|
|
|
trace_if_enabled(*parser, L"end switch");
|
2013-12-31 22:37:37 +00:00
|
|
|
return result;
|
2013-12-27 09:38:43 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 00:45:00 +00:00
|
|
|
end_execution_reason_t parse_execution_context_t::run_while_statement(
|
2020-07-03 18:16:51 +00:00
|
|
|
const ast::while_header_t &header, const ast::job_list_t &contents,
|
2020-01-24 00:45:00 +00:00
|
|
|
const block_t *associated_block) {
|
|
|
|
end_execution_reason_t ret = end_execution_reason_t::ok;
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2019-01-21 00:37:20 +00:00
|
|
|
// "The exit status of the while loop shall be the exit status of the last compound-list-2
|
|
|
|
// executed, or zero if none was executed."
|
|
|
|
// Here are more detailed requirements:
|
|
|
|
// - If we execute the loop body zero times, or the loop body is empty, the status is success.
|
|
|
|
// - An empty loop body is treated as true, both in the loop condition and after loop exit.
|
|
|
|
// - The exit status of the last command is visible in the loop condition. (i.e. do not set the
|
|
|
|
// exit status to true BEFORE executing the loop condition).
|
|
|
|
// We achieve this by restoring the status if the loop condition fails, plus a special
|
|
|
|
// affordance for the first condition.
|
|
|
|
bool first_cond_check = true;
|
|
|
|
|
2019-10-19 01:08:22 +00:00
|
|
|
trace_if_enabled(*parser, L"while");
|
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// Run while the condition is true.
|
|
|
|
for (;;) {
|
2019-01-21 00:37:20 +00:00
|
|
|
// Save off the exit status if it came from the loop body. We'll restore it if the condition
|
|
|
|
// is false.
|
2019-02-25 09:21:32 +00:00
|
|
|
auto cond_saved_status =
|
2019-05-12 21:00:44 +00:00
|
|
|
first_cond_check ? statuses_t::just(EXIT_SUCCESS) : parser->get_last_statuses();
|
2019-01-21 00:37:20 +00:00
|
|
|
first_cond_check = false;
|
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// Check the condition.
|
2020-01-24 00:45:00 +00:00
|
|
|
end_execution_reason_t cond_ret =
|
2020-07-03 18:16:51 +00:00
|
|
|
this->run_job_conjunction(header.condition, associated_block);
|
2020-01-24 00:45:00 +00:00
|
|
|
if (cond_ret == end_execution_reason_t::ok) {
|
2020-07-03 18:16:51 +00:00
|
|
|
cond_ret = run_job_list(header.andor_tail, associated_block);
|
2015-12-19 22:45:45 +00:00
|
|
|
}
|
2016-05-02 19:31:33 +00:00
|
|
|
|
2019-01-21 00:37:20 +00:00
|
|
|
// If the loop condition failed to execute, then exit the loop without modifying the exit
|
|
|
|
// status. If the loop condition executed with a failure status, restore the status and then
|
|
|
|
// exit the loop.
|
2020-01-24 00:45:00 +00:00
|
|
|
if (cond_ret != end_execution_reason_t::ok) {
|
2019-01-21 00:37:20 +00:00
|
|
|
break;
|
2019-05-12 21:00:44 +00:00
|
|
|
} else if (parser->get_last_status() != EXIT_SUCCESS) {
|
|
|
|
parser->set_last_statuses(cond_saved_status);
|
2013-12-31 22:37:37 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// Check cancellation.
|
2019-12-18 02:10:29 +00:00
|
|
|
if (auto reason = check_end_execution()) {
|
|
|
|
ret = *reason;
|
2013-12-31 22:37:37 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2018-03-31 21:57:24 +00:00
|
|
|
// Push a while block and then check its cancellation reason.
|
2019-05-19 06:12:34 +00:00
|
|
|
auto &ld = parser->libdata();
|
|
|
|
ld.loop_status = loop_status_t::normals;
|
2019-10-19 01:08:22 +00:00
|
|
|
|
2019-05-19 21:40:06 +00:00
|
|
|
block_t *wb = parser->push_block(block_t::while_block());
|
2018-01-14 10:41:37 +00:00
|
|
|
this->run_job_list(contents, wb);
|
2019-12-18 02:10:29 +00:00
|
|
|
auto cancel_reason = this->check_end_execution();
|
2018-03-31 21:57:24 +00:00
|
|
|
parser->pop_block(wb);
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2020-01-24 00:45:00 +00:00
|
|
|
if (cancel_reason == end_execution_reason_t::control_flow) {
|
2016-05-02 19:31:33 +00:00
|
|
|
// Handle break or continue.
|
2019-05-24 23:09:10 +00:00
|
|
|
bool do_break = (ld.loop_status == loop_status_t::breaks);
|
|
|
|
ld.loop_status = loop_status_t::normals;
|
|
|
|
if (do_break) {
|
2013-12-30 00:23:26 +00:00
|
|
|
break;
|
2019-05-24 23:09:10 +00:00
|
|
|
} else {
|
|
|
|
continue;
|
2013-12-30 00:23:26 +00:00
|
|
|
}
|
2013-12-29 06:52:06 +00:00
|
|
|
}
|
2016-05-02 19:31:33 +00: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.
|
2019-05-12 22:04:18 +00:00
|
|
|
if (no_exec()) {
|
2014-07-11 18:28:10 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-12-26 20:24:00 +00:00
|
|
|
}
|
2019-10-19 01:08:22 +00:00
|
|
|
trace_if_enabled(*parser, L"end while");
|
2013-12-31 22:37:37 +00:00
|
|
|
return ret;
|
2013-12-24 21:17:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 01:34:46 +00:00
|
|
|
// Reports an error. Always returns end_execution_reason_t::error.
|
2020-07-03 18:16:51 +00:00
|
|
|
end_execution_reason_t parse_execution_context_t::report_error(int status, const ast::node_t &node,
|
2020-01-24 00:45:00 +00:00
|
|
|
const wchar_t *fmt, ...) const {
|
2020-07-03 18:16:51 +00:00
|
|
|
auto r = node.source_range();
|
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// Create an error.
|
2016-02-28 02:37:59 +00:00
|
|
|
parse_error_list_t error_list = parse_error_list_t(1);
|
|
|
|
parse_error_t *error = &error_list.at(0);
|
2020-07-03 18:16:51 +00:00
|
|
|
error->source_start = r.start;
|
|
|
|
error->source_length = r.length;
|
2016-05-02 19:31:33 +00:00
|
|
|
error->code = parse_error_syntax; // hackish
|
2016-02-28 02:37:59 +00:00
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start(va, fmt);
|
|
|
|
error->text = vformat_string(fmt, va);
|
|
|
|
va_end(va);
|
|
|
|
|
2020-01-24 01:34:46 +00:00
|
|
|
return this->report_errors(status, error_list);
|
2014-03-22 00:13:33 +00:00
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2020-01-24 00:45:00 +00:00
|
|
|
end_execution_reason_t parse_execution_context_t::report_errors(
|
2020-01-24 01:34:46 +00:00
|
|
|
int status, const parse_error_list_t &error_list) const {
|
2020-07-12 18:35:27 +00:00
|
|
|
if (!ctx.check_cancel()) {
|
2016-05-02 19:31:33 +00:00
|
|
|
if (error_list.empty()) {
|
2019-05-29 06:07:04 +00:00
|
|
|
FLOG(error, L"Error reported but no error text found.");
|
2014-03-22 00:13:33 +00:00
|
|
|
}
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// Get a backtrace.
|
2014-01-09 02:20:55 +00:00
|
|
|
wcstring backtrace_and_desc;
|
2017-12-22 22:40:15 +00:00
|
|
|
parser->get_backtrace(pstree->src, error_list, backtrace_and_desc);
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// Print it.
|
2017-01-03 05:11:53 +00:00
|
|
|
if (!should_suppress_stderr_for_tests()) {
|
2019-03-12 21:06:01 +00:00
|
|
|
std::fwprintf(stderr, L"%ls", backtrace_and_desc.c_str());
|
2017-01-03 05:11:53 +00:00
|
|
|
}
|
2013-12-24 21:17:24 +00:00
|
|
|
|
2020-01-24 01:34:46 +00:00
|
|
|
// Mark status.
|
|
|
|
parser->set_last_statuses(statuses_t::just(status));
|
|
|
|
}
|
2020-01-24 00:45:00 +00:00
|
|
|
return end_execution_reason_t::error;
|
2013-12-27 09:38:43 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 18:16:51 +00:00
|
|
|
// static
|
|
|
|
parse_execution_context_t::ast_args_list_t parse_execution_context_t::get_argument_nodes(
|
|
|
|
const ast::argument_list_t &args) {
|
|
|
|
ast_args_list_t result;
|
|
|
|
for (const ast::argument_t &arg : args) result.push_back(&arg);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
parse_execution_context_t::ast_args_list_t parse_execution_context_t::get_argument_nodes(
|
|
|
|
const ast::argument_or_redirection_list_t &args) {
|
|
|
|
ast_args_list_t result;
|
|
|
|
for (const ast::argument_or_redirection_t &v : args) {
|
|
|
|
if (v.is_argument()) result.push_back(&v.argument());
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
/// Handle the case of command not found.
|
2020-01-24 00:45:00 +00:00
|
|
|
end_execution_reason_t parse_execution_context_t::handle_command_not_found(
|
2020-07-03 18:16:51 +00:00
|
|
|
const wcstring &cmd_str, const ast::decorated_statement_t &statement, int err_code) {
|
2016-05-02 19:31:33 +00: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 09:40:40 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
const wchar_t *const cmd = cmd_str.c_str();
|
Support FOO=bar syntax for passing variables to individual commands
This adds initial support for statements with prefixed variable assignments.
Statments like this are supported:
a=1 b=$a echo $b # outputs 1
Just like in other shells, the left-hand side of each assignment must
be a valid variable identifier (no quoting/escaping). Array indexing
(PATH[1]=/bin ls $PATH) is *not* yet supported, but can be added fairly
easily.
The right hand side may be any valid string token, like a command
substitution, or a brace expansion.
Since `a=* foo` is equivalent to `begin set -lx a *; foo; end`,
the assignment, like `set`, uses nullglob behavior, e.g. below command
can safely be used to check if a directory is empty.
x=/nothing/{,.}* test (count $x) -eq 0
Generic file completion is done after the equal sign, so for example
pressing tab after something like `HOME=/` completes files in the
root directory
Subcommand completion works, so something like
`GIT_DIR=repo.git and command git ` correctly calls git completions
(but the git completion does not use the variable as of now).
The variable assignment is highlighted like an argument.
Closes #6048
2019-10-23 01:13:29 +00:00
|
|
|
if (err_code != ENOENT) {
|
2020-01-24 01:34:46 +00:00
|
|
|
return this->report_error(STATUS_NOT_EXECUTABLE, statement,
|
|
|
|
_(L"The file '%ls' is not executable by this user"), cmd);
|
2016-05-02 19:31:33 +00:00
|
|
|
} else {
|
|
|
|
// Handle unrecognized commands with standard command not found handler that can make better
|
|
|
|
// error messages.
|
2013-12-30 00:23:26 +00:00
|
|
|
wcstring_list_t event_args;
|
2015-04-18 19:53:43 +00:00
|
|
|
{
|
2020-07-03 18:16:51 +00:00
|
|
|
ast_args_list_t args = get_argument_nodes(statement.args_or_redirs);
|
2020-01-24 00:45:00 +00:00
|
|
|
end_execution_reason_t arg_result =
|
2018-01-15 23:15:45 +00:00
|
|
|
this->expand_arguments_from_nodes(args, &event_args, failglob);
|
2015-04-18 19:53:43 +00:00
|
|
|
|
2020-01-24 00:45:00 +00:00
|
|
|
if (arg_result != end_execution_reason_t::ok) {
|
2015-04-18 19:53:43 +00:00
|
|
|
return arg_result;
|
|
|
|
}
|
|
|
|
|
|
|
|
event_args.insert(event_args.begin(), cmd_str);
|
|
|
|
}
|
|
|
|
|
Call "fish_command_not_found" if a command wasn't found
Previously, when a command wasn't found, fish would emit the
"fish_command_not_found" *event*.
This was annoying as it was hard to override (the code ended up
checking for a function called `__fish_command_not_found_handler`
anyway!), the setup was ugly,
and it's useless - there is no use case for multiple command-not-found handlers.
Instead, let's just call a function `fish_command_not_found` if it
exists, or print the default message otherwise.
The event is completely removed, but because a missing event is not an error
(MEISNAE in C++-speak) this isn't an issue.
Note that, for backwards-compatibility, we still keep the default
handler function around even tho the new one is hard-coded in C++.
Also, if we detect a previous handler, the new handler just calls it.
This way, the backwards-compatible way to install a custom handler is:
```fish
function __fish_command_not_found_handler --on-event fish_command_not_found
# do a little dance, make a little love, get down tonight
end
```
and the new hotness is
```fish
function fish_command_not_found
# do the thing
end
```
Fixes #7293.
2020-08-29 19:54:13 +00:00
|
|
|
wcstring buffer;
|
|
|
|
wcstring error;
|
|
|
|
|
|
|
|
// Redirect to stderr
|
|
|
|
auto io = io_chain_t{};
|
2020-11-22 13:39:48 +00:00
|
|
|
io.append_from_specs({redirection_spec_t{STDOUT_FILENO, redirection_mode_t::fd, L"2"}},
|
|
|
|
L"");
|
Call "fish_command_not_found" if a command wasn't found
Previously, when a command wasn't found, fish would emit the
"fish_command_not_found" *event*.
This was annoying as it was hard to override (the code ended up
checking for a function called `__fish_command_not_found_handler`
anyway!), the setup was ugly,
and it's useless - there is no use case for multiple command-not-found handlers.
Instead, let's just call a function `fish_command_not_found` if it
exists, or print the default message otherwise.
The event is completely removed, but because a missing event is not an error
(MEISNAE in C++-speak) this isn't an issue.
Note that, for backwards-compatibility, we still keep the default
handler function around even tho the new one is hard-coded in C++.
Also, if we detect a previous handler, the new handler just calls it.
This way, the backwards-compatible way to install a custom handler is:
```fish
function __fish_command_not_found_handler --on-event fish_command_not_found
# do a little dance, make a little love, get down tonight
end
```
and the new hotness is
```fish
function fish_command_not_found
# do the thing
end
```
Fixes #7293.
2020-08-29 19:54:13 +00:00
|
|
|
|
|
|
|
if (function_exists(L"fish_command_not_found", *parser)) {
|
|
|
|
buffer = L"fish_command_not_found";
|
|
|
|
for (const wcstring &arg : event_args) {
|
|
|
|
buffer.push_back(L' ');
|
|
|
|
buffer.append(escape_string(arg, ESCAPE_ALL));
|
|
|
|
}
|
|
|
|
auto prev_statuses = parser->get_last_statuses();
|
|
|
|
|
|
|
|
event_t event(event_type_t::generic);
|
|
|
|
event.desc.str_param1 = L"fish_command_not_found";
|
|
|
|
block_t *b = parser->push_block(block_t::event_block(event));
|
|
|
|
parser->eval(buffer, io);
|
|
|
|
parser->pop_block(b);
|
|
|
|
parser->set_last_statuses(std::move(prev_statuses));
|
|
|
|
} else {
|
|
|
|
// If we have no handler, just print it as a normal error.
|
|
|
|
error = _(L"Unknown command:");
|
|
|
|
if (!event_args.empty()) {
|
|
|
|
error.push_back(L' ');
|
|
|
|
error.append(escape_string(event_args[0], ESCAPE_ALL));
|
|
|
|
}
|
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
Call "fish_command_not_found" if a command wasn't found
Previously, when a command wasn't found, fish would emit the
"fish_command_not_found" *event*.
This was annoying as it was hard to override (the code ended up
checking for a function called `__fish_command_not_found_handler`
anyway!), the setup was ugly,
and it's useless - there is no use case for multiple command-not-found handlers.
Instead, let's just call a function `fish_command_not_found` if it
exists, or print the default message otherwise.
The event is completely removed, but because a missing event is not an error
(MEISNAE in C++-speak) this isn't an issue.
Note that, for backwards-compatibility, we still keep the default
handler function around even tho the new one is hard-coded in C++.
Also, if we detect a previous handler, the new handler just calls it.
This way, the backwards-compatible way to install a custom handler is:
```fish
function __fish_command_not_found_handler --on-event fish_command_not_found
# do a little dance, make a little love, get down tonight
end
```
and the new hotness is
```fish
function fish_command_not_found
# do the thing
end
```
Fixes #7293.
2020-08-29 19:54:13 +00:00
|
|
|
// Here we want to report an error (so it shows a backtrace).
|
|
|
|
// If the handler printed text, that's already shown, so error will be empty.
|
|
|
|
return this->report_error(STATUS_CMD_UNKNOWN, statement, error.c_str());
|
2013-12-30 00:23:26 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-29 00:18:38 +00:00
|
|
|
|
2020-01-24 00:45:00 +00:00
|
|
|
end_execution_reason_t parse_execution_context_t::expand_command(
|
2020-07-03 18:16:51 +00:00
|
|
|
const ast::decorated_statement_t &statement, wcstring *out_cmd,
|
2020-01-24 00:45:00 +00:00
|
|
|
wcstring_list_t *out_args) const {
|
2018-08-26 08:41:45 +00:00
|
|
|
// 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.
|
2020-07-03 18:16:51 +00:00
|
|
|
wcstring unexp_cmd = get_source(statement.command);
|
|
|
|
size_t pos_of_command_token = statement.command.range.start;
|
2018-08-26 08:41:45 +00:00
|
|
|
|
|
|
|
// Expand the string to produce completions, and report errors.
|
2019-04-22 22:06:52 +00:00
|
|
|
expand_result_t expand_err =
|
2020-01-16 01:14:47 +00:00
|
|
|
expand_to_command_and_args(unexp_cmd, ctx, out_cmd, out_args, &errors);
|
2019-04-22 22:06:52 +00:00
|
|
|
if (expand_err == expand_result_t::error) {
|
2019-10-04 18:28:47 +00:00
|
|
|
// Issue #5812 - the expansions were done on the command token,
|
|
|
|
// excluding prefixes such as " " or "if ".
|
|
|
|
// This means that the error positions are relative to the beginning
|
|
|
|
// of the token; we need to make them relative to the original source.
|
2020-07-03 18:16:51 +00:00
|
|
|
parse_error_offset_source_start(&errors, pos_of_command_token);
|
2020-01-24 01:34:46 +00:00
|
|
|
return report_errors(STATUS_ILLEGAL_CMD, errors);
|
2019-04-22 22:06:52 +00:00
|
|
|
} else if (expand_err == expand_result_t::wildcard_no_match) {
|
2020-01-24 01:34:46 +00:00
|
|
|
return report_error(STATUS_UNMATCHED_WILDCARD, statement, WILDCARD_ERR_MSG,
|
|
|
|
get_source(statement).c_str());
|
2018-08-26 08:41:45 +00:00
|
|
|
}
|
2020-01-22 19:46:02 +00:00
|
|
|
assert(expand_err == expand_result_t::ok);
|
2018-08-26 08:41:45 +00:00
|
|
|
|
|
|
|
// Complain if the resulting expansion was empty, or expanded to an empty string.
|
2020-02-17 10:12:01 +00:00
|
|
|
// For no-exec it's okay, as we can't really perform the expansion.
|
|
|
|
if (out_cmd->empty() && !no_exec()) {
|
2020-01-24 01:34:46 +00:00
|
|
|
return this->report_error(STATUS_ILLEGAL_CMD, statement,
|
|
|
|
_(L"The expanded command was empty."));
|
2018-08-26 08:41:45 +00:00
|
|
|
}
|
2020-01-24 00:45:00 +00:00
|
|
|
return end_execution_reason_t::ok;
|
2018-08-26 08:41:45 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
/// Creates a 'normal' (non-block) process.
|
2020-01-24 00:45:00 +00:00
|
|
|
end_execution_reason_t parse_execution_context_t::populate_plain_process(
|
2020-07-03 18:16:51 +00:00
|
|
|
job_t *job, process_t *proc, const ast::decorated_statement_t &statement) {
|
2019-11-19 02:34:50 +00:00
|
|
|
assert(job != nullptr);
|
|
|
|
assert(proc != nullptr);
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// We may decide that a command should be an implicit cd.
|
2013-12-30 00:23:26 +00:00
|
|
|
bool use_implicit_cd = false;
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2018-08-26 08:41:45 +00: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);
|
2020-01-24 00:45:00 +00:00
|
|
|
if (ret != end_execution_reason_t::ok) {
|
2018-08-26 08:41:45 +00:00
|
|
|
return ret;
|
2013-12-24 21:17:24 +00:00
|
|
|
}
|
2020-02-17 10:12:01 +00:00
|
|
|
// For no-exec, having an empty command is okay. We can't do anything more with it tho.
|
|
|
|
if (no_exec()) return end_execution_reason_t::ok;
|
2018-08-26 08:41:45 +00:00
|
|
|
assert(!cmd.empty() && "expand_command should not produce an empty command");
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// Determine the process type.
|
2013-12-31 22:37:37 +00:00
|
|
|
enum process_type_t process_type = process_type_for_command(statement, cmd);
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2014-01-07 22:57:58 +00:00
|
|
|
wcstring path_to_external_command;
|
2019-03-24 19:29:25 +00:00
|
|
|
if (process_type == process_type_t::external || process_type == process_type_t::exec) {
|
2016-05-02 19:31:33 +00:00
|
|
|
// Determine the actual command. This may be an implicit cd.
|
2018-09-16 19:48:50 +00:00
|
|
|
bool has_command = path_get_path(cmd, &path_to_external_command, parser->vars());
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-02 19:31:33 +00: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 00:23:26 +00:00
|
|
|
const int no_cmd_err_code = errno;
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// If the specified command does not exist, and is undecorated, try using an implicit cd.
|
2020-07-07 23:28:39 +00:00
|
|
|
if (!has_command && statement.decoration() == statement_decoration_t::none) {
|
2016-05-02 19:31:33 +00:00
|
|
|
// Implicit cd requires an empty argument and redirection list.
|
2020-07-03 18:16:51 +00:00
|
|
|
if (statement.args_or_redirs.empty()) {
|
2018-08-26 08:41:45 +00:00
|
|
|
// Ok, no arguments or redirections; check to see if the command is a directory.
|
2018-11-18 02:02:28 +00:00
|
|
|
use_implicit_cd =
|
2018-09-16 11:05:17 +00:00
|
|
|
path_as_implicit_cd(cmd, parser->vars().get_pwd_slash(), parser->vars())
|
|
|
|
.has_value();
|
2013-12-30 00:23:26 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
if (!has_command && !use_implicit_cd) {
|
2020-02-17 10:12:01 +00:00
|
|
|
// No command. If we're --no-execute return okay - it might be a function.
|
|
|
|
if (no_exec()) return end_execution_reason_t::ok;
|
2015-04-18 19:53:43 +00:00
|
|
|
return this->handle_command_not_found(cmd, statement, no_cmd_err_code);
|
2013-12-24 21:17:24 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2018-08-26 08:41:45 +00:00
|
|
|
// Produce the full argument list and the set of IO redirections.
|
|
|
|
wcstring_list_t cmd_args;
|
2019-12-13 00:44:24 +00:00
|
|
|
redirection_spec_list_t redirections;
|
2016-05-02 19:31:33 +00:00
|
|
|
if (use_implicit_cd) {
|
2018-08-26 08:41:45 +00:00
|
|
|
// Implicit cd is simple.
|
|
|
|
cmd_args = {L"cd", cmd};
|
2014-01-07 22:57:58 +00:00
|
|
|
path_to_external_command.clear();
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// If we have defined a wrapper around cd, use it, otherwise use the cd builtin.
|
2019-05-05 03:20:52 +00:00
|
|
|
process_type =
|
|
|
|
function_exists(L"cd", *parser) ? process_type_t::function : process_type_t::builtin;
|
2016-05-02 19:31:33 +00:00
|
|
|
} else {
|
2018-08-26 08:41:45 +00:00
|
|
|
// Not implicit cd.
|
2017-04-05 04:28:57 +00:00
|
|
|
const globspec_t glob_behavior = (cmd == L"set" || cmd == L"count") ? nullglob : failglob;
|
2018-08-26 08:41:45 +00: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());
|
2020-07-03 18:16:51 +00:00
|
|
|
|
|
|
|
ast_args_list_t arg_nodes = get_argument_nodes(statement.args_or_redirs);
|
2020-01-24 00:45:00 +00:00
|
|
|
end_execution_reason_t arg_result =
|
2018-08-26 08:41:45 +00:00
|
|
|
this->expand_arguments_from_nodes(arg_nodes, &cmd_args, glob_behavior);
|
2020-01-24 00:45:00 +00:00
|
|
|
if (arg_result != end_execution_reason_t::ok) {
|
2014-05-31 19:41:27 +00:00
|
|
|
return arg_result;
|
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// The set of IO redirections that we construct for the process.
|
2020-07-03 18:16:51 +00:00
|
|
|
auto reason = this->determine_redirections(statement.args_or_redirs, &redirections);
|
2020-01-24 01:34:46 +00:00
|
|
|
if (reason != end_execution_reason_t::ok) {
|
|
|
|
return reason;
|
2013-12-31 22:37:37 +00:00
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// Determine the process type.
|
2013-12-31 22:37:37 +00:00
|
|
|
process_type = process_type_for_command(statement, cmd);
|
2013-12-30 00:23:26 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// Populate the process.
|
2013-12-31 22:37:37 +00:00
|
|
|
proc->type = process_type;
|
2018-08-26 08:41:45 +00:00
|
|
|
proc->set_argv(cmd_args);
|
2019-12-13 00:44:24 +00:00
|
|
|
proc->set_redirection_specs(std::move(redirections));
|
2019-03-17 02:32:07 +00:00
|
|
|
proc->actual_cmd = std::move(path_to_external_command);
|
2020-01-24 00:45:00 +00:00
|
|
|
return end_execution_reason_t::ok;
|
2013-12-24 21:17:24 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 19:31:33 +00: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.
|
2020-01-24 00:45:00 +00:00
|
|
|
end_execution_reason_t parse_execution_context_t::expand_arguments_from_nodes(
|
2020-07-03 18:16:51 +00:00
|
|
|
const ast_args_list_t &argument_nodes, wcstring_list_t *out_arguments,
|
2018-01-15 23:15:45 +00:00
|
|
|
globspec_t glob_behavior) {
|
2016-05-02 19:31:33 +00: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-05-31 19:41:27 +00:00
|
|
|
out_arguments->reserve(out_arguments->size() + argument_nodes.size());
|
2020-01-16 00:13:41 +00:00
|
|
|
completion_list_t arg_expanded;
|
2020-07-03 18:16:51 +00:00
|
|
|
for (const ast::argument_t *arg_node : argument_nodes) {
|
2016-05-02 19:31:33 +00:00
|
|
|
// Expect all arguments to have source.
|
2020-11-14 22:12:53 +00:00
|
|
|
assert(arg_node->has_source() && "Argument should have source");
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// Expand this string.
|
2014-03-22 00:13:33 +00:00
|
|
|
parse_error_list_t errors;
|
2016-02-28 00:14:05 +00:00
|
|
|
arg_expanded.clear();
|
2020-11-14 22:12:53 +00:00
|
|
|
auto expand_ret =
|
|
|
|
expand_string(get_source(*arg_node), &arg_expanded, expand_flags_t{}, ctx, &errors);
|
2020-07-03 18:16:51 +00:00
|
|
|
parse_error_offset_source_start(&errors, arg_node->range.start);
|
2020-01-24 01:34:46 +00:00
|
|
|
switch (expand_ret.result) {
|
2019-04-22 22:06:52 +00:00
|
|
|
case expand_result_t::error: {
|
2020-01-24 01:34:46 +00:00
|
|
|
return this->report_errors(expand_ret.status, errors);
|
2013-12-24 21:17:24 +00:00
|
|
|
}
|
2020-01-24 01:34:46 +00:00
|
|
|
|
2020-01-22 19:58:10 +00:00
|
|
|
case expand_result_t::cancel: {
|
2020-01-24 00:45:00 +00:00
|
|
|
return end_execution_reason_t::cancelled;
|
2020-01-22 19:58:10 +00:00
|
|
|
}
|
2019-04-22 22:06:52 +00:00
|
|
|
case expand_result_t::wildcard_no_match: {
|
2016-05-02 19:31:33 +00:00
|
|
|
if (glob_behavior == failglob) {
|
2020-02-17 10:12:01 +00:00
|
|
|
// For no_exec, ignore the error - this might work at runtime.
|
|
|
|
if (no_exec()) return end_execution_reason_t::ok;
|
2016-05-02 19:31:33 +00:00
|
|
|
// Report the unmatched wildcard error and stop processing.
|
2020-07-03 18:16:51 +00:00
|
|
|
return report_error(STATUS_UNMATCHED_WILDCARD, *arg_node, WILDCARD_ERR_MSG,
|
|
|
|
get_source(*arg_node).c_str());
|
2016-02-08 18:49:26 +00:00
|
|
|
}
|
2013-12-24 21:17:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-04-22 22:06:52 +00:00
|
|
|
case expand_result_t::ok: {
|
2013-12-24 21:17:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-10-30 00:25:48 +00:00
|
|
|
default: {
|
|
|
|
DIE("unexpected expand_string() return value");
|
|
|
|
}
|
2013-12-24 21:17:24 +00:00
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2017-01-27 00:14:50 +00:00
|
|
|
// Now copy over any expanded arguments. Use std::move() to avoid extra allocations; this
|
2016-05-02 19:31:33 +00:00
|
|
|
// is called very frequently.
|
2017-01-27 00:14:50 +00: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-24 21:17:24 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2019-12-18 00:41:10 +00:00
|
|
|
// We may have received a cancellation during this expansion.
|
2019-12-18 02:10:29 +00:00
|
|
|
if (auto ret = check_end_execution()) {
|
|
|
|
return *ret;
|
2019-12-18 00:41:10 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 00:45:00 +00:00
|
|
|
return end_execution_reason_t::ok;
|
2013-12-24 21:17:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 01:34:46 +00:00
|
|
|
end_execution_reason_t parse_execution_context_t::determine_redirections(
|
2020-07-03 18:16:51 +00:00
|
|
|
const ast::argument_or_redirection_list_t &list, redirection_spec_list_t *out_redirections) {
|
2016-05-02 19:31:33 +00:00
|
|
|
// Get all redirection nodes underneath the statement.
|
2020-07-03 18:16:51 +00:00
|
|
|
for (const ast::argument_or_redirection_t &arg_or_redir : list) {
|
|
|
|
if (!arg_or_redir.is_redirection()) continue;
|
|
|
|
const ast::redirection_t &redir_node = arg_or_redir.redirection();
|
2019-10-14 20:20:31 +00:00
|
|
|
|
2020-07-03 18:16:51 +00:00
|
|
|
maybe_t<pipe_or_redir_t> oper = pipe_or_redir_t::from_string(get_source(redir_node.oper));
|
|
|
|
if (!oper || !oper->is_valid()) {
|
2019-12-13 00:44:24 +00:00
|
|
|
// TODO: figure out if this can ever happen. If so, improve this error message.
|
2020-07-03 18:16:51 +00:00
|
|
|
return report_error(STATUS_INVALID_ARGS, redir_node, _(L"Invalid redirection: %ls"),
|
|
|
|
get_source(redir_node).c_str());
|
2019-10-14 20:20:31 +00:00
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2019-04-25 18:34:49 +00:00
|
|
|
// PCA: I can't justify this skip_variables flag. It was like this when I got here.
|
2020-07-03 18:16:51 +00:00
|
|
|
wcstring target = get_source(redir_node.target);
|
2019-05-05 02:16:26 +00:00
|
|
|
bool target_expanded =
|
2020-01-16 01:14:47 +00:00
|
|
|
expand_one(target, no_exec() ? expand_flag::skip_variables : expand_flags_t{}, ctx);
|
2016-05-02 19:31:33 +00:00
|
|
|
if (!target_expanded || target.empty()) {
|
|
|
|
// TODO: Improve this error message.
|
2020-07-03 18:16:51 +00:00
|
|
|
return report_error(STATUS_INVALID_ARGS, redir_node,
|
2020-01-24 01:34:46 +00:00
|
|
|
_(L"Invalid redirection target: %ls"), target.c_str());
|
2013-12-24 21:17:24 +00:00
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2019-12-13 00:44:24 +00:00
|
|
|
// Make a redirection spec from the redirect token.
|
2020-07-03 18:16:51 +00:00
|
|
|
assert(oper && oper->is_valid() && "expected to have a valid redirection");
|
|
|
|
redirection_spec_t spec{oper->fd, oper->mode, std::move(target)};
|
2019-12-13 00:44:24 +00:00
|
|
|
|
|
|
|
// Validate this spec.
|
|
|
|
if (spec.mode == redirection_mode_t::fd && !spec.is_close() && !spec.get_target_as_fd()) {
|
|
|
|
const wchar_t *fmt =
|
|
|
|
_(L"Requested redirection to '%ls', which is not a valid file descriptor");
|
2020-07-03 18:16:51 +00:00
|
|
|
return report_error(STATUS_INVALID_ARGS, redir_node, fmt, spec.target.c_str());
|
2013-12-24 21:17:24 +00:00
|
|
|
}
|
2019-12-13 00:44:24 +00:00
|
|
|
out_redirections->push_back(std::move(spec));
|
2019-10-14 22:45:40 +00:00
|
|
|
|
2020-07-03 18:16:51 +00:00
|
|
|
if (oper->stderr_merge) {
|
2019-10-14 22:45:40 +00:00
|
|
|
// This was a redirect like &> which also modifies stderr.
|
|
|
|
// Also redirect stderr to stdout.
|
2019-12-13 00:44:24 +00:00
|
|
|
out_redirections->push_back(get_stderr_merge());
|
2019-10-14 22:45:40 +00:00
|
|
|
}
|
2013-12-24 21:17:24 +00:00
|
|
|
}
|
2020-01-24 01:34:46 +00:00
|
|
|
return end_execution_reason_t::ok;
|
2013-12-24 21:17:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 00:45:00 +00:00
|
|
|
end_execution_reason_t parse_execution_context_t::populate_not_process(
|
2020-07-03 18:16:51 +00:00
|
|
|
job_t *job, process_t *proc, const ast::not_statement_t ¬_statement) {
|
2019-10-15 21:37:10 +00:00
|
|
|
auto &flags = job->mut_flags();
|
|
|
|
flags.negate = !flags.negate;
|
2020-07-03 18:16:51 +00:00
|
|
|
return this->populate_job_process(job, proc, not_statement.contents, not_statement.variables);
|
2013-12-24 21:17:24 +00:00
|
|
|
}
|
|
|
|
|
2018-01-15 23:37:13 +00:00
|
|
|
template <typename Type>
|
2020-01-24 00:45:00 +00:00
|
|
|
end_execution_reason_t parse_execution_context_t::populate_block_process(
|
2020-07-03 18:16:51 +00:00
|
|
|
job_t *job, process_t *proc, const ast::statement_t &statement,
|
|
|
|
const Type &specific_statement) {
|
|
|
|
using namespace ast;
|
2019-03-24 19:29:25 +00:00
|
|
|
// We handle block statements by creating process_type_t::block_node, that will bounce back to
|
|
|
|
// us when it's time to execute them.
|
2016-10-09 21:38:26 +00:00
|
|
|
UNUSED(job);
|
2020-07-03 18:16:51 +00:00
|
|
|
static_assert(Type::AstType == type_t::block_statement ||
|
|
|
|
Type::AstType == type_t::if_statement ||
|
|
|
|
Type::AstType == type_t::switch_statement,
|
2018-01-15 23:37:13 +00:00
|
|
|
"Invalid block process");
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2020-07-03 18:16:51 +00:00
|
|
|
// Get the argument or redirections list.
|
|
|
|
// TODO: args_or_redirs should be available without resolving the statement type.
|
|
|
|
const argument_or_redirection_list_t *args_or_redirs = nullptr;
|
|
|
|
|
|
|
|
// Upcast to permit dropping the 'template' keyword.
|
|
|
|
const node_t &ss = specific_statement;
|
|
|
|
switch (Type::AstType) {
|
|
|
|
case type_t::block_statement:
|
|
|
|
args_or_redirs = &ss.as<block_statement_t>()->args_or_redirs;
|
|
|
|
break;
|
|
|
|
case type_t::if_statement:
|
|
|
|
args_or_redirs = &ss.as<if_statement_t>()->args_or_redirs;
|
|
|
|
break;
|
|
|
|
case type_t::switch_statement:
|
|
|
|
args_or_redirs = &ss.as<switch_statement_t>()->args_or_redirs;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DIE("Unexpected block node type");
|
|
|
|
}
|
|
|
|
assert(args_or_redirs && "Should have args_or_redirs");
|
|
|
|
|
2019-12-13 00:44:24 +00:00
|
|
|
redirection_spec_list_t redirections;
|
2020-07-03 18:16:51 +00:00
|
|
|
auto reason = this->determine_redirections(*args_or_redirs, &redirections);
|
2020-01-24 01:34:46 +00:00
|
|
|
if (reason == end_execution_reason_t::ok) {
|
|
|
|
proc->type = process_type_t::block_node;
|
|
|
|
proc->block_node_source = pstree;
|
2020-07-03 18:16:51 +00:00
|
|
|
proc->internal_block_node = &statement;
|
2020-01-24 01:34:46 +00:00
|
|
|
proc->set_redirection_specs(std::move(redirections));
|
2019-12-13 00:44:24 +00:00
|
|
|
}
|
2020-01-24 01:34:46 +00:00
|
|
|
return reason;
|
2013-12-26 20:24:00 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 00:45:00 +00:00
|
|
|
end_execution_reason_t parse_execution_context_t::apply_variable_assignments(
|
2020-07-03 18:16:51 +00:00
|
|
|
process_t *proc, const ast::variable_assignment_list_t &variable_assignment_list,
|
Support FOO=bar syntax for passing variables to individual commands
This adds initial support for statements with prefixed variable assignments.
Statments like this are supported:
a=1 b=$a echo $b # outputs 1
Just like in other shells, the left-hand side of each assignment must
be a valid variable identifier (no quoting/escaping). Array indexing
(PATH[1]=/bin ls $PATH) is *not* yet supported, but can be added fairly
easily.
The right hand side may be any valid string token, like a command
substitution, or a brace expansion.
Since `a=* foo` is equivalent to `begin set -lx a *; foo; end`,
the assignment, like `set`, uses nullglob behavior, e.g. below command
can safely be used to check if a directory is empty.
x=/nothing/{,.}* test (count $x) -eq 0
Generic file completion is done after the equal sign, so for example
pressing tab after something like `HOME=/` completes files in the
root directory
Subcommand completion works, so something like
`GIT_DIR=repo.git and command git ` correctly calls git completions
(but the git completion does not use the variable as of now).
The variable assignment is highlighted like an argument.
Closes #6048
2019-10-23 01:13:29 +00:00
|
|
|
const block_t **block) {
|
2020-07-03 18:16:51 +00:00
|
|
|
if (variable_assignment_list.empty()) return end_execution_reason_t::ok;
|
Support FOO=bar syntax for passing variables to individual commands
This adds initial support for statements with prefixed variable assignments.
Statments like this are supported:
a=1 b=$a echo $b # outputs 1
Just like in other shells, the left-hand side of each assignment must
be a valid variable identifier (no quoting/escaping). Array indexing
(PATH[1]=/bin ls $PATH) is *not* yet supported, but can be added fairly
easily.
The right hand side may be any valid string token, like a command
substitution, or a brace expansion.
Since `a=* foo` is equivalent to `begin set -lx a *; foo; end`,
the assignment, like `set`, uses nullglob behavior, e.g. below command
can safely be used to check if a directory is empty.
x=/nothing/{,.}* test (count $x) -eq 0
Generic file completion is done after the equal sign, so for example
pressing tab after something like `HOME=/` completes files in the
root directory
Subcommand completion works, so something like
`GIT_DIR=repo.git and command git ` correctly calls git completions
(but the git completion does not use the variable as of now).
The variable assignment is highlighted like an argument.
Closes #6048
2019-10-23 01:13:29 +00:00
|
|
|
*block = parser->push_block(block_t::variable_assignment_block());
|
2020-07-03 18:16:51 +00:00
|
|
|
for (const ast::variable_assignment_t &variable_assignment : variable_assignment_list) {
|
|
|
|
const wcstring &source = get_source(variable_assignment);
|
Support FOO=bar syntax for passing variables to individual commands
This adds initial support for statements with prefixed variable assignments.
Statments like this are supported:
a=1 b=$a echo $b # outputs 1
Just like in other shells, the left-hand side of each assignment must
be a valid variable identifier (no quoting/escaping). Array indexing
(PATH[1]=/bin ls $PATH) is *not* yet supported, but can be added fairly
easily.
The right hand side may be any valid string token, like a command
substitution, or a brace expansion.
Since `a=* foo` is equivalent to `begin set -lx a *; foo; end`,
the assignment, like `set`, uses nullglob behavior, e.g. below command
can safely be used to check if a directory is empty.
x=/nothing/{,.}* test (count $x) -eq 0
Generic file completion is done after the equal sign, so for example
pressing tab after something like `HOME=/` completes files in the
root directory
Subcommand completion works, so something like
`GIT_DIR=repo.git and command git ` correctly calls git completions
(but the git completion does not use the variable as of now).
The variable assignment is highlighted like an argument.
Closes #6048
2019-10-23 01:13:29 +00:00
|
|
|
auto equals_pos = variable_assignment_equals_pos(source);
|
|
|
|
assert(equals_pos);
|
2020-01-17 12:40:56 +00:00
|
|
|
const wcstring variable_name = source.substr(0, *equals_pos);
|
Support FOO=bar syntax for passing variables to individual commands
This adds initial support for statements with prefixed variable assignments.
Statments like this are supported:
a=1 b=$a echo $b # outputs 1
Just like in other shells, the left-hand side of each assignment must
be a valid variable identifier (no quoting/escaping). Array indexing
(PATH[1]=/bin ls $PATH) is *not* yet supported, but can be added fairly
easily.
The right hand side may be any valid string token, like a command
substitution, or a brace expansion.
Since `a=* foo` is equivalent to `begin set -lx a *; foo; end`,
the assignment, like `set`, uses nullglob behavior, e.g. below command
can safely be used to check if a directory is empty.
x=/nothing/{,.}* test (count $x) -eq 0
Generic file completion is done after the equal sign, so for example
pressing tab after something like `HOME=/` completes files in the
root directory
Subcommand completion works, so something like
`GIT_DIR=repo.git and command git ` correctly calls git completions
(but the git completion does not use the variable as of now).
The variable assignment is highlighted like an argument.
Closes #6048
2019-10-23 01:13:29 +00:00
|
|
|
const wcstring expression = source.substr(*equals_pos + 1);
|
2020-01-16 00:13:41 +00:00
|
|
|
completion_list_t expression_expanded;
|
Support FOO=bar syntax for passing variables to individual commands
This adds initial support for statements with prefixed variable assignments.
Statments like this are supported:
a=1 b=$a echo $b # outputs 1
Just like in other shells, the left-hand side of each assignment must
be a valid variable identifier (no quoting/escaping). Array indexing
(PATH[1]=/bin ls $PATH) is *not* yet supported, but can be added fairly
easily.
The right hand side may be any valid string token, like a command
substitution, or a brace expansion.
Since `a=* foo` is equivalent to `begin set -lx a *; foo; end`,
the assignment, like `set`, uses nullglob behavior, e.g. below command
can safely be used to check if a directory is empty.
x=/nothing/{,.}* test (count $x) -eq 0
Generic file completion is done after the equal sign, so for example
pressing tab after something like `HOME=/` completes files in the
root directory
Subcommand completion works, so something like
`GIT_DIR=repo.git and command git ` correctly calls git completions
(but the git completion does not use the variable as of now).
The variable assignment is highlighted like an argument.
Closes #6048
2019-10-23 01:13:29 +00:00
|
|
|
parse_error_list_t errors;
|
|
|
|
// TODO this is mostly copied from expand_arguments_from_nodes, maybe extract to function
|
2020-09-27 23:49:12 +00:00
|
|
|
auto expand_ret =
|
|
|
|
expand_string(expression, &expression_expanded, expand_flags_t{}, ctx, &errors);
|
2020-07-03 18:16:51 +00:00
|
|
|
parse_error_offset_source_start(&errors, variable_assignment.range.start + *equals_pos + 1);
|
2020-01-24 01:34:46 +00:00
|
|
|
switch (expand_ret.result) {
|
|
|
|
case expand_result_t::error:
|
|
|
|
return this->report_errors(expand_ret.status, errors);
|
|
|
|
|
|
|
|
case expand_result_t::cancel:
|
2020-01-24 00:45:00 +00:00
|
|
|
return end_execution_reason_t::cancelled;
|
2020-01-24 01:34:46 +00:00
|
|
|
|
Support FOO=bar syntax for passing variables to individual commands
This adds initial support for statements with prefixed variable assignments.
Statments like this are supported:
a=1 b=$a echo $b # outputs 1
Just like in other shells, the left-hand side of each assignment must
be a valid variable identifier (no quoting/escaping). Array indexing
(PATH[1]=/bin ls $PATH) is *not* yet supported, but can be added fairly
easily.
The right hand side may be any valid string token, like a command
substitution, or a brace expansion.
Since `a=* foo` is equivalent to `begin set -lx a *; foo; end`,
the assignment, like `set`, uses nullglob behavior, e.g. below command
can safely be used to check if a directory is empty.
x=/nothing/{,.}* test (count $x) -eq 0
Generic file completion is done after the equal sign, so for example
pressing tab after something like `HOME=/` completes files in the
root directory
Subcommand completion works, so something like
`GIT_DIR=repo.git and command git ` correctly calls git completions
(but the git completion does not use the variable as of now).
The variable assignment is highlighted like an argument.
Closes #6048
2019-10-23 01:13:29 +00:00
|
|
|
case expand_result_t::wildcard_no_match: // nullglob (equivalent to set)
|
2020-01-24 01:34:46 +00:00
|
|
|
case expand_result_t::ok:
|
Support FOO=bar syntax for passing variables to individual commands
This adds initial support for statements with prefixed variable assignments.
Statments like this are supported:
a=1 b=$a echo $b # outputs 1
Just like in other shells, the left-hand side of each assignment must
be a valid variable identifier (no quoting/escaping). Array indexing
(PATH[1]=/bin ls $PATH) is *not* yet supported, but can be added fairly
easily.
The right hand side may be any valid string token, like a command
substitution, or a brace expansion.
Since `a=* foo` is equivalent to `begin set -lx a *; foo; end`,
the assignment, like `set`, uses nullglob behavior, e.g. below command
can safely be used to check if a directory is empty.
x=/nothing/{,.}* test (count $x) -eq 0
Generic file completion is done after the equal sign, so for example
pressing tab after something like `HOME=/` completes files in the
root directory
Subcommand completion works, so something like
`GIT_DIR=repo.git and command git ` correctly calls git completions
(but the git completion does not use the variable as of now).
The variable assignment is highlighted like an argument.
Closes #6048
2019-10-23 01:13:29 +00:00
|
|
|
break;
|
2020-01-24 01:34:46 +00:00
|
|
|
|
Support FOO=bar syntax for passing variables to individual commands
This adds initial support for statements with prefixed variable assignments.
Statments like this are supported:
a=1 b=$a echo $b # outputs 1
Just like in other shells, the left-hand side of each assignment must
be a valid variable identifier (no quoting/escaping). Array indexing
(PATH[1]=/bin ls $PATH) is *not* yet supported, but can be added fairly
easily.
The right hand side may be any valid string token, like a command
substitution, or a brace expansion.
Since `a=* foo` is equivalent to `begin set -lx a *; foo; end`,
the assignment, like `set`, uses nullglob behavior, e.g. below command
can safely be used to check if a directory is empty.
x=/nothing/{,.}* test (count $x) -eq 0
Generic file completion is done after the equal sign, so for example
pressing tab after something like `HOME=/` completes files in the
root directory
Subcommand completion works, so something like
`GIT_DIR=repo.git and command git ` correctly calls git completions
(but the git completion does not use the variable as of now).
The variable assignment is highlighted like an argument.
Closes #6048
2019-10-23 01:13:29 +00:00
|
|
|
default: {
|
|
|
|
DIE("unexpected expand_string() return value");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wcstring_list_t vals;
|
|
|
|
for (auto &completion : expression_expanded) {
|
|
|
|
vals.emplace_back(std::move(completion.completion));
|
|
|
|
}
|
|
|
|
if (proc) proc->variable_assignments.push_back({variable_name, vals});
|
2020-03-08 03:44:58 +00:00
|
|
|
parser->set_var_and_fire(variable_name, ENV_LOCAL | ENV_EXPORT, std::move(vals));
|
Support FOO=bar syntax for passing variables to individual commands
This adds initial support for statements with prefixed variable assignments.
Statments like this are supported:
a=1 b=$a echo $b # outputs 1
Just like in other shells, the left-hand side of each assignment must
be a valid variable identifier (no quoting/escaping). Array indexing
(PATH[1]=/bin ls $PATH) is *not* yet supported, but can be added fairly
easily.
The right hand side may be any valid string token, like a command
substitution, or a brace expansion.
Since `a=* foo` is equivalent to `begin set -lx a *; foo; end`,
the assignment, like `set`, uses nullglob behavior, e.g. below command
can safely be used to check if a directory is empty.
x=/nothing/{,.}* test (count $x) -eq 0
Generic file completion is done after the equal sign, so for example
pressing tab after something like `HOME=/` completes files in the
root directory
Subcommand completion works, so something like
`GIT_DIR=repo.git and command git ` correctly calls git completions
(but the git completion does not use the variable as of now).
The variable assignment is highlighted like an argument.
Closes #6048
2019-10-23 01:13:29 +00:00
|
|
|
}
|
2020-01-24 00:45:00 +00:00
|
|
|
return end_execution_reason_t::ok;
|
Support FOO=bar syntax for passing variables to individual commands
This adds initial support for statements with prefixed variable assignments.
Statments like this are supported:
a=1 b=$a echo $b # outputs 1
Just like in other shells, the left-hand side of each assignment must
be a valid variable identifier (no quoting/escaping). Array indexing
(PATH[1]=/bin ls $PATH) is *not* yet supported, but can be added fairly
easily.
The right hand side may be any valid string token, like a command
substitution, or a brace expansion.
Since `a=* foo` is equivalent to `begin set -lx a *; foo; end`,
the assignment, like `set`, uses nullglob behavior, e.g. below command
can safely be used to check if a directory is empty.
x=/nothing/{,.}* test (count $x) -eq 0
Generic file completion is done after the equal sign, so for example
pressing tab after something like `HOME=/` completes files in the
root directory
Subcommand completion works, so something like
`GIT_DIR=repo.git and command git ` correctly calls git completions
(but the git completion does not use the variable as of now).
The variable assignment is highlighted like an argument.
Closes #6048
2019-10-23 01:13:29 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 00:45:00 +00:00
|
|
|
end_execution_reason_t parse_execution_context_t::populate_job_process(
|
2020-07-03 18:16:51 +00:00
|
|
|
job_t *job, process_t *proc, const ast::statement_t &statement,
|
|
|
|
const ast::variable_assignment_list_t &variable_assignments) {
|
|
|
|
using namespace ast;
|
2016-05-02 19:31:33 +00:00
|
|
|
// Get the "specific statement" which is boolean / block / if / switch / decorated.
|
2020-07-03 18:16:51 +00:00
|
|
|
const node_t &specific_statement = *statement.contents.contents;
|
2014-01-15 09:40:40 +00:00
|
|
|
|
Support FOO=bar syntax for passing variables to individual commands
This adds initial support for statements with prefixed variable assignments.
Statments like this are supported:
a=1 b=$a echo $b # outputs 1
Just like in other shells, the left-hand side of each assignment must
be a valid variable identifier (no quoting/escaping). Array indexing
(PATH[1]=/bin ls $PATH) is *not* yet supported, but can be added fairly
easily.
The right hand side may be any valid string token, like a command
substitution, or a brace expansion.
Since `a=* foo` is equivalent to `begin set -lx a *; foo; end`,
the assignment, like `set`, uses nullglob behavior, e.g. below command
can safely be used to check if a directory is empty.
x=/nothing/{,.}* test (count $x) -eq 0
Generic file completion is done after the equal sign, so for example
pressing tab after something like `HOME=/` completes files in the
root directory
Subcommand completion works, so something like
`GIT_DIR=repo.git and command git ` correctly calls git completions
(but the git completion does not use the variable as of now).
The variable assignment is highlighted like an argument.
Closes #6048
2019-10-23 01:13:29 +00:00
|
|
|
const block_t *block = nullptr;
|
2020-01-24 00:45:00 +00:00
|
|
|
end_execution_reason_t result =
|
|
|
|
this->apply_variable_assignments(proc, variable_assignments, &block);
|
Support FOO=bar syntax for passing variables to individual commands
This adds initial support for statements with prefixed variable assignments.
Statments like this are supported:
a=1 b=$a echo $b # outputs 1
Just like in other shells, the left-hand side of each assignment must
be a valid variable identifier (no quoting/escaping). Array indexing
(PATH[1]=/bin ls $PATH) is *not* yet supported, but can be added fairly
easily.
The right hand side may be any valid string token, like a command
substitution, or a brace expansion.
Since `a=* foo` is equivalent to `begin set -lx a *; foo; end`,
the assignment, like `set`, uses nullglob behavior, e.g. below command
can safely be used to check if a directory is empty.
x=/nothing/{,.}* test (count $x) -eq 0
Generic file completion is done after the equal sign, so for example
pressing tab after something like `HOME=/` completes files in the
root directory
Subcommand completion works, so something like
`GIT_DIR=repo.git and command git ` correctly calls git completions
(but the git completion does not use the variable as of now).
The variable assignment is highlighted like an argument.
Closes #6048
2019-10-23 01:13:29 +00:00
|
|
|
cleanup_t scope([&]() {
|
|
|
|
if (block) parser->pop_block(block);
|
|
|
|
});
|
2020-01-24 01:34:46 +00:00
|
|
|
if (result != end_execution_reason_t::ok) return result;
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
switch (specific_statement.type) {
|
2020-07-03 18:16:51 +00:00
|
|
|
case type_t::not_statement: {
|
|
|
|
result =
|
|
|
|
this->populate_not_process(job, proc, *specific_statement.as<not_statement_t>());
|
2013-12-24 21:17:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-07-03 18:16:51 +00:00
|
|
|
case type_t::block_statement:
|
|
|
|
result = this->populate_block_process(job, proc, statement,
|
|
|
|
*specific_statement.as<block_statement_t>());
|
2018-01-15 23:37:13 +00:00
|
|
|
break;
|
2020-07-03 18:16:51 +00:00
|
|
|
case type_t::if_statement:
|
|
|
|
result = this->populate_block_process(job, proc, statement,
|
|
|
|
*specific_statement.as<if_statement_t>());
|
2018-01-15 23:37:13 +00:00
|
|
|
break;
|
2020-07-03 18:16:51 +00:00
|
|
|
case type_t::switch_statement:
|
|
|
|
result = this->populate_block_process(job, proc, statement,
|
|
|
|
*specific_statement.as<switch_statement_t>());
|
2013-12-24 21:17:24 +00:00
|
|
|
break;
|
2020-07-03 18:16:51 +00:00
|
|
|
case type_t::decorated_statement: {
|
|
|
|
result = this->populate_plain_process(job, proc,
|
|
|
|
*specific_statement.as<decorated_statement_t>());
|
2013-12-24 21:17:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-05-02 19:31:33 +00:00
|
|
|
default: {
|
2019-05-30 09:54:09 +00:00
|
|
|
FLOGF(error, L"'%ls' not handled by new parser yet.",
|
2019-06-04 03:30:48 +00:00
|
|
|
specific_statement.describe().c_str());
|
2013-12-26 20:24:00 +00:00
|
|
|
PARSER_DIE();
|
|
|
|
break;
|
2016-05-02 19:31:33 +00:00
|
|
|
}
|
2013-12-24 21:17:24 +00:00
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2013-12-24 21:17:24 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-01-24 00:45:00 +00:00
|
|
|
end_execution_reason_t parse_execution_context_t::populate_job_from_job_node(
|
2020-07-03 18:16:51 +00:00
|
|
|
job_t *j, const ast::job_t &job_node, const block_t *associated_block) {
|
2016-10-09 21:38:26 +00:00
|
|
|
UNUSED(associated_block);
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2020-07-03 18:16:51 +00:00
|
|
|
// We are going to construct process_t structures for every statement in the job.
|
2016-05-02 19:31:33 +00:00
|
|
|
// Create processes. Each one may fail.
|
2017-01-23 17:28:34 +00:00
|
|
|
process_list_t processes;
|
|
|
|
processes.emplace_back(new process_t());
|
2020-07-03 18:16:51 +00:00
|
|
|
end_execution_reason_t result = this->populate_job_process(
|
|
|
|
j, processes.back().get(), job_node.statement, job_node.variables);
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2020-07-03 18:16:51 +00:00
|
|
|
// Construct process_ts for job continuations (pipelines).
|
|
|
|
for (const ast::job_continuation_t &jc : job_node.continuation) {
|
2020-01-24 00:45:00 +00:00
|
|
|
if (result != end_execution_reason_t::ok) {
|
2018-01-14 09:17:57 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-05-02 19:31:33 +00:00
|
|
|
// Handle the pipe, whose fd may not be the obvious stdout.
|
2020-07-03 18:16:51 +00:00
|
|
|
auto parsed_pipe = pipe_or_redir_t::from_string(get_source(jc.pipe));
|
2019-10-14 20:20:31 +00:00
|
|
|
assert(parsed_pipe.has_value() && parsed_pipe->is_pipe && "Failed to parse valid pipe");
|
|
|
|
if (!parsed_pipe->is_valid()) {
|
2020-07-03 18:16:51 +00:00
|
|
|
result = report_error(STATUS_INVALID_ARGS, jc.pipe, ILLEGAL_FD_ERR_MSG,
|
|
|
|
get_source(jc.pipe).c_str());
|
2014-01-13 11:57:59 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-10-14 20:20:31 +00:00
|
|
|
processes.back()->pipe_write_fd = parsed_pipe->fd;
|
2019-10-14 22:45:40 +00:00
|
|
|
if (parsed_pipe->stderr_merge) {
|
|
|
|
// This was a pipe like &| which redirects both stdout and stderr.
|
|
|
|
// Also redirect stderr to stdout.
|
2019-12-13 00:44:24 +00:00
|
|
|
auto specs = processes.back()->redirection_specs();
|
|
|
|
specs.push_back(get_stderr_merge());
|
|
|
|
processes.back()->set_redirection_specs(std::move(specs));
|
2019-10-14 22:45:40 +00:00
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// Store the new process (and maybe with an error).
|
2017-01-23 17:28:34 +00:00
|
|
|
processes.emplace_back(new process_t());
|
2020-07-03 18:16:51 +00:00
|
|
|
result = this->populate_job_process(j, processes.back().get(), jc.statement, jc.variables);
|
2013-12-24 21:17:24 +00:00
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2017-01-23 17:28:34 +00: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-02 19:31:33 +00:00
|
|
|
// Return what happened.
|
2020-01-24 00:45:00 +00:00
|
|
|
if (result == end_execution_reason_t::ok) {
|
2016-05-02 19:31:33 +00:00
|
|
|
// Link up the processes.
|
2016-10-23 20:58:12 +00:00
|
|
|
assert(!processes.empty()); //!OCLINT(multiple unary operator)
|
2017-01-23 17:28:34 +00:00
|
|
|
j->processes = std::move(processes);
|
2013-12-31 22:37:37 +00:00
|
|
|
}
|
|
|
|
return result;
|
2013-12-24 21:17:24 +00:00
|
|
|
}
|
|
|
|
|
2019-05-05 05:12:31 +00:00
|
|
|
static bool remove_job(parser_t &parser, job_t *job) {
|
|
|
|
for (auto j = parser.jobs().begin(); j != parser.jobs().end(); ++j) {
|
2018-12-31 03:25:16 +00:00
|
|
|
if (j->get() == job) {
|
2019-05-05 05:12:31 +00:00
|
|
|
parser.jobs().erase(j);
|
2018-12-31 03:25:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-01 20:43:57 +00:00
|
|
|
/// Decide if a job node should be 'time'd.
|
|
|
|
/// For historical reasons the 'not' and 'time' prefix are "inside out". That is, it's
|
|
|
|
/// 'not time cmd'. Note that a time appearing anywhere in the pipeline affects the whole job.
|
|
|
|
/// `sleep 1 | not time true` will time the whole job!
|
|
|
|
static bool job_node_wants_timing(const ast::job_t &job_node) {
|
|
|
|
// Does our job have the job-level time prefix?
|
|
|
|
if (job_node.time) return true;
|
|
|
|
|
|
|
|
// Helper to return true if a node is 'not time ...' or 'not not time...' or...
|
|
|
|
auto is_timed_not_statement = [](const ast::statement_t &stat) {
|
|
|
|
const auto *ns = stat.contents->try_as<ast::not_statement_t>();
|
|
|
|
while (ns) {
|
|
|
|
if (ns->time) return true;
|
|
|
|
ns = ns->contents.try_as<ast::not_statement_t>();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Do we have a 'not time ...' anywhere in our pipeline?
|
|
|
|
if (is_timed_not_statement(job_node.statement)) return true;
|
|
|
|
for (const ast::job_continuation_t &jc : job_node.continuation) {
|
|
|
|
if (is_timed_not_statement(jc.statement)) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-03 18:16:51 +00:00
|
|
|
end_execution_reason_t parse_execution_context_t::run_1_job(const ast::job_t &job_node,
|
2020-01-24 00:45:00 +00:00
|
|
|
const block_t *associated_block) {
|
2019-12-18 02:10:29 +00:00
|
|
|
if (auto ret = check_end_execution()) {
|
|
|
|
return *ret;
|
2013-12-29 00:18:38 +00:00
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2020-02-17 13:21:43 +00:00
|
|
|
// We definitely do not want to execute anything if we're told we're --no-execute!
|
|
|
|
if (no_exec()) return end_execution_reason_t::ok;
|
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// Get terminal modes.
|
2013-12-24 21:17:24 +00:00
|
|
|
struct termios tmodes = {};
|
2019-05-27 21:52:48 +00:00
|
|
|
if (parser->is_interactive() && tcgetattr(STDIN_FILENO, &tmodes)) {
|
2016-10-22 18:21:13 +00:00
|
|
|
// Need real error handling here.
|
|
|
|
wperror(L"tcgetattr");
|
2020-01-24 01:34:46 +00:00
|
|
|
parser->set_last_statuses(statuses_t::just(STATUS_CMD_ERROR));
|
2020-01-24 00:45:00 +00:00
|
|
|
return end_execution_reason_t::error;
|
2013-12-24 21:17:24 +00:00
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// Increment the eval_level for the duration of this command.
|
2018-02-12 05:42:23 +00:00
|
|
|
scoped_push<int> saved_eval_level(&parser->eval_level, parser->eval_level + 1);
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// Save the node index.
|
2020-07-03 18:16:51 +00:00
|
|
|
scoped_push<const ast::job_t *> saved_node(&executing_job_node, &job_node);
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// Profiling support.
|
2014-02-09 22:04:43 +00:00
|
|
|
profile_item_t *profile_item = this->parser->create_profile_item();
|
2020-07-22 22:35:14 +00:00
|
|
|
const auto start_time = profile_item ? profile_item_t::now() : 0;
|
2014-02-09 22:04:43 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// When we encounter a block construct (e.g. while loop) in the general case, we create a "block
|
2018-02-11 03:16:35 +00:00
|
|
|
// process" containing its node. This allows us to handle block-level redirections.
|
2016-05-02 19:31:33 +00: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)) {
|
2020-07-03 18:16:51 +00:00
|
|
|
bool do_time = job_node.time.has_value();
|
2020-02-17 10:38:21 +00:00
|
|
|
// If no-exec has been given, there is nothing to time.
|
2020-07-03 18:16:51 +00:00
|
|
|
cleanup_t timer = push_timer(do_time && !no_exec());
|
Support FOO=bar syntax for passing variables to individual commands
This adds initial support for statements with prefixed variable assignments.
Statments like this are supported:
a=1 b=$a echo $b # outputs 1
Just like in other shells, the left-hand side of each assignment must
be a valid variable identifier (no quoting/escaping). Array indexing
(PATH[1]=/bin ls $PATH) is *not* yet supported, but can be added fairly
easily.
The right hand side may be any valid string token, like a command
substitution, or a brace expansion.
Since `a=* foo` is equivalent to `begin set -lx a *; foo; end`,
the assignment, like `set`, uses nullglob behavior, e.g. below command
can safely be used to check if a directory is empty.
x=/nothing/{,.}* test (count $x) -eq 0
Generic file completion is done after the equal sign, so for example
pressing tab after something like `HOME=/` completes files in the
root directory
Subcommand completion works, so something like
`GIT_DIR=repo.git and command git ` correctly calls git completions
(but the git completion does not use the variable as of now).
The variable assignment is highlighted like an argument.
Closes #6048
2019-10-23 01:13:29 +00:00
|
|
|
const block_t *block = nullptr;
|
2020-01-24 00:45:00 +00:00
|
|
|
end_execution_reason_t result =
|
2020-07-03 18:16:51 +00:00
|
|
|
this->apply_variable_assignments(nullptr, job_node.variables, &block);
|
Support FOO=bar syntax for passing variables to individual commands
This adds initial support for statements with prefixed variable assignments.
Statments like this are supported:
a=1 b=$a echo $b # outputs 1
Just like in other shells, the left-hand side of each assignment must
be a valid variable identifier (no quoting/escaping). Array indexing
(PATH[1]=/bin ls $PATH) is *not* yet supported, but can be added fairly
easily.
The right hand side may be any valid string token, like a command
substitution, or a brace expansion.
Since `a=* foo` is equivalent to `begin set -lx a *; foo; end`,
the assignment, like `set`, uses nullglob behavior, e.g. below command
can safely be used to check if a directory is empty.
x=/nothing/{,.}* test (count $x) -eq 0
Generic file completion is done after the equal sign, so for example
pressing tab after something like `HOME=/` completes files in the
root directory
Subcommand completion works, so something like
`GIT_DIR=repo.git and command git ` correctly calls git completions
(but the git completion does not use the variable as of now).
The variable assignment is highlighted like an argument.
Closes #6048
2019-10-23 01:13:29 +00:00
|
|
|
cleanup_t scope([&]() {
|
|
|
|
if (block) parser->pop_block(block);
|
|
|
|
});
|
|
|
|
|
2020-07-03 18:16:51 +00:00
|
|
|
const ast::node_t *specific_statement = job_node.statement.contents.get();
|
|
|
|
assert(specific_statement_type_is_redirectable_block(*specific_statement));
|
2020-01-24 00:45:00 +00:00
|
|
|
if (result == end_execution_reason_t::ok) {
|
2020-07-03 18:16:51 +00:00
|
|
|
switch (specific_statement->type) {
|
|
|
|
case ast::type_t::block_statement: {
|
|
|
|
result = this->run_block_statement(
|
|
|
|
*specific_statement->as<ast::block_statement_t>(), associated_block);
|
Support FOO=bar syntax for passing variables to individual commands
This adds initial support for statements with prefixed variable assignments.
Statments like this are supported:
a=1 b=$a echo $b # outputs 1
Just like in other shells, the left-hand side of each assignment must
be a valid variable identifier (no quoting/escaping). Array indexing
(PATH[1]=/bin ls $PATH) is *not* yet supported, but can be added fairly
easily.
The right hand side may be any valid string token, like a command
substitution, or a brace expansion.
Since `a=* foo` is equivalent to `begin set -lx a *; foo; end`,
the assignment, like `set`, uses nullglob behavior, e.g. below command
can safely be used to check if a directory is empty.
x=/nothing/{,.}* test (count $x) -eq 0
Generic file completion is done after the equal sign, so for example
pressing tab after something like `HOME=/` completes files in the
root directory
Subcommand completion works, so something like
`GIT_DIR=repo.git and command git ` correctly calls git completions
(but the git completion does not use the variable as of now).
The variable assignment is highlighted like an argument.
Closes #6048
2019-10-23 01:13:29 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-07-03 18:16:51 +00:00
|
|
|
case ast::type_t::if_statement: {
|
|
|
|
result = this->run_if_statement(*specific_statement->as<ast::if_statement_t>(),
|
|
|
|
associated_block);
|
Support FOO=bar syntax for passing variables to individual commands
This adds initial support for statements with prefixed variable assignments.
Statments like this are supported:
a=1 b=$a echo $b # outputs 1
Just like in other shells, the left-hand side of each assignment must
be a valid variable identifier (no quoting/escaping). Array indexing
(PATH[1]=/bin ls $PATH) is *not* yet supported, but can be added fairly
easily.
The right hand side may be any valid string token, like a command
substitution, or a brace expansion.
Since `a=* foo` is equivalent to `begin set -lx a *; foo; end`,
the assignment, like `set`, uses nullglob behavior, e.g. below command
can safely be used to check if a directory is empty.
x=/nothing/{,.}* test (count $x) -eq 0
Generic file completion is done after the equal sign, so for example
pressing tab after something like `HOME=/` completes files in the
root directory
Subcommand completion works, so something like
`GIT_DIR=repo.git and command git ` correctly calls git completions
(but the git completion does not use the variable as of now).
The variable assignment is highlighted like an argument.
Closes #6048
2019-10-23 01:13:29 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-07-03 18:16:51 +00:00
|
|
|
case ast::type_t::switch_statement: {
|
|
|
|
result = this->run_switch_statement(
|
|
|
|
*specific_statement->as<ast::switch_statement_t>());
|
Support FOO=bar syntax for passing variables to individual commands
This adds initial support for statements with prefixed variable assignments.
Statments like this are supported:
a=1 b=$a echo $b # outputs 1
Just like in other shells, the left-hand side of each assignment must
be a valid variable identifier (no quoting/escaping). Array indexing
(PATH[1]=/bin ls $PATH) is *not* yet supported, but can be added fairly
easily.
The right hand side may be any valid string token, like a command
substitution, or a brace expansion.
Since `a=* foo` is equivalent to `begin set -lx a *; foo; end`,
the assignment, like `set`, uses nullglob behavior, e.g. below command
can safely be used to check if a directory is empty.
x=/nothing/{,.}* test (count $x) -eq 0
Generic file completion is done after the equal sign, so for example
pressing tab after something like `HOME=/` completes files in the
root directory
Subcommand completion works, so something like
`GIT_DIR=repo.git and command git ` correctly calls git completions
(but the git completion does not use the variable as of now).
The variable assignment is highlighted like an argument.
Closes #6048
2019-10-23 01:13:29 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
// Other types should be impossible due to the
|
|
|
|
// specific_statement_type_is_redirectable_block check.
|
|
|
|
PARSER_DIE();
|
|
|
|
break;
|
|
|
|
}
|
2016-05-02 19:31:33 +00:00
|
|
|
}
|
2014-01-07 18:45:36 +00:00
|
|
|
}
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2019-11-19 02:34:50 +00:00
|
|
|
if (profile_item != nullptr) {
|
2020-07-22 22:35:14 +00:00
|
|
|
profile_item->duration = profile_item_t::now() - start_time;
|
2018-02-12 05:42:23 +00:00
|
|
|
profile_item->level = parser->eval_level;
|
2020-07-03 18:16:51 +00:00
|
|
|
profile_item->cmd =
|
|
|
|
profiling_cmd_name_for_redirectable_block(*specific_statement, *this->pstree);
|
2015-07-20 09:34:57 +00:00
|
|
|
profile_item->skipped = false;
|
2014-02-09 22:04:43 +00:00
|
|
|
}
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2014-02-09 22:04:43 +00:00
|
|
|
return result;
|
2013-12-24 21:17:24 +00:00
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2019-06-23 19:39:29 +00:00
|
|
|
const auto &ld = parser->libdata();
|
|
|
|
|
2019-04-30 06:08:01 +00:00
|
|
|
auto job_control_mode = get_job_control_mode();
|
2019-06-23 19:39:29 +00:00
|
|
|
bool wants_job_control =
|
|
|
|
(job_control_mode == job_control_t::all) ||
|
2020-01-30 00:08:10 +00:00
|
|
|
((job_control_mode == job_control_t::interactive) && parser->is_interactive()) ||
|
2020-05-30 21:05:07 +00:00
|
|
|
(ctx.job_group && ctx.job_group->wants_job_control());
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2019-06-23 19:39:29 +00:00
|
|
|
job_t::properties_t props{};
|
2020-07-03 18:16:51 +00:00
|
|
|
props.initial_background = job_node.bg.has_value();
|
2019-06-23 19:39:29 +00:00
|
|
|
props.skip_notification =
|
2019-05-27 21:52:48 +00:00
|
|
|
ld.is_subshell || ld.is_block || ld.is_event || !parser->is_interactive();
|
2019-06-26 18:28:27 +00:00
|
|
|
props.from_event_handler = ld.is_event;
|
2020-02-08 22:14:21 +00:00
|
|
|
props.job_control = wants_job_control;
|
2020-09-01 20:43:57 +00:00
|
|
|
props.wants_timing = job_node_wants_timing(job_node);
|
|
|
|
|
|
|
|
// It's an error to have 'time' in a background job.
|
|
|
|
if (props.wants_timing && props.initial_background) {
|
|
|
|
return this->report_error(STATUS_INVALID_ARGS, job_node, ERROR_TIME_BACKGROUND);
|
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2020-07-17 21:05:23 +00:00
|
|
|
shared_ptr<job_t> job = std::make_shared<job_t>(props, get_source(job_node));
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2019-05-12 21:42:18 +00:00
|
|
|
// We are about to populate a job. One possible argument to the job is a command substitution
|
|
|
|
// which may be interested in the job that's populating it, via '--on-job-exit caller'. Record
|
|
|
|
// the job ID here.
|
2020-02-08 23:43:21 +00:00
|
|
|
scoped_push<internal_job_id_t> caller_id(&parser->libdata().caller_id, job->internal_job_id);
|
2014-04-02 07:32:08 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// Populate the job. This may fail for reasons like command_not_found. If this fails, an error
|
|
|
|
// will have been printed.
|
2020-01-24 00:45:00 +00:00
|
|
|
end_execution_reason_t pop_result =
|
2018-01-16 00:00:19 +00:00
|
|
|
this->populate_job_from_job_node(job.get(), job_node, associated_block);
|
2020-02-08 23:43:21 +00:00
|
|
|
caller_id.restore();
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2019-05-12 21:42:18 +00:00
|
|
|
// Clean up the job on failure or cancellation.
|
2020-01-24 00:45:00 +00:00
|
|
|
if (pop_result == end_execution_reason_t::ok) {
|
2020-09-01 22:09:53 +00:00
|
|
|
// Resolve the job's group and mark if this job is the first to get it.
|
Implement cancel groups
This concerns how "internal job groups" know to stop executing when an
external command receives a "cancel signal" (SIGINT or SIGQUIT). For
example:
while true
sleep 1
end
The intent is that if any 'sleep' exits from a cancel signal, then so would
the while loop. This is why you can hit control-C to end the loop even
if the SIGINT is delivered to sleep and not fish.
Here the 'while' loop is considered an "internal job group" (no separate
pgid, bash would not fork) while each 'sleep' is a separate external
command with its own job group, pgroup, etc. Prior to this change, after
running each 'sleep', parse_execution_context_t would check to see if its
exit status was a cancel signal, and if so, stash it into an int that the
cancel checker would check. But this became unwieldy: now there were three
sources of cancellation signals (that int, the job group, and fish itself).
Introduce the notion of a "cancellation group" which is a set of job
groups that should cancel together. Even though the while loop and sleep
are in different job groups, they are in the same cancellation group. When
any job gets a SIGINT or SIGQUIT, it marks that signal in its cancellation
group, which prevents running new jobs in that group.
This reduces the number of signals to check from 3 to 2; eventually we can
teach cancellation groups how to check fish's own signals and then it will
just be 1.
2020-09-02 22:06:05 +00:00
|
|
|
job->group = job_group_t::resolve_group_for_job(*job, cancel_group, ctx.job_group);
|
2020-09-01 22:09:53 +00:00
|
|
|
assert(job->group && "Should not have a null group");
|
|
|
|
job->mut_flags().is_group_root = (job->group != ctx.job_group);
|
2020-01-30 00:39:44 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// Success. Give the job to the parser - it will clean it up.
|
2017-01-26 22:47:32 +00:00
|
|
|
parser->job_add(job);
|
2013-12-27 09:38:43 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// Check to see if this contained any external commands.
|
2013-12-26 20:55:10 +00:00
|
|
|
bool job_contained_external_command = false;
|
2017-01-26 22:47:32 +00:00
|
|
|
for (const auto &proc : job->processes) {
|
2019-03-24 19:29:25 +00:00
|
|
|
if (proc->type == process_type_t::external) {
|
2013-12-26 20:55:10 +00:00
|
|
|
job_contained_external_command = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// Actually execute the job.
|
2020-12-14 01:01:06 +00:00
|
|
|
if (!exec_job(*parser, job, block_io)) {
|
|
|
|
// No process in the job successfully launched.
|
|
|
|
// Ensure statuses are set (#7540).
|
|
|
|
if (auto statuses = job->get_statuses()) {
|
|
|
|
parser->set_last_statuses(statuses.value());
|
|
|
|
parser->libdata().status_count++;
|
|
|
|
}
|
2019-05-05 05:12:31 +00:00
|
|
|
remove_job(*this->parser, job.get());
|
2018-11-18 23:14:08 +00:00
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2019-10-22 00:21:40 +00:00
|
|
|
// Update universal variables on external conmmands.
|
2019-04-10 23:15:47 +00:00
|
|
|
// TODO: justify this, why not on every command?
|
2016-05-02 19:31:33 +00:00
|
|
|
if (job_contained_external_command) {
|
2019-04-10 23:15:47 +00:00
|
|
|
parser->vars().universal_barrier();
|
2013-12-24 21:17:24 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2019-11-19 02:34:50 +00:00
|
|
|
if (profile_item != nullptr) {
|
2020-07-22 22:35:14 +00:00
|
|
|
profile_item->duration = profile_item_t::now() - start_time;
|
2018-02-12 05:42:23 +00:00
|
|
|
profile_item->level = parser->eval_level;
|
2017-01-26 22:47:32 +00:00
|
|
|
profile_item->cmd = job ? job->command() : wcstring();
|
2020-01-24 00:45:00 +00:00
|
|
|
profile_item->skipped = (pop_result != end_execution_reason_t::ok);
|
2013-12-24 21:17:24 +00:00
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2019-04-30 03:58:58 +00:00
|
|
|
job_reap(*parser, false); // clean up jobs
|
2019-12-18 02:10:29 +00:00
|
|
|
return pop_result;
|
2013-12-24 21:17:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 00:45:00 +00:00
|
|
|
end_execution_reason_t parse_execution_context_t::run_job_conjunction(
|
2020-07-03 18:16:51 +00:00
|
|
|
const ast::job_conjunction_t &job_expr, const block_t *associated_block) {
|
|
|
|
if (auto reason = check_end_execution()) {
|
|
|
|
return *reason;
|
|
|
|
}
|
|
|
|
end_execution_reason_t result = run_1_job(job_expr.job, associated_block);
|
|
|
|
|
|
|
|
for (const ast::job_conjunction_continuation_t &jc : job_expr.continuations) {
|
|
|
|
if (result != end_execution_reason_t::ok) {
|
|
|
|
return result;
|
|
|
|
}
|
2019-12-18 02:10:29 +00:00
|
|
|
if (auto reason = check_end_execution()) {
|
2020-07-03 18:16:51 +00:00
|
|
|
return *reason;
|
2019-12-18 02:10:29 +00:00
|
|
|
}
|
2020-07-03 18:16:51 +00:00
|
|
|
// Check the conjunction type.
|
2018-03-02 02:30:48 +00:00
|
|
|
bool skip = false;
|
2020-07-03 18:16:51 +00:00
|
|
|
switch (jc.conjunction.type) {
|
2020-07-08 18:12:15 +00:00
|
|
|
case parse_token_type_t::andand:
|
2020-07-03 18:16:51 +00:00
|
|
|
// AND. Skip if the last job failed.
|
|
|
|
skip = parser->get_last_status() != 0;
|
|
|
|
break;
|
2020-07-08 18:12:15 +00:00
|
|
|
case parse_token_type_t::oror:
|
2020-07-03 18:16:51 +00:00
|
|
|
// OR. Skip if the last job succeeded.
|
|
|
|
skip = parser->get_last_status() == 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DIE("Unexpected job conjunction type");
|
2018-03-02 02:30:48 +00:00
|
|
|
}
|
2019-05-05 10:09:25 +00:00
|
|
|
if (!skip) {
|
2020-07-03 18:16:51 +00:00
|
|
|
result = run_1_job(jc.job, associated_block);
|
2018-03-02 02:30:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-07-03 18:16:51 +00:00
|
|
|
end_execution_reason_t parse_execution_context_t::test_and_run_1_job_conjunction(
|
|
|
|
const ast::job_conjunction_t &jc, const block_t *associated_block) {
|
|
|
|
// Test this job conjunction if it has an 'and' or 'or' decorator.
|
|
|
|
// If it passes, then run it.
|
|
|
|
if (auto reason = check_end_execution()) {
|
|
|
|
return *reason;
|
|
|
|
}
|
|
|
|
// Maybe skip the job if it has a leading and/or.
|
|
|
|
bool skip = false;
|
|
|
|
if (jc.decorator.has_value()) {
|
|
|
|
switch (jc.decorator->kw) {
|
|
|
|
case parse_keyword_t::kw_and:
|
|
|
|
// AND. Skip if the last job failed.
|
|
|
|
skip = parser->get_last_status() != 0;
|
|
|
|
break;
|
|
|
|
case parse_keyword_t::kw_or:
|
|
|
|
// OR. Skip if the last job succeeded.
|
|
|
|
skip = parser->get_last_status() == 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DIE("Unexpected keyword");
|
2019-12-18 02:10:29 +00:00
|
|
|
}
|
2020-07-03 18:16:51 +00:00
|
|
|
}
|
|
|
|
// Skipping is treated as success.
|
|
|
|
if (skip) {
|
|
|
|
return end_execution_reason_t::ok;
|
|
|
|
} else {
|
|
|
|
return this->run_job_conjunction(jc, associated_block);
|
|
|
|
}
|
|
|
|
}
|
2018-03-03 02:09:16 +00:00
|
|
|
|
2020-07-03 18:16:51 +00:00
|
|
|
end_execution_reason_t parse_execution_context_t::run_job_list(const ast::job_list_t &job_list_node,
|
|
|
|
const block_t *associated_block) {
|
|
|
|
auto result = end_execution_reason_t::ok;
|
|
|
|
for (const ast::job_conjunction_t &jc : job_list_node) {
|
|
|
|
result = test_and_run_1_job_conjunction(jc, associated_block);
|
2013-12-24 21:17:24 +00:00
|
|
|
}
|
2020-07-03 18:16:51 +00:00
|
|
|
// Returns the result of the last job executed or skipped.
|
|
|
|
return result;
|
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2020-07-03 18:16:51 +00:00
|
|
|
end_execution_reason_t parse_execution_context_t::run_job_list(
|
|
|
|
const ast::andor_job_list_t &job_list_node, const block_t *associated_block) {
|
|
|
|
auto result = end_execution_reason_t::ok;
|
|
|
|
for (const ast::andor_job_t &aoj : job_list_node) {
|
|
|
|
result = test_and_run_1_job_conjunction(aoj.job, associated_block);
|
|
|
|
}
|
2018-03-03 02:09:16 +00:00
|
|
|
// Returns the result of the last job executed or skipped.
|
2013-12-26 21:24:10 +00:00
|
|
|
return result;
|
2013-12-26 20:24:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 18:16:51 +00:00
|
|
|
end_execution_reason_t parse_execution_context_t::eval_node(const ast::statement_t &statement,
|
2020-01-24 00:45:00 +00:00
|
|
|
const block_t *associated_block) {
|
2020-07-03 18:16:51 +00:00
|
|
|
// Note we only expect block-style statements here. No not statements.
|
2020-01-24 00:45:00 +00:00
|
|
|
enum end_execution_reason_t status = end_execution_reason_t::ok;
|
2020-07-03 18:16:51 +00:00
|
|
|
const ast::node_t *contents = statement.contents.get();
|
|
|
|
if (const auto *block = contents->try_as<ast::block_statement_t>()) {
|
|
|
|
status = this->run_block_statement(*block, associated_block);
|
|
|
|
} else if (const auto *ifstat = contents->try_as<ast::if_statement_t>()) {
|
|
|
|
status = this->run_if_statement(*ifstat, associated_block);
|
|
|
|
} else if (const auto *switchstat = contents->try_as<ast::switch_statement_t>()) {
|
|
|
|
status = this->run_switch_statement(*switchstat);
|
2018-02-11 03:16:35 +00:00
|
|
|
} else {
|
2020-07-03 18:16:51 +00:00
|
|
|
FLOGF(error, L"Unexpected node %ls found in %s", statement.describe().c_str(),
|
2019-06-04 03:30:48 +00:00
|
|
|
__FUNCTION__);
|
2018-02-11 03:16:35 +00:00
|
|
|
abort();
|
2013-12-27 09:38:43 +00:00
|
|
|
}
|
2018-02-11 03:16:35 +00:00
|
|
|
return status;
|
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2020-07-03 18:16:51 +00:00
|
|
|
end_execution_reason_t parse_execution_context_t::eval_node(const ast::job_list_t &job_list,
|
2020-01-24 00:45:00 +00:00
|
|
|
const block_t *associated_block) {
|
2019-11-10 20:36:46 +00:00
|
|
|
assert(associated_block && "Null block");
|
|
|
|
|
|
|
|
// Check for infinite recursion: a function which immediately calls itself..
|
2018-02-11 03:16:35 +00:00
|
|
|
wcstring func_name;
|
2020-07-03 18:16:51 +00:00
|
|
|
if (const auto *infinite_recursive_node =
|
|
|
|
this->infinite_recursive_statement_in_job_list(job_list, &func_name)) {
|
2018-02-11 03:16:35 +00:00
|
|
|
// We have an infinite recursion.
|
2020-07-03 18:16:51 +00:00
|
|
|
return this->report_error(STATUS_CMD_ERROR, *infinite_recursive_node,
|
2020-01-24 01:34:46 +00:00
|
|
|
INFINITE_FUNC_RECURSION_ERR_MSG, func_name.c_str());
|
2018-02-11 03:16:35 +00:00
|
|
|
}
|
2019-11-10 20:36:46 +00:00
|
|
|
|
|
|
|
// Check for stack overflow. The TOP check ensures we only do this for function calls.
|
2019-12-22 23:37:14 +00:00
|
|
|
if (associated_block->type() == block_type_t::top && parser->function_stack_is_overflowing()) {
|
2020-01-24 01:34:46 +00:00
|
|
|
return this->report_error(STATUS_CMD_ERROR, job_list, CALL_STACK_LIMIT_EXCEEDED_ERR_MSG);
|
2019-11-10 20:36:46 +00:00
|
|
|
}
|
|
|
|
return this->run_job_list(job_list, associated_block);
|
2013-12-24 21:17:24 +00:00
|
|
|
}
|
2014-03-02 00:04:13 +00:00
|
|
|
|
2020-07-03 18:16:51 +00:00
|
|
|
int parse_execution_context_t::line_offset_of_node(const ast::job_t *node) {
|
2016-05-02 19:31:33 +00:00
|
|
|
// If we're not executing anything, return -1.
|
2018-02-12 04:08:40 +00:00
|
|
|
if (!node) {
|
2014-03-02 00:04:13 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// If for some reason we're executing a node without source, return -1.
|
2020-07-03 18:16:51 +00:00
|
|
|
auto range = node->try_source_range();
|
2018-02-12 04:08:40 +00:00
|
|
|
if (!range) {
|
2014-03-02 00:04:13 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2016-05-02 19:31:33 +00:00
|
|
|
|
2018-02-12 04:08:40 +00:00
|
|
|
return this->line_offset_of_character_at_offset(range->start);
|
2014-09-28 00:32:54 +00:00
|
|
|
}
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2016-05-02 19:31:33 +00: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-22 22:40:15 +00:00
|
|
|
assert(offset <= pstree->src.size());
|
2014-03-02 00:04:13 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// Easy hack to handle 0.
|
|
|
|
if (offset == 0) {
|
2014-03-16 23:45:00 +00:00
|
|
|
return 0;
|
2014-03-02 00:04:13 +00:00
|
|
|
}
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2016-05-02 19:31:33 +00: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-22 22:40:15 +00:00
|
|
|
const wchar_t *str = pstree->src.c_str();
|
2016-05-02 19:31:33 +00:00
|
|
|
if (offset > cached_lineno_offset) {
|
2014-03-02 00:04:13 +00:00
|
|
|
size_t i;
|
2018-11-14 10:24:55 +00:00
|
|
|
for (i = cached_lineno_offset; i < offset && str[i] != L'\0'; i++) {
|
2016-05-02 19:31:33 +00:00
|
|
|
// Add one for every newline we find in the range [cached_lineno_offset, offset).
|
|
|
|
if (str[i] == L'\n') {
|
2014-03-02 00:04:13 +00:00
|
|
|
cached_lineno_count++;
|
|
|
|
}
|
|
|
|
}
|
2016-05-02 19:31:33 +00: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 00:04:13 +00:00
|
|
|
cached_lineno_count--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cached_lineno_offset = offset;
|
|
|
|
}
|
2014-03-16 23:45:00 +00:00
|
|
|
return cached_lineno_count;
|
|
|
|
}
|
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
int parse_execution_context_t::get_current_line_number() {
|
2014-03-16 23:45:00 +00:00
|
|
|
int line_number = -1;
|
2018-02-12 04:08:40 +00:00
|
|
|
int line_offset = this->line_offset_of_node(this->executing_job_node);
|
2016-05-02 19:31:33 +00:00
|
|
|
if (line_offset >= 0) {
|
|
|
|
// The offset is 0 based; the number is 1 based.
|
2014-03-16 23:45:00 +00:00
|
|
|
line_number = line_offset + 1;
|
|
|
|
}
|
|
|
|
return line_number;
|
2014-03-02 00:04:13 +00:00
|
|
|
}
|
2014-03-17 05:06:32 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
int parse_execution_context_t::get_current_source_offset() const {
|
2014-03-17 05:06:32 +00:00
|
|
|
int result = -1;
|
2018-02-12 04:08:40 +00:00
|
|
|
if (executing_job_node) {
|
2020-07-03 18:16:51 +00:00
|
|
|
if (auto range = executing_job_node->try_source_range()) {
|
2018-02-12 04:08:40 +00:00
|
|
|
result = static_cast<int>(range->start);
|
2014-03-17 05:06:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|