mirror of
https://github.com/fish-shell/fish-shell.git
synced 2026-06-02 05:41:16 -03:00
Apply new indentation, brace, and whitespace style
This commit is contained in:
155
function.cpp
155
function.cpp
@@ -48,7 +48,8 @@ static function_map_t loaded_functions;
|
||||
static pthread_mutex_t functions_lock;
|
||||
|
||||
/* Autoloader for functions */
|
||||
class function_autoload_t : public autoload_t {
|
||||
class function_autoload_t : public autoload_t
|
||||
{
|
||||
public:
|
||||
function_autoload_t();
|
||||
virtual void command_removed(const wcstring &cmd);
|
||||
@@ -58,8 +59,8 @@ static function_autoload_t function_autoloader;
|
||||
|
||||
/** Constructor */
|
||||
function_autoload_t::function_autoload_t() : autoload_t(L"fish_function_path",
|
||||
internal_function_scripts,
|
||||
sizeof internal_function_scripts / sizeof *internal_function_scripts)
|
||||
internal_function_scripts,
|
||||
sizeof internal_function_scripts / sizeof *internal_function_scripts)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -67,7 +68,8 @@ function_autoload_t::function_autoload_t() : autoload_t(L"fish_function_path",
|
||||
static bool function_remove_ignore_autoload(const wcstring &name);
|
||||
|
||||
/** Callback when an autoloaded function is removed */
|
||||
void function_autoload_t::command_removed(const wcstring &cmd) {
|
||||
void function_autoload_t::command_removed(const wcstring &cmd)
|
||||
{
|
||||
function_remove_ignore_autoload(cmd);
|
||||
}
|
||||
|
||||
@@ -85,65 +87,66 @@ static bool is_autoload = false;
|
||||
Make sure that if the specified function is a dynamically loaded
|
||||
function, it has been fully loaded.
|
||||
*/
|
||||
static int load( const wcstring &name )
|
||||
static int load(const wcstring &name)
|
||||
{
|
||||
ASSERT_IS_MAIN_THREAD();
|
||||
scoped_lock lock(functions_lock);
|
||||
bool was_autoload = is_autoload;
|
||||
int res;
|
||||
bool was_autoload = is_autoload;
|
||||
int res;
|
||||
function_map_t::iterator iter = loaded_functions.find(name);
|
||||
if( iter != loaded_functions.end() && !iter->second.is_autoload ) {
|
||||
if (iter != loaded_functions.end() && !iter->second.is_autoload)
|
||||
{
|
||||
/* We have a non-autoload version already */
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
is_autoload = true;
|
||||
res = function_autoloader.load( name, true );
|
||||
is_autoload = was_autoload;
|
||||
return res;
|
||||
is_autoload = true;
|
||||
res = function_autoloader.load(name, true);
|
||||
is_autoload = was_autoload;
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
Insert a list of all dynamically loaded functions into the
|
||||
specified list.
|
||||
*/
|
||||
static void autoload_names( std::set<wcstring> &names, int get_hidden )
|
||||
static void autoload_names(std::set<wcstring> &names, int get_hidden)
|
||||
{
|
||||
size_t i;
|
||||
size_t i;
|
||||
|
||||
const env_var_t path_var_wstr = env_get_string( L"fish_function_path" );
|
||||
const env_var_t path_var_wstr = env_get_string(L"fish_function_path");
|
||||
if (path_var_wstr.missing())
|
||||
return;
|
||||
const wchar_t *path_var = path_var_wstr.c_str();
|
||||
const wchar_t *path_var = path_var_wstr.c_str();
|
||||
|
||||
wcstring_list_t path_list;
|
||||
|
||||
tokenize_variable_array( path_var, path_list );
|
||||
for( i=0; i<path_list.size(); i++ )
|
||||
{
|
||||
const wcstring &ndir_str = path_list.at(i);
|
||||
const wchar_t *ndir = (wchar_t *)ndir_str.c_str();
|
||||
DIR *dir = wopendir( ndir );
|
||||
if( !dir )
|
||||
continue;
|
||||
|
||||
wcstring name;
|
||||
while (wreaddir(dir, name))
|
||||
tokenize_variable_array(path_var, path_list);
|
||||
for (i=0; i<path_list.size(); i++)
|
||||
{
|
||||
const wchar_t *fn = name.c_str();
|
||||
const wchar_t *suffix;
|
||||
if( !get_hidden && fn[0] == L'_' )
|
||||
continue;
|
||||
const wcstring &ndir_str = path_list.at(i);
|
||||
const wchar_t *ndir = (wchar_t *)ndir_str.c_str();
|
||||
DIR *dir = wopendir(ndir);
|
||||
if (!dir)
|
||||
continue;
|
||||
|
||||
suffix = wcsrchr( fn, L'.' );
|
||||
if( suffix && (wcscmp( suffix, L".fish" ) == 0 ) )
|
||||
{
|
||||
wcstring name;
|
||||
while (wreaddir(dir, name))
|
||||
{
|
||||
const wchar_t *fn = name.c_str();
|
||||
const wchar_t *suffix;
|
||||
if (!get_hidden && fn[0] == L'_')
|
||||
continue;
|
||||
|
||||
suffix = wcsrchr(fn, L'.');
|
||||
if (suffix && (wcscmp(suffix, L".fish") == 0))
|
||||
{
|
||||
wcstring name(fn, suffix - fn);
|
||||
names.insert(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir(dir);
|
||||
}
|
||||
closedir(dir);
|
||||
}
|
||||
}
|
||||
|
||||
void function_init()
|
||||
@@ -178,16 +181,16 @@ function_info_t::function_info_t(const function_info_t &data, const wchar_t *fil
|
||||
{
|
||||
}
|
||||
|
||||
void function_add( const function_data_t &data, const parser_t &parser )
|
||||
void function_add(const function_data_t &data, const parser_t &parser)
|
||||
{
|
||||
ASSERT_IS_MAIN_THREAD();
|
||||
|
||||
CHECK( ! data.name.empty(), );
|
||||
CHECK( data.definition, );
|
||||
scoped_lock lock(functions_lock);
|
||||
CHECK(! data.name.empty(),);
|
||||
CHECK(data.definition,);
|
||||
scoped_lock lock(functions_lock);
|
||||
|
||||
/* Remove the old function */
|
||||
function_remove( data.name );
|
||||
function_remove(data.name);
|
||||
|
||||
|
||||
/* Create and store a new function */
|
||||
@@ -197,25 +200,25 @@ void function_add( const function_data_t &data, const parser_t &parser )
|
||||
loaded_functions.insert(new_pair);
|
||||
|
||||
/* Add event handlers */
|
||||
for( std::vector<event_t>::const_iterator iter = data.events.begin(); iter != data.events.end(); ++iter )
|
||||
{
|
||||
event_add_handler( &*iter );
|
||||
}
|
||||
for (std::vector<event_t>::const_iterator iter = data.events.begin(); iter != data.events.end(); ++iter)
|
||||
{
|
||||
event_add_handler(&*iter);
|
||||
}
|
||||
}
|
||||
|
||||
int function_exists( const wcstring &cmd )
|
||||
int function_exists(const wcstring &cmd)
|
||||
{
|
||||
if( parser_keywords_is_reserved(cmd) )
|
||||
return 0;
|
||||
if (parser_keywords_is_reserved(cmd))
|
||||
return 0;
|
||||
scoped_lock lock(functions_lock);
|
||||
load(cmd);
|
||||
return loaded_functions.find(cmd) != loaded_functions.end();
|
||||
}
|
||||
|
||||
int function_exists_no_autoload( const wcstring &cmd, const env_vars_snapshot_t &vars )
|
||||
int function_exists_no_autoload(const wcstring &cmd, const env_vars_snapshot_t &vars)
|
||||
{
|
||||
if( parser_keywords_is_reserved(cmd) )
|
||||
return 0;
|
||||
if (parser_keywords_is_reserved(cmd))
|
||||
return 0;
|
||||
scoped_lock lock(functions_lock);
|
||||
return loaded_functions.find(cmd) != loaded_functions.end() || function_autoloader.can_load(cmd, vars);
|
||||
}
|
||||
@@ -225,19 +228,20 @@ static bool function_remove_ignore_autoload(const wcstring &name)
|
||||
scoped_lock lock(functions_lock);
|
||||
bool erased = (loaded_functions.erase(name) > 0);
|
||||
|
||||
if (erased) {
|
||||
if (erased)
|
||||
{
|
||||
event_t ev(EVENT_ANY);
|
||||
ev.function_name=name;
|
||||
event_remove( &ev );
|
||||
event_remove(&ev);
|
||||
}
|
||||
return erased;
|
||||
|
||||
}
|
||||
|
||||
void function_remove( const wcstring &name )
|
||||
void function_remove(const wcstring &name)
|
||||
{
|
||||
if (function_remove_ignore_autoload(name))
|
||||
function_autoloader.unload( name );
|
||||
function_autoloader.unload(name);
|
||||
}
|
||||
|
||||
static const function_info_t *function_get(const wcstring &name)
|
||||
@@ -246,9 +250,12 @@ static const function_info_t *function_get(const wcstring &name)
|
||||
// We need a way to correctly check if a lock is locked (or better yet, make our lock non-recursive)
|
||||
//ASSERT_IS_LOCKED(functions_lock);
|
||||
function_map_t::iterator iter = loaded_functions.find(name);
|
||||
if (iter == loaded_functions.end()) {
|
||||
if (iter == loaded_functions.end())
|
||||
{
|
||||
return NULL;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
return &iter->second;
|
||||
}
|
||||
}
|
||||
@@ -257,7 +264,8 @@ bool function_get_definition(const wcstring &name, wcstring *out_definition)
|
||||
{
|
||||
scoped_lock lock(functions_lock);
|
||||
const function_info_t *func = function_get(name);
|
||||
if (func && out_definition) {
|
||||
if (func && out_definition)
|
||||
{
|
||||
out_definition->assign(func->definition);
|
||||
}
|
||||
return func != NULL;
|
||||
@@ -283,20 +291,24 @@ bool function_get_desc(const wcstring &name, wcstring *out_desc)
|
||||
/* Empty length string goes to NULL */
|
||||
scoped_lock lock(functions_lock);
|
||||
const function_info_t *func = function_get(name);
|
||||
if (out_desc && func && ! func->description.empty()) {
|
||||
if (out_desc && func && ! func->description.empty())
|
||||
{
|
||||
out_desc->assign(_(func->description.c_str()));
|
||||
return true;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void function_set_desc(const wcstring &name, const wcstring &desc)
|
||||
{
|
||||
load(name);
|
||||
load(name);
|
||||
scoped_lock lock(functions_lock);
|
||||
function_map_t::iterator iter = loaded_functions.find(name);
|
||||
if (iter != loaded_functions.end()) {
|
||||
if (iter != loaded_functions.end())
|
||||
{
|
||||
iter->second.description = desc;
|
||||
}
|
||||
}
|
||||
@@ -306,27 +318,30 @@ bool function_copy(const wcstring &name, const wcstring &new_name)
|
||||
bool result = false;
|
||||
scoped_lock lock(functions_lock);
|
||||
function_map_t::const_iterator iter = loaded_functions.find(name);
|
||||
if (iter != loaded_functions.end()) {
|
||||
// This new instance of the function shouldn't be tied to the definition file of the original, so pass NULL filename, etc.
|
||||
if (iter != loaded_functions.end())
|
||||
{
|
||||
// This new instance of the function shouldn't be tied to the definition file of the original, so pass NULL filename, etc.
|
||||
const function_map_t::value_type new_pair(new_name, function_info_t(iter->second, NULL, 0, false));
|
||||
loaded_functions.insert(new_pair);
|
||||
result = true;
|
||||
}
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
wcstring_list_t function_get_names(int get_hidden)
|
||||
{
|
||||
std::set<wcstring> names;
|
||||
scoped_lock lock(functions_lock);
|
||||
autoload_names(names, get_hidden);
|
||||
autoload_names(names, get_hidden);
|
||||
|
||||
function_map_t::const_iterator iter;
|
||||
for (iter = loaded_functions.begin(); iter != loaded_functions.end(); ++iter) {
|
||||
for (iter = loaded_functions.begin(); iter != loaded_functions.end(); ++iter)
|
||||
{
|
||||
const wcstring &name = iter->first;
|
||||
|
||||
/* Maybe skip hidden */
|
||||
if (! get_hidden) {
|
||||
if (! get_hidden)
|
||||
{
|
||||
if (name.empty() || name.at(0) == L'_') continue;
|
||||
}
|
||||
names.insert(name);
|
||||
|
||||
Reference in New Issue
Block a user