Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Tao Bao | 39119ad | 2016-10-10 22:52:18 -0700 | [diff] [blame] | 17 | #include "expr.h" |
| 18 | |
| 19 | #include <stdarg.h> |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
Tao Bao | 39119ad | 2016-10-10 22:52:18 -0700 | [diff] [blame] | 22 | #include <string.h> |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 23 | #include <unistd.h> |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 24 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 25 | #include <memory> |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 26 | #include <string> |
Tao Bao | 39119ad | 2016-10-10 22:52:18 -0700 | [diff] [blame] | 27 | #include <unordered_map> |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 28 | #include <vector> |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 29 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 30 | #include <android-base/parseint.h> |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 31 | #include <android-base/stringprintf.h> |
| 32 | #include <android-base/strings.h> |
| 33 | |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 34 | // Functions should: |
| 35 | // |
| 36 | // - return a malloc()'d string |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 37 | // - if Evaluate() on any argument returns nullptr, return nullptr. |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 38 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 39 | static bool BooleanString(const std::string& s) { |
| 40 | return !s.empty(); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 43 | bool Evaluate(State* state, const std::unique_ptr<Expr>& expr, std::string* result) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 44 | if (result == nullptr) { |
| 45 | return false; |
| 46 | } |
| 47 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 48 | std::unique_ptr<Value> v(expr->fn(expr->name.c_str(), state, expr->argv)); |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 49 | if (!v) { |
| 50 | return false; |
| 51 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 52 | if (v->type != VAL_STRING) { |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 53 | ErrorAbort(state, kArgsParsingFailure, "expecting string, got value type %d", v->type); |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 54 | return false; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 55 | } |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 56 | |
| 57 | *result = v->data; |
| 58 | return true; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 61 | Value* EvaluateValue(State* state, const std::unique_ptr<Expr>& expr) { |
| 62 | return expr->fn(expr->name.c_str(), state, expr->argv); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 65 | Value* StringValue(const char* str) { |
| 66 | if (str == nullptr) { |
| 67 | return nullptr; |
| 68 | } |
| 69 | return new Value(VAL_STRING, str); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 72 | Value* StringValue(const std::string& str) { |
| 73 | return StringValue(str.c_str()); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 76 | Value* ConcatFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 77 | if (argv.empty()) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 78 | return StringValue(""); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 79 | } |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 80 | std::string result; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 81 | for (size_t i = 0; i < argv.size(); ++i) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 82 | std::string str; |
| 83 | if (!Evaluate(state, argv[i], &str)) { |
| 84 | return nullptr; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 85 | } |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 86 | result += str; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 87 | } |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 88 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 89 | return StringValue(result); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 92 | Value* IfElseFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 93 | if (argv.size() != 2 && argv.size() != 3) { |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 94 | state->errmsg = "ifelse expects 2 or 3 arguments"; |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 95 | return nullptr; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 96 | } |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 97 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 98 | std::string cond; |
| 99 | if (!Evaluate(state, argv[0], &cond)) { |
| 100 | return nullptr; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 101 | } |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 102 | |
| 103 | if (!cond.empty()) { |
| 104 | return EvaluateValue(state, argv[1]); |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 105 | } else if (argv.size() == 3) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 106 | return EvaluateValue(state, argv[2]); |
| 107 | } |
| 108 | |
| 109 | return StringValue(""); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 112 | Value* AbortFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 113 | std::string msg; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 114 | if (!argv.empty() && Evaluate(state, argv[0], &msg)) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 115 | state->errmsg = msg; |
| 116 | } else { |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 117 | state->errmsg = "called abort()"; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 118 | } |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 119 | return nullptr; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 122 | Value* AssertFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 123 | for (size_t i = 0; i < argv.size(); ++i) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 124 | std::string result; |
| 125 | if (!Evaluate(state, argv[i], &result)) { |
| 126 | return nullptr; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 127 | } |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 128 | if (result.empty()) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 129 | int len = argv[i]->end - argv[i]->start; |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 130 | state->errmsg = "assert failed: " + state->script.substr(argv[i]->start, len); |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 131 | return nullptr; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 132 | } |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 133 | } |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 134 | return StringValue(""); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 137 | Value* SleepFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 138 | std::string val; |
| 139 | if (!Evaluate(state, argv[0], &val)) { |
| 140 | return nullptr; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 141 | } |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 142 | |
| 143 | int v; |
| 144 | if (!android::base::ParseInt(val.c_str(), &v, 0)) { |
| 145 | return nullptr; |
| 146 | } |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 147 | sleep(v); |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 148 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 149 | return StringValue(val); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 152 | Value* StdoutFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
| 153 | for (size_t i = 0; i < argv.size(); ++i) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 154 | std::string v; |
| 155 | if (!Evaluate(state, argv[i], &v)) { |
| 156 | return nullptr; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 157 | } |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 158 | fputs(v.c_str(), stdout); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 159 | } |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 160 | return StringValue(""); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 163 | Value* LogicalAndFn(const char* name, State* state, |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 164 | const std::vector<std::unique_ptr<Expr>>& argv) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 165 | std::string left; |
| 166 | if (!Evaluate(state, argv[0], &left)) { |
| 167 | return nullptr; |
| 168 | } |
| 169 | if (BooleanString(left)) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 170 | return EvaluateValue(state, argv[1]); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 171 | } else { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 172 | return StringValue(""); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 176 | Value* LogicalOrFn(const char* name, State* state, |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 177 | const std::vector<std::unique_ptr<Expr>>& argv) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 178 | std::string left; |
| 179 | if (!Evaluate(state, argv[0], &left)) { |
| 180 | return nullptr; |
| 181 | } |
| 182 | if (!BooleanString(left)) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 183 | return EvaluateValue(state, argv[1]); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 184 | } else { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 185 | return StringValue(left); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 189 | Value* LogicalNotFn(const char* name, State* state, |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 190 | const std::vector<std::unique_ptr<Expr>>& argv) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 191 | std::string val; |
| 192 | if (!Evaluate(state, argv[0], &val)) { |
| 193 | return nullptr; |
| 194 | } |
| 195 | |
| 196 | return StringValue(BooleanString(val) ? "" : "t"); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 199 | Value* SubstringFn(const char* name, State* state, |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 200 | const std::vector<std::unique_ptr<Expr>>& argv) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 201 | std::string needle; |
| 202 | if (!Evaluate(state, argv[0], &needle)) { |
| 203 | return nullptr; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 206 | std::string haystack; |
| 207 | if (!Evaluate(state, argv[1], &haystack)) { |
| 208 | return nullptr; |
| 209 | } |
| 210 | |
| 211 | std::string result = (haystack.find(needle) != std::string::npos) ? "t" : ""; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 212 | return StringValue(result); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 215 | Value* EqualityFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 216 | std::string left; |
| 217 | if (!Evaluate(state, argv[0], &left)) { |
| 218 | return nullptr; |
| 219 | } |
| 220 | std::string right; |
| 221 | if (!Evaluate(state, argv[1], &right)) { |
| 222 | return nullptr; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 225 | const char* result = (left == right) ? "t" : ""; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 226 | return StringValue(result); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 229 | Value* InequalityFn(const char* name, State* state, |
| 230 | const std::vector<std::unique_ptr<Expr>>& argv) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 231 | std::string left; |
| 232 | if (!Evaluate(state, argv[0], &left)) { |
| 233 | return nullptr; |
| 234 | } |
| 235 | std::string right; |
| 236 | if (!Evaluate(state, argv[1], &right)) { |
| 237 | return nullptr; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 240 | const char* result = (left != right) ? "t" : ""; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 241 | return StringValue(result); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 244 | Value* SequenceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 245 | std::unique_ptr<Value> left(EvaluateValue(state, argv[0])); |
| 246 | if (!left) { |
| 247 | return nullptr; |
| 248 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 249 | return EvaluateValue(state, argv[1]); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 252 | Value* LessThanIntFn(const char* name, State* state, |
| 253 | const std::vector<std::unique_ptr<Expr>>& argv) { |
| 254 | if (argv.size() != 2) { |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 255 | state->errmsg = "less_than_int expects 2 arguments"; |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 256 | return nullptr; |
Doug Zongker | e3da02e | 2009-06-12 16:13:52 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 259 | std::vector<std::string> args; |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 260 | if (!ReadArgs(state, argv, &args)) { |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 261 | return nullptr; |
| 262 | } |
Doug Zongker | e3da02e | 2009-06-12 16:13:52 -0700 | [diff] [blame] | 263 | |
Chih-Hung Hsieh | 54a2747 | 2016-04-18 11:30:55 -0700 | [diff] [blame] | 264 | // Parse up to at least long long or 64-bit integers. |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 265 | int64_t l_int; |
| 266 | if (!android::base::ParseInt(args[0].c_str(), &l_int)) { |
| 267 | state->errmsg = "failed to parse int in " + args[0]; |
| 268 | return nullptr; |
Doug Zongker | e3da02e | 2009-06-12 16:13:52 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Chih-Hung Hsieh | 54a2747 | 2016-04-18 11:30:55 -0700 | [diff] [blame] | 271 | int64_t r_int; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 272 | if (!android::base::ParseInt(args[1].c_str(), &r_int)) { |
| 273 | state->errmsg = "failed to parse int in " + args[1]; |
| 274 | return nullptr; |
Doug Zongker | e3da02e | 2009-06-12 16:13:52 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 277 | return StringValue(l_int < r_int ? "t" : ""); |
Doug Zongker | e3da02e | 2009-06-12 16:13:52 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 280 | Value* GreaterThanIntFn(const char* name, State* state, |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 281 | const std::vector<std::unique_ptr<Expr>>& argv) { |
| 282 | if (argv.size() != 2) { |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 283 | state->errmsg = "greater_than_int expects 2 arguments"; |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 284 | return nullptr; |
Doug Zongker | e3da02e | 2009-06-12 16:13:52 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 287 | std::vector<std::string> args; |
| 288 | if (!ReadArgs(state, argv, &args)) { |
| 289 | return nullptr; |
| 290 | } |
Doug Zongker | e3da02e | 2009-06-12 16:13:52 -0700 | [diff] [blame] | 291 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 292 | // Parse up to at least long long or 64-bit integers. |
| 293 | int64_t l_int; |
| 294 | if (!android::base::ParseInt(args[0].c_str(), &l_int)) { |
| 295 | state->errmsg = "failed to parse int in " + args[0]; |
| 296 | return nullptr; |
| 297 | } |
| 298 | |
| 299 | int64_t r_int; |
| 300 | if (!android::base::ParseInt(args[1].c_str(), &r_int)) { |
| 301 | state->errmsg = "failed to parse int in " + args[1]; |
| 302 | return nullptr; |
| 303 | } |
| 304 | |
| 305 | return StringValue(l_int > r_int ? "t" : ""); |
Doug Zongker | e3da02e | 2009-06-12 16:13:52 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 308 | Value* Literal(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 309 | return StringValue(name); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 312 | // ----------------------------------------------------------------- |
| 313 | // the function table |
| 314 | // ----------------------------------------------------------------- |
| 315 | |
Tao Bao | 39119ad | 2016-10-10 22:52:18 -0700 | [diff] [blame] | 316 | static std::unordered_map<std::string, Function> fn_table; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 317 | |
Tao Bao | 39119ad | 2016-10-10 22:52:18 -0700 | [diff] [blame] | 318 | void RegisterFunction(const std::string& name, Function fn) { |
| 319 | fn_table[name] = fn; |
| 320 | } |
| 321 | |
| 322 | Function FindFunction(const std::string& name) { |
| 323 | if (fn_table.find(name) == fn_table.end()) { |
| 324 | return nullptr; |
| 325 | } else { |
| 326 | return fn_table[name]; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 327 | } |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | void RegisterBuiltins() { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 331 | RegisterFunction("ifelse", IfElseFn); |
| 332 | RegisterFunction("abort", AbortFn); |
| 333 | RegisterFunction("assert", AssertFn); |
| 334 | RegisterFunction("concat", ConcatFn); |
| 335 | RegisterFunction("is_substring", SubstringFn); |
| 336 | RegisterFunction("stdout", StdoutFn); |
| 337 | RegisterFunction("sleep", SleepFn); |
Doug Zongker | e3da02e | 2009-06-12 16:13:52 -0700 | [diff] [blame] | 338 | |
| 339 | RegisterFunction("less_than_int", LessThanIntFn); |
| 340 | RegisterFunction("greater_than_int", GreaterThanIntFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | |
| 344 | // ----------------------------------------------------------------- |
| 345 | // convenience methods for functions |
| 346 | // ----------------------------------------------------------------- |
| 347 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 348 | // Evaluate the expressions in argv, and put the results of strings in args. If any expression |
| 349 | // evaluates to nullptr, return false. Return true on success. |
| 350 | bool ReadArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv, |
| 351 | std::vector<std::string>* args) { |
| 352 | return ReadArgs(state, argv, args, 0, argv.size()); |
| 353 | } |
| 354 | |
| 355 | bool ReadArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv, |
| 356 | std::vector<std::string>* args, size_t start, size_t len) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 357 | if (args == nullptr) { |
| 358 | return false; |
| 359 | } |
Tianjie Xu | bf607c4 | 2017-03-23 15:12:22 -0700 | [diff] [blame] | 360 | if (start + len > argv.size()) { |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 361 | return false; |
| 362 | } |
| 363 | for (size_t i = start; i < start + len; ++i) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 364 | std::string var; |
| 365 | if (!Evaluate(state, argv[i], &var)) { |
| 366 | args->clear(); |
| 367 | return false; |
| 368 | } |
| 369 | args->push_back(var); |
| 370 | } |
| 371 | return true; |
| 372 | } |
| 373 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 374 | // Evaluate the expressions in argv, and put the results of Value* in args. If any expression |
| 375 | // evaluate to nullptr, return false. Return true on success. |
| 376 | bool ReadValueArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv, |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 377 | std::vector<std::unique_ptr<Value>>* args) { |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 378 | return ReadValueArgs(state, argv, args, 0, argv.size()); |
| 379 | } |
| 380 | |
| 381 | bool ReadValueArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv, |
| 382 | std::vector<std::unique_ptr<Value>>* args, size_t start, size_t len) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 383 | if (args == nullptr) { |
| 384 | return false; |
| 385 | } |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 386 | if (len == 0 || start + len > argv.size()) { |
| 387 | return false; |
| 388 | } |
| 389 | for (size_t i = start; i < start + len; ++i) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 390 | std::unique_ptr<Value> v(EvaluateValue(state, argv[i])); |
| 391 | if (!v) { |
| 392 | args->clear(); |
| 393 | return false; |
| 394 | } |
| 395 | args->push_back(std::move(v)); |
| 396 | } |
| 397 | return true; |
| 398 | } |
| 399 | |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 400 | // Use printf-style arguments to compose an error message to put into |
| 401 | // *state. Returns nullptr. |
| 402 | Value* ErrorAbort(State* state, const char* format, ...) { |
| 403 | va_list ap; |
| 404 | va_start(ap, format); |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 405 | android::base::StringAppendV(&state->errmsg, format, ap); |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 406 | va_end(ap); |
| 407 | return nullptr; |
| 408 | } |
| 409 | |
| 410 | Value* ErrorAbort(State* state, CauseCode cause_code, const char* format, ...) { |
| 411 | va_list ap; |
| 412 | va_start(ap, format); |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 413 | android::base::StringAppendV(&state->errmsg, format, ap); |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 414 | va_end(ap); |
| 415 | state->cause_code = cause_code; |
| 416 | return nullptr; |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 417 | } |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 418 | |
| 419 | State::State(const std::string& script, void* cookie) : |
| 420 | script(script), |
| 421 | cookie(cookie) { |
| 422 | } |
| 423 | |