Commit Graph

202 Commits

Author SHA1 Message Date
ridiculousfish
8ec1467dda Implement (but do not yet adopt) Environment in Rust
This implements the primary environment stack, and other environments such
as the null and snapshot environments, in Rust. These are used to implement
the push and pop from block scoped commands such as `for` and `begin`, and
also function calls.
2023-05-07 15:15:56 -07:00
Johannes Altmanninger
6ede7f8009 Delete wcstring_list_t
We don't want it in Rust. Remove it to smoothen the transition.
2023-04-19 01:03:16 +02:00
Johannes Altmanninger
05bad5eda1 Port common.{h,cpp} to Rust
Most of it is duplicated, hence untested.

Functions like mbrtowc are not exposed by the libc crate, so declare them
ourselves.
Since we don't know the definition of C macros, add two big hacks to make
this work:
1. Replace MB_LEN_MAX and mbstate_t with values (resp types) that should
   be large enough for any implementation.
2. Detect the definition of MB_CUR_MAX in the build script. This requires
   more changes for each new libc. We could also use this approach for 1.

Additionally, this commit brings a small behavior change to
read_unquoted_escape(): we cannot decode surrogate code points like \UDE01
into a Rust char, so use � (\UFFFD, replacement character) instead.
Previously, we added such code points to a wcstring; looks like they were
ignored when printed.
2023-04-02 15:17:06 +02:00
Johannes Altmanninger
998cb7f1cd New wcs2zstring to explicitly convert to zero-terminated strings
wcs2string converts a wide string to a narrow one.  The result is
null-terminated and may also contain interior null-characters.
std::string allows this.

Rust's null-terminated string, CString, does not like interior null-characters.
This means we will need to use Vec<u8> or OsString for the places where we
use interior null-characters.
On the other hand, we want to use CString for places that require a
null-terminator, because other Rust types don't guarantee the null-terminator.

Turns out there is basically no overlap between the two use cases, so make
it two functions. Their equivalents in Rust will have the same name, so
we'll only need to adjust the type when porting.
2023-04-02 15:17:06 +02:00
ridiculousfish
d843b67d2d Initial Rust commit 2023-02-02 19:34:47 -07:00
Aaron Gyes
daf5e11179 Spelling fixes
Found with scspell
2022-10-28 20:10:09 -07:00
ridiculousfish
5f4583b52d Revert "Re-implement macro to constexpr transition"
This reverts commit 3d8f98c395.

In addition to the issues mentioned on the GitHub page for this commit,
it also broke the CentOS 7 build.

Note one can locally test the CentOS 7 build via:

    ./docker/docker_run_tests.sh ./docker/centos7.Dockerfile
2022-09-20 11:58:37 -07:00
Mahmoud Al-Qudsi
3d8f98c395 Re-implement macro to constexpr transition
Be more careful with sign extension issues stemming from the differences in how
an untyped literal is promoted to an integer vs how a typed (and signed) `char`
is promoted to an integer.
2022-09-19 18:10:41 -05:00
Mahmoud Al-Qudsi
7c3e4a7ccb Revert "Convert constant macros to constexpr expressions"
This reverts commit e1626818f7.
2022-09-19 17:42:11 -05:00
Mahmoud Al-Qudsi
e1626818f7 Convert constant macros to constexpr expressions
Also convert some `const[expr] static xxx` to `const[expr] xxx` where it makes
sense to let the compiler deduce on its own whether or not to allocate storage
for a constant variable rather than imposing our view that it should have STATIC
storage set aside for it.

A few call sites were not making use of the `XXX_LEN` definitions and were
calling `strlen(XXX)` - these have been updated to use `const_strlen(XXX)`
instead.

I'm not sure if any toolchains will have raise any issues with these changes...
CI will tell!
2022-09-19 17:17:09 -05:00
ridiculousfish
3eae0a9b6a clang-format all C++ files
This mostly re-sorts headers that got desorted after the IWYU
application in 14d2a6d8ff.
2022-08-21 15:02:19 -07:00
Aaron Gyes
14d2a6d8ff IWYU-guided #include rejiggering.
Let's hope this doesn't causes build failures for e.g. musl: I just
know it's good on macOS and our Linux CI.

