Commit Graph

3988 Commits

Author SHA1 Message Date
Fabian Homborg
ac7e3a53c1 screen: Allow for doubled escapes in the tmux sequence
See
https://github.com/tmux/tmux/wiki/FAQ#what-is-the-passthrough-escape-sequence-and-how-do-i-use-it.
2021-08-30 18:11:34 +02:00
Fabian Homborg
c02cc110e0 screen: Support tmux escape sequences
Tmux has support for wrapping arbitrary escape sequences inside

```
\ePtmux;\e%s\e\\
```

Since this ends like the screen title escape, we just reuse that.

Characteristically, this is basically undocumented, but we already use
it in e.g. fish_vi_cursor.
2021-08-30 16:55:14 +02:00
Mahmoud Al-Qudsi
069d396ebc Merge branch 'ctest' 2021-08-29 13:03:16 -05:00
Rosen Penev
e62e6c35f7 add constexpr
This was not const before.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2021-08-29 19:14:27 +02:00
Mahmoud Al-Qudsi
c82d1980bf Prevent same history test from running multiple times at once
The default matching logic for fish_tests was prefix based, so when we
were running `history` we were also running all history tests. This
causes the test to fail for an unknown reason.
2021-08-29 08:56:12 +02:00
Mahmoud Al-Qudsi
aaac759d9a Make test a custom target again and add top-level test targets
Even though we are using CMake's ctest for testing, we still define our
own `make test` target rather than use its default for many reasons:

 * CMake doesn't run tests in-proc or even add each tests as an
   individual node in the ninja dependency tree, instead it just bundles
   all tests into a target called `test` that always just shells out to
   `ctest`, so there are no build-related benefits to not doing that
   ourselves.
 * CMake devs insist that it is appropriate for `make test` to never
   depend on `make all`, i.e. running `make test` does not require any
   of the binaries to be built before testing.
 * The only way to have a test depend on a binary is to add a fake test
   with a name like "build_fish" that executes CMake recursively to
   build the `fish` target.
 * It is not possible to set top-level CTest options/settings such as
   CTEST_PARALLEL_LEVEL from within the CMake configuration file.
 * Circling back to the point about individual tests not being actual
   Makefile targets, CMake does not offer any way to execute a named
   test via the `make`/`ninja`/whatever interface; the only way to
   manually invoke test `foo` is to to manually run `ctest` and specify
   a regex matching `foo` as an argument, e.g. `ctest -R ^foo$`... which
   is really crazy.

With this patch, it is now possible to execute any single test by name,
by invoking the build directly, e.g. to run the `universal.fish` check:
`cmake --build build --target universal.fish` or
`ninja -C build universal.fish`. Unfortunately, this is not integrated
into the Makefile wrapper, so `make universal.fish` won't work (although
this can potentially be hacked around).
2021-08-29 08:56:12 +02:00
Mahmoud Al-Qudsi
26092456d4 Add CMake enumeration of low-level fish_tests.cpp tests
Instead of compiling `fish_tests.cpp` dynamically with weakly-linked
symbols and asking it to print the list of all available tests, we
use a magic string `#define`'d as a no-op to allow CMake to regex search
for matching test groups. This speeds up configuration somewhat (by not
compiling anything), but more importantly, it's much less brittle and
doesn't involve and linker dark magic.

There's of course still no getting around the fact that it's really ugly.
2021-08-29 08:56:12 +02:00
Fabian Homborg
4c3cb119fc Actually return when reading file failed
Fixes #8258.
2021-08-29 08:36:54 +02:00
ridiculousfish
7a1c005b42 Switch to using timef instead of gettimeofday
This encapsulates the tricky arithmetic inside timef(), which uses
gettimeofday.
2021-08-27 16:25:33 -07:00
ridiculousfish
7d537eefbb proc_get_jiffies to accept pid directly
No need to accept the mutable proc here.
2021-08-27 13:05:27 -07:00
ridiculousfish
f577c221eb Introduce get_by_sorted_name
Given that we have several lists of things sorted by name, replace a
bunch of ad-hoc lower_bound calls with a single function.
2021-08-26 13:40:37 -07:00
Fabian Homborg
ee2d2caeaa escape_code_length: Test colors last
We have a *lot* of color sequences to try and tparm is slow (on the
whole, when you do this thousands of times).

So let's just check colors last, which makes everything else (which is
comparatively nothing) faster, while barely impacting
colors (benchmarking confirms no measurable difference).

Fixes #8253.
2021-08-26 21:01:55 +02:00
Rosen Penev
a36a26c28b clang-tidy: replace NULL with nullptr
Signed-off-by: Rosen Penev <rosenp@gmail.com>
2021-08-25 16:10:17 -07:00
Rosen Penev
ffd5716e70 clang-tidy: replace push_back with emplace_back
clang-tidy marks these as needing emplace_back as the types to not
match.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2021-08-25 03:19:46 +02:00
Rosen Penev
a9b4127f68 clang-tidy: run through normal checks
There's a .clang-tidy file in here.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2021-08-21 21:59:05 +02:00
Fabian Homborg
70e3e0beac Also remove ephemeral item if command is effectively empty
Fixes #8232.

