Commit Graph

500 Commits

Author SHA1 Message Date
Johannes Altmanninger
61d506afd0 builtin status: use program name constant again 2023-09-14 20:42:25 +02:00
Fabian Boehm
fa390334a8 Remove wcs/wcwidth ffi calls
This was ported a while back, we can just call the rust version
2023-09-05 21:57:38 +02:00
Henrik Hørlück Berg
676c3c9bc2 Re-use DEFAULT_PATH in setup_path
- No need to hard-code a different default
2023-09-05 11:38:59 +02:00
Henrik Hørlück Berg
008764a2cd Make DEFAULT_PATH allow overriding system binaries
This is in regards to a comment on 290d07a833, which resulted in 46c967903d.
Those commits handled the default path when it is unset on startup.
DEFAULT_PATH is used when PATH is unset at runtime as far as I can tell.

As far as I can tell this has had the non-overidding ordering behavior since inception
(or at least 17 years ago ea998b03f2).
2023-09-05 11:38:59 +02:00
Henrik Hørlück Berg
eacbd6156d Port and adopt main written in Rust
We don't change anything about compilation-setup, we just immediately jump to
Rust, making the eventual final swap to a Rust entrypoint very easy.

There are some string-usage and format-string differences that are generally
quite messy.
2023-09-05 11:38:59 +02:00
Henrik Hørlück Berg
96e58dac21 Port env_init to Rust
- This does not adopt it.
2023-09-05 11:38:59 +02:00
Henrik Hørlück Berg
e7f8fb04cc Add BUILD_VERSION to lib.rs
In CMake this used a `version` file in the CARGO_MANIFEST_DIR, but
relying on that is problematic due to change-detection, as if we add
`cargo-rerun-if-changed:version`, cargo would rerun every time if the file does
not exist, since cargo would expect the file to be generated by the
build-script. We could generate it, but that relies on the output of `git
describe`, whose dependencies we can only limit to anything in the
`.git`-folder, again causing unnecessary build-script runs.

Instead, this reads the `FISH_BUILD_VERSION`-env-variable at compile time
instead of the `version`-file, and falls back to calling git-describe through
the `git_version`-proc-macro. We thus do not need to deal with extraneous
build-script running.
2023-09-05 11:38:59 +02:00
Henrik Hørlück Berg
360ba46660 Make ConfigPaths store as PathBuf
- These are paths, we can just store them raw.
2023-09-05 11:38:59 +02:00
Henrik Hørlück Berg
25d207a8ce Make get_current_exe use impl AsRef<Path>
- It does not need to require the default to be valid UTF8
2023-09-05 11:38:59 +02:00
Henrik Hørlück Berg
a92804a8a1 Port MISSING_HELP and MISSING (arg)
C++ main used getopt (no w!), which appears to internally print
error-messages. The Rust version will use `wgetopter_t`, and therefore needs to
print this itself.
2023-09-05 11:38:59 +02:00
Henrik Hørlück Berg
7c2311abd7 Make methods in path not unnecessairly take &mut 2023-09-05 11:38:59 +02:00
Henrik Hørlück Berg
cf8e0ae1b5 Make ParsedSource::new pub
It will be used from main
2023-09-05 11:38:59 +02:00
Henrik Hørlück Berg
3777bc941f Port history::start_private_mode to Rust 2023-09-05 11:38:59 +02:00
Henrik Hørlück Berg
c5c5043d7e Make PROGRAM_NAME safe, fix cmp
- It is currently never set, but will be set once `main` is ported
- `should_suppress_stderr_for_tests` used to be PROGRAM_NAME !=
  TESTS_PROGRAM_NAME, but the equivalent C++ code was
  `!std::wcscmp(program_name, TESTS_PROGRAM_NAME)`, and `wcsmp` returns
  zero if they are equal, thus is equivalent to `==` in Rust
