API cleanup

darcs-hash:20051007103651-ac50b-3b518ce795adf2d39b8ebfcba6a2df7c36411b15.gz
This commit is contained in:
axel 2005-10-07 20:36:51 +10:00
parent b11aa09142
commit 8ff36deeb4
13 changed files with 171 additions and 156 deletions

View File

@ -1744,12 +1744,12 @@ static int builtin_complete( wchar_t **argv )
case 'p':
cmd_type = PATH;
cmd = expand_backslash( wcsdup(woptarg), 1);
cmd = expand_unescape( woptarg, 1);
break;
case 'c':
cmd_type = COMMAND;
cmd = expand_backslash( wcsdup(woptarg), 1);
cmd = expand_unescape( woptarg, 1);
break;
case 'd':
@ -2744,7 +2744,7 @@ static int builtin_case( wchar_t **argv )
for( i=1; i<argc; i++ )
{
free( unescaped );
unescaped = expand_backslash( wcsdup( argv[i] ), 1);
unescaped = expand_unescape( argv[i], 1);
if( wildcard_match( current_block->switch_value, unescaped ) )
{

View File

@ -221,7 +221,7 @@ static int fill_buffer_from_list(string_buffer_t *sb, array_list_t *list)
Print the names of all environment variables in the scope, with or without values,
with or without escaping
*/
static void print_variables(int include_values, int escape, int scope)
static void print_variables(int include_values, int esc, int scope)
{
array_list_t names;
int i;
@ -234,14 +234,15 @@ static void print_variables(int include_values, int escape, int scope)
for( i = 0; i < al_get_count(&names); i++ )
{
wchar_t *key = (wchar_t *)al_get( &names, i );
/* Why does expand_escape free its argument ?! */
wchar_t *e_key = escape ? expand_escape(wcsdup(key), 1) : wcsdup(key);
wchar_t *e_key = esc ? escape(key, 1) : wcsdup(key);
sb_append(sb_out, e_key);
if( include_values )
{
wchar_t *value = env_get(key);
wchar_t *e_value = escape ? expand_escape_variable(value) : wcsdup(value);
wchar_t *e_value;
e_value = esc ? expand_escape_variable(value) : wcsdup(value);
sb_append2(sb_out, L" ", e_value, (void *)0);
free(e_value);
}

200
common.c
View File

@ -809,98 +809,98 @@ void debug( int level, wchar_t *msg, ... )
fwprintf( stderr, L"\n" );
}
wchar_t *escape( wchar_t *in,
wchar_t *escape( const wchar_t *in,
int escape_all )
{
wchar_t *killme=in;
wchar_t *out = malloc( sizeof(wchar_t)*(wcslen(in)*4 + 1));
if( out != 0 )
wchar_t *pos=out;
if( !out )
die_mem();
while( *in != 0 )
{
wchar_t *pos=out;
while( *in != 0 )
switch( *in )
{
switch( *in )
{
case L'\t':
*(pos++) = L'\\';
*(pos++) = L't';
break;
case L'\t':
*(pos++) = L'\\';
*(pos++) = L't';
break;
case L'\n':
*(pos++) = L'\\';
*(pos++) = L'n';
break;
case L'\n':
*(pos++) = L'\\';
*(pos++) = L'n';
break;
case L'\b':
*(pos++) = L'\\';
*(pos++) = L'b';
break;
case L'\b':
*(pos++) = L'\\';
*(pos++) = L'b';
break;
case L'\r':
*(pos++) = L'\\';
*(pos++) = L'r';
break;
case L'\r':
*(pos++) = L'\\';
*(pos++) = L'r';
break;
case L'\e':
*(pos++) = L'\\';
*(pos++) = L'e';
break;
case L'\e':
*(pos++) = L'\\';
*(pos++) = L'e';
break;
case L'\\':
case L'&':
case L'$':
case L' ':
case L'#':
case L'^':
case L'<':
case L'>':
case L'@':
case L'(':
case L')':
case L'{':
case L'}':
case L'?':
case L'*':
case L'|':
case L';':
case L':':
case L'\'':
case L'\"':
if( escape_all )
*pos++ = L'\\';
case L'\\':
case L'&':
case L'$':
case L' ':
case L'#':
case L'^':
case L'<':
case L'>':
case L'@':
case L'(':
case L')':
case L'{':
case L'}':
case L'?':
case L'*':
case L'|':
case L';':
case L':':
case L'\'':
case L'\"':
if( escape_all )
*pos++ = L'\\';
*pos++ = *in;
break;
default:
if( *in < 32 )
{
int tmp = (*in)%16;
*pos++ = L'\\';
*pos++ = L'x';
*pos++ = ((*in>15)? L'1' : L'0');
*pos++ = tmp > 9? L'a'+(tmp-10):L'0'+tmp;
}
else
*pos++ = *in;
break;
default:
if( *in < 32 )
{
int tmp = (*in)%16;
*pos++ = L'\\';
*pos++ = L'x';
*pos++ = ((*in>15)? L'1' : L'0');
*pos++ = tmp > 9? L'a'+(tmp-10):L'0'+tmp;
}
else
*pos++ = *in;
break;
}
in++;
break;
}
*pos = 0;
free(killme);
in++;
}
*pos = 0;
return out;
}
wchar_t *unescape( wchar_t * in, int escape_special )
wchar_t *unescape( const wchar_t * orig, int escape_special )
{
int in_pos, out_pos, len = wcslen( in );
int in_pos, out_pos, len = wcslen( orig );
int c;
int bracket_count=0;
wchar_t prev=0;
wchar_t *in = wcsdup(orig);
if( !in )
die_mem();
for( in_pos=0, out_pos=0; in_pos<len; prev=in[out_pos], out_pos++, in_pos++ )
{
c = in[in_pos];
@ -909,10 +909,8 @@ wchar_t *unescape( wchar_t * in, int escape_special )
switch( in[++in_pos] )
{
case L'\0':
error( SYNTAX_ERROR, L"Unexpected end of string", -1 );
return in;
break;
free(in);
return 0;
case L'n':
in[out_pos]=L'\n';
@ -1067,9 +1065,9 @@ wchar_t *unescape( wchar_t * in, int escape_special )
int len;
if( end == 0 )
{
error( SYNTAX_ERROR, L"Unexpected end of string", -1 );
return in;
{
free(in);
return 0;
}
len = end- &in[in_pos]-1;
@ -1150,10 +1148,10 @@ static int sprint_rand_digits( char *str, int maxlen )
struct timeval tv;
/*
Seed the pseudo-random generator based on time - this assumes
that consecutive calls to gettimeofday will return different values
and ignores errors returned by gettimeofday.
Cast to unsigned so that wrapping occurs on overflow as per ANSI C.
Seed the pseudo-random generator based on time - this assumes
that consecutive calls to gettimeofday will return different values
and ignores errors returned by gettimeofday.
Cast to unsigned so that wrapping occurs on overflow as per ANSI C.
*/
(void)gettimeofday( &tv, NULL );
srand( (unsigned int)tv.tv_sec + (unsigned int)tv.tv_usec * 1000000UL );
@ -1190,9 +1188,9 @@ static char *gen_unique_nfs_filename( const char *filename )
hostname[hnlen] = '\0';
}
newname = malloc( orglen + 1 /* period */ + hnlen + 1 /* period */ +
/* max possible pid size: 0.31 ~= log(10)2 */
(int)(0.31 * sizeof(pid_t) * CHAR_BIT + 1)
+ 1 /* '\0' */ );
/* max possible pid size: 0.31 ~= log(10)2 */
(int)(0.31 * sizeof(pid_t) * CHAR_BIT + 1)
+ 1 /* '\0' */ );
if ( newname == NULL )
{
@ -1206,13 +1204,13 @@ static char *gen_unique_nfs_filename( const char *filename )
pidlen = sprint_pid_t( getpid(), newname + orglen + 1 + hnlen + 1 );
newname[orglen + 1 + hnlen + 1 + pidlen] = '\0';
/* debug( 1, L"gen_unique_nfs_filename returning with: newname = \"%s\"; "
L"HOST_NAME_MAX = %d; hnlen = %d; orglen = %d; "
L"sizeof(pid_t) = %d; maxpiddigits = %d; malloc'd size: %d",
newname, (int)HOST_NAME_MAX, hnlen, orglen,
(int)sizeof(pid_t),
(int)(0.31 * sizeof(pid_t) * CHAR_BIT + 1),
(int)(orglen + 1 + hnlen + 1 +
(int)(0.31 * sizeof(pid_t) * CHAR_BIT + 1) + 1) ); */
L"HOST_NAME_MAX = %d; hnlen = %d; orglen = %d; "
L"sizeof(pid_t) = %d; maxpiddigits = %d; malloc'd size: %d",
newname, (int)HOST_NAME_MAX, hnlen, orglen,
(int)sizeof(pid_t),
(int)(0.31 * sizeof(pid_t) * CHAR_BIT + 1),
(int)(orglen + 1 + hnlen + 1 +
(int)(0.31 * sizeof(pid_t) * CHAR_BIT + 1) + 1) ); */
return newname;
}
@ -1257,7 +1255,7 @@ int acquire_lock_file( const char *lockfile, const int timeout, int force )
if ( statbuf.st_nlink != 1 )
{
debug( 1, L"acquire_lock_file: number of hardlinks on unique "
L"tmpfile is %d instead of 1.", (int)statbuf.st_nlink );
L"tmpfile is %d instead of 1.", (int)statbuf.st_nlink );
goto done;
}
if( gettimeofday( &start, NULL ) != 0 )
@ -1281,7 +1279,7 @@ int acquire_lock_file( const char *lockfile, const int timeout, int force )
*/
if( link( linkfile, lockfile ) == 0 ||
( stat( linkfile, &statbuf ) == 0 &&
statbuf.st_nlink == 2 ) )
statbuf.st_nlink == 2 ) )
{
/* Successful lock */
ret = 1;
@ -1290,10 +1288,10 @@ int acquire_lock_file( const char *lockfile, const int timeout, int force )
elapsed = end.tv_sec + end.tv_usec/1000000.0 -
( start.tv_sec + start.tv_usec/1000000.0 );
/*
The check for elapsed < 0 is to deal with the unlikely event
that after the loop is entered the system time is set forward
past the loop's end time. This would otherwise result in a
(practically) infinite loop.
The check for elapsed < 0 is to deal with the unlikely event
that after the loop is entered the system time is set forward
past the loop's end time. This would otherwise result in a
(practically) infinite loop.
*/
if( timed_out || elapsed >= timeout || elapsed < 0 )
{
@ -1314,14 +1312,14 @@ int acquire_lock_file( const char *lockfile, const int timeout, int force )
force was not specified
*/
debug( 1, L"acquire_lock_file: timed out "
L"trying to obtain lockfile %s using "
L"linkfile %s", lockfile, linkfile );
L"trying to obtain lockfile %s using "
L"linkfile %s", lockfile, linkfile );
break;
}
}
nanosleep( &pollint, NULL );
} while( gettimeofday( &end, NULL ) == 0 );
done:
done:
/* The linkfile is not needed once the lockfile has been created */
(void)unlink( linkfile );
free( linkfile );

