aboutsummaryrefslogtreecommitdiffhomepage
path: root/function.cpp
blob: e8452fa63b5503e7afba9b70a158be4be754feb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/** \file function.c

    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.
*/

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <wchar.h>
#include <unistd.h>
#include <termios.h>
#include <signal.h>
#include <pthread.h>
#include <errno.h>
#include <map>
#include <set>

#include "wutil.h"
#include "fallback.h"
#include "util.h"

#include "function.h"
#include "proc.h"
#include "parser.h"
#include "common.h"
#include "intern.h"
#include "event.h"
#include "reader.h"
#include "parse_util.h"
#include "parser_keywords.h"
#include "env.h"
#include "expand.h"
#include "halloc.h"
#include "halloc_util.h"

class function_internal_info_t
{
public:
    /** Function definition */
    wcstring definition;
    
    /** Function description */
    wcstring description;
    
	/**
	   File where this function was defined
	*/
    const wchar_t *definition_file;
    
	/**
	   Line where definition started
	*/
	int definition_offset;
    
	/**
	   List of all named arguments for this function
	 */
    wcstring_list_t named_arguments;
    
	/**
	   Flag for specifying that this function was automatically loaded
	*/
    bool is_autoload;
    
	/**
	   Set to non-zero if invoking this function shadows the variables
	   of the underlying function.
	 */
	bool shadows;
};

/**
   Table containing all functions
*/
typedef std::map<wcstring, function_internal_info_t> function_map_t;
static function_map_t loaded_functions;

/* Lock for functions */
static pthread_mutex_t functions_lock;

/* Helper macro for vomiting */
#define VOMIT_ON_FAILURE(a) do { if (0 != (a)) { int err = errno; fprintf(stderr, "%s failed on line %d in file %s: %d (%s)\n", #a, __LINE__, __FILE__, err, strerror(err)); abort(); }} while (0)

static int kLockDepth = 0;
static char kLockFunction[1024];

/**
   Lock and unlock the functions hash
*/
static void lock_functions(const char *func) {
    VOMIT_ON_FAILURE(pthread_mutex_lock(&functions_lock));
    if (! kLockDepth++) {
        strcat(kLockFunction, func);
    }
}

static void unlock_functions(void) {
    if (! --kLockDepth) {
        memset(kLockFunction, 0, sizeof kLockFunction);
    }
    VOMIT_ON_FAILURE(pthread_mutex_unlock(&functions_lock));
}

#define LOCK_FUNCTIONS() lock_functions(__FUNCTION__)
#define UNLOCK_FUNCTIONS() unlock_functions()


/**
   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...
*/
static int is_autoload = 0;

/**
   Make sure that if the specified function is a dynamically loaded
   function, it has been fully loaded.
*/
static int load( const wchar_t *name )
{
    ASSERT_IS_MAIN_THREAD();
	int was_autoload = is_autoload;
	int res;
    LOCK_FUNCTIONS();
    function_map_t::iterator iter = loaded_functions.find(name);
	if( iter !=  loaded_functions.end() && !iter->second.is_autoload ) {
        UNLOCK_FUNCTIONS();
		return 0;
    }
    UNLOCK_FUNCTIONS();
	
	is_autoload = 1;	
	res = parse_util_load( name,
						   L"fish_function_path",
						   &function_remove,
						   1 );
	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 )
{
	size_t i;
	
	const wcstring path_var_wstr =  env_get_string( L"fish_function_path" );
	const wchar_t *path_var = path_var_wstr.empty()?NULL:path_var_wstr.c_str(); 

	if( ! path_var )
		return;
	
    wcstring_list_t path_list;

	tokenize_variable_array2( 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))
		{
			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);
	}
}

void function_init()
{
    pthread_mutexattr_t a;
    VOMIT_ON_FAILURE(pthread_mutexattr_init(&a));
    VOMIT_ON_FAILURE(pthread_mutexattr_settype(&a,PTHREAD_MUTEX_RECURSIVE));
    VOMIT_ON_FAILURE(pthread_mutex_init(&functions_lock, &a));
    VOMIT_ON_FAILURE(pthread_mutexattr_destroy(&a));
}

void function_destroy()
{
    LOCK_FUNCTIONS();
    loaded_functions.clear();
    UNLOCK_FUNCTIONS();
}


void function_add( function_data_t *data )
{
	int i;
	
	CHECK( data->name, );
	CHECK( data->definition, );
	LOCK_FUNCTIONS();
	function_remove( data->name );
	    
    function_internal_info_t &info = loaded_functions[data->name];
    
	info.definition_offset = parse_util_lineno( parser_get_buffer(), current_block->tok_pos )-1;
	info.definition = data->definition;

	if( data->named_arguments )
	{
		for( i=0; i<al_get_count( data->named_arguments ); i++ )
		{
            info.named_arguments.push_back((wchar_t *)al_get( data->named_arguments, i ));
		}
	}
	
	
	if (data->description)
        info.description = data->description;
	info.definition_file = intern(reader_current_filename());
	info.is_autoload = is_autoload;
	info.shadows = data->shadows;
	
	
	for( i=0; i<al_get_count( data->events ); i++ )
	{
		event_add_handler( (event_t *)al_get( data->events, i ) );
	}
	UNLOCK_FUNCTIONS();
}

