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) { |
Doug Zongker | e3da02e | 2009-06-12 16:13:52 -0700 | [diff] [blame] | 110 | free(state->errmsg); |
| 111 | state->errmsg = strdup("ifelse expects 2 or 3 arguments"); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 112 | return NULL; |
| 113 | } |
| 114 | char* cond = Evaluate(state, argv[0]); |
| 115 | if (cond == NULL) { |
| 116 | return NULL; |
| 117 | } |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 118 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 119 | if (BooleanString(cond) == true) { |
| 120 | free(cond); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 121 | return EvaluateValue(state, argv[1]); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 122 | } else { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 123 | if (argc == 3) { |
| 124 | free(cond); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 125 | return EvaluateValue(state, argv[2]); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 126 | } else { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 127 | return StringValue(cond); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 128 | } |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 129 | } |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 132 | Value* AbortFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 133 | char* msg = NULL; |
| 134 | if (argc > 0) { |
| 135 | msg = Evaluate(state, argv[0]); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 136 | } |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 137 | free(state->errmsg); |
| 138 | if (msg) { |
| 139 | state->errmsg = msg; |
| 140 | } else { |
| 141 | state->errmsg = strdup("called abort()"); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 142 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 143 | return NULL; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 146 | Value* AssertFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 147 | int i; |
| 148 | for (i = 0; i < argc; ++i) { |
| 149 | char* v = Evaluate(state, argv[i]); |
| 150 | if (v == NULL) { |
| 151 | return NULL; |
| 152 | } |
| 153 | int b = BooleanString(v); |
| 154 | free(v); |
| 155 | if (!b) { |
| 156 | int prefix_len; |
| 157 | int len = argv[i]->end - argv[i]->start; |
Tao Bao | 2a5a49d | 2015-08-20 12:10:46 -0700 | [diff] [blame] | 158 | char* err_src = reinterpret_cast<char*>(malloc(len + 20)); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 159 | strcpy(err_src, "assert failed: "); |
| 160 | prefix_len = strlen(err_src); |
| 161 | memcpy(err_src + prefix_len, state->script + argv[i]->start, len); |
| 162 | err_src[prefix_len + len] = '\0'; |
| 163 | free(state->errmsg); |
| 164 | state->errmsg = err_src; |
| 165 | return NULL; |
| 166 | } |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 167 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 168 | return StringValue(strdup("")); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 171 | Value* SleepFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 172 | char* val = Evaluate(state, argv[0]); |
| 173 | if (val == NULL) { |
| 174 | return NULL; |
| 175 | } |
| 176 | int v = strtol(val, NULL, 10); |
| 177 | sleep(v); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 178 | return StringValue(val); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 181 | Value* StdoutFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 182 | int i; |
| 183 | for (i = 0; i < argc; ++i) { |
| 184 | char* v = Evaluate(state, argv[i]); |
| 185 | if (v == NULL) { |
| 186 | return NULL; |
| 187 | } |
| 188 | fputs(v, stdout); |
| 189 | free(v); |
| 190 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 191 | return StringValue(strdup("")); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 194 | Value* LogicalAndFn(const char* name, State* state, |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 195 | int argc, Expr* argv[]) { |
| 196 | char* left = Evaluate(state, argv[0]); |
| 197 | if (left == NULL) return NULL; |
| 198 | if (BooleanString(left) == true) { |
| 199 | free(left); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 200 | return EvaluateValue(state, argv[1]); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 201 | } else { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 202 | return StringValue(left); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 206 | Value* LogicalOrFn(const char* name, State* state, |
| 207 | int argc, Expr* argv[]) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 208 | char* left = Evaluate(state, argv[0]); |
| 209 | if (left == NULL) return NULL; |
| 210 | if (BooleanString(left) == false) { |
| 211 | free(left); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 212 | return EvaluateValue(state, argv[1]); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 213 | } else { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 214 | return StringValue(left); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 218 | Value* LogicalNotFn(const char* name, State* state, |
| 219 | int argc, Expr* argv[]) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 220 | char* val = Evaluate(state, argv[0]); |
| 221 | if (val == NULL) return NULL; |
| 222 | bool bv = BooleanString(val); |
| 223 | free(val); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 224 | return StringValue(strdup(bv ? "" : "t")); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 227 | Value* SubstringFn(const char* name, State* state, |
| 228 | int argc, Expr* argv[]) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 229 | char* needle = Evaluate(state, argv[0]); |
| 230 | if (needle == NULL) return NULL; |
| 231 | char* haystack = Evaluate(state, argv[1]); |
| 232 | if (haystack == NULL) { |
| 233 | free(needle); |
| 234 | return NULL; |
| 235 | } |
| 236 | |
| 237 | char* result = strdup(strstr(haystack, needle) ? "t" : ""); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 238 | free(needle); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 239 | free(haystack); |
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* EqualityFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 244 | char* left = Evaluate(state, argv[0]); |
| 245 | if (left == NULL) return NULL; |
| 246 | char* right = Evaluate(state, argv[1]); |
| 247 | if (right == NULL) { |
| 248 | free(left); |
| 249 | return NULL; |
| 250 | } |
| 251 | |
| 252 | char* result = strdup(strcmp(left, right) == 0 ? "t" : ""); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 253 | free(left); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 254 | free(right); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 255 | return StringValue(result); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 258 | Value* InequalityFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 259 | char* left = Evaluate(state, argv[0]); |
| 260 | if (left == NULL) return NULL; |
| 261 | char* right = Evaluate(state, argv[1]); |
| 262 | if (right == NULL) { |
| 263 | free(left); |
| 264 | return NULL; |
| 265 | } |
| 266 | |
| 267 | char* result = strdup(strcmp(left, right) != 0 ? "t" : ""); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 268 | free(left); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 269 | free(right); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 270 | return StringValue(result); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 273 | Value* SequenceFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 274 | Value* left = EvaluateValue(state, argv[0]); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 275 | if (left == NULL) return NULL; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 276 | FreeValue(left); |
| 277 | return EvaluateValue(state, argv[1]); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 280 | Value* LessThanIntFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | e3da02e | 2009-06-12 16:13:52 -0700 | [diff] [blame] | 281 | if (argc != 2) { |
| 282 | free(state->errmsg); |
| 283 | state->errmsg = strdup("less_than_int expects 2 arguments"); |
| 284 | return NULL; |
| 285 | } |
| 286 | |
| 287 | char* left; |
| 288 | char* right; |
| 289 | if (ReadArgs(state, argv, 2, &left, &right) < 0) return NULL; |
| 290 | |
| 291 | bool result = false; |
| 292 | char* end; |
| 293 | |
Chih-Hung Hsieh | 54a2747 | 2016-04-18 11:30:55 -0700 | [diff] [blame] | 294 | // Parse up to at least long long or 64-bit integers. |
| 295 | int64_t l_int = static_cast<int64_t>(strtoll(left, &end, 10)); |
Doug Zongker | e3da02e | 2009-06-12 16:13:52 -0700 | [diff] [blame] | 296 | if (left[0] == '\0' || *end != '\0') { |
Doug Zongker | e3da02e | 2009-06-12 16:13:52 -0700 | [diff] [blame] | 297 | goto done; |
| 298 | } |
| 299 | |
Chih-Hung Hsieh | 54a2747 | 2016-04-18 11:30:55 -0700 | [diff] [blame] | 300 | int64_t r_int; |
| 301 | r_int = static_cast<int64_t>(strtoll(right, &end, 10)); |
Doug Zongker | e3da02e | 2009-06-12 16:13:52 -0700 | [diff] [blame] | 302 | if (right[0] == '\0' || *end != '\0') { |
Doug Zongker | e3da02e | 2009-06-12 16:13:52 -0700 | [diff] [blame] | 303 | goto done; |
| 304 | } |
| 305 | |
| 306 | result = l_int < r_int; |
| 307 | |
| 308 | done: |
| 309 | free(left); |
| 310 | free(right); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 311 | return StringValue(strdup(result ? "t" : "")); |
Doug Zongker | e3da02e | 2009-06-12 16:13:52 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 314 | Value* GreaterThanIntFn(const char* name, State* state, |
| 315 | int argc, Expr* argv[]) { |
Doug Zongker | e3da02e | 2009-06-12 16:13:52 -0700 | [diff] [blame] | 316 | if (argc != 2) { |
| 317 | free(state->errmsg); |
| 318 | state->errmsg = strdup("greater_than_int expects 2 arguments"); |
| 319 | return NULL; |
| 320 | } |
| 321 | |
| 322 | Expr* temp[2]; |
| 323 | temp[0] = argv[1]; |
| 324 | temp[1] = argv[0]; |
| 325 | |
| 326 | return LessThanIntFn(name, state, 2, temp); |
| 327 | } |
| 328 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 329 | Value* Literal(const char* name, State* state, int argc, Expr* argv[]) { |
| 330 | return StringValue(strdup(name)); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 333 | Expr* Build(Function fn, YYLTYPE loc, int count, ...) { |
| 334 | va_list v; |
| 335 | va_start(v, count); |
Tao Bao | 2a5a49d | 2015-08-20 12:10:46 -0700 | [diff] [blame] | 336 | Expr* e = reinterpret_cast<Expr*>(malloc(sizeof(Expr))); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 337 | e->fn = fn; |
| 338 | e->name = "(operator)"; |
| 339 | e->argc = count; |
Tao Bao | 2a5a49d | 2015-08-20 12:10:46 -0700 | [diff] [blame] | 340 | e->argv = reinterpret_cast<Expr**>(malloc(count * sizeof(Expr*))); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 341 | int i; |
| 342 | for (i = 0; i < count; ++i) { |
| 343 | e->argv[i] = va_arg(v, Expr*); |
| 344 | } |
| 345 | va_end(v); |
| 346 | e->start = loc.start; |
| 347 | e->end = loc.end; |
| 348 | return e; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | // ----------------------------------------------------------------- |
| 352 | // the function table |
| 353 | // ----------------------------------------------------------------- |
| 354 | |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 355 | static int fn_entries = 0; |
| 356 | static int fn_size = 0; |
| 357 | NamedFunction* fn_table = NULL; |
| 358 | |
| 359 | void RegisterFunction(const char* name, Function fn) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 360 | if (fn_entries >= fn_size) { |
| 361 | fn_size = fn_size*2 + 1; |
Tao Bao | 2a5a49d | 2015-08-20 12:10:46 -0700 | [diff] [blame] | 362 | fn_table = reinterpret_cast<NamedFunction*>(realloc(fn_table, fn_size * sizeof(NamedFunction))); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 363 | } |
| 364 | fn_table[fn_entries].name = name; |
| 365 | fn_table[fn_entries].fn = fn; |
| 366 | ++fn_entries; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | static int fn_entry_compare(const void* a, const void* b) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 370 | const char* na = ((const NamedFunction*)a)->name; |
| 371 | const char* nb = ((const NamedFunction*)b)->name; |
| 372 | return strcmp(na, nb); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | void FinishRegistration() { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 376 | qsort(fn_table, fn_entries, sizeof(NamedFunction), fn_entry_compare); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | Function FindFunction(const char* name) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 380 | NamedFunction key; |
| 381 | key.name = name; |
Tao Bao | 2a5a49d | 2015-08-20 12:10:46 -0700 | [diff] [blame] | 382 | NamedFunction* nf = reinterpret_cast<NamedFunction*>(bsearch(&key, fn_table, fn_entries, |
| 383 | sizeof(NamedFunction), fn_entry_compare)); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 384 | if (nf == NULL) { |
| 385 | return NULL; |
| 386 | } |
| 387 | return nf->fn; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | void RegisterBuiltins() { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 391 | RegisterFunction("ifelse", IfElseFn); |
| 392 | RegisterFunction("abort", AbortFn); |
| 393 | RegisterFunction("assert", AssertFn); |
| 394 | RegisterFunction("concat", ConcatFn); |
| 395 | RegisterFunction("is_substring", SubstringFn); |
| 396 | RegisterFunction("stdout", StdoutFn); |
| 397 | RegisterFunction("sleep", SleepFn); |
Doug Zongker | e3da02e | 2009-06-12 16:13:52 -0700 | [diff] [blame] | 398 | |
| 399 | RegisterFunction("less_than_int", LessThanIntFn); |
| 400 | RegisterFunction("greater_than_int", GreaterThanIntFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | |
| 404 | // ----------------------------------------------------------------- |
| 405 | // convenience methods for functions |
| 406 | // ----------------------------------------------------------------- |
| 407 | |
| 408 | // Evaluate the expressions in argv, giving 'count' char* (the ... is |
| 409 | // zero or more char** to put them in). If any expression evaluates |
| 410 | // to NULL, free the rest and return -1. Return 0 on success. |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 411 | int ReadArgs(State* state, Expr* argv[], int count, ...) { |
Tao Bao | 2a5a49d | 2015-08-20 12:10:46 -0700 | [diff] [blame] | 412 | char** args = reinterpret_cast<char**>(malloc(count * sizeof(char*))); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 413 | va_list v; |
| 414 | va_start(v, count); |
| 415 | int i; |
| 416 | for (i = 0; i < count; ++i) { |
| 417 | args[i] = Evaluate(state, argv[i]); |
| 418 | if (args[i] == NULL) { |
| 419 | va_end(v); |
| 420 | int j; |
| 421 | for (j = 0; j < i; ++j) { |
| 422 | free(args[j]); |
| 423 | } |
Kenny Root | 21854cc | 2010-02-17 18:31:48 -0800 | [diff] [blame] | 424 | free(args); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 425 | return -1; |
| 426 | } |
| 427 | *(va_arg(v, char**)) = args[i]; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 428 | } |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 429 | va_end(v); |
Kenny Root | 21854cc | 2010-02-17 18:31:48 -0800 | [diff] [blame] | 430 | free(args); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 431 | return 0; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 434 | // Evaluate the expressions in argv, giving 'count' Value* (the ... is |
| 435 | // zero or more Value** to put them in). If any expression evaluates |
| 436 | // to NULL, free the rest and return -1. Return 0 on success. |
| 437 | int ReadValueArgs(State* state, Expr* argv[], int count, ...) { |
Tao Bao | 2a5a49d | 2015-08-20 12:10:46 -0700 | [diff] [blame] | 438 | Value** args = reinterpret_cast<Value**>(malloc(count * sizeof(Value*))); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 439 | va_list v; |
| 440 | va_start(v, count); |
| 441 | int i; |
| 442 | for (i = 0; i < count; ++i) { |
| 443 | args[i] = EvaluateValue(state, argv[i]); |
| 444 | if (args[i] == NULL) { |
| 445 | va_end(v); |
| 446 | int j; |
| 447 | for (j = 0; j < i; ++j) { |
| 448 | FreeValue(args[j]); |
| 449 | } |
| 450 | free(args); |
| 451 | return -1; |
| 452 | } |
| 453 | *(va_arg(v, Value**)) = args[i]; |
| 454 | } |
| 455 | va_end(v); |
| 456 | free(args); |
| 457 | return 0; |
| 458 | } |
| 459 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 460 | // Evaluate the expressions in argv, returning an array of char* |
| 461 | // results. If any evaluate to NULL, free the rest and return NULL. |
| 462 | // The caller is responsible for freeing the returned array and the |
| 463 | // strings it contains. |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 464 | char** ReadVarArgs(State* state, int argc, Expr* argv[]) { |
| 465 | char** args = (char**)malloc(argc * sizeof(char*)); |
| 466 | int i = 0; |
| 467 | for (i = 0; i < argc; ++i) { |
| 468 | args[i] = Evaluate(state, argv[i]); |
| 469 | if (args[i] == NULL) { |
| 470 | int j; |
| 471 | for (j = 0; j < i; ++j) { |
| 472 | free(args[j]); |
| 473 | } |
| 474 | free(args); |
| 475 | return NULL; |
| 476 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 477 | } |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 478 | return args; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 479 | } |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 480 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 481 | // Evaluate the expressions in argv, returning an array of Value* |
| 482 | // results. If any evaluate to NULL, free the rest and return NULL. |
| 483 | // The caller is responsible for freeing the returned array and the |
| 484 | // Values it contains. |
| 485 | Value** ReadValueVarArgs(State* state, int argc, Expr* argv[]) { |
| 486 | Value** args = (Value**)malloc(argc * sizeof(Value*)); |
| 487 | int i = 0; |
| 488 | for (i = 0; i < argc; ++i) { |
| 489 | args[i] = EvaluateValue(state, argv[i]); |
| 490 | if (args[i] == NULL) { |
| 491 | int j; |
| 492 | for (j = 0; j < i; ++j) { |
| 493 | FreeValue(args[j]); |
| 494 | } |
| 495 | free(args); |
| 496 | return NULL; |
| 497 | } |
| 498 | } |
| 499 | return args; |
| 500 | } |
| 501 | |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 502 | static void ErrorAbortV(State* state, const char* format, va_list ap) { |
| 503 | std::string buffer; |
| 504 | android::base::StringAppendV(&buffer, format, ap); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 505 | free(state->errmsg); |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 506 | state->errmsg = strdup(buffer.c_str()); |
| 507 | return; |
| 508 | } |
| 509 | |
| 510 | // Use printf-style arguments to compose an error message to put into |
| 511 | // *state. Returns nullptr. |
| 512 | Value* ErrorAbort(State* state, const char* format, ...) { |
| 513 | va_list ap; |
| 514 | va_start(ap, format); |
| 515 | ErrorAbortV(state, format, ap); |
| 516 | va_end(ap); |
| 517 | return nullptr; |
| 518 | } |
| 519 | |
| 520 | Value* ErrorAbort(State* state, CauseCode cause_code, const char* format, ...) { |
| 521 | va_list ap; |
| 522 | va_start(ap, format); |
| 523 | ErrorAbortV(state, format, ap); |
| 524 | va_end(ap); |
| 525 | state->cause_code = cause_code; |
| 526 | return nullptr; |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 527 | } |