Commit graph

350 commits

Author SHA1 Message Date
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
Andreas Kling 6ad427993a Everywhere: Behaviour => Behavior 2021-09-07 13:53:14 +02:00
Ali Mohammad Pur 97e97bccab Everywhere: Make ByteBuffer::{create_*,copy}() OOM-safe 2021-09-06 01:53:26 +02:00
Daniel Bertalan d7b6cc6421 Everywhere: Prevent risky implicit casts of (Nonnull)RefPtr
Our existing implementation did not check the element type of the other
pointer in the constructors and move assignment operators. This meant
that some operations that would require explicit casting on raw pointers
were done implicitly, such as:
- downcasting a base class to a derived class (e.g. `Kernel::Inode` =>
  `Kernel::ProcFSDirectoryInode` in Kernel/ProcFS.cpp),
- casting to an unrelated type (e.g. `Promise<bool>` => `Promise<Empty>`
  in LibIMAP/Client.cpp)

This, of course, allows gross violations of the type system, and makes
the need to type-check less obvious before downcasting. Luckily, while
adding the `static_ptr_cast`s, only two truly incorrect usages were
found; in the other instances, our casts just needed to be made
explicit.
2021-09-03 23:20:23 +02:00
Andreas Kling eaf88cc78a AK: Rename create<T> => make_ref_counted<T>
And also try_create<T> => try_make_ref_counted<T>.

A global "create" was a bit much. The new name matches make<T> better,
which we've used for making single-owner objects since forever.
2021-09-03 02:36:09 +02:00
sin-ack 8ea22121ac Userland: Migrate to argument-less deferred_invoke
Only one place used this argument and it was to hold on to a strong ref
for the object. Since we already do that now, there's no need to keep
this argument around since this can be easily captured.

This commit contains no changes.
2021-09-02 03:47:47 +04:30
Tobias Christiansen 32b4470ea3 Shell: Use new Statistics tool in 'time -n' command
The new Statistics utility is now used when calling 'time -n' to get
some more information of the timings. For now only the standard
deviation is given in addition to the average.

This commit completely undos #9645 because everything that touched moved
into AK::Statistics.
2021-08-31 16:38:22 +02:00
Andreas Kling 47420e72b8 Shell: Fix a TOCTOU in popd by simplifying it
This builtin was doing a lot of redundant work, including doing a stat()
followed by a chdir(), when just a chdir() would suffice.

SonarCloud: https://sonarcloud.io/project/issues?id=SerenityOS_serenity&issues=AXuVPAHNk92xXUF3qTNb&open=AXuVPAHNk92xXUF3qTNb
2021-08-30 18:35:36 +02:00
Ali Mohammad Pur 74c3359bed Shell: Use a relative path in builtin_cd for chdir if possible
This kinda sorta addresses the Shell side of #9655, however the fact
that `chdir` (and most other syscalls that take paths) are artifically
limited to a length of PATH_MAX remains.
2021-08-28 20:02:27 +02:00
Musab Kılıç 3edeb9b7e7 Shell: Use variable instead of iteration_times.size() in builtin_time 2021-08-27 23:16:53 +02:00
Musab Kılıç 29a9be6503 Shell: Add iteration_times.ensure_capacity() in builtin_time 2021-08-27 23:16:53 +02:00
Ralf Donau 19aeb71d15 Shell: Use String::join for the command in timing report 2021-08-23 22:46:41 +02:00
Valtteri Koskivuori b28b4c643e Shell: Avoid a needless loop in builtin_time()
Not performance sensitive, but perhaps a bit neater? :^)
2021-08-23 22:44:32 +02:00
Andreas Kling b9ab7a5095 Shell: Support time -n <iterations>
You can now specify a number of iterations when timing a command.
The default value is 1 and behaves exactly as before.

If the iteration count is greater than 1, the command will be executed
that many times, and then you get a little timing report afterwards with
the average runtime per iteration, and also the average runtime
excluding the very first iteration. (Excluding the first iteration is
useful when it's slowed down by cold caches, etc.)

This is something I've been doing manually forever (running `time foo`
and then eyeballing the results to headmath an average) and this makes
that whole process so much nicer. :^)
2021-08-23 20:15:14 +02:00
Timothy Flynn 02e3633b7f AK: Move FormatParser definition from header to implementation file
This is primarily to be able to remove the GenericLexer include out of
Format.h as well. A subsequent commit will add AK::Result to
GenericLexer, which will cause naming conflicts with other structures
named Result. This can be avoided (for now) by preventing nearly every
file in the system from implicitly including GenericLexer.

