2012-11-18 10:23:22 +00:00
|
|
|
/** \file function.h
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
Prototypes for functions for storing and retrieving function
|
2012-11-18 10:23:22 +00:00
|
|
|
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.
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
|
|
|
|
2005-10-04 15:11:39 +00:00
|
|
|
#ifndef FISH_FUNCTION_H
|
|
|
|
#define FISH_FUNCTION_H
|
|
|
|
|
|
|
|
#include <wchar.h>
|
2014-10-02 22:59:24 +00:00
|
|
|
#include <map>
|
2005-10-04 15:11:39 +00:00
|
|
|
|
|
|
|
#include "util.h"
|
2012-01-14 07:44:18 +00:00
|
|
|
#include "common.h"
|
2012-02-08 10:34:31 +00:00
|
|
|
#include "event.h"
|
2014-10-02 22:59:24 +00:00
|
|
|
#include "env.h"
|
2005-10-04 15:11:39 +00:00
|
|
|
|
2012-01-16 20:10:08 +00:00
|
|
|
class parser_t;
|
2012-07-21 03:39:31 +00:00
|
|
|
class env_vars_snapshot_t;
|
2012-01-16 20:10:08 +00:00
|
|
|
|
2007-04-22 22:10:33 +00:00
|
|
|
/**
|
2008-01-16 01:06:01 +00:00
|
|
|
Structure describing a function. This is used by the parser to
|
|
|
|
store data on a function while parsing it. It is not used
|
|
|
|
internally to store functions, the function_internal_data_t
|
|
|
|
structure is used for that purpose. Parhaps these two should be
|
|
|
|
merged.
|
|
|
|
*/
|
2012-02-08 10:34:31 +00:00
|
|
|
struct function_data_t
|
2007-04-22 22:10:33 +00:00
|
|
|
{
|
2012-11-19 00:30:30 +00:00
|
|
|
/**
|
|
|
|
Name of function
|
|
|
|
*/
|
|
|
|
wcstring name;
|
|
|
|
/**
|
|
|
|
Description of function
|
|
|
|
*/
|
|
|
|
wcstring description;
|
|
|
|
/**
|
|
|
|
Function definition
|
|
|
|
*/
|
2013-10-12 19:04:31 +00:00
|
|
|
const wchar_t *definition;
|
2012-11-19 00:30:30 +00:00
|
|
|
/**
|
|
|
|
List of all event handlers for this function
|
|
|
|
*/
|
|
|
|
std::vector<event_t> events;
|
|
|
|
/**
|
|
|
|
List of all named arguments for this function
|
|
|
|
*/
|
|
|
|
wcstring_list_t named_arguments;
|
2014-10-02 22:59:24 +00:00
|
|
|
/**
|
|
|
|
List of all variables that are inherited from the function definition scope.
|
|
|
|
The variable values are snapshotted when function_add() is called.
|
|
|
|
*/
|
|
|
|
wcstring_list_t inherit_vars;
|
2012-11-19 00:30:30 +00:00
|
|
|
/**
|
|
|
|
Set to non-zero if invoking this function shadows the variables
|
|
|
|
of the underlying function.
|
|
|
|
*/
|
|
|
|
int shadows;
|
2012-02-08 10:34:31 +00:00
|
|
|
};
|
2012-01-24 04:32:36 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
class function_info_t
|
|
|
|
{
|
2012-01-24 04:32:36 +00:00
|
|
|
public:
|
2012-03-03 23:20:30 +00:00
|
|
|
/** Constructs relevant information from the function_data */
|
|
|
|
function_info_t(const function_data_t &data, const wchar_t *filename, int def_offset, bool autoload);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-03-03 23:20:30 +00:00
|
|
|
/** Used by function_copy */
|
|
|
|
function_info_t(const function_info_t &data, const wchar_t *filename, int def_offset, bool autoload);
|
|
|
|
|
2012-01-24 04:32:36 +00:00
|
|
|
/** Function definition */
|
2012-03-03 23:20:30 +00:00
|
|
|
const wcstring definition;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-03-03 23:20:30 +00:00
|
|
|
/** Function description. Only the description may be changed after the function is created. */
|
2012-01-24 04:32:36 +00:00
|
|
|
wcstring description;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
/** File where this function was defined (intern'd string) */
|
2012-03-03 23:20:30 +00:00
|
|
|
const wchar_t * const definition_file;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
/** Line where definition started */
|
|
|
|
const int definition_offset;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
/** List of all named arguments for this function */
|
2012-03-03 23:20:30 +00:00
|
|
|
const wcstring_list_t named_arguments;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2014-10-02 22:59:24 +00:00
|
|
|
/** Mapping of all variables that were inherited from the function definition scope to their values */
|
|
|
|
const std::map<wcstring,env_var_t> inherit_vars;
|
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
/** Flag for specifying that this function was automatically loaded */
|
2012-03-03 23:20:30 +00:00
|
|
|
const bool is_autoload;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
/** Set to true if invoking this function shadows the variables of the underlying function. */
|
|
|
|
const bool shadows;
|
2012-01-24 04:32:36 +00:00
|
|
|
};
|
2007-04-22 22:10:33 +00:00
|
|
|
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/**
|
2012-11-18 10:23:22 +00:00
|
|
|
Initialize function data
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
|
|
|
void function_init();
|
2006-11-15 14:16:49 +00:00
|
|
|
|
2014-03-16 23:45:00 +00:00
|
|
|
/** Add a function. definition_line_offset is the line number of the function's definition within its source file */
|
|
|
|
void function_add(const function_data_t &data, const parser_t &parser, int definition_line_offset = 0);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2013-10-12 19:55:11 +00:00
|
|
|
/** Removes a function from our internal table, returning true if it was found and false if not */
|
|
|
|
bool function_remove_ignore_autoload(const wcstring &name);
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/**
|
|
|
|
Remove the function with the specified name.
|
|
|
|
*/
|
2012-11-19 00:30:30 +00:00
|
|
|
void function_remove(const wcstring &name);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-01-24 04:32:36 +00:00
|
|
|
/**
|
2012-05-18 02:37:46 +00:00
|
|
|
Returns by reference the definition of the function with the name \c name.
|
|
|
|
Returns true if successful, false if no function with the given name exists.
|
2012-01-24 04:32:36 +00:00
|
|
|
*/
|
2012-11-19 00:30:30 +00:00
|
|
|
bool function_get_definition(const wcstring &name, wcstring *out_definition);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
/**
|
2012-05-18 02:46:08 +00:00
|
|
|
Returns by reference the description of the function with the name \c name.
|
|
|
|
Returns true if the function exists and has a nonempty description, false if it does not.
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
2012-11-19 00:30:30 +00:00
|
|
|
bool function_get_desc(const wcstring &name, wcstring *out_desc);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Sets the description of the function with the name \c name.
|
|
|
|
*/
|
2012-11-19 00:30:30 +00:00
|
|
|
void function_set_desc(const wcstring &name, const wcstring &desc);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
/**
|
2006-11-15 14:16:49 +00:00
|
|
|
Returns true if the function with the name name exists.
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
2012-11-19 00:30:30 +00:00
|
|
|
int function_exists(const wcstring &name);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
/**
|
|
|
|
Returns true if the function with the name name exists, without triggering autoload.
|
|
|
|
*/
|
2012-11-19 00:30:30 +00:00
|
|
|
int function_exists_no_autoload(const wcstring &name, const env_vars_snapshot_t &vars);
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/**
|
2012-01-14 07:44:18 +00:00
|
|
|
Returns all function names.
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
\param get_hidden whether to include hidden functions, i.e. ones starting with an underscore
|
|
|
|
*/
|
2012-11-19 00:30:30 +00:00
|
|
|
wcstring_list_t function_get_names(int get_hidden);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2006-02-08 17:37:18 +00:00
|
|
|
/**
|
|
|
|
Returns tha absolute path of the file where the specified function
|
|
|
|
was defined. Returns 0 if the file was defined on the commandline.
|
2006-11-15 14:16:49 +00:00
|
|
|
|
|
|
|
This function does not autoload functions, it will only work on
|
|
|
|
functions that have already been defined.
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-05-18 02:46:08 +00:00
|
|
|
This returns an intern'd string.
|
2006-02-08 17:37:18 +00:00
|
|
|
*/
|
2012-11-19 00:30:30 +00:00
|
|
|
const wchar_t *function_get_definition_file(const wcstring &name);
|
2006-01-26 14:48:10 +00:00
|
|
|
|
2006-02-08 17:37:18 +00:00
|
|
|
/**
|
2006-11-15 14:16:49 +00:00
|
|
|
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.
|
2006-02-08 17:37:18 +00:00
|
|
|
*/
|
2012-11-19 00:30:30 +00:00
|
|
|
int function_get_definition_offset(const wcstring &name);
|
2006-01-26 14:48:10 +00:00
|
|
|
|
2007-04-16 20:06:11 +00:00
|
|
|
/**
|
|
|
|
Returns a list of all named arguments of the specified function.
|
|
|
|
*/
|
2012-11-19 00:30:30 +00:00
|
|
|
wcstring_list_t function_get_named_arguments(const wcstring &name);
|
2007-04-16 20:06:11 +00:00
|
|
|
|
2014-10-02 22:59:24 +00:00
|
|
|
/**
|
|
|
|
Returns a mapping of all variables of the specified function that were inherited
|
|
|
|
from the scope of the function definition to their values.
|
|
|
|
*/
|
|
|
|
std::map<wcstring,env_var_t> function_get_inherit_vars(const wcstring &name);
|
|
|
|
|
2010-09-07 17:31:05 +00:00
|
|
|
/**
|
|
|
|
Creates a new function using the same definition as the specified function.
|
2012-03-03 23:20:30 +00:00
|
|
|
Returns true if copy is successful.
|
2010-09-07 17:31:05 +00:00
|
|
|
*/
|
2012-11-19 00:30:30 +00:00
|
|
|
bool function_copy(const wcstring &name, const wcstring &new_name);
|
2010-09-07 17:31:05 +00:00
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns whether this function shadows variables of the underlying function
|
|
|
|
*/
|
2012-11-19 00:30:30 +00:00
|
|
|
int function_get_shadows(const wcstring &name);
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-10-04 15:11:39 +00:00
|
|
|
#endif
|