2016-05-03 03:31:33 +08:00
|
|
|
// Provides the "linkage" between a parse_node_tree_t and actual execution structures (job_t, etc.).
|
2013-12-25 05:17:24 +08:00
|
|
|
#ifndef FISH_PARSE_EXECUTION_H
|
|
|
|
#define FISH_PARSE_EXECUTION_H
|
|
|
|
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <stddef.h>
|
2016-04-21 14:00:54 +08:00
|
|
|
|
2015-07-25 23:14:25 +08:00
|
|
|
#include "common.h"
|
|
|
|
#include "io.h"
|
|
|
|
#include "parse_constants.h"
|
2013-12-25 05:17:24 +08:00
|
|
|
#include "parse_tree.h"
|
|
|
|
#include "proc.h"
|
|
|
|
|
2019-05-20 05:44:17 +08:00
|
|
|
class block_t;
|
2020-01-16 09:14:47 +08:00
|
|
|
class operation_context_t;
|
2015-07-25 23:14:25 +08:00
|
|
|
class parser_t;
|
2013-12-25 05:17:24 +08:00
|
|
|
|
2019-12-18 10:10:29 +08:00
|
|
|
/// An eval_result represents evaluation errors including wildcards which failed to match, syntax
|
|
|
|
/// errors, or other expansion errors. It also tracks when evaluation was skipped due to signal
|
|
|
|
/// cancellation. Note it does not track the exit status of commands.
|
2020-01-24 08:45:00 +08:00
|
|
|
enum class end_execution_reason_t {
|
2019-12-18 10:10:29 +08:00
|
|
|
/// Evaluation was successfull.
|
|
|
|
ok,
|
|
|
|
|
|
|
|
/// Evaluation was skipped due to control flow (break or return).
|
|
|
|
control_flow,
|
|
|
|
|
|
|
|
/// Evaluation was cancelled, e.g. because of a signal or exit.
|
|
|
|
cancelled,
|
|
|
|
|
|
|
|
/// A parse error or failed expansion (but not an error exit status from a command).
|
|
|
|
error,
|
2014-01-01 06:37:37 +08:00
|
|
|
};
|
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
class parse_execution_context_t {
|
|
|
|
private:
|
2017-12-23 06:40:15 +08:00
|
|
|
parsed_source_ref_t pstree;
|
2016-05-03 03:31:33 +08:00
|
|
|
parser_t *const parser;
|
2020-01-16 09:14:47 +08:00
|
|
|
const operation_context_t &ctx;
|
2018-02-12 12:08:40 +08:00
|
|
|
// The currently executing job node, used to indicate the line number.
|
|
|
|
tnode_t<grammar::job> executing_job_node{};
|
2016-05-03 03:31:33 +08:00
|
|
|
// Cached line number information.
|
2017-12-23 06:40:15 +08:00
|
|
|
size_t cached_lineno_offset = 0;
|
|
|
|
int cached_lineno_count = 0;
|
2019-12-09 05:03:42 +08:00
|
|
|
// The lineage for any jobs created by this context.
|
|
|
|
const job_lineage_t lineage;
|
2016-05-03 03:31:33 +08:00
|
|
|
// No copying allowed.
|
2018-11-04 15:58:44 +08:00
|
|
|
parse_execution_context_t(const parse_execution_context_t &) = delete;
|
|
|
|
parse_execution_context_t &operator=(const parse_execution_context_t &) = delete;
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2019-12-18 10:10:29 +08:00
|
|
|
// Check to see if we should end execution.
|
|
|
|
// \return the eval result to end with, or none() to continue on.
|
2020-01-24 08:45:00 +08:00
|
|
|
// This will never return end_execution_reason_t::ok.
|
|
|
|
maybe_t<end_execution_reason_t> check_end_execution() const;
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2020-01-24 09:34:46 +08:00
|
|
|
// Report an error, setting $status to \p status. Always returns
|
|
|
|
// 'end_execution_reason_t::error'.
|
|
|
|
end_execution_reason_t report_error(int status, const parse_node_t &node, const wchar_t *fmt,
|
|
|
|
...) const;
|
|
|
|
end_execution_reason_t report_errors(int status, const parse_error_list_t &error_list) const;
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
/// Command not found support.
|
2020-01-24 08:45:00 +08:00
|
|
|
end_execution_reason_t handle_command_not_found(const wcstring &cmd,
|
|
|
|
tnode_t<grammar::plain_statement> statement,
|
|
|
|
int err_code);
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Utilities
|
2013-12-25 05:17:24 +08:00
|
|
|
wcstring get_source(const parse_node_t &node) const;
|
2018-01-14 07:36:14 +08:00
|
|
|
tnode_t<grammar::plain_statement> infinite_recursive_statement_in_job_list(
|
2018-01-14 17:26:28 +08:00
|
|
|
tnode_t<grammar::job_list> job_list, wcstring *out_func_name) const;
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2018-08-26 16:41:45 +08:00
|
|
|
// Expand a command which may contain variables, producing an expand command and possibly
|
|
|
|
// arguments. Prints an error message on error.
|
2020-01-24 08:45:00 +08:00
|
|
|
end_execution_reason_t expand_command(tnode_t<grammar::plain_statement> statement,
|
|
|
|
wcstring *out_cmd, wcstring_list_t *out_args) const;
|
2018-08-26 16:41:45 +08:00
|
|
|
|
2018-03-03 10:09:16 +08:00
|
|
|
/// Return whether we should skip a job with the given bool statement type.
|
2019-12-20 12:41:53 +08:00
|
|
|
bool should_skip(parse_job_decoration_t type) const;
|
2018-03-03 10:09:16 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
/// Indicates whether a job is a simple block (one block, no redirections).
|
2018-01-16 08:00:19 +08:00
|
|
|
bool job_is_simple_block(tnode_t<grammar::job> job) const;
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2018-01-16 07:15:45 +08:00
|
|
|
enum process_type_t process_type_for_command(tnode_t<grammar::plain_statement> statement,
|
2016-05-03 03:31:33 +08:00
|
|
|
const wcstring &cmd) const;
|
2020-01-24 08:45:00 +08:00
|
|
|
end_execution_reason_t apply_variable_assignments(
|
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 09:13:29 +08:00
|
|
|
process_t *proc, tnode_t<grammar::variable_assignments> variable_assignments,
|
|
|
|
const block_t **block);
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// These create process_t structures from statements.
|
2020-01-24 08:45:00 +08:00
|
|
|
end_execution_reason_t populate_job_process(
|
|
|
|
job_t *job, process_t *proc, tnode_t<grammar::statement> statement,
|
|
|
|
tnode_t<grammar::variable_assignments> variable_assignments);
|
|
|
|
end_execution_reason_t populate_not_process(job_t *job, process_t *proc,
|
|
|
|
tnode_t<grammar::not_statement> not_statement);
|
|
|
|
end_execution_reason_t populate_plain_process(job_t *job, process_t *proc,
|
|
|
|
tnode_t<grammar::plain_statement> statement);
|
2018-01-16 07:37:13 +08:00
|
|
|
|
|
|
|
template <typename Type>
|
2020-01-24 08:45:00 +08:00
|
|
|
end_execution_reason_t populate_block_process(job_t *job, process_t *proc,
|
|
|
|
tnode_t<grammar::statement> statement,
|
|
|
|
tnode_t<Type> specific_statement);
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// These encapsulate the actual logic of various (block) statements.
|
2020-01-24 08:45:00 +08:00
|
|
|
end_execution_reason_t run_block_statement(tnode_t<grammar::block_statement> statement,
|
|
|
|
const block_t *associated_block);
|
|
|
|
end_execution_reason_t run_for_statement(tnode_t<grammar::for_header> header,
|
|
|
|
tnode_t<grammar::job_list> contents);
|
|
|
|
end_execution_reason_t run_if_statement(tnode_t<grammar::if_statement> statement,
|
|
|
|
const block_t *associated_block);
|
|
|
|
end_execution_reason_t run_switch_statement(tnode_t<grammar::switch_statement> statement);
|
|
|
|
end_execution_reason_t run_while_statement(tnode_t<grammar::while_header> header,
|
|
|
|
tnode_t<grammar::job_list> contents,
|
|
|
|
const block_t *associated_block);
|
|
|
|
end_execution_reason_t run_function_statement(tnode_t<grammar::block_statement> statement,
|
|
|
|
tnode_t<grammar::function_header> header);
|
|
|
|
end_execution_reason_t run_begin_statement(tnode_t<grammar::job_list> contents);
|
2016-05-03 03:31:33 +08:00
|
|
|
|
|
|
|
enum globspec_t { failglob, nullglob };
|
2018-01-16 07:15:45 +08:00
|
|
|
using argument_node_list_t = std::vector<tnode_t<grammar::argument>>;
|
2020-01-24 08:45:00 +08:00
|
|
|
end_execution_reason_t expand_arguments_from_nodes(const argument_node_list_t &argument_nodes,
|
|
|
|
wcstring_list_t *out_arguments,
|
|
|
|
globspec_t glob_behavior);
|
2016-05-03 03:31:33 +08:00
|
|
|
|
2020-01-24 09:34:46 +08:00
|
|
|
// Determines the list of redirections for a node.
|
|
|
|
end_execution_reason_t determine_redirections(
|
|
|
|
tnode_t<grammar::arguments_or_redirections_list> node,
|
|
|
|
redirection_spec_list_t *out_redirections);
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2020-01-24 08:45:00 +08:00
|
|
|
end_execution_reason_t run_1_job(tnode_t<grammar::job> job, const block_t *associated_block);
|
|
|
|
end_execution_reason_t run_job_conjunction(tnode_t<grammar::job_conjunction> job_expr,
|
|
|
|
const block_t *associated_block);
|
2018-01-16 08:08:06 +08:00
|
|
|
template <typename Type>
|
2020-01-24 08:45:00 +08:00
|
|
|
end_execution_reason_t run_job_list(tnode_t<Type> job_list_node,
|
|
|
|
const block_t *associated_block);
|
|
|
|
end_execution_reason_t populate_job_from_job_node(job_t *j, tnode_t<grammar::job> job_node,
|
|
|
|
const block_t *associated_block);
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2018-02-12 12:08:40 +08:00
|
|
|
// Returns the line number of the node. Not const since it touches cached_lineno_offset.
|
|
|
|
int line_offset_of_node(tnode_t<grammar::job> node);
|
2019-11-19 08:54:36 +08:00
|
|
|
int line_offset_of_character_at_offset(size_t offset);
|
2014-03-17 07:45:00 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
public:
|
2020-01-16 09:14:47 +08:00
|
|
|
parse_execution_context_t(parsed_source_ref_t pstree, parser_t *p,
|
|
|
|
const operation_context_t &ctx, job_lineage_t lineage);
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
/// Returns the current line number, indexed from 1. Not const since it touches
|
|
|
|
/// cached_lineno_offset.
|
2014-03-02 08:04:13 +08:00
|
|
|
int get_current_line_number();
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
/// Returns the source offset, or -1.
|
2014-03-17 13:06:32 +08:00
|
|
|
int get_current_source_offset() const;
|
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
/// Returns the source string.
|
2017-12-23 06:40:15 +08:00
|
|
|
const wcstring &get_source() const { return pstree->src; }
|
|
|
|
|
|
|
|
/// Return the parse tree.
|
|
|
|
const parse_node_tree_t &tree() const { return pstree->tree; }
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2018-02-11 11:16:35 +08:00
|
|
|
/// Start executing at the given node. Returns 0 if there was no error, 1 if there was an
|
2016-05-03 03:31:33 +08:00
|
|
|
/// error.
|
2020-01-24 08:45:00 +08:00
|
|
|
end_execution_reason_t eval_node(tnode_t<grammar::statement> statement,
|
|
|
|
const block_t *associated_block);
|
|
|
|
end_execution_reason_t eval_node(tnode_t<grammar::job_list> job_list,
|
|
|
|
const block_t *associated_block);
|
2013-12-25 05:17:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|