blob: b8b8d7e5a190f6878373ab0dd77c8931842584c8 [file] [log] [blame]
Shinichiro Hamaji1d545aa2015-06-23 15:29:13 +09001// 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 Ukai744bb2b2015-06-25 00:10:52 +090015// +build ignore
16
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090017#include "func.h"
18
Shinichiro Hamaji62b16e72015-07-02 01:42:31 +090019#include <errno.h>
Shinichiro Hamajid5271452015-06-17 18:50:03 +090020#include <limits.h>
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090021#include <stdio.h>
Shinichiro Hamajid5271452015-06-17 18:50:03 +090022#include <stdlib.h>
Shinichiro Hamaji62b16e72015-07-02 01:42:31 +090023#include <unistd.h>
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090024
Shinichiro Hamajid87e59e2015-06-17 18:18:34 +090025#include <algorithm>
Shinichiro Hamajid5271452015-06-17 18:50:03 +090026#include <iterator>
Shinichiro Hamajicf0cd682015-06-18 16:18:13 +090027#include <memory>
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090028#include <unordered_map>
29
Shinichiro Hamaji80456fb2015-06-18 14:56:10 +090030#include "ast.h"
Shinichiro Hamaji9619b362015-06-16 16:13:25 +090031#include "eval.h"
Shinichiro Hamaji94d6f2a2015-07-05 05:32:25 +090032#include "fileutil.h"
Shinichiro Hamaji5f57a992015-06-30 19:39:39 +090033#include "find.h"
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090034#include "log.h"
Shinichiro Hamaji80456fb2015-06-18 14:56:10 +090035#include "parser.h"
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090036#include "stats.h"
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090037#include "strutil.h"
Shinichiro Hamajie7992752015-06-29 18:38:35 +090038#include "symtab.h"
Shinichiro Hamajicf0cd682015-06-18 16:18:13 +090039#include "var.h"
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090040
41namespace {
42
Shinichiro Hamaji2e6cbfc2015-06-16 18:46:50 +090043void PatsubstFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamajia6a17a42015-06-18 20:11:19 +090044 shared_ptr<string> pat_str = args[0]->Eval(ev);
Shinichiro Hamaji2e6cbfc2015-06-16 18:46:50 +090045 shared_ptr<string> repl = args[1]->Eval(ev);
46 shared_ptr<string> str = args[2]->Eval(ev);
Shinichiro Hamaji37591ce2015-06-16 19:36:05 +090047 WordWriter ww(s);
Shinichiro Hamajia6a17a42015-06-18 20:11:19 +090048 Pattern pat(*pat_str);
Shinichiro Hamaji2e6cbfc2015-06-16 18:46:50 +090049 for (StringPiece tok : WordScanner(*str)) {
Shinichiro Hamaji37591ce2015-06-16 19:36:05 +090050 ww.MaybeAddWhitespace();
Shinichiro Hamajia6a17a42015-06-18 20:11:19 +090051 pat.AppendSubst(tok, *repl, s);
Shinichiro Hamaji2e6cbfc2015-06-16 18:46:50 +090052 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +090053}
54
Shinichiro Hamaji37591ce2015-06-16 19:36:05 +090055void StripFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
56 shared_ptr<string> str = args[0]->Eval(ev);
57 WordWriter ww(s);
58 for (StringPiece tok : WordScanner(*str)) {
59 ww.Write(tok);
60 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +090061}
62
Shinichiro Hamaji37591ce2015-06-16 19:36:05 +090063void SubstFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
64 shared_ptr<string> pat = args[0]->Eval(ev);
65 shared_ptr<string> repl = args[1]->Eval(ev);
66 shared_ptr<string> str = args[2]->Eval(ev);
Shinichiro Hamaji5af931d2015-07-06 15:37:59 +090067 if (pat->empty()) {
68 *s += *str;
69 *s += *repl;
70 return;
71 }
Shinichiro Hamaji37591ce2015-06-16 19:36:05 +090072 size_t index = 0;
73 while (index < str->size()) {
74 size_t found = str->find(*pat, index);
75 if (found == string::npos)
76 break;
77 AppendString(StringPiece(*str).substr(index, found - index), s);
78 AppendString(*repl, s);
79 index = found + pat->size();
80 }
81 AppendString(StringPiece(*str).substr(index), s);
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +090082}
83
Shinichiro Hamaji00cc6582015-06-17 18:12:46 +090084void FindstringFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
85 shared_ptr<string> find = args[0]->Eval(ev);
86 shared_ptr<string> in = args[1]->Eval(ev);
87 if (in->find(*find) != string::npos)
88 AppendString(*find, s);
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +090089}
90
Shinichiro Hamaji00cc6582015-06-17 18:12:46 +090091void FilterFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
92 shared_ptr<string> pat_buf = args[0]->Eval(ev);
93 shared_ptr<string> text = args[1]->Eval(ev);
Shinichiro Hamajia6a17a42015-06-18 20:11:19 +090094 vector<Pattern> pats;
95 for (StringPiece pat : WordScanner(*pat_buf)) {
96 pats.push_back(Pattern(pat));
97 }
Shinichiro Hamaji00cc6582015-06-17 18:12:46 +090098 WordWriter ww(s);
99 for (StringPiece tok : WordScanner(*text)) {
Shinichiro Hamajia6a17a42015-06-18 20:11:19 +0900100 for (const Pattern& pat : pats) {
101 if (pat.Match(tok)) {
Shinichiro Hamaji00cc6582015-06-17 18:12:46 +0900102 ww.Write(tok);
103 break;
104 }
105 }
106 }
107}
108
109void FilterOutFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
110 shared_ptr<string> pat_buf = args[0]->Eval(ev);
111 shared_ptr<string> text = args[1]->Eval(ev);
Shinichiro Hamajia6a17a42015-06-18 20:11:19 +0900112 vector<Pattern> pats;
113 for (StringPiece pat : WordScanner(*pat_buf)) {
114 pats.push_back(Pattern(pat));
115 }
Shinichiro Hamaji00cc6582015-06-17 18:12:46 +0900116 WordWriter ww(s);
117 for (StringPiece tok : WordScanner(*text)) {
118 bool matched = false;
Shinichiro Hamajia6a17a42015-06-18 20:11:19 +0900119 for (const Pattern& pat : pats) {
120 if (pat.Match(tok)) {
Shinichiro Hamaji00cc6582015-06-17 18:12:46 +0900121 matched = true;
122 break;
123 }
124 }
125 if (!matched)
126 ww.Write(tok);
127 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900128}
129
Shinichiro Hamajid87e59e2015-06-17 18:18:34 +0900130void SortFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
131 shared_ptr<string> list = args[0]->Eval(ev);
132 vector<StringPiece> toks;
133 WordScanner(*list).Split(&toks);
134 sort(toks.begin(), toks.end());
135 WordWriter ww(s);
136 StringPiece prev;
137 for (StringPiece tok : toks) {
138 if (prev != tok) {
139 ww.Write(tok);
140 prev = tok;
141 }
142 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900143}
144
Shinichiro Hamajid5271452015-06-17 18:50:03 +0900145static int GetNumericValueForFunc(const string& buf) {
146 StringPiece s = TrimLeftSpace(buf);
147 char* end;
148 long n = strtol(s.data(), &end, 10);
149 if (n < 0 || n == LONG_MAX || s.data() + s.size() != end) {
150 return -1;
151 }
152 return n;
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900153}
154
Shinichiro Hamajid5271452015-06-17 18:50:03 +0900155void WordFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
156 shared_ptr<string> n_str = args[0]->Eval(ev);
157 int n = GetNumericValueForFunc(*n_str);
158 if (n < 0) {
159 ev->Error(StringPrintf(
160 "*** non-numeric first argument to `word' function: '%s'.",
161 n_str->c_str()));
162 }
163 if (n == 0) {
164 ev->Error("*** first argument to `word' function must be greater than 0.");
165 }
166
167 shared_ptr<string> text = args[1]->Eval(ev);
168 for (StringPiece tok : WordScanner(*text)) {
169 n--;
170 if (n == 0) {
171 AppendString(tok, s);
172 break;
173 }
174 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900175}
176
Shinichiro Hamajid5271452015-06-17 18:50:03 +0900177void WordlistFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
178 shared_ptr<string> s_str = args[0]->Eval(ev);
179 int si = GetNumericValueForFunc(*s_str);
180 if (si < 0) {
181 ev->Error(StringPrintf(
182 "*** non-numeric first argument to `wordlist' function: '%s'.",
183 s_str->c_str()));
184 }
185 if (si == 0) {
186 ev->Error(StringPrintf(
187 "*** invalid first argument to `wordlist' function: %s`",
188 s_str->c_str()));
189 }
190
191 shared_ptr<string> e_str = args[1]->Eval(ev);
192 int ei = GetNumericValueForFunc(*e_str);
193 if (ei < 0) {
194 ev->Error(StringPrintf(
195 "*** non-numeric second argument to `wordlist' function: '%s'.",
196 e_str->c_str()));
197 }
198
199 shared_ptr<string> text = args[2]->Eval(ev);
200 int i = 0;
201 WordWriter ww(s);
202 for (StringPiece tok : WordScanner(*text)) {
203 i++;
204 if (si <= i && i <= ei) {
205 ww.Write(tok);
206 }
207 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900208}
209
Shinichiro Hamajid5271452015-06-17 18:50:03 +0900210void WordsFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
211 shared_ptr<string> text = args[0]->Eval(ev);
212 WordScanner ws(*text);
213 int n = 0;
214 for (auto iter = ws.begin(); iter != ws.end(); ++iter)
215 n++;
216 char buf[32];
217 sprintf(buf, "%d", n);
218 *s += buf;
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900219}
220
Shinichiro Hamajid5271452015-06-17 18:50:03 +0900221void FirstwordFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
222 shared_ptr<string> text = args[0]->Eval(ev);
223 for (StringPiece tok : WordScanner(*text)) {
224 AppendString(tok, s);
225 return;
226 }
227}
228
229void LastwordFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
230 shared_ptr<string> text = args[0]->Eval(ev);
231 StringPiece last;
232 for (StringPiece tok : WordScanner(*text)) {
233 last = tok;
234 }
235 AppendString(last, s);
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900236}
237
Shinichiro Hamaji30b8e602015-06-17 19:28:54 +0900238void JoinFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
239 shared_ptr<string> list1 = args[0]->Eval(ev);
240 shared_ptr<string> list2 = args[1]->Eval(ev);
241 WordScanner ws1(*list1);
242 WordScanner ws2(*list2);
243 WordWriter ww(s);
244 for (WordScanner::Iterator iter1 = ws1.begin(), iter2 = ws2.begin();
245 iter1 != ws1.end() && iter2 != ws2.end();
246 ++iter1, ++iter2) {
247 ww.Write(*iter1);
248 // Use |AppendString| not to append extra ' '.
249 AppendString(*iter2, s);
250 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900251}
252
Shinichiro Hamajifcf1b762015-06-18 03:43:54 +0900253void WildcardFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +0900254 COLLECT_STATS("func wildcard time");
Shinichiro Hamaji284f3d12015-06-17 19:29:01 +0900255 shared_ptr<string> pat = args[0]->Eval(ev);
Shinichiro Hamajidf1fc8b2015-06-29 15:45:21 +0900256 if (ev->avoid_io()) {
257 *s += "$(/bin/ls -d ";
258 *s += *pat;
259 *s += " 2> /dev/null)";
260 return;
261 }
262
Shinichiro Hamajifcf1b762015-06-18 03:43:54 +0900263 WordWriter ww(s);
Shinichiro Hamaji0e3873a2015-07-05 15:48:28 +0900264 vector<string>* files;
Shinichiro Hamaji284f3d12015-06-17 19:29:01 +0900265 for (StringPiece tok : WordScanner(*pat)) {
Shinichiro Hamaji8f68bd32015-06-18 11:01:51 +0900266 ScopedTerminator st(tok);
Shinichiro Hamaji0e3873a2015-07-05 15:48:28 +0900267 Glob(tok.data(), &files);
268 sort(files->begin(), files->end());
269 for (const string& file : *files) {
Shinichiro Hamajife97c412015-06-29 16:57:57 +0900270 ww.Write(file);
271 }
Shinichiro Hamaji284f3d12015-06-17 19:29:01 +0900272 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900273}
274
Shinichiro Hamaji67f9a702015-06-18 06:00:57 +0900275void DirFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
276 shared_ptr<string> text = args[0]->Eval(ev);
277 WordWriter ww(s);
278 for (StringPiece tok : WordScanner(*text)) {
279 ww.Write(Dirname(tok));
Shinichiro Hamaji55906852015-06-29 16:40:33 +0900280 s->push_back('/');
Shinichiro Hamaji67f9a702015-06-18 06:00:57 +0900281 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900282}
283
Shinichiro Hamaji67f9a702015-06-18 06:00:57 +0900284void NotdirFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
285 shared_ptr<string> text = args[0]->Eval(ev);
286 WordWriter ww(s);
287 for (StringPiece tok : WordScanner(*text)) {
288 if (tok == "/") {
Shinichiro Hamaji388e8582015-07-03 16:51:46 +0900289 ww.Write(StringPiece(""));
Shinichiro Hamaji67f9a702015-06-18 06:00:57 +0900290 } else {
291 ww.Write(Basename(tok));
292 }
293 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900294}
295
Shinichiro Hamaji67f9a702015-06-18 06:00:57 +0900296void SuffixFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
297 shared_ptr<string> text = args[0]->Eval(ev);
298 WordWriter ww(s);
299 for (StringPiece tok : WordScanner(*text)) {
300 StringPiece suf = GetExt(tok);
301 if (!suf.empty())
302 ww.Write(suf);
303 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900304}
305
Shinichiro Hamaji67f9a702015-06-18 06:00:57 +0900306void BasenameFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
307 shared_ptr<string> text = args[0]->Eval(ev);
308 WordWriter ww(s);
309 for (StringPiece tok : WordScanner(*text)) {
310 ww.Write(StripExt(tok));
311 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900312}
313
Shinichiro Hamaji5d694f02015-06-18 06:05:36 +0900314void AddsuffixFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
315 shared_ptr<string> suf = args[0]->Eval(ev);
316 shared_ptr<string> text = args[1]->Eval(ev);
317 WordWriter ww(s);
318 for (StringPiece tok : WordScanner(*text)) {
319 ww.Write(tok);
320 *s += *suf;
321 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900322}
323
Shinichiro Hamaji5d694f02015-06-18 06:05:36 +0900324void AddprefixFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
325 shared_ptr<string> pre = args[0]->Eval(ev);
326 shared_ptr<string> text = args[1]->Eval(ev);
327 WordWriter ww(s);
328 for (StringPiece tok : WordScanner(*text)) {
329 ww.Write(*pre);
330 AppendString(tok, s);
331 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900332}
333
Shinichiro Hamaji8a963582015-06-18 07:05:58 +0900334void RealpathFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
335 shared_ptr<string> text = args[0]->Eval(ev);
Shinichiro Hamajidf1fc8b2015-06-29 15:45:21 +0900336 if (ev->avoid_io()) {
337 *s += "KATI_TODO(realpath)";
338 return;
339 }
340
Shinichiro Hamaji8a963582015-06-18 07:05:58 +0900341 WordWriter ww(s);
342 for (StringPiece tok : WordScanner(*text)) {
Shinichiro Hamaji8f68bd32015-06-18 11:01:51 +0900343 ScopedTerminator st(tok);
Shinichiro Hamaji8a963582015-06-18 07:05:58 +0900344 char buf[PATH_MAX];
345 if (realpath(tok.data(), buf))
346 *s += buf;
Shinichiro Hamaji8a963582015-06-18 07:05:58 +0900347 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900348}
349
Shinichiro Hamaji8a963582015-06-18 07:05:58 +0900350void AbspathFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
351 shared_ptr<string> text = args[0]->Eval(ev);
352 WordWriter ww(s);
353 string buf;
354 for (StringPiece tok : WordScanner(*text)) {
355 AbsPath(tok, &buf);
356 ww.Write(buf);
357 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900358}
359
Shinichiro Hamajifead3b72015-06-18 15:31:15 +0900360void IfFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
361 shared_ptr<string> cond = args[0]->Eval(ev);
362 if (cond->empty()) {
363 if (args.size() > 2)
364 args[2]->Eval(ev, s);
365 } else {
366 args[1]->Eval(ev, s);
367 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900368}
369
Shinichiro Hamajifead3b72015-06-18 15:31:15 +0900370void AndFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
371 shared_ptr<string> cond;
372 for (Value* a : args) {
373 cond = a->Eval(ev);
374 if (cond->empty())
375 return;
376 }
377 if (cond.get()) {
378 *s += *cond;
379 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900380}
381
Shinichiro Hamajifead3b72015-06-18 15:31:15 +0900382void OrFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
383 for (Value* a : args) {
384 shared_ptr<string> cond = a->Eval(ev);
385 if (!cond->empty()) {
386 *s += *cond;
387 return;
388 }
389 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900390}
391
Shinichiro Hamaji81699be2015-06-22 18:07:38 +0900392void ValueFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
393 shared_ptr<string> var_name = args[0]->Eval(ev);
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900394 Var* var = ev->LookupVar(Intern(*var_name));
Shinichiro Hamaji81699be2015-06-22 18:07:38 +0900395 AppendString(var->String().as_string(), s);
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900396}
397
Shinichiro Hamajifead3b72015-06-18 15:31:15 +0900398void EvalFunc(const vector<Value*>& args, Evaluator* ev, string*) {
Shinichiro Hamaji76ff9832015-06-18 17:11:22 +0900399 // TODO: eval leaks everything... for now.
400 //shared_ptr<string> text = args[0]->Eval(ev);
401 string* text = new string;
402 args[0]->Eval(ev, text);
Shinichiro Hamaji80456fb2015-06-18 14:56:10 +0900403 vector<AST*> asts;
404 Parse(*text, ev->loc(), &asts);
405 for (AST* ast : asts) {
406 LOG("%s", ast->DebugString().c_str());
407 ast->Eval(ev);
Shinichiro Hamaji76ff9832015-06-18 17:11:22 +0900408 //delete ast;
Shinichiro Hamaji80456fb2015-06-18 14:56:10 +0900409 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900410}
411
Shinichiro Hamaji5f57a992015-06-30 19:39:39 +0900412//#define TEST_FIND_EMULATOR
413
414#ifdef TEST_FIND_EMULATOR
415static string SortWordsInString(StringPiece s) {
416 vector<string> toks;
417 for (StringPiece tok : WordScanner(s)) {
418 toks.push_back(tok.as_string());
419 }
420 sort(toks.begin(), toks.end());
421 return JoinStrings(toks, " ");
422}
423#endif
424
Shinichiro Hamaji68e712b2015-07-17 06:11:08 +0900425
426// A hack for Android build. We need to evaluate things like $((3+4))
427// when we emit ninja file, because the result of such expressions
428// will be passed to other make functions.
429// TODO: Maybe we should introduce a helper binary which evaluate
430// make expressions at ninja-time.
431static bool HasNoIoInShellScript(const string& cmd) {
432 if (cmd.empty())
433 return true;
434 if (HasPrefix(cmd, "echo $((") && cmd[cmd.size()-1] == ')')
435 return true;
436 return false;
437}
438
Shinichiro Hamajifcf1b762015-06-18 03:43:54 +0900439void ShellFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
440 shared_ptr<string> cmd = args[0]->Eval(ev);
Shinichiro Hamaji68e712b2015-07-17 06:11:08 +0900441 if (ev->avoid_io() && !HasNoIoInShellScript(*cmd)) {
Shinichiro Hamajidf1fc8b2015-06-29 15:45:21 +0900442 *s += "$(";
443 *s += *cmd;
444 *s += ")";
445 return;
446 }
447
Shinichiro Hamajifcf1b762015-06-18 03:43:54 +0900448 LOG("ShellFunc: %s", cmd->c_str());
Shinichiro Hamaji5f57a992015-06-30 19:39:39 +0900449
450#ifdef TEST_FIND_EMULATOR
451 bool need_check = false;
452 string out2;
Shinichiro Hamaji4e950e62015-07-02 02:59:55 +0900453 if (FindEmulator::Get() && FindEmulator::Get()->HandleFind(*cmd, &out2)) {
Shinichiro Hamaji5f57a992015-06-30 19:39:39 +0900454 need_check = true;
455 }
456#else
Shinichiro Hamaji4e950e62015-07-02 02:59:55 +0900457 if (FindEmulator::Get() && FindEmulator::Get()->HandleFind(*cmd, s))
Shinichiro Hamaji5f57a992015-06-30 19:39:39 +0900458 return;
459#endif
460
Shinichiro Hamaji5f57a992015-06-30 19:39:39 +0900461 COLLECT_STATS_WITH_SLOW_REPORT("func shell time", cmd->c_str());
Shinichiro Hamajifcf1b762015-06-18 03:43:54 +0900462 string out;
Shinichiro Hamaji94d6f2a2015-07-05 05:32:25 +0900463 shared_ptr<string> shell = ev->EvalVar(kShellSym);
464 RunCommand(*shell, *cmd, false, &out);
Shinichiro Hamajifcf1b762015-06-18 03:43:54 +0900465
466 while (out[out.size()-1] == '\n')
467 out.pop_back();
468 for (size_t i = 0; i < out.size(); i++) {
469 if (out[i] == '\n')
470 out[i] = ' ';
471 }
Shinichiro Hamaji5f57a992015-06-30 19:39:39 +0900472
473#ifdef TEST_FIND_EMULATOR
474 if (need_check) {
475 string sorted = SortWordsInString(out);
476 out2 = SortWordsInString(out2);
477 if (sorted != out2) {
478 ERROR("FindEmulator is broken: %s\n%s\nvs\n%s",
479 cmd->c_str(), sorted.c_str(), out2.c_str());
480 }
481 }
482#endif
483
Shinichiro Hamajifcf1b762015-06-18 03:43:54 +0900484 *s += out;
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900485}
486
Shinichiro Hamaji3064f1f2015-06-18 16:32:09 +0900487void CallFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
488 static const char* tmpvar_names[] = {
489 "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
490 };
491
492 shared_ptr<string> func_name = args[0]->Eval(ev);
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900493 Var* func = ev->LookupVar(Intern(*func_name));
Shinichiro Hamaji3064f1f2015-06-18 16:32:09 +0900494 vector<unique_ptr<SimpleVar>> av;
Shinichiro Hamaji3064f1f2015-06-18 16:32:09 +0900495 for (size_t i = 1; i < args.size(); i++) {
Shinichiro Hamajif62e9a72015-06-26 04:18:21 +0900496 unique_ptr<SimpleVar> s(
497 new SimpleVar(args[i]->Eval(ev), VarOrigin::AUTOMATIC));
Shinichiro Hamaji3064f1f2015-06-18 16:32:09 +0900498 av.push_back(move(s));
499 }
Shinichiro Hamaji5d53bc72015-06-26 08:31:54 +0900500 vector<unique_ptr<ScopedVar>> sv;
501 for (size_t i = 1; i < args.size(); i++) {
502 sv.push_back(move(unique_ptr<ScopedVar>(
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900503 new ScopedVar(ev->mutable_vars(),
504 Intern(tmpvar_names[i]), av[i-1].get()))));
Shinichiro Hamaji5d53bc72015-06-26 08:31:54 +0900505 }
Shinichiro Hamaji3064f1f2015-06-18 16:32:09 +0900506 func->Eval(ev, s);
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900507}
508
Shinichiro Hamajicf0cd682015-06-18 16:18:13 +0900509void ForeachFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
510 shared_ptr<string> varname = args[0]->Eval(ev);
511 shared_ptr<string> list = args[1]->Eval(ev);
512 WordWriter ww(s);
513 for (StringPiece tok : WordScanner(*list)) {
514 unique_ptr<SimpleVar> v(new SimpleVar(
Shinichiro Hamajif62e9a72015-06-26 04:18:21 +0900515 make_shared<string>(tok.data(), tok.size()), VarOrigin::AUTOMATIC));
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900516 ScopedVar sv(ev->mutable_vars(), Intern(*varname), v.get());
Shinichiro Hamajicf0cd682015-06-18 16:18:13 +0900517 ww.MaybeAddWhitespace();
518 args[2]->Eval(ev, s);
519 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900520}
521
Shinichiro Hamajic22fdb42015-06-22 18:10:40 +0900522void OriginFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
523 shared_ptr<string> var_name = args[0]->Eval(ev);
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900524 Var* var = ev->LookupVar(Intern(*var_name));
Shinichiro Hamajif62e9a72015-06-26 04:18:21 +0900525 *s += GetOriginStr(var->Origin());
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900526}
527
Shinichiro Hamajic22fdb42015-06-22 18:10:40 +0900528void FlavorFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
529 shared_ptr<string> var_name = args[0]->Eval(ev);
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900530 Var* var = ev->LookupVar(Intern(*var_name));
Shinichiro Hamajic22fdb42015-06-22 18:10:40 +0900531 *s += var->Flavor();
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900532}
533
Shinichiro Hamajidf1fc8b2015-06-29 15:45:21 +0900534void InfoFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900535 shared_ptr<string> a = args[0]->Eval(ev);
Shinichiro Hamajidf1fc8b2015-06-29 15:45:21 +0900536 if (ev->avoid_io()) {
537 *s += "KATI_TODO(info)";
538 return;
539 }
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900540 printf("%s\n", a->c_str());
541 fflush(stdout);
542}
543
Shinichiro Hamajidf1fc8b2015-06-29 15:45:21 +0900544void WarningFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamaji9619b362015-06-16 16:13:25 +0900545 shared_ptr<string> a = args[0]->Eval(ev);
Shinichiro Hamajidf1fc8b2015-06-29 15:45:21 +0900546 if (ev->avoid_io()) {
547 *s += "KATI_TODO(warning)";
548 return;
549 }
Shinichiro Hamaji8ee8c372015-06-16 16:19:40 +0900550 printf("%s:%d: %s\n", LOCF(ev->loc()), a->c_str());
Shinichiro Hamaji9619b362015-06-16 16:13:25 +0900551 fflush(stdout);
552}
553
Shinichiro Hamajidf1fc8b2015-06-29 15:45:21 +0900554void ErrorFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamaji9619b362015-06-16 16:13:25 +0900555 shared_ptr<string> a = args[0]->Eval(ev);
Shinichiro Hamajidf1fc8b2015-06-29 15:45:21 +0900556 if (ev->avoid_io()) {
557 *s += "KATI_TODO(error)";
558 return;
559 }
Shinichiro Hamaji9619b362015-06-16 16:13:25 +0900560 ev->Error(StringPrintf("*** %s.", a->c_str()));
561}
562
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900563FuncInfo g_func_infos[] = {
Shinichiro Hamajifead3b72015-06-18 15:31:15 +0900564 { "patsubst", &PatsubstFunc, 3, 3, false, false },
565 { "strip", &StripFunc, 1, 1, false, false },
566 { "subst", &SubstFunc, 3, 3, false, false },
567 { "findstring", &FindstringFunc, 2, 2, false, false },
568 { "filter", &FilterFunc, 2, 2, false, false },
569 { "filter-out", &FilterOutFunc, 2, 2, false, false },
570 { "sort", &SortFunc, 1, 1, false, false },
571 { "word", &WordFunc, 2, 2, false, false },
572 { "wordlist", &WordlistFunc, 3, 3, false, false },
573 { "words", &WordsFunc, 1, 1, false, false },
574 { "firstword", &FirstwordFunc, 1, 1, false, false },
575 { "lastword", &LastwordFunc, 1, 1, false, false },
Shinichiro Hamaji2e6cbfc2015-06-16 18:46:50 +0900576
Shinichiro Hamajifead3b72015-06-18 15:31:15 +0900577 { "join", &JoinFunc, 2, 2, false, false },
578 { "wildcard", &WildcardFunc, 1, 1, false, false },
579 { "dir", &DirFunc, 1, 1, false, false },
580 { "notdir", &NotdirFunc, 1, 1, false, false },
581 { "suffix", &SuffixFunc, 1, 1, false, false },
582 { "basename", &BasenameFunc, 1, 1, false, false },
583 { "addsuffix", &AddsuffixFunc, 2, 2, false, false },
584 { "addprefix", &AddprefixFunc, 2, 2, false, false },
585 { "realpath", &RealpathFunc, 1, 1, false, false },
586 { "abspath", &AbspathFunc, 1, 1, false, false },
Shinichiro Hamaji30b8e602015-06-17 19:28:54 +0900587
Shinichiro Hamajifead3b72015-06-18 15:31:15 +0900588 { "if", &IfFunc, 3, 2, false, true },
589 { "and", &AndFunc, 0, 0, true, false },
590 { "or", &OrFunc, 0, 0, true, false },
Shinichiro Hamajie22fe8e2015-06-18 07:11:54 +0900591
Shinichiro Hamajifead3b72015-06-18 15:31:15 +0900592 { "value", &ValueFunc, 1, 1, false, false },
593 { "eval", &EvalFunc, 1, 1, false, false },
594 { "shell", &ShellFunc, 1, 1, false, false },
595 { "call", &CallFunc, 0, 0, false, false },
596 { "foreach", &ForeachFunc, 3, 3, false, false },
Shinichiro Hamajie22fe8e2015-06-18 07:11:54 +0900597
Shinichiro Hamajifead3b72015-06-18 15:31:15 +0900598 { "origin", &OriginFunc, 1, 1, false, false },
599 { "flavor", &FlavorFunc, 1, 1, false, false },
Shinichiro Hamajie22fe8e2015-06-18 07:11:54 +0900600
Shinichiro Hamajifead3b72015-06-18 15:31:15 +0900601 { "info", &InfoFunc, 1, 1, false, false },
602 { "warning", &WarningFunc, 1, 1, false, false },
603 { "error", &ErrorFunc, 1, 1, false, false },
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900604};
605
606unordered_map<StringPiece, FuncInfo*>* g_func_info_map;
607
608} // namespace
609
610void InitFuncTable() {
611 g_func_info_map = new unordered_map<StringPiece, FuncInfo*>;
612 for (size_t i = 0; i < sizeof(g_func_infos) / sizeof(g_func_infos[0]); i++) {
613 FuncInfo* fi = &g_func_infos[i];
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900614 bool ok = g_func_info_map->emplace(fi->name, fi).second;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900615 CHECK(ok);
616 }
617}
618
619void QuitFuncTable() {
620 delete g_func_info_map;
621}
622
623FuncInfo* GetFuncInfo(StringPiece name) {
624 auto found = g_func_info_map->find(name);
625 if (found == g_func_info_map->end())
626 return NULL;
627 return found->second;
628}