Commit Graph

67 Commits

Author SHA1 Message Date
Ali Mohammad Pur
84777fbe62 Shell: Allow the user to set the prompt using PROMPT()
This allows the prompt to be dynamically configurable, making it
possible to display various bits of information in the prompt.
2023-09-07 11:52:37 -06:00
Liav A
fb60db7b00 Shell: Add builtin command to reset the internal state
The new builtin command "reset" now resets the entire internal state by
virtually destructing the Shell state and re-constructing it.

This helps for example when setting a new hostname and wanting to view
it in the current Shell program.
2023-08-11 18:48:53 +03:30
Andreas Kling
ddbe6bd7b4 Userland: Rename Core::Object to Core::EventReceiver
This is a more precise description of what this class actually does.
2023-08-06 20:39:51 +02:00
Andreas Kling
26647f2b10 LibCore: Remove the Core::Objects::all_objects() list
Nobody actually used the list of all Core::Objects anyway.
2023-08-06 18:09:25 +02:00
Ali Mohammad Pur
1696411f66 Shell: Reset the custom Shell keybinds before calling Editor::get_line()
Fixes #19301.
2023-06-12 10:44:44 +03:30
jwijenbergh
9531abcb1a Shell: Add a "." builtin that aliases to "source" in POSIX
This patch adds an alias to the source builtin when an user types ".".
We cannot just add an enumeration entry using __ENUMERATE_SHELL_BUILTIN
because "." is not a valid name in a function.
This patch adds handling similarly to the name rewriting of ":".

This alias is limited to POSIX mode only.
2023-06-05 09:25:11 +03:30
Ali Mohammad Pur
24c7995743 Shell: Rename the verb {lookup => look_up} 2023-05-05 09:35:24 +03:30
Ali Mohammad Pur
4d00b372c8 Shell: Make the builtins' POSIX mode requirement more explicit
Instead of checking in each individual function, add the mode to
ENUMERATE_SHELL_BUILTINS() and let has_builtin() handle it.
2023-05-05 09:35:24 +03:30
Ali Mohammad Pur
1403e56535 Shell: Place all variables in the closest nonlocal frame in POSIX mode 2023-05-05 09:35:24 +03:30
Ali Mohammad Pur
fca5a34ad3 Shell: Allow assignment-prefixed commands to run builtins
`env` is not capable of running shell builtins, so make a simple
builtin in its place.
2023-05-05 09:35:24 +03:30
Ali Mohammad Pur
8a042cd9cb Shell: Allow non-exhaustive 'case' statements in POSIX 2023-05-05 09:35:24 +03:30
Ali Mohammad Pur
ef551a045d Shell: Add support for the POSIX 'read' builtin 2023-05-05 09:35:24 +03:30
Ali Mohammad Pur
79c76d67ce Shell: Add the 'break' and 'continue' POSIX builtins
These only support n=1 for now.
2023-05-05 09:35:24 +03:30
Andreas Kling
3de8dd921e Userland: Remove serialize-to-JSON functions only used for Inspector 2023-04-25 14:48:40 +02:00
Cameron Youell
da305017de Shell: Use JsonArray::append in save_to
We can convert it into a failable function here easily
2023-04-24 09:21:51 +02:00
Ali Mohammad Pur
7c312980b0 Shell: Implement arithmetic expansions for POSIX mode
This also adds a 'math' immediate function that can be used in Shell
proper to do arithmetic stuff.
2023-03-22 09:46:16 +01:00
Ali Mohammad Pur
d997b794fa Shell: Load a different rc file when in POSIX mode 2023-03-22 09:46:16 +01:00
Andreas Kling
21db2b7b90 Everywhere: Remove NonnullOwnPtr.h includes 2023-03-06 23:46:35 +01:00
Andreas Kling
359d6e7b0b Everywhere: Stop using NonnullOwnPtrVector
Same as NonnullRefPtrVector: weird semantics, questionable benefits.
2023-03-06 23:46:35 +01:00
Andreas Kling
8a48246ed1 Everywhere: Stop using NonnullRefPtrVector
This class had slightly confusing semantics and the added weirdness
doesn't seem worth it just so we can say "." instead of "->" when
iterating over a vector of NNRPs.

