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