Other changes in this commit are to add the GenericLexer include to
files where it is missing.
2021-08-19 23:49:25 +02:00
sin-ack 4c6a97e757 Shell: Make caller specify the string parsing end condition
Heredocs have a different parse end condition than double-quoted
strings. parse_doublequoted_string_inner would assume that a string
would always end in a double quote, so let's generalize it to
parse_string_inner and have it take a StringEndCondition enum which
specifies how the string terminates.
2021-08-13 01:20:35 +04:30
sin-ack c419b1ade6 Shell: Remove dbgln related to process group IDs
This is insignificant debugging information and will print out during
runs with Lagom.
2021-08-12 22:42:50 +02:00
Jean-Baptiste Boric 2084289162 Userland: Fix PATH environment variable ordering 2021-08-12 18:56:30 +02:00
Gunnar Beutner 07cc7eed29 Shell: Make sure TTY echo is enabled when running external commands
When running external commands via "Shell -c" LibLine turns of TTY echo
before running the command. This ensures that it is turned on.
2021-08-04 03:14:59 +04:30
TheFightingCatfish f67c2c97b1 Shell: Improve the parsing of history event designators 2021-08-02 02:58:55 +04:30
Ali Mohammad Pur 124ca30d49 Shell: Don't assume that only the current shell may continue children
That can happen because of anyone sending the process a SIGCONT.
Fixes an issue where continuing a process launched by the shell from
the System Monitor would cause the shell to spin on waitpid().
2021-07-17 02:00:24 +04:30
Gunnar Beutner c7265ee6bd Assistant: Keep the Terminal window open after the command has run 2021-07-16 13:05:55 +02:00
TheFightingCatfish 72e661b542 Shell: Add unalias builtin
Add shell unalias builtin to remove aliases
2021-07-13 11:57:11 +04:30
Daniel Bertalan e32692ea68 Shell: Use correct printf format string for size_t 2021-07-08 10:11:00 +02:00
Gunnar Beutner 3bbe86d8ea Everywhere: Prefer using "..."sv over StringView { "..." } 2021-07-04 14:24:03 +02:00
Max Wipfli 9b8f35259c AK: Remove the LexicalPath::is_valid() API
Since this is always set to true on the non-default constructor and
subsequently never modified, it is somewhat pointless. Furthermore,
there are arguably no invalid relative paths.
2021-06-30 11:13:54 +02:00
Daniel Bertalan 65b2d3add3 Shell: Don't do null check on NonnullRefPtr<T>
This will cause a problem when `NonnullRefPtr<T>::operator T*` will be
declared as RETURNS_NONNULL. Clang emits a warning for this pointless
null check, which breaks CI.
2021-06-29 22:57:52 +04:30
Daniel Bertalan f820917a76 Everywhere: Use nothrow new with adopt_{ref,own}_if_nonnull
This commit converts naked `new`s to `AK::try_make` and `AK::try_create`
wherever possible. If the called constructor is private, this can not be
done, so we instead now use the standard-defined and compiler-agnostic
`new (nothrow)`.
2021-06-24 17:35:49 +04:30
coderdreams 6bc7f2204e Tests: Run each test in their respective directories
This is so they can find their associated resources and it's
the same behavior as in Lagom.

This also required changing some tests so that they could
write their resources in a writable location.
2021-06-22 18:54:40 +04:30
Gunnar Beutner 631d36fd98 Everywhere: Add component declarations
This adds component declarations so that users can select to not build
certain parts of the OS.
2021-06-17 11:03:51 +02:00
Brian Gianforcaro 8063ca881d Shell: Remove unused InlineLinkedList header include 2021-06-16 10:40:01 +02:00
Gunnar Beutner d476144565 Userland: Allow building SerenityOS with -funsigned-char
Some of the code assumed that chars were always signed while that is
not the case on ARM hosts.

Also, some of the code tried to use EOF (-1) in a way similar to what
fgetc() does, however instead of storing the characters in an int
variable a char was used.

While this seemed to work it also meant that character 0xFF would be
incorrectly seen as an end-of-file.

