Commit Graph

2933 Commits

Author SHA1 Message Date
ridiculousfish
73106198c8 Update to latest widecharwidth
Fixes a Cygwin incompatibility. Fixes #6549
2020-01-30 21:17:37 -08:00
ridiculousfish
2c4e15e267 Fix the Cygwin build for uvar notifiers 2020-01-30 20:44:29 -08:00
ridiculousfish
97c456a986 Improve support for job control in non-interactive scenarios
Avoid complaining about ENOTTY results from tcsetpgrp, and ensure we
ignore SIGTTOU the first time job control is enabled.
2020-01-30 15:18:10 -08:00
Fabian Homborg
9735a18add Always return a value, even in unreachable code
To placate dumb compilers, we sometimes have to include code that will
never be executed.

Should fix the build when -Werror=return-type is in effect.
2020-01-30 20:39:42 +01:00
ridiculousfish
70195164d4 Refactor child_set_group 2020-01-30 11:27:21 -08:00
ridiculousfish
a243e65939 Rename pgroup_mode to pgroup_provenance 2020-01-30 11:14:31 -08:00
ridiculousfish
aaaca9773a Unconditionally call set_child_group() after posix_spawn
Previously we did this conditionally only if GLIBC is defined, but
it looks harmless to do this unconditionally. Let's do it.
2020-01-30 11:07:28 -08:00
ridiculousfish
10da6df506 Factor out logic about how pgroups are assigned
Introduce pgroup_provenance_t, a type which captures "where the pgroup
comes from." This centralizes some logic around how pgroups are
assigned, and it anticipates concurrent execution.
2020-01-30 10:50:16 -08:00
ridiculousfish
d22c6af7a3 clang-format all C++ files 2020-01-30 10:50:11 -08:00
Fabian Homborg
cc7d9cc2ed flog: Save & restore errno
In some cases on some platforms this could clobber errno, so doing something like

    aThingThatFailsWithErrno();
    FLOG(category, "Some message");
    wperror("something");

would print the wrong error (presumably if that category was enabled).

In our case it was our (very) old friend RHEL6 returning ESPIPE instead of EISDIR.

Fixes #6545.
2020-01-30 17:31:30 +01:00
Johannes Altmanninger
f4d0bafbd4 remove stale typedef 2020-01-30 16:32:47 +01:00
David Adam
696057ab57 Merge branch 'Integration_3.1.0' 2020-01-30 17:31:43 +08:00
David Adam
b313ba555a iothread: add missing #include
Closes #6553.
2020-01-30 17:21:15 +08:00
ridiculousfish
40ff4215a8 Express the "nested job control" idea directly
Prior to this fix, we would infer that nested jobs need job control.
Just pass that along explicitly in the job lineage.
2020-01-29 16:10:28 -08:00
ridiculousfish
28a8d0dbf7 Continued adoption of autoclose_fd_t and exec_close 2020-01-29 14:16:16 -08:00
ridiculousfish
3d47f042ac Be more consistent about using autoclose_fd_t and exec_close
Simplifying and improving file descriptor handling discipline.
2020-01-29 13:55:20 -08:00
ridiculousfish
29af84d733 Migrate get_interpreter into postfork.cpp
It's only used after fork.
2020-01-29 13:43:40 -08:00
ridiculousfish
376529a46d Clean up reader_read
Stop having reader_read close the input file descriptor. Make other
modernizations.
2020-01-28 11:39:26 -08:00
ridiculousfish
fc4557c784 Eliminate wopen()
It was large and mostly unnecessary. Prefer wopen() followed by
fdopen().
2020-01-28 10:43:37 -08:00
ridiculousfish
af2265b4b0 Migrate create_directory inside path.cpp
It is not used outside of path.cpp
2020-01-28 10:30:51 -08:00
ridiculousfish
1bbfb7044b Remove wopen
It was unused
2020-01-28 10:26:31 -08:00
ridiculousfish
f27958ef4d Clean up the open_cloexec interface
Remove the ability to make it non-cloexec - nobody was using it.
2020-01-28 10:25:49 -08:00
David Adam
dd1e526017 tests: remove an outdated comment
Code removed in af22d6b732
2020-01-28 16:54:29 +08:00
ridiculousfish
af22d6b732 Stop pretending to be interactive during the cancellation tests
Prior to this fix, the cancellation C++ test would mark the parser as
interactive in an effort to install interactive signal handling (so that,
for example, SIGINT would stop the job and return control to the user).

However this flag would also cause fish to attempt to save and restore tty modes
across the job. This would fail since there is no tty, and so the job would fail
with an unexpected error code.

We don't need to mark the parser as interactive, we can just remove that line.

Fixes #6539.
2020-01-27 12:13:46 -08:00
ridiculousfish
7d486c121a Revert "Add noexcept to completion_t definitions as well as declarations"
This reverts commit 89f5ae3e6a.

