Commit graph

51 commits

Author SHA1 Message Date
Andreas Kling
c4e2fd8123 Shell: Move to Userland/Shell/ 2021-01-12 12:04:07 +01:00
AnotherTest
335221d830 Shell: Run function declarations in the current process
Fixes #4817
2021-01-06 15:40:45 +01:00
AnotherTest
3d51bcaa4e Shell: Give commands and globs a source position
This will help us have nicer error reporting.
2021-01-03 17:13:00 +01:00
AnotherTest
cdebdaea2d Shell: Move AST::create() into the header and use it 2021-01-03 17:13:00 +01:00
asynts
7e62ffbc6e AK+Format: Remove TypeErasedFormatParams& from format function. 2020-12-30 20:33:53 +01:00
AnotherTest
5e5eb615ec Shell: Add runtime errors and implement break/continue
Such errors are raised when SyntaxError nodes are executed, and are also
used for internal control flow.
The 'break' and 'continue' commands are currently only allowed inside
for loops, and outside function bodies.

This also adds a 'loop' keyword for infinite loops.
2020-12-29 16:55:43 +01:00
AnotherTest
4668dfa7c7 Shell: Make Subshell actually create a subshell
Previously, a "subshell" would just be executed in the parent shell.
2020-12-15 20:58:32 +01:00
AnotherTest
5325d6871d Shell: Make <return> go to a new line when the command is incomplete
"incomplete" meaning that it has a syntax error that can be recovered
from by continuing the input.
2020-12-08 23:34:38 +01:00
AnotherTest
1a4ac3531f Shell: Allow parts of globs to be named in match expressions
This patchset allows a match expression to have a list of names for its
glob parts, which are assigned to the matched values in the body of the
match.
For example,
```sh
stuff=foobarblahblah/target_{1..30}
for $stuff {
    match $it {
        */* as (dir sub) {
            echo "doing things with $sub in $dir"
            make -C $dir $sub # or whatever...
        }
    }
}
```

With this, match expressions are now significantly more powerful!
2020-10-29 11:53:01 +01:00
AnotherTest
f4b7a688b1 Shell: Rename {source,dest}_fd to {old,new}_fd
This makes `Rewiring' much more understandable, and un-confuses the uses
of `dup2()'.
Also fixes `dup2()' bugs.
2020-10-29 11:53:01 +01:00
AnotherTest
8de70e8ce7 Shell: Implement AK::Formatter::format() for AST::Command
...and use that to display jobs.
2020-10-26 14:28:38 +01:00
AnotherTest
5640e1bc3a Shell: Add support for brace expansions
This adds support for (basic) brace expansions with the following
syntaxes:
- `{expr?,expr?,expr?,...}` which is directly equivalent to `(expr expr
  expr ...)`, with the missing expressions replaced with an empty string
  literal.
- `{expr..expr}` which is a new range expansion, with two modes:
    - if both expressions are one unicode code point long, the range is
      equivalent to the two code points and all code points between the
      two (numerically).
    - if both expressions are numeric, the range is equivalent to both
      numbers, and all numbers between the two.
    - otherwise, it is equivalent to `(expr expr)`.

Closes #3832.
2020-10-25 10:09:27 +01:00
AnotherTest
f164b808b5 Shell: Move everything to the Shell namespace
Also provide a basic default-constructor.
2020-10-04 23:12:28 +02:00
AnotherTest
a10cfee0d4 Shell: Track line numbers and the positions of some keywords 2020-09-30 20:05:24 +02:00
AnotherTest
b3dd97a694 Shell: Add a (very basic) formatter 2020-09-26 21:28:35 +02:00
AnotherTest
d64e00a5f6 Shell: Rename two 'fd' class members to have an 'm_' prefix 2020-09-26 21:28:35 +02:00
AnotherTest
51e598cf0b Shell: Use NonnullRefPtr to store non-null subnodes
Also replaces null-is-invalid nodes with syntax error nodes.
2020-09-26 21:28:35 +02:00
AnotherTest
4c6f7846b4 Shell: Add 'match' expressions
This commit adds an equivalent to the sh 'case' construct, except it's
much more pleasing to look at and write:
```sh
match "$something" {
    p1 { echo "p1!" }
    p2 { echo "p2!" }
    *  { echo "string catch-all!" }
}
```
is the equivalent of:
```sh
case $something in
    p1)
        echo "p1!"
        ;;
    p2)
        echo "p2!"
        ;;
    *)
        echo "catch-all!"
        ;;
