From 42ebfffce836cdb9e3a65699080823ce79df3175 Mon Sep 17 00:00:00 2001 From: Rene Rivera Date: Wed, 13 Jul 2005 04:26:01 +0000 Subject: [PATCH] Add SHELL, execute command an capture output, builtin. Original work from Craig Rodrigues, implemented slightly modified. [SVN r30027] --- historic/jam/src/builtins.c | 58 +++++++++++++++++++++++++++++++++++-- historic/jam/src/builtins.h | 1 + historic/jam/src/jam.h | 9 ++++++ jam_src/builtins.c | 58 +++++++++++++++++++++++++++++++++++-- jam_src/builtins.h | 1 + jam_src/jam.h | 9 ++++++ 6 files changed, 130 insertions(+), 6 deletions(-) diff --git a/historic/jam/src/builtins.c b/historic/jam/src/builtins.c index 5ef7e557f..3cac3c6cc 100644 --- a/historic/jam/src/builtins.c +++ b/historic/jam/src/builtins.c @@ -151,6 +151,13 @@ load_builtins() bind_builtin( "TEMPORARY" , builtin_flags, T_FLAG_TEMP, 0 ) ); + { + char * args[] = { "targets", "*", 0 }; + bind_builtin( + "ISFILE", + builtin_flags, T_FLAG_ISFILE, 0 ); + } + duplicate_rule( "HdrMacro" , bind_builtin( "HDRMACRO" , builtin_hdrmacro, 0, 0 ) ); @@ -312,9 +319,9 @@ load_builtins() # endif { - char * args[] = { "targets", "*", 0 }; - bind_builtin( "ISFILE", - builtin_flags, T_FLAG_NOUPDATE, 0 ); + char * args[] = { "command", 0 }; + bind_builtin( "SHELL", + builtin_shell, 0, args ); } /* Initialize builtin modules */ @@ -1604,3 +1611,48 @@ bjam_import_rule(PyObject* self, PyObject* args) } #endif + +#ifdef HAVE_POPEN +#if defined(_MSC_VER) || defined(__BORLANDC__) + #define popen _popen + #define pclose _pclose +#endif + +LIST *builtin_shell( PARSE *parse, FRAME *frame ) +{ + LIST* arg = lol_get( frame->args, 0 ); + LIST* result = 0; + string s; + int ret; + char buffer[1024]; + FILE *p = NULL; + + string_new( &s ); + + fflush(NULL); + + p = popen(arg->string, "r"); + if ( p == NULL ) + return L0; + + while ( (ret = fread(buffer, sizeof(char), sizeof(buffer)-1, p)) > 0 ) + { + buffer[ret+1] = 0; + string_append( &s, buffer ); + } + + pclose(p); + + result = list_new( L0, newstr(s.value) ); + string_free(&s); + return result; +} + +#else + +LIST *builtin_shell( PARSE *parse, FRAME *frame ) +{ + return L0; +} + +#endif diff --git a/historic/jam/src/builtins.h b/historic/jam/src/builtins.h index 72404406f..3a0b50945 100644 --- a/historic/jam/src/builtins.h +++ b/historic/jam/src/builtins.h @@ -46,6 +46,7 @@ LIST *builtin_user_module( PARSE *parse, FRAME *frame ); LIST *builtin_nearest_user_location( PARSE *parse, FRAME *frame ); LIST *builtin_check_if_file( PARSE *parse, FRAME *frame ); LIST *builtin_python_import_rule( PARSE *parse, FRAME *frame ); +LIST *builtin_shell( PARSE *parse, FRAME *frame ); void backtrace( FRAME *frame ); diff --git a/historic/jam/src/jam.h b/historic/jam/src/jam.h index 107d186ab..ec9cd5f99 100644 --- a/historic/jam/src/jam.h +++ b/historic/jam/src/jam.h @@ -42,6 +42,9 @@ #include #endif +/* Assume popen support is available unless known otherwise. */ +#define HAVE_POPEN 1 + /* * VMS, OPENVMS */ @@ -116,6 +119,12 @@ # define OS_AS400 # endif +/* Metrowerks Standard Library on Windows. */ + +# ifdef __MSL__ +#undef HAVE_POPEN +#endif + # endif /* diff --git a/jam_src/builtins.c b/jam_src/builtins.c index 5ef7e557f..3cac3c6cc 100644 --- a/jam_src/builtins.c +++ b/jam_src/builtins.c @@ -151,6 +151,13 @@ load_builtins() bind_builtin( "TEMPORARY" , builtin_flags, T_FLAG_TEMP, 0 ) ); + { + char * args[] = { "targets", "*", 0 }; + bind_builtin( + "ISFILE", + builtin_flags, T_FLAG_ISFILE, 0 ); + } + duplicate_rule( "HdrMacro" , bind_builtin( "HDRMACRO" , builtin_hdrmacro, 0, 0 ) ); @@ -312,9 +319,9 @@ load_builtins() # endif { - char * args[] = { "targets", "*", 0 }; - bind_builtin( "ISFILE", - builtin_flags, T_FLAG_NOUPDATE, 0 ); + char * args[] = { "command", 0 }; + bind_builtin( "SHELL", + builtin_shell, 0, args ); } /* Initialize builtin modules */ @@ -1604,3 +1611,48 @@ bjam_import_rule(PyObject* self, PyObject* args) } #endif + +#ifdef HAVE_POPEN +#if defined(_MSC_VER) || defined(__BORLANDC__) + #define popen _popen + #define pclose _pclose +#endif + +LIST *builtin_shell( PARSE *parse, FRAME *frame ) +{ + LIST* arg = lol_get( frame->args, 0 ); + LIST* result = 0; + string s; + int ret; + char buffer[1024]; + FILE *p = NULL; + + string_new( &s ); + + fflush(NULL); + + p = popen(arg->string, "r"); + if ( p == NULL ) + return L0; + + while ( (ret = fread(buffer, sizeof(char), sizeof(buffer)-1, p)) > 0 ) + { + buffer[ret+1] = 0; + string_append( &s, buffer ); + } + + pclose(p); + + result = list_new( L0, newstr(s.value) ); + string_free(&s); + return result; +} + +#else + +LIST *builtin_shell( PARSE *parse, FRAME *frame ) +{ + return L0; +} + +#endif diff --git a/jam_src/builtins.h b/jam_src/builtins.h index 72404406f..3a0b50945 100644 --- a/jam_src/builtins.h +++ b/jam_src/builtins.h @@ -46,6 +46,7 @@ LIST *builtin_user_module( PARSE *parse, FRAME *frame ); LIST *builtin_nearest_user_location( PARSE *parse, FRAME *frame ); LIST *builtin_check_if_file( PARSE *parse, FRAME *frame ); LIST *builtin_python_import_rule( PARSE *parse, FRAME *frame ); +LIST *builtin_shell( PARSE *parse, FRAME *frame ); void backtrace( FRAME *frame ); diff --git a/jam_src/jam.h b/jam_src/jam.h index 107d186ab..ec9cd5f99 100644 --- a/jam_src/jam.h +++ b/jam_src/jam.h @@ -42,6 +42,9 @@ #include #endif +/* Assume popen support is available unless known otherwise. */ +#define HAVE_POPEN 1 + /* * VMS, OPENVMS */ @@ -116,6 +119,12 @@ # define OS_AS400 # endif +/* Metrowerks Standard Library on Windows. */ + +# ifdef __MSL__ +#undef HAVE_POPEN +#endif + # endif /*