This reverts commit e2a3dae58b.
This idea failed because ./share was not complete when bliding via cmake;
it misses critical files such as config.fish.
fish tries to be relocatable by looking for directories relative to its
executable. These directories are not found when running fish from
within a cmake build because the etc directory is not present. Stop requiring
this directory to be present since it's not critical for running fish.
Fixes#4825
Don't mmap history files on remote file systems
This merges some changes to history that may help to mitigate the crashes seen in #5088 . These SIGBUS crashes occur when reading a memory mapping whose underlying file was truncated. It's not clear why this should occur more often on NFS (or ever). However memory mapping over NFS is sketchy anyways so this is desirable regardless.
Migrate the mmap() logic into a new class history_file_contents_t which
will serve to encapsulate conditional logic if we choose to use read()
instead of mmap().
This adds a new string command split0, which splits on zero bytes.
split0 has superpowers because its output is not further split on
newlines when used in command substitutions.
separated_buffer_t encapsulates the logic around discarding (which
was previously duplicated between output_stream_t and io_buffer_t),
and will also encapsulate the logic around explicitly separated
output.
If just one of the range ends is negative, this now forces direction away from it.
I.e. if the beginning is negative, we go in reverse.
If the end is negative, we go forwards.
This fixes cases like
$var[2..-1]
if $var only has one element.
While supported by gcc and clang, \e is a gcc-specific extension and not
formally defined in the C or C++ standards.
See [0] for a list of valid escapes.
[0]: https://stackoverflow.com/a/10220539/17027
We've tried numerous approaches to mitigate the race condition between
`posix_spawn` and the `setpgid` call, but unfortunately due to the flags
we pass to `posix_spawn`, it (rarely? never?) results in `vfork()` being
used, which means it is never executed atomically. Since it is executed
out-of-band, we must manually call `setpgid` in case `posix_spawn`
hasn't gotten around to doing that yet, but in the event that it has, an
EACCES error can be returned.
Closes#4884. Closes#4715. See also #4778.
On systems where the terminfo for TERM does not contain a string for
attributes such as enter_underline_mode, etc. fish was crashing with a
fatal error message and a note to email the developers.
These are non-essential text attribute changes and should not trigger
such a failure.
[9/13] Building CXX object CMakeFiles/fishlib.dir/src/builtin_string.cpp.o
../src/builtin_string.cpp:1221:12: warning: mangled name of 'string_transform' will change in C++17 due to non-throwing exception specification in function signature [-Wc++17-compat-mangling]
static int string_transform(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv, decltype(std::towlower) func) {
^
1 warning generated.
Our completion machinery calls our `__fish_describe_command` function
to describe commands via apropos. Only it trusts the output a bit too
much, so it crashes when any line from that is shorter than the
original string.
Fix this by skipping any string that is shorter than the original,
since it can't be a match anyway.
Also stop doing wcslen so often - std::strings are nice!
Fixes#5014.
There really is no need to
- Timeout just because the _first_ character was a control character
- Timeout because of any control character other than escape
The reason to timeout because the '\e' sequence can appear by itself (signifying
pressing the escape key) and still make
sense - e.g. vi-mode has it bound to a rather important function!
But a \c can't appear by itself, so we can just block.
This allows binding sequences like \cx\ce and inputting them at a
leisurely pace rather than the frantic escape_timeout one.
It should also improve sequences that _include_ escape somewhere else.
E.g. something like a\eb ("a, then alt+b") should now time out for the "\eb" part,
allowing users to bind a\e ("a, then escape") to something else. Why you'd want to do
that, I have no idea. But it's more consistent, and that's nice!
For regex-mode, this should be enough to read NUL-delimited strings to act on, but not
quite patterns and replacements.
Glob-mode requires more work - it uses wcscmp internally, which is unsuitable.
Also the various styles have one function each with barely any
difference - mostly passing the corresponding STYLE argument.
Pack them into one function for escape and one for unescape to save
about 100 lines.
We're now actually handling wchar_t here, so comparing the 0x80 bit
would break for UTF-16, causing ASCII false-positives.
Also simplifies a bit, since we no longer need a second variable.
printf 'a\0b' | string length
used to print "1". Now it prints "3".
Note that this switches to using C++'s std::string::length, which
might give differing results.
The prompt is a fallback that is overridden via a function file
anyway.
Do that with the title as well, so we can use just builtins.
This removes error messages when $fish_function_path is borked.
Introduced by #4849 (add wait for processes by name)
../src/builtin_wait.cpp:23:14: warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
while (j = jobs.next()) {
~~^~~~~~~~~~~~~
../src/builtin_wait.cpp:23:14: note: place parentheses around the assignment to silence this warning
while (j = jobs.next()) {
^
( )
../src/builtin_wait.cpp:23:14: note: use '==' to turn this assignment into an equality comparison
while (j = jobs.next()) {
^
==
1 warning generated.
Turns out the segfaults we've been getting in our tests are because we set $TERM to "dumb".
So we only clear the line if the terminal isn't dumb.
This reverts commit 745a88f2f6.
Fixes#2320.
One key use of process expansion, used in currently-shipped code, is for running a function on
current shell exit.
Restore the use of %self as a valid argument (and add `self`) and document this change.
(faho: Remove bare "self")
This enables users to opt in (or out) of specific features by setting
the fish_features environment variable.
For example `set -U fish_features stderr-nocaret` to opt into removing the
caret redirection.
This partially reverts 5b489ca30f, with
carets acting as redirections unless the stderr-nocaret flag is set.
This flag is off by default but may be enabled on the command line:
fish --features stderr-nocaret
This introduces a new command line option --features which can be used for
enabling or disabling features for a particular fish session.
Examples:
fish --features stderr-nocaret
fish --features 3.0,no-stderr-nocaret
fish --features all
Note that the feature set cannot be changed in an existing session.
This teaches the status command to work with features.
'status features' will show a table listing all known features and whether
they are currently on or off.
`status test-feature` will test an individual feature, setting the exit status to
0 if the feature is on, 1 if off, 2 if unknown.
This introduces a new type features_t that exposes feature flags. The intent
is to allow a deprecation/incremental adoption path. This is not a general
purpose configuration mechanism, but instead allows for compatibility during
the transition as features are added/removed.
Each feature has a user-presentable short name and a short description. Their
values are tracked in a struct features_t.
We start with one feature stderr_nocaret, but it's not hooked up yet.
This was done in share/config.fish, but leads to surprising results if
that isn't read - e.g. because someone just built fish in the git
directory to test it without installing.
It's also not something that is any more or less complicated.
For compatibility, keep it in config.fish as well for the time being.
complete.cpp strips the path from commands before parsing for
completions, meaning that when we called `path_get_path()` against
`cmd`, if `./cmd` were typed in at the command line but `cmd` does not
exist in the PATH, then the command would incorrectly be flagged as not
present and the completions would be skipped.
This is also faster when an absolute/relative path is used for a
command, as we now search with the original path which skips searching
PATH directories unnecessarily.
Found when debugging why completions for `./configure` wouldn't work.
This brings back expansion of `%n` where `n` is a job id, but not as a
general parser syntax. This makes `jobs -p %n` work, which can be used
as part of the job control command chain, i.e.
```
cat &
fg (jobs -p %1)
```
fg/bg/wait can either be wrapped in a function to call `jobs -p` for
`%n` arguments, or they can be updated to take `%n` arguments
themselves.
The order of this list does not need to be strictly maintained any
longer.
Benchmarked with `hyperfine` as follows, where `bench1` is the existing
approach of binary search and `bench2` is the new unordered_set code,
(executed under bash because fish would always return non-zero). The
benchmark code checks each argv to see if it is a builtin keyword (both
return the same result):
```
hyperfine './bench1 $(shuf /usr/share/dict/words)' './bench2 $(shuf /usr/share/dict/words)'
Benchmark #1: ./bench1 $(shuf /usr/share/dict/words)
Time (mean ± σ): 68.4 ms ± 3.0 ms [User: 28.8 ms, System: 38.9 ms]
Range (min … max): 60.4 ms … 75.4 ms
Benchmark #2: ./bench2 $(shuf /usr/share/dict/words)
Time (mean ± σ): 61.4 ms ± 2.3 ms [User: 23.1 ms, System: 39.8 ms]
Range (min … max): 58.1 ms … 67.1 ms
Summary
'./bench2 $(shuf /usr/share/dict/words)' ran
1.11x faster than './bench1 $(shuf /usr/share/dict/words)'
```
Prior to this fix, the fish universal variables file claimed that
changes to it would be overwritten. This no longer true and has not
been true for a long time. Remove that warning.
This switches the universal variables file from a machine-specific
name to the fixed '.config/fish/fish_universal_variables'. The old file
name is migrated if necessary.
Fixes#1912
This removes the caret as a shorthand for redirecting stderr.
Note that stderr may be redirected to a file via 2>/some/path...
and may be redirected with a pipe via 2>|.
Fixes#4394
The previous commit caused the tests to fail since env_remove() was
returning a blanket `!0` when a variable couldn't be unset because it
didn't exist in the first place. This caused the wrong message to be
emitted since the code clashed with a return code for `env_set()`.
Added `ENV_NOT_FOUND` to signify that the variable requested unset
didn't exist in the first place, but _not_ printing the error message
currently so as not to break existing behavior before checking if this
is something we want.
Variables set in if and while conditions are in the enclosing block, not
the if/while statement block. For example:
if set -l var (somecommand) ; end
echo $var
will now work as expected.
Fixes#4820. Fixes#1212.
Currently, there are two possibilities for holes in the background:
- When there are two candidates with the same meaning (a long and a
short option or two candidates with the same description)
- When a candidate does not have a description (meaning the color
won't continue after it)
This changes both so the background just goes on.
In addition, it avoids making the background multiple times.
Fixes#4866.
The official fish documentation makes no mention of how `string split`
treats empty tokens, e.g. splitting 'key1##key2' on '#' or (more
confusingly) splitting '/path' on '/'. With this commit, `string split`
now has an option to exclude zero-length substrings from the resulting
array with a new `--no-empty/-n`. The default behavior of preserving
empty entries is kept so as to avoid breakage.
The two unicode glyphs used to represent missing new lines and redacted
characters for secure entry are both not present in the glyph tables of
the default font under Windows (Consolas and Lucida Console), use an
alternative glyph instead.
The "return" symbol is replaced with a pilcrow (¶) and the "redacted
character" symbol is replaced with a bullet (•). Both of these are
well-defined in almost all fonts as they're very old symbols. This
change only takes place if -DWSL is supplied by the build toolchain.
Note: this means a Windows SSH client connecting to a fish remote
instance on a non-Windows machine will still use the (unavailable)
default glyphs instead.
The newly added `:` command is implemented as a function (to avoid
increasing complexity by making it a builtin), but it is saved to a path
that does not match its filename (since its name is somewhat of a
special character that might cause problems during installation).
Directly probing the `colon` function for autoload causes `:` to be
correctly loaded, so doing just that after function paths are loaded
upon startup.
This is a hack since the CPP code shouldn't really be aware of
individual functions, perhaps there is a better way of doing this.
Fixes an issue introduced in 4414d5c888
where functions loaded from custom directories are not detected as being
valid for purposes of determining whether or not completions should be
called.
Work around #4810 by retrieving localizations at runtime to avoid issues
possibly caused by inserting into the static unordered_map during static
initialization.
Closes#810.
Line continuations (i.e. escaped new lines) now make sense again. With
the smart pipe support (pipes continue on to next line) recently added,
this hack to have continuations ignore comments makes no sense.
This is valid code:
```fish
echo hello |
# comment here
tr -d 'l'
```
this isn't:
```fish
echo hello | \
# comment here
tr -d 'l'
```
Reverts @snnw's 318daaffb2Closes#2928. Closes#2929.
From the discussion in #3802, handling spaces within braces more
gracefully. Leading and trailing whitespace that isn't quoted or escaped
is stripped, whitespace in the middle is preserved. Any whitespace
encountered within expansion tokens is treated as a single space,
similar to how programming languages that don't hard break tokens/quotes
on line endings would.
The value is not electrified or tied and is read-only. It isn't cached
in the get_hostname_identifier() function as the ENV_GLOBAL $hostname
will cache it for its duration.
The behavior of `gethostname` in case of an insufficient buffer is
library and version dependent. Work around this by using a big enough
buffer then truncating the output to our desired max length.
The 0th index of the array was tested inside the loop instead of just
once outside it.
Also explain `input_mapping_is_match` control code behavior and
reasoning and simplify control flow.
Drops the % notation for process expansion. The existing notation was a
mess and expanded jobs, process ids, and process names via dark magic.
With this change, % is no longer a special character and can be used
unescaped with impunity.
The variables %self and %last, referring to fish's own pid and the pid
of the last backgrounded job respectively, have been replaced with $pid
and $last_pid. These are read-only variables, protected against being
redefined by the user.
Author's note: I would have personally preferred $fish_pid instead of
$pid but since we debated changing $version to $fish_version and then
reverted that change (with much acrimony), it makes no sense to break
with that precedent here. Additionally, $fish_last_pid is quite wordy.
Closes#4230. Closes#1202.
When number is infinite, not a number, larger than LONG_MAX or smaller
than LONG_MIN, print a corresponding error and return STATUS_CMD_ERROR.
This should fix the worst of the problems, by at least making them clear.
Fixes#4479.
Fixes#4768.
This allows prompts to react to $COLUMNS by e.g. omitting some parts.
We still fallback to a ">" prompt if that's still not short enough,
but now the user has a way of making a nicer prompt.
Fixes#904.
Fixes#4381.
If the head is not a valid, existent command, do not load and run custom
completion sources. This applies to both the autosuggestion provider and
manual user completions. File-based completions will still be offered.
Supersedes #4782 and #4783. Closes#4783. Closes#4782. Closes#2365.
This promotes "and" and "or" from a type of statement to "job
decorators," as a possible prefix on a job. The point is to rationalize
how they interact with && and ||.
In the new world 'and' and 'or' apply to a entire job conjunction, i.e.
they have "lower precedence." Example:
if [ $age -ge 0 ] && [ $age -le 18 ]
or [ $age -ge 75 ] && [ $age -le 100 ]
echo "Child or senior"
end
This should speed things up on slower PCs given that the vast majority
of shell commands are simple jobs consisting of a single command without
any pipelines, in which case there's no need for a keepalive process at
all. Applies to WSL only.
As a temporary workaround for the behavior described in
Microsoft/WSL#2997 wherein WSL does not correctly assign the spawned
child its own PID as its PGID, explicitly set the PGID for the newly
spawned process.
This now reports "TOO_MANY_ARGS" instead of no error (and triggering
an assertion).
We might want to add a new error type or report the missing operator
before, but this is okay for now.
This turns a bunch of ifs on their heads.
We often see this pattern in te:
```c
if (s->type != SOME_TYPE) {
// error handling
} else {
// normal code
}
```
Only, since we want to return the first error, we do
```c
if (s->type == SOME_TYPE) {
// normal code
} else if (s->type != TOK_ERROR) {
// Add a new error - if it already has type error
// this should already be handled.
}
```
One big issue is the comma operator, that means arity-1 functions can
take an arbitrary number of arguments. E.g.
```fish
math "sin(5,9)"
```
will return the value of sin for _9_, since this is read as "5 COMMA
9".
This enables some limited use of arguments for wrapping completions. The
simplest example is that complete gco -w 'git checkout' now works like
you would want: `gco <tab>` now invokes git's completions with the
`checkout` argument prepended.
Fixes#1976
Previously, in
ls ^a bcd
(with "^" as the cursor), kill-word would delete the "a" and then go
on, remove the space and the "bcd".
With this, it will only kill the "a".
Fixes#4747.
This is part of an effort to improve fish's Unicode handling. This commit
attempts to grapple with the fact that, certain characters (principally
emoji) were considered to have a wcwidth of 1 in Unicode 8, but a width of
2 in Unicode 9.
The system wcwidth() here cannot be trusted; terminal emulators do not
respect it. iTerm2 even allows this to be set in preferences.
This commit introduces a new function is_width_2_in_Uni9_but_1_in_Uni8() to
detect characters of version-ambiguous width. For these characters, it
returns a width guessed based on the value of TERM_PROGRAM and
TERM_VERSION, defaulting to 1. This value can be overridden by setting the
value of a new variable fish_emoji_width (presumably either to 1 or 2).
Fixes#4539, #2652.
`argparse`, `read`, `set`, `status`, `test` and `[` now can't be used
as function names anymore.
This is because (except for `test` and `[`) there is no way to wrap these properly, so any
function called that will be broken anyway.
For `test` (and `[`), there is nothing that can be added and there
have been confused users who created a function that then broke
everything.
Fixes#3000.
Prior to this fix, each redirection type was a separate token_type.
Unify these under a single type TOK_REDIRECT and break the redirection
type out into a new sub-type redirection_type_t.
Prior to this the tokenizer ran "one ahead", where tokenizer_t::next()
would in fact return the last-parsed token. Switch to parsing on demand
instead of running one ahead; this is simpler and prepares for tokenizer
changes.
Turns out the process-exit is only ever used in conjunction with
`%self`. Make that explicit by just adding a new "fish_exit" event,
and deprecate the general process-exit machinery.
Fixes#4700.
The previous attempt to support newlines after pipes changed the lexer to
swallow newlines after encountering a pipe. This has two problems that are
difficult to fix:
1. comments cannot be placed after the pipe
2. fish_indent won't know about the newlines, so it will erase them
Address these problems by removing the lexer behavior, and replacing it
with a new parser symbol "optional_newlines" allowing the newlines to be
reflected directly in the fish grammar.
Prior to this fix, if you attempt to complete from inside a quote and the
completion contained an entity that cannot be represented inside quotes
(i.e. \n \r \t \b), the result would be a broken mess of quotes. Rewrite
the implementation so that it exits the quotes, emits the correct unquoted
escape, and then re-enters the quotes.
Properly escape literal tildes in tab completion results. Currently we
always escape tildes in unquoted arguments; in the future we may escape
only leading tildes.
Fixes#2274
Prior to this fix, autoloads like function and completion autoloads
would check their path variable (like fish_function_path) on every
autoload request. Switch to invalidating it in response to the variable
changing.
This improves time on a microbenchmark:
for i in (seq 50000)
setenv test_env val$i
end
from ~11 seconds to ~6.5 seconds.
The job control functions were a bit messy, in particular
`set_child_group`'s name would imply that all it does is set the child
group, but in reality it used to set the child group (via `setpgid`),
set the job's pgrp if it hasn't been set, and possibly assign control of
the terminal to the newly-created job.
These have been split into separate functions. Now `set_child_group`
does just (and only) that, `maybe_assign_terminal` might assign the
terminal to the new pgrp, and `on_process_created` is used to set the
job properties the first time an external process is created. This might
also speed things up (but probably not noticeably) as there are no more
repeated calls to `getpgrp()` if JOB_CONTROL is not set.
Additionally, this closes#4715 by no longer unconditionally calling
`setpgid` on all new processes, including those created by `posix_spawn`
which does not need this since the child's pgrep is set at in the
arguments to that API call.
This switches function execution from the function's source code to
its stored node and pstree. This means we no longer have to re-parse
the function every time we execute it.
The idea is that we can return the shared pointer directly, avoiding
lots of annoying little getter functions that each need to take locks.
It also helps to pull together the data structures used to initialize
functions versus store them.
This concerns block nodes with redirections, like
begin ... end | grep ...
Prior to this fix, we passed in a pointer to the node. Switch to passing
in the tnode and parsed source ref. This improves type safety and better
aligns with the function-node plans.
Prior to this fix, functions stored a string representation of their
contents. Switch them to storing a parsed source reference and the
tnode of the contents. This is part of an effort to avoid reparsing
a function's contents every time it executes.
Add a fish-specific wrapper around std::mutex that records whether it is
locked in a bool. This is to make ASSERT_IS_LOCKED() simpler (it can just
check the boolean instead of relying on try_lock) which will make Coverity
Scan happier.
Some details: Coverity Scan was complaining about an apparent double-unlock
because it's unaware of the semantics of try_lock(). Specifically fish
asserts that a lock is locked by asserting that try_lock fails; if it
succeeds fish prints an error and then unlocks the lock (so as not to leave
it locked). This unlock is of course correct, but it confused Coverity Scan.
Use wcstring/string instead of a character array. The variable
`term_env` was not being freed before the function exited.
Fixes defect 7520324 in coverity scan.
keepalive processes are typically killed by the main shell process.
However if the main shell exits the keepalive may linger. In WSL
keepalives are used more often, and the lingering keepalives are both
leaks and prevent the tests from finishing.
Have keepalives poll for their parent process ID and exit when it
changes, so they can clean themselves up. The polling frequency can be
low.
Have WSL use a keepalive whenever the first process is external.
This works around the fact that WSL prohibits setting an exited
process as the group leader.
When the pager wants to use the full screen to show many options, it reserves
space at the top to see the command. Previously it pretended the command was a
prompt and engaged the prompt layout mechanism to compute these lines. Instead
let's juts count newlines since escape sequences within commands are very rare.
There were several issues with the way that the include tests for curses.h
were being done that were ultimately causing fish to use the headers from
ncurses but link against curses on platforms that provide an actual
libcurses.so that isn't just a symlink to libncurses.so
In particular, the old code was first testing for curses's cureses.h and then
falling back to libncurses's implementation of the same - but that logic was
reversed when it came to including term.h, in which case it was testing for
the ncurses term.h and falling back to the curses.h header. Long story short,
while cmake will link against libcurses.so if both libcurses.so and
libncurses.so are present (unless CURSES_NEED_NCURSES evaluates to TRUE, but
that makes ncurses a hard requirement), but we were brining in some of the
defines from the ncurses headers, causing SIGSEGV panics when fish ultimately
tried to access variables that weren't exported or were mapped to undefined
areas of memory in the other library.
Additionally it is an error to include termios.h prior to including the plain
Jane curses.h (not ncurses/curses.h), causing errors about unimplemented types
SGTTY/chtype. So far as I can tell, both curses.h and ncurses/curses.h pull in
termios.h themselves so it shouldn't even be necessary to manually include it,
but I have just moved its #include below that of curses.h
This special cases expansion of $history variables, so that slicing
history no longer needs to construct the entire history array. Speedup
is around 100x in my test.
Fixes#4650
Prior to this fix, if the user typed normal characters while the
completion pager was shown, it would begin searching. This feature was
not well liked, so we are going to instead just append the characters as
normal and disable paging. Control-S can be used to toggle the search
field.
Fixes#2249
try_get_child() was taking the address of a reference; clang was thereby
assuming it could not be null and so was dropping the null check. Ensure
we do not dereference a null pointer.
Fixes#4678
This was a symbol that represented either an argument or a redirection.
This was only used as part of argument_or_redirection_list.
It's simpler to just have these types be alternatives in the list type.