Change stupid call signature for tilde expand function

darcs-hash:20051129165202-ac50b-f442d0d75864317cc70059fffe5e8eb956ad54a6.gz
This commit is contained in:
axel 2005-11-30 02:52:02 +10:00
parent 9993ff07f2
commit 4a68a34c50
2 changed files with 57 additions and 38 deletions

View file

@ -99,16 +99,21 @@ static int is_clean( const wchar_t *in )
const wchar_t * str = in; const wchar_t * str = in;
/*
Test characters that have a special meaning in the first character position
*/
if( wcschr( UNCLEAN_FIRST, *str ) ) if( wcschr( UNCLEAN_FIRST, *str ) )
return 0; return 0;
/*
Test characters that have a special meaning in any character position
*/
while( *str ) while( *str )
{ {
if( wcschr( UNCLEAN, *str ) ) if( wcschr( UNCLEAN, *str ) )
return 0; return 0;
str++; str++;
} }
// debug( 1, L"%ls", in );
return 1; return 1;
} }
@ -357,8 +362,6 @@ static int find_process( const wchar_t *proc,
wchar_t *result; wchar_t *result;
job_t *j; job_t *j;
if( iswnumeric(proc) || (wcslen(proc)==0) ) if( iswnumeric(proc) || (wcslen(proc)==0) )
{ {
@ -1248,20 +1251,19 @@ wchar_t *expand_unescape( const wchar_t * in, int escape_special )
/** /**
Attempts tilde expansion. Of the string specified. If tilde Attempts tilde expansion. Of the string specified. If tilde
expansion is performed, the argument is freed and a new string is expansion is performed, the original string is freed and a new
allocated in its place. Horrible call signature. Should be string allocated using malloc is returned, otherwise, the original
altered. Fugly! string is returned.
*/ */
static int tilde_expand( wchar_t **ptr ) static wchar_t * expand_tilde_internal( wchar_t *in )
{ {
wchar_t *in = *ptr;
if( in[0] == HOME_DIRECTORY ) if( in[0] == HOME_DIRECTORY )
{ {
int tilde_error = 0; int tilde_error = 0;
wchar_t *home=0; wchar_t *home=0;
wchar_t *new_in; wchar_t *new_in=0;
wchar_t *old_in; wchar_t *old_in=0;
// fwprintf( stderr, L"Tilde expand ~%ls\n", (*ptr)+1 ); // fwprintf( stderr, L"Tilde expand ~%ls\n", (*ptr)+1 );
if( in[1] == '/' || in[1] == '\0' ) if( in[1] == '/' || in[1] == '\0' )
@ -1318,26 +1320,23 @@ static int tilde_expand( wchar_t **ptr )
free(name); free(name);
} }
if( !tilde_error ) if( !tilde_error && home && old_in )
{ {
new_in = wcsdupcat( home, old_in ); new_in = wcsdupcat( home, old_in );
free( in ); }
in = new_in; free(home);
free(home); free( in );
*ptr = in; return new_in;
}
} }
return 1; return in;
} }
wchar_t *expand_tilde(wchar_t *in) wchar_t *expand_tilde( wchar_t *in)
{ {
if( in[0] == L'~' ) if( in[0] == L'~' )
{ {
in[0] = HOME_DIRECTORY; in[0] = HOME_DIRECTORY;
tilde_expand( &in ); return expand_tilde_internal( in );
return in;
} }
return in; return in;
} }
@ -1351,8 +1350,6 @@ static void remove_internal_separator( const void *s, int conv )
wchar_t *in = (wchar_t *)s; wchar_t *in = (wchar_t *)s;
wchar_t *out=in; wchar_t *out=in;
// int changed=0;
while( *in ) while( *in )
{ {
switch( *in ) switch( *in )
@ -1376,16 +1373,11 @@ static void remove_internal_separator( const void *s, int conv )
} }
} }
*out=0; *out=0;
/* if( changed )
{
fwprintf( stderr, L" -> %ls\n", s );
}
*/
} }
/** /**
The real expansion function. All other expansion functions are wrappers to this one. The real expantion function. expand_one is just a wrapper around this one.
*/ */
int expand_string( wchar_t *str, int expand_string( wchar_t *str,
array_list_t *end_out, array_list_t *end_out,
@ -1451,7 +1443,7 @@ int expand_string( wchar_t *str,
1); 1);
free( (void *)al_get( in, i ) ); free( (void *)al_get( in, i ) );
if( !next ) if( !next )
continue; continue;
@ -1498,7 +1490,7 @@ int expand_string( wchar_t *str,
for( i=0; i<al_get_count( in ); i++ ) for( i=0; i<al_get_count( in ); i++ )
{ {
wchar_t *next = (wchar_t *)al_get( in, i ); wchar_t *next = (wchar_t *)al_get( in, i );
if(!tilde_expand( &next )) if( !(next=expand_tilde_internal( next ) ) )
{ {
al_destroy( in ); al_destroy( in );
al_destroy( out ); al_destroy( out );

View file

@ -27,6 +27,12 @@
#include "reader.h" #include "reader.h"
#include "expand.h" #include "expand.h"
/**
The maximum length of a filename token. This is a fallback value,
an attempt to find the true value using patchconf is always made.
*/
#define MAX_FILE_LENGTH 1024
int wildcard_has( const wchar_t *str, int internal ) int wildcard_has( const wchar_t *str, int internal )
{ {
wchar_t prev=0; wchar_t prev=0;
@ -312,7 +318,7 @@ int wildcard_expand( const wchar_t *wc,
int flags, int flags,
array_list_t *out ) array_list_t *out )
{ {
/* Points to the end of the current wildcard segment */ /* Points to the end of the current wildcard segment */
wchar_t *wc_end; wchar_t *wc_end;
@ -440,10 +446,8 @@ int wildcard_expand( const wchar_t *wc,
continue; continue;
} }
/* wprintf( L"Match %ls (%s) against %ls\n\n\n", name, "tjo", wc );*/
if( flags & ACCEPT_INCOMPLETE ) if( flags & ACCEPT_INCOMPLETE )
{ {
/* wprintf( L"match %ls to %ls\n", name, wc );*/
wchar_t *long_name = make_path( base_dir, name ); wchar_t *long_name = make_path( base_dir, name );
@ -493,21 +497,44 @@ int wildcard_expand( const wchar_t *wc,
Wilcard segment is not the last segment. Wilcard segment is not the last segment.
Recursively call wildcard_expand for all matching subdirectories. Recursively call wildcard_expand for all matching subdirectories.
*/ */
/*
wc_str is the part of the wildcarded string from the
beginning to the first slash
*/
wchar_t *wc_str; wchar_t *wc_str;
/*
new_dir is a scratch area containing the full path to a file/directory we are iterating over
*/
wchar_t *new_dir; wchar_t *new_dir;
static size_t ln=1024;
/*
The maximum length of a file element
*/
static size_t ln=MAX_FILE_LENGTH;
char * narrow_dir_string = wcs2str( dir_string ); char * narrow_dir_string = wcs2str( dir_string );
if( narrow_dir_string ) if( narrow_dir_string )
{ {
ln = pathconf( narrow_dir_string, _PC_NAME_MAX ); /* Find out how long the filename can be in a worst case scenario */ /*
Find out how long the filename can be in a worst case
scenario
*/
ln = pathconf( narrow_dir_string, _PC_NAME_MAX );
/*
If not specified, use som large number as fallback
*/
if( ln < 0 ) if( ln < 0 )
ln = 1024; ln = MAX_FILE_LENGTH;
free( narrow_dir_string ); free( narrow_dir_string );
} }
new_dir= malloc( sizeof(wchar_t)*(base_len+ln+2) ); new_dir= malloc( sizeof(wchar_t)*(base_len+ln+2) );
wc_str = wc_end?wcsndup(wc, wc_end-wc):wcsdup(wc); wc_str = wc_end?wcsndup(wc, wc_end-wc):wcsdup(wc);
if( (!new_dir) || (!wc_str) ) if( (!new_dir) || (!wc_str) )
{ {
die_mem(); die_mem();