Commit graph

189 commits

Author SHA1 Message Date
Idan Horowitz 086969277e Everywhere: Run clang-format 2022-04-01 21:24:45 +01:00
Ali Mohammad Pur e1cf51b0bd Shell: Add a shell option for autocompleting via the program itself
This feature needs a bit more work, so let's disable it by default.
Note that the shell will still use _complete_foo if it is defined
regardless of this setting.
2022-03-29 15:42:55 +02:00
Ali Mohammad Pur 5e541aaebd Shell: Keep the stdio and rpath pledges for execute_process()
If the command fails, we'd like to still be capable of printing out
diagnostics, so restore stdio and rpath.
Fixes #13281.
2022-03-27 21:05:44 +02:00
Ali Mohammad Pur f12d81ddf5 Shell: Limit the access of processes spawned for autocompletion
This commit limits the autocomplete processes to effectively have
readonly access to the fs, and only enough pledges to get the dynamic
loader working.
2022-03-26 21:34:56 +04:30
Ali Mohammad Pur ef5523231c Shell+LibCore: Provide argument help strings as display trivia 2022-03-26 21:34:56 +04:30
Ali Mohammad Pur 9453e0e6d2 Shell: Add an ArgsParser-based argument parser builtin
Afterall, why _shouldn't_ Shell functions have nice interfaces?
also helps with autocompletion :^)
2022-03-26 21:34:56 +04:30
Ali Mohammad Pur 7e4cc187d9 Shell: Implement program-aware autocompletion
A program can either respond to `--complete -- some args to complete`
directly, or add a `_complete_<program name>` invokable (i.e. shell
function, or just a plain binary in PATH) that completes the given
command and lists the completions on stdout.
Should such a completion fail or yield no results, we'll fall back to
the previous completion algorithm.
2022-03-26 21:34:56 +04:30
Ali Mohammad Pur e6bd1f8807 Shell: Add the 'join' and 'filter_glob' immediate functions
'split' was missing its other half, and to avoid globbing the
filesystem, let's keep the globbing to shell-internal state.
2022-03-26 21:34:56 +04:30
Lenny Maiorani dd05934539 Shell: Use default constructors/destructors
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#cother-other-default-operation-rules

"The compiler is more likely to get the default semantics right and
you cannot implement these functions better than the compiler."
2022-03-24 20:09:26 -07:00
Tim Schumacher 83609adbdf Shell: Stop parsing options after the script name 2022-03-11 08:41:21 +03:30
Ali Mohammad Pur a76730823a Shell: Be more smart with pasted stuff
Shell can now use LibLine's `on_paste` hook to more intelligently escape
pasted data, with the following heuristics:
- If the current command is invalid, just pile the pasted string on top
- If the cursor is *after* a command node, escape the pasted data,
  whichever way yields a smaller encoding
- If the cursor is at the start of or in the middle of a command name,
  paste the data as-is, assuming that the user wants to paste code
- If the cursor is otherwise in some argument, escape the pasted data
  according to which kind of string the cursor is in the middle of
  (double-quoted, single-quoted or a simple bareword)
2022-03-06 13:20:41 +01:00
Ali Mohammad Pur c4d9377477 Shell: Implement leftmost_trivial_literal() for Sequence nodes 2022-03-06 13:20:41 +01:00
Ali Mohammad Pur 0ea775f257 Shell: Allow completing StringLiterals as paths
This auto-escapes the token as well :^)
2022-03-06 13:20:41 +01:00
Ali Mohammad Pur 118590325a LibLine+Userland: Make suggestion offsets per-suggestion
This allows the user to modify different parts of the input with
different suggestions.
2022-03-06 13:20:41 +01:00
kperdlich a3ab8dcecd Shell: Use an opaque color for SyntaxError
Use an opaque color for SyntaxError in
Syntax Highlighter to avoid transparent errors.
2022-02-23 21:56:16 +00:00
Ryan Chandler 05cdfb8698 Shell: Start history counter from 1
Previously would show the list of history items starting from
an index of 0.

