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