From dc4f80f3acfdc6a98a543b535a9cb31823ed2835 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 7 Aug 2006 16:09:15 +0000 Subject: Added support for setting and adding the output of commands to variables. --- src/build.y | 8 ++++++++ src/builder.cpp | 24 ++++++++++++++++++++++++ src/builder.h | 2 ++ 3 files changed, 34 insertions(+) (limited to 'src') diff --git a/src/build.y b/src/build.y index d8cba53..d1f3945 100644 --- a/src/build.y +++ b/src/build.y @@ -212,6 +212,14 @@ setexpr: STRING '=' STRING { bld.varAddSet( $1, $3 ); } + | STRING '=' TOK_FROM TOK_COMMAND STRING + { + bld.varSet( $1, bld.cmdToString( $5 ).c_str() ); + } + | STRING TOK_ADDSET TOK_FROM TOK_COMMAND STRING + { + bld.varSet( $1, bld.cmdToString( $5 ).c_str() ); + } ; rule: TOK_RULE STRING diff --git a/src/builder.cpp b/src/builder.cpp index 8fd72bb..8c3bbbb 100644 --- a/src/builder.cpp +++ b/src/builder.cpp @@ -799,3 +799,27 @@ std::list Builder::findTargets( const char *sRegex ) return lTmp; } +std::string Builder::cmdToString( const char *sCmd ) +{ + std::string buf; + FILE *pr = popen( sCmd, "r" ); + if( pr == NULL ) + throw BuildException("Couldn't execute program \"%s\"", sCmd ); + + char cbuf[2048]; + + for(;;) + { + int nRead = fread( cbuf, 1, 2048, pr ); + for( int j = 0; j < nRead; j++ ) + if( cbuf[j] != '\n' && cbuf[j] != '\r' ) + buf.append( cbuf+j, 1 ); + if( feof( pr ) ) + break; + } + + pclose( pr ); + + return buf; +} + diff --git a/src/builder.h b/src/builder.h index 195296a..c570dbb 100644 --- a/src/builder.h +++ b/src/builder.h @@ -137,6 +137,8 @@ public: void regexVars( RegExp *re, varmap &map ); std::string varRepl( const char *sSrc, const char *cont, varmap *mExtra ); + std::string cmdToString( const char *sCmd ); + private: void requiresNormal( const char *sBase, const char *sReq ); void requiresRegexp( const char *sBase, const char *sReq ); -- cgit v1.2.3