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