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