2006-01-30 16:51:50 +00:00
|
|
|
/** \file parse_util.c
|
|
|
|
|
2006-02-19 01:54:38 +00:00
|
|
|
Various mostly unrelated utility functions related to parsing,
|
|
|
|
loading and evaluating fish code.
|
2006-10-28 16:41:22 +00:00
|
|
|
|
|
|
|
This library can be seen as a 'toolbox' for functions that are
|
|
|
|
used in many places in fish and that are somehow related to
|
|
|
|
parsing the code.
|
2006-01-30 16:51:50 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2006-02-28 13:17:16 +00:00
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <wctype.h>
|
|
|
|
|
|
|
|
#include <wchar.h>
|
|
|
|
|
2006-02-08 09:20:05 +00:00
|
|
|
#include <time.h>
|
2006-01-30 16:51:50 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
2006-02-28 13:17:16 +00:00
|
|
|
#include "fallback.h"
|
2006-01-30 16:51:50 +00:00
|
|
|
#include "util.h"
|
2006-02-28 13:17:16 +00:00
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
#include "wutil.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "tokenizer.h"
|
|
|
|
#include "parse_util.h"
|
2006-02-08 09:20:05 +00:00
|
|
|
#include "expand.h"
|
|
|
|
#include "intern.h"
|
|
|
|
#include "exec.h"
|
2006-02-14 19:56:36 +00:00
|
|
|
#include "env.h"
|
2006-11-01 10:54:12 +00:00
|
|
|
#include "signal.h"
|
2006-02-19 01:14:32 +00:00
|
|
|
#include "wildcard.h"
|
2006-02-09 15:50:20 +00:00
|
|
|
#include "halloc_util.h"
|
2006-02-08 09:20:05 +00:00
|
|
|
|
2006-10-10 22:45:46 +00:00
|
|
|
/**
|
|
|
|
Maximum number of autoloaded items opf a specific type to keep in
|
|
|
|
memory at a time.
|
|
|
|
*/
|
|
|
|
#define AUTOLOAD_MAX 10
|
|
|
|
|
|
|
|
/**
|
|
|
|
Minimum time, in seconds, before an autoloaded item will be
|
|
|
|
unloaded
|
|
|
|
*/
|
|
|
|
#define AUTOLOAD_MIN_AGE 60
|
|
|
|
|
2006-07-12 14:22:42 +00:00
|
|
|
/**
|
|
|
|
A structure representing the autoload state for a specific variable, e.g. fish_complete_path
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
A table containing the modification times of all loaded
|
|
|
|
files. Failed loads (non-existing files) have modification time
|
|
|
|
0.
|
|
|
|
*/
|
|
|
|
hash_table_t load_time;
|
|
|
|
/**
|
|
|
|
A string containg the path used to find any files to load. If
|
|
|
|
this differs from the current environment variable, the
|
|
|
|
autoloader needs to drop all loaded files and reload them.
|
|
|
|
*/
|
|
|
|
wchar_t *old_path;
|
|
|
|
/**
|
|
|
|
A table containing all the files that are currently being
|
|
|
|
loaded. This is here to help prevent recursion.
|
|
|
|
*/
|
|
|
|
hash_table_t is_loading;
|
|
|
|
}
|
|
|
|
autoload_t;
|
|
|
|
|
|
|
|
|
2006-02-08 09:20:05 +00:00
|
|
|
/**
|
|
|
|
Set of files which have been autoloaded
|
|
|
|
*/
|
2006-02-08 18:44:37 +00:00
|
|
|
static hash_table_t *all_loaded=0;
|
2006-01-30 16:51:50 +00:00
|
|
|
|
2006-02-05 13:10:35 +00:00
|
|
|
int parse_util_lineno( const wchar_t *str, int len )
|
|
|
|
{
|
2006-02-19 18:19:32 +00:00
|
|
|
/**
|
|
|
|
First cached state
|
|
|
|
*/
|
2006-06-14 13:22:40 +00:00
|
|
|
static wchar_t *prev_str = 0;
|
2006-02-19 18:19:32 +00:00
|
|
|
static int i=0;
|
|
|
|
static int res = 1;
|
2006-02-05 13:10:35 +00:00
|
|
|
|
2006-02-19 18:19:32 +00:00
|
|
|
/**
|
|
|
|
Second cached state
|
|
|
|
*/
|
2006-06-14 13:22:40 +00:00
|
|
|
static wchar_t *prev_str2 = 0;
|
2006-02-05 13:10:35 +00:00
|
|
|
static int i2 = 0;
|
|
|
|
static int res2 = 1;
|
2006-06-21 00:48:36 +00:00
|
|
|
|
|
|
|
CHECK( str, 0 );
|
|
|
|
|
2006-02-05 13:10:35 +00:00
|
|
|
if( str != prev_str || i>len )
|
|
|
|
{
|
|
|
|
if( prev_str2 == str && i2 <= len )
|
|
|
|
{
|
2006-06-14 13:22:40 +00:00
|
|
|
wchar_t *tmp_str = prev_str;
|
2006-02-05 13:10:35 +00:00
|
|
|
int tmp_i = i;
|
|
|
|
int tmp_res = res;
|
|
|
|
prev_str = prev_str2;
|
|
|
|
i=i2;
|
|
|
|
res=res2;
|
|
|
|
|
|
|
|
prev_str2 = tmp_str;
|
|
|
|
i2 = tmp_i;
|
|
|
|
res2 = tmp_res;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
prev_str2 = prev_str;
|
|
|
|
i2 = i;
|
|
|
|
res2=res;
|
|
|
|
|
2006-06-14 13:22:40 +00:00
|
|
|
prev_str = (wchar_t *)str;
|
2006-02-05 13:10:35 +00:00
|
|
|
i=0;
|
|
|
|
res=1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-06 13:47:57 +00:00
|
|
|
for( ; str[i] && i<len; i++ )
|
2006-02-05 13:10:35 +00:00
|
|
|
{
|
|
|
|
if( str[i] == L'\n' )
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-02-05 13:10:35 +00:00
|
|
|
res++;
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
2006-02-05 13:10:35 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
int parse_util_locate_cmdsubst( const wchar_t *in,
|
2006-06-14 13:22:40 +00:00
|
|
|
wchar_t **begin,
|
|
|
|
wchar_t **end,
|
2006-01-30 16:51:50 +00:00
|
|
|
int allow_incomplete )
|
|
|
|
{
|
2006-06-14 13:22:40 +00:00
|
|
|
wchar_t *pos;
|
2006-01-30 16:51:50 +00:00
|
|
|
wchar_t prev=0;
|
|
|
|
int syntax_error=0;
|
|
|
|
int paran_count=0;
|
|
|
|
|
2006-06-14 13:22:40 +00:00
|
|
|
wchar_t *paran_begin=0, *paran_end=0;
|
2006-01-30 16:51:50 +00:00
|
|
|
|
2006-06-21 00:48:36 +00:00
|
|
|
CHECK( in, 0 );
|
|
|
|
|
2006-06-15 00:59:31 +00:00
|
|
|
for( pos = (wchar_t *)in; *pos; pos++ )
|
2006-01-30 16:51:50 +00:00
|
|
|
{
|
|
|
|
if( prev != '\\' )
|
|
|
|
{
|
|
|
|
if( wcschr( L"\'\"", *pos ) )
|
|
|
|
{
|
2006-06-14 13:22:40 +00:00
|
|
|
wchar_t *q_end = quote_end( pos );
|
|
|
|
if( q_end && *q_end)
|
2006-01-30 16:51:50 +00:00
|
|
|
{
|
2006-06-14 13:22:40 +00:00
|
|
|
pos=q_end;
|
2006-01-30 16:51:50 +00:00
|
|
|
}
|
|
|
|
else
|
2006-06-15 00:59:31 +00:00
|
|
|
{
|
2006-01-30 16:51:50 +00:00
|
|
|
break;
|
2006-06-15 00:59:31 +00:00
|
|
|
}
|
2006-01-30 16:51:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( *pos == '(' )
|
|
|
|
{
|
|
|
|
if(( paran_count == 0)&&(paran_begin==0))
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-01-30 16:51:50 +00:00
|
|
|
paran_begin = pos;
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
2006-02-19 18:19:32 +00:00
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
paran_count++;
|
|
|
|
}
|
|
|
|
else if( *pos == ')' )
|
|
|
|
{
|
2007-01-15 17:51:44 +00:00
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
paran_count--;
|
2007-01-15 17:51:44 +00:00
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
if( (paran_count == 0) && (paran_end == 0) )
|
|
|
|
{
|
|
|
|
paran_end = pos;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( paran_count < 0 )
|
|
|
|
{
|
|
|
|
syntax_error = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
prev = *pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
syntax_error |= (paran_count < 0 );
|
|
|
|
syntax_error |= ((paran_count>0)&&(!allow_incomplete));
|
|
|
|
|
|
|
|
if( syntax_error )
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( paran_begin == 0 )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-02-22 15:41:52 +00:00
|
|
|
if( begin )
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-02-22 15:41:52 +00:00
|
|
|
*begin = paran_begin;
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
|
|
|
|
2006-02-22 15:41:52 +00:00
|
|
|
if( end )
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-06-14 13:22:40 +00:00
|
|
|
*end = paran_count?(wchar_t *)in+wcslen(in):paran_end;
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
2006-01-30 16:51:50 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void parse_util_cmdsubst_extent( const wchar_t *buff,
|
|
|
|
int cursor_pos,
|
2006-06-14 13:22:40 +00:00
|
|
|
wchar_t **a,
|
|
|
|
wchar_t **b )
|
2006-01-30 16:51:50 +00:00
|
|
|
{
|
2006-06-14 13:22:40 +00:00
|
|
|
wchar_t *begin, *end;
|
|
|
|
wchar_t *pos;
|
2006-07-03 10:46:47 +00:00
|
|
|
const wchar_t *cursor = buff + cursor_pos;
|
2006-01-30 16:51:50 +00:00
|
|
|
|
2006-06-21 00:48:36 +00:00
|
|
|
CHECK( buff, );
|
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
if( a )
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-07-03 10:46:47 +00:00
|
|
|
*a = (wchar_t *)buff;
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
if( b )
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-07-03 10:46:47 +00:00
|
|
|
*b = (wchar_t *)buff+wcslen(buff);
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
2006-07-03 10:46:47 +00:00
|
|
|
|
2006-06-14 13:22:40 +00:00
|
|
|
pos = (wchar_t *)buff;
|
2006-01-30 16:51:50 +00:00
|
|
|
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
if( parse_util_locate_cmdsubst( pos,
|
|
|
|
&begin,
|
|
|
|
&end,
|
|
|
|
1 ) <= 0)
|
|
|
|
{
|
2006-07-03 10:46:47 +00:00
|
|
|
/*
|
|
|
|
No subshell found
|
|
|
|
*/
|
2006-01-30 16:51:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !end )
|
|
|
|
{
|
2006-06-14 13:22:40 +00:00
|
|
|
end = (wchar_t *)buff + wcslen(buff);
|
2006-01-30 16:51:50 +00:00
|
|
|
}
|
|
|
|
|
2006-07-03 10:46:47 +00:00
|
|
|
if(( begin < cursor ) && (end >= cursor) )
|
2006-01-30 16:51:50 +00:00
|
|
|
{
|
|
|
|
begin++;
|
2007-01-15 17:51:44 +00:00
|
|
|
|
2006-07-03 10:46:47 +00:00
|
|
|
if( a )
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-07-03 10:46:47 +00:00
|
|
|
*a = begin;
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
|
|
|
|
2006-07-03 10:46:47 +00:00
|
|
|
if( b )
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-07-03 10:46:47 +00:00
|
|
|
*b = end;
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-06-15 00:59:31 +00:00
|
|
|
|
|
|
|
if( !*end )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
pos = end+1;
|
|
|
|
}
|
2006-07-03 10:46:47 +00:00
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Get the beginning and end of the job or process definition under the cursor
|
|
|
|
*/
|
|
|
|
static void job_or_process_extent( const wchar_t *buff,
|
|
|
|
int cursor_pos,
|
2006-06-14 13:22:40 +00:00
|
|
|
wchar_t **a,
|
|
|
|
wchar_t **b,
|
2006-01-30 16:51:50 +00:00
|
|
|
int process )
|
|
|
|
{
|
2006-06-14 13:22:40 +00:00
|
|
|
wchar_t *begin, *end;
|
2006-01-30 16:51:50 +00:00
|
|
|
int pos;
|
|
|
|
wchar_t *buffcpy;
|
|
|
|
int finished=0;
|
|
|
|
|
|
|
|
tokenizer tok;
|
|
|
|
|
2006-06-21 00:48:36 +00:00
|
|
|
CHECK( buff, );
|
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
if( a )
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-01-30 16:51:50 +00:00
|
|
|
*a=0;
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
if( b )
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-01-30 16:51:50 +00:00
|
|
|
*b = 0;
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
2006-01-30 16:51:50 +00:00
|
|
|
|
|
|
|
parse_util_cmdsubst_extent( buff, cursor_pos, &begin, &end );
|
|
|
|
if( !end || !begin )
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-01-30 16:51:50 +00:00
|
|
|
return;
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
pos = cursor_pos - (begin - buff);
|
|
|
|
|
|
|
|
if( a )
|
|
|
|
{
|
|
|
|
*a = begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( b )
|
|
|
|
{
|
|
|
|
*b = end;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffcpy = wcsndup( begin, end-begin );
|
|
|
|
|
|
|
|
if( !buffcpy )
|
|
|
|
{
|
2006-07-03 10:39:57 +00:00
|
|
|
DIE_MEM();
|
2006-01-30 16:51:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for( tok_init( &tok, buffcpy, TOK_ACCEPT_UNFINISHED );
|
|
|
|
tok_has_next( &tok ) && !finished;
|
|
|
|
tok_next( &tok ) )
|
|
|
|
{
|
|
|
|
int tok_begin = tok_get_pos( &tok );
|
|
|
|
|
|
|
|
switch( tok_last_type( &tok ) )
|
|
|
|
{
|
|
|
|
case TOK_PIPE:
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-01-30 16:51:50 +00:00
|
|
|
if( !process )
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-01-30 16:51:50 +00:00
|
|
|
break;
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
case TOK_END:
|
|
|
|
case TOK_BACKGROUND:
|
|
|
|
{
|
|
|
|
|
|
|
|
if( tok_begin >= pos )
|
|
|
|
{
|
|
|
|
finished=1;
|
|
|
|
if( b )
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-06-14 13:22:40 +00:00
|
|
|
*b = (wchar_t *)buff + tok_begin;
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
2006-01-30 16:51:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( a )
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-06-14 13:22:40 +00:00
|
|
|
*a = (wchar_t *)buff + tok_begin+1;
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
2006-01-30 16:51:50 +00:00
|
|
|
}
|
2007-01-15 17:51:44 +00:00
|
|
|
|
|
|
|
break;
|
2006-01-30 16:51:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free( buffcpy);
|
|
|
|
|
|
|
|
tok_destroy( &tok );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void parse_util_process_extent( const wchar_t *buff,
|
|
|
|
int pos,
|
2006-06-14 13:22:40 +00:00
|
|
|
wchar_t **a,
|
|
|
|
wchar_t **b )
|
2006-01-30 16:51:50 +00:00
|
|
|
{
|
|
|
|
job_or_process_extent( buff, pos, a, b, 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
void parse_util_job_extent( const wchar_t *buff,
|
|
|
|
int pos,
|
2006-06-14 13:22:40 +00:00
|
|
|
wchar_t **a,
|
|
|
|
wchar_t **b )
|
2006-01-30 16:51:50 +00:00
|
|
|
{
|
|
|
|
job_or_process_extent( buff,pos,a, b, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void parse_util_token_extent( const wchar_t *buff,
|
|
|
|
int cursor_pos,
|
2006-06-14 13:22:40 +00:00
|
|
|
wchar_t **tok_begin,
|
|
|
|
wchar_t **tok_end,
|
|
|
|
wchar_t **prev_begin,
|
|
|
|
wchar_t **prev_end )
|
2006-01-30 16:51:50 +00:00
|
|
|
{
|
2006-06-14 13:22:40 +00:00
|
|
|
wchar_t *begin, *end;
|
2006-01-30 16:51:50 +00:00
|
|
|
int pos;
|
|
|
|
wchar_t *buffcpy;
|
|
|
|
|
|
|
|
tokenizer tok;
|
|
|
|
|
2006-06-14 13:22:40 +00:00
|
|
|
wchar_t *a, *b, *pa, *pb;
|
|
|
|
|
2006-06-21 00:48:36 +00:00
|
|
|
CHECK( buff, );
|
|
|
|
|
2006-06-14 13:22:40 +00:00
|
|
|
assert( cursor_pos >= 0 );
|
2006-01-30 16:51:50 +00:00
|
|
|
|
|
|
|
a = b = pa = pb = 0;
|
|
|
|
|
|
|
|
parse_util_cmdsubst_extent( buff, cursor_pos, &begin, &end );
|
|
|
|
|
|
|
|
if( !end || !begin )
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-01-30 16:51:50 +00:00
|
|
|
return;
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
2006-07-03 10:46:47 +00:00
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
pos = cursor_pos - (begin - buff);
|
2006-07-03 10:46:47 +00:00
|
|
|
|
2006-06-14 13:22:40 +00:00
|
|
|
a = (wchar_t *)buff + pos;
|
2006-01-30 16:51:50 +00:00
|
|
|
b = a;
|
2006-06-14 13:22:40 +00:00
|
|
|
pa = (wchar_t *)buff + pos;
|
2006-01-30 16:51:50 +00:00
|
|
|
pb = pa;
|
2006-07-03 10:46:47 +00:00
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
assert( begin >= buff );
|
|
|
|
assert( begin <= (buff+wcslen(buff) ) );
|
|
|
|
assert( end >= begin );
|
|
|
|
assert( end <= (buff+wcslen(buff) ) );
|
2006-07-03 10:46:47 +00:00
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
buffcpy = wcsndup( begin, end-begin );
|
2006-07-03 10:46:47 +00:00
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
if( !buffcpy )
|
|
|
|
{
|
2006-07-03 10:39:57 +00:00
|
|
|
DIE_MEM();
|
2006-01-30 16:51:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for( tok_init( &tok, buffcpy, TOK_ACCEPT_UNFINISHED );
|
|
|
|
tok_has_next( &tok );
|
|
|
|
tok_next( &tok ) )
|
|
|
|
{
|
|
|
|
int tok_begin = tok_get_pos( &tok );
|
|
|
|
int tok_end=tok_begin;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Calculate end of token
|
|
|
|
*/
|
|
|
|
if( tok_last_type( &tok ) == TOK_STRING )
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-01-30 16:51:50 +00:00
|
|
|
tok_end +=wcslen(tok_last(&tok));
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
2006-01-30 16:51:50 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Cursor was before beginning of this token, means that the
|
|
|
|
cursor is between two tokens, so we set it to a zero element
|
|
|
|
string and break
|
|
|
|
*/
|
|
|
|
if( tok_begin > pos )
|
|
|
|
{
|
2006-06-14 13:22:40 +00:00
|
|
|
a = b = (wchar_t *)buff + pos;
|
2006-01-30 16:51:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
If cursor is inside the token, this is the token we are
|
|
|
|
looking for. If so, set a and b and break
|
|
|
|
*/
|
2006-07-24 12:46:29 +00:00
|
|
|
if( (tok_last_type( &tok ) == TOK_STRING) && (tok_end >= pos ) )
|
|
|
|
{
|
2006-01-30 16:51:50 +00:00
|
|
|
a = begin + tok_get_pos( &tok );
|
|
|
|
b = a + wcslen(tok_last(&tok));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Remember previous string token
|
|
|
|
*/
|
|
|
|
if( tok_last_type( &tok ) == TOK_STRING )
|
|
|
|
{
|
|
|
|
pa = begin + tok_get_pos( &tok );
|
|
|
|
pb = pa + wcslen(tok_last(&tok));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free( buffcpy);
|
|
|
|
|
|
|
|
tok_destroy( &tok );
|
|
|
|
|
|
|
|
if( tok_begin )
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-01-30 16:51:50 +00:00
|
|
|
*tok_begin = a;
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
if( tok_end )
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-01-30 16:51:50 +00:00
|
|
|
*tok_end = b;
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
if( prev_begin )
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-01-30 16:51:50 +00:00
|
|
|
*prev_begin = pa;
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
if( prev_end )
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-01-30 16:51:50 +00:00
|
|
|
*prev_end = pb;
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
assert( pa >= buff );
|
|
|
|
assert( pa <= (buff+wcslen(buff) ) );
|
|
|
|
assert( pb >= pa );
|
|
|
|
assert( pb <= (buff+wcslen(buff) ) );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-02-09 15:50:20 +00:00
|
|
|
/**
|
|
|
|
Free hash value, but not hash key
|
|
|
|
*/
|
2006-07-12 14:22:42 +00:00
|
|
|
static void clear_hash_value( void *key, void *data, void *aux )
|
2006-02-09 15:50:20 +00:00
|
|
|
{
|
2006-07-12 14:22:42 +00:00
|
|
|
if( aux )
|
|
|
|
{
|
|
|
|
wchar_t *name = (wchar_t *)key;
|
2006-10-28 16:41:22 +00:00
|
|
|
time_t *time = (time_t *)data;
|
2006-07-12 14:22:42 +00:00
|
|
|
void (*handler)(const wchar_t *)= (void (*)(const wchar_t *))aux;
|
2006-10-28 16:41:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
If time[0] is 0, that means the file really doesn't exist,
|
|
|
|
it's simply been checked before. We should not unload it.
|
|
|
|
*/
|
|
|
|
if( time[0] && handler )
|
|
|
|
{
|
|
|
|
// debug( 0, L"Unloading function %ls", name );
|
2006-10-19 11:46:32 +00:00
|
|
|
handler( name );
|
2006-10-28 16:41:22 +00:00
|
|
|
}
|
2006-07-12 14:22:42 +00:00
|
|
|
}
|
|
|
|
|
2006-02-09 15:50:20 +00:00
|
|
|
free( (void *)data );
|
|
|
|
}
|
|
|
|
|
2006-02-19 01:54:38 +00:00
|
|
|
/**
|
|
|
|
Part of the autoloader cleanup
|
|
|
|
*/
|
2006-07-12 14:22:42 +00:00
|
|
|
static void clear_loaded_entry( void *key,
|
|
|
|
void *data,
|
|
|
|
void *handler )
|
2006-02-09 15:50:20 +00:00
|
|
|
{
|
2006-07-12 14:22:42 +00:00
|
|
|
autoload_t *loaded = (autoload_t *)data;
|
|
|
|
hash_foreach2( &loaded->load_time,
|
|
|
|
&clear_hash_value,
|
|
|
|
handler );
|
|
|
|
hash_destroy( &loaded->load_time );
|
2006-07-22 10:15:49 +00:00
|
|
|
hash_destroy( &loaded->is_loading );
|
2006-07-12 14:22:42 +00:00
|
|
|
|
|
|
|
free( loaded->old_path );
|
2006-02-09 15:50:20 +00:00
|
|
|
free( loaded );
|
|
|
|
free( (void *)key );
|
|
|
|
}
|
|
|
|
|
2006-02-19 01:54:38 +00:00
|
|
|
/**
|
|
|
|
The autoloader cleanup function. It is run on shutdown and frees
|
|
|
|
any memory used by the autoloader code to keep track of loaded
|
|
|
|
files.
|
|
|
|
*/
|
2006-02-09 15:50:20 +00:00
|
|
|
static void parse_util_destroy()
|
|
|
|
{
|
|
|
|
if( all_loaded )
|
|
|
|
{
|
2006-07-12 14:22:42 +00:00
|
|
|
hash_foreach2( all_loaded,
|
|
|
|
&clear_loaded_entry,
|
|
|
|
0 );
|
2006-02-09 15:50:20 +00:00
|
|
|
|
|
|
|
hash_destroy( all_loaded );
|
|
|
|
free( all_loaded );
|
|
|
|
all_loaded = 0;
|
|
|
|
}
|
|
|
|
}
|
2006-01-30 16:51:50 +00:00
|
|
|
|
2006-07-12 14:22:42 +00:00
|
|
|
void parse_util_load_reset( const wchar_t *path_var_name,
|
|
|
|
void (*on_load)(const wchar_t *cmd) )
|
2006-02-15 02:46:44 +00:00
|
|
|
{
|
2006-07-12 14:22:42 +00:00
|
|
|
CHECK( path_var_name, );
|
|
|
|
|
2006-02-15 02:46:44 +00:00
|
|
|
if( all_loaded )
|
|
|
|
{
|
|
|
|
void *key, *data;
|
2006-07-12 14:22:42 +00:00
|
|
|
hash_remove( all_loaded, path_var_name, &key, &data );
|
2006-02-15 02:46:44 +00:00
|
|
|
if( key )
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-07-12 14:22:42 +00:00
|
|
|
clear_loaded_entry( key, data, (void *)on_load );
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
2006-02-15 02:46:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-07-12 14:22:42 +00:00
|
|
|
int parse_util_unload( const wchar_t *cmd,
|
|
|
|
const wchar_t *path_var_name,
|
|
|
|
void (*on_load)(const wchar_t *cmd) )
|
|
|
|
{
|
|
|
|
autoload_t *loaded;
|
|
|
|
void *val;
|
|
|
|
|
|
|
|
CHECK( path_var_name, 0 );
|
|
|
|
CHECK( cmd, 0 );
|
|
|
|
|
|
|
|
if( !all_loaded )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
loaded = (autoload_t *)hash_get( all_loaded, path_var_name );
|
|
|
|
|
|
|
|
if( !loaded )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
hash_remove( &loaded->load_time, cmd, 0, &val );
|
|
|
|
if( val )
|
|
|
|
{
|
|
|
|
if( on_load )
|
|
|
|
{
|
2006-11-12 12:17:25 +00:00
|
|
|
on_load( cmd );
|
2006-07-12 14:22:42 +00:00
|
|
|
}
|
|
|
|
free( val );
|
|
|
|
}
|
|
|
|
|
|
|
|
return !!val;
|
|
|
|
}
|
2006-02-15 02:46:44 +00:00
|
|
|
|
2007-01-15 17:51:44 +00:00
|
|
|
/**
|
|
|
|
|
|
|
|
Unload all autoloaded items that have expired, that where loaded in
|
|
|
|
the specified path.
|
|
|
|
|
|
|
|
\param path_var_name The variable containing the path to autoload in
|
|
|
|
\param skip unloading the the specified file
|
|
|
|
\param on_load the callback function to call for every unloaded file
|
|
|
|
|
|
|
|
*/
|
2006-10-12 12:08:59 +00:00
|
|
|
static void parse_util_autounload( const wchar_t *path_var_name,
|
|
|
|
const wchar_t *skip,
|
2006-10-10 22:45:46 +00:00
|
|
|
void (*on_load)(const wchar_t *cmd) )
|
|
|
|
{
|
|
|
|
autoload_t *loaded;
|
|
|
|
int loaded_count=0;
|
|
|
|
|
|
|
|
if( !all_loaded )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
loaded = (autoload_t *)hash_get( all_loaded, path_var_name );
|
|
|
|
if( !loaded )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( hash_get_count( &loaded->load_time ) >= AUTOLOAD_MAX )
|
|
|
|
{
|
|
|
|
time_t oldest_access = time(0) - AUTOLOAD_MIN_AGE;
|
|
|
|
wchar_t *oldest_item=0;
|
|
|
|
int i;
|
|
|
|
array_list_t key;
|
|
|
|
al_init( &key );
|
|
|
|
hash_get_keys( &loaded->load_time, &key );
|
|
|
|
for( i=0; i<al_get_count( &key ); i++ )
|
|
|
|
{
|
|
|
|
wchar_t *item = (wchar_t *)al_get( &key, i );
|
|
|
|
time_t *tm = hash_get( &loaded->load_time, item );
|
|
|
|
|
|
|
|
if( wcscmp( item, skip ) == 0 )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !tm[0] )
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-10-10 22:45:46 +00:00
|
|
|
continue;
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
|
|
|
|
2006-10-16 15:40:22 +00:00
|
|
|
if( hash_get( &loaded->is_loading, item ) )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2006-10-10 22:45:46 +00:00
|
|
|
|
|
|
|
loaded_count++;
|
|
|
|
|
|
|
|
if( tm[1] < oldest_access )
|
|
|
|
{
|
|
|
|
oldest_access = tm[1];
|
|
|
|
oldest_item = item;
|
|
|
|
}
|
|
|
|
}
|
2006-10-12 13:30:12 +00:00
|
|
|
al_destroy( &key );
|
2006-10-10 22:45:46 +00:00
|
|
|
|
|
|
|
if( oldest_item && loaded_count > AUTOLOAD_MAX)
|
|
|
|
{
|
|
|
|
parse_util_unload( oldest_item, path_var_name, on_load );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_util_load_internal( const wchar_t *cmd,
|
2006-10-12 12:08:59 +00:00
|
|
|
void (*on_load)(const wchar_t *cmd),
|
|
|
|
int reload,
|
|
|
|
autoload_t *loaded,
|
|
|
|
array_list_t *path_list );
|
2006-08-28 11:00:19 +00:00
|
|
|
|
|
|
|
|
2006-02-08 09:20:05 +00:00
|
|
|
int parse_util_load( const wchar_t *cmd,
|
2006-02-19 17:01:16 +00:00
|
|
|
const wchar_t *path_var_name,
|
2006-02-15 02:46:44 +00:00
|
|
|
void (*on_load)(const wchar_t *cmd),
|
|
|
|
int reload )
|
2006-02-08 09:20:05 +00:00
|
|
|
{
|
2006-08-28 16:27:34 +00:00
|
|
|
array_list_t *path_list=0;
|
2006-02-12 19:03:01 +00:00
|
|
|
|
2006-07-12 14:22:42 +00:00
|
|
|
autoload_t *loaded;
|
2006-02-12 19:03:01 +00:00
|
|
|
|
2006-07-12 14:22:42 +00:00
|
|
|
wchar_t *path_var;
|
2006-02-19 17:01:16 +00:00
|
|
|
|
2006-08-28 11:00:19 +00:00
|
|
|
int res;
|
|
|
|
int c, c2;
|
|
|
|
|
2006-07-12 14:22:42 +00:00
|
|
|
CHECK( path_var_name, 0 );
|
|
|
|
CHECK( cmd, 0 );
|
2006-10-10 22:45:46 +00:00
|
|
|
|
2006-10-29 21:09:11 +00:00
|
|
|
CHECK_BLOCK( 0 );
|
|
|
|
|
2006-08-28 11:00:19 +00:00
|
|
|
// debug( 0, L"Autoload %ls in %ls", cmd, path_var_name );
|
|
|
|
|
2006-10-28 16:41:22 +00:00
|
|
|
parse_util_autounload( path_var_name, cmd, on_load );
|
2006-07-12 14:22:42 +00:00
|
|
|
path_var = env_get( path_var_name );
|
|
|
|
|
2006-02-08 09:20:05 +00:00
|
|
|
/*
|
2006-08-28 11:00:19 +00:00
|
|
|
Do we know where to look?
|
2006-02-08 09:20:05 +00:00
|
|
|
*/
|
|
|
|
if( !path_var )
|
2006-07-12 14:22:42 +00:00
|
|
|
{
|
2006-02-08 09:20:05 +00:00
|
|
|
return 0;
|
2006-07-12 14:22:42 +00:00
|
|
|
}
|
2006-08-28 11:00:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Init if this is the first time we try to autoload anything
|
|
|
|
*/
|
2006-02-08 18:44:37 +00:00
|
|
|
if( !all_loaded )
|
|
|
|
{
|
2006-02-17 12:52:05 +00:00
|
|
|
all_loaded = malloc( sizeof( hash_table_t ) );
|
|
|
|
halloc_register_function_void( global_context, &parse_util_destroy );
|
2006-02-08 18:44:37 +00:00
|
|
|
if( !all_loaded )
|
|
|
|
{
|
2006-07-03 10:39:57 +00:00
|
|
|
DIE_MEM();
|
2006-02-08 18:44:37 +00:00
|
|
|
}
|
|
|
|
hash_init( all_loaded, &hash_wcs_func, &hash_wcs_cmp );
|
2006-02-17 12:52:05 +00:00
|
|
|
}
|
2006-02-08 18:44:37 +00:00
|
|
|
|
2006-07-12 14:22:42 +00:00
|
|
|
loaded = (autoload_t *)hash_get( all_loaded, path_var_name );
|
|
|
|
|
|
|
|
if( loaded )
|
|
|
|
{
|
2006-10-28 16:41:22 +00:00
|
|
|
/*
|
|
|
|
Check if the lookup path has changed. If so, drop all loaded
|
|
|
|
files and start from scratch.
|
|
|
|
*/
|
|
|
|
if( wcscmp( path_var, loaded->old_path ) != 0 )
|
|
|
|
{
|
|
|
|
parse_util_load_reset( path_var_name, on_load);
|
|
|
|
reload = parse_util_load( cmd, path_var_name, on_load, reload );
|
|
|
|
return reload;
|
|
|
|
}
|
|
|
|
|
2006-08-28 11:00:19 +00:00
|
|
|
/**
|
|
|
|
Warn and fail on infinite recursion
|
|
|
|
*/
|
2006-07-12 14:22:42 +00:00
|
|
|
if( hash_get( &loaded->is_loading, cmd ) )
|
|
|
|
{
|
2006-08-28 11:00:19 +00:00
|
|
|
debug( 0,
|
2006-10-19 11:46:32 +00:00
|
|
|
_( L"Could not autoload item '%ls', it is already being autoloaded. "
|
|
|
|
L"This is a circular dependency in the autoloading scripts, please remove it."),
|
2006-08-28 11:00:19 +00:00
|
|
|
cmd );
|
2006-07-12 14:22:42 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-10-28 16:41:22 +00:00
|
|
|
|
2006-07-12 14:22:42 +00:00
|
|
|
}
|
|
|
|
else
|
2006-02-08 09:20:05 +00:00
|
|
|
{
|
2006-07-12 14:22:42 +00:00
|
|
|
/*
|
2006-08-28 11:00:19 +00:00
|
|
|
We have never tried to autoload using this path name before,
|
|
|
|
set up initial data
|
2006-07-12 14:22:42 +00:00
|
|
|
*/
|
2006-10-28 16:41:22 +00:00
|
|
|
// debug( 0, L"Create brand new autoload_t for %ls->%ls", path_var_name, path_var );
|
2006-08-28 11:00:19 +00:00
|
|
|
loaded = malloc( sizeof( autoload_t ) );
|
2006-02-08 09:20:05 +00:00
|
|
|
if( !loaded )
|
|
|
|
{
|
2006-07-03 10:39:57 +00:00
|
|
|
DIE_MEM();
|
2006-02-08 09:20:05 +00:00
|
|
|
}
|
2006-07-12 14:22:42 +00:00
|
|
|
hash_init( &loaded->load_time, &hash_wcs_func, &hash_wcs_cmp );
|
2006-02-19 17:01:16 +00:00
|
|
|
hash_put( all_loaded, wcsdup(path_var_name), loaded );
|
2006-08-28 11:00:19 +00:00
|
|
|
|
2006-07-12 14:22:42 +00:00
|
|
|
hash_init( &loaded->is_loading, &hash_wcs_func, &hash_wcs_cmp );
|
|
|
|
|
|
|
|
loaded->old_path = wcsdup( path_var );
|
2006-02-08 09:20:05 +00:00
|
|
|
}
|
|
|
|
|
2006-08-28 11:00:19 +00:00
|
|
|
|
2006-08-28 16:27:34 +00:00
|
|
|
path_list = al_new( global_context);
|
2006-08-28 11:00:19 +00:00
|
|
|
|
|
|
|
tokenize_variable_array( path_var, path_list );
|
|
|
|
|
2006-08-28 16:27:34 +00:00
|
|
|
c = al_get_count( path_list );
|
2006-08-28 11:00:19 +00:00
|
|
|
|
2006-07-12 14:22:42 +00:00
|
|
|
hash_put( &loaded->is_loading, cmd, cmd );
|
2006-08-28 11:00:19 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Do the actual work in the internal helper function
|
|
|
|
*/
|
|
|
|
|
2006-10-10 22:45:46 +00:00
|
|
|
res = parse_util_load_internal( cmd, on_load, reload, loaded, path_list );
|
2006-08-28 11:00:19 +00:00
|
|
|
|
2006-10-29 21:09:11 +00:00
|
|
|
autoload_t *loaded2 = (autoload_t *)hash_get( all_loaded, path_var_name );
|
|
|
|
if( loaded2 == loaded )
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
Cleanup
|
|
|
|
*/
|
|
|
|
hash_remove( &loaded->is_loading, cmd, 0, 0 );
|
|
|
|
}
|
|
|
|
|
2006-08-28 16:27:34 +00:00
|
|
|
c2 = al_get_count( path_list );
|
|
|
|
|
2006-08-28 11:00:19 +00:00
|
|
|
al_foreach( path_list, &free );
|
2006-08-28 16:27:34 +00:00
|
|
|
al_destroy( path_list );
|
|
|
|
free( path_list );
|
|
|
|
|
2006-08-28 11:00:19 +00:00
|
|
|
/**
|
|
|
|
Make sure we didn't 'drop' something
|
|
|
|
*/
|
2006-08-28 16:27:34 +00:00
|
|
|
|
2006-08-28 11:00:19 +00:00
|
|
|
assert( c == c2 );
|
2006-07-12 14:22:42 +00:00
|
|
|
|
2006-08-28 11:00:19 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
This internal helper function does all the real work. By using two
|
|
|
|
functions, the internal function can return on various places in
|
|
|
|
the code, and the caller can take care of various cleanup work.
|
|
|
|
*/
|
2006-07-12 14:22:42 +00:00
|
|
|
|
2006-10-10 22:45:46 +00:00
|
|
|
static int parse_util_load_internal( const wchar_t *cmd,
|
2006-10-12 12:08:59 +00:00
|
|
|
void (*on_load)(const wchar_t *cmd),
|
|
|
|
int reload,
|
|
|
|
autoload_t *loaded,
|
|
|
|
array_list_t *path_list )
|
2006-08-28 11:00:19 +00:00
|
|
|
{
|
|
|
|
static string_buffer_t *path=0;
|
|
|
|
time_t *tm;
|
|
|
|
int i;
|
|
|
|
int reloaded = 0;
|
2006-09-03 22:58:39 +00:00
|
|
|
|
2006-02-08 09:20:05 +00:00
|
|
|
/*
|
|
|
|
Get modification time of file
|
|
|
|
*/
|
2006-07-12 14:22:42 +00:00
|
|
|
tm = (time_t *)hash_get( &loaded->load_time, cmd );
|
2006-08-28 11:00:19 +00:00
|
|
|
|
2006-02-08 09:20:05 +00:00
|
|
|
/*
|
|
|
|
Did we just check this?
|
|
|
|
*/
|
|
|
|
if( tm )
|
2006-02-17 12:52:05 +00:00
|
|
|
{
|
2006-02-14 19:55:17 +00:00
|
|
|
if(time(0)-tm[1]<=1)
|
2006-02-17 12:52:05 +00:00
|
|
|
{
|
2006-02-08 09:20:05 +00:00
|
|
|
return 0;
|
2006-02-17 12:52:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-08 09:20:05 +00:00
|
|
|
/*
|
|
|
|
Return if already loaded and we are skipping reloading
|
|
|
|
*/
|
|
|
|
if( !reload && tm )
|
2006-07-12 14:22:42 +00:00
|
|
|
{
|
2006-02-08 09:20:05 +00:00
|
|
|
return 0;
|
2006-07-12 14:22:42 +00:00
|
|
|
}
|
2006-02-12 19:03:01 +00:00
|
|
|
|
|
|
|
if( !path )
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-02-12 19:03:01 +00:00
|
|
|
path = sb_halloc( global_context );
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
2006-02-12 19:03:01 +00:00
|
|
|
else
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-02-12 19:03:01 +00:00
|
|
|
sb_clear( path );
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
2006-02-12 19:03:01 +00:00
|
|
|
|
2006-02-08 09:20:05 +00:00
|
|
|
/*
|
|
|
|
Iterate over path searching for suitable completion files
|
|
|
|
*/
|
2006-02-12 19:03:01 +00:00
|
|
|
for( i=0; i<al_get_count( path_list ); i++ )
|
2006-02-08 09:20:05 +00:00
|
|
|
{
|
|
|
|
struct stat buf;
|
2006-02-12 19:03:01 +00:00
|
|
|
wchar_t *next = (wchar_t *)al_get( path_list, i );
|
|
|
|
sb_clear( path );
|
|
|
|
sb_append2( path, next, L"/", cmd, L".fish", (void *)0 );
|
2006-09-03 22:58:39 +00:00
|
|
|
|
2006-02-12 19:03:01 +00:00
|
|
|
if( (wstat( (wchar_t *)path->buff, &buf )== 0) &&
|
|
|
|
(waccess( (wchar_t *)path->buff, R_OK ) == 0) )
|
2006-02-08 09:20:05 +00:00
|
|
|
{
|
2006-02-17 12:52:05 +00:00
|
|
|
if( !tm || (tm[0] != buf.st_mtime ) )
|
2006-02-08 09:20:05 +00:00
|
|
|
{
|
2006-02-12 19:03:01 +00:00
|
|
|
wchar_t *esc = escape( (wchar_t *)path->buff, 1 );
|
2006-02-08 09:20:05 +00:00
|
|
|
wchar_t *src_cmd = wcsdupcat( L". ", esc );
|
2006-09-03 23:00:06 +00:00
|
|
|
free( esc );
|
2006-02-12 19:03:01 +00:00
|
|
|
|
2006-02-08 09:20:05 +00:00
|
|
|
if( !tm )
|
|
|
|
{
|
|
|
|
tm = malloc(sizeof(time_t)*2);
|
|
|
|
if( !tm )
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-07-03 10:39:57 +00:00
|
|
|
DIE_MEM();
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
2006-02-08 09:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tm[0] = buf.st_mtime;
|
|
|
|
tm[1] = time(0);
|
2006-07-12 14:22:42 +00:00
|
|
|
hash_put( &loaded->load_time,
|
2006-02-08 09:20:05 +00:00
|
|
|
intern( cmd ),
|
|
|
|
tm );
|
|
|
|
|
2006-02-19 18:19:32 +00:00
|
|
|
if( on_load )
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-02-19 18:19:32 +00:00
|
|
|
on_load(cmd );
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
|
|
|
|
2006-02-08 09:20:05 +00:00
|
|
|
/*
|
|
|
|
Source the completion file for the specified completion
|
|
|
|
*/
|
2006-10-19 15:36:03 +00:00
|
|
|
if( exec_subshell( src_cmd, 0 ) == -1 )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Do nothing on failiure
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2006-02-08 09:20:05 +00:00
|
|
|
free(src_cmd);
|
|
|
|
reloaded = 1;
|
|
|
|
}
|
2006-10-28 16:41:22 +00:00
|
|
|
else if( tm )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
If we are rechecking an autoload file, and it hasn't
|
|
|
|
changed, update the 'last check' timestamp.
|
|
|
|
*/
|
|
|
|
tm[1] = time(0);
|
|
|
|
}
|
|
|
|
|
2006-09-03 22:58:39 +00:00
|
|
|
break;
|
2006-02-08 09:20:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
If no file was found we insert the current time. Later we only
|
|
|
|
research if the current time is at least five seconds later.
|
|
|
|
This way, the files won't be searched over and over again.
|
|
|
|
*/
|
|
|
|
if( !tm )
|
|
|
|
{
|
|
|
|
tm = malloc(sizeof(time_t)*2);
|
|
|
|
if( !tm )
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-07-03 10:39:57 +00:00
|
|
|
DIE_MEM();
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
2006-02-08 09:20:05 +00:00
|
|
|
|
|
|
|
tm[0] = 0;
|
|
|
|
tm[1] = time(0);
|
2006-07-12 14:22:42 +00:00
|
|
|
hash_put( &loaded->load_time, intern( cmd ), tm );
|
2006-02-08 09:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return reloaded;
|
|
|
|
}
|
|
|
|
|
2006-02-14 19:56:36 +00:00
|
|
|
void parse_util_set_argv( wchar_t **argv )
|
|
|
|
{
|
|
|
|
if( *argv )
|
|
|
|
{
|
|
|
|
wchar_t **arg;
|
|
|
|
string_buffer_t sb;
|
|
|
|
sb_init( &sb );
|
|
|
|
|
|
|
|
for( arg=argv; *arg; arg++ )
|
|
|
|
{
|
|
|
|
if( arg != argv )
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-02-14 19:56:36 +00:00
|
|
|
sb_append( &sb, ARRAY_SEP_STR );
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
2006-02-14 19:56:36 +00:00
|
|
|
sb_append( &sb, *arg );
|
|
|
|
}
|
|
|
|
|
|
|
|
env_set( L"argv", (wchar_t *)sb.buff, ENV_LOCAL );
|
|
|
|
sb_destroy( &sb );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
env_set( L"argv", 0, ENV_LOCAL );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-19 01:14:32 +00:00
|
|
|
wchar_t *parse_util_unescape_wildcards( const wchar_t *str )
|
|
|
|
{
|
|
|
|
wchar_t *in, *out;
|
2006-07-12 14:22:42 +00:00
|
|
|
wchar_t *unescaped;
|
|
|
|
|
|
|
|
CHECK( str, 0 );
|
|
|
|
|
|
|
|
unescaped = wcsdup(str);
|
2006-02-19 01:14:32 +00:00
|
|
|
|
|
|
|
if( !unescaped )
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-07-03 10:39:57 +00:00
|
|
|
DIE_MEM();
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
2006-02-19 01:14:32 +00:00
|
|
|
|
|
|
|
for( in=out=unescaped; *in; in++ )
|
|
|
|
{
|
|
|
|
switch( *in )
|
|
|
|
{
|
|
|
|
case L'\\':
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-02-19 01:14:32 +00:00
|
|
|
if( *(in+1) )
|
|
|
|
{
|
|
|
|
in++;
|
|
|
|
*(out++)=*in;
|
|
|
|
}
|
|
|
|
*(out++)=*in;
|
|
|
|
break;
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
|
|
|
|
2006-02-19 01:14:32 +00:00
|
|
|
case L'*':
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-02-19 01:14:32 +00:00
|
|
|
*(out++)=ANY_STRING;
|
|
|
|
break;
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
|
|
|
|
2006-02-19 01:14:32 +00:00
|
|
|
case L'?':
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-02-19 01:14:32 +00:00
|
|
|
*(out++)=ANY_CHAR;
|
|
|
|
break;
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
|
|
|
|
2006-02-19 01:14:32 +00:00
|
|
|
default:
|
2007-01-15 17:51:44 +00:00
|
|
|
{
|
2006-02-19 01:14:32 +00:00
|
|
|
*(out++)=*in;
|
|
|
|
break;
|
2007-01-15 17:51:44 +00:00
|
|
|
}
|
|
|
|
}
|
2006-02-19 01:14:32 +00:00
|
|
|
}
|
|
|
|
return unescaped;
|
|
|
|
}
|
2006-02-14 19:56:36 +00:00
|
|
|
|