Apparently that broke gcc.
2020-01-26 20:59:58 -08:00
ridiculousfish
7a2a6d76f3 Migrate a test from C++ to littlecheck
This eliminates noisy warnings about tcsetpgrp when the tests are run
without a tty, as reported in #6539
2020-01-26 20:59:08 -08:00
ridiculousfish
ae77f1b163 Make locked_consumed_job_ids heap allocated rather than static
This prevents its destructor from running during normal exit.
Fixes #6539
2020-01-26 20:33:50 -08:00
ridiculousfish
89f5ae3e6a Add noexcept to completion_t definitions as well as declarations
This is required if we want to compile with exceptions enabled (which we
currently do not).
2020-01-26 20:33:47 -08:00
Fabian Homborg
3bb15defbb
Replace debug() with flog
PR #6511 

Flog has the advantage of having *categories*, not severities, so it'll be easier to get output for a certain subsystem now.
2020-01-26 14:13:17 +01:00
ridiculousfish
d29bc720cd Remove some nonsense in exec_close
exec_close should assert that the fd is valid, then loop while EINTR.
Nothing else is needed.
2020-01-25 19:08:30 -08:00
ridiculousfish
86897cafd6 Clean up, debug, optimize some command description generation
Use some more move semantics to reduce allocations.

Correctly handle the case where the completion is empty. For example, if
you type:

    ls<tab>

we get an empty completion (since ls is already a valid command), but we
still want to show its description.

Remove some unsafe statics - these are unsafe today in weird cases where
completions might invoke complete recursively, and also will soon be
unsafe with concurrent execution.
2020-01-25 18:26:31 -08:00
ridiculousfish
38f4330683 Rationalize $status and errors
Prior to this fix, fish was rather inconsistent in when $status gets set
in response to an error. For example, a failed expansion like "$foo["
would not modify $status.

This makes the following inter-related changes:

1. String expansion now directly returns the value to set for $status on
error. The value is always used.

2. parser_t::eval() now directly returns the proc_status_t, which cleans
up a lot of call sites.

3. We expose a new function exec_subshell_for_expand() which ignores
$status but returns errors specifically related to subshell expansion.

4. We reify the notion of "expansion breaking" errors. These include
command-not-found, expand syntax errors, and others.

The upshot is we are more consistent about always setting $status on
errors.
2020-01-25 17:28:41 -08:00
ridiculousfish
81e78c78aa Rename eval_result_t to end_execution_reason_t
We're getting ready to stop returning eval_result_t from parser_t::eval
2020-01-25 14:11:32 -08:00
ridiculousfish
e640a01ea5 Express cancellation as a possible result of expand_string
This allows us to properly thread control-C signals from command
substitutions into the expanding string.
2020-01-25 14:11:32 -08:00
ridiculousfish
4f205f38b4 Clean up a few bits about discarding buffers
We weren't properly propagating the 'discarded' stuff from output
streams to buffers. Fix that.
2020-01-24 16:08:56 -08:00
ridiculousfish
dbbe8a2da5 Clarify expand_result_t comments 2020-01-22 11:51:40 -08:00
ridiculousfish
2af710f7c0 Eliminate expand_result_t::wildcard_match
This was an internal implementation detail that all callers had to deal
with. Just get rid of it.
2020-01-22 11:49:59 -08:00
ridiculousfish
e398f66772 Run clang-format 2020-01-21 14:43:17 -08:00
Fabian Homborg
26fa774f44 Add path flog category 2020-01-19 14:55:39 +01:00
Fabian Homborg
f632a9e998 Add env-dispatch flog category 2020-01-19 14:55:39 +01:00
Fabian Homborg
4cb3ce0314 Add a 5 debug to the iothread flog 2020-01-19 14:55:39 +01:00
Fabian Homborg
483018222d Add complete flog category 2020-01-19 14:55:39 +01:00
Fabian Homborg
246882b52d Add proc-pgroup flog category
I'm not *super*-happy with this, because pgroups and terminal
ownership and such are quite entertwined.

But hey, if all fails just use `proc'*'`
2020-01-19 14:55:24 +01:00
Fabian Homborg
0c49f45028 fish_key_reader: Remove debug messages
These are related to *signal handlers* in fish_key_reader, and I don't
think this code needed to be touched since it was added.
2020-01-19 14:55:24 +01:00
Fabian Homborg
fe931aeea7 Add more messages to termowner flog category 2020-01-19 14:55:24 +01:00
Fabian Homborg
8b1ac0912b Add reader flog category
For both input and reader, because the "reader" term is more general
and we don't have enough messages to justify multiple categories
2020-01-19 14:55:08 +01:00
Fabian Homborg
a48926dee5 Add and use "should_flog" macro
Useful to figure out if a flog category is enabled.

We only use it in one place, but it seems like the sort of thing that
should exist.
2020-01-19 14:55:08 +01:00
Fabian Homborg
349b9e9dee Remove commented out debugs 2020-01-19 14:54:53 +01:00
Fabian Homborg
b09ae82ecf Remove a few less useful debug messages
These were level 5, so I'd bet nobody ever saw them
2020-01-19 14:22:50 +01:00
Fabian Homborg
5da4f7e7c5 Add flog category for config reading 2020-01-19 14:22:50 +01:00
Fabian Homborg
123676075f Add flog category for history *file* 2020-01-19 14:22:50 +01:00
Fabian Homborg
6793d35340 Add flog categories for parse-productions
One for usable messages, another for the chatty ones.

