2005-09-20 13:26:39 +00:00
|
|
|
/** \file function.c
|
2006-11-15 14:16:49 +00:00
|
|
|
|
|
|
|
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 taken care of by the parser and to some degree the builtin
|
|
|
|
handling library.
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
2006-08-11 01:18:35 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
2006-01-20 14:27:21 +00:00
|
|
|
#include "wutil.h"
|
2006-02-28 13:17:16 +00:00
|
|
|
#include "fallback.h"
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "util.h"
|
2006-02-28 13:17:16 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "function.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#include "parser.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "intern.h"
|
2005-10-05 22:37:08 +00:00
|
|
|
#include "event.h"
|
2006-01-26 14:48:10 +00:00
|
|
|
#include "reader.h"
|
2006-02-05 13:10:35 +00:00
|
|
|
#include "parse_util.h"
|
2006-02-08 09:20:05 +00:00
|
|
|
#include "env.h"
|
2006-02-08 15:29:09 +00:00
|
|
|
#include "expand.h"
|
2006-11-15 14:16:49 +00:00
|
|
|
#include "halloc.h"
|
|
|
|
#include "halloc_util.h"
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Struct describing a function
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
/** Function definition */
|
|
|
|
wchar_t *cmd;
|
|
|
|
/** Function description */
|
|
|
|
wchar_t *desc;
|
2006-06-17 13:07:08 +00:00
|
|
|
/**
|
|
|
|
File where this function was defined
|
|
|
|
*/
|
2006-01-26 14:48:10 +00:00
|
|
|
const wchar_t *definition_file;
|
2006-06-17 13:07:08 +00:00
|
|
|
/**
|
|
|
|
Line where definition started
|
|
|
|
*/
|
2006-01-26 14:48:10 +00:00
|
|
|
int definition_offset;
|
2006-06-17 13:07:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Flag for specifying that this function was automatically loaded
|
|
|
|
*/
|
2006-02-08 09:20:05 +00:00
|
|
|
int is_autoload;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2005-10-05 22:37:08 +00:00
|
|
|
function_data_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Table containing all functions
|
|
|
|
*/
|
|
|
|
static hash_table_t function;
|
2006-06-17 13:07:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Kludgy flag set by the load function in order to tell function_add
|
|
|
|
that the function being defined is autoloaded. There should be a
|
|
|
|
better way to do this...
|
|
|
|
*/
|
2006-02-08 09:20:05 +00:00
|
|
|
static int is_autoload = 0;
|
|
|
|
|
2006-02-08 17:37:18 +00:00
|
|
|
/**
|
|
|
|
Make sure that if the specified function is a dynamically loaded
|
|
|
|
function, it has been fully loaded.
|
|
|
|
*/
|
2006-02-08 09:20:05 +00:00
|
|
|
static int load( const wchar_t *name )
|
|
|
|
{
|
|
|
|
int was_autoload = is_autoload;
|
|
|
|
int res;
|
|
|
|
function_data_t *data;
|
|
|
|
data = (function_data_t *)hash_get( &function, name );
|
|
|
|
if( data && !data->is_autoload )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
is_autoload = 1;
|
|
|
|
res = parse_util_load( name,
|
2006-02-19 17:01:16 +00:00
|
|
|
L"fish_function_path",
|
2006-02-08 09:20:05 +00:00
|
|
|
&function_remove,
|
|
|
|
1 );
|
|
|
|
is_autoload = was_autoload;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2006-02-08 17:37:18 +00:00
|
|
|
/**
|
|
|
|
Insert a list of all dynamically loaded functions into the
|
|
|
|
specified list.
|
|
|
|
*/
|
|
|
|
static void autoload_names( array_list_t *out, int get_hidden )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
array_list_t path_list;
|
|
|
|
const wchar_t *path_var = env_get( L"fish_function_path" );
|
|
|
|
|
|
|
|
if( ! path_var )
|
|
|
|
return;
|
|
|
|
|
|
|
|
al_init( &path_list );
|
|
|
|
|
2006-05-29 11:13:42 +00:00
|
|
|
tokenize_variable_array( path_var, &path_list );
|
2006-02-08 17:37:18 +00:00
|
|
|
for( i=0; i<al_get_count( &path_list ); i++ )
|
|
|
|
{
|
|
|
|
wchar_t *ndir = (wchar_t *)al_get( &path_list, i );
|
|
|
|
DIR *dir = wopendir( ndir );
|
2006-02-13 21:44:16 +00:00
|
|
|
if( !dir )
|
|
|
|
continue;
|
|
|
|
|
2006-02-08 17:37:18 +00:00
|
|
|
struct wdirent *next;
|
|
|
|
while( (next=wreaddir(dir))!=0 )
|
|
|
|
{
|
|
|
|
wchar_t *fn = next->d_name;
|
|
|
|
wchar_t *suffix;
|
|
|
|
if( !get_hidden && fn[0] == L'_' )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
suffix = wcsrchr( fn, L'.' );
|
|
|
|
if( suffix && (wcscmp( suffix, L".fish" ) == 0 ) )
|
|
|
|
{
|
2006-02-17 10:13:39 +00:00
|
|
|
const wchar_t *dup;
|
2006-02-08 17:37:18 +00:00
|
|
|
*suffix = 0;
|
2006-02-13 21:44:16 +00:00
|
|
|
dup = intern( fn );
|
2006-02-08 17:37:18 +00:00
|
|
|
if( !dup )
|
2006-07-03 10:39:57 +00:00
|
|
|
DIE_MEM();
|
2006-02-08 17:37:18 +00:00
|
|
|
al_push( out, dup );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
}
|
2006-06-12 21:47:42 +00:00
|
|
|
al_foreach( &path_list, &free );
|
2006-02-08 17:37:18 +00:00
|
|
|
al_destroy( &path_list );
|
2006-02-08 09:20:05 +00:00
|
|
|
}
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
void function_init()
|
|
|
|
{
|
|
|
|
hash_init( &function,
|
|
|
|
&hash_wcs_func,
|
|
|
|
&hash_wcs_cmp );
|
|
|
|
}
|
|
|
|
|
2006-11-15 14:16:49 +00:00
|
|
|
static void clear_entry( void *key, void *value )
|
|
|
|
{
|
|
|
|
halloc_free( value );
|
|
|
|
}
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
void function_destroy()
|
|
|
|
{
|
2006-11-15 14:16:49 +00:00
|
|
|
hash_foreach( &function, &clear_entry );
|
2005-09-20 13:26:39 +00:00
|
|
|
hash_destroy( &function );
|
|
|
|
}
|
|
|
|
|
2006-01-26 14:48:10 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
void function_add( const wchar_t *name,
|
|
|
|
const wchar_t *val,
|
2005-09-20 23:42:00 +00:00
|
|
|
const wchar_t *desc,
|
2006-10-25 20:28:36 +00:00
|
|
|
array_list_t *events )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2005-10-05 22:37:08 +00:00
|
|
|
int i;
|
2006-01-14 01:58:01 +00:00
|
|
|
wchar_t *cmd_end;
|
2006-01-30 16:51:50 +00:00
|
|
|
function_data_t *d;
|
2005-10-05 22:37:08 +00:00
|
|
|
|
2006-06-21 00:48:36 +00:00
|
|
|
CHECK( name, );
|
|
|
|
CHECK( val, );
|
2006-06-08 00:01:45 +00:00
|
|
|
|
2006-02-05 13:10:35 +00:00
|
|
|
function_remove( name );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2006-11-15 14:16:49 +00:00
|
|
|
d = halloc( 0, sizeof( function_data_t ) );
|
2006-06-07 23:32:06 +00:00
|
|
|
d->definition_offset = parse_util_lineno( parser_get_buffer(), current_block->tok_pos )-1;
|
2006-11-15 14:16:49 +00:00
|
|
|
d->cmd = halloc_wcsdup( d, val );
|
2006-02-05 13:10:35 +00:00
|
|
|
|
2006-01-14 01:58:01 +00:00
|
|
|
cmd_end = d->cmd + wcslen(d->cmd)-1;
|
|
|
|
|
2006-11-15 14:16:49 +00:00
|
|
|
d->desc = desc?halloc_wcsdup( d, desc ):0;
|
2006-02-05 13:10:35 +00:00
|
|
|
d->definition_file = intern(reader_current_filename());
|
2006-02-08 09:20:05 +00:00
|
|
|
d->is_autoload = is_autoload;
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
hash_put( &function, intern(name), d );
|
2006-01-26 14:48:10 +00:00
|
|
|
|
2005-10-05 22:37:08 +00:00
|
|
|
for( i=0; i<al_get_count( events ); i++ )
|
|
|
|
{
|
|
|
|
event_add_handler( (event_t *)al_get( events, i ) );
|
|
|
|
}
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int function_exists( const wchar_t *cmd )
|
|
|
|
{
|
2006-06-08 00:01:45 +00:00
|
|
|
|
2006-06-21 00:48:36 +00:00
|
|
|
CHECK( cmd, 0 );
|
|
|
|
|
2006-02-08 09:20:05 +00:00
|
|
|
if( parser_is_reserved(cmd) )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
load( cmd );
|
2005-09-20 13:26:39 +00:00
|
|
|
return (hash_get(&function, cmd) != 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
void function_remove( const wchar_t *name )
|
|
|
|
{
|
|
|
|
void *key;
|
2006-06-12 21:47:42 +00:00
|
|
|
void *dv;
|
2005-09-20 13:26:39 +00:00
|
|
|
function_data_t *d;
|
2005-10-05 22:37:08 +00:00
|
|
|
event_t ev;
|
2006-06-08 00:01:45 +00:00
|
|
|
|
2006-06-21 00:48:36 +00:00
|
|
|
CHECK( name, );
|
2006-07-12 14:22:42 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
hash_remove( &function,
|
|
|
|
name,
|
2006-06-12 21:47:42 +00:00
|
|
|
&key,
|
2006-03-26 11:23:39 +00:00
|
|
|
&dv );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2006-03-26 11:23:39 +00:00
|
|
|
d=(function_data_t *)dv;
|
|
|
|
|
2006-02-05 13:10:35 +00:00
|
|
|
if( !key )
|
2005-09-20 13:26:39 +00:00
|
|
|
return;
|
|
|
|
|
2006-02-05 13:10:35 +00:00
|
|
|
ev.type=EVENT_ANY;
|
|
|
|
ev.function_name=name;
|
|
|
|
event_remove( &ev );
|
|
|
|
|
2006-11-15 14:16:49 +00:00
|
|
|
halloc_free( d );
|
2006-07-12 14:22:42 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Notify the autoloader that the specified function is erased, but
|
|
|
|
only if this call to fish_remove is not made by the autoloader
|
|
|
|
itself.
|
|
|
|
*/
|
|
|
|
if( !is_autoload )
|
|
|
|
{
|
|
|
|
parse_util_unload( name, L"fish_function_path", 0 );
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const wchar_t *function_get_definition( const wchar_t *argv )
|
|
|
|
{
|
2006-02-08 09:20:05 +00:00
|
|
|
function_data_t *data;
|
2006-06-08 00:01:45 +00:00
|
|
|
|
2006-06-21 00:48:36 +00:00
|
|
|
CHECK( argv, 0 );
|
|
|
|
|
2006-02-08 09:20:05 +00:00
|
|
|
load( argv );
|
|
|
|
data = (function_data_t *)hash_get( &function, argv );
|
2005-09-20 13:26:39 +00:00
|
|
|
if( data == 0 )
|
|
|
|
return 0;
|
|
|
|
return data->cmd;
|
|
|
|
}
|
|
|
|
|
|
|
|
const wchar_t *function_get_desc( const wchar_t *argv )
|
|
|
|
{
|
2006-02-08 09:20:05 +00:00
|
|
|
function_data_t *data;
|
2006-06-08 00:01:45 +00:00
|
|
|
|
2006-06-21 00:48:36 +00:00
|
|
|
CHECK( argv, 0 );
|
|
|
|
|
2006-02-08 09:20:05 +00:00
|
|
|
load( argv );
|
|
|
|
data = (function_data_t *)hash_get( &function, argv );
|
2005-09-20 13:26:39 +00:00
|
|
|
if( data == 0 )
|
|
|
|
return 0;
|
|
|
|
|
2006-03-01 16:53:47 +00:00
|
|
|
return _(data->desc);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void function_set_desc( const wchar_t *name, const wchar_t *desc )
|
|
|
|
{
|
2006-02-08 09:20:05 +00:00
|
|
|
function_data_t *data;
|
2006-06-21 00:48:36 +00:00
|
|
|
|
|
|
|
CHECK( name, );
|
|
|
|
CHECK( desc, );
|
2006-06-08 00:01:45 +00:00
|
|
|
|
2006-02-08 09:20:05 +00:00
|
|
|
load( name );
|
|
|
|
data = (function_data_t *)hash_get( &function, name );
|
2005-09-20 13:26:39 +00:00
|
|
|
if( data == 0 )
|
|
|
|
return;
|
|
|
|
|
2006-11-15 14:16:49 +00:00
|
|
|
data->desc = halloc_wcsdup( data, desc );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2006-06-20 00:50:10 +00:00
|
|
|
/**
|
|
|
|
Search arraylist of strings for specified string
|
|
|
|
*/
|
2006-05-14 09:46:35 +00:00
|
|
|
static int al_contains_str( array_list_t *list, const wchar_t * str )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for( i=0; i<al_get_count( list ); i++ )
|
|
|
|
{
|
|
|
|
if( wcscmp( al_get( list, i ), str) == 0 )
|
2006-11-15 14:16:49 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2006-05-14 09:46:35 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/**
|
|
|
|
Helper function for removing hidden functions
|
|
|
|
*/
|
2006-06-12 21:47:42 +00:00
|
|
|
static void get_names_internal( void *key,
|
|
|
|
void *val,
|
2005-09-20 13:26:39 +00:00
|
|
|
void *aux )
|
|
|
|
{
|
|
|
|
wchar_t *name = (wchar_t *)key;
|
2006-10-25 20:28:36 +00:00
|
|
|
if( name[0] != L'_' && !al_contains_str( (array_list_t *)aux, name ) )
|
2006-05-14 09:46:35 +00:00
|
|
|
{
|
|
|
|
al_push( (array_list_t *)aux, name );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Helper function for removing hidden functions
|
|
|
|
*/
|
2006-06-12 21:47:42 +00:00
|
|
|
static void get_names_internal_all( void *key,
|
|
|
|
void *val,
|
|
|
|
void *aux )
|
2006-05-14 09:46:35 +00:00
|
|
|
{
|
|
|
|
wchar_t *name = (wchar_t *)key;
|
|
|
|
|
|
|
|
if( !al_contains_str( (array_list_t *)aux, name ) )
|
|
|
|
{
|
2005-09-20 13:26:39 +00:00
|
|
|
al_push( (array_list_t *)aux, name );
|
2006-05-14 09:46:35 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void function_get_names( array_list_t *list, int get_hidden )
|
|
|
|
{
|
2006-06-21 00:48:36 +00:00
|
|
|
CHECK( list, );
|
|
|
|
|
2006-02-08 17:37:18 +00:00
|
|
|
autoload_names( list, get_hidden );
|
2006-02-08 09:20:05 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( get_hidden )
|
2006-05-14 09:46:35 +00:00
|
|
|
{
|
|
|
|
hash_foreach2( &function, &get_names_internal_all, list );
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
else
|
2006-05-14 09:46:35 +00:00
|
|
|
{
|
2005-09-20 13:26:39 +00:00
|
|
|
hash_foreach2( &function, &get_names_internal, list );
|
2006-05-14 09:46:35 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
}
|
2005-10-05 22:37:08 +00:00
|
|
|
|
2006-01-26 14:48:10 +00:00
|
|
|
const wchar_t *function_get_definition_file( const wchar_t *argv )
|
|
|
|
{
|
2006-02-08 09:20:05 +00:00
|
|
|
function_data_t *data;
|
2006-06-08 00:01:45 +00:00
|
|
|
|
2006-06-21 00:48:36 +00:00
|
|
|
CHECK( argv, 0 );
|
|
|
|
|
2006-02-08 09:20:05 +00:00
|
|
|
data = (function_data_t *)hash_get( &function, argv );
|
2006-01-26 14:48:10 +00:00
|
|
|
if( data == 0 )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return data->definition_file;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int function_get_definition_offset( const wchar_t *argv )
|
|
|
|
{
|
2006-02-08 09:20:05 +00:00
|
|
|
function_data_t *data;
|
2006-06-08 00:01:45 +00:00
|
|
|
|
2006-06-21 00:48:36 +00:00
|
|
|
CHECK( argv, -1 );
|
|
|
|
|
2006-02-08 09:20:05 +00:00
|
|
|
data = (function_data_t *)hash_get( &function, argv );
|
2006-01-26 14:48:10 +00:00
|
|
|
if( data == 0 )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return data->definition_offset;
|
|
|
|
}
|
|
|
|
|