aboutsummaryrefslogtreecommitdiffhomepage
path: root/builtin.c
diff options
context:
space:
mode:
authorGravatar axel <axel@liljencrantz.se>2005-10-19 22:07:44 +1000
committerGravatar axel <axel@liljencrantz.se>2005-10-19 22:07:44 +1000
commit83fcc293055a8eb9b94171cc0c665a9691d08f01 (patch)
treee007a2840d292fffe42b1e2ada14ecf9196b402b /builtin.c
parent4fb2dc3f55f1b55ba9893a7f4a321bb641451539 (diff)
Do not break stdin when reading a script
darcs-hash:20051019120744-ac50b-67cc9a5c4f6509eb8a012aff32fcd9606933d0ab.gz
Diffstat (limited to 'builtin.c')
-rw-r--r--builtin.c51
1 files changed, 14 insertions, 37 deletions
diff --git a/builtin.c b/builtin.c
index 8197b600..b0a05e28 100644
--- a/builtin.c
+++ b/builtin.c
@@ -1956,7 +1956,7 @@ static int builtin_complete( wchar_t **argv )
*/
static int builtin_source( wchar_t ** argv )
{
- int stdin_org;
+ int fd;
int res;
/*
@@ -1976,19 +1976,7 @@ static int builtin_source( wchar_t ** argv )
return 1;
}
- if( (stdin_org=dup( 0 )) == -1)
- {
- builtin_wperror(L"dup");
- return 1;
- }
-
- if( close( 0 ) )
- {
- builtin_wperror(L"close");
- return 1;
- }
-
- if( wopen( argv[1], O_RDONLY ) == -1 )
+ if( ( fd = wopen( argv[1], O_RDONLY ) ) == -1 )
{
builtin_wperror( L"open" );
res = 1;
@@ -1996,8 +1984,14 @@ static int builtin_source( wchar_t ** argv )
else
{
reader_push_current_filename( argv[1] );
+
+ /*
+ Push a new non-shadowwing variable scope to the stack. That
+ way one can use explicitly local variables in sourced files
+ that will die on return to the calling file.
+ */
env_push(0);
- res = reader_read();
+ res = reader_read( fd );
env_pop();
if( res )
{
@@ -2006,31 +2000,14 @@ static int builtin_source( wchar_t ** argv )
argv[0],
argv[1]
);
-
}
- if( close( 0 ) )
- {
- builtin_wperror(L"close");
- res = errno;
- }
- reader_pop_current_filename();
- }
+ /*
+ Do not close fd after calling reader_read. reader_read
+ automatically closes it before calling eval.
+ */
- if( dup( stdin_org ) == -1)
- {
- builtin_wperror(L"dup");
- res = errno;
- fwprintf( stderr, L"Could not restore stdout\n" );
- sanity_lose();
- }
-
- if( close( stdin_org ) )
- {
- builtin_wperror(L"close");
- res = errno;
- fwprintf( stderr, L"Could not restore stdout\n" );
- sanity_lose();
+ reader_pop_current_filename();
}
return res;