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