Note that this needed to have expect_prompt used in the pexpect test -
we might want to add a "catchup" there so you can just ignore the
prompt counter for a bit and pick it back up later.
2021-08-20 19:38:16 +02:00
Rosen Penev
b748417af7 clang-tidy: replace size comparisons with empty
Signed-off-by: Rosen Penev <rosenp@gmail.com>
2021-08-20 18:32:45 +02:00
Rosen Penev
1af9e5d21e clang-tidy: simplify two bool returns
Signed-off-by: Rosen Penev <rosenp@gmail.com>
2021-08-20 18:32:15 +02:00
Fabian Homborg
d4f7e25584
Replace strerror/sys_errlist after fork with our own errors (#8234)
* Remove safe_strerror, safe_perror and safe_append

This no longer works on new glibcs because they removed sys_errlist.

So just hardcode the relevant errno messages (and phrase them better).

Fixes #4183.

Co-authored-by: Johannes Altmanninger <aclopte@gmail.com>
2021-08-20 17:17:01 +02:00
Rosen Penev
90f006b1cd clang-tidy: use delete
The clang warning for pending_signals_t was about the operator=
return type being wrong (misc-unconventional-assign-operator).

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2021-08-20 01:33:33 +02:00
Johannes Altmanninger
5de05a810c Tell clang-tidy that expander_t::stage_variables intentionally takes values
We don't want to convert the input to a "wcstring &" because
"stage_variables" needs to have the same type as other stages, so we
can use it in a loop. Communicate that to clang-tidy.

We also don't want to take "wcstring &&". As the Google style guide
states, it's not really beneficial here, and it potentially hurts
readability because it's a relatively obscure feature.
The rest of our code contains a bunch of && parameters.  We might
want to get rid of some of them.

Closes #8227
2021-08-20 01:21:21 +02:00
Rosen Penev
ffa3e0b4f4 convert const ref to value
clang-tidy wrongly sees an std::move to a const ref parameter and
believes it to be pointless. The copy constructor however is deleted.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2021-08-20 01:16:24 +02:00
Rosen Penev
4ea5189c4f clang-tidy: const reference conversions
These are only read from.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2021-08-20 01:15:48 +02:00
Rosen Penev
f9af33f223 clang-tidy: remove pointless virtual
override is already used.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2021-08-20 01:15:23 +02:00
Rosen Penev
faf51e0693 clang-tidy: use for range loops
Signed-off-by: Rosen Penev <rosenp@gmail.com>
2021-08-20 01:14:25 +02:00
ridiculousfish
2ca66cff53 Disable job control inside command substitutions
This disables job control inside command substitutions. Prior to this
change, a cmdsub might get its own process group. This caused it to fail
to cancel loops properly. For example:

    while true ; echo (sleep 5) ; end

could not be control-C cancelled, because the signal would go to sleep,
and so the loop would continue on. The simplest way to fix this is to
match other shells and not use job control in cmdsubs.

Related is #1362
2021-08-18 22:20:03 +08:00
Mahmoud Al-Qudsi
3291102045 Refactor deferred_process handling to be more clearly safe
The previous layout confused me for a minute as it suggested it was
possible for `pipe_next_read` to be moved twice (once in the first
conditional block, then again when the deferred process conditional
called `continue` - if and only if the deferred process *was* the last
process in the job. This patch clarifies that can't be the case.
2021-08-17 20:10:19 -05:00
Mahmoud Al-Qudsi
c014c23662 Fix undefined behavior in closing a moved pipe
`pipe_next_read` is moved in the body of the loop, and not
re-initialized the last go around. However, we call
`pipe_next_read.close()` after the loop, which is undefined behavior (as
it's been moved).

Best case scenario, the compiler passed the address of our copy of the
struct to `exec_process_in_job` and beyond, it went out of scope there,
the value of `fd` was set to closed (minus one), and we explicitly call
`.close()` again, in which case it does nothing.

Worst case scenario, the compiler re-uses the storage for the now-moved
struct for something else and our call to `.close()` ends up closing
some other value of `fd` (valid or invalid) and things break.

Aside from the fact that we obviously don't need to close it since it's
not assigned for the last process in the job, it's a RAII object so we
don't have to worry about manually closing it in the first place.
2021-08-17 19:52:15 -05:00
Mahmoud Al-Qudsi
57615504d0 Eliminate variable unused after refactor of wcstringutil.cpp 2021-08-17 19:23:13 -05:00
Mahmoud Al-Qudsi
426fa82f8f Fix recently broken escape_code_length() result
`escape_code_length()` was converted from returning a `size_t` to
returning a `maybe_t<size_t>` but that subtly broke all existing call
sites by forcing all input to go through the slow path of assuming a
zero-length escape sequence was found.

This is because all callers predicated their next action on what amounts
to `if (escape_code_length(...))` which would correctly skip the slow
path when `escape_code_length` returned zero, but after the conversion
to `maybe_t` contained not `maybe_t::none()` but rather
`maybe_t::some(0)` due to coercion of the result from the `size_t` local
`esc_seq_len` to the `maybe_t<size_t>` return value - which, when
coerced to a boolean returns *true* for `maybe_t::some(0)` rather than
false.

The regression was introduced in 7ad855a844
and did not ship in any released versions so no harm, no foul.
2021-08-17 19:04:10 -05:00
Mahmoud Al-Qudsi
daa366eb5a maybe.h: reference header new
This is required for the usage of placement new. Not an issue for fish
as it gets picked up from elsewhere, but it lets one use it in a C++
test directly this way.
2021-08-17 18:57:16 -05:00
Rosen Penev
ba91b39715 replace push_back with emplate_back
The latter forwards the arguments directly.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2021-08-17 18:56:19 -05:00
Fabian Homborg
e9ee1820d6 Default emoji width to 2 for iTerm
Hallelujah, they switched to Unicode 9.

See #8220.
2021-08-17 13:30:34 +02:00
Mahmoud Al-Qudsi
5326462116 Catch more zero-index expressions
This expands the sanity check for literal zero indexes that was not
updated when range expansions was introduced.

Closes #8213
2021-08-15 13:48:41 -05:00
Fabian Homborg
c4593828f4
commandline: Add --is-valid option (#8142)
* commandline: Add --is-valid option to query whether it's syntactically complete

This means querying when the commandline is in a state that it could
be executed. Because our `execute` bind function also inserts a
newline if it isn't.

One case that's not handled right now: `execute` also expands
abbreviations, those can technically make the commandline invalid
again.

Unfortunately we have no real way to *check* without doing the
replacement.

Also since abbreviations are only available in command position when
you _execute_ them the commandline will most likely be valid.

This is enough to make transient prompts work:

```fish
function reset-transient --on-event fish_postexec
    set -g TRANSIENT 0
end

function maybe_execute
    if commandline --is-valid
        set -g TRANSIENT 1
        commandline -f repaint
    else
        set -g TRANSIENT 0
    end
    commandline -f execute
end

bind \r maybe_execute
```

and then in `fish_prompt` react to $TRANSIENT being set to 1.
2021-08-14 11:29:22 +02:00
Fabian Homborg
eee38836cf set -q: Return 255 if no variable name was passed
Previously this strictly returned the number of unset variables. So if
no variable was given, it would return *true*, which is highly
suspect.
2021-08-14 10:55:21 +02:00
Fabian Homborg
b5e5732be1 Point wildcard error at a more specific help section
"Expansion" covers *all* the expansions, that's a bit of a handful.

Directly point people towards globbing.
2021-08-11 18:40:37 +02:00
ridiculousfish
fdf8f17397 Stop using thread local vectors
These don't build on macOS 10.9, and are unnecessary anyways.
Thread local variables should only be simple primitives.
2021-08-10 13:07:13 -07:00
Johannes Altmanninger
6bd25ff63a Reword comment 2021-08-10 21:01:39 +02:00
Rosen Penev
a00ebc65af remove make_pair
There are better alternatives with C++11.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2021-08-05 12:12:28 +02:00
Fabian Homborg
2087a3ca63 Let visible length work with CR and LF
Because we are, ultimately, interested in how many cells a string
occupies, we *have* to handle carriage return (`\r`) and line
feed (`\n`).

A carriage return sets the current tally to 0, and only the longest
tally is kept. The idea here is that the last position is the same as
the last position of the longest string. So:

abcdef\r123

ends up looking like

123def

which is the same width as abcdef, 6.

A line feed meanwhile means we flush the current tally and start a new
one. Every line is printed separately, even if it's given as one.

That's because, well, counting the width over multiple lines
doesn't *help*.

As a sidenote: This is necessarily imperfect, because, while we may
know the width of the terminal ($COLUMNS), we don't know the current
cursor position. So we can only give the width, and the user can then
figure something out on their own.

But for the common case of figuring out how wide the prompt is, this
should do.
2021-08-04 21:09:47 +02:00
Fabian Homborg
a05fc52fc8 Ignore second escape inside an escape code 2021-08-04 21:09:47 +02:00
Fabian Homborg
ca551fdeb9 string: Add length --visible for visible length
Without escapes.

The new option is a bit cheesy, but "width" isn't as expressive and
requires an argument.

Maybe we want "pad" to also require --visible?
2021-08-04 21:09:47 +02:00
Fabian Homborg
a4756ce561 string: Make pad pad to terminal width
This just changes it so it subtracts escape sequences, according to
the current terminal.
2021-08-04 21:09:47 +02:00
Fabian Homborg
7ad855a844 screen: Make escape_code_length public
Uncached, but we don't want to keep this globally, I think?

This is useful for doing string pad/length without escapes.
2021-08-04 21:09:47 +02:00
Fabian Homborg
0059192f61 Allow erasing vars via function-scope
This triggered an assert because the remove code had no idea how to
find the function scope.

Oops!
2021-08-04 17:55:41 +02:00
Fabian Homborg
733114fefb
Add set --function (#8145)
* Add `set --function`

This makes the function's scope available, even inside of blocks. Outside of blocks it's the toplevel local scope.

This removes the need to declare variables locally before use, and will probably end up being the main way variables get set.

E.g.:

```fish
set -l thing
if condition
    set thing one
else
    set thing two
end
```

could be written as

```fish
if condition
    set -f thing one
else
    set -f thing two
end
```

Note: Many scripts shipped with fish use workarounds like `and`/`or`
instead of `if`, so it isn't easy to find good examples.

Also, if there isn't an else-branch in that above, just with

```fish
if condition
    set -f thing one
end
```

that means something different from setting it before! Now, if
`condition` isn't true, it would use a global (or universal) variable of
te same name!

Some more interesting parts:

Because it *is* a local scope, setting a variable `-f` and
`-l` in the toplevel of a function ends up the same:

```fish
function foo2
    set -l foo bar
    set -f foo baz # modifies the *same* variable!
end
```

but setting it locally inside a block creates a new local variable
that shadows the function-scoped variable:

```fish
function foo3
    set -f foo bar
    begin
        set -l foo banana
        # $foo is banana
    end
    # $foo is bar again
end
```

This is how local variables already work. "Local" is actually "block-scoped".

Also `set --show` will only show the closest local scope, so it won't
show a shadowed function-level variable. Again, this is how local
variables already work, and could be done as a separate change.

As a fun tidbit, functions with --no-scope-shadowing can now use this to set variables in the calling function. That's probably okay given that it's already an escape hatch (but to be clear: if it turns out to problematic I reserve the right to remove it).

Fixes #565
2021-08-01 20:08:12 +02:00
Johannes Altmanninger
66709571ed fish_indent: handle tokens with trailing escaped newlines
Fixes #8197
2021-08-01 18:59:45 +02:00
Johannes Altmanninger
3a375c2399 reader: fix regressions when moving between lines
Fixes some regressions from 35ca42413 ("Simplify some parse_util functions").
The tmux tests are not beautiful but I find them easy to write.
Probably a pexpect test would also be enough here?
2021-08-01 17:50:44 +02:00
Fabian Homborg
06acc201f4 Disallow NULLs in function names and paths
These aren't compatible with unix semantics.

Fixes #8195 harder.
2021-08-01 12:23:31 +02:00
Fabian Homborg
0157ac35a4 Autoload: Ignore empty and effectively empty commands
Fixes #8195.
2021-08-01 12:16:46 +02:00
Mahmoud Al-Qudsi
97e514d7ff Use more consistent names for event_t function impls
The names in the implementation differed from those in the header, but
the header names were definitely better (because they correlated across
function calls).
2021-07-31 15:26:09 -05:00
Fabian Homborg
80888eed57 Remove read_only stuff from env_var_t
This doesn't work.

The real thing that tells if something is read-only is
electric_var_t::readonly().

This wasn't used, and we provide no way to make a variable read-only,
which makes this an unnecessary footgun.
2021-07-30 15:33:08 +02:00
Fabian Homborg
dd3cdbcfc9 Fix crash if $PWD is used as for-loop variable
for PWD in foo; true; end

prints:

>..src/parse_execution.cpp:461: end_execution_reason_t parse_execution_context_t::run_for_statement(const ast::for_header_t&, const ast::job_list_t&): Assertion `retval == ENV_OK' failed.

because this used the wrong way to see if something is read-only.
2021-07-30 15:33:04 +02:00
Fabian Homborg
55732f445a set: Use env_var_t::flags_for() to see if it's read-only
env_var_t::read_only() is basically broken.

It doesn't work for $PWD, as best as I can tell no variable is
read-only except for a hardcoded list of some of the electric ones.

So we should probably remove the entire read_only and
setting_read_only mechanism.
2021-07-30 15:32:58 +02:00
Fabian Homborg
bf1fd733d0 Revert "Extend the fast path of fish_wcstod"
This breaks in comma-using locales (like my own de_DE.UTF-8), because
it still uses the locale-dependent strtod, which will then refuse to
read

   1234.567

Using strtod_l (not in POSIX, I think?) might help, but might also be
a lot slower. Let's revert this for now and figure out if that is
workable.

This reverts commit fba86fb821.
2021-07-29 16:29:12 +02:00
ridiculousfish
fba86fb821 Extend the fast path of fish_wcstod
fish_wcstod had a "fast path" which looked for all digits, otherwise
falling back to wcstod_l. However we now pass the C locale to wcstod_l,
so it is safe to extend the fast path to all ASCII characters.

In practice math parsing would pass strings here like "123 + 456" and
the space and + were knocking us off the fast path. benchmarks/math.fish
goes from 2.3 to 1.4 seconds with this change.
2021-07-28 16:14:55 -07:00
ridiculousfish
32e23c84f4 Clean up parser_t::push_block
Fix some unnecessary copying and unused variables.
2021-07-28 15:37:34 -07:00
ridiculousfish
789261a40c Stop storing is_breakpoint inside the parser
This can also be trivially computed from the block list.
2021-07-28 13:56:33 -07:00
ridiculousfish
b914c94cc1 Stop storing 'is_block' inside the parser
is_block is a field which supports 'status is-block', and also controls
whether notifications get posted. However there is no reason to store
this as a distinct field since it is trivially computed from the block
list. Stop storing it. No functional changes in this commit.
2021-07-28 13:56:33 -07:00
Fabian Homborg
b3cdf4afe1 Hardcode $PWD as read-only for set --show
Through a mechanism I don't entirely understand, $PWD is sometimes
writable (so that `cd` can change it) and sometimes not.

In this case we ended up with it writable, which is wrong.

See #8179.
2021-07-28 22:13:22 +02:00
Fabian Homborg
3db78232c6 Show if a var is read-only with set --show
Fixes #8179.
2021-07-28 21:13:03 +02:00
Fabian Homborg
8939a71ec6 An empty string means we're on the first line
Oops, this broke up-or-search!
2021-07-27 20:11:32 +02:00
Fabian Homborg
48e696bbb4 Update commandline state before completion
Fixes #8175.
2021-07-27 19:03:35 +02:00
Fabian Homborg
35ca42413d Simplify some parse_util functions
Don't just reflexively drop down to wchar_t.
2021-07-27 18:39:56 +02:00
Fabian Homborg
29e9f4838a Run parse_util_detect_errors on -c commands
This didn't do all the syntax checks, so something like

    fish -c 'echo foo; and $status'

complained of a missing command `0` (i.e. $status), and

    fish -c 'echo foo | exec grep'

hit an assert!

So we do what read_ni does, parse each command into an ast, run
parse_util_detect_errors on it if it worked and then eval the ast.

It is possible to do this neater by modifying parser::eval, but I
can't find where.
2021-07-27 18:37:20 +02:00
Fabian Homborg
08209b3d9a Forbid $status as a command
This is slightly unclean. Even tho it would otherwise be syntactically
valid, using $status as a command is very very very likely to be an
error, like

    if not $status

We have reports of this surprisingly regularly, including #2773.

Because $status can only ever be a value from 0 to 255, it is also
very unlikely to be an actual command, and that command is very
unlikely to do what you want.

So we simply point the user towards the "conditions" help section,
that should explain things.
2021-07-27 18:37:20 +02:00
Fabian Homborg
b9ba3020f8 Don't check config directories with --no-config
If we don't use 'em, we should not complain about 'em.
2021-07-27 18:35:20 +02:00
Fabian Homborg
d32e1c12be tinyexpr: Check for nan in ncr
Turns out this takes ages.

Fixes #8170
2021-07-26 18:40:50 +02:00
Fabian Homborg
4bb1c72a91 Revert "Clear to eol before outputting line in multi-line prompt"
This means, if we repaint with a shorter prompt, we won't overwrite the longer parts.

This reintroduces #8002, but that's a much rarer usecase - having a prompt that fills the entire screen,
in certain terminals.

This reverts commit d3ceba107e.

Fixes #8163.
2021-07-24 09:28:39 +02:00
Johannes Altmanninger
a2b30053dc Teach fish_indent about our feature flags
So it can handle syntax changes that call for different formatting.
2021-07-23 22:58:51 +02:00
Johannes Altmanninger
cc32b4f2a7 Make '&' only background if followed by a separating character
This is opt-in through a new feature flag "ampersand-nobg-in-token".

When this flag and "qmark-noglob" are enabled, this command no longer
needs quoting:

	curl https://example.com/thing?foo=bar&duran=duran

Compared to the previous approach e1570a4 ("Let '&' only separate as
the first char of a word"), this has some advantages:

1. "&&" and "&>" are no longer affected. They are still special, even
   if used between tokens without spaces, like "echo bar&>foo".
   Maybe this is not really *better*, but it avoids risking to annoy
   users by breaking the old variant.

2. "&" is still special if at the end of a token, like in "sleep 1&".

Word movement is not affected by the semantics change, so Alt-F and
friends still stop at every "&".
2021-07-23 22:58:51 +02:00
ridiculousfish
938879a85a Remove a stale comment and add a missing initializer 2021-07-23 11:22:45 -07:00
ridiculousfish
5f7e03ccf4 Introduce noncopyable_t and nonmovable_t
These are little helper types that allow us to get rid of lots of
'=delete' declarations.
2021-07-23 11:19:42 -07:00
ridiculousfish
e9ff3f2e65 Remove a stale comment. 2021-07-23 11:19:42 -07:00
Fabian Homborg
859edc9c2c Implicitly use $PWD in $CDPATH in completions and highlighting
We already do for the actual cd-ing itself.

Missed in #4484.

Fixes #8161.
2021-07-23 17:22:06 +02:00
ridiculousfish
ce371e1881 Put back support for undocumented -I option to commandline
This allows operating on a user-specified commandline instead of the
true contents. This was inadvertently removed in a32248277f.
2021-07-21 15:35:22 -07:00
Fabian Homborg
3359e5d2e9
Let "return" exit a script (#8148)
Currently, if a "return" is given outside of a function, we'd just
throw an error.

That always struck me as a bit weird, given that scripts can also
return a value.

So simply let "return" outside also exit the script, kinda like "exit"
does.

However, unlike "exit" it doesn't quit an interactive shell - it seems
weird to have "return" do that as well. It sets $status, so it can be
used to quickly set that, in case you want to test something.
2021-07-21 22:33:39 +02:00
ridiculousfish
a32248277f Make commandline state thread safe
Today the reader exposes its internals directly, e.g. to the commandline
builtin. This is of course not thread safe. For example in concurrent
execution, running `commandline` twice in separate threads would cause a
race and likely a crash.

Fix this by factoring all the commandline state into a new type
'commandline_state_t'. Make it a singleton (there is only one command
line
after all) and protect it with a lock.

No user visible change here.
2021-07-21 11:51:46 -07:00
ridiculousfish
49c8ed9765 Migrate the fix for 6892 into reader itself
No functional change here; this migrates the fix ensuring that history
items are available in the builtin interactive read command into the
reader itself, in preparation for removing reader_get_history().
2021-07-21 11:51:46 -07:00
Evan Miller
c4773d4052 Work around setpgid error on older Apple platforms
Expand the #7474 fix to Apple platforms. The issue affects older
OS versions, including Mac OS X 10.4.11.
2021-07-20 17:27:10 +02:00
ridiculousfish
b0dc72ee78 Minor clean up of set_buffer_maintaining_pager 2021-07-18 13:18:55 -07:00
Johannes Altmanninger
52f7e83113 Complete revert of faulty polling workaround
62d8f7277 ("Revert "Avoid excessive polling of universal variable
file"") was not a complete revert, which seems to have caused the
problem reported in https://github.com/IlanCosman/tide/issues/171.

See #8088
2021-07-18 18:41:04 +02:00
ridiculousfish
8abc8315de Remove some more ASSERT_IS_MAIN_THREADs
These aren't helping and are blocking testing of concurrent execution.
No functional change here.
2021-07-17 12:20:54 -07:00
Fabian Homborg
e9a793532e Stop cd "" from crashing
Fixes #8147.
2021-07-17 19:03:15 +02:00
Fabian Homborg
f3f6e4a982 string: Add "--groups-only" to match
This adds a simple way of picking bits from a string that might be a
bit nicer than having to resort to a full `replace`.

Fixes #6056
2021-07-16 20:27:54 +02:00
ridiculousfish
f345464879 Simplify ASSERT_SORT_ORDER
In practice this only looked at the name property, so we can simplify it
by using an ordinary template function instead of a macro.
2021-07-15 13:15:24 -07:00
ridiculousfish
a638c4f01d const_strlen to be aware of interior nul chars
Prior to this change, const_strlen would only look for trailing nul
chars. Teach it about interior nul chars and add some tests.
2021-07-15 13:07:09 -07:00
ridiculousfish
ccd1b4e4f4 Clean up of get_function_name
Have it return a real string, instead of a pointer with uncertain
lifetime.
2021-07-15 11:36:43 -07:00
ridiculousfish
bad1b84513 Remove the index parameter from parser_t::is_function
It was always 0 in practice.
2021-07-15 11:04:06 -07:00
ridiculousfish
8bed818039 Remove some main thread assertions that are not helping
This is to make experimenting with concurrent execution easier.
No functional change in this commit.
2021-07-15 10:49:27 -07:00
ridiculousfish
6960a56f29 parse_util_locate_brackets_of_type to only find cmdsubs
Now that we have a separate function for parsing slices, we no longer
need to support parsing slices in the same function as cmdsubs.
2021-07-14 13:59:48 -07:00
ridiculousfish
52c354a60f Simplify slice parsing in highlighting
Factor out parsing of slices, which is only used for highlighting.
2021-07-14 13:59:48 -07:00
ridiculousfish
a2dfd87928 Simplify parse_util_get_parameter_info
We no longer use any part of the "parameter info" except its quote type.
Just return the quote type directly.
2021-07-14 13:59:48 -07:00
ridiculousfish
083d8c5d23 Minor cleanup of certain parsing and quote finding functions
This makes more variables const and removes some suspicious casts.
2021-07-14 13:59:46 -07:00
Johannes Altmanninger
0ab6735450 Support $(cmd) command substitution as alternative to (cmd)
For consistency with "$(cmd)" and with other shells.
2021-07-13 21:33:42 +02:00
Johannes Altmanninger
ec3d3a481b Support "$(cmd)" command substitution without line splitting
This adds a hack to the parser. Given a command

	echo "x$()y z"

we virtually insert double quotes before and after the command
substitution, so the command internally looks like

	echo "x"$()"y z"

This hack allows to reuse the existing logic for handling (recursive)
command substitutions.

This makes the quoting syntax more complex; external highlighters
should consider adding this if possible.

The upside (more Bash compatibility) seems worth it.

Closes #159
2021-07-13 21:33:42 +02:00
ridiculousfish
179073ce62 Clear the control-C cancel flag earlier, allowing event handlers to run
When the user presses control-C, fish marks a cancellation signal which
prevents fish script from running, allowing it to properly unwind.
Prior to this commit, the signal was cleared in the reader. However this
missed the case where a binding would set $fish_bind_mode which would
trigger event handlers: the event handlers would be skipped because of
the cancellation flag was still set. This is similar to #6937.

Let's clear the flag earlier, as soon as we it's set, in inputter_t.
Fixes #8125.
2021-07-11 18:04:44 -07:00
Fabian Homborg
0e1f5108ae
string: Allow collect --allow-empty to avoid empty ellision (#8054)
* string: Allow `collect --no-empty` to avoid empty ellision

Currently we still have that issue where

    test -n (thing | string collect)

can return true if `thing` doesn't print anything, because the
collected argument will still be removed.

So, what we do is allow `--no-empty` to be used, in which case we
print one empty argument.

This means

    test -n (thing | string collect -n)

can now be safely used.

"no-empty" isn't the best name for this flag, but string's design
really incentivizes reusing names, and it's not *terrible*.

* Switch to `--allow-empty`

`--no-empty` does the exact opposite for `string split` and split0.

Since `-a`/`--allow-empty` already exists, use it.
2021-07-09 21:20:58 +02:00
Jean Mertz
2575145682 Enable OSC 0 when running in WezTerm 2021-07-08 16:10:11 +02:00
ridiculousfish
b395b33776 Migrate remaining calls from debug_safe to FLOGF_SAFE
This removes debug_level and remaining debug bits.

We also simplify some of the exec errors, reducing them to a single
line.
2021-07-05 15:47:56 -07:00
ridiculousfish
28bf44d698 Add async-safe flog support
This allows using flog in a limited way after calling fork, or from
signal handlers.
2021-07-05 14:56:38 -07:00
Johannes Altmanninger
62d8f7277b Revert "Avoid excessive polling of universal variable file"
This reverts commit b56b230076.
which somehow made us miss repaints on uvar notifications.

The commit was a workaround for a polling bug which was later properly
fixed by 7c5b8b855 ("Use the uvar notifier pipe timestamp to avoid
excessive polling"), so it's no longer necessary.

Add a system test. If I had a better understanding of the bug I could
probably write a better test.

Fixes #8088
2021-07-03 14:31:37 +02:00
Johannes Altmanninger
874fc439dd Remove stale path validation logic
We used to warn about PATH and CDPATH that are not valid directories,
but only if they contain colons.
However, the warning was a false positive because we would split
those values by colons anyway. So there is nothing left we want to
warn about.

Fixes #8095
2021-07-03 08:45:47 +02:00
Evan Miller
23518e7ad8 FISH_USE_POSIX_SPAWN and HAVE_SPAWN_H fixes
FISH_USE_POSIX_SPAWN is always defined, thanks to the line

   #define FISH_USE_POSIX_SPAWN HAVE_SPAWN_H

So replace #ifdef with #if to fix compilation on platforms lacking
spawn.h. Also make the spawn.h inclusion condition consistent across
files.
2021-07-01 14:34:30 +02:00
Mahmoud Al-Qudsi
ba44c4242f Fix incorrect comparison of function pointers
The sort routine was using the address of the **function pointer**
`signal(int signal)` rather than the union payload of the same name.

Perhaps one of the two should be renamed.
2021-06-28 18:06:04 -05:00
ridiculousfish
8ddbf85768 Correct a suspicious assigning of a value to a reference 2021-06-26 20:06:40 -07:00
Johannes Altmanninger
48c1550f61 Point to builtins begin/end when a failed command starts with "{"
Closes #6415
2021-06-23 21:47:40 +02:00
Johannes Altmanninger
565a7e4bc5 Minor refactoring to use early return in "handle_command_not_found" 2021-06-23 21:47:40 +02:00
Johannes Altmanninger
be0b451207 commandline: allow to get/set cursor position relative to token/process/job
With a command line like

	a | b <cursor> | c

 "commandline -C 0 --current-process" will place the cursor just left of "b".
2021-06-23 20:51:20 +02:00
Johannes Altmanninger
016569905e commandline: move handling of cursor option
No behavior change intended.
Will use the "buffer_part" computation in the next commit.
2021-06-23 20:51:20 +02:00
ridiculousfish
3b4c71c546 Catch invalid function names in highlighting and autosuggestion
Prior to this change, if you were to type `./fish_indent` it woul dbe
colored as valid, because the path
`$fish_functions_path/./fish_indent.fish` is a real file. However of
course this is not actually executed as a function. Teach
function_exists to return false for function names which are invalid.
2021-06-22 12:37:45 -07:00
Johannes Altmanninger
2bc8057780 Remove redundant parens 2021-06-15 03:27:55 +02:00
Johannes Altmanninger
4908f9bb40 Remove spurious character escape 2021-06-15 01:19:01 +02:00
ridiculousfish
1c20bdcbf9 Minor improvements to file_id_t 2021-06-13 17:20:04 -07:00
ridiculousfish
ba2e7db7e8 Notice when exit has been run from within fish_prompt
This allows `exit` to tell the reader to stop, when run inside
fish_prompt. Fixes #8033.
2021-06-12 10:20:49 -07:00
Fabian Homborg
7059eaa4ab Revert "Disallow escaped characters in variable expansion"
This reverts commit 555af37616.
2021-06-10 16:46:17 +02:00
Fabian Homborg
95103893e6 output: Allow "--background foo" and "-b foo" for background colors
This only accepted "--background=". Really what we need to do is do an
actual getopt, but that wants a null-terminated array and is tightly
coupled to set_color.

Fixes #8053.
2021-06-10 10:42:30 +02:00
Fabian Homborg
046db09f90
Try to set LC_CTYPE to something UTF-8 capable (#8031)
* Try to set LC_CTYPE to something UTF-8 capable

When fish is started with LC_CTYPE=C (even just effectively, often via
LC_ALL=C!), it's basically broken. There's no way to handle non-ASCII
characters with a C locale unless we want to write our
locale-independent replacements for all of the system functions.

Since we're not going to do that, let's try to find *some locale* for
LC_CTYPE.

We already do that in __fish_setlocale, but that's

- a bit of a weird thing that reads unstandardized system
  configuration files
- allows setting locale to C explicitly

So it's still easily possible to end up in a broken configuration.

Now, the issue with this is that there is (AFAICT) no portable way to
get a list of all allowed locales and C.UTF-8 is not standardized, so
we have no one locale to fall back on and are forced to try a few. The
list we have here is quite arbitrary, but it's a start.

Python does something similar and only tries C.UTF-8, C.utf8 and
"UTF-8".

Once C.UTF-8 is (hopefully) standardized, that will just start
working (tm).

Note that we do not *export* the fixed LC_CTYPE variable, so external
programs still have to deal with the C locale, but we have no real
business messing with the user's environment.

To turn it off: $fish_allow_singlebyte_locale, if set to something true (like "1"),
will re-run the locale initialization and skip the bit where we force
LC_CTYPE to be utf8-capable.

This is mainly used in our tests, but might also be useful if people
are trying to do something weird.
2021-06-06 09:28:32 +02:00
ridiculousfish
e74b9d53df Do not use posix_spawn on glibc < 2.24
This concerns the behavior of posix_spawn for shebangless scripts. At some
point, glibc started executing them using `sh`, which is desirable for
fish's shebangless support (see #7802). On glibcs without that behavior
the shebangless test fails. So this change disables posix_spawn on older
glibcs.

It's not easy to figure out when that happened but it definitely happens
in glibc 2.28, and does not happen in glibc 2.17. Presumably the new
behavior is present in glibc 2.24 (see BZ#23264) so that's the cutoff:
posix_spawn is no longer allowed on glibc < 2.24.

This fixes the noshebang test failures on Ubuntu Xenial and Centos 7.
See discussion at bottom of #8021.
2021-05-31 13:38:56 -07:00
ridiculousfish
50c851d10e Clean up use_posix_spawn
Switch from a global variable to a real function. Make the value atomic.
Clean up handle_fish_use_posix_spawn_change().
2021-05-31 13:38:56 -07:00
Fabian Homborg
671d820277 set_color: Print an error for unknown options
This was forgotten, so e.g. calling `set_color --bg foo` results in
nothing being printed, which might result in strings being removed - #5443.
2021-05-27 19:03:55 +02:00
ridiculousfish
9820307d23 Move builtin_bind to out-of-line
There was no point in inlining this code.
2021-05-25 17:39:55 -07:00
ridiculousfish
08950b1077 Revert "Bravely set job control to full at startup"
Now that `$last_pid` is never fish's pid, we no longer need to force
jobs to run in their own pgroup. Restore the job control behavior to
what it was prior, so that signals may be delivered properly in
non-interactive mode.

This reverts commit 3255999794
2021-05-25 15:28:53 -07:00
ridiculousfish
33f3c03dae Allow on-job-exit handlers to be added for any pid in the job
Prior to this change, a function with an on-job-exit event handler must be
added with the pgid of the job. But sometimes the pgid of the job is fish
itself (if job control is disabled) and the previous commit made last_pid
an actual pid from the job, instead of its pgroup.

Switch on-job-exit to accept any pid from the job (except fish itself).
This allows it to be used directly with $last_pid, except that it now
works if job control is off. This is implemented by "resolving" the pid to
the internal job id at the point the event handler is added.

Also switch to passing the last pid of the job, rather than its pgroup.
This aligns better with $last_pid.
2021-05-25 15:28:53 -07:00
ridiculousfish
f3d78e21d1 Switch last_pid from the pgroup to the actual last pid
When a job is placed in the background, fish will set the `$last_pid`
variable. Prior to this change, `$last_pid` was set to the process group
leader of the job. However this caussed problems when the job ran in
fish's process group, because then fish itself would be the process group
leader and commands like `wait` would not work.

Switch `$last_pid` to be the actual last pid of the pipeline. This brings
it in line with the `$!` variable from zsh and bash.

This is technically a breaking change, but it is unlikely to cause
problems, because `$last_pid` was already rather broken.

Fixes #5036
Fixes #5832
Fixes #7721
2021-05-25 15:28:53 -07:00
David Dorfman
f2448e3f0e env: remove trailing null-terminator from default path 2021-05-25 08:12:21 +02:00
ridiculousfish
35f77a5473 Switch locale_variables and curses_variables from vector to array
No reason to have these be a heap-allocated vector.
2021-05-22 12:50:26 -07:00
Fabian Homborg
fe4eaba563 Fix set_cloexec check
Fixes #8023.
2021-05-22 18:09:15 +02:00
ridiculousfish
9d696ba7d2 Remove some assignments of wcs2string to references
wcs2string returns a std::string by value; it should not be assigned to a
reference variable.
2021-05-21 13:11:56 -07:00
ridiculousfish
9928404920 Remove some static_asserts out of the common.h header
These asserts require a recursive template instantiation and are currently
checked for every file that pulls in common.h. Place them in a .cpp file so
they are only checked once, hopefully improving compile time.
2021-05-21 13:06:43 -07:00
ridiculousfish
7123e2f25d Remove another errant negation 2021-05-20 11:10:09 -07:00
ridiculousfish
fac8f14e07 Correct a negated pgid
When printing the description of an event, there was an errant negation
from when fish stored the pgid negated. Remove it.
2021-05-20 11:07:36 -07:00
ridiculousfish
504a969a24 Separate on-job-exit and and on-process-exit events
It is possible to run a function when a process exits via `function
--on-process-exit`, or when a job exits via `function --on-job-exits`.
Internally these were distinguished by the pid in the event: if it was
positive, then it was a process exit. If negative, it represents a pgid
and is a job exit. If zero, it fires for both jobs and processes, which is
pretty weird.

Switch to tracking these explicitly. Separate out the --on-process-exit
and --on-job-exit event types into separate types. Stop negating pgids as
well.
2021-05-19 11:29:08 -07:00
Fabian Homborg
63dd046f99 screen: Remove errant line
This was an experiment that was accidentally committed. Sorry!
2021-05-18 11:27:30 +02:00
Fabian Homborg
5743a536b0 proc: Include sys/wait.h
Might fix build on FreeBSD.
2021-05-18 10:18:50 +02:00
Fabian Homborg
c19a6e912d Readd awkward unused-result dance
This was removed in 962b0f8b90,
presumably with the idea that casting to void, like before, was
enough.

It's not, at least with gcc 11.1
2021-05-18 09:44:29 +02:00
Fabian Homborg
4b2bce7b83 screen: Remove useless .c_str() and wcslen calls
This passed the wchar_t* to outputter::writestr(), which then had to
do a wcslen on it, when it already has a perfectly cromulent
wcstring overload.

Just use that one.
2021-05-18 09:11:56 +02:00
Fabian Homborg
25595a94c7 screen: Also move cursor after printing the prompt
This helps with width issues when no right prompt is used.
2021-05-18 09:09:59 +02:00
Fabian Homborg
0660ea5be7 Move the cursor to the beginning before printing right prompt
This makes the right prompt position independent of the width of the
commandline, which prevents staircase effects. That means, with "X"
standing in as a character that the terminal and fish disagree on:

```
> echo X           rightprompt
```

will stay like that instead of creating a staircase like

```
> echo X            rightpromp
t> echo X             rightpromp
pt> echo X
```

and so on.

The cursor still won't be *correct*, but it will be wrong in a less
annoying way.
2021-05-18 09:09:59 +02:00
ridiculousfish
d3ceba107e Clear to eol before outputting line in multi-line prompt
If the user has a multi-line prompt, we will emit a clr_eol on every
line except the last (see #7404). Prior to this change we would emit
clr_eol after the line, but in some terminals, if the line extended the
width of the tty, the last character would be deleted. Switch to
emitting clr_eol first; now the last character will not be cut off.

Fixes #8002
2021-05-17 21:44:17 -07:00
ridiculousfish
6d00ad1045 Ensure that on-process-exit events fire for reaped jobs
This ensures that if a job exits before we have set up the
on-process-exit handler, the handler will still fire.

Fixes #7210
2021-05-17 15:28:32 -07:00
ridiculousfish
60d75e9aa0 Remove proc_create_event
Switch to a set of factory functions inside event_t.
No user-visible change here.
2021-05-17 15:26:59 -07:00
ridiculousfish
962b0f8b90 Pass $status to process-exit event handlers in all cases
Previously, an event handler would receive -1 if the process exited due
to a signal. Instead pass the same value as $status.
2021-05-17 15:25:27 -07:00
ridiculousfish
82fd8fe9fb Refactor wait handles
In preparation for using wait handles in --on-process-exit events, factor
wait handles into their own wait handle store. Also switch them to
per-process instead of per-job, which is a simplification.
2021-05-17 15:25:21 -07:00
Fabian Homborg
a427bf207a reader: Fix crash when text is empty after stripping spaces
This crashed on Fedora with the rpm packages, but not when building
from source, so some compiler option triggers it.

But the root cause is us running `text.front()` on an empty string,
which isn't something you should do.

Fixes #8009.
2021-05-16 22:16:22 +02:00
Karolina Gontarek
31f3c16857 Resolve relative paths in command names for complete -p
Fixes #6001
2021-05-16 21:52:38 +02:00
ridiculousfish
63ee28c1de Fix a misleading comment 2021-05-15 22:05:35 -07:00
ridiculousfish
5de63c9cbb Reimplement builtin_wait using wait handles
This switches builtin_wait from waiting on jobs in the active job list, to
waiting on the wait handles. The wait handles may be either derived from
the job list itself, or from saved wait handles from jobs that exited in
the background.

Fixes #7210
2021-05-15 21:48:15 -07:00
ridiculousfish
632e150152 Introduce notion of "wait handles"
This is preparing to address the problem where fish cannot wait on a
reaped job, because it only looks at the active job list. Introduce the
idea of a "wait handle," which is a thing that `wait` can use to check if
a job is finished. A job may produce its wait handle on demand, and
parser_t will save the wait handle from wait-able jobs at the point they
are reaped.

This change merely introduces the idea; the next change makes builtin_wait
start using it.
2021-05-15 20:20:50 -07:00