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