Commit Graph

75 Commits

Author SHA1 Message Date
Johannes Altmanninger
3a9b4149da Replace localtime_r with a 64-bit-time_t wrapper
Part of #10634
2024-08-07 13:11:22 +02:00
Peter Ammon
0651ca0d9b
Unify FileId structs
We had two of these! Just use one.
2024-07-27 18:48:51 -07:00
Peter Ammon
1cbd18cc30
Tweak the allowed clippy set and fix some 2024-06-30 11:38:15 -07:00
Peter Ammon
6b4dbf3b05
Remove additional dead code 2024-06-29 18:03:52 -07:00
Peter Ammon
300fcfdba7
Factor out line counting
This moves the line counting logic from parse_execution into a new type, in
preparation for further refactoring.
2024-06-29 18:03:52 -07:00
Peter Ammon
d059bdb877
Bring topic monitor naming in line with Rust conventions 2024-06-23 17:06:20 -07:00
Peter Ammon
6163999ec7
Remove the ParserRef type
No need to pass around Rc any more.
2024-06-23 16:49:11 -07:00
Peter Ammon
4557d9fc09
Remove the notion of principal parser
The "principal" parser is the one and only today; in the future we hope to
have multiple parsers to execute fish script in parallel.

Having a globally accessible "principle" parser is suspicious; now we can
get rid of it.
2024-06-23 16:49:11 -07:00
Peter Ammon
631516398e
Remove the notion of the "principal" environment stack
The "principal" environment stack was the one that was associated with the
"principal" parser and would dispatch changes like to TZ, etc.

This was always very suspicious, as a global; now we can remove it.
2024-06-23 16:49:11 -07:00
Peter Ammon
dbf54f49ff
Remove principal_parser() from the last of the tests 2024-06-23 16:49:11 -07:00
Peter Ammon
fd84dc4cdd
Remove principal_parser() from yet more of the tests 2024-06-23 16:49:11 -07:00
Peter Ammon
2bd3bcf7fc
Remove principal_parser() from yet more of the tests 2024-06-23 16:49:11 -07:00
Peter Ammon
0d7e8c22a6
Remove principal_parser() from yet more of the tests 2024-06-23 16:49:11 -07:00
Peter Ammon
077f439283
Remove uses of EnvStack::principal() in the tests 2024-06-23 16:49:11 -07:00
Peter Ammon
0e96a420d6
Remove a use of EnvStack::principal()
Try to get off of globals.
2024-06-23 16:49:11 -07:00
Peter Ammon
9ad875cdb7
Enforce that nobody can push/pop from the global environment stack
This is just a precaution.
2024-06-23 16:39:39 -07:00
ridiculousfish
dee692759a
Split Reader off from ReaderData
Prior to this commit, there was a stack of ReaderDatas, each one has a
reference to a Parser (same Parser in each, for now). However, the current
ReaderData is globally accessible. Because it holds a Parser, effectively
anything can run fish script; this also prevents us from making the Parser
&mut.

Split these up. Create ReaderData, which holds the data portion of the
reader machinery, and then create Reader which holds a ReaderData and a
Parser. Now `reader_current_data()` can only return the data itself; it
cannot execute fish script.

This results in some other nice simplifications.
2024-06-23 16:39:39 -07:00
ridiculousfish
c297df38c7
Migrate the Inputter type to a trait
This is a start on untangling input. Prior to this, a ReaderData and an
Inputter would communicate with each other; this is natural in C++ but
difficult in Rust because the Reader would own an Inputter and therefore
the Inputter could not easily reference the Reader. This was previously
"resolved" via unsafe code.

Fix this by collapsing Inputter into Reader. Now they're the same object!
Migrate Inputter's logic into a trait, so we get some modularity, and then
directly implement the remaining input methods on ReaderData.
2024-06-23 16:39:39 -07:00
ridiculousfish
c9a76bd634
Make OperationContext not hold a Parser via Rc
Exploit Rust's lifetimes. This will lead to simplifications.
2024-06-23 16:39:39 -07:00
Peter Ammon
5a45b189da
Make EnvStackSetResult use Rust naming conventions 2024-06-15 15:57:28 -07:00
Mahmoud Al-Qudsi
7bf3b57e47 Fix buggy test_pthread() condvar test
There's no guarantee that a condition variable is stateful. The docs for
`Condvar::notify_one()` actually say the opposite:

> If there is a blocked thread on this condition variable, then it will be woken
> up from its call to wait or wait_timeout. Calls to notify_one are not buffered
> in any way.

