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