View File

@ -247,10 +247,10 @@ void debug( int level, wchar_t *msg, ... );
\return The escaped string, or 0 if there is not enough memory
*/
wchar_t *escape( wchar_t *in,
wchar_t *escape( const wchar_t *in,
int escape_all );
wchar_t *unescape( wchar_t * in, int escape_special );
wchar_t *unescape( const wchar_t * in, int escape_special );
void block();
void unblock();

View File

@ -820,7 +820,9 @@ static const wchar_t *complete_get_desc_suffix( const wchar_t *suff_orig )
}
}
suff = expand_escape( suff, 0 );
wchar_t *tmp = expand_escape( suff, 0 );
free(suff);
suff = tmp;
wchar_t *desc = (wchar_t *)hash_get( suffix_hash, suff );
@ -1043,7 +1045,7 @@ static void complete_cmd_desc( const wchar_t *cmd, array_list_t *comp )
return;
}
esc = expand_escape( wcsdup(cmd_start), 1 );
esc = expand_escape( cmd_start, 1 );
if( esc )
{
@ -1495,7 +1497,7 @@ void complete_load( wchar_t *cmd,
{
if( !tm || (*tm != buf.st_mtime ) )
{
wchar_t *esc = expand_escape( wcsdup((wchar_t *)path.buff), 1 );
wchar_t *esc = expand_escape( (wchar_t *)path.buff, 1 );
wchar_t *src_cmd = wcsdupcat( L". ", esc );
/* if( tm )
@ -2164,7 +2166,7 @@ static void append_switch( string_buffer_t *out,
if( !argument || argument==L"" )
return;
esc = expand_escape( wcsdup(argument), 1 );
esc = expand_escape( argument, 1 );
sb_printf( out, L" --%ls %ls", opt, esc );
free(esc);
}

View File

@ -219,13 +219,16 @@ static void parse_message( wchar_t *msg,
tmp = wcschr( name, L':' );
if( tmp )
{
wchar_t *val_unescaped;
wchar_t *key =malloc( sizeof( wchar_t)*(tmp-name+1));
memcpy( key, name, sizeof( wchar_t)*(tmp-name));
key[tmp-name]=0;
val = tmp+1;
val = unescape( wcsdup(val), 0 );
val = unescape( val, 0 );
var_entry_t *entry =
malloc( sizeof(var_entry_t) + sizeof(wchar_t)*(wcslen(val)+1) );
@ -386,7 +389,7 @@ message_t *create_message( int type,
val_in=L"";
}
wchar_t *esc = escape(wcsdup(val_in),1);
wchar_t *esc = escape(val_in,1);
if( !esc )
break;

View File

@ -324,7 +324,7 @@ static void event_fire_internal( event_t *event, array_list_t *arguments )
for( j=0; j<al_get_count(arguments); j++ )
{
wchar_t *arg_esc = escape( wcsdup( (wchar_t *)al_get( arguments, j)), 0 );
wchar_t *arg_esc = escape( (wchar_t *)al_get( arguments, j), 0 );
sb_append( b, L" " );
sb_append( b, arg_esc );
free( arg_esc );

View File

@ -115,7 +115,7 @@ void expand_variable_array( const wchar_t *val, array_list_t *out )
}
}
wchar_t *expand_escape( wchar_t *in,
wchar_t *expand_escape( const wchar_t *in,
int escape_all )
{
return escape( in, escape_all );
@ -177,7 +177,7 @@ wchar_t *expand_escape_variable( const wchar_t *in )
}
else
{
wchar_t *val = expand_escape( wcsdup(el), 1 );
wchar_t *val = expand_escape( el, 1 );
sb_append( &buff, val );
free( val );
}
@ -203,7 +203,7 @@ wchar_t *expand_escape_variable( const wchar_t *in )
}
else
{
wchar_t *val = expand_escape( wcsdup(el), 1 );
wchar_t *val = expand_escape( el, 1 );
sb_append( &buff, val );
free( val );
}
@ -1117,10 +1117,11 @@ static int expand_subshell( wchar_t *in, array_list_t *out )
for( i=0; i<al_get_count( &sub_res ); i++ )
{
wchar_t *sub_item;
wchar_t *sub_item, *sub_item2;
sub_item = (wchar_t *)al_get( &sub_res, i );
sub_item = expand_escape( sub_item, 1 );
int item_len = wcslen( sub_item );
sub_item2 = expand_escape( sub_item, 1 );
free(sub_item);
int item_len = wcslen( sub_item2 );
for( j=0; j<al_get_count( &tail_expand ); j++ )
{
@ -1131,14 +1132,14 @@ static int expand_subshell( wchar_t *in, array_list_t *out )
sb_append_substring( &whole_item, in, len1 );
sb_append_char( &whole_item, INTERNAL_SEPARATOR );
sb_append_substring( &whole_item, sub_item, item_len );
sb_append_substring( &whole_item, sub_item2, item_len );
sb_append_char( &whole_item, INTERNAL_SEPARATOR );
sb_append( &whole_item, tail_item );
al_push( out, whole_item.buff );
}
free( sub_item );
free( sub_item2 );
}
free(in);
@ -1152,9 +1153,12 @@ static int expand_subshell( wchar_t *in, array_list_t *out )
}
wchar_t *expand_backslash( wchar_t * in, int escape_special )
wchar_t *expand_unescape( const wchar_t * in, int escape_special )
{
return unescape( in, escape_special );
wchar_t *res = unescape( in, escape_special );
if( !res )
error( SYNTAX_ERROR, L"Unexpected end of string", -1 );
return res;
}
/**
@ -1178,7 +1182,6 @@ static int tilde_expand( wchar_t **ptr )
if( in[1] == '/' || in[1] == '\0' )
{
/* Current users home directory */
struct passwd *userinfo;
home = env_get( L"HOME" );
if( home )
@ -1350,9 +1353,16 @@ int expand_string( wchar_t *str,
for( i=0; i<al_get_count( in ); i++ )
{
wchar_t *next = expand_backslash((wchar_t *)al_get( in, i ),
1);
wchar_t *next;
next = expand_unescape( (wchar_t *)al_get( in, i ),
1);
free( (void *)al_get( in, i ) );
if( !next )
continue;
if( EXPAND_SKIP_VARIABLES & flags )
{
wchar_t *tmp;

View File

@ -110,15 +110,32 @@ int expand_string( wchar_t *in, array_list_t *out, int flag );
*/
wchar_t *expand_one( wchar_t *in, int flag );
/**
Expand backslashed escapes and substitute them with their unescaped
counterparts. Also optionally change the wildcards, the tilde
character and a few more into constants which are defined to be
outside of the valid character space, but still inside the valid
space for a wchar_t. This assumes that a wchar_t is at least 32
bits long AND that the characterset is UCS4 or some other 31-bit
character set.
The result must be free()d. The original string is not modified. If
an invalid sequence is specified, 0 is returned.
*/
wchar_t *expand_unescape( const wchar_t * in, int escape_special );
/**
Replace special characters with escape sequences. Newline is
replaced with \n, etc.
The result must be free()d. The original string is not modified.
\param in The string to be escaped
\param escape_all Whether all characters wich hold special meaning in fish (Pipe, semicolon, etc,) should be escaped, or only unprintable characters
\return The escaped string, or 0 if there is not enough memory
\return The escaped string
*/
wchar_t *expand_escape( wchar_t *in, int escape_all );
wchar_t *expand_escape( const wchar_t *in, int escape_all );
/**
Convert the variable value to a human readable form, i.e. escape things, handle arrays, etc. Suitable for pretty-printing.
@ -149,22 +166,6 @@ int expand_locate_subshell( wchar_t *in,
int flags );
/**
Expand backslashed escapes and substitute them with their unescaped
counterparts. Also optionally change the wildcards, the tilde
character and a few more into constants which are defined to be
outside of the valid character space, but still inside the valid
space for a wchar_t. This assumes that a wchar_t is at least 32
bits long AND that the characterset is UCS4 or some other 31-bit
character set.
Since removing the escape sequences can never lengthen the string,
the specified string is modified instead of allocating a new one.
*/
wchar_t *expand_backslash( wchar_t * in, int escape_special );
/**
Tokenize the specified string into the specified array_list_t.
Each new element is allocated using malloc and must be freed by the

View File

@ -284,7 +284,7 @@ static void completion_print( int cols,
}
else
{
wchar_t *tmp = escape( wcsdup(el), 1 );
wchar_t *tmp = escape( el, 1 );
whole_comp_width = my_wcswidth( tmp );
free(tmp);
}

4
kill.c
View File

@ -95,7 +95,7 @@ void kill_add( wchar_t *str )
wchar_t *disp;
if( (disp = env_get( L"DISPLAY" )) )
{
wchar_t *escaped_str = expand_escape( wcsdup(str), 1 );
wchar_t *escaped_str = expand_escape( str, 1 );
wchar_t *cmd = wcsdupcat2(L"echo ", escaped_str, L"|xsel -b",0);
exec_subshell( cmd, 0 );
free( cut_buffer );
@ -137,7 +137,7 @@ static void kill_check_x_buffer()
for( i=0; i<al_get_count( &list ); i++ )
{
wchar_t *next_line = expand_escape(wcsdup(al_get( &list, i )), 0);
wchar_t *next_line = expand_escape( (wchar_t *)al_get( &list, i ), 0);
if( i==0 )
{
new_cut_buffer = next_line;

View File

@ -290,7 +290,7 @@ void writestr_ellipsis( const wchar_t *str, int max_width )
int write_escaped_str( const wchar_t *str, int max_len )
{
wchar_t *out = escape( wcsdup(str), 1 );
wchar_t *out = escape( str, 1 );
int i;
int len = my_wcswidth( out );
int written=0;

View File

@ -1171,7 +1171,7 @@ static void completion_insert( wchar_t *val, int is_complete )
if( quote == L'\0' )
{
replaced = expand_escape( wcsdup(val), 1 );
replaced = expand_escape( val, 1 );
}
else
{
@ -1200,7 +1200,7 @@ static void completion_insert( wchar_t *val, int is_complete )
if( unescapable )
{
free( replaced );
wchar_t *tmp = expand_escape( wcsdup(val), 1 );
wchar_t *tmp = expand_escape( val, 1 );
replaced = wcsdupcat( L" ", tmp );
free( tmp);
replaced[0]=quote;
@ -1241,7 +1241,7 @@ static void run_pager( wchar_t *prefix, int is_quoted, array_list_t *comp )
if( !prefix || (wcslen(prefix)==0))
prefix_esc = wcsdup(L"\"\"");
else
prefix_esc = escape( wcsdup(prefix),1);
prefix_esc = escape( prefix,1);
sb_init( &cmd );
sb_printf( &cmd,
@ -1253,7 +1253,7 @@ static void run_pager( wchar_t *prefix, int is_quoted, array_list_t *comp )
for( i=0; i<al_get_count( comp); i++ )
{
wchar_t *el = escape( wcsdup((wchar_t*)al_get( comp, i )),1);
wchar_t *el = escape( (wchar_t*)al_get( comp, i ),1);
sb_printf( &cmd, L" %ls", el );
free(el);
}