From 93dc7d4cc10776278398f497127df1f49ab0483c Mon Sep 17 00:00:00 2001 From: Siteshwar Vashisht Date: Mon, 18 Jun 2012 22:50:40 +0530 Subject: [PATCH] Add support for querying variables with scope options as requested in issue #132 --- builtin_set.cpp | 2 +- env.cpp | 63 ++++++++++++++++-------- tests/test4.in | 127 ++++++++++++++++++++++++++++++++++++++++++++++++ tests/test4.out | 18 +++++++ 4 files changed, 189 insertions(+), 21 deletions(-) diff --git a/builtin_set.cpp b/builtin_set.cpp index fae6e5014..0cd8044a3 100644 --- a/builtin_set.cpp +++ b/builtin_set.cpp @@ -498,7 +498,7 @@ static int builtin_set( parser_t &parser, wchar_t **argv ) also specify scope */ - if( query && (erase || list || global || local || universal || exportv || unexport ) ) + if( query && (erase || list) ) { append_format(stderr_buffer, BUILTIN_ERR_COMBO, diff --git a/env.cpp b/env.cpp index 6e6a8d741..ef53906ab 100644 --- a/env.cpp +++ b/env.cpp @@ -1147,36 +1147,46 @@ int env_exist( const wchar_t *key, int mode ) { 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; } } - if( ! (mode & ENV_UNIVERSAL) ) + if( !(mode & ENV_UNIVERSAL) ) { env = (mode & ENV_GLOBAL)?global_env:top; while( env != 0 ) { var_table_t::iterator result = env->env.find( key ); + if ( result != env->env.end() ) { res = result->second; - } - else - { - res = 0; - } - if( res != 0 ) - { - return 1; + if (mode & ENV_EXPORT) + { + return res->exportv == 1; + } + else if (mode & ENV_UNEXPORT) + { + return res->exportv == 0; + } + + return 1; } - - if( mode & ENV_LOCAL ) - { - break; - } - + + if ( mode & ENV_LOCAL ) + break; + if( env->new_scope ) { env = global_env; @@ -1187,8 +1197,8 @@ int env_exist( const wchar_t *key, int mode ) } } } - - if( ! (mode & ENV_LOCAL) && ! (mode & ENV_GLOBAL) ) + + if( !(mode & ENV_LOCAL) && !(mode & ENV_GLOBAL) ) { if( ! get_proc_had_barrier()) { @@ -1197,10 +1207,23 @@ int env_exist( const wchar_t *key, int mode ) } item = env_universal_get( key ); - - } - return item != 0; + 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 1; + } + } + + return 0; } /** diff --git a/tests/test4.in b/tests/test4.in index 4155c0e86..5d94ba4a8 100644 --- a/tests/test4.in +++ b/tests/test4.in @@ -33,3 +33,130 @@ set -g smurf yellow call3 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 diff --git a/tests/test4.out b/tests/test4.out index 54f64544b..5a14f80a2 100644 --- a/tests/test4.out +++ b/tests/test4.out @@ -2,3 +2,21 @@ Test 1 pass Test 2 pass Test 3 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