Commit Graph

361 Commits

Author SHA1 Message Date
Ben Wiederhake
f20d04726a LibFileSystem+Everything: Remove resolve_executable_from_environment 2023-06-06 23:46:36 +02:00
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
Ben Wiederhake
365b2bf945 Shell: Prefer File and FileSystem over DeprecatedFile 2023-05-30 00:21:59 +02:00
Ben Wiederhake
0c86ee3876 Shell: Rewrite Shell::possibly_print_error without deprecated API
Also, not allocating copies of the current string all the time should be
a tiny bit more efficient. We can do this because the 'current_line' is
only used while reporting the error, so it will not be used once we
begin reading the next line.
2023-05-30 00:21:59 +02:00
Ben Wiederhake
8df8f4d75a Shell: Rewrite Shell::runnable_path_for without deprecated API
Also, not allocating a vector for the parts should be a tiny bit more
efficient.
2023-05-30 00:21:59 +02:00
Undefine
0374351595 Shell: Properly detect the kill executable 2023-05-21 14:59:26 +01:00
Ben Wiederhake
f0ee630ee9 Shell: Prefer File::read_until_eof over DeprecatedFile::read_all 2023-05-19 21:36:37 +02:00
Ben Wiederhake
e77f59b7d3 Shell: Prefer FileSystem over DeprecatedFile 2023-05-15 06:50:43 +02:00
Ben Wiederhake
bc2a517ade Userland: Merge DEFAULT_PATH into LibFileSystem/FileSystem.h 2023-05-13 07:53:13 +02:00
Lucas CHOLLET
8c34959b53 AK: Add the Input word to input-only buffered streams
This concerns both `BufferedSeekable` and `BufferedFile`.
2023-05-09 11:18:46 +02:00
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
367da548ff Shell: Make null_or_alternative actually look up the given variable 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
0d8b90aec6 Shell: Allow newlines before do in POSIX for loops 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
e2336d9de5 Shell: Add support for unset -v in POSIX mode 2023-05-05 09:35:24 +03:30
Ali Mohammad Pur
71441ea932 Shell: Allow lossy conversion from list -> string in POSIX mode
Similar to bash, this operation returns only the first element because
reasons.
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
aecd91aedc Shell: Correctly handle escaped quote characters in strings
Previously "foo\`bar" was treated literally, but \` is a valid escape
character.
This commit makes that string correctly evaluate as "foo`bar".
2023-05-05 09:35:24 +03:30
Ali Mohammad Pur
bbfedf17b7 Shell: Actually execute loop/case bodies when running them 2023-05-05 09:35:24 +03:30
Ali Mohammad Pur
7b031138fc Shell: Correctly handle commands after heredoc contents
Previously we did not emit a newline after the ending heredoc key, which
wreaked havoc on the parser logic, leading to parse errors.
2023-05-05 09:35:24 +03:30
Ali Mohammad Pur
93413f8682 Shell: Use the correct range for named variable expansions
Previously we were starting the variable expansions before the starting
'$' and calculating the wrong length, this commit makes it so we
calculate the right range for them.
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
Andreas Kling
411d36719e LibCore: Simplify Core::Notifier by only allowing one event type
Not a single client of this API actually used the event mask feature to
listen for readability AND writability.

