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 | |
| 17 | #include <string.h> |
| 18 | #include <stdbool.h> |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <stdarg.h> |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 22 | #include <unistd.h> |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 23 | |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 24 | #include <string> |
| 25 | |
| 26 | #include <android-base/stringprintf.h> |
| 27 | #include <android-base/strings.h> |
| 28 | |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 29 | #include "expr.h" |
| 30 | |
| 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 | 876b939 | 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 | 876b939 | 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 | 876b939 | 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 | 876b939 | 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 | 876b939 | 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 | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 322 | Expr* Build(Function fn, YYLTYPE loc, int count, ...) { |
| 323 | va_list v; |
| 324 | va_start(v, count); |
Tao Bao | 2a5a49d | 2015-08-20 12:10:46 -0700 | [diff] [blame] | 325 | Expr* e = reinterpret_cast<Expr*>(malloc(sizeof(Expr))); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 326 | e->fn = fn; |
| 327 | e->name = "(operator)"; |
| 328 | e->argc = count; |
Tao Bao | 2a5a49d | 2015-08-20 12:10:46 -0700 | [diff] [blame] | 329 | e->argv = reinterpret_cast<Expr**>(malloc(count * sizeof(Expr*))); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 330 | int i; |
| 331 | for (i = 0; i < count; ++i) { |
| 332 | e->argv[i] = va_arg(v, Expr*); |
| 333 | } |
| 334 | va_end(v); |
| 335 | e->start = loc.start; |
| 336 | e->end = loc.end; |
| 337 | return e; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | // ----------------------------------------------------------------- |
| 341 | // the function table |
| 342 | // ----------------------------------------------------------------- |
| 343 | |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 344 | static int fn_entries = 0; |
| 345 | static int fn_size = 0; |
| 346 | NamedFunction* fn_table = NULL; |
| 347 | |
| 348 | void RegisterFunction(const char* name, Function fn) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 349 | if (fn_entries >= fn_size) { |
| 350 | fn_size = fn_size*2 + 1; |
Tao Bao | 2a5a49d | 2015-08-20 12:10:46 -0700 | [diff] [blame] | 351 | fn_table = reinterpret_cast<NamedFunction*>(realloc(fn_table, fn_size * sizeof(NamedFunction))); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 352 | } |
| 353 | fn_table[fn_entries].name = name; |
| 354 | fn_table[fn_entries].fn = fn; |
| 355 | ++fn_entries; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | static int fn_entry_compare(const void* a, const void* b) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 359 | const char* na = ((const NamedFunction*)a)->name; |
| 360 | const char* nb = ((const NamedFunction*)b)->name; |
| 361 | return strcmp(na, nb); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | void FinishRegistration() { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 365 | qsort(fn_table, fn_entries, sizeof(NamedFunction), fn_entry_compare); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | Function FindFunction(const char* name) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 369 | NamedFunction key; |
| 370 | key.name = name; |
Tao Bao | 2a5a49d | 2015-08-20 12:10:46 -0700 | [diff] [blame] | 371 | NamedFunction* nf = reinterpret_cast<NamedFunction*>(bsearch(&key, fn_table, fn_entries, |
| 372 | sizeof(NamedFunction), fn_entry_compare)); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 373 | if (nf == NULL) { |
| 374 | return NULL; |
| 375 | } |
| 376 | return nf->fn; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | void RegisterBuiltins() { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 380 | RegisterFunction("ifelse", IfElseFn); |
| 381 | RegisterFunction("abort", AbortFn); |
| 382 | RegisterFunction("assert", AssertFn); |
| 383 | RegisterFunction("concat", ConcatFn); |
| 384 | RegisterFunction("is_substring", SubstringFn); |
| 385 | RegisterFunction("stdout", StdoutFn); |
| 386 | RegisterFunction("sleep", SleepFn); |
Doug Zongker | e3da02e | 2009-06-12 16:13:52 -0700 | [diff] [blame] | 387 | |
| 388 | RegisterFunction("less_than_int", LessThanIntFn); |
| 389 | RegisterFunction("greater_than_int", GreaterThanIntFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | |
| 393 | // ----------------------------------------------------------------- |
| 394 | // convenience methods for functions |
| 395 | // ----------------------------------------------------------------- |
| 396 | |
| 397 | // Evaluate the expressions in argv, giving 'count' char* (the ... is |
| 398 | // zero or more char** to put them in). If any expression evaluates |
| 399 | // to NULL, free the rest and return -1. Return 0 on success. |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 400 | int ReadArgs(State* state, Expr* argv[], int count, ...) { |
Tao Bao | 2a5a49d | 2015-08-20 12:10:46 -0700 | [diff] [blame] | 401 | char** args = reinterpret_cast<char**>(malloc(count * sizeof(char*))); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 402 | va_list v; |
| 403 | va_start(v, count); |
| 404 | int i; |
| 405 | for (i = 0; i < count; ++i) { |
| 406 | args[i] = Evaluate(state, argv[i]); |
| 407 | if (args[i] == NULL) { |
| 408 | va_end(v); |
| 409 | int j; |
| 410 | for (j = 0; j < i; ++j) { |
| 411 | free(args[j]); |
| 412 | } |
Kenny Root | 21854cc | 2010-02-17 18:31:48 -0800 | [diff] [blame] | 413 | free(args); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 414 | return -1; |
| 415 | } |
| 416 | *(va_arg(v, char**)) = args[i]; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 417 | } |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 418 | va_end(v); |
Kenny Root | 21854cc | 2010-02-17 18:31:48 -0800 | [diff] [blame] | 419 | free(args); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 420 | return 0; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 423 | // Evaluate the expressions in argv, giving 'count' Value* (the ... is |
| 424 | // zero or more Value** to put them in). If any expression evaluates |
| 425 | // to NULL, free the rest and return -1. Return 0 on success. |
| 426 | int ReadValueArgs(State* state, Expr* argv[], int count, ...) { |
Tao Bao | 2a5a49d | 2015-08-20 12:10:46 -0700 | [diff] [blame] | 427 | Value** args = reinterpret_cast<Value**>(malloc(count * sizeof(Value*))); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 428 | va_list v; |
| 429 | va_start(v, count); |
| 430 | int i; |
| 431 | for (i = 0; i < count; ++i) { |
| 432 | args[i] = EvaluateValue(state, argv[i]); |
| 433 | if (args[i] == NULL) { |
| 434 | va_end(v); |
| 435 | int j; |
| 436 | for (j = 0; j < i; ++j) { |
| 437 | FreeValue(args[j]); |
| 438 | } |
| 439 | free(args); |
| 440 | return -1; |
| 441 | } |
| 442 | *(va_arg(v, Value**)) = args[i]; |
| 443 | } |
| 444 | va_end(v); |
| 445 | free(args); |
| 446 | return 0; |
| 447 | } |
| 448 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 449 | // Evaluate the expressions in argv, returning an array of char* |
| 450 | // results. If any evaluate to NULL, free the rest and return NULL. |
| 451 | // The caller is responsible for freeing the returned array and the |
| 452 | // strings it contains. |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 453 | char** ReadVarArgs(State* state, int argc, Expr* argv[]) { |
| 454 | char** args = (char**)malloc(argc * sizeof(char*)); |
| 455 | int i = 0; |
| 456 | for (i = 0; i < argc; ++i) { |
| 457 | args[i] = Evaluate(state, argv[i]); |
| 458 | if (args[i] == NULL) { |
| 459 | int j; |
| 460 | for (j = 0; j < i; ++j) { |
| 461 | free(args[j]); |
| 462 | } |
| 463 | free(args); |
| 464 | return NULL; |
| 465 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 466 | } |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 467 | return args; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 468 | } |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 469 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 470 | // Evaluate the expressions in argv, returning an array of Value* |
| 471 | // results. If any evaluate to NULL, free the rest and return NULL. |
| 472 | // The caller is responsible for freeing the returned array and the |
| 473 | // Values it contains. |
| 474 | Value** ReadValueVarArgs(State* state, int argc, Expr* argv[]) { |
| 475 | Value** args = (Value**)malloc(argc * sizeof(Value*)); |
| 476 | int i = 0; |
| 477 | for (i = 0; i < argc; ++i) { |
| 478 | args[i] = EvaluateValue(state, argv[i]); |
| 479 | if (args[i] == NULL) { |
| 480 | int j; |
| 481 | for (j = 0; j < i; ++j) { |
| 482 | FreeValue(args[j]); |
| 483 | } |
| 484 | free(args); |
| 485 | return NULL; |
| 486 | } |
| 487 | } |
| 488 | return args; |
| 489 | } |
| 490 | |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 491 | // Use printf-style arguments to compose an error message to put into |
| 492 | // *state. Returns nullptr. |
| 493 | Value* ErrorAbort(State* state, const char* format, ...) { |
| 494 | va_list ap; |
| 495 | va_start(ap, format); |
Tao Bao | 876b939 | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 496 | android::base::StringAppendV(&state->errmsg, format, ap); |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 497 | va_end(ap); |
| 498 | return nullptr; |
| 499 | } |
| 500 | |
| 501 | Value* ErrorAbort(State* state, CauseCode cause_code, const char* format, ...) { |
| 502 | va_list ap; |
| 503 | va_start(ap, format); |
Tao Bao | 876b939 | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 504 | android::base::StringAppendV(&state->errmsg, format, ap); |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 505 | va_end(ap); |
| 506 | state->cause_code = cause_code; |
| 507 | return nullptr; |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 508 | } |
Tao Bao | 876b939 | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 509 | |
| 510 | State::State(const std::string& script, void* cookie) : |
| 511 | script(script), |
| 512 | cookie(cookie) { |
| 513 | } |
| 514 | |