Commit Graph

665 Commits

Author SHA1 Message Date
Johannes Altmanninger
0e707b88f0 argparse: fix error message for missing option argument
case #1 in #6483
2020-01-08 14:38:05 +01:00
Johannes Altmanninger
992c864f26 Don't overwrite unrelated variables with for-loop-variables
for-loops that were not inside a function could overwrite global
and universal variables with the loop variable.  Avoid this by making
for-loop-variables local variables in their enclosing scope.

This means that if someone does:

    set a global
    for a in local; end
    echo $a

The local $a will shadow the global one (but not be visible in child
scopes). Which is surprising, but less dangerous than the previous
behavior.

The detection whether the loop is running inside a function was failing
inside command substitutions. Remove this special handling of functions
alltogether, it's not needed anymore.

Fixes #6480
2020-01-08 09:10:14 +01:00
Norio Nomura
cc7618985a Don't override exit status when stderr is closed by 2>&-
fixes #6470
2020-01-07 19:57:35 +01:00
Fabian Homborg
24970bb549 checks: Use "sleep" without "s" suffix
FreeBSD's sleep doesn't accept it.
2020-01-05 18:41:56 +01:00
ridiculousfish
62302ee172 Properly print leading comments and indentation in functions
Store the entire function declaration, not just its job list.
This allows us to extract the body of the function complete with any
leading comments and indents.

Fixes #5285
2020-01-03 14:40:28 -08:00
Johannes Altmanninger
c3374edc59 Reject time with background jobs
This check could probably done earlier in the parser but it works.
2020-01-03 01:07:49 -06:00
Johannes Altmanninger
3de95038b0 Make "time" a job prefix
In particular, this allows `true && time true`, or `true; and time true`,
and both `time not true` as well as `not time true` (like bash).

time is valid only as job _prefix_, so `true | time true` could call
`/bin/time` (same in bash)

See discussion in #6442
2020-01-03 01:07:49 -06:00
ridiculousfish
efa9d5dd6a Port cd tests to littlecheck 2019-12-31 14:16:20 -08:00
ridiculousfish
91404f1762 Rename job_ids check to job-ids
Other tests use a dash.
2019-12-31 13:32:04 -08:00
ridiculousfish
9a11c03097 Correct the job_ids test on the Mac
The Mac doesn't provide CPU percentages so the column is omitted, causing
the test to fail. Use a regex to cover both cases.
2019-12-31 13:17:26 -08:00
ridiculousfish
b691d3130e Don't give job IDs to block processes either
Extend the commit 8e17d29e04 to block processes, for example:

    begin ; stuff ; end

or if/while blocks as well.

Note there's an existing optimization where we do not create a job for a
block if it has no redirections.
2019-12-31 13:12:24 -08:00
ridiculousfish
a6e5583b5b Correct reordering of jobs in job_promote
job_promote attempts to bring the most recently "touched" job to the front
of the job list. It did this via:

    std::rotate(begin, job, end)

However this has the effect of pushing job-1 to the end. That is,
promoting '2' in [1, 2, 3] would result in [2, 3, 1].

Correct this by replacing it with:

    std::rotate(begin, job, job+1);

now we get the desired [2, 1, 3].

Also add a test.
2019-12-31 12:41:11 -08:00
Johannes Altmanninger
9f48fc6285 Fix status when function/block evaluation is cancelled
It looks like the last status already contains the signal that cancelled
execution.

Also make `fish -c something` always return the last exit status of
"something", instead of hardcoded 127 if exited or signalled.

Fixes #6444
2019-12-23 17:38:19 +01:00
ridiculousfish
97dd5ece26 Remove redirection_is_to_real_file
This was previously required so that, if there was a redirection to a
file, we would fork a process to create the file even if there was no
output. For example `echo -n >/tmp/file.txt` would have to create
file.txt even though it would be empty.

However now we open the file before fork, so we no longer need special
logic around this.
2019-12-20 14:40:57 -08:00
Fabian Homborg
dcf8b0e3aa Add test for time 2019-12-20 17:13:21 +01:00
Johannes Altmanninger
1410f938aa read: discard IFS delimiters before the last token
Do this only when splitting on IFS characters which usually contains
whitespace characters --- read --delimiter is unchanged; it still
consumes no more than one delimiter per variable. This seems better,
because it allows arbitrary delimiters in the last field.

