| 2147 | } |
| 2148 | |
| 2149 | bool cmSystemTools::EnvDiff::ParseOperation(std::string const& envmod) |
| 2150 | { |
| 2151 | char path_sep = GetSystemPathlistSeparator(); |
| 2152 | |
| 2153 | auto apply_diff = [this](std::string const& name, |
| 2154 | std::function<void(std::string&)> const& apply) { |
| 2155 | cm::optional<std::string> old_value = diff[name]; |
| 2156 | std::string output; |
| 2157 | if (old_value) { |
| 2158 | output = *old_value; |
| 2159 | } else { |
| 2160 | char const* curval = cmSystemTools::GetEnv(name); |
| 2161 | if (curval) { |
| 2162 | output = curval; |
| 2163 | } |
| 2164 | } |
| 2165 | apply(output); |
| 2166 | diff[name] = output; |
| 2167 | }; |
| 2168 | |
| 2169 | // Split on `=` |
| 2170 | auto const eq_loc = envmod.find_first_of('='); |
| 2171 | if (eq_loc == std::string::npos) { |
| 2172 | cmSystemTools::Error(cmStrCat( |
| 2173 | "Error: Missing `=` after the variable name in: ", envmod, '\n')); |
| 2174 | return false; |
| 2175 | } |
| 2176 | |
| 2177 | auto const name = envmod.substr(0, eq_loc); |
| 2178 | |
| 2179 | // Split value on `:` |
| 2180 | auto const op_value_start = eq_loc + 1; |
| 2181 | auto const colon_loc = envmod.find_first_of(':', op_value_start); |
| 2182 | if (colon_loc == std::string::npos) { |
| 2183 | cmSystemTools::Error( |
| 2184 | cmStrCat("Error: Missing `:` after the operation in: ", envmod, '\n')); |
| 2185 | return false; |
| 2186 | } |
| 2187 | auto const op = envmod.substr(op_value_start, colon_loc - op_value_start); |
| 2188 | |
| 2189 | auto const value_start = colon_loc + 1; |
| 2190 | auto const value = envmod.substr(value_start); |
| 2191 | |
| 2192 | // Determine what to do with the operation. |
| 2193 | if (op == "reset"_s) { |
| 2194 | auto entry = diff.find(name); |
| 2195 | if (entry != diff.end()) { |
| 2196 | diff.erase(entry); |
| 2197 | } |
| 2198 | } else if (op == "set"_s) { |
| 2199 | diff[name] = value; |
| 2200 | } else if (op == "unset"_s) { |
| 2201 | diff[name] = cm::nullopt; |
| 2202 | } else if (op == "string_append"_s) { |
| 2203 | apply_diff(name, [&value](std::string& output) { output += value; }); |
| 2204 | } else if (op == "string_prepend"_s) { |
| 2205 | apply_diff(name, |
| 2206 | [&value](std::string& output) { output.insert(0, value); }); |