This patch replaces NonnullRefPtrVector<T> with Vector<NNRP<T>>.
2023-03-06 23:46:35 +01:00
Ali Mohammad Pur
0c28fd41ed Shell: Convert all immediately convertible fallible functions to ErrorOr 2023-02-28 15:52:24 +03:30
Ali Mohammad Pur
5f950df3d4 Shell: Make Immediate expression invocation fallible
This removes a whole bunch of FIXMEs in the immediate expr
implementations as well :^)
2023-02-28 15:52:24 +03:30
Ali Mohammad Pur
007767fc14 Shell: Convert builtins to use the modern main() style
That is, return ErrorOr<int>, handle fallible ops with TRY() and accept
a Main::Arguments.
Note that we do not populate the argc/argv members of Main::Arguments,
so all accesses have to go through .strings.
2023-02-28 15:52:24 +03:30
Andreas Kling
68b5df6bf1 Shell: Fix (and paper over) various const-correctness issues 2023-02-21 00:54:04 +01:00
Ali Mohammad Pur
4efc632e15 Shell: Allow the heredoc node to act as a redirection too
This will be used in a future commit to implement POSIX sh heredocs.
2023-02-18 06:55:46 +03:30
Ali Mohammad Pur
2a276c86d4 Shell: Start implementing a POSIX-compliant parser
The parser is still very much a work-in-progress, but it can currently
parse most of the basic bits, the only *completely* unimplemented things
in the parser are:
- heredocs (io_here)
- alias expansion
- arithmetic expansion

There are a whole suite of bugs, and syntax highlighting is unreliable
at best.
For now, this is not attached anywhere, a future commit will enable it
for /bin/sh or a `Shell --posix` invocation.
2023-02-13 23:00:15 +03:30
Vetrox
0e26f2657e Shell: Add where builtin
The builtin is based on the behaviour of the z-shell.
Namely it tries to resolve every argument one by one.

When resolving (in the order below) the following results can occur:
  1. The argument is a shell built-in command. Then print it.
  2. The argument is an alias. In this case we print the mapped value.
  3. The argument was found in the `PATH` environment variable.
     In this case we print the resolved absolute path
     and try to find more occurences in the `PATH` environment variable.
  4. None of the above. If no earlier argument got resolved,
     we print the error `{argument} not found`.

If at least one argument got resolved we exit with exit code 0,
otherwise 1.

By not using Core::File to resolve the executable in the environment
but rather using a modified version of the code we print every
matching executable of the given name. This behaviour matches
up with the z-shell.

The builtin has the following flags to modify the behaviour according
to the users needs:
  - `-p --path-only`: This skips the built-in and alias checks
  (step 1 & 2)
  - `-s --follow-symlink`: This follows the symlinks of an executable to
  its symlink-free location.
  - `-w --type`: This displays the type of the found object
  without any additional descriptive information.
2022-12-27 07:25:42 +03:30
Linus Groh
6e19ab2bbc AK+Everywhere: Rename String to DeprecatedString
We have a new, improved string type coming up in AK (OOM aware, no null
state), and while it's going to use UTF-8, the name UTF8String is a
mouthful - so let's free up the String name by renaming the existing
class.
Making the old one have an annoying name will hopefully also help with
quick adoption :^)
2022-12-06 08:54:33 +01:00
Tim Schumacher
5f99934dce Userland: Consolidate most PATH resolving into a single implementation
We previously had at least three different implementations for resolving
executables in the PATH, all of which had slightly different
characteristics.

Merge those into a single implementation to keep the behaviour
consistent, and maybe to make that implementation more configurable in
the future.
2022-08-23 19:00:04 +01:00
ForLoveOfCats
99c0b895fe Shell: Highlight commands with a hyperlink to open their help pages 2022-04-21 09:12:37 +04:30
Ali Mohammad Pur
4ea9ca06b4 Shell: Allow completions to request further action from the shell
The shell now expects a JSON object of the form {"kind":<kind>,...} per
line in the standard output of the completion process, where 'kind' is
one of:
- "plain": Just a plain suggestion.
- "program": Prompts the shell to complete a program name starting with
  the given "name".
- "proxy": Prompts the shell to act as if a completion for "argv" was
  requested.
- "path": Prompts the shell to complete a path given the "base" and
  "part" (same as completing part in cwd=base).
2022-04-18 19:53:10 +04:30
Humberto Alves
5506932788 Shell: Refresh PATH cache after running shellrc files
This fixes the highlight of runnable commands, whenever PATH variable
is changed in one of the shellrc files.
2022-04-03 23:28:01 +02:00
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
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
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
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
0ea775f257 Shell: Allow completing StringLiterals as paths
This auto-escapes the token as well :^)
2022-03-06 13:20:41 +01:00
Daniel Bertalan
5b64abe76e Shell: Use StringView instead of String const& where feasible 2022-01-29 23:08:27 +01:00
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
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
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
Andreas Kling
8b1108e485 Everywhere: Pass AK::StringView by value 2021-11-11 01:27:46 +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
Ali Mohammad Pur
97e97bccab Everywhere: Make ByteBuffer::{create_*,copy}() OOM-safe 2021-09-06 01:53:26 +02:00
TheFightingCatfish
72e661b542 Shell: Add unalias builtin
Add shell unalias builtin to remove aliases
2021-07-13 11:57:11 +04:30
Ali Mohammad Pur
b2ef18d538 LibLine+Shell: Allow some programs to modify the current termios
This setting can be controlled by setting the
PROGRAMS_ALLOWED_TO_MODIFY_DEFAULT_TERMIOS _local_ shell variable to a
list containing such programs.
2021-05-24 23:26:49 +04:30