blob: 25c41c1f7f782ccdde3a8d78bbac614edac1d3cc [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 Hamajifc14d5f2015-07-28 17:07:57 +090043// TODO: This code is very similar to
44// NinjaGenerator::TranslateCommand. Factor them out.
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +090045void StripShellComment(string* cmd) {
Shinichiro Hamajifc14d5f2015-07-28 17:07:57 +090046 if (cmd->find('#') == string::npos)
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +090047 return;
Shinichiro Hamajifc14d5f2015-07-28 17:07:57 +090048
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +090049 string res;
Shinichiro Hamajifc14d5f2015-07-28 17:07:57 +090050 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 Hamajifb415ad2015-08-14 17:19:34 +090075 res += *in;
Shinichiro Hamajifc14d5f2015-07-28 17:07:57 +090076 break;
77
78 case '\\':
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +090079 res += '\\';
Shinichiro Hamajifc14d5f2015-07-28 17:07:57 +090080 break;
81
82 default:
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +090083 res += *in;
Shinichiro Hamajifc14d5f2015-07-28 17:07:57 +090084 }
85
86 if (*in == '\\') {
87 prev_backslash = !prev_backslash;
88 } else {
89 prev_backslash = false;
90 }
91
92 prev_char = *in;
93 }
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +090094 cmd->swap(res);
Shinichiro Hamajifc14d5f2015-07-28 17:07:57 +090095}
96
Shinichiro Hamaji2e6cbfc2015-06-16 18:46:50 +090097void PatsubstFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +090098 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 Hamaji37591ce2015-06-16 19:36:05 +0900101 WordWriter ww(s);
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900102 Pattern pat(pat_str);
103 for (StringPiece tok : WordScanner(str)) {
Shinichiro Hamaji37591ce2015-06-16 19:36:05 +0900104 ww.MaybeAddWhitespace();
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900105 pat.AppendSubst(tok, repl, s);
Shinichiro Hamaji2e6cbfc2015-06-16 18:46:50 +0900106 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900107}
108
Shinichiro Hamaji37591ce2015-06-16 19:36:05 +0900109void StripFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900110 const string&& str = args[0]->Eval(ev);
Shinichiro Hamaji37591ce2015-06-16 19:36:05 +0900111 WordWriter ww(s);
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900112 for (StringPiece tok : WordScanner(str)) {
Shinichiro Hamaji37591ce2015-06-16 19:36:05 +0900113 ww.Write(tok);
114 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900115}
116
Shinichiro Hamaji37591ce2015-06-16 19:36:05 +0900117void SubstFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900118 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 Hamaji5af931d2015-07-06 15:37:59 +0900124 return;
125 }
Shinichiro Hamaji37591ce2015-06-16 19:36:05 +0900126 size_t index = 0;
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900127 while (index < str.size()) {
128 size_t found = str.find(pat, index);
Shinichiro Hamaji37591ce2015-06-16 19:36:05 +0900129 if (found == string::npos)
130 break;
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900131 AppendString(StringPiece(str).substr(index, found - index), s);
132 AppendString(repl, s);
133 index = found + pat.size();
Shinichiro Hamaji37591ce2015-06-16 19:36:05 +0900134 }
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900135 AppendString(StringPiece(str).substr(index), s);
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900136}
137
Shinichiro Hamaji00cc6582015-06-17 18:12:46 +0900138void FindstringFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900139 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 Hamaji4f22f5c2015-06-16 16:28:25 +0900143}
144
Shinichiro Hamaji00cc6582015-06-17 18:12:46 +0900145void FilterFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900146 const string&& pat_buf = args[0]->Eval(ev);
147 const string&& text = args[1]->Eval(ev);
Shinichiro Hamajia6a17a42015-06-18 20:11:19 +0900148 vector<Pattern> pats;
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900149 for (StringPiece pat : WordScanner(pat_buf)) {
Shinichiro Hamajia6a17a42015-06-18 20:11:19 +0900150 pats.push_back(Pattern(pat));
151 }
Shinichiro Hamaji00cc6582015-06-17 18:12:46 +0900152 WordWriter ww(s);
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900153 for (StringPiece tok : WordScanner(text)) {
Shinichiro Hamajia6a17a42015-06-18 20:11:19 +0900154 for (const Pattern& pat : pats) {
155 if (pat.Match(tok)) {
Shinichiro Hamaji00cc6582015-06-17 18:12:46 +0900156 ww.Write(tok);
157 break;
158 }
159 }
160 }
161}
162
163void FilterOutFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900164 const string&& pat_buf = args[0]->Eval(ev);
165 const string&& text = args[1]->Eval(ev);
Shinichiro Hamajia6a17a42015-06-18 20:11:19 +0900166 vector<Pattern> pats;
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900167 for (StringPiece pat : WordScanner(pat_buf)) {
Shinichiro Hamajia6a17a42015-06-18 20:11:19 +0900168 pats.push_back(Pattern(pat));
169 }
Shinichiro Hamaji00cc6582015-06-17 18:12:46 +0900170 WordWriter ww(s);
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900171 for (StringPiece tok : WordScanner(text)) {
Shinichiro Hamaji00cc6582015-06-17 18:12:46 +0900172 bool matched = false;
Shinichiro Hamajia6a17a42015-06-18 20:11:19 +0900173 for (const Pattern& pat : pats) {
174 if (pat.Match(tok)) {
Shinichiro Hamaji00cc6582015-06-17 18:12:46 +0900175 matched = true;
176 break;
177 }
178 }
179 if (!matched)
180 ww.Write(tok);
181 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900182}
183
Shinichiro Hamajid87e59e2015-06-17 18:18:34 +0900184void SortFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900185 const string&& list = args[0]->Eval(ev);
Shinichiro Hamajid87e59e2015-06-17 18:18:34 +0900186 vector<StringPiece> toks;
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900187 WordScanner(list).Split(&toks);
Shinichiro Hamajid87e59e2015-06-17 18:18:34 +0900188 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 Hamaji4f22f5c2015-06-16 16:28:25 +0900197}
198
Shinichiro Hamajid5271452015-06-17 18:50:03 +0900199static 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 Hamaji4f22f5c2015-06-16 16:28:25 +0900207}
208
Shinichiro Hamajid5271452015-06-17 18:50:03 +0900209void WordFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900210 const string&& n_str = args[0]->Eval(ev);
211 int n = GetNumericValueForFunc(n_str);
Shinichiro Hamajid5271452015-06-17 18:50:03 +0900212 if (n < 0) {
213 ev->Error(StringPrintf(
214 "*** non-numeric first argument to `word' function: '%s'.",
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900215 n_str.c_str()));
Shinichiro Hamajid5271452015-06-17 18:50:03 +0900216 }
217 if (n == 0) {
218 ev->Error("*** first argument to `word' function must be greater than 0.");
219 }
220
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900221 const string&& text = args[1]->Eval(ev);
222 for (StringPiece tok : WordScanner(text)) {
Shinichiro Hamajid5271452015-06-17 18:50:03 +0900223 n--;
224 if (n == 0) {
225 AppendString(tok, s);
226 break;
227 }
228 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900229}
230
Shinichiro Hamajid5271452015-06-17 18:50:03 +0900231void WordlistFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900232 const string&& s_str = args[0]->Eval(ev);
233 int si = GetNumericValueForFunc(s_str);
Shinichiro Hamajid5271452015-06-17 18:50:03 +0900234 if (si < 0) {
235 ev->Error(StringPrintf(
236 "*** non-numeric first argument to `wordlist' function: '%s'.",
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900237 s_str.c_str()));
Shinichiro Hamajid5271452015-06-17 18:50:03 +0900238 }
239 if (si == 0) {
240 ev->Error(StringPrintf(
241 "*** invalid first argument to `wordlist' function: %s`",
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900242 s_str.c_str()));
Shinichiro Hamajid5271452015-06-17 18:50:03 +0900243 }
244
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900245 const string&& e_str = args[1]->Eval(ev);
246 int ei = GetNumericValueForFunc(e_str);
Shinichiro Hamajid5271452015-06-17 18:50:03 +0900247 if (ei < 0) {
248 ev->Error(StringPrintf(
249 "*** non-numeric second argument to `wordlist' function: '%s'.",
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900250 e_str.c_str()));
Shinichiro Hamajid5271452015-06-17 18:50:03 +0900251 }
252
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900253 const string&& text = args[2]->Eval(ev);
Shinichiro Hamajid5271452015-06-17 18:50:03 +0900254 int i = 0;
255 WordWriter ww(s);
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900256 for (StringPiece tok : WordScanner(text)) {
Shinichiro Hamajid5271452015-06-17 18:50:03 +0900257 i++;
258 if (si <= i && i <= ei) {
259 ww.Write(tok);
260 }
261 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900262}
263
Shinichiro Hamajid5271452015-06-17 18:50:03 +0900264void WordsFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900265 const string&& text = args[0]->Eval(ev);
266 WordScanner ws(text);
Shinichiro Hamajid5271452015-06-17 18:50:03 +0900267 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 Hamaji4f22f5c2015-06-16 16:28:25 +0900273}
274
Shinichiro Hamajid5271452015-06-17 18:50:03 +0900275void FirstwordFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900276 const string&& text = args[0]->Eval(ev);
277 for (StringPiece tok : WordScanner(text)) {
Shinichiro Hamajid5271452015-06-17 18:50:03 +0900278 AppendString(tok, s);
279 return;
280 }
281}
282
283void LastwordFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900284 const string&& text = args[0]->Eval(ev);
Shinichiro Hamajid5271452015-06-17 18:50:03 +0900285 StringPiece last;
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900286 for (StringPiece tok : WordScanner(text)) {
Shinichiro Hamajid5271452015-06-17 18:50:03 +0900287 last = tok;
288 }
289 AppendString(last, s);
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900290}
291
Shinichiro Hamaji30b8e602015-06-17 19:28:54 +0900292void JoinFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900293 const string&& list1 = args[0]->Eval(ev);
294 const string&& list2 = args[1]->Eval(ev);
295 WordScanner ws1(list1);
296 WordScanner ws2(list2);
Shinichiro Hamaji30b8e602015-06-17 19:28:54 +0900297 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 Hamaji4f22f5c2015-06-16 16:28:25 +0900305}
306
Shinichiro Hamajifcf1b762015-06-18 03:43:54 +0900307void WildcardFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +0900308 COLLECT_STATS("func wildcard time");
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900309 const string&& pat = args[0]->Eval(ev);
Shinichiro Hamaji7409aee2015-07-28 14:52:37 +0900310 // Note GNU make does not delay the execution of $(wildcard) so we
311 // do not need to check avoid_io here.
Shinichiro Hamajifcf1b762015-06-18 03:43:54 +0900312 WordWriter ww(s);
Shinichiro Hamaji0e3873a2015-07-05 15:48:28 +0900313 vector<string>* files;
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900314 for (StringPiece tok : WordScanner(pat)) {
Shinichiro Hamaji8f68bd32015-06-18 11:01:51 +0900315 ScopedTerminator st(tok);
Shinichiro Hamaji0e3873a2015-07-05 15:48:28 +0900316 Glob(tok.data(), &files);
317 sort(files->begin(), files->end());
318 for (const string& file : *files) {
Shinichiro Hamajife97c412015-06-29 16:57:57 +0900319 ww.Write(file);
320 }
Shinichiro Hamaji284f3d12015-06-17 19:29:01 +0900321 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900322}
323
Shinichiro Hamaji67f9a702015-06-18 06:00:57 +0900324void DirFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900325 const string&& text = args[0]->Eval(ev);
Shinichiro Hamaji67f9a702015-06-18 06:00:57 +0900326 WordWriter ww(s);
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900327 for (StringPiece tok : WordScanner(text)) {
Shinichiro Hamaji67f9a702015-06-18 06:00:57 +0900328 ww.Write(Dirname(tok));
Shinichiro Hamaji55906852015-06-29 16:40:33 +0900329 s->push_back('/');
Shinichiro Hamaji67f9a702015-06-18 06:00:57 +0900330 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900331}
332
Shinichiro Hamaji67f9a702015-06-18 06:00:57 +0900333void NotdirFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900334 const string&& text = args[0]->Eval(ev);
Shinichiro Hamaji67f9a702015-06-18 06:00:57 +0900335 WordWriter ww(s);
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900336 for (StringPiece tok : WordScanner(text)) {
Shinichiro Hamaji67f9a702015-06-18 06:00:57 +0900337 if (tok == "/") {
Shinichiro Hamaji388e8582015-07-03 16:51:46 +0900338 ww.Write(StringPiece(""));
Shinichiro Hamaji67f9a702015-06-18 06:00:57 +0900339 } else {
340 ww.Write(Basename(tok));
341 }
342 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900343}
344
Shinichiro Hamaji67f9a702015-06-18 06:00:57 +0900345void SuffixFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900346 const string&& text = args[0]->Eval(ev);
Shinichiro Hamaji67f9a702015-06-18 06:00:57 +0900347 WordWriter ww(s);
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900348 for (StringPiece tok : WordScanner(text)) {
Shinichiro Hamaji67f9a702015-06-18 06:00:57 +0900349 StringPiece suf = GetExt(tok);
350 if (!suf.empty())
351 ww.Write(suf);
352 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900353}
354
Shinichiro Hamaji67f9a702015-06-18 06:00:57 +0900355void BasenameFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900356 const string&& text = args[0]->Eval(ev);
Shinichiro Hamaji67f9a702015-06-18 06:00:57 +0900357 WordWriter ww(s);
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900358 for (StringPiece tok : WordScanner(text)) {
Shinichiro Hamaji67f9a702015-06-18 06:00:57 +0900359 ww.Write(StripExt(tok));
360 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900361}
362
Shinichiro Hamaji5d694f02015-06-18 06:05:36 +0900363void AddsuffixFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900364 const string&& suf = args[0]->Eval(ev);
365 const string&& text = args[1]->Eval(ev);
Shinichiro Hamaji5d694f02015-06-18 06:05:36 +0900366 WordWriter ww(s);
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900367 for (StringPiece tok : WordScanner(text)) {
Shinichiro Hamaji5d694f02015-06-18 06:05:36 +0900368 ww.Write(tok);
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900369 *s += suf;
Shinichiro Hamaji5d694f02015-06-18 06:05:36 +0900370 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900371}
372
Shinichiro Hamaji5d694f02015-06-18 06:05:36 +0900373void AddprefixFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900374 const string&& pre = args[0]->Eval(ev);
375 const string&& text = args[1]->Eval(ev);
Shinichiro Hamaji5d694f02015-06-18 06:05:36 +0900376 WordWriter ww(s);
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900377 for (StringPiece tok : WordScanner(text)) {
378 ww.Write(pre);
Shinichiro Hamaji5d694f02015-06-18 06:05:36 +0900379 AppendString(tok, s);
380 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900381}
382
Shinichiro Hamaji8a963582015-06-18 07:05:58 +0900383void RealpathFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900384 const string&& text = args[0]->Eval(ev);
Shinichiro Hamajidf1fc8b2015-06-29 15:45:21 +0900385 if (ev->avoid_io()) {
Shinichiro Hamaji65dce542015-07-28 15:39:03 +0900386 *s += "$(realpath ";
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900387 *s += text;
Shinichiro Hamaji65dce542015-07-28 15:39:03 +0900388 *s += " 2> /dev/null)";
Shinichiro Hamajidf1fc8b2015-06-29 15:45:21 +0900389 return;
390 }
391
Shinichiro Hamaji8a963582015-06-18 07:05:58 +0900392 WordWriter ww(s);
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900393 for (StringPiece tok : WordScanner(text)) {
Shinichiro Hamaji8f68bd32015-06-18 11:01:51 +0900394 ScopedTerminator st(tok);
Shinichiro Hamaji8a963582015-06-18 07:05:58 +0900395 char buf[PATH_MAX];
396 if (realpath(tok.data(), buf))
397 *s += buf;
Shinichiro Hamaji8a963582015-06-18 07:05:58 +0900398 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900399}
400
Shinichiro Hamaji8a963582015-06-18 07:05:58 +0900401void AbspathFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900402 const string&& text = args[0]->Eval(ev);
Shinichiro Hamaji8a963582015-06-18 07:05:58 +0900403 WordWriter ww(s);
404 string buf;
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900405 for (StringPiece tok : WordScanner(text)) {
Shinichiro Hamaji8a963582015-06-18 07:05:58 +0900406 AbsPath(tok, &buf);
407 ww.Write(buf);
408 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900409}
410
Shinichiro Hamajifead3b72015-06-18 15:31:15 +0900411void IfFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900412 const string&& cond = args[0]->Eval(ev);
413 if (cond.empty()) {
Shinichiro Hamajifead3b72015-06-18 15:31:15 +0900414 if (args.size() > 2)
415 args[2]->Eval(ev, s);
416 } else {
417 args[1]->Eval(ev, s);
418 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900419}
420
Shinichiro Hamajifead3b72015-06-18 15:31:15 +0900421void AndFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900422 string cond;
Shinichiro Hamajifead3b72015-06-18 15:31:15 +0900423 for (Value* a : args) {
424 cond = a->Eval(ev);
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900425 if (cond.empty())
Shinichiro Hamajifead3b72015-06-18 15:31:15 +0900426 return;
427 }
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900428 if (!cond.empty()) {
429 *s += cond;
Shinichiro Hamajifead3b72015-06-18 15:31:15 +0900430 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900431}
432
Shinichiro Hamajifead3b72015-06-18 15:31:15 +0900433void OrFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
434 for (Value* a : args) {
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900435 const string&& cond = a->Eval(ev);
436 if (!cond.empty()) {
437 *s += cond;
Shinichiro Hamajifead3b72015-06-18 15:31:15 +0900438 return;
439 }
440 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900441}
442
Shinichiro Hamaji81699be2015-06-22 18:07:38 +0900443void ValueFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900444 const string&& var_name = args[0]->Eval(ev);
445 Var* var = ev->LookupVar(Intern(var_name));
Shinichiro Hamaji81699be2015-06-22 18:07:38 +0900446 AppendString(var->String().as_string(), s);
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900447}
448
Shinichiro Hamajifead3b72015-06-18 15:31:15 +0900449void EvalFunc(const vector<Value*>& args, Evaluator* ev, string*) {
Shinichiro Hamaji76ff9832015-06-18 17:11:22 +0900450 // TODO: eval leaks everything... for now.
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900451 //const string text = args[0]->Eval(ev);
Shinichiro Hamaji76ff9832015-06-18 17:11:22 +0900452 string* text = new string;
453 args[0]->Eval(ev, text);
Shinichiro Hamaji80456fb2015-06-18 14:56:10 +0900454 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 Hamaji76ff9832015-06-18 17:11:22 +0900459 //delete ast;
Shinichiro Hamaji80456fb2015-06-18 14:56:10 +0900460 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900461}
462
Shinichiro Hamaji5f57a992015-06-30 19:39:39 +0900463//#define TEST_FIND_EMULATOR
464
Shinichiro Hamaji68e712b2015-07-17 06:11:08 +0900465// 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.
470static 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 Hamajic9b0aca2015-07-31 16:47:56 +0900478static void ShellFuncImpl(const string& shell, const string& cmd,
Shinichiro Hamajic9b0aca2015-07-31 16:47:56 +0900479 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 Hamajic9b0aca2015-07-31 16:47:56 +0900495 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 Willemsen48d6e8c2015-08-05 14:38:34 -0700505 FormatForCommandSubstitution(s);
Shinichiro Hamajic9b0aca2015-07-31 16:47:56 +0900506
507#ifdef TEST_FIND_EMULATOR
508 if (need_check) {
Dan Willemsen48d6e8c2015-08-05 14:38:34 -0700509 if (*s != out2) {
Shinichiro Hamajic9b0aca2015-07-31 16:47:56 +0900510 ERROR("FindEmulator is broken: %s\n%s\nvs\n%s",
Dan Willemsen48d6e8c2015-08-05 14:38:34 -0700511 cmd.c_str(), s->c_str(), out2.c_str());
Shinichiro Hamajic9b0aca2015-07-31 16:47:56 +0900512 }
513 }
514#endif
515}
516
Shinichiro Hamaji4db9edc2015-08-13 16:43:02 +0900517static vector<CommandResult*> g_command_results;
Shinichiro Hamajic9b0aca2015-07-31 16:47:56 +0900518
Shinichiro Hamaji180b4092015-08-13 17:11:18 +0900519bool ShouldStoreCommandResult(StringPiece cmd) {
520 if (HasWord(cmd, "date") || HasWord(cmd, "echo"))
521 return false;
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +0900522 if (g_flags.ignore_dirty_pattern) {
523 Pattern pat(g_flags.ignore_dirty_pattern);
Shinichiro Hamaji180b4092015-08-13 17:11:18 +0900524 for (StringPiece tok : WordScanner(cmd)) {
525 if (pat.Match(tok))
526 return false;
527 }
528 }
529 return true;
530}
531
Shinichiro Hamajifcf1b762015-06-18 03:43:54 +0900532void ShellFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900533 string cmd = args[0]->Eval(ev);
534 if (ev->avoid_io() && !HasNoIoInShellScript(cmd)) {
535 StripShellComment(&cmd);
Shinichiro Hamajidf1fc8b2015-06-29 15:45:21 +0900536 *s += "$(";
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900537 *s += cmd;
Shinichiro Hamajidf1fc8b2015-06-29 15:45:21 +0900538 *s += ")";
539 return;
540 }
541
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900542 const string&& shell = ev->EvalVar(kShellSym);
Shinichiro Hamajifcf1b762015-06-18 03:43:54 +0900543
Shinichiro Hamajic9b0aca2015-07-31 16:47:56 +0900544 string out;
545 FindCommand* fc = NULL;
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900546 ShellFuncImpl(shell, cmd, &out, &fc);
547 if (ShouldStoreCommandResult(cmd)) {
Shinichiro Hamaji4db9edc2015-08-13 16:43:02 +0900548 CommandResult* cr = new CommandResult();
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900549 cr->cmd = cmd;
Shinichiro Hamaji4db9edc2015-08-13 16:43:02 +0900550 cr->find.reset(fc);
551 cr->result = out;
552 g_command_results.push_back(cr);
Shinichiro Hamajifcf1b762015-06-18 03:43:54 +0900553 }
554 *s += out;
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900555}
556
Shinichiro Hamaji3064f1f2015-06-18 16:32:09 +0900557void CallFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Colin Cross580cc1f2015-07-27 13:18:18 -0700558 static const string tmpvar_names[] = {
Shinichiro Hamaji3064f1f2015-06-18 16:32:09 +0900559 "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
560 };
561
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900562 const string&& func_name = args[0]->Eval(ev);
563 Var* func = ev->LookupVar(Intern(func_name));
Shinichiro Hamaji3064f1f2015-06-18 16:32:09 +0900564 vector<unique_ptr<SimpleVar>> av;
Shinichiro Hamaji3064f1f2015-06-18 16:32:09 +0900565 for (size_t i = 1; i < args.size(); i++) {
Shinichiro Hamajif62e9a72015-06-26 04:18:21 +0900566 unique_ptr<SimpleVar> s(
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900567 new SimpleVar(args[i]->Eval(ev), VarOrigin::AUTOMATIC));
Shinichiro Hamaji3064f1f2015-06-18 16:32:09 +0900568 av.push_back(move(s));
569 }
Shinichiro Hamaji5d53bc72015-06-26 08:31:54 +0900570 vector<unique_ptr<ScopedVar>> sv;
Dan Willemsenb4467962015-08-06 23:59:22 -0700571 for (size_t i = 1; ; i++) {
Colin Cross580cc1f2015-07-27 13:18:18 -0700572 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 Willemsenb4467962015-08-06 23:59:22 -0700580 if (i < args.size()) {
Shinichiro Hamajie978a892015-08-17 16:53:40 +0900581 sv.emplace_back(new ScopedVar(ev->mutable_vars(),
582 Intern(tmpvar_name), av[i-1].get()));
Dan Willemsenb4467962015-08-06 23:59:22 -0700583 } 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 Hamajie978a892015-08-17 16:53:40 +0900589 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 Willemsenb4467962015-08-06 23:59:22 -0700592 }
Shinichiro Hamaji5d53bc72015-06-26 08:31:54 +0900593 }
Shinichiro Hamaji3064f1f2015-06-18 16:32:09 +0900594 func->Eval(ev, s);
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900595}
596
Shinichiro Hamajicf0cd682015-06-18 16:18:13 +0900597void ForeachFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900598 const string&& varname = args[0]->Eval(ev);
599 const string&& list = args[1]->Eval(ev);
Shinichiro Hamajicf0cd682015-06-18 16:18:13 +0900600 WordWriter ww(s);
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900601 for (StringPiece tok : WordScanner(list)) {
Shinichiro Hamajicf0cd682015-06-18 16:18:13 +0900602 unique_ptr<SimpleVar> v(new SimpleVar(
Shinichiro Hamaji5081c712015-08-14 16:49:20 +0900603 tok.as_string(), VarOrigin::AUTOMATIC));
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900604 ScopedVar sv(ev->mutable_vars(), Intern(varname), v.get());
Shinichiro Hamajicf0cd682015-06-18 16:18:13 +0900605 ww.MaybeAddWhitespace();
606 args[2]->Eval(ev, s);
607 }
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900608}
609
Shinichiro Hamajic22fdb42015-06-22 18:10:40 +0900610void OriginFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900611 const string&& var_name = args[0]->Eval(ev);
612 Var* var = ev->LookupVar(Intern(var_name));
Shinichiro Hamajif62e9a72015-06-26 04:18:21 +0900613 *s += GetOriginStr(var->Origin());
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900614}
615
Shinichiro Hamajic22fdb42015-06-22 18:10:40 +0900616void FlavorFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900617 const string&& var_name = args[0]->Eval(ev);
618 Var* var = ev->LookupVar(Intern(var_name));
Shinichiro Hamajic22fdb42015-06-22 18:10:40 +0900619 *s += var->Flavor();
Shinichiro Hamaji4f22f5c2015-06-16 16:28:25 +0900620}
621
Shinichiro Hamaji86e11332015-07-28 15:24:17 +0900622void InfoFunc(const vector<Value*>& args, Evaluator* ev, string*) {
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900623 const string&& a = args[0]->Eval(ev);
Shinichiro Hamajidf1fc8b2015-06-29 15:45:21 +0900624 if (ev->avoid_io()) {
Dan Willemsene6f68582015-08-21 11:13:28 -0700625 ev->add_delayed_output_command(StringPrintf("echo -e \"%s\"", EchoEscape(a).c_str()));
Shinichiro Hamajidf1fc8b2015-06-29 15:45:21 +0900626 return;
627 }
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900628 printf("%s\n", a.c_str());
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900629 fflush(stdout);
630}
631
Shinichiro Hamaji86e11332015-07-28 15:24:17 +0900632void WarningFunc(const vector<Value*>& args, Evaluator* ev, string*) {
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900633 const string&& a = args[0]->Eval(ev);
Shinichiro Hamajidf1fc8b2015-06-29 15:45:21 +0900634 if (ev->avoid_io()) {
Shinichiro Hamaji86e11332015-07-28 15:24:17 +0900635 ev->add_delayed_output_command(
Dan Willemsene6f68582015-08-21 11:13:28 -0700636 StringPrintf("echo -e \"%s:%d: %s\" 2>&1", LOCF(ev->loc()), EchoEscape(a).c_str()));
Shinichiro Hamajidf1fc8b2015-06-29 15:45:21 +0900637 return;
638 }
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900639 printf("%s:%d: %s\n", LOCF(ev->loc()), a.c_str());
Shinichiro Hamaji9619b362015-06-16 16:13:25 +0900640 fflush(stdout);
641}
642
Shinichiro Hamaji86e11332015-07-28 15:24:17 +0900643void ErrorFunc(const vector<Value*>& args, Evaluator* ev, string*) {
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900644 const string&& a = args[0]->Eval(ev);
Shinichiro Hamajidf1fc8b2015-06-29 15:45:21 +0900645 if (ev->avoid_io()) {
Shinichiro Hamaji86e11332015-07-28 15:24:17 +0900646 ev->add_delayed_output_command(
Dan Willemsene6f68582015-08-21 11:13:28 -0700647 StringPrintf("echo -e \"%s:%d: *** %s.\" 2>&1 && false",
648 LOCF(ev->loc()), EchoEscape(a).c_str()));
Shinichiro Hamajidf1fc8b2015-06-29 15:45:21 +0900649 return;
650 }
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +0900651 ev->Error(StringPrintf("*** %s.", a.c_str()));
Shinichiro Hamaji9619b362015-06-16 16:13:25 +0900652}
653
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900654FuncInfo g_func_infos[] = {
Shinichiro Hamajifead3b72015-06-18 15:31:15 +0900655 { "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 Hamaji2e6cbfc2015-06-16 18:46:50 +0900667
Shinichiro Hamajifead3b72015-06-18 15:31:15 +0900668 { "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 Hamaji30b8e602015-06-17 19:28:54 +0900678
Shinichiro Hamajifead3b72015-06-18 15:31:15 +0900679 { "if", &IfFunc, 3, 2, false, true },
680 { "and", &AndFunc, 0, 0, true, false },
681 { "or", &OrFunc, 0, 0, true, false },
Shinichiro Hamajie22fe8e2015-06-18 07:11:54 +0900682
Shinichiro Hamajifead3b72015-06-18 15:31:15 +0900683 { "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 Hamajie22fe8e2015-06-18 07:11:54 +0900688
Shinichiro Hamajifead3b72015-06-18 15:31:15 +0900689 { "origin", &OriginFunc, 1, 1, false, false },
690 { "flavor", &FlavorFunc, 1, 1, false, false },
Shinichiro Hamajie22fe8e2015-06-18 07:11:54 +0900691
Shinichiro Hamajifead3b72015-06-18 15:31:15 +0900692 { "info", &InfoFunc, 1, 1, false, false },
693 { "warning", &WarningFunc, 1, 1, false, false },
694 { "error", &ErrorFunc, 1, 1, false, false },
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900695};
696
697unordered_map<StringPiece, FuncInfo*>* g_func_info_map;
698
699} // namespace
700
701void 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 Hamajie7992752015-06-29 18:38:35 +0900705 bool ok = g_func_info_map->emplace(fi->name, fi).second;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900706 CHECK(ok);
707 }
708}
709
710void QuitFuncTable() {
711 delete g_func_info_map;
712}
713
714FuncInfo* 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 Hamajic9b0aca2015-07-31 16:47:56 +0900720
Shinichiro Hamaji4db9edc2015-08-13 16:43:02 +0900721const vector<CommandResult*>& GetShellCommandResults() {
722 return g_command_results;
Shinichiro Hamajic9b0aca2015-07-31 16:47:56 +0900723}