Commit Graph

87 Commits

Author SHA1 Message Date
Johannes Altmanninger
7d5b44e828 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-11-25 09:20:51 +01:00
ridiculousfish
2555ecf757 Remove the forbidden function stack
Detect forbidden functions directly from the associated block_t.
Also unify where we do stack overflow detection.
2019-11-10 12:36:46 -08:00
ridiculousfish
a7f1d2c0c7 Add support for fish_trace variable to trace execution
This adds support for `fish_trace`, a new variable intended to serve the
same purpose as `set -x` as in bash. Setting this variable to anything
non-empty causes execution to be traced. In the future we may give more
specific meaning to the value of the variable.

The user's prompt is not traced unless you run it explicitly. Events are
also not traced because it is noisy; however autoloading is.

Fixes #3427
2019-11-02 14:40:57 -07:00
LawAbidingCactus
305a657694 fix typos 2019-10-23 19:38:44 +02:00
Johannes Altmanninger
2d2e15b63d Silently ignore recursive calls to complete -C without parameter
In e167714899 we allowed recursive calls
to complete. However, some completions use infinite recursion in their
completions and rely on `complete` to silently stop as soon as it is
called recursively twice without parameter (thus completing the
current commandline). For example:

complete -c su -s -xa "(complete -C(commandline -ct))"
su -c <TAB>

Infinite recursion happens because (commandline -ct) is an empty list,
which would print an error message.  This commmit explicitly detects
such recursive calls where `complete` has no parameter and silently
terminates.  This enables above completion (like before raising the
recursion limit) while still allowing legitimate cases with limited
recursion.

Closes #6171
2019-10-15 13:36:28 +02:00
ridiculousfish
29dede8139 Migrate parse_util_detect_errors to a free function 2019-08-04 14:49:56 -07:00
ridiculousfish
4a2c709fb1 Eliminate shell_is_interactive
We used to have a global notion of "is the shell interactive" but soon we
will want to have multiple independent execution threads, only some of
which may be interactive. Start tracking this data per-parser.
2019-06-29 11:28:26 -07:00
ridiculousfish
6637ccd3a2 Keep an fd for the cwd in the parser
To support distinct parsers having different working directories, we need
to keep the working directory alive, and also retain a non-path reference
to it.
2019-06-10 09:27:51 -07:00
ridiculousfish
1baa479bbf Eliminate the global list of scoped transient commandlines
Store this in a parser's libdata instead.
2019-06-09 14:11:25 -07:00
ridiculousfish
ff55249447 Make events per-parser
This makes the following changes:

1. Events in background threads are executed in those threads, instead of
being silently dropped

2. Blocked events are now per-parser instead of global

3. Events are posted in builtin_set instead of within the environment stack

The last one means that we no longer support event handlers for implicit
sets like (example) argv. Instead only the `set` builtin (and also `cd`)
post variable-change events.

Events from universal variable changes are still not fully rationalized.
2019-06-03 02:48:35 -07:00
ridiculousfish
e91d68266c Eliminate reader_current_filename
Store this in the parser libdata instead.
2019-05-22 13:51:27 -07:00
ridiculousfish
686b84396c Migrate the return bool outside of block_t
This is a flag that gets set by the return function. But we only need one,
not per-block. Move it into libdata.
2019-05-22 13:51:27 -07:00
ridiculousfish
11209b7553 Switch the block stack to a deque instead of vector of shared pointers
That makes the block stack easier to copy.
2019-05-21 10:25:48 -07:00
ridiculousfish
ad57133c7f Switch parser_t to hold its variables via shared_ptr
Preparation for variable stacks with finite lifetimes.
2019-05-21 10:25:48 -07:00
ridiculousfish
159d6d669a Remove all block_t subclasses 2019-05-19 14:44:40 -07:00
ridiculousfish
eff4873eca Stop creating subclasses of block_t
Move all block_t creation methods to static methods, and stop creating
subclasses (all of which are now empty).
2019-05-19 14:40:35 -07:00
ridiculousfish
cf92b7626c Migrate event_block's event into block_t 2019-05-19 13:07:06 -07:00
ridiculousfish
8697fa063b Migrate source_block's source_file into block_t
Continue to work towards flattening this hierarchy.
2019-05-19 13:01:59 -07:00
ridiculousfish
fec0e40b5e Migrate function_block name and args into block_t
The goal is to eliminate this block hierarchy.
2019-05-19 12:56:07 -07:00
ridiculousfish
cd7e8f4103 Migrate loop status from blocks into libdata
Blocks will soon need to be shared across parsers. Migrate the loop status
(like break or continue) from the block into the libdata. It turns out we
only ever need one, we don't need to track this per-block.

Make it an enum class.
2019-05-19 12:50:05 -07:00
ridiculousfish
c42eb0eb4f Remove the process from function_block_t
Prior to this fix, a function_block stored a process_t, which was only used
when printing backtraces. Switch this to an array of arguments, and make
various other cleanups around null terminated argument arrays.
2019-05-18 21:09:04 -07:00
ridiculousfish
508c3a8005 Make is_event and other globals part of parser_t libdata 2019-05-18 19:03:45 -07:00
ridiculousfish
5158ee812b Eliminate the job from block_t
This exists only to support the '--on-job-exit caller' feature.
Just store the calling job ID directly in the parser's libdata.
2019-05-18 18:50:28 -07:00
ridiculousfish
1719d6f136 Make $status and $pipestatus per-parser
Another step towards allowing multiple parsers to execute in parallel.
2019-05-12 14:00:44 -07:00
ridiculousfish
8a8b2513b5 Eliminate the global jobs() function
All job lists are attached to a parser now.
2019-05-05 11:33:08 -07:00
Fabian Homborg
c2970f9618 Reformat all files
This runs build_tools/style.fish, which runs clang-format on C++, fish_indent on fish and (new) black on python.

