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 | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 43 | bool Evaluate(State* state, Expr* expr, std::string* result) { |
| 44 | if (result == nullptr) { |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | std::unique_ptr<Value> v(expr->fn(expr->name, state, expr->argc, expr->argv)); |
| 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 | |
| 61 | Value* EvaluateValue(State* state, Expr* expr) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 62 | return expr->fn(expr->name, state, expr->argc, 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 | |
| 76 | Value* ConcatFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 77 | if (argc == 0) { |
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; |
| 81 | for (int i = 0; i < argc; ++i) { |
| 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 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 92 | Value* IfElseFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 93 | if (argc != 2 && argc != 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]); |
| 105 | } else if (argc == 3) { |
| 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 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 112 | Value* AbortFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 113 | std::string msg; |
| 114 | if (argc > 0 && 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 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 122 | Value* AssertFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 123 | for (int i = 0; i < argc; ++i) { |
| 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 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 137 | Value* SleepFn(const char* name, State* state, int argc, 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 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 152 | Value* StdoutFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 153 | for (int i = 0; i < argc; ++i) { |
| 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, |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 164 | int argc, 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, |
| 177 | int argc, 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, |
| 190 | int argc, 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, |
| 200 | int argc, 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 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 215 | Value* EqualityFn(const char* name, State* state, int argc, 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 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 229 | Value* InequalityFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 230 | std::string left; |
| 231 | if (!Evaluate(state, argv[0], &left)) { |
| 232 | return nullptr; |
| 233 | } |
| 234 | std::string right; |
| 235 | if (!Evaluate(state, argv[1], &right)) { |
| 236 | return nullptr; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 239 | const char* result = (left != right) ? "t" : ""; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 240 | return StringValue(result); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 243 | Value* SequenceFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 244 | std::unique_ptr<Value> left(EvaluateValue(state, argv[0])); |
| 245 | if (!left) { |
| 246 | return nullptr; |
| 247 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 248 | return EvaluateValue(state, argv[1]); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 251 | Value* LessThanIntFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | e3da02e | 2009-06-12 16:13:52 -0700 | [diff] [blame] | 252 | if (argc != 2) { |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 253 | state->errmsg = "less_than_int expects 2 arguments"; |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 254 | return nullptr; |
Doug Zongker | e3da02e | 2009-06-12 16:13:52 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | char* left; |
| 258 | char* right; |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 259 | if (ReadArgs(state, argv, 2, &left, &right) < 0) return nullptr; |
Doug Zongker | e3da02e | 2009-06-12 16:13:52 -0700 | [diff] [blame] | 260 | |
| 261 | bool result = false; |
| 262 | char* end; |
| 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. |
| 265 | int64_t l_int = static_cast<int64_t>(strtoll(left, &end, 10)); |
Doug Zongker | e3da02e | 2009-06-12 16:13:52 -0700 | [diff] [blame] | 266 | if (left[0] == '\0' || *end != '\0') { |
Doug Zongker | e3da02e | 2009-06-12 16:13:52 -0700 | [diff] [blame] | 267 | goto done; |
| 268 | } |
| 269 | |
Chih-Hung Hsieh | 54a2747 | 2016-04-18 11:30:55 -0700 | [diff] [blame] | 270 | int64_t r_int; |
| 271 | r_int = static_cast<int64_t>(strtoll(right, &end, 10)); |
Doug Zongker | e3da02e | 2009-06-12 16:13:52 -0700 | [diff] [blame] | 272 | if (right[0] == '\0' || *end != '\0') { |
Doug Zongker | e3da02e | 2009-06-12 16:13:52 -0700 | [diff] [blame] | 273 | goto done; |
| 274 | } |
| 275 | |
| 276 | result = l_int < r_int; |
| 277 | |
| 278 | done: |
| 279 | free(left); |
| 280 | free(right); |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 281 | return StringValue(result ? "t" : ""); |
Doug Zongker | e3da02e | 2009-06-12 16:13:52 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 284 | Value* GreaterThanIntFn(const char* name, State* state, |
| 285 | int argc, Expr* argv[]) { |
Doug Zongker | e3da02e | 2009-06-12 16:13:52 -0700 | [diff] [blame] | 286 | if (argc != 2) { |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 287 | state->errmsg = "greater_than_int expects 2 arguments"; |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 288 | return nullptr; |
Doug Zongker | e3da02e | 2009-06-12 16:13:52 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | Expr* temp[2]; |
| 292 | temp[0] = argv[1]; |
| 293 | temp[1] = argv[0]; |
| 294 | |
| 295 | return LessThanIntFn(name, state, 2, temp); |
| 296 | } |
| 297 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 298 | Value* Literal(const char* name, State* state, int argc, Expr* argv[]) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 299 | return StringValue(name); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 302 | // ----------------------------------------------------------------- |
| 303 | // the function table |
| 304 | // ----------------------------------------------------------------- |
| 305 | |
Tao Bao | 39119ad | 2016-10-10 22:52:18 -0700 | [diff] [blame] | 306 | static std::unordered_map<std::string, Function> fn_table; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 307 | |
Tao Bao | 39119ad | 2016-10-10 22:52:18 -0700 | [diff] [blame] | 308 | void RegisterFunction(const std::string& name, Function fn) { |
| 309 | fn_table[name] = fn; |
| 310 | } |
| 311 | |
| 312 | Function FindFunction(const std::string& name) { |
| 313 | if (fn_table.find(name) == fn_table.end()) { |
| 314 | return nullptr; |
| 315 | } else { |
| 316 | return fn_table[name]; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 317 | } |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | void RegisterBuiltins() { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 321 | RegisterFunction("ifelse", IfElseFn); |
| 322 | RegisterFunction("abort", AbortFn); |
| 323 | RegisterFunction("assert", AssertFn); |
| 324 | RegisterFunction("concat", ConcatFn); |
| 325 | RegisterFunction("is_substring", SubstringFn); |
| 326 | RegisterFunction("stdout", StdoutFn); |
| 327 | RegisterFunction("sleep", SleepFn); |
Doug Zongker | e3da02e | 2009-06-12 16:13:52 -0700 | [diff] [blame] | 328 | |
| 329 | RegisterFunction("less_than_int", LessThanIntFn); |
| 330 | RegisterFunction("greater_than_int", GreaterThanIntFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | |
| 334 | // ----------------------------------------------------------------- |
| 335 | // convenience methods for functions |
| 336 | // ----------------------------------------------------------------- |
| 337 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 338 | // Evaluate the expressions in argv, and put the results of strings in |
| 339 | // args. If any expression evaluates to nullptr, free the rest and return |
| 340 | // false. Return true on success. |
| 341 | bool ReadArgs(State* state, int argc, Expr* argv[], std::vector<std::string>* args) { |
| 342 | if (args == nullptr) { |
| 343 | return false; |
| 344 | } |
| 345 | for (int i = 0; i < argc; ++i) { |
| 346 | std::string var; |
| 347 | if (!Evaluate(state, argv[i], &var)) { |
| 348 | args->clear(); |
| 349 | return false; |
| 350 | } |
| 351 | args->push_back(var); |
| 352 | } |
| 353 | return true; |
| 354 | } |
| 355 | |
| 356 | // Evaluate the expressions in argv, and put the results of Value* in |
| 357 | // args. If any expression evaluate to nullptr, free the rest and return |
| 358 | // false. Return true on success. |
| 359 | bool ReadValueArgs(State* state, int argc, Expr* argv[], |
| 360 | std::vector<std::unique_ptr<Value>>* args) { |
| 361 | if (args == nullptr) { |
| 362 | return false; |
| 363 | } |
| 364 | for (int i = 0; i < argc; ++i) { |
| 365 | std::unique_ptr<Value> v(EvaluateValue(state, argv[i])); |
| 366 | if (!v) { |
| 367 | args->clear(); |
| 368 | return false; |
| 369 | } |
| 370 | args->push_back(std::move(v)); |
| 371 | } |
| 372 | return true; |
| 373 | } |
| 374 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 375 | // Evaluate the expressions in argv, giving 'count' char* (the ... is |
| 376 | // zero or more char** to put them in). If any expression evaluates |
| 377 | // to NULL, free the rest and return -1. Return 0 on success. |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 378 | int ReadArgs(State* state, Expr* argv[], int count, ...) { |
Tao Bao | 2a5a49d | 2015-08-20 12:10:46 -0700 | [diff] [blame] | 379 | char** args = reinterpret_cast<char**>(malloc(count * sizeof(char*))); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 380 | va_list v; |
| 381 | va_start(v, count); |
| 382 | int i; |
| 383 | for (i = 0; i < count; ++i) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 384 | std::string str; |
| 385 | if (!Evaluate(state, argv[i], &str) || |
| 386 | (args[i] = strdup(str.c_str())) == nullptr) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 387 | va_end(v); |
| 388 | int j; |
| 389 | for (j = 0; j < i; ++j) { |
| 390 | free(args[j]); |
| 391 | } |
Kenny Root | 21854cc | 2010-02-17 18:31:48 -0800 | [diff] [blame] | 392 | free(args); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 393 | return -1; |
| 394 | } |
| 395 | *(va_arg(v, char**)) = args[i]; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 396 | } |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 397 | va_end(v); |
Kenny Root | 21854cc | 2010-02-17 18:31:48 -0800 | [diff] [blame] | 398 | free(args); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 399 | return 0; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 402 | // Evaluate the expressions in argv, giving 'count' Value* (the ... is |
| 403 | // zero or more Value** to put them in). If any expression evaluates |
| 404 | // to NULL, free the rest and return -1. Return 0 on success. |
| 405 | int ReadValueArgs(State* state, Expr* argv[], int count, ...) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 406 | Value** args = new Value*[count]; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 407 | va_list v; |
| 408 | va_start(v, count); |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 409 | for (int i = 0; i < count; ++i) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 410 | args[i] = EvaluateValue(state, argv[i]); |
| 411 | if (args[i] == NULL) { |
| 412 | va_end(v); |
| 413 | int j; |
| 414 | for (j = 0; j < i; ++j) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 415 | delete args[j]; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 416 | } |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 417 | delete[] args; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 418 | return -1; |
| 419 | } |
| 420 | *(va_arg(v, Value**)) = args[i]; |
| 421 | } |
| 422 | va_end(v); |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 423 | delete[] args; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 424 | return 0; |
| 425 | } |
| 426 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 427 | // Evaluate the expressions in argv, returning an array of char* |
| 428 | // results. If any evaluate to NULL, free the rest and return NULL. |
| 429 | // The caller is responsible for freeing the returned array and the |
| 430 | // strings it contains. |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 431 | char** ReadVarArgs(State* state, int argc, Expr* argv[]) { |
| 432 | char** args = (char**)malloc(argc * sizeof(char*)); |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 433 | for (int i = 0; i < argc; ++i) { |
| 434 | std::string str; |
| 435 | if (!Evaluate(state, argv[i], &str) || |
| 436 | (args[i] = strdup(str.c_str())) == nullptr) { |
| 437 | for (int j = 0; j < i; ++j) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 438 | free(args[j]); |
| 439 | } |
| 440 | free(args); |
| 441 | return NULL; |
| 442 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 443 | } |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 444 | return args; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 445 | } |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 446 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 447 | // Evaluate the expressions in argv, returning an array of Value* |
| 448 | // results. If any evaluate to NULL, free the rest and return NULL. |
| 449 | // The caller is responsible for freeing the returned array and the |
| 450 | // Values it contains. |
| 451 | Value** ReadValueVarArgs(State* state, int argc, Expr* argv[]) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 452 | Value** args = new Value*[argc]; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 453 | int i = 0; |
| 454 | for (i = 0; i < argc; ++i) { |
| 455 | args[i] = EvaluateValue(state, argv[i]); |
| 456 | if (args[i] == NULL) { |
| 457 | int j; |
| 458 | for (j = 0; j < i; ++j) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 459 | delete args[j]; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 460 | } |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 461 | delete[] args; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 462 | return NULL; |
| 463 | } |
| 464 | } |
| 465 | return args; |
| 466 | } |
| 467 | |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 468 | // Use printf-style arguments to compose an error message to put into |
| 469 | // *state. Returns nullptr. |
| 470 | Value* ErrorAbort(State* state, const char* format, ...) { |
| 471 | va_list ap; |
| 472 | va_start(ap, format); |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 473 | android::base::StringAppendV(&state->errmsg, format, ap); |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 474 | va_end(ap); |
| 475 | return nullptr; |
| 476 | } |
| 477 | |
| 478 | Value* ErrorAbort(State* state, CauseCode cause_code, const char* format, ...) { |
| 479 | va_list ap; |
| 480 | va_start(ap, format); |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 481 | android::base::StringAppendV(&state->errmsg, format, ap); |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 482 | va_end(ap); |
| 483 | state->cause_code = cause_code; |
| 484 | return nullptr; |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 485 | } |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 486 | |
| 487 | State::State(const std::string& script, void* cookie) : |
| 488 | script(script), |
| 489 | cookie(cookie) { |
| 490 | } |
| 491 | |