aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar axel <axel@liljencrantz.se>2005-11-30 02:52:02 +1000
committerGravatar axel <axel@liljencrantz.se>2005-11-30 02:52:02 +1000
commit4a68a34c50781a4b775c9dc86301db85bca34d79 (patch)
tree671e822e9ca30986f85a41ce004e736c656ee9c8
parent9993ff07f2a89b249c7b39a017b87945f153e1e8 (diff)
Change stupid call signature for tilde expand function
darcs-hash:20051129165202-ac50b-f442d0d75864317cc70059fffe5e8eb956ad54a6.gz
-rw-r--r--expand.c56
-rw-r--r--wildcard.c39
2 files changed, 57 insertions, 38 deletions
diff --git a/expand.c b/expand.c
index 75f1f808..6c9322f6 100644
--- a/expand.c
+++ b/expand.c
@@ -99,16 +99,21 @@ static int is_clean( const wchar_t *in )
const wchar_t * str = in;
+ /*
+ Test characters that have a special meaning in the first character position
+ */
if( wcschr( UNCLEAN_FIRST, *str ) )
return 0;
+
+ /*
+ Test characters that have a special meaning in any character position
+ */
while( *str )
{
if( wcschr( UNCLEAN, *str ) )
return 0;
str++;
}
-
-// debug( 1, L"%ls", in );
return 1;
}
@@ -357,8 +362,6 @@ static int find_process( const wchar_t *proc,
wchar_t *result;
job_t *j;
-
-
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
- expansion is performed, the argument is freed and a new string is
- allocated in its place. Horrible call signature. Should be
- altered. Fugly!
+ expansion is performed, the original string is freed and a new
+ string allocated using malloc is returned, otherwise, the original
+ 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 )
{
int tilde_error = 0;
wchar_t *home=0;
- wchar_t *new_in;
- wchar_t *old_in;
+ wchar_t *new_in=0;
+ wchar_t *old_in=0;
// fwprintf( stderr, L"Tilde expand ~%ls\n", (*ptr)+1 );
if( in[1] == '/' || in[1] == '\0' )
@@ -1318,26 +1320,23 @@ static int tilde_expand( wchar_t **ptr )
free(name);
}
- if( !tilde_error )
+ if( !tilde_error && home && old_in )
{
new_in = wcsdupcat( home, old_in );
- free( in );
- in = new_in;
- free(home);
- *ptr = in;
- }
-
+ }
+ free(home);
+ free( 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'~' )
{
in[0] = HOME_DIRECTORY;
- tilde_expand( &in );
- return in;
+ return expand_tilde_internal( 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 *out=in;
-// int changed=0;
-
while( *in )
{
switch( *in )
@@ -1376,16 +1373,11 @@ static void remove_internal_separator( const void *s, int conv )
}
}
*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,
array_list_t *end_out,
@@ -1451,7 +1443,7 @@ int expand_string( wchar_t *str,
1);
free( (void *)al_get( in, i ) );
-
+
if( !next )
continue;
@@ -1498,7 +1490,7 @@ int expand_string( wchar_t *str,
for( i=0; i<al_get_count( 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( out );
diff --git a/wildcard.c b/wildcard.c
index 5ecdb916..4d291458 100644
--- a/wildcard.c
+++ b/wildcard.c
@@ -27,6 +27,12 @@
#include "reader.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 )
{
wchar_t prev=0;
@@ -312,7 +318,7 @@ int wildcard_expand( const wchar_t *wc,
int flags,
array_list_t *out )
{
-
+
/* Points to the end of the current wildcard segment */
wchar_t *wc_end;
@@ -440,10 +446,8 @@ int wildcard_expand( const wchar_t *wc,
continue;
}
-/* wprintf( L"Match %ls (%s) against %ls\n\n\n", name, "tjo", wc );*/
if( flags & ACCEPT_INCOMPLETE )
{
- /* wprintf( L"match %ls to %ls\n", name, wc );*/
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.
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;
+
+ /*
+ new_dir is a scratch area containing the full path to a file/directory we are iterating over
+ */
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 );
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 )
- ln = 1024;
+ ln = MAX_FILE_LENGTH;
free( narrow_dir_string );
}
new_dir= malloc( sizeof(wchar_t)*(base_len+ln+2) );
wc_str = wc_end?wcsndup(wc, wc_end-wc):wcsdup(wc);
+
if( (!new_dir) || (!wc_str) )
{
die_mem();