2005-09-20 21:26:39 +08:00
/**\file expand.c
String expansion functions . These functions perform several kinds of
2011-12-27 11:18:46 +08:00
parameter expansion .
2005-09-20 21:26:39 +08:00
*/
2006-08-11 09:18:35 +08:00
# include "config.h"
2005-09-20 21:26:39 +08:00
# include <stdlib.h>
# include <stdio.h>
# include <wchar.h>
# include <string.h>
# include <wctype.h>
# include <errno.h>
# include <pwd.h>
# include <unistd.h>
# include <sys/types.h>
# include <termios.h>
# include <dirent.h>
# include <sys/stat.h>
# include <unistd.h>
# include <signal.h>
2005-12-26 06:00:44 +08:00
# include <assert.h>
2011-12-27 11:18:46 +08:00
# include <vector>
2005-12-26 06:00:44 +08:00
2005-09-20 21:26:39 +08:00
# ifdef SunOS
# include <procfs.h>
# endif
2006-02-28 21:17:16 +08:00
# include "fallback.h"
2005-09-20 21:26:39 +08:00
# include "util.h"
2006-02-28 21:17:16 +08:00
2005-09-20 21:26:39 +08:00
# include "common.h"
# include "wutil.h"
# include "env.h"
# include "proc.h"
# include "parser.h"
# include "expand.h"
# include "wildcard.h"
# include "exec.h"
2006-02-09 23:50:20 +08:00
# include "signal.h"
2005-09-20 21:26:39 +08:00
# include "tokenizer.h"
# include "complete.h"
2006-07-20 06:55:49 +08:00
2006-01-31 00:51:50 +08:00
# include "parse_util.h"
2006-08-24 21:39:04 +08:00
# include "halloc.h"
2006-02-10 08:35:39 +08:00
# include "halloc_util.h"
2005-09-20 21:26:39 +08:00
2006-07-20 21:02:46 +08:00
/**
Error issued on invalid variable name
*/
# define COMPLETE_VAR_DESC _( L"The '$' character begins a variable name. The character '%lc', which directly followed a '$', is not allowed as a part of a variable name, and variable names may not be zero characters long. To learn more about variable expansion in fish, type 'help expand-variable'.")
/**
Error issued on invalid variable name
*/
# define COMPLETE_VAR_NULL_DESC _( L"The '$' begins a variable name. It was given at the end of an argument. Variable names may not be zero characters long. To learn more about variable expansion in fish, type 'help expand-variable'.")
/**
Error issued on invalid variable name
*/
2006-07-21 07:33:19 +08:00
# define COMPLETE_VAR_BRACKET_DESC _( L"Did you mean %ls{$%ls}%ls? The '$' character begins a variable name. A bracket, which directly followed a '$', is not allowed as a part of a variable name, and variable names may not be zero characters long. To learn more about variable expansion in fish, type 'help expand-variable'." )
2006-07-20 21:02:46 +08:00
/**
Error issued on invalid variable name
*/
# define COMPLETE_VAR_PARAN_DESC _( L"Did you mean (COMMAND)? In fish, the '$' character is only used for accessing variables. To learn more about command substitution in fish, type 'help expand-command-substitution'.")
2005-09-20 21:26:39 +08:00
/**
Description for child process
*/
2006-01-17 20:48:30 +08:00
# define COMPLETE_CHILD_PROCESS_DESC _( L"Child process")
2005-09-20 21:26:39 +08:00
/**
Description for non - child process
*/
2006-01-17 20:48:30 +08:00
# define COMPLETE_PROCESS_DESC _( L"Process")
2005-09-20 21:26:39 +08:00
/**
Description for long job
*/
2006-01-17 20:48:30 +08:00
# define COMPLETE_JOB_DESC _( L"Job")
2005-09-20 21:26:39 +08:00
/**
Description for short job . The job command is concatenated
*/
2007-02-24 16:11:31 +08:00
# define COMPLETE_JOB_DESC_VAL _( L"Job: %ls")
2005-09-20 21:26:39 +08:00
/**
Description for the shells own pid
*/
2006-01-17 20:48:30 +08:00
# define COMPLETE_SELF_DESC _( L"Shell process")
2005-09-20 21:26:39 +08:00
/**
Description for the shells own pid
*/
2006-01-17 20:48:30 +08:00
# define COMPLETE_LAST_DESC _( L"Last background job")
2005-09-20 21:26:39 +08:00
/**
String in process expansion denoting ourself
*/
# define SELF_STR L"self"
/**
String in process expansion denoting last background job
*/
# define LAST_STR L"last"
2005-10-26 18:51:02 +08:00
/**
Characters which make a string unclean if they are the first
2009-02-21 18:43:30 +08:00
character of the string . See \ c expand_is_clean ( ) .
2005-10-26 18:51:02 +08:00
*/
# define UNCLEAN_FIRST L"~%"
/**
2009-02-21 18:43:30 +08:00
Unclean characters . See \ c expand_is_clean ( ) .
2005-10-26 18:51:02 +08:00
*/
# define UNCLEAN L"$*?\\\"'({})"
2011-12-27 11:18:46 +08:00
/* An adapter class for calling functions that expect a wchar_t * and array_list_t */
class wcstring_adapter
{
private :
std : : vector < wcstring > * outputs ;
public :
wchar_t * str ;
array_list_t lst ;
wcstring_adapter ( const wcstring & in , std : : vector < wcstring > & outs )
{
outputs = & outs ;
str = wcsdup ( in . c_str ( ) ) ;
al_init ( & lst ) ;
}
~ wcstring_adapter ( )
{
int i , max = al_get_count ( & lst ) ;
for ( i = 0 ; i < max ; i + + ) {
wchar_t * tmp = ( wchar_t * ) al_get ( & lst , i ) ;
outputs - > push_back ( tmp ) ;
free ( tmp ) ;
}
al_destroy ( & lst ) ;
//str is free'd by the function we called
}
} ;
2006-05-22 06:16:04 +08:00
int expand_is_clean ( const wchar_t * in )
2005-10-26 18:51:02 +08:00
{
2006-01-31 03:53:10 +08:00
2005-10-26 18:51:02 +08:00
const wchar_t * str = in ;
2006-06-21 08:48:36 +08:00
CHECK ( in , 1 ) ;
2005-11-30 00:52:02 +08:00
/*
Test characters that have a special meaning in the first character position
*/
2005-10-26 18:51:02 +08:00
if ( wcschr ( UNCLEAN_FIRST , * str ) )
return 0 ;
2006-01-31 03:53:10 +08:00
2005-11-30 00:52:02 +08:00
/*
Test characters that have a special meaning in any character position
*/
2005-10-26 18:51:02 +08:00
while ( * str )
{
if ( wcschr ( UNCLEAN , * str ) )
return 0 ;
str + + ;
}
2006-01-31 03:53:10 +08:00
2005-10-26 18:51:02 +08:00
return 1 ;
}
2005-09-20 21:26:39 +08:00
/**
2006-01-31 03:53:10 +08:00
Return the environment variable value for the string starting at \ c in .
2005-09-20 21:26:39 +08:00
*/
2012-01-13 00:09:00 +08:00
static const wchar_t * expand_var ( wchar_t * in )
2005-09-20 21:26:39 +08:00
{
if ( ! in )
return 0 ;
2012-01-12 06:51:11 +08:00
return env_get ( in ) ;
2005-09-20 21:26:39 +08:00
}
/**
Test if the specified string does not contain character which can
not be used inside a quoted string .
*/
static int is_quotable ( wchar_t * str )
{
switch ( * str )
{
case 0 :
return 1 ;
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
case L ' \n ' :
case L ' \t ' :
case L ' \r ' :
case L ' \b ' :
2007-08-22 15:52:39 +08:00
case L ' \x1b ' :
2005-09-20 21:26:39 +08:00
return 0 ;
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
default :
2006-01-31 03:53:10 +08:00
return is_quotable ( str + 1 ) ;
2005-09-20 21:26:39 +08:00
}
return 0 ;
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
}
wchar_t * expand_escape_variable ( const wchar_t * in )
{
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
array_list_t l ;
string_buffer_t buff ;
2006-01-31 03:53:10 +08:00
2006-06-21 08:48:36 +08:00
CHECK ( in , 0 ) ;
2005-09-20 21:26:39 +08:00
al_init ( & l ) ;
2006-05-29 19:13:42 +08:00
tokenize_variable_array ( in , & l ) ;
2005-09-20 21:26:39 +08:00
sb_init ( & buff ) ;
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
switch ( al_get_count ( & l ) )
{
case 0 :
2005-11-03 00:49:13 +08:00
sb_append ( & buff , L " '' " ) ;
2006-01-31 03:53:10 +08:00
break ;
2011-12-27 11:18:46 +08:00
2005-09-20 21:26:39 +08:00
case 1 :
{
wchar_t * el = ( wchar_t * ) al_get ( & l , 0 ) ;
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
if ( wcschr ( el , L ' ' ) & & is_quotable ( el ) )
{
2007-09-29 05:32:27 +08:00
sb_append ( & buff ,
2005-11-03 00:49:13 +08:00
L " ' " ,
2005-09-20 21:26:39 +08:00
el ,
2005-11-03 00:49:13 +08:00
L " ' " ,
2011-12-27 11:18:46 +08:00
NULL ) ;
2005-09-20 21:26:39 +08:00
}
else
{
2006-02-06 23:15:52 +08:00
wchar_t * val = escape ( el , 1 ) ;
2005-09-20 21:26:39 +08:00
sb_append ( & buff , val ) ;
free ( val ) ;
}
free ( el ) ;
break ;
}
default :
{
int j ;
for ( j = 0 ; j < al_get_count ( & l ) ; j + + )
{
wchar_t * el = ( wchar_t * ) al_get ( & l , j ) ;
if ( j )
sb_append ( & buff , L " " ) ;
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
if ( is_quotable ( el ) )
{
2007-09-29 05:32:27 +08:00
sb_append ( & buff ,
2005-11-03 00:49:13 +08:00
L " ' " ,
2005-09-20 21:26:39 +08:00
el ,
2005-11-03 00:49:13 +08:00
L " ' " ,
2011-12-27 11:18:46 +08:00
NULL ) ;
2005-09-20 21:26:39 +08:00
}
else
{
2006-02-06 23:15:52 +08:00
wchar_t * val = escape ( el , 1 ) ;
2005-09-20 21:26:39 +08:00
sb_append ( & buff , val ) ;
free ( val ) ;
}
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
free ( el ) ;
2006-01-31 03:53:10 +08:00
}
}
2005-09-20 21:26:39 +08:00
}
al_destroy ( & l ) ;
return ( wchar_t * ) buff . buff ;
2012-01-14 18:42:17 +08:00
}
2006-01-31 03:53:10 +08:00
2012-01-14 18:42:17 +08:00
wcstring expand_escape_variable2 ( const wcstring & in ) {
wchar_t * tmp = expand_escape_variable ( in . c_str ( ) ) ;
wcstring result ( tmp ? tmp : L " " ) ;
free ( tmp ) ;
return result ;
2005-09-20 21:26:39 +08:00
}
/**
Tests if all characters in the wide string are numeric
*/
static int iswnumeric ( const wchar_t * n )
{
2006-05-22 05:46:01 +08:00
for ( ; * n ; n + + )
{
if ( * n < L ' 0 ' | | * n > L ' 9 ' )
{
return 0 ;
}
}
return 1 ;
2005-09-20 21:26:39 +08:00
}
/**
See if the process described by \ c proc matches the commandline \ c
cmd
*/
2006-01-31 03:53:10 +08:00
static int match_pid ( const wchar_t * cmd ,
2005-09-20 21:26:39 +08:00
const wchar_t * proc ,
2006-12-14 19:58:11 +08:00
int flags ,
int * offset )
2005-09-20 21:26:39 +08:00
{
/* Test for direct match */
2011-12-27 11:18:46 +08:00
2005-09-20 21:26:39 +08:00
if ( wcsncmp ( cmd , proc , wcslen ( proc ) ) = = 0 )
2006-12-14 19:58:11 +08:00
{
if ( offset )
* offset = 0 ;
2005-09-20 21:26:39 +08:00
return 1 ;
2006-12-14 19:58:11 +08:00
}
2011-12-27 11:18:46 +08:00
2006-01-31 03:53:10 +08:00
/*
2006-02-07 19:48:57 +08:00
Test if the commandline is a path to the command , if so we try
to match against only the command part
2005-09-20 21:26:39 +08:00
*/
wchar_t * first_token = tok_first ( cmd ) ;
if ( first_token = = 0 )
return 0 ;
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
wchar_t * start = 0 ;
wchar_t prev = 0 ;
wchar_t * p ;
2006-01-31 03:53:10 +08:00
/*
2006-02-07 19:48:57 +08:00
This should be done by basename ( ) , if it wasn ' t for the fact
that is does not accept wide strings
2005-09-20 21:26:39 +08:00
*/
for ( p = first_token ; * p ; p + + )
{
if ( * p = = L ' / ' & & prev ! = L ' \\ ' )
start = p ;
prev = * p ;
}
if ( start )
{
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
if ( wcsncmp ( start + 1 , proc , wcslen ( proc ) ) = = 0 )
{
2006-12-14 19:58:11 +08:00
if ( offset )
* offset = start + 1 - first_token ;
2011-12-27 11:18:46 +08:00
2005-09-20 21:26:39 +08:00
free ( first_token ) ;
2006-12-14 19:58:11 +08:00
2005-09-20 21:26:39 +08:00
return 1 ;
}
}
free ( first_token ) ;
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
return 0 ;
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
}
/**
Searches for a job with the specified job id , or a job or process
which has the string \ c proc as a prefix of its commandline .
2007-02-24 16:11:31 +08:00
If the ACCEPT_INCOMPLETE flag is set , the remaining string for any matches
2005-11-30 03:50:30 +08:00
are inserted .
2005-09-20 21:26:39 +08:00
2007-02-24 16:11:31 +08:00
Otherwise , any job matching the specified string is matched , and
the job pgid is returned . If no job matches , all child processes
are searched . If no child processes match , and < tt > fish < / tt > can
understand the contents of the / proc filesystem , all the users
processes are searched for matches .
2005-09-20 21:26:39 +08:00
*/
2006-01-31 03:53:10 +08:00
static int find_process ( const wchar_t * proc ,
int flags ,
2012-01-17 00:56:47 +08:00
std : : vector < completion_t > & out )
2005-09-20 21:26:39 +08:00
{
DIR * dir ;
2006-05-22 05:46:01 +08:00
wchar_t * pdir_name ;
wchar_t * pfile_name ;
2005-09-20 21:26:39 +08:00
wchar_t * cmd = 0 ;
int sz = 0 ;
int found = 0 ;
2006-01-31 03:53:10 +08:00
wchar_t * result ;
2005-09-20 21:26:39 +08:00
job_t * j ;
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
if ( iswnumeric ( proc ) | | ( wcslen ( proc ) = = 0 ) )
{
/*
2006-01-31 03:53:10 +08:00
This is a numeric job string , like ' % 2 '
2005-09-20 21:26:39 +08:00
*/
if ( flags & ACCEPT_INCOMPLETE )
{
for ( j = first_job ; j ! = 0 ; j = j - > next )
{
wchar_t jid [ 16 ] ;
if ( j - > command = = 0 )
continue ;
swprintf ( jid , 16 , L " %d " , j - > job_id ) ;
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
if ( wcsncmp ( proc , jid , wcslen ( proc ) ) = = 0 )
{
2007-02-24 16:11:31 +08:00
string_buffer_t desc_buff ;
2011-12-27 11:18:46 +08:00
2007-02-24 16:11:31 +08:00
sb_init ( & desc_buff ) ;
2011-12-27 11:18:46 +08:00
sb_printf ( & desc_buff ,
2007-02-24 16:11:31 +08:00
COMPLETE_JOB_DESC_VAL ,
j - > command ) ;
2011-12-27 11:18:46 +08:00
completion_allocate ( out ,
2007-02-24 16:11:31 +08:00
jid + wcslen ( proc ) ,
( wchar_t * ) desc_buff . buff ,
0 ) ;
2006-01-31 03:53:10 +08:00
2007-02-24 16:11:31 +08:00
sb_destroy ( & desc_buff ) ;
2005-09-20 21:26:39 +08:00
}
}
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
}
else
{
2006-01-31 03:53:10 +08:00
2007-01-09 11:20:05 +08:00
int jid ;
wchar_t * end ;
2011-12-27 11:18:46 +08:00
2007-01-09 11:20:05 +08:00
errno = 0 ;
jid = wcstol ( proc , & end , 10 ) ;
if ( jid > 0 & & ! errno & & ! * end )
2005-09-20 21:26:39 +08:00
{
2006-04-28 21:21:37 +08:00
j = job_get ( jid ) ;
if ( ( j ! = 0 ) & & ( j - > command ! = 0 ) )
2005-09-20 21:26:39 +08:00
{
2011-12-27 11:18:46 +08:00
2006-04-28 21:21:37 +08:00
{
2011-12-27 11:18:46 +08:00
result = ( wchar_t * ) malloc ( sizeof ( wchar_t ) * 16 ) ;
2006-04-28 21:21:37 +08:00
swprintf ( result , 16 , L " %d " , j - > pgid ) ;
2012-01-17 00:56:47 +08:00
completion_t data_to_push ;
data_to_push . completion = result ;
out . push_back ( data_to_push ) ;
2006-04-28 21:21:37 +08:00
found = 1 ;
}
2005-09-20 21:26:39 +08:00
}
2006-01-31 03:53:10 +08:00
}
2005-09-20 21:26:39 +08:00
}
}
if ( found )
return 1 ;
for ( j = first_job ; j ! = 0 ; j = j - > next )
{
2006-12-14 19:58:11 +08:00
int offset ;
2011-12-27 11:18:46 +08:00
2005-09-20 21:26:39 +08:00
if ( j - > command = = 0 )
continue ;
2011-12-27 11:18:46 +08:00
2006-12-14 19:58:11 +08:00
if ( match_pid ( j - > command , proc , flags , & offset ) )
2005-09-20 21:26:39 +08:00
{
if ( flags & ACCEPT_INCOMPLETE )
{
2011-12-27 11:18:46 +08:00
completion_allocate ( out ,
2007-02-24 16:11:31 +08:00
j - > command + offset + wcslen ( proc ) ,
COMPLETE_JOB_DESC ,
0 ) ;
2005-09-20 21:26:39 +08:00
}
else
{
2011-12-27 11:18:46 +08:00
result = ( wchar_t * ) malloc ( sizeof ( wchar_t ) * 16 ) ;
2005-09-20 21:26:39 +08:00
swprintf ( result , 16 , L " %d " , j - > pgid ) ;
2012-01-17 00:56:47 +08:00
completion_t data_to_push ;
data_to_push . completion = result ;
out . push_back ( data_to_push ) ;
2005-09-20 21:26:39 +08:00
found = 1 ;
}
}
}
if ( found )
{
return 1 ;
}
for ( j = first_job ; j ; j = j - > next )
{
process_t * p ;
if ( j - > command = = 0 )
continue ;
for ( p = j - > first_process ; p ; p = p - > next )
{
2006-12-14 19:58:11 +08:00
int offset ;
2011-12-27 11:18:46 +08:00
2005-09-20 21:26:39 +08:00
if ( p - > actual_cmd = = 0 )
continue ;
2006-01-31 03:53:10 +08:00
2006-12-14 19:58:11 +08:00
if ( match_pid ( p - > actual_cmd , proc , flags , & offset ) )
2005-09-20 21:26:39 +08:00
{
if ( flags & ACCEPT_INCOMPLETE )
{
2011-12-27 11:18:46 +08:00
completion_allocate ( out ,
2007-02-24 16:11:31 +08:00
p - > actual_cmd + offset + wcslen ( proc ) ,
COMPLETE_CHILD_PROCESS_DESC ,
0 ) ;
2005-09-20 21:26:39 +08:00
}
else
{
2011-12-27 11:18:46 +08:00
result = ( wchar_t * ) malloc ( sizeof ( wchar_t ) * 16 ) ;
2005-09-20 21:26:39 +08:00
swprintf ( result , 16 , L " %d " , p - > pid ) ;
2012-01-17 00:56:47 +08:00
completion_t data_to_push ;
data_to_push . completion = result ;
out . push_back ( data_to_push ) ;
2005-09-20 21:26:39 +08:00
found = 1 ;
}
}
}
}
if ( found )
{
return 1 ;
}
if ( ! ( dir = opendir ( " /proc " ) ) )
{
/*
2006-01-31 03:53:10 +08:00
This system does not have a / proc filesystem .
2005-09-20 21:26:39 +08:00
*/
return 1 ;
}
2006-01-31 03:53:10 +08:00
2011-12-27 11:18:46 +08:00
pdir_name = ( wchar_t * ) malloc ( sizeof ( wchar_t ) * 256 ) ;
pfile_name = ( wchar_t * ) malloc ( sizeof ( wchar_t ) * 64 ) ;
2006-05-22 05:46:01 +08:00
wcscpy ( pdir_name , L " /proc/ " ) ;
2011-12-27 11:18:46 +08:00
wcstring nameStr ;
while ( wreaddir ( dir , nameStr ) )
2005-09-20 21:26:39 +08:00
{
2011-12-27 11:18:46 +08:00
const wchar_t * name = nameStr . c_str ( ) ;
2005-09-20 21:26:39 +08:00
struct stat buf ;
2006-05-22 05:46:01 +08:00
if ( ! iswnumeric ( name ) )
2005-09-20 21:26:39 +08:00
continue ;
2006-05-22 05:46:01 +08:00
wcscpy ( pdir_name + 6 , name ) ;
if ( wstat ( pdir_name , & buf ) )
2005-09-20 21:26:39 +08:00
{
continue ;
2006-01-31 03:53:10 +08:00
}
2005-09-20 21:26:39 +08:00
if ( buf . st_uid ! = getuid ( ) )
{
continue ;
}
2006-05-22 05:46:01 +08:00
wcscpy ( pfile_name , pdir_name ) ;
wcscat ( pfile_name , L " /cmdline " ) ;
2006-01-31 03:53:10 +08:00
2006-05-22 05:46:01 +08:00
if ( ! wstat ( pfile_name , & buf ) )
2005-09-20 21:26:39 +08:00
{
/*
the ' cmdline ' file exists , it should contain the commandline
*/
FILE * cmdfile ;
2006-01-31 03:53:10 +08:00
2006-05-22 05:46:01 +08:00
if ( ( cmdfile = wfopen ( pfile_name , " r " ) ) = = 0 )
2005-09-20 21:26:39 +08:00
{
wperror ( L " fopen " ) ;
continue ;
}
2006-01-31 03:53:10 +08:00
2006-02-09 23:50:20 +08:00
signal_block ( ) ;
2006-01-31 03:53:10 +08:00
fgetws2 ( & cmd , & sz , cmdfile ) ;
2006-02-09 23:50:20 +08:00
signal_unblock ( ) ;
2011-12-27 11:18:46 +08:00
2005-09-20 21:26:39 +08:00
fclose ( cmdfile ) ;
}
else
{
# ifdef SunOS
2006-05-22 05:46:01 +08:00
wcscpy ( pfile_name , pdir_name ) ;
wcscat ( pfile_name , L " /psinfo " ) ;
if ( ! wstat ( pfile_name , & buf ) )
2005-09-20 21:26:39 +08:00
{
psinfo_t info ;
FILE * psfile ;
2006-01-31 03:53:10 +08:00
2006-05-22 05:46:01 +08:00
if ( ( psfile = wfopen ( pfile_name , " r " ) ) = = 0 )
2005-09-20 21:26:39 +08:00
{
wperror ( L " fopen " ) ;
continue ;
}
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
if ( fread ( & info , sizeof ( info ) , 1 , psfile ) )
{
if ( cmd ! = 0 )
free ( cmd ) ;
cmd = str2wcs ( info . pr_fname ) ;
}
fclose ( psfile ) ;
}
else
# endif
{
if ( cmd ! = 0 )
{
* cmd = 0 ;
}
}
}
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
if ( cmd ! = 0 )
{
2006-12-14 19:58:11 +08:00
int offset ;
2011-12-27 11:18:46 +08:00
2006-12-14 19:58:11 +08:00
if ( match_pid ( cmd , proc , flags , & offset ) )
2006-01-31 03:53:10 +08:00
{
2005-09-20 21:26:39 +08:00
if ( flags & ACCEPT_INCOMPLETE )
{
2011-12-27 11:18:46 +08:00
completion_allocate ( out ,
2007-02-24 16:11:31 +08:00
cmd + offset + wcslen ( proc ) ,
COMPLETE_PROCESS_DESC ,
0 ) ;
2005-09-20 21:26:39 +08:00
}
else
{
2006-05-22 05:46:01 +08:00
wchar_t * res = wcsdup ( name ) ;
2012-01-17 00:56:47 +08:00
if ( res ) {
completion_t data_to_push ;
data_to_push . completion = res ;
out . push_back ( data_to_push ) ;
}
2005-09-20 21:26:39 +08:00
}
}
}
}
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
if ( cmd ! = 0 )
free ( cmd ) ;
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
free ( pdir_name ) ;
free ( pfile_name ) ;
2006-01-31 03:53:10 +08:00
closedir ( dir ) ;
2005-09-20 21:26:39 +08:00
return 1 ;
}
/**
Process id expansion
*/
2006-01-31 03:53:10 +08:00
static int expand_pid ( wchar_t * in ,
2005-09-20 21:26:39 +08:00
int flags ,
2012-01-17 00:56:47 +08:00
std : : vector < completion_t > & out )
2005-09-20 21:26:39 +08:00
{
2006-06-20 08:50:10 +08:00
2006-07-21 09:08:31 +08:00
CHECK ( in , 0 ) ;
2012-01-17 00:56:47 +08:00
// CHECK( out, 0 );
2011-12-27 11:18:46 +08:00
2005-09-20 21:26:39 +08:00
if ( * in ! = PROCESS_EXPAND )
{
2012-01-17 00:56:47 +08:00
completion_t data_to_push ;
data_to_push . completion = in ;
out . push_back ( data_to_push ) ;
2005-09-20 21:26:39 +08:00
return 1 ;
}
if ( flags & ACCEPT_INCOMPLETE )
{
if ( wcsncmp ( in + 1 , SELF_STR , wcslen ( in + 1 ) ) = = 0 )
{
2011-12-27 11:18:46 +08:00
completion_allocate ( out ,
2007-02-24 16:11:31 +08:00
SELF_STR + wcslen ( in + 1 ) ,
2011-12-27 11:18:46 +08:00
COMPLETE_SELF_DESC ,
2007-02-24 16:11:31 +08:00
0 ) ;
2006-01-31 03:53:10 +08:00
}
2005-09-20 21:26:39 +08:00
else if ( wcsncmp ( in + 1 , LAST_STR , wcslen ( in + 1 ) ) = = 0 )
{
2011-12-27 11:18:46 +08:00
completion_allocate ( out ,
LAST_STR + wcslen ( in + 1 ) ,
COMPLETE_LAST_DESC ,
2007-02-24 16:11:31 +08:00
0 ) ;
2006-01-31 03:53:10 +08:00
}
2005-09-20 21:26:39 +08:00
}
else
{
if ( wcscmp ( ( in + 1 ) , SELF_STR ) = = 0 )
{
2011-12-27 11:18:46 +08:00
wchar_t * str = ( wchar_t * ) malloc ( sizeof ( wchar_t ) * 32 ) ;
2012-01-17 00:56:47 +08:00
// free(in);
2005-09-20 21:26:39 +08:00
swprintf ( str , 32 , L " %d " , getpid ( ) ) ;
2012-01-17 00:56:47 +08:00
completion_t data_to_push ;
data_to_push . completion = str ;
out . push_back ( data_to_push ) ;
2006-01-31 03:53:10 +08:00
return 1 ;
}
2005-09-20 21:26:39 +08:00
if ( wcscmp ( ( in + 1 ) , LAST_STR ) = = 0 )
{
wchar_t * str ;
if ( proc_last_bg_pid > 0 )
{
2011-12-27 11:18:46 +08:00
str = ( wchar_t * ) malloc ( sizeof ( wchar_t ) * 32 ) ;
2005-09-20 21:26:39 +08:00
free ( in ) ;
swprintf ( str , 32 , L " %d " , proc_last_bg_pid ) ;
2012-01-17 00:56:47 +08:00
completion_t data_to_push ;
data_to_push . completion = str ;
out . push_back ( data_to_push ) ;
2005-09-20 21:26:39 +08:00
}
2006-01-31 03:53:10 +08:00
return 1 ;
}
}
2012-01-17 00:56:47 +08:00
int prev = out . size ( ) ;
2005-09-20 21:26:39 +08:00
if ( ! find_process ( in + 1 , flags , out ) )
return 0 ;
2006-01-31 03:53:10 +08:00
2012-01-17 00:56:47 +08:00
if ( prev = = out . size ( ) )
2005-09-20 21:26:39 +08:00
{
if ( flags & ACCEPT_INCOMPLETE )
free ( in ) ;
else
{
2006-10-19 23:41:27 +08:00
free ( in ) ;
return 0 ;
2005-09-20 21:26:39 +08:00
}
2006-01-31 03:53:10 +08:00
}
2005-09-20 21:26:39 +08:00
else
{
free ( in ) ;
}
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
return 1 ;
}
2012-01-17 00:56:47 +08:00
/*
static int expand_pid2 ( const wcstring & in , int flags , std : : vector < completion_t > & outputs )
2011-12-27 11:18:46 +08:00
{
wcstring_adapter adapter ( in , outputs ) ;
return expand_pid ( adapter . str , flags , & adapter . lst ) ;
}
2012-01-17 00:56:47 +08:00
*/
2006-07-20 21:02:46 +08:00
void expand_variable_error ( const wchar_t * token , int token_pos , int error_pos )
{
int stop_pos = token_pos + 1 ;
2011-12-27 11:18:46 +08:00
2006-07-20 21:02:46 +08:00
switch ( token [ stop_pos ] )
{
case BRACKET_BEGIN :
{
2006-07-21 07:33:19 +08:00
wchar_t * cpy = wcsdup ( token ) ;
* ( cpy + token_pos ) = 0 ;
wchar_t * name = & cpy [ stop_pos + 1 ] ;
wchar_t * end = wcschr ( name , BRACKET_END ) ;
wchar_t * post ;
int is_var = 0 ;
if ( end )
{
post = end + 1 ;
* end = 0 ;
2011-12-27 11:18:46 +08:00
2006-07-21 07:33:19 +08:00
if ( ! wcsvarname ( name ) )
{
is_var = 1 ;
}
}
2011-12-27 11:18:46 +08:00
2006-07-21 07:33:19 +08:00
if ( is_var )
{
error ( SYNTAX_ERROR ,
error_pos ,
COMPLETE_VAR_BRACKET_DESC ,
cpy ,
name ,
2011-12-27 11:18:46 +08:00
post ) ;
2006-07-21 07:33:19 +08:00
}
else
{
error ( SYNTAX_ERROR ,
error_pos ,
COMPLETE_VAR_BRACKET_DESC ,
L " " ,
L " VARIABLE " ,
L " " ) ;
}
free ( cpy ) ;
2011-12-27 11:18:46 +08:00
2006-07-20 21:02:46 +08:00
break ;
}
2011-12-27 11:18:46 +08:00
2006-07-20 21:02:46 +08:00
case INTERNAL_SEPARATOR :
{
error ( SYNTAX_ERROR ,
error_pos ,
2011-12-27 11:18:46 +08:00
COMPLETE_VAR_PARAN_DESC ) ;
2006-07-20 21:02:46 +08:00
break ;
}
2011-12-27 11:18:46 +08:00
2006-07-20 21:02:46 +08:00
case 0 :
{
error ( SYNTAX_ERROR ,
error_pos ,
COMPLETE_VAR_NULL_DESC ) ;
break ;
}
2011-12-27 11:18:46 +08:00
2006-07-20 21:02:46 +08:00
default :
{
error ( SYNTAX_ERROR ,
error_pos ,
COMPLETE_VAR_DESC ,
token [ stop_pos ] ) ;
break ;
}
}
}
2008-01-14 00:47:47 +08:00
/**
Parse an array slicing specification
*/
2011-12-27 11:18:46 +08:00
static int parse_slice ( const wchar_t * in , wchar_t * * end_ptr , array_list_t * idx )
2006-08-24 21:39:04 +08:00
{
2011-12-27 11:18:46 +08:00
2006-08-24 21:39:04 +08:00
wchar_t * end ;
2011-12-27 11:18:46 +08:00
2006-08-24 21:39:04 +08:00
int pos = 1 ;
// debug( 0, L"parse_slice on '%ls'", in );
2011-12-27 11:18:46 +08:00
2006-08-24 21:39:04 +08:00
while ( 1 )
{
long tmp ;
2011-12-27 11:18:46 +08:00
2006-08-24 21:39:04 +08:00
while ( iswspace ( in [ pos ] ) | | ( in [ pos ] = = INTERNAL_SEPARATOR ) )
2011-12-27 11:18:46 +08:00
pos + + ;
2006-08-24 21:39:04 +08:00
if ( in [ pos ] = = L ' ] ' )
{
pos + + ;
break ;
}
2011-12-27 11:18:46 +08:00
2006-08-24 21:39:04 +08:00
errno = 0 ;
tmp = wcstol ( & in [ pos ] , & end , 10 ) ;
if ( ( errno ) | | ( end = = & in [ pos ] ) )
{
return 1 ;
}
// debug( 0, L"Push idx %d", tmp );
2011-12-27 11:18:46 +08:00
2006-08-24 21:39:04 +08:00
al_push_long ( idx , tmp ) ;
pos = end - in ;
}
2011-12-27 11:18:46 +08:00
2006-08-24 21:39:04 +08:00
if ( end_ptr )
{
// debug( 0, L"Remainder is '%ls', slice def was %d characters long", in+pos, pos );
2011-12-27 11:18:46 +08:00
* end_ptr = ( wchar_t * ) ( in + pos ) ;
2006-08-24 21:39:04 +08:00
}
// debug( 0, L"ok, done" );
2011-12-27 11:18:46 +08:00
return 0 ;
}
2010-09-18 09:51:16 +08:00
2011-12-27 11:18:46 +08:00
static int parse_slice2 ( const wchar_t * in , wchar_t * * end_ptr , std : : vector < long > & idx )
{
wchar_t * end ;
int pos = 1 ;
// debug( 0, L"parse_slice on '%ls'", in );
while ( 1 )
{
long tmp ;
while ( iswspace ( in [ pos ] ) | | ( in [ pos ] = = INTERNAL_SEPARATOR ) )
pos + + ;
if ( in [ pos ] = = L ' ] ' )
{
pos + + ;
break ;
}
errno = 0 ;
tmp = wcstol ( & in [ pos ] , & end , 10 ) ;
if ( ( errno ) | | ( end = = & in [ pos ] ) )
{
return 1 ;
}
// debug( 0, L"Push idx %d", tmp );
idx . push_back ( tmp ) ;
pos = end - in ;
}
if ( end_ptr )
{
// debug( 0, L"Remainder is '%ls', slice def was %d characters long", in+pos, pos );
* end_ptr = ( wchar_t * ) ( in + pos ) ;
}
// debug( 0, L"ok, done" );
2006-08-24 21:39:04 +08:00
return 0 ;
}
2006-02-13 03:03:01 +08:00
/**
Expand all environment variables in the string * ptr .
This function is slow , fragile and complicated . There are lots of
little corner cases , like $ $ foo should do a double expansion ,
$ foo $ bar should not double expand bar , etc . Also , it ' s easy to
accidentally leak memory on array out of bounds errors an various
other situations . All in all , this function should be rewritten ,
split out into multiple logical units and carefully tested . After
that , it can probably be optimized to do fewer memory allocations ,
fewer string scans and overall just less work . But until that
happens , don ' t edit it unless you know exactly what you are doing ,
and do proper testing afterwards .
*/
static int expand_variables ( wchar_t * in , array_list_t * out , int last_idx )
2006-02-12 19:29:30 +08:00
{
2006-02-13 03:03:01 +08:00
wchar_t c ;
wchar_t prev_char = 0 ;
int i , j ;
int is_ok = 1 ;
int empty = 0 ;
static string_buffer_t * var_tmp = 0 ;
static array_list_t * var_idx_list = 0 ;
2006-07-21 09:08:31 +08:00
CHECK ( in , 0 ) ;
CHECK ( out , 0 ) ;
2011-12-27 11:18:46 +08:00
2006-02-12 19:29:30 +08:00
if ( ! var_tmp )
{
var_tmp = sb_halloc ( global_context ) ;
if ( ! var_tmp )
2006-07-03 18:39:57 +08:00
DIE_MEM ( ) ;
2006-02-12 19:29:30 +08:00
}
else
{
sb_clear ( var_tmp ) ;
}
if ( ! var_idx_list )
{
var_idx_list = al_halloc ( global_context ) ;
if ( ! var_idx_list )
2006-07-03 18:39:57 +08:00
DIE_MEM ( ) ;
2006-02-12 19:29:30 +08:00
}
else
{
al_truncate ( var_idx_list , 0 ) ;
}
2006-01-31 03:53:10 +08:00
2006-02-12 19:29:30 +08:00
for ( i = last_idx ; ( i > = 0 ) & & is_ok & & ! empty ; i - - )
2005-09-20 21:26:39 +08:00
{
c = in [ i ] ;
2005-11-03 00:49:13 +08:00
if ( ( c = = VARIABLE_EXPAND ) | | ( c = = VARIABLE_EXPAND_SINGLE ) )
2005-09-20 21:26:39 +08:00
{
int start_pos = i + 1 ;
int stop_pos ;
int var_len , new_len ;
2012-01-13 00:09:00 +08:00
const wchar_t * var_val ;
2006-01-31 03:53:10 +08:00
wchar_t * new_in ;
2005-11-03 00:49:13 +08:00
int is_single = ( c = = VARIABLE_EXPAND_SINGLE ) ;
2006-02-12 19:29:30 +08:00
int var_name_stop_pos ;
2011-12-27 11:18:46 +08:00
2005-09-20 21:26:39 +08:00
stop_pos = start_pos ;
while ( 1 )
{
if ( ! ( in [ stop_pos ] ) )
break ;
if ( ! ( iswalnum ( in [ stop_pos ] ) | |
( wcschr ( L " _ " , in [ stop_pos ] ) ! = 0 ) ) )
break ;
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
stop_pos + + ;
}
2006-02-12 19:29:30 +08:00
var_name_stop_pos = stop_pos ;
2011-12-27 11:18:46 +08:00
2005-09-20 21:26:39 +08:00
/* printf( "Stop for '%c'\n", in[stop_pos]);*/
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
var_len = stop_pos - start_pos ;
2005-12-07 21:03:30 +08:00
if ( var_len = = 0 )
{
2011-12-27 11:18:46 +08:00
expand_variable_error ( in , stop_pos - 1 , - 1 ) ;
2005-12-07 21:03:30 +08:00
is_ok = 0 ;
break ;
}
2006-01-31 03:53:10 +08:00
2006-02-12 19:29:30 +08:00
sb_append_substring ( var_tmp , & in [ start_pos ] , var_len ) ;
2012-01-12 06:51:11 +08:00
var_val = expand_var ( ( wchar_t * ) var_tmp - > buff ) ;
2006-01-31 03:53:10 +08:00
2006-02-12 19:29:30 +08:00
if ( var_val )
2005-11-03 00:49:13 +08:00
{
int all_vars = 1 ;
2006-02-12 19:29:30 +08:00
array_list_t var_item_list ;
al_init ( & var_item_list ) ;
2006-08-24 21:39:04 +08:00
2005-11-03 00:49:13 +08:00
if ( in [ stop_pos ] = = L ' [ ' )
2006-01-31 03:53:10 +08:00
{
2006-08-24 21:39:04 +08:00
wchar_t * slice_end ;
all_vars = 0 ;
2011-12-27 11:18:46 +08:00
2006-08-24 21:39:04 +08:00
if ( parse_slice ( & in [ stop_pos ] , & slice_end , var_idx_list ) )
2005-11-03 00:49:13 +08:00
{
2006-08-24 21:39:04 +08:00
error ( SYNTAX_ERROR ,
- 1 ,
2011-12-27 11:18:46 +08:00
L " Invalid index value " ) ;
2006-08-24 21:39:04 +08:00
is_ok = 0 ;
2011-12-27 11:18:46 +08:00
}
2006-08-24 21:39:04 +08:00
stop_pos = ( slice_end - in ) ;
2011-12-27 11:18:46 +08:00
}
2005-11-03 00:49:13 +08:00
if ( is_ok )
2006-01-31 03:53:10 +08:00
{
2006-05-29 19:13:42 +08:00
tokenize_variable_array ( var_val , & var_item_list ) ;
2005-11-03 00:49:13 +08:00
if ( ! all_vars )
2006-01-31 03:53:10 +08:00
{
2005-11-03 00:49:13 +08:00
int j ;
2006-02-12 19:29:30 +08:00
for ( j = 0 ; j < al_get_count ( var_idx_list ) ; j + + )
2005-11-03 00:49:13 +08:00
{
2006-08-01 00:55:11 +08:00
long tmp = al_get_long ( var_idx_list , j ) ;
2006-06-04 17:35:32 +08:00
if ( tmp < 0 )
{
tmp = al_get_count ( & var_item_list ) + tmp + 1 ;
}
2006-08-01 00:48:16 +08:00
/*
Check that we are within array
bounds . If not , truncate the list to
exit .
*/
2006-02-12 19:29:30 +08:00
if ( tmp < 1 | | tmp > al_get_count ( & var_item_list ) )
2005-09-20 21:26:39 +08:00
{
2006-01-31 03:53:10 +08:00
error ( SYNTAX_ERROR ,
2005-12-08 00:06:47 +08:00
- 1 ,
2006-06-05 04:14:51 +08:00
ARRAY_BOUNDS_ERR ) ;
2005-11-03 00:49:13 +08:00
is_ok = 0 ;
2006-02-12 19:29:30 +08:00
al_truncate ( var_idx_list , j ) ;
2005-11-03 00:49:13 +08:00
break ;
2006-01-31 03:53:10 +08:00
}
2005-11-03 00:49:13 +08:00
else
{
2006-08-01 00:48:16 +08:00
/* Replace each index in var_idx_list inplace with the string value at the specified index */
2011-12-27 11:18:46 +08:00
al_set ( var_idx_list , j , wcsdup ( ( const wchar_t * ) al_get ( & var_item_list , tmp - 1 ) ) ) ;
2005-09-20 21:26:39 +08:00
}
}
2006-08-01 00:48:16 +08:00
/* Free strings in list var_item_list and truncate it */
2006-06-13 05:47:42 +08:00
al_foreach ( & var_item_list , & free ) ;
2006-02-12 19:29:30 +08:00
al_truncate ( & var_item_list , 0 ) ;
2006-01-31 03:53:10 +08:00
/* Add items from list idx back to list l */
2006-02-12 19:29:30 +08:00
al_push_all ( & var_item_list , var_idx_list ) ;
2005-11-03 00:49:13 +08:00
}
2006-01-31 03:53:10 +08:00
}
2006-02-07 19:48:57 +08:00
if ( is_ok )
2005-11-03 00:49:13 +08:00
{
2011-12-27 11:18:46 +08:00
2006-02-07 19:48:57 +08:00
if ( is_single )
{
string_buffer_t res ;
in [ i ] = 0 ;
2011-12-27 11:18:46 +08:00
2006-02-12 19:29:30 +08:00
sb_init ( & res ) ;
2006-02-07 19:48:57 +08:00
sb_append ( & res , in ) ;
sb_append_char ( & res , INTERNAL_SEPARATOR ) ;
2006-01-31 03:53:10 +08:00
2006-02-12 19:29:30 +08:00
for ( j = 0 ; j < al_get_count ( & var_item_list ) ; j + + )
2006-01-31 03:53:10 +08:00
{
2006-02-12 19:29:30 +08:00
wchar_t * next = ( wchar_t * ) al_get ( & var_item_list , j ) ;
2011-12-27 11:18:46 +08:00
2006-02-07 19:48:57 +08:00
if ( is_ok )
{
if ( j ! = 0 )
sb_append ( & res , L " " ) ;
sb_append ( & res , next ) ;
}
free ( next ) ;
2005-11-03 00:49:13 +08:00
}
2006-02-07 19:48:57 +08:00
sb_append ( & res , & in [ stop_pos ] ) ;
2006-02-12 19:29:30 +08:00
is_ok & = expand_variables ( ( wchar_t * ) res . buff , out , i ) ;
2006-01-31 03:53:10 +08:00
}
2006-02-07 19:48:57 +08:00
else
2005-09-20 21:26:39 +08:00
{
2006-02-12 19:29:30 +08:00
for ( j = 0 ; j < al_get_count ( & var_item_list ) ; j + + )
2005-09-20 21:26:39 +08:00
{
2006-02-12 19:29:30 +08:00
wchar_t * next = ( wchar_t * ) al_get ( & var_item_list , j ) ;
if ( is_ok & & ( i = = 0 ) & & ( ! in [ stop_pos ] ) )
2005-09-20 21:26:39 +08:00
{
2006-02-12 19:29:30 +08:00
al_push ( out , next ) ;
}
else
{
2011-12-27 11:18:46 +08:00
2006-02-12 19:29:30 +08:00
if ( is_ok )
2006-02-07 19:48:57 +08:00
{
2006-02-12 19:29:30 +08:00
new_len = wcslen ( in ) - ( stop_pos - start_pos + 1 ) ;
new_len + = wcslen ( next ) + 2 ;
2011-12-27 11:18:46 +08:00
if ( ! ( new_in = ( wchar_t * ) malloc ( sizeof ( wchar_t ) * new_len ) ) )
2006-02-07 19:48:57 +08:00
{
2006-07-03 18:39:57 +08:00
DIE_MEM ( ) ;
2006-02-07 19:48:57 +08:00
}
else
2006-02-12 19:29:30 +08:00
{
2011-12-27 11:18:46 +08:00
2006-04-13 20:18:51 +08:00
wcslcpy ( new_in , in , start_pos ) ;
2006-02-12 19:29:30 +08:00
if ( start_pos > 1 & & new_in [ start_pos - 2 ] ! = VARIABLE_EXPAND )
{
new_in [ start_pos - 1 ] = INTERNAL_SEPARATOR ;
new_in [ start_pos ] = L ' \0 ' ;
}
else
new_in [ start_pos - 1 ] = L ' \0 ' ;
2011-12-27 11:18:46 +08:00
2006-02-12 19:29:30 +08:00
wcscat ( new_in , next ) ;
wcscat ( new_in , & in [ stop_pos ] ) ;
2011-12-27 11:18:46 +08:00
2006-02-12 19:29:30 +08:00
is_ok & = expand_variables ( new_in , out , i ) ;
}
2006-02-07 19:48:57 +08:00
}
2006-02-12 19:29:30 +08:00
free ( next ) ;
2005-09-20 21:26:39 +08:00
}
2011-12-27 11:18:46 +08:00
2005-09-20 21:26:39 +08:00
}
2006-01-31 03:53:10 +08:00
}
2005-09-20 21:26:39 +08:00
}
2011-12-27 11:18:46 +08:00
2005-11-03 00:49:13 +08:00
free ( in ) ;
2006-02-12 19:29:30 +08:00
al_destroy ( & var_item_list ) ;
2006-01-31 03:53:10 +08:00
return is_ok ;
2005-11-03 00:49:13 +08:00
}
else
{
/*
Expand a non - existing variable
*/
if ( c = = VARIABLE_EXPAND )
2005-09-20 21:26:39 +08:00
{
2005-11-03 00:49:13 +08:00
/*
2005-12-07 23:57:17 +08:00
Regular expansion , i . e . expand this argument to nothing
2005-11-03 00:49:13 +08:00
*/
2005-09-20 21:26:39 +08:00
empty = 1 ;
}
2005-11-03 00:49:13 +08:00
else
{
/*
2006-01-31 03:53:10 +08:00
Expansion to single argument .
2005-11-03 00:49:13 +08:00
*/
string_buffer_t res ;
sb_init ( & res ) ;
2006-01-31 03:53:10 +08:00
2005-11-03 00:49:13 +08:00
in [ i ] = 0 ;
2006-01-31 03:53:10 +08:00
2005-11-03 00:49:13 +08:00
sb_append ( & res , in ) ;
sb_append ( & res , & in [ stop_pos ] ) ;
2006-02-12 19:29:30 +08:00
is_ok & = expand_variables ( ( wchar_t * ) res . buff , out , i ) ;
2005-11-03 00:49:13 +08:00
free ( in ) ;
return is_ok ;
}
2005-09-20 21:26:39 +08:00
}
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
}
2006-01-31 03:53:10 +08:00
prev_char = c ;
2005-09-20 21:26:39 +08:00
}
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
if ( ! empty )
{
al_push ( out , in ) ;
}
else
{
free ( in ) ;
}
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
return is_ok ;
}
2012-01-17 00:56:47 +08:00
static int expand_variables2 ( wchar_t * in , std : : vector < completion_t > & out , int last_idx )
2011-12-27 11:18:46 +08:00
{
2012-01-17 00:56:47 +08:00
wchar_t c ;
wchar_t prev_char = 0 ;
int i , j ;
int is_ok = 1 ;
int empty = 0 ;
static string_buffer_t * var_tmp = 0 ;
static array_list_t * var_idx_list = 0 ;
CHECK ( in , 0 ) ;
// CHECK( out, 0 );
if ( ! var_tmp )
{
var_tmp = sb_halloc ( global_context ) ;
if ( ! var_tmp )
DIE_MEM ( ) ;
}
else
{
sb_clear ( var_tmp ) ;
}
if ( ! var_idx_list )
{
var_idx_list = al_halloc ( global_context ) ;
if ( ! var_idx_list )
DIE_MEM ( ) ;
}
else
{
al_truncate ( var_idx_list , 0 ) ;
}
for ( i = last_idx ; ( i > = 0 ) & & is_ok & & ! empty ; i - - )
{
c = in [ i ] ;
if ( ( c = = VARIABLE_EXPAND ) | | ( c = = VARIABLE_EXPAND_SINGLE ) )
{
int start_pos = i + 1 ;
int stop_pos ;
int var_len , new_len ;
const wchar_t * var_val ;
wchar_t * new_in ;
int is_single = ( c = = VARIABLE_EXPAND_SINGLE ) ;
int var_name_stop_pos ;
stop_pos = start_pos ;
while ( 1 )
{
if ( ! ( in [ stop_pos ] ) )
break ;
if ( ! ( iswalnum ( in [ stop_pos ] ) | |
( wcschr ( L " _ " , in [ stop_pos ] ) ! = 0 ) ) )
break ;
stop_pos + + ;
}
var_name_stop_pos = stop_pos ;
/* printf( "Stop for '%c'\n", in[stop_pos]);*/
var_len = stop_pos - start_pos ;
if ( var_len = = 0 )
{
expand_variable_error ( in , stop_pos - 1 , - 1 ) ;
is_ok = 0 ;
break ;
}
sb_append_substring ( var_tmp , & in [ start_pos ] , var_len ) ;
var_val = expand_var ( ( wchar_t * ) var_tmp - > buff ) ;
if ( var_val )
{
int all_vars = 1 ;
array_list_t var_item_list ;
al_init ( & var_item_list ) ;
if ( in [ stop_pos ] = = L ' [ ' )
{
wchar_t * slice_end ;
all_vars = 0 ;
if ( parse_slice ( & in [ stop_pos ] , & slice_end , var_idx_list ) )
{
error ( SYNTAX_ERROR ,
- 1 ,
L " Invalid index value " ) ;
is_ok = 0 ;
}
stop_pos = ( slice_end - in ) ;
}
if ( is_ok )
{
tokenize_variable_array ( var_val , & var_item_list ) ;
if ( ! all_vars )
{
int j ;
for ( j = 0 ; j < al_get_count ( var_idx_list ) ; j + + )
{
long tmp = al_get_long ( var_idx_list , j ) ;
if ( tmp < 0 )
{
tmp = al_get_count ( & var_item_list ) + tmp + 1 ;
}
/*
Check that we are within array
bounds . If not , truncate the list to
exit .
*/
if ( tmp < 1 | | tmp > al_get_count ( & var_item_list ) )
{
error ( SYNTAX_ERROR ,
- 1 ,
ARRAY_BOUNDS_ERR ) ;
is_ok = 0 ;
al_truncate ( var_idx_list , j ) ;
break ;
}
else
{
/* Replace each index in var_idx_list inplace with the string value at the specified index */
al_set ( var_idx_list , j , wcsdup ( ( const wchar_t * ) al_get ( & var_item_list , tmp - 1 ) ) ) ;
}
}
/* Free strings in list var_item_list and truncate it */
al_foreach ( & var_item_list , & free ) ;
al_truncate ( & var_item_list , 0 ) ;
/* Add items from list idx back to list l */
al_push_all ( & var_item_list , var_idx_list ) ;
}
}
if ( is_ok )
{
if ( is_single )
{
string_buffer_t res ;
in [ i ] = 0 ;
sb_init ( & res ) ;
sb_append ( & res , in ) ;
sb_append_char ( & res , INTERNAL_SEPARATOR ) ;
for ( j = 0 ; j < al_get_count ( & var_item_list ) ; j + + )
{
wchar_t * next = ( wchar_t * ) al_get ( & var_item_list , j ) ;
if ( is_ok )
{
if ( j ! = 0 )
sb_append ( & res , L " " ) ;
sb_append ( & res , next ) ;
}
free ( next ) ;
}
sb_append ( & res , & in [ stop_pos ] ) ;
is_ok & = expand_variables2 ( ( wchar_t * ) res . buff , out , i ) ;
}
else
{
for ( j = 0 ; j < al_get_count ( & var_item_list ) ; j + + )
{
wchar_t * next = ( wchar_t * ) al_get ( & var_item_list , j ) ;
if ( is_ok & & ( i = = 0 ) & & ( ! in [ stop_pos ] ) )
{
completion_t data_to_push = { next } ;
out . push_back ( data_to_push ) ;
}
else
{
if ( is_ok )
{
new_len = wcslen ( in ) - ( stop_pos - start_pos + 1 ) ;
new_len + = wcslen ( next ) + 2 ;
if ( ! ( new_in = ( wchar_t * ) malloc ( sizeof ( wchar_t ) * new_len ) ) )
{
DIE_MEM ( ) ;
}
else
{
wcslcpy ( new_in , in , start_pos ) ;
if ( start_pos > 1 & & new_in [ start_pos - 2 ] ! = VARIABLE_EXPAND )
{
new_in [ start_pos - 1 ] = INTERNAL_SEPARATOR ;
new_in [ start_pos ] = L ' \0 ' ;
}
else
new_in [ start_pos - 1 ] = L ' \0 ' ;
wcscat ( new_in , next ) ;
wcscat ( new_in , & in [ stop_pos ] ) ;
is_ok & = expand_variables2 ( new_in , out , i ) ;
}
}
free ( next ) ;
}
}
}
}
// free(in);
al_destroy ( & var_item_list ) ;
return is_ok ;
}
else
{
/*
Expand a non - existing variable
*/
if ( c = = VARIABLE_EXPAND )
{
/*
Regular expansion , i . e . expand this argument to nothing
*/
empty = 1 ;
}
else
{
/*
Expansion to single argument .
*/
string_buffer_t res ;
sb_init ( & res ) ;
in [ i ] = 0 ;
sb_append ( & res , in ) ;
sb_append ( & res , & in [ stop_pos ] ) ;
is_ok & = expand_variables2 ( ( wchar_t * ) res . buff , out , i ) ;
free ( in ) ;
return is_ok ;
}
}
}
prev_char = c ;
}
if ( ! empty )
{
completion_t data_to_push = { in } ;
out . push_back ( data_to_push ) ;
}
else
{
// free( in );
}
return is_ok ;
2011-12-27 11:18:46 +08:00
}
2005-09-20 21:26:39 +08:00
/**
Perform bracket expansion
*/
2012-01-17 00:56:47 +08:00
static int expand_brackets ( wchar_t * in , int flags , std : : vector < completion_t > & out )
2005-09-20 21:26:39 +08:00
{
wchar_t * pos ;
int syntax_error = 0 ;
2006-01-31 03:53:10 +08:00
int bracket_count = 0 ;
2005-09-20 21:26:39 +08:00
wchar_t * bracket_begin = 0 , * bracket_end = 0 ;
wchar_t * last_sep = 0 ;
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
wchar_t * item_begin ;
int len1 , len2 , tot_len ;
2006-06-20 08:50:10 +08:00
2006-07-21 09:08:31 +08:00
CHECK ( in , 0 ) ;
2012-01-17 00:56:47 +08:00
// CHECK( out, 0 );
2011-12-27 11:18:46 +08:00
2006-01-31 03:53:10 +08:00
for ( pos = in ;
2006-06-17 22:26:52 +08:00
( * pos ) & & ! syntax_error ;
2005-09-20 21:26:39 +08:00
pos + + )
{
switch ( * pos )
{
case BRACKET_BEGIN :
{
2006-06-17 22:26:52 +08:00
bracket_begin = pos ;
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
bracket_count + + ;
break ;
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
}
case BRACKET_END :
{
bracket_count - - ;
2006-06-17 22:26:52 +08:00
if ( bracket_end < bracket_begin )
{
2005-09-20 21:26:39 +08:00
bracket_end = pos ;
2006-06-17 22:26:52 +08:00
}
2011-12-27 11:18:46 +08:00
2005-09-20 21:26:39 +08:00
if ( bracket_count < 0 )
{
syntax_error = 1 ;
}
break ;
}
case BRACKET_SEP :
{
if ( bracket_count = = 1 )
last_sep = pos ;
}
}
}
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
if ( bracket_count > 0 )
{
if ( ! ( flags & ACCEPT_INCOMPLETE ) )
2006-06-17 22:26:52 +08:00
{
2005-09-20 21:26:39 +08:00
syntax_error = 1 ;
2006-06-17 22:26:52 +08:00
}
2005-09-20 21:26:39 +08:00
else
{
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
string_buffer_t mod ;
sb_init ( & mod ) ;
if ( last_sep )
{
sb_append_substring ( & mod , in , bracket_begin - in + 1 ) ;
sb_append ( & mod , last_sep + 1 ) ;
sb_append_char ( & mod , BRACKET_END ) ;
}
else
{
sb_append ( & mod , in ) ;
sb_append_char ( & mod , BRACKET_END ) ;
}
return expand_brackets ( ( wchar_t * ) mod . buff , 1 , out ) ;
}
}
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
if ( syntax_error )
{
2006-01-31 03:53:10 +08:00
error ( SYNTAX_ERROR ,
2005-12-08 00:06:47 +08:00
- 1 ,
2006-02-14 19:48:04 +08:00
_ ( L " Mismatched brackets " ) ) ;
2005-09-20 21:26:39 +08:00
return 0 ;
}
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
if ( bracket_begin = = 0 )
{
2012-01-17 00:56:47 +08:00
completion_t data_to_push = { in } ;
out . push_back ( data_to_push ) ;
2005-09-20 21:26:39 +08:00
return 1 ;
}
len1 = ( bracket_begin - in ) ;
len2 = wcslen ( bracket_end ) - 1 ;
tot_len = len1 + len2 ;
item_begin = bracket_begin + 1 ;
for ( pos = ( bracket_begin + 1 ) ; 1 ; pos + + )
{
if ( bracket_count = = 0 )
{
if ( ( * pos = = BRACKET_SEP ) | | ( pos = = bracket_end ) )
{
wchar_t * whole_item ;
int item_len = pos - item_begin ;
2006-01-31 03:53:10 +08:00
2011-12-27 11:18:46 +08:00
whole_item = ( wchar_t * ) malloc ( sizeof ( wchar_t ) * ( tot_len + item_len + 1 ) ) ;
2006-04-13 20:18:51 +08:00
wcslcpy ( whole_item , in , len1 + 1 ) ;
wcslcpy ( whole_item + len1 , item_begin , item_len + 1 ) ;
2005-09-20 21:26:39 +08:00
wcscpy ( whole_item + len1 + item_len , bracket_end + 1 ) ;
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
expand_brackets ( whole_item , flags , out ) ;
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
item_begin = pos + 1 ;
if ( pos = = bracket_end )
break ;
2006-01-31 03:53:10 +08:00
}
2005-09-20 21:26:39 +08:00
}
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
if ( * pos = = BRACKET_BEGIN )
{
bracket_count + + ;
}
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
if ( * pos = = BRACKET_END )
{
bracket_count - - ;
}
}
2006-01-31 03:53:10 +08:00
free ( in ) ;
return 1 ;
2005-09-20 21:26:39 +08:00
}
2012-01-17 00:56:47 +08:00
/*
static int expand_brackets2 ( const wcstring & in , int flags , std : : vector < wcstring > & outputs )
2011-12-27 11:18:46 +08:00
{
wcstring_adapter adapter ( in , outputs ) ;
return expand_brackets ( adapter . str , flags , & adapter . lst ) ;
}
2012-01-17 00:56:47 +08:00
*/
2005-09-20 21:26:39 +08:00
/**
2006-08-22 22:38:31 +08:00
Perform cmdsubst expansion
2005-09-20 21:26:39 +08:00
*/
2006-08-22 22:38:31 +08:00
static int expand_cmdsubst ( wchar_t * in , array_list_t * out )
2005-09-20 21:26:39 +08:00
{
2006-06-14 21:22:40 +08:00
wchar_t * paran_begin = 0 , * paran_end = 0 ;
2006-08-24 21:39:04 +08:00
int len1 ;
2005-09-20 21:26:39 +08:00
wchar_t prev = 0 ;
wchar_t * subcmd ;
2006-08-24 21:39:04 +08:00
array_list_t * sub_res , * tail_expand ;
2005-09-20 21:26:39 +08:00
int i , j ;
2006-01-31 00:51:50 +08:00
const wchar_t * item_begin ;
2011-12-27 11:18:46 +08:00
wchar_t * tail_begin = 0 ;
2006-08-24 21:39:04 +08:00
void * context ;
2005-09-20 21:26:39 +08:00
2006-07-21 09:08:31 +08:00
CHECK ( in , 0 ) ;
CHECK ( out , 0 ) ;
2011-12-27 11:18:46 +08:00
2006-08-24 21:39:04 +08:00
2006-01-31 03:53:10 +08:00
switch ( parse_util_locate_cmdsubst ( in ,
2006-01-31 00:51:50 +08:00
& paran_begin ,
& paran_end ,
0 ) )
2005-09-20 21:26:39 +08:00
{
case - 1 :
2006-01-31 03:53:10 +08:00
error ( SYNTAX_ERROR ,
2005-12-08 00:06:47 +08:00
- 1 ,
L " Mismatched parans " ) ;
2005-09-20 21:26:39 +08:00
return 0 ;
case 0 :
al_push ( out , in ) ;
2006-01-31 03:53:10 +08:00
return 1 ;
2005-09-20 21:26:39 +08:00
case 1 :
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
break ;
2006-01-31 03:53:10 +08:00
}
2005-09-20 21:26:39 +08:00
2006-08-24 21:39:04 +08:00
context = halloc ( 0 , 0 ) ;
2005-09-20 21:26:39 +08:00
len1 = ( paran_begin - in ) ;
prev = 0 ;
item_begin = paran_begin + 1 ;
2006-08-24 21:39:04 +08:00
sub_res = al_halloc ( context ) ;
2011-12-27 11:18:46 +08:00
if ( ! ( subcmd = ( wchar_t * ) halloc ( context , sizeof ( wchar_t ) * ( paran_end - paran_begin ) ) ) )
2005-09-20 21:26:39 +08:00
{
2011-12-27 11:18:46 +08:00
halloc_free ( context ) ;
2006-01-31 03:53:10 +08:00
return 0 ;
2005-09-20 21:26:39 +08:00
}
2006-01-31 03:53:10 +08:00
2006-04-13 20:18:51 +08:00
wcslcpy ( subcmd , paran_begin + 1 , paran_end - paran_begin ) ;
2006-01-31 03:53:10 +08:00
subcmd [ paran_end - paran_begin - 1 ] = 0 ;
2006-10-19 23:36:03 +08:00
if ( exec_subshell ( subcmd , sub_res ) = = - 1 )
{
halloc_free ( context ) ;
error ( CMDSUBST_ERROR , - 1 , L " Unknown error while evaulating command substitution " ) ;
return 0 ;
}
2006-08-24 21:39:04 +08:00
tail_begin = paran_end + 1 ;
if ( * tail_begin = = L ' [ ' )
{
array_list_t * slice_idx = al_halloc ( context ) ;
wchar_t * slice_end ;
2011-12-27 11:18:46 +08:00
2006-08-24 21:39:04 +08:00
if ( parse_slice ( tail_begin , & slice_end , slice_idx ) )
{
2011-12-27 11:18:46 +08:00
halloc_free ( context ) ;
2006-08-24 21:39:04 +08:00
error ( SYNTAX_ERROR , - 1 , L " Invalid index value " ) ;
return 0 ;
}
else
{
array_list_t * sub_res2 = al_halloc ( context ) ;
tail_begin = slice_end ;
for ( i = 0 ; i < al_get_count ( slice_idx ) ; i + + )
{
long idx = al_get_long ( slice_idx , i ) ;
if ( idx < 0 )
{
idx = al_get_count ( sub_res ) + idx + 1 ;
}
2011-12-27 11:18:46 +08:00
2006-08-24 21:39:04 +08:00
if ( idx < 1 | | idx > al_get_count ( sub_res ) )
{
halloc_free ( context ) ;
error ( SYNTAX_ERROR , - 1 , L " Invalid index value " ) ;
return 0 ;
}
2011-12-27 11:18:46 +08:00
2006-08-24 21:39:04 +08:00
idx = idx - 1 ;
2011-12-27 11:18:46 +08:00
2006-08-24 21:39:04 +08:00
al_push ( sub_res2 , al_get ( sub_res , idx ) ) ;
// debug( 0, L"Pushing item '%ls' with index %d onto sliced result", al_get( sub_res, idx ), idx );
2011-12-27 11:18:46 +08:00
2006-08-24 21:39:04 +08:00
al_set ( sub_res , idx , 0 ) ;
}
al_foreach ( sub_res , & free ) ;
2011-12-27 11:18:46 +08:00
sub_res = sub_res2 ;
2006-09-08 22:12:41 +08:00
}
2006-08-24 21:39:04 +08:00
}
tail_expand = al_halloc ( context ) ;
2006-01-31 03:53:10 +08:00
2006-08-24 21:39:04 +08:00
/*
Recursively call ourselves to expand any remaining command
2006-09-08 22:12:41 +08:00
substitutions . The result of this recursive call usiung the tail
2006-08-24 21:39:04 +08:00
of the string is inserted into the tail_expand array list
*/
expand_cmdsubst ( wcsdup ( tail_begin ) , tail_expand ) ;
2006-01-31 03:53:10 +08:00
2006-08-24 21:39:04 +08:00
/*
Combine the result of the current command substitution with the
2006-09-08 22:12:41 +08:00
result of the recursive tail expansion
2006-08-24 21:39:04 +08:00
*/
for ( i = 0 ; i < al_get_count ( sub_res ) ; i + + )
2005-09-20 21:26:39 +08:00
{
2005-10-07 18:36:51 +08:00
wchar_t * sub_item , * sub_item2 ;
2006-08-24 21:39:04 +08:00
sub_item = ( wchar_t * ) al_get ( sub_res , i ) ;
2006-02-06 23:15:52 +08:00
sub_item2 = escape ( sub_item , 1 ) ;
2006-01-31 03:53:10 +08:00
free ( sub_item ) ;
2005-10-07 18:36:51 +08:00
int item_len = wcslen ( sub_item2 ) ;
2006-01-31 03:53:10 +08:00
2006-08-24 21:39:04 +08:00
for ( j = 0 ; j < al_get_count ( tail_expand ) ; j + + )
2005-09-20 21:26:39 +08:00
{
string_buffer_t whole_item ;
2006-08-24 21:39:04 +08:00
wchar_t * tail_item = ( wchar_t * ) al_get ( tail_expand , j ) ;
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
sb_init ( & whole_item ) ;
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
sb_append_substring ( & whole_item , in , len1 ) ;
sb_append_char ( & whole_item , INTERNAL_SEPARATOR ) ;
2005-10-07 18:36:51 +08:00
sb_append_substring ( & whole_item , sub_item2 , item_len ) ;
2005-09-20 21:26:39 +08:00
sb_append_char ( & whole_item , INTERNAL_SEPARATOR ) ;
2006-01-31 03:53:10 +08:00
sb_append ( & whole_item , tail_item ) ;
2011-12-27 11:18:46 +08:00
2006-02-22 23:41:52 +08:00
al_push ( out , whole_item . buff ) ;
2005-09-20 21:26:39 +08:00
}
2011-12-27 11:18:46 +08:00
2005-10-07 18:36:51 +08:00
free ( sub_item2 ) ;
2006-01-31 03:53:10 +08:00
}
2005-09-20 21:26:39 +08:00
free ( in ) ;
2006-01-31 03:53:10 +08:00
2006-08-24 21:39:04 +08:00
al_foreach ( tail_expand , & free ) ;
2011-12-27 11:18:46 +08:00
halloc_free ( context ) ;
return 1 ;
}
/**
Perform cmdsubst expansion
*/
2012-01-17 00:56:47 +08:00
static int expand_cmdsubst2 ( const wcstring & input , std : : vector < completion_t > & outList )
2011-12-27 11:18:46 +08:00
{
wchar_t * paran_begin = 0 , * paran_end = 0 ;
int len1 ;
wchar_t prev = 0 ;
std : : vector < wcstring > sub_res ;
size_t i , j ;
const wchar_t * item_begin ;
wchar_t * tail_begin = 0 ;
const wchar_t * const in = input . c_str ( ) ;
2012-01-17 00:56:47 +08:00
completion_t data_to_push ;
2011-12-27 11:18:46 +08:00
switch ( parse_util_locate_cmdsubst ( in ,
& paran_begin ,
& paran_end ,
0 ) )
{
case - 1 :
error ( SYNTAX_ERROR ,
- 1 ,
L " Mismatched parans " ) ;
return 0 ;
case 0 :
2012-01-17 00:56:47 +08:00
data_to_push . completion = input ;
outList . push_back ( data_to_push ) ;
2011-12-27 11:18:46 +08:00
return 1 ;
case 1 :
break ;
}
len1 = ( paran_begin - in ) ;
prev = 0 ;
item_begin = paran_begin + 1 ;
const wcstring subcmd ( paran_begin + 1 , paran_end - paran_begin - 1 ) ;
if ( exec_subshell2 ( subcmd , sub_res ) = = - 1 )
{
error ( CMDSUBST_ERROR , - 1 , L " Unknown error while evaulating command substitution " ) ;
return 0 ;
}
tail_begin = paran_end + 1 ;
if ( * tail_begin = = L ' [ ' )
{
std : : vector < long > slice_idx ;
wchar_t * slice_end ;
if ( parse_slice2 ( tail_begin , & slice_end , slice_idx ) )
{
error ( SYNTAX_ERROR , - 1 , L " Invalid index value " ) ;
return 0 ;
}
else
{
std : : vector < wcstring > sub_res2 ;
tail_begin = slice_end ;
for ( i = 0 ; i < slice_idx . size ( ) ; i + + )
{
long idx = slice_idx . at ( i ) ;
if ( idx < 0 )
{
idx = sub_res . size ( ) + idx + 1 ;
}
if ( idx < 1 | | ( size_t ) idx > sub_res . size ( ) )
{
error ( SYNTAX_ERROR , - 1 , L " Invalid index value " ) ;
return 0 ;
}
idx = idx - 1 ;
sub_res2 . push_back ( sub_res . at ( idx ) ) ;
// debug( 0, L"Pushing item '%ls' with index %d onto sliced result", al_get( sub_res, idx ), idx );
//sub_res[idx] = 0; // ??
}
sub_res = sub_res2 ;
}
}
/*
Recursively call ourselves to expand any remaining command
substitutions . The result of this recursive call using the tail
of the string is inserted into the tail_expand array list
*/
2012-01-17 00:56:47 +08:00
std : : vector < completion_t > tail_expand ;
2011-12-27 11:18:46 +08:00
expand_cmdsubst2 ( tail_begin , tail_expand ) ;
/*
Combine the result of the current command substitution with the
result of the recursive tail expansion
*/
for ( i = 0 ; i < sub_res . size ( ) ; i + + )
{
wcstring sub_item = sub_res . at ( i ) ;
wcstring sub_item2 = escape_string ( sub_item , 1 ) ;
for ( j = 0 ; j < tail_expand . size ( ) ; j + + )
{
wcstring whole_item ;
2012-01-17 00:56:47 +08:00
wcstring tail_item = tail_expand . at ( j ) . completion ;
2011-12-27 11:18:46 +08:00
//sb_append_substring( &whole_item, in, len1 );
whole_item . append ( in , len1 ) ;
//sb_append_char( &whole_item, INTERNAL_SEPARATOR );
whole_item . push_back ( INTERNAL_SEPARATOR ) ;
//sb_append_substring( &whole_item, sub_item2, item_len );
whole_item . append ( sub_item2 ) ;
//sb_append_char( &whole_item, INTERNAL_SEPARATOR );
whole_item . push_back ( INTERNAL_SEPARATOR ) ;
//sb_append( &whole_item, tail_item );
whole_item . append ( tail_item ) ;
//al_push( out, whole_item.buff );
2012-01-17 00:56:47 +08:00
completion_t data_to_push ;
data_to_push . completion = whole_item ;
outList . push_back ( data_to_push ) ;
2011-12-27 11:18:46 +08:00
}
}
2006-01-31 03:53:10 +08:00
return 1 ;
2005-09-20 21:26:39 +08:00
}
2006-06-20 08:50:10 +08:00
/**
Wrapper around unescape funtion . Issues an error ( ) on failiure .
*/
2006-02-06 23:15:52 +08:00
static wchar_t * expand_unescape ( const wchar_t * in , int escape_special )
2005-09-20 21:26:39 +08:00
{
2005-10-07 18:36:51 +08:00
wchar_t * res = unescape ( in , escape_special ) ;
if ( ! res )
2005-12-08 00:06:47 +08:00
error ( SYNTAX_ERROR , - 1 , L " Unexpected end of string " ) ;
2005-10-07 18:36:51 +08:00
return res ;
2005-09-20 21:26:39 +08:00
}
2011-12-27 11:18:46 +08:00
static wcstring expand_unescape_string ( const wcstring & in , int escape_special )
{
wcstring tmp = in ;
unescape_string ( tmp , escape_special ) ;
/* Need to detect error here */
return tmp ;
}
2005-09-20 21:26:39 +08:00
/**
2011-12-27 11:18:46 +08:00
Attempts tilde expansion of the string specified , modifying it in place .
2005-09-20 21:26:39 +08:00
*/
2011-12-27 11:18:46 +08:00
static void expand_tilde_internal ( wcstring & input )
2005-09-20 21:26:39 +08:00
{
2011-12-27 11:18:46 +08:00
const wchar_t * const in = input . c_str ( ) ;
2005-09-20 21:26:39 +08:00
if ( in [ 0 ] = = HOME_DIRECTORY )
{
int tilde_error = 0 ;
2011-12-27 11:18:46 +08:00
size_t tail_idx ;
wcstring home ;
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
if ( in [ 1 ] = = ' / ' | | in [ 1 ] = = ' \0 ' )
{
/* Current users home directory */
2006-01-31 03:53:10 +08:00
2011-12-27 11:18:46 +08:00
home = env_get_string ( L " HOME " ) ;
tail_idx = 1 ;
2005-09-20 21:26:39 +08:00
}
else
{
/* Some other users home directory */
2011-12-27 11:18:46 +08:00
const wchar_t * name_end = wcschr ( in , L ' / ' ) ;
if ( name_end )
{
tail_idx = name_end - in ;
}
else
{
tail_idx = wcslen ( in ) ;
}
tail_idx = name_end - in ;
wcstring name_str = input . substr ( 1 , name_end - in - 1 ) ;
const char * name_cstr = wcs2str ( name_str . c_str ( ) ) ;
2006-01-31 03:53:10 +08:00
struct passwd * userinfo =
2011-12-27 11:18:46 +08:00
getpwnam ( name_cstr ) ;
free ( ( void * ) name_cstr ) ;
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
if ( userinfo = = 0 )
{
tilde_error = 1 ;
}
else
{
2011-12-27 11:18:46 +08:00
home = str2wcstring ( userinfo - > pw_dir ) ;
2005-09-20 21:26:39 +08:00
}
}
2006-01-31 03:53:10 +08:00
2011-12-27 11:18:46 +08:00
if ( ! tilde_error )
{
input . replace ( input . begin ( ) , input . begin ( ) + tail_idx , home ) ;
}
2005-09-20 21:26:39 +08:00
}
2006-01-31 03:53:10 +08:00
}
2005-09-20 21:26:39 +08:00
2011-12-27 11:18:46 +08:00
static wchar_t * expand_tilde_internal_compat ( wchar_t * in )
2005-09-20 21:26:39 +08:00
{
2011-12-27 11:18:46 +08:00
wcstring tmp = in ;
expand_tilde_internal ( tmp ) ;
free ( in ) ;
return wcsdup ( tmp . c_str ( ) ) ;
}
2010-09-18 09:51:16 +08:00
2011-12-27 11:18:46 +08:00
void expand_tilde ( wcstring & input )
{
if ( input [ 0 ] = = L ' ~ ' )
2005-09-20 21:26:39 +08:00
{
2011-12-27 11:18:46 +08:00
input [ 0 ] = HOME_DIRECTORY ;
expand_tilde_internal ( input ) ;
2005-09-20 21:26:39 +08:00
}
2011-12-27 11:18:46 +08:00
}
wchar_t * expand_tilde_compat ( wchar_t * input )
{
if ( input [ 0 ] = = L ' ~ ' ) {
input [ 0 ] = HOME_DIRECTORY ;
return expand_tilde_internal_compat ( input ) ;
}
return input ;
2005-09-20 21:26:39 +08:00
}
/**
Remove any internal separators . Also optionally convert wildcard characters to
2006-07-21 09:08:31 +08:00
regular equivalents . This is done to support EXPAND_SKIP_WILDCARDS .
2005-09-20 21:26:39 +08:00
*/
static void remove_internal_separator ( const void * s , int conv )
{
wchar_t * in = ( wchar_t * ) s ;
wchar_t * out = in ;
2011-12-27 11:18:46 +08:00
2006-07-21 09:08:31 +08:00
CHECK ( s , ) ;
2005-09-20 21:26:39 +08:00
while ( * in )
{
switch ( * in )
{
case INTERNAL_SEPARATOR :
in + + ;
break ;
2011-12-27 11:18:46 +08:00
2005-09-20 21:26:39 +08:00
case ANY_CHAR :
in + + ;
* out + + = conv ? L ' ? ' : ANY_CHAR ;
break ;
case ANY_STRING :
in + + ;
* out + + = conv ? L ' * ' : ANY_STRING ;
break ;
2006-01-31 03:53:10 +08:00
2009-02-03 07:47:51 +08:00
case ANY_STRING_RECURSIVE :
in + + ;
2009-02-21 18:43:30 +08:00
* out + + = conv ? L ' * ' : ANY_STRING_RECURSIVE ;
2009-02-03 07:47:51 +08:00
break ;
2005-09-20 21:26:39 +08:00
default :
* out + + = * in + + ;
}
}
* out = 0 ;
}
2011-12-27 11:18:46 +08:00
static void remove_internal_separator2 ( wcstring & s , int conv )
{
wchar_t * tmp = wcsdup ( s . c_str ( ) ) ;
remove_internal_separator ( tmp , conv ) ;
s = tmp ;
free ( tmp ) ;
}
2012-01-17 00:56:47 +08:00
int expand_string2 ( const wcstring & input , std : : vector < completion_t > & output , int flags )
2011-12-27 11:18:46 +08:00
{
2012-01-17 00:56:47 +08:00
std : : vector < completion_t > list1 , list2 ;
std : : vector < completion_t > * in , * out ;
2011-12-27 11:18:46 +08:00
size_t i ;
int cmdsubst_ok = 1 ;
int res = EXPAND_OK ;
if ( ( ! ( flags & ACCEPT_INCOMPLETE ) ) & & expand_is_clean ( input . c_str ( ) ) )
{
2012-01-17 00:56:47 +08:00
completion_t data_to_push ;
data_to_push . completion = input ;
output . push_back ( data_to_push ) ;
2011-12-27 11:18:46 +08:00
return EXPAND_OK ;
}
if ( EXPAND_SKIP_CMDSUBST & flags )
{
wchar_t * begin , * end ;
if ( parse_util_locate_cmdsubst ( input . c_str ( ) ,
& begin ,
& end ,
1 ) ! = 0 )
{
error ( CMDSUBST_ERROR , - 1 , L " Command substitutions not allowed " ) ;
return EXPAND_ERROR ;
}
2012-01-17 00:56:47 +08:00
completion_t data_to_push ;
data_to_push . completion = input ;
list1 . push_back ( data_to_push ) ;
2011-12-27 11:18:46 +08:00
}
else
{
cmdsubst_ok = expand_cmdsubst2 ( input , list1 ) ;
}
if ( ! cmdsubst_ok )
{
return EXPAND_ERROR ;
}
else
{
in = & list1 ;
out = & list2 ;
for ( i = 0 ; i < in - > size ( ) ; i + + )
{
/*
We accept incomplete strings here , since complete uses
expand_string to expand incomplete strings from the
commandline .
*/
int unescape_flags = UNESCAPE_SPECIAL | UNESCAPE_INCOMPLETE ;
2012-01-17 00:56:47 +08:00
wcstring next = expand_unescape_string ( in - > at ( i ) . completion , unescape_flags ) ;
2011-12-27 11:18:46 +08:00
if ( EXPAND_SKIP_VARIABLES & flags )
{
for ( size_t i = 0 ; i < next . size ( ) ; i + + ) {
if ( next . at ( i ) = = VARIABLE_EXPAND ) {
next [ i ] = L ' $ ' ;
}
}
2012-01-17 00:56:47 +08:00
completion_t data_to_push ;
data_to_push . completion = next ;
out - > push_back ( data_to_push ) ;
2011-12-27 11:18:46 +08:00
}
else
{
2012-01-17 00:56:47 +08:00
if ( ! expand_variables2 ( next . empty ( ) ? NULL : const_cast < wchar_t * > ( next . c_str ( ) ) , * out , next . size ( ) - 1 ) )
2011-12-27 11:18:46 +08:00
{
return EXPAND_ERROR ;
}
}
}
in - > clear ( ) ;
in = & list2 ;
out = & list1 ;
for ( i = 0 ; i < in - > size ( ) ; i + + )
{
2012-01-17 00:56:47 +08:00
wcstring next = in - > at ( i ) . completion ;
2011-12-27 11:18:46 +08:00
2012-01-17 00:56:47 +08:00
if ( ! expand_brackets ( const_cast < wchar_t * > ( next . c_str ( ) ) , flags , * out ) )
2011-12-27 11:18:46 +08:00
{
return EXPAND_ERROR ;
}
}
in - > clear ( ) ;
in = & list1 ;
out = & list2 ;
for ( i = 0 ; i < in - > size ( ) ; i + + )
{
2012-01-17 00:56:47 +08:00
wcstring next = in - > at ( i ) . completion ;
2011-12-27 11:18:46 +08:00
expand_tilde_internal ( next ) ;
if ( flags & ACCEPT_INCOMPLETE )
{
if ( next [ 0 ] = = PROCESS_EXPAND )
{
/*
If process expansion matches , we are not
interested in other completions , so we
short - circut and return
*/
2012-01-17 00:56:47 +08:00
expand_pid ( const_cast < wchar_t * > ( next . c_str ( ) ) , flags , output ) ;
2011-12-27 11:18:46 +08:00
return EXPAND_OK ;
}
else
{
2012-01-17 00:56:47 +08:00
completion_t data_to_push ;
data_to_push . completion = next ;
out - > push_back ( data_to_push ) ;
2011-12-27 11:18:46 +08:00
}
}
else
{
2012-01-17 00:56:47 +08:00
if ( ! expand_pid ( const_cast < wchar_t * > ( next . c_str ( ) ) , flags , * out ) )
2011-12-27 11:18:46 +08:00
{
return EXPAND_ERROR ;
}
}
}
in - > clear ( ) ;
in = & list2 ;
out = & list1 ;
for ( i = 0 ; i < in - > size ( ) ; i + + )
{
2012-01-17 00:56:47 +08:00
wcstring next_str = in - > at ( i ) . completion ;
2011-12-27 11:18:46 +08:00
int wc_res ;
remove_internal_separator2 ( next_str , EXPAND_SKIP_WILDCARDS & flags ) ;
const wchar_t * next = next_str . c_str ( ) ;
if ( ( ( flags & ACCEPT_INCOMPLETE ) & & ( ! ( flags & EXPAND_SKIP_WILDCARDS ) ) ) | |
wildcard_has ( next , 1 ) )
{
const wchar_t * start , * rest ;
2012-01-17 00:56:47 +08:00
std : : vector < completion_t > * list = out ;
2011-12-27 11:18:46 +08:00
if ( next [ 0 ] = = ' / ' )
{
start = L " / " ;
rest = & next [ 1 ] ;
}
else
{
start = L " " ;
rest = next ;
}
if ( flags & ACCEPT_INCOMPLETE )
{
list = & output ;
}
2012-01-17 00:56:47 +08:00
wc_res = wildcard_expand_string ( rest , start , flags , * list ) ;
2011-12-27 11:18:46 +08:00
if ( ! ( flags & ACCEPT_INCOMPLETE ) )
{
switch ( wc_res )
{
case 0 :
{
if ( ! ( flags & ACCEPT_INCOMPLETE ) )
{
if ( res = = EXPAND_OK )
res = EXPAND_WILDCARD_NO_MATCH ;
break ;
}
}
case 1 :
{
size_t j ;
res = EXPAND_WILDCARD_MATCH ;
2012-01-17 00:56:47 +08:00
sort_completions ( * out ) ;
2011-12-27 11:18:46 +08:00
for ( j = 0 ; j < out - > size ( ) ; j + + )
{
2012-01-17 00:56:47 +08:00
output . push_back ( out - > at ( j ) ) ;
2011-12-27 11:18:46 +08:00
}
out - > clear ( ) ;
break ;
}
case - 1 :
{
return EXPAND_ERROR ;
}
}
}
}
else
{
if ( flags & ACCEPT_INCOMPLETE )
{
}
else
{
2012-01-17 00:56:47 +08:00
completion_t data_to_push ;
data_to_push . completion = next ;
output . push_back ( data_to_push ) ;
2011-12-27 11:18:46 +08:00
}
}
}
}
return res ;
}
2007-02-19 07:25:20 +08:00
2005-09-20 21:26:39 +08:00
/**
2005-12-07 23:57:17 +08:00
The real expansion function . expand_one is just a wrapper around this one .
2005-09-20 21:26:39 +08:00
*/
2012-01-17 00:56:47 +08:00
/*
2006-02-09 23:50:20 +08:00
int expand_string ( void * context ,
wchar_t * str ,
2006-01-31 03:53:10 +08:00
array_list_t * end_out ,
2005-09-20 21:26:39 +08:00
int flags )
{
array_list_t list1 , list2 ;
array_list_t * in , * out ;
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
int i ;
2006-08-22 22:38:31 +08:00
int cmdsubst_ok = 1 ;
2005-12-04 00:43:56 +08:00
int res = EXPAND_OK ;
2006-02-09 23:50:20 +08:00
int start_count = al_get_count ( end_out ) ;
2006-01-31 03:53:10 +08:00
2006-06-21 08:48:36 +08:00
CHECK ( str , EXPAND_ERROR ) ;
CHECK ( end_out , EXPAND_ERROR ) ;
2006-05-22 06:16:04 +08:00
if ( ( ! ( flags & ACCEPT_INCOMPLETE ) ) & & expand_is_clean ( str ) )
2005-10-26 18:51:02 +08:00
{
2006-02-09 23:50:20 +08:00
halloc_register ( context , str ) ;
2005-10-26 18:51:02 +08:00
al_push ( end_out , str ) ;
2005-12-04 00:43:56 +08:00
return EXPAND_OK ;
2005-10-26 18:51:02 +08:00
}
2005-09-20 21:26:39 +08:00
al_init ( & list1 ) ;
al_init ( & list2 ) ;
2006-08-22 22:38:31 +08:00
if ( EXPAND_SKIP_CMDSUBST & flags )
2005-09-20 21:26:39 +08:00
{
2006-06-14 21:22:40 +08:00
wchar_t * begin , * end ;
2011-12-27 11:18:46 +08:00
2006-02-22 23:41:52 +08:00
if ( parse_util_locate_cmdsubst ( str ,
& begin ,
& end ,
1 ) ! = 0 )
2005-09-20 21:26:39 +08:00
{
2012-01-17 00:56:47 +08:00
error ( CMDSUBST_ERROR , 1 , L " Command substitutions not allowed " ) ;
2006-02-22 23:41:52 +08:00
free ( str ) ;
al_destroy ( & list1 ) ;
al_destroy ( & list2 ) ;
return EXPAND_ERROR ;
2006-01-31 03:53:10 +08:00
}
2005-09-20 21:26:39 +08:00
al_push ( & list1 , str ) ;
}
else
{
2006-08-22 22:38:31 +08:00
cmdsubst_ok = expand_cmdsubst ( str , & list1 ) ;
2005-09-20 21:26:39 +08:00
}
2006-08-22 22:38:31 +08:00
if ( ! cmdsubst_ok )
2005-09-20 21:26:39 +08:00
{
al_destroy ( & list1 ) ;
2005-12-04 00:43:56 +08:00
return EXPAND_ERROR ;
2005-09-20 21:26:39 +08:00
}
else
{
in = & list1 ;
out = & list2 ;
for ( i = 0 ; i < al_get_count ( in ) ; i + + )
{
2005-10-07 18:36:51 +08:00
wchar_t * next ;
2012-01-17 00:56:47 +08:00
*/
2007-01-19 00:27:00 +08:00
/*
We accept incomplete strings here , since complete uses
expand_string to expand incomplete strings from the
commandline .
*/
2012-01-17 00:56:47 +08:00
/* int unescape_flags = UNESCAPE_SPECIAL | UNESCAPE_INCOMPLETE;
2007-01-19 00:27:00 +08:00
next = expand_unescape ( ( wchar_t * ) al_get ( in , i ) , unescape_flags ) ;
2005-10-07 18:36:51 +08:00
free ( ( void * ) al_get ( in , i ) ) ;
2006-01-31 03:53:10 +08:00
2005-10-07 18:36:51 +08:00
if ( ! next )
2006-02-22 23:41:52 +08:00
{
debug ( 2 , L " Got null string on line %d of file %s " , __LINE__ , __FILE__ ) ;
2011-12-27 11:18:46 +08:00
continue ;
2006-02-22 23:41:52 +08:00
}
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
if ( EXPAND_SKIP_VARIABLES & flags )
{
wchar_t * tmp ;
for ( tmp = next ; * tmp ; tmp + + )
if ( * tmp = = VARIABLE_EXPAND )
* tmp = L ' $ ' ;
al_push ( out , next ) ;
}
else
{
2006-02-12 19:29:30 +08:00
if ( ! expand_variables ( next , out , wcslen ( next ) - 1 ) )
2005-09-20 21:26:39 +08:00
{
al_destroy ( in ) ;
al_destroy ( out ) ;
2006-01-31 03:53:10 +08:00
return EXPAND_ERROR ;
2005-09-20 21:26:39 +08:00
}
}
}
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
al_truncate ( in , 0 ) ;
in = & list2 ;
out = & list1 ;
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
for ( i = 0 ; i < al_get_count ( in ) ; i + + )
{
wchar_t * next = ( wchar_t * ) al_get ( in , i ) ;
2006-02-22 23:41:52 +08:00
if ( ! next )
{
debug ( 2 , L " Got null string on line %d of file %s " , __LINE__ , __FILE__ ) ;
2011-12-27 11:18:46 +08:00
continue ;
}
2006-02-22 23:41:52 +08:00
2012-01-17 00:56:47 +08:00
if ( ! expand_brackets ( next , flags , * out ) )
2005-09-20 21:26:39 +08:00
{
al_destroy ( in ) ;
al_destroy ( out ) ;
2005-12-04 00:43:56 +08:00
return EXPAND_ERROR ;
2005-09-20 21:26:39 +08:00
}
}
al_truncate ( in , 0 ) ;
in = & list1 ;
out = & list2 ;
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
for ( i = 0 ; i < al_get_count ( in ) ; i + + )
{
2006-01-31 03:53:10 +08:00
wchar_t * next = ( wchar_t * ) al_get ( in , i ) ;
2006-02-22 23:41:52 +08:00
if ( ! next )
{
debug ( 2 , L " Got null string on line %d of file %s " , __LINE__ , __FILE__ ) ;
2011-12-27 11:18:46 +08:00
continue ;
2010-09-18 09:51:16 +08:00
}
2006-02-22 23:41:52 +08:00
2011-12-27 11:18:46 +08:00
if ( ! ( next = expand_tilde_internal_compat ( next ) ) )
2005-09-20 21:26:39 +08:00
{
al_destroy ( in ) ;
al_destroy ( out ) ;
2005-12-04 00:43:56 +08:00
return EXPAND_ERROR ;
2005-09-20 21:26:39 +08:00
}
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
if ( flags & ACCEPT_INCOMPLETE )
{
if ( * next = = PROCESS_EXPAND )
2012-01-17 00:56:47 +08:00
{ */
2005-12-04 00:43:56 +08:00
/*
2005-12-07 23:57:17 +08:00
If process expansion matches , we are not
2005-12-04 00:43:56 +08:00
interested in other completions , so we
short - circut and return
*/
2012-01-17 00:56:47 +08:00
/* expand_pid( next, flags, end_out );
2005-09-20 21:26:39 +08:00
al_destroy ( in ) ;
al_destroy ( out ) ;
2005-12-04 00:43:56 +08:00
return EXPAND_OK ;
2005-09-20 21:26:39 +08:00
}
else
2007-02-24 16:11:31 +08:00
{
2005-09-20 21:26:39 +08:00
al_push ( out , next ) ;
2007-02-24 16:11:31 +08:00
}
2005-09-20 21:26:39 +08:00
}
else
{
if ( ! expand_pid ( next , flags , out ) )
{
al_destroy ( in ) ;
al_destroy ( out ) ;
2005-12-04 00:43:56 +08:00
return EXPAND_ERROR ;
2005-09-20 21:26:39 +08:00
}
}
}
al_truncate ( in , 0 ) ;
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
in = & list2 ;
2006-01-31 03:53:10 +08:00
out = & list1 ;
2005-09-20 21:26:39 +08:00
for ( i = 0 ; i < al_get_count ( in ) ; i + + )
{
wchar_t * next = ( wchar_t * ) al_get ( in , i ) ;
int wc_res ;
2006-02-22 23:41:52 +08:00
if ( ! next )
{
debug ( 2 , L " Got null string on line %d of file %s " , __LINE__ , __FILE__ ) ;
2011-12-27 11:18:46 +08:00
continue ;
}
2006-02-22 23:41:52 +08:00
2006-01-31 03:53:10 +08:00
remove_internal_separator ( next , EXPAND_SKIP_WILDCARDS & flags ) ;
2011-12-27 11:18:46 +08:00
2006-01-31 03:53:10 +08:00
if ( ( ( flags & ACCEPT_INCOMPLETE ) & & ( ! ( flags & EXPAND_SKIP_WILDCARDS ) ) ) | |
2005-09-20 21:26:39 +08:00
wildcard_has ( next , 1 ) )
{
2012-01-14 19:41:50 +08:00
const wchar_t * start , * rest ;
2007-02-24 16:11:31 +08:00
array_list_t * list = out ;
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
if ( next [ 0 ] = = ' / ' )
{
2007-02-24 16:11:31 +08:00
start = L " / " ;
rest = & next [ 1 ] ;
2005-09-20 21:26:39 +08:00
}
else
{
2007-02-24 16:11:31 +08:00
start = L " " ;
rest = next ;
}
if ( flags & ACCEPT_INCOMPLETE )
{
list = end_out ;
2005-09-20 21:26:39 +08:00
}
2011-12-27 11:18:46 +08:00
2007-02-24 16:11:31 +08:00
wc_res = wildcard_expand ( rest , start , flags , list ) ;
2005-09-20 21:26:39 +08:00
free ( next ) ;
2007-02-24 16:11:31 +08:00
if ( ! ( flags & ACCEPT_INCOMPLETE ) )
2005-09-20 21:26:39 +08:00
{
2011-12-27 11:18:46 +08:00
2007-02-24 16:11:31 +08:00
switch ( wc_res )
2006-02-22 23:41:52 +08:00
{
2007-02-24 16:11:31 +08:00
case 0 :
2006-01-31 03:53:10 +08:00
{
2007-02-24 16:11:31 +08:00
if ( ! ( flags & ACCEPT_INCOMPLETE ) )
{
if ( res = = EXPAND_OK )
res = EXPAND_WILDCARD_NO_MATCH ;
break ;
}
2005-09-20 21:26:39 +08:00
}
2011-12-27 11:18:46 +08:00
2007-02-24 16:11:31 +08:00
case 1 :
2006-02-22 23:41:52 +08:00
{
2007-02-24 16:11:31 +08:00
int j ;
res = EXPAND_WILDCARD_MATCH ;
sort_list ( out ) ;
for ( j = 0 ; j < al_get_count ( out ) ; j + + )
2006-02-22 23:41:52 +08:00
{
2007-02-24 16:11:31 +08:00
wchar_t * next = ( wchar_t * ) al_get ( out , j ) ;
if ( ! next )
{
debug ( 2 , L " Got null string on line %d of file %s " , __LINE__ , __FILE__ ) ;
2011-12-27 11:18:46 +08:00
continue ;
}
2007-02-24 16:11:31 +08:00
al_push ( end_out , next ) ;
}
al_truncate ( out , 0 ) ;
break ;
2006-02-22 23:41:52 +08:00
}
2007-01-10 00:47:05 +08:00
2007-02-24 16:11:31 +08:00
case - 1 :
{
al_foreach ( out , & free ) ;
al_destroy ( in ) ;
al_destroy ( out ) ;
return EXPAND_ERROR ;
}
2011-12-27 11:18:46 +08:00
2007-02-24 16:11:31 +08:00
}
2006-01-31 03:53:10 +08:00
}
2011-12-27 11:18:46 +08:00
2005-09-20 21:26:39 +08:00
}
else
{
if ( flags & ACCEPT_INCOMPLETE )
2006-02-22 23:41:52 +08:00
{
2005-09-20 21:26:39 +08:00
free ( next ) ;
2006-02-22 23:41:52 +08:00
}
2005-09-20 21:26:39 +08:00
else
2006-02-22 23:41:52 +08:00
{
2005-09-20 21:26:39 +08:00
al_push ( end_out , next ) ;
2006-02-22 23:41:52 +08:00
}
2006-01-31 03:53:10 +08:00
}
2007-02-24 16:11:31 +08:00
2005-09-20 21:26:39 +08:00
}
al_destroy ( in ) ;
al_destroy ( out ) ;
}
2006-02-09 23:50:20 +08:00
if ( context )
{
for ( i = start_count ; i < al_get_count ( end_out ) ; i + + )
{
2006-02-10 08:35:39 +08:00
halloc_register ( context , ( void * ) al_get ( end_out , i ) ) ;
2006-02-09 23:50:20 +08:00
}
}
2007-02-19 07:25:20 +08:00
2011-12-27 11:18:46 +08:00
2005-12-04 00:43:56 +08:00
return res ;
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
}
2012-01-17 00:56:47 +08:00
*/
2005-09-20 21:26:39 +08:00
2006-02-09 23:50:20 +08:00
wchar_t * expand_one ( void * context , wchar_t * string , int flags )
2005-09-20 21:26:39 +08:00
{
2012-01-17 00:56:47 +08:00
std : : vector < completion_t > l ;
2005-09-20 21:26:39 +08:00
int res ;
wchar_t * one ;
2006-06-21 08:48:36 +08:00
CHECK ( string , 0 ) ;
2011-12-27 11:18:46 +08:00
2006-05-22 06:16:04 +08:00
if ( ( ! ( flags & ACCEPT_INCOMPLETE ) ) & & expand_is_clean ( string ) )
2006-02-09 23:50:20 +08:00
{
halloc_register ( context , string ) ;
2006-01-31 03:53:10 +08:00
return string ;
2006-02-09 23:50:20 +08:00
}
2011-12-27 11:18:46 +08:00
2012-01-17 00:56:47 +08:00
// al_init( &l );
res = expand_string2 ( string , l , flags ) ;
2005-09-20 21:26:39 +08:00
if ( ! res )
{
one = 0 ;
}
else
{
2012-01-17 00:56:47 +08:00
if ( l . size ( ) ! = 1 )
2005-09-20 21:26:39 +08:00
{
one = 0 ;
}
else
{
2012-01-17 00:56:47 +08:00
one = wcsdup ( l . at ( 0 ) . completion . c_str ( ) ) ;
// al_set( &l, 0, 0 );
2005-09-20 21:26:39 +08:00
}
}
2005-10-26 18:51:02 +08:00
2012-01-17 00:56:47 +08:00
// al_foreach( &l, &free );
// al_destroy( &l );
2006-02-09 23:50:20 +08:00
2006-02-10 08:35:39 +08:00
halloc_register ( context , one ) ;
2005-09-20 21:26:39 +08:00
return one ;
}
2012-01-17 00:56:47 +08:00
2005-09-20 21:26:39 +08:00