2016-05-02 19:31:33 +00:00
|
|
|
// Provides the "linkage" between a parse_node_tree_t and actual execution structures (job_t, etc.).
|
2013-12-24 21:17:24 +00:00
|
|
|
#ifndef FISH_PARSE_EXECUTION_H
|
|
|
|
#define FISH_PARSE_EXECUTION_H
|
|
|
|
|
2015-07-25 15:14:25 +00:00
|
|
|
#include <stddef.h>
|
2016-04-21 06:00:54 +00:00
|
|
|
|
2015-07-25 15:14:25 +00:00
|
|
|
#include "common.h"
|
|
|
|
#include "io.h"
|
|
|
|
#include "parse_constants.h"
|
2013-12-24 21:17:24 +00:00
|
|
|
#include "parse_tree.h"
|
|
|
|
#include "proc.h"
|
|
|
|
|
2019-05-19 21:44:17 +00:00
|
|
|
class block_t;
|
2015-07-25 15:14:25 +00:00
|
|
|
class parser_t;
|
2013-12-24 21:17:24 +00:00
|
|
|
|
2019-12-18 01:31:18 +00:00
|
|
|
enum parse_execution_result_t {
|
|
|
|
/// The job was successfully executed (though it have failed on its own).
|
|
|
|
parse_execution_success,
|
|
|
|
/// The job did not execute due to some error (e.g. failed to wildcard expand). An error will
|
|
|
|
/// have been printed and proc_last_status will have been set.
|
|
|
|
parse_execution_errored,
|
|
|
|
/// The job was cancelled (e.g. Ctrl-C).
|
|
|
|
parse_execution_cancelled,
|
2013-12-31 22:37:37 +00:00
|
|
|
};
|
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
class parse_execution_context_t {
|
|
|
|
private:
|
2017-12-22 22:40:15 +00:00
|
|
|
parsed_source_ref_t pstree;
|
2016-05-02 19:31:33 +00:00
|
|
|
parser_t *const parser;
|
2018-02-12 04:08:40 +00:00
|
|
|
// The currently executing job node, used to indicate the line number.
|
|
|
|
tnode_t<grammar::job> executing_job_node{};
|
2016-05-02 19:31:33 +00:00
|
|
|
// Cached line number information.
|
2017-12-22 22:40:15 +00:00
|
|
|
size_t cached_lineno_offset = 0;
|
|
|
|
int cached_lineno_count = 0;
|
2019-12-08 21:03:42 +00:00
|
|
|
// The lineage for any jobs created by this context.
|
|
|
|
const job_lineage_t lineage;
|
2016-05-02 19:31:33 +00:00
|
|
|
// No copying allowed.
|
2018-11-04 07:58:44 +00: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 09:40:40 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// Should I cancel?
|
2013-12-29 00:18:38 +00:00
|
|
|
bool should_cancel_execution(const block_t *block) const;
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// Ways that we can stop executing a block. These are in a sort of ascending order of
|
|
|
|
// importance, e.g. `exit` should trump `break`.
|
|
|
|
enum execution_cancellation_reason_t {
|
2013-12-30 00:23:26 +00:00
|
|
|
execution_cancellation_none,
|
|
|
|
execution_cancellation_loop_control,
|
|
|
|
execution_cancellation_skip,
|
|
|
|
execution_cancellation_exit
|
|
|
|
};
|
|
|
|
execution_cancellation_reason_t cancellation_reason(const block_t *block) const;
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2019-12-18 01:31:18 +00:00
|
|
|
// Report an error. Always returns true.
|
|
|
|
parse_execution_result_t report_error(const parse_node_t &node, const wchar_t *fmt, ...) const;
|
|
|
|
parse_execution_result_t report_errors(const parse_error_list_t &error_list) const;
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// Wildcard error helper.
|
2019-12-18 01:31:18 +00:00
|
|
|
parse_execution_result_t report_unmatched_wildcard_error(
|
|
|
|
const parse_node_t &unmatched_wildcard) const;
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
/// Command not found support.
|
2019-12-18 01:31:18 +00:00
|
|
|
parse_execution_result_t handle_command_not_found(const wcstring &cmd,
|
|
|
|
tnode_t<grammar::plain_statement> statement,
|
|
|
|
int err_code);
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// Utilities
|
2013-12-24 21:17:24 +00:00
|
|
|
wcstring get_source(const parse_node_t &node) const;
|
2018-01-13 23:36:14 +00:00
|
|
|
tnode_t<grammar::plain_statement> infinite_recursive_statement_in_job_list(
|
2018-01-14 09:26:28 +00:00
|
|
|
tnode_t<grammar::job_list> job_list, wcstring *out_func_name) const;
|
2017-09-09 04:14:26 +00:00
|
|
|
bool is_function_context() const;
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2018-08-26 08:41:45 +00:00
|
|
|
// Expand a command which may contain variables, producing an expand command and possibly
|
|
|
|
// arguments. Prints an error message on error.
|
2019-12-18 01:31:18 +00:00
|
|
|
parse_execution_result_t expand_command(tnode_t<grammar::plain_statement> statement,
|
|
|
|
wcstring *out_cmd, wcstring_list_t *out_args) const;
|
2018-08-26 08:41:45 +00:00
|
|
|
|
2018-03-03 02:09:16 +00:00
|
|
|
/// Return whether we should skip a job with the given bool statement type.
|
|
|
|
bool should_skip(parse_bool_statement_type_t type) const;
|
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
/// Indicates whether a job is a simple block (one block, no redirections).
|
2018-01-16 00:00:19 +00:00
|
|
|
bool job_is_simple_block(tnode_t<grammar::job> job) const;
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2018-01-15 23:15:45 +00:00
|
|
|
enum process_type_t process_type_for_command(tnode_t<grammar::plain_statement> statement,
|
2016-05-02 19:31:33 +00:00
|
|
|
const wcstring &cmd) const;
|
2019-12-18 01:31:18 +00:00
|
|
|
parse_execution_result_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 01:13:29 +00:00
|
|
|
process_t *proc, tnode_t<grammar::variable_assignments> variable_assignments,
|
|
|
|
const block_t **block);
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// These create process_t structures from statements.
|
2019-12-18 01:31:18 +00:00
|
|
|
parse_execution_result_t populate_job_process(
|
|
|
|
job_t *job, process_t *proc, tnode_t<grammar::statement> statement,
|
|
|
|
tnode_t<grammar::variable_assignments> variable_assignments);
|
|
|
|
parse_execution_result_t populate_not_process(job_t *job, process_t *proc,
|
|
|
|
tnode_t<grammar::not_statement> not_statement);
|
|
|
|
parse_execution_result_t populate_plain_process(job_t *job, process_t *proc,
|
|
|
|
tnode_t<grammar::plain_statement> statement);
|
2018-01-15 23:37:13 +00:00
|
|
|
|
|
|
|
template <typename Type>
|
2019-12-18 01:31:18 +00:00
|
|
|
parse_execution_result_t populate_block_process(job_t *job, process_t *proc,
|
|
|
|
tnode_t<grammar::statement> statement,
|
|
|
|
tnode_t<Type> specific_statement);
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
// These encapsulate the actual logic of various (block) statements.
|
2019-12-18 01:31:18 +00:00
|
|
|
parse_execution_result_t run_block_statement(tnode_t<grammar::block_statement> statement,
|
|
|
|
const block_t *associated_block);
|
|
|
|
parse_execution_result_t run_for_statement(tnode_t<grammar::for_header> header,
|
|
|
|
tnode_t<grammar::job_list> contents);
|
|
|
|
parse_execution_result_t run_if_statement(tnode_t<grammar::if_statement> statement,
|
|
|
|
const block_t *associated_block);
|
|
|
|
parse_execution_result_t run_switch_statement(tnode_t<grammar::switch_statement> statement);
|
|
|
|
parse_execution_result_t run_while_statement(tnode_t<grammar::while_header> header,
|
|
|
|
tnode_t<grammar::job_list> contents,
|
|
|
|
const block_t *associated_block);
|
|
|
|
parse_execution_result_t run_function_statement(tnode_t<grammar::function_header> header,
|
|
|
|
tnode_t<grammar::job_list> body);
|
|
|
|
parse_execution_result_t run_begin_statement(tnode_t<grammar::job_list> contents);
|
2016-05-02 19:31:33 +00:00
|
|
|
|
|
|
|
enum globspec_t { failglob, nullglob };
|
2018-01-15 23:15:45 +00:00
|
|
|
using argument_node_list_t = std::vector<tnode_t<grammar::argument>>;
|
2019-12-18 01:31:18 +00:00
|
|
|
parse_execution_result_t expand_arguments_from_nodes(const argument_node_list_t &argument_nodes,
|
|
|
|
wcstring_list_t *out_arguments,
|
|
|
|
globspec_t glob_behavior);
|
2016-05-02 19:31:33 +00:00
|
|
|
|
2019-12-13 00:44:24 +00:00
|
|
|
// Determines the list of redirections for a node. Returns none() on failure, for example, an
|
|
|
|
// invalid fd.
|
|
|
|
bool determine_redirections(tnode_t<grammar::arguments_or_redirections_list> node,
|
|
|
|
redirection_spec_list_t *out_redirs);
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2019-12-18 01:31:18 +00:00
|
|
|
parse_execution_result_t run_1_job(tnode_t<grammar::job> job, const block_t *associated_block);
|
|
|
|
parse_execution_result_t run_job_conjunction(tnode_t<grammar::job_conjunction> job_expr,
|
|
|
|
const block_t *associated_block);
|
2018-01-16 00:08:06 +00:00
|
|
|
template <typename Type>
|
2019-12-18 01:31:18 +00:00
|
|
|
parse_execution_result_t run_job_list(tnode_t<Type> job_list_node,
|
|
|
|
const block_t *associated_block);
|
|
|
|
parse_execution_result_t populate_job_from_job_node(job_t *j, tnode_t<grammar::job> job_node,
|
|
|
|
const block_t *associated_block);
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2018-02-12 04:08:40 +00: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 00:54:36 +00:00
|
|
|
int line_offset_of_character_at_offset(size_t offset);
|
2014-03-16 23:45:00 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
public:
|
2019-12-08 21:03:42 +00:00
|
|
|
parse_execution_context_t(parsed_source_ref_t pstree, parser_t *p, job_lineage_t lineage);
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
/// Returns the current line number, indexed from 1. Not const since it touches
|
|
|
|
/// cached_lineno_offset.
|
2014-03-02 00:04:13 +00:00
|
|
|
int get_current_line_number();
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
/// Returns the source offset, or -1.
|
2014-03-17 05:06:32 +00:00
|
|
|
int get_current_source_offset() const;
|
|
|
|
|
2016-05-02 19:31:33 +00:00
|
|
|
/// Returns the source string.
|
2017-12-22 22:40:15 +00: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 09:40:40 +00:00
|
|
|
|
2018-02-11 03:16:35 +00:00
|
|
|
/// Start executing at the given node. Returns 0 if there was no error, 1 if there was an
|
2016-05-02 19:31:33 +00:00
|
|
|
/// error.
|
2019-12-18 01:31:18 +00:00
|
|
|
parse_execution_result_t eval_node(tnode_t<grammar::statement> statement,
|
|
|
|
const block_t *associated_block);
|
|
|
|
parse_execution_result_t eval_node(tnode_t<grammar::job_list> job_list,
|
|
|
|
const block_t *associated_block);
|
2013-12-24 21:17:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|