aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--CONTRIBUTING.md6
-rwxr-xr-xbuild_tools/lint.fish23
-rwxr-xr-xbuild_tools/style.fish42
-rw-r--r--share/functions/__fish_cancel_commandline.fish13
-rw-r--r--share/functions/dirh.fish50
-rw-r--r--share/functions/fish_default_key_bindings.fish2
-rw-r--r--share/functions/fish_vi_key_bindings.fish10
-rw-r--r--src/autoload.cpp340
-rw-r--r--src/autoload.h154
-rw-r--r--src/builtin.cpp3764
-rw-r--r--src/builtin.h184
-rw-r--r--src/fish_indent.cpp14
-rw-r--r--src/fish_tests.cpp4
-rw-r--r--src/history.cpp1746
-rw-r--r--src/history.h336
-rw-r--r--src/parse_constants.h9
-rw-r--r--src/parse_productions.cpp8
-rw-r--r--src/parse_tree.cpp137
-rw-r--r--src/parse_tree.h4
-rw-r--r--tests/indent.in15
-rw-r--r--tests/indent.out10
-rw-r--r--tests/interactive.fish16
23 files changed, 2708 insertions, 4180 deletions
diff --git a/.gitignore b/.gitignore
index 717e3243..2420bb4e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -32,6 +32,7 @@ toc.txt
user_doc/
xcuserdata
test/
+tests/*.tmp.*
FISH-BUILD-VERSION-FILE
version
messages.pot
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 2c847319..e73bc344 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -40,7 +40,9 @@ The following sections discuss the specific rules for the style that should be u
make style
```
-before commiting your change. If you've already committed your changes that's okay since it will then check the files in the most recent commit. This can be useful after you've merged someone elses change and want to check that it's style is acceptable.
+before commiting your change. That will run `git-clang-format` to rewrite just the lines you're modifying.
+
+If you've already committed your changes that's okay since it will then check the files in the most recent commit. This can be useful after you've merged someone elses change and want to check that it's style is acceptable. However, in that case it will run `clang-format` to ensure the entire file, not just the lines modified by the commit, conform to the style.
If you want to check the style of the entire code base run
@@ -48,6 +50,8 @@ If you want to check the style of the entire code base run
make style-all
```
+That command will refuse to restyle any files if you have uncommitted changes.
+
### Suppressing Reformatting of the Code
If you have a good reason for doing so you can tell `clang-format` to not reformat a block of code by enclosing it in comments like this:
diff --git a/build_tools/lint.fish b/build_tools/lint.fish
index 96efc71b..287269da 100755
--- a/build_tools/lint.fish
+++ b/build_tools/lint.fish
@@ -34,13 +34,8 @@ if test $all = yes
else
# We haven't been asked to lint all the source. If there are uncommitted
# changes lint those, else lint the files in the most recent commit.
- set pending (git status --porcelain --short --untracked-files=all | sed -e 's/^ *//')
- if set -q pending[1]
- # There are pending changes so lint those files.
- for arg in $pending
- set files $files (string split -m 1 ' ' $arg)[2]
- end
- else
+ set files (git status --porcelain --short --untracked-files=all | sed -e 's/^ *[^ ]* *//')
+ if not set -q files[1]
# No pending changes so lint the files in the most recent commit.
set files (git show --word-diff=porcelain --name-only --pretty=oneline head)[2..-1]
end
@@ -56,10 +51,9 @@ if set -q c_files[1]
echo ========================================
echo Running cppcheck
echo ========================================
- # The stderr to stdout redirection is because cppcheck, incorrectly
- # IMHO, writes its diagnostic messages to stderr. Anyone running
- # this who wants to capture its output will expect those messages to be
- # written to stdout.
+ # The stderr to stdout redirection is because cppcheck, incorrectly IMHO, writes its
+ # diagnostic messages to stderr. Anyone running this who wants to capture its output will
+ # expect those messages to be written to stdout.
cppcheck -q --verbose --std=posix --std=c11 --language=c++ --template "[{file}:{line}]: {severity} ({id}): {message}" --suppress=missingIncludeSystem --inline-suppr --enable=$cppchecks $cppcheck_args $c_files 2>& 1
end
@@ -68,10 +62,9 @@ if set -q c_files[1]
echo ========================================
echo Running oclint
echo ========================================
- # The stderr to stdout redirection is because oclint, incorrectly
- # writes its final summary counts of the errors detected to stderr.
- # Anyone running this who wants to capture its output will expect those
- # messages to be written to stdout.
+ # The stderr to stdout redirection is because oclint, incorrectly writes its final summary
+ # counts of the errors detected to stderr. Anyone running this who wants to capture its
+ # output will expect those messages to be written to stdout.
if test (uname -s) = "Darwin"
if not test -f compile_commands.json
xcodebuild > xcodebuild.log
diff --git a/build_tools/style.fish b/build_tools/style.fish
index 76cf920d..62d8d5c7 100755
--- a/build_tools/style.fish
+++ b/build_tools/style.fish
@@ -6,6 +6,7 @@
# This runs C++ files and fish scripts (*.fish) through their respective code
# formatting programs.
#
+set git_clang_format no
set c_files
set f_files
set all no
@@ -21,17 +22,21 @@ if set -q argv[1]
end
if test $all = yes
+ set files (git status --porcelain --short --untracked-files=all | sed -e 's/^ *[^ ]* *//')
+ if set -q files[1]
+ echo
+ echo You have uncommited changes. Cowardly refusing to restyle the entire code base.
+ echo
+ exit 1
+ end
set c_files src/*.h src/*.cpp
set f_files ***.fish
else
- # We haven't been asked to reformat all the source. If there are uncommitted
- # changes reformat those, else reformat the files in the most recent commit.
- set pending (git status --porcelain --short --untracked-files=all | sed -e 's/^ *//')
- if count $pending > /dev/null
- # There are pending changes so lint those files.
- for arg in $pending
- set files $files (string split -m 1 ' ' $arg)[2]
- end
+ # We haven't been asked to reformat all the source. If there are uncommitted changes reformat
+ # those using `git clang-format`. Else reformat the files in the most recent commit.
+ set files (git status --porcelain --short --untracked-files=all | sed -e 's/^ *[^ ]* *//')
+ if set -q files[1]
+ set git_clang_format yes
else
# No pending changes so lint the files in the most recent commit.
set files (git show --name-only --pretty=oneline head | tail --lines=+2)
@@ -45,18 +50,32 @@ end
# Run the C++ reformatter if we have any C++ files.
if set -q c_files[1]
- if type -q clang-format
+ if test $git_clang_format = yes
+ if type -q git-clang-format
+ echo
+ echo ========================================
+ echo Running git-clang-format
+ echo ========================================
+ git add $c_files
+ git-clang-format
+ else
+ echo
+ echo 'WARNING: Cannot find git-clang-format command'
+ echo
+ end
+ else if type -q clang-format
echo
echo ========================================
echo Running clang-format
echo ========================================
for file in $c_files
- clang-format $file > $file.new
+ clang-format $file >$file.new
if cmp --quiet $file $file.new
echo $file was correctly formatted
rm $file.new
else
echo $file was NOT correctly formatted
+ chmod --reference=$file $file.new
mv $file.new $file
end
end
@@ -78,12 +97,13 @@ if set -q f_files[1]
echo Running fish_indent
echo ========================================
for file in $f_files
- fish_indent < $file > $file.new
+ fish_indent <$file >$file.new
if cmp --quiet $file $file.new
echo $file was correctly formatted
rm $file.new
else
echo $file was NOT correctly formatted
+ chmod --reference=$file $file.new
mv $file.new $file
end
end
diff --git a/share/functions/__fish_cancel_commandline.fish b/share/functions/__fish_cancel_commandline.fish
new file mode 100644
index 00000000..f10be75f
--- /dev/null
+++ b/share/functions/__fish_cancel_commandline.fish
@@ -0,0 +1,13 @@
+# This is meant to be bound to something like \cC.
+function __fish_cancel_commandline
+ set -l cmd (commandline)
+ if test -n "$cmd"
+ commandline -C 1000000
+ echo (set_color -b bryellow black)"^C"(set_color normal)
+ for i in (seq (commandline -L))
+ echo ""
+ end
+ commandline ""
+ commandline -f repaint
+ end
+end
diff --git a/share/functions/dirh.fish b/share/functions/dirh.fish
index 44244e8c..942b1ed9 100644
--- a/share/functions/dirh.fish
+++ b/share/functions/dirh.fish
@@ -1,38 +1,26 @@
+function dirh --description "Print the current directory history (the prev and next lists)"
+ if set -q argv[1]
+ switch $argv[1]
+ case -h --h --he --hel --help
+ __fish_print_help dirh
+ return 0
+ end
+ end
-function dirh --description "Print the current directory history (the back- and fwd- lists)"
-
- if count $argv >/dev/null
- switch $argv[1]
- case -h --h --he --hel --help
- __fish_print_help dirh
- return 0
- end
- end
-
- # Avoid set comment
- set -l current (command pwd)
- set -l separator " "
- set -l line_len (math (count $dirprev) + (echo $dirprev $current $dirnext | wc -m) )
- if test $line_len -gt $COLUMNS
- # Print one entry per line if history is long
- set separator "\n"
- end
-
- for i in $dirprev
- echo -n -e $i$separator
- end
+ set -l dirc (count $dirprev)
+ set -l dirprev_rev $dirprev[-1..1]
+ for i in (seq $dirc -1 1)
+ printf '%2d) %s\n' $i $dirprev_rev[$i]
+ end
- set_color $fish_color_history_current
- echo -n -e $current$separator
- set_color normal
+ echo (set_color $fish_color_history_current)' ' $PWD(set_color normal)
- # BSD seq 0 outputs '1 0' instead of nothing
- if count $dirnext > /dev/null
- for i in (seq (echo (count $dirnext)) -1 1)
- echo -n -e $dirnext[$i]$separator
+ set -l dirc (count $dirnext)
+ if test $dirc -gt 0
+ for i in (seq $dirc)
+ printf '%2d) %s\n' $i $dirnext[$i]
end
end
- echo
+ echo
end
-
diff --git a/share/functions/fish_default_key_bindings.fish b/share/functions/fish_default_key_bindings.fish
index 8d86c7ee..0f0d578c 100644
--- a/share/functions/fish_default_key_bindings.fish
+++ b/share/functions/fish_default_key_bindings.fish
@@ -109,7 +109,7 @@ function fish_default_key_bindings -d "Default (Emacs-like) key bindings for fis
bind $argv \el __fish_list_current_token
bind $argv \ew 'set tok (commandline -pt); if test $tok[1]; echo; whatis $tok[1]; commandline -f repaint; end'
bind $argv \cl 'clear; commandline -f repaint'
- bind $argv \cc 'commandline ""'
+ bind $argv \cc __fish_cancel_commandline
bind $argv \cu backward-kill-line
bind $argv \cw backward-kill-path-component
bind $argv \ed 'set -l cmd (commandline); if test -z "$cmd"; echo; dirh; commandline -f repaint; else; commandline -f kill-word; end'
diff --git a/share/functions/fish_vi_key_bindings.fish b/share/functions/fish_vi_key_bindings.fish
index f6395667..b1fc2659 100644
--- a/share/functions/fish_vi_key_bindings.fish
+++ b/share/functions/fish_vi_key_bindings.fish
@@ -22,22 +22,20 @@ function fish_vi_key_bindings --description 'vi-like key bindings for fish'
# Remove the default self-insert bindings in default mode
bind -e "" -M default
# Add way to kill current command line while in insert mode.
- bind -M insert \cc 'commandline ""'
+ bind -M insert \cc __fish_cancel_commandline
# Add a way to switch from insert to normal (command) mode.
bind -M insert -m default \e backward-char force-repaint
- #
- # normal (command) mode
- #
+ # Default (command) mode
bind :q exit
bind \cd exit
- bind \cc 'commandline ""'
+ bind -m insert \cc __fish_cancel_commandline
bind h backward-char
bind l forward-char
bind \e\[C forward-char
bind \e\[D backward-char
- # Some linux VTs output these (why?)
+ # Some terminals output these when they're in in keypad mode.
bind \eOC forward-char
bind \eOD backward-char
diff --git a/src/autoload.cpp b/src/autoload.cpp
index c5029362..1c081734 100644
--- a/src/autoload.cpp
+++ b/src/autoload.cpp
@@ -1,103 +1,81 @@
-/** \file autoload.cpp
-
-The classes responsible for autoloading functions and completions.
-*/
-
-#include "config.h" // IWYU pragma: keep
+// The classes responsible for autoloading functions and completions.
#include "autoload.h"
-#include "wutil.h"
-#include "common.h"
-#include "signal.h" // IWYU pragma: keep - needed for CHECK_BLOCK
-#include "env.h"
-#include "exec.h"
#include <assert.h>
#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>
#include <wchar.h>
+#include <algorithm>
#include <string>
#include <utility>
#include <vector>
-#include <algorithm>
+#include "common.h"
+#include "config.h" // IWYU pragma: keep
+#include "env.h"
+#include "exec.h"
+#include "signal.h" // IWYU pragma: keep - needed for CHECK_BLOCK
+#include "wutil.h"
-/* The time before we'll recheck an autoloaded file */
+// The time before we'll recheck an autoloaded file.
static const int kAutoloadStalenessInterval = 15;
-file_access_attempt_t access_file(const wcstring &path, int mode)
-{
- //printf("Touch %ls\n", path.c_str());
+file_access_attempt_t access_file(const wcstring &path, int mode) {
+ // printf("Touch %ls\n", path.c_str());
file_access_attempt_t result = {};
struct stat statbuf;
- if (wstat(path, &statbuf))
- {
+ if (wstat(path, &statbuf)) {
result.error = errno;
- }
- else
- {
+ } else {
result.mod_time = statbuf.st_mtime;
- if (waccess(path, mode))
- {
+ if (waccess(path, mode)) {
result.error = errno;
- }
- else
- {
+ } else {
result.accessible = true;
}
}
- // Note that we record the last checked time after the call, on the assumption that in a slow filesystem, the lag comes before the kernel check, not after.
+ // Note that we record the last checked time after the call, on the assumption that in a slow
+ // filesystem, the lag comes before the kernel check, not after.
result.stale = false;
result.last_checked = time(NULL);
return result;
}
-autoload_t::autoload_t(const wcstring &env_var_name_var, const builtin_script_t * const scripts, size_t script_count) :
- lock(),
- env_var_name(env_var_name_var),
- builtin_scripts(scripts),
- builtin_script_count(script_count)
-{
+autoload_t::autoload_t(const wcstring &env_var_name_var, const builtin_script_t *const scripts,
+ size_t script_count)
+ : lock(),
+ env_var_name(env_var_name_var),
+ builtin_scripts(scripts),
+ builtin_script_count(script_count) {
pthread_mutex_init(&lock, NULL);
}
-autoload_t::~autoload_t()
-{
- pthread_mutex_destroy(&lock);
-}
+autoload_t::~autoload_t() { pthread_mutex_destroy(&lock); }
-void autoload_t::node_was_evicted(autoload_function_t *node)
-{
- // This should only ever happen on the main thread
+void autoload_t::node_was_evicted(autoload_function_t *node) {
+ // This should only ever happen on the main thread.
ASSERT_IS_MAIN_THREAD();
- // Tell ourselves that the command was removed if it was loaded
- if (node->is_loaded)
- this->command_removed(node->key);
+ // Tell ourselves that the command was removed if it was loaded.
+ if (node->is_loaded) this->command_removed(node->key);
delete node;
}
-int autoload_t::unload(const wcstring &cmd)
-{
- return this->evict_node(cmd);
-}
+int autoload_t::unload(const wcstring &cmd) { return this->evict_node(cmd); }
-int autoload_t::load(const wcstring &cmd, bool reload)
-{
+int autoload_t::load(const wcstring &cmd, bool reload) {
int res;
CHECK_BLOCK(0);
ASSERT_IS_MAIN_THREAD();
env_var_t path_var = env_get_string(env_var_name);
- /*
- Do we know where to look?
- */
- if (path_var.empty())
- return 0;
+ // Do we know where to look?
+ if (path_var.empty()) return 0;
- /* Check if the lookup path has changed. If so, drop all loaded files. path_var may only be inspected on the main thread. */
- if (path_var != this->last_path)
- {
+ // Check if the lookup path has changed. If so, drop all loaded files. path_var may only be
+ // inspected on the main thread.
+ if (path_var != this->last_path) {
this->last_path = path_var;
this->last_path_tokenized.clear();
tokenize_variable_array(this->last_path, this->last_path_tokenized);
@@ -106,248 +84,215 @@ int autoload_t::load(const wcstring &cmd, bool reload)
this->evict_all_nodes();
}
- /* Mark that we're loading this. Hang onto the iterator for fast erasing later. Note that std::set has guarantees about not invalidating iterators, so this is safe to do across the callouts below. */
+ // Mark that we're loading this. Hang onto the iterator for fast erasing later. Note that
+ // std::set has guarantees about not invalidating iterators, so this is safe to do across the
+ // callouts below.
typedef std::set<wcstring>::iterator set_iterator_t;
std::pair<set_iterator_t, bool> insert_result = is_loading_set.insert(cmd);
set_iterator_t where = insert_result.first;
bool inserted = insert_result.second;
- /** Warn and fail on infinite recursion. It's OK to do this because this function is only called on the main thread. */
- if (! inserted)
- {
- /* We failed to insert */
- debug(0,
- _(L"Could not autoload item '%ls', it is already being autoloaded. "
- L"This is a circular dependency in the autoloading scripts, please remove it."),
+ // Warn and fail on infinite recursion. It's OK to do this because this function is only called
+ // on the main thread.
+ if (!inserted) {
+ // We failed to insert.
+ debug(0, _(L"Could not autoload item '%ls', it is already being autoloaded. "
+ L"This is a circular dependency in the autoloading scripts, please remove it."),
cmd.c_str());
return 1;
}
- /* Try loading it */
+ // Try loading it.
res = this->locate_file_and_maybe_load_it(cmd, true, reload, this->last_path_tokenized);
-
- /* Clean up */
+ // Clean up.
is_loading_set.erase(where);
-
return res;
}
-bool autoload_t::can_load(const wcstring &cmd, const env_vars_snapshot_t &vars)
-{
+bool autoload_t::can_load(const wcstring &cmd, const env_vars_snapshot_t &vars) {
const env_var_t path_var = vars.get(env_var_name);
- if (path_var.missing_or_empty())
- return false;
+ if (path_var.missing_or_empty()) return false;
std::vector<wcstring> path_list;
tokenize_variable_array(path_var, path_list);
return this->locate_file_and_maybe_load_it(cmd, false, false, path_list);
}
-static bool script_name_precedes_script_name(const builtin_script_t &script1, const builtin_script_t &script2)
-{
+static bool script_name_precedes_script_name(const builtin_script_t &script1,
+ const builtin_script_t &script2) {
return wcscmp(script1.name, script2.name) < 0;
}
-/** Check whether the given command is loaded. */
-bool autoload_t::has_tried_loading(const wcstring &cmd)
-{
+// Check whether the given command is loaded.
+bool autoload_t::has_tried_loading(const wcstring &cmd) {
scoped_lock locker(lock);
- autoload_function_t * func = this->get_node(cmd);
+ autoload_function_t *func = this->get_node(cmd);
return func != NULL;
}
-static bool is_stale(const autoload_function_t *func)
-{
- /** Return whether this function is stale. Internalized functions can never be stale. */
- return ! func->is_internalized && time(NULL) - func->access.last_checked > kAutoloadStalenessInterval;
+static bool is_stale(const autoload_function_t *func) {
+ // Return whether this function is stale. Internalized functions can never be stale.
+ return !func->is_internalized &&
+ time(NULL) - func->access.last_checked > kAutoloadStalenessInterval;
}
-autoload_function_t *autoload_t::get_autoloaded_function_with_creation(const wcstring &cmd, bool allow_eviction)
-{
+autoload_function_t *autoload_t::get_autoloaded_function_with_creation(const wcstring &cmd,
+ bool allow_eviction) {
ASSERT_IS_LOCKED(lock);
autoload_function_t *func = this->get_node(cmd);
- if (! func)
- {
+ if (!func) {
func = new autoload_function_t(cmd);
- if (allow_eviction)
- {
+ if (allow_eviction) {
this->add_node(func);
- }
- else
- {
+ } else {
this->add_node_without_eviction(func);
}
}
return func;
}
-/**
- This internal helper function does all the real work. By using two
- functions, the internal function can return on various places in
- the code, and the caller can take care of various cleanup work.
-
- cmd: the command name ('grep')
- really_load: whether to actually parse it as a function, or just check it it exists
- reload: whether to reload it if it's already loaded
- path_list: the set of paths to check
-
- Result: if really_load is true, returns whether the function was loaded. Otherwise returns whether the function existed.
-*/
-bool autoload_t::locate_file_and_maybe_load_it(const wcstring &cmd, bool really_load, bool reload, const wcstring_list_t &path_list)
-{
- /* Note that we are NOT locked in this function! */
+// This internal helper function does all the real work. By using two functions, the internal
+// function can return on various places in the code, and the caller can take care of various
+// cleanup work.
+//
+// cmd: the command name ('grep')
+// really_load: whether to actually parse it as a function, or just check it it exists
+// reload: whether to reload it if it's already loaded
+// path_list: the set of paths to check
+//
+// Result: if really_load is true, returns whether the function was loaded. Otherwise returns
+// whether the function existed.
+bool autoload_t::locate_file_and_maybe_load_it(const wcstring &cmd, bool really_load, bool reload,
+ const wcstring_list_t &path_list) {
+ // Note that we are NOT locked in this function!
bool reloaded = 0;
- /* Try using a cached function. If we really want the function to be loaded, require that it be really loaded. If we're not reloading, allow stale functions. */
+ // Try using a cached function. If we really want the function to be loaded, require that it be
+ // really loaded. If we're not reloading, allow stale functions.
{
- bool allow_stale_functions = ! reload;
+ bool allow_stale_functions = !reload;
- /* Take a lock */
scoped_lock locker(lock);
+ autoload_function_t *func = this->get_node(cmd); // get the function
- /* Get the function */
- autoload_function_t * func = this->get_node(cmd);
-
- /* Determine if we can use this cached function */
+ // Determine if we can use this cached function.
bool use_cached;
- if (! func)
- {
- /* Can't use a function that doesn't exist */
+ if (!func) {
+ // Can't use a function that doesn't exist.
use_cached = false;
- }
- else if (really_load && ! func->is_placeholder && ! func->is_loaded)
- {
- /* Can't use an unloaded function */
- use_cached = false;
- }
- else if (! allow_stale_functions && is_stale(func))
- {
- /* Can't use a stale function */
- use_cached = false;
- }
- else
- {
- /* I guess we can use it */
- use_cached = true;
+ } else if (really_load && !func->is_placeholder && !func->is_loaded) {
+ use_cached = false; // can't use an unloaded function
+ } else if (!allow_stale_functions && is_stale(func)) {
+ use_cached = false; // can't use a stale function
+ } else {
+ use_cached = true; // I guess we can use it
}
- /* If we can use this function, return whether we were able to access it */
- if (use_cached)
- {
+ // If we can use this function, return whether we were able to access it.
+ if (use_cached) {
assert(func != NULL);
return func->is_internalized || func->access.accessible;
}
}
- /* The source of the script will end up here */
+ // The source of the script will end up here.
wcstring script_source;
bool has_script_source = false;
- /* Whether we found an accessible file */
+ // Whether we found an accessible file.
bool found_file = false;
- /* Look for built-in scripts via a binary search */
+ // Look for built-in scripts via a binary search.
const builtin_script_t *matching_builtin_script = NULL;
- if (builtin_script_count > 0)
- {
+ if (builtin_script_count > 0) {
const builtin_script_t test_script = {cmd.c_str(), NULL};
const builtin_script_t *array_end = builtin_scripts + builtin_script_count;
- const builtin_script_t *found = std::lower_bound(builtin_scripts, array_end, test_script, script_name_precedes_script_name);
- if (found != array_end && ! wcscmp(found->name, test_script.name))
- {
- /* We found it */
+ const builtin_script_t *found = std::lower_bound(builtin_scripts, array_end, test_script,
+ script_name_precedes_script_name);
+ if (found != array_end && !wcscmp(found->name, test_script.name)) {
matching_builtin_script = found;
}
}
- if (matching_builtin_script)
- {
+ if (matching_builtin_script) {
has_script_source = true;
script_source = str2wcstring(matching_builtin_script->def);
- /* Make a node representing this function */
+ // Make a node representing this function.
scoped_lock locker(lock);
autoload_function_t *func = this->get_autoloaded_function_with_creation(cmd, really_load);
- /* This function is internalized */
+ // This function is internalized.
func->is_internalized = true;
- /* It's a fiction to say the script is loaded at this point, but we're definitely going to load it down below. */
+ // It's a fiction to say the script is loaded at this point, but we're definitely going to
+ // load it down below.
if (really_load) func->is_loaded = true;
}
- if (! has_script_source)
- {
- /* Iterate over path searching for suitable completion files */
- for (size_t i=0; i<path_list.size(); i++)
- {
+ if (!has_script_source) {
+ // Iterate over path searching for suitable completion files.
+ for (size_t i = 0; i < path_list.size(); i++) {
wcstring next = path_list.at(i);
wcstring path = next + L"/" + cmd + L".fish";
const file_access_attempt_t access = access_file(path, R_OK);
- if (access.accessible)
- {
- /* Found it! */
+ if (access.accessible) {
found_file = true;
- /* Now we're actually going to take the lock. */
+ // Now we're actually going to take the lock.
scoped_lock locker(lock);
autoload_function_t *func = this->get_node(cmd);
- /* Generate the source if we need to load it */
- bool need_to_load_function = really_load && (func == NULL || func->access.mod_time != access.mod_time || ! func->is_loaded);
- if (need_to_load_function)
- {
-
- /* Generate the script source */
+ // Generate the source if we need to load it.
+ bool need_to_load_function =
+ really_load &&
+ (func == NULL || func->access.mod_time != access.mod_time || !func->is_loaded);
+ if (need_to_load_function) {
+ // Generate the script source.
wcstring esc = escape_string(path, 1);
script_source = L"source " + esc;
has_script_source = true;
- /* Remove any loaded command because we are going to reload it. Note that this will deadlock if command_removed calls back into us. */
- if (func && func->is_loaded)
- {
+ // Remove any loaded command because we are going to reload it. Note that this
+ // will deadlock if command_removed calls back into us.
+ if (func && func->is_loaded) {
command_removed(cmd);
func->is_placeholder = false;
}
- /* Mark that we're reloading it */
+ // Mark that we're reloading it.
reloaded = true;
}
- /* Create the function if we haven't yet. This does not load it. Do not trigger eviction unless we are actually loading, because we don't want to evict off of the main thread. */
- if (! func)
- {
+ // Create the function if we haven't yet. This does not load it. Do not trigger
+ // eviction unless we are actually loading, because we don't want to evict off of
+ // the main thread.
+ if (!func) {
func = get_autoloaded_function_with_creation(cmd, really_load);
}
- /* It's a fiction to say the script is loaded at this point, but we're definitely going to load it down below. */
+ // It's a fiction to say the script is loaded at this point, but we're definitely
+ // going to load it down below.
if (need_to_load_function) func->is_loaded = true;
- /* Unconditionally record our access time */
+ // Unconditionally record our access time.
func->access = access;
break;
}
}
- /*
- If no file or builtin script was found we insert a placeholder function.
- Later we only research if the current time is at least five seconds later.
- This way, the files won't be searched over and over again.
- */
- if (! found_file && ! has_script_source)
- {
+ // If no file or builtin script was found we insert a placeholder function. Later we only
+ // research if the current time is at least five seconds later. This way, the files won't be
+ // searched over and over again.
+ if (!found_file && !has_script_source) {
scoped_lock locker(lock);
- /* Generate a placeholder */
+ // Generate a placeholder.
autoload_function_t *func = this->get_node(cmd);
- if (! func)
- {
+ if (!func) {
func = new autoload_function_t(cmd);
func->is_placeholder = true;
- if (really_load)
- {
+ if (really_load) {
this->add_node(func);
- }
- else
- {
+ } else {
this->add_node_without_eviction(func);
}
}
@@ -355,22 +300,15 @@ bool autoload_t::locate_file_and_maybe_load_it(const wcstring &cmd, bool really_
}
}
- /* If we have a script, either built-in or a file source, then run it */
- if (really_load && has_script_source)
- {
- if (exec_subshell(script_source, false /* do not apply exit status */) == -1)
- {
- /* Do nothing on failure */
- }
-
+ // If we have a script, either built-in or a file source, then run it.
+ if (really_load && has_script_source) {
+ // Do nothing on failure.
+ exec_subshell(script_source, false /* do not apply exit status */);
}
- if (really_load)
- {
+ if (really_load) {
return reloaded;
- }
- else
- {
+ } else {
return found_file || has_script_source;
}
}
diff --git a/src/autoload.h b/src/autoload.h
index f0ad9d03..5f8b4d31 100644
--- a/src/autoload.h
+++ b/src/autoload.h
@@ -1,8 +1,4 @@
-/** \file autoload.h
-
- The classes responsible for autoloading functions and completions.
-*/
-
+// The classes responsible for autoloading functions and completions.
#ifndef FISH_AUTOLOAD_H
#define FISH_AUTOLOAD_H
@@ -13,118 +9,104 @@
#include "common.h"
#include "lru.h"
-/** A struct responsible for recording an attempt to access a file. */
-struct file_access_attempt_t
-{
- time_t mod_time; /** The modification time of the file */
- time_t last_checked; /** When we last checked the file */
- bool accessible; /** Whether we believe we could access this file */
- bool stale; /** Whether the access attempt is stale */
- int error; /** If we could not access the file, the error code */
+// A struct responsible for recording an attempt to access a file.
+struct file_access_attempt_t {
+ time_t mod_time; // modification time of the file
+ time_t last_checked; // when we last checked the file
+ bool accessible; // whether we believe we could access this file
+ bool stale; // whether the access attempt is stale
+ int error; // if we could not access the file, the error code
};
file_access_attempt_t access_file(const wcstring &path, int mode);
-struct autoload_function_t : public lru_node_t
-{
- explicit autoload_function_t(const wcstring &key) : lru_node_t(key), access(), is_loaded(false), is_placeholder(false), is_internalized(false) { }
- file_access_attempt_t access; /** The last access attempt */
- bool is_loaded; /** Whether we have actually loaded this function */
- bool is_placeholder; /** Whether we are a placeholder that stands in for "no such function". If this is true, then is_loaded must be false. */
- bool is_internalized; /** Whether this function came from a builtin "internalized" script */
+struct autoload_function_t : public lru_node_t {
+ explicit autoload_function_t(const wcstring &key)
+ : lru_node_t(key),
+ access(),
+ is_loaded(false),
+ is_placeholder(false),
+ is_internalized(false) {}
+ file_access_attempt_t access; // the last access attempt
+ bool is_loaded; // whether we have actually loaded this function
+ // Whether we are a placeholder that stands in for "no such function". If this is true, then
+ // is_loaded must be false.
+ bool is_placeholder;
+ // Whether this function came from a builtin "internalized" script.
+ bool is_internalized;
};
-struct builtin_script_t
-{
+struct builtin_script_t {
const wchar_t *name;
const char *def;
};
class env_vars_snapshot_t;
-/**
- A class that represents a path from which we can autoload, and the autoloaded contents.
- */
-class autoload_t : private lru_cache_t<autoload_function_t>
-{
-private:
-
- /** Lock for thread safety */
+// A class that represents a path from which we can autoload, and the autoloaded contents.
+class autoload_t : private lru_cache_t<autoload_function_t> {
+ private:
+ // Lock for thread safety.
pthread_mutex_t lock;
-
- /** The environment variable name */
+ // The environment variable name.
const wcstring env_var_name;
-
- /** Builtin script array */
+ // Builtin script array.
const struct builtin_script_t *const builtin_scripts;
-
- /** Builtin script count */
+ // Builtin script count.
const size_t builtin_script_count;
-
- /** The path from which we most recently autoloaded */
+ // The path from which we most recently autoloaded.
wcstring last_path;
-
- /** That path, tokenized (split on separators) */
+ // That path, tokenized (split on separators).
wcstring_list_t last_path_tokenized;
- /**
- A table containing all the files that are currently being
- loaded. This is here to help prevent recursion.
- */
+ // A table containing all the files that are currently being loaded. This is here to help
+ // prevent recursion.
std::set<wcstring> is_loading_set;
- void remove_all_functions(void)
- {
- this->evict_all_nodes();
- }
+ void remove_all_functions(void) { this->evict_all_nodes(); }
- bool locate_file_and_maybe_load_it(const wcstring &cmd, bool really_load, bool reload, const wcstring_list_t &path_list);
+ bool locate_file_and_maybe_load_it(const wcstring &cmd, bool really_load, bool reload,
+ const wcstring_list_t &path_list);
virtual void node_was_evicted(autoload_function_t *node);
- autoload_function_t *get_autoloaded_function_with_creation(const wcstring &cmd, bool allow_eviction);
-
-protected:
- /** Overridable callback for when a command is removed */
- virtual void command_removed(const wcstring &cmd) { }
-
-public:
-
- /** Create an autoload_t for the given environment variable name */
- autoload_t(const wcstring &env_var_name_var, const builtin_script_t *scripts, size_t script_count);
-
- /** Destructor */
- virtual ~autoload_t();
-
- /**
- Autoload the specified file, if it exists in the specified path. Do
- not load it multiple times unless its timestamp changes or
- parse_util_unload is called.
-
- Autoloading one file may unload another.
-
- \param cmd the filename to search for. The suffix '.fish' is always added to this name
- \param on_unload a callback function to run if a suitable file is found, which has not already been run. unload will also be called for old files which are unloaded.
- \param reload wheter to recheck file timestamps on already loaded files
- */
+ autoload_function_t *get_autoloaded_function_with_creation(const wcstring &cmd,
+ bool allow_eviction);
+
+ protected:
+ // Overridable callback for when a command is removed.
+ virtual void command_removed(const wcstring &cmd) {}
+
+ public:
+ // Create an autoload_t for the given environment variable name.
+ autoload_t(const wcstring &env_var_name_var, const builtin_script_t *scripts,
+ size_t script_count);
+
+ virtual ~autoload_t(); // destructor
+
+ // Autoload the specified file, if it exists in the specified path. Do not load it multiple
+ // times unless its timestamp changes or parse_util_unload is called.
+ //
+ // Autoloading one file may unload another.
+ //
+ // \param cmd the filename to search for. The suffix '.fish' is always added to this name
+ // \param on_unload a callback function to run if a suitable file is found, which has not
+ // already been run. unload will also be called for old files which are unloaded.
+ // \param reload wheter to recheck file timestamps on already loaded files
int load(const wcstring &cmd, bool reload);
- /** Check whether we have tried loading the given command. Does not do any I/O. */
+ // Check whether we have tried loading the given command. Does not do any I/O.
bool has_tried_loading(const wcstring &cmd);
- /**
- Tell the autoloader that the specified file, in the specified path,
- is no longer loaded.
-
- \param cmd the filename to search for. The suffix '.fish' is always added to this name
- \param on_unload a callback function which will be called before (re)loading a file, may be used to unload the previous file.
- \return non-zero if the file was removed, zero if the file had not yet been loaded
- */
+ // Tell the autoloader that the specified file, in the specified path, is no longer loaded.
+ //
+ // \param cmd the filename to search for. The suffix '.fish' is always added to this name
+ // \param on_unload a callback function which will be called before (re)loading a file, may be
+ // used to unload the previous file.
+ // \return non-zero if the file was removed, zero if the file had not yet been loaded
int unload(const wcstring &cmd);
- /** Check whether the given command could be loaded, but do not load it. */
+ // Check whether the given command could be loaded, but do not load it.
bool can_load(const wcstring &cmd, const env_vars_snapshot_t &vars);
-
};
-
#endif
diff --git a/src/builtin.cpp b/src/builtin.cpp
index 86213a76..87b69b6c 100644
--- a/src/builtin.cpp
+++ b/src/builtin.cpp
@@ -1,186 +1,146 @@
-/** \file builtin.c
- Functions for executing builtin functions.
+// Functions for executing builtin functions.
+//
+// How to add a new builtin function:
+//
+// 1). Create a function in builtin.c with the following signature:
+//
+// <tt>static int builtin_NAME( parser_t &parser, wchar_t ** args )</tt>
+//
+// where NAME is the name of the builtin, and args is a zero-terminated list of arguments.
+//
+// 2). Add a line like { L"NAME", &builtin_NAME, N_(L"Bla bla bla") }, to the builtin_data_t
+// variable. The description is used by the completion system. Note that this array is sorted.
+//
+// 3). Create a file doc_src/NAME.txt, containing the manual for the builtin in Doxygen-format.
+// Check the other builtin manuals for proper syntax.
+//
+// 4). Use 'git add doc_src/NAME.txt' to start tracking changes to the documentation file.
+#include "config.h" // IWYU pragma: keep
- How to add a new builtin function:
-
- 1). Create a function in builtin.c with the following signature:
-
- <tt>static int builtin_NAME( parser_t &parser, wchar_t ** args )</tt>
-
- where NAME is the name of the builtin, and args is a zero-terminated list of arguments.
-
- 2). Add a line like { L"NAME", &builtin_NAME, N_(L"Bla bla bla") }, to the builtin_data_t variable. The description is used by the completion system. Note that this array is sorted!
-
- 3). Create a file doc_src/NAME.txt, containing the manual for the builtin in Doxygen-format. Check the other builtin manuals for proper syntax.
-
- 4). Use 'git add doc_src/NAME.txt' to start tracking changes to the documentation file.
-
-*/
-
-#include "config.h" // IWYU pragma: keep
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <wchar.h>
-#include <unistd.h>
+#include <assert.h>
#include <errno.h>
-#include <sys/stat.h>
#include <fcntl.h>
-#include <string.h>
#include <signal.h>
-#include <wctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
#include <time.h>
-#include <stack>
-#include <assert.h>
+#include <unistd.h>
+#include <wchar.h>
+#include <wctype.h>
#include <algorithm>
#include <map>
+#include <stack>
#include <string>
#include <utility>
-#include "fallback.h" // IWYU pragma: keep
+#include "fallback.h" // IWYU pragma: keep
-#include "wutil.h"
#include "builtin.h"
-#include "function.h"
#include "complete.h"
-#include "proc.h"
-#include "parser.h"
-#include "reader.h"
#include "env.h"
-#include "wgetopt.h"
-#include "tokenizer.h"
-#include "input.h"
-#include "intern.h"
#include "event.h"
-#include "signal.h"
#include "exec.h"
+#include "expand.h"
+#include "function.h"
#include "highlight.h"
+#include "history.h"
+#include "input.h"
+#include "intern.h"
+#include "parse_constants.h"
+#include "parse_tree.h"
#include "parse_util.h"
+#include "parser.h"
#include "parser_keywords.h"
-#include "expand.h"
#include "path.h"
-#include "history.h"
-#include "parse_tree.h"
-#include "parse_constants.h"
+#include "proc.h"
+#include "reader.h"
+#include "signal.h"
+#include "tokenizer.h"
#include "wcstringutil.h"
+#include "wgetopt.h"
+#include "wutil.h"
-/**
- The default prompt for the read command
-*/
+// The default prompt for the read command.
#define DEFAULT_READ_PROMPT L"set_color green; echo -n read; set_color normal; echo -n \"> \""
-/**
- The mode name to pass to history and input
-*/
-
+// The mode name to pass to history and input.
#define READ_MODE_NAME L"fish_read"
-/**
- The send stuff to foreground message
-*/
-#define FG_MSG _( L"Send job %d, '%ls' to foreground\n" )
+// The send stuff to foreground message.
+#define FG_MSG _(L"Send job %d, '%ls' to foreground\n")
-/**
- Datastructure to describe a builtin.
-*/
-struct builtin_data_t
-{
- /**
- Name of the builtin
- */
+// Datastructure to describe a builtin.
+struct builtin_data_t {
+ // Name of the builtin.
const wchar_t *name;
- /**
- Function pointer tothe builtin implementation
- */
+ // Function pointer tothe builtin implementation.
int (*func)(parser_t &parser, io_streams_t &streams, wchar_t **argv);
- /**
- Description of what the builtin does
- */
+ // Description of what the builtin does.
const wchar_t *desc;
bool operator<(const wcstring &) const;
bool operator<(const builtin_data_t *) const;
};
-bool builtin_data_t::operator<(const wcstring &other) const
-{
+bool builtin_data_t::operator<(const wcstring &other) const {
return wcscmp(this->name, other.c_str()) < 0;
}
-bool builtin_data_t::operator<(const builtin_data_t *other) const
-{
+bool builtin_data_t::operator<(const builtin_data_t *other) const {
return wcscmp(this->name, other->name) < 0;
}
-/**
- Counts the number of non null pointers in the specified array
-*/
-int builtin_count_args(const wchar_t * const * argv)
-{
+// Counts the number of non null pointers in the specified array.
+int builtin_count_args(const wchar_t *const *argv) {
int argc = 1;
- while (argv[argc] != NULL)
- {
+ while (argv[argc] != NULL) {
argc++;
}
return argc;
}
-/**
- This function works like wperror, but it prints its result into
- the streams.err string instead of to stderr. Used by the builtin
- commands.
-*/
-
-static void builtin_wperror(const wchar_t *s, io_streams_t &streams)
-{
+// This function works like wperror, but it prints its result into the streams.err string instead of
+// to stderr. Used by the builtin commands.
+static void builtin_wperror(const wchar_t *s, io_streams_t &streams) {
char *err = strerror(errno);
- if (s != NULL)
- {
+ if (s != NULL) {
streams.err.append(s);
streams.err.append(L": ");
}
- if (err != NULL)
- {
+ if (err != NULL) {
const wcstring werr = str2wcstring(err);
streams.err.append(werr);
streams.err.push_back(L'\n');
}
}
-/**
- Count the number of times the specified character occurs in the specified string
-*/
-static int count_char(const wchar_t *str, wchar_t c)
-{
+// Count the number of times the specified character occurs in the specified string.
+static int count_char(const wchar_t *str, wchar_t c) {
int res = 0;
- for (; *str; str++)
- {
- res += (*str==c);
+ for (; *str; str++) {
+ res += (*str == c);
}
return res;
}
-wcstring builtin_help_get(parser_t &parser, io_streams_t &streams, const wchar_t *name)
-{
- /* This won't ever work if no_exec is set */
- if (no_exec)
- return wcstring();
+wcstring builtin_help_get(parser_t &parser, io_streams_t &streams, const wchar_t *name) {
+ // This won't ever work if no_exec is set.
+ if (no_exec) return wcstring();
wcstring_list_t lst;
wcstring out;
const wcstring name_esc = escape_string(name, 1);
wcstring cmd = format_string(L"__fish_print_help %ls", name_esc.c_str());
- if (!streams.out_is_redirected && isatty(STDOUT_FILENO))
- {
+ if (!streams.out_is_redirected && isatty(STDOUT_FILENO)) {
// since we're using a subshell, __fish_print_help can't tell we're in
// a terminal. Tell it ourselves.
int cols = common_get_width();
cmd = format_string(L"__fish_print_help --tty-width %d %ls", cols, name_esc.c_str());
}
- if (exec_subshell(cmd, lst, false /* don't apply exit status */) >= 0)
- {
- for (size_t i=0; i<lst.size(); i++)
- {
+ if (exec_subshell(cmd, lst, false /* don't apply exit status */) >= 0) {
+ for (size_t i = 0; i < lst.size(); i++) {
out.append(lst.at(i));
out.push_back(L'\n');
}
@@ -188,225 +148,165 @@ wcstring builtin_help_get(parser_t &parser, io_streams_t &streams, const wchar_t
return out;
}
-/**
- Print help for the specified builtin. If \c b is sb_err, also print
- the line information
-
- If \c b is the buffer representing standard error, and the help
- message is about to be printed to an interactive screen, it may be
- shortened to fit the screen.
-
-
-*/
-
-void builtin_print_help(parser_t &parser, io_streams_t &streams, const wchar_t *cmd, output_stream_t &b)
-{
+// Print help for the specified builtin. If \c b is sb_err, also print the line information.
+//
+// If \c b is the buffer representing standard error, and the help message is about to be printed to
+// an interactive screen, it may be shortened to fit the screen.
+void builtin_print_help(parser_t &parser, io_streams_t &streams, const wchar_t *cmd,
+ output_stream_t &b) {
bool is_stderr = (&b == &streams.err);
- if (is_stderr)
- {
+ if (is_stderr) {
b.append(parser.current_line());
}
-
+
const wcstring h = builtin_help_get(parser, streams, cmd);
-
- if (!h.size())
- return;
-
+
+ if (!h.size()) return;
+
wchar_t *str = wcsdup(h.c_str());
- if (str)
- {
+ if (str) {
bool is_short = false;
- if (is_stderr)
- {
-
- /*
- Interactive mode help to screen - only print synopsis if
- the rest won't fit
- */
-
+ if (is_stderr) {
+ // Interactive mode help to screen - only print synopsis if the rest won't fit.
int screen_height, lines;
-
+
screen_height = common_get_height();
lines = count_char(str, L'\n');
- if (! get_is_interactive() || (lines > 2*screen_height/3))
- {
+ if (!get_is_interactive() || (lines > 2 * screen_height / 3)) {
wchar_t *pos;
- int cut=0;
+ int cut = 0;
int i;
-
+
is_short = true;
-
- /*
- First move down 4 lines
- */
-
+
+ // First move down 4 lines.
pos = str;
- for (i=0; (i<4) && pos && *pos; i++)
- {
- pos = wcschr(pos+1, L'\n');
+ for (i = 0; (i < 4) && pos && *pos; i++) {
+ pos = wcschr(pos + 1, L'\n');
}
-
- if (pos && *pos)
- {
-
- /*
- Then find the next empty line
- */
- for (; *pos; pos++)
- {
- if (*pos == L'\n')
- {
+
+ if (pos && *pos) {
+ // Then find the next empty line.
+ for (; *pos; pos++) {
+ if (*pos == L'\n') {
wchar_t *pos2;
int is_empty = 1;
-
- for (pos2 = pos+1; *pos2; pos2++)
- {
- if (*pos2 == L'\n')
- break;
-
- if (*pos2 != L'\t' && *pos2 !=L' ')
- {
+
+ for (pos2 = pos + 1; *pos2; pos2++) {
+ if (*pos2 == L'\n') break;
+
+ if (*pos2 != L'\t' && *pos2 != L' ') {
is_empty = 0;
break;
}
}
- if (is_empty)
- {
- /*
- And cut it
- */
- *(pos2+1)=L'\0';
+ if (is_empty) {
+ // And cut it.
+ *(pos2 + 1) = L'\0';
cut = 1;
break;
}
}
}
}
-
- /*
- We did not find a good place to cut message to
- shorten it - so we make sure we don't print
- anything.
- */
- if (!cut)
- {
+
+ // We did not find a good place to cut message to shorten it - so we make sure we
+ // don't print anything.
+ if (!cut) {
*str = 0;
}
-
}
}
-
+
b.append(str);
- if (is_short)
- {
+ if (is_short) {
b.append_format(_(L"%ls: Type 'help %ls' for related documentation\n\n"), cmd, cmd);
}
-
+
free(str);
}
}
-
-/**
- Perform error reporting for encounter with unknown option
-*/
-static void builtin_unknown_option(parser_t &parser, io_streams_t &streams, const wchar_t *cmd, const wchar_t *opt)
-{
+// Perform error reporting for encounter with unknown option.
+static void builtin_unknown_option(parser_t &parser, io_streams_t &streams, const wchar_t *cmd,
+ const wchar_t *opt) {
streams.err.append_format(BUILTIN_ERR_UNKNOWN, cmd, opt);
builtin_print_help(parser, streams, cmd, streams.err);
}
-/**
- Perform error reporting for encounter with missing argument
-*/
-static void builtin_missing_argument(parser_t &parser, io_streams_t &streams, const wchar_t *cmd, const wchar_t *opt)
-{
+// Perform error reporting for encounter with missing argument.
+static void builtin_missing_argument(parser_t &parser, io_streams_t &streams, const wchar_t *cmd,
+ const wchar_t *opt) {
streams.err.append_format(BUILTIN_ERR_MISSING, cmd, opt);
builtin_print_help(parser, streams, cmd, streams.err);
}
-/*
- Here follows the definition of all builtin commands. The function
- names are all on the form builtin_NAME where NAME is the name of the
- builtin. so the function name for the builtin 'fg' is
- 'builtin_fg'.
-
- A few builtins, including 'while', 'command' and 'builtin' are not
- defined here as they are handled directly by the parser. (They are
- not parsed as commands, instead they only alter the parser state)
-
- The builtins 'break' and 'continue' are so closely related that they
- share the same implementation, namely 'builtin_break_continue.
-
- Several other builtins, including jobs, ulimit and set are so big
- that they have been given their own file. These files are all named
- 'builtin_NAME.c', where NAME is the name of the builtin. These files
- are included directly below.
-
-*/
-
-
-#include "builtin_set.cpp"
+// Here follows the definition of all builtin commands. The function names are all on the form
+// builtin_NAME where NAME is the name of the builtin. so the function name for the builtin 'fg' is
+// 'builtin_fg'.
+//
+// A few builtins, including 'while', 'command' and 'builtin' are not defined here as they are
+// handled directly by the parser. (They are not parsed as commands, instead they only alter the
+// parser state)
+//
+// The builtins 'break' and 'continue' are so closely related that they share the same
+// implementation, namely 'builtin_break_continue.
+//
+// Several other builtins, including jobs, ulimit and set are so big that they have been given their
+// own file. These files are all named 'builtin_NAME.c', where NAME is the name of the builtin.
+// These files are included directly below.
#include "builtin_commandline.cpp"
#include "builtin_complete.cpp"
-#include "builtin_ulimit.cpp"
#include "builtin_jobs.cpp"
-#include "builtin_set_color.cpp"
#include "builtin_printf.cpp"
+#include "builtin_set.cpp"
+#include "builtin_set_color.cpp"
+#include "builtin_ulimit.cpp"
-/* builtin_test lives in builtin_test.cpp */
+// builtin_test lives in builtin_test.cpp
int builtin_test(parser_t &parser, io_streams_t &streams, wchar_t **argv);
-/* builtin_string lives in builtin_string.cpp */
+// builtin_string lives in builtin_string.cpp
int builtin_string(parser_t &parser, io_streams_t &streams, wchar_t **argv);
-/**
- List a single key binding.
- Returns false if no binding with that sequence and mode exists.
- */
-static bool builtin_bind_list_one(const wcstring &seq, const wcstring &bind_mode, io_streams_t &streams)
-{
+// List a single key binding.
+// Returns false if no binding with that sequence and mode exists.
+static bool builtin_bind_list_one(const wcstring &seq, const wcstring &bind_mode,
+ io_streams_t &streams) {
std::vector<wcstring> ecmds;
wcstring sets_mode;
- if (!input_mapping_get(seq, bind_mode, &ecmds, &sets_mode))
- {
+ if (!input_mapping_get(seq, bind_mode, &ecmds, &sets_mode)) {
return false;
}
streams.out.append(L"bind");
- // Append the mode flags if applicable
- if (bind_mode != DEFAULT_BIND_MODE)
- {
+ // Append the mode flags if applicable.
+ if (bind_mode != DEFAULT_BIND_MODE) {
const wcstring emode = escape_string(bind_mode, ESCAPE_ALL);
streams.out.append(L" -M ");
streams.out.append(emode);
}
- if (sets_mode != bind_mode)
- {
+ if (sets_mode != bind_mode) {
const wcstring esets_mode = escape_string(sets_mode, ESCAPE_ALL);
streams.out.append(L" -m ");
streams.out.append(esets_mode);
}
- // Append the name
+ // Append the name.
wcstring tname;
- if (input_terminfo_get_name(seq, &tname))
- {
- // Note that we show -k here because we have an input key name
- streams.out.append_format( L" -k %ls", tname.c_str());
- }
- else
- {
- // No key name, so no -k; we show the escape sequence directly
+ if (input_terminfo_get_name(seq, &tname)) {
+ // Note that we show -k here because we have an input key name.
+ streams.out.append_format(L" -k %ls", tname.c_str());
+ } else {
+ // No key name, so no -k; we show the escape sequence directly.
const wcstring eseq = escape_string(seq, ESCAPE_ALL);
- streams.out.append_format( L" %ls", eseq.c_str());
+ streams.out.append_format(L" %ls", eseq.c_str());
}
- // Now show the list of commands
- for (size_t i = 0; i < ecmds.size(); i++)
- {
+ // Now show the list of commands.
+ for (size_t i = 0; i < ecmds.size(); i++) {
const wcstring &ecmd = ecmds.at(i);
const wcstring escaped_ecmd = escape_string(ecmd, ESCAPE_ALL);
streams.out.push_back(' ');
@@ -417,19 +317,13 @@ static bool builtin_bind_list_one(const wcstring &seq, const wcstring &bind_mode
return true;
}
-/**
- List all current key bindings
- */
-static void builtin_bind_list(const wchar_t *bind_mode, io_streams_t &streams)
-{
+// List all current key bindings.
+static void builtin_bind_list(const wchar_t *bind_mode, io_streams_t &streams) {
const std::vector<input_mapping_name_t> lst = input_mapping_get_names();
for (std::vector<input_mapping_name_t>::const_iterator it = lst.begin(), end = lst.end();
- it != end;
- ++it)
- {
- if (bind_mode != NULL && bind_mode != it->mode)
- {
+ it != end; ++it) {
+ if (bind_mode != NULL && bind_mode != it->mode) {
continue;
}
@@ -437,146 +331,106 @@ static void builtin_bind_list(const wchar_t *bind_mode, io_streams_t &streams)
}
}
-/**
- Print terminfo key binding names to string buffer used for standard output.
-
- \param all if set, all terminfo key binding names will be
- printed. If not set, only ones that are defined for this terminal
- are printed.
- */
-static void builtin_bind_key_names(int all, io_streams_t &streams)
-{
+// Print terminfo key binding names to string buffer used for standard output.
+//
+// \param all if set, all terminfo key binding names will be printed. If not set, only ones that
+// are defined for this terminal are printed.
+static void builtin_bind_key_names(int all, io_streams_t &streams) {
const wcstring_list_t names = input_terminfo_get_names(!all);
- for (size_t i=0; i<names.size(); i++)
- {
+ for (size_t i = 0; i < names.size(); i++) {
const wcstring &name = names.at(i);
- streams.out.append_format( L"%ls\n", name.c_str());
+ streams.out.append_format(L"%ls\n", name.c_str());
}
}
-/**
- Print all the special key binding functions to string buffer used for standard output.
-
- */
-static void builtin_bind_function_names(io_streams_t &streams)
-{
+// Print all the special key binding functions to string buffer used for standard output.
+static void builtin_bind_function_names(io_streams_t &streams) {
wcstring_list_t names = input_function_get_names();
- for (size_t i=0; i<names.size(); i++)
- {
+ for (size_t i = 0; i < names.size(); i++) {
const wchar_t *seq = names.at(i).c_str();
- streams.out.append_format( L"%ls\n", seq);
+ streams.out.append_format(L"%ls\n", seq);
}
}
// Wraps input_terminfo_get_sequence(), appending the correct error messages as needed.
-static int get_terminfo_sequence(const wchar_t *seq, wcstring *out_seq, io_streams_t &streams)
-{
- if (input_terminfo_get_sequence(seq, out_seq))
- {
+static int get_terminfo_sequence(const wchar_t *seq, wcstring *out_seq, io_streams_t &streams) {
+ if (input_terminfo_get_sequence(seq, out_seq)) {
return 1;
}
wcstring eseq = escape_string(seq, 0);
- switch (errno)
- {
- case ENOENT:
- {
- streams.err.append_format(_(L"%ls: No key with name '%ls' found\n"), L"bind", eseq.c_str());
+ switch (errno) {
+ case ENOENT: {
+ streams.err.append_format(_(L"%ls: No key with name '%ls' found\n"), L"bind",
+ eseq.c_str());
break;
}
-
- case EILSEQ:
- {
- streams.err.append_format(_(L"%ls: Key with name '%ls' does not have any mapping\n"), L"bind", eseq.c_str());
+ case EILSEQ: {
+ streams.err.append_format(_(L"%ls: Key with name '%ls' does not have any mapping\n"),
+ L"bind", eseq.c_str());
break;
}
-
- default:
- {
- streams.err.append_format(_(L"%ls: Unknown error trying to bind to key named '%ls'\n"), L"bind", eseq.c_str());
+ default: {
+ streams.err.append_format(_(L"%ls: Unknown error trying to bind to key named '%ls'\n"),
+ L"bind", eseq.c_str());
break;
}
}
return 0;
}
-/**
- Add specified key binding.
- */
-static int builtin_bind_add(const wchar_t *seq, const wchar_t * const *cmds, size_t cmds_len,
+// Add specified key binding.
+static int builtin_bind_add(const wchar_t *seq, const wchar_t *const *cmds, size_t cmds_len,
const wchar_t *mode, const wchar_t *sets_mode, int terminfo,
- io_streams_t &streams)
-{
-
- if (terminfo)
- {
+ io_streams_t &streams) {
+ if (terminfo) {
wcstring seq2;
- if (get_terminfo_sequence(seq, &seq2, streams))
- {
+ if (get_terminfo_sequence(seq, &seq2, streams)) {
input_mapping_add(seq2.c_str(), cmds, cmds_len, mode, sets_mode);
- }
- else
- {
+ } else {
return 1;
}
- }
- else
- {
+ } else {
input_mapping_add(seq, cmds, cmds_len, mode, sets_mode);
}
return 0;
-
}
-/**
- Erase specified key bindings
-
- \param seq an array of all key bindings to erase
- \param all if specified, _all_ key bindings will be erased
- \param mode if specified, only bindings from that mode will be erased. If not given and \c all is \c false, \c DEFAULT_BIND_MODE will be used.
- */
-static int builtin_bind_erase(wchar_t **seq, int all, const wchar_t *mode, int use_terminfo, io_streams_t &streams)
-{
- if (all)
- {
+// Erase specified key bindings
+//
+// \param seq an array of all key bindings to erase
+// \param all if specified, _all_ key bindings will be erased
+// \param mode if specified, only bindings from that mode will be erased. If not given and \c all is
+// \c false, \c DEFAULT_BIND_MODE will be used.
+static int builtin_bind_erase(wchar_t **seq, int all, const wchar_t *mode, int use_terminfo,
+ io_streams_t &streams) {
+ if (all) {
const std::vector<input_mapping_name_t> lst = input_mapping_get_names();
for (std::vector<input_mapping_name_t>::const_iterator it = lst.begin(), end = lst.end();
- it != end;
- ++it)
- {
- if (mode == NULL || mode == it->mode)
- {
+ it != end; ++it) {
+ if (mode == NULL || mode == it->mode) {
input_mapping_erase(it->seq, it->mode);
}
}
return 0;
- }
- else
- {
+ } else {
int res = 0;
if (mode == NULL) mode = DEFAULT_BIND_MODE;
- while (*seq)
- {
- if (use_terminfo)
- {
+ while (*seq) {
+ if (use_terminfo) {
wcstring seq2;
- if (get_terminfo_sequence(*seq++, &seq2, streams))
- {
+ if (get_terminfo_sequence(*seq++, &seq2, streams)) {
input_mapping_erase(seq2, mode);
- }
- else
- {
+ } else {
res = 1;
}
- }
- else
- {
+ } else {
input_mapping_erase(*seq++, mode);
}
}
@@ -585,205 +439,152 @@ static int builtin_bind_erase(wchar_t **seq, int all, const wchar_t *mode, int u
}
}
-
-/**
- The bind builtin, used for setting character sequences
-*/
-static int builtin_bind(parser_t &parser, io_streams_t &streams, wchar_t **argv)
-{
+// The bind builtin, used for setting character sequences.
+static int builtin_bind(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
wgetopter_t w;
- enum
- {
- BIND_INSERT,
- BIND_ERASE,
- BIND_KEY_NAMES,
- BIND_FUNCTION_NAMES
- };
-
- int argc=builtin_count_args(argv);
+ enum { BIND_INSERT, BIND_ERASE, BIND_KEY_NAMES, BIND_FUNCTION_NAMES };
+ int argc = builtin_count_args(argv);
int mode = BIND_INSERT;
int res = STATUS_BUILTIN_OK;
int all = 0;
-
const wchar_t *bind_mode = DEFAULT_BIND_MODE;
bool bind_mode_given = false;
const wchar_t *sets_bind_mode = DEFAULT_BIND_MODE;
bool sets_bind_mode_given = false;
-
int use_terminfo = 0;
- w.woptind=0;
+ w.woptind = 0;
- static const struct woption long_options[] =
- {
- { L"all", no_argument, 0, 'a' },
- { L"erase", no_argument, 0, 'e' },
- { L"function-names", no_argument, 0, 'f' },
- { L"help", no_argument, 0, 'h' },
- { L"key", no_argument, 0, 'k' },
- { L"key-names", no_argument, 0, 'K' },
- { L"mode", required_argument, 0, 'M' },
- { L"sets-mode", required_argument, 0, 'm' },
- { 0, 0, 0, 0 }
- };
+ static const struct woption long_options[] = {{L"all", no_argument, 0, 'a'},
+ {L"erase", no_argument, 0, 'e'},
+ {L"function-names", no_argument, 0, 'f'},
+ {L"help", no_argument, 0, 'h'},
+ {L"key", no_argument, 0, 'k'},
+ {L"key-names", no_argument, 0, 'K'},
+ {L"mode", required_argument, 0, 'M'},
+ {L"sets-mode", required_argument, 0, 'm'},
+ {0, 0, 0, 0}};
- while (1)
- {
+ while (1) {
int opt_index = 0;
- int opt = w.wgetopt_long(argc,
- argv,
- L"aehkKfM:m:",
- long_options,
- &opt_index);
+ int opt = w.wgetopt_long(argc, argv, L"aehkKfM:m:", long_options, &opt_index);
- if (opt == -1)
- break;
+ if (opt == -1) break;
- switch (opt)
- {
- case 0:
- if (long_options[opt_index].flag != 0)
- break;
- streams.err.append_format(BUILTIN_ERR_UNKNOWN,
- argv[0],
- long_options[opt_index].name);
+ switch (opt) {
+ case 0: {
+ if (long_options[opt_index].flag != 0) break;
+ streams.err.append_format(BUILTIN_ERR_UNKNOWN, argv[0],
+ long_options[opt_index].name);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
-
- case 'a':
+ }
+ case 'a': {
all = 1;
break;
-
- case 'e':
+ }
+ case 'e': {
mode = BIND_ERASE;
break;
-
- case 'h':
+ }
+ case 'h': {
builtin_print_help(parser, streams, argv[0], streams.out);
return STATUS_BUILTIN_OK;
-
- case 'k':
+ }
+ case 'k': {
use_terminfo = 1;
break;
-
- case 'K':
+ }
+ case 'K': {
mode = BIND_KEY_NAMES;
break;
-
- case 'f':
+ }
+ case 'f': {
mode = BIND_FUNCTION_NAMES;
break;
-
- case 'M':
+ }
+ case 'M': {
bind_mode = w.woptarg;
bind_mode_given = true;
break;
-
- case 'm':
+ }
+ case 'm': {
sets_bind_mode = w.woptarg;
sets_bind_mode_given = true;
break;
-
- case '?':
- builtin_unknown_option(parser, streams, argv[0], argv[w.woptind-1]);
+ }
+ case '?': {
+ builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
return STATUS_BUILTIN_ERROR;
-
-
+ }
}
}
- /*
- * if mode is given, but not new mode, default to new mode to mode
- */
- if (bind_mode_given && !sets_bind_mode_given)
- {
+ // if mode is given, but not new mode, default to new mode to mode.
+ if (bind_mode_given && !sets_bind_mode_given) {
sets_bind_mode = bind_mode;
}
- switch (mode)
- {
-
- case BIND_ERASE:
- {
- if (builtin_bind_erase(&argv[w.woptind], all, bind_mode_given ? bind_mode : NULL, use_terminfo, streams))
- {
+ switch (mode) {
+ case BIND_ERASE: {
+ if (builtin_bind_erase(&argv[w.woptind], all, bind_mode_given ? bind_mode : NULL,
+ use_terminfo, streams)) {
res = STATUS_BUILTIN_ERROR;
}
break;
}
-
- case BIND_INSERT:
- {
- switch (argc-w.woptind)
- {
- case 0:
- {
+ case BIND_INSERT: {
+ switch (argc - w.woptind) {
+ case 0: {
builtin_bind_list(bind_mode_given ? bind_mode : NULL, streams);
break;
}
-
- case 1:
- {
+ case 1: {
wcstring seq;
- if (use_terminfo)
- {
- if (!get_terminfo_sequence(argv[w.woptind], &seq, streams))
- {
+ if (use_terminfo) {
+ if (!get_terminfo_sequence(argv[w.woptind], &seq, streams)) {
res = STATUS_BUILTIN_ERROR;
- // get_terminfo_sequence already printed the error
+ // get_terminfo_sequence already printed the error.
break;
}
- }
- else
- {
+ } else {
seq = argv[w.woptind];
}
- if (!builtin_bind_list_one(seq, bind_mode, streams))
- {
+ if (!builtin_bind_list_one(seq, bind_mode, streams)) {
res = STATUS_BUILTIN_ERROR;
wcstring eseq = escape_string(argv[w.woptind], 0);
- if (use_terminfo)
- {
- streams.err.append_format(_(L"%ls: No binding found for key '%ls'\n"), argv[0], eseq.c_str());
- }
- else
- {
- streams.err.append_format(_(L"%ls: No binding found for sequence '%ls'\n"), argv[0], eseq.c_str());
+ if (use_terminfo) {
+ streams.err.append_format(_(L"%ls: No binding found for key '%ls'\n"),
+ argv[0], eseq.c_str());
+ } else {
+ streams.err.append_format(
+ _(L"%ls: No binding found for sequence '%ls'\n"), argv[0],
+ eseq.c_str());
}
}
break;
}
-
- default:
- {
- if (builtin_bind_add(argv[w.woptind], argv + (w.woptind + 1), argc - (w.woptind + 1), bind_mode, sets_bind_mode, use_terminfo, streams))
- {
+ default: {
+ if (builtin_bind_add(argv[w.woptind], argv + (w.woptind + 1),
+ argc - (w.woptind + 1), bind_mode, sets_bind_mode,
+ use_terminfo, streams)) {
res = STATUS_BUILTIN_ERROR;
}
break;
}
-
}
break;
}
-
- case BIND_KEY_NAMES:
- {
+ case BIND_KEY_NAMES: {
builtin_bind_key_names(all, streams);
break;
}
-
-
- case BIND_FUNCTION_NAMES:
- {
+ case BIND_FUNCTION_NAMES: {
builtin_bind_function_names(streams);
break;
}
-
-
- default:
- {
+ default: {
res = STATUS_BUILTIN_ERROR;
streams.err.append_format(_(L"%ls: Invalid state\n"), argv[0]);
break;
@@ -793,236 +594,156 @@ static int builtin_bind(parser_t &parser, io_streams_t &streams, wchar_t **argv)
return res;
}
-
-/**
- The block builtin, used for temporarily blocking events
-*/
-static int builtin_block(parser_t &parser, io_streams_t &streams, wchar_t **argv)
-{
+// The block builtin, used for temporarily blocking events.
+static int builtin_block(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
wgetopter_t w;
- enum
- {
+ enum {
UNSET,
GLOBAL,
LOCAL,
- }
- ;
+ };
- int scope=UNSET;
+ int scope = UNSET;
int erase = 0;
- int argc=builtin_count_args(argv);
+ int argc = builtin_count_args(argv);
- w.woptind=0;
+ w.woptind = 0;
- static const struct woption
- long_options[] =
- {
- {
- L"erase", no_argument, 0, 'e'
- }
- ,
- {
- L"local", no_argument, 0, 'l'
- }
- ,
- {
- L"global", no_argument, 0, 'g'
- }
- ,
- {
- L"help", no_argument, 0, 'h'
- }
- ,
- {
- 0, 0, 0, 0
- }
- }
- ;
+ static const struct woption long_options[] = {{L"erase", no_argument, 0, 'e'},
+ {L"local", no_argument, 0, 'l'},
+ {L"global", no_argument, 0, 'g'},
+ {L"help", no_argument, 0, 'h'},
+ {0, 0, 0, 0}};
- while (1)
- {
+ while (1) {
int opt_index = 0;
-
- int opt = w.wgetopt_long(argc,
- argv,
- L"elgh",
- long_options,
- &opt_index);
- if (opt == -1)
- break;
-
- switch (opt)
- {
- case 0:
- if (long_options[opt_index].flag != 0)
- break;
- streams.err.append_format(BUILTIN_ERR_UNKNOWN,
- argv[0],
- long_options[opt_index].name);
+ int opt = w.wgetopt_long(argc, argv, L"elgh", long_options, &opt_index);
+ if (opt == -1) break;
+
+ switch (opt) {
+ case 0: {
+ if (long_options[opt_index].flag != 0) break;
+ streams.err.append_format(BUILTIN_ERR_UNKNOWN, argv[0],
+ long_options[opt_index].name);
builtin_print_help(parser, streams, argv[0], streams.err);
-
return STATUS_BUILTIN_ERROR;
- case 'h':
+ }
+ case 'h': {
builtin_print_help(parser, streams, argv[0], streams.out);
return STATUS_BUILTIN_OK;
-
- case 'g':
+ }
+ case 'g': {
scope = GLOBAL;
break;
-
- case 'l':
+ }
+ case 'l': {
scope = LOCAL;
break;
-
- case 'e':
+ }
+ case 'e': {
erase = 1;
break;
-
- case '?':
- builtin_unknown_option(parser, streams, argv[0], argv[w.woptind-1]);
+ }
+ case '?': {
+ builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
return STATUS_BUILTIN_ERROR;
-
+ }
}
-
}
- if (erase)
- {
- if (scope != UNSET)
- {
- streams.err.append_format(_(L"%ls: Can not specify scope when removing block\n"), argv[0]);
+ if (erase) {
+ if (scope != UNSET) {
+ streams.err.append_format(_(L"%ls: Can not specify scope when removing block\n"),
+ argv[0]);
return STATUS_BUILTIN_ERROR;
}
- if (parser.global_event_blocks.empty())
- {
+ if (parser.global_event_blocks.empty()) {
streams.err.append_format(_(L"%ls: No blocks defined\n"), argv[0]);
return STATUS_BUILTIN_ERROR;
}
parser.global_event_blocks.pop_front();
- }
- else
- {
+ } else {
size_t block_idx = 0;
block_t *block = parser.block_at_index(block_idx);
event_blockage_t eb = {};
- eb.typemask = (1<<EVENT_ANY);
+ eb.typemask = (1 << EVENT_ANY);
- switch (scope)
- {
- case LOCAL:
- {
+ switch (scope) {
+ case LOCAL: {
// If this is the outermost block, then we're global
- if (block_idx + 1 >= parser.block_count())
- {
+ if (block_idx + 1 >= parser.block_count()) {
block = NULL;
}
break;
}
- case GLOBAL:
- {
- block=NULL;
+ case GLOBAL: {
+ block = NULL;
}
- case UNSET:
- {
- while (block != NULL && block->type() != FUNCTION_CALL && block->type() != FUNCTION_CALL_NO_SHADOW)
- {
+ case UNSET: {
+ while (block != NULL && block->type() != FUNCTION_CALL &&
+ block->type() != FUNCTION_CALL_NO_SHADOW) {
// Set it in function scope
block = parser.block_at_index(++block_idx);
}
}
}
- if (block)
- {
+ if (block) {
block->event_blocks.push_front(eb);
- }
- else
- {
+ } else {
parser.global_event_blocks.push_front(eb);
}
}
return STATUS_BUILTIN_OK;
-
}
-/**
- The builtin builtin, used for giving builtins precedence over
- functions. Mostly handled by the parser. All this code does is some
- additional operational modes, such as printing a list of all
- builtins, printing help, etc.
-*/
-static int builtin_builtin(parser_t &parser, io_streams_t &streams, wchar_t **argv)
-{
- int argc=builtin_count_args(argv);
- int list=0;
+// The builtin builtin, used for giving builtins precedence over functions. Mostly handled by the
+// parser. All this code does is some additional operational modes, such as printing a list of all
+// builtins, printing help, etc.
+static int builtin_builtin(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
+ int argc = builtin_count_args(argv);
+ int list = 0;
wgetopter_t w;
- static const struct woption
- long_options[] =
- {
- {
- L"names", no_argument, 0, 'n'
- }
- ,
- {
- L"help", no_argument, 0, 'h'
- }
- ,
- {
- 0, 0, 0, 0
- }
- }
- ;
+ static const struct woption long_options[] = {
+ {L"names", no_argument, 0, 'n'}, {L"help", no_argument, 0, 'h'}, {0, 0, 0, 0}};
- while (1)
- {
+ while (1) {
int opt_index = 0;
- int opt = w.wgetopt_long(argc,
- argv,
- L"nh",
- long_options,
- &opt_index);
- if (opt == -1)
- break;
+ int opt = w.wgetopt_long(argc, argv, L"nh", long_options, &opt_index);
+ if (opt == -1) break;
- switch (opt)
- {
- case 0:
- if (long_options[opt_index].flag != 0)
- break;
- streams.err.append_format(BUILTIN_ERR_UNKNOWN,
- argv[0],
- long_options[opt_index].name);
+ switch (opt) {
+ case 0: {
+ if (long_options[opt_index].flag != 0) break;
+ streams.err.append_format(BUILTIN_ERR_UNKNOWN, argv[0],
+ long_options[opt_index].name);
builtin_print_help(parser, streams, argv[0], streams.err);
-
-
return STATUS_BUILTIN_ERROR;
- case 'h':
+ }
+ case 'h': {
builtin_print_help(parser, streams, argv[0], streams.out);
return STATUS_BUILTIN_OK;
-
- case 'n':
- list=1;
+ }
+ case 'n': {
+ list = 1;
break;
-
- case '?':
- builtin_unknown_option(parser, streams, argv[0], argv[w.woptind-1]);
+ }
+ case '?': {
+ builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
return STATUS_BUILTIN_ERROR;
-
+ }
}
-
}
- if (list)
- {
+ if (list) {
wcstring_list_t names = builtin_get_names();
sort(names.begin(), names.end());
- for (size_t i=0; i<names.size(); i++)
- {
+ for (size_t i = 0; i < names.size(); i++) {
const wchar_t *el = names.at(i).c_str();
streams.out.append(el);
@@ -1032,64 +753,39 @@ static int builtin_builtin(parser_t &parser, io_streams_t &streams, wchar_t **ar
return STATUS_BUILTIN_OK;
}
-/**
- Implementation of the builtin emit command, used to create events.
- */
-static int builtin_emit(parser_t &parser, io_streams_t &streams, wchar_t **argv)
-{
+// Implementation of the builtin emit command, used to create events.
+static int builtin_emit(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
wgetopter_t w;
- int argc=builtin_count_args(argv);
+ int argc = builtin_count_args(argv);
- static const struct woption
- long_options[] =
- {
- {
- L"help", no_argument, 0, 'h'
- }
- ,
- {
- 0, 0, 0, 0
- }
- }
- ;
+ static const struct woption long_options[] = {{L"help", no_argument, 0, 'h'}, {0, 0, 0, 0}};
- while (1)
- {
+ while (1) {
int opt_index = 0;
- int opt = w.wgetopt_long(argc,
- argv,
- L"h",
- long_options,
- &opt_index);
- if (opt == -1)
- break;
+ int opt = w.wgetopt_long(argc, argv, L"h", long_options, &opt_index);
+ if (opt == -1) break;
- switch (opt)
- {
- case 0:
- if (long_options[opt_index].flag != 0)
- break;
- streams.err.append_format(BUILTIN_ERR_UNKNOWN,
- argv[0],
- long_options[opt_index].name);
+ switch (opt) {
+ case 0: {
+ if (long_options[opt_index].flag != 0) break;
+ streams.err.append_format(BUILTIN_ERR_UNKNOWN, argv[0],
+ long_options[opt_index].name);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
-
- case 'h':
+ }
+ case 'h': {
builtin_print_help(parser, streams, argv[0], streams.out);
return STATUS_BUILTIN_OK;
-
- case '?':
- builtin_unknown_option(parser, streams, argv[0], argv[w.woptind-1]);
+ }
+ case '?': {
+ builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
return STATUS_BUILTIN_ERROR;
-
+ }
}
-
}
- if (!argv[w.woptind])
- {
+ if (!argv[w.woptind]) {
streams.err.append_format(L"%ls: expected event name\n", argv[0]);
return STATUS_BUILTIN_ERROR;
}
@@ -1100,438 +796,308 @@ static int builtin_emit(parser_t &parser, io_streams_t &streams, wchar_t **argv)
return STATUS_BUILTIN_OK;
}
-
-/**
- Implementation of the builtin 'command'. Actual command running is handled by
- the parser, this just processes the flags.
-*/
-static int builtin_command(parser_t &parser, io_streams_t &streams, wchar_t **argv)
-{
+// Implementation of the builtin 'command'. Actual command running is handled by the parser, this
+// just processes the flags.
+static int builtin_command(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
wgetopter_t w;
- int argc=builtin_count_args(argv);
- int print_path=0;
+ int argc = builtin_count_args(argv);
+ int print_path = 0;
- w.woptind=0;
+ w.woptind = 0;
- static const struct woption
- long_options[] =
- {
- { L"search", no_argument, 0, 's' },
- { L"help", no_argument, 0, 'h' },
- { 0, 0, 0, 0 }
- };
+ static const struct woption long_options[] = {
+ {L"search", no_argument, 0, 's'}, {L"help", no_argument, 0, 'h'}, {0, 0, 0, 0}};
- while (1)
- {
+ while (1) {
int opt_index = 0;
- int opt = w.wgetopt_long(argc,
- argv,
- L"svh",
- long_options,
- &opt_index);
- if (opt == -1)
- break;
+ int opt = w.wgetopt_long(argc, argv, L"svh", long_options, &opt_index);
+ if (opt == -1) break;
- switch (opt)
- {
- case 0:
- if (long_options[opt_index].flag != 0)
- break;
- streams.err.append_format(BUILTIN_ERR_UNKNOWN,
- argv[0],
- long_options[opt_index].name);
+ switch (opt) {
+ case 0: {
+ if (long_options[opt_index].flag != 0) break;
+ streams.err.append_format(BUILTIN_ERR_UNKNOWN, argv[0],
+ long_options[opt_index].name);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
-
- case 'h':
+ }
+ case 'h': {
builtin_print_help(parser, streams, argv[0], streams.out);
return STATUS_BUILTIN_OK;
-
+ }
case 's':
- case 'v':
- print_path=1;
+ case 'v': {
+ print_path = 1;
break;
-
- case '?':
- builtin_unknown_option(parser, streams, argv[0], argv[w.woptind-1]);
+ }
+ case '?': {
+ builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
return STATUS_BUILTIN_ERROR;
-
+ }
}
-
}
- if (!print_path)
- {
+ if (!print_path) {
builtin_print_help(parser, streams, argv[0], streams.out);
return STATUS_BUILTIN_ERROR;
}
- int found=0;
+ int found = 0;
- for (int idx = w.woptind; argv[idx]; ++idx)
- {
+ for (int idx = w.woptind; argv[idx]; ++idx) {
const wchar_t *command_name = argv[idx];
wcstring path;
- if (path_get_path(command_name, &path))
- {
- streams.out.append_format( L"%ls\n", path.c_str());
+ if (path_get_path(command_name, &path)) {
+ streams.out.append_format(L"%ls\n", path.c_str());
++found;
}
}
return found ? STATUS_BUILTIN_OK : STATUS_BUILTIN_ERROR;
}
-/**
- A generic bultin that only supports showing a help message. This is
- only a placeholder that prints the help message. Useful for
- commands that live in the parser.
-*/
-static int builtin_generic(parser_t &parser, io_streams_t &streams, wchar_t **argv)
-{
+// A generic bultin that only supports showing a help message. This is only a placeholder that
+// prints the help message. Useful for commands that live in the parser.
+static int builtin_generic(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
wgetopter_t w;
- int argc=builtin_count_args(argv);
+ int argc = builtin_count_args(argv);
- /* Hackish - if we have no arguments other than the command, we are a "naked invocation" and we just print help */
- if (argc == 1)
- {
+ // Hackish - if we have no arguments other than the command, we are a "naked invocation" and we
+ // just print help.
+ if (argc == 1) {
builtin_print_help(parser, streams, argv[0], streams.out);
return STATUS_BUILTIN_ERROR;
}
- static const struct woption
- long_options[] =
- {
- { L"help", no_argument, 0, 'h' },
- { 0, 0, 0, 0 }
- };
+ static const struct woption long_options[] = {{L"help", no_argument, 0, 'h'}, {0, 0, 0, 0}};
- while (1)
- {
+ while (1) {
int opt_index = 0;
- int opt = w.wgetopt_long(argc,
- argv,
- L"h",
- long_options,
- &opt_index);
- if (opt == -1)
- break;
+ int opt = w.wgetopt_long(argc, argv, L"h", long_options, &opt_index);
+ if (opt == -1) break;
- switch (opt)
- {
- case 0:
- if (long_options[opt_index].flag != 0)
- break;
- streams.err.append_format(BUILTIN_ERR_UNKNOWN,
- argv[0],
- long_options[opt_index].name);
+ switch (opt) {
+ case 0: {
+ if (long_options[opt_index].flag != 0) break;
+ streams.err.append_format(BUILTIN_ERR_UNKNOWN, argv[0],
+ long_options[opt_index].name);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
-
- case 'h':
+ }
+ case 'h': {
builtin_print_help(parser, streams, argv[0], streams.out);
return STATUS_BUILTIN_OK;
-
- case '?':
- builtin_unknown_option(parser, streams, argv[0], argv[w.woptind-1]);
+ }
+ case '?': {
+ builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
return STATUS_BUILTIN_ERROR;
-
+ }
}
-
}
return STATUS_BUILTIN_ERROR;
}
-/**
- Return a definition of the specified function. Used by the functions builtin.
-*/
-static wcstring functions_def(const wcstring &name)
-{
- CHECK(! name.empty(), L"");
+// Return a definition of the specified function. Used by the functions builtin.
+static wcstring functions_def(const wcstring &name) {
+ CHECK(!name.empty(), L"");
wcstring out;
-
wcstring desc, def;
function_get_desc(name, &desc);
function_get_definition(name, &def);
-
event_t search(EVENT_ANY);
-
search.function_name = name;
-
std::vector<event_t *> ev;
event_get(search, &ev);
out.append(L"function ");
- /* Typically we prefer to specify the function name first, e.g. "function foo --description bar"
- But If the function name starts with a -, we'll need to output it after all the options. */
+ // Typically we prefer to specify the function name first, e.g. "function foo --description bar"
+ // But If the function name starts with a -, we'll need to output it after all the options.
bool defer_function_name = (name.at(0) == L'-');
- if (! defer_function_name)
- {
+ if (!defer_function_name) {
out.append(escape_string(name, true));
}
- if (! desc.empty())
- {
+ if (!desc.empty()) {
wcstring esc_desc = escape_string(desc, true);
out.append(L" --description ");
out.append(esc_desc);
}
- if (!function_get_shadows(name))
- {
+ if (!function_get_shadows(name)) {
out.append(L" --no-scope-shadowing");
}
- for (size_t i=0; i<ev.size(); i++)
- {
+ for (size_t i = 0; i < ev.size(); i++) {
event_t *next = ev.at(i);
- switch (next->type)
- {
- case EVENT_SIGNAL:
- {
+ switch (next->type) {
+ case EVENT_SIGNAL: {
append_format(out, L" --on-signal %ls", sig2wcs(next->param1.signal));
break;
}
-
- case EVENT_VARIABLE:
- {
+ case EVENT_VARIABLE: {
append_format(out, L" --on-variable %ls", next->str_param1.c_str());
break;
}
-
- case EVENT_EXIT:
- {
+ case EVENT_EXIT: {
if (next->param1.pid > 0)
append_format(out, L" --on-process-exit %d", next->param1.pid);
else
append_format(out, L" --on-job-exit %d", -next->param1.pid);
break;
}
-
- case EVENT_JOB_ID:
- {
+ case EVENT_JOB_ID: {
const job_t *j = job_get(next->param1.job_id);
- if (j)
- append_format(out, L" --on-job-exit %d", j->pgid);
+ if (j) append_format(out, L" --on-job-exit %d", j->pgid);
break;
}
-
- case EVENT_GENERIC:
- {
+ case EVENT_GENERIC: {
append_format(out, L" --on-event %ls", next->str_param1.c_str());
break;
}
-
}
-
}
-
wcstring_list_t named = function_get_named_arguments(name);
- if (! named.empty())
- {
+ if (!named.empty()) {
append_format(out, L" --argument");
- for (size_t i=0; i < named.size(); i++)
- {
+ for (size_t i = 0; i < named.size(); i++) {
append_format(out, L" %ls", named.at(i).c_str());
}
}
- /* Output the function name if we deferred it */
- if (defer_function_name)
- {
+ // Output the function name if we deferred it.
+ if (defer_function_name) {
out.append(L" -- ");
out.append(escape_string(name, true));
}
- /* Output any inherited variables as `set -l` lines */
- std::map<wcstring,env_var_t> inherit_vars = function_get_inherit_vars(name);
- for (std::map<wcstring,env_var_t>::const_iterator it = inherit_vars.begin(), end = inherit_vars.end(); it != end; ++it)
- {
+ // Output any inherited variables as `set -l` lines.
+ std::map<wcstring, env_var_t> inherit_vars = function_get_inherit_vars(name);
+ for (std::map<wcstring, env_var_t>::const_iterator it = inherit_vars.begin(),
+ end = inherit_vars.end();
+ it != end; ++it) {
wcstring_list_t lst;
- if (!it->second.missing())
- {
+ if (!it->second.missing()) {
tokenize_variable_array(it->second, lst);
}
- /* This forced tab is crummy, but we don't know what indentation style the function uses */
+ // This forced tab is crummy, but we don't know what indentation style the function uses.
append_format(out, L"\n\tset -l %ls", it->first.c_str());
- for (wcstring_list_t::const_iterator arg_it = lst.begin(), arg_end = lst.end(); arg_it != arg_end; ++arg_it)
- {
+ for (wcstring_list_t::const_iterator arg_it = lst.begin(), arg_end = lst.end();
+ arg_it != arg_end; ++arg_it) {
wcstring earg = escape_string(*arg_it, ESCAPE_ALL);
out.push_back(L' ');
out.append(earg);
}
}
- /* This forced tab is sort of crummy - not all functions start with a tab */
+ // This forced tab is sort of crummy - not all functions start with a tab.
append_format(out, L"\n\t%ls", def.c_str());
- /* Append a newline before the 'end', unless there already is one there */
- if (! string_suffixes_string(L"\n", def))
- {
+ // Append a newline before the 'end', unless there already is one there.
+ if (!string_suffixes_string(L"\n", def)) {
out.push_back(L'\n');
}
out.append(L"end\n");
return out;
}
-
-/**
- The functions builtin, used for listing and erasing functions.
-*/
-static int builtin_functions(parser_t &parser, io_streams_t &streams, wchar_t **argv)
-{
+// The functions builtin, used for listing and erasing functions.
+static int builtin_functions(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
wgetopter_t w;
int i;
- int erase=0;
- wchar_t *desc=0;
+ int erase = 0;
+ wchar_t *desc = 0;
- int argc=builtin_count_args(argv);
- int list=0;
- int show_hidden=0;
+ int argc = builtin_count_args(argv);
+ int list = 0;
+ int show_hidden = 0;
int res = STATUS_BUILTIN_OK;
int query = 0;
int copy = 0;
- static const struct woption
- long_options[] =
- {
- {
- L"erase", no_argument, 0, 'e'
- }
- ,
- {
- L"description", required_argument, 0, 'd'
- }
- ,
- {
- L"names", no_argument, 0, 'n'
- }
- ,
- {
- L"all", no_argument, 0, 'a'
- }
- ,
- {
- L"help", no_argument, 0, 'h'
- }
- ,
- {
- L"query", no_argument, 0, 'q'
- }
- ,
- {
- L"copy", no_argument, 0, 'c'
- }
- ,
- {
- 0, 0, 0, 0
- }
- }
- ;
+ static const struct woption long_options[] = {
+ {L"erase", no_argument, 0, 'e'}, {L"description", required_argument, 0, 'd'},
+ {L"names", no_argument, 0, 'n'}, {L"all", no_argument, 0, 'a'},
+ {L"help", no_argument, 0, 'h'}, {L"query", no_argument, 0, 'q'},
+ {L"copy", no_argument, 0, 'c'}, {0, 0, 0, 0}};
- while (1)
- {
+ while (1) {
int opt_index = 0;
- int opt = w.wgetopt_long(argc,
- argv,
- L"ed:nahqc",
- long_options,
- &opt_index);
- if (opt == -1)
- break;
+ int opt = w.wgetopt_long(argc, argv, L"ed:nahqc", long_options, &opt_index);
+ if (opt == -1) break;
- switch (opt)
- {
- case 0:
- if (long_options[opt_index].flag != 0)
- break;
- streams.err.append_format(BUILTIN_ERR_UNKNOWN,
- argv[0],
- long_options[opt_index].name);
+ switch (opt) {
+ case 0: {
+ if (long_options[opt_index].flag != 0) break;
+ streams.err.append_format(BUILTIN_ERR_UNKNOWN, argv[0],
+ long_options[opt_index].name);
builtin_print_help(parser, streams, argv[0], streams.err);
-
-
return STATUS_BUILTIN_ERROR;
-
- case 'e':
- erase=1;
+ }
+ case 'e': {
+ erase = 1;
break;
-
- case 'd':
- desc=w.woptarg;
+ }
+ case 'd': {
+ desc = w.woptarg;
break;
-
- case 'n':
- list=1;
+ }
+ case 'n': {
+ list = 1;
break;
-
- case 'a':
- show_hidden=1;
+ }
+ case 'a': {
+ show_hidden = 1;
break;
-
- case 'h':
+ }
+ case 'h': {
builtin_print_help(parser, streams, argv[0], streams.out);
return STATUS_BUILTIN_OK;
-
- case 'q':
+ }
+ case 'q': {
query = 1;
break;
-
- case 'c':
+ }
+ case 'c': {
copy = 1;
break;
-
- case '?':
- builtin_unknown_option(parser, streams, argv[0], argv[w.woptind-1]);
+ }
+ case '?': {
+ builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
return STATUS_BUILTIN_ERROR;
-
+ }
}
-
}
- /*
- Erase, desc, query, copy and list are mutually exclusive
- */
- if ((erase + (!!desc) + list + query + copy) > 1)
- {
- streams.err.append_format(_(L"%ls: Invalid combination of options\n"),
- argv[0]);
+ // Erase, desc, query, copy and list are mutually exclusive.
+ if ((erase + (!!desc) + list + query + copy) > 1) {
+ streams.err.append_format(_(L"%ls: Invalid combination of options\n"), argv[0]);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
}
- if (erase)
- {
+ if (erase) {
int i;
- for (i=w.woptind; i<argc; i++)
- function_remove(argv[i]);
+ for (i = w.woptind; i < argc; i++) function_remove(argv[i]);
return STATUS_BUILTIN_OK;
- }
- else if (desc)
- {
+ } else if (desc) {
wchar_t *func;
- if (argc-w.woptind != 1)
- {
- streams.err.append_format(_(L"%ls: Expected exactly one function name\n"),
- argv[0]);
+ if (argc - w.woptind != 1) {
+ streams.err.append_format(_(L"%ls: Expected exactly one function name\n"), argv[0]);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
}
func = argv[w.woptind];
- if (!function_exists(func))
- {
- streams.err.append_format(_(L"%ls: Function '%ls' does not exist\n"),
- argv[0],
- func);
+ if (!function_exists(func)) {
+ streams.err.append_format(_(L"%ls: Function '%ls' does not exist\n"), argv[0], func);
builtin_print_help(parser, streams, argv[0], streams.err);
@@ -1541,98 +1107,78 @@ static int builtin_functions(parser_t &parser, io_streams_t &streams, wchar_t **
function_set_desc(func, desc);
return STATUS_BUILTIN_OK;
- }
- else if (list || (argc==w.woptind))
- {
+ } else if (list || (argc == w.woptind)) {
int is_screen = !streams.out_is_redirected && isatty(STDOUT_FILENO);
size_t i;
wcstring_list_t names = function_get_names(show_hidden);
std::sort(names.begin(), names.end());
- if (is_screen)
- {
+ if (is_screen) {
wcstring buff;
- for (i=0; i<names.size(); i++)
- {
+ for (i = 0; i < names.size(); i++) {
buff.append(names.at(i));
buff.append(L", ");
}
streams.out.append(reformat_for_screen(buff));
- }
- else
- {
- for (i=0; i<names.size(); i++)
- {
+ } else {
+ for (i = 0; i < names.size(); i++) {
streams.out.append(names.at(i).c_str());
streams.out.append(L"\n");
}
}
return STATUS_BUILTIN_OK;
- }
- else if (copy)
- {
+ } else if (copy) {
wcstring current_func;
wcstring new_func;
- if (argc-w.woptind != 2)
- {
- streams.err.append_format(_(L"%ls: Expected exactly two names (current function name, and new function name)\n"),
- argv[0]);
+ if (argc - w.woptind != 2) {
+ streams.err.append_format(_(L"%ls: Expected exactly two names (current function name, "
+ L"and new function name)\n"),
+ argv[0]);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
}
current_func = argv[w.woptind];
- new_func = argv[w.woptind+1];
+ new_func = argv[w.woptind + 1];
- if (!function_exists(current_func))
- {
- streams.err.append_format(_(L"%ls: Function '%ls' does not exist\n"),
- argv[0],
- current_func.c_str());
+ if (!function_exists(current_func)) {
+ streams.err.append_format(_(L"%ls: Function '%ls' does not exist\n"), argv[0],
+ current_func.c_str());
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
}
- if ((wcsfuncname(new_func) != 0) || parser_keywords_is_reserved(new_func))
- {
- streams.err.append_format(_(L"%ls: Illegal function name '%ls'\n"),
- argv[0],
- new_func.c_str());
+ if ((wcsfuncname(new_func) != 0) || parser_keywords_is_reserved(new_func)) {
+ streams.err.append_format(_(L"%ls: Illegal function name '%ls'\n"), argv[0],
+ new_func.c_str());
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
}
- // keep things simple: don't allow existing names to be copy targets.
- if (function_exists(new_func))
- {
- streams.err.append_format(_(L"%ls: Function '%ls' already exists. Cannot create copy '%ls'\n"),
- argv[0],
- new_func.c_str(),
- current_func.c_str());
+ // Keep things simple: don't allow existing names to be copy targets.
+ if (function_exists(new_func)) {
+ streams.err.append_format(
+ _(L"%ls: Function '%ls' already exists. Cannot create copy '%ls'\n"), argv[0],
+ new_func.c_str(), current_func.c_str());
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
}
- if (function_copy(current_func, new_func))
- return STATUS_BUILTIN_OK;
+ if (function_copy(current_func, new_func)) return STATUS_BUILTIN_OK;
return STATUS_BUILTIN_ERROR;
}
- for (i=w.woptind; i<argc; i++)
- {
+ for (i = w.woptind; i < argc; i++) {
if (!function_exists(argv[i]))
res++;
- else
- {
- if (!query)
- {
- if (i != w.woptind)
- streams.out.append(L"\n");
+ else {
+ if (!query) {
+ if (i != w.woptind) streams.out.append(L"\n");
streams.out.append(functions_def(argv[i]));
}
@@ -1642,83 +1188,90 @@ static int builtin_functions(parser_t &parser, io_streams_t &streams, wchar_t **
return res;
}
-static unsigned int builtin_echo_digit(wchar_t wc, unsigned int base)
-{
- // base must be hex or octal
- assert(base == 8 || base == 16);
- switch (wc)
- {
- case L'0':
+static unsigned int builtin_echo_digit(wchar_t wc, unsigned int base) {
+ assert(base == 8 || base == 16); // base must be hex or octal
+ switch (wc) {
+ case L'0': {
return 0;
- case L'1':
+ }
+ case L'1': {
return 1;
- case L'2':
+ }
+ case L'2': {
return 2;
- case L'3':
+ }
+ case L'3': {
return 3;
- case L'4':
+ }
+ case L'4': {
return 4;
- case L'5':
+ }
+ case L'5': {
return 5;
- case L'6':
+ }
+ case L'6': {
return 6;
- case L'7':
+ }
+ case L'7': {
return 7;
+ }
}
- if (base == 16) switch (wc)
- {
- case L'8':
+ if (base == 16) switch (wc) {
+ case L'8': {
return 8;
- case L'9':
+ }
+ case L'9': {
return 9;
+ }
case L'a':
- case L'A':
+ case L'A': {
return 10;
+ }
case L'b':
- case L'B':
+ case L'B': {
return 11;
+ }
case L'c':
- case L'C':
+ case L'C': {
return 12;
+ }
case L'd':
- case L'D':
+ case L'D': {
return 13;
+ }
case L'e':
- case L'E':
+ case L'E': {
return 14;
+ }
case L'f':
- case L'F':
+ case L'F': {
return 15;
+ }
}
return UINT_MAX;
}
-/* Parse a numeric escape sequence in str, returning whether we succeeded.
- Also return the number of characters consumed and the resulting value.
- Supported escape sequences:
-
- \0nnn: octal value, zero to three digits
- \nnn: octal value, one to three digits
- \xhh: hex value, one to two digits
-*/
-static bool builtin_echo_parse_numeric_sequence(const wchar_t *str, size_t *consumed, unsigned char *out_val)
-{
+// Parse a numeric escape sequence in str, returning whether we succeeded. Also return the number of
+// characters consumed and the resulting value. Supported escape sequences:
+//
+// \0nnn: octal value, zero to three digits
+// \nnn: octal value, one to three digits
+// \xhh: hex value, one to two digits
+static bool builtin_echo_parse_numeric_sequence(const wchar_t *str, size_t *consumed,
+ unsigned char *out_val) {
bool success = false;
- unsigned int start = 0; //the first character of the numeric part of the sequence
+ unsigned int start = 0; // the first character of the numeric part of the sequence
unsigned int base = 0, max_digits = 0;
- if (builtin_echo_digit(str[0], 8) != UINT_MAX)
- {
+ if (builtin_echo_digit(str[0], 8) != UINT_MAX) {
// Octal escape
base = 8;
- // If the first digit is a 0, we allow four digits (including that zero)
- // Otherwise we allow 3.
+ // If the first digit is a 0, we allow four digits (including that zero); otherwise, we
+ // allow 3.
max_digits = (str[0] == L'0' ? 4 : 3);
- }
- else if (str[0] == L'x')
- {
+ } else if (str[0] == L'x') {
// Hex escape
base = 16;
max_digits = 2;
@@ -1727,20 +1280,17 @@ static bool builtin_echo_parse_numeric_sequence(const wchar_t *str, size_t *cons
start = 1;
}
- if (base != 0)
- {
+ if (base != 0) {
unsigned int idx;
- unsigned char val = 0; //resulting character
- for (idx = start; idx < start + max_digits; idx++)
- {
+ unsigned char val = 0; // resulting character
+ for (idx = start; idx < start + max_digits; idx++) {
unsigned int digit = builtin_echo_digit(str[idx], base);
if (digit == UINT_MAX) break;
val = val * base + digit;
}
- // We succeeded if we consumed at least one digit
- if (idx > start)
- {
+ // We succeeded if we consumed at least one digit.
+ if (idx > start) {
*consumed = idx;
*out_val = val;
success = true;
@@ -1749,140 +1299,137 @@ static bool builtin_echo_parse_numeric_sequence(const wchar_t *str, size_t *cons
return success;
}
-/** The echo builtin.
- bash only respects -n if it's the first argument. We'll do the same.
- We also support a new option -s to mean "no spaces"
-*/
-
-static int builtin_echo(parser_t &parser, io_streams_t &streams, wchar_t **argv)
-{
+// The echo builtin.
+//
+// Bash only respects -n if it's the first argument. We'll do the same. We also support a new option
+// -s to mean "no spaces"
+static int builtin_echo(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
/* Skip first arg */
- if (! *argv++)
- return STATUS_BUILTIN_ERROR;
+ if (!*argv++) return STATUS_BUILTIN_ERROR;
- /* Process options. Options must come at the beginning - the first non-option kicks us out. */
+ // Process options. Options must come at the beginning - the first non-option kicks us out.
bool print_newline = true, print_spaces = true, interpret_special_chars = false;
size_t option_idx = 0;
- for (option_idx = 0; argv[option_idx] != NULL; option_idx++)
- {
+ for (option_idx = 0; argv[option_idx] != NULL; option_idx++) {
const wchar_t *arg = argv[option_idx];
assert(arg != NULL);
bool arg_is_valid_option = false;
- if (arg[0] == L'-')
- {
+ if (arg[0] == L'-') {
// We have a leading dash. Ensure that every subseqnent character is a valid option.
size_t i = 1;
- while (arg[i] != L'\0' && wcschr(L"nesE", arg[i]) != NULL)
- {
+ while (arg[i] != L'\0' && wcschr(L"nesE", arg[i]) != NULL) {
i++;
}
- // We must have at least two characters to be a valid option, and have consumed the whole string
+ // We must have at least two characters to be a valid option, and have consumed the
+ // whole string.
arg_is_valid_option = (i >= 2 && arg[i] == L'\0');
}
-
- if (! arg_is_valid_option)
- {
- // This argument is not an option, so there are no more options
+
+ if (!arg_is_valid_option) {
+ // This argument is not an option, so there are no more options.
break;
}
-
+
// Ok, we are sure the argument is an option. Parse it.
assert(arg_is_valid_option);
- for (size_t i=1; arg[i] != L'\0'; i++)
- {
- switch (arg[i])
- {
- case L'n':
+ for (size_t i = 1; arg[i] != L'\0'; i++) {
+ switch (arg[i]) {
+ case L'n': {
print_newline = false;
break;
- case L'e':
+ }
+ case L'e': {
interpret_special_chars = true;
break;
- case L's':
+ }
+ case L's': {
print_spaces = false;
break;
- case L'E':
+ }
+ case L'E': {
interpret_special_chars = false;
break;
- default:
+ }
+ default: {
assert(0 && "Unexpected character in builtin_echo argument");
break;
+ }
}
}
}
- /* The special character \c can be used to indicate no more output */
+ // The special character \c can be used to indicate no more output.
bool continue_output = true;
-
+
/* Skip over the options */
- const wchar_t * const *args_to_echo = argv + option_idx;
- for (size_t idx = 0; continue_output && args_to_echo[idx] != NULL; idx++)
- {
- if (print_spaces && idx > 0)
- {
+ const wchar_t *const *args_to_echo = argv + option_idx;
+ for (size_t idx = 0; continue_output && args_to_echo[idx] != NULL; idx++) {
+ if (print_spaces && idx > 0) {
streams.out.push_back(' ');
}
const wchar_t *str = args_to_echo[idx];
- for (size_t j=0; continue_output && str[j]; j++)
- {
- if (! interpret_special_chars || str[j] != L'\\')
- {
- /* Not an escape */
+ for (size_t j = 0; continue_output && str[j]; j++) {
+ if (!interpret_special_chars || str[j] != L'\\') {
+ // Not an escape.
streams.out.push_back(str[j]);
- }
- else
- {
- /* Most escapes consume one character in addition to the backslash; the numeric sequences may consume more, while an unrecognized escape sequence consumes none. */
+ } else {
+ // Most escapes consume one character in addition to the backslash; the numeric
+ // sequences may consume more, while an unrecognized escape sequence consumes none.
wchar_t wc;
size_t consumed = 1;
- switch (str[j+1])
- {
- case L'a':
+ switch (str[j + 1]) {
+ case L'a': {
wc = L'\a';
break;
- case L'b':
+ }
+ case L'b': {
wc = L'\b';
break;
- case L'e':
+ }
+ case L'e': {
wc = L'\x1B';
break;
- case L'f':
+ }
+ case L'f': {
wc = L'\f';
break;
- case L'n':
+ }
+ case L'n': {
wc = L'\n';
break;
- case L'r':
+ }
+ case L'r': {
wc = L'\r';
break;
- case L't':
+ }
+ case L't': {
wc = L'\t';
break;
- case L'v':
+ }
+ case L'v': {
wc = L'\v';
break;
- case L'\\':
+ }
+ case L'\\': {
wc = L'\\';
break;
-
- case L'c':
+ }
+ case L'c': {
wc = 0;
continue_output = false;
break;
-
- default:
- {
- /* Octal and hex escape sequences */
+ }
+ default: {
+ // Octal and hex escape sequences.
unsigned char narrow_val = 0;
- if (builtin_echo_parse_numeric_sequence(str + j + 1, &consumed, &narrow_val))
- {
- /* Here consumed must have been set to something. The narrow_val is a literal byte that we want to output (#1894) */
+ if (builtin_echo_parse_numeric_sequence(str + j + 1, &consumed,
+ &narrow_val)) {
+ // Here consumed must have been set to something. The narrow_val is a
+ // literal byte that we want to output (#1894).
wc = ENCODE_DIRECT_BASE + narrow_val % 256;
- }
- else
- {
- /* Not a recognized escape. We consume only the backslash. */
+ } else {
+ // Not a recognized escape. We consume only the backslash.
wc = L'\\';
consumed = 0;
}
@@ -1890,18 +1437,17 @@ static int builtin_echo(parser_t &parser, io_streams_t &streams, wchar_t **argv)
}
}
- /* Skip over characters that were part of this escape sequence (but not the backslash, which will be handled by the loop increment */
+ // Skip over characters that were part of this escape sequence (but not the
+ // backslash, which will be handled by the loop increment.
j += consumed;
- if (continue_output)
- {
+ if (continue_output) {
streams.out.push_back(wc);
}
}
}
}
- if (print_newline && continue_output)
- {
+ if (print_newline && continue_output) {
streams.out.push_back('\n');
}
return STATUS_BUILTIN_OK;
@@ -1909,41 +1455,38 @@ static int builtin_echo(parser_t &parser, io_streams_t &streams, wchar_t **argv)
// The pwd builtin. We don't respect -P to resolve symbolic links because we
// try to always resolve them.
-static int builtin_pwd(parser_t &parser, io_streams_t &streams, wchar_t **argv)
-{
+static int builtin_pwd(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
wcstring res = wgetcwd();
- if (res.empty())
- {
+ if (res.empty()) {
return STATUS_BUILTIN_ERROR;
- }
- else
- {
+ } else {
streams.out.append(res);
streams.out.push_back(L'\n');
return STATUS_BUILTIN_OK;
}
}
-/** Adds a function to the function set. It calls into function.cpp to perform any heavy lifting. */
-int define_function(parser_t &parser, io_streams_t &streams, const wcstring_list_t &c_args, const wcstring &contents, int definition_line_offset, wcstring *out_err)
-{
+// Adds a function to the function set. It calls into function.cpp to perform any heavy lifting.
+int define_function(parser_t &parser, io_streams_t &streams, const wcstring_list_t &c_args,
+ const wcstring &contents, int definition_line_offset, wcstring *out_err) {
wgetopter_t w;
assert(out_err != NULL);
- /* wgetopt expects 'function' as the first argument. Make a new wcstring_list with that property. */
+ // wgetopt expects 'function' as the first argument. Make a new wcstring_list with that
+ // property.
wcstring_list_t args;
args.push_back(L"function");
args.insert(args.end(), c_args.begin(), c_args.end());
- /* Hackish const_cast matches the one in builtin_run */
+ // Hackish const_cast matches the one in builtin_run.
const null_terminated_array_t<wchar_t> argv_array(args);
wchar_t **argv = const_cast<wchar_t **>(argv_array.get());
int argc = builtin_count_args(argv);
- int res=STATUS_BUILTIN_OK;
- wchar_t *desc=0;
+ int res = STATUS_BUILTIN_OK;
+ wchar_t *desc = 0;
std::vector<event_t> events;
-
+
bool has_named_arguments = false;
wcstring_list_t named_arguments;
wcstring_list_t inherit_vars;
@@ -1951,197 +1494,142 @@ int define_function(parser_t &parser, io_streams_t &streams, const wcstring_list
bool shadows = true;
wcstring_list_t wrap_targets;
-
- /* If -a/--argument-names is specified before the function name,
- then the function name is the last positional, e.g. `function -a arg1 arg2 name`.
- If it is specified after the function name (or not specified at all) then the
- function name is the first positional. This is the common case. */
+
+ // If -a/--argument-names is specified before the function name, then the function name is the
+ // last positional, e.g. `function -a arg1 arg2 name`. If it is specified after the function
+ // name (or not specified at all) then the function name is the first positional. This is the
+ // common case.
bool name_is_first_positional = true;
wcstring_list_t positionals;
-
- const struct woption long_options[] =
- {
- { L"description", required_argument, 0, 'd' },
- { L"on-signal", required_argument, 0, 's' },
- { L"on-job-exit", required_argument, 0, 'j' },
- { L"on-process-exit", required_argument, 0, 'p' },
- { L"on-variable", required_argument, 0, 'v' },
- { L"on-event", required_argument, 0, 'e' },
- { L"wraps", required_argument, 0, 'w' },
- { L"help", no_argument, 0, 'h' },
- { L"argument-names", no_argument, 0, 'a' },
- { L"no-scope-shadowing", no_argument, 0, 'S' },
- { L"inherit-variable", required_argument, 0, 'V' },
- { 0, 0, 0, 0 }
- };
- while (1 && (!res))
- {
+ const struct woption long_options[] = {{L"description", required_argument, 0, 'd'},
+ {L"on-signal", required_argument, 0, 's'},
+ {L"on-job-exit", required_argument, 0, 'j'},
+ {L"on-process-exit", required_argument, 0, 'p'},
+ {L"on-variable", required_argument, 0, 'v'},
+ {L"on-event", required_argument, 0, 'e'},
+ {L"wraps", required_argument, 0, 'w'},
+ {L"help", no_argument, 0, 'h'},
+ {L"argument-names", no_argument, 0, 'a'},
+ {L"no-scope-shadowing", no_argument, 0, 'S'},
+ {L"inherit-variable", required_argument, 0, 'V'},
+ {0, 0, 0, 0}};
+
+ while (1 && (!res)) {
int opt_index = 0;
- // The leading - here specifies RETURN_IN_ORDER
- int opt = w.wgetopt_long(argc,
- argv,
- L"-d:s:j:p:v:e:w:haSV:",
- long_options,
- &opt_index);
- if (opt == -1)
- break;
-
- switch (opt)
- {
- case 0:
- if (long_options[opt_index].flag != 0)
- break;
-
-
-
- append_format(*out_err,
- BUILTIN_ERR_UNKNOWN,
- argv[0],
- long_options[opt_index].name);
+ // The leading - here specifies RETURN_IN_ORDER.
+ int opt = w.wgetopt_long(argc, argv, L"-d:s:j:p:v:e:w:haSV:", long_options, &opt_index);
+ if (opt == -1) break;
+ switch (opt) {
+ case 0: {
+ if (long_options[opt_index].flag != 0) break;
+ append_format(*out_err, BUILTIN_ERR_UNKNOWN, argv[0], long_options[opt_index].name);
res = 1;
break;
-
- case 'd':
- desc=w.woptarg;
+ }
+ case 'd': {
+ desc = w.woptarg;
break;
-
- case 's':
- {
+ }
+ case 's': {
int sig = wcs2sig(w.woptarg);
-
- if (sig < 0)
- {
- append_format(*out_err,
- _(L"%ls: Unknown signal '%ls'"),
- argv[0],
- w.woptarg);
- res=1;
+ if (sig < 0) {
+ append_format(*out_err, _(L"%ls: Unknown signal '%ls'"), argv[0], w.woptarg);
+ res = 1;
break;
}
events.push_back(event_t::signal_event(sig));
break;
}
- case 'v':
- {
- if (wcsvarname(w.woptarg))
- {
- append_format(*out_err,
- _(L"%ls: Invalid variable name '%ls'"),
- argv[0],
+ case 'v': {
+ if (wcsvarname(w.woptarg)) {
+ append_format(*out_err, _(L"%ls: Invalid variable name '%ls'"), argv[0],
w.woptarg);
- res=STATUS_BUILTIN_ERROR;
+ res = STATUS_BUILTIN_ERROR;
break;
}
events.push_back(event_t::variable_event(w.woptarg));
break;
}
-
-
- case 'e':
- {
+ case 'e': {
events.push_back(event_t::generic_event(w.woptarg));
break;
}
-
case 'j':
- case 'p':
- {
+ case 'p': {
pid_t pid;
wchar_t *end;
event_t e(EVENT_ANY);
- if ((opt == 'j') &&
- (wcscasecmp(w.woptarg, L"caller") == 0))
- {
+ if ((opt == 'j') && (wcscasecmp(w.woptarg, L"caller") == 0)) {
int job_id = -1;
- if (is_subshell)
- {
+ if (is_subshell) {
size_t block_idx = 0;
- /* Find the outermost substitution block */
- for (block_idx = 0; ; block_idx++)
- {
+ // Find the outermost substitution block.
+ for (block_idx = 0;; block_idx++) {
const block_t *b = parser.block_at_index(block_idx);
- if (b == NULL || b->type() == SUBST)
- break;
+ if (b == NULL || b->type() == SUBST) break;
}
- /* Go one step beyond that, to get to the caller */
+ // Go one step beyond that, to get to the caller.
const block_t *caller_block = parser.block_at_index(block_idx + 1);
- if (caller_block != NULL && caller_block->job != NULL)
- {
+ if (caller_block != NULL && caller_block->job != NULL) {
job_id = caller_block->job->job_id;
}
}
- if (job_id == -1)
- {
+ if (job_id == -1) {
append_format(*out_err,
_(L"%ls: Cannot find calling job for event handler"),
argv[0]);
- res=1;
- }
- else
- {
+ res = 1;
+ } else {
e.type = EVENT_JOB_ID;
e.param1.job_id = job_id;
}
- }
- else
- {
+ } else {
errno = 0;
pid = fish_wcstoi(w.woptarg, &end, 10);
- if (errno || !end || *end)
- {
- append_format(*out_err,
- _(L"%ls: Invalid process id %ls"),
- argv[0],
+ if (errno || !end || *end) {
+ append_format(*out_err, _(L"%ls: Invalid process id %ls"), argv[0],
w.woptarg);
- res=1;
+ res = 1;
break;
}
-
e.type = EVENT_EXIT;
- e.param1.pid = (opt=='j'?-1:1)*abs(pid);
+ e.param1.pid = (opt == 'j' ? -1 : 1) * abs(pid);
}
- if (res)
- {
- /* nothing */
- }
- else
- {
+ if (!res) {
events.push_back(e);
}
break;
}
-
- case 'a':
+ case 'a': {
has_named_arguments = true;
- /* The function name is the first positional unless -a comes before all positionals */
- name_is_first_positional = ! positionals.empty();
+ // The function name is the first positional unless -a comes before all positionals.
+ name_is_first_positional = !positionals.empty();
break;
-
- case 'S':
+ }
+ case 'S': {
shadows = 0;
break;
-
- case 'w':
+ }
+ case 'w': {
wrap_targets.push_back(w.woptarg);
break;
-
- case 'V':
- {
- if (wcsvarname(w.woptarg))
- {
- append_format(*out_err, _(L"%ls: Invalid variable name '%ls'"), argv[0], w.woptarg);
+ }
+ case 'V': {
+ if (wcsvarname(w.woptarg)) {
+ append_format(*out_err, _(L"%ls: Invalid variable name '%ls'"), argv[0],
+ w.woptarg);
res = STATUS_BUILTIN_ERROR;
break;
}
@@ -2149,120 +1637,87 @@ int define_function(parser_t &parser, io_streams_t &streams, const wcstring_list
inherit_vars.push_back(w.woptarg);
break;
}
-
- case 'h':
+ case 'h': {
builtin_print_help(parser, streams, argv[0], streams.out);
return STATUS_BUILTIN_OK;
-
- case 1:
+ }
+ case 1: {
assert(w.woptarg != NULL);
positionals.push_back(w.woptarg);
break;
-
- case '?':
- builtin_unknown_option(parser, streams, argv[0], argv[w.woptind-1]);
+ }
+ case '?': {
+ builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
res = 1;
break;
-
+ }
}
-
}
- if (!res)
- {
- /* Determine the function name, and remove it from the list of positionals */
+ if (!res) {
+ // Determine the function name, and remove it from the list of positionals.
wcstring function_name;
bool name_is_missing = positionals.empty();
- if (! name_is_missing)
- {
- if (name_is_first_positional)
- {
+ if (!name_is_missing) {
+ if (name_is_first_positional) {
function_name = positionals.front();
positionals.erase(positionals.begin());
- }
- else
- {
+ } else {
function_name = positionals.back();
positionals.erase(positionals.end() - 1);
}
}
-
- if (name_is_missing)
- {
- append_format(*out_err,
- _(L"%ls: Expected function name"),
- argv[0]);
- res=1;
- }
- else if (wcsfuncname(function_name))
- {
- append_format(*out_err,
- _(L"%ls: Illegal function name '%ls'"),
- argv[0],
- function_name.c_str());
- res=1;
- }
- else if (parser_keywords_is_reserved(function_name))
- {
-
- append_format(*out_err,
- _(L"%ls: The name '%ls' is reserved,\nand can not be used as a function name"),
- argv[0],
+ if (name_is_missing) {
+ append_format(*out_err, _(L"%ls: Expected function name"), argv[0]);
+ res = 1;
+ } else if (wcsfuncname(function_name)) {
+ append_format(*out_err, _(L"%ls: Illegal function name '%ls'"), argv[0],
function_name.c_str());
- res=1;
- }
- else if (function_name.empty())
- {
+ res = 1;
+ } else if (parser_keywords_is_reserved(function_name)) {
+ append_format(
+ *out_err,
+ _(L"%ls: The name '%ls' is reserved,\nand can not be used as a function name"),
+ argv[0], function_name.c_str());
+
+ res = 1;
+ } else if (function_name.empty()) {
append_format(*out_err, _(L"%ls: No function name given"), argv[0]);
- res=1;
- }
- else
- {
- if (has_named_arguments)
- {
- /* All remaining positionals are named arguments */
+ res = 1;
+ } else {
+ if (has_named_arguments) {
+ // All remaining positionals are named arguments.
named_arguments.swap(positionals);
- for (size_t i=0; i < named_arguments.size(); i++)
- {
- if (wcsvarname(named_arguments.at(i)))
- {
- append_format(*out_err,
- _(L"%ls: Invalid variable name '%ls'"),
- argv[0],
+ for (size_t i = 0; i < named_arguments.size(); i++) {
+ if (wcsvarname(named_arguments.at(i))) {
+ append_format(*out_err, _(L"%ls: Invalid variable name '%ls'"), argv[0],
named_arguments.at(i).c_str());
res = STATUS_BUILTIN_ERROR;
break;
}
}
- }
- else if (! positionals.empty())
- {
- // +1 because we already got the function name
- append_format(*out_err,
- _(L"%ls: Expected one argument, got %lu"),
- argv[0],
+ } else if (!positionals.empty()) {
+ // +1 because we already got the function name.
+ append_format(*out_err, _(L"%ls: Expected one argument, got %lu"), argv[0],
(unsigned long)(positionals.size() + 1));
- res=1;
+ res = 1;
}
}
- if (!res)
- {
- /* Here we actually define the function! */
+ if (!res) {
+ // Here we actually define the function!
function_data_t d;
d.name = function_name;
- if (desc)
- d.description = desc;
+ if (desc) d.description = desc;
d.events.swap(events);
d.shadows = shadows;
d.named_arguments.swap(named_arguments);
d.inherit_vars.swap(inherit_vars);
- for (size_t i=0; i<d.events.size(); i++)
- {
+ for (size_t i = 0; i < d.events.size(); i++) {
event_t &e = d.events.at(i);
e.function_name = d.name;
}
@@ -2271,9 +1726,8 @@ int define_function(parser_t &parser, io_streams_t &streams, const wcstring_list
function_add(d, parser, definition_line_offset);
- // Handle wrap targets
- for (size_t w=0; w < wrap_targets.size(); w++)
- {
+ // Handle wrap targets.
+ for (size_t w = 0; w < wrap_targets.size(); w++) {
complete_add_wrapper(function_name, wrap_targets.at(w));
}
}
@@ -2282,76 +1736,47 @@ int define_function(parser_t &parser, io_streams_t &streams, const wcstring_list
return res;
}
-/**
- The random builtin. For generating random numbers.
-*/
-static int builtin_random(parser_t &parser, io_streams_t &streams, wchar_t **argv)
-{
- static int seeded=0;
+// The random builtin. For generating random numbers.
+static int builtin_random(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
+ static int seeded = 0;
static struct drand48_data seed_buffer;
int argc = builtin_count_args(argv);
wgetopter_t w;
- static const struct woption
- long_options[] =
- {
- {
- L"help", no_argument, 0, 'h'
- }
- ,
- {
- 0, 0, 0, 0
- }
- }
- ;
+ static const struct woption long_options[] = {{L"help", no_argument, 0, 'h'}, {0, 0, 0, 0}};
- while (1)
- {
+ while (1) {
int opt_index = 0;
- int opt = w.wgetopt_long(argc,
- argv,
- L"h",
- long_options,
- &opt_index);
- if (opt == -1)
- break;
+ int opt = w.wgetopt_long(argc, argv, L"h", long_options, &opt_index);
+ if (opt == -1) break;
- switch (opt)
- {
- case 0:
- if (long_options[opt_index].flag != 0)
- break;
- streams.err.append_format(BUILTIN_ERR_UNKNOWN,
- argv[0],
- long_options[opt_index].name);
+ switch (opt) {
+ case 0: {
+ if (long_options[opt_index].flag != 0) break;
+ streams.err.append_format(BUILTIN_ERR_UNKNOWN, argv[0],
+ long_options[opt_index].name);
builtin_print_help(parser, streams, argv[0], streams.err);
-
return STATUS_BUILTIN_ERROR;
-
- case 'h':
+ }
+ case 'h': {
builtin_print_help(parser, streams, argv[0], streams.out);
break;
-
- case '?':
- builtin_unknown_option(parser, streams, argv[0], argv[w.woptind-1]);
+ }
+ case '?': {
+ builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
return STATUS_BUILTIN_ERROR;
-
+ }
}
-
}
- switch (argc-w.woptind)
- {
- case 0:
- {
+ switch (argc - w.woptind) {
+ case 0: {
long res;
-
- if (!seeded)
- {
- seeded=1;
+ if (!seeded) {
+ seeded = 1;
srand48_r(time(0), &seed_buffer);
}
lrand48_r(&seed_buffer, &res);
@@ -2360,32 +1785,25 @@ static int builtin_random(parser_t &parser, io_streams_t &streams, wchar_t **arg
streams.out.append_format(L"%ld\n", labs(res % 32768));
break;
}
-
- case 1:
- {
+ case 1: {
long foo;
- wchar_t *end=0;
+ wchar_t *end = 0;
- errno=0;
+ errno = 0;
foo = wcstol(argv[w.woptind], &end, 10);
- if (errno || *end)
- {
+ if (errno || *end) {
streams.err.append_format(_(L"%ls: Seed value '%ls' is not a valid number\n"),
- argv[0],
- argv[w.woptind]);
+ argv[0], argv[w.woptind]);
return STATUS_BUILTIN_ERROR;
}
- seeded=1;
+ seeded = 1;
srand48_r(foo, &seed_buffer);
break;
}
-
- default:
- {
- streams.err.append_format(_(L"%ls: Expected zero or one argument, got %d\n"),
- argv[0],
- argc-w.woptind);
+ default: {
+ streams.err.append_format(_(L"%ls: Expected zero or one argument, got %d\n"), argv[0],
+ argc - w.woptind);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
}
@@ -2393,12 +1811,8 @@ static int builtin_random(parser_t &parser, io_streams_t &streams, wchar_t **arg
return STATUS_BUILTIN_OK;
}
-
-/**
- The read builtin. Reads from stdin and stores the values in environment variables.
-*/
-static int builtin_read(parser_t &parser, io_streams_t &streams, wchar_t **argv)
-{
+// The read builtin. Reads from stdin and stores the values in environment variables.
+static int builtin_read(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
wgetopter_t w;
wcstring buff;
int i, argc = builtin_count_args(argv);
@@ -2406,260 +1820,181 @@ static int builtin_read(parser_t &parser, io_streams_t &streams, wchar_t **argv)
const wchar_t *prompt = DEFAULT_READ_PROMPT;
const wchar_t *right_prompt = L"";
const wchar_t *commandline = L"";
- int exit_res=STATUS_BUILTIN_OK;
+ int exit_res = STATUS_BUILTIN_OK;
const wchar_t *mode_name = READ_MODE_NAME;
- int nchars=0;
+ int nchars = 0;
wchar_t *end;
int shell = 0;
int array = 0;
bool split_null = false;
- while (1)
- {
- static const struct woption
- long_options[] =
- {
- {
- L"export", no_argument, 0, 'x'
- }
- ,
- {
- L"global", no_argument, 0, 'g'
- }
- ,
- {
- L"local", no_argument, 0, 'l'
- }
- ,
- {
- L"universal", no_argument, 0, 'U'
- }
- ,
- {
- L"unexport", no_argument, 0, 'u'
- }
- ,
- {
- L"prompt", required_argument, 0, 'p'
- }
- ,
- {
- L"right-prompt", required_argument, 0, 'R'
- }
- ,
- {
- L"command", required_argument, 0, 'c'
- }
- ,
- {
- L"mode-name", required_argument, 0, 'm'
- }
- ,
- {
- L"nchars", required_argument, 0, 'n'
- }
- ,
- {
- L"shell", no_argument, 0, 's'
- }
- ,
- {
- L"array", no_argument, 0, 'a'
- }
- ,
- {
- L"null", no_argument, 0, 'z'
- }
- ,
- {
- L"help", no_argument, 0, 'h'
- }
- ,
- {
- 0, 0, 0, 0
- }
- }
- ;
+ while (1) {
+ static const struct woption long_options[] = {{L"export", no_argument, 0, 'x'},
+ {L"global", no_argument, 0, 'g'},
+ {L"local", no_argument, 0, 'l'},
+ {L"universal", no_argument, 0, 'U'},
+ {L"unexport", no_argument, 0, 'u'},
+ {L"prompt", required_argument, 0, 'p'},
+ {L"right-prompt", required_argument, 0, 'R'},
+ {L"command", required_argument, 0, 'c'},
+ {L"mode-name", required_argument, 0, 'm'},
+ {L"nchars", required_argument, 0, 'n'},
+ {L"shell", no_argument, 0, 's'},
+ {L"array", no_argument, 0, 'a'},
+ {L"null", no_argument, 0, 'z'},
+ {L"help", no_argument, 0, 'h'},
+ {0, 0, 0, 0}};
int opt_index = 0;
- int opt = w.wgetopt_long(argc,
- argv,
- L"xglUup:R:c:hm:n:saz",
- long_options,
- &opt_index);
- if (opt == -1)
- break;
+ int opt = w.wgetopt_long(argc, argv, L"xglUup:R:c:hm:n:saz", long_options, &opt_index);
+ if (opt == -1) break;
- switch (opt)
- {
- case 0:
- if (long_options[opt_index].flag != 0)
- break;
- streams.err.append_format(BUILTIN_ERR_UNKNOWN,
- argv[0],
- long_options[opt_index].name);
+ switch (opt) {
+ case 0: {
+ if (long_options[opt_index].flag != 0) break;
+ streams.err.append_format(BUILTIN_ERR_UNKNOWN, argv[0],
+ long_options[opt_index].name);
builtin_print_help(parser, streams, argv[0], streams.err);
-
return STATUS_BUILTIN_ERROR;
-
- case L'x':
+ }
+ case L'x': {
place |= ENV_EXPORT;
break;
-
- case L'g':
+ }
+ case L'g': {
place |= ENV_GLOBAL;
break;
-
- case L'l':
+ }
+ case L'l': {
place |= ENV_LOCAL;
break;
-
- case L'U':
+ }
+ case L'U': {
place |= ENV_UNIVERSAL;
break;
-
- case L'u':
+ }
+ case L'u': {
place |= ENV_UNEXPORT;
break;
-
- case L'p':
+ }
+ case L'p': {
prompt = w.woptarg;
break;
-
- case L'R':
+ }
+ case L'R': {
right_prompt = w.woptarg;
break;
-
- case L'c':
+ }
+ case L'c': {
commandline = w.woptarg;
break;
-
- case L'm':
+ }
+ case L'm': {
mode_name = w.woptarg;
break;
-
- case L'n':
+ }
+ case L'n': {
errno = 0;
nchars = fish_wcstoi(w.woptarg, &end, 10);
- if (errno || *end != 0)
- {
- switch (errno)
- {
+ if (errno || *end != 0) {
+ switch (errno) {
case ERANGE:
- streams.err.append_format( _(L"%ls: Argument '%ls' is out of range\n"),
- argv[0],
- w.woptarg);
+ streams.err.append_format(_(L"%ls: Argument '%ls' is out of range\n"),
+ argv[0], w.woptarg);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
default:
- streams.err.append_format( _(L"%ls: Argument '%ls' must be an integer\n"),
- argv[0],
- w.woptarg);
+ streams.err.append_format(
+ _(L"%ls: Argument '%ls' must be an integer\n"), argv[0], w.woptarg);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
}
}
break;
-
- case 's':
+ }
+ case 's': {
shell = 1;
break;
-
- case 'a':
+ }
+ case 'a': {
array = 1;
break;
-
- case L'z':
+ }
+ case L'z': {
split_null = true;
break;
-
- case 'h':
+ }
+ case 'h': {
builtin_print_help(parser, streams, argv[0], streams.out);
return STATUS_BUILTIN_OK;
-
- case L'?':
- builtin_unknown_option(parser, streams, argv[0], argv[w.woptind-1]);
+ }
+ case L'?': {
+ builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
return STATUS_BUILTIN_ERROR;
+ }
}
-
}
- if ((place & ENV_UNEXPORT) && (place & ENV_EXPORT))
- {
- streams.err.append_format(BUILTIN_ERR_EXPUNEXP,
- argv[0]);
-
+ if ((place & ENV_UNEXPORT) && (place & ENV_EXPORT)) {
+ streams.err.append_format(BUILTIN_ERR_EXPUNEXP, argv[0]);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
}
- if ((place&ENV_LOCAL?1:0) + (place & ENV_GLOBAL?1:0) + (place & ENV_UNIVERSAL?1:0) > 1)
- {
- streams.err.append_format(BUILTIN_ERR_GLOCAL,
- argv[0]);
+ if ((place & ENV_LOCAL ? 1 : 0) + (place & ENV_GLOBAL ? 1 : 0) +
+ (place & ENV_UNIVERSAL ? 1 : 0) >
+ 1) {
+ streams.err.append_format(BUILTIN_ERR_GLOCAL, argv[0]);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
}
- if (array && w.woptind+1 != argc)
- {
- streams.err.append_format(_(L"%ls: --array option requires a single variable name.\n"), argv[0]);
+ if (array && w.woptind + 1 != argc) {
+ streams.err.append_format(_(L"%ls: --array option requires a single variable name.\n"),
+ argv[0]);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
}
- /*
- Verify all variable names
- */
- for (i=w.woptind; i<argc; i++)
- {
+ // Verify all variable names.
+ for (i = w.woptind; i < argc; i++) {
wchar_t *src;
- if (!wcslen(argv[i]))
- {
+ if (!wcslen(argv[i])) {
streams.err.append_format(BUILTIN_ERR_VARNAME_ZERO, argv[0]);
return STATUS_BUILTIN_ERROR;
}
- for (src=argv[i]; *src; src++)
- {
- if ((!iswalnum(*src)) && (*src != L'_'))
- {
+ for (src = argv[i]; *src; src++) {
+ if ((!iswalnum(*src)) && (*src != L'_')) {
streams.err.append_format(BUILTIN_ERR_VARCHAR, argv[0], *src);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
}
}
-
}
- /*
- The call to reader_readline may change woptind, so we save it away here
- */
- i=w.woptind;
+ // The call to reader_readline may change woptind, so we save it away here.
+ i = w.woptind;
- /*
- Check if we should read interactively using \c reader_readline()
- */
- if (isatty(0) && streams.stdin_fd == STDIN_FILENO && !split_null)
- {
+ // Check if we should read interactively using \c reader_readline().
+ if (isatty(0) && streams.stdin_fd == STDIN_FILENO && !split_null) {
const wchar_t *line;
reader_push(mode_name);
reader_set_left_prompt(prompt);
reader_set_right_prompt(right_prompt);
- if (shell)
- {
+ if (shell) {
reader_set_complete_function(&complete);
reader_set_highlight_function(&highlight_shell);
reader_set_test_function(&reader_shell_test);
}
- /* No autosuggestions or abbreviations in builtin_read */
+ // No autosuggestions or abbreviations in builtin_read.
reader_set_allow_autosuggesting(false);
reader_set_expand_abbreviations(false);
reader_set_exit_on_interrupt(true);
@@ -2670,177 +2005,139 @@ static int builtin_read(parser_t &parser, io_streams_t &streams, wchar_t **argv)
event_fire_generic(L"fish_prompt");
line = reader_readline(nchars);
proc_pop_interactive();
- if (line)
- {
- if (0 < nchars && nchars < wcslen(line))
- {
- // line may be longer than nchars if a keybinding used `commandline -i`
+ if (line) {
+ if (0 < nchars && nchars < wcslen(line)) {
+ // Line may be longer than nchars if a keybinding used `commandline -i`
// note: we're deliberately throwing away the tail of the commandline.
// It shouldn't be unread because it was produced with `commandline -i`,
// not typed.
buff = wcstring(line, nchars);
- }
- else
- {
+ } else {
buff = wcstring(line);
}
- }
- else
- {
+ } else {
exit_res = STATUS_BUILTIN_ERROR;
}
reader_pop();
- }
- else
- {
- int eof=0;
+ } else {
+ int eof = 0;
buff.clear();
- while (1)
- {
+ while (1) {
int finished = 0;
wchar_t res = 0;
mbstate_t state = {};
- while (!finished)
- {
+ while (!finished) {
char b;
- if (read_blocked(streams.stdin_fd, &b, 1) <= 0)
- {
- eof=1;
+ if (read_blocked(streams.stdin_fd, &b, 1) <= 0) {
+ eof = 1;
break;
}
- if (MB_CUR_MAX == 1) // single-byte locale
+ if (MB_CUR_MAX == 1) // single-byte locale
{
res = (unsigned char)b;
finished = 1;
- }
- else {
+ } else {
size_t sz = mbrtowc(&res, &b, 1, &state);
- switch (sz)
- {
- case (size_t)-1:
+ switch (sz) {
+ case (size_t)-1: {
memset(&state, 0, sizeof(state));
break;
-
- case (size_t)-2:
+ }
+ case (size_t)-2: {
break;
-
- default:
+ }
+ default: {
finished = 1;
break;
+ }
}
}
}
- if (eof)
- break;
+ if (eof) break;
- if (!split_null && res == L'\n')
- break;
+ if (!split_null && res == L'\n') break;
- if (split_null && res == L'\0')
- break;
+ if (split_null && res == L'\0') break;
buff.push_back(res);
- if (0 < nchars && (size_t)nchars <= buff.size())
- {
+ if (0 < nchars && (size_t)nchars <= buff.size()) {
break;
}
}
- if (buff.empty() && eof)
- {
+ if (buff.empty() && eof) {
exit_res = 1;
}
}
- if (i != argc && !exit_res)
- {
+ if (i != argc && !exit_res) {
env_var_t ifs = env_get_string(L"IFS");
- if (ifs.missing_or_empty())
- {
- /* Every character is a separate token */
+ if (ifs.missing_or_empty()) {
+ // Every character is a separate token.
size_t bufflen = buff.size();
- if (array)
- {
- if (bufflen > 0)
- {
- wcstring chars(bufflen+(bufflen-1), ARRAY_SEP);
+ if (array) {
+ if (bufflen > 0) {
+ wcstring chars(bufflen + (bufflen - 1), ARRAY_SEP);
wcstring::iterator out = chars.begin();
- for (wcstring::const_iterator it = buff.begin(), end = buff.end(); it != end; ++it)
- {
+ for (wcstring::const_iterator it = buff.begin(), end = buff.end(); it != end;
+ ++it) {
*out = *it;
out += 2;
}
env_set(argv[i], chars.c_str(), place);
- }
- else
- {
+ } else {
env_set(argv[i], NULL, place);
}
- }
- else
- {
+ } else {
size_t j = 0;
- for (; i+1 < argc; ++i)
- {
- if (j < bufflen)
- {
+ for (; i + 1 < argc; ++i) {
+ if (j < bufflen) {
wchar_t buffer[2] = {buff[j++], 0};
env_set(argv[i], buffer, place);
- }
- else
- {
+ } else {
env_set(argv[i], L"", place);
}
}
if (i < argc) env_set(argv[i], &buff[j], place);
}
- }
- else if (array)
- {
+ } else if (array) {
wcstring tokens;
tokens.reserve(buff.size());
bool empty = true;
-
- for (wcstring_range loc = wcstring_tok(buff, ifs); loc.first != wcstring::npos; loc = wcstring_tok(buff, ifs, loc))
- {
+
+ for (wcstring_range loc = wcstring_tok(buff, ifs); loc.first != wcstring::npos;
+ loc = wcstring_tok(buff, ifs, loc)) {
if (!empty) tokens.push_back(ARRAY_SEP);
tokens.append(buff, loc.first, loc.second);
empty = false;
}
env_set(argv[i], empty ? NULL : tokens.c_str(), place);
- }
- else
- {
- wcstring_range loc = wcstring_range(0,0);
+ } else {
+ wcstring_range loc = wcstring_range(0, 0);
- while (i<argc)
- {
- loc = wcstring_tok(buff, (i+1<argc) ? ifs : wcstring(), loc);
- env_set(argv[i], loc.first == wcstring::npos ? L"" : &buff.c_str()[loc.first], place);
+ while (i < argc) {
+ loc = wcstring_tok(buff, (i + 1 < argc) ? ifs : wcstring(), loc);
+ env_set(argv[i], loc.first == wcstring::npos ? L"" : &buff.c_str()[loc.first],
+ place);
++i;
}
-
}
}
return exit_res;
}
-/**
- The status builtin. Gives various status information on fish.
-*/
-static int builtin_status(parser_t &parser, io_streams_t &streams, wchar_t **argv)
-{
+// The status builtin. Gives various status information on fish.
+static int builtin_status(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
wgetopter_t w;
- enum
- {
+ enum {
NORMAL,
IS_SUBST,
IS_BLOCK,
@@ -2853,216 +2150,151 @@ static int builtin_status(parser_t &parser, io_streams_t &streams, wchar_t **arg
DONE,
CURRENT_FILENAME,
CURRENT_LINE_NUMBER
- }
- ;
+ };
int mode = NORMAL;
int argc = builtin_count_args(argv);
- int res=STATUS_BUILTIN_OK;
-
-
- const struct woption
- long_options[] =
- {
- {
- L"help", no_argument, 0, 'h'
- }
- ,
- {
- L"is-command-substitution", no_argument, 0, 'c'
- }
- ,
- {
- L"is-block", no_argument, 0, 'b'
- }
- ,
- {
- L"is-interactive", no_argument, 0, 'i'
- }
- ,
- {
- L"is-login", no_argument, 0, 'l'
- }
- ,
- {
- L"is-full-job-control", no_argument, &mode, IS_FULL_JOB_CONTROL
- }
- ,
- {
- L"is-interactive-job-control", no_argument, &mode, IS_INTERACTIVE_JOB_CONTROL
- }
- ,
- {
- L"is-no-job-control", no_argument, &mode, IS_NO_JOB_CONTROL
- }
- ,
- {
- L"current-filename", no_argument, 0, 'f'
- }
- ,
- {
- L"current-line-number", no_argument, 0, 'n'
- }
- ,
- {
- L"job-control", required_argument, 0, 'j'
- }
- ,
- {
- L"print-stack-trace", no_argument, 0, 't'
- }
- ,
- {
- 0, 0, 0, 0
- }
- }
- ;
+ int res = STATUS_BUILTIN_OK;
- while (1)
- {
+ const struct woption long_options[] = {
+ {L"help", no_argument, 0, 'h'},
+ {L"is-command-substitution", no_argument, 0, 'c'},
+ {L"is-block", no_argument, 0, 'b'},
+ {L"is-interactive", no_argument, 0, 'i'},
+ {L"is-login", no_argument, 0, 'l'},
+ {L"is-full-job-control", no_argument, &mode, IS_FULL_JOB_CONTROL},
+ {L"is-interactive-job-control", no_argument, &mode, IS_INTERACTIVE_JOB_CONTROL},
+ {L"is-no-job-control", no_argument, &mode, IS_NO_JOB_CONTROL},
+ {L"current-filename", no_argument, 0, 'f'},
+ {L"current-line-number", no_argument, 0, 'n'},
+ {L"job-control", required_argument, 0, 'j'},
+ {L"print-stack-trace", no_argument, 0, 't'},
+ {0, 0, 0, 0}};
+
+ while (1) {
int opt_index = 0;
- int opt = w.wgetopt_long(argc,
- argv,
- L":cbilfnhj:t",
- long_options,
- &opt_index);
- if (opt == -1)
- break;
+ int opt = w.wgetopt_long(argc, argv, L":cbilfnhj:t", long_options, &opt_index);
+ if (opt == -1) break;
- switch (opt)
- {
- case 0:
- if (long_options[opt_index].flag != 0)
- break;
- streams.err.append_format(BUILTIN_ERR_UNKNOWN,
- argv[0],
- long_options[opt_index].name);
+ switch (opt) {
+ case 0: {
+ if (long_options[opt_index].flag != 0) break;
+ streams.err.append_format(BUILTIN_ERR_UNKNOWN, argv[0],
+ long_options[opt_index].name);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
-
- case 'c':
+ }
+ case 'c': {
mode = IS_SUBST;
break;
-
- case 'b':
+ }
+ case 'b': {
mode = IS_BLOCK;
break;
-
- case 'i':
+ }
+ case 'i': {
mode = IS_INTERACTIVE;
break;
-
- case 'l':
+ }
+ case 'l': {
mode = IS_LOGIN;
break;
-
- case 'f':
+ }
+ case 'f': {
mode = CURRENT_FILENAME;
break;
-
- case 'n':
+ }
+ case 'n': {
mode = CURRENT_LINE_NUMBER;
break;
-
- case 'h':
+ }
+ case 'h': {
builtin_print_help(parser, streams, argv[0], streams.out);
return STATUS_BUILTIN_OK;
-
- case 'j':
+ }
+ case 'j': {
if (wcscmp(w.woptarg, L"full") == 0)
job_control_mode = JOB_CONTROL_ALL;
else if (wcscmp(w.woptarg, L"interactive") == 0)
job_control_mode = JOB_CONTROL_INTERACTIVE;
else if (wcscmp(w.woptarg, L"none") == 0)
job_control_mode = JOB_CONTROL_NONE;
- else
- {
- streams.err.append_format(L"%ls: Invalid job control mode '%ls'\n",
- L"status", w.woptarg);
+ else {
+ streams.err.append_format(L"%ls: Invalid job control mode '%ls'\n", L"status",
+ w.woptarg);
res = 1;
}
mode = DONE;
break;
-
- case 't':
+ }
+ case 't': {
mode = STACK_TRACE;
break;
-
-
- case ':':
- builtin_missing_argument(parser, streams, argv[0], argv[w.woptind-1]);
+ }
+ case ':': {
+ builtin_missing_argument(parser, streams, argv[0], argv[w.woptind - 1]);
return STATUS_BUILTIN_ERROR;
-
- case '?':
- builtin_unknown_option(parser, streams, argv[0], argv[w.woptind-1]);
+ }
+ case '?': {
+ builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
return STATUS_BUILTIN_ERROR;
-
+ }
}
-
}
- if (!res)
- {
-
- switch (mode)
- {
- case CURRENT_FILENAME:
- {
+ if (!res) {
+ switch (mode) {
+ case CURRENT_FILENAME: {
const wchar_t *fn = parser.current_filename();
- if (!fn)
- fn = _(L"Standard input");
+ if (!fn) fn = _(L"Standard input");
- streams.out.append_format( L"%ls\n", fn);
+ streams.out.append_format(L"%ls\n", fn);
break;
}
-
- case CURRENT_LINE_NUMBER:
- {
- streams.out.append_format( L"%d\n", parser.get_lineno());
+ case CURRENT_LINE_NUMBER: {
+ streams.out.append_format(L"%d\n", parser.get_lineno());
break;
}
-
- case IS_INTERACTIVE:
+ case IS_INTERACTIVE: {
return !is_interactive_session;
-
- case IS_SUBST:
+ }
+ case IS_SUBST: {
return !is_subshell;
-
- case IS_BLOCK:
+ }
+ case IS_BLOCK: {
return !is_block;
-
- case IS_LOGIN:
+ }
+ case IS_LOGIN: {
return !is_login;
-
- case IS_FULL_JOB_CONTROL:
+ }
+ case IS_FULL_JOB_CONTROL: {
return job_control_mode != JOB_CONTROL_ALL;
-
- case IS_INTERACTIVE_JOB_CONTROL:
+ }
+ case IS_INTERACTIVE_JOB_CONTROL: {
return job_control_mode != JOB_CONTROL_INTERACTIVE;
-
- case IS_NO_JOB_CONTROL:
+ }
+ case IS_NO_JOB_CONTROL: {
return job_control_mode != JOB_CONTROL_NONE;
-
- case STACK_TRACE:
- {
+ }
+ case STACK_TRACE: {
streams.out.append(parser.stack_trace());
break;
}
-
- case NORMAL:
- {
+ case NORMAL: {
if (is_login)
- streams.out.append_format( _(L"This is a login shell\n"));
+ streams.out.append_format(_(L"This is a login shell\n"));
else
- streams.out.append_format( _(L"This is not a login shell\n"));
+ streams.out.append_format(_(L"This is not a login shell\n"));
- streams.out.append_format( _(L"Job control: %ls\n"),
- job_control_mode==JOB_CONTROL_INTERACTIVE?_(L"Only on interactive jobs"):
- (job_control_mode==JOB_CONTROL_NONE ? _(L"Never") : _(L"Always")));
+ streams.out.append_format(
+ _(L"Job control: %ls\n"),
+ job_control_mode == JOB_CONTROL_INTERACTIVE
+ ? _(L"Only on interactive jobs")
+ : (job_control_mode == JOB_CONTROL_NONE ? _(L"Never") : _(L"Always")));
streams.out.append(parser.stack_trace());
break;
}
@@ -3072,262 +2304,179 @@ static int builtin_status(parser_t &parser, io_streams_t &streams, wchar_t **arg
return res;
}
-
-/**
- The exit builtin. Calls reader_exit to exit and returns the value specified.
-*/
-static int builtin_exit(parser_t &parser, io_streams_t &streams, wchar_t **argv)
-{
+// The exit builtin. Calls reader_exit to exit and returns the value specified.
+static int builtin_exit(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
int argc = builtin_count_args(argv);
- long ec=0;
- switch (argc)
- {
- case 1:
- {
+ long ec = 0;
+ switch (argc) {
+ case 1: {
ec = proc_get_last_status();
break;
}
-
- case 2:
- {
+ case 2: {
wchar_t *end;
errno = 0;
- ec = wcstol(argv[1],&end,10);
- if (errno || *end != 0)
- {
- streams.err.append_format(_(L"%ls: Argument '%ls' must be an integer\n"),
- argv[0],
- argv[1]);
+ ec = wcstol(argv[1], &end, 10);
+ if (errno || *end != 0) {
+ streams.err.append_format(_(L"%ls: Argument '%ls' must be an integer\n"), argv[0],
+ argv[1]);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
}
break;
}
-
- default:
- {
- streams.err.append_format(BUILTIN_ERR_TOO_MANY_ARGUMENTS,
- argv[0]);
+ default: {
+ streams.err.append_format(BUILTIN_ERR_TOO_MANY_ARGUMENTS, argv[0]);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
}
-
}
reader_exit(1, 0);
return (int)ec;
}
-/**
- The cd builtin. Changes the current directory to the one specified
- or to $HOME if none is specified. The directory can be relative to
- any directory in the CDPATH variable.
-*/
-static int builtin_cd(parser_t &parser, io_streams_t &streams, wchar_t **argv)
-{
+// The cd builtin. Changes the current directory to the one specified or to $HOME if none is
+// specified. The directory can be relative to any directory in the CDPATH variable.
+static int builtin_cd(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
env_var_t dir_in;
wcstring dir;
- int res=STATUS_BUILTIN_OK;
-
+ int res = STATUS_BUILTIN_OK;
- if (argv[1] == NULL)
- {
+ if (argv[1] == NULL) {
dir_in = env_get_string(L"HOME");
- if (dir_in.missing_or_empty())
- {
- streams.err.append_format(_(L"%ls: Could not find home directory\n"),
- argv[0]);
+ if (dir_in.missing_or_empty()) {
+ streams.err.append_format(_(L"%ls: Could not find home directory\n"), argv[0]);
}
- }
- else
- {
+ } else {
dir_in = env_var_t(argv[1]);
}
bool got_cd_path = false;
- if (! dir_in.missing())
- {
+ if (!dir_in.missing()) {
got_cd_path = path_get_cdpath(dir_in, &dir);
}
- if (!got_cd_path)
- {
- if (errno == ENOTDIR)
- {
- streams.err.append_format(_(L"%ls: '%ls' is not a directory\n"),
- argv[0],
- dir_in.c_str());
- }
- else if (errno == ENOENT)
- {
- streams.err.append_format(_(L"%ls: The directory '%ls' does not exist\n"),
- argv[0],
- dir_in.c_str());
- }
- else if (errno == EROTTEN)
- {
- streams.err.append_format(_(L"%ls: '%ls' is a rotten symlink\n"),
- argv[0],
- dir_in.c_str());
+ if (!got_cd_path) {
+ if (errno == ENOTDIR) {
+ streams.err.append_format(_(L"%ls: '%ls' is not a directory\n"), argv[0],
+ dir_in.c_str());
+ } else if (errno == ENOENT) {
+ streams.err.append_format(_(L"%ls: The directory '%ls' does not exist\n"), argv[0],
+ dir_in.c_str());
+ } else if (errno == EROTTEN) {
+ streams.err.append_format(_(L"%ls: '%ls' is a rotten symlink\n"), argv[0],
+ dir_in.c_str());
- }
- else
- {
+ } else {
streams.err.append_format(_(L"%ls: Unknown error trying to locate directory '%ls'\n"),
- argv[0],
- dir_in.c_str());
-
+ argv[0], dir_in.c_str());
}
-
- if (!get_is_interactive())
- {
+ if (!get_is_interactive()) {
streams.err.append(parser.current_line());
}
res = 1;
- }
- else if (wchdir(dir) != 0)
- {
+ } else if (wchdir(dir) != 0) {
struct stat buffer;
int status;
status = wstat(dir, &buffer);
- if (!status && S_ISDIR(buffer.st_mode))
- {
- streams.err.append_format(_(L"%ls: Permission denied: '%ls'\n"),
- argv[0],
- dir.c_str());
+ if (!status && S_ISDIR(buffer.st_mode)) {
+ streams.err.append_format(_(L"%ls: Permission denied: '%ls'\n"), argv[0], dir.c_str());
+ } else {
+ streams.err.append_format(_(L"%ls: '%ls' is not a directory\n"), argv[0], dir.c_str());
}
- else
- {
- streams.err.append_format(_(L"%ls: '%ls' is not a directory\n"),
- argv[0],
- dir.c_str());
- }
-
- if (!get_is_interactive())
- {
+ if (!get_is_interactive()) {
streams.err.append(parser.current_line());
}
res = 1;
- }
- else if (!env_set_pwd())
- {
- res=1;
+ } else if (!env_set_pwd()) {
+ res = 1;
streams.err.append_format(_(L"%ls: Could not set PWD variable\n"), argv[0]);
}
return res;
}
-/**
- Implementation of the builtin count command, used to count the
- number of arguments sent to it.
- */
-static int builtin_count(parser_t &parser, io_streams_t &streams, wchar_t **argv)
-{
+// Implementation of the builtin count command, used to count the number of arguments sent to it.
+static int builtin_count(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
int argc;
argc = builtin_count_args(argv);
- streams.out.append_format( L"%d\n", argc-1);
- return !(argc-1);
+ streams.out.append_format(L"%d\n", argc - 1);
+ return !(argc - 1);
}
-/**
- Implementation of the builtin contains command, used to check if a
- specified string is part of a list.
- */
-static int builtin_contains(parser_t &parser, io_streams_t &streams, wchar_t ** argv)
-{
+// Implementation of the builtin contains command, used to check if a specified string is part of a
+// list.
+static int builtin_contains(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
wgetopter_t w;
int argc;
argc = builtin_count_args(argv);
wchar_t *needle;
bool should_output_index = false;
- const struct woption long_options[] =
- {
- { L"help", no_argument, 0, 'h' } ,
- { L"index", no_argument, 0, 'i' },
- { 0, 0, 0, 0 }
- };
+ const struct woption long_options[] = {
+ {L"help", no_argument, 0, 'h'}, {L"index", no_argument, 0, 'i'}, {0, 0, 0, 0}};
- while (1)
- {
+ while (1) {
int opt_index = 0;
- int opt = w.wgetopt_long(argc,
- argv,
- L"+hi",
- long_options,
- &opt_index);
- if (opt == -1)
- break;
+ int opt = w.wgetopt_long(argc, argv, L"+hi", long_options, &opt_index);
+ if (opt == -1) break;
- switch (opt)
- {
- case 0:
- assert(opt_index >= 0 && (size_t)opt_index < sizeof long_options / sizeof *long_options);
- if (long_options[opt_index].flag != 0)
- break;
- streams.err.append_format(BUILTIN_ERR_UNKNOWN,
- argv[0],
- long_options[opt_index].name);
+ switch (opt) {
+ case 0: {
+ assert(opt_index >= 0 &&
+ (size_t)opt_index < sizeof long_options / sizeof *long_options);
+ if (long_options[opt_index].flag != 0) break;
+ streams.err.append_format(BUILTIN_ERR_UNKNOWN, argv[0],
+ long_options[opt_index].name);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
-
-
- case 'h':
+ }
+ case 'h': {
builtin_print_help(parser, streams, argv[0], streams.out);
return STATUS_BUILTIN_OK;
-
-
- case ':':
- builtin_missing_argument(parser, streams, argv[0], argv[w.woptind-1]);
+ }
+ case ':': {
+ builtin_missing_argument(parser, streams, argv[0], argv[w.woptind - 1]);
return STATUS_BUILTIN_ERROR;
-
- case '?':
- builtin_unknown_option(parser, streams, argv[0], argv[w.woptind-1]);
+ }
+ case '?': {
+ builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
return STATUS_BUILTIN_ERROR;
-
- case 'i':
+ }
+ case 'i': {
should_output_index = true;
break;
+ }
}
-
}
needle = argv[w.woptind];
- if (!needle)
- {
+ if (!needle) {
streams.err.append_format(_(L"%ls: Key not specified\n"), argv[0]);
- }
- else
- {
- for (int i=w.woptind+1; i<argc; i++)
- {
-
- if (!wcscmp(needle, argv[i]))
- {
- if (should_output_index) streams.out.append_format( L"%d\n", i-w.woptind);
+ } else {
+ for (int i = w.woptind + 1; i < argc; i++) {
+ if (!wcscmp(needle, argv[i])) {
+ if (should_output_index) streams.out.append_format(L"%d\n", i - w.woptind);
return 0;
}
}
}
return 1;
-
}
-
/**
The . (dot) builtin, sometimes called source. Evaluates the contents of a file.
*/
-static int builtin_source(parser_t &parser, io_streams_t &streams, wchar_t **argv)
-{
+static int builtin_source(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
ASSERT_IS_MAIN_THREAD();
int fd;
int res = STATUS_BUILTIN_OK;
@@ -3338,32 +2487,27 @@ static int builtin_source(parser_t &parser, io_streams_t &streams, wchar_t **arg
const wchar_t *fn, *fn_intern;
- if (argc < 2 || (wcscmp(argv[1], L"-") == 0))
- {
+ if (argc < 2 || (wcscmp(argv[1], L"-") == 0)) {
fn = L"-";
fn_intern = fn;
fd = dup(streams.stdin_fd);
- }
- else
- {
-
- if ((fd = wopen_cloexec(argv[1], O_RDONLY)) == -1)
- {
- streams.err.append_format(_(L"%ls: Error encountered while sourcing file '%ls':\n"), argv[0], argv[1]);
+ } else {
+ if ((fd = wopen_cloexec(argv[1], O_RDONLY)) == -1) {
+ streams.err.append_format(_(L"%ls: Error encountered while sourcing file '%ls':\n"),
+ argv[0], argv[1]);
builtin_wperror(L"source", streams);
return STATUS_BUILTIN_ERROR;
}
- if (fstat(fd, &buf) == -1)
- {
+ if (fstat(fd, &buf) == -1) {
close(fd);
- streams.err.append_format(_(L"%ls: Error encountered while sourcing file '%ls':\n"), argv[0], argv[1]);
+ streams.err.append_format(_(L"%ls: Error encountered while sourcing file '%ls':\n"),
+ argv[0], argv[1]);
builtin_wperror(L"source", streams);
return STATUS_BUILTIN_ERROR;
}
- if (!S_ISREG(buf.st_mode))
- {
+ if (!S_ISREG(buf.st_mode)) {
close(fd);
streams.err.append_format(_(L"%ls: '%ls' is not a file\n"), argv[0], argv[1]);
return STATUS_BUILTIN_ERROR;
@@ -3381,166 +2525,103 @@ static int builtin_source(parser_t &parser, io_streams_t &streams, wchar_t **arg
parser.pop_block();
- if (res)
- {
- streams.err.append_format(_(L"%ls: Error while reading file '%ls'\n"),
- argv[0],
- fn_intern == intern_static(L"-") ? L"<stdin>" : fn_intern);
- }
- else
- {
+ if (res) {
+ streams.err.append_format(_(L"%ls: Error while reading file '%ls'\n"), argv[0],
+ fn_intern == intern_static(L"-") ? L"<stdin>" : fn_intern);
+ } else {
res = proc_get_last_status();
}
- /*
- Do not close fd after calling reader_read. reader_read
- automatically closes it before calling eval.
- */
-
+ // Do not close fd after calling reader_read. reader_read automatically closes it before calling
+ // eval.
reader_pop_current_filename();
return res;
}
-/**
- Make the specified job the first job of the job list. Moving jobs
- around in the list makes the list reflect the order in which the
- jobs were used.
-*/
-static void make_first(job_t *j)
-{
- job_promote(j);
-}
-
-
-/**
- Builtin for putting a job in the foreground
-*/
-static int builtin_fg(parser_t &parser, io_streams_t &streams, wchar_t **argv)
-{
- job_t *j=NULL;
+// Make the specified job the first job of the job list. Moving jobs around in the list makes the
+// list reflect the order in which the jobs were used.
+static void make_first(job_t *j) { job_promote(j); }
- if (argv[1] == 0)
- {
- /*
- Select last constructed job (I.e. first job in the job que)
- that is possible to put in the foreground
- */
+// Builtin for putting a job in the foreground.
+static int builtin_fg(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
+ job_t *j = NULL;
+ if (argv[1] == 0) {
+ // Select last constructed job (I.e. first job in the job que) that is possible to put in
+ // the foreground.
job_iterator_t jobs;
- while ((j = jobs.next()))
- {
+ while ((j = jobs.next())) {
if (job_get_flag(j, JOB_CONSTRUCTED) && (!job_is_completed(j)) &&
- ((job_is_stopped(j) || (!job_get_flag(j, JOB_FOREGROUND))) && job_get_flag(j, JOB_CONTROL)))
- {
+ ((job_is_stopped(j) || (!job_get_flag(j, JOB_FOREGROUND))) &&
+ job_get_flag(j, JOB_CONTROL))) {
break;
}
}
- if (!j)
- {
- streams.err.append_format(_(L"%ls: There are no suitable jobs\n"),
- argv[0]);
+ if (!j) {
+ streams.err.append_format(_(L"%ls: There are no suitable jobs\n"), argv[0]);
}
- }
- else if (argv[2] != 0)
- {
- /*
- Specifying what more than one job to put to the foreground
- is a syntax error, we still try to locate the job argv[1],
- since we want to know if this is an ambigous job
- specification or if this is an malformed job id
- */
+ } else if (argv[2] != 0) {
+ // Specifying what more than one job to put to the foreground is a syntax error, we still
+ // try to locate the job argv[1], since we want to know if this is an ambigous job
+ // specification or if this is an malformed job id.
wchar_t *endptr;
int pid;
int found_job = 0;
errno = 0;
pid = fish_wcstoi(argv[1], &endptr, 10);
- if (!(*endptr || errno))
- {
+ if (!(*endptr || errno)) {
j = job_get_from_pid(pid);
- if (j)
- found_job = 1;
+ if (j) found_job = 1;
}
- if (found_job)
- {
- streams.err.append_format(_(L"%ls: Ambiguous job\n"),
- argv[0]);
- }
- else
- {
- streams.err.append_format(_(L"%ls: '%ls' is not a job\n"),
- argv[0],
- argv[1]);
+ if (found_job) {
+ streams.err.append_format(_(L"%ls: Ambiguous job\n"), argv[0]);
+ } else {
+ streams.err.append_format(_(L"%ls: '%ls' is not a job\n"), argv[0], argv[1]);
}
builtin_print_help(parser, streams, argv[0], streams.err);
- j=0;
+ j = 0;
- }
- else
- {
+ } else {
wchar_t *end;
int pid;
errno = 0;
pid = abs(fish_wcstoi(argv[1], &end, 10));
- if (*end || errno)
- {
- streams.err.append_format(BUILTIN_ERR_NOT_NUMBER,
- argv[0],
- argv[1]);
+ if (*end || errno) {
+ streams.err.append_format(BUILTIN_ERR_NOT_NUMBER, argv[0], argv[1]);
builtin_print_help(parser, streams, argv[0], streams.err);
- }
- else
- {
+ } else {
j = job_get_from_pid(pid);
- if (!j || !job_get_flag(j, JOB_CONSTRUCTED) || job_is_completed(j))
- {
- streams.err.append_format(_(L"%ls: No suitable job: %d\n"),
- argv[0],
- pid);
+ if (!j || !job_get_flag(j, JOB_CONSTRUCTED) || job_is_completed(j)) {
+ streams.err.append_format(_(L"%ls: No suitable job: %d\n"), argv[0], pid);
builtin_print_help(parser, streams, argv[0], streams.err);
- j=0;
- }
- else if (!job_get_flag(j, JOB_CONTROL))
- {
- streams.err.append_format(_(L"%ls: Can't put job %d, '%ls' to foreground because it is not under job control\n"),
- argv[0],
- pid,
- j->command_wcstr());
+ j = 0;
+ } else if (!job_get_flag(j, JOB_CONTROL)) {
+ streams.err.append_format(_(L"%ls: Can't put job %d, '%ls' to foreground because "
+ L"it is not under job control\n"),
+ argv[0], pid, j->command_wcstr());
builtin_print_help(parser, streams, argv[0], streams.err);
- j=0;
+ j = 0;
}
}
}
- if (j)
- {
- if (streams.err_is_redirected)
- {
- streams.err.append_format(FG_MSG,
- j->job_id,
- j->command_wcstr());
- }
- else
- {
- /*
- If we aren't redirecting, send output to real stderr,
- since stuff in sb_err won't get printed until the
- command finishes.
- */
- fwprintf(stderr,
- FG_MSG,
- j->job_id,
- j->command_wcstr());
+ if (j) {
+ if (streams.err_is_redirected) {
+ streams.err.append_format(FG_MSG, j->job_id, j->command_wcstr());
+ } else {
+ // If we aren't redirecting, send output to real stderr, since stuff in sb_err won't get
+ // printed until the command finishes.
+ fwprintf(stderr, FG_MSG, j->job_id, j->command_wcstr());
}
const wcstring ft = tok_first(j->command());
- if (! ft.empty())
- env_set(L"_", ft.c_str(), ENV_EXPORT);
+ if (!ft.empty()) env_set(L"_", ft.c_str(), ENV_EXPORT);
reader_write_title(j->command());
make_first(j);
@@ -3551,33 +2632,21 @@ static int builtin_fg(parser_t &parser, io_streams_t &streams, wchar_t **argv)
return j != 0;
}
-/**
- Helper function for builtin_bg()
-*/
-static int send_to_bg(parser_t &parser, io_streams_t &streams, job_t *j, const wchar_t *name)
-{
- if (j == 0)
- {
- streams.err.append_format(_(L"%ls: Unknown job '%ls'\n"),
- L"bg",
- name);
+// Helper function for builtin_bg().
+static int send_to_bg(parser_t &parser, io_streams_t &streams, job_t *j, const wchar_t *name) {
+ if (j == 0) {
+ streams.err.append_format(_(L"%ls: Unknown job '%ls'\n"), L"bg", name);
builtin_print_help(parser, streams, L"bg", streams.err);
return STATUS_BUILTIN_ERROR;
- }
- else if (!job_get_flag(j, JOB_CONTROL))
- {
- streams.err.append_format(_(L"%ls: Can't put job %d, '%ls' to background because it is not under job control\n"),
- L"bg",
- j->job_id,
- j->command_wcstr());
+ } else if (!job_get_flag(j, JOB_CONTROL)) {
+ streams.err.append_format(
+ _(L"%ls: Can't put job %d, '%ls' to background because it is not under job control\n"),
+ L"bg", j->job_id, j->command_wcstr());
builtin_print_help(parser, streams, L"bg", streams.err);
return STATUS_BUILTIN_ERROR;
- }
- else
- {
- streams.err.append_format(_(L"Send job %d '%ls' to background\n"),
- j->job_id,
- j->command_wcstr());
+ } else {
+ streams.err.append_format(_(L"Send job %d '%ls' to background\n"), j->job_id,
+ j->command_wcstr());
}
make_first(j);
job_set_flag(j, JOB_FOREGROUND, 0);
@@ -3585,62 +2654,43 @@ static int send_to_bg(parser_t &parser, io_streams_t &streams, job_t *j, const w
return STATUS_BUILTIN_OK;
}
-
-/**
- Builtin for putting a job in the background
-*/
-static int builtin_bg(parser_t &parser, io_streams_t &streams, wchar_t **argv)
-{
+// Builtin for putting a job in the background.
+static int builtin_bg(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
int res = STATUS_BUILTIN_OK;
- if (argv[1] == 0)
- {
+ if (argv[1] == 0) {
job_t *j;
job_iterator_t jobs;
- while ((j = jobs.next()))
- {
- if (job_is_stopped(j) && job_get_flag(j, JOB_CONTROL) && (!job_is_completed(j)))
- {
+ while ((j = jobs.next())) {
+ if (job_is_stopped(j) && job_get_flag(j, JOB_CONTROL) && (!job_is_completed(j))) {
break;
}
}
- if (!j)
- {
- streams.err.append_format(_(L"%ls: There are no suitable jobs\n"),
- argv[0]);
+ if (!j) {
+ streams.err.append_format(_(L"%ls: There are no suitable jobs\n"), argv[0]);
res = 1;
- }
- else
- {
+ } else {
res = send_to_bg(parser, streams, j, _(L"(default)"));
}
- }
- else
- {
+ } else {
wchar_t *end;
int i;
int pid;
int err = 0;
- for (i=1; argv[i]; i++)
- {
- errno=0;
+ for (i = 1; argv[i]; i++) {
+ errno = 0;
pid = fish_wcstoi(argv[i], &end, 10);
- if (errno || pid < 0 || *end || !job_get_from_pid(pid))
- {
- streams.err.append_format(_(L"%ls: '%ls' is not a job\n"),
- argv[0],
- argv[i]);
+ if (errno || pid < 0 || *end || !job_get_from_pid(pid)) {
+ streams.err.append_format(_(L"%ls: '%ls' is not a job\n"), argv[0], argv[i]);
err = 1;
break;
}
}
- if (!err)
- {
- for (i=1; !res && argv[i]; i++)
- {
+ if (!err) {
+ for (i = 1; !res && argv[i]; i++) {
pid = fish_wcstoi(argv[i], 0, 10);
res |= send_to_bg(parser, streams, job_get_from_pid(pid), *argv);
}
@@ -3650,48 +2700,36 @@ static int builtin_bg(parser_t &parser, io_streams_t &streams, wchar_t **argv)
return res;
}
-
-/**
- This function handles both the 'continue' and the 'break' builtins
- that are used for loop control.
-*/
-static int builtin_break_continue(parser_t &parser, io_streams_t &streams, wchar_t **argv)
-{
- int is_break = (wcscmp(argv[0],L"break")==0);
+// This function handles both the 'continue' and the 'break' builtins that are used for loop
+// control.
+static int builtin_break_continue(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
+ int is_break = (wcscmp(argv[0], L"break") == 0);
int argc = builtin_count_args(argv);
-
- if (argc != 1)
- {
- streams.err.append_format(BUILTIN_ERR_UNKNOWN,
- argv[0],
- argv[1]);
+ if (argc != 1) {
+ streams.err.append_format(BUILTIN_ERR_UNKNOWN, argv[0], argv[1]);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
}
- /* Find the index of the enclosing for or while loop. Recall that incrementing loop_idx goes 'up' to outer blocks */
+ // Find the index of the enclosing for or while loop. Recall that incrementing loop_idx goes
+ // 'up' to outer blocks.
size_t loop_idx;
- for (loop_idx = 0; loop_idx < parser.block_count(); loop_idx++)
- {
+ for (loop_idx = 0; loop_idx < parser.block_count(); loop_idx++) {
const block_t *b = parser.block_at_index(loop_idx);
- if (b->type() == WHILE || b->type() == FOR)
- break;
+ if (b->type() == WHILE || b->type() == FOR) break;
}
- if (loop_idx >= parser.block_count())
- {
- streams.err.append_format(_(L"%ls: Not inside of loop\n"),
- argv[0]);
+ if (loop_idx >= parser.block_count()) {
+ streams.err.append_format(_(L"%ls: Not inside of loop\n"), argv[0]);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
}
- /* Skip blocks interior to the loop */
+ // Skip blocks interior to the loop.
size_t block_idx = loop_idx;
- while (block_idx--)
- {
+ while (block_idx--) {
parser.block_at_index(block_idx)->skip = true;
}
@@ -3702,13 +2740,8 @@ static int builtin_break_continue(parser_t &parser, io_streams_t &streams, wchar
return STATUS_BUILTIN_OK;
}
-/**
- Implementation of the builtin breakpoint command, used to launch the
- interactive debugger.
- */
-
-static int builtin_breakpoint(parser_t &parser, io_streams_t &streams, wchar_t **argv)
-{
+// Implementation of the builtin breakpoint command, used to launch the interactive debugger.
+static int builtin_breakpoint(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
parser.push_block(new breakpoint_block_t());
reader_read(STDIN_FILENO, streams.io_chain ? *streams.io_chain : io_chain_t());
@@ -3718,61 +2751,49 @@ static int builtin_breakpoint(parser_t &parser, io_streams_t &streams, wchar_t *
return proc_get_last_status();
}
-
-/**
- Function for handling the \c return builtin
-*/
-static int builtin_return(parser_t &parser, io_streams_t &streams, wchar_t **argv)
-{
+// Function for handling the \c return builtin.
+static int builtin_return(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
int argc = builtin_count_args(argv);
int status = proc_get_last_status();
- switch (argc)
- {
- case 1:
+ switch (argc) {
+ case 1: {
break;
- case 2:
- {
+ }
+ case 2: {
wchar_t *end;
errno = 0;
- status = fish_wcstoi(argv[1],&end,10);
- if (errno || *end != 0)
- {
- streams.err.append_format(_(L"%ls: Argument '%ls' must be an integer\n"),
- argv[0],
- argv[1]);
+ status = fish_wcstoi(argv[1], &end, 10);
+ if (errno || *end != 0) {
+ streams.err.append_format(_(L"%ls: Argument '%ls' must be an integer\n"), argv[0],
+ argv[1]);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
}
break;
}
- default:
- streams.err.append_format(_(L"%ls: Too many arguments\n"),
- argv[0]);
+ default: {
+ streams.err.append_format(_(L"%ls: Too many arguments\n"), argv[0]);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
+ }
}
- /* Find the function block */
+ // Find the function block.
size_t function_block_idx;
- for (function_block_idx = 0; function_block_idx < parser.block_count(); function_block_idx++)
- {
+ for (function_block_idx = 0; function_block_idx < parser.block_count(); function_block_idx++) {
const block_t *b = parser.block_at_index(function_block_idx);
- if (b->type() == FUNCTION_CALL || b->type() == FUNCTION_CALL_NO_SHADOW)
- break;
+ if (b->type() == FUNCTION_CALL || b->type() == FUNCTION_CALL_NO_SHADOW) break;
}
- if (function_block_idx >= parser.block_count())
- {
- streams.err.append_format(_(L"%ls: Not inside of function\n"),
- argv[0]);
+ if (function_block_idx >= parser.block_count()) {
+ streams.err.append_format(_(L"%ls: Not inside of function\n"), argv[0]);
builtin_print_help(parser, streams, argv[0], streams.err);
return STATUS_BUILTIN_ERROR;
}
- /* Skip everything up to (and then including) the function block */
- for (size_t i=0; i < function_block_idx; i++)
- {
+ // Skip everything up to (and then including) the function block.
+ for (size_t i = 0; i < function_block_idx; i++) {
block_t *b = parser.block_at_index(i);
b->skip = true;
}
@@ -3780,11 +2801,8 @@ static int builtin_return(parser_t &parser, io_streams_t &streams, wchar_t **arg
return status;
}
-/**
- History of commands executed by user
-*/
-static int builtin_history(parser_t &parser, io_streams_t &streams, wchar_t **argv)
-{
+// History of commands executed by user.
+static int builtin_history(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
int argc = builtin_count_args(argv);
bool search_history = false;
@@ -3794,18 +2812,15 @@ static int builtin_history(parser_t &parser, io_streams_t &streams, wchar_t **ar
bool clear_history = false;
bool merge_history = false;
- static const struct woption long_options[] =
- {
- { L"prefix", no_argument, 0, 'p' },
- { L"delete", no_argument, 0, 'd' },
- { L"search", no_argument, 0, 's' },
- { L"contains", no_argument, 0, 'c' },
- { L"save", no_argument, 0, 'v' },
- { L"clear", no_argument, 0, 'l' },
- { L"merge", no_argument, 0, 'm' },
- { L"help", no_argument, 0, 'h' },
- { 0, 0, 0, 0 }
- };
+ static const struct woption long_options[] = {{L"prefix", no_argument, 0, 'p'},
+ {L"delete", no_argument, 0, 'd'},
+ {L"search", no_argument, 0, 's'},
+ {L"contains", no_argument, 0, 'c'},
+ {L"save", no_argument, 0, 'v'},
+ {L"clear", no_argument, 0, 'l'},
+ {L"merge", no_argument, 0, 'm'},
+ {L"help", no_argument, 0, 'h'},
+ {0, 0, 0, 0}};
int opt = 0;
int opt_index = 0;
@@ -3813,53 +2828,60 @@ static int builtin_history(parser_t &parser, io_streams_t &streams, wchar_t **ar
wgetopter_t w;
history_t *history = reader_get_history();
- /* Use the default history if we have none (which happens if invoked non-interactively, e.g. from webconfig.py */
- if (! history)
- history = &history_t::history_with_name(L"fish");
+ // Use the default history if we have none (which happens if invoked non-interactively, e.g.
+ // from webconfig.py.
+ if (!history) history = &history_t::history_with_name(L"fish");
- while ((opt = w.wgetopt_long_only(argc, argv, L"pdscvl", long_options, &opt_index)) != EOF)
- {
- switch (opt)
- {
- case 'p':
+ while ((opt = w.wgetopt_long_only(argc, argv, L"pdscvl", long_options, &opt_index)) != EOF) {
+ switch (opt) {
+ case 'p': {
search_prefix = true;
break;
- case 'd':
+ }
+ case 'd': {
delete_item = true;
break;
- case 's':
+ }
+ case 's': {
search_history = true;
break;
- case 'c':
+ }
+ case 'c': {
break;
- case 'v':
+ }
+ case 'v': {
save_history = true;
break;
- case 'l':
+ }
+ case 'l': {
clear_history = true;
break;
- case 'm':
+ }
+ case 'm': {
merge_history = true;
break;
- case 'h':
+ }
+ case 'h': {
builtin_print_help(parser, streams, argv[0], streams.out);
return STATUS_BUILTIN_OK;
break;
- case '?':
- streams.err.append_format(BUILTIN_ERR_UNKNOWN, argv[0], argv[w.woptind-1]);
+ }
+ case '?': {
+ streams.err.append_format(BUILTIN_ERR_UNKNOWN, argv[0], argv[w.woptind - 1]);
return STATUS_BUILTIN_ERROR;
break;
- default:
- streams.err.append_format(BUILTIN_ERR_UNKNOWN, argv[0], argv[w.woptind-1]);
+ }
+ default: {
+ streams.err.append_format(BUILTIN_ERR_UNKNOWN, argv[0], argv[w.woptind - 1]);
return STATUS_BUILTIN_ERROR;
+ }
}
}
- /* Everything after is an argument */
+ // Everything after is an argument.
const wcstring_list_t args(argv + w.woptind, argv + argc);
- if (argc == 1)
- {
+ if (argc == 1) {
wcstring full_history;
history->get_string_representation(&full_history, wcstring(L"\n"));
streams.out.append(full_history);
@@ -3867,27 +2889,25 @@ static int builtin_history(parser_t &parser, io_streams_t &streams, wchar_t **ar
return STATUS_BUILTIN_OK;
}
- if (merge_history)
- {
+ if (merge_history) {
history->incorporate_external_changes();
return STATUS_BUILTIN_OK;
}
- if (search_history)
- {
+ if (search_history) {
int res = STATUS_BUILTIN_ERROR;
- for (wcstring_list_t::const_iterator iter = args.begin(); iter != args.end(); ++iter)
- {
+ for (wcstring_list_t::const_iterator iter = args.begin(); iter != args.end(); ++iter) {
const wcstring &search_string = *iter;
- if (search_string.empty())
- {
- streams.err.append_format(BUILTIN_ERR_COMBO2, argv[0], L"Use --search with either --contains or --prefix");
+ if (search_string.empty()) {
+ streams.err.append_format(BUILTIN_ERR_COMBO2, argv[0],
+ L"Use --search with either --contains or --prefix");
return res;
}
- history_search_t searcher = history_search_t(*history, search_string, search_prefix?HISTORY_SEARCH_TYPE_PREFIX:HISTORY_SEARCH_TYPE_CONTAINS);
- while (searcher.go_backwards())
- {
+ history_search_t searcher = history_search_t(
+ *history, search_string,
+ search_prefix ? HISTORY_SEARCH_TYPE_PREFIX : HISTORY_SEARCH_TYPE_CONTAINS);
+ while (searcher.go_backwards()) {
streams.out.append(searcher.current_string());
streams.out.append(L"\n");
res = STATUS_BUILTIN_OK;
@@ -3896,10 +2916,8 @@ static int builtin_history(parser_t &parser, io_streams_t &streams, wchar_t **ar
return res;
}
- if (delete_item)
- {
- for (wcstring_list_t::const_iterator iter = args.begin(); iter != args.end(); ++iter)
- {
+ if (delete_item) {
+ for (wcstring_list_t::const_iterator iter = args.begin(); iter != args.end(); ++iter) {
wcstring delete_string = *iter;
if (delete_string[0] == '"' && delete_string[delete_string.length() - 1] == '"')
delete_string = delete_string.substr(1, delete_string.length() - 2);
@@ -3909,14 +2927,12 @@ static int builtin_history(parser_t &parser, io_streams_t &streams, wchar_t **ar
return STATUS_BUILTIN_OK;
}
- if (save_history)
- {
+ if (save_history) {
history->save();
return STATUS_BUILTIN_OK;
}
- if (clear_history)
- {
+ if (clear_history) {
history->clear();
history->save();
return STATUS_BUILTIN_OK;
@@ -3970,190 +2986,156 @@ int builtin_parse(parser_t &parser, io_streams_t &streams, wchar_t **argv)
}
#endif
-int builtin_true(parser_t &parser, io_streams_t &streams, wchar_t **argv)
-{
+int builtin_true(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
return STATUS_BUILTIN_OK;
}
-int builtin_false(parser_t &parser, io_streams_t &streams, wchar_t **argv)
-{
+int builtin_false(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
return STATUS_BUILTIN_ERROR;
}
-/*
- END OF BUILTIN COMMANDS
- Below are functions for handling the builtin commands.
- THESE MUST BE SORTED BY NAME! Completion lookup uses binary search.
-*/
+// END OF BUILTIN COMMANDS
+// Below are functions for handling the builtin commands.
+// THESE MUST BE SORTED BY NAME! Completion lookup uses binary search.
-/**
- Data about all the builtin commands in fish.
- Functions that are bound to builtin_generic are handled directly by the parser.
- NOTE: These must be kept in sorted order!
-*/
-static const builtin_data_t builtin_datas[]=
-{
- { L"[", &builtin_test, N_(L"Test a condition") },
+// Data about all the builtin commands in fish.
+// Functions that are bound to builtin_generic are handled directly by the parser.
+// NOTE: These must be kept in sorted order!
+static const builtin_data_t builtin_datas[] = {
+ {L"[", &builtin_test, N_(L"Test a condition")},
#if 0
// Disabled for the 2.2.0 release: https://github.com/fish-shell/fish-shell/issues/1809.
{ L"__fish_parse", &builtin_parse, N_(L"Try out the new parser") },
#endif
- { L"and", &builtin_generic, N_(L"Execute command if previous command suceeded") },
- { L"begin", &builtin_generic, N_(L"Create a block of code") },
- { L"bg", &builtin_bg, N_(L"Send job to background") },
- { L"bind", &builtin_bind, N_(L"Handle fish key bindings") },
- { L"block", &builtin_block, N_(L"Temporarily block delivery of events") },
- { L"break", &builtin_break_continue, N_(L"Stop the innermost loop") },
- { L"breakpoint", &builtin_breakpoint, N_(L"Temporarily halt execution of a script and launch an interactive debug prompt") },
- { L"builtin", &builtin_builtin, N_(L"Run a builtin command instead of a function") },
- { L"case", &builtin_generic, N_(L"Conditionally execute a block of commands") },
- { L"cd", &builtin_cd, N_(L"Change working directory") },
- { L"command", &builtin_command, N_(L"Run a program instead of a function or builtin") },
- { L"commandline", &builtin_commandline, N_(L"Set or get the commandline") },
- { L"complete", &builtin_complete, N_(L"Edit command specific completions") },
- { L"contains", &builtin_contains, N_(L"Search for a specified string in a list") },
- { L"continue", &builtin_break_continue, N_(L"Skip the rest of the current lap of the innermost loop") },
- { L"count", &builtin_count, N_(L"Count the number of arguments") },
- { L"echo", &builtin_echo, N_(L"Print arguments") },
- { L"else", &builtin_generic, N_(L"Evaluate block if condition is false") },
- { L"emit", &builtin_emit, N_(L"Emit an event") },
- { L"end", &builtin_generic, N_(L"End a block of commands") },
- { L"exec", &builtin_generic, N_(L"Run command in current process") },
- { L"exit", &builtin_exit, N_(L"Exit the shell") },
- { L"false", &builtin_false, N_(L"Return an unsuccessful result") },
- { L"fg", &builtin_fg, N_(L"Send job to foreground") },
- { L"for", &builtin_generic, N_(L"Perform a set of commands multiple times") },
- { L"function", &builtin_generic, N_(L"Define a new function") },
- { L"functions", &builtin_functions, N_(L"List or remove functions") },
- { L"history", &builtin_history, N_(L"History of commands executed by user") },
- { L"if", &builtin_generic, N_(L"Evaluate block if condition is true") },
- { L"jobs", &builtin_jobs, N_(L"Print currently running jobs") },
- { L"not", &builtin_generic, N_(L"Negate exit status of job") },
- { L"or", &builtin_generic, N_(L"Execute command if previous command failed") },
- { L"printf", &builtin_printf, N_(L"Prints formatted text") },
- { L"pwd", &builtin_pwd, N_(L"Print the working directory") },
- { L"random", &builtin_random, N_(L"Generate random number") },
- { L"read", &builtin_read, N_(L"Read a line of input into variables") },
- { L"return", &builtin_return, N_(L"Stop the currently evaluated function") },
- { L"set", &builtin_set, N_(L"Handle environment variables") },
- { L"set_color", &builtin_set_color, N_(L"Set the terminal color") },
- { L"source", &builtin_source, N_(L"Evaluate contents of file") },
- { L"status", &builtin_status, N_(L"Return status information about fish") },
- { L"string", &builtin_string, N_(L"Manipulate strings") },
- { L"switch", &builtin_generic, N_(L"Conditionally execute a block of commands") },
- { L"test", &builtin_test, N_(L"Test a condition") },
- { L"true", &builtin_true, N_(L"Return a successful result") },
- { L"ulimit", &builtin_ulimit, N_(L"Set or get the shells resource usage limits") },
- { L"while", &builtin_generic, N_(L"Perform a command multiple times") }
-};
+ {L"and", &builtin_generic, N_(L"Execute command if previous command suceeded")},
+ {L"begin", &builtin_generic, N_(L"Create a block of code")},
+ {L"bg", &builtin_bg, N_(L"Send job to background")},
+ {L"bind", &builtin_bind, N_(L"Handle fish key bindings")},
+ {L"block", &builtin_block, N_(L"Temporarily block delivery of events")},
+ {L"break", &builtin_break_continue, N_(L"Stop the innermost loop")},
+ {L"breakpoint", &builtin_breakpoint,
+ N_(L"Temporarily halt execution of a script and launch an interactive debug prompt")},
+ {L"builtin", &builtin_builtin, N_(L"Run a builtin command instead of a function")},
+ {L"case", &builtin_generic, N_(L"Conditionally execute a block of commands")},
+ {L"cd", &builtin_cd, N_(L"Change working directory")},
+ {L"command", &builtin_command, N_(L"Run a program instead of a function or builtin")},
+ {L"commandline", &builtin_commandline, N_(L"Set or get the commandline")},
+ {L"complete", &builtin_complete, N_(L"Edit command specific completions")},
+ {L"contains", &builtin_contains, N_(L"Search for a specified string in a list")},
+ {L"continue", &builtin_break_continue,
+ N_(L"Skip the rest of the current lap of the innermost loop")},
+ {L"count", &builtin_count, N_(L"Count the number of arguments")},
+ {L"echo", &builtin_echo, N_(L"Print arguments")},
+ {L"else", &builtin_generic, N_(L"Evaluate block if condition is false")},
+ {L"emit", &builtin_emit, N_(L"Emit an event")},
+ {L"end", &builtin_generic, N_(L"End a block of commands")},
+ {L"exec", &builtin_generic, N_(L"Run command in current process")},
+ {L"exit", &builtin_exit, N_(L"Exit the shell")},
+ {L"false", &builtin_false, N_(L"Return an unsuccessful result")},
+ {L"fg", &builtin_fg, N_(L"Send job to foreground")},
+ {L"for", &builtin_generic, N_(L"Perform a set of commands multiple times")},
+ {L"function", &builtin_generic, N_(L"Define a new function")},
+ {L"functions", &builtin_functions, N_(L"List or remove functions")},
+ {L"history", &builtin_history, N_(L"History of commands executed by user")},
+ {L"if", &builtin_generic, N_(L"Evaluate block if condition is true")},
+ {L"jobs", &builtin_jobs, N_(L"Print currently running jobs")},
+ {L"not", &builtin_generic, N_(L"Negate exit status of job")},
+ {L"or", &builtin_generic, N_(L"Execute command if previous command failed")},
+ {L"printf", &builtin_printf, N_(L"Prints formatted text")},
+ {L"pwd", &builtin_pwd, N_(L"Print the working directory")},
+ {L"random", &builtin_random, N_(L"Generate random number")},
+ {L"read", &builtin_read, N_(L"Read a line of input into variables")},
+ {L"return", &builtin_return, N_(L"Stop the currently evaluated function")},
+ {L"set", &builtin_set, N_(L"Handle environment variables")},
+ {L"set_color", &builtin_set_color, N_(L"Set the terminal color")},
+ {L"source", &builtin_source, N_(L"Evaluate contents of file")},
+ {L"status", &builtin_status, N_(L"Return status information about fish")},
+ {L"string", &builtin_string, N_(L"Manipulate strings")},
+ {L"switch", &builtin_generic, N_(L"Conditionally execute a block of commands")},
+ {L"test", &builtin_test, N_(L"Test a condition")},
+ {L"true", &builtin_true, N_(L"Return a successful result")},
+ {L"ulimit", &builtin_ulimit, N_(L"Set or get the shells resource usage limits")},
+ {L"while", &builtin_generic, N_(L"Perform a command multiple times")}};
#define BUILTIN_COUNT (sizeof builtin_datas / sizeof *builtin_datas)
-static const builtin_data_t *builtin_lookup(const wcstring &name)
-{
+static const builtin_data_t *builtin_lookup(const wcstring &name) {
const builtin_data_t *array_end = builtin_datas + BUILTIN_COUNT;
const builtin_data_t *found = std::lower_bound(builtin_datas, array_end, name);
- if (found != array_end && name == found->name)
- {
+ if (found != array_end && name == found->name) {
return found;
- }
- else
- {
+ } else {
return NULL;
}
}
-void builtin_init()
-{
- for (size_t i=0; i < BUILTIN_COUNT; i++)
- {
+void builtin_init() {
+ for (size_t i = 0; i < BUILTIN_COUNT; i++) {
intern_static(builtin_datas[i].name);
}
}
-void builtin_destroy()
-{
-}
+void builtin_destroy() {}
-int builtin_exists(const wcstring &cmd)
-{
- return !!builtin_lookup(cmd);
-}
+int builtin_exists(const wcstring &cmd) { return !!builtin_lookup(cmd); }
-/**
- Return true if the specified builtin should handle it's own help,
- false otherwise.
-*/
-static int internal_help(const wchar_t *cmd)
-{
+// Return true if the specified builtin should handle it's own help, false otherwise.
+static int internal_help(const wchar_t *cmd) {
CHECK(cmd, 0);
- return contains(cmd, L"for", L"while", L"function",
- L"if", L"end", L"switch", L"case", L"count", L"printf");
+ return contains(cmd, L"for", L"while", L"function", L"if", L"end", L"switch", L"case", L"count",
+ L"printf");
}
+int builtin_run(parser_t &parser, const wchar_t *const *argv, io_streams_t &streams) {
+ int (*cmd)(parser_t & parser, io_streams_t & streams, const wchar_t *const *argv) = NULL;
-int builtin_run(parser_t &parser, const wchar_t * const *argv, io_streams_t &streams)
-{
- int (*cmd)(parser_t &parser, io_streams_t &streams, const wchar_t * const *argv)=NULL;
-
CHECK(argv, STATUS_BUILTIN_ERROR);
CHECK(argv[0], STATUS_BUILTIN_ERROR);
const builtin_data_t *data = builtin_lookup(argv[0]);
- cmd = (int (*)(parser_t &parser, io_streams_t &streams, const wchar_t * const*))(data ? data->func : NULL);
+ cmd = (int (*)(parser_t & parser, io_streams_t & streams, const wchar_t *const *))(
+ data ? data->func : NULL);
- if (argv[1] != 0 && !internal_help(argv[0]))
- {
- if (argv[2] == 0 && (parse_util_argument_is_help(argv[1], 0)))
- {
+ if (argv[1] != 0 && !internal_help(argv[0])) {
+ if (argv[2] == 0 && (parse_util_argument_is_help(argv[1], 0))) {
builtin_print_help(parser, streams, argv[0], streams.out);
return STATUS_BUILTIN_OK;
}
}
- if (data != NULL)
- {
+ if (data != NULL) {
int status;
status = cmd(parser, streams, argv);
return status;
- }
- else
- {
+ } else {
debug(0, UNKNOWN_BUILTIN_ERR_MSG, argv[0]);
}
return STATUS_BUILTIN_ERROR;
}
-
-wcstring_list_t builtin_get_names(void)
-{
+wcstring_list_t builtin_get_names(void) {
wcstring_list_t result;
result.reserve(BUILTIN_COUNT);
- for (size_t i=0; i < BUILTIN_COUNT; i++)
- {
+ for (size_t i = 0; i < BUILTIN_COUNT; i++) {
result.push_back(builtin_datas[i].name);
}
return result;
}
-void builtin_get_names(std::vector<completion_t> *list)
-{
+void builtin_get_names(std::vector<completion_t> *list) {
assert(list != NULL);
list->reserve(list->size() + BUILTIN_COUNT);
- for (size_t i=0; i < BUILTIN_COUNT; i++)
- {
+ for (size_t i = 0; i < BUILTIN_COUNT; i++) {
append_completion(list, builtin_datas[i].name);
}
}
-wcstring builtin_get_desc(const wcstring &name)
-{
+wcstring builtin_get_desc(const wcstring &name) {
wcstring result;
const builtin_data_t *builtin = builtin_lookup(name);
- if (builtin)
- {
+ if (builtin) {
result = _(builtin->desc);
}
return result;
diff --git a/src/builtin.h b/src/builtin.h
index f747eb9b..cea7318a 100644
--- a/src/builtin.h
+++ b/src/builtin.h
@@ -1,144 +1,100 @@
-/** \file builtin.h
- Prototypes for functions for executing builtin functions.
-*/
-
+// Prototypes for functions for executing builtin functions.
#ifndef FISH_BUILTIN_H
#define FISH_BUILTIN_H
-#include <stddef.h> // for size_t
-#include <vector> // for vector
+#include <stddef.h> // for size_t
+#include <vector> // for vector
-#include "io.h"
#include "common.h"
+#include "io.h"
class completion_t;
class parser_t;
-enum
-{
- COMMAND_NOT_BUILTIN,
- BUILTIN_REGULAR,
- BUILTIN_FUNCTION
-}
-;
-
-/**
- Error message on missing argument
-*/
-#define BUILTIN_ERR_MISSING _( L"%ls: Expected argument for option %ls\n" )
-
-/**
- Error message on invalid combination of options
-*/
-#define BUILTIN_ERR_COMBO _( L"%ls: Invalid combination of options\n" )
-
-/**
- Error message on invalid combination of options
-*/
-#define BUILTIN_ERR_COMBO2 _( L"%ls: Invalid combination of options,\n%ls\n" )
-
-/**
- Error message on multiple scope levels for variables
-*/
-#define BUILTIN_ERR_GLOCAL _( L"%ls: Variable scope can only be one of universal, global and local\n" )
-
-/**
- Error message for specifying both export and unexport to set/read
-*/
-#define BUILTIN_ERR_EXPUNEXP _( L"%ls: Variable can't be both exported and unexported\n" )
-
-/**
- Error message for unknown switch
-*/
-#define BUILTIN_ERR_UNKNOWN _( L"%ls: Unknown option '%ls'\n" )
-
-/**
- Error message for invalid character in variable name
-*/
-#define BUILTIN_ERR_VARCHAR _( L"%ls: Invalid character '%lc' in variable name. Only alphanumerical characters and underscores are valid in a variable name.\n" )
-
-/**
- Error message for invalid (empty) variable name
-*/
-#define BUILTIN_ERR_VARNAME_ZERO _( L"%ls: Variable name can not be the empty string\n" )
-
-/**
- Error message when too many arguments are supplied to a builtin
-*/
-#define BUILTIN_ERR_TOO_MANY_ARGUMENTS _( L"%ls: Too many arguments\n" )
-
-#define BUILTIN_ERR_NOT_NUMBER _( L"%ls: Argument '%ls' is not a number\n" )
-
-/**
- Initialize builtin data.
-*/
-void builtin_init();
+enum { COMMAND_NOT_BUILTIN, BUILTIN_REGULAR, BUILTIN_FUNCTION };
-/**
- Destroy builtin data.
-*/
-void builtin_destroy();
+// Error message on missing argument.
+#define BUILTIN_ERR_MISSING _(L"%ls: Expected argument for option %ls\n")
-/**
- Is there a builtin command with the given name?
-*/
-int builtin_exists(const wcstring &cmd);
+// Error message on invalid combination of options.
+#define BUILTIN_ERR_COMBO _(L"%ls: Invalid combination of options\n")
+
+// Error message on invalid combination of options.
+#define BUILTIN_ERR_COMBO2 _(L"%ls: Invalid combination of options,\n%ls\n")
+
+// Error message on multiple scope levels for variables.
+#define BUILTIN_ERR_GLOCAL \
+ _(L"%ls: Variable scope can only be one of universal, global and local\n")
+
+// Error message for specifying both export and unexport to set/read.
+#define BUILTIN_ERR_EXPUNEXP _(L"%ls: Variable can't be both exported and unexported\n")
+
+// Error message for unknown switch.
+#define BUILTIN_ERR_UNKNOWN _(L"%ls: Unknown option '%ls'\n")
+
+// Error message for invalid character in variable name.
+#define BUILTIN_ERR_VARCHAR \
+ _(L"%ls: Invalid character '%lc' in variable name. Only alphanumerical characters and " \
+ L"underscores are valid in a variable name.\n")
-/**
- Execute a builtin command
+// Error message for invalid (empty) variable name.
+#define BUILTIN_ERR_VARNAME_ZERO _(L"%ls: Variable name can not be the empty string\n")
- \param parser The parser being used
- \param argv Array containing the command and parameters
- of the builtin. The list is terminated by a
- null pointer. This syntax resembles the syntax
- for exec.
- \param io the io redirections to perform on this builtin.
+// Error message when too many arguments are supplied to a builtin.
+#define BUILTIN_ERR_TOO_MANY_ARGUMENTS _(L"%ls: Too many arguments\n")
- \return the exit status of the builtin command
-*/
-int builtin_run(parser_t &parser, const wchar_t * const *argv, io_streams_t &streams);
+#define BUILTIN_ERR_NOT_NUMBER _(L"%ls: Argument '%ls' is not a number\n")
-/** Returns a list of all builtin names */
+// Initialize builtin data.
+void builtin_init();
+
+// Destroy builtin data.
+void builtin_destroy();
+
+// Is there a builtin command with the given name?
+int builtin_exists(const wcstring &cmd);
+
+// Execute a builtin command
+//
+// \param parser The parser being used
+// \param argv Array containing the command and parameters of the builtin. The list is terminated
+// by a null pointer. This syntax resembles the syntax for exec.
+// \param io the io redirections to perform on this builtin.
+//
+// \return the exit status of the builtin command
+int builtin_run(parser_t &parser, const wchar_t *const *argv, io_streams_t &streams);
+
+// Returns a list of all builtin names.
wcstring_list_t builtin_get_names();
-/** Insert all builtin names into list. */
+// Insert all builtin names into list.
void builtin_get_names(std::vector<completion_t> *list);
-/**
- Return a one-line description of the specified builtin.
-*/
+// Return a one-line description of the specified builtin.
wcstring builtin_get_desc(const wcstring &b);
-
-
-/** Support for setting and removing transient command lines.
- This is used by 'complete -C' in order to make
- the commandline builtin operate on the string to complete instead
- of operating on whatever is to be completed. It's also used by
- completion wrappers, to allow a command to appear as the command
- being wrapped for the purposes of completion.
-
- Instantiating an instance of builtin_commandline_scoped_transient_t
- pushes the command as the new transient commandline. The destructor removes it.
- It will assert if construction/destruction does not happen in a stack-like (LIFO) order.
-*/
-class builtin_commandline_scoped_transient_t
-{
+// Support for setting and removing transient command lines. This is used by 'complete -C' in order
+// to make the commandline builtin operate on the string to complete instead of operating on
+// whatever is to be completed. It's also used by completion wrappers, to allow a command to appear
+// as the command being wrapped for the purposes of completion.
+//
+// Instantiating an instance of builtin_commandline_scoped_transient_t pushes the command as the new
+// transient commandline. The destructor removes it. It will assert if construction/destruction does
+// not happen in a stack-like (LIFO) order.
+class builtin_commandline_scoped_transient_t {
size_t token;
- public:
+
+ public:
explicit builtin_commandline_scoped_transient_t(const wcstring &cmd);
~builtin_commandline_scoped_transient_t();
};
-
-/**
- Run the __fish_print_help function to obtain the help information
- for the specified command.
-*/
+// Run the __fish_print_help function to obtain the help information for the specified command.
wcstring builtin_help_get(parser_t &parser, const wchar_t *cmd);
-/** Defines a function, like builtin_function. Returns 0 on success. args should NOT contain 'function' as the first argument. */
-int define_function(parser_t &parser, io_streams_t &streams, const wcstring_list_t &c_args, const wcstring &contents, int definition_line_offset, wcstring *out_err);
-
+// Defines a function, like builtin_function. Returns 0 on success. args should NOT contain
+// 'function' as the first argument.
+int define_function(parser_t &parser, io_streams_t &streams, const wcstring_list_t &c_args,
+ const wcstring &contents, int definition_line_offset, wcstring *out_err);
#endif
diff --git a/src/fish_indent.cpp b/src/fish_indent.cpp
index 0e31f28e..a469ccf7 100644
--- a/src/fish_indent.cpp
+++ b/src/fish_indent.cpp
@@ -102,10 +102,10 @@ static void dump_node(indent_t node_indent, const parse_node_t &node, const wcst
nextc_str[1] = L'c';
nextc_str[2] = nextc + '@';
}
- fwprintf(stderr, L"{off %4d, len %4d, indent %2u, %ls} [%ls|%ls|%ls]\n",
+ fwprintf(stderr, L"{off %4d, len %4d, indent %2u, kw %ls, %ls} [%ls|%ls|%ls]\n",
node.source_start, node.source_length, node_indent,
- parser_token_types[node.type].c_str(), prevc_str, source.substr(node.source_start,
- node.source_length).c_str(), nextc_str);
+ keyword_description(node.keyword), token_type_description(node.type),
+ prevc_str, source.substr(node.source_start, node.source_length).c_str(), nextc_str);
}
static void prettify_node_recursive(const wcstring &source, const parse_node_tree_t &tree,
@@ -153,7 +153,13 @@ static void prettify_node_recursive(const wcstring &source, const parse_node_tre
else if ((node_type >= FIRST_PARSE_TOKEN_TYPE && node_type <= LAST_PARSE_TOKEN_TYPE) ||
node_type == parse_special_type_parse_error)
{
- if (node.has_source())
+ if (node.keyword != parse_keyword_none)
+ {
+ append_whitespace(node_indent, do_indent, *has_new_line, out_result);
+ out_result->append(keyword_description(node.keyword));
+ *has_new_line = false;
+ }
+ else if (node.has_source())
{
// Some type representing a particular token.
if (prev_node_type != parse_token_type_redirection)
diff --git a/src/fish_tests.cpp b/src/fish_tests.cpp
index 6250513c..ef1956cc 100644
--- a/src/fish_tests.cpp
+++ b/src/fish_tests.cpp
@@ -3628,11 +3628,11 @@ static void test_new_parser_ll2(void)
const parse_node_tree_t::parse_node_list_t node_list = tree.find_nodes(tree.at(0), tests2[i].type);
if (node_list.size() == 0)
{
- err(L"Failed to find node of type '%ls'", token_type_description(tests2[i].type).c_str());
+ err(L"Failed to find node of type '%ls'", token_type_description(tests2[i].type));
}
else if (node_list.size() > 1)
{
- err(L"Found too many nodes of type '%ls'", token_type_description(tests2[i].type).c_str());
+ err(L"Found too many nodes of type '%ls'", token_type_description(tests2[i].type));
}
}
}
diff --git a/src/history.cpp b/src/history.cpp
index ff99db9a..af5d062e 100644
--- a/src/history.cpp
+++ b/src/history.cpp
@@ -1,159 +1,133 @@
-/** \file history.c
- History functions, part of the user interface.
-*/
-#include "config.h"
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <wchar.h>
+// History functions, part of the user interface.
+#include "history.h"
+#include <assert.h>
+#include <ctype.h>
#include <errno.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <sys/mman.h>
#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
#include <time.h>
-#include <assert.h>
-#include <ctype.h>
+#include <unistd.h>
+#include <wchar.h>
#include <wctype.h>
+#include <algorithm>
#include <iterator>
-
-#include "fallback.h" // IWYU pragma: keep
-#include "sanity.h"
-#include "reader.h"
-#include "parse_tree.h"
-#include "wutil.h"
-#include "history.h"
+#include <map>
#include "common.h"
-#include "path.h"
-#include "signal.h"
-#include "iothread.h"
+#include "config.h"
#include "env.h"
+#include "fallback.h" // IWYU pragma: keep
+#include "iothread.h"
#include "lru.h"
#include "parse_constants.h"
-#include <map>
-#include <algorithm>
-
-/*
-
-Our history format is intended to be valid YAML. Here it is:
-
- - cmd: ssh blah blah blah
- when: 2348237
- paths:
- - /path/to/something
- - /path/to/something_else
-
- Newlines are replaced by \n. Backslashes are replaced by \\.
-*/
+#include "parse_tree.h"
+#include "path.h"
+#include "reader.h"
+#include "sanity.h"
+#include "signal.h"
+#include "wutil.h"
-/** When we rewrite the history, the number of items we keep */
+// Our history format is intended to be valid YAML. Here it is:
+//
+// - cmd: ssh blah blah blah
+// when: 2348237
+// paths:
+// - /path/to/something
+// - /path/to/something_else
+//
+// Newlines are replaced by \n. Backslashes are replaced by \\.
+
+// When we rewrite the history, the number of items we keep.
#define HISTORY_SAVE_MAX (1024 * 256)
-/** Whether we print timing information */
+// Whether we print timing information.
#define LOG_TIMES 0
-/** Default buffer size for flushing to the history file */
-#define HISTORY_OUTPUT_BUFFER_SIZE (4096 * 4)
+// Default buffer size for flushing to the history file.
+#define HISTORY_OUTPUT_BUFFER_SIZE (16 * 1024)
-namespace
-{
+namespace {
-/* Helper class for certain output. This is basically a string that allows us to ensure we only flush at record boundaries, and avoids the copying of ostringstream. Have you ever tried to implement your own streambuf? Total insanity. */
-class history_output_buffer_t
-{
- /* A null-terminated C string */
+// Helper class for certain output. This is basically a string that allows us to ensure we only
+// flush at record boundaries, and avoids the copying of ostringstream. Have you ever tried to
+// implement your own streambuf? Total insanity.
+class history_output_buffer_t {
+ // A null-terminated C string.
std::vector<char> buffer;
- /* Offset is the offset of the null terminator */
+ // Offset is the offset of the null terminator.
size_t offset;
- static size_t safe_strlen(const char *s)
- {
- return s ? strlen(s) : 0;
- }
-
-public:
+ static size_t safe_strlen(const char *s) { return s ? strlen(s) : 0; }
- /* Add a bit more to HISTORY_OUTPUT_BUFFER_SIZE because we flush once we've exceeded that size */
- history_output_buffer_t() : buffer(HISTORY_OUTPUT_BUFFER_SIZE + 128, '\0'), offset(0)
- {
- }
+ public:
+ // Add a bit more to HISTORY_OUTPUT_BUFFER_SIZE because we flush once we've exceeded that size.
+ history_output_buffer_t() : buffer(HISTORY_OUTPUT_BUFFER_SIZE + 128, '\0'), offset(0) {}
- /* Append one or more strings */
- void append(const char *s1, const char *s2 = NULL, const char *s3 = NULL)
- {
+ // Append one or more strings.
+ void append(const char *s1, const char *s2 = NULL, const char *s3 = NULL) {
const char *ptrs[4] = {s1, s2, s3, NULL};
const size_t lengths[4] = {safe_strlen(s1), safe_strlen(s2), safe_strlen(s3), 0};
- /* Determine the additional size we'll need */
+ // Determine the additional size we'll need.
size_t additional_length = 0;
- for (size_t i=0; i < sizeof lengths / sizeof *lengths; i++)
- {
+ for (size_t i = 0; i < sizeof lengths / sizeof *lengths; i++) {
additional_length += lengths[i];
}
- /* Allocate that much, plus a null terminator */
+ // Allocate that much, plus a null terminator.
size_t required_size = offset + additional_length + 1;
- if (required_size > buffer.size())
- {
+ if (required_size > buffer.size()) {
buffer.resize(required_size, '\0');
}
- /* Copy */
- for (size_t i=0; ptrs[i] != NULL; i++)
- {
+ // Copy.
+ for (size_t i = 0; ptrs[i] != NULL; i++) {
memmove(&buffer.at(offset), ptrs[i], lengths[i]);
offset += lengths[i];
}
- /* Null terminator was appended by virtue of the resize() above (or in a previous invocation). */
+ // Null terminator was appended by virtue of the resize() above (or in a previous
+ // invocation).
assert(buffer.at(buffer.size() - 1) == '\0');
}
- /* Output to a given fd, resetting our buffer. Returns true on success, false on error */
- bool flush_to_fd(int fd)
- {
+ // Output to a given fd, resetting our buffer. Returns true on success, false on error.
+ bool flush_to_fd(int fd) {
bool result = write_loop(fd, &buffer.at(0), offset) >= 0;
offset = 0;
return result;
}
- /* Return how much data we've accumulated */
- size_t output_size() const
- {
- return offset;
- }
+ // Return how much data we've accumulated.
+ size_t output_size() const { return offset; }
};
-class time_profiler_t
-{
+class time_profiler_t {
const char *what;
double start;
-public:
- explicit time_profiler_t(const char *w)
- {
- if (LOG_TIMES)
- {
+ public:
+ explicit time_profiler_t(const char *w) {
+ if (LOG_TIMES) {
what = w;
start = timef();
}
}
- ~time_profiler_t()
- {
- if (LOG_TIMES)
- {
+ ~time_profiler_t() {
+ if (LOG_TIMES) {
double end = timef();
fprintf(stderr, "(LOG_TIMES %s: %02f msec)\n", what, (end - start) * 1000);
}
}
};
-/* Lock a file via fcntl; returns true on success, false on failure. */
-static bool history_file_lock(int fd, short type)
-{
+// Lock a file via fcntl; returns true on success, false on failure.
+static bool history_file_lock(int fd, short type) {
assert(type == F_RDLCK || type == F_WRLCK);
struct flock flk = {};
flk.l_type = type;
@@ -162,101 +136,84 @@ static bool history_file_lock(int fd, short type)
return ret != -1;
}
-/* Our LRU cache is used for restricting the amount of history we have, and limiting how long we order it. */
-class history_lru_node_t : public lru_node_t
-{
-public:
+// Our LRU cache is used for restricting the amount of history we have, and limiting how long we
+// order it.
+class history_lru_node_t : public lru_node_t {
+ public:
time_t timestamp;
path_list_t required_paths;
- explicit history_lru_node_t(const history_item_t &item) :
- lru_node_t(item.str()),
- timestamp(item.timestamp()),
- required_paths(item.get_required_paths())
- {}
+ explicit history_lru_node_t(const history_item_t &item)
+ : lru_node_t(item.str()),
+ timestamp(item.timestamp()),
+ required_paths(item.get_required_paths()) {}
};
-class history_lru_cache_t : public lru_cache_t<history_lru_node_t>
-{
-protected:
+class history_lru_cache_t : public lru_cache_t<history_lru_node_t> {
+ protected:
+ // Override to delete evicted nodes.
+ virtual void node_was_evicted(history_lru_node_t *node) { delete node; }
- /* Override to delete evicted nodes */
- virtual void node_was_evicted(history_lru_node_t *node)
- {
- delete node;
- }
+ public:
+ explicit history_lru_cache_t(size_t max) : lru_cache_t<history_lru_node_t>(max) {}
-public:
- explicit history_lru_cache_t(size_t max) : lru_cache_t<history_lru_node_t>(max) { }
+ // Function to add a history item.
+ void add_item(const history_item_t &item) {
+ // Skip empty items.
+ if (item.empty()) return;
- /* Function to add a history item */
- void add_item(const history_item_t &item)
- {
- /* Skip empty items */
- if (item.empty())
- return;
-
- /* See if it's in the cache. If it is, update the timestamp. If not, we create a new node and add it. Note that calling get_node promotes the node to the front. */
+ // See if it's in the cache. If it is, update the timestamp. If not, we create a new node
+ // and add it. Note that calling get_node promotes the node to the front.
history_lru_node_t *node = this->get_node(item.str());
- if (node != NULL)
- {
+ if (node != NULL) {
node->timestamp = std::max(node->timestamp, item.timestamp());
- /* What to do about paths here? Let's just ignore them */
- }
- else
- {
+ // What to do about paths here? Let's just ignore them.
+ } else {
node = new history_lru_node_t(item);
this->add_node(node);
}
}
};
-class history_collection_t
-{
+class history_collection_t {
pthread_mutex_t m_lock;
std::map<wcstring, history_t *> m_histories;
-public:
- history_collection_t()
- {
- VOMIT_ON_FAILURE_NO_ERRNO(pthread_mutex_init(&m_lock, NULL));
- }
- ~history_collection_t()
- {
- for (std::map<wcstring, history_t*>::const_iterator i = m_histories.begin(); i != m_histories.end(); ++i)
- {
+ public:
+ history_collection_t() { VOMIT_ON_FAILURE_NO_ERRNO(pthread_mutex_init(&m_lock, NULL)); }
+ ~history_collection_t() {
+ for (std::map<wcstring, history_t *>::const_iterator i = m_histories.begin();
+ i != m_histories.end(); ++i) {
delete i->second;
}
pthread_mutex_destroy(&m_lock);
}
- history_t& alloc(const wcstring &name);
+ history_t &alloc(const wcstring &name);
void save();
};
-} //anonymous namespace
+} // anonymous namespace
static history_collection_t histories;
static wcstring history_filename(const wcstring &name, const wcstring &suffix);
-/** Replaces newlines with a literal backslash followed by an n, and replaces backslashes with two backslashes. */
+// Replaces newlines with a literal backslash followed by an n, and replaces backslashes with two
+// backslashes.
static void escape_yaml(std::string *str);
-/** Undoes escape_yaml */
+// Inverse of escape_yaml.
static void unescape_yaml(std::string *str);
-/* We can merge two items if they are the same command. We use the more recent timestamp, more recent identifier, and the longer list of required paths. */
-bool history_item_t::merge(const history_item_t &item)
-{
+// We can merge two items if they are the same command. We use the more recent timestamp, more
+// recent identifier, and the longer list of required paths.
+bool history_item_t::merge(const history_item_t &item) {
bool result = false;
- if (this->contents == item.contents)
- {
+ if (this->contents == item.contents) {
this->creation_timestamp = std::max(this->creation_timestamp, item.creation_timestamp);
- if (this->required_paths.size() < item.required_paths.size())
- {
+ if (this->required_paths.size() < item.required_paths.size()) {
this->required_paths = item.required_paths;
}
- if (this->identifier < item.identifier)
- {
+ if (this->identifier < item.identifier) {
this->identifier = item.identifier;
}
result = true;
@@ -264,50 +221,48 @@ bool history_item_t::merge(const history_item_t &item)
return result;
}
-history_item_t::history_item_t(const wcstring &str) : contents(str), creation_timestamp(time(NULL)), identifier(0)
-{
-}
+history_item_t::history_item_t(const wcstring &str)
+ : contents(str), creation_timestamp(time(NULL)), identifier(0) {}
-history_item_t::history_item_t(const wcstring &str, time_t when, history_identifier_t ident) : contents(str), creation_timestamp(when), identifier(ident)
-{
-}
+history_item_t::history_item_t(const wcstring &str, time_t when, history_identifier_t ident)
+ : contents(str), creation_timestamp(when), identifier(ident) {}
-bool history_item_t::matches_search(const wcstring &term, enum history_search_type_t type) const
-{
- switch (type)
- {
-
- case HISTORY_SEARCH_TYPE_CONTAINS:
- /* We consider equal strings to NOT match a contains search (so that you don't have to see history equal to what you typed). The length check ensures that. */
+bool history_item_t::matches_search(const wcstring &term, enum history_search_type_t type) const {
+ switch (type) {
+ case HISTORY_SEARCH_TYPE_CONTAINS: {
+ // We consider equal strings to NOT match a contains search (so that you don't have to
+ // see history equal to what you typed). The length check ensures that.
return contents.size() > term.size() && contents.find(term) != wcstring::npos;
-
- case HISTORY_SEARCH_TYPE_PREFIX:
- /* We consider equal strings to match a prefix search, so that autosuggest will allow suggesting what you've typed */
+ }
+ case HISTORY_SEARCH_TYPE_PREFIX: {
+ // We consider equal strings to match a prefix search, so that autosuggest will allow
+ // suggesting what you've typed.
return string_prefixes_string(term, contents);
-
- default:
+ }
+ default: {
sanity_lose();
return false;
+ }
}
}
-/* Append our YAML history format to the provided vector at the given offset, updating the offset */
-static void append_yaml_to_buffer(const wcstring &wcmd, time_t timestamp, const path_list_t &required_paths, history_output_buffer_t *buffer)
-{
+// Append our YAML history format to the provided vector at the given offset, updating the offset.
+static void append_yaml_to_buffer(const wcstring &wcmd, time_t timestamp,
+ const path_list_t &required_paths,
+ history_output_buffer_t *buffer) {
std::string cmd = wcs2string(wcmd);
escape_yaml(&cmd);
buffer->append("- cmd: ", cmd.c_str(), "\n");
char timestamp_str[96];
- snprintf(timestamp_str, sizeof timestamp_str, "%ld", (long) timestamp);
+ snprintf(timestamp_str, sizeof timestamp_str, "%ld", (long)timestamp);
buffer->append(" when: ", timestamp_str, "\n");
- if (! required_paths.empty())
- {
+ if (!required_paths.empty()) {
buffer->append(" paths:\n");
- for (path_list_t::const_iterator iter = required_paths.begin(); iter != required_paths.end(); ++iter)
- {
+ for (path_list_t::const_iterator iter = required_paths.begin();
+ iter != required_paths.end(); ++iter) {
std::string path = wcs2string(*iter);
escape_yaml(&path);
buffer->append(" - ", path.c_str(), "\n");
@@ -316,157 +271,143 @@ static void append_yaml_to_buffer(const wcstring &wcmd, time_t timestamp, const
}
// Parse a timestamp line that looks like this: spaces, "when:", spaces, timestamp, newline
-// The string is NOT null terminated; however we do know it contains a newline, so stop when we reach it
-static bool parse_timestamp(const char *str, time_t *out_when)
-{
+// The string is NOT null terminated; however we do know it contains a newline, so stop when we
+// reach it
+static bool parse_timestamp(const char *str, time_t *out_when) {
const char *cursor = str;
- /* Advance past spaces */
- while (*cursor == ' ')
- cursor++;
+ // Advance past spaces.
+ while (*cursor == ' ') cursor++;
- /* Look for "when:" */
+ // Look for "when:".
size_t when_len = 5;
- if (strncmp(cursor, "when:", when_len) != 0)
- return false;
+ if (strncmp(cursor, "when:", when_len) != 0) return false;
cursor += when_len;
- /* Advance past spaces */
- while (*cursor == ' ')
- cursor++;
+ // Advance past spaces.
+ while (*cursor == ' ') cursor++;
- /* Try to parse a timestamp. */
+ // Try to parse a timestamp.
long timestamp = 0;
- if (isdigit(*cursor) && (timestamp = strtol(cursor, NULL, 0)) > 0)
- {
+ if (isdigit(*cursor) && (timestamp = strtol(cursor, NULL, 0)) > 0) {
*out_when = (time_t)timestamp;
return true;
}
return false;
}
-// Returns a pointer to the start of the next line, or NULL
-// The next line must itself end with a newline
-// Note that the string is not null terminated
-static const char *next_line(const char *start, size_t length)
-{
- /* Handle the hopeless case */
- if (length < 1)
- return NULL;
+// Returns a pointer to the start of the next line, or NULL. The next line must itself end with a
+// newline. Note that the string is not null terminated.
+static const char *next_line(const char *start, size_t length) {
+ // Handle the hopeless case.
+ if (length < 1) return NULL;
- /* Get a pointer to the end, that we must not pass */
- const char * const end = start + length;
+ // Get a pointer to the end, that we must not pass.
+ const char *const end = start + length;
- /* Skip past the next newline */
+ // Skip past the next newline.
const char *nextline = (const char *)memchr(start, '\n', length);
- if (! nextline || nextline >= end)
- {
+ if (!nextline || nextline >= end) {
return NULL;
}
- /* Skip past the newline character itself */
- if (++nextline >= end)
- {
+ // Skip past the newline character itself.
+ if (++nextline >= end) {
return NULL;
}
- /* Make sure this new line is itself "newline terminated". If it's not, return NULL; */
+ // Make sure this new line is itself "newline terminated". If it's not, return NULL.
const char *next_newline = (const char *)memchr(nextline, '\n', end - nextline);
- if (! next_newline)
- {
+ if (!next_newline) {
return NULL;
}
- /* Done */
return nextline;
}
-// Support for iteratively locating the offsets of history items
+// Support for iteratively locating the offsets of history items.
// Pass the address and length of a mapped region.
-// Pass a pointer to a cursor size_t, initially 0
-// If custoff_timestamp is nonzero, skip items created at or after that timestamp
-// Returns (size_t)(-1) when done
-static size_t offset_of_next_item_fish_2_0(const char *begin, size_t mmap_length, size_t *inout_cursor, time_t cutoff_timestamp)
-{
+// Pass a pointer to a cursor size_t, initially 0.
+// If custoff_timestamp is nonzero, skip items created at or after that timestamp.
+// Returns (size_t)(-1) when done.
+static size_t offset_of_next_item_fish_2_0(const char *begin, size_t mmap_length,
+ size_t *inout_cursor, time_t cutoff_timestamp) {
size_t cursor = *inout_cursor;
size_t result = (size_t)(-1);
- while (cursor < mmap_length)
- {
+ while (cursor < mmap_length) {
const char *line_start = begin + cursor;
- /* Advance the cursor to the next line */
+ // Advance the cursor to the next line.
const char *newline = (const char *)memchr(line_start, '\n', mmap_length - cursor);
- if (newline == NULL)
- break;
+ if (newline == NULL) break;
- /* Advance the cursor past this line. +1 is for the newline */
+ // Advance the cursor past this line. +1 is for the newline.
cursor = newline - begin + 1;
- /* Skip lines with a leading space, since these are in the interior of one of our items */
- if (line_start[0] == ' ')
- continue;
+ // Skip lines with a leading space, since these are in the interior of one of our items.
+ if (line_start[0] == ' ') continue;
- /* Skip very short lines to make one of the checks below easier */
- if (newline - line_start < 3)
- continue;
+ // Skip very short lines to make one of the checks below easier.
+ if (newline - line_start < 3) continue;
- /* Try to be a little YAML compatible. Skip lines with leading %, ---, or ... */
- if (! memcmp(line_start, "%", 1) ||
- ! memcmp(line_start, "---", 3) ||
- ! memcmp(line_start, "...", 3))
+ // Try to be a little YAML compatible. Skip lines with leading %, ---, or ...
+ if (!memcmp(line_start, "%", 1) || !memcmp(line_start, "---", 3) ||
+ !memcmp(line_start, "...", 3))
continue;
-
- /* Hackish: fish 1.x rewriting a fish 2.0 history file can produce lines with lots of leading "- cmd: - cmd: - cmd:". Trim all but one leading "- cmd:". */
+ // Hackish: fish 1.x rewriting a fish 2.0 history file can produce lines with lots of
+ // leading "- cmd: - cmd: - cmd:". Trim all but one leading "- cmd:".
const char *double_cmd = "- cmd: - cmd: ";
const size_t double_cmd_len = strlen(double_cmd);
- while (newline - line_start > double_cmd_len && ! memcmp(line_start, double_cmd, double_cmd_len))
- {
- /* Skip over just one of the - cmd. In the end there will be just one left. */
+ while (newline - line_start > double_cmd_len &&
+ !memcmp(line_start, double_cmd, double_cmd_len)) {
+ // Skip over just one of the - cmd. In the end there will be just one left.
line_start += strlen("- cmd: ");
}
- /* Hackish: fish 1.x rewriting a fish 2.0 history file can produce commands like "when: 123456". Ignore those. */
+ // Hackish: fish 1.x rewriting a fish 2.0 history file can produce commands like "when:
+ // 123456". Ignore those.
const char *cmd_when = "- cmd: when:";
const size_t cmd_when_len = strlen(cmd_when);
- if (newline - line_start >= cmd_when_len && ! memcmp(line_start, cmd_when, cmd_when_len))
+ if (newline - line_start >= cmd_when_len && !memcmp(line_start, cmd_when, cmd_when_len))
continue;
-
- /* At this point, we know line_start is at the beginning of an item. But maybe we want to skip this item because of timestamps. A 0 cutoff means we don't care; if we do care, then try parsing out a timestamp. */
- if (cutoff_timestamp != 0)
- {
- /* Hackish fast way to skip items created after our timestamp. This is the mechanism by which we avoid "seeing" commands from other sessions that started after we started. We try hard to ensure that our items are sorted by their timestamps, so in theory we could just break, but I don't think that works well if (for example) the clock changes. So we'll read all subsequent items.
- */
- const char * const end = begin + mmap_length;
-
- /* Walk over lines that we think are interior. These lines are not null terminated, but are guaranteed to contain a newline. */
+ // At this point, we know line_start is at the beginning of an item. But maybe we want to
+ // skip this item because of timestamps. A 0 cutoff means we don't care; if we do care, then
+ // try parsing out a timestamp.
+ if (cutoff_timestamp != 0) {
+ // Hackish fast way to skip items created after our timestamp. This is the mechanism by
+ // which we avoid "seeing" commands from other sessions that started after we started.
+ // We try hard to ensure that our items are sorted by their timestamps, so in theory we
+ // could just break, but I don't think that works well if (for example) the clock
+ // changes. So we'll read all subsequent items.
+ const char *const end = begin + mmap_length;
+
+ // Walk over lines that we think are interior. These lines are not null terminated, but
+ // are guaranteed to contain a newline.
bool has_timestamp = false;
time_t timestamp = 0;
const char *interior_line;
for (interior_line = next_line(line_start, end - line_start);
- interior_line != NULL && ! has_timestamp;
- interior_line = next_line(interior_line, end - interior_line))
- {
-
- /* If the first character is not a space, it's not an interior line, so we're done */
- if (interior_line[0] != ' ')
- break;
+ interior_line != NULL && !has_timestamp;
+ interior_line = next_line(interior_line, end - interior_line)) {
+ // If the first character is not a space, it's not an interior line, so we're done.
+ if (interior_line[0] != ' ') break;
- /* Hackish optimization: since we just stepped over some interior line, update the cursor so we don't have to look at these lines next time */
+ // Hackish optimization: since we just stepped over some interior line, update the
+ // cursor so we don't have to look at these lines next time.
cursor = interior_line - begin;
- /* Try parsing a timestamp from this line. If we succeed, the loop will break. */
+ // Try parsing a timestamp from this line. If we succeed, the loop will break.
has_timestamp = parse_timestamp(interior_line, &timestamp);
}
- /* Skip this item if the timestamp is past our cutoff. */
- if (has_timestamp && timestamp > cutoff_timestamp)
- {
+ // Skip this item if the timestamp is past our cutoff.
+ if (has_timestamp && timestamp > cutoff_timestamp) {
continue;
}
}
- /* We made it through the gauntlet. */
+ // We made it through the gauntlet.
result = line_start - begin;
break;
}
@@ -474,13 +415,11 @@ static size_t offset_of_next_item_fish_2_0(const char *begin, size_t mmap_length
return result;
}
-
-// Same as offset_of_next_item_fish_2_0, but for fish 1.x (pre fishfish)
+// Same as offset_of_next_item_fish_2_0, but for fish 1.x (pre fishfish).
// Adapted from history_populate_from_mmap in history.c
-static size_t offset_of_next_item_fish_1_x(const char *begin, size_t mmap_length, size_t *inout_cursor, time_t cutoff_timestamp)
-{
- if (mmap_length == 0 || *inout_cursor >= mmap_length)
- return (size_t)(-1);
+static size_t offset_of_next_item_fish_1_x(const char *begin, size_t mmap_length,
+ size_t *inout_cursor, time_t cutoff_timestamp) {
+ if (mmap_length == 0 || *inout_cursor >= mmap_length) return (size_t)(-1);
const char *end = begin + mmap_length;
const char *pos;
@@ -490,32 +429,23 @@ static size_t offset_of_next_item_fish_1_x(const char *begin, size_t mmap_length
bool all_done = false;
size_t result = *inout_cursor;
- for (pos = begin + *inout_cursor; pos < end && ! all_done; pos++)
- {
-
- if (do_push)
- {
+ for (pos = begin + *inout_cursor; pos < end && !all_done; pos++) {
+ if (do_push) {
ignore_newline = (*pos == '#');
do_push = false;
}
- switch (*pos)
- {
- case '\\':
- {
+ switch (*pos) {
+ case '\\': {
pos++;
break;
}
-
- case '\n':
- {
- if (ignore_newline)
- {
+ case '\n': {
+ if (ignore_newline) {
ignore_newline = false;
- }
- else
- {
- /* Note: pos will be left pointing just after this newline, because of the ++ in the loop */
+ } else {
+ // Note: pos will be left pointing just after this newline, because of the ++ in
+ // the loop.
all_done = true;
}
break;
@@ -526,151 +456,140 @@ static size_t offset_of_next_item_fish_1_x(const char *begin, size_t mmap_length
return result;
}
-// Returns the offset of the next item based on the given history type, or -1
-static size_t offset_of_next_item(const char *begin, size_t mmap_length, history_file_type_t mmap_type, size_t *inout_cursor, time_t cutoff_timestamp)
-{
+// Returns the offset of the next item based on the given history type, or -1.
+static size_t offset_of_next_item(const char *begin, size_t mmap_length,
+ history_file_type_t mmap_type, size_t *inout_cursor,
+ time_t cutoff_timestamp) {
size_t result;
- switch (mmap_type)
- {
- case history_type_fish_2_0:
- result = offset_of_next_item_fish_2_0(begin, mmap_length, inout_cursor, cutoff_timestamp);
+ switch (mmap_type) {
+ case history_type_fish_2_0: {
+ result =
+ offset_of_next_item_fish_2_0(begin, mmap_length, inout_cursor, cutoff_timestamp);
break;
-
- case history_type_fish_1_x:
- result = offset_of_next_item_fish_1_x(begin, mmap_length, inout_cursor, cutoff_timestamp);
+ }
+ case history_type_fish_1_x: {
+ result =
+ offset_of_next_item_fish_1_x(begin, mmap_length, inout_cursor, cutoff_timestamp);
break;
-
+ }
default:
- case history_type_unknown:
+ case history_type_unknown: {
// Oh well
result = (size_t)(-1);
break;
+ }
}
return result;
}
-history_t & history_collection_t::alloc(const wcstring &name)
-{
- /* Note that histories are currently never deleted, so we can return a reference to them without using something like shared_ptr */
+history_t &history_collection_t::alloc(const wcstring &name) {
+ // Note that histories are currently never deleted, so we can return a reference to them without
+ // using something like shared_ptr.
scoped_lock locker(m_lock);
- history_t *& current = m_histories[name];
- if (current == NULL)
- current = new history_t(name);
+ history_t *&current = m_histories[name];
+ if (current == NULL) current = new history_t(name);
return *current;
}
-history_t & history_t::history_with_name(const wcstring &name)
-{
- return histories.alloc(name);
-}
-
-history_t::history_t(const wcstring &pname) :
- name(pname),
- first_unwritten_new_item_index(0),
- has_pending_item(false),
- disable_automatic_save_counter(0),
- mmap_start(NULL),
- mmap_length(0),
- mmap_type(history_file_type_t(-1)),
- mmap_file_id(kInvalidFileID),
- boundary_timestamp(time(NULL)),
- countdown_to_vacuum(-1),
- loaded_old(false),
- chaos_mode(false)
-{
+history_t &history_t::history_with_name(const wcstring &name) { return histories.alloc(name); }
+
+history_t::history_t(const wcstring &pname)
+ : name(pname),
+ first_unwritten_new_item_index(0),
+ has_pending_item(false),
+ disable_automatic_save_counter(0),
+ mmap_start(NULL),
+ mmap_length(0),
+ mmap_type(history_file_type_t(-1)),
+ mmap_file_id(kInvalidFileID),
+ boundary_timestamp(time(NULL)),
+ countdown_to_vacuum(-1),
+ loaded_old(false),
+ chaos_mode(false) {
pthread_mutex_init(&lock, NULL);
}
-history_t::~history_t()
-{
- pthread_mutex_destroy(&lock);
-}
+history_t::~history_t() { pthread_mutex_destroy(&lock); }
-void history_t::add(const history_item_t &item, bool pending)
-{
+void history_t::add(const history_item_t &item, bool pending) {
scoped_lock locker(lock);
- /* Try merging with the last item */
- if (! new_items.empty() && new_items.back().merge(item))
- {
- /* We merged, so we don't have to add anything. Maybe this item was pending, but it just got merged with an item that is not pending, so pending just becomes false. */
+ // Try merging with the last item.
+ if (!new_items.empty() && new_items.back().merge(item)) {
+ // We merged, so we don't have to add anything. Maybe this item was pending, but it just got
+ // merged with an item that is not pending, so pending just becomes false.
this->has_pending_item = false;
- }
- else
- {
- /* We have to add a new item */
+ } else {
+ // We have to add a new item.
new_items.push_back(item);
this->has_pending_item = pending;
save_internal_unless_disabled();
}
}
-void history_t::save_internal_unless_disabled()
-{
- /* This must be called while locked */
+void history_t::save_internal_unless_disabled() {
+ // This must be called while locked.
ASSERT_IS_LOCKED(lock);
- /* Respect disable_automatic_save_counter */
- if (disable_automatic_save_counter > 0)
- {
+ // Respect disable_automatic_save_counter.
+ if (disable_automatic_save_counter > 0) {
return;
}
- /* We may or may not vacuum. We try to vacuum every kVacuumFrequency items, but start the countdown at a random number so that even if the user never runs more than 25 commands, we'll eventually vacuum. If countdown_to_vacuum is -1, it means we haven't yet picked a value for the counter. */
+ // We may or may not vacuum. We try to vacuum every kVacuumFrequency items, but start the
+ // countdown at a random number so that even if the user never runs more than 25 commands, we'll
+ // eventually vacuum. If countdown_to_vacuum is -1, it means we haven't yet picked a value for
+ // the counter.
const int kVacuumFrequency = 25;
- if (countdown_to_vacuum < 0)
- {
+ if (countdown_to_vacuum < 0) {
static unsigned int seed = (unsigned int)time(NULL);
- /* Generate a number in the range [0, kVacuumFrequency) */
+ // Generate a number in the range [0, kVacuumFrequency).
countdown_to_vacuum = rand_r(&seed) / (RAND_MAX / kVacuumFrequency + 1);
}
- /* Determine if we're going to vacuum */
+ // Determine if we're going to vacuum.
bool vacuum = false;
- if (countdown_to_vacuum == 0)
- {
+ if (countdown_to_vacuum == 0) {
countdown_to_vacuum = kVacuumFrequency;
vacuum = true;
}
- /* This might be a good candidate for moving to a background thread */
+ // This might be a good candidate for moving to a background thread.
time_profiler_t profiler(vacuum ? "save_internal vacuum" : "save_internal no vacuum");
this->save_internal(vacuum);
- /* Update our countdown */
+ // Update our countdown.
assert(countdown_to_vacuum > 0);
countdown_to_vacuum--;
}
-void history_t::add(const wcstring &str, history_identifier_t ident, bool pending)
-{
+void history_t::add(const wcstring &str, history_identifier_t ident, bool pending) {
time_t when = time(NULL);
- /* Big hack: do not allow timestamps equal to our boundary date. This is because we include items whose timestamps are equal to our boundary when reading old history, so we can catch "just closed" items. But this means that we may interpret our own items, that we just wrote, as old items, if we wrote them in the same second as our birthdate.
- */
- if (when == this->boundary_timestamp)
- {
+ // Big hack: do not allow timestamps equal to our boundary date. This is because we include
+ // items whose timestamps are equal to our boundary when reading old history, so we can catch
+ // "just closed" items. But this means that we may interpret our own items, that we just wrote,
+ // as old items, if we wrote them in the same second as our birthdate.
+ if (when == this->boundary_timestamp) {
when++;
}
this->add(history_item_t(str, when, ident), pending);
}
-void history_t::remove(const wcstring &str)
-{
- /* Add to our list of deleted items */
+void history_t::remove(const wcstring &str) {
+ // Add to our list of deleted items.
deleted_items.insert(str);
- /* Remove from our list of new items */
+ // Remove from our list of new items.
size_t idx = new_items.size();
- while (idx--)
- {
- if (new_items.at(idx).str() == str)
- {
+ while (idx--) {
+ if (new_items.at(idx).str() == str) {
new_items.erase(new_items.begin() + idx);
- /* If this index is before our first_unwritten_new_item_index, then subtract one from that index so it stays pointing at the same item. If it is equal to or larger, then we have not yet writen this item, so we don't have to adjust the index. */
- if (idx < first_unwritten_new_item_index)
- {
+ // If this index is before our first_unwritten_new_item_index, then subtract one from
+ // that index so it stays pointing at the same item. If it is equal to or larger, then
+ // we have not yet writen this item, so we don't have to adjust the index.
+ if (idx < first_unwritten_new_item_index) {
first_unwritten_new_item_index--;
}
}
@@ -678,157 +597,139 @@ void history_t::remove(const wcstring &str)
assert(first_unwritten_new_item_index <= new_items.size());
}
-void history_t::set_valid_file_paths(const wcstring_list_t &valid_file_paths, history_identifier_t ident)
-{
- /* 0 identifier is used to mean "not necessary" */
- if (ident == 0)
- {
+void history_t::set_valid_file_paths(const wcstring_list_t &valid_file_paths,
+ history_identifier_t ident) {
+ // 0 identifier is used to mean "not necessary".
+ if (ident == 0) {
return;
}
scoped_lock locker(lock);
- /* Look for an item with the given identifier. It is likely to be at the end of new_items */
- for (history_item_list_t::reverse_iterator iter = new_items.rbegin(); iter != new_items.rend(); ++iter)
- {
- if (iter->identifier == ident)
- {
- /* Found it */
+ // Look for an item with the given identifier. It is likely to be at the end of new_items.
+ for (history_item_list_t::reverse_iterator iter = new_items.rbegin(); iter != new_items.rend();
+ ++iter) {
+ if (iter->identifier == ident) { // found it
iter->required_paths = valid_file_paths;
break;
}
}
}
-void history_t::get_string_representation(wcstring *result, const wcstring &separator)
-{
+void history_t::get_string_representation(wcstring *result, const wcstring &separator) {
scoped_lock locker(lock);
bool first = true;
std::set<wcstring> seen;
- /* If we have a pending item, we skip the first encountered (i.e. last) new item */
+ // If we have a pending item, we skip the first encountered (i.e. last) new item.
bool next_is_pending = this->has_pending_item;
- /* Append new items. Note that in principle we could use const_reverse_iterator, but we do not because reverse_iterator is not convertible to const_reverse_iterator ( http://github.com/fish-shell/fish-shell/issues/431 ) */
- for (history_item_list_t::reverse_iterator iter=new_items.rbegin(); iter < new_items.rend(); ++iter)
- {
- /* Skip a pending item if we have one */
- if (next_is_pending)
- {
+ // Append new items. Note that in principle we could use const_reverse_iterator, but we do not
+ // because reverse_iterator is not convertible to const_reverse_iterator. See
+ // http://github.com/fish-shell/fish-shell/issues/431.
+ for (history_item_list_t::reverse_iterator iter = new_items.rbegin(); iter < new_items.rend();
+ ++iter) {
+ // Skip a pending item if we have one.
+ if (next_is_pending) {
next_is_pending = false;
continue;
}
- /* Skip duplicates */
- if (! seen.insert(iter->str()).second)
- continue;
+ // Skip duplicates.
+ if (!seen.insert(iter->str()).second) continue;
- if (! first)
- result->append(separator);
+ if (!first) result->append(separator);
result->append(iter->str());
first = false;
}
- /* Append old items */
+ // Append old items.
load_old_if_needed();
- for (std::deque<size_t>::reverse_iterator iter = old_item_offsets.rbegin(); iter != old_item_offsets.rend(); ++iter)
- {
+ for (std::deque<size_t>::reverse_iterator iter = old_item_offsets.rbegin();
+ iter != old_item_offsets.rend(); ++iter) {
size_t offset = *iter;
- const history_item_t item = history_t::decode_item(mmap_start + offset, mmap_length - offset, mmap_type);
+ const history_item_t item =
+ history_t::decode_item(mmap_start + offset, mmap_length - offset, mmap_type);
- /* Skip duplicates */
- if (! seen.insert(item.str()).second)
- continue;
+ // Skip duplicates.
+ if (!seen.insert(item.str()).second) continue;
- if (! first)
- result->append(separator);
+ if (!first) result->append(separator);
result->append(item.str());
first = false;
}
}
-history_item_t history_t::item_at_index(size_t idx)
-{
+history_item_t history_t::item_at_index(size_t idx) {
scoped_lock locker(lock);
- /* 0 is considered an invalid index */
+ // 0 is considered an invalid index.
assert(idx > 0);
idx--;
- /* Determine how many "resolved" (non-pending) items we have. We can have at most one pending item, and it's always the last one. */
+ // Determine how many "resolved" (non-pending) items we have. We can have at most one pending
+ // item, and it's always the last one.
size_t resolved_new_item_count = new_items.size();
- if (this->has_pending_item && resolved_new_item_count > 0)
- {
+ if (this->has_pending_item && resolved_new_item_count > 0) {
resolved_new_item_count -= 1;
}
- /* idx=0 corresponds to the last resolved item */
- if (idx < resolved_new_item_count)
- {
+ // idx == 0 corresponds to the last resolved item.
+ if (idx < resolved_new_item_count) {
return new_items.at(resolved_new_item_count - idx - 1);
}
- /* Now look in our old items */
+ // Now look in our old items.
idx -= resolved_new_item_count;
load_old_if_needed();
size_t old_item_count = old_item_offsets.size();
- if (idx < old_item_count)
- {
- /* idx=0 corresponds to last item in old_item_offsets */
+ if (idx < old_item_count) {
+ // idx == 0 corresponds to last item in old_item_offsets.
size_t offset = old_item_offsets.at(old_item_count - idx - 1);
return history_t::decode_item(mmap_start + offset, mmap_length - offset, mmap_type);
}
- /* Index past the valid range, so return an empty history item */
+ // Index past the valid range, so return an empty history item.
return history_item_t(wcstring(), 0);
}
-/* Read one line, stripping off any newline, and updating cursor. Note that our input string is NOT null terminated; it's just a memory mapped file. */
-static size_t read_line(const char *base, size_t cursor, size_t len, std::string &result)
-{
- /* Locate the newline */
+// Read one line, stripping off any newline, and updating cursor. Note that our input string is NOT
+// null terminated; it's just a memory mapped file.
+static size_t read_line(const char *base, size_t cursor, size_t len, std::string &result) {
+ // Locate the newline.
assert(cursor <= len);
const char *start = base + cursor;
const char *newline = (char *)memchr(start, '\n', len - cursor);
- if (newline != NULL)
- {
- /* We found a newline. */
+ if (newline != NULL) { // we found a newline
result.assign(start, newline - start);
-
- /* Return the amount to advance the cursor; skip over the newline */
+ // Return the amount to advance the cursor; skip over the newline.
return newline - start + 1;
- }
- else
- {
- /* We ran off the end */
+ } else {
+ // We ran off the end.
result.clear();
return len - cursor;
}
}
-/* Trims leading spaces in the given string, returning how many there were */
-static size_t trim_leading_spaces(std::string &str)
-{
+// Trims leading spaces in the given string, returning how many there were.
+static size_t trim_leading_spaces(std::string &str) {
size_t i = 0, max = str.size();
- while (i < max && str[i] == ' ')
- i++;
+ while (i < max && str[i] == ' ') i++;
str.erase(0, i);
return i;
}
-static bool extract_prefix_and_unescape_yaml(std::string *key, std::string *value, const std::string &line)
-{
+static bool extract_prefix_and_unescape_yaml(std::string *key, std::string *value,
+ const std::string &line) {
size_t where = line.find(":");
- if (where != std::string::npos)
- {
+ if (where != std::string::npos) {
key->assign(line, 0, where);
- // skip a space after the : if necessary
+ // Skip a space after the : if necessary.
size_t val_start = where + 1;
- if (val_start < line.size() && line.at(val_start) == ' ')
- val_start++;
+ if (val_start < line.size() && line.at(val_start) == ' ') val_start++;
value->assign(line, val_start, line.size() - val_start);
unescape_yaml(key);
@@ -837,9 +738,8 @@ static bool extract_prefix_and_unescape_yaml(std::string *key, std::string *valu
return where != std::string::npos;
}
-/* Decode an item via the fish 2.0 format */
-history_item_t history_t::decode_item_fish_2_0(const char *base, size_t len)
-{
+// Decode an item via the fish 2.0 format.
+history_item_t history_t::decode_item_fish_2_0(const char *base, size_t len) {
wcstring cmd;
time_t when = 0;
path_list_t paths;
@@ -847,60 +747,48 @@ history_item_t history_t::decode_item_fish_2_0(const char *base, size_t len)
size_t indent = 0, cursor = 0;
std::string key, value, line;
- /* Read the "- cmd:" line */
+ // Read the "- cmd:" line.
size_t advance = read_line(base, cursor, len, line);
trim_leading_spaces(line);
- if (! extract_prefix_and_unescape_yaml(&key, &value, line) || key != "- cmd")
- {
+ if (!extract_prefix_and_unescape_yaml(&key, &value, line) || key != "- cmd") {
goto done;
}
cursor += advance;
cmd = str2wcstring(value);
- /* Read the remaining lines */
- for (;;)
- {
- /* Read a line */
+ // Read the remaining lines.
+ for (;;) {
size_t advance = read_line(base, cursor, len, line);
- /* Count and trim leading spaces */
size_t this_indent = trim_leading_spaces(line);
- if (indent == 0)
- indent = this_indent;
+ if (indent == 0) indent = this_indent;
- if (this_indent == 0 || indent != this_indent)
- break;
+ if (this_indent == 0 || indent != this_indent) break;
- if (! extract_prefix_and_unescape_yaml(&key, &value, line))
- break;
+ if (!extract_prefix_and_unescape_yaml(&key, &value, line)) break;
- /* We are definitely going to consume this line */
+ // We are definitely going to consume this line.
cursor += advance;
- if (key == "when")
- {
- /* Parse an int from the timestamp. Should this fail, strtol returns 0; that's acceptable. */
+ if (key == "when") {
+ // Parse an int from the timestamp. Should this fail, strtol returns 0; that's
+ // acceptable.
char *end = NULL;
long tmp = strtol(value.c_str(), &end, 0);
when = tmp;
- }
- else if (key == "paths")
- {
- /* Read lines starting with " - " until we can't read any more */
- for (;;)
- {
+ } else if (key == "paths") {
+ // Read lines starting with " - " until we can't read any more.
+ for (;;) {
size_t advance = read_line(base, cursor, len, line);
- if (trim_leading_spaces(line) <= indent)
- break;
+ if (trim_leading_spaces(line) <= indent) break;
- if (strncmp(line.c_str(), "- ", 2))
- break;
+ if (strncmp(line.c_str(), "- ", 2)) break;
- /* We're going to consume this line */
+ // We're going to consume this line.
cursor += advance;
- /* Skip the leading dash-space and then store this path it */
+ // Skip the leading dash-space and then store this path it.
line.erase(0, 2);
unescape_yaml(&line);
paths.push_back(str2wcstring(line));
@@ -913,10 +801,8 @@ done:
return result;
}
-history_item_t history_t::decode_item(const char *base, size_t len, history_file_type_t type)
-{
- switch (type)
- {
+history_item_t history_t::decode_item(const char *base, size_t len, history_file_type_t type) {
+ switch (type) {
case history_type_fish_1_x:
return history_t::decode_item_fish_1_x(base, len);
case history_type_fish_2_0:
@@ -926,35 +812,24 @@ history_item_t history_t::decode_item(const char *base, size_t len, history_file
}
}
-/**
- Remove backslashes from all newlines. This makes a string from the
- history file better formated for on screen display.
-*/
-static wcstring history_unescape_newlines_fish_1_x(const wcstring &in_str)
-{
+// Remove backslashes from all newlines. This makes a string from the history file better formated
+// for on screen display.
+static wcstring history_unescape_newlines_fish_1_x(const wcstring &in_str) {
wcstring out;
- for (const wchar_t *in = in_str.c_str(); *in; in++)
- {
- if (*in == L'\\')
- {
- if (*(in+1)!= L'\n')
- {
+ for (const wchar_t *in = in_str.c_str(); *in; in++) {
+ if (*in == L'\\') {
+ if (*(in + 1) != L'\n') {
out.push_back(*in);
}
- }
- else
- {
+ } else {
out.push_back(*in);
}
}
return out;
}
-
-/* Decode an item via the fish 1.x format. Adapted from fish 1.x's item_get(). */
-history_item_t history_t::decode_item_fish_1_x(const char *begin, size_t length)
-{
-
+// Decode an item via the fish 1.x format. Adapted from fish 1.x's item_get().
+history_item_t history_t::decode_item_fish_1_x(const char *begin, size_t length) {
const char *end = begin + length;
const char *pos = begin;
wcstring out;
@@ -963,74 +838,56 @@ history_item_t history_t::decode_item_fish_1_x(const char *begin, size_t length)
bool timestamp_mode = false;
time_t timestamp = 0;
- while (1)
- {
+ while (1) {
wchar_t c;
size_t res;
mbstate_t state = {};
- if (MB_CUR_MAX == 1) // single-byte locale
- {
+ if (MB_CUR_MAX == 1) { // single-byte locale
c = (unsigned char)*pos;
res = 1;
- }
- else
- {
+ } else {
res = mbrtowc(&c, pos, end - pos, &state);
}
- if (res == (size_t)-1)
- {
+ if (res == (size_t)-1) {
pos++;
continue;
- }
- else if (res == (size_t)-2)
- {
+ } else if (res == (size_t)-2) {
break;
- }
- else if (res == (size_t)0)
- {
+ } else if (res == (size_t)0) {
pos++;
continue;
}
pos += res;
- if (c == L'\n')
- {
- if (timestamp_mode)
- {
+ if (c == L'\n') {
+ if (timestamp_mode) {
const wchar_t *time_string = out.c_str();
- while (*time_string && !iswdigit(*time_string))
- time_string++;
- errno=0;
+ while (*time_string && !iswdigit(*time_string)) time_string++;
+ errno = 0;
- if (*time_string)
- {
+ if (*time_string) {
time_t tm;
wchar_t *end;
errno = 0;
tm = (time_t)wcstol(time_string, &end, 10);
- if (tm && !errno && !*end)
- {
+ if (tm && !errno && !*end) {
timestamp = tm;
}
-
}
out.clear();
timestamp_mode = false;
continue;
}
- if (!was_backslash)
- break;
+ if (!was_backslash) break;
}
- if (first_char)
- {
- if (c == L'#')
- timestamp_mode = true;
+ if (first_char) {
+ if (c == L'#') timestamp_mode = true;
}
first_char = false;
@@ -1038,81 +895,71 @@ history_item_t history_t::decode_item_fish_1_x(const char *begin, size_t length)
out.push_back(c);
was_backslash = ((c == L'\\') && !was_backslash);
-
}
out = history_unescape_newlines_fish_1_x(out);
return history_item_t(out, timestamp);
}
-
-/* Try to infer the history file type based on inspecting the data */
-static history_file_type_t infer_file_type(const char *data, size_t len)
-{
+// Try to infer the history file type based on inspecting the data.
+static history_file_type_t infer_file_type(const char *data, size_t len) {
history_file_type_t result = history_type_unknown;
- if (len > 0)
- {
- /* Old fish started with a # */
- if (data[0] == '#')
- {
+ if (len > 0) { // old fish started with a #
+ if (data[0] == '#') {
result = history_type_fish_1_x;
- }
- else
- {
- /* Assume new fish */
+ } else { // assume new fish
result = history_type_fish_2_0;
}
}
return result;
}
-void history_t::populate_from_mmap(void)
-{
+void history_t::populate_from_mmap(void) {
mmap_type = infer_file_type(mmap_start, mmap_length);
size_t cursor = 0;
- for (;;)
- {
- size_t offset = offset_of_next_item(mmap_start, mmap_length, mmap_type, &cursor, boundary_timestamp);
- // If we get back -1, we're done
- if (offset == (size_t)(-1))
- break;
+ for (;;) {
+ size_t offset =
+ offset_of_next_item(mmap_start, mmap_length, mmap_type, &cursor, boundary_timestamp);
+ // If we get back -1, we're done.
+ if (offset == (size_t)(-1)) break;
- // Remember this item
+ // Remember this item.
old_item_offsets.push_back(offset);
}
}
-/* Do a private, read-only map of the entirety of a history file with the given name. Returns true if successful. Returns the mapped memory region by reference. */
-bool history_t::map_file(const wcstring &name, const char **out_map_start, size_t *out_map_len, file_id_t *file_id)
-{
+// Do a private, read-only map of the entirety of a history file with the given name. Returns true
+// if successful. Returns the mapped memory region by reference.
+bool history_t::map_file(const wcstring &name, const char **out_map_start, size_t *out_map_len,
+ file_id_t *file_id) {
bool result = false;
wcstring filename = history_filename(name, L"");
- if (! filename.empty())
- {
+ if (!filename.empty()) {
int fd = wopen_cloexec(filename, O_RDONLY);
- if (fd >= 0)
- {
-
- /* Get the file ID if requested */
- if (file_id != NULL)
- *file_id = file_id_for_fd(fd);
-
- /* Take a read lock to guard against someone else appending. This is released when the file is closed (below). We will read the file after releasing the lock, but that's not a problem, because we never modify already written data. In short, the purpose of this lock is to ensure we don't see the file size change mid-update.
-
- We may fail to lock (e.g. on lockless NFS - see https://github.com/fish-shell/fish-shell/issues/685 ). In that case, we proceed as if it did not fail. The risk is that we may get an incomplete history item; this is unlikely because we only treat an item as valid if it has a terminating newline.
-
- Simulate a failing lock in chaos_mode
- */
- if (! chaos_mode) history_file_lock(fd, F_RDLCK);
+ if (fd >= 0) {
+ // Get the file ID if requested.
+ if (file_id != NULL) *file_id = file_id_for_fd(fd);
+
+ // Take a read lock to guard against someone else appending. This is released when the
+ // file is closed (below). We will read the file after releasing the lock, but that's
+ // not a problem, because we never modify already written data. In short, the purpose of
+ // this lock is to ensure we don't see the file size change mid-update.
+ //
+ // We may fail to lock (e.g. on lockless NFS - see
+ // https://github.com/fish-shell/fish-shell/issues/685 ). In that case, we proceed as
+ // if it did not fail. The risk is that we may get an incomplete history item; this
+ // is unlikely because we only treat an item as valid if it has a terminating
+ // newline.
+ //
+ // Simulate a failing lock in chaos_mode.
+ if (!chaos_mode) history_file_lock(fd, F_RDLCK);
off_t len = lseek(fd, 0, SEEK_END);
- if (len != (off_t)-1)
- {
+ if (len != (off_t)-1) {
size_t mmap_length = (size_t)len;
- if (lseek(fd, 0, SEEK_SET) == 0)
- {
+ if (lseek(fd, 0, SEEK_SET) == 0) {
char *mmap_start;
- if ((mmap_start = (char *)mmap(0, mmap_length, PROT_READ, MAP_PRIVATE, fd, 0)) != MAP_FAILED)
- {
+ if ((mmap_start = (char *)mmap(0, mmap_length, PROT_READ, MAP_PRIVATE, fd,
+ 0)) != MAP_FAILED) {
result = true;
*out_map_start = mmap_start;
*out_map_len = mmap_length;
@@ -1125,82 +972,69 @@ bool history_t::map_file(const wcstring &name, const char **out_map_start, size_
return result;
}
-bool history_t::load_old_if_needed(void)
-{
+bool history_t::load_old_if_needed(void) {
if (loaded_old) return true;
loaded_old = true;
-
// PCA not sure why signals were blocked here
- //signal_block();
+ // signal_block();
bool ok = false;
- if (map_file(name, &mmap_start, &mmap_length, &mmap_file_id))
- {
- // Here we've mapped the file
+ if (map_file(name, &mmap_start, &mmap_length, &mmap_file_id)) {
+ // Here we've mapped the file.
ok = true;
time_profiler_t profiler("populate_from_mmap");
this->populate_from_mmap();
}
- //signal_unblock();
+ // signal_unblock();
return ok;
}
-void history_search_t::skip_matches(const wcstring_list_t &skips)
-{
+void history_search_t::skip_matches(const wcstring_list_t &skips) {
external_skips = skips;
std::sort(external_skips.begin(), external_skips.end());
}
-bool history_search_t::should_skip_match(const wcstring &str) const
-{
+bool history_search_t::should_skip_match(const wcstring &str) const {
return std::binary_search(external_skips.begin(), external_skips.end(), str);
}
-bool history_search_t::go_forwards()
-{
- /* Pop the top index (if more than one) and return if we have any left */
- if (prev_matches.size() > 1)
- {
+bool history_search_t::go_forwards() {
+ // Pop the top index (if more than one) and return if we have any left.
+ if (prev_matches.size() > 1) {
prev_matches.pop_back();
return true;
}
return false;
}
-bool history_search_t::go_backwards()
-{
- /* Backwards means increasing our index */
+bool history_search_t::go_backwards() {
+ // Backwards means increasing our index.
const size_t max_idx = (size_t)(-1);
size_t idx = 0;
- if (! prev_matches.empty())
- idx = prev_matches.back().first;
+ if (!prev_matches.empty()) idx = prev_matches.back().first;
- if (idx == max_idx)
- return false;
+ if (idx == max_idx) return false;
const bool main_thread = is_main_thread();
- while (++idx < max_idx)
- {
- if (main_thread ? reader_interrupted() : reader_thread_job_is_stale())
- {
+ while (++idx < max_idx) {
+ if (main_thread ? reader_interrupted() : reader_thread_job_is_stale()) {
return false;
}
const history_item_t item = history->item_at_index(idx);
- /* We're done if it's empty or we cancelled */
- if (item.empty())
- {
+ // We're done if it's empty or we cancelled.
+ if (item.empty()) {
return false;
}
- /* Look for a term that matches and that we haven't seen before */
+ // Look for a term that matches and that we haven't seen before.
const wcstring &str = item.str();
- if (item.matches_search(term, search_type) && ! match_already_made(str) && ! should_skip_match(str))
- {
+ if (item.matches_search(term, search_type) && !match_already_made(str) &&
+ !should_skip_match(str)) {
prev_matches.push_back(prev_match_t(idx, item));
return true;
}
@@ -1208,110 +1042,86 @@ bool history_search_t::go_backwards()
return false;
}
-/** Goes to the end (forwards) */
-void history_search_t::go_to_end(void)
-{
- prev_matches.clear();
-}
-
-/** Returns if we are at the end, which is where we start. */
-bool history_search_t::is_at_end(void) const
-{
- return prev_matches.empty();
-}
+// Goes to the end (forwards).
+void history_search_t::go_to_end(void) { prev_matches.clear(); }
+// Returns if we are at the end, which is where we start.
+bool history_search_t::is_at_end(void) const { return prev_matches.empty(); }
-/** Goes to the beginning (backwards) */
-void history_search_t::go_to_beginning(void)
-{
- /* Just go backwards as far as we can */
+// Goes to the beginning (backwards).
+void history_search_t::go_to_beginning(void) {
+ // Go backwards as far as we can.
while (go_backwards())
- ;
+ ; // noop
}
-
-history_item_t history_search_t::current_item() const
-{
- assert(! prev_matches.empty());
+history_item_t history_search_t::current_item() const {
+ assert(!prev_matches.empty());
return prev_matches.back().second;
}
-wcstring history_search_t::current_string() const
-{
+wcstring history_search_t::current_string() const {
history_item_t item = this->current_item();
return item.str();
}
-bool history_search_t::match_already_made(const wcstring &match) const
-{
- for (std::vector<prev_match_t>::const_iterator iter = prev_matches.begin(); iter != prev_matches.end(); ++iter)
- {
- if (iter->second.str() == match)
- return true;
+bool history_search_t::match_already_made(const wcstring &match) const {
+ for (std::vector<prev_match_t>::const_iterator iter = prev_matches.begin();
+ iter != prev_matches.end(); ++iter) {
+ if (iter->second.str() == match) return true;
}
return false;
}
-static void replace_all(std::string *str, const char *needle, const char *replacement)
-{
+static void replace_all(std::string *str, const char *needle, const char *replacement) {
size_t needle_len = strlen(needle), replacement_len = strlen(replacement);
size_t offset = 0;
- while ((offset = str->find(needle, offset)) != std::string::npos)
- {
+ while ((offset = str->find(needle, offset)) != std::string::npos) {
str->replace(offset, needle_len, replacement);
offset += replacement_len;
}
}
-static void escape_yaml(std::string *str)
-{
- replace_all(str, "\\", "\\\\"); //replace one backslash with two
- replace_all(str, "\n", "\\n"); //replace newline with backslash + literal n
+static void escape_yaml(std::string *str) {
+ replace_all(str, "\\", "\\\\"); // replace one backslash with two
+ replace_all(str, "\n", "\\n"); // replace newline with backslash + literal n
}
-/* This function is called frequently, so it ought to be fast. */
-static void unescape_yaml(std::string *str)
-{
+// This function is called frequently, so it ought to be fast.
+static void unescape_yaml(std::string *str) {
size_t cursor = 0, size = str->size();
- while (cursor < size)
- {
+ while (cursor < size) {
// Operate on a const version of str, to avoid needless COWs that at() does.
const std::string &const_str = *str;
- // Look for a backslash
+ // Look for a backslash.
size_t backslash = const_str.find('\\', cursor);
- if (backslash == std::string::npos || backslash + 1 >= size)
- {
- // Either not found, or found as the last character
+ if (backslash == std::string::npos || backslash + 1 >= size) {
+ // Either not found, or found as the last character.
break;
- }
- else
- {
- // Backslash found. Maybe we'll do something about it. Be sure to invoke the const version of at().
+ } else {
+ // Backslash found. Maybe we'll do something about it. Be sure to invoke the const
+ // version of at().
char escaped_char = const_str.at(backslash + 1);
- if (escaped_char == '\\')
- {
+ if (escaped_char == '\\') {
// Two backslashes in a row. Delete the second one.
str->erase(backslash + 1, 1);
size--;
- }
- else if (escaped_char == 'n')
- {
+ } else if (escaped_char == 'n') {
// Backslash + n. Replace with a newline.
str->replace(backslash, 2, "\n");
size--;
}
- // The character at index backslash has now been made whole; start at the next character
+ // The character at index backslash has now been made whole; start at the next
+ // character.
cursor = backslash + 1;
}
}
}
-static wcstring history_filename(const wcstring &name, const wcstring &suffix)
-{
+static wcstring history_filename(const wcstring &name, const wcstring &suffix) {
wcstring path;
- if (! path_get_data(path))
- return L"";
+ if (!path_get_data(path)) return L"";
wcstring result = path;
result.append(L"/");
@@ -1321,12 +1131,10 @@ static wcstring history_filename(const wcstring &name, const wcstring &suffix)
return result;
}
-void history_t::clear_file_state()
-{
+void history_t::clear_file_state() {
ASSERT_IS_LOCKED(lock);
- /* Erase everything we know about our file */
- if (mmap_start != NULL && mmap_start != MAP_FAILED)
- {
+ // Erase everything we know about our file.
+ if (mmap_start != NULL && mmap_start != MAP_FAILED) {
munmap((void *)mmap_start, mmap_length);
}
mmap_start = NULL;
@@ -1335,110 +1143,103 @@ void history_t::clear_file_state()
old_item_offsets.clear();
}
-void history_t::compact_new_items()
-{
- /* Keep only the most recent items with the given contents. This algorithm could be made more efficient, but likely would consume more memory too. */
+void history_t::compact_new_items() {
+ // Keep only the most recent items with the given contents. This algorithm could be made more
+ // efficient, but likely would consume more memory too.
std::set<wcstring> seen;
size_t idx = new_items.size();
- while (idx--)
- {
+ while (idx--) {
const history_item_t &item = new_items[idx];
- if (! seen.insert(item.contents).second)
- {
- // This item was not inserted because it was already in the set, so delete the item at this index
+ if (!seen.insert(item.contents).second) {
+ // This item was not inserted because it was already in the set, so delete the item at
+ // this index.
new_items.erase(new_items.begin() + idx);
- if (idx < first_unwritten_new_item_index)
- {
- /* Decrement first_unwritten_new_item_index if we are deleting a previously written item */
+ if (idx < first_unwritten_new_item_index) {
+ // Decrement first_unwritten_new_item_index if we are deleting a previously written
+ // item.
first_unwritten_new_item_index--;
}
}
}
}
-bool history_t::save_internal_via_rewrite()
-{
- /* This must be called while locked */
+bool history_t::save_internal_via_rewrite() {
+ // This must be called while locked.
ASSERT_IS_LOCKED(lock);
-
bool ok = false;
wcstring tmp_name_template = history_filename(name, L".XXXXXX");
- if (! tmp_name_template.empty())
- {
- /* Make an LRU cache to save only the last N elements */
+ if (!tmp_name_template.empty()) {
+ // Make an LRU cache to save only the last N elements.
history_lru_cache_t lru(HISTORY_SAVE_MAX);
- /* Insert old items in, from old to new. Merge them with our new items, inserting items with earlier timestamps first. */
+ // Insert old items in, from old to new. Merge them with our new items, inserting items with
+ // earlier timestamps first.
history_item_list_t::const_iterator new_item_iter = new_items.begin();
- /* Map in existing items (which may have changed out from underneath us, so don't trust our old mmap'd data) */
+ // Map in existing items (which may have changed out from underneath us, so don't trust our
+ // old mmap'd data).
const char *local_mmap_start = NULL;
size_t local_mmap_size = 0;
- if (map_file(name, &local_mmap_start, &local_mmap_size, NULL))
- {
- const history_file_type_t local_mmap_type = infer_file_type(local_mmap_start, local_mmap_size);
+ if (map_file(name, &local_mmap_start, &local_mmap_size, NULL)) {
+ const history_file_type_t local_mmap_type =
+ infer_file_type(local_mmap_start, local_mmap_size);
size_t cursor = 0;
- for (;;)
- {
- size_t offset = offset_of_next_item(local_mmap_start, local_mmap_size, local_mmap_type, &cursor, 0);
- /* If we get back -1, we're done */
- if (offset == (size_t)(-1))
- break;
-
- /* Try decoding an old item */
- const history_item_t old_item = history_t::decode_item(local_mmap_start + offset, local_mmap_size - offset, local_mmap_type);
- if (old_item.empty() || deleted_items.count(old_item.str()) > 0)
- {
-// debug(0, L"Item is deleted : %s\n", old_item.str().c_str());
+ for (;;) {
+ size_t offset = offset_of_next_item(local_mmap_start, local_mmap_size,
+ local_mmap_type, &cursor, 0);
+ // If we get back -1, we're done.
+ if (offset == (size_t)(-1)) break;
+
+ // Try decoding an old item.
+ const history_item_t old_item = history_t::decode_item(
+ local_mmap_start + offset, local_mmap_size - offset, local_mmap_type);
+ if (old_item.empty() || deleted_items.count(old_item.str()) > 0) {
+ // debug(0, L"Item is deleted : %s\n",
+ // old_item.str().c_str());
continue;
}
- /* The old item may actually be more recent than our new item, if it came from another session. Insert all new items at the given index with an earlier timestamp. */
- for (; new_item_iter != new_items.end(); ++new_item_iter)
- {
- if (new_item_iter->timestamp() < old_item.timestamp())
- {
- /* This "new item" is in fact older. */
+ // The old item may actually be more recent than our new item, if it came from
+ // another session. Insert all new items at the given index with an earlier
+ // timestamp.
+ for (; new_item_iter != new_items.end(); ++new_item_iter) {
+ if (new_item_iter->timestamp() < old_item.timestamp()) {
+ // This "new item" is in fact older.
lru.add_item(*new_item_iter);
- }
- else
- {
- /* The new item is not older. */
+ } else {
+ // The new item is not older.
break;
}
}
- /* Now add this old item */
+ // Now add this old item.
lru.add_item(old_item);
}
munmap((void *)local_mmap_start, local_mmap_size);
}
- /* Insert any remaining new items */
- for (; new_item_iter != new_items.end(); ++new_item_iter)
- {
+ // Insert any remaining new items.
+ for (; new_item_iter != new_items.end(); ++new_item_iter) {
lru.add_item(*new_item_iter);
}
signal_block();
- /* Try to create a temporary file, up to 10 times. We don't use mkstemps because we want to open it CLO_EXEC. This should almost always succeed on the first try. */
+ // Try to create a temporary file, up to 10 times. We don't use mkstemps because we want to
+ // open it CLO_EXEC. This should almost always succeed on the first try.
int out_fd = -1;
wcstring tmp_name;
- for (size_t attempt = 0; attempt < 10 && out_fd == -1; attempt++)
- {
+ for (size_t attempt = 0; attempt < 10 && out_fd == -1; attempt++) {
char *narrow_str = wcs2str(tmp_name_template.c_str());
#if HAVE_MKOSTEMP
out_fd = mkostemp(narrow_str, O_CLOEXEC);
- if (out_fd >= 0)
- {
+ if (out_fd >= 0) {
tmp_name = str2wcstring(narrow_str);
}
#else
- if (narrow_str && mktemp(narrow_str))
- {
- /* It was successfully templated; try opening it atomically */
+ if (narrow_str && mktemp(narrow_str)) {
+ // It was successfully templated; try opening it atomically.
tmp_name = str2wcstring(narrow_str);
out_fd = wopen_cloexec(tmp_name, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC, 0600);
}
@@ -1446,60 +1247,46 @@ bool history_t::save_internal_via_rewrite()
free(narrow_str);
}
- if (out_fd >= 0)
- {
- /* Write them out */
+ if (out_fd >= 0) {
+ // Write them out.
bool errored = false;
history_output_buffer_t buffer;
- for (history_lru_cache_t::iterator iter = lru.begin(); iter != lru.end(); ++iter)
- {
+ for (history_lru_cache_t::iterator iter = lru.begin(); iter != lru.end(); ++iter) {
const history_lru_node_t *node = *iter;
append_yaml_to_buffer(node->key, node->timestamp, node->required_paths, &buffer);
- if (buffer.output_size() >= HISTORY_OUTPUT_BUFFER_SIZE && ! buffer.flush_to_fd(out_fd))
- {
+ if (buffer.output_size() >= HISTORY_OUTPUT_BUFFER_SIZE &&
+ !buffer.flush_to_fd(out_fd)) {
errored = true;
break;
}
}
- if (! errored && buffer.flush_to_fd(out_fd))
- {
+ if (!errored && buffer.flush_to_fd(out_fd)) {
ok = true;
}
- if (! ok)
- {
- /*
- This message does not have high enough priority to
- be shown by default.
- */
+ if (!ok) {
+ // This message does not have high enough priority to be shown by default.
debug(2, L"Error when writing history file");
- }
- else
- {
+ } else {
wcstring new_name = history_filename(name, wcstring());
- /* Ensure we maintain the ownership and permissions of the original (#2355).
- * If the stat fails, we assume (hope) our default permissions are correct. This
- * corresponds to e.g. someone running sudo -E as the very first command. If they
- * did, it would be tricky to set the permissions correctly. (bash doesn't get this
- * case right either). */
+ // Ensure we maintain the ownership and permissions of the original (#2355). If the
+ // stat fails, we assume (hope) our default permissions are correct. This
+ // corresponds to e.g. someone running sudo -E as the very first command. If they
+ // did, it would be tricky to set the permissions correctly. (bash doesn't get this
+ // case right either).
struct stat sbuf;
- if (wstat(new_name, &sbuf) >= 0)
- {
- /* Success */
- if (fchown(out_fd, sbuf.st_uid, sbuf.st_gid) == -1)
- {
+ if (wstat(new_name, &sbuf) >= 0) { // success
+ if (fchown(out_fd, sbuf.st_uid, sbuf.st_gid) == -1) {
debug(2, L"Error %d when changing ownership of history file", errno);
}
- if (fchmod(out_fd, sbuf.st_mode) == -1)
- {
+ if (fchmod(out_fd, sbuf.st_mode) == -1) {
debug(2, L"Error %d when changing mode of history file", errno);
}
}
- if (wrename(tmp_name, new_name) == -1)
- {
+ if (wrename(tmp_name, new_name) == -1) {
debug(2, L"Error %d when renaming history file", errno);
}
}
@@ -1508,90 +1295,92 @@ bool history_t::save_internal_via_rewrite()
signal_unblock();
- /* Make sure we clear all nodes, since this doesn't happen automatically */
+ // Make sure we clear all nodes, since this doesn't happen automatically.
lru.evict_all_nodes();
}
- if (ok)
- {
- /* We've saved everything, so we have no more unsaved items */
+ if (ok) {
+ // We've saved everything, so we have no more unsaved items.
this->first_unwritten_new_item_index = new_items.size();
- /* We deleted our deleted items */
+ // We deleted our deleted items.
this->deleted_items.clear();
- /* Our history has been written to the file, so clear our state so we can re-reference the file. */
+ // Our history has been written to the file, so clear our state so we can re-reference the
+ // file.
this->clear_file_state();
}
-
return ok;
}
-bool history_t::save_internal_via_appending()
-{
- /* This must be called while locked */
+bool history_t::save_internal_via_appending() {
+ // This must be called while locked.
ASSERT_IS_LOCKED(lock);
- /* No deleting allowed */
+ // No deleting allowed.
assert(deleted_items.empty());
bool ok = false;
- /* If the file is different (someone vacuumed it) then we need to update our mmap */
+ // If the file is different (someone vacuumed it) then we need to update our mmap.
bool file_changed = false;
- /* Get the path to the real history file */
+ // Get the path to the real history file.
wcstring history_path = history_filename(name, wcstring());
signal_block();
- /* Open the file */
+ // Open the file.
int out_fd = wopen_cloexec(history_path, O_WRONLY | O_APPEND);
- if (out_fd >= 0)
- {
- /* Check to see if the file changed */
- if (file_id_for_fd(out_fd) != mmap_file_id)
- file_changed = true;
-
- /* Exclusive lock on the entire file. This is released when we close the file (below). This may fail on (e.g.) lockless NFS. If so, proceed as if it did not fail; the risk is that we may get interleaved history items, which is considered better than no history, or forcing everything through the slow copy-move mode. We try to minimize this possibility by writing with O_APPEND.
-
- Simulate a failing lock in chaos_mode
- */
- if (! chaos_mode) history_file_lock(out_fd, F_WRLCK);
-
- /* We (hopefully successfully) took the exclusive lock. Append to the file.
- Note that this is sketchy for a few reasons:
- - Another shell may have appended its own items with a later timestamp, so our file may no longer be sorted by timestamp.
- - Another shell may have appended the same items, so our file may now contain duplicates.
-
- We cannot modify any previous parts of our file, because other instances may be reading those portions. We can only append.
-
- Originally we always rewrote the file on saving, which avoided both of these problems. However, appending allows us to save history after every command, which is nice!
-
- Periodically we "clean up" the file by rewriting it, so that most of the time it doesn't have duplicates, although we don't yet sort by timestamp (the timestamp isn't really used for much anyways).
- */
-
- /* So far so good. Write all items at or after first_unwritten_new_item_index. Note that we write even a pending item - pending items are ignored by history within the command itself, but should still be written to the file. */
-
+ if (out_fd >= 0) {
+ // Check to see if the file changed.
+ if (file_id_for_fd(out_fd) != mmap_file_id) file_changed = true;
+
+ // Exclusive lock on the entire file. This is released when we close the file (below). This
+ // may fail on (e.g.) lockless NFS. If so, proceed as if it did not fail; the risk is that
+ // we may get interleaved history items, which is considered better than no history, or
+ // forcing everything through the slow copy-move mode. We try to minimize this possibility
+ // by writing with O_APPEND.
+ //
+ // Simulate a failing lock in chaos_mode
+ if (!chaos_mode) history_file_lock(out_fd, F_WRLCK);
+
+ // We (hopefully successfully) took the exclusive lock. Append to the file.
+ // Note that this is sketchy for a few reasons:
+ // - Another shell may have appended its own items with a later timestamp, so our file may
+ // no longer be sorted by timestamp.
+ // - Another shell may have appended the same items, so our file may now contain
+ // duplicates.
+ //
+ // We cannot modify any previous parts of our file, because other instances may be reading
+ // those portions. We can only append.
+ //
+ // Originally we always rewrote the file on saving, which avoided both of these problems.
+ // However, appending allows us to save history after every command, which is nice!
+ //
+ // Periodically we "clean up" the file by rewriting it, so that most of the time it doesn't
+ // have duplicates, although we don't yet sort by timestamp (the timestamp isn't really used
+ // for much anyways).
+
+ // So far so good. Write all items at or after first_unwritten_new_item_index. Note that we
+ // write even a pending item - pending items are ignored by history within the command
+ // itself, but should still be written to the file.
bool errored = false;
history_output_buffer_t buffer;
- while (first_unwritten_new_item_index < new_items.size())
- {
+ while (first_unwritten_new_item_index < new_items.size()) {
const history_item_t &item = new_items.at(first_unwritten_new_item_index);
append_yaml_to_buffer(item.str(), item.timestamp(), item.get_required_paths(), &buffer);
- if (buffer.output_size() >= HISTORY_OUTPUT_BUFFER_SIZE)
- {
- errored = ! buffer.flush_to_fd(out_fd);
+ if (buffer.output_size() >= HISTORY_OUTPUT_BUFFER_SIZE) {
+ errored = !buffer.flush_to_fd(out_fd);
if (errored) break;
}
- /* We wrote this item, hooray */
+ // We wrote this item, hooray.
first_unwritten_new_item_index++;
}
- if (! errored && buffer.flush_to_fd(out_fd))
- {
+ if (!errored && buffer.flush_to_fd(out_fd)) {
ok = true;
}
@@ -1600,145 +1389,116 @@ bool history_t::save_internal_via_appending()
signal_unblock();
- /* If someone has replaced the file, forget our file state */
- if (file_changed)
- {
+ // If someone has replaced the file, forget our file state.
+ if (file_changed) {
this->clear_file_state();
}
return ok;
}
-/** Save the specified mode to file; optionally also vacuums */
-void history_t::save_internal(bool vacuum)
-{
- /* This must be called while locked */
+// Save the specified mode to file; optionally also vacuums.
+void history_t::save_internal(bool vacuum) {
ASSERT_IS_LOCKED(lock);
- /* Nothing to do if there's no new items */
- if (first_unwritten_new_item_index >= new_items.size() && deleted_items.empty())
- return;
+ // Nothing to do if there's no new items.
+ if (first_unwritten_new_item_index >= new_items.size() && deleted_items.empty()) return;
- /* Compact our new items so we don't have duplicates */
+ // Compact our new items so we don't have duplicates.
this->compact_new_items();
- /* Try saving. If we have items to delete, we have to rewrite the file. If we do not, we can append to it. */
+ // Try saving. If we have items to delete, we have to rewrite the file. If we do not, we can
+ // append to it.
bool ok = false;
- if (! vacuum && deleted_items.empty())
- {
- /* Try doing a fast append */
+ if (!vacuum && deleted_items.empty()) {
+ // Try doing a fast append.
ok = save_internal_via_appending();
}
- if (! ok)
- {
- /* We did not or could not append; rewrite the file ("vacuum" it) */
+ if (!ok) {
+ // We did not or could not append; rewrite the file ("vacuum" it).
ok = this->save_internal_via_rewrite();
}
}
-void history_t::save(void)
-{
+void history_t::save(void) {
scoped_lock locker(lock);
this->save_internal(false);
}
-void history_t::disable_automatic_saving()
-{
+void history_t::disable_automatic_saving() {
scoped_lock locker(lock);
disable_automatic_save_counter++;
- assert(disable_automatic_save_counter != 0); // overflow!
+ assert(disable_automatic_save_counter != 0); // overflow!
}
-void history_t::enable_automatic_saving()
-{
+void history_t::enable_automatic_saving() {
scoped_lock locker(lock);
- assert(disable_automatic_save_counter > 0); //underflow
+ assert(disable_automatic_save_counter > 0); // underflow
disable_automatic_save_counter--;
save_internal_unless_disabled();
}
-
-void history_t::clear(void)
-{
+void history_t::clear(void) {
scoped_lock locker(lock);
new_items.clear();
deleted_items.clear();
first_unwritten_new_item_index = 0;
old_item_offsets.clear();
wcstring filename = history_filename(name, L"");
- if (! filename.empty())
- wunlink(filename);
+ if (!filename.empty()) wunlink(filename);
this->clear_file_state();
-
}
-bool history_t::is_empty(void)
-{
+bool history_t::is_empty(void) {
scoped_lock locker(lock);
- /* If we have new items, we're not empty */
- if (! new_items.empty())
- return false;
+ // If we have new items, we're not empty.
+ if (!new_items.empty()) return false;
bool empty = false;
- if (loaded_old)
- {
- /* If we've loaded old items, see if we have any offsets */
+ if (loaded_old) {
+ // If we've loaded old items, see if we have any offsets.
empty = old_item_offsets.empty();
- }
- else
- {
- /* If we have not loaded old items, don't actually load them (which may be expensive); just stat the file and see if it exists and is nonempty */
+ } else {
+ // If we have not loaded old items, don't actually load them (which may be expensive); just
+ // stat the file and see if it exists and is nonempty.
const wcstring where = history_filename(name, L"");
struct stat buf = {};
- if (wstat(where, &buf) != 0)
- {
- /* Access failed, assume missing */
+ if (wstat(where, &buf) != 0) {
+ // Access failed, assume missing.
empty = true;
- }
- else
- {
- /* We're empty if the file is empty */
+ } else {
+ // We're empty if the file is empty.
empty = (buf.st_size == 0);
}
}
return empty;
}
-
-/* Populates from older location (in config path, rather than data path)
- This is accomplished by clearing ourselves, and copying the contents of
- the old history file to the new history file. The new contents will
- automatically be re-mapped later.
-*/
-void history_t::populate_from_config_path()
-{
+// Populates from older location (in config path, rather than data path) This is accomplished by
+// clearing ourselves, and copying the contents of the old history file to the new history file.
+// The new contents will automatically be re-mapped later.
+void history_t::populate_from_config_path() {
wcstring old_file;
if (path_get_config(old_file)) {
old_file.append(L"/");
old_file.append(name);
old_file.append(L"_history");
int src_fd = wopen_cloexec(old_file, O_RDONLY, 0);
- if (src_fd != -1)
- {
+ if (src_fd != -1) {
wcstring new_file = history_filename(name, wcstring());
- /* clear must come after we've retrieved the new_file name,
- and before we open destination file descriptor,
- since it destroys the name and the file */
+ // Clear must come after we've retrieved the new_file name, and before we open
+ // destination file descriptor, since it destroys the name and the file.
this->clear();
int dst_fd = wopen_cloexec(new_file, O_WRONLY | O_CREAT, 0644);
-
char buf[BUFSIZ];
ssize_t size;
while ((size = read(src_fd, buf, BUFSIZ)) > 0) {
ssize_t written = write(dst_fd, buf, static_cast<size_t>(size));
if (written < 0) {
- /*
- This message does not have high enough priority to
- be shown by default.
- */
+ // This message does not have high enough priority to be shown by default.
debug(2, L"Error when writing history file");
break;
}
@@ -1750,263 +1510,215 @@ void history_t::populate_from_config_path()
}
}
+// Indicate whether we ought to import the bash history file into fish.
+static bool should_import_bash_history_line(const std::string &line) {
+ if (line.empty()) return false;
-/* Indicate whether we ought to import the bash history file into fish */
-static bool should_import_bash_history_line(const std::string &line)
-{
- if (line.empty())
- return false;
-
- /* Very naive tests! Skip export; probably should skip others. */
- const char * const ignore_prefixes[] =
- {
- "export ",
- "#"
- };
+ // Very naive tests! Skip export; probably should skip others.
+ const char *const ignore_prefixes[] = {"export ", "#"};
- for (size_t i=0; i < sizeof ignore_prefixes / sizeof *ignore_prefixes; i++)
- {
+ for (size_t i = 0; i < sizeof ignore_prefixes / sizeof *ignore_prefixes; i++) {
const char *prefix = ignore_prefixes[i];
- if (! line.compare(0, strlen(prefix), prefix))
- {
+ if (!line.compare(0, strlen(prefix), prefix)) {
return false;
}
}
- /* Skip lines with backticks */
- if (line.find('`') != std::string::npos)
- return false;
+ // Skip lines with backticks.
+ if (line.find('`') != std::string::npos) return false;
return true;
}
-void history_t::populate_from_bash(FILE *stream)
-{
- /* Bash's format is very simple: just lines with #s for comments.
- Ignore a few commands that are bash-specific. This list ought to be expanded.
- */
+void history_t::populate_from_bash(FILE *stream) {
+ // Bash's format is very simple: just lines with #s for comments. Ignore a few commands that are
+ // bash-specific. This list ought to be expanded.
std::string line;
- for (;;)
- {
+ for (;;) {
line.clear();
bool success = false, has_newline = false;
- /* Loop until we've read a line */
- do
- {
+ // Loop until we've read a line.
+ do {
char buff[128];
- success = !! fgets(buff, sizeof buff, stream);
- if (success)
- {
- /* Skip the newline */
+ success = !!fgets(buff, sizeof buff, stream);
+ if (success) {
+ // Skip the newline.
char *newline = strchr(buff, '\n');
if (newline) *newline = '\0';
has_newline = (newline != NULL);
- /* Append what we've got */
+ // Append what we've got.
line.append(buff);
}
- }
- while (success && ! has_newline);
+ } while (success && !has_newline);
- /* Maybe add this line */
- if (should_import_bash_history_line(line))
- {
+ // Maybe add this line.
+ if (should_import_bash_history_line(line)) {
this->add(str2wcstring(line));
}
- if (line.empty())
- break;
+ if (line.empty()) break;
}
}
-void history_t::incorporate_external_changes()
-{
- /* To incorporate new items, we simply update our timestamp to now, so that items from previous instances get added. We then clear the file state so that we remap the file. Note that this is somehwhat expensive because we will be going back over old items. An optimization would be to preserve old_item_offsets so that they don't have to be recomputed. (However, then items *deleted* in other instances would not show up here). */
+void history_t::incorporate_external_changes() {
+ // To incorporate new items, we simply update our timestamp to now, so that items from previous
+ // instances get added. We then clear the file state so that we remap the file. Note that this
+ // is somehwhat expensive because we will be going back over old items. An optimization would be
+ // to preserve old_item_offsets so that they don't have to be recomputed. (However, then items
+ // *deleted* in other instances would not show up here).
time_t new_timestamp = time(NULL);
scoped_lock locker(lock);
- /* If for some reason the clock went backwards, we don't want to start dropping items; therefore we only do work if time has progressed. This also makes multiple calls cheap. */
- if (new_timestamp > this->boundary_timestamp)
- {
+ // If for some reason the clock went backwards, we don't want to start dropping items; therefore
+ // we only do work if time has progressed. This also makes multiple calls cheap.
+ if (new_timestamp > this->boundary_timestamp) {
this->boundary_timestamp = new_timestamp;
this->clear_file_state();
}
}
-void history_init()
-{
-}
-
+void history_init() {}
-void history_collection_t::save()
-{
- /* Save all histories */
- for (std::map<wcstring, history_t *>::iterator iter = m_histories.begin(); iter != m_histories.end(); ++iter)
- {
+void history_collection_t::save() {
+ // Save all histories.
+ for (std::map<wcstring, history_t *>::iterator iter = m_histories.begin();
+ iter != m_histories.end(); ++iter) {
history_t *hist = iter->second;
hist->save();
}
}
-void history_destroy()
-{
- histories.save();
-}
-
+void history_destroy() { histories.save(); }
-void history_sanity_check()
-{
- /*
- No sanity checking implemented yet...
- */
+void history_sanity_check() {
+ // No sanity checking implemented yet...
}
-int file_detection_context_t::perform_file_detection(bool test_all)
-{
+int file_detection_context_t::perform_file_detection(bool test_all) {
ASSERT_IS_BACKGROUND_THREAD();
valid_paths.clear();
int result = 1;
- for (path_list_t::const_iterator iter = potential_paths.begin(); iter != potential_paths.end(); ++iter)
- {
- if (path_is_valid(*iter, working_directory))
- {
- /* Push the original (possibly relative) path */
+ for (path_list_t::const_iterator iter = potential_paths.begin(); iter != potential_paths.end();
+ ++iter) {
+ if (path_is_valid(*iter, working_directory)) {
+ // Push the original (possibly relative) path.
valid_paths.push_back(*iter);
- }
- else
- {
- /* Not a valid path */
+ } else {
+ // Not a valid path.
result = 0;
- if (! test_all)
- break;
+ if (!test_all) break;
}
}
return result;
}
-bool file_detection_context_t::paths_are_valid(const path_list_t &paths)
-{
+bool file_detection_context_t::paths_are_valid(const path_list_t &paths) {
this->potential_paths = paths;
return perform_file_detection(false) > 0;
}
-file_detection_context_t::file_detection_context_t(history_t *hist, history_identifier_t ident) :
- history(hist),
- working_directory(env_get_pwd_slash()),
- history_item_identifier(ident)
-{
-}
+file_detection_context_t::file_detection_context_t(history_t *hist, history_identifier_t ident)
+ : history(hist), working_directory(env_get_pwd_slash()), history_item_identifier(ident) {}
-static int threaded_perform_file_detection(file_detection_context_t *ctx)
-{
+static int threaded_perform_file_detection(file_detection_context_t *ctx) {
ASSERT_IS_BACKGROUND_THREAD();
assert(ctx != NULL);
return ctx->perform_file_detection(true /* test all */);
}
-static void perform_file_detection_done(file_detection_context_t *ctx, int success)
-{
+static void perform_file_detection_done(file_detection_context_t *ctx, int success) {
ASSERT_IS_MAIN_THREAD();
- /* Now that file detection is done, update the history item with the valid file paths */
+ // Now that file detection is done, update the history item with the valid file paths.
ctx->history->set_valid_file_paths(ctx->valid_paths, ctx->history_item_identifier);
- /* Allow saving again */
+ // Allow saving again.
ctx->history->enable_automatic_saving();
- /* Done with the context. */
+ // Done with the context.
delete ctx;
}
-static bool string_could_be_path(const wcstring &potential_path)
-{
- // Assume that things with leading dashes aren't paths
- if (potential_path.empty() || potential_path.at(0) == L'-')
- {
+static bool string_could_be_path(const wcstring &potential_path) {
+ // Assume that things with leading dashes aren't paths.
+ if (potential_path.empty() || potential_path.at(0) == L'-') {
return false;
}
return true;
}
-void history_t::add_pending_with_file_detection(const wcstring &str)
-{
+void history_t::add_pending_with_file_detection(const wcstring &str) {
ASSERT_IS_MAIN_THREAD();
path_list_t potential_paths;
- /* Find all arguments that look like they could be file paths */
+ // Find all arguments that look like they could be file paths.
bool impending_exit = false;
parse_node_tree_t tree;
parse_tree_from_string(str, parse_flag_none, &tree, NULL);
size_t count = tree.size();
- for (size_t i=0; i < count; i++)
- {
+ for (size_t i = 0; i < count; i++) {
const parse_node_t &node = tree.at(i);
- if (! node.has_source())
- {
+ if (!node.has_source()) {
continue;
}
- if (node.type == symbol_argument)
- {
+ if (node.type == symbol_argument) {
wcstring potential_path = node.get_source(str);
bool unescaped = unescape_string_in_place(&potential_path, UNESCAPE_DEFAULT);
- if (unescaped && string_could_be_path(potential_path))
- {
+ if (unescaped && string_could_be_path(potential_path)) {
potential_paths.push_back(potential_path);
}
- }
- else if (node.type == symbol_plain_statement)
- {
- /* Hack hack hack - if the command is likely to trigger an exit, then don't do background file detection, because we won't be able to write it to our history file before we exit. */
- if (tree.decoration_for_plain_statement(node) == parse_statement_decoration_exec)
- {
+ } else if (node.type == symbol_plain_statement) {
+ // Hack hack hack - if the command is likely to trigger an exit, then don't do
+ // background file detection, because we won't be able to write it to our history file
+ // before we exit.
+ if (tree.decoration_for_plain_statement(node) == parse_statement_decoration_exec) {
impending_exit = true;
}
wcstring command;
tree.command_for_plain_statement(node, str, &command);
unescape_string_in_place(&command, UNESCAPE_DEFAULT);
- if (contains(command, L"exit", L"reboot"))
- {
+ if (contains(command, L"exit", L"reboot")) {
impending_exit = true;
}
}
}
- /* If we got a path, we'll perform file detection for autosuggestion hinting */
+ // If we got a path, we'll perform file detection for autosuggestion hinting.
history_identifier_t identifier = 0;
- if (! potential_paths.empty() && ! impending_exit)
- {
- /* Grab the next identifier */
+ if (!potential_paths.empty() && !impending_exit) {
+ // Grab the next identifier.
static history_identifier_t sLastIdentifier = 0;
identifier = ++sLastIdentifier;
- /* Create a new detection context */
+ // Create a new detection context.
file_detection_context_t *context = new file_detection_context_t(this, identifier);
context->potential_paths.swap(potential_paths);
- /* Prevent saving until we're done, so we have time to get the paths */
+ // Prevent saving until we're done, so we have time to get the paths.
this->disable_automatic_saving();
- /* Kick it off. Even though we haven't added the item yet, it updates the item on the main thread, so we can't race */
+ // Kick it off. Even though we haven't added the item yet, it updates the item on the main
+ // thread, so we can't race.
iothread_perform(threaded_perform_file_detection, perform_file_detection_done, context);
}
- /* Actually add the item to the history. */
+ // Actually add the item to the history.
this->add(str, identifier, true /* pending */);
- /* If we think we're about to exit, save immediately, regardless of any disabling. This may cause us to lose file hinting for some commands, but it beats losing history items */
- if (impending_exit)
- {
+ // If we think we're about to exit, save immediately, regardless of any disabling. This may
+ // cause us to lose file hinting for some commands, but it beats losing history items.
+ if (impending_exit) {
this->save();
}
}
-/* Very simple, just mark that we have no more pending items */
-void history_t::resolve_pending()
-{
+// Very simple, just mark that we have no more pending items.
+void history_t::resolve_pending() {
scoped_lock locker(lock);
this->has_pending_item = false;
}
diff --git a/src/history.h b/src/history.h
index 58291f22..b278c3cc 100644
--- a/src/history.h
+++ b/src/history.h
@@ -1,377 +1,353 @@
-/** \file history.h
- Prototypes for history functions, part of the user interface.
-*/
-
+// Prototypes for history functions, part of the user interface.
#ifndef FISH_HISTORY_H
#define FISH_HISTORY_H
-#include "common.h"
-#include "wutil.h"
-#include <deque>
-#include <vector>
-#include <utility>
-#include <set>
#include <pthread.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <time.h>
+#include <deque>
+#include <set>
#include <string>
+#include <utility>
+#include <vector>
+#include "common.h"
+#include "wutil.h"
-/* fish supports multiple shells writing to history at once. Here is its strategy:
-
-1. All history files are append-only. Data, once written, is never modified.
-2. A history file may be re-written ("vacuumed"). This involves reading in the file and writing a new one, while performing maintenance tasks: discarding items in an LRU fashion until we reach the desired maximum count, removing duplicates, and sorting them by timestamp (eventually, not implemented yet). The new file is atomically moved into place via rename().
-3. History files are mapped in via mmap(). Before the file is mapped, the file takes a fcntl read lock. The purpose of this lock is to avoid seeing a transient state where partial data has been written to the file.
-4. History is appended to under a fcntl write lock.
-5. The chaos_mode boolean can be set to true to do things like lower buffer sizes which can trigger race conditions. This is useful for testing.
-*/
+// Fish supports multiple shells writing to history at once. Here is its strategy:
+//
+// 1. All history files are append-only. Data, once written, is never modified.
+//
+// 2. A history file may be re-written ("vacuumed"). This involves reading in the file and writing a
+// new one, while performing maintenance tasks: discarding items in an LRU fashion until we reach
+// the desired maximum count, removing duplicates, and sorting them by timestamp (eventually, not
+// implemented yet). The new file is atomically moved into place via rename().
+//
+// 3. History files are mapped in via mmap(). Before the file is mapped, the file takes a fcntl read
+// lock. The purpose of this lock is to avoid seeing a transient state where partial data has been
+// written to the file.
+//
+// 4. History is appended to under a fcntl write lock.
+//
+// 5. The chaos_mode boolean can be set to true to do things like lower buffer sizes which can
+// trigger race conditions. This is useful for testing.
typedef std::vector<wcstring> path_list_t;
-enum history_search_type_t
-{
- /** The history searches for strings containing the given string */
+enum history_search_type_t {
+ // The history searches for strings containing the given string.
HISTORY_SEARCH_TYPE_CONTAINS,
-
- /** The history searches for strings starting with the given string */
+ // The history searches for strings starting with the given string.
HISTORY_SEARCH_TYPE_PREFIX
};
typedef uint32_t history_identifier_t;
-class history_item_t
-{
+class history_item_t {
friend class history_t;
friend class history_tests_t;
-private:
+ private:
explicit history_item_t(const wcstring &str);
explicit history_item_t(const wcstring &, time_t, history_identifier_t ident = 0);
- /** Attempts to merge two compatible history items together */
+ // Attempts to merge two compatible history items together.
bool merge(const history_item_t &item);
- /** The actual contents of the entry */
+ // The actual contents of the entry.
wcstring contents;
- /** Original creation time for the entry */
+ // Original creation time for the entry.
time_t creation_timestamp;
- /** Sometimes unique identifier used for hinting */
+ // Sometimes unique identifier used for hinting.
history_identifier_t identifier;
- /** Paths that we require to be valid for this item to be autosuggested */
+ // Paths that we require to be valid for this item to be autosuggested.
path_list_t required_paths;
-public:
- const wcstring &str() const
- {
- return contents;
- }
+ public:
+ const wcstring &str() const { return contents; }
- bool empty() const
- {
- return contents.empty();
- }
+ bool empty() const { return contents.empty(); }
- /* Whether our contents matches a search term. */
+ // Whether our contents matches a search term.
bool matches_search(const wcstring &term, enum history_search_type_t type) const;
- time_t timestamp() const
- {
- return creation_timestamp;
- }
+ time_t timestamp() const { return creation_timestamp; }
- const path_list_t &get_required_paths() const
- {
- return required_paths;
- }
+ const path_list_t &get_required_paths() const { return required_paths; }
- bool operator==(const history_item_t &other) const
- {
- return contents == other.contents &&
- creation_timestamp == other.creation_timestamp &&
+ bool operator==(const history_item_t &other) const {
+ return contents == other.contents && creation_timestamp == other.creation_timestamp &&
required_paths == other.required_paths;
}
};
typedef std::deque<history_item_t> history_item_list_t;
-/* The type of file that we mmap'd */
-enum history_file_type_t
-{
- history_type_unknown,
- history_type_fish_2_0,
- history_type_fish_1_x
-};
+// The type of file that we mmap'd.
+enum history_file_type_t { history_type_unknown, history_type_fish_2_0, history_type_fish_1_x };
-class history_t
-{
+class history_t {
friend class history_tests_t;
-private:
- /** No copying */
- history_t(const history_t&);
- history_t &operator=(const history_t&);
- /** Privately add an item. If pending, the item will not be returned by history searches until a call to resolve_pending. */
+ private:
+ // No copying.
+ history_t(const history_t &);
+ history_t &operator=(const history_t &);
+
+ // Privately add an item. If pending, the item will not be returned by history searches until a
+ // call to resolve_pending.
void add(const history_item_t &item, bool pending = false);
- /** Lock for thread safety */
+ // Lock for thread safety.
pthread_mutex_t lock;
- /** Internal function */
+ // Internal function.
void clear_file_state();
- /** The name of this list. Used for picking a suitable filename and for switching modes. */
+ // The name of this list. Used for picking a suitable filename and for switching modes.
const wcstring name;
- /** New items. Note that these are NOT discarded on save. We need to keep these around so we can distinguish between items in our history and items in the history of other shells that were started after we were started. */
+ // New items. Note that these are NOT discarded on save. We need to keep these around so we can
+ // distinguish between items in our history and items in the history of other shells that were
+ // started after we were started.
history_item_list_t new_items;
- /** The index of the first new item that we have not yet written. */
+ // The index of the first new item that we have not yet written.
size_t first_unwritten_new_item_index;
- /** Whether we have a pending item. If so, the most recently added item is ignored by item_at_index. */
+ // Whether we have a pending item. If so, the most recently added item is ignored by
+ // item_at_index.
bool has_pending_item;
- /** Whether we should disable saving to the file for a time */
+ // Whether we should disable saving to the file for a time.
uint32_t disable_automatic_save_counter;
- /** Deleted item contents. */
+ // Deleted item contents.
std::set<wcstring> deleted_items;
- /** The mmaped region for the history file */
+ // The mmaped region for the history file.
const char *mmap_start;
- /** The size of the mmap'd region */
+ // The size of the mmap'd region.
size_t mmap_length;
- /** The type of file we mmap'd */
+ // The type of file we mmap'd.
history_file_type_t mmap_type;
- /** The file ID of the file we mmap'd */
+ // The file ID of the file we mmap'd.
file_id_t mmap_file_id;
- /** The boundary timestamp distinguishes old items from new items. Items whose timestamps are <= the boundary are considered "old". Items whose timestemps are > the boundary are new, and are ignored by this instance (unless they came from this instance). The timestamp may be adjusted by incorporate_external_changes() */
+ // The boundary timestamp distinguishes old items from new items. Items whose timestamps are <=
+ // the boundary are considered "old". Items whose timestemps are > the boundary are new, and are
+ // ignored by this instance (unless they came from this instance). The timestamp may be adjusted
+ // by incorporate_external_changes().
time_t boundary_timestamp;
- /** How many items we add until the next vacuum. Initially a random value. */
+ // How many items we add until the next vacuum. Initially a random value.
int countdown_to_vacuum;
- /** Figure out the offsets of our mmap data */
+ // Figure out the offsets of our mmap data.
void populate_from_mmap(void);
- /** List of old items, as offsets into out mmap data */
+ // List of old items, as offsets into out mmap data.
std::deque<size_t> old_item_offsets;
- /** Whether we've loaded old items */
+ // Whether we've loaded old items.
bool loaded_old;
- /** Loads old if necessary */
+ // Loads old if necessary.
bool load_old_if_needed(void);
- /** Memory maps the history file if necessary */
+ // Memory maps the history file if necessary.
bool mmap_if_needed(void);
- /** Deletes duplicates in new_items. */
+ // Deletes duplicates in new_items.
void compact_new_items();
- /** Saves history by rewriting the file */
+ // Saves history by rewriting the file.
bool save_internal_via_rewrite();
- /** Saves history by appending to the file */
+ // Saves history by appending to the file.
bool save_internal_via_appending();
- /** Saves history */
+ // Saves history.
void save_internal(bool vacuum);
- /** Saves history, maybe */
+ // Saves history unless doing so is disabled.
void save_internal_unless_disabled();
- /* Do a private, read-only map of the entirety of a history file with the given name. Returns true if successful. Returns the mapped memory region by reference. */
- bool map_file(const wcstring &name, const char **out_map_start, size_t *out_map_len, file_id_t *file_id);
+ // Do a private, read-only map of the entirety of a history file with the given name. Returns
+ // true if successful. Returns the mapped memory region by reference.
+ bool map_file(const wcstring &name, const char **out_map_start, size_t *out_map_len,
+ file_id_t *file_id);
- /** Whether we're in maximum chaos mode, useful for testing */
+ // Whether we're in maximum chaos mode, useful for testing.
bool chaos_mode;
- /* Versioned decoding */
+ // Versioned decoding.
static history_item_t decode_item_fish_2_0(const char *base, size_t len);
static history_item_t decode_item_fish_1_x(const char *base, size_t len);
static history_item_t decode_item(const char *base, size_t len, history_file_type_t type);
-public:
- /** Constructor */
- explicit history_t(const wcstring &);
+ public:
+ explicit history_t(const wcstring &); // constructor
+ ~history_t(); // desctructor
- /** Destructor */
- ~history_t();
+ // Returns history with the given name, creating it if necessary.
+ static history_t &history_with_name(const wcstring &name);
- /** Returns history with the given name, creating it if necessary */
- static history_t & history_with_name(const wcstring &name);
-
- /** Determines whether the history is empty. Unfortunately this cannot be const, since it may require populating the history. */
+ // Determines whether the history is empty. Unfortunately this cannot be const, since it may
+ // require populating the history.
bool is_empty(void);
- /** Add a new history item to the end. If pending is set, the item will not be returned by item_at_index until a call to resolve_pending(). Pending items are tracked with an offset into the array of new items, so adding a non-pending item has the effect of resolving all pending items. */
+ // Add a new history item to the end. If pending is set, the item will not be returned by
+ // item_at_index until a call to resolve_pending(). Pending items are tracked with an offset
+ // into the array of new items, so adding a non-pending item has the effect of resolving all
+ // pending items.
void add(const wcstring &str, history_identifier_t ident = 0, bool pending = false);
- /** Remove a history item */
+ // Remove a history item.
void remove(const wcstring &str);
- /** Add a new pending history item to the end, and then begin file detection on the items to determine which arguments are paths */
+ // Add a new pending history item to the end, and then begin file detection on the items to
+ // determine which arguments are paths
void add_pending_with_file_detection(const wcstring &str);
- /** Resolves any pending history items, so that they may be returned in history searches. */
+ // Resolves any pending history items, so that they may be returned in history searches.
void resolve_pending();
- /** Saves history */
+ // Saves history.
void save();
- /** Enable / disable automatic saving. Main thread only! */
+ // Enable / disable automatic saving. Main thread only!
void disable_automatic_saving();
void enable_automatic_saving();
- /** Irreversibly clears history */
+ // Irreversibly clears history.
void clear();
- /** Populates from older location ()in config path, rather than data path) */
+ // Populates from older location ()in config path, rather than data path).
void populate_from_config_path();
- /** Populates from a bash history file */
+ // Populates from a bash history file.
void populate_from_bash(FILE *f);
- /** Incorporates the history of other shells into this history */
+ // Incorporates the history of other shells into this history.
void incorporate_external_changes();
- /* Gets all the history into a string with ARRAY_SEP_STR. This is intended for the $history environment variable. This may be long! */
+ // Gets all the history into a string with ARRAY_SEP_STR. This is intended for the $history
+ // environment variable. This may be long!
void get_string_representation(wcstring *result, const wcstring &separator);
- /** Sets the valid file paths for the history item with the given identifier */
+ // Sets the valid file paths for the history item with the given identifier.
void set_valid_file_paths(const wcstring_list_t &valid_file_paths, history_identifier_t ident);
- /** Return the specified history at the specified index. 0 is the index of the current commandline. (So the most recent item is at index 1.) */
+ // Return the specified history at the specified index. 0 is the index of the current
+ // commandline. (So the most recent item is at index 1.)
history_item_t item_at_index(size_t idx);
};
-class history_search_t
-{
-
- /** The history in which we are searching */
- history_t * history;
+class history_search_t {
+ // The history in which we are searching.
+ history_t *history;
- /** Our type */
+ // Our type.
enum history_search_type_t search_type;
- /** Our list of previous matches as index, value. The end is the current match. */
+ // Our list of previous matches as index, value. The end is the current match.
typedef std::pair<size_t, history_item_t> prev_match_t;
std::vector<prev_match_t> prev_matches;
- /** Returns yes if a given term is in prev_matches. */
+ // Returns yes if a given term is in prev_matches.
bool match_already_made(const wcstring &match) const;
- /** The search term */
+ // The search term.
wcstring term;
- /** Additional strings to skip (sorted) */
+ // Additional strings to skip (sorted).
wcstring_list_t external_skips;
bool should_skip_match(const wcstring &str) const;
-public:
-
- /** Gets the search term */
- const wcstring &get_term() const
- {
- return term;
- }
+ public:
+ // Gets the search term.
+ const wcstring &get_term() const { return term; }
- /** Sets additional string matches to skip */
+ // Sets additional string matches to skip.
void skip_matches(const wcstring_list_t &skips);
- /** Finds the next search term (forwards in time). Returns true if one was found. */
+ // Finds the next search term (forwards in time). Returns true if one was found.
bool go_forwards(void);
- /** Finds the previous search result (backwards in time). Returns true if one was found. */
+ // Finds the previous search result (backwards in time). Returns true if one was found.
bool go_backwards(void);
- /** Goes to the end (forwards) */
+ // Goes to the end (forwards).
void go_to_end(void);
- /** Returns if we are at the end. We start out at the end. */
+ // Returns if we are at the end. We start out at the end.
bool is_at_end(void) const;
- /** Goes to the beginning (backwards) */
+ // Goes to the beginning (backwards).
void go_to_beginning(void);
- /** Returns the current search result item. asserts if there is no current item. */
+ // Returns the current search result item. asserts if there is no current item.
history_item_t current_item(void) const;
- /** Returns the current search result item contents. asserts if there is no current item. */
+ // Returns the current search result item contents. asserts if there is no current item.
wcstring current_string(void) const;
+ // Constructor.
+ history_search_t(history_t &hist, const wcstring &str,
+ enum history_search_type_t type = HISTORY_SEARCH_TYPE_CONTAINS)
+ : history(&hist), search_type(type), term(str) {}
- /** Constructor */
- history_search_t(history_t &hist, const wcstring &str, enum history_search_type_t type = HISTORY_SEARCH_TYPE_CONTAINS) :
- history(&hist),
- search_type(type),
- term(str)
- {}
-
- /* Default constructor */
- history_search_t() :
- history(),
- search_type(HISTORY_SEARCH_TYPE_CONTAINS),
- term()
- {}
-
+ // Default constructor.
+ history_search_t() : history(), search_type(HISTORY_SEARCH_TYPE_CONTAINS), term() {}
};
-/**
- Init history library. The history file won't actually be loaded
- until the first time a history search is performed.
-*/
+// Init history library. The history file won't actually be loaded until the first time a history
+// search is performed.
void history_init();
-/**
- Saves the new history to disc.
-*/
+// Saves the new history to disk.
void history_destroy();
-/**
- Perform sanity checks
-*/
+// Perform sanity checks.
void history_sanity_check();
-/* A helper class for threaded detection of paths */
-struct file_detection_context_t
-{
- /* Constructor */
+// A helper class for threaded detection of paths.
+struct file_detection_context_t {
+ // Constructor.
explicit file_detection_context_t(history_t *hist, history_identifier_t ident = 0);
- /* Determine which of potential_paths are valid, and put them in valid_paths */
+ // Determine which of potential_paths are valid, and put them in valid_paths.
int perform_file_detection();
- /* The history associated with this context */
- history_t * const history;
+ // The history associated with this context.
+ history_t *const history;
- /* The working directory at the time the command was issued */
+ // The working directory at the time the command was issued.
wcstring working_directory;
- /* Paths to test */
+ // Paths to test.
path_list_t potential_paths;
- /* Paths that were found to be valid */
+ // Paths that were found to be valid.
path_list_t valid_paths;
- /* Identifier of the history item to which we are associated */
+ // Identifier of the history item to which we are associated.
const history_identifier_t history_item_identifier;
- /* Performs file detection. Returns 1 if every path in potential_paths is valid, 0 otherwise. If test_all is true, tests every path; otherwise stops as soon as it reaches an invalid path. */
+ // Performs file detection. Returns 1 if every path in potential_paths is valid, 0 otherwise. If
+ // test_all is true, tests every path; otherwise stops as soon as it reaches an invalid path.
int perform_file_detection(bool test_all);
- /* Determine whether the given paths are all valid */
+ // Determine whether the given paths are all valid.
bool paths_are_valid(const path_list_t &paths);
};
-
#endif
diff --git a/src/parse_constants.h b/src/parse_constants.h
index afc88c6c..4668516b 100644
--- a/src/parse_constants.h
+++ b/src/parse_constants.h
@@ -73,6 +73,7 @@ enum parse_token_type_t
parse_special_type_parse_error,
parse_special_type_tokenizer_error,
parse_special_type_comment,
+ LAST_TOKEN_TYPE = parse_special_type_comment,
FIRST_TERMINAL_TYPE = parse_token_type_string,
LAST_TERMINAL_TYPE = parse_token_type_terminate,
@@ -83,9 +84,13 @@ enum parse_token_type_t
LAST_PARSE_TOKEN_TYPE = parse_token_type_end
} __packed;
// Array of strings corresponding to the enums above instantiated in parse_tree.cpp.
-extern wcstring parser_token_types[];
+extern const wchar_t * const parser_token_types[];
-/* These must be maintained in sorted order (except for none, which isn't a keyword). This enables us to do binary search. */
+// These must be maintained in sorted order (except for none, which isn't a keyword). This enables
+// us to do binary search.
+//
+// IMPORTANT: If the following enum is modified you must update the corresponding keyword_map array
+// in parse_tree.cpp.
enum parse_keyword_t
{
parse_keyword_none,
diff --git a/src/parse_productions.cpp b/src/parse_productions.cpp
index 1a44ff48..6ec8e56a 100644
--- a/src/parse_productions.cpp
+++ b/src/parse_productions.cpp
@@ -409,7 +409,7 @@ const production_t *parse_productions::production_for_token(parse_token_type_t n
const bool log_it = false;
if (log_it)
{
- fprintf(stderr, "Resolving production for %ls with input token <%ls>\n", token_type_description(node_type).c_str(), input1.describe().c_str());
+ fprintf(stderr, "Resolving production for %ls with input token <%ls>\n", token_type_description(node_type), input1.describe().c_str());
}
/* Fetch the function to resolve the list of productions */
@@ -452,14 +452,14 @@ const production_t *parse_productions::production_for_token(parse_token_type_t n
case parse_token_type_background:
case parse_token_type_end:
case parse_token_type_terminate:
- fprintf(stderr, "Terminal token type %ls passed to %s\n", token_type_description(node_type).c_str(), __FUNCTION__);
+ fprintf(stderr, "Terminal token type %ls passed to %s\n", token_type_description(node_type), __FUNCTION__);
PARSER_DIE();
break;
case parse_special_type_parse_error:
case parse_special_type_tokenizer_error:
case parse_special_type_comment:
- fprintf(stderr, "Special type %ls passed to %s\n", token_type_description(node_type).c_str(), __FUNCTION__);
+ fprintf(stderr, "Special type %ls passed to %s\n", token_type_description(node_type), __FUNCTION__);
PARSER_DIE();
break;
@@ -477,7 +477,7 @@ const production_t *parse_productions::production_for_token(parse_token_type_t n
{
if (log_it)
{
- fprintf(stderr, "Node type '%ls' has no production for input '%ls' (in %s)\n", token_type_description(node_type).c_str(), input1.describe().c_str(), __FUNCTION__);
+ fprintf(stderr, "Node type '%ls' has no production for input '%ls' (in %s)\n", token_type_description(node_type), input1.describe().c_str(), __FUNCTION__);
}
}
diff --git a/src/parse_tree.cpp b/src/parse_tree.cpp
index cdaec9ef..95493955 100644
--- a/src/parse_tree.cpp
+++ b/src/parse_tree.cpp
@@ -17,7 +17,7 @@
#include <algorithm>
// This array provides strings for each symbol in enum parse_token_type_t in parse_constants.h.
-wcstring parser_token_types[] = {
+const wchar_t * const token_type_map[] = {
L"token_type_invalid",
L"symbol_job_list",
L"symbol_job",
@@ -165,101 +165,16 @@ void parse_error_offset_source_start(parse_error_list_t *errors, size_t amt)
}
}
-/** Returns a string description of the given token type */
-wcstring token_type_description(parse_token_type_t type)
+// Returns a string description for the given token type.
+const wchar_t *token_type_description(parse_token_type_t type)
{
- switch (type)
- {
- case token_type_invalid:
- return L"invalid";
-
- case symbol_job_list:
- return L"job_list";
- case symbol_job:
- return L"job";
- case symbol_job_continuation:
- return L"job_continuation";
-
- case symbol_statement:
- return L"statement";
- case symbol_block_statement:
- return L"block_statement";
- case symbol_block_header:
- return L"block_header";
- case symbol_for_header:
- return L"for_header";
- case symbol_while_header:
- return L"while_header";
- case symbol_begin_header:
- return L"begin_header";
- case symbol_function_header:
- return L"function_header";
-
- case symbol_if_statement:
- return L"if_statement";
- case symbol_if_clause:
- return L"if_clause";
- case symbol_else_clause:
- return L"else_clause";
- case symbol_else_continuation:
- return L"else_continuation";
-
- case symbol_switch_statement:
- return L"switch_statement";
- case symbol_case_item_list:
- return L"case_item_list";
- case symbol_case_item:
- return L"case_item";
-
- case symbol_andor_job_list:
- return L"andor_job_list";
- case symbol_argument_list:
- return L"argument_list";
- case symbol_freestanding_argument_list:
- return L"freestanding_argument_list";
-
- case symbol_boolean_statement:
- return L"boolean_statement";
- case symbol_decorated_statement:
- return L"decorated_statement";
- case symbol_plain_statement:
- return L"plain_statement";
- case symbol_arguments_or_redirections_list:
- return L"arguments_or_redirections_list";
- case symbol_argument_or_redirection:
- return L"argument_or_redirection";
- case symbol_argument:
- return L"symbol_argument";
- case symbol_redirection:
- return L"symbol_redirection";
- case symbol_optional_background:
- return L"optional_background";
- case symbol_end_command:
- return L"symbol_end_command";
-
+ if (type >= 0 && type <= LAST_TOKEN_TYPE) return token_type_map[type];
- case parse_token_type_string:
- return L"token_string";
- case parse_token_type_pipe:
- return L"token_pipe";
- case parse_token_type_redirection:
- return L"token_redirection";
- case parse_token_type_background:
- return L"token_background";
- case parse_token_type_end:
- return L"token_end";
- case parse_token_type_terminate:
- return L"token_terminate";
-
- case parse_special_type_parse_error:
- return L"parse_error";
- case parse_special_type_tokenizer_error:
- return L"tokenizer_error";
- case parse_special_type_comment:
- return L"comment";
-
- }
- return format_string(L"Unknown token type %ld", static_cast<long>(type));
+ // This leaks memory but it should never be run unless we have a bug elsewhere in the code.
+ const wcstring d = format_string(L"unknown_token_type_%ld", static_cast<long>(type));
+ wchar_t *d2 = new wchar_t[d.size() + 1];
+ // cppcheck-suppress memleak
+ return std::wcscpy(d2, d.c_str());
}
#define LONGIFY(x) L ## x
@@ -291,23 +206,22 @@ keyword_map[] =
KEYWORD_MAP(while)
};
-wcstring keyword_description(parse_keyword_t k)
+const wchar_t *keyword_description(parse_keyword_t type)
{
- if (k >= 0 && k <= LAST_KEYWORD)
- {
- return keyword_map[k].name;
- }
- else
- {
- return format_string(L"Unknown keyword type %ld", static_cast<long>(k));
- }
+ if (type >= 0 && type <= LAST_KEYWORD) return keyword_map[type].name;
+
+ // This leaks memory but it should never be run unless we have a bug elsewhere in the code.
+ const wcstring d = format_string(L"unknown_keyword_%ld", static_cast<long>(type));
+ wchar_t *d2 = new wchar_t[d.size() + 1];
+ // cppcheck-suppress memleak
+ return std::wcscpy(d2, d.c_str());
}
static wcstring token_type_user_presentable_description(parse_token_type_t type, parse_keyword_t keyword = parse_keyword_none)
{
if (keyword != parse_keyword_none)
{
- return format_string(L"keyword '%ls'", keyword_description(keyword).c_str());
+ return format_string(L"keyword '%ls'", keyword_description(keyword));
}
switch (type)
@@ -338,7 +252,7 @@ static wcstring token_type_user_presentable_description(parse_token_type_t type,
return L"end of the input";
default:
- return format_string(L"a %ls", token_type_description(type).c_str());
+ return format_string(L"a %ls", token_type_description(type));
}
}
@@ -383,7 +297,7 @@ wcstring parse_token_t::describe() const
wcstring result = token_type_description(type);
if (keyword != parse_keyword_none)
{
- append_format(result, L" <%ls>", keyword_description(keyword).c_str());
+ append_format(result, L" <%ls>", keyword_description(keyword));
}
return result;
}
@@ -542,7 +456,7 @@ struct parse_stack_element_t
wcstring result = token_type_description(type);
if (keyword != parse_keyword_none)
{
- append_format(result, L" <%ls>", keyword_description(keyword).c_str());
+ append_format(result, L" <%ls>", keyword_description(keyword));
}
return result;
}
@@ -611,7 +525,8 @@ class parse_ll_t
{
parse_token_type_t type = production_element_type(elem);
parse_keyword_t keyword = production_element_keyword(elem);
- fprintf(stderr, "\t%ls <%ls>\n", token_type_description(type).c_str(), keyword_description(keyword).c_str());
+ fprintf(stderr, "\t%ls <%ls>\n", token_type_description(type),
+ keyword_description(keyword));
count++;
}
}
@@ -1078,9 +993,11 @@ bool parse_ll_t::top_node_handle_terminal_types(parse_token_t token)
if (matched)
{
- // Success. Tell the node that it matched this token, and what its source range is
- // In the parse phase, we only set source ranges for terminal types. We propagate ranges to parent nodes afterwards.
+ // Success. Tell the node that it matched this token, and what its source range is in
+ // the parse phase, we only set source ranges for terminal types. We propagate ranges to
+ // parent nodes afterwards.
parse_node_t &node = node_for_top_symbol();
+ node.keyword = token.keyword;
node.source_start = token.source_start;
node.source_length = token.source_length;
}
diff --git a/src/parse_tree.h b/src/parse_tree.h
index 67632685..8ae9ee88 100644
--- a/src/parse_tree.h
+++ b/src/parse_tree.h
@@ -63,8 +63,8 @@ typedef unsigned int parse_tree_flags_t;
wcstring parse_dump_tree(const parse_node_tree_t &tree, const wcstring &src);
-wcstring token_type_description(parse_token_type_t type);
-wcstring keyword_description(parse_keyword_t type);
+const wchar_t *token_type_description(parse_token_type_t type);
+const wchar_t *keyword_description(parse_keyword_t type);
/* Node flags */
enum
diff --git a/tests/indent.in b/tests/indent.in
index a1143c67..2d98dd8e 100644
--- a/tests/indent.in
+++ b/tests/indent.in
@@ -89,3 +89,18 @@ echo \nTest redir formatting
echo -n '
echo < stdin >>appended yes 2>&1 no > stdout maybe 2>& 4 | cat 2>| cat
' | ../test/root/bin/fish_indent
+
+echo \nTest normalization of keywords
+# issue 2921
+echo -n '
+i\
+f true
+ echo yes
+en\
+d
+
+"whil\
+e" true
+ "builtin" yes
+en"d"
+' | ../test/root/bin/fish_indent
diff --git a/tests/indent.out b/tests/indent.out
index 6b38a72f..5a4d9dc0 100644
--- a/tests/indent.out
+++ b/tests/indent.out
@@ -95,3 +95,13 @@ end
Test redir formatting
echo <stdin >>appended yes 2>&1 no >stdout maybe 2>&4 | cat 2>| cat
+
+Test normalization of keywords
+
+if true
+ echo yes
+end
+
+while true
+ builtin yes
+end
diff --git a/tests/interactive.fish b/tests/interactive.fish
index 9e0d344f..76b0562e 100644
--- a/tests/interactive.fish
+++ b/tests/interactive.fish
@@ -4,6 +4,9 @@
# should be running it via `make test` to ensure the environment is properly
# setup.
+# This is a list of flakey tests that often succeed when rerun.
+set TESTS_TO_RETRY bind.expect
+
# Change to directory containing this script
cd (dirname (status -f))
@@ -69,10 +72,19 @@ function test_file
end
end
-set -l failed
+set failed
for i in $files_to_test
if not test_file $i
- set failed $failed $i
+ # Retry flakey tests once.
+ if contains $i $TESTS_TO_RETRY
+ say -o cyan "Rerunning test $i since it is known to be flakey"
+ rm -f $i.tmp.*
+ if not test_file $i
+ set failed $failed $i
+ end
+ else
+ set failed $failed $i
+ end
end
end