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 | 30b8e60 | 2015-06-17 19:28:54 +0900 | [diff] [blame] | 19 | #include <glob.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 | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 23 | |
Shinichiro Hamaji | d87e59e | 2015-06-17 18:18:34 +0900 | [diff] [blame] | 24 | #include <algorithm> |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 25 | #include <iterator> |
Shinichiro Hamaji | cf0cd68 | 2015-06-18 16:18:13 +0900 | [diff] [blame] | 26 | #include <memory> |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 27 | #include <unordered_map> |
| 28 | |
Shinichiro Hamaji | 80456fb | 2015-06-18 14:56:10 +0900 | [diff] [blame] | 29 | #include "ast.h" |
Shinichiro Hamaji | 9619b36 | 2015-06-16 16:13:25 +0900 | [diff] [blame] | 30 | #include "eval.h" |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 31 | #include "log.h" |
Shinichiro Hamaji | 80456fb | 2015-06-18 14:56:10 +0900 | [diff] [blame] | 32 | #include "parser.h" |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 33 | #include "strutil.h" |
Shinichiro Hamaji | cf0cd68 | 2015-06-18 16:18:13 +0900 | [diff] [blame] | 34 | #include "var.h" |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 35 | |
| 36 | namespace { |
| 37 | |
Shinichiro Hamaji | 2e6cbfc | 2015-06-16 18:46:50 +0900 | [diff] [blame] | 38 | void PatsubstFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 39 | shared_ptr<string> pat_str = args[0]->Eval(ev); |
Shinichiro Hamaji | 2e6cbfc | 2015-06-16 18:46:50 +0900 | [diff] [blame] | 40 | shared_ptr<string> repl = args[1]->Eval(ev); |
| 41 | shared_ptr<string> str = args[2]->Eval(ev); |
Shinichiro Hamaji | 37591ce | 2015-06-16 19:36:05 +0900 | [diff] [blame] | 42 | WordWriter ww(s); |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 43 | Pattern pat(*pat_str); |
Shinichiro Hamaji | 2e6cbfc | 2015-06-16 18:46:50 +0900 | [diff] [blame] | 44 | for (StringPiece tok : WordScanner(*str)) { |
Shinichiro Hamaji | 37591ce | 2015-06-16 19:36:05 +0900 | [diff] [blame] | 45 | ww.MaybeAddWhitespace(); |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 46 | pat.AppendSubst(tok, *repl, s); |
Shinichiro Hamaji | 2e6cbfc | 2015-06-16 18:46:50 +0900 | [diff] [blame] | 47 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 48 | } |
| 49 | |
Shinichiro Hamaji | 37591ce | 2015-06-16 19:36:05 +0900 | [diff] [blame] | 50 | void StripFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 51 | shared_ptr<string> str = args[0]->Eval(ev); |
| 52 | WordWriter ww(s); |
| 53 | for (StringPiece tok : WordScanner(*str)) { |
| 54 | ww.Write(tok); |
| 55 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 56 | } |
| 57 | |
Shinichiro Hamaji | 37591ce | 2015-06-16 19:36:05 +0900 | [diff] [blame] | 58 | void SubstFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 59 | shared_ptr<string> pat = args[0]->Eval(ev); |
| 60 | shared_ptr<string> repl = args[1]->Eval(ev); |
| 61 | shared_ptr<string> str = args[2]->Eval(ev); |
| 62 | size_t index = 0; |
| 63 | while (index < str->size()) { |
| 64 | size_t found = str->find(*pat, index); |
| 65 | if (found == string::npos) |
| 66 | break; |
| 67 | AppendString(StringPiece(*str).substr(index, found - index), s); |
| 68 | AppendString(*repl, s); |
| 69 | index = found + pat->size(); |
| 70 | } |
| 71 | AppendString(StringPiece(*str).substr(index), s); |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 72 | } |
| 73 | |
Shinichiro Hamaji | 00cc658 | 2015-06-17 18:12:46 +0900 | [diff] [blame] | 74 | void FindstringFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 75 | shared_ptr<string> find = args[0]->Eval(ev); |
| 76 | shared_ptr<string> in = args[1]->Eval(ev); |
| 77 | if (in->find(*find) != string::npos) |
| 78 | AppendString(*find, s); |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 79 | } |
| 80 | |
Shinichiro Hamaji | 00cc658 | 2015-06-17 18:12:46 +0900 | [diff] [blame] | 81 | void FilterFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 82 | shared_ptr<string> pat_buf = args[0]->Eval(ev); |
| 83 | shared_ptr<string> text = args[1]->Eval(ev); |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 84 | vector<Pattern> pats; |
| 85 | for (StringPiece pat : WordScanner(*pat_buf)) { |
| 86 | pats.push_back(Pattern(pat)); |
| 87 | } |
Shinichiro Hamaji | 00cc658 | 2015-06-17 18:12:46 +0900 | [diff] [blame] | 88 | WordWriter ww(s); |
| 89 | for (StringPiece tok : WordScanner(*text)) { |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 90 | for (const Pattern& pat : pats) { |
| 91 | if (pat.Match(tok)) { |
Shinichiro Hamaji | 00cc658 | 2015-06-17 18:12:46 +0900 | [diff] [blame] | 92 | ww.Write(tok); |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void FilterOutFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 100 | shared_ptr<string> pat_buf = args[0]->Eval(ev); |
| 101 | shared_ptr<string> text = args[1]->Eval(ev); |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 102 | vector<Pattern> pats; |
| 103 | for (StringPiece pat : WordScanner(*pat_buf)) { |
| 104 | pats.push_back(Pattern(pat)); |
| 105 | } |
Shinichiro Hamaji | 00cc658 | 2015-06-17 18:12:46 +0900 | [diff] [blame] | 106 | WordWriter ww(s); |
| 107 | for (StringPiece tok : WordScanner(*text)) { |
| 108 | bool matched = false; |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 109 | for (const Pattern& pat : pats) { |
| 110 | if (pat.Match(tok)) { |
Shinichiro Hamaji | 00cc658 | 2015-06-17 18:12:46 +0900 | [diff] [blame] | 111 | matched = true; |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | if (!matched) |
| 116 | ww.Write(tok); |
| 117 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 118 | } |
| 119 | |
Shinichiro Hamaji | d87e59e | 2015-06-17 18:18:34 +0900 | [diff] [blame] | 120 | void SortFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 121 | shared_ptr<string> list = args[0]->Eval(ev); |
| 122 | vector<StringPiece> toks; |
| 123 | WordScanner(*list).Split(&toks); |
| 124 | sort(toks.begin(), toks.end()); |
| 125 | WordWriter ww(s); |
| 126 | StringPiece prev; |
| 127 | for (StringPiece tok : toks) { |
| 128 | if (prev != tok) { |
| 129 | ww.Write(tok); |
| 130 | prev = tok; |
| 131 | } |
| 132 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 133 | } |
| 134 | |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 135 | static int GetNumericValueForFunc(const string& buf) { |
| 136 | StringPiece s = TrimLeftSpace(buf); |
| 137 | char* end; |
| 138 | long n = strtol(s.data(), &end, 10); |
| 139 | if (n < 0 || n == LONG_MAX || s.data() + s.size() != end) { |
| 140 | return -1; |
| 141 | } |
| 142 | return n; |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 143 | } |
| 144 | |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 145 | void WordFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 146 | shared_ptr<string> n_str = args[0]->Eval(ev); |
| 147 | int n = GetNumericValueForFunc(*n_str); |
| 148 | if (n < 0) { |
| 149 | ev->Error(StringPrintf( |
| 150 | "*** non-numeric first argument to `word' function: '%s'.", |
| 151 | n_str->c_str())); |
| 152 | } |
| 153 | if (n == 0) { |
| 154 | ev->Error("*** first argument to `word' function must be greater than 0."); |
| 155 | } |
| 156 | |
| 157 | shared_ptr<string> text = args[1]->Eval(ev); |
| 158 | for (StringPiece tok : WordScanner(*text)) { |
| 159 | n--; |
| 160 | if (n == 0) { |
| 161 | AppendString(tok, s); |
| 162 | break; |
| 163 | } |
| 164 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 165 | } |
| 166 | |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 167 | void WordlistFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 168 | shared_ptr<string> s_str = args[0]->Eval(ev); |
| 169 | int si = GetNumericValueForFunc(*s_str); |
| 170 | if (si < 0) { |
| 171 | ev->Error(StringPrintf( |
| 172 | "*** non-numeric first argument to `wordlist' function: '%s'.", |
| 173 | s_str->c_str())); |
| 174 | } |
| 175 | if (si == 0) { |
| 176 | ev->Error(StringPrintf( |
| 177 | "*** invalid first argument to `wordlist' function: %s`", |
| 178 | s_str->c_str())); |
| 179 | } |
| 180 | |
| 181 | shared_ptr<string> e_str = args[1]->Eval(ev); |
| 182 | int ei = GetNumericValueForFunc(*e_str); |
| 183 | if (ei < 0) { |
| 184 | ev->Error(StringPrintf( |
| 185 | "*** non-numeric second argument to `wordlist' function: '%s'.", |
| 186 | e_str->c_str())); |
| 187 | } |
| 188 | |
| 189 | shared_ptr<string> text = args[2]->Eval(ev); |
| 190 | int i = 0; |
| 191 | WordWriter ww(s); |
| 192 | for (StringPiece tok : WordScanner(*text)) { |
| 193 | i++; |
| 194 | if (si <= i && i <= ei) { |
| 195 | ww.Write(tok); |
| 196 | } |
| 197 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 198 | } |
| 199 | |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 200 | void WordsFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 201 | shared_ptr<string> text = args[0]->Eval(ev); |
| 202 | WordScanner ws(*text); |
| 203 | int n = 0; |
| 204 | for (auto iter = ws.begin(); iter != ws.end(); ++iter) |
| 205 | n++; |
| 206 | char buf[32]; |
| 207 | sprintf(buf, "%d", n); |
| 208 | *s += buf; |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 209 | } |
| 210 | |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 211 | void FirstwordFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 212 | shared_ptr<string> text = args[0]->Eval(ev); |
| 213 | for (StringPiece tok : WordScanner(*text)) { |
| 214 | AppendString(tok, s); |
| 215 | return; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | void LastwordFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 220 | shared_ptr<string> text = args[0]->Eval(ev); |
| 221 | StringPiece last; |
| 222 | for (StringPiece tok : WordScanner(*text)) { |
| 223 | last = tok; |
| 224 | } |
| 225 | AppendString(last, s); |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 226 | } |
| 227 | |
Shinichiro Hamaji | 30b8e60 | 2015-06-17 19:28:54 +0900 | [diff] [blame] | 228 | void JoinFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 229 | shared_ptr<string> list1 = args[0]->Eval(ev); |
| 230 | shared_ptr<string> list2 = args[1]->Eval(ev); |
| 231 | WordScanner ws1(*list1); |
| 232 | WordScanner ws2(*list2); |
| 233 | WordWriter ww(s); |
| 234 | for (WordScanner::Iterator iter1 = ws1.begin(), iter2 = ws2.begin(); |
| 235 | iter1 != ws1.end() && iter2 != ws2.end(); |
| 236 | ++iter1, ++iter2) { |
| 237 | ww.Write(*iter1); |
| 238 | // Use |AppendString| not to append extra ' '. |
| 239 | AppendString(*iter2, s); |
| 240 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 241 | } |
| 242 | |
Shinichiro Hamaji | fcf1b76 | 2015-06-18 03:43:54 +0900 | [diff] [blame] | 243 | void WildcardFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | 284f3d1 | 2015-06-17 19:29:01 +0900 | [diff] [blame] | 244 | shared_ptr<string> pat = args[0]->Eval(ev); |
Shinichiro Hamaji | fcf1b76 | 2015-06-18 03:43:54 +0900 | [diff] [blame] | 245 | WordWriter ww(s); |
Shinichiro Hamaji | 284f3d1 | 2015-06-17 19:29:01 +0900 | [diff] [blame] | 246 | for (StringPiece tok : WordScanner(*pat)) { |
Shinichiro Hamaji | 8f68bd3 | 2015-06-18 11:01:51 +0900 | [diff] [blame] | 247 | ScopedTerminator st(tok); |
Shinichiro Hamaji | fcf1b76 | 2015-06-18 03:43:54 +0900 | [diff] [blame] | 248 | // TODO: Make this faster by not always using glob. |
| 249 | glob_t gl; |
| 250 | glob(tok.data(), GLOB_NOSORT, NULL, &gl); |
| 251 | for (size_t i = 0; i < gl.gl_pathc; i++) { |
| 252 | ww.Write(gl.gl_pathv[i]); |
| 253 | } |
| 254 | globfree(&gl); |
Shinichiro Hamaji | 284f3d1 | 2015-06-17 19:29:01 +0900 | [diff] [blame] | 255 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 256 | } |
| 257 | |
Shinichiro Hamaji | 67f9a70 | 2015-06-18 06:00:57 +0900 | [diff] [blame] | 258 | void DirFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 259 | shared_ptr<string> text = args[0]->Eval(ev); |
| 260 | WordWriter ww(s); |
| 261 | for (StringPiece tok : WordScanner(*text)) { |
| 262 | ww.Write(Dirname(tok)); |
| 263 | if (tok != "/") |
| 264 | s->push_back('/'); |
| 265 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 266 | } |
| 267 | |
Shinichiro Hamaji | 67f9a70 | 2015-06-18 06:00:57 +0900 | [diff] [blame] | 268 | void NotdirFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 269 | shared_ptr<string> text = args[0]->Eval(ev); |
| 270 | WordWriter ww(s); |
| 271 | for (StringPiece tok : WordScanner(*text)) { |
| 272 | if (tok == "/") { |
| 273 | ww.Write(STRING_PIECE("")); |
| 274 | } else { |
| 275 | ww.Write(Basename(tok)); |
| 276 | } |
| 277 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 278 | } |
| 279 | |
Shinichiro Hamaji | 67f9a70 | 2015-06-18 06:00:57 +0900 | [diff] [blame] | 280 | void SuffixFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 281 | shared_ptr<string> text = args[0]->Eval(ev); |
| 282 | WordWriter ww(s); |
| 283 | for (StringPiece tok : WordScanner(*text)) { |
| 284 | StringPiece suf = GetExt(tok); |
| 285 | if (!suf.empty()) |
| 286 | ww.Write(suf); |
| 287 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 288 | } |
| 289 | |
Shinichiro Hamaji | 67f9a70 | 2015-06-18 06:00:57 +0900 | [diff] [blame] | 290 | void BasenameFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 291 | shared_ptr<string> text = args[0]->Eval(ev); |
| 292 | WordWriter ww(s); |
| 293 | for (StringPiece tok : WordScanner(*text)) { |
| 294 | ww.Write(StripExt(tok)); |
| 295 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 296 | } |
| 297 | |
Shinichiro Hamaji | 5d694f0 | 2015-06-18 06:05:36 +0900 | [diff] [blame] | 298 | void AddsuffixFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 299 | shared_ptr<string> suf = args[0]->Eval(ev); |
| 300 | shared_ptr<string> text = args[1]->Eval(ev); |
| 301 | WordWriter ww(s); |
| 302 | for (StringPiece tok : WordScanner(*text)) { |
| 303 | ww.Write(tok); |
| 304 | *s += *suf; |
| 305 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 306 | } |
| 307 | |
Shinichiro Hamaji | 5d694f0 | 2015-06-18 06:05:36 +0900 | [diff] [blame] | 308 | void AddprefixFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 309 | shared_ptr<string> pre = args[0]->Eval(ev); |
| 310 | shared_ptr<string> text = args[1]->Eval(ev); |
| 311 | WordWriter ww(s); |
| 312 | for (StringPiece tok : WordScanner(*text)) { |
| 313 | ww.Write(*pre); |
| 314 | AppendString(tok, s); |
| 315 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 316 | } |
| 317 | |
Shinichiro Hamaji | 8a96358 | 2015-06-18 07:05:58 +0900 | [diff] [blame] | 318 | void RealpathFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 319 | shared_ptr<string> text = args[0]->Eval(ev); |
| 320 | WordWriter ww(s); |
| 321 | for (StringPiece tok : WordScanner(*text)) { |
Shinichiro Hamaji | 8f68bd3 | 2015-06-18 11:01:51 +0900 | [diff] [blame] | 322 | ScopedTerminator st(tok); |
Shinichiro Hamaji | 8a96358 | 2015-06-18 07:05:58 +0900 | [diff] [blame] | 323 | char buf[PATH_MAX]; |
| 324 | if (realpath(tok.data(), buf)) |
| 325 | *s += buf; |
Shinichiro Hamaji | 8a96358 | 2015-06-18 07:05:58 +0900 | [diff] [blame] | 326 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 327 | } |
| 328 | |
Shinichiro Hamaji | 8a96358 | 2015-06-18 07:05:58 +0900 | [diff] [blame] | 329 | void AbspathFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 330 | shared_ptr<string> text = args[0]->Eval(ev); |
| 331 | WordWriter ww(s); |
| 332 | string buf; |
| 333 | for (StringPiece tok : WordScanner(*text)) { |
| 334 | AbsPath(tok, &buf); |
| 335 | ww.Write(buf); |
| 336 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 337 | } |
| 338 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 339 | void IfFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 340 | shared_ptr<string> cond = args[0]->Eval(ev); |
| 341 | if (cond->empty()) { |
| 342 | if (args.size() > 2) |
| 343 | args[2]->Eval(ev, s); |
| 344 | } else { |
| 345 | args[1]->Eval(ev, s); |
| 346 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 347 | } |
| 348 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 349 | void AndFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 350 | shared_ptr<string> cond; |
| 351 | for (Value* a : args) { |
| 352 | cond = a->Eval(ev); |
| 353 | if (cond->empty()) |
| 354 | return; |
| 355 | } |
| 356 | if (cond.get()) { |
| 357 | *s += *cond; |
| 358 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 359 | } |
| 360 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 361 | void OrFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 362 | for (Value* a : args) { |
| 363 | shared_ptr<string> cond = a->Eval(ev); |
| 364 | if (!cond->empty()) { |
| 365 | *s += *cond; |
| 366 | return; |
| 367 | } |
| 368 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 369 | } |
| 370 | |
Shinichiro Hamaji | 81699be | 2015-06-22 18:07:38 +0900 | [diff] [blame] | 371 | void ValueFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 372 | shared_ptr<string> var_name = args[0]->Eval(ev); |
| 373 | Var* var = ev->LookupVar(*var_name); |
| 374 | AppendString(var->String().as_string(), s); |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 375 | } |
| 376 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 377 | void EvalFunc(const vector<Value*>& args, Evaluator* ev, string*) { |
Shinichiro Hamaji | 76ff983 | 2015-06-18 17:11:22 +0900 | [diff] [blame] | 378 | // TODO: eval leaks everything... for now. |
| 379 | //shared_ptr<string> text = args[0]->Eval(ev); |
| 380 | string* text = new string; |
| 381 | args[0]->Eval(ev, text); |
Shinichiro Hamaji | 80456fb | 2015-06-18 14:56:10 +0900 | [diff] [blame] | 382 | vector<AST*> asts; |
| 383 | Parse(*text, ev->loc(), &asts); |
| 384 | for (AST* ast : asts) { |
| 385 | LOG("%s", ast->DebugString().c_str()); |
| 386 | ast->Eval(ev); |
Shinichiro Hamaji | 76ff983 | 2015-06-18 17:11:22 +0900 | [diff] [blame] | 387 | //delete ast; |
Shinichiro Hamaji | 80456fb | 2015-06-18 14:56:10 +0900 | [diff] [blame] | 388 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 389 | } |
| 390 | |
Shinichiro Hamaji | fcf1b76 | 2015-06-18 03:43:54 +0900 | [diff] [blame] | 391 | void ShellFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 392 | shared_ptr<string> cmd = args[0]->Eval(ev); |
| 393 | LOG("ShellFunc: %s", cmd->c_str()); |
| 394 | string out; |
| 395 | // TODO: Handle $(SHELL). |
| 396 | FILE* fp = popen(cmd->c_str(), "r"); |
| 397 | while (true) { |
| 398 | char buf[4096]; |
| 399 | size_t r = fread(buf, 1, 4096, fp); |
| 400 | out.append(buf, buf+r); |
| 401 | if (r == 0) { |
| 402 | fclose(fp); |
| 403 | break; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | while (out[out.size()-1] == '\n') |
| 408 | out.pop_back(); |
| 409 | for (size_t i = 0; i < out.size(); i++) { |
| 410 | if (out[i] == '\n') |
| 411 | out[i] = ' '; |
| 412 | } |
| 413 | *s += out; |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 414 | } |
| 415 | |
Shinichiro Hamaji | 3064f1f | 2015-06-18 16:32:09 +0900 | [diff] [blame] | 416 | void CallFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 417 | static const char* tmpvar_names[] = { |
| 418 | "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" |
| 419 | }; |
| 420 | |
| 421 | shared_ptr<string> func_name = args[0]->Eval(ev); |
| 422 | Var* func = ev->LookupVar(*func_name); |
| 423 | vector<unique_ptr<SimpleVar>> av; |
Shinichiro Hamaji | 3064f1f | 2015-06-18 16:32:09 +0900 | [diff] [blame] | 424 | for (size_t i = 1; i < args.size(); i++) { |
Shinichiro Hamaji | f62e9a7 | 2015-06-26 04:18:21 +0900 | [diff] [blame] | 425 | unique_ptr<SimpleVar> s( |
| 426 | new SimpleVar(args[i]->Eval(ev), VarOrigin::AUTOMATIC)); |
Shinichiro Hamaji | 3064f1f | 2015-06-18 16:32:09 +0900 | [diff] [blame] | 427 | av.push_back(move(s)); |
| 428 | } |
Shinichiro Hamaji | 5d53bc7 | 2015-06-26 08:31:54 +0900 | [diff] [blame^] | 429 | vector<unique_ptr<ScopedVar>> sv; |
| 430 | for (size_t i = 1; i < args.size(); i++) { |
| 431 | sv.push_back(move(unique_ptr<ScopedVar>( |
| 432 | new ScopedVar(ev->mutable_vars(), tmpvar_names[i], av[i-1].get())))); |
| 433 | } |
Shinichiro Hamaji | 3064f1f | 2015-06-18 16:32:09 +0900 | [diff] [blame] | 434 | func->Eval(ev, s); |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 435 | } |
| 436 | |
Shinichiro Hamaji | cf0cd68 | 2015-06-18 16:18:13 +0900 | [diff] [blame] | 437 | void ForeachFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 438 | shared_ptr<string> varname = args[0]->Eval(ev); |
| 439 | shared_ptr<string> list = args[1]->Eval(ev); |
| 440 | WordWriter ww(s); |
| 441 | for (StringPiece tok : WordScanner(*list)) { |
| 442 | unique_ptr<SimpleVar> v(new SimpleVar( |
Shinichiro Hamaji | f62e9a7 | 2015-06-26 04:18:21 +0900 | [diff] [blame] | 443 | make_shared<string>(tok.data(), tok.size()), VarOrigin::AUTOMATIC)); |
Shinichiro Hamaji | cf0cd68 | 2015-06-18 16:18:13 +0900 | [diff] [blame] | 444 | ScopedVar sv(ev->mutable_vars(), *varname, v.get()); |
| 445 | ww.MaybeAddWhitespace(); |
| 446 | args[2]->Eval(ev, s); |
| 447 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 448 | } |
| 449 | |
Shinichiro Hamaji | c22fdb4 | 2015-06-22 18:10:40 +0900 | [diff] [blame] | 450 | void OriginFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 451 | shared_ptr<string> var_name = args[0]->Eval(ev); |
| 452 | Var* var = ev->LookupVar(*var_name); |
Shinichiro Hamaji | f62e9a7 | 2015-06-26 04:18:21 +0900 | [diff] [blame] | 453 | *s += GetOriginStr(var->Origin()); |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 454 | } |
| 455 | |
Shinichiro Hamaji | c22fdb4 | 2015-06-22 18:10:40 +0900 | [diff] [blame] | 456 | void FlavorFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 457 | shared_ptr<string> var_name = args[0]->Eval(ev); |
| 458 | Var* var = ev->LookupVar(*var_name); |
| 459 | *s += var->Flavor(); |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 460 | } |
| 461 | |
Shinichiro Hamaji | 2e6cbfc | 2015-06-16 18:46:50 +0900 | [diff] [blame] | 462 | void InfoFunc(const vector<Value*>& args, Evaluator* ev, string*) { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 463 | shared_ptr<string> a = args[0]->Eval(ev); |
| 464 | printf("%s\n", a->c_str()); |
| 465 | fflush(stdout); |
| 466 | } |
| 467 | |
Shinichiro Hamaji | 2e6cbfc | 2015-06-16 18:46:50 +0900 | [diff] [blame] | 468 | void WarningFunc(const vector<Value*>& args, Evaluator* ev, string*) { |
Shinichiro Hamaji | 9619b36 | 2015-06-16 16:13:25 +0900 | [diff] [blame] | 469 | shared_ptr<string> a = args[0]->Eval(ev); |
Shinichiro Hamaji | 8ee8c37 | 2015-06-16 16:19:40 +0900 | [diff] [blame] | 470 | printf("%s:%d: %s\n", LOCF(ev->loc()), a->c_str()); |
Shinichiro Hamaji | 9619b36 | 2015-06-16 16:13:25 +0900 | [diff] [blame] | 471 | fflush(stdout); |
| 472 | } |
| 473 | |
Shinichiro Hamaji | 2e6cbfc | 2015-06-16 18:46:50 +0900 | [diff] [blame] | 474 | void ErrorFunc(const vector<Value*>& args, Evaluator* ev, string*) { |
Shinichiro Hamaji | 9619b36 | 2015-06-16 16:13:25 +0900 | [diff] [blame] | 475 | shared_ptr<string> a = args[0]->Eval(ev); |
| 476 | ev->Error(StringPrintf("*** %s.", a->c_str())); |
| 477 | } |
| 478 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 479 | FuncInfo g_func_infos[] = { |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 480 | { "patsubst", &PatsubstFunc, 3, 3, false, false }, |
| 481 | { "strip", &StripFunc, 1, 1, false, false }, |
| 482 | { "subst", &SubstFunc, 3, 3, false, false }, |
| 483 | { "findstring", &FindstringFunc, 2, 2, false, false }, |
| 484 | { "filter", &FilterFunc, 2, 2, false, false }, |
| 485 | { "filter-out", &FilterOutFunc, 2, 2, false, false }, |
| 486 | { "sort", &SortFunc, 1, 1, false, false }, |
| 487 | { "word", &WordFunc, 2, 2, false, false }, |
| 488 | { "wordlist", &WordlistFunc, 3, 3, false, false }, |
| 489 | { "words", &WordsFunc, 1, 1, false, false }, |
| 490 | { "firstword", &FirstwordFunc, 1, 1, false, false }, |
| 491 | { "lastword", &LastwordFunc, 1, 1, false, false }, |
Shinichiro Hamaji | 2e6cbfc | 2015-06-16 18:46:50 +0900 | [diff] [blame] | 492 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 493 | { "join", &JoinFunc, 2, 2, false, false }, |
| 494 | { "wildcard", &WildcardFunc, 1, 1, false, false }, |
| 495 | { "dir", &DirFunc, 1, 1, false, false }, |
| 496 | { "notdir", &NotdirFunc, 1, 1, false, false }, |
| 497 | { "suffix", &SuffixFunc, 1, 1, false, false }, |
| 498 | { "basename", &BasenameFunc, 1, 1, false, false }, |
| 499 | { "addsuffix", &AddsuffixFunc, 2, 2, false, false }, |
| 500 | { "addprefix", &AddprefixFunc, 2, 2, false, false }, |
| 501 | { "realpath", &RealpathFunc, 1, 1, false, false }, |
| 502 | { "abspath", &AbspathFunc, 1, 1, false, false }, |
Shinichiro Hamaji | 30b8e60 | 2015-06-17 19:28:54 +0900 | [diff] [blame] | 503 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 504 | { "if", &IfFunc, 3, 2, false, true }, |
| 505 | { "and", &AndFunc, 0, 0, true, false }, |
| 506 | { "or", &OrFunc, 0, 0, true, false }, |
Shinichiro Hamaji | e22fe8e | 2015-06-18 07:11:54 +0900 | [diff] [blame] | 507 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 508 | { "value", &ValueFunc, 1, 1, false, false }, |
| 509 | { "eval", &EvalFunc, 1, 1, false, false }, |
| 510 | { "shell", &ShellFunc, 1, 1, false, false }, |
| 511 | { "call", &CallFunc, 0, 0, false, false }, |
| 512 | { "foreach", &ForeachFunc, 3, 3, false, false }, |
Shinichiro Hamaji | e22fe8e | 2015-06-18 07:11:54 +0900 | [diff] [blame] | 513 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 514 | { "origin", &OriginFunc, 1, 1, false, false }, |
| 515 | { "flavor", &FlavorFunc, 1, 1, false, false }, |
Shinichiro Hamaji | e22fe8e | 2015-06-18 07:11:54 +0900 | [diff] [blame] | 516 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 517 | { "info", &InfoFunc, 1, 1, false, false }, |
| 518 | { "warning", &WarningFunc, 1, 1, false, false }, |
| 519 | { "error", &ErrorFunc, 1, 1, false, false }, |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 520 | }; |
| 521 | |
| 522 | unordered_map<StringPiece, FuncInfo*>* g_func_info_map; |
| 523 | |
| 524 | } // namespace |
| 525 | |
| 526 | void InitFuncTable() { |
| 527 | g_func_info_map = new unordered_map<StringPiece, FuncInfo*>; |
| 528 | for (size_t i = 0; i < sizeof(g_func_infos) / sizeof(g_func_infos[0]); i++) { |
| 529 | FuncInfo* fi = &g_func_infos[i]; |
| 530 | bool ok = g_func_info_map->insert(make_pair(Intern(fi->name), fi)).second; |
| 531 | CHECK(ok); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | void QuitFuncTable() { |
| 536 | delete g_func_info_map; |
| 537 | } |
| 538 | |
| 539 | FuncInfo* GetFuncInfo(StringPiece name) { |
| 540 | auto found = g_func_info_map->find(name); |
| 541 | if (found == g_func_info_map->end()) |
| 542 | return NULL; |
| 543 | return found->second; |
| 544 | } |