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"
|
|
|
|
|
2015-07-25 23:14:25 +08:00
|
|
|
class parser_t;
|
2013-12-29 08:18:38 +08:00
|
|
|
struct block_t;
|
2013-12-25 05:17:24 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
enum parse_execution_result_t {
|
|
|
|
/// The job was successfully executed (though it have failed on its own).
|
2014-01-01 06:37:37 +08:00
|
|
|
parse_execution_success,
|
2016-05-03 03:31:33 +08:00
|
|
|
/// 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.
|
2014-01-01 06:37:37 +08:00
|
|
|
parse_execution_errored,
|
2016-05-03 03:31:33 +08:00
|
|
|
/// The job was cancelled (e.g. Ctrl-C).
|
2014-01-01 06:37:37 +08:00
|
|
|
parse_execution_cancelled,
|
2016-05-03 03:31:33 +08:00
|
|
|
/// The job was skipped (e.g. due to a not-taken 'and' command). This is a special return
|
|
|
|
/// allowed only from the populate functions, not the run functions.
|
2014-01-01 06:37:37 +08:00
|
|
|
parse_execution_skipped
|
|
|
|
};
|
|
|
|
|
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;
|
2013-12-29 08:18:38 +08:00
|
|
|
io_chain_t block_io;
|
2016-05-03 03:31:33 +08:00
|
|
|
parser_t *const parser;
|
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;
|
2018-11-04 15:58:44 +08:00
|
|
|
// The parent job for any jobs created by this context.
|
|
|
|
const std::shared_ptr<job_t> parent_job;
|
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
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Should I cancel?
|
2013-12-29 08:18:38 +08:00
|
|
|
bool should_cancel_execution(const block_t *block) const;
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08: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 08:23:26 +08: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 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Report an error. Always returns true.
|
2014-03-22 08:13:33 +08:00
|
|
|
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 &errors) const;
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// Wildcard error helper.
|
|
|
|
parse_execution_result_t report_unmatched_wildcard_error(
|
2018-08-26 16:41:45 +08:00
|
|
|
const parse_node_t &unmatched_wildcard) const;
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
/// Command not found support.
|
|
|
|
parse_execution_result_t handle_command_not_found(const wcstring &cmd,
|
2018-01-16 04:12:42 +08:00
|
|
|
tnode_t<grammar::plain_statement> statement,
|
2016-05-03 03:31:33 +08:00
|
|
|
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;
|
2017-09-09 12:14:26 +08:00
|
|
|
bool is_function_context() 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.
|
|
|
|
parse_execution_result_t expand_command(tnode_t<grammar::plain_statement> statement,
|
|
|
|
wcstring *out_cmd, wcstring_list_t *out_args) const;
|
|
|
|
|
2018-03-03 10:09:16 +08: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-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;
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
// These create process_t structures from statements.
|
|
|
|
parse_execution_result_t populate_job_process(job_t *job, process_t *proc,
|
2018-01-16 03:45:47 +08:00
|
|
|
tnode_t<grammar::statement> statement);
|
2018-03-03 10:09:16 +08:00
|
|
|
parse_execution_result_t populate_not_process(job_t *job, process_t *proc,
|
|
|
|
tnode_t<grammar::not_statement> not_statement);
|
2016-05-03 03:31:33 +08:00
|
|
|
parse_execution_result_t populate_plain_process(job_t *job, process_t *proc,
|
2018-01-16 04:12:42 +08:00
|
|
|
tnode_t<grammar::plain_statement> statement);
|
2018-01-16 07:37:13 +08:00
|
|
|
|
|
|
|
template <typename Type>
|
2016-05-03 03:31:33 +08:00
|
|
|
parse_execution_result_t populate_block_process(job_t *job, process_t *proc,
|
2018-02-11 11:16:35 +08:00
|
|
|
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.
|
2018-04-01 05:57:24 +08:00
|
|
|
parse_execution_result_t run_block_statement(tnode_t<grammar::block_statement> statement,
|
|
|
|
const block_t *associated_block);
|
2018-01-14 17:47:09 +08:00
|
|
|
parse_execution_result_t run_for_statement(tnode_t<grammar::for_header> header,
|
|
|
|
tnode_t<grammar::job_list> contents);
|
2018-04-01 05:57:24 +08:00
|
|
|
parse_execution_result_t run_if_statement(tnode_t<grammar::if_statement> statement,
|
|
|
|
const block_t *associated_block);
|
2018-01-14 18:30:18 +08:00
|
|
|
parse_execution_result_t run_switch_statement(tnode_t<grammar::switch_statement> statement);
|
2018-01-14 18:41:37 +08:00
|
|
|
parse_execution_result_t run_while_statement(tnode_t<grammar::while_header> statement,
|
2018-04-01 05:57:24 +08:00
|
|
|
tnode_t<grammar::job_list> contents,
|
|
|
|
const block_t *associated_block);
|
2018-01-14 18:45:27 +08:00
|
|
|
parse_execution_result_t run_function_statement(tnode_t<grammar::function_header> header,
|
2018-02-10 13:53:06 +08:00
|
|
|
tnode_t<grammar::job_list> body);
|
2018-01-21 06:05:34 +08:00
|
|
|
parse_execution_result_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>>;
|
|
|
|
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-03 03:31:33 +08:00
|
|
|
|
|
|
|
// Determines the IO chain. Returns true on success, false on error.
|
2018-01-16 07:37:13 +08:00
|
|
|
bool determine_io_chain(tnode_t<grammar::arguments_or_redirections_list> node,
|
|
|
|
io_chain_t *out_chain);
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2018-01-16 08:00:19 +08:00
|
|
|
parse_execution_result_t run_1_job(tnode_t<grammar::job> job, const block_t *associated_block);
|
2019-05-05 18:09:25 +08:00
|
|
|
parse_execution_result_t run_job_conjunction(tnode_t<grammar::job_conjunction> job_conj,
|
|
|
|
const block_t *associated_block);
|
2018-01-16 08:08:06 +08:00
|
|
|
template <typename Type>
|
|
|
|
parse_execution_result_t run_job_list(tnode_t<Type> job_list_node,
|
2016-05-03 03:31:33 +08:00
|
|
|
const block_t *associated_block);
|
2018-01-14 17:17:57 +08:00
|
|
|
parse_execution_result_t populate_job_from_job_node(job_t *j, tnode_t<grammar::job> job_node,
|
2016-05-03 03:31:33 +08:00
|
|
|
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);
|
2014-09-28 08:32:54 +08:00
|
|
|
int line_offset_of_character_at_offset(size_t char_idx);
|
2014-03-17 07:45:00 +08:00
|
|
|
|
2016-05-03 03:31:33 +08:00
|
|
|
public:
|
2018-11-04 15:58:44 +08:00
|
|
|
parse_execution_context_t(parsed_source_ref_t pstree, parser_t *p,
|
|
|
|
std::shared_ptr<job_t> parent);
|
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.
|
2018-02-11 11:16:35 +08:00
|
|
|
parse_execution_result_t eval_node(tnode_t<grammar::statement> statement,
|
|
|
|
const block_t *associated_block, const io_chain_t &io);
|
|
|
|
parse_execution_result_t eval_node(tnode_t<grammar::job_list> job_list,
|
|
|
|
const block_t *associated_block, const io_chain_t &io);
|
2013-12-25 05:17:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|