ENH: Added OUTPUT_STRIP_TRAILING_WHITESPACE and ERROR_STRIP_TRAILING_WHITESPACE options to EXECUTE_PROCESS command. These allow it to behave more like the old EXEC_PROGRAM command that it is supposed to replace.

This commit is contained in:
Brad King 2006-10-16 11:32:28 -04:00
parent 641a0ad878
commit e593fbf6ee
2 changed files with 49 additions and 5 deletions

View File

@ -19,7 +19,15 @@
#include <cmsys/Process.h> #include <cmsys/Process.h>
void cmExecuteProcessCommandFixText(std::vector<char>& output); #include <ctype.h> /* isspace */
static bool cmExecuteProcessCommandIsWhitespace(char c)
{
return (isspace((int)c) || c == '\n' || c == '\r');
}
void cmExecuteProcessCommandFixText(std::vector<char>& output,
bool strip_trailing_whitespace);
// cmExecuteProcessCommand // cmExecuteProcessCommand
bool cmExecuteProcessCommand bool cmExecuteProcessCommand
@ -36,6 +44,8 @@ bool cmExecuteProcessCommand
size_t command_index = 0; size_t command_index = 0;
bool output_quiet = false; bool output_quiet = false;
bool error_quiet = false; bool error_quiet = false;
bool output_strip_trailing_whitespace = false;
bool error_strip_trailing_whitespace = false;
std::string timeout_string; std::string timeout_string;
std::string input_file; std::string input_file;
std::string output_file; std::string output_file;
@ -166,10 +176,27 @@ bool cmExecuteProcessCommand
doing_command = false; doing_command = false;
error_quiet = true; error_quiet = true;
} }
else if(args[i] == "OUTPUT_STRIP_TRAILING_WHITESPACE")
{
doing_command = false;
output_strip_trailing_whitespace = true;
}
else if(args[i] == "ERROR_STRIP_TRAILING_WHITESPACE")
{
doing_command = false;
error_strip_trailing_whitespace = true;
}
else if(doing_command) else if(doing_command)
{ {
cmds[command_index].push_back(args[i].c_str()); cmds[command_index].push_back(args[i].c_str());
} }
else
{
cmOStringStream e;
e << " given unknown argument \"" << args[i] << "\".";
this->SetError(e.str().c_str());
return false;
}
} }
if ( !this->Makefile->CanIWriteThisFile(output_file.c_str()) ) if ( !this->Makefile->CanIWriteThisFile(output_file.c_str()) )
@ -294,8 +321,10 @@ bool cmExecuteProcessCommand
cmsysProcess_WaitForExit(cp, 0); cmsysProcess_WaitForExit(cp, 0);
// Fix the text in the output strings. // Fix the text in the output strings.
cmExecuteProcessCommandFixText(tempOutput); cmExecuteProcessCommandFixText(tempOutput,
cmExecuteProcessCommandFixText(tempError); output_strip_trailing_whitespace);
cmExecuteProcessCommandFixText(tempError,
error_strip_trailing_whitespace);
// Store the output obtained. // Store the output obtained.
if(!output_variable.empty() && tempOutput.size()) if(!output_variable.empty() && tempOutput.size())
@ -344,7 +373,8 @@ bool cmExecuteProcessCommand
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
void cmExecuteProcessCommandFixText(std::vector<char>& output) void cmExecuteProcessCommandFixText(std::vector<char>& output,
bool strip_trailing_whitespace)
{ {
// Remove \0 characters and the \r part of \r\n pairs. // Remove \0 characters and the \r part of \r\n pairs.
unsigned int in_index = 0; unsigned int in_index = 0;
@ -358,6 +388,18 @@ void cmExecuteProcessCommandFixText(std::vector<char>& output)
output[out_index++] = c; output[out_index++] = c;
} }
} }
// Remove trailing whitespace if requested.
if(strip_trailing_whitespace)
{
while(out_index > 0 &&
cmExecuteProcessCommandIsWhitespace(output[out_index-1]))
{
--out_index;
}
}
// Shrink the vector to the size needed.
output.resize(out_index); output.resize(out_index);
// Put a terminator on the text string. // Put a terminator on the text string.

View File

@ -78,7 +78,9 @@ public:
" [OUTPUT_FILE <file>]\n" " [OUTPUT_FILE <file>]\n"
" [ERROR_FILE <file>]\n" " [ERROR_FILE <file>]\n"
" [OUTPUT_QUIET]\n" " [OUTPUT_QUIET]\n"
" [ERROR_QUIET])\n" " [ERROR_QUIET]\n"
" [OUTPUT_STRIP_TRAILING_WHITESPACE]\n"
" [ERROR_STRIP_TRAILING_WHITESPACE])\n"
"Runs the given sequence of one or more commands with the standard " "Runs the given sequence of one or more commands with the standard "
"output of each process piped to the standard input of the next. " "output of each process piped to the standard input of the next. "
"A single standard error pipe is used for all processes. " "A single standard error pipe is used for all processes. "