| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036 |
- /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
- file Copyright.txt or https://cmake.org/licensing for details. */
- #define _SCL_SECURE_NO_WARNINGS
- #include "cmStringCommand.h"
- #include "cmsys/RegularExpression.hxx"
- #include <algorithm>
- #include <ctype.h>
- #include <iterator>
- #include <memory>
- #include <sstream>
- #include <stdio.h>
- #include <stdlib.h>
- #include "cmAlgorithms.h"
- #include "cmCryptoHash.h"
- #include "cmExecutionStatus.h"
- #include "cmGeneratorExpression.h"
- #include "cmMakefile.h"
- #include "cmMessageType.h"
- #include "cmRange.h"
- #include "cmStringAlgorithms.h"
- #include "cmStringReplaceHelper.h"
- #include "cmSystemTools.h"
- #include "cmTimestamp.h"
- #include "cmUuid.h"
- namespace {
- bool HandleConfigureCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status);
- bool HandleAsciiCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status);
- bool HandleRegexCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status);
- bool RegexMatch(std::vector<std::string> const& args,
- cmExecutionStatus& status);
- bool RegexMatchAll(std::vector<std::string> const& args,
- cmExecutionStatus& status);
- bool RegexReplace(std::vector<std::string> const& args,
- cmExecutionStatus& status);
- bool HandleHashCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status);
- bool HandleToUpperLowerCommand(std::vector<std::string> const& args,
- bool toUpper, cmExecutionStatus& status);
- bool HandleCompareCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status);
- bool HandleReplaceCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status);
- bool HandleLengthCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status);
- bool HandleSubstringCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status);
- bool HandleAppendCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status);
- bool HandlePrependCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status);
- bool HandleConcatCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status);
- bool HandleJoinCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status);
- bool HandleStripCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status);
- bool HandleRepeatCommand(std::vector<std::string> const& args,
- cmMakefile& makefile);
- bool HandleRandomCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status);
- bool HandleFindCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status);
- bool HandleTimestampCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status);
- bool HandleMakeCIdentifierCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status);
- bool HandleGenexStripCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status);
- bool HandleUuidCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status);
- bool joinImpl(std::vector<std::string> const& args, std::string const& glue,
- size_t varIdx, cmMakefile& makefile);
- }
- bool cmStringCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status)
- {
- if (args.empty()) {
- status.SetError("must be called with at least one argument.");
- return false;
- }
- const std::string& subCommand = args[0];
- if (subCommand == "REGEX") {
- return HandleRegexCommand(args, status);
- }
- if (subCommand == "REPLACE") {
- return HandleReplaceCommand(args, status);
- }
- if (subCommand == "MD5" || subCommand == "SHA1" || subCommand == "SHA224" ||
- subCommand == "SHA256" || subCommand == "SHA384" ||
- subCommand == "SHA512" || subCommand == "SHA3_224" ||
- subCommand == "SHA3_256" || subCommand == "SHA3_384" ||
- subCommand == "SHA3_512") {
- return HandleHashCommand(args, status);
- }
- if (subCommand == "TOLOWER") {
- return HandleToUpperLowerCommand(args, false, status);
- }
- if (subCommand == "TOUPPER") {
- return HandleToUpperLowerCommand(args, true, status);
- }
- if (subCommand == "COMPARE") {
- return HandleCompareCommand(args, status);
- }
- if (subCommand == "ASCII") {
- return HandleAsciiCommand(args, status);
- }
- if (subCommand == "CONFIGURE") {
- return HandleConfigureCommand(args, status);
- }
- if (subCommand == "LENGTH") {
- return HandleLengthCommand(args, status);
- }
- if (subCommand == "APPEND") {
- return HandleAppendCommand(args, status);
- }
- if (subCommand == "PREPEND") {
- return HandlePrependCommand(args, status);
- }
- if (subCommand == "CONCAT") {
- return HandleConcatCommand(args, status);
- }
- if (subCommand == "JOIN") {
- return HandleJoinCommand(args, status);
- }
- if (subCommand == "SUBSTRING") {
- return HandleSubstringCommand(args, status);
- }
- if (subCommand == "STRIP") {
- return HandleStripCommand(args, status);
- }
- if (subCommand == "REPEAT") {
- return HandleRepeatCommand(args, status.GetMakefile());
- }
- if (subCommand == "RANDOM") {
- return HandleRandomCommand(args, status);
- }
- if (subCommand == "FIND") {
- return HandleFindCommand(args, status);
- }
- if (subCommand == "TIMESTAMP") {
- return HandleTimestampCommand(args, status);
- }
- if (subCommand == "MAKE_C_IDENTIFIER") {
- return HandleMakeCIdentifierCommand(args, status);
- }
- if (subCommand == "GENEX_STRIP") {
- return HandleGenexStripCommand(args, status);
- }
- if (subCommand == "UUID") {
- return HandleUuidCommand(args, status);
- }
- std::string e = "does not recognize sub-command " + subCommand;
- status.SetError(e);
- return false;
- }
- namespace {
- bool HandleHashCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status)
- {
- #if !defined(CMAKE_BOOTSTRAP)
- if (args.size() != 3) {
- std::ostringstream e;
- e << args[0] << " requires an output variable and an input string";
- status.SetError(e.str());
- return false;
- }
- std::unique_ptr<cmCryptoHash> hash(cmCryptoHash::New(args[0]));
- if (hash) {
- std::string out = hash->HashString(args[2]);
- status.GetMakefile().AddDefinition(args[1], out);
- return true;
- }
- return false;
- #else
- std::ostringstream e;
- e << args[0] << " not available during bootstrap";
- status.SetError(e.str().c_str());
- return false;
- #endif
- }
- bool HandleToUpperLowerCommand(std::vector<std::string> const& args,
- bool toUpper, cmExecutionStatus& status)
- {
- if (args.size() < 3) {
- status.SetError("no output variable specified");
- return false;
- }
- std::string const& outvar = args[2];
- std::string output;
- if (toUpper) {
- output = cmSystemTools::UpperCase(args[1]);
- } else {
- output = cmSystemTools::LowerCase(args[1]);
- }
- // Store the output in the provided variable.
- status.GetMakefile().AddDefinition(outvar, output);
- return true;
- }
- bool HandleAsciiCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status)
- {
- if (args.size() < 3) {
- status.SetError("No output variable specified");
- return false;
- }
- std::string::size_type cc;
- std::string const& outvar = args.back();
- std::string output;
- for (cc = 1; cc < args.size() - 1; cc++) {
- int ch = atoi(args[cc].c_str());
- if (ch > 0 && ch < 256) {
- output += static_cast<char>(ch);
- } else {
- std::string error = "Character with code ";
- error += args[cc];
- error += " does not exist.";
- status.SetError(error);
- return false;
- }
- }
- // Store the output in the provided variable.
- status.GetMakefile().AddDefinition(outvar, output);
- return true;
- }
- bool HandleConfigureCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status)
- {
- if (args.size() < 2) {
- status.SetError("No input string specified.");
- return false;
- }
- if (args.size() < 3) {
- status.SetError("No output variable specified.");
- return false;
- }
- // Parse options.
- bool escapeQuotes = false;
- bool atOnly = false;
- for (unsigned int i = 3; i < args.size(); ++i) {
- if (args[i] == "@ONLY") {
- atOnly = true;
- } else if (args[i] == "ESCAPE_QUOTES") {
- escapeQuotes = true;
- } else {
- std::ostringstream err;
- err << "Unrecognized argument \"" << args[i] << "\"";
- status.SetError(err.str());
- return false;
- }
- }
- // Configure the string.
- std::string output;
- status.GetMakefile().ConfigureString(args[1], output, atOnly, escapeQuotes);
- // Store the output in the provided variable.
- status.GetMakefile().AddDefinition(args[2], output);
- return true;
- }
- bool HandleRegexCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status)
- {
- if (args.size() < 2) {
- status.SetError("sub-command REGEX requires a mode to be specified.");
- return false;
- }
- std::string const& mode = args[1];
- if (mode == "MATCH") {
- if (args.size() < 5) {
- status.SetError("sub-command REGEX, mode MATCH needs "
- "at least 5 arguments total to command.");
- return false;
- }
- return RegexMatch(args, status);
- }
- if (mode == "MATCHALL") {
- if (args.size() < 5) {
- status.SetError("sub-command REGEX, mode MATCHALL needs "
- "at least 5 arguments total to command.");
- return false;
- }
- return RegexMatchAll(args, status);
- }
- if (mode == "REPLACE") {
- if (args.size() < 6) {
- status.SetError("sub-command REGEX, mode REPLACE needs "
- "at least 6 arguments total to command.");
- return false;
- }
- return RegexReplace(args, status);
- }
- std::string e = "sub-command REGEX does not recognize mode " + mode;
- status.SetError(e);
- return false;
- }
- bool RegexMatch(std::vector<std::string> const& args,
- cmExecutionStatus& status)
- {
- //"STRING(REGEX MATCH <regular_expression> <output variable>
- // <input> [<input>...])\n";
- std::string const& regex = args[2];
- std::string const& outvar = args[3];
- status.GetMakefile().ClearMatches();
- // Compile the regular expression.
- cmsys::RegularExpression re;
- if (!re.compile(regex.c_str())) {
- std::string e =
- "sub-command REGEX, mode MATCH failed to compile regex \"" + regex +
- "\".";
- status.SetError(e);
- return false;
- }
- // Concatenate all the last arguments together.
- std::string input = cmJoin(cmMakeRange(args).advance(4), std::string());
- // Scan through the input for all matches.
- std::string output;
- if (re.find(input)) {
- status.GetMakefile().StoreMatches(re);
- std::string::size_type l = re.start();
- std::string::size_type r = re.end();
- if (r - l == 0) {
- std::string e = "sub-command REGEX, mode MATCH regex \"" + regex +
- "\" matched an empty string.";
- status.SetError(e);
- return false;
- }
- output = input.substr(l, r - l);
- }
- // Store the output in the provided variable.
- status.GetMakefile().AddDefinition(outvar, output);
- return true;
- }
- bool RegexMatchAll(std::vector<std::string> const& args,
- cmExecutionStatus& status)
- {
- //"STRING(REGEX MATCHALL <regular_expression> <output variable> <input>
- // [<input>...])\n";
- std::string const& regex = args[2];
- std::string const& outvar = args[3];
- status.GetMakefile().ClearMatches();
- // Compile the regular expression.
- cmsys::RegularExpression re;
- if (!re.compile(regex.c_str())) {
- std::string e =
- "sub-command REGEX, mode MATCHALL failed to compile regex \"" + regex +
- "\".";
- status.SetError(e);
- return false;
- }
- // Concatenate all the last arguments together.
- std::string input = cmJoin(cmMakeRange(args).advance(4), std::string());
- // Scan through the input for all matches.
- std::string output;
- const char* p = input.c_str();
- while (re.find(p)) {
- status.GetMakefile().ClearMatches();
- status.GetMakefile().StoreMatches(re);
- std::string::size_type l = re.start();
- std::string::size_type r = re.end();
- if (r - l == 0) {
- std::string e = "sub-command REGEX, mode MATCHALL regex \"" + regex +
- "\" matched an empty string.";
- status.SetError(e);
- return false;
- }
- if (!output.empty()) {
- output += ";";
- }
- output += std::string(p + l, r - l);
- p += r;
- }
- // Store the output in the provided variable.
- status.GetMakefile().AddDefinition(outvar, output);
- return true;
- }
- bool RegexReplace(std::vector<std::string> const& args,
- cmExecutionStatus& status)
- {
- //"STRING(REGEX REPLACE <regular_expression> <replace_expression>
- // <output variable> <input> [<input>...])\n"
- std::string const& regex = args[2];
- std::string const& replace = args[3];
- std::string const& outvar = args[4];
- cmStringReplaceHelper replaceHelper(regex, replace, &status.GetMakefile());
- if (!replaceHelper.IsReplaceExpressionValid()) {
- status.SetError(
- "sub-command REGEX, mode REPLACE: " + replaceHelper.GetError() + ".");
- return false;
- }
- status.GetMakefile().ClearMatches();
- if (!replaceHelper.IsRegularExpressionValid()) {
- std::string e =
- "sub-command REGEX, mode REPLACE failed to compile regex \"" + regex +
- "\".";
- status.SetError(e);
- return false;
- }
- // Concatenate all the last arguments together.
- const std::string input =
- cmJoin(cmMakeRange(args).advance(5), std::string());
- std::string output;
- if (!replaceHelper.Replace(input, output)) {
- status.SetError(
- "sub-command REGEX, mode REPLACE: " + replaceHelper.GetError() + ".");
- return false;
- }
- // Store the output in the provided variable.
- status.GetMakefile().AddDefinition(outvar, output);
- return true;
- }
- bool HandleFindCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status)
- {
- // check if all required parameters were passed
- if (args.size() < 4 || args.size() > 5) {
- status.SetError("sub-command FIND requires 3 or 4 parameters.");
- return false;
- }
- // check if the reverse flag was set or not
- bool reverseMode = false;
- if (args.size() == 5 && args[4] == "REVERSE") {
- reverseMode = true;
- }
- // if we have 5 arguments the last one must be REVERSE
- if (args.size() == 5 && args[4] != "REVERSE") {
- status.SetError("sub-command FIND: unknown last parameter");
- return false;
- }
- // 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
- if (outvar == "REVERSE") {
- status.SetError("sub-command FIND does not allow one to select REVERSE as "
- "the output variable. "
- "Maybe you missed the actual output variable?");
- return false;
- }
- // try to find the character and return its position
- size_t pos;
- if (!reverseMode) {
- pos = sstring.find(schar);
- } else {
- pos = sstring.rfind(schar);
- }
- if (std::string::npos != pos) {
- std::ostringstream s;
- s << pos;
- status.GetMakefile().AddDefinition(outvar, s.str());
- return true;
- }
- // the character was not found, but this is not really an error
- status.GetMakefile().AddDefinition(outvar, "-1");
- return true;
- }
- bool HandleCompareCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status)
- {
- if (args.size() < 2) {
- status.SetError("sub-command COMPARE requires a mode to be specified.");
- return false;
- }
- std::string const& mode = args[1];
- if ((mode == "EQUAL") || (mode == "NOTEQUAL") || (mode == "LESS") ||
- (mode == "LESS_EQUAL") || (mode == "GREATER") ||
- (mode == "GREATER_EQUAL")) {
- if (args.size() < 5) {
- std::string e = "sub-command COMPARE, mode ";
- e += mode;
- e += " needs at least 5 arguments total to command.";
- status.SetError(e);
- return false;
- }
- const std::string& left = args[2];
- const std::string& right = args[3];
- const std::string& outvar = args[4];
- bool result;
- if (mode == "LESS") {
- result = (left < right);
- } else if (mode == "LESS_EQUAL") {
- result = (left <= right);
- } else if (mode == "GREATER") {
- result = (left > right);
- } else if (mode == "GREATER_EQUAL") {
- result = (left >= right);
- } else if (mode == "EQUAL") {
- result = (left == right);
- } else // if(mode == "NOTEQUAL")
- {
- result = !(left == right);
- }
- if (result) {
- status.GetMakefile().AddDefinition(outvar, "1");
- } else {
- status.GetMakefile().AddDefinition(outvar, "0");
- }
- return true;
- }
- std::string e = "sub-command COMPARE does not recognize mode " + mode;
- status.SetError(e);
- return false;
- }
- bool HandleReplaceCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status)
- {
- if (args.size() < 5) {
- status.SetError("sub-command REPLACE requires at least four arguments.");
- return false;
- }
- const std::string& matchExpression = args[1];
- const std::string& replaceExpression = args[2];
- const std::string& variableName = args[3];
- std::string input = cmJoin(cmMakeRange(args).advance(4), std::string());
- cmsys::SystemTools::ReplaceString(input, matchExpression.c_str(),
- replaceExpression.c_str());
- status.GetMakefile().AddDefinition(variableName, input);
- return true;
- }
- bool HandleSubstringCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status)
- {
- if (args.size() != 5) {
- status.SetError("sub-command SUBSTRING requires four arguments.");
- return false;
- }
- 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);
- if (begin < 0 || begin > intStringLength) {
- std::ostringstream ostr;
- ostr << "begin index: " << begin << " is out of range 0 - "
- << stringLength;
- status.SetError(ostr.str());
- return false;
- }
- if (end < -1) {
- std::ostringstream ostr;
- ostr << "end index: " << end << " should be -1 or greater";
- status.SetError(ostr.str());
- return false;
- }
- status.GetMakefile().AddDefinition(variableName,
- stringValue.substr(begin, end));
- return true;
- }
- bool HandleLengthCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status)
- {
- if (args.size() != 3) {
- status.SetError("sub-command LENGTH requires two arguments.");
- return false;
- }
- const std::string& stringValue = args[1];
- const std::string& variableName = args[2];
- size_t length = stringValue.size();
- char buffer[1024];
- sprintf(buffer, "%d", static_cast<int>(length));
- status.GetMakefile().AddDefinition(variableName, buffer);
- return true;
- }
- bool HandleAppendCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status)
- {
- if (args.size() < 2) {
- status.SetError("sub-command APPEND requires at least one argument.");
- return false;
- }
- // Skip if nothing to append.
- if (args.size() < 3) {
- return true;
- }
- const std::string& variable = args[1];
- std::string value;
- const char* oldValue = status.GetMakefile().GetDefinition(variable);
- if (oldValue) {
- value = oldValue;
- }
- value += cmJoin(cmMakeRange(args).advance(2), std::string());
- status.GetMakefile().AddDefinition(variable, value);
- return true;
- }
- bool HandlePrependCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status)
- {
- if (args.size() < 2) {
- status.SetError("sub-command PREPEND requires at least one argument.");
- return false;
- }
- // Skip if nothing to prepend.
- if (args.size() < 3) {
- return true;
- }
- const std::string& variable = args[1];
- std::string value = cmJoin(cmMakeRange(args).advance(2), std::string());
- const char* oldValue = status.GetMakefile().GetDefinition(variable);
- if (oldValue) {
- value += oldValue;
- }
- status.GetMakefile().AddDefinition(variable, value);
- return true;
- }
- bool HandleConcatCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status)
- {
- if (args.size() < 2) {
- status.SetError("sub-command CONCAT requires at least one argument.");
- return false;
- }
- return joinImpl(args, std::string(), 1, status.GetMakefile());
- }
- bool HandleJoinCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status)
- {
- if (args.size() < 3) {
- status.SetError("sub-command JOIN requires at least two arguments.");
- return false;
- }
- return joinImpl(args, args[1], 2, status.GetMakefile());
- }
- bool joinImpl(std::vector<std::string> const& args, std::string const& glue,
- const size_t varIdx, cmMakefile& makefile)
- {
- std::string const& variableName = args[varIdx];
- // NOTE Items to concat/join placed right after the variable for
- // both `CONCAT` and `JOIN` sub-commands.
- std::string value = cmJoin(cmMakeRange(args).advance(varIdx + 1), glue);
- makefile.AddDefinition(variableName, value);
- return true;
- }
- bool HandleMakeCIdentifierCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status)
- {
- if (args.size() != 3) {
- status.SetError("sub-command MAKE_C_IDENTIFIER requires two arguments.");
- return false;
- }
- const std::string& input = args[1];
- const std::string& variableName = args[2];
- status.GetMakefile().AddDefinition(variableName,
- cmSystemTools::MakeCidentifier(input));
- return true;
- }
- bool HandleGenexStripCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status)
- {
- if (args.size() != 3) {
- status.SetError("sub-command GENEX_STRIP requires two arguments.");
- return false;
- }
- const std::string& input = args[1];
- std::string result = cmGeneratorExpression::Preprocess(
- input, cmGeneratorExpression::StripAllGeneratorExpressions);
- const std::string& variableName = args[2];
- status.GetMakefile().AddDefinition(variableName, result);
- return true;
- }
- bool HandleStripCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status)
- {
- if (args.size() != 3) {
- status.SetError("sub-command STRIP requires two arguments.");
- return false;
- }
- 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;
- for (cc = 0; cc < inStringLength; ++cc) {
- if (!isspace(*ptr)) {
- if (startPos > inStringLength) {
- startPos = cc;
- }
- endPos = cc;
- }
- ++ptr;
- }
- size_t outLength = 0;
- // if the input string didn't contain any non-space characters, return
- // an empty string
- if (startPos > inStringLength) {
- outLength = 0;
- startPos = 0;
- } else {
- outLength = endPos - startPos + 1;
- }
- status.GetMakefile().AddDefinition(variableName,
- stringValue.substr(startPos, outLength));
- return true;
- }
- bool HandleRepeatCommand(std::vector<std::string> const& args,
- cmMakefile& makefile)
- {
- // `string(REPEAT "<str>" <times> OUTPUT_VARIABLE)`
- enum ArgPos : std::size_t
- {
- SUB_COMMAND,
- VALUE,
- TIMES,
- OUTPUT_VARIABLE,
- TOTAL_ARGS
- };
- if (args.size() != ArgPos::TOTAL_ARGS) {
- makefile.IssueMessage(MessageType::FATAL_ERROR,
- "sub-command REPEAT requires three arguments.");
- return true;
- }
- unsigned long times;
- if (!cmStrToULong(args[ArgPos::TIMES], ×)) {
- makefile.IssueMessage(MessageType::FATAL_ERROR,
- "repeat count is not a positive number.");
- return true;
- }
- const auto& stringValue = args[ArgPos::VALUE];
- const auto& variableName = args[ArgPos::OUTPUT_VARIABLE];
- const auto inStringLength = stringValue.size();
- std::string result;
- switch (inStringLength) {
- case 0u:
- // Nothing to do for zero length input strings
- break;
- case 1u:
- // NOTE If the string to repeat consists of the only character,
- // use the appropriate constructor.
- result = std::string(times, stringValue[0]);
- break;
- default:
- result = std::string(inStringLength * times, char{});
- for (auto i = 0u; i < times; ++i) {
- std::copy(cm::cbegin(stringValue), cm::cend(stringValue),
- &result[i * inStringLength]);
- }
- break;
- }
- makefile.AddDefinition(variableName, result);
- return true;
- }
- bool HandleRandomCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status)
- {
- if (args.size() < 2 || args.size() == 3 || args.size() == 5) {
- status.SetError("sub-command RANDOM requires at least one argument.");
- return false;
- }
- static bool seeded = false;
- bool force_seed = false;
- unsigned int seed = 0;
- int length = 5;
- const char cmStringCommandDefaultAlphabet[] = "qwertyuiopasdfghjklzxcvbnm"
- "QWERTYUIOPASDFGHJKLZXCVBNM"
- "0123456789";
- std::string alphabet;
- if (args.size() > 3) {
- size_t i = 1;
- size_t stopAt = args.size() - 2;
- for (; i < stopAt; ++i) {
- if (args[i] == "LENGTH") {
- ++i;
- length = atoi(args[i].c_str());
- } else if (args[i] == "ALPHABET") {
- ++i;
- alphabet = args[i];
- } else if (args[i] == "RANDOM_SEED") {
- ++i;
- seed = static_cast<unsigned int>(atoi(args[i].c_str()));
- force_seed = true;
- }
- }
- }
- if (alphabet.empty()) {
- alphabet = cmStringCommandDefaultAlphabet;
- }
- double sizeofAlphabet = static_cast<double>(alphabet.size());
- if (sizeofAlphabet < 1) {
- status.SetError("sub-command RANDOM invoked with bad alphabet.");
- return false;
- }
- if (length < 1) {
- status.SetError("sub-command RANDOM invoked with bad length.");
- return false;
- }
- const std::string& variableName = args.back();
- std::vector<char> result;
- if (!seeded || force_seed) {
- seeded = true;
- srand(force_seed ? seed : cmSystemTools::RandomSeed());
- }
- const char* alphaPtr = alphabet.c_str();
- for (int cc = 0; cc < length; cc++) {
- int idx = static_cast<int>(sizeofAlphabet * rand() / (RAND_MAX + 1.0));
- result.push_back(*(alphaPtr + idx));
- }
- result.push_back(0);
- status.GetMakefile().AddDefinition(variableName, result.data());
- return true;
- }
- bool HandleTimestampCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status)
- {
- if (args.size() < 2) {
- status.SetError("sub-command TIMESTAMP requires at least one argument.");
- return false;
- }
- if (args.size() > 4) {
- status.SetError("sub-command TIMESTAMP takes at most three arguments.");
- return false;
- }
- unsigned int argsIndex = 1;
- const std::string& outputVariable = args[argsIndex++];
- std::string formatString;
- if (args.size() > argsIndex && args[argsIndex] != "UTC") {
- formatString = args[argsIndex++];
- }
- bool utcFlag = false;
- if (args.size() > argsIndex) {
- if (args[argsIndex] == "UTC") {
- utcFlag = true;
- } else {
- std::string e = " TIMESTAMP sub-command does not recognize option " +
- args[argsIndex] + ".";
- status.SetError(e);
- return false;
- }
- }
- cmTimestamp timestamp;
- std::string result = timestamp.CurrentTime(formatString, utcFlag);
- status.GetMakefile().AddDefinition(outputVariable, result);
- return true;
- }
- bool HandleUuidCommand(std::vector<std::string> const& args,
- cmExecutionStatus& status)
- {
- #if !defined(CMAKE_BOOTSTRAP)
- unsigned int argsIndex = 1;
- if (args.size() < 2) {
- status.SetError("UUID sub-command requires an output variable.");
- return false;
- }
- const std::string& outputVariable = args[argsIndex++];
- std::string uuidNamespaceString;
- std::string uuidName;
- std::string uuidType;
- bool uuidUpperCase = false;
- while (args.size() > argsIndex) {
- if (args[argsIndex] == "NAMESPACE") {
- ++argsIndex;
- if (argsIndex >= args.size()) {
- status.SetError("UUID sub-command, NAMESPACE requires a value.");
- return false;
- }
- uuidNamespaceString = args[argsIndex++];
- } else if (args[argsIndex] == "NAME") {
- ++argsIndex;
- if (argsIndex >= args.size()) {
- status.SetError("UUID sub-command, NAME requires a value.");
- return false;
- }
- uuidName = args[argsIndex++];
- } else if (args[argsIndex] == "TYPE") {
- ++argsIndex;
- if (argsIndex >= args.size()) {
- status.SetError("UUID sub-command, TYPE requires a value.");
- return false;
- }
- uuidType = args[argsIndex++];
- } else if (args[argsIndex] == "UPPER") {
- ++argsIndex;
- uuidUpperCase = true;
- } else {
- std::string e =
- "UUID sub-command does not recognize option " + args[argsIndex] + ".";
- status.SetError(e);
- return false;
- }
- }
- std::string uuid;
- cmUuid uuidGenerator;
- std::vector<unsigned char> uuidNamespace;
- if (!uuidGenerator.StringToBinary(uuidNamespaceString, uuidNamespace)) {
- status.SetError("UUID sub-command, malformed NAMESPACE UUID.");
- return false;
- }
- if (uuidType == "MD5") {
- uuid = uuidGenerator.FromMd5(uuidNamespace, uuidName);
- } else if (uuidType == "SHA1") {
- uuid = uuidGenerator.FromSha1(uuidNamespace, uuidName);
- } else {
- std::string e = "UUID sub-command, unknown TYPE '" + uuidType + "'.";
- status.SetError(e);
- return false;
- }
- if (uuid.empty()) {
- status.SetError("UUID sub-command, generation failed.");
- return false;
- }
- if (uuidUpperCase) {
- uuid = cmSystemTools::UpperCase(uuid);
- }
- status.GetMakefile().AddDefinition(outputVariable, uuid);
- return true;
- #else
- std::ostringstream e;
- e << args[0] << " not available during bootstrap";
- status.SetError(e.str().c_str());
- return false;
- #endif
- }
- }
|