Let's simplify the API and have only one hook: on_activation.
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
Cameron Youell
8134dccdc7 AK: Add new failable JsonArray::{append/set} functions
Move all old usages to the more explicit `JsonArray:must_{append/set}`
2023-04-24 09:21:51 +02:00
Tim Schumacher
8370377832 Shell: Add map_fixed to the pledges for command autocompletion 2023-04-12 07:06:54 -04:00
Ben Wiederhake
560133a0c6 Everywhere: Remove unused DeprecatedString includes 2023-04-09 22:00:54 +02:00
Ali Mohammad Pur
1173adb90a Shell: Don't require ArgsParser values to be null-terminated 2023-03-31 06:55:46 +02:00
Tim Schumacher
d01ac59b82 Shell: Skip rc files when not running interactively
This makes debugging a bit nicer.
2023-03-29 03:39:09 +03:30
Tim Schumacher
9a6b5a53a7 Shell: Correctly mark the end line of a parsed list 2023-03-29 03:39:09 +03:30
Tim Schumacher
46c22ee49d Shell: Allow appending empty string literals
Otherwise, we just silently drop arguments that are empty strings.
2023-03-29 03:39:09 +03:30
Tim Schumacher
c26639eac2 Shell: Properly skip POSIX Fi tokens 2023-03-29 03:39:09 +03:30
Tim Schumacher
b1739029ef Shell: Evaluate the program name before parsing arguments
Otherwise, we are too late to catch the "load the POSIX-compatible
shellrc" branch.
2023-03-29 03:39:09 +03:30
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
d0112d76e9 Shell: Resolve nonexistent string env variables to the empty string
We did this for lists, but not for strings.
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
Cameron Youell
1d24f394c6 Everywhere: Use LibFileSystem where trivial 2023-03-21 19:03:21 +00:00
Sam Atkins
6d8f046fd0 LibGfx+Userland: Make TextAttributes::underline_style optional
Rather than having a style AND a field saying whether to use the style,
just make the style Optional.
2023-03-15 14:55:49 +01:00
Tim Schumacher
ecd1862859 AK: Rename Stream::write_entire_buffer to Stream::write_until_depleted
No functional changes.
2023-03-13 15:16:20 +00:00
Tim Schumacher
a3f73e7d85 AK: Rename Stream::read_entire_buffer to Stream::read_until_filled
No functional changes.
2023-03-13 15:16:20 +00:00
Ali Mohammad Pur
56b5b78d7b Shell: Error out on invalid `export' argument
Previously `export =` would crash the shell, make this an error instead.
2023-03-10 22:33:30 +03:30
Andreas Kling
4797fad91c Shell: Fix bogus C-style casts from NonnullOwnPtr<T>* to T*
Thanks UBSAN for spotting this!
2023-03-07 09:42:20 +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
689ca370d4 Everywhere: Remove NonnullRefPtr.h includes 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
500044906d LibCore+Everywhere: Remove ArgsParser::add*(char const*&)
This is not guaranteed to always work correctly as ArgsParser deals in
StringViews and might have a non-properly-null-terminated string as a
value. As a bonus, using StringView (and DeprecatedString where
necessary) leads to nicer looking code too :^)
2023-03-01 10:47:19 +01:00
Linus Groh
51c3967516 Everywhere: Use '_{short_,}string' literals more
This mostly updates code what was written before but merged after these
were added.
2023-02-28 15:15:36 +00:00
Ali Mohammad Pur
db886fe18b Userland+AK: Stop using getopt() for ArgsParser
This commit moves the implementation of getopt into AK, and converts its
API to understand and use StringView instead of char*.
Everything else is caught in the crossfire of making
Option::accept_value() take a StringView instead of a char const*.

With this, we must now pass a Span<StringView> to ArgsParser::parse(),
applications using LibMain are unaffected, but anything not using that
or taking its own argc/argv has to construct a Vector<StringView> for
this method.
2023-02-28 15:52:24 +03:30
Ali Mohammad Pur
b2b851b361 Shell: Convert the POSIX parser/lexer to ErrorOr 2023-02-28 15:52:24 +03:30
Ali Mohammad Pur
0c28fd41ed Shell: Convert all immediately convertible fallible functions to ErrorOr 2023-02-28 15:52:24 +03:30
Ali Mohammad Pur
e403dbabfa Shell: Convert the remaining fallible AST 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
Ali Mohammad Pur
d575c50f3e Shell: Make AST::dump() ErrorOr-aware 2023-02-28 15:52:24 +03:30
Ali Mohammad Pur
beeb58bd93 Shell+LibCodeComprehension: Start replacing {Deprecated => }String
This starts by switching all AST members to Strings, and dealing with
the fallout.
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
99d264afbe Shell: Correct the out-of-bounds expansion bailing condition
Also adds a couple (useful) debug prints which helped track down the
problem.
2023-02-18 06:55:46 +03:30
Ali Mohammad Pur
d56fbaa7e8 Shell: Allow underscores in normal expansion names 2023-02-18 06:55:46 +03:30
Ali Mohammad Pur
a6d77162f4 Shell: Correctly keep track of special parameter length
We were previously treating special expansions (e.g. $#) as zero-length
expansions, which made the shell repeat them literally after expanding
them.
2023-02-18 06:55:46 +03:30
Ali Mohammad Pur
6da438e992 Shell: Clear expansions after committing a word in the POSIX parser 2023-02-18 06:55:46 +03:30
Ali Mohammad Pur
2881bb4c3a Shell: Add support for heredocs to the POSIX parser 2023-02-18 06:55:46 +03:30
Ali Mohammad Pur
a5e4bc4faf Shell: Add a '--posix' mode to the 'dump' builtin 2023-02-18 06:55:46 +03:30
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
eb20fbe145 Everywhere: Use /bin/Shell as the shebang for Shell scripts 2023-02-13 23:00:15 +03:30
Ali Mohammad Pur
beaae6b420 Shell: Use the POSIX parser if run as /bin/sh or passed the --posix flag
Note that the default shellrc is most likely not valid posix sh code, so
passing --skip-shellrc is suggested until we come up with a separate
shellrc file for the POSIX parser to use when interactive.
2023-02-13 23:00:15 +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
Tim Schumacher
874c7bba28 LibCore: Remove Stream.h 2023-02-13 00:50:07 +00:00
Tim Schumacher
d43a7eae54 LibCore: Rename File to DeprecatedFile
As usual, this removes many unused includes and moves used includes
further down the chain.
2023-02-13 00:50:07 +00:00
Tim Schumacher
093cf428a3 AK: Move memory streams from LibCore 2023-01-29 19:16:44 -07:00
Linus Groh
6e7459322d AK: Remove StringBuilder::build() in favor of to_deprecated_string()
Having an alias function that only wraps another one is silly, and
keeping the more obvious name should flush out more uses of deprecated
strings.
No behavior change.
2023-01-27 20:38:49 +00:00
Sam Atkins
95b1cf2dcd Shell: Replace uses of JsonObject::get_deprecated()/get_ptr() 2023-01-27 08:07:24 -05:00
Sam Atkins
1dd6b7f5b7 AK+Everywhere: Rename JsonObject::get() to ::get_deprecated()
This is a preparatory step to making `get()` return `ErrorOr`.
2023-01-17 19:52:52 -05:00
Tim Schumacher
2150e1b3a5 Shell: Use AllocatingMemoryStream to search for the IFS 2023-01-14 00:33:35 +03:30
Sam Atkins
a8cf0c9371 LibCore+Userland: Make Core::Timer::create_single_shot() return ErrorOr
clang-format sure has some interesting opinions about where to put a
method call that comes after a lambda. :thonk:
2023-01-12 11:25:51 +01:00
Andrew Kaster
a492e2018d Userland: Silence warnings from ElapsedTimer::elapsed() type change
We changed elapsed() to return i64 instead of int as that's what
AK::Time::to_milliseconds() returns, causing a bunch of implicit lossy
conversions in callers. Clean those up with a mix of type changes and
casts.
2023-01-07 14:51:04 +01:00
Ben Wiederhake
b83cb09db1 Everywhere: Fix badly-formatted includes
In 7c5e30daaa, the focus was "only" on
Userland/Libraries/, whereas this commit cleans up the remaining
headers in the repo, and any new badly-formatted include.
2023-01-02 11:06:15 -05:00
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
Ali Mohammad Pur
4311c2164e Shell: Disable interactive mode on '-c'
This also disables the full suite of interactive stuff in LibLine.
2022-12-12 13:57:06 +00:00
Linus Groh
57dc179b1f Everywhere: Rename to_{string => deprecated_string}() where applicable
This will make it easier to support both string types at the same time
while we convert code, and tracking down remaining uses.

One big exception is Value::to_string() in LibJS, where the name is
dictated by the ToString AO.
2022-12-06 08:54:33 +01:00
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
Kyle Lanmon
31290c8527 LibSyntax: Teach each highlighter about it's comment syntax 2022-11-27 18:28:43 -07:00
Ali Mohammad Pur
77cc8c5612 Shell: Re-block SIGTT{IN,OU} on start
This also shows up when trying to read from a 0x0 buffer as it has no
PTY attached, avoid the mess by just blocking them (instead of ignoring
them).
2022-11-03 00:01:42 +03:30
Tim Schumacher
ce2f1b845f Everywhere: Mark dependencies of most targets as PRIVATE
Otherwise, we end up propagating those dependencies into targets that
link against that library, which creates unnecessary link-time
dependencies.

Also included are changes to readd now missing dependencies to tools
that actually need them.
2022-11-01 14:49:09 +00:00
demostanis
3e8b5ac920 AK+Everywhere: Turn bool keep_empty to an enum in split* functions 2022-10-24 23:29:18 +01:00
Andrew Kaster
1ae0cfd08b CMake+Userland: Use CMakeLists from Userland to build Lagom Libraries
Also do this for Shell.

This greatly simplifies the CMakeLists in Lagom, replacing many glob
patterns with a big list of libraries. There are still a few special
libraries that need some help to conform to the pattern, like LibELF and
LibWebView.

It also lets us remove essentially all of the Serenity or Lagom binary
directory detection logic from code generators, as now both projects
directories enter the generator logic from the same place.
2022-10-16 16:36:39 +02:00
Andrew Kaster
539fb08551 Userland: Remove unecessary uses of __serenity__ macro 2022-10-10 12:23:12 +02:00
Andrew Kaster
828441852f Everywhere: Replace uses of __serenity__ with AK_OS_SERENITY
Now that we have OS macros for essentially every supported OS, let's try
to use them everywhere.
2022-10-10 12:23:12 +02:00
networkException
4230dbbb21 AK+Everywhere: Replace "protocol" with "scheme" url helpers
URL had properly named replacements for protocol(), set_protocol() and
create_with_file_protocol() already. This patch removes these function
and updates all call sites to use the functions named according to the
specification.

See https://url.spec.whatwg.org/#concept-url-scheme
2022-09-29 09:39:04 +01:00
Ben Wiederhake
79ec6ed03d Shell: Fix 'Command:' output for built-in 'time' command 2022-09-16 05:38:09 +00:00
Brian Gianforcaro
d0a1775369 Everywhere: Fix a variety of typos
Spelling fixes found by `codespell`.
2022-09-14 04:46:49 +00:00
Ben Wiederhake
0e901f8c68 Shell: Sort CompletionSuggestions for paths lexicographically
This used to be ordered by inode, which can be surprising.
2022-09-12 16:49:48 +01:00
Sam Atkins
edc3ed2a0e AK: Allow creating NonnullPtrVectors from an initializer list
This was present in Vector already. Clang-format fixed some const
positions automatically too.

Also removed a now-ambiguous and unnecessary constructor from Shell.
2022-09-08 18:53:08 +02: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
Tim Schumacher
39a3775f48 Userland: Rely on a single authoritative source for the default PATH 2022-08-23 19:00:04 +01:00
Ali Mohammad Pur
c90cf67dc6 Shell: Avoid StringView OOB access in Formatter ctor 2022-08-11 14:12:55 +00:00