2016-05-03 08:22:44 +08:00
|
|
|
// The fish parser.
|
2005-10-04 23:11:39 +08:00
|
|
|
#ifndef FISH_PARSER_H
|
|
|
|
#define FISH_PARSER_H
|
|
|
|
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <stddef.h>
|
2017-04-30 12:33:50 +08:00
|
|
|
#include <unistd.h>
|
2017-02-11 10:47:02 +08:00
|
|
|
|
|
|
|
#include <csignal>
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <list>
|
2017-02-11 10:47:02 +08:00
|
|
|
#include <memory>
|
|
|
|
#include <type_traits>
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <vector>
|
2005-10-04 23:11:39 +08:00
|
|
|
|
2015-07-25 23:14:25 +08:00
|
|
|
#include "common.h"
|
2006-02-01 23:49:11 +08:00
|
|
|
#include "event.h"
|
2016-02-28 10:25:58 +08:00
|
|
|
#include "expand.h"
|
2020-01-16 09:14:47 +08:00
|
|
|
#include "operation_context.h"
|
2016-05-03 08:22:44 +08:00
|
|
|
#include "parse_constants.h"
|
2019-12-18 10:10:29 +08:00
|
|
|
#include "parse_execution.h"
|
2016-05-03 08:22:44 +08:00
|
|
|
#include "parse_tree.h"
|
|
|
|
#include "proc.h"
|
2020-07-23 06:35:14 +08:00
|
|
|
#include "util.h"
|
2021-05-12 03:01:08 +08:00
|
|
|
#include "wait_handle.h"
|
2015-07-25 23:14:25 +08:00
|
|
|
|
2016-04-21 14:00:54 +08:00
|
|
|
class io_chain_t;
|
2005-10-04 23:11:39 +08:00
|
|
|
|
2019-02-21 13:41:35 +08:00
|
|
|
/// event_blockage_t represents a block on events.
|
2019-05-05 18:09:25 +08:00
|
|
|
struct event_blockage_t {};
|
2012-02-08 13:04:51 +08:00
|
|
|
|
2012-08-27 13:42:29 +08:00
|
|
|
typedef std::list<event_blockage_t> event_blockage_list_t;
|
2012-02-08 13:04:51 +08:00
|
|
|
|
2019-02-21 14:42:58 +08:00
|
|
|
inline bool event_block_list_blocks_type(const event_blockage_list_t &ebls) {
|
2019-02-21 13:41:35 +08:00
|
|
|
return !ebls.empty();
|
2012-02-08 13:04:51 +08:00
|
|
|
}
|
2005-12-12 06:21:01 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
/// Types of blocks.
|
2019-12-23 07:37:14 +08:00
|
|
|
enum class block_type_t {
|
|
|
|
while_block, /// While loop block
|
|
|
|
for_block, /// For loop block
|
|
|
|
if_block, /// If block
|
|
|
|
function_call, /// Function invocation block
|
|
|
|
function_call_no_shadow, /// Function invocation block with no variable shadowing
|
|
|
|
switch_block, /// Switch block
|
|
|
|
subst, /// Command substitution scope
|
|
|
|
top, /// Outermost block
|
|
|
|
begin, /// Unconditional block
|
|
|
|
source, /// Block created by the . (source) builtin
|
|
|
|
event, /// Block created on event notifier invocation
|
|
|
|
breakpoint, /// Breakpoint block
|
|
|
|
variable_assignment, /// Variable assignment before a command
|
2014-03-03 05:46:30 +08:00
|
|
|
};
|
2012-08-27 13:42:29 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
/// Possible states for a loop.
|
2019-05-19 14:12:34 +08:00
|
|
|
enum class loop_status_t {
|
|
|
|
normals, /// current loop block executed as normal
|
|
|
|
breaks, /// current loop block should be removed
|
|
|
|
continues, /// current loop block should be skipped
|
2014-10-31 16:15:50 +08:00
|
|
|
};
|
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
/// block_t represents a block of commands.
|
2019-05-20 05:44:17 +08:00
|
|
|
class block_t {
|
|
|
|
/// Construct from a block type.
|
2016-02-28 11:38:15 +08:00
|
|
|
explicit block_t(block_type_t t);
|
2012-08-27 14:16:20 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
/// Type of block.
|
|
|
|
const block_type_t block_type;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// Name of file that created this block. This string is intern'd.
|
2018-02-12 12:10:57 +08:00
|
|
|
const wchar_t *src_filename{nullptr};
|
2016-05-03 08:22:44 +08:00
|
|
|
/// Line number where this block was created.
|
2018-02-12 12:10:57 +08:00
|
|
|
int src_lineno{0};
|
2016-05-03 08:22:44 +08:00
|
|
|
/// Whether we should pop the environment variable stack when we're popped off of the block
|
|
|
|
/// stack.
|
2018-02-12 12:10:57 +08:00
|
|
|
bool wants_pop_env{false};
|
|
|
|
/// List of event blocks.
|
|
|
|
event_blockage_list_t event_blocks{};
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2019-05-20 03:55:16 +08:00
|
|
|
// If this is a function block, the function name and arguments.
|
|
|
|
// Otherwise empty.
|
|
|
|
wcstring function_name{};
|
|
|
|
wcstring_list_t function_args{};
|
|
|
|
|
2019-05-20 04:01:59 +08:00
|
|
|
// If this is a source block, the source'd file, interned.
|
|
|
|
// Otherwise nothing.
|
|
|
|
const wchar_t *sourced_file{};
|
|
|
|
|
2019-05-20 04:07:06 +08:00
|
|
|
// If this is an event block, the event. Otherwise ignored.
|
|
|
|
maybe_t<event_t> event;
|
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
block_type_t type() const { return this->block_type; }
|
|
|
|
|
|
|
|
/// Description of the block, for debugging.
|
|
|
|
wcstring description() const;
|
|
|
|
|
2019-12-23 07:07:41 +08:00
|
|
|
/// \return if we are a function call (with or without shadowing).
|
2019-12-23 07:37:14 +08:00
|
|
|
bool is_function_call() const {
|
|
|
|
return type() == block_type_t::function_call ||
|
|
|
|
type() == block_type_t::function_call_no_shadow;
|
2019-12-23 07:07:41 +08:00
|
|
|
}
|
|
|
|
|
2019-05-20 05:40:06 +08:00
|
|
|
/// Entry points for creating blocks.
|
|
|
|
static block_t if_block();
|
|
|
|
static block_t event_block(event_t evt);
|
|
|
|
static block_t function_block(wcstring name, wcstring_list_t args, bool shadows);
|
|
|
|
static block_t source_block(const wchar_t *src);
|
|
|
|
static block_t for_block();
|
|
|
|
static block_t while_block();
|
|
|
|
static block_t switch_block();
|
|
|
|
static block_t scope_block(block_type_t type);
|
|
|
|
static block_t breakpoint_block();
|
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
|
|
|
static block_t variable_assignment_block();
|
2019-05-20 05:40:06 +08:00
|
|
|
|
2019-05-20 05:44:17 +08:00
|
|
|
~block_t();
|
2012-08-27 13:42:29 +08:00
|
|
|
};
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
struct profile_item_t {
|
2020-07-23 06:35:14 +08:00
|
|
|
using microseconds_t = long long;
|
|
|
|
|
|
|
|
/// Time spent executing the command, including nested blocks.
|
|
|
|
microseconds_t duration{};
|
|
|
|
|
|
|
|
/// The block level of the specified command. Nested blocks and command substitutions both
|
2016-05-03 08:22:44 +08:00
|
|
|
/// increase the block level.
|
2020-07-23 06:35:14 +08:00
|
|
|
size_t level{};
|
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
/// If the execution of this command was skipped.
|
2020-07-23 06:35:14 +08:00
|
|
|
bool skipped{};
|
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
/// The command string.
|
2020-07-23 06:35:14 +08:00
|
|
|
wcstring cmd{};
|
|
|
|
|
|
|
|
/// \return the current time as a microsecond timestamp since the epoch.
|
|
|
|
static microseconds_t now() { return get_time(); }
|
2012-01-21 03:24:43 +08:00
|
|
|
};
|
2012-01-20 02:28:44 +08:00
|
|
|
|
2013-12-27 05:24:10 +08:00
|
|
|
class parse_execution_context_t;
|
2015-07-25 23:14:25 +08:00
|
|
|
class completion_t;
|
2019-06-03 17:31:13 +08:00
|
|
|
struct event_t;
|
2012-01-23 12:47:13 +08:00
|
|
|
|
2019-10-22 08:21:40 +08:00
|
|
|
/// Miscellaneous data used to avoid recursion and others.
|
2019-04-29 07:11:49 +08:00
|
|
|
struct library_data_t {
|
|
|
|
/// A counter incremented every time a command executes.
|
|
|
|
uint64_t exec_count{0};
|
2019-04-29 07:41:58 +08:00
|
|
|
|
2020-08-03 04:37:19 +08:00
|
|
|
/// A counter incremented every time a command produces a $status.
|
|
|
|
uint64_t status_count{0};
|
|
|
|
|
2019-04-29 09:13:55 +08:00
|
|
|
/// Last reader run count.
|
|
|
|
uint64_t last_exec_run_counter{UINT64_MAX};
|
|
|
|
|
2020-09-26 20:13:56 +08:00
|
|
|
/// Number of recursive calls to the internal completion function.
|
|
|
|
uint32_t complete_recursion_level{0};
|
2019-04-30 11:58:58 +08:00
|
|
|
|
2020-09-12 01:23:26 +08:00
|
|
|
/// If we're currently repainting the commandline.
|
|
|
|
/// Useful to stop infinite loops.
|
|
|
|
bool is_repaint{false};
|
|
|
|
|
2019-10-06 21:34:43 +08:00
|
|
|
/// Whether we called builtin_complete -C without parameter.
|
|
|
|
bool builtin_complete_current_commandline{false};
|
|
|
|
|
2019-04-30 11:58:58 +08:00
|
|
|
/// Whether we are currently cleaning processes.
|
|
|
|
bool is_cleaning_procs{false};
|
2019-05-13 05:42:18 +08:00
|
|
|
|
2020-02-09 07:43:21 +08:00
|
|
|
/// The internal job id of the job being populated, or 0 if none.
|
2019-05-13 05:42:18 +08:00
|
|
|
/// This supports the '--on-job-exit caller' feature.
|
2020-02-09 07:43:21 +08:00
|
|
|
internal_job_id_t caller_id{0};
|
2019-05-13 09:02:57 +08:00
|
|
|
|
|
|
|
/// Whether we are running a subshell command.
|
|
|
|
bool is_subshell{false};
|
|
|
|
|
|
|
|
/// Whether we are running a block of commands.
|
|
|
|
bool is_block{false};
|
|
|
|
|
|
|
|
/// Whether we are running due to a `breakpoint` command.
|
|
|
|
bool is_breakpoint{false};
|
|
|
|
|
|
|
|
/// Whether we are running an event handler. This is not a bool because we keep count of the
|
|
|
|
/// event nesting level.
|
|
|
|
int is_event{0};
|
2019-05-19 14:12:34 +08:00
|
|
|
|
2019-05-28 05:52:48 +08:00
|
|
|
/// Whether we are currently interactive.
|
|
|
|
bool is_interactive{false};
|
|
|
|
|
2019-10-19 09:08:22 +08:00
|
|
|
/// Whether to suppress fish_trace output. This occurs in the prompt, event handlers, and key
|
|
|
|
/// bindings.
|
|
|
|
bool suppress_fish_trace{false};
|
|
|
|
|
2019-05-19 14:12:34 +08:00
|
|
|
/// Whether we should break or continue the current loop.
|
2020-08-16 05:41:11 +08:00
|
|
|
/// This is set by the 'break' and 'continue' commands.
|
2019-05-19 14:12:34 +08:00
|
|
|
enum loop_status_t loop_status { loop_status_t::normals };
|
2019-05-20 10:29:25 +08:00
|
|
|
|
|
|
|
/// Whether we should return from the current function.
|
2020-08-16 05:41:11 +08:00
|
|
|
/// This is set by the 'return' command.
|
2019-05-20 10:29:25 +08:00
|
|
|
bool returning{false};
|
2019-05-23 04:34:03 +08:00
|
|
|
|
2020-08-16 05:41:11 +08:00
|
|
|
/// Whether we should stop executing.
|
|
|
|
/// This is set by the 'exit' command, and unset after 'reader_read'.
|
|
|
|
/// Note this only exits up to the "current script boundary." That is, a call to exit within a
|
|
|
|
/// 'source' or 'read' command will only exit up to that command.
|
|
|
|
bool exit_current_script{false};
|
|
|
|
|
2019-12-12 03:49:51 +08:00
|
|
|
/// The read limit to apply to captured subshell output, or 0 for none.
|
|
|
|
size_t read_limit{0};
|
|
|
|
|
2019-05-23 04:34:03 +08:00
|
|
|
/// The current filename we are evaluating, either from builtin source or on the command line.
|
|
|
|
/// This is an intern'd string.
|
|
|
|
const wchar_t *current_filename{};
|
2019-06-03 17:31:13 +08:00
|
|
|
|
|
|
|
/// List of events that have been sent but have not yet been delivered because they are blocked.
|
2020-07-20 03:03:10 +08:00
|
|
|
std::vector<shared_ptr<const event_t>> blocked_events{};
|
2019-06-10 05:11:25 +08:00
|
|
|
|
|
|
|
/// A stack of fake values to be returned by builtin_commandline. This is used by the completion
|
|
|
|
/// machinery when wrapping: e.g. if `tig` wraps `git` then git completions need to see git on
|
|
|
|
/// the command line.
|
|
|
|
wcstring_list_t transient_commandlines{};
|
2019-06-11 00:27:51 +08:00
|
|
|
|
|
|
|
/// A file descriptor holding the current working directory, for use in openat().
|
|
|
|
/// This is never null and never invalid.
|
|
|
|
std::shared_ptr<const autoclose_fd_t> cwd_fd{};
|
2019-04-29 07:11:49 +08:00
|
|
|
};
|
|
|
|
|
2020-01-16 09:14:47 +08:00
|
|
|
class operation_context_t;
|
|
|
|
|
2020-01-24 09:34:46 +08:00
|
|
|
/// The result of parser_t::eval family.
|
|
|
|
struct eval_res_t {
|
|
|
|
/// The value for $status.
|
|
|
|
proc_status_t status;
|
|
|
|
|
|
|
|
/// If set, there was an error that should be considered a failed expansion, such as
|
|
|
|
/// command-not-found. For example, `touch (not-a-command)` will not invoke 'touch' because
|
|
|
|
/// command-not-found will mark break_expand.
|
2021-07-24 02:22:45 +08:00
|
|
|
bool break_expand{false};
|
2020-01-24 09:34:46 +08:00
|
|
|
|
|
|
|
/// If set, no commands were executed and there we no errors.
|
|
|
|
bool was_empty{false};
|
|
|
|
|
2020-08-03 04:37:19 +08:00
|
|
|
/// If set, no commands produced a $status value.
|
|
|
|
bool no_status{false};
|
|
|
|
|
2020-01-24 09:34:46 +08:00
|
|
|
/* implicit */ eval_res_t(proc_status_t status, bool break_expand = false,
|
2020-08-03 04:37:19 +08:00
|
|
|
bool was_empty = false, bool no_status = false)
|
|
|
|
: status(status), break_expand(break_expand), was_empty(was_empty), no_status(no_status) {}
|
2020-01-24 09:34:46 +08:00
|
|
|
};
|
|
|
|
|
2019-02-25 04:12:24 +08:00
|
|
|
class parser_t : public std::enable_shared_from_this<parser_t> {
|
2013-12-25 05:17:24 +08:00
|
|
|
friend class parse_execution_context_t;
|
2020-07-13 03:20:38 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
private:
|
2018-02-12 14:00:17 +08:00
|
|
|
/// The current execution context.
|
|
|
|
std::unique_ptr<parse_execution_context_t> execution_context;
|
2021-05-16 03:50:25 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
/// The jobs associated with this parser.
|
2019-05-05 13:12:31 +08:00
|
|
|
job_list_t job_list;
|
2021-05-16 03:50:25 +08:00
|
|
|
|
2021-05-12 03:01:08 +08:00
|
|
|
/// Our store of recorded wait-handles. These are jobs that finished in the background, and have
|
2021-05-16 03:50:25 +08:00
|
|
|
/// been reaped, but may still be wait'ed on.
|
2021-05-12 03:01:08 +08:00
|
|
|
wait_handle_store_t wait_handles;
|
2021-05-16 03:50:25 +08:00
|
|
|
|
2019-05-21 00:42:18 +08:00
|
|
|
/// The list of blocks. This is a deque because we give out raw pointers to callers, who hold
|
|
|
|
/// them across manipulating this stack.
|
2019-12-23 07:07:41 +08:00
|
|
|
/// This is in "reverse" order: the topmost block is at the front. This enables iteration from
|
|
|
|
/// top down using range-based for loops.
|
|
|
|
std::deque<block_t> block_list;
|
2018-02-12 13:42:23 +08:00
|
|
|
/// The 'depth' of the fish call stack.
|
|
|
|
int eval_level = -1;
|
2018-09-10 16:17:57 +08:00
|
|
|
/// Set of variables for the parser.
|
2019-05-21 00:27:46 +08:00
|
|
|
const std::shared_ptr<env_stack_t> variables;
|
2019-04-29 07:11:49 +08:00
|
|
|
/// Miscellaneous library data.
|
|
|
|
library_data_t library_data{};
|
2014-03-17 07:45:00 +08:00
|
|
|
|
2020-07-23 06:35:14 +08:00
|
|
|
/// List of profile items.
|
|
|
|
/// This must be a deque because we return pointers to them to callers,
|
2017-01-22 06:33:17 +08:00
|
|
|
/// who may hold them across blocks (which would cause reallocations internal
|
2020-07-23 06:35:14 +08:00
|
|
|
/// to profile_items). deque does not move items on reallocation.
|
|
|
|
std::deque<profile_item_t> profile_items;
|
2014-03-17 07:45:00 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
// No copying allowed.
|
|
|
|
parser_t(const parser_t &);
|
|
|
|
parser_t &operator=(const parser_t &);
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
/// Adds a job to the beginning of the job list.
|
2017-01-27 06:47:32 +08:00
|
|
|
void job_add(shared_ptr<job_t> job);
|
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-20 07:16:41 +08:00
|
|
|
|
2021-07-16 02:21:32 +08:00
|
|
|
/// \return whether we are currently evaluating a function.
|
|
|
|
bool is_function() const;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2018-09-09 16:36:21 +08:00
|
|
|
/// Create a parser.
|
|
|
|
parser_t();
|
2019-05-21 00:27:46 +08:00
|
|
|
parser_t(std::shared_ptr<env_stack_t> vars);
|
2018-09-09 16:36:21 +08:00
|
|
|
|
|
|
|
/// The main parser.
|
2019-02-25 04:12:24 +08:00
|
|
|
static std::shared_ptr<parser_t> principal;
|
2018-09-09 16:36:21 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
public:
|
|
|
|
/// Get the "principal" parser, whatever that is.
|
2012-01-23 13:40:08 +08:00
|
|
|
static parser_t &principal_parser();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
/// Global event blocks.
|
2012-08-27 13:42:29 +08:00
|
|
|
event_blockage_list_t global_event_blocks;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
/// Evaluate the expressions contained in cmd.
|
|
|
|
///
|
|
|
|
/// \param cmd the string to evaluate
|
|
|
|
/// \param io io redirections to perform on all started jobs
|
2020-05-31 05:05:07 +08:00
|
|
|
/// \param job_group if set, the job group to give to spawned jobs.
|
2019-12-23 08:27:03 +08:00
|
|
|
/// \param block_type The type of block to push on the block stack, which must be either 'top'
|
|
|
|
/// or 'subst'.
|
2020-01-24 09:34:46 +08:00
|
|
|
/// \return the result of evaluation.
|
2020-05-31 05:05:07 +08:00
|
|
|
eval_res_t eval(const wcstring &cmd, const io_chain_t &io,
|
|
|
|
const job_group_ref_t &job_group = {},
|
2020-01-24 09:34:46 +08:00
|
|
|
block_type_t block_type = block_type_t::top);
|
2012-01-17 03:09:19 +08:00
|
|
|
|
2017-12-23 06:40:15 +08:00
|
|
|
/// Evaluate the parsed source ps.
|
2019-12-18 07:39:05 +08:00
|
|
|
/// Because the source has been parsed, a syntax error is impossible.
|
2020-01-24 09:34:46 +08:00
|
|
|
eval_res_t eval(const parsed_source_ref_t &ps, const io_chain_t &io,
|
2020-05-31 05:05:07 +08:00
|
|
|
const job_group_ref_t &job_group = {},
|
2020-02-09 06:34:10 +08:00
|
|
|
block_type_t block_type = block_type_t::top);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2018-02-12 11:34:12 +08:00
|
|
|
/// Evaluates a node.
|
2020-07-04 02:16:51 +08:00
|
|
|
/// The node type must be ast_t::statement_t or ast::job_list_t.
|
2018-02-11 11:16:35 +08:00
|
|
|
template <typename T>
|
2020-07-04 02:16:51 +08:00
|
|
|
eval_res_t eval_node(const parsed_source_ref_t &ps, const T &node, const io_chain_t &block_io,
|
2020-05-31 05:05:07 +08:00
|
|
|
const job_group_ref_t &job_group,
|
2020-01-24 09:34:46 +08:00
|
|
|
block_type_t block_type = block_type_t::top);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
/// Evaluate line as a list of parameters, i.e. tokenize it and perform parameter expansion and
|
2019-05-05 10:16:26 +08:00
|
|
|
/// cmdsubst execution on the tokens. Errors are ignored. If a parser is provided, it is used
|
|
|
|
/// for command substitution expansion.
|
2020-01-16 08:13:41 +08:00
|
|
|
static completion_list_t expand_argument_list(const wcstring &arg_list_src,
|
2020-01-16 09:14:47 +08:00
|
|
|
expand_flags_t flags,
|
|
|
|
const operation_context_t &ctx);
|
2016-05-03 08:22:44 +08:00
|
|
|
|
2017-06-24 13:14:21 +08:00
|
|
|
/// Returns a string describing the current parser position in the format 'FILENAME (line
|
2016-05-03 08:22:44 +08:00
|
|
|
/// LINE_NUMBER): LINE'. Example:
|
|
|
|
///
|
|
|
|
/// init.fish (line 127): ls|grep pancake
|
2014-03-17 05:49:51 +08:00
|
|
|
wcstring current_line();
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
/// Returns the current line number.
|
2012-01-17 03:16:12 +08:00
|
|
|
int get_lineno() const;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2019-12-23 07:07:41 +08:00
|
|
|
/// Returns the block at the given index. 0 corresponds to the innermost block. Returns nullptr
|
2016-05-03 08:22:44 +08:00
|
|
|
/// when idx is at or equal to the number of blocks.
|
2013-12-21 09:41:21 +08:00
|
|
|
const block_t *block_at_index(size_t idx) const;
|
|
|
|
block_t *block_at_index(size_t idx);
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2019-12-23 07:07:41 +08:00
|
|
|
/// Return the list of blocks. The first block is at the top.
|
|
|
|
const std::deque<block_t> &blocks() const { return block_list; }
|
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
/// Returns the current (innermost) block.
|
2016-07-31 02:48:39 +08:00
|
|
|
block_t *current_block();
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
/// Get the list of jobs.
|
2019-05-05 13:12:31 +08:00
|
|
|
job_list_t &jobs() { return job_list; }
|
|
|
|
const job_list_t &jobs() const { return job_list; }
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2018-09-10 16:17:57 +08:00
|
|
|
/// Get the variables.
|
2019-05-21 00:27:46 +08:00
|
|
|
env_stack_t &vars() { return *variables; }
|
|
|
|
const env_stack_t &vars() const { return *variables; }
|
2018-09-10 16:17:57 +08:00
|
|
|
|
2019-04-29 07:11:49 +08:00
|
|
|
/// Get the library data.
|
|
|
|
library_data_t &libdata() { return library_data; }
|
|
|
|
const library_data_t &libdata() const { return library_data; }
|
|
|
|
|
2021-05-12 03:01:08 +08:00
|
|
|
/// Get our wait handle store.
|
|
|
|
wait_handle_store_t &get_wait_handles() { return wait_handles; }
|
|
|
|
const wait_handle_store_t &get_wait_handles() const { return wait_handles; }
|
2021-05-16 03:50:25 +08:00
|
|
|
|
2019-05-13 05:00:44 +08:00
|
|
|
/// Get and set the last proc statuses.
|
|
|
|
int get_last_status() const { return vars().get_last_status(); }
|
|
|
|
statuses_t get_last_statuses() const { return vars().get_last_statuses(); }
|
|
|
|
void set_last_statuses(statuses_t s) { vars().set_last_statuses(std::move(s)); }
|
|
|
|
|
2020-03-08 11:44:58 +08:00
|
|
|
/// Cover of vars().set(), which also fires any returned event handlers.
|
|
|
|
/// \return a value like ENV_OK.
|
|
|
|
int set_var_and_fire(const wcstring &key, env_mode_flags_t mode, wcstring val);
|
|
|
|
int set_var_and_fire(const wcstring &key, env_mode_flags_t mode, wcstring_list_t vals);
|
|
|
|
int set_empty_var_and_fire(const wcstring &key, env_mode_flags_t mode);
|
|
|
|
|
2019-05-20 05:40:06 +08:00
|
|
|
/// Pushes a new block. Returns a pointer to the block, stored in the parser. The pointer is
|
|
|
|
/// valid until the call to pop_block()
|
|
|
|
block_t *push_block(block_t &&b);
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
/// Remove the outermost block, asserting it's the given one.
|
2019-11-19 08:54:36 +08:00
|
|
|
void pop_block(const block_t *expected);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
/// Return a description of the given blocktype.
|
2019-12-23 07:37:14 +08:00
|
|
|
static const wchar_t *get_block_desc(block_type_t block);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2017-06-24 13:14:21 +08:00
|
|
|
/// Return the function name for the specified stack frame. Default is one (current frame).
|
2021-07-16 02:21:32 +08:00
|
|
|
maybe_t<wcstring> get_function_name(int level = 1);
|
2017-04-13 06:34:25 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
/// Promotes a job to the front of the list.
|
2012-02-28 10:43:24 +08:00
|
|
|
void job_promote(job_t *job);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
/// Return the job with the specified job id. If id is 0 or less, return the last job used.
|
2021-05-14 03:11:00 +08:00
|
|
|
const job_t *job_with_id(job_id_t job_id) const;
|
|
|
|
|
|
|
|
/// Return the job with the specified internal job id.
|
|
|
|
const job_t *job_with_internal_id(internal_job_id_t job_id) const;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
/// Returns the job with the given pid.
|
2018-08-10 07:46:11 +08:00
|
|
|
job_t *job_get_from_pid(pid_t pid) const;
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2020-07-23 06:35:14 +08:00
|
|
|
/// Returns a new profile item if profiling is active. The caller should fill it in.
|
|
|
|
/// The parser_t will deallocate it.
|
|
|
|
/// If profiling is not active, this returns nullptr.
|
2014-02-10 06:04:43 +08:00
|
|
|
profile_item_t *create_profile_item();
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2021-01-09 00:25:00 +08:00
|
|
|
/// Remove the profiling items.
|
|
|
|
void clear_profiling();
|
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
/// Output profiling data to the given filename.
|
2014-02-10 06:04:43 +08:00
|
|
|
void emit_profiling(const char *path) const;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2020-07-23 06:35:14 +08:00
|
|
|
void get_backtrace(const wcstring &src, const parse_error_list_t &errors,
|
|
|
|
wcstring &output) const;
|
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
/// Returns the file currently evaluated by the parser. This can be different than
|
2019-11-25 19:03:25 +08:00
|
|
|
/// reader_current_filename, e.g. if we are evaluating a function defined in a different file
|
2016-05-03 08:22:44 +08:00
|
|
|
/// than the one curently read.
|
2012-01-17 03:16:12 +08:00
|
|
|
const wchar_t *current_filename() const;
|
2006-01-26 23:47:22 +08:00
|
|
|
|
2019-05-28 05:52:48 +08:00
|
|
|
/// Return if we are interactive, which means we are executing a command that the user typed in
|
|
|
|
/// (and not, say, a prompt).
|
|
|
|
bool is_interactive() const { return libdata().is_interactive; }
|
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
/// Return a string representing the current stack trace.
|
2015-09-22 02:24:49 +08:00
|
|
|
wcstring stack_trace() const;
|
2017-01-22 06:53:10 +08:00
|
|
|
|
2019-11-11 04:36:46 +08:00
|
|
|
/// \return whether the number of functions in the stack exceeds our stack depth limit.
|
|
|
|
bool function_stack_is_overflowing() const;
|
|
|
|
|
2019-05-05 09:17:18 +08:00
|
|
|
/// \return a shared pointer reference to this parser.
|
|
|
|
std::shared_ptr<parser_t> shared();
|
|
|
|
|
2020-01-16 09:14:47 +08:00
|
|
|
/// \return a cancel poller for checking if this parser has been signalled.
|
|
|
|
cancel_checker_t cancel_checker() const;
|
|
|
|
|
|
|
|
/// \return the operation context for this parser.
|
|
|
|
operation_context_t context();
|
|
|
|
|
2017-01-22 06:53:10 +08:00
|
|
|
~parser_t();
|
2012-01-17 03:16:12 +08:00
|
|
|
};
|
2006-09-06 04:43:47 +08:00
|
|
|
|
2005-10-04 23:11:39 +08:00
|
|
|
#endif
|