1 // Provides the "linkage" between an ast and actual execution structures (job_t, etc.). 2 #ifndef FISH_PARSE_EXECUTION_H 3 #define FISH_PARSE_EXECUTION_H 4 5 #include <stddef.h> 6 7 #include "ast.h" 8 #include "common.h" 9 #include "io.h" 10 #include "parse_constants.h" 11 #include "parse_tree.h" 12 #include "proc.h" 13 14 class block_t; 15 class cancellation_group_t; 16 class operation_context_t; 17 class parser_t; 18 19 /// An eval_result represents evaluation errors including wildcards which failed to match, syntax 20 /// errors, or other expansion errors. It also tracks when evaluation was skipped due to signal 21 /// cancellation. Note it does not track the exit status of commands. 22 enum class end_execution_reason_t { 23 /// Evaluation was successfull. 24 ok, 25 26 /// Evaluation was skipped due to control flow (break or return). 27 control_flow, 28 29 /// Evaluation was cancelled, e.g. because of a signal or exit. 30 cancelled, 31 32 /// A parse error or failed expansion (but not an error exit status from a command). 33 error, 34 }; 35 36 class parse_execution_context_t { 37 private: 38 parsed_source_ref_t pstree; 39 parser_t *const parser; 40 const operation_context_t &ctx; 41 const std::shared_ptr<cancellation_group_t> cancel_group; 42 43 // The currently executing job node, used to indicate the line number. 44 const ast::job_t *executing_job_node{}; 45 46 // Cached line number information. 47 size_t cached_lineno_offset = 0; 48 int cached_lineno_count = 0; 49 50 /// The block IO chain. 51 /// For example, in `begin; foo ; end < file.txt` this would have the 'file.txt' IO. 52 io_chain_t block_io{}; 53 54 // No copying allowed. 55 parse_execution_context_t(const parse_execution_context_t &) = delete; 56 parse_execution_context_t &operator=(const parse_execution_context_t &) = delete; 57 58 // Check to see if we should end execution. 59 // \return the eval result to end with, or none() to continue on. 60 // This will never return end_execution_reason_t::ok. 61 maybe_t<end_execution_reason_t> check_end_execution() const; 62 63 // Report an error, setting $status to \p status. Always returns 64 // 'end_execution_reason_t::error'. 65 end_execution_reason_t report_error(int status, const ast::node_t &node, const wchar_t *fmt, 66 ...) const; 67 end_execution_reason_t report_errors(int status, const parse_error_list_t &error_list) const; 68 69 /// Command not found support. 70 end_execution_reason_t handle_command_not_found(const wcstring &cmd, 71 const ast::decorated_statement_t &statement, 72 int err_code); 73 74 // Utilities 75 wcstring get_source(const ast::node_t &node) const; 76 const ast::decorated_statement_t *infinite_recursive_statement_in_job_list( 77 const ast::job_list_t &jobs, wcstring *out_func_name) const; 78 79 // Expand a command which may contain variables, producing an expand command and possibly 80 // arguments. Prints an error message on error. 81 end_execution_reason_t expand_command(const ast::decorated_statement_t &statement, 82 wcstring *out_cmd, wcstring_list_t *out_args) const; 83 84 /// Indicates whether a job is a simple block (one block, no redirections). 85 bool job_is_simple_block(const ast::job_t &job) const; 86 87 enum process_type_t process_type_for_command(const ast::decorated_statement_t &statement, 88 const wcstring &cmd) const; 89 end_execution_reason_t apply_variable_assignments( 90 process_t *proc, const ast::variable_assignment_list_t &variable_assignment_list, 91 const block_t **block); 92 93 // These create process_t structures from statements. 94 end_execution_reason_t populate_job_process( 95 job_t *job, process_t *proc, const ast::statement_t &statement, 96 const ast::variable_assignment_list_t &variable_assignments_list_t); 97 end_execution_reason_t populate_not_process(job_t *job, process_t *proc, 98 const ast::not_statement_t ¬_statement); 99 end_execution_reason_t populate_plain_process(job_t *job, process_t *proc, 100 const ast::decorated_statement_t &statement); 101 102 template <typename Type> 103 end_execution_reason_t populate_block_process(job_t *job, process_t *proc, 104 const ast::statement_t &statement, 105 const Type &specific_statement); 106 107 // These encapsulate the actual logic of various (block) statements. 108 end_execution_reason_t run_block_statement(const ast::block_statement_t &statement, 109 const block_t *associated_block); 110 end_execution_reason_t run_for_statement(const ast::for_header_t &header, 111 const ast::job_list_t &contents); 112 end_execution_reason_t run_if_statement(const ast::if_statement_t &statement, 113 const block_t *associated_block); 114 end_execution_reason_t run_switch_statement(const ast::switch_statement_t &statement); 115 end_execution_reason_t run_while_statement(const ast::while_header_t &header, 116 const ast::job_list_t &contents, 117 const block_t *associated_block); 118 end_execution_reason_t run_function_statement(const ast::block_statement_t &statement, 119 const ast::function_header_t &header); 120 end_execution_reason_t run_begin_statement(const ast::job_list_t &contents); 121 122 enum globspec_t { failglob, nullglob }; 123 using ast_args_list_t = std::vector<const ast::argument_t *>; 124 125 static ast_args_list_t get_argument_nodes(const ast::argument_list_t &args); 126 static ast_args_list_t get_argument_nodes(const ast::argument_or_redirection_list_t &args); 127 128 end_execution_reason_t expand_arguments_from_nodes(const ast_args_list_t &argument_nodes, 129 wcstring_list_t *out_arguments, 130 globspec_t glob_behavior); 131 132 // Determines the list of redirections for a node. 133 end_execution_reason_t determine_redirections(const ast::argument_or_redirection_list_t &list, 134 redirection_spec_list_t *out_redirections); 135 136 end_execution_reason_t run_1_job(const ast::job_t &job, const block_t *associated_block); 137 end_execution_reason_t test_and_run_1_job_conjunction(const ast::job_conjunction_t &jc, 138 const block_t *associated_block); 139 end_execution_reason_t run_job_conjunction(const ast::job_conjunction_t &job_expr, 140 const block_t *associated_block); 141 end_execution_reason_t run_job_list(const ast::job_list_t &job_list_node, 142 const block_t *associated_block); 143 end_execution_reason_t run_job_list(const ast::andor_job_list_t &job_list_node, 144 const block_t *associated_block); 145 end_execution_reason_t populate_job_from_job_node(job_t *j, const ast::job_t &job_node, 146 const block_t *associated_block); 147 148 // Returns the line number of the node. Not const since it touches cached_lineno_offset. 149 int line_offset_of_node(const ast::job_t *node); 150 int line_offset_of_character_at_offset(size_t offset); 151 152 public: 153 /// Construct a context in preparation for evaluating a node in a tree, with the given block_io. 154 /// The cancel group is never null and should be provided when resolving job groups. 155 /// The execution context may access the parser and parent job group (if any) through ctx. 156 parse_execution_context_t(parsed_source_ref_t pstree, const operation_context_t &ctx, 157 std::shared_ptr<cancellation_group_t> cancel_group, 158 io_chain_t block_io); 159 160 /// Returns the current line number, indexed from 1. Not const since it touches 161 /// cached_lineno_offset. 162 int get_current_line_number(); 163 164 /// Returns the source offset, or -1. 165 int get_current_source_offset() const; 166 167 /// Returns the source string. get_source()168 const wcstring &get_source() const { return pstree->src; } 169 170 /// Return the parsed ast. ast()171 const ast::ast_t &ast() const { return pstree->ast; } 172 173 /// Start executing at the given node. Returns 0 if there was no error, 1 if there was an 174 /// error. 175 end_execution_reason_t eval_node(const ast::statement_t &statement, 176 const block_t *associated_block); 177 end_execution_reason_t eval_node(const ast::job_list_t &job_list, 178 const block_t *associated_block); 179 }; 180 181 #endif 182