It's been a long time.

One fix this brings, is I discovered we #include assert.h or cassert
in a lot of places. If those ever happen to be in a file that doesn't
include common.h, or we are before common.h gets included, we're
unawaringly working with the system 'assert' macro again, which
may get disabled for debug builds or at least has different
behavior on crash. We undef 'assert' and redefine it in common.h.

Those were all eliminated, except in one catch-22 spot for
maybe.h: it can't include common.h. A fix might be to
make a fish_assert.h that *usually* common.h exports.
2022-08-20 23:55:18 -07:00
Fabian Homborg
ef28931293 Stop migrating legacy uvar paths
These were changed in fish 3.0 in December 2018.

This means upgrading from fish 2.7.1 or earlier to the next fish
version will require users to set their universal variable again.
2022-03-17 18:15:11 +01:00
ridiculousfish
57a9fe492e Allow using poll() to check for readability
Cygwin tests are failing because cygwin has a low limit of only 64 fds in
select(). Extend select_wrapper_t to also support using poll(), according to
a FISH_USE_POLL new define. All systems now use poll() except for Mac.

Rename select_wrapper_t to fd_readable_set_t since now it may not wrap
select().

This allows the deep-cmdsub.fish test to pass on Cygwin.
2022-01-02 16:36:33 -08:00
ridiculousfish
0d67dd19a1 Switch path_get_data_is_remote to returning a real class enum
End the tricky use of maybe_t<bool> by using a real class enum.
2021-12-18 20:48:13 -08:00
Aaron Gyes
ccf0b69c3d Apply Johannes' patch.
I goofed this up.
2021-12-11 10:49:40 -08:00
Aaron Gyes
e65405ef52 int -> maybe_t<bool> 2021-12-09 00:29:37 -08:00
ridiculousfish
15a3caf244 Refactor env_universal_callbacks
Reduce some allocations and simplify how events are emitted.
2021-11-14 17:39:52 -08:00
ridiculousfish
389b75fe42 Restyle codebase with clang-format 2021-11-08 12:21:11 -08:00
Fabian Homborg
7850a10c45 Stop some wcs2stringing
These are paths that barely change, narrowing them *twice* per file
load makes absolutely no sense.
2021-10-14 17:02:50 +02:00
Aaron Gyes
f9def20180 Update ConfigureChecks, only do the mtime hack for Linux.. on Linux
- Only check for HAVE_CLOCK_GETTIME and HAVE_FUTIMENS on Linux, since
they are only used to implement a Linux-specific workaround related
to mtime precision.
- Make sure that hack is limited to Linux builds
- HAVE_SYS_SYSCTL_H was unused, but we should have been using it
- HAVE_TERMIOS_H was unused, remove it

The only functional change is that unix machines with clock_gettime
and futimens will not bother with a Linux-specific hack, and won't
waste time checking for either during cmake configuration either.
2021-10-02 17:02:53 -07:00
ridiculousfish
15cee66df1 Wrap even more stuff in anonymous namespaces 2021-09-30 11:33:03 -07:00
ridiculousfish
9a2482557d get_hostname_identifier to not return empty hostnames
When getting the hostname to construct the legacy uvar path, if the
hostname is empty, we will create a path pointing at a directory. On
BSDs this path can be successfully open'd and we will produce errors
about invalid uvar files.
2021-09-17 11:18:39 -07:00
ridiculousfish
d6075885ef Reduce named pipe uvar notifier poll time from 100 msec to 10 msec
To broadcast a uvar change on Linux, we write to a named pipe, wait a bit,
and then read it back. While the pipe is readable, fish will enter a "polling
mode" where it will check for uvar changes every N msec, until the pipe is no
longer readable. If the pipe stays readable for too long (5 seconds), fish
will try to drain it; this may happen if broadcasting instance of fish is
killed before it can read back its data.

