2009-09-28 19:43:28 +04:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
2002-11-07 01:35:27 +03:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
2002-11-07 01:35:27 +03:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
|
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the License for more information.
|
|
|
|
============================================================================*/
|
2002-11-07 01:35:27 +03:00
|
|
|
#include "cmStringCommand.h"
|
2016-04-29 16:40:20 +03:00
|
|
|
|
2011-11-16 19:12:18 +04:00
|
|
|
#include "cmCryptoHash.h"
|
|
|
|
|
2003-06-23 22:10:12 +04:00
|
|
|
#include <cmsys/RegularExpression.hxx>
|
2005-10-17 17:10:20 +04:00
|
|
|
#include <cmsys/SystemTools.hxx>
|
2003-06-23 22:10:12 +04:00
|
|
|
|
2003-07-10 21:25:54 +04:00
|
|
|
#include <ctype.h>
|
2016-04-29 17:53:13 +03:00
|
|
|
#include <stdlib.h> // required for atoi
|
2007-04-24 00:48:56 +04:00
|
|
|
#include <time.h>
|
|
|
|
|
2012-10-05 23:55:07 +04:00
|
|
|
#include <cmTimestamp.h>
|
2014-08-26 00:44:06 +04:00
|
|
|
#include <cmUuid.h>
|
2012-10-05 23:55:07 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
bool cmStringCommand::InitialPass(std::vector<std::string> const& args,
|
|
|
|
cmExecutionStatus&)
|
2002-11-07 01:35:27 +03:00
|
|
|
{
|
2016-09-16 00:59:29 +03:00
|
|
|
if (args.empty()) {
|
2002-11-07 01:35:27 +03:00
|
|
|
this->SetError("must be called with at least one argument.");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
const std::string& subCommand = args[0];
|
|
|
|
if (subCommand == "REGEX") {
|
2002-11-07 01:35:27 +03:00
|
|
|
return this->HandleRegexCommand(args);
|
2016-09-16 23:45:24 +03:00
|
|
|
}
|
|
|
|
if (subCommand == "REPLACE") {
|
2005-10-17 17:10:20 +04:00
|
|
|
return this->HandleReplaceCommand(args);
|
2016-09-16 23:45:24 +03:00
|
|
|
}
|
|
|
|
if (subCommand == "MD5" || subCommand == "SHA1" || subCommand == "SHA224" ||
|
|
|
|
subCommand == "SHA256" || subCommand == "SHA384" ||
|
|
|
|
subCommand == "SHA512") {
|
2011-11-16 19:12:18 +04:00
|
|
|
return this->HandleHashCommand(args);
|
2016-09-16 23:45:24 +03:00
|
|
|
}
|
|
|
|
if (subCommand == "TOLOWER") {
|
2003-07-10 21:25:54 +04:00
|
|
|
return this->HandleToUpperLowerCommand(args, false);
|
2016-09-16 23:45:24 +03:00
|
|
|
}
|
|
|
|
if (subCommand == "TOUPPER") {
|
2003-07-10 21:25:54 +04:00
|
|
|
return this->HandleToUpperLowerCommand(args, true);
|
2016-09-16 23:45:24 +03:00
|
|
|
}
|
|
|
|
if (subCommand == "COMPARE") {
|
2002-12-05 02:44:39 +03:00
|
|
|
return this->HandleCompareCommand(args);
|
2016-09-16 23:45:24 +03:00
|
|
|
}
|
|
|
|
if (subCommand == "ASCII") {
|
2003-01-02 01:34:47 +03:00
|
|
|
return this->HandleAsciiCommand(args);
|
2016-09-16 23:45:24 +03:00
|
|
|
}
|
|
|
|
if (subCommand == "CONFIGURE") {
|
2004-03-04 18:05:14 +03:00
|
|
|
return this->HandleConfigureCommand(args);
|
2016-09-16 23:45:24 +03:00
|
|
|
}
|
|
|
|
if (subCommand == "LENGTH") {
|
2005-10-17 17:56:42 +04:00
|
|
|
return this->HandleLengthCommand(args);
|
2016-09-16 23:45:24 +03:00
|
|
|
}
|
|
|
|
if (subCommand == "APPEND") {
|
2015-07-06 23:28:04 +03:00
|
|
|
return this->HandleAppendCommand(args);
|
2016-09-16 23:45:24 +03:00
|
|
|
}
|
|
|
|
if (subCommand == "CONCAT") {
|
2013-10-21 20:49:15 +04:00
|
|
|
return this->HandleConcatCommand(args);
|
2016-09-16 23:45:24 +03:00
|
|
|
}
|
|
|
|
if (subCommand == "SUBSTRING") {
|
2005-10-17 17:56:42 +04:00
|
|
|
return this->HandleSubstringCommand(args);
|
2016-09-16 23:45:24 +03:00
|
|
|
}
|
|
|
|
if (subCommand == "STRIP") {
|
2007-04-27 05:50:52 +04:00
|
|
|
return this->HandleStripCommand(args);
|
2016-09-16 23:45:24 +03:00
|
|
|
}
|
|
|
|
if (subCommand == "RANDOM") {
|
2007-04-23 19:04:12 +04:00
|
|
|
return this->HandleRandomCommand(args);
|
2016-09-16 23:45:24 +03:00
|
|
|
}
|
|
|
|
if (subCommand == "FIND") {
|
2011-02-15 13:20:47 +03:00
|
|
|
return this->HandleFindCommand(args);
|
2016-09-16 23:45:24 +03:00
|
|
|
}
|
|
|
|
if (subCommand == "TIMESTAMP") {
|
2012-10-05 23:55:07 +04:00
|
|
|
return this->HandleTimestampCommand(args);
|
2016-09-16 23:45:24 +03:00
|
|
|
}
|
|
|
|
if (subCommand == "MAKE_C_IDENTIFIER") {
|
2013-03-21 22:34:32 +04:00
|
|
|
return this->HandleMakeCIdentifierCommand(args);
|
2016-09-16 23:45:24 +03:00
|
|
|
}
|
|
|
|
if (subCommand == "GENEX_STRIP") {
|
2014-03-18 19:21:08 +04:00
|
|
|
return this->HandleGenexStripCommand(args);
|
2016-09-16 23:45:24 +03:00
|
|
|
}
|
|
|
|
if (subCommand == "UUID") {
|
2014-08-26 00:44:06 +04:00
|
|
|
return this->HandleUuidCommand(args);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2011-02-15 13:20:47 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
std::string e = "does not recognize sub-command " + subCommand;
|
2014-03-11 03:04:11 +04:00
|
|
|
this->SetError(e);
|
2002-11-07 01:35:27 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-11-16 19:12:18 +04:00
|
|
|
bool cmStringCommand::HandleHashCommand(std::vector<std::string> const& args)
|
|
|
|
{
|
2011-11-16 20:03:30 +04:00
|
|
|
#if defined(CMAKE_BUILD_WITH_CMAKE)
|
2016-05-16 17:34:04 +03:00
|
|
|
if (args.size() != 3) {
|
2015-01-05 22:31:31 +03:00
|
|
|
std::ostringstream e;
|
2011-11-16 19:12:18 +04:00
|
|
|
e << args[0] << " requires an output variable and an input string";
|
2014-03-11 03:04:11 +04:00
|
|
|
this->SetError(e.str());
|
2011-11-16 19:12:18 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2011-11-16 19:12:18 +04:00
|
|
|
|
2016-06-28 17:17:52 +03:00
|
|
|
CM_AUTO_PTR<cmCryptoHash> hash(cmCryptoHash::New(args[0].c_str()));
|
2016-05-16 17:34:04 +03:00
|
|
|
if (hash.get()) {
|
2014-03-11 03:04:11 +04:00
|
|
|
std::string out = hash->HashString(args[2]);
|
|
|
|
this->Makefile->AddDefinition(args[1], out.c_str());
|
2011-11-16 19:12:18 +04:00
|
|
|
return true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2011-11-16 19:12:18 +04:00
|
|
|
return false;
|
2011-11-16 20:03:30 +04:00
|
|
|
#else
|
2015-01-05 22:31:31 +03:00
|
|
|
std::ostringstream e;
|
2011-11-16 20:03:30 +04:00
|
|
|
e << args[0] << " not available during bootstrap";
|
|
|
|
this->SetError(e.str().c_str());
|
|
|
|
return false;
|
|
|
|
#endif
|
2011-11-16 19:12:18 +04:00
|
|
|
}
|
|
|
|
|
2003-07-10 21:25:54 +04:00
|
|
|
bool cmStringCommand::HandleToUpperLowerCommand(
|
|
|
|
std::vector<std::string> const& args, bool toUpper)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (args.size() < 3) {
|
2003-07-10 21:25:54 +04:00
|
|
|
this->SetError("no output variable specified");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2003-07-10 21:25:54 +04:00
|
|
|
|
|
|
|
std::string outvar = args[2];
|
|
|
|
std::string output;
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (toUpper) {
|
2003-07-10 21:25:54 +04:00
|
|
|
output = cmSystemTools::UpperCase(args[1]);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2003-07-10 21:25:54 +04:00
|
|
|
output = cmSystemTools::LowerCase(args[1]);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2003-07-10 21:25:54 +04:00
|
|
|
|
|
|
|
// Store the output in the provided variable.
|
2014-03-11 03:04:11 +04:00
|
|
|
this->Makefile->AddDefinition(outvar, output.c_str());
|
2003-07-10 21:25:54 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2003-01-02 01:34:47 +03:00
|
|
|
bool cmStringCommand::HandleAsciiCommand(std::vector<std::string> const& args)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (args.size() < 3) {
|
2003-01-02 01:34:47 +03:00
|
|
|
this->SetError("No output variable specified");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2003-01-02 01:34:47 +03:00
|
|
|
std::string::size_type cc;
|
2016-05-16 17:34:04 +03:00
|
|
|
std::string outvar = args[args.size() - 1];
|
2003-01-02 01:34:47 +03:00
|
|
|
std::string output = "";
|
2016-05-16 17:34:04 +03:00
|
|
|
for (cc = 1; cc < args.size() - 1; cc++) {
|
2003-01-02 01:34:47 +03:00
|
|
|
int ch = atoi(args[cc].c_str());
|
2016-05-16 17:34:04 +03:00
|
|
|
if (ch > 0 && ch < 256) {
|
2003-01-02 01:34:47 +03:00
|
|
|
output += static_cast<char>(ch);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2003-01-02 01:34:47 +03:00
|
|
|
std::string error = "Character with code ";
|
2009-09-30 19:41:34 +04:00
|
|
|
error += args[cc];
|
2003-01-02 01:34:47 +03:00
|
|
|
error += " does not exist.";
|
2014-03-11 03:04:11 +04:00
|
|
|
this->SetError(error);
|
2003-01-02 01:34:47 +03:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2003-01-02 01:34:47 +03:00
|
|
|
// Store the output in the provided variable.
|
2014-03-11 03:04:11 +04:00
|
|
|
this->Makefile->AddDefinition(outvar, output.c_str());
|
2003-01-02 01:34:47 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-03-04 18:05:14 +03:00
|
|
|
bool cmStringCommand::HandleConfigureCommand(
|
|
|
|
std::vector<std::string> const& args)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (args.size() < 2) {
|
2004-03-04 18:05:14 +03:00
|
|
|
this->SetError("No input string specified.");
|
|
|
|
return false;
|
2016-09-16 23:45:24 +03:00
|
|
|
}
|
|
|
|
if (args.size() < 3) {
|
2004-03-04 18:05:14 +03:00
|
|
|
this->SetError("No output variable specified.");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2004-03-04 18:05:14 +03:00
|
|
|
|
|
|
|
// Parse options.
|
|
|
|
bool escapeQuotes = false;
|
|
|
|
bool atOnly = false;
|
2016-05-16 17:34:04 +03:00
|
|
|
for (unsigned int i = 3; i < args.size(); ++i) {
|
|
|
|
if (args[i] == "@ONLY") {
|
2004-03-04 18:05:14 +03:00
|
|
|
atOnly = true;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (args[i] == "ESCAPE_QUOTES") {
|
2004-03-04 18:05:14 +03:00
|
|
|
escapeQuotes = true;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2015-01-05 22:31:31 +03:00
|
|
|
std::ostringstream err;
|
2004-03-04 18:05:14 +03:00
|
|
|
err << "Unrecognized argument \"" << args[i] << "\"";
|
2014-03-11 03:04:11 +04:00
|
|
|
this->SetError(err.str());
|
2004-03-04 18:05:14 +03:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2004-03-04 18:05:14 +03:00
|
|
|
|
|
|
|
// Configure the string.
|
|
|
|
std::string output;
|
2006-03-15 19:02:08 +03:00
|
|
|
this->Makefile->ConfigureString(args[1], output, atOnly, escapeQuotes);
|
2004-03-04 18:05:14 +03:00
|
|
|
|
|
|
|
// Store the output in the provided variable.
|
2014-03-11 03:04:11 +04:00
|
|
|
this->Makefile->AddDefinition(args[2], output.c_str());
|
2004-03-04 18:05:14 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2002-11-07 01:35:27 +03:00
|
|
|
bool cmStringCommand::HandleRegexCommand(std::vector<std::string> const& args)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (args.size() < 2) {
|
2002-11-07 01:35:27 +03:00
|
|
|
this->SetError("sub-command REGEX requires a mode to be specified.");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2002-11-07 01:35:27 +03:00
|
|
|
std::string mode = args[1];
|
2016-05-16 17:34:04 +03:00
|
|
|
if (mode == "MATCH") {
|
|
|
|
if (args.size() < 5) {
|
2002-11-07 01:35:27 +03:00
|
|
|
this->SetError("sub-command REGEX, mode MATCH needs "
|
|
|
|
"at least 5 arguments total to command.");
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
return this->RegexMatch(args);
|
2016-09-16 23:45:24 +03:00
|
|
|
}
|
|
|
|
if (mode == "MATCHALL") {
|
2016-05-16 17:34:04 +03:00
|
|
|
if (args.size() < 5) {
|
2002-11-07 01:35:27 +03:00
|
|
|
this->SetError("sub-command REGEX, mode MATCHALL needs "
|
|
|
|
"at least 5 arguments total to command.");
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
return this->RegexMatchAll(args);
|
2016-09-16 23:45:24 +03:00
|
|
|
}
|
|
|
|
if (mode == "REPLACE") {
|
2016-05-16 17:34:04 +03:00
|
|
|
if (args.size() < 6) {
|
2008-08-26 20:54:06 +04:00
|
|
|
this->SetError("sub-command REGEX, mode REPLACE needs "
|
2002-11-07 01:35:27 +03:00
|
|
|
"at least 6 arguments total to command.");
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
return this->RegexReplace(args);
|
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
std::string e = "sub-command REGEX does not recognize mode " + mode;
|
2014-03-11 03:04:11 +04:00
|
|
|
this->SetError(e);
|
2002-11-07 01:35:27 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmStringCommand::RegexMatch(std::vector<std::string> const& args)
|
|
|
|
{
|
2006-03-10 21:54:57 +03:00
|
|
|
//"STRING(REGEX MATCH <regular_expression> <output variable>
|
|
|
|
// <input> [<input>...])\n";
|
2002-11-07 01:35:27 +03:00
|
|
|
std::string regex = args[2];
|
|
|
|
std::string outvar = args[3];
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2014-12-01 18:51:49 +03:00
|
|
|
this->Makefile->ClearMatches();
|
2002-11-07 01:35:27 +03:00
|
|
|
// Compile the regular expression.
|
2003-06-23 22:10:12 +04:00
|
|
|
cmsys::RegularExpression re;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!re.compile(regex.c_str())) {
|
2012-08-13 21:42:58 +04:00
|
|
|
std::string e =
|
2016-05-16 17:34:04 +03:00
|
|
|
"sub-command REGEX, mode MATCH failed to compile regex \"" + regex +
|
|
|
|
"\".";
|
2014-03-11 03:04:11 +04:00
|
|
|
this->SetError(e);
|
2002-11-07 01:35:27 +03:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2015-02-11 00:23:41 +03:00
|
|
|
// Concatenate all the last arguments together.
|
2015-07-18 11:45:18 +03:00
|
|
|
std::string input = cmJoin(cmMakeRange(args).advance(4), std::string());
|
2015-02-11 00:23:41 +03:00
|
|
|
|
2002-11-07 01:35:27 +03:00
|
|
|
// Scan through the input for all matches.
|
|
|
|
std::string output;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (re.find(input.c_str())) {
|
2014-12-01 18:51:49 +03:00
|
|
|
this->Makefile->StoreMatches(re);
|
2002-11-07 01:35:27 +03:00
|
|
|
std::string::size_type l = re.start();
|
|
|
|
std::string::size_type r = re.end();
|
2016-05-16 17:34:04 +03:00
|
|
|
if (r - l == 0) {
|
|
|
|
std::string e = "sub-command REGEX, mode MATCH regex \"" + regex +
|
2006-03-10 21:54:57 +03:00
|
|
|
"\" matched an empty string.";
|
2014-03-11 03:04:11 +04:00
|
|
|
this->SetError(e);
|
2002-11-07 01:35:27 +03:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
output = input.substr(l, r - l);
|
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-11-07 01:35:27 +03:00
|
|
|
// Store the output in the provided variable.
|
2014-03-11 03:04:11 +04:00
|
|
|
this->Makefile->AddDefinition(outvar, output.c_str());
|
2002-11-07 01:35:27 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmStringCommand::RegexMatchAll(std::vector<std::string> const& args)
|
|
|
|
{
|
2012-08-13 21:42:58 +04:00
|
|
|
//"STRING(REGEX MATCHALL <regular_expression> <output variable> <input>
|
2006-03-10 21:54:57 +03:00
|
|
|
// [<input>...])\n";
|
2002-11-07 01:35:27 +03:00
|
|
|
std::string regex = args[2];
|
|
|
|
std::string outvar = args[3];
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2014-12-01 18:51:49 +03:00
|
|
|
this->Makefile->ClearMatches();
|
2002-11-07 01:35:27 +03:00
|
|
|
// Compile the regular expression.
|
2003-06-23 22:10:12 +04:00
|
|
|
cmsys::RegularExpression re;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!re.compile(regex.c_str())) {
|
2006-03-10 21:54:57 +03:00
|
|
|
std::string e =
|
2016-05-16 17:34:04 +03:00
|
|
|
"sub-command REGEX, mode MATCHALL failed to compile regex \"" + regex +
|
|
|
|
"\".";
|
2014-03-11 03:04:11 +04:00
|
|
|
this->SetError(e);
|
2002-11-07 01:35:27 +03:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2015-02-11 00:23:41 +03:00
|
|
|
// Concatenate all the last arguments together.
|
2015-07-18 11:45:18 +03:00
|
|
|
std::string input = cmJoin(cmMakeRange(args).advance(4), std::string());
|
2015-02-11 00:23:41 +03:00
|
|
|
|
2002-11-07 01:35:27 +03:00
|
|
|
// Scan through the input for all matches.
|
|
|
|
std::string output;
|
|
|
|
const char* p = input.c_str();
|
2016-05-16 17:34:04 +03:00
|
|
|
while (re.find(p)) {
|
2014-12-01 18:51:49 +03:00
|
|
|
this->Makefile->StoreMatches(re);
|
2002-11-07 01:35:27 +03:00
|
|
|
std::string::size_type l = re.start();
|
|
|
|
std::string::size_type r = re.end();
|
2016-05-16 17:34:04 +03:00
|
|
|
if (r - l == 0) {
|
|
|
|
std::string e = "sub-command REGEX, mode MATCHALL regex \"" + regex +
|
|
|
|
"\" matched an empty string.";
|
2014-03-11 03:04:11 +04:00
|
|
|
this->SetError(e);
|
2002-11-07 01:35:27 +03:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (!output.empty()) {
|
2002-11-07 01:35:27 +03:00
|
|
|
output += ";";
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
output += std::string(p + l, r - l);
|
|
|
|
p += r;
|
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-11-07 01:35:27 +03:00
|
|
|
// Store the output in the provided variable.
|
2014-03-11 03:04:11 +04:00
|
|
|
this->Makefile->AddDefinition(outvar, output.c_str());
|
2002-11-07 01:35:27 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmStringCommand::RegexReplace(std::vector<std::string> const& args)
|
|
|
|
{
|
2012-08-13 21:42:58 +04:00
|
|
|
//"STRING(REGEX REPLACE <regular_expression> <replace_expression>
|
2006-03-10 21:54:57 +03:00
|
|
|
// <output variable> <input> [<input>...])\n"
|
2002-11-07 01:35:27 +03:00
|
|
|
std::string regex = args[2];
|
2012-08-13 21:42:58 +04:00
|
|
|
std::string replace = args[3];
|
2002-11-07 01:35:27 +03:00
|
|
|
std::string outvar = args[4];
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-11-07 01:35:27 +03:00
|
|
|
// Pull apart the replace expression to find the escaped [0-9] values.
|
|
|
|
std::vector<RegexReplacement> replacement;
|
|
|
|
std::string::size_type l = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
while (l < replace.length()) {
|
2002-11-07 01:35:27 +03:00
|
|
|
std::string::size_type r = replace.find("\\", l);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (r == std::string::npos) {
|
2002-11-07 01:35:27 +03:00
|
|
|
r = replace.length();
|
2016-05-16 17:34:04 +03:00
|
|
|
replacement.push_back(replace.substr(l, r - l));
|
|
|
|
} else {
|
|
|
|
if (r - l > 0) {
|
|
|
|
replacement.push_back(replace.substr(l, r - l));
|
2002-11-07 01:35:27 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
if (r == (replace.length() - 1)) {
|
2002-11-07 01:35:27 +03:00
|
|
|
this->SetError("sub-command REGEX, mode REPLACE: "
|
|
|
|
"replace-expression ends in a backslash.");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if ((replace[r + 1] >= '0') && (replace[r + 1] <= '9')) {
|
|
|
|
replacement.push_back(replace[r + 1] - '0');
|
|
|
|
} else if (replace[r + 1] == 'n') {
|
2002-11-07 01:35:27 +03:00
|
|
|
replacement.push_back("\n");
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (replace[r + 1] == '\\') {
|
2002-11-07 01:35:27 +03:00
|
|
|
replacement.push_back("\\");
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2002-11-07 01:35:27 +03:00
|
|
|
std::string e = "sub-command REGEX, mode REPLACE: Unknown escape \"";
|
|
|
|
e += replace.substr(r, 2);
|
2009-10-02 22:51:43 +04:00
|
|
|
e += "\" in replace-expression.";
|
2014-03-11 03:04:11 +04:00
|
|
|
this->SetError(e);
|
2002-11-07 01:35:27 +03:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
r += 2;
|
2002-11-07 01:35:27 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
l = r;
|
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2014-12-01 18:51:49 +03:00
|
|
|
this->Makefile->ClearMatches();
|
2002-11-07 01:35:27 +03:00
|
|
|
// Compile the regular expression.
|
2003-06-23 22:10:12 +04:00
|
|
|
cmsys::RegularExpression re;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!re.compile(regex.c_str())) {
|
2012-08-13 21:42:58 +04:00
|
|
|
std::string e =
|
2016-05-16 17:34:04 +03:00
|
|
|
"sub-command REGEX, mode REPLACE failed to compile regex \"" + regex +
|
|
|
|
"\".";
|
2014-03-11 03:04:11 +04:00
|
|
|
this->SetError(e);
|
2002-11-07 01:35:27 +03:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2015-02-11 00:23:41 +03:00
|
|
|
// Concatenate all the last arguments together.
|
2015-07-18 11:45:18 +03:00
|
|
|
std::string input = cmJoin(cmMakeRange(args).advance(5), std::string());
|
2015-02-11 00:23:41 +03:00
|
|
|
|
2002-11-07 01:35:27 +03:00
|
|
|
// Scan through the input for all matches.
|
|
|
|
std::string output;
|
|
|
|
std::string::size_type base = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
while (re.find(input.c_str() + base)) {
|
2014-12-01 18:51:49 +03:00
|
|
|
this->Makefile->StoreMatches(re);
|
2002-12-03 00:35:04 +03:00
|
|
|
std::string::size_type l2 = re.start();
|
2002-11-07 01:35:27 +03:00
|
|
|
std::string::size_type r = re.end();
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-11-07 01:35:27 +03:00
|
|
|
// Concatenate the part of the input that was not matched.
|
2002-12-03 00:35:04 +03:00
|
|
|
output += input.substr(base, l2);
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-11-07 01:35:27 +03:00
|
|
|
// Make sure the match had some text.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (r - l2 == 0) {
|
|
|
|
std::string e = "sub-command REGEX, mode REPLACE regex \"" + regex +
|
|
|
|
"\" matched an empty string.";
|
2014-03-11 03:04:11 +04:00
|
|
|
this->SetError(e);
|
2002-11-07 01:35:27 +03:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-11-07 01:35:27 +03:00
|
|
|
// Concatenate the replacement for the match.
|
2016-05-16 17:34:04 +03:00
|
|
|
for (unsigned int i = 0; i < replacement.size(); ++i) {
|
|
|
|
if (replacement[i].number < 0) {
|
2002-11-07 01:35:27 +03:00
|
|
|
// This is just a plain-text part of the replacement.
|
|
|
|
output += replacement[i].value;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2002-11-07 01:35:27 +03:00
|
|
|
// Replace with part of the match.
|
|
|
|
int n = replacement[i].number;
|
|
|
|
std::string::size_type start = re.start(n);
|
|
|
|
std::string::size_type end = re.end(n);
|
2016-05-16 17:34:04 +03:00
|
|
|
std::string::size_type len = input.length() - base;
|
|
|
|
if ((start != std::string::npos) && (end != std::string::npos) &&
|
|
|
|
(start <= len) && (end <= len)) {
|
|
|
|
output += input.substr(base + start, end - start);
|
|
|
|
} else {
|
2002-11-07 01:35:27 +03:00
|
|
|
std::string e =
|
2016-05-16 17:34:04 +03:00
|
|
|
"sub-command REGEX, mode REPLACE: replace expression \"" +
|
|
|
|
replace + "\" contains an out-of-range escape for regex \"" +
|
|
|
|
regex + "\".";
|
2014-03-11 03:04:11 +04:00
|
|
|
this->SetError(e);
|
2002-11-07 01:35:27 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-11-07 01:35:27 +03:00
|
|
|
// Move past the match.
|
|
|
|
base += r;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-11-07 01:35:27 +03:00
|
|
|
// Concatenate the text after the last match.
|
2016-05-16 17:34:04 +03:00
|
|
|
output += input.substr(base, input.length() - base);
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-11-07 01:35:27 +03:00
|
|
|
// Store the output in the provided variable.
|
2014-03-11 03:04:11 +04:00
|
|
|
this->Makefile->AddDefinition(outvar, output.c_str());
|
2002-11-07 01:35:27 +03:00
|
|
|
return true;
|
|
|
|
}
|
2002-12-05 02:44:39 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
bool cmStringCommand::HandleFindCommand(std::vector<std::string> const& args)
|
2011-02-15 13:20:47 +03:00
|
|
|
{
|
|
|
|
// check if all required parameters were passed
|
2016-05-16 17:34:04 +03:00
|
|
|
if (args.size() < 4 || args.size() > 5) {
|
2011-02-15 13:20:47 +03:00
|
|
|
this->SetError("sub-command FIND requires 3 or 4 parameters.");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2011-02-15 13:20:47 +03:00
|
|
|
|
|
|
|
// check if the reverse flag was set or not
|
|
|
|
bool reverseMode = false;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (args.size() == 5 && args[4] == "REVERSE") {
|
2011-02-15 13:20:47 +03:00
|
|
|
reverseMode = true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2011-02-15 13:20:47 +03:00
|
|
|
|
|
|
|
// if we have 5 arguments the last one must be REVERSE
|
2016-05-16 17:34:04 +03:00
|
|
|
if (args.size() == 5 && args[4] != "REVERSE") {
|
2011-02-15 13:20:47 +03:00
|
|
|
this->SetError("sub-command FIND: unknown last parameter");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2011-02-15 13:20:47 +03:00
|
|
|
|
|
|
|
// local parameter names.
|
|
|
|
const std::string& sstring = args[1];
|
|
|
|
const std::string& schar = args[2];
|
|
|
|
const std::string& outvar = args[3];
|
|
|
|
|
|
|
|
// ensure that the user cannot accidentally specify REVERSE as a variable
|
2016-05-16 17:34:04 +03:00
|
|
|
if (outvar == "REVERSE") {
|
2016-03-29 21:24:20 +03:00
|
|
|
this->SetError("sub-command FIND does not allow one to select REVERSE as "
|
2011-02-15 13:20:47 +03:00
|
|
|
"the output variable. "
|
|
|
|
"Maybe you missed the actual output variable?");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2011-02-15 13:20:47 +03:00
|
|
|
|
|
|
|
// try to find the character and return its position
|
|
|
|
size_t pos;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!reverseMode) {
|
2011-02-15 13:20:47 +03:00
|
|
|
pos = sstring.find(schar);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2011-02-15 13:20:47 +03:00
|
|
|
pos = sstring.rfind(schar);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (std::string::npos != pos) {
|
2015-01-05 22:31:31 +03:00
|
|
|
std::ostringstream s;
|
2011-02-15 13:20:47 +03:00
|
|
|
s << pos;
|
2014-03-11 03:04:11 +04:00
|
|
|
this->Makefile->AddDefinition(outvar, s.str().c_str());
|
2011-02-15 13:20:47 +03:00
|
|
|
return true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2011-02-15 13:20:47 +03:00
|
|
|
|
|
|
|
// the character was not found, but this is not really an error
|
2014-03-11 03:04:11 +04:00
|
|
|
this->Makefile->AddDefinition(outvar, "-1");
|
2011-02-15 13:20:47 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
bool cmStringCommand::HandleCompareCommand(
|
|
|
|
std::vector<std::string> const& args)
|
2002-12-05 02:44:39 +03:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (args.size() < 2) {
|
2002-12-05 02:44:39 +03:00
|
|
|
this->SetError("sub-command COMPARE requires a mode to be specified.");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2002-12-05 02:44:39 +03:00
|
|
|
std::string mode = args[1];
|
2016-05-16 17:34:04 +03:00
|
|
|
if ((mode == "EQUAL") || (mode == "NOTEQUAL") || (mode == "LESS") ||
|
2016-08-05 21:11:46 +03:00
|
|
|
(mode == "LESS_EQUAL") || (mode == "GREATER") ||
|
|
|
|
(mode == "GREATER_EQUAL")) {
|
2016-05-16 17:34:04 +03:00
|
|
|
if (args.size() < 5) {
|
2002-12-05 02:44:39 +03:00
|
|
|
std::string e = "sub-command COMPARE, mode ";
|
|
|
|
e += mode;
|
|
|
|
e += " needs at least 5 arguments total to command.";
|
2014-03-11 03:04:11 +04:00
|
|
|
this->SetError(e);
|
2002-12-05 02:44:39 +03:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-12-05 02:44:39 +03:00
|
|
|
const std::string& left = args[2];
|
2012-08-13 21:42:58 +04:00
|
|
|
const std::string& right = args[3];
|
2002-12-05 02:44:39 +03:00
|
|
|
const std::string& outvar = args[4];
|
|
|
|
bool result;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (mode == "LESS") {
|
2002-12-05 02:44:39 +03:00
|
|
|
result = (left < right);
|
2016-08-05 21:11:46 +03:00
|
|
|
} else if (mode == "LESS_EQUAL") {
|
|
|
|
result = (left <= right);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (mode == "GREATER") {
|
2002-12-05 02:44:39 +03:00
|
|
|
result = (left > right);
|
2016-08-05 21:11:46 +03:00
|
|
|
} else if (mode == "GREATER_EQUAL") {
|
|
|
|
result = (left >= right);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (mode == "EQUAL") {
|
2002-12-05 02:44:39 +03:00
|
|
|
result = (left == right);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else // if(mode == "NOTEQUAL")
|
|
|
|
{
|
2002-12-05 02:44:39 +03:00
|
|
|
result = !(left == right);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (result) {
|
2014-03-11 03:04:11 +04:00
|
|
|
this->Makefile->AddDefinition(outvar, "1");
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2014-03-11 03:04:11 +04:00
|
|
|
this->Makefile->AddDefinition(outvar, "0");
|
2012-08-13 21:42:58 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
std::string e = "sub-command COMPARE does not recognize mode " + mode;
|
2014-03-11 03:04:11 +04:00
|
|
|
this->SetError(e);
|
2002-12-05 02:44:39 +03:00
|
|
|
return false;
|
|
|
|
}
|
2005-10-17 17:10:20 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
bool cmStringCommand::HandleReplaceCommand(
|
|
|
|
std::vector<std::string> const& args)
|
2005-10-17 17:10:20 +04:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (args.size() < 5) {
|
2009-10-02 22:51:43 +04:00
|
|
|
this->SetError("sub-command REPLACE requires at least four arguments.");
|
2005-10-17 17:10:20 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2005-10-17 17:10:20 +04:00
|
|
|
|
|
|
|
const std::string& matchExpression = args[1];
|
|
|
|
const std::string& replaceExpression = args[2];
|
|
|
|
const std::string& variableName = args[3];
|
|
|
|
|
2015-07-18 11:45:18 +03:00
|
|
|
std::string input = cmJoin(cmMakeRange(args).advance(4), std::string());
|
2005-10-17 17:10:20 +04:00
|
|
|
|
2012-08-13 21:42:58 +04:00
|
|
|
cmsys::SystemTools::ReplaceString(input, matchExpression.c_str(),
|
2006-03-10 21:54:57 +03:00
|
|
|
replaceExpression.c_str());
|
2005-10-17 17:10:20 +04:00
|
|
|
|
2014-03-11 03:04:11 +04:00
|
|
|
this->Makefile->AddDefinition(variableName, input.c_str());
|
2005-10-17 17:10:20 +04:00
|
|
|
return true;
|
|
|
|
}
|
2005-10-17 17:56:42 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
bool cmStringCommand::HandleSubstringCommand(
|
|
|
|
std::vector<std::string> const& args)
|
2005-10-17 17:56:42 +04:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (args.size() != 5) {
|
2009-10-02 22:51:43 +04:00
|
|
|
this->SetError("sub-command SUBSTRING requires four arguments.");
|
2005-10-17 17:56:42 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2005-10-17 17:56:42 +04:00
|
|
|
|
|
|
|
const std::string& stringValue = args[1];
|
|
|
|
int begin = atoi(args[2].c_str());
|
|
|
|
int end = atoi(args[3].c_str());
|
|
|
|
const std::string& variableName = args[4];
|
|
|
|
|
|
|
|
size_t stringLength = stringValue.size();
|
|
|
|
int intStringLength = static_cast<int>(stringLength);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (begin < 0 || begin > intStringLength) {
|
2015-01-05 22:31:31 +03:00
|
|
|
std::ostringstream ostr;
|
2006-03-10 21:54:57 +03:00
|
|
|
ostr << "begin index: " << begin << " is out of range 0 - "
|
|
|
|
<< stringLength;
|
2014-03-11 03:04:11 +04:00
|
|
|
this->SetError(ostr.str());
|
2005-10-17 17:56:42 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (end < -1) {
|
2015-01-05 22:31:31 +03:00
|
|
|
std::ostringstream ostr;
|
2014-11-13 01:57:16 +03:00
|
|
|
ostr << "end index: " << end << " should be -1 or greater";
|
2014-03-11 03:04:11 +04:00
|
|
|
this->SetError(ostr.str());
|
2005-10-17 17:56:42 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2005-10-17 17:56:42 +04:00
|
|
|
|
2014-03-11 03:04:11 +04:00
|
|
|
this->Makefile->AddDefinition(variableName,
|
2006-03-15 19:02:08 +03:00
|
|
|
stringValue.substr(begin, end).c_str());
|
2005-10-17 17:56:42 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
bool cmStringCommand::HandleLengthCommand(std::vector<std::string> const& args)
|
2005-10-17 17:56:42 +04:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (args.size() != 3) {
|
2005-10-17 17:56:42 +04:00
|
|
|
this->SetError("sub-command LENGTH requires two arguments.");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2005-10-17 17:56:42 +04:00
|
|
|
|
|
|
|
const std::string& stringValue = args[1];
|
|
|
|
const std::string& variableName = args[2];
|
|
|
|
|
|
|
|
size_t length = stringValue.size();
|
|
|
|
char buffer[1024];
|
2005-10-18 17:42:35 +04:00
|
|
|
sprintf(buffer, "%d", static_cast<int>(length));
|
2005-10-17 17:56:42 +04:00
|
|
|
|
2014-03-11 03:04:11 +04:00
|
|
|
this->Makefile->AddDefinition(variableName, buffer);
|
2005-10-17 17:56:42 +04:00
|
|
|
return true;
|
|
|
|
}
|
2007-04-23 19:04:12 +04:00
|
|
|
|
2015-07-06 23:28:04 +03:00
|
|
|
bool cmStringCommand::HandleAppendCommand(std::vector<std::string> const& args)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (args.size() < 2) {
|
2015-07-06 23:28:04 +03:00
|
|
|
this->SetError("sub-command APPEND requires at least one argument.");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-06 23:28:04 +03:00
|
|
|
|
|
|
|
// Skip if nothing to append.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (args.size() < 3) {
|
2015-07-06 23:28:04 +03:00
|
|
|
return true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-06 23:28:04 +03:00
|
|
|
|
|
|
|
const std::string& variable = args[1];
|
|
|
|
|
|
|
|
std::string value;
|
|
|
|
const char* oldValue = this->Makefile->GetDefinition(variable);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (oldValue) {
|
2015-07-06 23:28:04 +03:00
|
|
|
value = oldValue;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-18 11:45:18 +03:00
|
|
|
value += cmJoin(cmMakeRange(args).advance(2), std::string());
|
2015-07-06 23:28:04 +03:00
|
|
|
this->Makefile->AddDefinition(variable, value.c_str());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
bool cmStringCommand::HandleConcatCommand(std::vector<std::string> const& args)
|
2013-10-21 20:49:15 +04:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (args.size() < 2) {
|
2013-10-21 20:49:15 +04:00
|
|
|
this->SetError("sub-command CONCAT requires at least one argument.");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-10-21 20:49:15 +04:00
|
|
|
|
|
|
|
std::string const& variableName = args[1];
|
2015-07-18 11:45:18 +03:00
|
|
|
std::string value = cmJoin(cmMakeRange(args).advance(2), std::string());
|
2013-10-21 20:49:15 +04:00
|
|
|
|
2014-03-11 03:04:11 +04:00
|
|
|
this->Makefile->AddDefinition(variableName, value.c_str());
|
2013-10-21 20:49:15 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
bool cmStringCommand::HandleMakeCIdentifierCommand(
|
|
|
|
std::vector<std::string> const& args)
|
2013-03-21 22:34:32 +04:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (args.size() != 3) {
|
2013-03-21 22:34:32 +04:00
|
|
|
this->SetError("sub-command MAKE_C_IDENTIFIER requires two arguments.");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-03-21 22:34:32 +04:00
|
|
|
|
|
|
|
const std::string& input = args[1];
|
|
|
|
const std::string& variableName = args[2];
|
|
|
|
|
2014-03-11 03:04:11 +04:00
|
|
|
this->Makefile->AddDefinition(variableName,
|
2016-05-16 17:34:04 +03:00
|
|
|
cmSystemTools::MakeCidentifier(input).c_str());
|
2013-03-21 22:34:32 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
bool cmStringCommand::HandleGenexStripCommand(
|
|
|
|
std::vector<std::string> const& args)
|
2014-03-18 19:21:08 +04:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (args.size() != 3) {
|
2014-03-18 19:21:08 +04:00
|
|
|
this->SetError("sub-command GENEX_STRIP requires two arguments.");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-03-18 19:21:08 +04:00
|
|
|
|
|
|
|
const std::string& input = args[1];
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
std::string result = cmGeneratorExpression::Preprocess(
|
|
|
|
input, cmGeneratorExpression::StripAllGeneratorExpressions);
|
2014-03-18 19:21:08 +04:00
|
|
|
|
|
|
|
const std::string& variableName = args[2];
|
|
|
|
|
|
|
|
this->Makefile->AddDefinition(variableName, result.c_str());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
bool cmStringCommand::HandleStripCommand(std::vector<std::string> const& args)
|
2007-04-27 05:50:52 +04:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (args.size() != 3) {
|
2009-10-02 22:51:43 +04:00
|
|
|
this->SetError("sub-command STRIP requires two arguments.");
|
2007-04-27 05:50:52 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-04-27 05:50:52 +04:00
|
|
|
|
|
|
|
const std::string& stringValue = args[1];
|
|
|
|
const std::string& variableName = args[2];
|
|
|
|
size_t inStringLength = stringValue.size();
|
|
|
|
size_t startPos = inStringLength + 1;
|
|
|
|
size_t endPos = 0;
|
|
|
|
const char* ptr = stringValue.c_str();
|
|
|
|
size_t cc;
|
2016-05-16 17:34:04 +03:00
|
|
|
for (cc = 0; cc < inStringLength; ++cc) {
|
|
|
|
if (!isspace(*ptr)) {
|
|
|
|
if (startPos > inStringLength) {
|
2007-04-27 05:50:52 +04:00
|
|
|
startPos = cc;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
endPos = cc;
|
2007-04-27 05:50:52 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
++ptr;
|
|
|
|
}
|
2007-04-27 05:50:52 +04:00
|
|
|
|
2007-12-16 15:56:43 +03:00
|
|
|
size_t outLength = 0;
|
|
|
|
|
2012-08-13 21:42:58 +04:00
|
|
|
// if the input string didn't contain any non-space characters, return
|
2007-12-16 15:56:43 +03:00
|
|
|
// an empty string
|
2016-05-16 17:34:04 +03:00
|
|
|
if (startPos > inStringLength) {
|
2007-12-16 15:56:43 +03:00
|
|
|
outLength = 0;
|
|
|
|
startPos = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
|
|
|
outLength = endPos - startPos + 1;
|
|
|
|
}
|
2007-12-16 15:56:43 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
this->Makefile->AddDefinition(
|
|
|
|
variableName, stringValue.substr(startPos, outLength).c_str());
|
2007-04-27 05:50:52 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
bool cmStringCommand::HandleRandomCommand(std::vector<std::string> const& args)
|
2007-04-23 19:04:12 +04:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (args.size() < 2 || args.size() == 3 || args.size() == 5) {
|
2007-04-23 19:04:12 +04:00
|
|
|
this->SetError("sub-command RANDOM requires at least one argument.");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-04-23 19:04:12 +04:00
|
|
|
|
2009-11-06 18:07:10 +03:00
|
|
|
static bool seeded = false;
|
|
|
|
bool force_seed = false;
|
2011-05-23 23:57:41 +04:00
|
|
|
unsigned int seed = 0;
|
2007-04-23 19:04:12 +04:00
|
|
|
int length = 5;
|
|
|
|
const char cmStringCommandDefaultAlphabet[] = "qwertyuiopasdfghjklzxcvbnm"
|
2016-05-16 17:34:04 +03:00
|
|
|
"QWERTYUIOPASDFGHJKLZXCVBNM"
|
|
|
|
"0123456789";
|
2007-04-23 19:04:12 +04:00
|
|
|
std::string alphabet;
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (args.size() > 3) {
|
2007-04-23 19:04:12 +04:00
|
|
|
size_t i = 1;
|
|
|
|
size_t stopAt = args.size() - 2;
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
for (; i < stopAt; ++i) {
|
|
|
|
if (args[i] == "LENGTH") {
|
2007-04-23 19:04:12 +04:00
|
|
|
++i;
|
|
|
|
length = atoi(args[i].c_str());
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (args[i] == "ALPHABET") {
|
2007-04-23 19:04:12 +04:00
|
|
|
++i;
|
|
|
|
alphabet = args[i];
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (args[i] == "RANDOM_SEED") {
|
2009-11-06 18:07:10 +03:00
|
|
|
++i;
|
2011-05-23 23:57:41 +04:00
|
|
|
seed = static_cast<unsigned int>(atoi(args[i].c_str()));
|
2009-11-06 18:07:10 +03:00
|
|
|
force_seed = true;
|
2007-04-23 19:04:12 +04:00
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (alphabet.empty()) {
|
2007-04-23 19:04:12 +04:00
|
|
|
alphabet = cmStringCommandDefaultAlphabet;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-04-23 19:04:12 +04:00
|
|
|
|
2010-06-25 16:48:59 +04:00
|
|
|
double sizeofAlphabet = static_cast<double>(alphabet.size());
|
2016-05-16 17:34:04 +03:00
|
|
|
if (sizeofAlphabet < 1) {
|
2007-04-23 19:04:12 +04:00
|
|
|
this->SetError("sub-command RANDOM invoked with bad alphabet.");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (length < 1) {
|
2007-04-23 19:04:12 +04:00
|
|
|
this->SetError("sub-command RANDOM invoked with bad length.");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
const std::string& variableName = args[args.size() - 1];
|
2007-04-23 19:04:12 +04:00
|
|
|
|
|
|
|
std::vector<char> result;
|
2009-11-06 18:07:10 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!seeded || force_seed) {
|
2009-11-06 18:07:10 +03:00
|
|
|
seeded = true;
|
2016-05-16 17:34:04 +03:00
|
|
|
srand(force_seed ? seed : cmSystemTools::RandomSeed());
|
|
|
|
}
|
2009-11-06 18:07:10 +03:00
|
|
|
|
2007-04-23 19:04:12 +04:00
|
|
|
const char* alphaPtr = alphabet.c_str();
|
|
|
|
int cc;
|
2016-05-16 17:34:04 +03:00
|
|
|
for (cc = 0; cc < length; cc++) {
|
|
|
|
int idx = (int)(sizeofAlphabet * rand() / (RAND_MAX + 1.0));
|
2007-04-23 19:04:12 +04:00
|
|
|
result.push_back(*(alphaPtr + idx));
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-04-23 19:04:12 +04:00
|
|
|
result.push_back(0);
|
|
|
|
|
2014-03-11 03:04:11 +04:00
|
|
|
this->Makefile->AddDefinition(variableName, &*result.begin());
|
2007-04-23 19:04:12 +04:00
|
|
|
return true;
|
|
|
|
}
|
2012-10-05 23:55:07 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
bool cmStringCommand::HandleTimestampCommand(
|
|
|
|
std::vector<std::string> const& args)
|
2012-10-05 23:55:07 +04:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (args.size() < 2) {
|
2012-10-05 23:55:07 +04:00
|
|
|
this->SetError("sub-command TIMESTAMP requires at least one argument.");
|
|
|
|
return false;
|
2016-09-16 23:45:24 +03:00
|
|
|
}
|
|
|
|
if (args.size() > 4) {
|
2012-10-05 23:55:07 +04:00
|
|
|
this->SetError("sub-command TIMESTAMP takes at most three arguments.");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-10-05 23:55:07 +04:00
|
|
|
|
2012-12-05 22:14:56 +04:00
|
|
|
unsigned int argsIndex = 1;
|
2012-10-05 23:55:07 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
const std::string& outputVariable = args[argsIndex++];
|
2012-10-05 23:55:07 +04:00
|
|
|
|
|
|
|
std::string formatString;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (args.size() > argsIndex && args[argsIndex] != "UTC") {
|
2012-10-05 23:55:07 +04:00
|
|
|
formatString = args[argsIndex++];
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-10-05 23:55:07 +04:00
|
|
|
|
|
|
|
bool utcFlag = false;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (args.size() > argsIndex) {
|
|
|
|
if (args[argsIndex] == "UTC") {
|
2012-10-05 23:55:07 +04:00
|
|
|
utcFlag = true;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2012-10-05 23:55:07 +04:00
|
|
|
std::string e = " TIMESTAMP sub-command does not recognize option " +
|
2016-05-16 17:34:04 +03:00
|
|
|
args[argsIndex] + ".";
|
2014-03-11 03:04:11 +04:00
|
|
|
this->SetError(e);
|
2012-10-05 23:55:07 +04:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-10-05 23:55:07 +04:00
|
|
|
|
|
|
|
cmTimestamp timestamp;
|
|
|
|
std::string result = timestamp.CurrentTime(formatString, utcFlag);
|
2014-03-11 03:04:11 +04:00
|
|
|
this->Makefile->AddDefinition(outputVariable, result.c_str());
|
2012-10-05 23:55:07 +04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2014-08-26 00:44:06 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
bool cmStringCommand::HandleUuidCommand(std::vector<std::string> const& args)
|
2014-08-26 00:44:06 +04:00
|
|
|
{
|
|
|
|
#if defined(CMAKE_BUILD_WITH_CMAKE)
|
|
|
|
unsigned int argsIndex = 1;
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (args.size() < 2) {
|
2014-08-26 00:44:06 +04:00
|
|
|
this->SetError("UUID sub-command requires an output variable.");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-08-26 00:44:06 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
const std::string& outputVariable = args[argsIndex++];
|
2014-08-26 00:44:06 +04:00
|
|
|
|
|
|
|
std::string uuidNamespaceString;
|
|
|
|
std::string uuidName;
|
|
|
|
std::string uuidType;
|
|
|
|
bool uuidUpperCase = false;
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
while (args.size() > argsIndex) {
|
|
|
|
if (args[argsIndex] == "NAMESPACE") {
|
2014-08-26 00:44:06 +04:00
|
|
|
++argsIndex;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (argsIndex >= args.size()) {
|
2014-08-26 00:44:06 +04:00
|
|
|
this->SetError("UUID sub-command, NAMESPACE requires a value.");
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
uuidNamespaceString = args[argsIndex++];
|
|
|
|
} else if (args[argsIndex] == "NAME") {
|
2014-08-26 00:44:06 +04:00
|
|
|
++argsIndex;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (argsIndex >= args.size()) {
|
2014-08-26 00:44:06 +04:00
|
|
|
this->SetError("UUID sub-command, NAME requires a value.");
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
uuidName = args[argsIndex++];
|
|
|
|
} else if (args[argsIndex] == "TYPE") {
|
2014-08-26 00:44:06 +04:00
|
|
|
++argsIndex;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (argsIndex >= args.size()) {
|
2014-08-26 00:44:06 +04:00
|
|
|
this->SetError("UUID sub-command, TYPE requires a value.");
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
uuidType = args[argsIndex++];
|
|
|
|
} else if (args[argsIndex] == "UPPER") {
|
2014-08-26 00:44:06 +04:00
|
|
|
++argsIndex;
|
|
|
|
uuidUpperCase = true;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
|
|
|
std::string e =
|
|
|
|
"UUID sub-command does not recognize option " + args[argsIndex] + ".";
|
2014-08-26 00:44:06 +04:00
|
|
|
this->SetError(e);
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-08-26 00:44:06 +04:00
|
|
|
|
|
|
|
std::string uuid;
|
|
|
|
cmUuid uuidGenerator;
|
|
|
|
|
|
|
|
std::vector<unsigned char> uuidNamespace;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!uuidGenerator.StringToBinary(uuidNamespaceString, uuidNamespace)) {
|
2014-08-26 00:44:06 +04:00
|
|
|
this->SetError("UUID sub-command, malformed NAMESPACE UUID.");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-08-26 00:44:06 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (uuidType == "MD5") {
|
2014-08-26 00:44:06 +04:00
|
|
|
uuid = uuidGenerator.FromMd5(uuidNamespace, uuidName);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (uuidType == "SHA1") {
|
2014-08-26 00:44:06 +04:00
|
|
|
uuid = uuidGenerator.FromSha1(uuidNamespace, uuidName);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2014-08-26 00:44:06 +04:00
|
|
|
std::string e = "UUID sub-command, unknown TYPE '" + uuidType + "'.";
|
|
|
|
this->SetError(e);
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-08-26 00:44:06 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (uuid.empty()) {
|
2014-08-26 00:44:06 +04:00
|
|
|
this->SetError("UUID sub-command, generation failed.");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-08-26 00:44:06 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (uuidUpperCase) {
|
2014-08-26 00:44:06 +04:00
|
|
|
uuid = cmSystemTools::UpperCase(uuid);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-08-26 00:44:06 +04:00
|
|
|
|
|
|
|
this->Makefile->AddDefinition(outputVariable, uuid.c_str());
|
|
|
|
return true;
|
|
|
|
#else
|
2015-01-05 22:31:31 +03:00
|
|
|
std::ostringstream e;
|
2014-08-26 00:44:06 +04:00
|
|
|
e << args[0] << " not available during bootstrap";
|
|
|
|
this->SetError(e.str().c_str());
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|