Fixes #6406
2019-12-19 23:44:58 +01:00
Mahmoud Al-Qudsi
d90d4f849c Force symlink creation in realpath tests
If an earlier test was aborted, the symlink would still be present and
the subsequent run will fail to create the symlink as it wasn't forced.
2019-12-18 12:31:24 -06:00
Johannes Altmanninger
b1144a1fde completion: fix file completion of redirection targets
This fixes a regression introduced in
6fb7f9b6b - Fix completion for builtins with subcommands
2019-12-16 12:45:39 +01:00
ridiculousfish
9be77d1f9c Correctly handle "self fd redirections"
This adds a test for the obscure case where an fd is redirected to
itself. This is tricky because the dup2 will not clear the CLO_EXEC bit.
So do it manually; also posix_spawn can't be used in this case.
2019-12-13 16:51:49 -08:00
ridiculousfish
d6c71d77a9 Correctly cloexec file redirections
The IO cleanup left file redirections open in the child. For example,
/bin/cmd < file.txt would redirect stdin but also leave the file open.
Ensure these get closed properly.
2019-12-13 16:16:19 -08:00
ridiculousfish
c0b3be9fb4 Stop storing block_io in job_t
Prior to this fix, a job would hold onto any IO redirections from its
parent. For example:

    begin
        echo a
    end < file.txt

The "echo a" job would hold a reference to the I/O redirection.
The problem is that jobs then extend the life of pipes until the job is
cleaned up. This can prevent pipes from closing, leading to hangs.

Fix this by not storing the block IO; this ensures that jobs do not
prolong the life of pipes.

Fixes #6397
2019-12-11 16:34:20 -08:00
ridiculousfish
92a16921bf Add a test to verify that processes get the right pgrps in pipelines 2019-12-08 13:45:24 -08:00
ridiculousfish
d47541a3d7 Add a tricky test to verify disowning an in-flight job
There's some logic in fish to prevent blowing up when an under-construction
job is disowned. Add a test for it.
2019-12-08 11:44:21 -08:00
ridiculousfish
098fe86ebf Port empty functions test to littlecheck 2019-12-07 11:02:22 -08:00
Peter Collingbourne
6dc4ac60ae Use $PWD to retrieve the current directory in __fish_move_last.
(command pwd) uses the system's implementation of pwd. At least the GNU
coreutils implementation defaults to -P, which resulted in symlinks being
expanded when switching between directories with nextd/prevd.
2019-12-07 12:20:31 +01:00
Fabian Homborg
115892ccd2 alias: Use read --tokenize
This did some weird unescaping to try to extract the first word.

So we're now more likely to be *correct*, and the alias benchmark is
about 20% *faster*.

Call it a win-win.
2019-12-01 18:14:26 +01:00
Fabian Homborg
86133b0a2b Add read --tokenize
This splits a string into variables according to the shell's
tokenization rules, considering quoting, escaping etc.

This runs an automatic `unescape` on the string so it's presented like
it would be passed to the command. E.g.

    printf '%s\n' a\ b

returns the tokens

printf
%s\n
a b

It might be useful to add another mode "--tokenize-raw" that doesn't
do that, but this seems to be the more useful of the two.

Fixes #3823.
2019-12-01 18:14:26 +01:00
Fabian Homborg
662fb3f3d1 Fix line numbers in functions
This added the function offset *again*, but it's already included in
the line for the current file.

And yes, I have explicitly tested a function file with a function
defined at a later line.

Fixes #6350
2019-11-26 18:12:24 +01:00
Johannes Altmanninger
f36705bb66 Fix error messages for "and" and "or" after pipe
Fixes #6347
2019-11-26 14:03:53 +01:00
Johannes Altmanninger
97969a9363 Restore error messages for bare variable assignment
Since #6287, bare variable assignments do not parse, which broke
the "Unsupported use of '='" error message.

This commit catches parse errors that occur on bare variable assignments.
When a statement node fails to parse, then we check if there is at least one
prefixing variable assignment. If so, we emit the old error message.

See also #6347
2019-11-26 13:59:17 +01:00
ridiculousfish
305409a025 Fix the vi mode bind test
Note support for the 'replace' mode.
2019-11-25 16:43:54 -08:00
Jason
3cf6ebc0e1 Amend typos and grammar errors 2019-11-25 13:07:15 +01:00
Johannes Altmanninger
7d5b44e828 Support FOO=bar syntax for passing variables to individual commands
This adds initial support for statements with prefixed variable assignments.
Statments like this are supported:

a=1 b=$a echo $b        # outputs 1

Just like in other shells, the left-hand side of each assignment must
be a valid variable identifier (no quoting/escaping).  Array indexing
(PATH[1]=/bin ls $PATH) is *not* yet supported, but can be added fairly
easily.

The right hand side may be any valid string token, like a command
substitution, or a brace expansion.

Since `a=* foo` is equivalent to `begin set -lx a *; foo; end`,
the assignment, like `set`, uses nullglob behavior, e.g. below command
can safely be used to check if a directory is empty.

x=/nothing/{,.}* test (count $x) -eq 0

Generic file completion is done after the equal sign, so for example
pressing tab after something like `HOME=/` completes files in the
root directory
Subcommand completion works, so something like
`GIT_DIR=repo.git and command git ` correctly calls git completions
(but the git completion does not use the variable as of now).

The variable assignment is highlighted like an argument.