static int function_exists_internal( const wchar_t *cmd, bool autoload )
{
	int res;
	CHECK( cmd, 0 );
	
	if( parser_keywords_is_reserved(cmd) )
		return 0;
	
    LOCK_FUNCTIONS();
	if ( autoload ) load( cmd );
    res = loaded_functions.find(cmd) != loaded_functions.end();
	UNLOCK_FUNCTIONS();
    return res;
}

int function_exists( const wchar_t *cmd )
{
    return function_exists_internal( cmd, true );
}

int function_exists_no_autoload( const wchar_t *cmd )
{
    return function_exists_internal( cmd, false );    
}

void function_remove( const wchar_t *name )
{
	event_t ev;
	
	CHECK( name, );

    LOCK_FUNCTIONS();
    bool erased = (loaded_functions.erase(name) > 0);
	
	if( !erased ) {
        UNLOCK_FUNCTIONS();
		return;
    }

	ev.type=EVENT_ANY;
	ev.function_name=name;	
	event_remove( &ev );

	/*
	  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 );
	}
    UNLOCK_FUNCTIONS();
}
	
const wchar_t *function_get_definition( const wchar_t *name )
{
    const wchar_t *result = NULL;
	CHECK( name, 0 );
	LOCK_FUNCTIONS();
	load( name );
    function_map_t::iterator iter = loaded_functions.find(name);
    if (iter != loaded_functions.end())
        result = iter->second.definition.c_str();
    UNLOCK_FUNCTIONS();
    return result;
}

wcstring_list_t function_get_named_arguments( const wchar_t *name )
{
    wcstring_list_t result;
	CHECK( name, result );
    LOCK_FUNCTIONS();
	load( name );
    function_map_t::iterator iter = loaded_functions.find(name);
    if (iter != loaded_functions.end())
        result = iter->second.named_arguments;
    UNLOCK_FUNCTIONS();
    return result;
}

int function_get_shadows( const wchar_t *name )
{
	bool result = false;
	CHECK( name, 0 );
	LOCK_FUNCTIONS();
	load( name );
    function_map_t::const_iterator iter = loaded_functions.find(name);
    if (iter != loaded_functions.end())
        result = iter->second.shadows;
    UNLOCK_FUNCTIONS();
    return result;
}

	
const wchar_t *function_get_desc( const wchar_t *name )
{
    const wchar_t *result = NULL;
	CHECK( name, 0 );
	load( name );
    LOCK_FUNCTIONS();
    function_map_t::const_iterator iter = loaded_functions.find(name);
    if (iter != loaded_functions.end())
        result = iter->second.description.c_str();
    UNLOCK_FUNCTIONS();
    
    /* Empty length string goes to NULL */
    if (result && ! result[0])
        result = NULL;
            
    return result ? _(result) : NULL;
}

void function_set_desc( const wchar_t *name, const wchar_t *desc )
{
	CHECK( name, );
	CHECK( desc, );
	
	load( name );
    LOCK_FUNCTIONS();
    function_map_t::iterator iter = loaded_functions.find(name);
    if (iter != loaded_functions.end())
        iter->second.description = desc;
    UNLOCK_FUNCTIONS();
}

int function_copy( const wchar_t *name, const wchar_t *new_name )
{
    int result = 0;
    LOCK_FUNCTIONS();
    function_map_t::const_iterator iter = loaded_functions.find(name);
    if (iter != loaded_functions.end()) {
        function_internal_info_t &new_info = loaded_functions[new_name];
        new_info = iter->second;
        
		// This new instance of the function shouldn't be tied to the def 
		// file of the original. 
		new_info.definition_file = 0;
		new_info.is_autoload = 0;
        
        result = 1;
    }
    UNLOCK_FUNCTIONS();
	return result;
}

wcstring_list_t function_get_names( int get_hidden )
{
    std::set<wcstring> names;
    LOCK_FUNCTIONS();
	autoload_names( names, get_hidden );
	
    function_map_t::const_iterator iter;
    for (iter = loaded_functions.begin(); iter != loaded_functions.end(); iter++) {
        const wcstring &name = iter->first;
        
        /* Maybe skip hidden */
        if (! get_hidden) {
            if (name.size() == 0 || name.at(0) == L'_') continue;
        }
        
        names.insert(name);
    }
    UNLOCK_FUNCTIONS();
    
    return wcstring_list_t(names.begin(), names.end());
}

const wchar_t *function_get_definition_file( const wchar_t *name )
{
    const wchar_t *result = NULL;

	CHECK( name, 0 );
    LOCK_FUNCTIONS();
    function_map_t::const_iterator iter = loaded_functions.find(name);
    if (iter != loaded_functions.end())
        result = iter->second.definition_file;
    UNLOCK_FUNCTIONS();
    return result;
}


int function_get_definition_offset( const wchar_t *name )
{
    int result = -1;

	CHECK( name, -1 );
    LOCK_FUNCTIONS();
    function_map_t::const_iterator iter = loaded_functions.find(name);
    if (iter != loaded_functions.end())
        result = iter->second.definition_offset;
    UNLOCK_FUNCTIONS();
    return result;
}