If anything is wrong with the formatting, we should fix the tools, but automated formatting is worth it.
2019-05-05 12:09:25 +02:00
ridiculousfish
9fb98baba6 Thread the parser into process_clean_after_marking 2019-05-04 20:58:35 -07:00
ridiculousfish
36998eee55 Make more miscellaneous globals thread safe 2019-05-04 20:58:35 -07:00
ridiculousfish
0de1611bf1 Migrate builtin_complete recursion_level into parser_t::libdata 2019-05-04 20:58:35 -07:00
ridiculousfish
b0a695e9fa Migrate the global exec_count into the parser 2019-05-04 20:58:35 -07:00
ridiculousfish
bffacd2fbf Thread a parser into expansion
Expansion may perform command substitution, which needs to know the parser
to use.
2019-05-04 19:30:00 -07:00
ridiculousfish
923a7ca0f0 Thread the parser into complete()
Eliminates uses of principal_parser
2019-05-04 18:17:18 -07:00
Mahmoud Al-Qudsi
b2a1da602f Fix error propagation in parser_t::eval
It was unconditionally returning `parse_execution_success`. This was
causing certain parser errors to incorrectly return after evaluation
with `$status` equal to `0`, as reported after `eval`, `source`, or
sub-`fish` execution.
2019-04-13 17:28:16 -05:00
ridiculousfish
dd007c29f4 Revert "parser: try to avoid some strings being copied"
This reverts commit 7a74198aa3.

Believe it or not this commit actually increased copying. When accepting
a value you know you're going to take ownership of, just accept it by
value; then temporaries can invoke the move ctor and blah blah blah.

We really need a lightweight refcounted pass-by-value string to make this
less error prone.
2019-04-01 20:22:02 -07:00
Mahmoud Al-Qudsi
f8e0e0ef82 Remove abstractions around job list
Directly access the job list without the intermediate job_iterator_t,
and remove functions that are ripe for abuse by modifying a local
enumeration of the same list instead of operating on the iterators
directly (e.g. proc.cpp iterates jobs, and mid-iteration calls
parser::job_remove(j) with the job (and not the iterator to the job),
causing an invisible invalidation of the pre-existing local iterators.
2019-03-28 18:55:36 -05:00
Aaron Gyes
7a74198aa3 parser: try to avoid some strings being copied 2019-03-23 12:34:48 -07:00
ridiculousfish
815e20066b parser_t to become enable_shared_from_this 2019-02-24 12:12:24 -08:00
ridiculousfish
780b53ba73 Convert event_type_t to an enum class 2019-02-23 13:17:28 -08:00
ridiculousfish
003998c921 Event blocks just block all events
In a galaxy far, far away, event_blockage_t was intended to block only cetain
events. But it always just blocked everything. Eliminate the event block
mask.
2019-02-23 13:02:07 -08:00
ridiculousfish
6f52e6bb1c Instantize contents of exec.cpp and others 2019-01-10 20:07:47 -08:00
ridiculousfish
421fbdd52a Instantize env_get_pwd_slash
This requires threading environment_t through many places, such as completions
and history. We introduce null_environment_t for when the environment isn't
important.
2019-01-10 20:01:28 -08:00
ridiculousfish
a47f6859bd Equip parser_t with a variable stack
Prepares to eliminate env_get and env_set by accessing variables through
a parser.
2019-01-10 20:29:10 -08:00
ridiculousfish
895c2c4af0 Minor cleanup of parser interface 2019-01-10 20:29:10 -08:00
ridiculousfish
3770d9fb7a Teach each job about its parent
The parent of a job is the parent pipeline that executed the function or
block corresponding to this job. This will help simplify
process_mark_finished_children().
2018-11-04 01:40:07 -08:00
Jon Eyolfson
c3ca108dbe Added const to methods which 'ought to be const' based on Const Checker 2018-08-09 19:05:47 -07:00
ridiculousfish
137c2559d9 Remove parser_error
It was unused.
2018-02-18 20:29:28 -08:00
ridiculousfish
da84b38430 Constructors to accept more parameters by value
In cases where the constructor needs to take ownership of parameters,
pass them by value and use std::move.
2018-02-18 19:12:45 -08:00
ridiculousfish
d536b152f7 Simplify the parser_t::eval() return type to void
The return value was unused.
2018-02-12 10:55:55 -08:00
ridiculousfish
4eb73862fc Switch parser_t::execution_contexts to only storing one
We only ever need the topmost (currently executing) context. We can
store the stack via the C stack rather than an explicit std::vector.
2018-02-12 10:55:42 -08:00
ridiculousfish
92d5c28730 Move eval_level into parser_t
This avoids the ping-ponging of eval_level through
parse_execution_context. Simply store the global eval level in the
parser_t.
2018-02-12 10:55:31 -08:00