Careful reading of fgetc() reveals that fgetc() stores character
data in an int where valid characters are in the range of 0-255 and
the EOF value is explicitly outside of that range (usually -1).
2021-06-13 18:52:58 +02:00
Andreas Kling dc65f54c06 AK: Rename Vector::append(Vector) => Vector::extend(Vector)
Let's make it a bit more clear when we're appending the elements from
one vector to the end of another vector.
2021-06-12 13:24:45 +02:00
Jelle Raaijmakers 00fc0a6cf0 Shell: Make time stop parsing options on first non-option 2021-06-08 11:30:58 +02:00
Jelle Raaijmakers 250f8eccf3 LibCore: Support fine-grained failure behavior for ArgsParser 2021-06-08 11:30:58 +02:00
Ali Mohammad Pur 71b4433b0d LibWeb+LibSyntax: Implement nested syntax highlighters
And use them to highlight javascript in HTML source.
This commit also changes how TextDocumentSpan::data is interpreted,
as it used to be an opaque pointer, but everyone stuffed an enum value
inside it, which made the values not unique to each highlighter;
that field is now a u64 serial id.
The syntax highlighters don't need to change their ways of stuffing
token types into that field, but a highlighter that calls another
nested highlighter needs to register the nested types for use with
token pairs.
2021-06-07 14:45:49 +04:30
Max Wipfli 261f233060 Shell: Fix off-by-one error in SyntaxHighlighter
This changes the Shell syntax highlighter to conform to the now-fixed
rendering of syntax highlighting spans in GUI::TextEditor.

This also adds some debug output to make debugging easier.
2021-06-05 00:32:28 +04:30
Max Wipfli bc8d16ad28 Everywhere: Replace ctype.h to avoid narrowing conversions
This replaces ctype.h with CharacterType.h everywhere I could find
issues with narrowing conversions. While using it will probably make
sense almost everywhere in the future, the most critical places should
have been addressed.
2021-06-03 13:31:46 +02:00
Ali Mohammad Pur 944855ca18 AK+Everywhere: Fix compiletime format parsing of replacement fields 2021-06-01 23:12:17 +04:30
Andreas Kling 12a42edd13 Everywhere: codepoint => code point 2021-06-01 10:01:11 +02:00
Max Wipfli 628c7f094f LibGUI+Shell+bt+ls: Use proper APIs for creating file URLs
This patch replaces ad-hoc generation of file URL strings with using
URL::create_with_file_scheme().
2021-06-01 09:28:05 +02:00
Ben Wiederhake a7c265f341 Everywhere: Sort out superfluous QuickSort.h imports
They were sorta unneeded. :^)
2021-05-29 23:41:54 +01:00
Andrew Kaster fae7c436e6 Shell: Disable the valid test as it has a high failure rate on target
Tracked by #7336
2021-05-27 15:18:03 +02:00
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
Andreas Kling de395a3df2 AK+Everywhere: Consolidate String::index_of() and String::find()
We had two functions for doing mostly the same thing. Combine both
of them into String::find() and use that everywhere.

Also add some tests to cover basic behavior.
2021-05-24 11:59:18 +02:00
Andrew Kaster c6eff55439 Shell: Make sure all tests put their temp dirs in /tmp
Follow-on to #7337. Been seeing other CI test failures that point to
these temp directories, so let's just move all of them to /tmp. I'm sure
someone will write ext2fs stress tests later :^)

Example:

/usr/Tests/Shell/control-structure-as-command.sh
  Core::Socket: Failed to connect() to /tmp/portal/inspectables: ...
  + rm -rf shell-test 2>/dev/null
  + mkdir shell-test
  Error: The action has timed out.
2021-05-22 00:24:27 +04:30
Andrew Kaster 413d98fb45 Shell: Use /tmp for all file operations valid.sh test
Not doing so sometimes intermittently caused the '*' glob expansion test
to fail and lock up the shell.
2021-05-21 13:30:45 +04:30
Andrew Kaster a5889b9aad Shell: Hide job times behind SHELL_JOB_DEBUG flag 2021-05-21 12:05:34 +04:30
Ali Mohammad Pur fdfa5c0bc7 Shell: Avoid moving AK::Function instances while inside them 2021-05-19 21:36:57 +02:00
Andreas Kling 31d4bcf5bf Userland: Tighten a *lot* of pledges! :^)
Since applications using Core::EventLoop no longer need to create a
socket in /tmp/rpc/, and also don't need to listen for incoming
connections on this socket, we can remove a whole bunch of pledges!
2021-05-13 23:28:40 +02:00
sin-ack f248145e64 Shell: Fix incorrect fcntl usage
FD_CLOEXEC is a file descriptor flag, so one must use F_{G,S}ETFD
instead.
2021-05-13 19:18:22 +01:00
Ali Mohammad Pur a91a49337c LibCore+Everywhere: Move OpenMode out of IODevice
...and make it an enum class so people don't omit "OpenMode".
2021-05-12 11:00:45 +01:00
sin-ack ecbe17fb11 LibLine+Shell: Add dirty history flag and use it
This patch adds a new flag called history_dirty to Line::Editor that is
set when history is added to but written.  Applications can leverage
this flag to write history only when it changes.  This patch adds an
example usage of this functionality to Shell, which will now only save
the history when it is dirty.
2021-05-11 17:26:30 +01:00
Ali Mohammad Pur a527256356 Shell: Add an option to autosave history every N ms
...and set it to 10 seconds by default.
2021-05-11 10:19:07 +01:00
Ali Mohammad Pur b1fe5d5517 Shell: Parse '\t' in doublequoted strings as a tab character
This not being recognised is surprising.
2021-05-10 10:43:23 +02:00
Ali Mohammad Pur 417910fd28 Shell: Make escaping more intelligent
Instead of the previous only-escape-with-backslashes, extend the
escaping to one of:
- No escape
- Escape with backslash
- Escape with "\xhh" if control character that isn't easily represented
  as \X
