1 /*============================================================================
2 CMake - Cross Platform Makefile Generator
3 Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
5 Distributed under the OSI-approved BSD License (the "License");
6 see accompanying file Copyright.txt for details.
8 This software is distributed WITHOUT ANY WARRANTY; without even the
9 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License for more information.
11 ============================================================================*/
12 #ifndef cmStringCommand_h
13 #define cmStringCommand_h
15 #include "cmCommand.h"
20 class RegularExpression;
23 /** \class cmStringCommand
24 * \brief Common string operations
27 class cmStringCommand : public cmCommand
31 * This is a virtual constructor for the command.
33 virtual cmCommand* Clone()
35 return new cmStringCommand;
39 * This is called when the command is first encountered in
40 * the CMakeLists.txt file.
42 virtual bool InitialPass(std::vector<std::string> const& args,
43 cmExecutionStatus &status);
46 * This determines if the command is invoked when in script mode.
48 virtual bool IsScriptable() const { return true; }
51 * The name of the command as specified in CMakeList.txt.
53 virtual const char* GetName() const { return "string";}
56 * Succinct documentation.
58 virtual const char* GetTerseDocumentation() const
60 return "String operations.";
66 virtual const char* GetFullDocumentation() const
69 " string(REGEX MATCH <regular_expression>\n"
70 " <output variable> <input> [<input>...])\n"
71 " string(REGEX MATCHALL <regular_expression>\n"
72 " <output variable> <input> [<input>...])\n"
73 " string(REGEX REPLACE <regular_expression>\n"
74 " <replace_expression> <output variable>\n"
75 " <input> [<input>...])\n"
76 " string(REPLACE <match_string>\n"
77 " <replace_string> <output variable>\n"
78 " <input> [<input>...])\n"
79 " string(<MD5|SHA1|SHA224|SHA256|SHA384|SHA512>\n"
80 " <output variable> <input>)\n"
81 " string(COMPARE EQUAL <string1> <string2> <output variable>)\n"
82 " string(COMPARE NOTEQUAL <string1> <string2> <output variable>)\n"
83 " string(COMPARE LESS <string1> <string2> <output variable>)\n"
84 " string(COMPARE GREATER <string1> <string2> <output variable>)\n"
85 " string(ASCII <number> [<number> ...] <output variable>)\n"
86 " string(CONFIGURE <string1> <output variable>\n"
87 " [@ONLY] [ESCAPE_QUOTES])\n"
88 " string(TOUPPER <string1> <output variable>)\n"
89 " string(TOLOWER <string1> <output variable>)\n"
90 " string(LENGTH <string> <output variable>)\n"
91 " string(SUBSTRING <string> <begin> <length> <output variable>)\n"
92 " string(STRIP <string> <output variable>)\n"
93 " string(RANDOM [LENGTH <length>] [ALPHABET <alphabet>]\n"
94 " [RANDOM_SEED <seed>] <output variable>)\n"
95 " string(FIND <string> <substring> <output variable> [REVERSE])\n"
96 " string(TIMESTAMP <output variable> [<format string>] [UTC])\n"
97 " string(MAKE_C_IDENTIFIER <input string> <output variable>)\n"
98 "REGEX MATCH will match the regular expression once and store the "
99 "match in the output variable.\n"
100 "REGEX MATCHALL will match the regular expression as many times as "
101 "possible and store the matches in the output variable as a list.\n"
102 "REGEX REPLACE will match the regular expression as many times as "
103 "possible and substitute the replacement expression for the match "
104 "in the output. The replace expression may refer to paren-delimited "
105 "subexpressions of the match using \\1, \\2, ..., \\9. Note that "
106 "two backslashes (\\\\1) are required in CMake code to get a "
107 "backslash through argument parsing.\n"
108 "REPLACE will replace all occurrences of match_string in the input with "
109 "replace_string and store the result in the output.\n"
110 "MD5, SHA1, SHA224, SHA256, SHA384, and SHA512 "
111 "will compute a cryptographic hash of the input string.\n"
112 "COMPARE EQUAL/NOTEQUAL/LESS/GREATER will compare the strings and "
113 "store true or false in the output variable.\n"
114 "ASCII will convert all numbers into corresponding ASCII characters.\n"
115 "CONFIGURE will transform a string like CONFIGURE_FILE transforms "
117 "TOUPPER/TOLOWER will convert string to upper/lower characters.\n"
118 "LENGTH will return a given string's length.\n"
119 "SUBSTRING will return a substring of a given string. If length is "
120 "-1 the remainder of the string starting at begin will be returned.\n"
121 "STRIP will return a substring of a given string with leading "
122 "and trailing spaces removed.\n"
123 "RANDOM will return a random string of given length consisting of "
124 "characters from the given alphabet. Default length is 5 "
125 "characters and default alphabet is all numbers and upper and "
126 "lower case letters. If an integer RANDOM_SEED is given, its "
127 "value will be used to seed the random number generator.\n"
128 "FIND will return the position where the given substring was found "
129 "in the supplied string. If the REVERSE flag was used, the command "
130 "will search for the position of the last occurrence of the "
131 "specified substring.\n"
132 "The following characters have special meaning in regular expressions:\n"
133 " ^ Matches at beginning of input\n"
134 " $ Matches at end of input\n"
135 " . Matches any single character\n"
136 " [ ] Matches any character(s) inside the brackets\n"
137 " [^ ] Matches any character(s) not inside the brackets\n"
138 " - Inside brackets, specifies an inclusive range between\n"
139 " characters on either side e.g. [a-f] is [abcdef]\n"
140 " To match a literal - using brackets, make it the first\n"
141 " or the last character e.g. [+*/-] matches basic\n"
142 " mathematical operators.\n"
143 " * Matches preceding pattern zero or more times\n"
144 " + Matches preceding pattern one or more times\n"
145 " ? Matches preceding pattern zero or once only\n"
146 " | Matches a pattern on either side of the |\n"
147 " () Saves a matched subexpression, which can be referenced \n"
148 " in the REGEX REPLACE operation. Additionally it is saved\n"
149 " by all regular expression-related commands, including \n"
150 " e.g. if( MATCHES ), in the variables CMAKE_MATCH_(0..9).\n"
151 "*, + and ? have higher precedence than concatenation. | has lower "
152 "precedence than concatenation. This means that the regular expression "
153 "\"^ab+d$\" matches \"abbd\" but not \"ababd\", and the regular "
154 "expression \"^(ab|cd)$\" matches \"ab\" but not \"abd\".\n"
155 "TIMESTAMP will write a string representation of "
156 "the current date and/or time to the output variable.\n"
157 "Should the command be unable to obtain a timestamp "
158 "the output variable will be set to the empty string \"\".\n"
159 "The optional UTC flag requests the current date/time "
160 "representation to be in Coordinated Universal Time (UTC) "
161 "rather than local time.\n"
162 "The optional <format string> may contain the following "
163 "format specifiers: \n"
164 " %d The day of the current month (01-31).\n"
165 " %H The hour on a 24-hour clock (00-23).\n"
166 " %I The hour on a 12-hour clock (01-12).\n"
167 " %j The day of the current year (001-366).\n"
168 " %m The month of the current year (01-12).\n"
169 " %M The minute of the current hour (00-59).\n"
170 " %S The second of the current minute.\n"
171 " 60 represents a leap second. (00-60)\n"
172 " %U The week number of the current year (00-53).\n"
173 " %w The day of the current week. 0 is Sunday. (0-6)\n"
174 " %y The last two digits of the current year (00-99)\n"
175 " %Y The current year. \n"
176 "Unknown format specifiers will be ignored "
177 "and copied to the output as-is.\n"
178 "If no explicit <format string> is given it will default to:\n"
179 " %Y-%m-%dT%H:%M:%S for local time.\n"
180 " %Y-%m-%dT%H:%M:%SZ for UTC.\n"
181 "MAKE_C_IDENTIFIER will write a string which can be used as an "
185 cmTypeMacro(cmStringCommand, cmCommand);
186 static void ClearMatches(cmMakefile* mf);
187 static void StoreMatches(cmMakefile* mf, cmsys::RegularExpression& re);
189 bool HandleConfigureCommand(std::vector<std::string> const& args);
190 bool HandleAsciiCommand(std::vector<std::string> const& args);
191 bool HandleRegexCommand(std::vector<std::string> const& args);
192 bool RegexMatch(std::vector<std::string> const& args);
193 bool RegexMatchAll(std::vector<std::string> const& args);
194 bool RegexReplace(std::vector<std::string> const& args);
195 bool HandleHashCommand(std::vector<std::string> const& args);
196 bool HandleToUpperLowerCommand(std::vector<std::string> const& args,
198 bool HandleCompareCommand(std::vector<std::string> const& args);
199 bool HandleReplaceCommand(std::vector<std::string> const& args);
200 bool HandleLengthCommand(std::vector<std::string> const& args);
201 bool HandleSubstringCommand(std::vector<std::string> const& args);
202 bool HandleStripCommand(std::vector<std::string> const& args);
203 bool HandleRandomCommand(std::vector<std::string> const& args);
204 bool HandleFindCommand(std::vector<std::string> const& args);
205 bool HandleTimestampCommand(std::vector<std::string> const& args);
206 bool HandleMakeCIdentifierCommand(std::vector<std::string> const& args);
208 class RegexReplacement
211 RegexReplacement(const char* s): number(-1), value(s) {}
212 RegexReplacement(const std::string& s): number(-1), value(s) {}
213 RegexReplacement(int n): number(n), value() {}
214 RegexReplacement() {};