Commit Graph

4364 Commits

Author SHA1 Message Date
Johannes Altmanninger
2e8ecfdb44 Clarify escaping of ASCII control characters
We use "c > 0" but we actually mean "c != 0".  The former looks like the
other code path handles negative c.  Yet if c is negative, our code would
print a single escaped byte (\xXY) which is wrong because a negative value
has "sizeof wchar_t" bytes which is at least 2.

I think on platforms with 16-bit wchar_t it's possible that we actually
get a negative value but I haven't checked.
2022-07-27 11:24:35 +02:00
Johannes Altmanninger
f1b4366222 Consolidate logic in escape_string_script()
No functional change.
2022-07-27 11:24:35 +02:00
Johannes Altmanninger
83893558f9 Make ESCAPE_NO_PRINTABLES behavior a bit less weird
Since the fix for #3892, this escaping style escapes

	\n to \\n

as well as

	\\ to \\\\
	\' to \\'

I believe these two are the only printable characters that are escaped with
ESCAPE_NO_PRINTABLES.
The rationale is probably to keep the encoding unambiguous and reversible.
However that doesn't justify escaping the single quote. Probably this was
an accident, so let's revert that part.

This has the nice effect that single quotes will no longer be escaped
when rendered in the completion pager (which is consistent with other
special characters). Try it:

    complete : -a "aaa\'\; aaaa\'\;" -f

Also this makes the error output of builtin bind consistent:

    $ bind -e --preset \;
    $ bind -e --preset \'
    $ bind \;
    bind: No binding found for sequence “;”
    $ bind \'
    bind: No binding found for sequence “'”

the last line is clearly better than the old version:

    bind: No binding found for sequence “\'”

In general, the fact that ESCAPE_NO_PRINTABLES escapes the (printable)
backslash is weird but I guess it's fine because it looks more consistent to
users, even though the result is an undocumented subset of the fish language.
2022-07-27 11:24:35 +02:00
Johannes Altmanninger
8729623cec Make ESCAPE_ALL the default and call its inverse ESCAPE_NO_PRINTABLES
ESCAPE_ALL is not really a helpful name. Also it's the most common flag.
Let's make it the default so we can remove this unhelpful name.

While at it, let's add a default value for the flags argument, which helps
most callers.

The absence of ESCAPE_ALL makes it only escape nonprintable characters
(with some exceptions). We use this for displaying strings in the completion
pager as well as for the human-readable output of "set", "set -S", "bind"
and "functions".

No functional change.
2022-07-27 11:24:35 +02:00
Johannes Altmanninger
e5d5391687 Remove useless escaping of variable names
When listing variables, "set" tries to escape variable names.
Since variable names cannot have special characters, this doesn't do anything.

The escaping is one of the few places that does not use ESCAPE_ALL.  This has
complex behavior; let's alleviate the problem by getting rid of this call.

No functional change.
2022-07-27 11:24:35 +02:00
Johannes Altmanninger
3f90efca38 clang-format C++ files
Or should we stop using it?

I'm fine with either always or never using auto-formatting but our current
way of using it only sometimes is confusing.

No functional change.
2022-07-27 10:05:41 +02:00
Johannes Altmanninger
0c97fea5c4 Make pager refilter completions after undo/redo in search field
Almost all edits to our commandline are funneled through
reader_data_t::push_edit(). Notable exceptions are undo/redo (which move
across existing edits instead). Due to an oversight, undo/redo fail to
trigger commandline update hooks. Fix that.

Our behavior of triggering hooks only for the search field looks weird. I
reckon that the command line eventually catches up, but this means we trigger
some hooks redundantly. Once we figure that out we can remove the new function.
2022-07-26 15:29:52 +02:00
Johannes Altmanninger
fe2f6f0c63 Fix Escape in pager not removing the inserted completion if search field was used
command_line_has_transient_edit tracks the actual command line, not the
pager search field. We accidentally reset it after modifying the search field
which causes unexpected behavior - the commandline added by the completion
pager remains even after I press Escape.
2022-07-26 15:29:52 +02:00
Johannes Altmanninger
3d8f643a5e Remove duplicate logic to clear the transient bit when inserting into commandline
This is already done by the above call to insert_char.