This is a bit misleading though. Running `!0` would actually cause
the parser to error out and prevent you from running the command.
2022-02-22 21:26:25 +03:30
kperdlich ad722a9f06 Shell: Use an opaque default color for BarewordLiteral
Use an opaque default color for BarewordLiteral in
Syntax Highlighter to avoid transparent barewords.
2022-02-19 11:33:28 +01:00
Ali Mohammad Pur 222e580fa8 Shell: Use strncmp() instead of string.compare() for name completions
The "at most n bytes" behaviour of strncmp is required for this logic to
work, this was overlooked in 5b64abe when converting Strings to
StringViews, which lead to broken autocomplete.
2022-02-05 16:59:12 +03:30
Tom Martin 6ec4fd9d3c Shell: Add total time to builtin_time Timing Report 2022-02-04 15:09:22 +03:30
Daniel Bertalan 5b64abe76e Shell: Use StringView instead of String const& where feasible 2022-01-29 23:08:27 +01:00
Sam Atkins 45cf40653a Everywhere: Convert ByteBuffer factory methods from Optional -> ErrorOr
Apologies for the enormous commit, but I don't see a way to split this
up nicely. In the vast majority of cases it's a simple change. A few
extra places can use TRY instead of manual error checking though. :^)
2022-01-24 22:36:09 +01:00
Ali Mohammad Pur 78d1093dab Shell: Make Juxtaposition autocompletion smarter
Now something like `"$HOME"/` autocompletes correctly.
Note that only the first element of lists is used to autocomplete
things.
2022-01-21 18:58:28 +03:30
Ali Mohammad Pur 2e333b3571 Shell: Make SimpleVariable::hit_test_position not hit irrelevant offsets
Without this, any offset would be accepted as being part of the
SimpleVariable.
Fixes #11976 (by making it no longer crash).
2022-01-21 18:58:28 +03:30
Ali Mohammad Pur 5c0c126122 Shell: Add a "noop" builtin aliased to ":"
POSIX comes up with such silly names sometimes...
This builtin does nothing. at all.
2022-01-09 12:25:36 +01:00
Ali Mohammad Pur 6a245de911 Shell: Refresh PATH cache after 'unset PATH'
Note that `execvp` has a default value for PATH (both on Serenity and on
Linux) and so this does not 'fix' #11608.
2022-01-09 12:25:36 +01:00
Ali Mohammad Pur 310a18da1e Shell: Don't reset 'last_return_code' before running commands
Some variables depend on its value to function correctly.
Fixes the following issue:
    $ false; echo $?
    1
    $ false
    $ echo $?
    128
2022-01-09 12:25:36 +01:00
Ali Mohammad Pur ea66750640 Shell: Make interrupts kill the whole chain and not just the current job
This makes interrupting `sleep 10; echo hi` not print `hi` anymore,
which is the expected behaviour anyway.
Also fixes the problem with fast-running loops "eating" interrupts and
not quitting.
2022-01-09 11:16:17 +03:30
Lucas CHOLLET ed0f4bdfaf Shell: Port to LibMain 2022-01-09 03:22:10 +03:30
Andrew Kaster 170a7e263c Userland: Fail Core::find_executable_in_path on empty inputs
Before this patch, `which ""` or `type ""` would say that the empty
string is `/usr/local/bin/`.

Convert callers to consistently call is_empty() on the returned string
while we're at it, to support eventually removing the is_null() String
state in the future.
2022-01-04 07:38:42 +00:00
Ali Mohammad Pur 783e27f8f9 Shell: Make redirection errors raise ShellErrors
Naturally, this means that a command with a failing redirection will
not start, and so will terminate the pipeline (if any).
This also applies to the `exit` run when the shell is closed, fixing a
fun bug there as well (thanks to Discord user Salanty for pointing that
out) where closing the terminal (i.e. I/O error on the tty) with a
failing `exit` command would make the shell retry executing `exit` every
time, leading to an eventual stack overflow.
2021-12-31 02:19:45 +03:30
Daniel Bertalan 6b39c6b1bf Shell: Avoid many single byte write() syscalls when printing the prompt
Whenever the prompt is printed, we write a line's worth of space
characters to the terminal to ensure that the prompt ends up on a new
line even if there is dangling output on the current line.

We write these to the stderr, which is unbuffered, so each putc() call
would come with the overhead of a system call. Let's use a buffer
+ fwrite() instead, since heap allocation is much faster.
2021-12-30 14:26:29 +01:00
Ali Mohammad Pur 9bf81463f5 Shell: Don't skip over the first brace expansion entry if it's empty
Previously we were simply ignoring the empty entry in '{,x}', making it
resolve to a list with a single element '(x)', this commit makes that
work as expected and resolve to '("" x)'.
2021-12-16 03:26:59 +03:30
Ali Mohammad Pur 7ac8bd44f8 Shell: Set subshell flag after checking for its value in parent shell
This also reverts commit 07cc7eed29, as
that attempted to fix the issue this caused (and succeeded...but it
broke something else on linux).
2021-12-16 03:26:59 +03:30
Ali Mohammad Pur 6bf50bc40b Shell: Make the Join operation respect nodes that have a next chain
This would show up when resolving aliases, when an alias contains a
sequence.
Fixes #11219.
2021-12-13 16:26:10 +03:30
Michel Hermier 18f053ac31 Shell: Remove sigpipe.sh.out artefact after failure
Helps to avoid to erroneously add the file to git.
2021-12-13 16:20:29 +03:30
Daniel Bertalan 4a81b33c07 Everywhere: Fix -Winconsistent-missing-override warnings from Clang
This option is already enabled when building Lagom, so let's enable it
for the main build too. We will no longer be surprised by Lagom Clang
CI builds failing while everything compiles locally.