2023-09-05 11:38:59 +02:00
Henrik Hørlück Berg
e4df340f43 Forward CMake configs to Cargo 2023-09-05 11:38:59 +02:00
Henrik Hørlück Berg
5db08e1126 Make CARGO_MANIFEST_DIR for fish-rust equal to src 2023-09-05 11:38:59 +02:00
Johannes Altmanninger
a3311c9b09 Fix incorrect port of write_to_fd 2023-09-03 14:03:14 +02:00
yanshay
01db48a712
Added fish_sequence_key_delay_ms to set time to wait between sequence key presses (#7401) (#9926)
* added support for fish_sequence_key_delay_ms to set how long to wait between sequence key presses

* fixed cargo fmt
2023-08-30 23:12:22 +02:00
Fabian Boehm
b454b3bc40 Also allow command and in a pipeline
Similar to `time`, except that one is more common as a command.

Note that this will also allow `builtin and`, which is somewhat
useless, but then it is also useless outside of a pipeline.

Addition to #9985
2023-08-26 13:45:54 +02:00
Fabian Boehm
03e659f96d Bring back "(deleted)" hack for status fish-path
This is untested mostly because it is supremely awkward to test.

Fixes #9925
2023-08-25 22:02:55 +02:00
Fabian Boehm
482616f101 parse_util: Only reject time in a pipeline without decorator
This allows e.g. `foo | command time`, while still rejecting `foo | time`.

(this should really be done in the ast itself, but tbh most of
parse_util kinda should)

Fixes #9985
2023-08-25 19:45:15 +02:00
Fabian Boehm
03402e572d format 2023-08-25 16:28:41 +02:00
Fabian Boehm
06b89083d5 tinyexpr: Check for wcstod errors
This would otherwise unwrap() an Err and crash.
2023-08-25 16:15:52 +02:00
Henrik Hørlück Berg
05c44df1a4 Run cargo fmt with Rustfmt 1.6.0
- "1.6.0" now supports formatting let-else statements which we use liberally,
  and appears to have some fixes in regards to long-indented-lines with macros
  like `wgettext_ft!`
- This commit updates the formatting so that devs with the latest stable don't
  see random format-fixes upon running `cargo fmt`
2023-08-24 18:16:24 +02:00
Fabian Boehm
3caabdcbc9 Switch math to using Arguments
Removes some duplicated code and lets this do chunked reading.
2023-08-20 14:52:58 +02:00
ridiculousfish
46f9a8bb28 Stop using sprintf in builtin_random 2023-08-19 20:19:54 -07:00
ridiculousfish
e500250775 Minor improvement to get_depth in ast.rs 2023-08-19 20:19:38 -07:00
ridiculousfish
04299cb4c9 Remove RgbColor::description
This was unused; deriving Debug is sufficient.
2023-08-19 20:04:23 -07:00
ridiculousfish
eeecd6517d Remove FileId::dump
Instead just derive Debug. No reason for this to be custom.
2023-08-19 17:45:17 -07:00
ridiculousfish
d2f7a3507b Implement to_wstr() for ParseTokenType and ParseKeyword
This cleans up some messy call sites.
2023-08-19 17:45:10 -07:00
ridiculousfish
cc1e4b998a Remove some dead bridge code
This was obviated after the AST was ported to Rust.
2023-08-19 16:31:42 -07:00
Fabian Boehm
53a5ce52c5 Implement FLOGF formatting
Note: This *requires* an argument after the format string:

```rust
FLOGF!(debug, "foo");
```

won't compile. I think that's okay, because in that case you should
just use FLOG.

An alternative is to make it skip the sprintf.
2023-08-19 16:56:59 +02:00
Fabian Boehm
798d7427f7 Switch broken uses of FLOGF to FLOG
"FLOGF!" is supposed to treat its first argument as a format
string (but doesn't because that part isn't implemented currently).

That means running something like

```rust
FLOGF!(term_support, "curses var", var_name, "=", value);
```

That would rightly just print "curses var", ignoring the other
arguments.

By contrast, FLOG! is the literal "just join these as a string"
version.
2023-08-19 16:56:59 +02:00
Henrik Hørlück Berg
bc29b4aee1 Move edition and MSRV to workspace 2023-08-19 15:18:38 +02:00
Henrik Hørlück Berg
c23f419af1 Use the workspace from CMake
- Make CMake use the correct target-path
- Make build.rs use the correct target dir

Workspaces place it in the project root by default, the alternative to making
this change is to add a `.cargo/config.toml` file with

```toml
[build]
target-dir = "fish-rust/target"
```

Which I think is unnecessary, as we likely want to use the new location anyways.
2023-08-19 15:18:38 +02:00
Henrik Hørlück Berg
87df7b9adf Use a cargo workspace
- This allows running `cargo fmt/clippy/test/etc` from root
- Ideally the root should be the fish-rust package instead of being virtual, but
  that requires changed to CMake/Corrosion. This change should instead be
  completely compatible with our existing setup.
- This also means we will only have on `Cargo.lock` for all current and future
  crates.
2023-08-19 15:18:38 +02:00
Henrik Hørlück Berg
1fa56972b5 Fix clippy lint in widestring-suffix 2023-08-19 15:18:38 +02:00
Fabian Boehm
566123edc6
Port builtin count to rust (#9963)
* Port builtin count to rust

* Explicitly use wstring
2023-08-18 23:18:52 +02:00
Fabian Boehm
a29aa44183 functions: Fix command name
This was "function", needs to be "function*s*".

It was only an issue in the option parsing because we set cmd there
again instead of passing it. Maybe these should just be file-level constants?
2023-08-18 17:16:44 +02:00
Henrik Hørlück Berg
88da7121af Adopt appendln 2023-08-16 18:37:27 +02:00
Henrik Hørlück Berg
dceefcdaba Add an appenln method to output_stream_t
This is an alternative to the very common pattern of

```rust
streams.err.append(output);
streams.err.append1('\n');
```

Which has negative performance implications, see https://github.com/fish-shell/fish-shell/pull/9229

It takes `Into<WString>` to hopefully avoid allocating anew when the argument is
a WString with leftover capacity
2023-08-16 18:37:27 +02:00
ridiculousfish
d47b2a7e0b Refactor the killring to make it instanced
This improves test isolation.

Also standardize on the name "killring" instead of "kill list" and remove some
dead code.
2023-08-13 13:06:06 -07:00
ridiculousfish
2b25cd1654 Complete the transition of the kill ring and remove kill.cpp
This finishes off the transition of the kill ring from C++ to Rust, and removes
the C++ bits.
2023-08-13 13:06:06 -07:00
Fabian Boehm
995f12219b Use out_is_terminal
This removes some spurious unsafe and some imports.

Note: We don't use it in `test`, because that can be asked to check
arbitrary file descriptors, while this only checks stdout specifically.
2023-08-13 14:17:52 +02:00
Fabian Boehm
6489ef5ac0 Rewrite builtin functions in rust 2023-08-13 14:17:44 +02:00
Fabian Boehm
5e78cf8c41 Add io_streams_t::out_is_terminal()
This encapsulates a "is our output going to the terminal" check we do
in a few places - functions, type, set_color, possibly test
2023-08-13 14:17:44 +02:00
Fabian Boehm
b75f901376 Fix reformat_for_screen
This had an infinite loop because it had two checks broken
2023-08-13 14:17:44 +02:00
Fabian Boehm
ee8e790aa7 Fix event::print's header printing
Turns out doing `==` on Enums with values will do a deep comparison,
including the values.

So EventDescription::Signal(SIGTERM) is !=
EventDescription::Signal(SIGWINCH).

That's not what we want here, so this does a bit of a roundabout thing.
2023-08-13 14:17:44 +02:00
Fabian Boehm
4f86f303f5 Make functions for builtin functions public
event filter names, function::set_desc, common::reformat_for_screen

This is the first use for each
2023-08-13 14:17:44 +02:00
99jte
5b136d450f
Include the target of bad redirects in the error (#9947)
Fixes #8877
2023-08-13 14:01:32 +02:00
ridiculousfish
b2ff4d6bc0 Adopt Rust PosixSpawner
This removes the C++ posix_spawner_t, adopting the Rust implementation.
2023-08-12 17:15:21 -07:00
ridiculousfish
245f7db5b3 Port PosixSpawner to Rust
PosixSpawner is our wrapper around posix_spawn.
2023-08-12 16:59:38 -07:00
Fabian Boehm
27a11ef7fe builtin builtin: Print help if run without an action to do
Fixes #9942
2023-08-09 17:26:07 +02:00
Henrik Hørlück Berg
0844247b43 Prefer os-unix prelude over importing everything separately 2023-08-09 15:00:58 +02:00
Henrik Hørlück Berg
131e249b0c Adopt the builtin prelude 2023-08-09 15:00:58 +02:00
Henrik Hørlück Berg
773bafb7c7 Add a builtin prelude
- Most builtins share a lot of similar imports
2023-08-09 15:00:58 +02:00
Henrik Hørlück Berg
fae090ea67 Adopt the wchar prelude 2023-08-09 15:00:58 +02:00
Henrik Hørlück Berg
5d58652394 Add a wchar prelude
- This will hopefully make it easier to always include WExt and ToWString, and
  make using WStr/WString more natural
2023-08-09 15:00:58 +02:00
Henrik Hørlück Berg
3a484480bf Remove premature optimization
The `impl<T> Hash for &T` hashes the string itself[^1].
It is unclear if that is actually faster than just calling `keyfunc` multiple times (they should all be linear).
For context, Rust by default uses SipHash 1-3 db1b1919ba
An alternative would be to store it as raw pointers aka `*const T`, which have a cheaper hash impl.
That has a more complicated implementation + removes lifetimes.

This commit rather removes the premature optimization.

[^1]: Source: https://doc.rust-lang.org/std/ptr/fn.hash.html
2023-08-07 21:01:11 -07:00
Henrik Hørlück Berg
4a4171c34a Forward some error messages and fix a bug
- The Err-variants will be used by e.g. wildcard, so might as well change it
  now.
- `create_directory` should now not infinitely loop until  it fails with an
  error message that isn't `EAGAIN`
2023-08-07 21:01:11 -07:00
Henrik Hørlück Berg
f4a5de1fbf Port builtins/path to Rust 2023-08-07 21:01:11 -07:00
ridiculousfish
2d779fb194 Fix additional clippy lint errors
These lint errors appear new with clippy 0.1.72.
2023-08-05 17:29:53 -07:00
ridiculousfish
8771d8f903 Remove some pub(self)s
This fixes a clippy 0.1.72 lint
2023-08-05 15:50:07 -07:00
Henrik Hørlück Berg
900a048744 Don't segfault if user has an invalid locale
Fixes #9928
2023-08-03 19:55:05 +02:00
Fabian Boehm
5de19d2e84 Remove broken &
Fixes the build
2023-08-02 21:21:46 +02:00
David Adam
35aa7636eb fds: add make_fd_{,non}blocking implementations in Rust 2023-08-01 22:56:25 +08:00
David Adam
0b291355b2 wutil: add perror implementation that takes an io::Error 2023-08-01 22:56:25 +08:00
Henrik Hørlück Berg
2ec36338f2 Very minor leftover codereview var-renaming 2023-07-27 22:00:03 -07:00
Henrik Hørlück Berg
cdc08dbb71 Add back well-backed comment
- The dermination is from commit 7988cff6bd
- See PR https://github.com/fish-shell/fish-shell/pull/9139
2023-07-27 22:00:03 -07:00
Henrik Hørlück Berg
6dd2cd2b20 Fix behaviour in the presence of non-visible width
Padding with an unprintable character is now disallowed, like it was for other
zero-length characters.

`string shorten` now ignores escape sequences and non-printable characters
when calculating the visible width of the ellipsis used (except for `\b`,
which is treated as a width of -1).
Previously `fish_wcswidth` returned a length of -1 when the ellipsis-str
contained any non-printable character, causing the command to poentially
print a larger width than expected.

This also fixes an integer overflows in `string shorten`'s
`max` and `max2`, when the cumulative sum of character widths turned negative
(e.g. with any non-printable characters, or `\b` after the changes above).
The overflow potentially caused strings containing non-printable characters
to be truncated.

This adds test that verify the fixed behaviour.
2023-07-27 22:00:03 -07:00
Henrik Hørlück Berg
20be990fd9 Port builtins/string to Rust
- Add test to verify piped string replace exit code

Ensure fields parsing error messages are the same.

Note: C++ relied upon the value of the parsed value even when `errno` was set,
that is defined behaviour we should not rely on, and cannot easilt be replicated from Rust.
Therefore the Rust version will change the following error behaviour from:

```shell
> string split --fields=a "" abc
string split: Invalid fields value 'a'
> string split --fields=1a "" abc
string split: 1a: invalid integer
```

To:

```shell
> string split --fields=a "" abc
string split: a: invalid integer
> string split --fields=1a "" abc
string split: 1a: invalid integer
```
2023-07-27 22:00:03 -07:00
ridiculousfish
ade6650599 Remove FunctionPropertiesRef type alias
Per code review, this type alias was confusing.
2023-07-23 17:18:36 -07:00
ridiculousfish
e24a16bd31 function: make inherit_vars a boxed slice instead of a hash map
Empty hash maps muck around with TLS. Per code review, use a boxed slice
of a tuple instead. This has the nice benefit of printing inherited vars
in sorted order.
2023-07-23 17:18:36 -07:00
ridiculousfish
a672edc0d5 Adopt the new function store and rewrite builtin_function
This adopts the new function store, replacing the C++ version.

It also reimplements builtin_function in Rust, as these was too coupled to
the function store to handle in a separate commit.
2023-07-23 17:18:36 -07:00
ridiculousfish
076f317c31 Implement (but do not yet adopt) fish function store in Rust
This reimplements the function module in Rust. The function module stores the
global set of fish functions, and provides information about them.
2023-07-23 17:18:36 -07:00
ridiculousfish
2a13a30807 Clean up DirIter
DirIter had a serious bug where it would crash on an invalid path. Make it more
robust and rationalize its error handling. Move it into its own module and add
tests.
2023-07-16 12:05:29 -07:00
ridiculousfish
f5e5896c70 Remove the EventDescription wrapper type
Prior to this change, we had a silly wrapper type EventDescription which wrapped
EventType, which actually described the event.

Remove this wrapper and rename EventType to EventDescription (since it describes
more than just the type of event).
2023-07-15 11:59:08 -07:00
ridiculousfish
ecfabf4db8 argparse: Use a named constant for RETURN_IN_ORDER returns
The RETURN_IN_ORDER argparse mode (enabled via leading '-') causes non-options
(i.e. positionals) to be returned intermixed with options in the original order,
instead of being permuted to the end. Such positionals are identified via the
option sentinel of char code 1. Use a real named constant for this return,
rather than weird stuff like '\u{1}'
2023-07-15 11:35:13 -07:00
Henrik Hørlück Berg
63b23713f2 Support thread-safe feature-flag-dependant tests
This also allows scoped feature tests that makes testing feature flags thread-safe.
As in you can guarantee that the test actually has the correct feature flag
value, regardless of which other tests are running in parallell.
2023-07-11 12:05:38 -07:00
Henrik Hørlück Berg
f1cd43d58b Disallow using set outside of tests, minor fixes 2023-07-11 12:05:38 -07:00
Henrik Hørlück Berg
726819e8ee Clean up feature flags API
This also cleans up and removes unnecessary usage of FFI-oriented `feature_metadata_t`,
which is only used from Rust code after `builtins/status` was ported.
2023-07-11 12:05:38 -07:00
ridiculousfish
57afaf7fb2 Restore the behavior of remembering the CWD fd in the parser
This will be important for concurrent execution, because different parsers will
have different working directories.
2023-07-10 21:30:37 +08:00
David Adam
289fbecaa9 Rewrite cd builtin in Rust
Note this is slightly incomplete - the FD is not moved into the parser, and so
will be freed at the end of each directory change. The FD saved in the parser is
never actually used in existing code, so this doesn't break anything, but will
need to be corrected once the parser is ported.
2023-07-10 21:30:37 +08:00
ridiculousfish
c1e1efd747 Pull an allocation out of the string escape test inner loop 2023-07-08 11:26:32 -07:00
ridiculousfish
98d88e06ff Use setlocale() in the test_convert test
This "fixes" (or at least hides) the intermittent test_convert failures,
as we no longer race with other setlocale calls.
2023-07-08 11:22:47 -07:00
ridiculousfish
a99fa201b6 Make escape_test an ordinary function
This did not need to be a macro.
2023-07-08 11:19:44 -07:00
Henrik Hørlück Berg
7b0f9fd5f8 Double the speed of cargo test, actually run test
- Parallelize the slow tests if possible.
- `test_convert_ascii` was missing a `#[test]` annotation
2023-07-08 11:05:55 -07:00
Henrik Hørlück Berg
47f1dbe56c Make test_convert seedable, but generate the seed 2023-07-08 11:05:55 -07:00
David Adam
87307775fc fds: add comment on O_CLOEXEC fallback being dropped 2023-07-05 10:30:27 +08:00
ridiculousfish
bee422fea2 Use a faster, deterministic RNG in the string escape tests
This shaves about 9 seconds off of the runtime, and makes the test
deterministic.

We do not touch the test_convert test because there is a known failure and we
need to track it down before making it deterministic.
2023-07-04 13:27:53 -07:00
ridiculousfish
0bfe83ce88 Replace write! calls with explicit hex formatting
Rather than relying Rust's formatting, just compute the hex chars directly.

This shaves about 6 seconds off of the test runtime.
2023-07-04 13:27:53 -07:00
ridiculousfish
c48c0bb226 Replace sprintf call with write!
This reduces the time for the Rust tests from a few minutes to ~40 seconds.

Also fix some bogus comments which were ported from C++.
2023-07-04 13:27:53 -07:00
ridiculousfish
b16f617fb3 Migrate string and lock tests into their own files
Get some stuff out of the common module, which is growing large.

Also migrate the tests into "native" Rust tests so they will run in parallel.
We have to use an explicit setlocale() call to get a multibyte locale, for the
"crazy" tests.
2023-07-04 13:27:53 -07:00
ridiculousfish
eaf8e73c42 Make escape/unescape string_var hew more closely to the C++ 2023-07-04 13:27:53 -07:00
Henrik Hørlück Berg
970ed610df Avoid string copying to speed up asan 2023-07-04 13:27:53 -07:00
Henrik Hørlück Berg
595d593732 Fully migrate to Rust escape string tests and code
Co-Authored-By: Mahmoud Al-Qudsi <mqudsi@neosmart.net>
2023-07-04 13:27:53 -07:00
Henrik Hørlück Berg
0a4bcf7430 Port (un)escape-tests, fix a couple bugs 2023-07-04 13:27:53 -07:00
ridiculousfish
ec28a30bd6 Fix a clippy lint warning 2023-07-04 13:26:19 -07:00
ridiculousfish
35f8f421fe topic_monitor to migrate from wperror to perror
This avoids needing to use the ffi
2023-07-04 13:26:19 -07:00
ridiculousfish
15361f62ed signal.rs to stop using wperror
This needed to cross the ffi which is annoying in tests. Use the Rust perror()
instead.
2023-07-04 13:26:19 -07:00