esac
```

Since our shell does not treat lists as strings, matching lists is also
possible:

```sh
match (1foo 2foo foo3) {
    (?foo 2* *) { echo wowzers! }
    (* * *) { echo 3-element list catch-all }
}
```
2020-09-15 20:36:59 +02:00
AnotherTest
2b867ff555 Shell: Complete named function parameters inside the function body 2020-09-14 17:40:18 +02:00
AnotherTest
d1550ea64f Shell: Add support for functions
This implementation does not have support for 'return' yet.
2020-09-14 17:40:18 +02:00
AnotherTest
b194d79c53 Shell: Add the (now) free subshell support 2020-09-09 20:35:21 +02:00
AnotherTest
aa2df9277d Shell: Allow control structures to appear in pipe sequences
This makes commands like the following to be possible:
```sh
$ ls && for $(seq 10) { echo $it }
$ ls | for $(seq 1) { cat > foobar }
```
2020-09-09 20:35:21 +02:00
AnotherTest
715e11f692 Shell: Fix job control and backgrounding
This patchset makes the shell capable of lazily resolving and executing
sequences of commands, to allow for putting logical sequences in the
background.
In particular, it enables And/Or/Sequence nodes to be run in the background,
and consequently unmarks them as `would_execute`.
Doing so also fixes job control to an extent, as jobs are now capable of
having 'tails', so sequences can be put in the background while
preserving their following sequences.
2020-09-09 20:35:21 +02:00
AnotherTest
b90eb5c9ba Shell: Add 'if' expressions
```sh
if foo bar baz {
    quux
} else if foobar || whatever {
    echo I ran out of example words
} else {
    exit 2
}
```
2020-08-22 20:53:21 +02:00
AnotherTest
0676bd4afc Shell: Mark AST::Background as would_execute if its subnode does 2020-08-22 20:52:07 +02:00
AnotherTest
d4bcc689fb Shell: Make 'for' loops read their input as an stream
i.e. process an element as it becomes available.
2020-08-21 16:00:42 +02:00
Andreas Kling
85b02d887b Shell: Add create() factory function for PathRedirection 2020-08-12 12:15:30 +02:00
Andreas Kling
e8d665337a Shell: Fix another FdRedirection reference leak
Add a create() factory function to prevent this from happening again.
2020-08-12 12:13:33 +02:00
AnotherTest
ab3e787334 Shell: Moves pipelined processes to one process group 2020-08-12 11:41:18 +02:00
Andreas Kling
3b3d158649 Shell: Make Command::redirections a NonnullRefPtrVector 2020-08-07 09:42:12 +02:00
Andreas Kling
c29681cb03 Shell: Make VariableDeclarations::Variable store NonnullRefPtrs 2020-08-07 09:41:04 +02:00
Andreas Kling
e9c602bc83 Shell: Make resolve_without_cast() return NonnullRefPtr<Value> 2020-08-07 09:36:15 +02:00
Andreas Kling
420e809fee Shell: Store ListValue's values in a NonnullRefPtrVector<Value>
A ListValue never stores null Values, so it makes sense to restrict it.
This also propagates use of NonnullRefPtr to the create() helpers.
There's a small bit of awkwardness around the use of initializer_list,
since we cannot directly construct a NonnullRefPtrVector from one.
2020-08-07 09:33:05 +02:00
Andreas Kling
08e5371f44 Shell: Add some obvious move() calls in AST constructors 2020-08-07 09:19:59 +02:00
Andreas Kling
3cb8ae873c Shell: Use NonnullRefPtr to simplify some things in the parser/AST 2020-08-04 18:17:16 +02:00
AnotherTest
1d08cab9ab Shell: Correct FdRedirection inheriting from two RefCounted bases
Also add missing calls to `adopt()`.
2020-08-04 13:40:58 +02:00
AnotherTest
12af65c1c9 Shell: Add support for ARGV (and $*, $#)
This patchset also adds the 'shift' builtin, as well as the usual tests.
closes #2948.
2020-08-04 13:40:58 +02:00
AnotherTest
8e364b9780 Shell: Mark ForLoop as would_execute
This fixes #2825.
2020-07-17 23:18:15 +02:00
AnotherTest
b6066faa1f Shell: Add a 'for' loop
Closes #2760.
This commit adds a 'for' loop, and tweaks the syntax slightly to make &&
bind more tightly than || (allowing for `expr && if_ok || if_bad`) :^)
2020-07-16 16:01:10 +02:00
AnotherTest
95fc7dd03a Shell: Parse lists serially, and flatten them only when needed
This allows `((1 2 3) (4 5 6))` to remain nested until we explicitly
flatten it out.
2020-07-16 16:01:10 +02:00
AnotherTest
b0ce8d725a Shell: Move out run_commands and expand_aliases to be Shell member fns
This makes running commands from outside the AST chain easier.
2020-07-13 15:12:28 +02:00
AnotherTest
f9d3055691 Shell: Do not treat the ending newline as part of a comment
This allows the parser to finally parse the entire source into a single
AST.
As a result of allowing comments inside sequences, Sequence is also
marked as would_execute if its left or right node would.
2020-07-06 13:25:42 +02:00
AnotherTest
ff857cd358 Shell: Initial support for 'option' completions
Take one small step towards #2357.
Handle completing barewords starting with '-' by piping the requests to
the Shell::complete_option(program_name, option) :^)

Also implements completion for a single builtin (setopt) until we figure out how
to handle #2357.
2020-07-05 15:43:14 +02:00
AnotherTest
b8d1edb2a2 Shell: Add a 'setopt' builtin
This builtin sets (and unsets) boolean flags that alter the behaviour of
the shell.
The only flags added are
- inline_exec_keep_empty_segments: Keep empty segments in the result of
  splitting $(...) by $IFS
- verbose: Announce each command before executing it

It should be noted that the (rather extreme) verbosity of the names is
intentional, and will hopefully be alleviated by the next commit :^)
2020-07-05 15:43:14 +02:00
AnotherTest
d6de2b5828 Shell: Show descriptions about syntax errors
The description contains an error message and where in the source the
error happened.
2020-07-05 15:43:14 +02:00
AnotherTest
d2bdbc3e77 Shell: Mark And and Or nodes as execute nodes 2020-07-05 15:43:14 +02:00
AnotherTest
3a37e8c56f Shell: Provide completions to Tilde and its Juxtaposition.
This commit also removes the ExecutionInputType and directly uses
RefPtr<Shell> instead, since nothing else is needed for execution
purposes, and also makes the shell refuse to evaluate commands with
any sort of syntax error.
2020-07-05 15:43:14 +02:00
AnotherTest
c5d0aa9a44 Shell: Allow commands in variables, and properly substitute them on use
This allows the below interaction to work:
```
$ silence=(2>&1 >/dev/null)
$ do_noisy_thing with these args $silence
<nothing here lol>
```
2020-07-05 15:43:14 +02:00
AnotherTest
42304d7bf1 Shell: Parse a pipe sequence inside $(...) 2020-07-05 15:43:14 +02:00
AnotherTest
8e078cf4ab Shell: Expand Juxtaposition of lists to list products
This commit makes `echo x(foo bar)` create an argv of `echo xfoo xbar`,
essentially modeling brace expansions in some shells.
2020-07-05 15:43:14 +02:00