No functional change.
2022-07-26 15:20:35 +02:00
Johannes Altmanninger
671ad1f4a6 Fix typo 2022-07-26 15:20:19 +02:00
Johannes Altmanninger
bd5610349d Fix pager backwards movement on half-filled last column
If the completion pager renders as

	foo1 bar1 baz1 qux1
	foo2 bar2 baz2
	foo3 bar3 baz3

and we go backwards from "foo1" (using left arrow), we'll end up at "baz3",
not "qux1". Pretty smart!

If however we go backwards once more, nothing happens.

The root cause is that there are two different kinds of selection indices:
the one before rendering (9/qux1) and the one after we cleverly subtract
the half-filled last column (8/baz3). The backwards movement ends up
decrementing the first, so it moves from 9 to 8 and nothing changes in
the rendering.

Fix this by using the selection index that we actually rendered.

There is another caller that relies on the old behavior of using the unrendered
selection index. Make it use a dedicated overload that does not depend on
the rendering.
2022-07-24 17:12:28 +02:00
Johannes Altmanninger
12d4b50d5f Remove unused parameter from set_fully_disclosed() 2022-07-24 17:11:48 +02:00
Johannes Altmanninger
368b68ff47 Minor simplification of term_donate/term_steal
No functional change.
2022-07-24 17:11:48 +02:00
Fabian Boehm
bcd84c6908 Check for waitstatus orientation via cmake
Yeah we need the long way around because old glibc versions have weird WEXITSTATUS.
2022-07-24 16:40:33 +02:00
Fabian Boehm
122b6c1734 status: Only realpath if we got an absolute path
Otherwise realpath would add the cwd, which would be broken if fish
ever cd'd.

We could add the original cwd, but even that isn't enough, because we
need *the parent's* idea of cwd and $PATH.

Or, alternatively, what we need is for the OS to give us the actual
path to ourselves.
2022-07-24 14:31:15 +02:00
Fabian Boehm
d241f0853e status: Do add the command name to the error 2022-07-24 13:17:06 +02:00
Fabian Boehm
4f1c62ff43 status: Realpath the executable path
get_executable_path says: "This needs to be realpath'd"

So how about we do that? The only other place we use it is fish.cpp,
and we realpath it there already.

See #9085
2022-07-24 12:36:32 +02:00
Fabian Boehm
2cb0cada86 Remove sys/mount.h include
This seems to be unnecessary?
2022-07-24 12:24:42 +02:00
Johannes Altmanninger
8b378e9a44 Make complete-or-search select the first candidate
Our pager computes the selected completion based on its rendering. The number
of rows affect the selection, in particular when moving left from the top
left cell.  This computation breaks if the number of rows is zero, which
happens in at least
two scenarios:
1. If the completion pager was not shown (as is the case for complete-or-search)
2. If the search field had filtered away every candidate but not anymore.
I believe in these scenarios the selected completion index is always 0,
so let's fix the selection for that case.

Probably too minor for a changelog entry.

