From 4dc4edd0cc91b44101fa3adbbeb4db168d6cf15c Mon Sep 17 00:00:00 2001 From: Rene Rivera Date: Sun, 6 Nov 2005 22:33:39 +0000 Subject: [PATCH] Implement STDOUT/STDERR @() file functionality. [SVN r31582] --- historic/jam/src/filesys.c | 23 ++++++++++++++++++++++ historic/jam/src/filesys.h | 4 ++++ historic/jam/src/variable.c | 38 ++++++++++++++++++++++++++++--------- jam_src/filesys.c | 23 ++++++++++++++++++++++ jam_src/filesys.h | 4 ++++ jam_src/variable.c | 38 ++++++++++++++++++++++++++++--------- 6 files changed, 112 insertions(+), 18 deletions(-) diff --git a/historic/jam/src/filesys.c b/historic/jam/src/filesys.c index bec1d8fc3..604a80c19 100644 --- a/historic/jam/src/filesys.c +++ b/historic/jam/src/filesys.c @@ -3,6 +3,7 @@ # include "strings.h" # include "newstr.h" # include "filesys.h" +# include "lists.h" void file_build1( @@ -62,3 +63,25 @@ void file_done() { hashdone( filecache_hash ); } + +static LIST * files_to_remove = L0; + +static void remove_files_atexit(void) +{ + /* we do pop front in case this exit function is called + more than once */ + while ( files_to_remove ) + { + remove( files_to_remove->string ); + files_to_remove = list_pop_front( files_to_remove ); + } +} + +void file_remove_atexit( const char * path ) +{ + if ( L0 == files_to_remove ) + { + atexit(&remove_files_atexit); + } + files_to_remove = list_new( files_to_remove, newstr((char*)path) ); +} diff --git a/historic/jam/src/filesys.h b/historic/jam/src/filesys.h index c48b28bf4..a442d8268 100644 --- a/historic/jam/src/filesys.h +++ b/historic/jam/src/filesys.h @@ -47,4 +47,8 @@ file_info_t * file_query(char * filename); void file_done(); +/** Marks a path/file to be removed when jam exits. +*/ +void file_remove_atexit( const char * path ); + #endif diff --git a/historic/jam/src/variable.c b/historic/jam/src/variable.c index 70cabffc4..4938fe989 100644 --- a/historic/jam/src/variable.c +++ b/historic/jam/src/variable.c @@ -250,6 +250,7 @@ var_string( { string file_name_v; int file_name_l = 0; + const char * file_name_s = 0; /* expand the temporary file name var inline */ #if 0 @@ -262,17 +263,39 @@ var_string( #endif file_name_l = var_string(file_name_v.value,out,oute-out+1,lol); string_free(&file_name_v); - if ( file_name_l < 0 ) return file_name_l; + if ( file_name_l < 0 ) return -1; + file_name_s = out; + + /* for stdout/stderr we will create a temp file and generate + a command that outputs the content as needed. */ + if ( strcmp( "STDOUT", out ) == 0 || strcmp( "STDERR", out ) == 0 ) + { + int err_redir = strcmp( "STDERR", out ) == 0; + out[0] = '\0'; + file_name_s = path_tmpfile(); + file_name_l = strlen(file_name_s); + #ifdef OS_NT + if ( (out+7+file_name_l+(err_redir?5:0)) >= oute ) return -1; + sprintf( out,"type \"%s\"%s", + file_name_s, + err_redir ? " 1>&2" : "" ); + #else + if ( (out+6+file_name_l+(err_redir?5:0)) >= oute ) return -1; + sprintf( out,"cat \"%s\"%s", + file_name_s, + err_redir ? " 1>&2" : "" ); + #endif + /* we also make sure that the temp files created by this + get nuked eventually. */ + file_remove_atexit( file_name_s ); + } /* expand the file value into the file reference */ if ( !globs.noexec ) - var_string_to_file( split+3, ine-split-4, out, lol ); + var_string_to_file( split+3, ine-split-4, file_name_s, lol ); /* continue on with the expansion */ - if ( strcmp( "STDOUT", out ) == 0 || strcmp( "STDERR", out ) == 0 ) - out[0] = '\0'; - else - out += strlen(out); + out += strlen(out); } /* and continue with the parsing just past the @() reference */ @@ -445,8 +468,6 @@ var_get( char *symbol ) { result = list_new( L0, newstr( (char*)path_tmpfile() ) ); } - #if 0 - /* Not really usefull at the moment. */ else if ( strcmp( "STDOUT", symbol ) == 0 ) { result = list_new( L0, newstr( "STDOUT" ) ); @@ -455,7 +476,6 @@ var_get( char *symbol ) { result = list_new( L0, newstr( "STDERR" ) ); } - #endif else #endif { diff --git a/jam_src/filesys.c b/jam_src/filesys.c index bec1d8fc3..604a80c19 100644 --- a/jam_src/filesys.c +++ b/jam_src/filesys.c @@ -3,6 +3,7 @@ # include "strings.h" # include "newstr.h" # include "filesys.h" +# include "lists.h" void file_build1( @@ -62,3 +63,25 @@ void file_done() { hashdone( filecache_hash ); } + +static LIST * files_to_remove = L0; + +static void remove_files_atexit(void) +{ + /* we do pop front in case this exit function is called + more than once */ + while ( files_to_remove ) + { + remove( files_to_remove->string ); + files_to_remove = list_pop_front( files_to_remove ); + } +} + +void file_remove_atexit( const char * path ) +{ + if ( L0 == files_to_remove ) + { + atexit(&remove_files_atexit); + } + files_to_remove = list_new( files_to_remove, newstr((char*)path) ); +} diff --git a/jam_src/filesys.h b/jam_src/filesys.h index c48b28bf4..a442d8268 100644 --- a/jam_src/filesys.h +++ b/jam_src/filesys.h @@ -47,4 +47,8 @@ file_info_t * file_query(char * filename); void file_done(); +/** Marks a path/file to be removed when jam exits. +*/ +void file_remove_atexit( const char * path ); + #endif diff --git a/jam_src/variable.c b/jam_src/variable.c index 70cabffc4..4938fe989 100644 --- a/jam_src/variable.c +++ b/jam_src/variable.c @@ -250,6 +250,7 @@ var_string( { string file_name_v; int file_name_l = 0; + const char * file_name_s = 0; /* expand the temporary file name var inline */ #if 0 @@ -262,17 +263,39 @@ var_string( #endif file_name_l = var_string(file_name_v.value,out,oute-out+1,lol); string_free(&file_name_v); - if ( file_name_l < 0 ) return file_name_l; + if ( file_name_l < 0 ) return -1; + file_name_s = out; + + /* for stdout/stderr we will create a temp file and generate + a command that outputs the content as needed. */ + if ( strcmp( "STDOUT", out ) == 0 || strcmp( "STDERR", out ) == 0 ) + { + int err_redir = strcmp( "STDERR", out ) == 0; + out[0] = '\0'; + file_name_s = path_tmpfile(); + file_name_l = strlen(file_name_s); + #ifdef OS_NT + if ( (out+7+file_name_l+(err_redir?5:0)) >= oute ) return -1; + sprintf( out,"type \"%s\"%s", + file_name_s, + err_redir ? " 1>&2" : "" ); + #else + if ( (out+6+file_name_l+(err_redir?5:0)) >= oute ) return -1; + sprintf( out,"cat \"%s\"%s", + file_name_s, + err_redir ? " 1>&2" : "" ); + #endif + /* we also make sure that the temp files created by this + get nuked eventually. */ + file_remove_atexit( file_name_s ); + } /* expand the file value into the file reference */ if ( !globs.noexec ) - var_string_to_file( split+3, ine-split-4, out, lol ); + var_string_to_file( split+3, ine-split-4, file_name_s, lol ); /* continue on with the expansion */ - if ( strcmp( "STDOUT", out ) == 0 || strcmp( "STDERR", out ) == 0 ) - out[0] = '\0'; - else - out += strlen(out); + out += strlen(out); } /* and continue with the parsing just past the @() reference */ @@ -445,8 +468,6 @@ var_get( char *symbol ) { result = list_new( L0, newstr( (char*)path_tmpfile() ) ); } - #if 0 - /* Not really usefull at the moment. */ else if ( strcmp( "STDOUT", symbol ) == 0 ) { result = list_new( L0, newstr( "STDOUT" ) ); @@ -455,7 +476,6 @@ var_get( char *symbol ) { result = list_new( L0, newstr( "STDERR" ) ); } - #endif else #endif {