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