1 // The fish parser.
2 #ifndef FISH_PARSER_H
3 #define FISH_PARSER_H
4 
5 #include <stddef.h>
6 #include <unistd.h>
7 
8 #include <csignal>
9 #include <list>
10 #include <memory>
11 #include <type_traits>
12 #include <vector>
13 
14 #include "common.h"
15 #include "event.h"
16 #include "expand.h"
17 #include "operation_context.h"
18 #include "parse_constants.h"
19 #include "parse_execution.h"
20 #include "parse_tree.h"
21 #include "proc.h"
22 #include "util.h"
23 #include "wait_handle.h"
24 
25 class io_chain_t;
26 
27 /// event_blockage_t represents a block on events.
28 struct event_blockage_t {};
29 
30 typedef std::list<event_blockage_t> event_blockage_list_t;
31 
event_block_list_blocks_type(const event_blockage_list_t & ebls)32 inline bool event_block_list_blocks_type(const event_blockage_list_t &ebls) {
33     return !ebls.empty();
34 }
35 
36 /// Types of blocks.
37 enum class block_type_t {
38     while_block,              /// While loop block
39     for_block,                /// For loop block
40     if_block,                 /// If block
41     function_call,            /// Function invocation block
42     function_call_no_shadow,  /// Function invocation block with no variable shadowing
43     switch_block,             /// Switch block
44     subst,                    /// Command substitution scope
45     top,                      /// Outermost block
46     begin,                    /// Unconditional block
47     source,                   /// Block created by the . (source) builtin
48     event,                    /// Block created on event notifier invocation
49     breakpoint,               /// Breakpoint block
50     variable_assignment,      /// Variable assignment before a command
51 };
52 
53 /// Possible states for a loop.
54 enum class loop_status_t {
55     normals,    /// current loop block executed as normal
56     breaks,     /// current loop block should be removed
57     continues,  /// current loop block should be skipped
58 };
59 
60 /// block_t represents a block of commands.
61 class block_t {
62     /// Construct from a block type.
63     explicit block_t(block_type_t t);
64 
65     /// Type of block.
66     const block_type_t block_type;
67 
68    public:
69     /// Name of file that created this block. This string is intern'd.
70     const wchar_t *src_filename{nullptr};
71     /// Line number where this block was created.
72     int src_lineno{0};
73     /// Whether we should pop the environment variable stack when we're popped off of the block
74     /// stack.
75     bool wants_pop_env{false};
76     /// List of event blocks.
77     event_blockage_list_t event_blocks{};
78 
79     // If this is a function block, the function name and arguments.
80     // Otherwise empty.
81     wcstring function_name{};
82     wcstring_list_t function_args{};
83 
84     // If this is a source block, the source'd file, interned.
85     // Otherwise nothing.
86     const wchar_t *sourced_file{};
87 
88     // If this is an event block, the event. Otherwise ignored.
89     maybe_t<event_t> event;
90 
type()91     block_type_t type() const { return this->block_type; }
92 
93     /// Description of the block, for debugging.
94     wcstring description() const;
95 
96     /// \return if we are a function call (with or without shadowing).
is_function_call()97     bool is_function_call() const {
98         return type() == block_type_t::function_call ||
99                type() == block_type_t::function_call_no_shadow;
100     }
101 
102     /// Entry points for creating blocks.
103     static block_t if_block();
104     static block_t event_block(event_t evt);
105     static block_t function_block(wcstring name, wcstring_list_t args, bool shadows);
106     static block_t source_block(const wchar_t *src);
107     static block_t for_block();
108     static block_t while_block();
109     static block_t switch_block();
110     static block_t scope_block(block_type_t type);
111     static block_t breakpoint_block();
112     static block_t variable_assignment_block();
113 
114     ~block_t();
115 };
116 
117 struct profile_item_t {
118     using microseconds_t = long long;
119 
120     /// Time spent executing the command, including nested blocks.
121     microseconds_t duration{};
122 
123     /// The block level of the specified command. Nested blocks and command substitutions both
124     /// increase the block level.
125     size_t level{};
126 
127     /// If the execution of this command was skipped.
128     bool skipped{};
129 
130     /// The command string.
131     wcstring cmd{};
132 
133     /// \return the current time as a microsecond timestamp since the epoch.
nowprofile_item_t134     static microseconds_t now() { return get_time(); }
135 };
136 
137 class parse_execution_context_t;
138 class completion_t;
139 struct event_t;
140 
141 /// Miscellaneous data used to avoid recursion and others.
142 struct library_data_t {
143     /// A counter incremented every time a command executes.
144     uint64_t exec_count{0};
145 
146     /// A counter incremented every time a command produces a $status.
147     uint64_t status_count{0};
148 
149     /// Last reader run count.
150     uint64_t last_exec_run_counter{UINT64_MAX};
151 
152     /// Number of recursive calls to the internal completion function.
153     uint32_t complete_recursion_level{0};
154 
155     /// If we're currently repainting the commandline.
156     /// Useful to stop infinite loops.
157     bool is_repaint{false};
158 
159     /// Whether we called builtin_complete -C without parameter.
160     bool builtin_complete_current_commandline{false};
161 
162     /// Whether we are currently cleaning processes.
163     bool is_cleaning_procs{false};
164 
165     /// The internal job id of the job being populated, or 0 if none.
166     /// This supports the '--on-job-exit caller' feature.
167     internal_job_id_t caller_id{0};
168 
169     /// Whether we are running a subshell command.
170     bool is_subshell{false};
171 
172     /// Whether we are running a block of commands.
173     bool is_block{false};
174 
175     /// Whether we are running due to a `breakpoint` command.
176     bool is_breakpoint{false};
177 
178     /// Whether we are running an event handler. This is not a bool because we keep count of the
179     /// event nesting level.
180     int is_event{0};
181 
182     /// Whether we are currently interactive.
183     bool is_interactive{false};
184 
185     /// Whether to suppress fish_trace output. This occurs in the prompt, event handlers, and key
186     /// bindings.
187     bool suppress_fish_trace{false};
188 
189     /// Whether we should break or continue the current loop.
190     /// This is set by the 'break' and 'continue' commands.
191     enum loop_status_t loop_status { loop_status_t::normals };
192 
193     /// Whether we should return from the current function.
194     /// This is set by the 'return' command.
195     bool returning{false};
196 
197     /// Whether we should stop executing.
198     /// This is set by the 'exit' command, and unset after 'reader_read'.
199     /// Note this only exits up to the "current script boundary." That is, a call to exit within a
200     /// 'source' or 'read' command will only exit up to that command.
201     bool exit_current_script{false};
202 
203     /// The read limit to apply to captured subshell output, or 0 for none.
204     size_t read_limit{0};
205 
206     /// The current filename we are evaluating, either from builtin source or on the command line.
207     /// This is an intern'd string.
208     const wchar_t *current_filename{};
209 
210     /// List of events that have been sent but have not yet been delivered because they are blocked.
211     std::vector<shared_ptr<const event_t>> blocked_events{};
212 
213     /// A stack of fake values to be returned by builtin_commandline. This is used by the completion
214     /// machinery when wrapping: e.g. if `tig` wraps `git` then git completions need to see git on
215     /// the command line.
216     wcstring_list_t transient_commandlines{};
217 
218     /// A file descriptor holding the current working directory, for use in openat().
219     /// This is never null and never invalid.
220     std::shared_ptr<const autoclose_fd_t> cwd_fd{};
221 };
222 
223 class operation_context_t;
224 
225 /// The result of parser_t::eval family.
226 struct eval_res_t {
227     /// The value for $status.
228     proc_status_t status;
229 
230     /// If set, there was an error that should be considered a failed expansion, such as
231     /// command-not-found. For example, `touch (not-a-command)` will not invoke 'touch' because
232     /// command-not-found will mark break_expand.
233     bool break_expand;
234 
235     /// If set, no commands were executed and there we no errors.
236     bool was_empty{false};
237 
238     /// If set, no commands produced a $status value.
239     bool no_status{false};
240 
241     /* implicit */ eval_res_t(proc_status_t status, bool break_expand = false,
242                               bool was_empty = false, bool no_status = false)
statuseval_res_t243         : status(status), break_expand(break_expand), was_empty(was_empty), no_status(no_status) {}
244 };
245 
246 class parser_t : public std::enable_shared_from_this<parser_t> {
247     friend class parse_execution_context_t;
248 
249    private:
250     /// The current execution context.
251     std::unique_ptr<parse_execution_context_t> execution_context;
252 
253     /// The jobs associated with this parser.
254     job_list_t job_list;
255 
256     /// Our store of recorded wait-handles. These are jobs that finished in the background, and have
257     /// been reaped, but may still be wait'ed on.
258     wait_handle_store_t wait_handles;
259 
260     /// The list of blocks. This is a deque because we give out raw pointers to callers, who hold
261     /// them across manipulating this stack.
262     /// This is in "reverse" order: the topmost block is at the front. This enables iteration from
263     /// top down using range-based for loops.
264     std::deque<block_t> block_list;
265     /// The 'depth' of the fish call stack.
266     int eval_level = -1;
267     /// Set of variables for the parser.
268     const std::shared_ptr<env_stack_t> variables;
269     /// Miscellaneous library data.
270     library_data_t library_data{};
271 
272     /// List of profile items.
273     /// This must be a deque because we return pointers to them to callers,
274     /// who may hold them across blocks (which would cause reallocations internal
275     /// to profile_items). deque does not move items on reallocation.
276     std::deque<profile_item_t> profile_items;
277 
278     // No copying allowed.
279     parser_t(const parser_t &);
280     parser_t &operator=(const parser_t &);
281 
282     /// Adds a job to the beginning of the job list.
283     void job_add(shared_ptr<job_t> job);
284 
285     /// Returns the name of the currently evaluated function if we are currently evaluating a
286     /// function, null otherwise. This is tested by moving down the block-scope-stack, checking
287     /// every block if it is of type FUNCTION_CALL.
288     const wchar_t *is_function(size_t idx = 0) const;
289 
290     /// Create a parser.
291     parser_t();
292     parser_t(std::shared_ptr<env_stack_t> vars);
293 
294     /// The main parser.
295     static std::shared_ptr<parser_t> principal;
296 
297    public:
298     /// Get the "principal" parser, whatever that is.
299     static parser_t &principal_parser();
300 
301     /// Global event blocks.
302     event_blockage_list_t global_event_blocks;
303 
304     /// Evaluate the expressions contained in cmd.
305     ///
306     /// \param cmd the string to evaluate
307     /// \param io io redirections to perform on all started jobs
308     /// \param job_group if set, the job group to give to spawned jobs.
309     /// \param block_type The type of block to push on the block stack, which must be either 'top'
310     /// or 'subst'.
311     /// \param break_expand If not null, return by reference whether the error ought to be an expand
312     /// error. This includes nested expand errors, and command-not-found.
313     ///
314     /// \return the result of evaluation.
315     eval_res_t eval(const wcstring &cmd, const io_chain_t &io,
316                     const job_group_ref_t &job_group = {},
317                     block_type_t block_type = block_type_t::top);
318 
319     /// Evaluate the parsed source ps.
320     /// Because the source has been parsed, a syntax error is impossible.
321     eval_res_t eval(const parsed_source_ref_t &ps, const io_chain_t &io,
322                     const job_group_ref_t &job_group = {},
323                     block_type_t block_type = block_type_t::top);
324 
325     /// Evaluates a node.
326     /// The node type must be ast_t::statement_t or ast::job_list_t.
327     template <typename T>
328     eval_res_t eval_node(const parsed_source_ref_t &ps, const T &node, const io_chain_t &block_io,
329                          const job_group_ref_t &job_group,
330                          block_type_t block_type = block_type_t::top);
331 
332     /// Evaluate line as a list of parameters, i.e. tokenize it and perform parameter expansion and
333     /// cmdsubst execution on the tokens. Errors are ignored. If a parser is provided, it is used
334     /// for command substitution expansion.
335     static completion_list_t expand_argument_list(const wcstring &arg_list_src,
336                                                   expand_flags_t flags,
337                                                   const operation_context_t &ctx);
338 
339     /// Returns a string describing the current parser position in the format 'FILENAME (line
340     /// LINE_NUMBER): LINE'. Example:
341     ///
342     /// init.fish (line 127): ls|grep pancake
343     wcstring current_line();
344 
345     /// Returns the current line number.
346     int get_lineno() const;
347 
348     /// Returns the block at the given index. 0 corresponds to the innermost block. Returns nullptr
349     /// when idx is at or equal to the number of blocks.
350     const block_t *block_at_index(size_t idx) const;
351     block_t *block_at_index(size_t idx);
352 
353     /// Return the list of blocks. The first block is at the top.
blocks()354     const std::deque<block_t> &blocks() const { return block_list; }
355 
356     /// Returns the current (innermost) block.
357     block_t *current_block();
358 
359     /// Get the list of jobs.
jobs()360     job_list_t &jobs() { return job_list; }
jobs()361     const job_list_t &jobs() const { return job_list; }
362 
363     /// Get the variables.
vars()364     env_stack_t &vars() { return *variables; }
vars()365     const env_stack_t &vars() const { return *variables; }
366 
367     /// Get the library data.
libdata()368     library_data_t &libdata() { return library_data; }
libdata()369     const library_data_t &libdata() const { return library_data; }
370 
371     /// Get our wait handle store.
get_wait_handles()372     wait_handle_store_t &get_wait_handles() { return wait_handles; }
get_wait_handles()373     const wait_handle_store_t &get_wait_handles() const { return wait_handles; }
374 
375     /// Get and set the last proc statuses.
get_last_status()376     int get_last_status() const { return vars().get_last_status(); }
get_last_statuses()377     statuses_t get_last_statuses() const { return vars().get_last_statuses(); }
set_last_statuses(statuses_t s)378     void set_last_statuses(statuses_t s) { vars().set_last_statuses(std::move(s)); }
379 
380     /// Cover of vars().set(), which also fires any returned event handlers.
381     /// \return a value like ENV_OK.
382     int set_var_and_fire(const wcstring &key, env_mode_flags_t mode, wcstring val);
383     int set_var_and_fire(const wcstring &key, env_mode_flags_t mode, wcstring_list_t vals);
384     int set_empty_var_and_fire(const wcstring &key, env_mode_flags_t mode);
385 
386     /// Pushes a new block. Returns a pointer to the block, stored in the parser. The pointer is
387     /// valid until the call to pop_block()
388     block_t *push_block(block_t &&b);
389 
390     /// Remove the outermost block, asserting it's the given one.
391     void pop_block(const block_t *expected);
392 
393     /// Return a description of the given blocktype.
394     static const wchar_t *get_block_desc(block_type_t block);
395 
396     /// Return the function name for the specified stack frame. Default is one (current frame).
397     const wchar_t *get_function_name(int level = 1);
398 
399     /// Promotes a job to the front of the list.
400     void job_promote(job_t *job);
401 
402     /// Return the job with the specified job id. If id is 0 or less, return the last job used.
403     const job_t *job_with_id(job_id_t job_id) const;
404 
405     /// Return the job with the specified internal job id.
406     const job_t *job_with_internal_id(internal_job_id_t job_id) const;
407 
408     /// Returns the job with the given pid.
409     job_t *job_get_from_pid(pid_t pid) const;
410 
411     /// Returns a new profile item if profiling is active. The caller should fill it in.
412     /// The parser_t will deallocate it.
413     /// If profiling is not active, this returns nullptr.
414     profile_item_t *create_profile_item();
415 
416     /// Remove the profiling items.
417     void clear_profiling();
418 
419     /// Output profiling data to the given filename.
420     void emit_profiling(const char *path) const;
421 
422     void get_backtrace(const wcstring &src, const parse_error_list_t &errors,
423                        wcstring &output) const;
424 
425     /// Returns the file currently evaluated by the parser. This can be different than
426     /// reader_current_filename, e.g. if we are evaluating a function defined in a different file
427     /// than the one curently read.
428     const wchar_t *current_filename() const;
429 
430     /// Return if we are interactive, which means we are executing a command that the user typed in
431     /// (and not, say, a prompt).
is_interactive()432     bool is_interactive() const { return libdata().is_interactive; }
433 
434     /// Return a string representing the current stack trace.
435     wcstring stack_trace() const;
436 
437     /// \return whether the number of functions in the stack exceeds our stack depth limit.
438     bool function_stack_is_overflowing() const;
439 
440     /// \return a shared pointer reference to this parser.
441     std::shared_ptr<parser_t> shared();
442 
443     /// \return a cancel poller for checking if this parser has been signalled.
444     cancel_checker_t cancel_checker() const;
445 
446     /// \return the operation context for this parser.
447     operation_context_t context();
448 
449     ~parser_t();
450 };
451 
452 #endif
453