Shinichiro Hamaji | 1d545aa | 2015-06-23 15:29:13 +0900 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All rights reserved |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 15 | // +build ignore |
| 16 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 17 | #include "func.h" |
| 18 | |
Shinichiro Hamaji | 62b16e7 | 2015-07-02 01:42:31 +0900 | [diff] [blame] | 19 | #include <errno.h> |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 20 | #include <limits.h> |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 21 | #include <stdio.h> |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 22 | #include <stdlib.h> |
Shinichiro Hamaji | 62b16e7 | 2015-07-02 01:42:31 +0900 | [diff] [blame] | 23 | #include <unistd.h> |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 24 | |
Shinichiro Hamaji | d87e59e | 2015-06-17 18:18:34 +0900 | [diff] [blame] | 25 | #include <algorithm> |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 26 | #include <iterator> |
Shinichiro Hamaji | cf0cd68 | 2015-06-18 16:18:13 +0900 | [diff] [blame] | 27 | #include <memory> |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 28 | #include <unordered_map> |
| 29 | |
Shinichiro Hamaji | 80456fb | 2015-06-18 14:56:10 +0900 | [diff] [blame] | 30 | #include "ast.h" |
Shinichiro Hamaji | 9619b36 | 2015-06-16 16:13:25 +0900 | [diff] [blame] | 31 | #include "eval.h" |
Shinichiro Hamaji | 94d6f2a | 2015-07-05 05:32:25 +0900 | [diff] [blame] | 32 | #include "fileutil.h" |
Shinichiro Hamaji | 5f57a99 | 2015-06-30 19:39:39 +0900 | [diff] [blame] | 33 | #include "find.h" |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 34 | #include "log.h" |
Shinichiro Hamaji | 80456fb | 2015-06-18 14:56:10 +0900 | [diff] [blame] | 35 | #include "parser.h" |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 36 | #include "stats.h" |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 37 | #include "strutil.h" |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 38 | #include "symtab.h" |
Shinichiro Hamaji | cf0cd68 | 2015-06-18 16:18:13 +0900 | [diff] [blame] | 39 | #include "var.h" |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 40 | |
| 41 | namespace { |
| 42 | |
Shinichiro Hamaji | fc14d5f | 2015-07-28 17:07:57 +0900 | [diff] [blame] | 43 | // TODO: This code is very similar to |
| 44 | // NinjaGenerator::TranslateCommand. Factor them out. |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 45 | void StripShellComment(string* cmd) { |
Shinichiro Hamaji | fc14d5f | 2015-07-28 17:07:57 +0900 | [diff] [blame] | 46 | if (cmd->find('#') == string::npos) |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 47 | return; |
Shinichiro Hamaji | fc14d5f | 2015-07-28 17:07:57 +0900 | [diff] [blame] | 48 | |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 49 | string res; |
Shinichiro Hamaji | fc14d5f | 2015-07-28 17:07:57 +0900 | [diff] [blame] | 50 | bool prev_backslash = false; |
| 51 | // Set space as an initial value so the leading comment will be |
| 52 | // stripped out. |
| 53 | char prev_char = ' '; |
| 54 | char quote = 0; |
| 55 | bool done = false; |
| 56 | const char* in = cmd->c_str(); |
| 57 | for (; *in && !done; in++) { |
| 58 | switch (*in) { |
| 59 | case '#': |
| 60 | if (quote == 0 && isspace(prev_char)) { |
| 61 | while (*in && *in != '\n') |
| 62 | in++; |
| 63 | break; |
| 64 | } |
| 65 | |
| 66 | case '\'': |
| 67 | case '"': |
| 68 | case '`': |
| 69 | if (quote) { |
| 70 | if (quote == *in) |
| 71 | quote = 0; |
| 72 | } else if (!prev_backslash) { |
| 73 | quote = *in; |
| 74 | } |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 75 | res += *in; |
Shinichiro Hamaji | fc14d5f | 2015-07-28 17:07:57 +0900 | [diff] [blame] | 76 | break; |
| 77 | |
| 78 | case '\\': |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 79 | res += '\\'; |
Shinichiro Hamaji | fc14d5f | 2015-07-28 17:07:57 +0900 | [diff] [blame] | 80 | break; |
| 81 | |
| 82 | default: |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 83 | res += *in; |
Shinichiro Hamaji | fc14d5f | 2015-07-28 17:07:57 +0900 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | if (*in == '\\') { |
| 87 | prev_backslash = !prev_backslash; |
| 88 | } else { |
| 89 | prev_backslash = false; |
| 90 | } |
| 91 | |
| 92 | prev_char = *in; |
| 93 | } |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 94 | cmd->swap(res); |
Shinichiro Hamaji | fc14d5f | 2015-07-28 17:07:57 +0900 | [diff] [blame] | 95 | } |
| 96 | |
Shinichiro Hamaji | 2e6cbfc | 2015-06-16 18:46:50 +0900 | [diff] [blame] | 97 | void PatsubstFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 98 | const string&& pat_str = args[0]->Eval(ev); |
| 99 | const string&& repl = args[1]->Eval(ev); |
| 100 | const string&& str = args[2]->Eval(ev); |
Shinichiro Hamaji | 37591ce | 2015-06-16 19:36:05 +0900 | [diff] [blame] | 101 | WordWriter ww(s); |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 102 | Pattern pat(pat_str); |
| 103 | for (StringPiece tok : WordScanner(str)) { |
Shinichiro Hamaji | 37591ce | 2015-06-16 19:36:05 +0900 | [diff] [blame] | 104 | ww.MaybeAddWhitespace(); |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 105 | pat.AppendSubst(tok, repl, s); |
Shinichiro Hamaji | 2e6cbfc | 2015-06-16 18:46:50 +0900 | [diff] [blame] | 106 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 107 | } |
| 108 | |
Shinichiro Hamaji | 37591ce | 2015-06-16 19:36:05 +0900 | [diff] [blame] | 109 | void StripFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 110 | const string&& str = args[0]->Eval(ev); |
Shinichiro Hamaji | 37591ce | 2015-06-16 19:36:05 +0900 | [diff] [blame] | 111 | WordWriter ww(s); |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 112 | for (StringPiece tok : WordScanner(str)) { |
Shinichiro Hamaji | 37591ce | 2015-06-16 19:36:05 +0900 | [diff] [blame] | 113 | ww.Write(tok); |
| 114 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 115 | } |
| 116 | |
Shinichiro Hamaji | 37591ce | 2015-06-16 19:36:05 +0900 | [diff] [blame] | 117 | void SubstFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 118 | const string&& pat = args[0]->Eval(ev); |
| 119 | const string&& repl = args[1]->Eval(ev); |
| 120 | const string&& str = args[2]->Eval(ev); |
| 121 | if (pat.empty()) { |
| 122 | *s += str; |
| 123 | *s += repl; |
Shinichiro Hamaji | 5af931d | 2015-07-06 15:37:59 +0900 | [diff] [blame] | 124 | return; |
| 125 | } |
Shinichiro Hamaji | 37591ce | 2015-06-16 19:36:05 +0900 | [diff] [blame] | 126 | size_t index = 0; |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 127 | while (index < str.size()) { |
| 128 | size_t found = str.find(pat, index); |
Shinichiro Hamaji | 37591ce | 2015-06-16 19:36:05 +0900 | [diff] [blame] | 129 | if (found == string::npos) |
| 130 | break; |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 131 | AppendString(StringPiece(str).substr(index, found - index), s); |
| 132 | AppendString(repl, s); |
| 133 | index = found + pat.size(); |
Shinichiro Hamaji | 37591ce | 2015-06-16 19:36:05 +0900 | [diff] [blame] | 134 | } |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 135 | AppendString(StringPiece(str).substr(index), s); |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 136 | } |
| 137 | |
Shinichiro Hamaji | 00cc658 | 2015-06-17 18:12:46 +0900 | [diff] [blame] | 138 | void FindstringFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 139 | const string&& find = args[0]->Eval(ev); |
| 140 | const string&& in = args[1]->Eval(ev); |
| 141 | if (in.find(find) != string::npos) |
| 142 | AppendString(find, s); |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 143 | } |
| 144 | |
Shinichiro Hamaji | 00cc658 | 2015-06-17 18:12:46 +0900 | [diff] [blame] | 145 | void FilterFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 146 | const string&& pat_buf = args[0]->Eval(ev); |
| 147 | const string&& text = args[1]->Eval(ev); |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 148 | vector<Pattern> pats; |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 149 | for (StringPiece pat : WordScanner(pat_buf)) { |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 150 | pats.push_back(Pattern(pat)); |
| 151 | } |
Shinichiro Hamaji | 00cc658 | 2015-06-17 18:12:46 +0900 | [diff] [blame] | 152 | WordWriter ww(s); |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 153 | for (StringPiece tok : WordScanner(text)) { |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 154 | for (const Pattern& pat : pats) { |
| 155 | if (pat.Match(tok)) { |
Shinichiro Hamaji | 00cc658 | 2015-06-17 18:12:46 +0900 | [diff] [blame] | 156 | ww.Write(tok); |
| 157 | break; |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | void FilterOutFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 164 | const string&& pat_buf = args[0]->Eval(ev); |
| 165 | const string&& text = args[1]->Eval(ev); |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 166 | vector<Pattern> pats; |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 167 | for (StringPiece pat : WordScanner(pat_buf)) { |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 168 | pats.push_back(Pattern(pat)); |
| 169 | } |
Shinichiro Hamaji | 00cc658 | 2015-06-17 18:12:46 +0900 | [diff] [blame] | 170 | WordWriter ww(s); |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 171 | for (StringPiece tok : WordScanner(text)) { |
Shinichiro Hamaji | 00cc658 | 2015-06-17 18:12:46 +0900 | [diff] [blame] | 172 | bool matched = false; |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 173 | for (const Pattern& pat : pats) { |
| 174 | if (pat.Match(tok)) { |
Shinichiro Hamaji | 00cc658 | 2015-06-17 18:12:46 +0900 | [diff] [blame] | 175 | matched = true; |
| 176 | break; |
| 177 | } |
| 178 | } |
| 179 | if (!matched) |
| 180 | ww.Write(tok); |
| 181 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 182 | } |
| 183 | |
Shinichiro Hamaji | d87e59e | 2015-06-17 18:18:34 +0900 | [diff] [blame] | 184 | void SortFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 185 | const string&& list = args[0]->Eval(ev); |
Shinichiro Hamaji | d87e59e | 2015-06-17 18:18:34 +0900 | [diff] [blame] | 186 | vector<StringPiece> toks; |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 187 | WordScanner(list).Split(&toks); |
Shinichiro Hamaji | d87e59e | 2015-06-17 18:18:34 +0900 | [diff] [blame] | 188 | sort(toks.begin(), toks.end()); |
| 189 | WordWriter ww(s); |
| 190 | StringPiece prev; |
| 191 | for (StringPiece tok : toks) { |
| 192 | if (prev != tok) { |
| 193 | ww.Write(tok); |
| 194 | prev = tok; |
| 195 | } |
| 196 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 197 | } |
| 198 | |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 199 | static int GetNumericValueForFunc(const string& buf) { |
| 200 | StringPiece s = TrimLeftSpace(buf); |
| 201 | char* end; |
| 202 | long n = strtol(s.data(), &end, 10); |
| 203 | if (n < 0 || n == LONG_MAX || s.data() + s.size() != end) { |
| 204 | return -1; |
| 205 | } |
| 206 | return n; |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 207 | } |
| 208 | |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 209 | void WordFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 210 | const string&& n_str = args[0]->Eval(ev); |
| 211 | int n = GetNumericValueForFunc(n_str); |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 212 | if (n < 0) { |
| 213 | ev->Error(StringPrintf( |
| 214 | "*** non-numeric first argument to `word' function: '%s'.", |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 215 | n_str.c_str())); |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 216 | } |
| 217 | if (n == 0) { |
| 218 | ev->Error("*** first argument to `word' function must be greater than 0."); |
| 219 | } |
| 220 | |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 221 | const string&& text = args[1]->Eval(ev); |
| 222 | for (StringPiece tok : WordScanner(text)) { |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 223 | n--; |
| 224 | if (n == 0) { |
| 225 | AppendString(tok, s); |
| 226 | break; |
| 227 | } |
| 228 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 229 | } |
| 230 | |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 231 | void WordlistFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 232 | const string&& s_str = args[0]->Eval(ev); |
| 233 | int si = GetNumericValueForFunc(s_str); |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 234 | if (si < 0) { |
| 235 | ev->Error(StringPrintf( |
| 236 | "*** non-numeric first argument to `wordlist' function: '%s'.", |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 237 | s_str.c_str())); |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 238 | } |
| 239 | if (si == 0) { |
| 240 | ev->Error(StringPrintf( |
| 241 | "*** invalid first argument to `wordlist' function: %s`", |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 242 | s_str.c_str())); |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 243 | } |
| 244 | |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 245 | const string&& e_str = args[1]->Eval(ev); |
| 246 | int ei = GetNumericValueForFunc(e_str); |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 247 | if (ei < 0) { |
| 248 | ev->Error(StringPrintf( |
| 249 | "*** non-numeric second argument to `wordlist' function: '%s'.", |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 250 | e_str.c_str())); |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 251 | } |
| 252 | |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 253 | const string&& text = args[2]->Eval(ev); |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 254 | int i = 0; |
| 255 | WordWriter ww(s); |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 256 | for (StringPiece tok : WordScanner(text)) { |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 257 | i++; |
| 258 | if (si <= i && i <= ei) { |
| 259 | ww.Write(tok); |
| 260 | } |
| 261 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 262 | } |
| 263 | |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 264 | void WordsFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 265 | const string&& text = args[0]->Eval(ev); |
| 266 | WordScanner ws(text); |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 267 | int n = 0; |
| 268 | for (auto iter = ws.begin(); iter != ws.end(); ++iter) |
| 269 | n++; |
| 270 | char buf[32]; |
| 271 | sprintf(buf, "%d", n); |
| 272 | *s += buf; |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 273 | } |
| 274 | |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 275 | void FirstwordFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 276 | const string&& text = args[0]->Eval(ev); |
| 277 | for (StringPiece tok : WordScanner(text)) { |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 278 | AppendString(tok, s); |
| 279 | return; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | void LastwordFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 284 | const string&& text = args[0]->Eval(ev); |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 285 | StringPiece last; |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 286 | for (StringPiece tok : WordScanner(text)) { |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 287 | last = tok; |
| 288 | } |
| 289 | AppendString(last, s); |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 290 | } |
| 291 | |
Shinichiro Hamaji | 30b8e60 | 2015-06-17 19:28:54 +0900 | [diff] [blame] | 292 | void JoinFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 293 | const string&& list1 = args[0]->Eval(ev); |
| 294 | const string&& list2 = args[1]->Eval(ev); |
| 295 | WordScanner ws1(list1); |
| 296 | WordScanner ws2(list2); |
Shinichiro Hamaji | 30b8e60 | 2015-06-17 19:28:54 +0900 | [diff] [blame] | 297 | WordWriter ww(s); |
| 298 | for (WordScanner::Iterator iter1 = ws1.begin(), iter2 = ws2.begin(); |
| 299 | iter1 != ws1.end() && iter2 != ws2.end(); |
| 300 | ++iter1, ++iter2) { |
| 301 | ww.Write(*iter1); |
| 302 | // Use |AppendString| not to append extra ' '. |
| 303 | AppendString(*iter2, s); |
| 304 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 305 | } |
| 306 | |
Shinichiro Hamaji | fcf1b76 | 2015-06-18 03:43:54 +0900 | [diff] [blame] | 307 | void WildcardFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 308 | COLLECT_STATS("func wildcard time"); |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 309 | const string&& pat = args[0]->Eval(ev); |
Shinichiro Hamaji | 7409aee | 2015-07-28 14:52:37 +0900 | [diff] [blame] | 310 | // Note GNU make does not delay the execution of $(wildcard) so we |
| 311 | // do not need to check avoid_io here. |
Shinichiro Hamaji | fcf1b76 | 2015-06-18 03:43:54 +0900 | [diff] [blame] | 312 | WordWriter ww(s); |
Shinichiro Hamaji | 0e3873a | 2015-07-05 15:48:28 +0900 | [diff] [blame] | 313 | vector<string>* files; |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 314 | for (StringPiece tok : WordScanner(pat)) { |
Shinichiro Hamaji | 8f68bd3 | 2015-06-18 11:01:51 +0900 | [diff] [blame] | 315 | ScopedTerminator st(tok); |
Shinichiro Hamaji | 0e3873a | 2015-07-05 15:48:28 +0900 | [diff] [blame] | 316 | Glob(tok.data(), &files); |
| 317 | sort(files->begin(), files->end()); |
| 318 | for (const string& file : *files) { |
Shinichiro Hamaji | fe97c41 | 2015-06-29 16:57:57 +0900 | [diff] [blame] | 319 | ww.Write(file); |
| 320 | } |
Shinichiro Hamaji | 284f3d1 | 2015-06-17 19:29:01 +0900 | [diff] [blame] | 321 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 322 | } |
| 323 | |
Shinichiro Hamaji | 67f9a70 | 2015-06-18 06:00:57 +0900 | [diff] [blame] | 324 | void DirFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 325 | const string&& text = args[0]->Eval(ev); |
Shinichiro Hamaji | 67f9a70 | 2015-06-18 06:00:57 +0900 | [diff] [blame] | 326 | WordWriter ww(s); |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 327 | for (StringPiece tok : WordScanner(text)) { |
Shinichiro Hamaji | 67f9a70 | 2015-06-18 06:00:57 +0900 | [diff] [blame] | 328 | ww.Write(Dirname(tok)); |
Shinichiro Hamaji | 5590685 | 2015-06-29 16:40:33 +0900 | [diff] [blame] | 329 | s->push_back('/'); |
Shinichiro Hamaji | 67f9a70 | 2015-06-18 06:00:57 +0900 | [diff] [blame] | 330 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 331 | } |
| 332 | |
Shinichiro Hamaji | 67f9a70 | 2015-06-18 06:00:57 +0900 | [diff] [blame] | 333 | void NotdirFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 334 | const string&& text = args[0]->Eval(ev); |
Shinichiro Hamaji | 67f9a70 | 2015-06-18 06:00:57 +0900 | [diff] [blame] | 335 | WordWriter ww(s); |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 336 | for (StringPiece tok : WordScanner(text)) { |
Shinichiro Hamaji | 67f9a70 | 2015-06-18 06:00:57 +0900 | [diff] [blame] | 337 | if (tok == "/") { |
Shinichiro Hamaji | 388e858 | 2015-07-03 16:51:46 +0900 | [diff] [blame] | 338 | ww.Write(StringPiece("")); |
Shinichiro Hamaji | 67f9a70 | 2015-06-18 06:00:57 +0900 | [diff] [blame] | 339 | } else { |
| 340 | ww.Write(Basename(tok)); |
| 341 | } |
| 342 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 343 | } |
| 344 | |
Shinichiro Hamaji | 67f9a70 | 2015-06-18 06:00:57 +0900 | [diff] [blame] | 345 | void SuffixFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 346 | const string&& text = args[0]->Eval(ev); |
Shinichiro Hamaji | 67f9a70 | 2015-06-18 06:00:57 +0900 | [diff] [blame] | 347 | WordWriter ww(s); |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 348 | for (StringPiece tok : WordScanner(text)) { |
Shinichiro Hamaji | 67f9a70 | 2015-06-18 06:00:57 +0900 | [diff] [blame] | 349 | StringPiece suf = GetExt(tok); |
| 350 | if (!suf.empty()) |
| 351 | ww.Write(suf); |
| 352 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 353 | } |
| 354 | |
Shinichiro Hamaji | 67f9a70 | 2015-06-18 06:00:57 +0900 | [diff] [blame] | 355 | void BasenameFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 356 | const string&& text = args[0]->Eval(ev); |
Shinichiro Hamaji | 67f9a70 | 2015-06-18 06:00:57 +0900 | [diff] [blame] | 357 | WordWriter ww(s); |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 358 | for (StringPiece tok : WordScanner(text)) { |
Shinichiro Hamaji | 67f9a70 | 2015-06-18 06:00:57 +0900 | [diff] [blame] | 359 | ww.Write(StripExt(tok)); |
| 360 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 361 | } |
| 362 | |
Shinichiro Hamaji | 5d694f0 | 2015-06-18 06:05:36 +0900 | [diff] [blame] | 363 | void AddsuffixFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 364 | const string&& suf = args[0]->Eval(ev); |
| 365 | const string&& text = args[1]->Eval(ev); |
Shinichiro Hamaji | 5d694f0 | 2015-06-18 06:05:36 +0900 | [diff] [blame] | 366 | WordWriter ww(s); |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 367 | for (StringPiece tok : WordScanner(text)) { |
Shinichiro Hamaji | 5d694f0 | 2015-06-18 06:05:36 +0900 | [diff] [blame] | 368 | ww.Write(tok); |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 369 | *s += suf; |
Shinichiro Hamaji | 5d694f0 | 2015-06-18 06:05:36 +0900 | [diff] [blame] | 370 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 371 | } |
| 372 | |
Shinichiro Hamaji | 5d694f0 | 2015-06-18 06:05:36 +0900 | [diff] [blame] | 373 | void AddprefixFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 374 | const string&& pre = args[0]->Eval(ev); |
| 375 | const string&& text = args[1]->Eval(ev); |
Shinichiro Hamaji | 5d694f0 | 2015-06-18 06:05:36 +0900 | [diff] [blame] | 376 | WordWriter ww(s); |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 377 | for (StringPiece tok : WordScanner(text)) { |
| 378 | ww.Write(pre); |
Shinichiro Hamaji | 5d694f0 | 2015-06-18 06:05:36 +0900 | [diff] [blame] | 379 | AppendString(tok, s); |
| 380 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 381 | } |
| 382 | |
Shinichiro Hamaji | 8a96358 | 2015-06-18 07:05:58 +0900 | [diff] [blame] | 383 | void RealpathFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 384 | const string&& text = args[0]->Eval(ev); |
Shinichiro Hamaji | df1fc8b | 2015-06-29 15:45:21 +0900 | [diff] [blame] | 385 | if (ev->avoid_io()) { |
Shinichiro Hamaji | 65dce54 | 2015-07-28 15:39:03 +0900 | [diff] [blame] | 386 | *s += "$(realpath "; |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 387 | *s += text; |
Shinichiro Hamaji | 65dce54 | 2015-07-28 15:39:03 +0900 | [diff] [blame] | 388 | *s += " 2> /dev/null)"; |
Shinichiro Hamaji | df1fc8b | 2015-06-29 15:45:21 +0900 | [diff] [blame] | 389 | return; |
| 390 | } |
| 391 | |
Shinichiro Hamaji | 8a96358 | 2015-06-18 07:05:58 +0900 | [diff] [blame] | 392 | WordWriter ww(s); |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 393 | for (StringPiece tok : WordScanner(text)) { |
Shinichiro Hamaji | 8f68bd3 | 2015-06-18 11:01:51 +0900 | [diff] [blame] | 394 | ScopedTerminator st(tok); |
Shinichiro Hamaji | 8a96358 | 2015-06-18 07:05:58 +0900 | [diff] [blame] | 395 | char buf[PATH_MAX]; |
| 396 | if (realpath(tok.data(), buf)) |
| 397 | *s += buf; |
Shinichiro Hamaji | 8a96358 | 2015-06-18 07:05:58 +0900 | [diff] [blame] | 398 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 399 | } |
| 400 | |
Shinichiro Hamaji | 8a96358 | 2015-06-18 07:05:58 +0900 | [diff] [blame] | 401 | void AbspathFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 402 | const string&& text = args[0]->Eval(ev); |
Shinichiro Hamaji | 8a96358 | 2015-06-18 07:05:58 +0900 | [diff] [blame] | 403 | WordWriter ww(s); |
| 404 | string buf; |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 405 | for (StringPiece tok : WordScanner(text)) { |
Shinichiro Hamaji | 8a96358 | 2015-06-18 07:05:58 +0900 | [diff] [blame] | 406 | AbsPath(tok, &buf); |
| 407 | ww.Write(buf); |
| 408 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 409 | } |
| 410 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 411 | void IfFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 412 | const string&& cond = args[0]->Eval(ev); |
| 413 | if (cond.empty()) { |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 414 | if (args.size() > 2) |
| 415 | args[2]->Eval(ev, s); |
| 416 | } else { |
| 417 | args[1]->Eval(ev, s); |
| 418 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 419 | } |
| 420 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 421 | void AndFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 422 | string cond; |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 423 | for (Value* a : args) { |
| 424 | cond = a->Eval(ev); |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 425 | if (cond.empty()) |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 426 | return; |
| 427 | } |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 428 | if (!cond.empty()) { |
| 429 | *s += cond; |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 430 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 431 | } |
| 432 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 433 | void OrFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 434 | for (Value* a : args) { |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 435 | const string&& cond = a->Eval(ev); |
| 436 | if (!cond.empty()) { |
| 437 | *s += cond; |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 438 | return; |
| 439 | } |
| 440 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 441 | } |
| 442 | |
Shinichiro Hamaji | 81699be | 2015-06-22 18:07:38 +0900 | [diff] [blame] | 443 | void ValueFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 444 | const string&& var_name = args[0]->Eval(ev); |
| 445 | Var* var = ev->LookupVar(Intern(var_name)); |
Shinichiro Hamaji | 81699be | 2015-06-22 18:07:38 +0900 | [diff] [blame] | 446 | AppendString(var->String().as_string(), s); |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 447 | } |
| 448 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 449 | void EvalFunc(const vector<Value*>& args, Evaluator* ev, string*) { |
Shinichiro Hamaji | 76ff983 | 2015-06-18 17:11:22 +0900 | [diff] [blame] | 450 | // TODO: eval leaks everything... for now. |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 451 | //const string text = args[0]->Eval(ev); |
Shinichiro Hamaji | 76ff983 | 2015-06-18 17:11:22 +0900 | [diff] [blame] | 452 | string* text = new string; |
| 453 | args[0]->Eval(ev, text); |
Shinichiro Hamaji | 80456fb | 2015-06-18 14:56:10 +0900 | [diff] [blame] | 454 | vector<AST*> asts; |
| 455 | Parse(*text, ev->loc(), &asts); |
| 456 | for (AST* ast : asts) { |
| 457 | LOG("%s", ast->DebugString().c_str()); |
| 458 | ast->Eval(ev); |
Shinichiro Hamaji | 76ff983 | 2015-06-18 17:11:22 +0900 | [diff] [blame] | 459 | //delete ast; |
Shinichiro Hamaji | 80456fb | 2015-06-18 14:56:10 +0900 | [diff] [blame] | 460 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 461 | } |
| 462 | |
Shinichiro Hamaji | 5f57a99 | 2015-06-30 19:39:39 +0900 | [diff] [blame] | 463 | //#define TEST_FIND_EMULATOR |
| 464 | |
Shinichiro Hamaji | 68e712b | 2015-07-17 06:11:08 +0900 | [diff] [blame] | 465 | // A hack for Android build. We need to evaluate things like $((3+4)) |
| 466 | // when we emit ninja file, because the result of such expressions |
| 467 | // will be passed to other make functions. |
| 468 | // TODO: Maybe we should introduce a helper binary which evaluate |
| 469 | // make expressions at ninja-time. |
| 470 | static bool HasNoIoInShellScript(const string& cmd) { |
| 471 | if (cmd.empty()) |
| 472 | return true; |
| 473 | if (HasPrefix(cmd, "echo $((") && cmd[cmd.size()-1] == ')') |
| 474 | return true; |
| 475 | return false; |
| 476 | } |
| 477 | |
Shinichiro Hamaji | c9b0aca | 2015-07-31 16:47:56 +0900 | [diff] [blame] | 478 | static void ShellFuncImpl(const string& shell, const string& cmd, |
Shinichiro Hamaji | c9b0aca | 2015-07-31 16:47:56 +0900 | [diff] [blame] | 479 | string* s, FindCommand** fc) { |
| 480 | LOG("ShellFunc: %s", cmd.c_str()); |
| 481 | |
| 482 | #ifdef TEST_FIND_EMULATOR |
| 483 | bool need_check = false; |
| 484 | string out2; |
| 485 | #endif |
| 486 | if (FindEmulator::Get()) { |
| 487 | *fc = new FindCommand(); |
| 488 | if ((*fc)->Parse(cmd)) { |
| 489 | #ifdef TEST_FIND_EMULATOR |
| 490 | if (FindEmulator::Get()->HandleFind(cmd, **fc, &out2)) { |
| 491 | need_check = true; |
| 492 | } |
| 493 | #else |
| 494 | if (FindEmulator::Get()->HandleFind(cmd, **fc, s)) { |
Shinichiro Hamaji | c9b0aca | 2015-07-31 16:47:56 +0900 | [diff] [blame] | 495 | return; |
| 496 | } |
| 497 | #endif |
| 498 | } |
| 499 | delete *fc; |
| 500 | *fc = NULL; |
| 501 | } |
| 502 | |
| 503 | COLLECT_STATS_WITH_SLOW_REPORT("func shell time", cmd.c_str()); |
| 504 | RunCommand(shell, cmd, RedirectStderr::NONE, s); |
Dan Willemsen | 48d6e8c | 2015-08-05 14:38:34 -0700 | [diff] [blame] | 505 | FormatForCommandSubstitution(s); |
Shinichiro Hamaji | c9b0aca | 2015-07-31 16:47:56 +0900 | [diff] [blame] | 506 | |
| 507 | #ifdef TEST_FIND_EMULATOR |
| 508 | if (need_check) { |
Dan Willemsen | 48d6e8c | 2015-08-05 14:38:34 -0700 | [diff] [blame] | 509 | if (*s != out2) { |
Shinichiro Hamaji | c9b0aca | 2015-07-31 16:47:56 +0900 | [diff] [blame] | 510 | ERROR("FindEmulator is broken: %s\n%s\nvs\n%s", |
Dan Willemsen | 48d6e8c | 2015-08-05 14:38:34 -0700 | [diff] [blame] | 511 | cmd.c_str(), s->c_str(), out2.c_str()); |
Shinichiro Hamaji | c9b0aca | 2015-07-31 16:47:56 +0900 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | #endif |
| 515 | } |
| 516 | |
Shinichiro Hamaji | 4db9edc | 2015-08-13 16:43:02 +0900 | [diff] [blame] | 517 | static vector<CommandResult*> g_command_results; |
Shinichiro Hamaji | c9b0aca | 2015-07-31 16:47:56 +0900 | [diff] [blame] | 518 | |
Shinichiro Hamaji | 180b409 | 2015-08-13 17:11:18 +0900 | [diff] [blame] | 519 | bool ShouldStoreCommandResult(StringPiece cmd) { |
| 520 | if (HasWord(cmd, "date") || HasWord(cmd, "echo")) |
| 521 | return false; |
Shinichiro Hamaji | 003d06e | 2015-09-09 18:22:04 +0900 | [diff] [blame] | 522 | if (g_flags.ignore_dirty_pattern) { |
| 523 | Pattern pat(g_flags.ignore_dirty_pattern); |
Shinichiro Hamaji | 180b409 | 2015-08-13 17:11:18 +0900 | [diff] [blame] | 524 | for (StringPiece tok : WordScanner(cmd)) { |
| 525 | if (pat.Match(tok)) |
| 526 | return false; |
| 527 | } |
| 528 | } |
| 529 | return true; |
| 530 | } |
| 531 | |
Shinichiro Hamaji | fcf1b76 | 2015-06-18 03:43:54 +0900 | [diff] [blame] | 532 | void ShellFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 533 | string cmd = args[0]->Eval(ev); |
| 534 | if (ev->avoid_io() && !HasNoIoInShellScript(cmd)) { |
| 535 | StripShellComment(&cmd); |
Shinichiro Hamaji | df1fc8b | 2015-06-29 15:45:21 +0900 | [diff] [blame] | 536 | *s += "$("; |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 537 | *s += cmd; |
Shinichiro Hamaji | df1fc8b | 2015-06-29 15:45:21 +0900 | [diff] [blame] | 538 | *s += ")"; |
| 539 | return; |
| 540 | } |
| 541 | |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 542 | const string&& shell = ev->EvalVar(kShellSym); |
Shinichiro Hamaji | fcf1b76 | 2015-06-18 03:43:54 +0900 | [diff] [blame] | 543 | |
Shinichiro Hamaji | c9b0aca | 2015-07-31 16:47:56 +0900 | [diff] [blame] | 544 | string out; |
| 545 | FindCommand* fc = NULL; |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 546 | ShellFuncImpl(shell, cmd, &out, &fc); |
| 547 | if (ShouldStoreCommandResult(cmd)) { |
Shinichiro Hamaji | 4db9edc | 2015-08-13 16:43:02 +0900 | [diff] [blame] | 548 | CommandResult* cr = new CommandResult(); |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 549 | cr->cmd = cmd; |
Shinichiro Hamaji | 4db9edc | 2015-08-13 16:43:02 +0900 | [diff] [blame] | 550 | cr->find.reset(fc); |
| 551 | cr->result = out; |
| 552 | g_command_results.push_back(cr); |
Shinichiro Hamaji | fcf1b76 | 2015-06-18 03:43:54 +0900 | [diff] [blame] | 553 | } |
| 554 | *s += out; |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 555 | } |
| 556 | |
Shinichiro Hamaji | 3064f1f | 2015-06-18 16:32:09 +0900 | [diff] [blame] | 557 | void CallFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Colin Cross | 580cc1f | 2015-07-27 13:18:18 -0700 | [diff] [blame] | 558 | static const string tmpvar_names[] = { |
Shinichiro Hamaji | 3064f1f | 2015-06-18 16:32:09 +0900 | [diff] [blame] | 559 | "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" |
| 560 | }; |
| 561 | |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 562 | const string&& func_name = args[0]->Eval(ev); |
| 563 | Var* func = ev->LookupVar(Intern(func_name)); |
Shinichiro Hamaji | 3064f1f | 2015-06-18 16:32:09 +0900 | [diff] [blame] | 564 | vector<unique_ptr<SimpleVar>> av; |
Shinichiro Hamaji | 3064f1f | 2015-06-18 16:32:09 +0900 | [diff] [blame] | 565 | for (size_t i = 1; i < args.size(); i++) { |
Shinichiro Hamaji | f62e9a7 | 2015-06-26 04:18:21 +0900 | [diff] [blame] | 566 | unique_ptr<SimpleVar> s( |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 567 | new SimpleVar(args[i]->Eval(ev), VarOrigin::AUTOMATIC)); |
Shinichiro Hamaji | 3064f1f | 2015-06-18 16:32:09 +0900 | [diff] [blame] | 568 | av.push_back(move(s)); |
| 569 | } |
Shinichiro Hamaji | 5d53bc7 | 2015-06-26 08:31:54 +0900 | [diff] [blame] | 570 | vector<unique_ptr<ScopedVar>> sv; |
Dan Willemsen | b446796 | 2015-08-06 23:59:22 -0700 | [diff] [blame] | 571 | for (size_t i = 1; ; i++) { |
Colin Cross | 580cc1f | 2015-07-27 13:18:18 -0700 | [diff] [blame] | 572 | string s; |
| 573 | StringPiece tmpvar_name; |
| 574 | if (i < sizeof(tmpvar_names)/sizeof(tmpvar_names[0])) { |
| 575 | tmpvar_name = tmpvar_names[i]; |
| 576 | } else { |
| 577 | s = StringPrintf("%d", i); |
| 578 | tmpvar_name = s; |
| 579 | } |
Dan Willemsen | b446796 | 2015-08-06 23:59:22 -0700 | [diff] [blame] | 580 | if (i < args.size()) { |
Shinichiro Hamaji | e978a89 | 2015-08-17 16:53:40 +0900 | [diff] [blame] | 581 | sv.emplace_back(new ScopedVar(ev->mutable_vars(), |
| 582 | Intern(tmpvar_name), av[i-1].get())); |
Dan Willemsen | b446796 | 2015-08-06 23:59:22 -0700 | [diff] [blame] | 583 | } else { |
| 584 | // We need to blank further automatic vars |
| 585 | Var *v = ev->LookupVar(Intern(tmpvar_name)); |
| 586 | if (!v->IsDefined()) break; |
| 587 | if (v->Origin() != VarOrigin::AUTOMATIC) break; |
| 588 | |
Shinichiro Hamaji | e978a89 | 2015-08-17 16:53:40 +0900 | [diff] [blame] | 589 | av.emplace_back(new SimpleVar("", VarOrigin::AUTOMATIC)); |
| 590 | sv.emplace_back(new ScopedVar(ev->mutable_vars(), |
| 591 | Intern(tmpvar_name), av[i-1].get())); |
Dan Willemsen | b446796 | 2015-08-06 23:59:22 -0700 | [diff] [blame] | 592 | } |
Shinichiro Hamaji | 5d53bc7 | 2015-06-26 08:31:54 +0900 | [diff] [blame] | 593 | } |
Shinichiro Hamaji | 3064f1f | 2015-06-18 16:32:09 +0900 | [diff] [blame] | 594 | func->Eval(ev, s); |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 595 | } |
| 596 | |
Shinichiro Hamaji | cf0cd68 | 2015-06-18 16:18:13 +0900 | [diff] [blame] | 597 | void ForeachFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 598 | const string&& varname = args[0]->Eval(ev); |
| 599 | const string&& list = args[1]->Eval(ev); |
Shinichiro Hamaji | cf0cd68 | 2015-06-18 16:18:13 +0900 | [diff] [blame] | 600 | WordWriter ww(s); |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 601 | for (StringPiece tok : WordScanner(list)) { |
Shinichiro Hamaji | cf0cd68 | 2015-06-18 16:18:13 +0900 | [diff] [blame] | 602 | unique_ptr<SimpleVar> v(new SimpleVar( |
Shinichiro Hamaji | 5081c71 | 2015-08-14 16:49:20 +0900 | [diff] [blame] | 603 | tok.as_string(), VarOrigin::AUTOMATIC)); |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 604 | ScopedVar sv(ev->mutable_vars(), Intern(varname), v.get()); |
Shinichiro Hamaji | cf0cd68 | 2015-06-18 16:18:13 +0900 | [diff] [blame] | 605 | ww.MaybeAddWhitespace(); |
| 606 | args[2]->Eval(ev, s); |
| 607 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 608 | } |
| 609 | |
Shinichiro Hamaji | c22fdb4 | 2015-06-22 18:10:40 +0900 | [diff] [blame] | 610 | void OriginFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 611 | const string&& var_name = args[0]->Eval(ev); |
| 612 | Var* var = ev->LookupVar(Intern(var_name)); |
Shinichiro Hamaji | f62e9a7 | 2015-06-26 04:18:21 +0900 | [diff] [blame] | 613 | *s += GetOriginStr(var->Origin()); |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 614 | } |
| 615 | |
Shinichiro Hamaji | c22fdb4 | 2015-06-22 18:10:40 +0900 | [diff] [blame] | 616 | void FlavorFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 617 | const string&& var_name = args[0]->Eval(ev); |
| 618 | Var* var = ev->LookupVar(Intern(var_name)); |
Shinichiro Hamaji | c22fdb4 | 2015-06-22 18:10:40 +0900 | [diff] [blame] | 619 | *s += var->Flavor(); |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 620 | } |
| 621 | |
Shinichiro Hamaji | 86e1133 | 2015-07-28 15:24:17 +0900 | [diff] [blame] | 622 | void InfoFunc(const vector<Value*>& args, Evaluator* ev, string*) { |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 623 | const string&& a = args[0]->Eval(ev); |
Shinichiro Hamaji | df1fc8b | 2015-06-29 15:45:21 +0900 | [diff] [blame] | 624 | if (ev->avoid_io()) { |
Dan Willemsen | e6f6858 | 2015-08-21 11:13:28 -0700 | [diff] [blame] | 625 | ev->add_delayed_output_command(StringPrintf("echo -e \"%s\"", EchoEscape(a).c_str())); |
Shinichiro Hamaji | df1fc8b | 2015-06-29 15:45:21 +0900 | [diff] [blame] | 626 | return; |
| 627 | } |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 628 | printf("%s\n", a.c_str()); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 629 | fflush(stdout); |
| 630 | } |
| 631 | |
Shinichiro Hamaji | 86e1133 | 2015-07-28 15:24:17 +0900 | [diff] [blame] | 632 | void WarningFunc(const vector<Value*>& args, Evaluator* ev, string*) { |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 633 | const string&& a = args[0]->Eval(ev); |
Shinichiro Hamaji | df1fc8b | 2015-06-29 15:45:21 +0900 | [diff] [blame] | 634 | if (ev->avoid_io()) { |
Shinichiro Hamaji | 86e1133 | 2015-07-28 15:24:17 +0900 | [diff] [blame] | 635 | ev->add_delayed_output_command( |
Dan Willemsen | e6f6858 | 2015-08-21 11:13:28 -0700 | [diff] [blame] | 636 | StringPrintf("echo -e \"%s:%d: %s\" 2>&1", LOCF(ev->loc()), EchoEscape(a).c_str())); |
Shinichiro Hamaji | df1fc8b | 2015-06-29 15:45:21 +0900 | [diff] [blame] | 637 | return; |
| 638 | } |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 639 | printf("%s:%d: %s\n", LOCF(ev->loc()), a.c_str()); |
Shinichiro Hamaji | 9619b36 | 2015-06-16 16:13:25 +0900 | [diff] [blame] | 640 | fflush(stdout); |
| 641 | } |
| 642 | |
Shinichiro Hamaji | 86e1133 | 2015-07-28 15:24:17 +0900 | [diff] [blame] | 643 | void ErrorFunc(const vector<Value*>& args, Evaluator* ev, string*) { |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 644 | const string&& a = args[0]->Eval(ev); |
Shinichiro Hamaji | df1fc8b | 2015-06-29 15:45:21 +0900 | [diff] [blame] | 645 | if (ev->avoid_io()) { |
Shinichiro Hamaji | 86e1133 | 2015-07-28 15:24:17 +0900 | [diff] [blame] | 646 | ev->add_delayed_output_command( |
Dan Willemsen | e6f6858 | 2015-08-21 11:13:28 -0700 | [diff] [blame] | 647 | StringPrintf("echo -e \"%s:%d: *** %s.\" 2>&1 && false", |
| 648 | LOCF(ev->loc()), EchoEscape(a).c_str())); |
Shinichiro Hamaji | df1fc8b | 2015-06-29 15:45:21 +0900 | [diff] [blame] | 649 | return; |
| 650 | } |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 651 | ev->Error(StringPrintf("*** %s.", a.c_str())); |
Shinichiro Hamaji | 9619b36 | 2015-06-16 16:13:25 +0900 | [diff] [blame] | 652 | } |
| 653 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 654 | FuncInfo g_func_infos[] = { |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 655 | { "patsubst", &PatsubstFunc, 3, 3, false, false }, |
| 656 | { "strip", &StripFunc, 1, 1, false, false }, |
| 657 | { "subst", &SubstFunc, 3, 3, false, false }, |
| 658 | { "findstring", &FindstringFunc, 2, 2, false, false }, |
| 659 | { "filter", &FilterFunc, 2, 2, false, false }, |
| 660 | { "filter-out", &FilterOutFunc, 2, 2, false, false }, |
| 661 | { "sort", &SortFunc, 1, 1, false, false }, |
| 662 | { "word", &WordFunc, 2, 2, false, false }, |
| 663 | { "wordlist", &WordlistFunc, 3, 3, false, false }, |
| 664 | { "words", &WordsFunc, 1, 1, false, false }, |
| 665 | { "firstword", &FirstwordFunc, 1, 1, false, false }, |
| 666 | { "lastword", &LastwordFunc, 1, 1, false, false }, |
Shinichiro Hamaji | 2e6cbfc | 2015-06-16 18:46:50 +0900 | [diff] [blame] | 667 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 668 | { "join", &JoinFunc, 2, 2, false, false }, |
| 669 | { "wildcard", &WildcardFunc, 1, 1, false, false }, |
| 670 | { "dir", &DirFunc, 1, 1, false, false }, |
| 671 | { "notdir", &NotdirFunc, 1, 1, false, false }, |
| 672 | { "suffix", &SuffixFunc, 1, 1, false, false }, |
| 673 | { "basename", &BasenameFunc, 1, 1, false, false }, |
| 674 | { "addsuffix", &AddsuffixFunc, 2, 2, false, false }, |
| 675 | { "addprefix", &AddprefixFunc, 2, 2, false, false }, |
| 676 | { "realpath", &RealpathFunc, 1, 1, false, false }, |
| 677 | { "abspath", &AbspathFunc, 1, 1, false, false }, |
Shinichiro Hamaji | 30b8e60 | 2015-06-17 19:28:54 +0900 | [diff] [blame] | 678 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 679 | { "if", &IfFunc, 3, 2, false, true }, |
| 680 | { "and", &AndFunc, 0, 0, true, false }, |
| 681 | { "or", &OrFunc, 0, 0, true, false }, |
Shinichiro Hamaji | e22fe8e | 2015-06-18 07:11:54 +0900 | [diff] [blame] | 682 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 683 | { "value", &ValueFunc, 1, 1, false, false }, |
| 684 | { "eval", &EvalFunc, 1, 1, false, false }, |
| 685 | { "shell", &ShellFunc, 1, 1, false, false }, |
| 686 | { "call", &CallFunc, 0, 0, false, false }, |
| 687 | { "foreach", &ForeachFunc, 3, 3, false, false }, |
Shinichiro Hamaji | e22fe8e | 2015-06-18 07:11:54 +0900 | [diff] [blame] | 688 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 689 | { "origin", &OriginFunc, 1, 1, false, false }, |
| 690 | { "flavor", &FlavorFunc, 1, 1, false, false }, |
Shinichiro Hamaji | e22fe8e | 2015-06-18 07:11:54 +0900 | [diff] [blame] | 691 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 692 | { "info", &InfoFunc, 1, 1, false, false }, |
| 693 | { "warning", &WarningFunc, 1, 1, false, false }, |
| 694 | { "error", &ErrorFunc, 1, 1, false, false }, |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 695 | }; |
| 696 | |
| 697 | unordered_map<StringPiece, FuncInfo*>* g_func_info_map; |
| 698 | |
| 699 | } // namespace |
| 700 | |
| 701 | void InitFuncTable() { |
| 702 | g_func_info_map = new unordered_map<StringPiece, FuncInfo*>; |
| 703 | for (size_t i = 0; i < sizeof(g_func_infos) / sizeof(g_func_infos[0]); i++) { |
| 704 | FuncInfo* fi = &g_func_infos[i]; |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 705 | bool ok = g_func_info_map->emplace(fi->name, fi).second; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 706 | CHECK(ok); |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | void QuitFuncTable() { |
| 711 | delete g_func_info_map; |
| 712 | } |
| 713 | |
| 714 | FuncInfo* GetFuncInfo(StringPiece name) { |
| 715 | auto found = g_func_info_map->find(name); |
| 716 | if (found == g_func_info_map->end()) |
| 717 | return NULL; |
| 718 | return found->second; |
| 719 | } |
Shinichiro Hamaji | c9b0aca | 2015-07-31 16:47:56 +0900 | [diff] [blame] | 720 | |
Shinichiro Hamaji | 4db9edc | 2015-08-13 16:43:02 +0900 | [diff] [blame] | 721 | const vector<CommandResult*>& GetShellCommandResults() { |
| 722 | return g_command_results; |
Shinichiro Hamaji | c9b0aca | 2015-07-31 16:47:56 +0900 | [diff] [blame] | 723 | } |