Furthermore, the stronger `-Wsuggest-override` warning is enabled in
this commit, which enforces the use of the `override` keyword in all
classes, not just those which already have some methods marked as
`override`. This works with both GCC and Clang.
2021-12-11 13:14:15 -08:00
Sam Atkins b138070da9 Shell: Cast unused smart-pointer return values to void 2021-12-05 15:31:03 +01:00
Andreas Kling 216e21a1fa AK: Convert AK::Format formatting helpers to returning ErrorOr<void>
This isn't a complete conversion to ErrorOr<void>, but a good chunk.
The end goal here is to propagate buffer allocation failures to the
caller, and allow the use of TRY() with formatting functions.
2021-11-17 00:21:13 +01:00
Andreas Kling 8b1108e485 Everywhere: Pass AK::StringView by value 2021-11-11 01:27:46 +01:00
Andreas Kling 5f7d008791 AK+Everywhere: Stop including Vector.h from StringView.h
Preparation for using Error.h from Vector.h. This required moving some
things out of line.
2021-11-10 21:58:58 +01:00
Andreas Kling d1477bcb8e Shell: Replace Result<T, E> use with ErrorOr<T> 2021-11-08 00:35:27 +01:00
Musab Kılıç 3b8853c3cd Shell: Add min and max iteration times to time -n in builtin_time 2021-11-06 22:09:25 -07:00
Ben Wiederhake b8f11b1bae Everywhere: Remove unused ArgsParser header
Found while trying to enumerate all programs that use ArgsParser.
2021-11-01 21:12:58 +01:00
Ali Mohammad Pur d020d46846 Shell: Unwind execution after runtime errors
This commit makes the Shell check for errors after a node is run(), and
prevents further execution by unwinding until the error is cleared.
Fixes #10649.
2021-10-31 12:02:20 +01:00
Ben Wiederhake 48e4fb239a Shell: Prevent exponential explosion around '$(('
When parse_expression looks at '$((', there are two ways it can end up
in parse_expression again, three consumed characters later. All these
ways fail, so what happened was that the parser tried all possible
combinations, hence taking potentially an exponential amount of time.

1. parse_evaluate swallows the '$(', a new invocation of
   parse_expression swallows the other '(', and through
   parse_list_expression we're at another parse_expression.
2. parse_evaluate swallows the '$(', but returns a SyntaxError.
   parse_expression used to not recognize the error, and treated it as a
   regular AST node, calling into read_concat, then a new invocation of
   parse_expression swallows the other '(', and through
   parse_list_expression we're at another parse_expression.

Fixes #10561.

Found by OSS Fuzz, long-standing issue
https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=28113
2021-10-23 19:29:59 +01:00
Ali Mohammad Pur 045c85af4b Shell: Raise an error if an execute node ends up trying to run nothing
...while capturing its standard output.
As `$()` is an invalid construct, execute nodes are not supposed to
capture the output of no command being run; but it is possible to create
empty commands such as CastToCommand(Redirection(...)) or similar.
Make this a hard error instead of an unescapable select().
This was noticed in #10432, which should now error out like so:
```
Error: Cannot capture standard output when no command is being executed
  0| $(<$file)
~~~~~^^^^^^^^^
  1|
```
2021-10-11 10:56:01 +03:30
Nico Weber 9d06448bc7 Shell: Fix -Wunreachable-code warnings from clang 2021-10-08 23:33:46 +02:00
Ali Mohammad Pur b946a1ce48 Shell: Make ArgsParser not exit on failure in builtin_exit()
Fixes #10128.
2021-09-20 03:31:57 +04:30
Brian Gianforcaro 951cd68979 Shell: Use default instead of an empty constructor/destructor
Default implementations allow for more optimizations.
See: https://pvs-studio.com/en/docs/warnings/v832/
2021-09-16 17:17:13 +02:00
Brian Gianforcaro f6d179b304 Shell: Use ElapsedTimer::start_new() 2021-09-12 17:24:44 +00:00