In #8209 we have a case where fish is launched in the background to set a
uvar, and then immediately exits, leaving data on the pipe. This means that
we are perpetually in a polling mode until we hit that timeout. Reduce the
timeout to 1 second and the polling interval to 10 msec.

This improves #8209; it doesn't fix it fully but I think it's the best we can
do absent some other IPC mechanism.
2021-09-16 15:25:31 -07:00
ridiculousfish
71df8f8622 Do not flock the uvars file on remote filesystems
In rare cases this may cause the universal variable file to drop
an update, if two happen at the same time and HOME is on an nfs mount.
But this is considered better than hanging if nfs is lockless.

Fixes #7968.
2021-05-10 15:24:32 -07:00
ridiculousfish
ba33b6dcc8 Mild refactoring of flock logic inside env_universal_t
This reorganizes the flock code in env_universal_t, removing a static
variable and making the behavior more explicit.
2021-05-10 15:23:57 -07:00
ridiculousfish
083f2698f9 Remove internal lock from env_universal_t
env_universal_t locking discipline is now managed by env.cpp.
That is, the shared instance of env_universal_t is managed by a lock.
We no longer need to have an internal lock, so remove it.
2021-05-10 15:23:57 -07:00
ridiculousfish
e2a1b25a24 Continue refactoring env_universal_t
Previously an instance of env_universal_t had to be created with a file
path. Switch to allowing it to be created as empty, and later initialized
with the file path. This will help simplify the case where universal
variables are not used; they may simply be not initialized and so just
appear empty.
2021-05-10 15:23:08 -07:00
ridiculousfish
fa7402c415 Reorganize env_universal_t so that the public bits are at the top
No functional change here.
2021-05-10 14:23:07 -07:00
ridiculousfish
0d8cb0125a Remove the narrow_vars_path from universal variables
This was a cache of the wide vars_path, but it's not worth its
complexity.
2021-05-10 14:23:07 -07:00
ridiculousfish
6ab7945623 Mild refactoring of universal variables
This removes some unnecessary returns and other miscellaneous cleanup.
2021-05-10 14:23:07 -07:00
ridiculousfish
e0bf23ad26 Refactor the named pipe uvar notifier with a state machine
This attempts to simplify the named pipe notifier by switching to a state
machine model.
2021-05-04 11:59:12 -07:00
ridiculousfish
7c5b8b8556 Use the uvar notifier pipe timestamp to avoid excessive polling
In the named pipe notifier, notifications are broadcast by writing to the
pipe, waiting briefly, and then reading it back. When clients see the pipe
as readable, they report the uvars as potentially changed and fish will
sync against the uvar file.

Prior to this change, we synced repeatedly when the pipe was readable. But
we can do somewhat better by also checking the named pipe's timestamp (via
fstat). If the pipe has not changed, then we can skip the sync even if
there is currently data lingering on it.

With this change we should sync against the variable file less often
(typically once or twice per write); in the next change we refactor this
logic so it's easier to follow.
2021-05-04 11:59:12 -07:00
ridiculousfish
3a093e3ce8 Add a flog category for universal variable notifiers
Make it easier to debug this stuff.
2021-05-04 11:59:12 -07:00
ridiculousfish
e8a61ef4aa Introduce select_wrapper_t
select_wrapper_t wraps up the annoying bits of using select(): keeping
track of the max fd, passing null for boring parameters, and
constructing the timeout. Introduce a wrapper struct for this and
replace the existing uses of select() with the wrapper.
2021-04-17 16:43:27 -07:00
ridiculousfish
73ec89917a Remove the SIGIO signal handler and universal notifier
If fish launches a program and that program marks stdin as O_ASYNC, then
fish will start receiving SIGIO events on Mac. This occurs even though
the file descriptor itself does not have the O_ASYNC flag set.

SIGIO is reported as interrupting select which then breaks multiple-key
bindings, especially in vi-mode.

As the SIGIO based universal notifier is disabled, remove it and the
SIGIO handler itself. This allows fish to ignore properly ignore SIGIO.