Closes #6048
2019-11-25 09:20:51 +01:00
Fabian Homborg
aae111584d Disable localized number test on OpenBSD
This feature simply does not work there.
2019-11-16 12:11:09 +01:00
Ankush Patil
ee982c4f6c Fixes #6280 : Added right associativity to 'pow' function 2019-11-13 13:51:01 -08:00
ridiculousfish
2555ecf757 Remove the forbidden function stack
Detect forbidden functions directly from the associated block_t.
Also unify where we do stack overflow detection.
2019-11-10 12:36:46 -08:00
ridiculousfish
626237c9c3 Add a check for fish function stack overflow 2019-11-10 12:35:58 -08:00
Johannes Altmanninger
e94b9ccf3e Do import bash history commands containing && or || 2019-11-07 23:33:35 +01:00
Fabian Homborg
d77c465d23 string: Allow -eq again
Instead of forbidding it for both modes, allow it for both and make it
quiet for string.

Fixes #6282
2019-11-04 17:34:37 +01:00
Johannes Altmanninger
6fb7f9b6b8 Fix completion for builtins (with subcommands)
Presently the completion engine ignores builtins that are part of the
fish syntax. This can be a problem when completing a string that was
based on the output of `commandline -p`.  This changes completions to
treat these builtins like any other command.

This also disables generic (filename) completion inside comments and
after strings that do not tokenize.

Additionally, comments are stripped off the output of `commandline -p`.

Fixes #5415
Fixes #2705
2019-11-04 16:44:51 +01:00
ridiculousfish
72bf5898d3 Clean up how PATH and CDPATH munging occurs
PATH and CDPATH have special behavior around empty elements. Express this
directly in env_stack_t::set rather than via variable dispatch; this is
cleaner.
2019-11-02 16:48:08 -07:00
ridiculousfish
a7f1d2c0c7 Add support for fish_trace variable to trace execution
This adds support for `fish_trace`, a new variable intended to serve the
same purpose as `set -x` as in bash. Setting this variable to anything
non-empty causes execution to be traced. In the future we may give more
specific meaning to the value of the variable.

The user's prompt is not traced unless you run it explicitly. Events are
also not traced because it is noisy; however autoloading is.

Fixes #3427
2019-11-02 14:40:57 -07:00
Johannes Altmanninger
6702c84d15 Prevent buffer overflow when custom completions edit the commandline
This was introduced in a7ea7648c3
"Completion: maintain cursor position when there is no completion"
2019-11-01 13:21:49 +01:00
Fabian Homborg
c0d8439f3a math: Print special error for logical operators
Until now, something like

`math '7 = 2'`

would complain about a "missing" operator.

Now we print an error about logical operators not being supported and
point the user towards `test`.

Fixes #6096
2019-11-01 08:43:13 +01:00
0x005c
067b30208d Fix math incorrect parenthesis error on missing term 2019-10-31 22:10:54 +01:00
ridiculousfish
0bfd897ee0 Add a special error message for |& 2019-10-27 15:24:57 -07:00
ridiculousfish
2a92e66902 Support for &> and &| as convenience redirections
This adds support for &> and &| syntax, which both redirect stdout, and
also apply a redirection of stderr to stdout.
2019-10-27 15:24:57 -07:00
Fabian Homborg
66938d206a string: Error out on match -eq
The `--entire` would enable output even though the `--quiet` should
have silenced it. These two don't make any sense together so print an
error, because the user could have just left off the `-q`.
2019-10-22 22:11:36 +02:00
Fabian Homborg
e0d623d4b7 tests/test_util: Fix wrong argument in delta
This spewed errors because the `math` invocation got no second
operand:

    Testing file checks/sigint.fish ... math: Error: Too few arguments
    '1571487730 -'

but only if the `date` didn't do milliseconds, which is the case on
FreeBSD and NetBSD.

(also force the variable to be global - we don't want to have a
universal causing trouble here)
2019-10-19 14:27:47 +02:00
Johannes Altmanninger
b7f35f949e Do not import vars that are equivalent to a universal exported var
Universal exported variables (created by `set -xU`) used to show up
both as universal and global variable in child instances of fish.

As a result, when changing an exported universal variable, the
new value would only be visible after a new login (or deleting the
variable from global scope in each fish instance).

Additionally, something like `set -xU EDITOR vim -g` would be imported
into the global scope as a single word resulting in failures to
execute $EDITOR in fish.

We cannot simply give precedence to universal variables, because
another process might have exported the same variable.  Instead, we
only skip importing a variable when it is equivalent to an exported
universal variable with the same name.  We compare their values after
joining with spaces, hence skipping those imports does not change the
environment fish passes to its children. Only the representation in
fish is changed from `"vim -g"` to `vim -g`.

Closes #5258.
This eliminates the issue #5348 for universal variables.
2019-10-19 12:41:57 +02:00