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 "REGEX MATCH will match the regular expression once and store the "
98 "match in the output variable.\n"
99 "REGEX MATCHALL will match the regular expression as many times as "
100 "possible and store the matches in the output variable as a list.\n"
101 "REGEX REPLACE will match the regular expression as many times as "
102 "possible and substitute the replacement expression for the match "
103 "in the output. The replace expression may refer to paren-delimited "
104 "subexpressions of the match using \\1, \\2, ..., \\9. Note that "
105 "two backslashes (\\\\1) are required in CMake code to get a "
106 "backslash through argument parsing.\n"
107 "REPLACE will replace all occurrences of match_string in the input with "
108 "replace_string and store the result in the output.\n"
109 "MD5, SHA1, SHA224, SHA256, SHA384, and SHA512 "
110 "will compute a cryptographic hash of the input string.\n"
111 "COMPARE EQUAL/NOTEQUAL/LESS/GREATER will compare the strings and "
112 "store true or false in the output variable.\n"
113 "ASCII will convert all numbers into corresponding ASCII characters.\n"
114 "CONFIGURE will transform a string like CONFIGURE_FILE transforms "
116 "TOUPPER/TOLOWER will convert string to upper/lower characters.\n"
117 "LENGTH will return a given string's length.\n"
118 "SUBSTRING will return a substring of a given string. If length is "
119 "-1 the remainder of the string starting at begin will be returned.\n"
120 "STRIP will return a substring of a given string with leading "
121 "and trailing spaces removed.\n"
122 "RANDOM will return a random string of given length consisting of "
123 "characters from the given alphabet. Default length is 5 "
124 "characters and default alphabet is all numbers and upper and "
125 "lower case letters. If an integer RANDOM_SEED is given, its "
126 "value will be used to seed the random number generator.\n"
127 "FIND will return the position where the given substring was found "
128 "in the supplied string. If the REVERSE flag was used, the command "
129 "will search for the position of the last occurrence of the "
130 "specified substring.\n"
131 "The following characters have special meaning in regular expressions:\n"
132 " ^ Matches at beginning of input\n"
133 " $ Matches at end of input\n"
134 " . Matches any single character\n"
135 " [ ] Matches any character(s) inside the brackets\n"
136 " [^ ] Matches any character(s) not inside the brackets\n"
137 " - Inside brackets, specifies an inclusive range between\n"
138 " characters on either side e.g. [a-f] is [abcdef]\n"
139 " To match a literal - using brackets, make it the first\n"
140 " or the last character e.g. [+*/-] matches basic\n"
141 " mathematical operators.\n"
142 " * Matches preceding pattern zero or more times\n"
143 " + Matches preceding pattern one or more times\n"
144 " ? Matches preceding pattern zero or once only\n"
145 " | Matches a pattern on either side of the |\n"
146 " () Saves a matched subexpression, which can be referenced \n"
147 " in the REGEX REPLACE operation. Additionally it is saved\n"
148 " by all regular expression-related commands, including \n"
149 " e.g. if( MATCHES ), in the variables CMAKE_MATCH_(0..9).\n"
150 "*, + and ? have higher precedence than concatenation. | has lower "
151 "precedence than concatenation. This means that the regular expression "
152 "\"^ab+d$\" matches \"abbd\" but not \"ababd\", and the regular "
153 "expression \"^(ab|cd)$\" matches \"ab\" but not \"abd\".\n"
154 "TIMESTAMP will write a string representation of "
155 "the current date and/or time to the output variable.\n"
156 "Should the command be unable to obtain a timestamp "
157 "the output variable will be set to the empty string \"\".\n"
158 "The optional UTC flag requests the current date/time "
159 "representation to be in Coordinated Universal Time (UTC) "
160 "rather than local time.\n"
161 "The optional <format string> may contain the following "
162 "format specifiers: \n"
163 " %d The day of the current month (01-31).\n"
164 " %H The hour on a 24-hour clock (00-23).\n"
165 " %I The hour on a 12-hour clock (01-12).\n"
166 " %j The day of the current year (001-366).\n"
167 " %m The month of the current year (01-12).\n"
168 " %M The minute of the current hour (00-59).\n"
169 " %S The second of the current minute.\n"
170 " 60 represents a leap second. (00-60)\n"
171 " %U The week number of the current year (00-53).\n"
172 " %w The day of the current week. 0 is Sunday. (0-6)\n"
173 " %y The last two digits of the current year (00-99)\n"
174 " %Y The current year. \n"
175 "Unknown format specifiers will be ignored "
176 "and copied to the output as-is.\n"
177 "If no explicit <format string> is given it will default to:\n"
178 " %Y-%m-%dT%H:%M:%S for local time.\n"
179 " %Y-%m-%dT%H:%M:%SZ for UTC.";
182 cmTypeMacro(cmStringCommand, cmCommand);
183 static void ClearMatches(cmMakefile* mf);
184 static void StoreMatches(cmMakefile* mf, cmsys::RegularExpression& re);
186 bool HandleConfigureCommand(std::vector<std::string> const& args);
187 bool HandleAsciiCommand(std::vector<std::string> const& args);
188 bool HandleRegexCommand(std::vector<std::string> const& args);
189 bool RegexMatch(std::vector<std::string> const& args);
190 bool RegexMatchAll(std::vector<std::string> const& args);
191 bool RegexReplace(std::vector<std::string> const& args);
192 bool HandleHashCommand(std::vector<std::string> const& args);
193 bool HandleToUpperLowerCommand(std::vector<std::string> const& args,
195 bool HandleCompareCommand(std::vector<std::string> const& args);
196 bool HandleReplaceCommand(std::vector<std::string> const& args);
197 bool HandleLengthCommand(std::vector<std::string> const& args);
198 bool HandleSubstringCommand(std::vector<std::string> const& args);
199 bool HandleStripCommand(std::vector<std::string> const& args);
200 bool HandleRandomCommand(std::vector<std::string> const& args);
201 bool HandleFindCommand(std::vector<std::string> const& args);
202 bool HandleTimestampCommand(std::vector<std::string> const& args);
204 class RegexReplacement
207 RegexReplacement(const char* s): number(-1), value(s) {}
208 RegexReplacement(const std::string& s): number(-1), value(s) {}
209 RegexReplacement(int n): number(n), value() {}
210 RegexReplacement() {};