Use like `--debug=parse-productions'*'`.
2020-01-19 14:22:50 +01:00
Fabian Homborg
14c2c623b4 Add flog category for uvar file
This used debug level 5, which means it was basically unusable.
2020-01-19 14:22:50 +01:00
Fabian Homborg
4e0ec080e3 Add flog category for terminal support
This is meant to show messages related to what a terminal supports.

In particular which color or emoji it can handle.
2020-01-19 14:22:50 +01:00
Fabian Homborg
024e03ab1e Replace debug(1) with FLOGF(warning) 2020-01-19 14:22:39 +01:00
Fabian Homborg
384f18a51c Remove last remaining debug(0)
Replace with FLOGF.
2020-01-19 13:31:25 +01:00
ridiculousfish
c14d54032f Add a cant_wait parameter to iothread_perform
Sometimes we must spawn a new thread, to avoid the risk of deadlock.
Ensure we always spawn a thread in those cases. In particular this
includes the fillthread.
2020-01-18 11:51:13 -08:00
Johannes Altmanninger
b18f605e4f Fix completions if previous arg is a variable
complete -C'echo $HOM ' would complete $HOM instead of a new token.
Fixes another regression introduced in
6fb7f9b6b - Fix completion for builtins with subcommands
2020-01-18 20:21:10 +01:00
Fabian Homborg
018e51c935 Just hardcode a thread limit of 1024
64 is too low (it's actually reachable), and every sensible system should have a limit above
this.

On OpenBSD and FreeBSD it's ULONG_MAX, on my linux system it's 61990.

Plus we currently fail by hanging if our limit is reached, so this
should improve things regardless.

On my linux system _POSIX_THREAD_THREADS_MAX works out to 64 here,
which is just too low, even tho the system can handle more.

Fixes #6503 harder.
2020-01-18 10:30:32 +01:00
Fabian Homborg
ca08cc331b Use PTHREAD_THREADS_MAX if available
Stops us deadlocking on OpenBSD if we need more than 4 threads.

Fixes #6503
2020-01-18 09:07:31 +01:00
Johannes Altmanninger
b62fa53807 set error code on failed command substitution to 255 instead of -1
the exit status ought to be in 0-255, e.g. exit -1
2020-01-17 17:36:18 +01:00
Johannes Altmanninger
4170e8cff2 typo 2020-01-17 17:27:23 +01:00
Johannes Altmanninger
05ddda9155 use variable assignments on commandline in completions
Fixes #6507

To do: If a variable assignment uses a command substitution that errors,
the error is printed, but without a proper location.
2020-01-17 14:53:35 +01:00
Johannes Altmanninger
c513724d7b Do not store reference to a temporary
Seems to have worked by accident. I could swear that I had fixed this..
2020-01-17 14:49:52 +01:00
ridiculousfish
1da09f2c52 Ensure new job IDs are never smaller than existing running jobs
This makes job IDs "monotone" in the sense that newly spawned jobs
always have larger IDs than existing jobs, as requested in #6053
2020-01-16 16:01:29 -08:00
ridiculousfish
1978ac87a1 Remove reader_test_should_cancel
Use cancel_checker more pervasively.
2020-01-16 15:21:45 -08:00
ridiculousfish
0f7bba5f0e Introduce operation_context_t
This commit recognizes an existing pattern: many operations need some
combination of a set of variables, a way to detect cancellation, and
sometimes a parser. For example, tab completion needs a parser to execute
custom completions, the variable set, should cancel on SIGINT. Background
autosuggestions don't need a parser, but they do need the variables and
should cancel if the user types something new. Etc.

This introduces a new triple operation_context_t that wraps these concepts
up. This simplifies many method signatures and argument passing.
2020-01-16 15:21:28 -08:00
ridiculousfish
db98ee13a9 Make complete() return the completion list directly
Returning it through a pointer was a remnant of pre C++-11 days.
2020-01-16 15:18:50 -08:00
ridiculousfish
4bb18eaf42 Use completion_list_t naming everywhere
std::vector<completion_t> -> completion_list_t
2020-01-16 15:18:49 -08:00
ridiculousfish
98e714f98f clang-format .cpp and .h files 2020-01-16 15:18:28 -08:00
ridiculousfish
6705a2efc6 Migrate a bunch of code out of common.h
Put it into wcstringutil, path, or a new file null_terminated_array.
2020-01-15 13:16:43 -08:00
ridiculousfish
273afca3da Remove some dead code 2020-01-15 11:59:40 -08:00
ridiculousfish
4f01eef88d Default some ivars in wildcard_expander_t 2020-01-15 11:43:10 -08:00
ridiculousfish
5f130c2d6d Give wildcard_expand_result_t a real return value
Use an enum instead of an int.
2020-01-15 11:11:43 -08:00
ridiculousfish
7e1270ae70 Be more disciplined about cancellation signals
Rather than storing a "should cancel" flag in the parser, store the
actual signal which triggered cancellation.
2020-01-14 15:20:04 -08:00
ridiculousfish
47b87dbeb7 Factor internal process short-circuiting together
When executing a buffered block or builtin, the usual approach is to
execute, collect output in a string, and then output that string to
stdout or whatever the redirections say. Similarly for stderr.

If we get no output, then we can elide the outputting which means
skipping the background thread. In this case we just mark the process as
finished immediately.

We do this in multiple locations which is confusing. Factor them all
together into a new function run_internal_process_or_short_circuit.
2020-01-13 14:46:31 -08:00
ridiculousfish
fe4f7fea5c run_internal_process can no longer fail, remove its bool return 2020-01-13 14:40:24 -08:00
ridiculousfish
1efa641105 Move some fflushes around in handle_builtin_output 2020-01-13 14:35:20 -08:00
ridiculousfish
7b25787e52 Mild refactoring of exec_block_or_func_process
Reduce some code duplication.
2020-01-13 14:28:28 -08:00
ridiculousfish
399062c219 Rationalize a bit of how failed exec() works
We don't need to mark the job as completed in this case, because the
caller will remove the job.
2020-01-13 14:18:43 -08:00
ridiculousfish
8d598dcffc Use handle_child_status when reaping internal procs
This reduces the number of distinct locations where we mark a process as
completed.
2020-01-13 14:12:05 -08:00
Fabian Homborg
31e6ae0099 Ignore EPERM for setpgid
In case we are a session leader, we get a harmless EPERM, yet we used
to quit. Stop doing that.

Fixes #6499.
2020-01-13 18:57:19 +01:00
Fabian Homborg
759d5a1fb3 Fix uvar error message
This used a wstring format when it was a narrow string.
2020-01-13 17:48:09 +01:00
Johannes Altmanninger
9a355d5482 complete: replace confusing comment with test case 2020-01-08 17:53:46 +01:00
Johannes Altmanninger
75fa3b6bae unbreak missing argument error on long option 2020-01-08 17:33:36 +01:00
Johannes Altmanninger
fdf398e435 show missing argument error only for last flag
closes #6483
2020-01-08 14:59:26 +01:00
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
ridiculousfish
e0cdea9bb6 Correct the usleep calculation in fish_test_helper
'fish_test_helper print_pid_then_sleep' tried to sleep for .5 seconds,
but instead it divided by .5 so it actually slept for 2 seconds.

This exceeds the maximum value on NetBSD so it wasn't sleeping at all
there.

Fixes #6476
2020-01-07 17:07:51 -08: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
484e590b7b Readd unconst cast to tparm calls
Removed in 0dfa7421f3, breaks build with NetBSD curses.
2020-01-05 22:28:39 +01:00
ridiculousfish
f1ce967dfa Do not allow empty items to be added to history
Empty items are used as sentinels to indicate that we've reached the end of
history, so they should not be added as actual items. Enforce this.

Fixes #6032
2020-01-05 12:47:02 -08:00
Fabian Homborg
0f362b4bb2 Change c{begin,end} to begin,end
Unfortunately old distributions are old.

Fixes build failures on GCC 4.8 (RHEL6/7).
2020-01-05 15:44:05 +01:00
Fabian Homborg
0a379135a4 exec_job: Only call getpgrp() once
This reduces the syscall count for `fish -c exit` from 651 to 566.

We don't attempt to *cache* the pgrp or anything, we just call it once
when we're about to execute the job to see if we are in foreground and
to assign it to the job, instead of once for checking foreground and
once to give it to the job.

Caching it with a simple `static` would get the count down to 480, but
it's possible for fish to have its pgroup changed.
2020-01-05 09:41:06 +01:00
Aaron Gyes
d0b0c9a77e minor pcre2_matcher_t cleanup 2020-01-03 16:17:41 -08:00
Aaron Gyes
5281aca659 builtin_string.cpp: remove effectively dead variable 2020-01-03 16:17:41 -08:00
Mahmoud Al-Qudsi
038084b8bf Optimize generation, filtering, and sort of completions 2020-01-03 17:54:14 -06: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
89880891d0 Revert "fish_tests to set HOME and other variables to temporary dir"
This reverts commit cca57a7a87.

The tests target already sets some variables - backing this out.
2020-01-01 17:24:14 -08:00
ridiculousfish
cca57a7a87 fish_tests to set HOME and other variables to temporary dir
Rather than placing files in the user's home directory, have fish_tests
manipulate HOME to be a temporary directory.
2020-01-01 16:34:56 -08:00
ridiculousfish
65e9f31c7a Use autoclose_fd_t more pervasively in history 2020-01-01 13:49:10 -08:00
ridiculousfish
5aa22adccc Make history_filename return a maybe_t<wcstring>
This function can fail, so rather than forcing clients to check the return
value as empty, allow it to return none().
2020-01-01 12:34:42 -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
Dan Zimmerman
8e17d29e04 Introduce the internal jobs for functions
This PR is aimed at improving how job ids are assigned. In particular,
previous to this commit, a job id would be consumed by functions (and
thus aliases). Since it's usual to use functions as command wrappers
this results in awkward job id assignments.

For example if the user is like me and just made the jump from vim -> neovim
then the user might create the following alias:
```
alias vim=nvim
```
Previous to this commit if the user ran `vim` after setting up this
alias, backgrounded (^Z) and ran `jobs` then the output might be:
```
Job	Group	State	Command
2	60267	stopped	nvim  $argv
```
If the user subsequently opened another vim (nvim) session, backgrounded
and ran jobs then they might see what follows:
```
Job	Group	State	Command
4	70542	stopped	nvim  $argv
2	60267	stopped	nvim  $argv
```
These job ids feel unnatural, especially when transitioning away from
e.g. bash where job ids are sequentially incremented (and aliases/functions
don't consume a job id).

See #6053 for more details.

As @ridiculousfish pointed out in
https://github.com/fish-shell/fish-shell/issues/6053#issuecomment-559899400,
we want to elide a job's job id if it corresponds to a single function in the
foreground. This translates to the following prerequisites:

- A job must correspond to a single process (i.e. the job continuation
    must be empty)
- A job must be in the foreground (i.e. `&` wasn't appended)
- The job's single process must resolve to a function invocation

If all of these conditions are true then we should mark a job as
"internal" and somehow remove it from consideration when any
infrastructure tries to interact with jobs / job ids.

I saw two paths to implement these requirements:

- At the time of job creation calculate whether or not a job is
  "internal" and use a separate list of job ids to track their ids.
  Additionally introduce a new flag denoting that a job is internal so
  that e.g. `jobs` doesn't list internal jobs
  - I started implementing this route but quickly realized I was
    computing the same information that would be computed later on (e.g.
    "is this job a single process" and "is this jobs statement a
    function"). Specifically I was computing data that populate_job_process
    would end up computing later anyway. Additionally this added some
    weird complexities to the job system (after the change there were two
    job id lists AND an additional flag that had to be taken into
    consideration)
- Once a function is about to be executed we release the current jobs
  job id if the prerequisites are satisfied (which at this point have
  been fully computed).
  - I opted for this solution since it seems cleaner. In this
  implementation "releasing a job id" is done by both calling
  `release_job_id` and by marking the internal job_id member variable to
  -1. The former operation allows subsequent child jobs to reuse that
  same job id (so e.g. the situation described in Motivation doesn't
  occur), and the latter ensures that no other job / job id
  infrastructure will interact with these jobs because valid jobs have
  positive job ids. The second operation causes job_id to become
  non-const which leads to the list of code changes outside of `exec.c`
  (i.e. a codemod from `job_t::job_id` -> `job_t::job_id()` and moving the
   old member variable to a non-const private `job_t::job_id_`)

Note: Its very possible I missed something and setting the job id to -1
will break some other infrastructure, please let me know if so!

I tried to run `make/ninja lint`, but a bunch of non-relevant issues
appeared (e.g. `fatal error: 'config.h' file not found`). I did
successfully clang-format (`git clang-format -f`) and run tests, though.
This PR closes #6053.
2019-12-31 10:08:50 -08:00
Fabian Homborg
033a832687
Merge pull request #6447 from neheb/clang2
Several more small clang-tidy cleanups
2019-12-31 18:47:24 +01:00
ridiculousfish
c963442999 Collapse io_data switch statements
Now that each io_data knows its source and target fd, we don't need to switch
on its types any more.
2019-12-29 15:51:22 -08:00
ridiculousfish
0af5608ce8 io_data_t to store the source_fd directly
Now that all io_data_ts know their source fd, just store it directly in
the base class. This will simplify some uses of io_data_t.
2019-12-29 15:14:08 -08:00
ridiculousfish
5d55004841 Stop adding close actions in pipe and bufferfills
Now that all pipes are marked CLOEXEC, there is no reason to add explicit
close calls here.
2019-12-29 15:00:12 -08:00
ridiculousfish
d0cefe8b65 Always mark pipes as cloexec
There is never a reason to keep these open in exec.
2019-12-29 14:57:16 -08:00
ridiculousfish
b784a0caa3 dup2_list_t::resolve_chain to stop returning maybe
It can no longer fail.
2019-12-29 14:49:05 -08:00
ridiculousfish
94dcd1cc07 Use the given parser when fetching certain histories 2019-12-29 14:26:46 -08:00
ridiculousfish
9f7972a08b clang-format C++ files 2019-12-29 14:25:42 -08:00
Johannes Altmanninger
3d9c0d3c69 Show the first few history entries in set | grep history
As before, but do so efficiently. See #6290
2019-12-29 17:43:25 +01:00
Rosen Penev
06cb0bbe9a
[clang-tidy] Add several references
Found with performance-unnecessary-value-param

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2019-12-26 21:55:53 -08:00
Rosen Penev
5501953c07
[clang-tidy] Add ending namespace comment
Found with llvm-namespace-comment
2019-12-26 21:37:17 -08:00
Rosen Penev
b1349f44f6
[clang-tidy] Add const to reference
Found with performance-unnecessary-copy-initialization
2019-12-26 21:37:15 -08:00
Rosen Penev
5d1ad8de91
[clang-tidy] Use std::move
Found with modernize-pass-by-value
2019-12-26 21:36:23 -08:00
Rosen Penev
49fbca8a8b
[clang-tidy] Remove redundant const in function declarations
Found with readability-avoid-const-params-in-decls
2019-12-26 21:25:12 -08:00
Rosen Penev
856fa0ca42
[clang-tidy] Use override instead of virtual
Found with modernize-use-override
2019-12-26 21:25:12 -08:00
Rosen Penev
f2e7def667
[clang-tidy] Remove const from strings
Found with readability-const-return-type
2019-12-26 21:25:12 -08:00
Rosen Penev
668f73c0d6
[clang-tidy] Fix wrong declaration
Found with readability-inconsistent-declaration-parameter-name
2019-12-26 21:25:12 -08:00
Rosen Penev
9e42b0100a
[clang-tidy] Remove redudant .get on smart pointer
Found with readability-redundant-smartptr-get
2019-12-26 21:25:11 -08:00
Rosen Penev
2ecc386121
[clang-tidy] Remove redundant c_str
Found with readability-redundant-string-cstr
2019-12-26 21:25:07 -08:00
Rosen Penev
d1e82b59bb
[clang-tidy] Switch from size to empty check
Found with readability-container-size-empty
2019-12-26 20:07:53 -08:00
ridiculousfish
df0681d393 Remove process_generation_count_t
It was unused.
2019-12-26 13:33:14 -08:00
ridiculousfish
126e6a929f Remove the ARRAY_SEP define
It's unused.
2019-12-26 13:17:34 -08:00
Fabian Homborg
e986970f4d Remove vi_arg_digit and vi_delete_to
They don't do anything anymore.
2019-12-25 10:44:27 +01: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
Mathieu Duponchelle
15c1b3ed4b Place fish in its own process group when launched with -i
Fixes #5909
2019-12-23 10:32:37 +01:00
ridiculousfish
c19407ab0f Default parser_t::eval()'s block type to top
This is the parameter value at every call site except one. Just make it the
default.
2019-12-22 16:27:03 -08:00
ridiculousfish
0c49dce75d Factor block description part of stack traces into a new function 2019-12-22 16:22:20 -08:00
ridiculousfish
a59f35a378 Make block_type_t an enum class 2019-12-22 15:37:14 -08:00
ridiculousfish
4529e7d183 Reverse the order of the block stack
Previously, the block stack was a true stack. However in most cases, you
want to traverse the stack from the topmost frame down. This is awkward
to do with range-based for loops.

Switch it to pushing new blocks to the front of the block list.
This simplifies some traversals.
2019-12-22 15:07:41 -08:00
ridiculousfish
10ac83ae32 lru to use std::map, not std::unordered_map
We depend on pointer stability here and it just seems easier
to think about it with std::map.
2019-12-21 17:09:21 -08:00
ridiculousfish
2e7cbaeaba Remove io_file_t::is_dev_null
This is no longer used.
2019-12-20 14:47:54 -08: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
Johannes Altmanninger
3274dbacf4 Fix autosuggestions for time 2019-12-20 09:21:17 +01:00
Mahmoud Al-Qudsi
664d6fb132 Convert time to a job decorator 2019-12-19 23:02:23 -06:00
Mahmoud Al-Qudsi
ca18d88138 Switch to C++11 chrono's steady_clock for portability reasons
`clock_gettime()` is apparently not readily available on many fairly
recent *nix systems.

Closes #6440
2019-12-19 21:26:46 -06: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
ridiculousfish
0531c02ce4 Remove 'user_supplied' flag for io_fd_t
user_supplied was used to distinguish IO redirections which were
explicit, vs those that came about through "transmogrphication." But
transmogrification is no more. Remove the flag.
2019-12-19 14:14:23 -08:00
Rosen Penev
9936362599 common.cpp: Don't always include cxxabi.h
cxxabi.h is not available with LLVM's libcxx
2019-12-18 21:03:51 -06:00
Mahmoud Al-Qudsi
103726767c [time] Convince GCC control flow does not reach end of function 2019-12-18 20:53:38 -06:00
Mahmoud Al-Qudsi
704a90deec Rename time unit enum to please gcc 2019-12-18 20:48:02 -06:00
Mahmoud Al-Qudsi
be37656067 Support time -v for detailed time output
This breaks down the usage into fish vs external processes.
2019-12-18 20:28:15 -06:00
Mahmoud Al-Qudsi
1ed5e7e478 Dynamically choose units for time output 2019-12-18 20:27:12 -06:00
Mahmoud Al-Qudsi
5956270015 Add time builtin that understands fish script and external executables
This now works:

```fish
function foo
    for n in (seq 1 100000)
        test $n -eq 42
    end
end

time foo
```
2019-12-18 20:27:08 -06:00
Mahmoud Al-Qudsi
92401d8ebb Fix unused return result hack to work on macOS/GCC 7.4+
As of GCC 7.4 (at least under macOS 10.10), the previous workaround of
casting a must-use result to `(void)` to avoid warnings about unused
code no longer works.

This workaround is uglier but it quiets these warnings.
2019-12-18 12:43:13 -06:00
Mahmoud Al-Qudsi
60670999ad Fix mismatched wchar_t sign comparison errors on some platforms
The C++ spec (as of C++17/n4713) does not specify the sign of `wchar_t`,
saying only (in section 6.7.1: Fundamental Types)

> Type wchar_t shall have the same size, signedness, and alignment
> requirements (6.6.5) as one of the other integral types, called its
> underlying type.

On most *nix platforms on AMD64 architecture, `wchar_t` is a signed type
and can be compared with `int32_t` without incident, but on at least
some platforms (tested: clang under FreeBSD 12.1 on AARCH64), `wchar_t`
appears to be unsigned leading to sign comparison warnings:

```
../src/widecharwidth/widechar_width.h:512:48: warning: comparison of
integers of different signs: 'const wchar_t' and 'int32_t' (aka 'int')
[-Wsign-compare]

    return where != std::end(arr) && where->lo <= c;
```

This patch forces the use of wchar_t for the range start/end values in
`widechar_range` and the associated comparison values.
2019-12-18 12:32:04 -06:00
ridiculousfish
e52433d6fe Fix gcc warnings harder 2019-12-17 19:51:00 -08:00
ridiculousfish
7ab373fbbd Fix some gcc warnings 2019-12-17 19:22:43 -08:00
ridiculousfish
311f47d683 Remove an errant fprintf from the tests 2019-12-17 18:26:57 -08:00
ridiculousfish
d4daa28690 Correctly set the exit status in block and function processes
Previously, if the user control-C'd out of a process, we would set a
bogus exit status in the process, but it was difficult to observe this
because we would be cancelling anyways. But set it properly.
2019-12-17 18:19:38 -08:00
ridiculousfish
b3d2cdc0ff Unify parse_execution_result_t and eval_result_t again
Do other cleanup to better express the difference between cancellation
and control flow.
2019-12-17 18:12:49 -08:00
ridiculousfish
b82b111e55 Revert "Unify parse_execution_result_t and eval_result_t"
This reverts commit c011f3a8e9.

There is a bug where cancellation is being reported for normal control
flow, not just for SIGINT.
2019-12-17 17:31:18 -08:00
ridiculousfish
c011f3a8e9 Unify parse_execution_result_t and eval_result_t
These are just the same thing now; make everything eval_result_t.
2019-12-17 16:52:15 -08:00
ridiculousfish
a58286baea Remove parse_execution_skipped
This was unused.
2019-12-17 16:44:42 -08:00
ridiculousfish
ebc262cfba Fix sporadic cancellation test failures
If a Control-C is received during expanding a command substitution, we
may execute the job anyways, because we do not check for cancellation
after the expansion. Ensure that does not happen.

This should fix sporadic test failures in the cancellation unit test.
2019-12-17 16:42:14 -08:00
ridiculousfish
72de8768d9 Remove RUNNING_IN_XCODE checks
This is never set any more.
2019-12-17 16:19:12 -08:00
ridiculousfish
09f8e05b80 Clean up the return type of parser_t::eval
parser_t::eval indicates whether there was a parse error. It can be
easily confused with the status of the execution. Use a real type to
make it more clear.
2019-12-17 16:16:43 -08:00
ridiculousfish
8bf9f52461 Always detach new pthreads
There are no longer any calls to pthread_join. Just make all pthreads
detached.
2019-12-16 14:08:46 -08:00
Johannes Altmanninger
24562a9f49 avoid allocation on lookup of $fish_trace
Looking up a variable by a string literal implicitly constructs a wcstring.
By avoiding that, we get a noticeable reduction of temporary allocations.

$ HOME=. heaptrack ./fish -c true
heaptrack stats:			# baseline
        allocations:            7635
        leaked allocations:     3277
        temporary allocations:  602
heaptrack stats:			# new
        allocations:            7565
        leaked allocations:     3267
        temporary allocations:  530
2019-12-16 16:35:41 +01: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
David Adam
58535408b0 redirection: add a default to redirection_spec_t::oflags switch
Fixes a compiler warning/error.
2019-12-14 10:43:47 +08: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
1f83fb47ce Finish the IO cleanup.
Remove some dead code and add missing dtors.
2019-12-12 17:47:08 -08:00
ridiculousfish
5cd9de1049 Eliminate resolve_file_redirections_to_fds
Now that file redirections store FDs and not paths, this function has
nothing to do and can be removed.
2019-12-12 17:34:44 -08:00
ridiculousfish
33aff87c10 Switch io_file_t to store an fd, not a path
Prior to this fix, a file redirection was turned into an io_file_t. This is
annoying because every place where we want to apply the redirection, we
might fail due to open() failing. Switch to opening the file at the point
we resolve the redirection spec. This will simplify a lot of code.
2019-12-12 17:34:40 -08:00
ridiculousfish
af473d4d0c Introduce redirection_spec_t
Prior to this change, a process after it has been constructed by
parse_execution, but before it is executed, was given a list of
io_data_t redirections. The problem is that redirections have a
sensitive ownership policy because they hold onto fds. This made it
rather hard to reason about fd lifetime.

Change these to redirection_spec_t. This is a textual description
of a redirection after expansion. It does not represent an open file and
so its lifetime is no longer important.

This enables files to be held only on the stack, and are no longer owned
by a process of indeterminate lifetime.
2019-12-12 16:44:24 -08:00
ridiculousfish
be685faeb8 Clean up how pipe fd avoidance works
fish has to ensure that the pipes it creates do not conflict with any
explicit fds named in redirections. Switch this code to using
autoclose_fd_t to make the ownership logic more explicit, and also
introduce fd_set_t to reduce the dependence on io_chain_t.
2019-12-12 14:58:18 -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
970288c854 Migrate the read limit into parser_t::libdata
It is more natural here than "on the last bufferfill."
2019-12-11 11:50:52 -08:00
Johannes Altmanninger
3f10da0d01 Persist new zero-length universal variables
Because of default initialization to an empty list, code that relies on
set -U __fish_init_3_x was run every time.
2019-12-11 11:32:58 +01:00
ridiculousfish
0b1af1ace4 Correct the use of the constructed pointer in job lineage
This was always being set to a different pointer. Ensure the root job
shares its constructed pointer with its children.
2019-12-10 18:32:56 -08:00
ridiculousfish
1e15aa4c4a Correctly style io_chain_t::print 2019-12-10 17:21:03 -08:00
ridiculousfish
0573e95b03 Resurrect io_print 2019-12-10 17:18:37 -08:00
ridiculousfish
eaa87ff885 Correct a comment
A comment claimed that 0 was STDOUT_FILENO. In fact this should be 1.

Also default the fd for pipe_or_redir_t to -1, as 0 is not a sensible
default.
2019-12-10 16:14:34 -08:00
ridiculousfish
f136d634eb Collapse a job's "parent stuff" into a new type job_lineage_t
Currently a job needs to know three things about its "parents:"

1. Any IO redirections for the block or function containing this job
2. The pgid for the parent job
3. Whether the parent job has been fully constructed (to defer self-disown)

These are all tracked in somewhat separate awkward ways. Collapse them
into a single new type job_lineage_t.
2019-12-08 15:03:07 -08:00
ridiculousfish
e3b8203187 Implement logger_t::log_fmt for narrow chars by trampolining to wide chars
This satifies the glibc.
2019-12-08 14:47:52 -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
2ec8cd3bca Remove a dead variable 2019-12-07 11:28:22 -08:00
ridiculousfish
f575c55f5b Migrate the logic to make empty functions succeed into the performer
This is a more natural place for this logic.
2019-12-07 11:06:54 -08:00
ridiculousfish
a7f6fd22d4 Factor block and fish function execution into a "performer" std::function
In preparation for concurrent execution, invert the control of function and
block execution. Allow a process to return an std::function that performs the
the execution. This can be run on either the main or a background thread
(eventually).
2019-12-07 10:29:06 -08:00
Michael Jarvis
1be8277f41 Minor tweak to silence warning
Silences a clang++ warning:

"using the result of an assignment as a condition without parentheses"
2019-12-03 19:34:37 -08: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
ridiculousfish
fb8e90de28 clang-format .cpp files 2019-11-29 23:57:19 -08:00
Rosen Penev
e4a7e7d4f7 [clang-tidy] Use strcmp family properly
Found with bugprone-suspicious-string-compare

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2019-11-29 23:46:51 -08:00
Rosen Penev
e45ae9df49 [clang-tidy] Enclose macro arguments in ()
Found with bugprone-macro-parentheses

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2019-11-29 23:46:51 -08:00
Rosen Penev
9507c3a159 [clang-tidy] Don't initialize member functions
Found with readability-redundant-member-init

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2019-11-29 23:46:51 -08:00
Rosen Penev
6f4a9d527c [clang-tidy] Use C++ using instead of C typedef
Found with modernize-use-using

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2019-11-29 23:46:51 -08:00
Rosen Penev
7d1cc992e5 [clang-tidy] Simplify boolean expressions
Found with readability-simplify-boolean-expr

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2019-11-29 23:46:51 -08:00
Rosen Penev
4087b2ee15 [clang-tidy] Use bool literals
Found with modernize-use-bool-literals

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2019-11-29 23:46:50 -08:00
Rosen Penev
c3fa8c04bf [clang-tidy] Don't empty initialize strings
Found with readability-redundant-string-init

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2019-11-29 23:46:50 -08:00
ridiculousfish
b5d0075406 Use iothread pool for background fillthreads
Background fillthreads are used when we want to populate a buffer from an
external command. The most common is command substitution.

Prior to this commit, fish would spin up a fillthread whenever required.
This ended up being quite expensive.

Switch to using the iothread pool instead. This enables reusing the same
thread(s), which prevents needing to spawn new threads. This shows a big
perf win on the alias benchmark (766 -> 378 ms).
2019-11-27 12:03:56 -08:00
ridiculousfish
106af5f56a Migrate some iothread functions into member functions of thread_pool_t
This reintroduces commits 22230a1a0d
and 9d7d70c204, now with the bug fixed.

The problem was when there was one thread waiting in the pool. We enqueue
an item onto the pool and attempt to wake up the thread. But before the
thread runs, we enqueue another item - this second enqueue will see the
thread waiting and attempt to wake it up as well. If the two work items
were dependent (reader/writer) then we would have a deadlock.

The fix is to check if the number of waiting threads is at least as large
as the queue. If the number of enqueued items exceeds the number of waiting
threads, then spawn a new thread always.
2019-11-27 12:03:56 -08:00