Some changes to migrate towards C++ and a multithreaded model

This commit is contained in:
ridiculousfish
2011-12-26 19:18:46 -08:00
parent 3f16ace678
commit 8d2f107d61
90 changed files with 7368 additions and 5981 deletions

View File

@@ -1,6 +1,6 @@
/** \file builtin_set.c Functions defining the set builtin
Functions used for implementing the set builtin.
Functions used for implementing the set builtin.
*/
#include "config.h"
@@ -61,19 +61,19 @@ static int my_env_set( const wchar_t *key, array_list_t *val, int scope )
int i;
int retcode = 0;
wchar_t *val_str=0;
if( is_path_variable( key ) )
{
int error = 0;
for( i=0; i<al_get_count( val ); i++ )
{
int show_perror = 0;
int show_hint = 0;
struct stat buff;
wchar_t *dir = (wchar_t *)al_get( val, i );
if( wstat( dir, &buff ) )
{
error = 1;
@@ -83,55 +83,55 @@ static int my_env_set( const wchar_t *key, array_list_t *val, int scope )
if( !( S_ISDIR(buff.st_mode) ) )
{
error = 1;
}
if( error )
{
wchar_t *colon;
sb_printf( sb_err,
sb_printf( sb_err,
_(BUILTIN_SET_PATH_ERROR),
L"set",
dir,
L"set",
dir,
key );
colon = wcschr( dir, L':' );
if( colon && *(colon+1) )
if( colon && *(colon+1) )
{
show_hint = 1;
}
}
if( show_perror )
{
builtin_wperror( L"set" );
}
if( show_hint )
{
sb_printf( sb_err,
sb_printf( sb_err,
_(BUILTIN_SET_PATH_HINT),
L"set",
key,
key,
wcschr( dir, L':' )+1);
}
if( error )
{
break;
}
}
if( error )
{
return 1;
}
}
sb_init( &sb );
@@ -148,9 +148,9 @@ static int my_env_set( const wchar_t *key, array_list_t *val, int scope )
}
}
val_str = (wchar_t *)sb.buff;
}
switch( env_set( key, val_str, scope | ENV_USER ) )
{
case ENV_PERM:
@@ -159,7 +159,7 @@ static int my_env_set( const wchar_t *key, array_list_t *val, int scope )
retcode=1;
break;
}
case ENV_INVALID:
{
sb_printf( sb_err, _(L"%ls: Unknown error"), L"set" );
@@ -173,13 +173,13 @@ static int my_env_set( const wchar_t *key, array_list_t *val, int scope )
return retcode;
}
/**
/**
Extract indexes from a destination argument of the form name[index1 index2...]
\param indexes the list to insert the new indexes into
\param src the source string to parse
\param name the name of the element. Return null if the name in \c src does not match this name
\param var_count the number of elements in the array to parse.
\param var_count the number of elements in the array to parse.
\return the total number of indexes parsed, or -1 on error
*/
@@ -188,58 +188,58 @@ static int parse_index( array_list_t *indexes,
const wchar_t *name,
int var_count )
{
int len;
size_t len;
int count = 0;
const wchar_t *src_orig = src;
if (src == 0)
{
return 0;
}
while (*src != L'\0' && (iswalnum(*src) || *src == L'_'))
{
src++;
}
if (*src != L'[')
{
sb_printf( sb_err, _(BUILTIN_SET_ARG_COUNT), L"set" );
sb_printf( sb_err, _(BUILTIN_SET_ARG_COUNT), L"set" );
return 0;
}
len = src-src_orig;
if( (wcsncmp( src_orig, name, len )!=0) || (wcslen(name) != (len)) )
{
sb_printf( sb_err,
sb_printf( sb_err,
_(L"%ls: Multiple variable names specified in single call (%ls and %.*ls)\n"),
L"set",
L"set",
name,
len,
src_orig);
return 0;
}
src++;
src++;
while (iswspace(*src))
while (iswspace(*src))
{
src++;
}
while (*src != L']')
while (*src != L']')
{
wchar_t *end;
long l_ind;
errno = 0;
l_ind = wcstol(src, &end, 10);
if( end==src || errno )
if( end==src || errno )
{
sb_printf(sb_err, _(L"%ls: Invalid index starting at '%ls'\n"), L"set", src);
return 0;
@@ -249,7 +249,7 @@ static int parse_index( array_list_t *indexes,
{
l_ind = var_count+l_ind+1;
}
al_push_long(indexes, l_ind);
src = end;
count++;
@@ -268,30 +268,30 @@ static int parse_index( array_list_t *indexes,
\return 0 if the operation was successfull, non-zero otherwise
*/
static int update_values( array_list_t *list,
static int update_values( array_list_t *list,
array_list_t *indexes,
array_list_t *values )
array_list_t *values )
{
int i;
/* Replace values where needed */
for( i = 0; i < al_get_count(indexes); i++ )
for( i = 0; i < al_get_count(indexes); i++ )
{
/*
The '- 1' below is because the indices in fish are
one-based, but the array_list_t uses zero-based indices
*/
long ind = al_get_long(indexes, i) - 1;
void *new = (void *) al_get(values, i);
const wchar_t *newv = (const wchar_t*) al_get(values, i);
if( ind < 0 )
{
return 1;
}
free((void *) al_get(list, ind));
al_set(list, ind, new != 0 ? wcsdup(new) : wcsdup(L""));
al_set(list, ind, newv != 0 ? wcsdup(newv) : wcsdup(L""));
}
return 0;
}
@@ -305,42 +305,42 @@ static int al_contains_long( array_list_t *list,
{
int i;
for (i = 0; i < al_get_count(list); i++)
for (i = 0; i < al_get_count(list); i++)
{
long current = al_get_long(list, i);
if( current != 0 && current == val )
if( current != 0 && current == val )
{
return 1;
}
}
return 0;
}
/**
Erase from a list values at specified indexes
Erase from a list values at specified indexes
*/
static void erase_values(array_list_t *list, array_list_t *indexes)
static void erase_values(array_list_t *list, array_list_t *indexes)
{
long i;
array_list_t result;
al_init(&result);
for (i = 0; i < al_get_count(list); i++)
for (i = 0; i < al_get_count(list); i++)
{
if (!al_contains_long(indexes, i + 1))
if (!al_contains_long(indexes, i + 1))
{
al_push(&result, al_get(list, i));
}
else
else
{
free( (void *)al_get(list, i));
}
}
al_truncate(list,0);
al_truncate(list,0);
al_push_all( list, &result );
al_destroy(&result);
}
@@ -350,31 +350,31 @@ static void erase_values(array_list_t *list, array_list_t *indexes)
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 esc, int scope)
static void print_variables(int include_values, int esc, int scope)
{
array_list_t names;
int i;
al_init( &names );
env_get_names( &names, scope );
sort_list( &names );
for( i = 0; i < al_get_count(&names); i++ )
{
wchar_t *key = (wchar_t *)al_get( &names, i );
wchar_t *e_key = esc ? escape(key, 0) : wcsdup(key);
sb_append(sb_out, e_key);
if( include_values )
if( include_values )
{
wchar_t *value = env_get(key);
wchar_t *e_value;
if( value )
{
int shorten = 0;
if( wcslen( value ) > 64 )
{
shorten = 1;
@@ -384,12 +384,12 @@ static void print_variables(int include_values, int esc, int scope)
DIE_MEM();
}
}
e_value = esc ? expand_escape_variable(value) : wcsdup(value);
sb_append(sb_out, L" ", e_value, (void *)0);
sb_append(sb_out, L" ", e_value, NULL);
free(e_value);
if( shorten )
{
sb_append(sb_out, L"\u2026");
@@ -398,7 +398,7 @@ static void print_variables(int include_values, int esc, int scope)
}
}
sb_append(sb_out, L"\n");
free(e_key);
}
@@ -411,57 +411,57 @@ static void print_variables(int include_values, int esc, int scope)
The set builtin. Creates, updates and erases environment variables
and environemnt variable arrays.
*/
static int builtin_set( wchar_t **argv )
static int builtin_set( wchar_t **argv )
{
/**
Variables used for parsing the argument list
*/
static const struct woption
long_options[] =
long_options[] =
{
{
L"export", no_argument, 0, 'x'
{
L"export", no_argument, 0, 'x'
}
,
{
L"global", no_argument, 0, 'g'
{
L"global", no_argument, 0, 'g'
}
,
{
L"local", no_argument, 0, 'l'
{
L"local", no_argument, 0, 'l'
}
,
{
L"erase", no_argument, 0, 'e'
{
L"erase", no_argument, 0, 'e'
}
,
{
L"names", no_argument, 0, 'n'
}
{
L"names", no_argument, 0, 'n'
}
,
{
L"unexport", no_argument, 0, 'u'
}
{
L"unexport", no_argument, 0, 'u'
}
,
{
L"universal", no_argument, 0, 'U'
}
{
L"universal", no_argument, 0, 'U'
}
,
{
L"query", no_argument, 0, 'q'
}
{
L"query", no_argument, 0, 'q'
}
,
{
L"help", no_argument, 0, 'h'
}
{
L"help", no_argument, 0, 'h'
}
,
{
0, 0, 0, 0
{
0, 0, 0, 0
}
}
;
const wchar_t *short_options = L"+xglenuUqh";
int argc = builtin_count_args(argv);
@@ -469,10 +469,10 @@ static int builtin_set( wchar_t **argv )
/*
Flags to set the work mode
*/
int local = 0, global = 0, export = 0;
int local = 0, global = 0, exportv = 0;
int erase = 0, list = 0, unexport=0;
int universal = 0, query=0;
/*
Variables used for performing the actual work
@@ -482,22 +482,22 @@ static int builtin_set( wchar_t **argv )
int scope;
int slice=0;
int i;
wchar_t *bad_char;
/* Parse options to obtain the requested operation and the modifiers */
woptind = 0;
while (1)
while (1)
{
int c = wgetopt_long(argc, argv, short_options, long_options, 0);
if (c == -1)
if (c == -1)
{
break;
}
switch(c)
switch(c)
{
case 0:
break;
@@ -511,7 +511,7 @@ static int builtin_set( wchar_t **argv )
break;
case 'x':
export = 1;
exportv = 1;
break;
case 'l':
@@ -556,23 +556,23 @@ static int builtin_set( wchar_t **argv )
also specify scope
*/
if( query && (erase || list || global || local || universal || export || unexport ) )
if( query && (erase || list || global || local || universal || exportv || unexport ) )
{
sb_printf(sb_err,
BUILTIN_ERR_COMBO,
argv[0] );
builtin_print_help( argv[0], sb_err );
return 1;
}
/* We can't both list and erase varaibles */
if( erase && list )
if( erase && list )
{
sb_printf(sb_err,
BUILTIN_ERR_COMBO,
argv[0] );
argv[0] );
builtin_print_help( argv[0], sb_err );
return 1;
@@ -581,7 +581,7 @@ static int builtin_set( wchar_t **argv )
/*
Variables can only have one scope
*/
if( local + global + universal > 1 )
if( local + global + universal > 1 )
{
sb_printf( sb_err,
BUILTIN_ERR_GLOCAL,
@@ -593,7 +593,7 @@ static int builtin_set( wchar_t **argv )
/*
Variables can only have one export status
*/
if( export && unexport )
if( exportv && unexport )
{
sb_printf( sb_err,
BUILTIN_ERR_EXPUNEXP,
@@ -605,7 +605,7 @@ static int builtin_set( wchar_t **argv )
/*
Calculate the scope value for variable assignement
*/
scope = (local ? ENV_LOCAL : 0) | (global ? ENV_GLOBAL : 0) | (export ? ENV_EXPORT : 0) | (unexport ? ENV_UNEXPORT : 0) | (universal ? ENV_UNIVERSAL:0) | ENV_USER;
scope = (local ? ENV_LOCAL : 0) | (global ? ENV_GLOBAL : 0) | (exportv ? ENV_EXPORT : 0) | (unexport ? ENV_UNEXPORT : 0) | (universal ? ENV_UNIVERSAL:0) | ENV_USER;
if( query )
{
@@ -621,7 +621,7 @@ static int builtin_set( wchar_t **argv )
if( !(dest = wcsdup(arg)))
{
DIE_MEM();
DIE_MEM();
}
if( wcschr( dest, L'[' ) )
@@ -629,18 +629,18 @@ static int builtin_set( wchar_t **argv )
slice = 1;
*wcschr( dest, L'[' )=0;
}
if( slice )
{
array_list_t indexes;
array_list_t result;
int j;
al_init( &result );
al_init( &indexes );
tokenize_variable_array( env_get( dest ), &result );
if( !parse_index( &indexes, arg, dest, al_get_count( &result ) ) )
{
builtin_print_help( argv[0], sb_err );
@@ -663,32 +663,32 @@ static int builtin_set( wchar_t **argv )
retcode++;
}
}
free( dest );
}
return retcode;
}
if( list )
if( list )
{
/* Maybe we should issue an error if there are any other arguments? */
print_variables(0, 0, scope);
return 0;
}
}
if( woptind == argc )
{
/*
Print values of variables
*/
if( erase )
if( erase )
{
sb_printf( sb_err,
_(L"%ls: Erase needs a variable name\n%ls\n"),
_(L"%ls: Erase needs a variable name\n%ls\n"),
argv[0] );
builtin_print_help( argv[0], sb_err );
retcode = 1;
}
@@ -696,13 +696,13 @@ static int builtin_set( wchar_t **argv )
{
print_variables( 1, 1, scope );
}
return retcode;
}
if( !(dest = wcsdup(argv[woptind])))
{
DIE_MEM();
DIE_MEM();
}
if( wcschr( dest, L'[' ) )
@@ -710,7 +710,7 @@ static int builtin_set( wchar_t **argv )
slice = 1;
*wcschr( dest, L'[' )=0;
}
if( !wcslen( dest ) )
{
free( dest );
@@ -718,7 +718,7 @@ static int builtin_set( wchar_t **argv )
builtin_print_help( argv[0], sb_err );
return 1;
}
if( (bad_char = wcsvarname( dest ) ) )
{
sb_printf( sb_err, BUILTIN_ERR_VARCHAR, argv[0], *bad_char );
@@ -726,7 +726,7 @@ static int builtin_set( wchar_t **argv )
free( dest );
return 1;
}
if( slice && erase && (scope != ENV_USER) )
{
free( dest );
@@ -734,12 +734,12 @@ static int builtin_set( wchar_t **argv )
builtin_print_help( argv[0], sb_err );
return 1;
}
/*
set assignment can work in two modes, either using slices or
using the whole array. We detect which mode is used here.
*/
if( slice )
{
@@ -750,22 +750,22 @@ static int builtin_set( wchar_t **argv )
array_list_t values;
array_list_t indexes;
array_list_t result;
al_init(&values);
al_init(&indexes);
al_init(&result);
tokenize_variable_array( env_get(dest), &result );
for( ; woptind<argc; woptind++ )
{
{
if( !parse_index( &indexes, argv[woptind], dest, al_get_count( &result ) ) )
{
builtin_print_help( argv[0], sb_err );
retcode = 1;
break;
}
val_count = argc-woptind-1;
idx_count = al_get_count( &indexes );
@@ -784,7 +784,7 @@ static int builtin_set( wchar_t **argv )
break;
}
}
}
}
if( !retcode )
{
@@ -802,12 +802,12 @@ static int builtin_set( wchar_t **argv )
array_list_t value;
al_init(&value);
while( woptind < argc )
while( woptind < argc )
{
al_push(&value, argv[woptind++]);
}
if( update_values( &result,
if( update_values( &result,
&indexes,
&value ) )
{
@@ -815,14 +815,14 @@ static int builtin_set( wchar_t **argv )
sb_printf( sb_err, ARRAY_BOUNDS_ERR );
sb_append( sb_err, L"\n" );
}
my_env_set(dest,
&result,
scope);
al_destroy( &value );
}
}
}
al_foreach( &result, &free );
@@ -830,12 +830,12 @@ static int builtin_set( wchar_t **argv )
al_destroy(&indexes);
al_destroy(&values);
}
else
{
woptind++;
/*
No slicing
*/
@@ -843,7 +843,7 @@ static int builtin_set( wchar_t **argv )
{
if( woptind != argc )
{
sb_printf( sb_err,
sb_printf( sb_err,
_(L"%ls: Values cannot be specfied with erase\n"),
argv[0] );
builtin_print_help( argv[0], sb_err );
@@ -858,21 +858,21 @@ static int builtin_set( wchar_t **argv )
{
array_list_t val;
al_init( &val );
for( i=woptind; i<argc; i++ )
{
al_push( &val, argv[i] );
}
retcode = my_env_set( dest, &val, scope );
al_destroy( &val );
}
al_destroy( &val );
}
}
free( dest );
return retcode;
}