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