- Escape with "\uhhhhhhhh" if unicode character that is too big to
  represent as "\xhh".

Fixes #6986.
2021-05-10 10:43:23 +02:00
Ali Mohammad Pur 22b244df45 Shell: Add support for \uhhhhhhhh escapes in strings
This will be replaced with the unicode character whose codepoint is
given by the unsigned 32-bit number 'hhhhhhhh' (hex).
2021-05-10 10:43:23 +02:00
Andreas Kling 8c3b603da3 Shell: Convert StringBuilder::appendf() => AK::Format 2021-05-07 21:12:09 +02:00
Ali Mohammad Pur b7fb12338c Shell: Only match entries from PATH when a program name is given
This commit makes the shell:
- highlight executables in the current directory as invalid, unless an
  explicit `./' is given (so, `./foo` isn't red, but `foo` is)
- not suggest executables in the current directory unless explicitly
  requested (by prepending `./`)
- not attempt to run an executable in the current directory that has
  been invoked as a program name and failed execvp().
  Note that `./foo` is still executed because it's not invoked as
  a name, but rather as a path.

Fixes the other half of #6774.
2021-05-02 19:46:33 +02:00
Ali Mohammad Pur bda69a5f59 Shell: Replace fprintf(stderr) => warnln() 2021-05-02 19:46:33 +02:00
Ali Mohammad Pur 6a9dced790 Shell: Update shebang handling logic
This bit of code was kept unmodified since it was first implemented,
and I'm not entirely convinced that it ever actually worked :P
This commit updates the code to use "modern" classes and constructs,
and fixes an issue where the shebang would still contain the '#!'
when it was passed to execvp().
Fixes #6774.
2021-05-02 19:46:33 +02:00
Ali Mohammad Pur 70728b49e2 Shell: Move the heredocs vector to a local value before processing it
Otherwise we would end up trying to parse the same heredoc entry, if it
contained a sequence terminated by a newline.
e.g. `<<-x\n$({` would attempt to read a heredoc entry after `x`, and
then after `{` while inside the first heredoc entry.
To make this work, we can simply empty the instance vector and keep the
state on the stack.
Issue found through oss-fuzz:
https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=33852
2021-05-01 15:58:31 +02:00
Ali Mohammad Pur 323a408d8c Shell: Make set_is_syntax_error() also copy the error location 2021-05-01 15:58:31 +02:00
Ali Mohammad Pur f1d49d391e Shell: Disallow non-bareword nodes as part of a heredoc key
Found by oss-fuzz:
https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=33854
2021-05-01 15:58:31 +02:00
Ali Mohammad Pur 4c40151160 Shell: Implement formatting for Heredocs 2021-04-29 20:25:56 +02:00
Ali Mohammad Pur d70f25bbe5 Shell: Add some tests for heredocs 2021-04-29 20:25:56 +02:00
Ali Mohammad Pur 3048274f5e Shell: Add support for heredocs
Closes #4283.
Heredocs are implemented in a way that makes them feel more like a
string (and not a weird redirection, a la bash).
There are two tunables, whether the string is dedented (`<<-` vs `<<~`)
and whether it allows interpolation (quoted key vs not).
To the familiar people, this is how Ruby handles them, and I feel is the
most elegant heredoc syntax.
Unlike the oddjob that is bash, heredocs are treated exactly as normal
strings, and can be used _anywhere_ where a string can be used.
They are *required* to appear in the same order as used after a newline
is seen when parsing the sequence that the heredoc is used in.
For instance:
```sh
echo <<-doc1 <<-doc2 | blah blah
contents for doc1
doc1
contents for doc2
doc2
```
The typical nice errors are also implemented :^)
2021-04-29 20:25:56 +02:00
Ali Mohammad Pur 7c8d39e002 Shell: Do not assume that all parts of a node are enclosed in the node
For instance, heredocs are made of two parts, and the second part is not
within the bounds of the first one.
2021-04-29 20:25:56 +02:00
Ali Mohammad Pur cf4935e806 Shell: Avoid position push/pop when checking for next_is()
This operation is not a rule and cannot produce nodes.
2021-04-29 20:25:56 +02:00
Ali Mohammad Pur 0d742557c6 Shell: Allow Syntax errors to be mutated while parsing
Some nodes (such as heredocs) cannot be validated immediately, so the
entire tree will need to be revalidated if we don't allow mutating
syntax errors.
2021-04-29 20:25:56 +02:00
Linus Groh 649d2faeab Everywhere: Use "the SerenityOS developers." in copyright headers
We had some inconsistencies before:

- Sometimes "The", sometimes "the"
- Sometimes trailing ".", sometimes no trailing "."

I picked the most common one (lowecase "the", trailing ".") and applied
it to all copyright headers.

By using the exact same string everywhere we can ensure nothing gets
missed during a global search (and replace), and that these
inconsistencies are not spread any further (as copyright headers are
commonly copied to new files).
2021-04-29 00:59:26 +02:00
Linus Groh dbe72fd962 Everywhere: Remove empty line after function body opening curly brace 2021-04-25 20:20:00 +02:00
Ali Mohammad Pur 0d2602c900 Shell: Add a 'kill' builtin that wraps the system's own
Fixes #6578.
2021-04-23 20:27:58 +02:00
Ali Mohammad Pur 95055d3a38 Shell: Add support for jobspecs in fg/bg/disown/wait 2021-04-23 20:27:58 +02:00
Andreas Kling b91c49364d AK: Rename adopt() to adopt_ref()
This makes it more symmetrical with adopt_own() (which is used to
create a NonnullOwnPtr from the result of a naked new.)
2021-04-23 16:46:57 +02:00
Gunnar Beutner 1e5a7ca0a7 Shell: Fix how cd handles the path argument
Previously this didn't work:

  $ cd -- /usr
  Invalid path '--'

This path fixes this issue and removes the unnecessary else
branch because we're already using realpath() later on to resolve
relative paths.
2021-04-23 11:33:57 +02:00
Brian Gianforcaro 1682f0b760 Everything: Move to SPDX license identifiers in all files.
SPDX License Identifiers are a more compact / standardized
way of representing file license information.

See: https://spdx.dev/resources/use/#identifiers

This was done with the `ambr` search and replace tool.

 ambr --no-parent-ignore --key-from-file --rep-from-file key.txt rep.txt *
2021-04-22 11:22:27 +02:00
Andreas Kling b41b6dd279 Shell: Convert String::format() => String::formatted() 2021-04-21 23:49:01 +02:00
Gunnar Beutner 111ac4b1f4 Shell: Auto-completion shouldn't suggest non-executable files for the program name 2021-04-20 17:02:10 +02:00
Ali Mohammad Pur efb14e95c4 Shell: Don't whine about tcsetpgrp() failing
We intentionally skimp out on checking isatty() before them to cut down
on syscalls, so we should also accept the errors and just let them be.
Closes #6471.
2021-04-19 16:28:33 +02:00
Ali Mohammad Pur 55914841b7 Shell/Tests: Replace 'type f -f' with 'type -f f'
The order of arguments seemed be confusing ArgsParser.
Fixes #6467.
2021-04-19 10:07:58 +02:00
Nicholas-Baron c4ede38542 Everything: Add -Wnon-virtual-dtor flag
This flag warns on classes which have `virtual` functions but do not
have a `virtual` destructor.

This patch adds both the flag and missing destructors. The access level
of the destructors was determined by a two rules of thumb:
1. A destructor should have a similar or lower access level to that of a
   constructor.
2. Having a `private` destructor implicitly deletes the default
   constructor, which is probably undesirable for "interface" types
   (classes with only virtual functions and no data).

In short, most of the added destructors are `protected`, unless the
compiler complained about access.
2021-04-15 20:57:13 +02:00
jacob gw 42e63d0a94 Shell: flush stderr and stdout when using builtins
This fixed some tests that were failing.
Thanks @alimpfard for this fix!
2021-04-13 00:02:46 +02:00
jacob gw cb22a6642d Shell: add type builtin 2021-04-13 00:02:46 +02:00
AnotherTest 18b3334738 Shell: Allow newlines between the function decl and its body
All other control structures are fine with this, so let's keep the
behaviour consistent.
2021-04-08 10:46:39 +02:00
AnotherTest c4cf4ef111 Shell: Place Pipe redirections at the beginning of the redirection list
This makes commands like `foo 2>&1 | bar` behave as expected (which is
to pipe both stdout and stderr of `foo` to stdin of `bar`).
Previously, this would've piped stderr of `foo` into stdout, and the
stdout of `foo` into the stdin of `bar`.
2021-03-31 23:49:26 +02:00
AnotherTest 5d19509616 Shell: Handle SIGCHLD after sending SIGCONT to job
This fixes `fg` and `bg` causing the shell to go into an infinite loop
of trying to `waitpid` until some current job changes state.

a.k.a. "Fix Shell backgrounding, yet again!" :P
2021-03-31 23:49:26 +02:00
AnotherTest 50f5959996 Shell: Use existing job state when waitpid() returns 0 in jobs 2021-03-31 23:49:26 +02:00
AnotherTest 47080941cc Shell: Replace '#if SH_DEBUG` with dbgln_if() and if constexpr 2021-03-31 23:49:26 +02:00
AnotherTest 2efa17184d Shell: Add tests for slices 2021-03-22 13:15:08 +01:00
AnotherTest 3b8fa5a753 Shell: Add support for indexing into variables
Now a variable may have an optional slice (only _one_ slice), which can
also use negative indices to index from the end.
This works on both lists and strings.
The contents of the slice have the same semantics as brace expansions.
For example:
```sh
$ x=(1 2 3 4 5 6)
$ echo $x[1..3] # select indices 1, 2, 3
2 3 4
$ echo $x[3,4,1,0] # select indices 3, 4, 1, 0 (in that order)
4 5 2 1
$ x="Well Hello Friends!"
$ echo $x[5..9]
Hello
```
2021-03-22 13:15:08 +01:00
Andreas Kling f59ad2dc57 Everywhere: Remove pessimizing and redundant move() 2021-03-17 16:30:15 +01:00
AnotherTest ef3679f9c3 Shell: Avoid unnecessarily taking control of the standard streams
As of a0506cb39e, this is no longer
needed to write to stdout.
Fixes #5776.
2021-03-16 09:41:37 +01:00
AnotherTest 125be2923c Shell: Consume the username when parsing '~user'
Otherwise it will stay there and be parsed as a juxtaposition.
Fixes #5798.
2021-03-15 09:06:21 +01:00
Andreas Kling ef1e5db1d0 Everywhere: Remove klog(), dbg() and purge all LogStream usage :^)
Good-bye LogStream. Long live AK::Format!
2021-03-12 17:29:37 +01:00
AnotherTest fb68aa1480 Shell: Don't blindly dereference the result of Parser::parse()
It _may_ return nullptr if there's nothing to return.
Fixes #5691.
2021-03-08 09:28:15 +01:00
AnotherTest 13b65b632a Shell: Add support for enumerating lists in for loops
With some odd syntax to boot:
```sh
$ for index i x in $whatever {}
```
2021-03-07 10:59:51 +01:00
AnotherTest a45b2ea6fb Shell: Add support for 'immediate' expressions as variable substitutions
This commit adds a few basic variable substitution operations:
- length
    Find the length of a string or a list
- length_across
    Find the lengths of things inside a list
- remove_{suffix,prefix}
    Remove a suffix or a prefix from all the passed values
- regex_replace
    Replace all matches of a given regex with a given template
- split
    Split the given string with the given delimiter (or to its
    code points if the delimiter is empty)
- concat_lists
    concatenates any given lists into one

Closes #4316 (the ancient version of this same feature)
2021-03-07 10:59:51 +01:00
AnotherTest a303b69caa Shell: Do not parse history events in scripts
That makes no sense!
2021-03-07 10:59:51 +01:00
AnotherTest c580348ebd Shell: Add functions to the PATH cache when rebuilding the cache
Otherwise functions would be highlighted as missing.
2021-03-07 10:58:42 +01:00
AnotherTest 4f6bf2931c Shell: Make the 'not' builtin return the correct exit code for functions 2021-03-07 10:58:42 +01:00
AnotherTest fec8d7d699 Shell: Corrently indent offset newlines when formatting
Previously, formatting the following would incorrectly skip the indents:
```
{
    foo

    bar
}
```
to create:
```
{

foo

bar
}
```
2021-03-07 10:58:42 +01:00
AnotherTest e1512d5968 Shell: Skip caching PATH and history load/save when not interactive
Non-interactive shells (i.e. when running scripts) do not need this
functionality, so they are a boatload of wasted time.
This significantly reduces the script startup and shutdown times when
there are lots of executables in PATH or lots of entries in the history.
2021-03-07 10:58:42 +01:00
AnotherTest 2089b24ab5 Shell: Adjust syntax highlighting logic for the new TextPosition logic 2021-03-04 11:21:43 +01:00
AnotherTest afc1097023 Shell: Make Node::hit_test_position() const 2021-03-04 11:21:43 +01:00
Andrew Kaster 2930014c2a Shell: Remove WAITID_ONCE workaround
This hashmap is no longer necessary, and the shell works just fine
without it now. Remove the conditionally compiled code.
2021-02-28 18:19:37 +01:00
Andrew Kaster 8fc862f710 Userland: Install shell tests on Serenity
Also make them runnable anywhere. Previously they required $PWD to be
the directory containing test-commons.inc, and for $PWD to be writable.
2021-02-28 18:19:37 +01:00
Linus Groh e265054c12 Everywhere: Remove a bunch of redundant 'AK::' namespace prefixes
This is basically just for consistency, it's quite strange to see
multiple AK container types next to each other, some with and some
without the namespace prefix - we're 'using AK::Foo;' a lot and should
leverage that. :^)
2021-02-26 16:59:56 +01:00
Andreas Kling 5d180d1f99 Everywhere: Rename ASSERT => VERIFY
(...and ASSERT_NOT_REACHED => VERIFY_NOT_REACHED)

Since all of these checks are done in release builds as well,
let's rename them to VERIFY to prevent confusion, as everyone is
used to assertions being compiled out in release.

We can introduce a new ASSERT macro that is specifically for debug
checks, but I'm doing this wholesale conversion first since we've
accumulated thousands of these already, and it's not immediately
obvious which ones are suitable for ASSERT.
2021-02-23 20:56:54 +01:00
AnotherTest 347d741afb AK+Userland: Extend the compiletime format string check to other functions
Thanks to @trflynn89 for the neat implicit consteval ctor trick!
This allows us to basically slap `CheckedFormatString` on any
formatting function, and have its format argument checked at compiletime.

Note that there is a validator bug where it doesn't parse inner replaced
fields like `{:~>{}}` correctly (what should be 'left align with next
argument as size' is parsed as `{:~>{` following a literal closing
brace), so the compiletime checks are disabled on these temporarily by
forcing them to be StringViews.

This commit also removes the now unused `AK::StringLiteral` type (which
was introduced for use with NTTP strings).
2021-02-23 13:59:33 +01:00
Andreas Kling d9bf4b4d41 Shell: Convert dbgprintf() => dbgln() 2021-02-17 16:18:53 +01:00
Andreas Kling a50ba0a491 LibSyntax: Make rehighlight() take Gfx::Palette as by const-reference 2021-02-11 23:52:39 +01:00
Andreas Kling 50308f6fda Shell: Move Shell syntax highlighter LibShell 2021-02-07 17:07:33 +01:00
AnotherTest 5204c9062c Shell: Make history index values not fitting in i32 a syntax error
A continuation of e3ec759.
Also found by oss-fuzz:
https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=30405
2021-02-07 13:12:56 +01:00
AnotherTest e3ec759f97 Shell: Make history range values larger than u32 a syntax error
Found by oss-fuzz:
https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=29792&sort=reported&q=serenity
2021-02-03 10:14:32 +01:00
AnotherTest 37bfaf4e7c Shell: Do not bail on signaled if condition
Fixes #5119
2021-01-26 21:19:22 +01:00
asynts eea72b9b5c Everywhere: Hook up remaining debug macros to Debug.h. 2021-01-25 09:47:36 +01:00
asynts acdcf59a33 Everywhere: Remove unnecessary debug comments.
It would be tempting to uncomment these statements, but that won't work
with the new changes.

This was done with the following commands:

    find . \( -name '*.cpp' -o -name '*.h' -o -name '*.in' \) -not -path './Toolchain/*' -not -path './Build/*' -exec awk -i inplace '$0 !~ /\/\/#define/ { if (!toggle) { print; } else { toggle = !toggle } } ; $0 ~/\/\/#define/ { toggle = 1 }' {} \;

    find . \( -name '*.cpp' -o -name '*.h' -o -name '*.in' \) -not -path './Toolchain/*' -not -path './Build/*' -exec awk -i inplace '$0 !~ /\/\/ #define/ { if (!toggle) { print; } else { toggle = !toggle } } ; $0 ~/\/\/ #define/ { toggle = 1 }' {} \;
2021-01-25 09:47:36 +01:00
AnotherTest 4981b30ea5 Shell: Run the command's next chain even when it's empty 2021-01-23 08:28:58 +01:00
AnotherTest be94a4ffa6 Shell: Block on the existing event loop instead of pushing a new one
This patch makes `Shell::block_on_job()` pump the event loop while the
job it's waiting for hasn't finished.
As this no longer pushes new event loops, it has the effect of
flattening the stack as well.
Fixes #4976.
2021-01-23 08:28:58 +01:00
AnotherTest 2bd77bc93b Shell: Make the parser read consecutive sequences without recursing
This fixes (the easy) part of #4976.
2021-01-23 08:28:58 +01:00
asynts fb8d3635d9 Everywhere: Replace a bundle of dbg with dbgln.
These changes are arbitrarily divided into multiple commits to make it
easier to find potentially introduced bugs with git bisect.
2021-01-22 22:14:30 +01:00
Linus Groh 421587c15c Everywhere: Fix typos 2021-01-22 18:41:29 +01:00
AnotherTest cb29dca029 Shell: Don't spam perror() on kill_job()
That function is allowed to be given a dead job, so don't bother with
perror()
2021-01-19 08:19:43 +01:00
AnotherTest 86f50aa74e Shell: Make tests use PASS/FAIL instead of exit codes
There's no guarantee that the last executed command will have a zero
exit code, and so the shell exit code may or may not be zero, even if
all the tests pass.
Also changes the `test || echo fail && exit` to
`if not test { echo fail && exit }`, since that's nicer-looking.
2021-01-19 08:19:43 +01:00
AnotherTest 5ec139e728 Shell: Make 'if' expressions return the unevaluated value of blocks
This makes it possible to actually put them in a sequence and cast them
to commands.
2021-01-19 08:19:43 +01:00
AnotherTest 50473003be Shell: Add a not builtin
`not` just takes a command, runs it, then negates its exit code (0->1,
non-zero->0).
2021-01-19 08:19:43 +01:00
AnotherTest 2843526513 Shell: Mark control structures to be executed in the current process 2021-01-19 08:19:43 +01:00
AnotherTest 1c78d12f1c Shell: Implement for_each_entry() for syntactic list nodes
This allows correct iteration over nested lists.
Also store values to variables without resolving them, to delay the
resolution step as much as possible (this helps with storing nested
lists in variables).
2021-01-19 08:19:43 +01:00
AnotherTest fc7a06af9d Shell: Consider numbers as word characters too
Otherwise `foobar2` wouldn't be a valid identifier
2021-01-19 08:19:43 +01:00
AnotherTest 8cfda86a45 Shell: Actually return the exit code of the file when running a file 2021-01-19 08:19:43 +01:00
AnotherTest cd6e5c064b Shell: Add a builtin that parses its sole argument and dumps its AST
Pretty useful for debugging.
2021-01-19 08:19:43 +01:00
AnotherTest 5b79d0d1a3 Shell: Allow newlines between else and if's closing brace
This is more flexible and intuitive. Fixes #4992.
2021-01-18 08:56:34 +01:00
Nick Vella fdc9b3c5a0 Shell: use exit code 127 on command not found 2021-01-16 09:05:01 +01:00
Nick Vella 1d11b62ca4 Shell: return the exit code of a '-c' command execution 2021-01-16 09:05:01 +01:00
AnotherTest c1b4e86004 Shell: Add formatter for history events 2021-01-15 19:13:03 +01:00
AnotherTest 239472ba69 Shell: Add (basic) support for history event designators
Closes #4888
2021-01-15 19:13:03 +01:00
Andreas Kling 1a08ac72ad LibC+Everywhere: Remove open_with_path_length() in favor of open()
This API was a mostly gratuitous deviation from POSIX that gave up some
portability in exchange for avoiding the occasional strlen().

I don't think that was actually achieving anything valuable, so let's
just chill out and have the same open() API as everyone else. :^)
2021-01-12 23:34:01 +01:00
AnotherTest a90905c54c Shell: Use lstat instead of access to check if glob target exists
Fixes #4905
2021-01-12 16:23:26 +01:00
Andreas Kling c4e2fd8123 Shell: Move to Userland/Shell/ 2021-01-12 12:04:07 +01:00