This test was relying on the main loop obtaining the lock and entering the
condition variable sleep before the thread was scheduled and got around to
notifying the condition variable. If this non-deterministic behavior was not
upheld, the test would time out since it would obtain the lock (either before or
after the variable were updated) then call `condvar.wait()` *after* the variable
had been updated and the condvar signalled, but without (atomically or even at
all) checking to see if the desired wake precondition was fulfilled. As the
child thread had already run and the wake notification was NOT buffered, there
was nothing to wake the running thread.

There really wasn't any way to salvage the test as originally written, since the
write to `ctx.val` was not in any way linked to the acquire/release of the mutex
so regardless of whether or not the main thread obtained the mutex and checked
the value precondition before calling `condvar.wait()`, the child thread's write
could have happened after the check but before the wait() call. As such, the
test has been rewritten to use `wait_while()` but then also updated to bail in
case of a timeout instead of hanging indefinitely (since neither the `ctest`
runner nor the `cargo test` harness was timing out; `cargo test` would only
report that the test had exceeded 60 seconds but as long as it was not executed
with `cargo test -- -Z --ensure-time` (which is only available under nightly),
the test would not halt.

If this test were *intentionally* written to test the scenario that was timing
out, it should be written deterministically in such a way that the main loop
did not run until after it was guaranteed that the variable had been updated
(i.e. by looping until val became 5 or waiting for an AtomicBool indicating the
update had completed to be set), but I'm not sure what the benefit in that would
be since the docs actually guarantee the opposite behavior (the notified state
is explicitly not cached/buffered).

If we have fish code written with the assumption that condvar notifications
prior to *any* call to `Condvar::wait()` *are* buffered, then that code should
of course be revisited in light of this.
2024-05-29 13:13:13 -05:00
Johannes Altmanninger
de7f39d627 builtin bind: make function keys lowercase (f1 instead of F1)
All other key names are lowercase so this inconsistency is weird.
2024-05-22 22:38:06 +02:00
Mahmoud Al-Qudsi
8c62f733b3 Extend certain WSL workarounds to WSLv2
This updates is_windows_subsystem_for_linux() to take a WSL version to test for
(any, v1, or v2) and returns the boolean result depending on the system. I've
benchmarked and when running on regular Linux, this is still just as fast as the
previous binary check; it's only when it's WSL that this takes about 20ns
longer to figure out which variant.

Note that older WSLv2 kernels had a `-microsoft-standard` suffix while newer
ones appear to have a `-microsoft-standard-WSL2` suffix, so we make sure to test
for the least common denominator. (It doesn't matter to us, but note that newer
WSLv2 kernels have four dots in the version string!)

WSL workarounds pertaining to the default Windows terminal or executable
behavior of win32 binaries under a WSL shell are extended to WSLv2 while those
specific to oddities in kernel behavior are confined to WSLv1 only. (It
technically wouldn't hurt to extend them to WSLv2 but there's no good reason to
do so, either.)
2024-05-20 14:14:25 -05:00
Mahmoud Al-Qudsi
0f18480559 Simplify Parser and EnvStack singletons and clarify thread semantics
`Parser` is a single-threaded `!Send`, `!Sync` type and does not need to use
`Arc` for anything. We were using it because that's all we had for the parser's
`EnvStack`, but though that is *technically* protected internally by a mutex
(shared with global EnvStack), there's nothing to say that other parsers with a
narrower scope/lifetime on other threads will be necessarily using the same
backing mutex.

We can safely marshal the existing `Arc<EnvStack>` we get from
`environment::principal()` into an `Rc<EnvStack>` since the underlying reference
is always valid. To prove this point, we could have PRINCIPAL_STACK be a static
`EnvStack` and have `environment::principal()` use `Arc::from_raw()` to turn
that into an `Arc<EnvStack>`, but there's no need to factorize this process.
2024-05-16 20:33:39 -05:00
Mahmoud Al-Qudsi
476b360eb8 Remove rust test dependency on cmake output
The test_history_formats test was reading from build/tests/ which is an artifact
of the cmake test runner. The source code tests should not depend on the cmake
test harness at all, so this is changed to read from the original test source in
the ./tests/ directory instead.
2024-05-04 20:49:49 -05:00
Mahmoud Al-Qudsi
a99a7e65e7 Fix build failures when cmake never used
FISH_BUILD_DIR (nominally, ./build) is created by cmake. If you only check out
the project via git and then run `cargo build`, this directory won't exist and
many of the tests will fail.
2024-05-04 19:47:16 -05:00
Mahmoud Al-Qudsi
72259f658f Fix format string for failing test
%ld expects a 4-byte parameter on 32-bit architectures and an 8-byte parameter
on 64-bit architectures, but we supplied are trying to supply a 64-bit parameter
that would overflow 32-bit storage.

Use %lld instead which expects a `long long` parameter, which should be 8-bytes
under both architectures.

See #10474
2024-05-04 19:40:31 -05:00
Johannes Altmanninger
b00899179f Don't indent multi-line quoted strings; do indent inside ()
On a command with multiline quoted string like

    begin
        echo "line1
    line2"
    end

we actually indent line2 which seeems misleading because the indentation
changes the behavior when typed into a script.

This has become more prominent since commits
- a37629f86 (fish_clipboard_copy: indent multiline commands, 2024-04-13)
- 611a0572b (builtins type/functions: indent interactively-defined functions, 2024-04-12)
- 222673f33 (edit_command_buffer: send indented commandline to editor, 2024-04-12)

which add indentation to an exported commandline.

Never indent quoted strings, to make sure the rendering matches the semantics.
Note that we do need to indent the opening quote which is fine because
it's on the same line.

While at it, indent command substitutions recursively.  That feature should
also be added to fish_indent's formatting mode (which is the default).
Fortunately the formatting mode already works fine with quoted strings;
it does not indent them. Not sure how that's done and whether indentation
can use the same logic.
2024-04-30 14:12:48 +02:00
Johannes Altmanninger
29be454652 Move parse_util tests to separate file 2024-04-30 14:00:06 +02:00
Fabian Boehm
69583f3030
Allow restricting abbreviations to specific commands (#10452)
This allows making something like

```fish
abbr --add gc --position anywhere --command git back 'reset --hard
HEAD^'
```

to expand "gc" to "reset --hard HEAD^", but only if the command is
git (including "command git gc" or "and git gc").

Fixes #9411
2024-04-24 18:09:04 +02:00
Johannes Altmanninger
e97a4fab71 Escape : and = in file completions
This is similar to f7dac82ed (Escape separators (colon and equals) to improve
completion, 2019-08-23) except we only escape : and = if they are the result of
file completions.  This way we avoid issues with custom completions like dd.
This also means that it won't work for things like __fish_complete_suffix
[*] but that can be fixed later, once we can serialize the DONT_ESCAPE flag.

By moving the escaping step earlier, this causes some unit test changes
which should not result in actual behavior change.

See also #6099

[*]: The new \: and \= does not leak from "complete -C" because that command
unescapes its output  -- unless --escape is given.
2024-04-20 13:34:08 +02:00
Johannes Altmanninger
a046b73ec7 Extract test logic for computing and applying completion
Also move one test so all the bracket tests are contiguous.
2024-04-20 13:34:08 +02:00
Verte
13230cdda0 Rewrite wgetopt.rs to Rustier syntax and naming
From https://github.com/fish-shell/fish-shell/pull/9515

Closes #9515
2024-04-17 11:26:51 -07:00
ridiculousfish
a996cafeeb Make history::remove take a &wstr instead of a WString
While it does need to store the string, we also need to use the string after
storing it, so we aren't getting any advantage from passing by value. Just pass
by reference to simplify the call sites.
2024-04-15 09:47:46 -07:00
Anurag Singh
c044d5e3f0 add history append subcommand 2024-04-15 08:31:16 +02:00
Johannes Altmanninger
e01fc62d69 Don't leak encoding of invalid codepoints into uvar file
When we read bytes like \xfc that don't produce a Unicode code point,
we encode them in a Unicode private use area.
This encoding should be transparent to the user.

We accidentally add it to uvar files as \uf6fc in this case.  When reading
it back, read_unquoted_escape() will fail at the "fish_reserved_codepoint(c)"
check. This check is to avoid external input being misinterpreted
as one of our in-band signalling characters like ANY_CHAR (for *).

For encoded raw bytes, this check probably doesn't really matter in terms of
security because the only thing we do with these bytes is convert them back
to raw. So we could allow unescaping them at this point, thus supporting
old uvar files.

However that seems like the wrong direction. PUA encoding should never leak.
So let's instead make sure to serialize it as \xfc instead of \f6fc going
forward.

Fixes #10313
2024-04-14 07:59:42 +02:00
Johannes Altmanninger
29dc307111 Insert some completions with quotes instead of backslashes
File names that have lots of spaces look quite ugly when inserted as
completions because every space will have a backslash.

Add an initial heuristic to decide when to use quotes instead of
backslash escapes.

Quote when
1. it's not an autosuggestion
2. we replace the token or insert a fresh one
3. we will add a space at the end

In future we could relax some of these requirements.

Requirement 2 means we don't quote when appending to an existing token.
Need to find a natural behavior here.

Re 3, if the completion adds no space, users will probably want to add more
characters, which looks a bit weird if the token has a trailing quote.
We could relax this requirement for directory completions, so «ls so»
completes to «ls 'some dir with spaces'/».

Closes #5433
2024-04-13 15:34:21 +02:00
Johannes Altmanninger
9db53e8d26 Allow abbreviating ctrl-/alt- as c-/a-
This makes them more convenient to use interactively, similar to the existing
\c and \a versions.  The resulting bind output keeps using the canonical
ctrl/alt version.

Not sure about s- because that's somewhat ambiguous, it could be "super".
2024-04-12 11:27:55 +02:00
Johannes Altmanninger
f61ef2c63d Display raw escape sequences the old way again
If a binding was input starting with "\e", it's usually a raw control sequence.
Today we display the canonical version like:

    bind --preset alt-\[,1,\;,5,C foo

even if the input is

    bind --preset \e\[1\;5C foo

Make it look like the input again.  This looks more familiar and less
surprising (especially since we canonicalize CSI to "alt-[").

Except that we use the \x01 representation instead of \ca because the
"control" part can be confusing. We're inside an escape sequence so it seems
highly unlikely that an ASCII control character actually comes from the user
holding the control key.

The downside is that this hides the canonical version; it might be surprising
that a raw-escape-sequence binding can be erased using the new syntax and
vice versa.
2024-04-09 00:07:27 +02:00
Johannes Altmanninger
d025b245f6 Fix parsing of single-digit function keys 2024-04-09 00:07:27 +02:00
Johannes Altmanninger
405c9c6aaf Remove unused import 2024-04-08 09:16:22 +02:00
Johannes Altmanninger
f285e85b0c Enable focus reporting only just before reading from stdin
Some terminals send the focus-in sequences ("^[I") whenever focus reporting is
enabled.  We enable focus reporting whenever we are finished running a command.
If we run two commands without reading in between, the focus sequences
will show up on the terminal.

Fix this by enabling focus-reporting as late as possible.

This fixes the problem with `^[I` showing up when running "cat" in
gnome-terminal https://github.com/fish-shell/fish-shell/issues/10411.

This begs the question if we should do the same for CSI u and bracketed paste.
It's difficult to answer that; let's hope we find motivating test cases.
If we enable CSI u too late, we might misinterpret key presses, so for now
we still enable those as early as possible.

Also, since we now read immediately after enabling focus events, we can get
rid of the hack where we defer enabling them until after the first prompt.
When I start a fresh terminal, the ^[I no longer shows up.
2024-04-06 11:22:19 +02:00
Fabian Boehm
1f2e0617d1 tests: Pass correct length for buffer
This allocated 64 bytes and then told snprinf it was 128. That's a
no-no even if we never need that much.
2024-04-03 20:15:17 +02:00
Johannes Altmanninger
8bf8b10f68 Extended & human-friendly keys
See the changelog additions for user-visible changes.

Since we enable/disable terminal protocols whenever we pass terminal ownership,
tests can no longer run in parallel on the same terminal.

For the same reason, readline shortcuts in the gdb REPL will not work anymore.
As a remedy, use gdbserver, or lobby for CSI u support in libreadline.

Add sleep to some tests, otherwise they fall (both in CI and locally).

There are two weird failures on FreeBSD remaining, disable them for now
https://github.com/fish-shell/fish-shell/pull/10359/checks?check_run_id=23330096362

Design and implementation borrows heavily from Kakoune.

In future, we should try to implement more of the kitty progressive
enhancements.

Closes #10359
2024-04-02 14:35:16 +02:00
Johannes Altmanninger
d0cdb142de Make CharEvent a native enum 2024-04-02 14:35:16 +02:00
Fabian Boehm
217b009e18
abbr: expand command abbrs after decorators (#10396)
Currently, we expand command-abbrs (those with `--position command`) after `if`, but not after `command` or `builtin` or `time`:

```fish
abbr --add gc "git checkout"
```

will expand as `if gc` but not as `command gc`.

This was explicitly tested, but I have no idea why it shouldn't be?
2024-03-27 17:17:55 +01:00
Johannes Altmanninger
5b324f8ecb Fix regression in parse_util_process_extent
Found on a two-line commandline

    for file in (path base<TAB>
    echo
2024-03-24 16:34:36 +01:00
Johannes Altmanninger
1216801474 Use RAII for restoring term modes
In particular, this allows restoring the terminal on crashes, which is
feasible now that we have the panic handler.  Since std::process::exit() skips
destructors, we need to reshuffle some code.  The "exit_without_destructors"
semantics (which std::process::exit() als has) was mostly necessary for C++
since Rust leaks global variables by default.
2024-03-24 16:34:36 +01:00
Johannes Altmanninger
3cfa09d1bd Make test_init() return a scope guard
To be used in the next commit.
2024-03-24 16:33:35 +01:00
Johannes Altmanninger
5c5ab4f179 Move termsize test into separate file 2024-03-24 12:18:20 +01:00