Add support for querying variables with scope options as requested in issue #132

This commit is contained in:
Siteshwar Vashisht 2012-06-18 22:50:40 +05:30
parent 1bead8adf7
commit 93dc7d4cc1
4 changed files with 189 additions and 21 deletions

View file

@ -498,7 +498,7 @@ static int builtin_set( parser_t &parser, wchar_t **argv )
also specify scope also specify scope
*/ */
if( query && (erase || list || global || local || universal || exportv || unexport ) ) if( query && (erase || list) )
{ {
append_format(stderr_buffer, append_format(stderr_buffer,
BUILTIN_ERR_COMBO, BUILTIN_ERR_COMBO,

39
env.cpp
View file

@ -1147,6 +1147,15 @@ int env_exist( const wchar_t *key, int mode )
{ {
if( is_read_only(key) || is_electric(key) ) if( is_read_only(key) || is_electric(key) )
{ {
//Such variables are never exported
if (mode & ENV_EXPORT)
{
return 0;
}
else if (mode & ENV_UNEXPORT)
{
return 1;
}
return 1; return 1;
} }
} }
@ -1158,24 +1167,25 @@ int env_exist( const wchar_t *key, int mode )
while( env != 0 ) while( env != 0 )
{ {
var_table_t::iterator result = env->env.find( key ); var_table_t::iterator result = env->env.find( key );
if ( result != env->env.end() ) if ( result != env->env.end() )
{ {
res = result->second; res = result->second;
}
else if (mode & ENV_EXPORT)
{ {
res = 0; return res->exportv == 1;
}
else if (mode & ENV_UNEXPORT)
{
return res->exportv == 0;
} }
if( res != 0 )
{
return 1; return 1;
} }
if ( mode & ENV_LOCAL ) if ( mode & ENV_LOCAL )
{
break; break;
}
if( env->new_scope ) if( env->new_scope )
{ {
@ -1198,9 +1208,22 @@ int env_exist( const wchar_t *key, int mode )
item = env_universal_get( key ); item = env_universal_get( key );
if (item != NULL)
{
if (mode & ENV_EXPORT)
{
return env_universal_get_export(key) == 1;
}
else if (mode & ENV_UNEXPORT)
{
return env_universal_get_export(key) == 0;
} }
return item != 0;
return 1;
}
}
return 0;
} }
/** /**

View file

@ -33,3 +33,130 @@ set -g smurf yellow
call3 call3
call4 call4
set -l foo 1
set -g bar 2
set -U baz 3
set -l -q foo
if test $status -ne 0
echo Test 5 fail
else
echo Test 5 pass
end;
if not set -g -q bar
echo Test 6 fail
else
echo Test 6 pass
end;
if not set -U -q baz
echo Test 7 fail
else
echo Test 7 pass
end;
set -u -l -q foo
if test $status -ne 0
echo Test 8 fail
else
echo Test 8 pass
end;
if not set -u -g -q bar
echo Test 9 fail
else
echo Test 9 pass
end;
if not set -u -U -q baz
echo Test 10 fail
else
echo Test 10 pass
end;
set -x -l -q foo
if test $status -eq 0
echo Test 11 fail
else
echo Test 11 pass
end;
if set -x -g -q bar
echo Test 12 fail
else
echo Test 12 pass
end;
if set -x -U -q baz
echo Test 13 fail
else
echo Test 13 pass
end;
set -x -l foo 1
set -x -g bar 2
set -x -U baz 3
set -l -q foo
if test $status -ne 0
echo Test 14 fail
else
echo Test 14 pass
end;
if not set -g -q bar
echo Test 15 fail
else
echo Test 15 pass
end;
if not set -U -q baz
echo Test 16 fail
else
echo Test 16 pass
end;
set -u -l -q foo
if test $status -ne 1
echo Test 17 fail
else
echo Test 17 pass
end;
if set -u -g -q bar
echo Test 18 fail
else
echo Test 18 pass
end;
if set -u -U -q baz
echo Test 19 fail
else
echo Test 19 pass
end;
set -x -l -q foo
if test $status -ne 0
echo Test 20 fail
else
echo Test 20 pass
end;
if not set -x -g -q bar
echo Test 21 fail
else
echo Test 21 pass
end;
if not set -x -U -q baz
echo Test 22 fail
else
echo Test 22 pass
end;
set -U -e baz

View file

@ -2,3 +2,21 @@ Test 1 pass
Test 2 pass Test 2 pass
Test 3 pass Test 3 pass
Test 4 pass Test 4 pass
Test 5 pass
Test 6 pass
Test 7 pass
Test 8 pass
Test 9 pass
Test 10 pass
Test 11 pass
Test 12 pass
Test 13 pass
Test 14 pass
Test 15 pass
Test 16 pass
Test 17 pass
Test 18 pass
Test 19 pass
Test 20 pass
Test 21 pass
Test 22 pass