Closes #9080
2022-07-24 10:23:13 +02:00
Samuel Venable
e4c7211cd6
Fix NetBSD executable path to not use procfs (#9085)
* Fix NetBSD executable path to not use procfs

* Update common.cpp
2022-07-24 09:26:33 +02:00
Fabian Boehm
a98301b021 Allow for EWOULDBLOCK instead of EAGAIN
Posix allows this as an alternative with the same semantics for read.

Found in conjunction with #9067.

Should be no functional difference on other systems.
2022-07-23 23:16:44 +02:00
Fabian Boehm
df5489e0a4 Allow for systems where wait status is signal/return
The wait status value, which we also use internally, is read by a
bunch of macros.

Unfortunately because we want to *create* such a value, and some
systems lack the "W_EXITCODE" macro to do that, we need to figure out
how it's encoded.

So we simply check a specific value, and assume the encoding from
that.

On Haiku the return status is in the lower byte, on other systems it's
typically the upper byte.

TODO: Test on musl (that's the other system without W_EXITCODE).

Fixes #9067
2022-07-23 23:16:44 +02:00
Fabian Boehm
64adfdee40 Remove wrong UNUSED annotation
This does in fact use streams
2022-07-23 18:02:46 +02:00
Fabian Boehm
407a455cfd realpath: Use physical PWD
This was an inadvertent change from
cc632d6ae9.

Because we used wgetcwd directly before, we always got the "physical"
resolved $PWD.

There's an argument to be made to use the logical $PWD here as well
but I prefer not to make changes lik that in a random commit without
good reason.
2022-07-18 20:45:30 +02:00
Fabian Boehm
5dfb64b547
Add path mtime (#9057)
This can be used to print the modification time, like `stat` with some
options.

The reason is that `stat` has caused us a number of portability
headaches:

1. It's not available everywhere by default
2. The versions are quite different

For instance, with GNU stat it's `stat -c '%Y'`, with macOS it's `stat
-f %m`.

So now checking a cache file can be done just with builtins.
2022-07-18 20:39:01 +02:00
Aaron Gyes
763240f1af Just remove the dumb comment. 2022-07-17 14:41:35 -07:00
Aaron Gyes
8f91ee7f6b builtin test: Implement -ot, -nt, -ef
These are non-POSIX extensions other test(1) utilities implement,
which compares the modification time of two files as proposed for
fish in #3589: testing if one file is newer than another file.

-ef is a common extension to test(1) which checks if two paths refer
to the same file, by comparing the dev and inode numbers.
2022-07-16 12:40:36 -07:00
Johannes Altmanninger
7bdc712615 Clean up weird edge-case for escaping unescaped brackets
As explained by the comment, this was dead code.  If it were ever executed,
it would cause very weird behavior because it would make some completions
randomly affect others.

Let's just print a warning (maybe this is better than crashing?).
2022-07-16 16:42:34 +02:00
Johannes Altmanninger
12cf31de96 Remove a redundant comment
Also add an issue reference since the commit message doesn't have one.
Of course a test would be even better.
2022-07-16 16:42:19 +02:00
Moheeb Aljaroudi
d4d0ac95b0 Fixed problem where fish would escape '~' when completing an unescaped
']'
2022-07-16 16:17:51 +02:00
SeekingBlues
173914af65
Highlight history searches correctly (#9066)
Previously, the search text is used to find out which part of the
updated command line should be highlighted during a history search. This
approach will cause the incorrect part to be highlighted when the line
contains multiple instances of the search text.

To address this, we have to find out exactly where to highlight, i.e.
the offset of the current token in the command line (0 if not a token
search) plus the offset of the search text in the match.
2022-07-13 16:48:04 +02:00
Fabian Boehm
cc632d6ae9 realpath: Use the parser's working dir
Future proofing, similar to what we do in `path resolve`.
2022-07-12 20:53:57 +02:00
Fabian Boehm
526b7e3b1b readdir_for_dirs: Actually filter out non-dirs
This function is supposed to return "the next directory". Because this
is imperfect, it only tries to.

Except it went to all the trouble of figuring out the type and then
just... returned it anyway.

This has nice speedups in globs with directory components like `*/` or
`**`. I have observed 1.1x to 2.0x.

We could also return when we know it's definitely a directory and then
skip a stat() later, but preliminary testing seemed to show that's not
worth much.
2022-07-12 16:50:00 +02:00
ridiculousfish
a9964cd6d0 Remove usage of PCRE2_SUBSTITUTE_LITERAL
We don't need this flag and this ties us to a newer version of PCRE2
than we would like. Fixes #9061.
2022-07-10 11:17:19 -07:00
Aaron Gyes
cbd0ec568c builtins/path.cpp: remove <glob.h>
I don't believe we use any system glob faciltiies.
2022-07-09 21:11:43 -07:00
Aaron Gyes
3e0f3c9f45 path.cpp: include its actual header with the prototype
path.h: fix that header so it can compile.
2022-07-09 21:04:03 -07:00
ridiculousfish
f7c411d5a5 Further cleanup of builtin_string regex matching
Take advantage of additional cleanup unlocked by this refactoring,
including eliminating unneeded error returns and simplifying some
control flow.

No user-visible behavior change expected here.
2022-07-09 16:44:12 -07:00
ridiculousfish
d46f402cea Adopt the new re in builtin_string
This switches builtin_string from using PCRE2 directly, to using the new re
component. This simplifies some code and removes redundancy.

No user-visible behavior change expected here.
2022-07-09 16:41:15 -07:00
ridiculousfish
7ae1727359 Factor out PCRE2 into new re component
This migrates our PCRE2 dependency from builtin/string.cpp to new files
re.h/re.cpp, allowing regexes to be used in other places in fish.

No user-visible behavior change expected here.
2022-07-09 16:37:20 -07:00
ridiculousfish
61b09ff4a7 Stop using a static unordered_map for string flag handlers
This switches the flag_to_function from a map to just an ordinary switch
statement. This saves some memory/startup time and removes some
relocations. No functional change here.
2022-07-04 13:40:55 -07:00
ridiculousfish
ffded81a00 Correct a misleading comment 2022-07-02 11:30:59 -07:00
Fabian Boehm
60f87ef3be Add error for EBADARCH
That's apparently errno 86 on macOS, and it's triggered when the
architecture is wrong.

I'll leave other macOS errors to the macOS users.

See #9052.
2022-07-02 10:11:00 +02:00
Fabian Boehm
d920610f96 Fix special readline functions after and/or
Here we needed to handle self-insert immediately, but we ended up
returning it.

Fixes #9051
2022-07-02 09:23:11 +02:00
Fabian Boehm
98ba66ed8e set_color: Print the given colors with --print-colors 2022-07-01 21:28:35 +02:00
Fabian Boehm
bd7934ccbf history: Refuse to merge in private mode
It makes *no* sense.

Fixes #9050.
2022-07-01 20:10:18 +02:00
Fabian Boehm
dde2d33098 set --show: Show the originally inherited value, if any
This adds a line to `set --show`s output like

```
$PATH: originally inherited as |/home/alfa/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/var/lib/flatpak/exports/bin|
```

to help with debugging.

Note that this means keeping an additional copy of the original
environment around. At most this would be one ARG_MAX's worth, which
is about 2M.
2022-06-27 20:33:26 +02:00
Fabian Boehm
04f6306a35 argparse: Stop reconverting to null_terminated_array_t
We already have a perfectly cromulent null_terminated_array here, so
just use it.

No visible changes here, possibly some memory use?
2022-06-27 17:45:08 +02:00
Fabian Boehm
993448d552 argparse: Allow usage without optspecs
It's still useful without, for instance to implement a command that
takes no options, or to check min-args or max-args.

(technically no optspecs, no min/max args and --ignore-unknown does
nothing, but that's a very specific error that we don't need to forbid)

Fixes #9006
2022-06-27 17:02:20 +02:00
Aaron Gyes
b73091b27b proc.cpp, fish_tests.cpp: use snprintf()
Resolves this warning:

> warning: 'sprintf' is deprecated: This function is provided for compatibility reasons only.  Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead. [-Wdeprecated-declarations]
2022-06-25 16:27:04 -07:00
Fabian Boehm
13a9f6b64e printf: Print special error for invalid octal numbers
(tbh these were always a mistake)

See #9035
2022-06-23 18:12:43 +02:00