mirror of
https://github.com/fish-shell/fish-shell.git
synced 2025-02-18 04:32:44 +08:00
restyle function module to match project style
Reduces lint errors from 39 to 27 (-31%). Line count from 619 to 498 (-20%). Another step in resolving issue #2902.
This commit is contained in:
parent
075811e588
commit
d3f155d895
360
src/function.cpp
360
src/function.cpp
|
@ -1,96 +1,76 @@
|
||||||
/** \file function.c
|
// Functions for storing and retrieving function information. These functions also take care of
|
||||||
|
// autoloading functions in the $fish_function_path. Actual function evaluation is taken care of by
|
||||||
Prototypes for functions for storing and retrieving function
|
// the parser and to some degree the builtin handling library.
|
||||||
information. These functions also take care of autoloading
|
//
|
||||||
functions in the $fish_function_path. Actual function evaluation
|
|
||||||
is taken care of by the parser and to some degree the builtin
|
|
||||||
handling library.
|
|
||||||
*/
|
|
||||||
// IWYU pragma: no_include <type_traits>
|
// IWYU pragma: no_include <type_traits>
|
||||||
#include <wchar.h>
|
|
||||||
#include <pthread.h>
|
|
||||||
#include <map>
|
|
||||||
#include <set>
|
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
|
#include <pthread.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <wchar.h>
|
||||||
|
#include <map>
|
||||||
|
#include <memory>
|
||||||
|
#include <set>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
#include "wutil.h" // IWYU pragma: keep
|
|
||||||
#include "fallback.h" // IWYU pragma: keep
|
|
||||||
#include "autoload.h"
|
#include "autoload.h"
|
||||||
#include "function.h"
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "intern.h"
|
|
||||||
#include "event.h"
|
|
||||||
#include "reader.h"
|
|
||||||
#include "parser_keywords.h"
|
|
||||||
#include "env.h"
|
#include "env.h"
|
||||||
|
#include "event.h"
|
||||||
|
#include "fallback.h" // IWYU pragma: keep
|
||||||
|
#include "function.h"
|
||||||
|
#include "intern.h"
|
||||||
|
#include "parser_keywords.h"
|
||||||
|
#include "reader.h"
|
||||||
|
#include "wutil.h" // IWYU pragma: keep
|
||||||
|
|
||||||
/**
|
/// Table containing all functions.
|
||||||
Table containing all functions
|
|
||||||
*/
|
|
||||||
typedef std::map<wcstring, function_info_t> function_map_t;
|
typedef std::map<wcstring, function_info_t> function_map_t;
|
||||||
static function_map_t loaded_functions;
|
static function_map_t loaded_functions;
|
||||||
|
|
||||||
/**
|
/// Functions that shouldn't be autoloaded (anymore).
|
||||||
Functions that shouldn't be autoloaded (anymore).
|
|
||||||
*/
|
|
||||||
static std::set<wcstring> function_tombstones;
|
static std::set<wcstring> function_tombstones;
|
||||||
|
|
||||||
/* Lock for functions */
|
/// Lock for functions.
|
||||||
static pthread_mutex_t functions_lock;
|
static pthread_mutex_t functions_lock;
|
||||||
|
|
||||||
/* Autoloader for functions */
|
/// Autoloader for functions.
|
||||||
class function_autoload_t : public autoload_t
|
class function_autoload_t : public autoload_t {
|
||||||
{
|
public:
|
||||||
public:
|
|
||||||
function_autoload_t();
|
function_autoload_t();
|
||||||
virtual void command_removed(const wcstring &cmd);
|
virtual void command_removed(const wcstring &cmd);
|
||||||
};
|
};
|
||||||
|
|
||||||
static function_autoload_t function_autoloader;
|
static function_autoload_t function_autoloader;
|
||||||
|
|
||||||
/** Constructor */
|
/// Constructor
|
||||||
function_autoload_t::function_autoload_t() : autoload_t(L"fish_function_path", NULL, 0)
|
function_autoload_t::function_autoload_t() : autoload_t(L"fish_function_path", NULL, 0) {}
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool function_remove_ignore_autoload(const wcstring &name, bool tombstone = true);
|
static bool function_remove_ignore_autoload(const wcstring &name, bool tombstone = true);
|
||||||
|
|
||||||
/** Callback when an autoloaded function is removed */
|
/// 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, false);
|
function_remove_ignore_autoload(cmd, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// Kludgy flag set by the load function in order to tell function_add that the function being
|
||||||
Kludgy flag set by the load function in order to tell function_add
|
/// defined is autoloaded. There should be a better way to do this...
|
||||||
that the function being defined is autoloaded. There should be a
|
|
||||||
better way to do this...
|
|
||||||
*/
|
|
||||||
static bool is_autoload = false;
|
static bool is_autoload = false;
|
||||||
|
|
||||||
/**
|
/// Make sure that if the specified function is a dynamically loaded function, it has been fully
|
||||||
Make sure that if the specified function is a dynamically loaded
|
/// loaded.
|
||||||
function, it has been fully loaded.
|
static int load(const wcstring &name) {
|
||||||
*/
|
|
||||||
static int load(const wcstring &name)
|
|
||||||
{
|
|
||||||
ASSERT_IS_MAIN_THREAD();
|
ASSERT_IS_MAIN_THREAD();
|
||||||
scoped_lock lock(functions_lock);
|
scoped_lock lock(functions_lock);
|
||||||
bool was_autoload = is_autoload;
|
bool was_autoload = is_autoload;
|
||||||
int res;
|
int res;
|
||||||
|
|
||||||
bool no_more_autoload = function_tombstones.count(name) > 0;
|
bool no_more_autoload = function_tombstones.count(name) > 0;
|
||||||
if (no_more_autoload)
|
if (no_more_autoload) return 0;
|
||||||
return 0;
|
|
||||||
|
|
||||||
function_map_t::iterator iter = loaded_functions.find(name);
|
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.
|
||||||
/* We have a non-autoload version already */
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,41 +80,31 @@ static int load(const wcstring &name)
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// Insert a list of all dynamically loaded functions into the specified list.
|
||||||
Insert a list of all dynamically loaded functions into the
|
static void autoload_names(std::set<wcstring> &names, int get_hidden) {
|
||||||
specified list.
|
|
||||||
*/
|
|
||||||
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())
|
if (path_var_wstr.missing()) return;
|
||||||
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;
|
wcstring_list_t path_list;
|
||||||
|
|
||||||
tokenize_variable_array(path_var, path_list);
|
tokenize_variable_array(path_var, path_list);
|
||||||
for (i=0; i<path_list.size(); i++)
|
for (i = 0; i < path_list.size(); i++) {
|
||||||
{
|
|
||||||
const wcstring &ndir_str = path_list.at(i);
|
const wcstring &ndir_str = path_list.at(i);
|
||||||
const wchar_t *ndir = (wchar_t *)ndir_str.c_str();
|
const wchar_t *ndir = (wchar_t *)ndir_str.c_str();
|
||||||
DIR *dir = wopendir(ndir);
|
DIR *dir = wopendir(ndir);
|
||||||
if (!dir)
|
if (!dir) continue;
|
||||||
continue;
|
|
||||||
|
|
||||||
wcstring name;
|
wcstring name;
|
||||||
while (wreaddir(dir, name))
|
while (wreaddir(dir, name)) {
|
||||||
{
|
|
||||||
const wchar_t *fn = name.c_str();
|
const wchar_t *fn = name.c_str();
|
||||||
const wchar_t *suffix;
|
const wchar_t *suffix;
|
||||||
if (!get_hidden && fn[0] == L'_')
|
if (!get_hidden && fn[0] == L'_') continue;
|
||||||
continue;
|
|
||||||
|
|
||||||
suffix = wcsrchr(fn, L'.');
|
suffix = wcsrchr(fn, L'.');
|
||||||
if (suffix && (wcscmp(suffix, L".fish") == 0))
|
if (suffix && (wcscmp(suffix, L".fish") == 0)) {
|
||||||
{
|
|
||||||
wcstring name(fn, suffix - fn);
|
wcstring name(fn, suffix - fn);
|
||||||
names.insert(name);
|
names.insert(name);
|
||||||
}
|
}
|
||||||
|
@ -143,236 +113,202 @@ static void autoload_names(std::set<wcstring> &names, int get_hidden)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void function_init()
|
void function_init() {
|
||||||
{
|
// PCA: This recursive lock was introduced early in my work. I would like to make this a
|
||||||
/* PCA: This recursive lock was introduced early in my work. I would like to make this a non-recursive lock but I haven't fully investigated all the call paths (for autoloading functions, etc.) */
|
// non-recursive lock but I haven't fully investigated all the call paths (for autoloading
|
||||||
|
// functions, etc.).
|
||||||
pthread_mutexattr_t a;
|
pthread_mutexattr_t a;
|
||||||
VOMIT_ON_FAILURE(pthread_mutexattr_init(&a));
|
VOMIT_ON_FAILURE(pthread_mutexattr_init(&a));
|
||||||
VOMIT_ON_FAILURE(pthread_mutexattr_settype(&a,PTHREAD_MUTEX_RECURSIVE));
|
VOMIT_ON_FAILURE(pthread_mutexattr_settype(&a, PTHREAD_MUTEX_RECURSIVE));
|
||||||
VOMIT_ON_FAILURE(pthread_mutex_init(&functions_lock, &a));
|
VOMIT_ON_FAILURE(pthread_mutex_init(&functions_lock, &a));
|
||||||
VOMIT_ON_FAILURE(pthread_mutexattr_destroy(&a));
|
VOMIT_ON_FAILURE(pthread_mutexattr_destroy(&a));
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::map<wcstring,env_var_t> snapshot_vars(const wcstring_list_t &vars)
|
static std::map<wcstring, env_var_t> snapshot_vars(const wcstring_list_t &vars) {
|
||||||
{
|
std::map<wcstring, env_var_t> result;
|
||||||
std::map<wcstring,env_var_t> result;
|
for (wcstring_list_t::const_iterator it = vars.begin(), end = vars.end(); it != end; ++it) {
|
||||||
for (wcstring_list_t::const_iterator it = vars.begin(), end = vars.end(); it != end; ++it)
|
|
||||||
{
|
|
||||||
result.insert(std::make_pair(*it, env_get_string(*it)));
|
result.insert(std::make_pair(*it, env_get_string(*it)));
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function_info_t::function_info_t(const function_data_t &data, const wchar_t *filename, int def_offset, bool autoload) :
|
function_info_t::function_info_t(const function_data_t &data, const wchar_t *filename,
|
||||||
definition(data.definition),
|
int def_offset, bool autoload)
|
||||||
description(data.description),
|
: definition(data.definition),
|
||||||
definition_file(intern(filename)),
|
description(data.description),
|
||||||
definition_offset(def_offset),
|
definition_file(intern(filename)),
|
||||||
named_arguments(data.named_arguments),
|
definition_offset(def_offset),
|
||||||
inherit_vars(snapshot_vars(data.inherit_vars)),
|
named_arguments(data.named_arguments),
|
||||||
is_autoload(autoload),
|
inherit_vars(snapshot_vars(data.inherit_vars)),
|
||||||
shadows(data.shadows)
|
is_autoload(autoload),
|
||||||
{
|
shadows(data.shadows) {}
|
||||||
}
|
|
||||||
|
|
||||||
function_info_t::function_info_t(const function_info_t &data, const wchar_t *filename, int def_offset, bool autoload) :
|
function_info_t::function_info_t(const function_info_t &data, const wchar_t *filename,
|
||||||
definition(data.definition),
|
int def_offset, bool autoload)
|
||||||
description(data.description),
|
: definition(data.definition),
|
||||||
definition_file(intern(filename)),
|
description(data.description),
|
||||||
definition_offset(def_offset),
|
definition_file(intern(filename)),
|
||||||
named_arguments(data.named_arguments),
|
definition_offset(def_offset),
|
||||||
inherit_vars(data.inherit_vars),
|
named_arguments(data.named_arguments),
|
||||||
is_autoload(autoload),
|
inherit_vars(data.inherit_vars),
|
||||||
shadows(data.shadows)
|
is_autoload(autoload),
|
||||||
{
|
shadows(data.shadows) {}
|
||||||
}
|
|
||||||
|
|
||||||
void function_add(const function_data_t &data, const parser_t &parser, int definition_line_offset)
|
void function_add(const function_data_t &data, const parser_t &parser, int definition_line_offset) {
|
||||||
{
|
|
||||||
ASSERT_IS_MAIN_THREAD();
|
ASSERT_IS_MAIN_THREAD();
|
||||||
|
|
||||||
CHECK(! data.name.empty(),);
|
CHECK(!data.name.empty(), );
|
||||||
CHECK(data.definition,);
|
CHECK(data.definition, );
|
||||||
scoped_lock lock(functions_lock);
|
scoped_lock lock(functions_lock);
|
||||||
|
|
||||||
/* Remove the old function */
|
// Remove the old function.
|
||||||
function_remove(data.name);
|
function_remove(data.name);
|
||||||
|
|
||||||
/* Create and store a new function */
|
// Create and store a new function.
|
||||||
const wchar_t *filename = reader_current_filename();
|
const wchar_t *filename = reader_current_filename();
|
||||||
|
|
||||||
const function_map_t::value_type new_pair(data.name, function_info_t(data, filename, definition_line_offset, is_autoload));
|
const function_map_t::value_type new_pair(
|
||||||
|
data.name, function_info_t(data, filename, definition_line_offset, is_autoload));
|
||||||
loaded_functions.insert(new_pair);
|
loaded_functions.insert(new_pair);
|
||||||
|
|
||||||
/* Add event handlers */
|
// Add event handlers.
|
||||||
for (std::vector<event_t>::const_iterator iter = data.events.begin(); iter != data.events.end(); ++iter)
|
for (std::vector<event_t>::const_iterator iter = data.events.begin(); iter != data.events.end();
|
||||||
{
|
++iter) {
|
||||||
event_add_handler(*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);
|
scoped_lock lock(functions_lock);
|
||||||
load(cmd);
|
load(cmd);
|
||||||
return loaded_functions.find(cmd) != loaded_functions.end();
|
return loaded_functions.find(cmd) != loaded_functions.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
void function_load(const wcstring &cmd)
|
void function_load(const wcstring &cmd) {
|
||||||
{
|
if (!parser_keywords_is_reserved(cmd)) {
|
||||||
if (! parser_keywords_is_reserved(cmd))
|
|
||||||
{
|
|
||||||
scoped_lock lock(functions_lock);
|
scoped_lock lock(functions_lock);
|
||||||
load(cmd);
|
load(cmd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
scoped_lock lock(functions_lock);
|
||||||
return loaded_functions.find(cmd) != loaded_functions.end() || function_autoloader.can_load(cmd, vars);
|
return loaded_functions.find(cmd) != loaded_functions.end() ||
|
||||||
|
function_autoloader.can_load(cmd, vars);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool function_remove_ignore_autoload(const wcstring &name, bool tombstone)
|
static bool function_remove_ignore_autoload(const wcstring &name, bool tombstone) {
|
||||||
{
|
// Note: the lock may be held at this point, but is recursive.
|
||||||
// Note: the lock may be held at this point, but is recursive
|
|
||||||
scoped_lock lock(functions_lock);
|
scoped_lock lock(functions_lock);
|
||||||
|
|
||||||
function_map_t::iterator iter = loaded_functions.find(name);
|
function_map_t::iterator iter = loaded_functions.find(name);
|
||||||
|
|
||||||
// not found. not erasing.
|
// Not found. Not erasing.
|
||||||
if (iter == loaded_functions.end())
|
if (iter == loaded_functions.end()) return false;
|
||||||
return false;
|
|
||||||
|
|
||||||
// removing an auto-loaded function. prevent it from being
|
// Removing an auto-loaded function. Prevent it from being auto-reloaded.
|
||||||
// auto-reloaded.
|
if (iter->second.is_autoload && tombstone) function_tombstones.insert(name);
|
||||||
if (iter->second.is_autoload && tombstone)
|
|
||||||
function_tombstones.insert(name);
|
|
||||||
|
|
||||||
loaded_functions.erase(iter);
|
loaded_functions.erase(iter);
|
||||||
|
|
||||||
event_t ev(EVENT_ANY);
|
event_t ev(EVENT_ANY);
|
||||||
ev.function_name=name;
|
ev.function_name = name;
|
||||||
event_remove(ev);
|
event_remove(ev);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void function_remove(const wcstring &name)
|
void function_remove(const wcstring &name) {
|
||||||
{
|
if (function_remove_ignore_autoload(name)) function_autoloader.unload(name);
|
||||||
if (function_remove_ignore_autoload(name))
|
|
||||||
function_autoloader.unload(name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static const function_info_t *function_get(const wcstring &name)
|
static const function_info_t *function_get(const wcstring &name) {
|
||||||
{
|
// The caller must lock the functions_lock before calling this; however our mutex is currently
|
||||||
// The caller must lock the functions_lock before calling this; however our mutex is currently recursive, so trylock will never fail
|
// recursive, so trylock will never fail. We need a way to correctly check if a lock is locked
|
||||||
// We need a way to correctly check if a lock is locked (or better yet, make our lock non-recursive)
|
// (or better yet, make our lock non-recursive).
|
||||||
//ASSERT_IS_LOCKED(functions_lock);
|
// ASSERT_IS_LOCKED(functions_lock);
|
||||||
function_map_t::iterator iter = loaded_functions.find(name);
|
function_map_t::iterator iter = loaded_functions.find(name);
|
||||||
if (iter == loaded_functions.end())
|
if (iter == loaded_functions.end()) {
|
||||||
{
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
return &iter->second;
|
return &iter->second;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool function_get_definition(const wcstring &name, wcstring *out_definition)
|
bool function_get_definition(const wcstring &name, wcstring *out_definition) {
|
||||||
{
|
|
||||||
scoped_lock lock(functions_lock);
|
scoped_lock lock(functions_lock);
|
||||||
const function_info_t *func = function_get(name);
|
const function_info_t *func = function_get(name);
|
||||||
if (func && out_definition)
|
if (func && out_definition) {
|
||||||
{
|
|
||||||
out_definition->assign(func->definition);
|
out_definition->assign(func->definition);
|
||||||
}
|
}
|
||||||
return func != NULL;
|
return func != NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
wcstring_list_t function_get_named_arguments(const wcstring &name)
|
wcstring_list_t function_get_named_arguments(const wcstring &name) {
|
||||||
{
|
|
||||||
scoped_lock lock(functions_lock);
|
scoped_lock lock(functions_lock);
|
||||||
const function_info_t *func = function_get(name);
|
const function_info_t *func = function_get(name);
|
||||||
return func ? func->named_arguments : wcstring_list_t();
|
return func ? func->named_arguments : wcstring_list_t();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map<wcstring,env_var_t> function_get_inherit_vars(const wcstring &name)
|
std::map<wcstring, env_var_t> function_get_inherit_vars(const wcstring &name) {
|
||||||
{
|
|
||||||
scoped_lock lock(functions_lock);
|
scoped_lock lock(functions_lock);
|
||||||
const function_info_t *func = function_get(name);
|
const function_info_t *func = function_get(name);
|
||||||
return func ? func->inherit_vars : std::map<wcstring,env_var_t>();
|
return func ? func->inherit_vars : std::map<wcstring, env_var_t>();
|
||||||
}
|
}
|
||||||
|
|
||||||
int function_get_shadows(const wcstring &name)
|
int function_get_shadows(const wcstring &name) {
|
||||||
{
|
|
||||||
scoped_lock lock(functions_lock);
|
scoped_lock lock(functions_lock);
|
||||||
const function_info_t *func = function_get(name);
|
const function_info_t *func = function_get(name);
|
||||||
return func ? func->shadows : false;
|
return func ? func->shadows : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool function_get_desc(const wcstring &name, wcstring *out_desc) {
|
||||||
bool function_get_desc(const wcstring &name, wcstring *out_desc)
|
// Empty length string goes to NULL.
|
||||||
{
|
|
||||||
/* Empty length string goes to NULL */
|
|
||||||
scoped_lock lock(functions_lock);
|
scoped_lock lock(functions_lock);
|
||||||
const function_info_t *func = function_get(name);
|
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()));
|
out_desc->assign(_(func->description.c_str()));
|
||||||
return true;
|
return true;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void function_set_desc(const wcstring &name, const wcstring &desc)
|
void function_set_desc(const wcstring &name, const wcstring &desc) {
|
||||||
{
|
|
||||||
load(name);
|
load(name);
|
||||||
scoped_lock lock(functions_lock);
|
scoped_lock lock(functions_lock);
|
||||||
function_map_t::iterator iter = loaded_functions.find(name);
|
function_map_t::iterator iter = loaded_functions.find(name);
|
||||||
if (iter != loaded_functions.end())
|
if (iter != loaded_functions.end()) {
|
||||||
{
|
|
||||||
iter->second.description = desc;
|
iter->second.description = desc;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool function_copy(const wcstring &name, const wcstring &new_name)
|
bool function_copy(const wcstring &name, const wcstring &new_name) {
|
||||||
{
|
|
||||||
bool result = false;
|
bool result = false;
|
||||||
scoped_lock lock(functions_lock);
|
scoped_lock lock(functions_lock);
|
||||||
function_map_t::const_iterator iter = loaded_functions.find(name);
|
function_map_t::const_iterator iter = loaded_functions.find(name);
|
||||||
if (iter != loaded_functions.end())
|
if (iter != loaded_functions.end()) {
|
||||||
{
|
// This new instance of the function shouldn't be tied to the definition file of the
|
||||||
// This new instance of the function shouldn't be tied to the definition file of the original, so pass NULL filename, etc.
|
// original, so pass NULL filename, etc.
|
||||||
const function_map_t::value_type new_pair(new_name, function_info_t(iter->second, NULL, 0, false));
|
const function_map_t::value_type new_pair(new_name,
|
||||||
|
function_info_t(iter->second, NULL, 0, false));
|
||||||
loaded_functions.insert(new_pair);
|
loaded_functions.insert(new_pair);
|
||||||
result = true;
|
result = true;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
wcstring_list_t function_get_names(int get_hidden)
|
wcstring_list_t function_get_names(int get_hidden) {
|
||||||
{
|
|
||||||
std::set<wcstring> names;
|
std::set<wcstring> names;
|
||||||
scoped_lock lock(functions_lock);
|
scoped_lock lock(functions_lock);
|
||||||
autoload_names(names, get_hidden);
|
autoload_names(names, get_hidden);
|
||||||
|
|
||||||
function_map_t::const_iterator iter;
|
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;
|
const wcstring &name = iter->first;
|
||||||
|
|
||||||
/* Maybe skip hidden */
|
// Maybe skip hidden.
|
||||||
if (! get_hidden)
|
if (!get_hidden) {
|
||||||
{
|
|
||||||
if (name.empty() || name.at(0) == L'_') continue;
|
if (name.empty() || name.at(0) == L'_') continue;
|
||||||
}
|
}
|
||||||
names.insert(name);
|
names.insert(name);
|
||||||
|
@ -380,46 +316,40 @@ wcstring_list_t function_get_names(int get_hidden)
|
||||||
return wcstring_list_t(names.begin(), names.end());
|
return wcstring_list_t(names.begin(), names.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
const wchar_t *function_get_definition_file(const wcstring &name)
|
const wchar_t *function_get_definition_file(const wcstring &name) {
|
||||||
{
|
|
||||||
scoped_lock lock(functions_lock);
|
scoped_lock lock(functions_lock);
|
||||||
const function_info_t *func = function_get(name);
|
const function_info_t *func = function_get(name);
|
||||||
return func ? func->definition_file : NULL;
|
return func ? func->definition_file : NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int function_get_definition_offset(const wcstring &name) {
|
||||||
int function_get_definition_offset(const wcstring &name)
|
|
||||||
{
|
|
||||||
scoped_lock lock(functions_lock);
|
scoped_lock lock(functions_lock);
|
||||||
const function_info_t *func = function_get(name);
|
const function_info_t *func = function_get(name);
|
||||||
return func ? func->definition_offset : -1;
|
return func ? func->definition_offset : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void function_prepare_environment(const wcstring &name, const wchar_t * const * argv, const std::map<wcstring, env_var_t> &inherited_vars)
|
void function_prepare_environment(const wcstring &name, const wchar_t *const *argv,
|
||||||
{
|
const std::map<wcstring, env_var_t> &inherited_vars) {
|
||||||
/* Three components of the environment:
|
// Three components of the environment:
|
||||||
1. argv
|
// 1. argv
|
||||||
2. named arguments
|
// 2. named arguments
|
||||||
3. inherited variables
|
// 3. inherited variables
|
||||||
*/
|
|
||||||
env_set_argv(argv);
|
env_set_argv(argv);
|
||||||
|
|
||||||
const wcstring_list_t named_arguments = function_get_named_arguments(name);
|
const wcstring_list_t named_arguments = function_get_named_arguments(name);
|
||||||
if (! named_arguments.empty())
|
if (!named_arguments.empty()) {
|
||||||
{
|
const wchar_t *const *arg;
|
||||||
const wchar_t * const *arg;
|
|
||||||
size_t i;
|
size_t i;
|
||||||
for (i=0, arg=argv; i < named_arguments.size(); i++)
|
for (i = 0, arg = argv; i < named_arguments.size(); i++) {
|
||||||
{
|
|
||||||
env_set(named_arguments.at(i).c_str(), *arg, ENV_LOCAL | ENV_USER);
|
env_set(named_arguments.at(i).c_str(), *arg, ENV_LOCAL | ENV_USER);
|
||||||
|
|
||||||
if (*arg)
|
if (*arg) arg++;
|
||||||
arg++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (std::map<wcstring,env_var_t>::const_iterator it = inherited_vars.begin(), end = inherited_vars.end(); it != end; ++it)
|
for (std::map<wcstring, env_var_t>::const_iterator it = inherited_vars.begin(),
|
||||||
{
|
end = inherited_vars.end();
|
||||||
|
it != end; ++it) {
|
||||||
env_set(it->first, it->second.missing() ? NULL : it->second.c_str(), ENV_LOCAL | ENV_USER);
|
env_set(it->first, it->second.missing() ? NULL : it->second.c_str(), ENV_LOCAL | ENV_USER);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
201
src/function.h
201
src/function.h
|
@ -1,194 +1,143 @@
|
||||||
/** \file function.h
|
// Prototypes for functions for storing and retrieving function information. These functions also
|
||||||
|
// take care of autoloading functions in the $fish_function_path. Actual function evaluation is
|
||||||
Prototypes for functions for storing and retrieving function
|
// taken care of by the parser and to some degree the builtin handling library.
|
||||||
information. These functions also take care of autoloading
|
|
||||||
functions in the $fish_function_path. Actual function evaluation
|
|
||||||
is taken care of by the parser and to some degree the builtin
|
|
||||||
handling library.
|
|
||||||
*/
|
|
||||||
#ifndef FISH_FUNCTION_H
|
#ifndef FISH_FUNCTION_H
|
||||||
#define FISH_FUNCTION_H
|
#define FISH_FUNCTION_H
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
#include <map>
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <map>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "event.h"
|
|
||||||
#include "env.h"
|
#include "env.h"
|
||||||
|
#include "event.h"
|
||||||
|
|
||||||
class parser_t;
|
class parser_t;
|
||||||
|
|
||||||
/**
|
/// Structure describing a function. This is used by the parser to store data on a function while
|
||||||
Structure describing a function. This is used by the parser to
|
/// parsing it. It is not used internally to store functions, the function_internal_data_t structure
|
||||||
store data on a function while parsing it. It is not used
|
/// is used for that purpose. Parhaps these two should be merged.
|
||||||
internally to store functions, the function_internal_data_t
|
struct function_data_t {
|
||||||
structure is used for that purpose. Parhaps these two should be
|
/// Name of function.
|
||||||
merged.
|
|
||||||
*/
|
|
||||||
struct function_data_t
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
Name of function
|
|
||||||
*/
|
|
||||||
wcstring name;
|
wcstring name;
|
||||||
/**
|
/// Description of function.
|
||||||
Description of function
|
|
||||||
*/
|
|
||||||
wcstring description;
|
wcstring description;
|
||||||
/**
|
/// Function definition.
|
||||||
Function definition
|
|
||||||
*/
|
|
||||||
const wchar_t *definition;
|
const wchar_t *definition;
|
||||||
/**
|
/// List of all event handlers for this function.
|
||||||
List of all event handlers for this function
|
|
||||||
*/
|
|
||||||
std::vector<event_t> events;
|
std::vector<event_t> events;
|
||||||
/**
|
/// List of all named arguments for this function.
|
||||||
List of all named arguments for this function
|
|
||||||
*/
|
|
||||||
wcstring_list_t named_arguments;
|
wcstring_list_t named_arguments;
|
||||||
/**
|
/// List of all variables that are inherited from the function definition scope. The variable
|
||||||
List of all variables that are inherited from the function definition scope.
|
/// values are snapshotted when function_add() is called.
|
||||||
The variable values are snapshotted when function_add() is called.
|
|
||||||
*/
|
|
||||||
wcstring_list_t inherit_vars;
|
wcstring_list_t inherit_vars;
|
||||||
/**
|
/// Set to non-zero if invoking this function shadows the variables of the underlying function.
|
||||||
Set to non-zero if invoking this function shadows the variables
|
|
||||||
of the underlying function.
|
|
||||||
*/
|
|
||||||
int shadows;
|
int shadows;
|
||||||
};
|
};
|
||||||
|
|
||||||
class function_info_t
|
class function_info_t {
|
||||||
{
|
public:
|
||||||
public:
|
/// Constructs relevant information from the function_data.
|
||||||
/** Constructs relevant information from the function_data */
|
function_info_t(const function_data_t &data, const wchar_t *filename, int def_offset,
|
||||||
function_info_t(const function_data_t &data, const wchar_t *filename, int def_offset, bool autoload);
|
bool autoload);
|
||||||
|
|
||||||
/** Used by function_copy */
|
/// Used by function_copy.
|
||||||
function_info_t(const function_info_t &data, const wchar_t *filename, int def_offset, bool autoload);
|
function_info_t(const function_info_t &data, const wchar_t *filename, int def_offset,
|
||||||
|
bool autoload);
|
||||||
|
|
||||||
/** Function definition */
|
/// Function definition.
|
||||||
const wcstring definition;
|
const wcstring definition;
|
||||||
|
|
||||||
/** Function description. Only the description may be changed after the function is created. */
|
/// Function description. Only the description may be changed after the function is created.
|
||||||
wcstring description;
|
wcstring description;
|
||||||
|
|
||||||
/** File where this function was defined (intern'd string) */
|
/// File where this function was defined (intern'd string).
|
||||||
const wchar_t * const definition_file;
|
const wchar_t *const definition_file;
|
||||||
|
|
||||||
/** Line where definition started */
|
/// Line where definition started.
|
||||||
const int definition_offset;
|
const int definition_offset;
|
||||||
|
|
||||||
/** List of all named arguments for this function */
|
/// List of all named arguments for this function.
|
||||||
const wcstring_list_t named_arguments;
|
const wcstring_list_t named_arguments;
|
||||||
|
|
||||||
/** Mapping of all variables that were inherited from the function definition scope to their values */
|
/// Mapping of all variables that were inherited from the function definition scope to their
|
||||||
const std::map<wcstring,env_var_t> inherit_vars;
|
/// values.
|
||||||
|
const std::map<wcstring, env_var_t> inherit_vars;
|
||||||
|
|
||||||
/** Flag for specifying that this function was automatically loaded */
|
/// Flag for specifying that this function was automatically loaded.
|
||||||
const bool is_autoload;
|
const bool is_autoload;
|
||||||
|
|
||||||
/** Set to true if invoking this function shadows the variables of the underlying function. */
|
/// Set to true if invoking this function shadows the variables of the underlying function.
|
||||||
const bool shadows;
|
const bool shadows;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// Initialize function data.
|
||||||
/**
|
|
||||||
Initialize function data
|
|
||||||
*/
|
|
||||||
void function_init();
|
void function_init();
|
||||||
|
|
||||||
/** Add a function. definition_line_offset is the line number of the function's definition within its source file */
|
/// Add a function. definition_line_offset is the line number of the function's definition within
|
||||||
void function_add(const function_data_t &data, const parser_t &parser, int definition_line_offset = 0);
|
/// its source file.
|
||||||
|
void function_add(const function_data_t &data, const parser_t &parser,
|
||||||
|
int definition_line_offset = 0);
|
||||||
|
|
||||||
/**
|
/// Remove the function with the specified name.
|
||||||
Remove the function with the specified name.
|
|
||||||
*/
|
|
||||||
void function_remove(const wcstring &name);
|
void function_remove(const wcstring &name);
|
||||||
|
|
||||||
/**
|
/// Returns by reference the definition of the function with the name \c name. Returns true if
|
||||||
Returns by reference the definition of the function with the name \c name.
|
/// successful, false if no function with the given name exists.
|
||||||
Returns true if successful, false if no function with the given name exists.
|
|
||||||
*/
|
|
||||||
bool function_get_definition(const wcstring &name, wcstring *out_definition);
|
bool function_get_definition(const wcstring &name, wcstring *out_definition);
|
||||||
|
|
||||||
/**
|
/// Returns by reference the description of the function with the name \c name. Returns true if the
|
||||||
Returns by reference the description of the function with the name \c name.
|
/// function exists and has a nonempty description, false if it does not.
|
||||||
Returns true if the function exists and has a nonempty description, false if it does not.
|
|
||||||
*/
|
|
||||||
bool function_get_desc(const wcstring &name, wcstring *out_desc);
|
bool function_get_desc(const wcstring &name, wcstring *out_desc);
|
||||||
|
|
||||||
/**
|
/// Sets the description of the function with the name \c name.
|
||||||
Sets the description of the function with the name \c name.
|
|
||||||
*/
|
|
||||||
void function_set_desc(const wcstring &name, const wcstring &desc);
|
void function_set_desc(const wcstring &name, const wcstring &desc);
|
||||||
|
|
||||||
/**
|
/// Returns true if the function with the name name exists.
|
||||||
Returns true if the function with the name name exists.
|
|
||||||
*/
|
|
||||||
int function_exists(const wcstring &name);
|
int function_exists(const wcstring &name);
|
||||||
|
|
||||||
/** Attempts to load a function if not yet loaded. This is used by the completion machinery. */
|
/// Attempts to load a function if not yet loaded. This is used by the completion machinery.
|
||||||
void function_load(const wcstring &name);
|
void function_load(const wcstring &name);
|
||||||
|
|
||||||
/**
|
/// Returns true if the function with the name name exists, without triggering autoload.
|
||||||
Returns true if the function with the name name exists, without triggering autoload.
|
|
||||||
*/
|
|
||||||
int function_exists_no_autoload(const wcstring &name, const env_vars_snapshot_t &vars);
|
int function_exists_no_autoload(const wcstring &name, const env_vars_snapshot_t &vars);
|
||||||
|
|
||||||
/**
|
/// Returns all function names.
|
||||||
Returns all function names.
|
///
|
||||||
|
/// \param get_hidden whether to include hidden functions, i.e. ones starting with an underscore.
|
||||||
\param get_hidden whether to include hidden functions, i.e. ones starting with an underscore
|
|
||||||
*/
|
|
||||||
wcstring_list_t function_get_names(int get_hidden);
|
wcstring_list_t function_get_names(int get_hidden);
|
||||||
|
|
||||||
/**
|
/// Returns tha absolute path of the file where the specified function was defined. Returns 0 if the
|
||||||
Returns tha absolute path of the file where the specified function
|
/// file was defined on the commandline.
|
||||||
was defined. Returns 0 if the file was defined on the commandline.
|
///
|
||||||
|
/// This function does not autoload functions, it will only work on functions that have already been
|
||||||
This function does not autoload functions, it will only work on
|
/// defined.
|
||||||
functions that have already been defined.
|
///
|
||||||
|
/// This returns an intern'd string.
|
||||||
This returns an intern'd string.
|
|
||||||
*/
|
|
||||||
const wchar_t *function_get_definition_file(const wcstring &name);
|
const wchar_t *function_get_definition_file(const wcstring &name);
|
||||||
|
|
||||||
/**
|
/// Returns the linenumber where the definition of the specified function started.
|
||||||
Returns the linenumber where the definition of the specified
|
///
|
||||||
function started.
|
/// This function does not autoload functions, it will only work on functions that have already been
|
||||||
|
/// defined.
|
||||||
This function does not autoload functions, it will only work on
|
|
||||||
functions that have already been defined.
|
|
||||||
*/
|
|
||||||
int function_get_definition_offset(const wcstring &name);
|
int function_get_definition_offset(const wcstring &name);
|
||||||
|
|
||||||
/**
|
/// Returns a list of all named arguments of the specified function.
|
||||||
Returns a list of all named arguments of the specified function.
|
|
||||||
*/
|
|
||||||
wcstring_list_t function_get_named_arguments(const wcstring &name);
|
wcstring_list_t function_get_named_arguments(const wcstring &name);
|
||||||
|
|
||||||
/**
|
/// Returns a mapping of all variables of the specified function that were inherited from the scope
|
||||||
Returns a mapping of all variables of the specified function that were inherited
|
/// of the function definition to their values.
|
||||||
from the scope of the function definition to their values.
|
std::map<wcstring, env_var_t> function_get_inherit_vars(const wcstring &name);
|
||||||
*/
|
|
||||||
std::map<wcstring,env_var_t> function_get_inherit_vars(const wcstring &name);
|
|
||||||
|
|
||||||
/**
|
/// Creates a new function using the same definition as the specified function. Returns true if copy
|
||||||
Creates a new function using the same definition as the specified function.
|
/// is successful.
|
||||||
Returns true if copy is successful.
|
|
||||||
*/
|
|
||||||
bool function_copy(const wcstring &name, const wcstring &new_name);
|
bool function_copy(const wcstring &name, const wcstring &new_name);
|
||||||
|
|
||||||
/**
|
/// Returns whether this function shadows variables of the underlying function.
|
||||||
Returns whether this function shadows variables of the underlying function
|
|
||||||
*/
|
|
||||||
int function_get_shadows(const wcstring &name);
|
int function_get_shadows(const wcstring &name);
|
||||||
|
|
||||||
/** Prepares the environment for executing a function.
|
/// Prepares the environment for executing a function.
|
||||||
*/
|
void function_prepare_environment(const wcstring &name, const wchar_t *const *argv,
|
||||||
void function_prepare_environment(const wcstring &name, const wchar_t * const * argv, const std::map<wcstring, env_var_t> &inherited_vars);
|
const std::map<wcstring, env_var_t> &inherited_vars);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue
Block a user