Minor code cleanup

darcs-hash:20060419124315-ac50b-e7e9cafe6850e1fe805bad82278d15c2de327f5d.gz
This commit is contained in:
axel 2006-04-19 22:43:15 +10:00
parent 76bafbef2a
commit 112ea1759a
3 changed files with 43 additions and 24 deletions

View file

@ -26,7 +26,11 @@ Functions used for implementing the set builtin.
#include "parser.h" #include "parser.h"
#include "translate.h" #include "translate.h"
static void my_env_set( wchar_t *key, wchar_t *val, int scope ) /**
Call env_set. On error, print a description of the problem to
stderr.
*/
static void my_env_set( const wchar_t *key, const wchar_t *val, int scope )
{ {
switch( env_set( key, val, scope | ENV_USER ) ) switch( env_set( key, val, scope | ENV_USER ) )
{ {
@ -126,7 +130,12 @@ static int parse_fill_indexes( array_list_t *indexes,
/** /**
Update a list by writing the specified values at the specified indexes Update a list \c list by writing copies (using wcsdup) of the
values specified by \c values to the indexes specified by \c
indexes. The previous entries at the specidied position will be
free'd.
\return The number of elements in the list after the modifications have been made
*/ */
static int update_values( array_list_t *list, static int update_values( array_list_t *list,
array_list_t *indexes, array_list_t *indexes,
@ -134,16 +143,12 @@ static int update_values( array_list_t *list,
{ {
int i; int i;
//fwprintf(stderr, L"Scan complete\n");
/* Replace values where needed */ /* Replace values where needed */
for( i = 0; i < al_get_count(indexes); i++ ) for( i = 0; i < al_get_count(indexes); i++ )
{ {
int ind = *(int *) al_get(indexes, i) - 1; int ind = *(int *) al_get(indexes, i) - 1;
void *new = (void *) al_get(values, i); void *new = (void *) al_get(values, i);
if (al_get(list, ind) != 0)
{
free((void *) al_get(list, ind)); free((void *) al_get(list, ind));
}
al_set(list, ind, new != 0 ? wcsdup(new) : wcsdup(L"")); al_set(list, ind, new != 0 ? wcsdup(new) : wcsdup(L""));
} }
@ -181,8 +186,6 @@ static int erase_values(array_list_t *list, array_list_t *indexes)
int i; int i;
array_list_t result; array_list_t result;
//fwprintf(stderr, L"Starting with %d\n", al_get_count(list));
al_init(&result); al_init(&result);
for (i = 0; i < al_get_count(list); i++) for (i = 0; i < al_get_count(list); i++)
@ -201,8 +204,6 @@ static int erase_values(array_list_t *list, array_list_t *indexes)
al_push_all( list, &result ); al_push_all( list, &result );
al_destroy(&result); al_destroy(&result);
//fwprintf(stderr, L"Remaining: %d\n", al_get_count(&result));
return al_get_count(list); return al_get_count(list);
} }
@ -219,8 +220,6 @@ static int fill_buffer_from_list(string_buffer_t *sb, array_list_t *list)
wchar_t *v = (wchar_t *) al_get(list, i); wchar_t *v = (wchar_t *) al_get(list, i);
if (v != 0) if (v != 0)
{ {
// fwprintf(stderr, L".\n");
// fwprintf(stderr, L"Collecting %ls from %d\n", v, i);
sb_append(sb, v); sb_append(sb, v);
} }
if (i < al_get_count(list) - 1) if (i < al_get_count(list) - 1)
@ -272,6 +271,10 @@ static void print_variables(int include_values, int esc, int scope)
int builtin_set( wchar_t **argv ) int builtin_set( wchar_t **argv )
{ {
/**
Variables used for parsing the argument list
*/
const static struct woption const static struct woption
long_options[] = long_options[] =
{ {
@ -305,7 +308,7 @@ int builtin_set( wchar_t **argv )
} }
; ;
wchar_t short_options[] = L"+xglenuUq"; const wchar_t *short_options = L"+xglenuUq";
int argc = builtin_count_args(argv); int argc = builtin_count_args(argv);
@ -375,6 +378,15 @@ int builtin_set( wchar_t **argv )
} }
} }
/*
Ok, all arguments have been parsed, let's validate them
*/
/*
If we are checking the existance of a variable (-q) we can not
also specify scope
*/
if( query && (erase || list || global || local || universal || export || unexport ) ) if( query && (erase || list || global || local || universal || export || unexport ) )
{ {
sb_printf(sb_err, sb_printf(sb_err,
@ -387,7 +399,7 @@ int builtin_set( wchar_t **argv )
} }
/* Check operation and modifiers sanity */ /* We can't both list and erase varaibles */
if( erase && list ) if( erase && list )
{ {
sb_printf(sb_err, sb_printf(sb_err,
@ -399,6 +411,9 @@ int builtin_set( wchar_t **argv )
return 1; return 1;
} }
/*
Variables can only have one scope
*/
if( local + global + universal > 1 ) if( local + global + universal > 1 )
{ {
sb_printf( sb_err, sb_printf( sb_err,
@ -409,6 +424,9 @@ int builtin_set( wchar_t **argv )
return 1; return 1;
} }
/*
Variables can only have one export status
*/
if( export && unexport ) if( export && unexport )
{ {
sb_printf( sb_err, sb_printf( sb_err,
@ -422,13 +440,16 @@ int builtin_set( wchar_t **argv )
if( query ) if( query )
{ {
/* /*
Query mode. Return number of specified variables that do not exist. Query mode. Return the number variables that do not exist
out of the specified variables.
*/ */
int i; int i;
for( i=woptind; i<argc; i++ ) for( i=woptind; i<argc; i++ )
{ {
if( !env_exist( argv[i] ) ) if( !env_exist( argv[i] ) )
{
retcode++; retcode++;
}
} }
return retcode; return retcode;
@ -439,7 +460,6 @@ int builtin_set( wchar_t **argv )
if( woptind < argc ) if( woptind < argc )
{ {
dest = wcsdup(argv[woptind++]); dest = wcsdup(argv[woptind++]);
//fwprintf(stderr, L"Dest: %ls\n", dest);
if( !wcslen( dest ) ) if( !wcslen( dest ) )
{ {
@ -456,7 +476,6 @@ int builtin_set( wchar_t **argv )
while( woptind < argc ) while( woptind < argc )
{ {
al_push(&values, argv[woptind++]); al_push(&values, argv[woptind++]);
// fwprintf(stderr, L"Val: %ls\n", argv[woptind - 1]);
} }
/* Extract variable name and indexes */ /* Extract variable name and indexes */
@ -471,7 +490,6 @@ int builtin_set( wchar_t **argv )
if( !retcode ) if( !retcode )
{ {
name = (wchar_t *) name_sb.buff; name = (wchar_t *) name_sb.buff;
//fwprintf(stderr, L"Name is %ls\n", name);
al_init(&indexes); al_init(&indexes);
retval = parse_fill_indexes(&indexes, dest); retval = parse_fill_indexes(&indexes, dest);
@ -613,7 +631,7 @@ int builtin_set( wchar_t **argv )
} }
/* Common cleanup */ /* Common cleanup */
//fwprintf(stderr, L"Cleanup\n");
free(dest); free(dest);
sb_destroy(&name_sb); sb_destroy(&name_sb);
al_destroy( &values ); al_destroy( &values );

View file

@ -887,6 +887,7 @@ long wcstol(const wchar_t *nptr,
nptr++; nptr++;
} }
} }
#endif #endif
#ifndef HAVE_WCSLCAT #ifndef HAVE_WCSLCAT