Fixes #7853
2021-04-03 18:11:29 -07:00
Fabian Homborg
791b42f065 Disable SIGIO notifier
It doesn't work on WSL, Solaris and Archlinux (and presumably that
means future versions of other linux distros).

In its current state I don't trust it enough to enable it anywhere by
default, especially since I'm not aware of an actual issue with the
named pipe (other than that the code is ugly).

Fixes #7774
2021-03-03 22:26:28 +01:00
Mahmoud Al-Qudsi
42d9f33e16 Use a platform whitelist for opting into the SIGIO notifier
My bet is that the Illumos, Cygwin, and WSL are not the only Unix-like
systems where the SIGIO notifier won't work, and since we have a good
enough and portable alternative that we can use be default on other
platforms where we don't specifically know it'll work, it doesn't make
sense not to go with that one instead.

Even if this patch is reverted at some point and we go back to
blacklisting platforms that *don't* support the SIGIO strategy, this is
almost certainly the right choice for inclusion in a minor release like
3.2.0.

See discussion in #6585.
2021-02-24 14:40:17 -06:00
Mahmoud Al-Qudsi
2cf5fd3d5d Avoid hard compilation errors on platforms w/out O_ASYNC
Those platforms should not be using the sigio notifier in the first
place, this just stops them from failing to be able to compile fish
altogether.

See #6585
2021-02-21 22:39:32 -06:00
Ilya Grigoriev
622f2868e1 Fix set -U when fish_variables is a symlink
Previously, `set -U` would overwrite the symlink with a
regular file.

Fixes https://github.com/fish-shell/fish-shell/issues/7466
2021-02-20 14:24:11 -08:00
ridiculousfish
89687e7db7 Fix a warning building on Linux
Initialize saved_errno
2021-01-09 13:14:54 -08:00
Mahmoud Al-Qudsi
640f4444f5 Disable SIGIO notifier on WSL
It currently does not trigger the uvar notifier and fails the automated
tests.

See #7429.
2020-11-06 20:49:44 -06:00
Rosen Penev
334be56021 run codebase through clang-tidy
Signed-off-by: Rosen Penev <rosenp@gmail.com>
2020-10-25 22:48:49 -07:00
ridiculousfish
a34b9036ba Enable SIGIO notifier on FreeBSD
Now that SIGIO works on FreeBSD, switch to that uvar notifier
2020-10-25 14:53:56 -07:00
ridiculousfish
8bb20a8d91 Remove use of POLL_IN in SIGIO uvar notifier
This fixes up the SIGIO notifier in preparation for using it on BSD. It
removes the reliance on the signal's si_code, which is not available in
BSD, and it properly handles the BSD behavior where SIGIO is delivered on
a read even if the read returns EAGAIN.
2020-10-25 14:52:37 -07:00
Fabian Homborg
5abc6060a4 Disable sigio notifier on FreeBSD
This fails for unknown reasons.

Not looking like a great *replacement* for the named pipe now, tbh.

See #7400.
2020-10-14 20:40:15 +02:00
Fabian Homborg
e949b1de02 ifdef SIGIO handling
This relies on POLL_IN which apparently isn't a thing on OpenBSD
2020-10-06 17:34:50 +02:00
ridiculousfish
f9e426813c Do not complain about fcntl(F_SETOWN) failing
On WSL1, fcntl(F_SETOWN) will fail and this would report an error.
Suppress this error message since it is not very interesting.
The effect is to disable real-time universal variable propagation.
2020-10-03 15:54:27 -07:00
ridiculousfish
558dd6e53d Add sigio-based universal notifier strategy
Introduce a new strategy for notifying other fish processes of universal
variable changes, as a planned replacement for the complex
strategy_named_pipe. The new strategy still uses a named pipe, but instead
of select() on it, it arranges for SIGIO to be delivered when data is
available. If a SIGIO has been seen since the last check, it means the file
needs to be re-read.
2020-10-01 13:19:41 -07:00
ridiculousfish
700fe4f131 Moderize universal variable notifiers
Use some C++11 features.
2020-10-01 13:27:13 -07:00