2 c734c0e9 2021-08-03 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 c734c0e9 2021-08-03 op * Permission to use, copy, modify, and distribute this software for any
5 c734c0e9 2021-08-03 op * purpose with or without fee is hereby granted, provided that the above
6 c734c0e9 2021-08-03 op * copyright notice and this permission notice appear in all copies.
8 c734c0e9 2021-08-03 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 c734c0e9 2021-08-03 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 c734c0e9 2021-08-03 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 c734c0e9 2021-08-03 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 c734c0e9 2021-08-03 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 c734c0e9 2021-08-03 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 c734c0e9 2021-08-03 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 c734c0e9 2021-08-03 op #include "compat.h"
19 9820edbf 2021-08-06 op #include <sys/socket.h>
20 9820edbf 2021-08-06 op #include <sys/stat.h>
21 9820edbf 2021-08-06 op #include <sys/wait.h>
23 c734c0e9 2021-08-03 op #include <assert.h>
24 9820edbf 2021-08-06 op #include <endian.h>
25 9820edbf 2021-08-06 op #include <errno.h>
26 9820edbf 2021-08-06 op #include <fcntl.h>
27 c734c0e9 2021-08-03 op #include <inttypes.h>
28 9820edbf 2021-08-06 op #include <poll.h>
29 9820edbf 2021-08-06 op #include <pwd.h>
30 32b8add4 2021-12-14 op #include <regex.h>
31 9820edbf 2021-08-06 op #include <signal.h>
32 c734c0e9 2021-08-03 op #include <stdio.h>
33 c734c0e9 2021-08-03 op #include <stdlib.h>
34 c734c0e9 2021-08-03 op #include <string.h>
35 c734c0e9 2021-08-03 op #include <syslog.h>
36 dd1d2343 2021-08-05 op #include <unistd.h>
38 9820edbf 2021-08-06 op #include "client.h"
39 c734c0e9 2021-08-03 op #include "log.h"
40 58329cc1 2021-08-06 op #include "script.h"
41 58329cc1 2021-08-06 op #include "utils.h"
43 c734c0e9 2021-08-03 op #define DEBUG 0
45 9820edbf 2021-08-06 op #ifndef INFTIM
46 9820edbf 2021-08-06 op #define INFTIM -1
49 9820edbf 2021-08-06 op static const char *argv0;
51 9820edbf 2021-08-06 op static uint8_t *lastmsg;
53 9820edbf 2021-08-06 op static struct imsgbuf ibuf;
54 9820edbf 2021-08-06 op static int ibuf_inuse;
55 370b4f2e 2021-08-07 op static int child_out = -1;
57 c734c0e9 2021-08-03 op static struct procs procs = TAILQ_HEAD_INITIALIZER(procs);
58 c734c0e9 2021-08-03 op static struct tests tests = TAILQ_HEAD_INITIALIZER(tests);
60 d728769d 2021-08-07 op static int ntests;
62 d9d02161 2021-08-04 op static struct opstacks blocks = TAILQ_HEAD_INITIALIZER(blocks);
63 d9d02161 2021-08-04 op static struct opstacks args = TAILQ_HEAD_INITIALIZER(args);
65 5de2d36f 2021-08-04 op #define STACK_HEIGHT 64
66 c734c0e9 2021-08-03 op static struct value vstack[STACK_HEIGHT];
67 c734c0e9 2021-08-03 op static int stackh;
69 f9cd3e06 2021-08-04 op static struct envs envs = TAILQ_HEAD_INITIALIZER(envs);
71 c734c0e9 2021-08-03 op static struct value v_false = {.type = V_NUM, .v = {.num = 0}};
72 c734c0e9 2021-08-03 op static struct value v_true = {.type = V_NUM, .v = {.num = 1}};
74 6019438a 2021-08-05 op static uint8_t lasttag;
76 dd1d2343 2021-08-05 op static int debug;
77 4315b20a 2021-08-05 op static int syntaxcheck;
79 f3289b7e 2021-08-06 op static const char *filler;
81 f9cd3e06 2021-08-04 op static inline void
82 f3289b7e 2021-08-06 op before_printing(void)
84 f3289b7e 2021-08-06 op if (filler != NULL) {
85 f3289b7e 2021-08-06 op printf("%s", filler);
86 f3289b7e 2021-08-06 op filler = NULL;
90 f3289b7e 2021-08-06 op static inline void
91 370b4f2e 2021-08-07 op check_for_output(void)
93 370b4f2e 2021-08-07 op static char buf[BUFSIZ];
94 370b4f2e 2021-08-07 op struct pollfd pfd;
97 370b4f2e 2021-08-07 op pfd.fd = child_out;
98 370b4f2e 2021-08-07 op pfd.events = POLLIN;
99 370b4f2e 2021-08-07 op if (poll(&pfd, 1, 0) == -1)
100 370b4f2e 2021-08-07 op fatal("poll");
102 370b4f2e 2021-08-07 op if (!(pfd.revents & POLLIN))
106 2305d578 2021-08-07 op if ((r = read(child_out, buf, sizeof(buf))) == -1) {
107 2305d578 2021-08-07 op if (errno == EAGAIN)
109 370b4f2e 2021-08-07 op fatal("read");
113 370b4f2e 2021-08-07 op before_printing();
114 370b4f2e 2021-08-07 op fwrite(buf, 1, r, stdout);
118 370b4f2e 2021-08-07 op static inline void
119 54736a95 2021-08-04 op peekn(int depth, struct value *v)
121 54736a95 2021-08-04 op if (depth > stackh)
122 54736a95 2021-08-04 op errx(1, "can't peek the stack at %d: underflow",
124 54736a95 2021-08-04 op memcpy(v, &vstack[stackh - depth], sizeof(*v));
127 54736a95 2021-08-04 op printf("peeking(%d) ", depth); pp_val(v); printf("\n");
131 54736a95 2021-08-04 op static inline void
132 f9cd3e06 2021-08-04 op popv(struct value *v)
134 f9cd3e06 2021-08-04 op if (stackh == 0)
135 f9cd3e06 2021-08-04 op errx(1, "can't pop the stack: underflow");
136 f9cd3e06 2021-08-04 op memcpy(v, &vstack[--stackh], sizeof(*v));
139 f9cd3e06 2021-08-04 op printf("popping "); pp_val(v); printf("\n");
143 f9cd3e06 2021-08-04 op static inline void
146 54736a95 2021-08-04 op struct value v;
148 54736a95 2021-08-04 op while (n-- > 0)
152 54736a95 2021-08-04 op static inline void
153 f9cd3e06 2021-08-04 op pushv(struct value *v)
155 f9cd3e06 2021-08-04 op if (stackh == STACK_HEIGHT)
156 f9cd3e06 2021-08-04 op errx(1, "can't push the stack: overflow");
159 f9cd3e06 2021-08-04 op printf("pushing "); pp_val(v); printf("\n");
162 f9cd3e06 2021-08-04 op memcpy(&vstack[stackh++], v, sizeof(*v));
165 f9cd3e06 2021-08-04 op static inline void
166 f9cd3e06 2021-08-04 op pushbool(int n)
168 f9cd3e06 2021-08-04 op pushv(n ? &v_true : &v_false);
171 a9e9e3f7 2021-08-07 op static inline void
172 a9e9e3f7 2021-08-07 op pushnum(int64_t n)
174 a9e9e3f7 2021-08-07 op struct value v;
176 a9e9e3f7 2021-08-07 op v.type = V_NUM;
181 d9d02161 2021-08-04 op static inline struct opstack *
182 d9d02161 2021-08-04 op pushstack(struct opstacks *stack)
184 d9d02161 2021-08-04 op struct opstack *ops;
186 d9d02161 2021-08-04 op ops = xcalloc(1, sizeof(*ops));
187 d9d02161 2021-08-04 op TAILQ_INSERT_HEAD(stack, ops, entry);
191 d9d02161 2021-08-04 op static inline struct opstack *
192 d9d02161 2021-08-04 op peek(struct opstacks *stack)
194 d9d02161 2021-08-04 op if (TAILQ_EMPTY(stack))
195 d9d02161 2021-08-04 op errx(1, "%s: args underflow", __func__);
197 d9d02161 2021-08-04 op return TAILQ_FIRST(stack);
200 d9d02161 2021-08-04 op static inline struct op *
201 d9d02161 2021-08-04 op finalize(struct opstacks *stack, int *argc)
203 d9d02161 2021-08-04 op struct opstack *ops;
204 d9d02161 2021-08-04 op struct op *op;
206 d9d02161 2021-08-04 op if (TAILQ_EMPTY(stack))
207 d9d02161 2021-08-04 op errx(1, "%s: args underflow", __func__);
209 d9d02161 2021-08-04 op ops = peek(stack);
210 d9d02161 2021-08-04 op TAILQ_REMOVE(&args, ops, entry);
211 d9d02161 2021-08-04 op op = ops->base.next;
213 d9d02161 2021-08-04 op if (argc != NULL)
214 d9d02161 2021-08-04 op *argc = ops->counter;
220 d9d02161 2021-08-04 op static inline void
221 d9d02161 2021-08-04 op push(struct opstacks *stack, struct op *op)
223 d9d02161 2021-08-04 op struct opstack *ops;
225 d9d02161 2021-08-04 op ops = peek(stack);
226 d9d02161 2021-08-04 op if (ops->last == NULL) {
227 d9d02161 2021-08-04 op ops->base.next = op;
228 d9d02161 2021-08-04 op ops->last = op;
230 d9d02161 2021-08-04 op ops->last->next = op;
231 d9d02161 2021-08-04 op ops->last = op;
234 d9d02161 2021-08-04 op ops->counter++;
237 f9cd3e06 2021-08-04 op static inline void
238 f9cd3e06 2021-08-04 op pushenv(void)
240 f9cd3e06 2021-08-04 op struct env *e;
242 f9cd3e06 2021-08-04 op e = xcalloc(1, sizeof(*e));
243 f9cd3e06 2021-08-04 op TAILQ_INSERT_HEAD(&envs, e, entry);
246 f9cd3e06 2021-08-04 op static inline struct env *
247 f9cd3e06 2021-08-04 op currentenv(void)
249 f9cd3e06 2021-08-04 op assert(!TAILQ_EMPTY(&envs));
250 f9cd3e06 2021-08-04 op return TAILQ_FIRST(&envs);
256 f9cd3e06 2021-08-04 op struct env *e;
257 f9cd3e06 2021-08-04 op struct binding *b, *tb;
259 f9cd3e06 2021-08-04 op e = currentenv();
260 f9cd3e06 2021-08-04 op TAILQ_REMOVE(&envs, e, entry);
262 0b3307f1 2021-08-08 op TAILQ_FOREACH_SAFE(b, &e->bindings, entry, tb)
268 f9cd3e06 2021-08-04 op static inline int
269 f9cd3e06 2021-08-04 op setvar(char *sym, struct op *op)
271 f9cd3e06 2021-08-04 op struct binding *b;
272 f9cd3e06 2021-08-04 op struct env *e;
273 511b3aa7 2021-08-07 op int ret, height;
275 511b3aa7 2021-08-07 op height = stackh;
276 f9cd3e06 2021-08-04 op if ((ret = eval(op)) != EVAL_OK)
279 511b3aa7 2021-08-07 op if (stackh != height + 1) {
280 511b3aa7 2021-08-07 op before_printing();
281 511b3aa7 2021-08-07 op printf("trying to assign to `%s' a void value: ", sym);
283 511b3aa7 2021-08-07 op printf("\n");
284 511b3aa7 2021-08-07 op return EVAL_ERR;
287 f9cd3e06 2021-08-04 op b = xcalloc(1, sizeof(*b));
288 f9cd3e06 2021-08-04 op b->name = sym;
289 f9cd3e06 2021-08-04 op popv(&b->val);
291 f9cd3e06 2021-08-04 op e = TAILQ_FIRST(&envs);
292 f9cd3e06 2021-08-04 op TAILQ_INSERT_HEAD(&e->bindings, b, entry);
294 f9cd3e06 2021-08-04 op return EVAL_OK;
297 6eb34747 2021-08-05 op static inline void
298 6eb34747 2021-08-05 op setvar_raw(char *sym, struct op *op)
300 6eb34747 2021-08-05 op struct binding *b;
301 6eb34747 2021-08-05 op struct env *e;
303 6eb34747 2021-08-05 op b = xcalloc(1, sizeof(*b));
304 6eb34747 2021-08-05 op b->name = sym;
307 6eb34747 2021-08-05 op e = TAILQ_FIRST(&envs);
308 6eb34747 2021-08-05 op TAILQ_INSERT_HEAD(&e->bindings, b, entry);
311 f9cd3e06 2021-08-04 op static inline int
312 f9cd3e06 2021-08-04 op getvar(const char *sym, struct value *v)
314 f9cd3e06 2021-08-04 op struct env *e;
315 f9cd3e06 2021-08-04 op struct binding *b;
317 f9cd3e06 2021-08-04 op TAILQ_FOREACH(e, &envs, entry) {
318 f9cd3e06 2021-08-04 op TAILQ_FOREACH(b, &e->bindings, entry) {
319 f9cd3e06 2021-08-04 op if (!strcmp(sym, b->name)) {
320 f9cd3e06 2021-08-04 op memcpy(v, &b->val, sizeof(*v));
321 f9cd3e06 2021-08-04 op return EVAL_OK;
326 f3289b7e 2021-08-06 op before_printing();
327 f9cd3e06 2021-08-04 op fprintf(stderr, "unbound variable %s\n", sym);
328 f9cd3e06 2021-08-04 op return EVAL_ERR;
331 6eb34747 2021-08-05 op static inline int
332 6eb34747 2021-08-05 op getvar_raw(const char *sym, struct op **raw)
334 6eb34747 2021-08-05 op struct env *e;
335 6eb34747 2021-08-05 op struct binding *b;
337 6eb34747 2021-08-05 op TAILQ_FOREACH(e, &envs, entry) {
338 6eb34747 2021-08-05 op TAILQ_FOREACH(b, &e->bindings, entry) {
339 6eb34747 2021-08-05 op if (!strcmp(sym, b->name)) {
340 6eb34747 2021-08-05 op *raw = b->raw;
341 6eb34747 2021-08-05 op return EVAL_OK;
346 6eb34747 2021-08-05 op return EVAL_ERR;
350 c734c0e9 2021-08-03 op global_set(char *sym, struct op *op)
352 f9cd3e06 2021-08-04 op struct binding *b;
353 f9cd3e06 2021-08-04 op struct env *e;
355 f9cd3e06 2021-08-04 op /* TODO: check for duplicates */
357 e599ffc4 2021-08-04 op if (op->type != OP_LITERAL &&
358 e599ffc4 2021-08-04 op (op->type == OP_CAST && op->v.cast.expr->type != OP_LITERAL))
361 f9cd3e06 2021-08-04 op b = xcalloc(1, sizeof(*b));
362 f9cd3e06 2021-08-04 op b->name = sym;
364 e599ffc4 2021-08-04 op /* it's only a cast on a literal! */
365 3a74f37a 2021-08-04 op if (op->type == OP_CAST) {
366 3a74f37a 2021-08-04 op if (eval(op) != EVAL_OK) {
370 3a74f37a 2021-08-04 op popv(&b->val);
372 e599ffc4 2021-08-04 op memcpy(&b->val, &op->v.literal, sizeof(b->val));
374 f9cd3e06 2021-08-04 op e = TAILQ_LAST(&envs, envs);
375 f9cd3e06 2021-08-04 op TAILQ_INSERT_HEAD(&e->bindings, b, entry);
381 c734c0e9 2021-08-03 op newop(int type)
383 c734c0e9 2021-08-03 op struct op *op;
385 f53af81c 2021-08-04 op op = xcalloc(1, sizeof(*op));
386 c734c0e9 2021-08-03 op op->type = type;
392 8a2def82 2021-08-12 op free_op_rec(struct op *op)
394 8a2def82 2021-08-12 op struct op *n;
396 8a2def82 2021-08-12 op while (op != NULL) {
397 8a2def82 2021-08-12 op n = op->next;
404 c734c0e9 2021-08-03 op free_op(struct op *op)
406 8a2def82 2021-08-12 op if (op == NULL)
409 8a2def82 2021-08-12 op switch (op->type) {
410 8a2def82 2021-08-12 op case OP_REST:
411 8a2def82 2021-08-12 op case OP_LITERAL:
412 8a2def82 2021-08-12 op case OP_VARGS:
414 8a2def82 2021-08-12 op case OP_ASSIGN:
415 8a2def82 2021-08-12 op free(op->v.assign.name);
416 8a2def82 2021-08-12 op free_op_rec(op->v.assign.expr);
418 8a2def82 2021-08-12 op case OP_ASSERT:
419 8a2def82 2021-08-12 op free_op_rec(op->v.assert);
421 8a2def82 2021-08-12 op case OP_FUNCALL:
422 8a2def82 2021-08-12 op free_op_rec(op->v.funcall.argv);
425 8a2def82 2021-08-12 op free(op->v.var);
427 8a2def82 2021-08-12 op case OP_CAST:
428 8a2def82 2021-08-12 op free_op_rec(op->v.cast.expr);
430 8a2def82 2021-08-12 op case OP_CMP_EQ:
431 8a2def82 2021-08-12 op free_op_rec(op->v.cmp_eq.a);
432 8a2def82 2021-08-12 op free_op_rec(op->v.cmp_eq.b);
434 8a2def82 2021-08-12 op case OP_FACCESS:
435 8a2def82 2021-08-12 op free_op_rec(op->v.faccess.expr);
436 8a2def82 2021-08-12 op free(op->v.faccess.field);
438 8a2def82 2021-08-12 op case OP_SFAIL:
439 8a2def82 2021-08-12 op free(op->v.sfail.msg);
440 8a2def82 2021-08-12 op free_op_rec(op->v.sfail.expr);
443 8a2def82 2021-08-12 op /* unreachable */
451 0e62706a 2021-08-04 op op_rest(void)
453 0e62706a 2021-08-04 op return newop(OP_REST);
457 c734c0e9 2021-08-03 op op_assign(char *sym, struct op *expr)
459 c734c0e9 2021-08-03 op struct op *op;
461 c734c0e9 2021-08-03 op op = newop(OP_ASSIGN);
462 c734c0e9 2021-08-03 op op->v.assign.name = sym;
463 c734c0e9 2021-08-03 op op->v.assign.expr = expr;
469 c734c0e9 2021-08-03 op op_assert(struct op *expr)
471 c734c0e9 2021-08-03 op struct op *op;
473 c734c0e9 2021-08-03 op op = newop(OP_ASSERT);
474 c734c0e9 2021-08-03 op op->v.assert = expr;
480 c734c0e9 2021-08-03 op op_var(char *sym)
482 c734c0e9 2021-08-03 op struct op *op;
484 c734c0e9 2021-08-03 op op = newop(OP_VAR);
485 c734c0e9 2021-08-03 op op->v.var = sym;
491 c734c0e9 2021-08-03 op op_lit_str(char *str)
493 c734c0e9 2021-08-03 op struct op *op;
495 c734c0e9 2021-08-03 op op = newop(OP_LITERAL);
496 dcb493d2 2021-08-04 op op->v.literal.type = V_STR;
497 c734c0e9 2021-08-03 op op->v.literal.v.str = str;
503 c734c0e9 2021-08-03 op op_lit_num(uint64_t n)
505 c734c0e9 2021-08-03 op struct op *op;
507 c734c0e9 2021-08-03 op op = newop(OP_LITERAL);
508 c734c0e9 2021-08-03 op op->v.literal.type = V_NUM;
509 c734c0e9 2021-08-03 op op->v.literal.v.num = n;
515 c734c0e9 2021-08-03 op op_cmp_eq(struct op *a, struct op *b)
517 c734c0e9 2021-08-03 op struct op *op;
519 c734c0e9 2021-08-03 op op = newop(OP_CMP_EQ);
520 c734c0e9 2021-08-03 op op->v.cmp_eq.a = a;
521 c734c0e9 2021-08-03 op op->v.cmp_eq.b = b;
527 c734c0e9 2021-08-03 op op_cast(struct op *expr, int totype)
529 c734c0e9 2021-08-03 op struct op *op;
531 c734c0e9 2021-08-03 op op = newop(OP_CAST);
532 c734c0e9 2021-08-03 op op->v.cast.expr = expr;
533 c734c0e9 2021-08-03 op op->v.cast.totype = totype;
539 acf1403a 2021-08-05 op op_faccess(struct op *expr, char *field)
541 acf1403a 2021-08-05 op struct op *op;
543 acf1403a 2021-08-05 op op = newop(OP_FACCESS);
544 acf1403a 2021-08-05 op op->v.faccess.expr = expr;
545 acf1403a 2021-08-05 op op->v.faccess.field = field;
551 8250ab19 2021-08-07 op op_sfail(struct op *expr, char *msg)
553 8250ab19 2021-08-07 op struct op *op;
555 8250ab19 2021-08-07 op op = newop(OP_SFAIL);
556 8250ab19 2021-08-07 op op->v.sfail.expr = expr;
557 8250ab19 2021-08-07 op op->v.sfail.msg = msg;
563 a9e9e3f7 2021-08-07 op op_vargs(void)
565 a9e9e3f7 2021-08-07 op struct op *op;
567 a9e9e3f7 2021-08-07 op op = newop(OP_VARGS);
573 da4de0c1 2021-08-04 op ppf_val(FILE *f, struct value *val)
577 c734c0e9 2021-08-03 op switch (val->type) {
579 da4de0c1 2021-08-04 op fprintf(f, "%s", val->v.str);
582 da4de0c1 2021-08-04 op fprintf(f, "\"%s\"", val->v.str);
585 ae3939af 2021-08-04 op fprintf(f, "%"PRIi64, val->v.num);
588 ae3939af 2021-08-04 op fprintf(f, "%"PRIu8, val->v.u8);
591 ae3939af 2021-08-04 op fprintf(f, "%"PRIu16, val->v.u16);
594 ae3939af 2021-08-04 op fprintf(f, "%"PRIu32, val->v.u32);
597 9820edbf 2021-08-06 op fprintf(f, "(");
598 9820edbf 2021-08-06 op for (i = 0; i < val->v.msg.len; ++i)
599 9820edbf 2021-08-06 op fprintf(f, "%x%s", val->v.msg.msg[i],
600 9820edbf 2021-08-06 op i == val->v.msg.len-1 ? "" : " ");
601 9820edbf 2021-08-06 op fprintf(f, ")");
603 983e73a6 2021-08-08 op case V_QIDVEC:
604 983e73a6 2021-08-08 op fprintf(f, "qids[n=%zu]", val->v.qidvec.len);
607 da4de0c1 2021-08-04 op fprintf(f, "<unknown value>");
613 da4de0c1 2021-08-04 op pp_val(struct value *val)
615 da4de0c1 2021-08-04 op ppf_val(stdout, val);
619 47c583a9 2021-08-07 op val_type(struct value *v)
621 47c583a9 2021-08-07 op switch (v->type) {
622 47c583a9 2021-08-07 op case V_SYM: return "symbol";
623 47c583a9 2021-08-07 op case V_STR: return "string";
624 47c583a9 2021-08-07 op case V_NUM: return "number";
625 47c583a9 2021-08-07 op case V_MSG: return "message";
626 47c583a9 2021-08-07 op case V_QID: return "qid";
627 47c583a9 2021-08-07 op case V_U8: return "u8";
628 47c583a9 2021-08-07 op case V_U16: return "u16";
629 47c583a9 2021-08-07 op case V_U32: return "u32";
630 47c583a9 2021-08-07 op default: return "unknown";
635 c734c0e9 2021-08-03 op val_trueish(struct value *a)
637 8ec0e863 2021-12-02 op if (val_isnum(a))
638 8ec0e863 2021-12-02 op return val_tonum(a);
643 c734c0e9 2021-08-03 op val_isnum(struct value *a)
645 c734c0e9 2021-08-03 op return a->type == V_NUM
646 c734c0e9 2021-08-03 op || a->type == V_U8
647 c734c0e9 2021-08-03 op || a->type == V_U16
648 c734c0e9 2021-08-03 op || a->type == V_U32;
652 309658f3 2021-08-04 op val_tonum(struct value *a)
654 309658f3 2021-08-04 op switch (a->type) {
655 309658f3 2021-08-04 op case V_NUM: return a->v.num;
656 309658f3 2021-08-04 op case V_U8: return a->v.u8;
657 309658f3 2021-08-04 op case V_U16: return a->v.u16;
658 309658f3 2021-08-04 op case V_U32: return a->v.u32;
660 f3289b7e 2021-08-06 op before_printing();
661 309658f3 2021-08-04 op fprintf(stderr, "%s: given value is not a number\n", __func__);
667 c734c0e9 2021-08-03 op val_eq(struct value *a, struct value *b)
669 c734c0e9 2021-08-03 op if (val_isnum(a) && val_isnum(b))
670 309658f3 2021-08-04 op return val_tonum(a) == val_tonum(b);
672 c734c0e9 2021-08-03 op if (a->type != b->type)
675 c734c0e9 2021-08-03 op switch (a->type) {
678 c734c0e9 2021-08-03 op return !strcmp(a->v.str, b->v.str);
684 da4de0c1 2021-08-04 op static inline const char *
685 da4de0c1 2021-08-04 op pp_totype(int totype)
688 da4de0c1 2021-08-04 op * Not all of these are valid cast type thought, including
689 da4de0c1 2021-08-04 op * every possibility only to aid debugging.
691 da4de0c1 2021-08-04 op switch (totype) {
692 da4de0c1 2021-08-04 op case V_STR: return "str";
693 da4de0c1 2021-08-04 op case V_SYM: return "sym";
694 da4de0c1 2021-08-04 op case V_NUM: return "num";
695 da4de0c1 2021-08-04 op case V_QID: return "qid";
696 da4de0c1 2021-08-04 op case V_U8: return "u8";
697 da4de0c1 2021-08-04 op case V_U16: return "u16";
698 da4de0c1 2021-08-04 op case V_U32: return "u32";
699 da4de0c1 2021-08-04 op default: return "unknown";
704 da4de0c1 2021-08-04 op val_cast(struct value *a, int totype)
708 ae3939af 2021-08-04 op #define NUMCAST(val, t, c, totype, max) do { \
709 ae3939af 2021-08-04 op if (val > max) { \
710 f3289b7e 2021-08-06 op before_printing(); \
711 ae3939af 2021-08-04 op fprintf(stderr, "can't cast %"PRIu64 \
712 ae3939af 2021-08-04 op " to %s\n", val, pp_totype(totype)); \
713 ae3939af 2021-08-04 op return EVAL_ERR; \
715 ae3939af 2021-08-04 op a->type = totype; \
716 ae3939af 2021-08-04 op a->v.t = (c)val; \
717 ae3939af 2021-08-04 op return EVAL_OK; \
720 652f7308 2021-08-07 op if (a->type == totype)
721 652f7308 2021-08-07 op return EVAL_OK;
723 da4de0c1 2021-08-04 op if (!val_isnum(a)) {
724 f3289b7e 2021-08-06 op before_printing();
725 04a9c38f 2021-08-04 op fprintf(stderr, "can't cast ");
726 da4de0c1 2021-08-04 op ppf_val(stderr, a);
727 da4de0c1 2021-08-04 op fprintf(stderr, " to type %s\n", pp_totype(totype));
728 da4de0c1 2021-08-04 op return EVAL_ERR;
731 da4de0c1 2021-08-04 op v = a->v.num;
732 da4de0c1 2021-08-04 op switch (totype) {
733 ae3939af 2021-08-04 op case V_U8: NUMCAST(v, u8, uint8_t, totype, UINT8_MAX);
734 ae3939af 2021-08-04 op case V_U16: NUMCAST(v, u16, uint16_t, totype, UINT16_MAX);
735 ae3939af 2021-08-04 op case V_U32: NUMCAST(v, u32, uint32_t, totype, UINT32_MAX);
737 f3289b7e 2021-08-06 op before_printing();
738 04a9c38f 2021-08-04 op fprintf(stderr, "can't cast %"PRIu64" to %s\n",
739 da4de0c1 2021-08-04 op v, pp_totype(totype));
740 da4de0c1 2021-08-04 op return EVAL_ERR;
743 da4de0c1 2021-08-04 op #undef NUMCAST
747 acf1403a 2021-08-05 op val_faccess(struct value *a, const char *field, struct value *ret)
749 983e73a6 2021-08-08 op uint8_t mtype;
750 983e73a6 2021-08-08 op uint16_t len;
751 983e73a6 2021-08-08 op const char *errstr;
753 d8d5d55f 2021-08-07 op #define MSGTYPE(m) *(m.msg + 4) /* skip the length */
755 acf1403a 2021-08-05 op switch (a->type) {
757 acf1403a 2021-08-05 op /* TODO: add path. needs uint64_t values thought! */
758 acf1403a 2021-08-05 op if (!strcmp(field, "vers")) {
759 acf1403a 2021-08-05 op ret->type = V_U32;
760 acf1403a 2021-08-05 op memcpy(&ret->v.u32, a->v.qid+1, 4);
761 47c583a9 2021-08-07 op return EVAL_OK;
762 acf1403a 2021-08-05 op } else if (!strcmp(field, "type")) {
763 acf1403a 2021-08-05 op ret->type = V_U8;
764 acf1403a 2021-08-05 op ret->v.u8 = *a->v.qid;
765 47c583a9 2021-08-07 op return EVAL_OK;
770 a4273600 2021-08-07 op mtype = MSGTYPE(a->v.msg);
771 d1355133 2021-08-07 op if (!strcmp(field, "type")) {
772 d1355133 2021-08-07 op ret->type = V_U8;
773 d8d5d55f 2021-08-07 op ret->v.u8 = MSGTYPE(a->v.msg);
774 47c583a9 2021-08-07 op return EVAL_OK;
775 2cb7d94c 2021-08-07 op } else if (!strcmp(field, "tag")) {
776 2cb7d94c 2021-08-07 op ret->type = V_U16;
777 2cb7d94c 2021-08-07 op memcpy(&ret->v.u16, &a->v.msg.msg[5], 2);
778 2cb7d94c 2021-08-07 op ret->v.u16 = le16toh(ret->v.u16);
779 d8d5d55f 2021-08-07 op return EVAL_OK;
780 a4273600 2021-08-07 op } else if (!strcmp(field, "msize") && mtype == Rversion) {
781 d8d5d55f 2021-08-07 op ret->type = V_U32;
782 d8d5d55f 2021-08-07 op memcpy(&ret->v.u32, &a->v.msg.msg[7], 4);
783 d8d5d55f 2021-08-07 op ret->v.u32 = le32toh(ret->v.u32);
784 2cb7d94c 2021-08-07 op return EVAL_OK;
785 a4273600 2021-08-07 op } else if (!strcmp(field, "qid") && mtype == Rattach) {
786 a4273600 2021-08-07 op ret->type = V_QID;
787 a4273600 2021-08-07 op memcpy(&ret->v.qid, &a->v.msg.msg[7], QIDSIZE);
788 983e73a6 2021-08-08 op return EVAL_OK;
789 983e73a6 2021-08-08 op } else if (!strcmp(field, "nwqid") && mtype == Rwalk) {
790 983e73a6 2021-08-08 op ret->type = V_U16;
791 983e73a6 2021-08-08 op memcpy(&ret->v.u16, &a->v.msg.msg[7], 2);
792 983e73a6 2021-08-08 op ret->v.u16 = le16toh(ret->v.u16);
793 a4273600 2021-08-07 op return EVAL_OK;
794 983e73a6 2021-08-08 op } else if (!strcmp(field, "wqid") && mtype == Rwalk) {
795 983e73a6 2021-08-08 op ret->type = V_QIDVEC;
796 983e73a6 2021-08-08 op ret->v.qidvec.start = &a->v.msg.msg[9];
797 983e73a6 2021-08-08 op memcpy(&len, &a->v.msg.msg[7], 2);
798 983e73a6 2021-08-08 op len = le16toh(len);
799 983e73a6 2021-08-08 op ret->v.qidvec.len = len;
800 983e73a6 2021-08-08 op return EVAL_OK;
804 983e73a6 2021-08-08 op case V_QIDVEC:
805 983e73a6 2021-08-08 op len = strtonum(field, 0, MAXWELEM, &errstr);
806 983e73a6 2021-08-08 op if (errstr != NULL) {
807 983e73a6 2021-08-08 op before_printing();
808 983e73a6 2021-08-08 op printf("can't access qid #%s: %s\n", field, errstr);
809 983e73a6 2021-08-08 op return EVAL_ERR;
812 983e73a6 2021-08-08 op if (len >= a->v.qidvec.len) {
813 983e73a6 2021-08-08 op before_printing();
814 983e73a6 2021-08-08 op printf("can't access qid #%d: out-of-bound "
815 983e73a6 2021-08-08 op "(max %zu)\n", len, a->v.qidvec.len);
816 983e73a6 2021-08-08 op return EVAL_ERR;
819 983e73a6 2021-08-08 op ret->type = V_QID;
820 983e73a6 2021-08-08 op memcpy(&ret->v.qid, a->v.qidvec.start + len * QIDSIZE,
823 983e73a6 2021-08-08 op return EVAL_OK;
829 47c583a9 2021-08-07 op before_printing();
830 47c583a9 2021-08-07 op printf("can't access field `%s' on type %s (", field, val_type(a));
832 47c583a9 2021-08-07 op printf(")\n");
833 47c583a9 2021-08-07 op return EVAL_ERR;
835 d8d5d55f 2021-08-07 op #undef MSGTYPE
839 c734c0e9 2021-08-03 op pp_op(struct op *op)
841 3e23847c 2021-08-04 op struct op *aux;
843 c734c0e9 2021-08-03 op switch (op->type) {
844 0e62706a 2021-08-04 op case OP_REST:
845 0e62706a 2021-08-04 op printf("...");
847 c734c0e9 2021-08-03 op case OP_ASSIGN:
848 c734c0e9 2021-08-03 op printf("%s = ", op->v.assign.name);
849 c734c0e9 2021-08-03 op pp_op(op->v.assign.expr);
851 c734c0e9 2021-08-03 op case OP_ASSERT:
852 c734c0e9 2021-08-03 op printf("assert ");
853 c734c0e9 2021-08-03 op pp_op(op->v.assert);
855 c734c0e9 2021-08-03 op case OP_FUNCALL:
856 3e23847c 2021-08-04 op printf("funcall %s(", op->v.funcall.proc->name);
857 3e23847c 2021-08-04 op for (aux = op->v.funcall.argv; aux != NULL; aux = aux->next) {
859 3e23847c 2021-08-04 op if (aux->next != NULL)
860 3e23847c 2021-08-04 op printf(", ");
864 c734c0e9 2021-08-03 op case OP_LITERAL:
865 c734c0e9 2021-08-03 op pp_val(&op->v.literal);
868 c734c0e9 2021-08-03 op printf("%s", op->v.var);
870 c734c0e9 2021-08-03 op case OP_CAST:
871 c734c0e9 2021-08-03 op pp_op(op->v.cast.expr);
873 c734c0e9 2021-08-03 op switch (op->v.cast.totype) {
874 c734c0e9 2021-08-03 op case V_U8: printf("u8"); break;
875 c734c0e9 2021-08-03 op case V_U16: printf("u16"); break;
876 c734c0e9 2021-08-03 op case V_U32: printf("u32"); break;
877 c734c0e9 2021-08-03 op case V_STR: printf("str"); break;
878 c734c0e9 2021-08-03 op default: printf("???"); break;
881 c734c0e9 2021-08-03 op case OP_CMP_EQ:
882 c734c0e9 2021-08-03 op pp_op(op->v.cmp_eq.a);
883 c734c0e9 2021-08-03 op printf(" == ");
884 c734c0e9 2021-08-03 op pp_op(op->v.cmp_eq.b);
886 acf1403a 2021-08-05 op case OP_FACCESS:
887 acf1403a 2021-08-05 op pp_op(op->v.faccess.expr);
888 acf1403a 2021-08-05 op printf(".%s", op->v.faccess.field);
890 8250ab19 2021-08-07 op case OP_SFAIL:
891 8250ab19 2021-08-07 op printf("should-fail ");
892 8250ab19 2021-08-07 op pp_op(op->v.sfail.expr);
893 8250ab19 2021-08-07 op if (op->v.sfail.msg != NULL)
894 8250ab19 2021-08-07 op printf(": \"%s\"", op->v.sfail.msg);
896 a9e9e3f7 2021-08-07 op case OP_VARGS:
897 a9e9e3f7 2021-08-07 op printf("vargs");
900 c734c0e9 2021-08-03 op printf(" ???[%d] ", op->type);
905 c734c0e9 2021-08-03 op pp_block(struct op *op)
907 c734c0e9 2021-08-03 op while (op != NULL) {
908 c734c0e9 2021-08-03 op printf("> ");
910 c734c0e9 2021-08-03 op printf("\n");
912 c734c0e9 2021-08-03 op op = op->next;
917 c734c0e9 2021-08-03 op eval(struct op *op)
919 c734c0e9 2021-08-03 op struct value a, b;
920 c734c0e9 2021-08-03 op struct proc *proc;
921 29c35556 2021-08-07 op struct op *t, *tnext;
926 c734c0e9 2021-08-03 op printf("\n");
929 c734c0e9 2021-08-03 op switch (op->type) {
930 6eb34747 2021-08-05 op case OP_REST:
932 c31f55f5 2021-08-07 op * Try to load the rest argument. Note that it can be
935 c31f55f5 2021-08-07 op if ((ret = getvar_raw("...", &t)) == EVAL_OK)
936 c31f55f5 2021-08-07 op if ((ret = eval(t)) != EVAL_OK)
940 c734c0e9 2021-08-03 op case OP_ASSIGN:
941 f9cd3e06 2021-08-04 op ret = setvar(op->v.assign.name, op->v.assign.expr);
942 f9cd3e06 2021-08-04 op if (ret != EVAL_OK)
946 c734c0e9 2021-08-03 op case OP_ASSERT:
947 333d8d7d 2021-08-04 op if ((ret = eval(op->v.assert)) != EVAL_OK)
950 c734c0e9 2021-08-03 op if (!val_trueish(&a)) {
951 f3289b7e 2021-08-06 op before_printing();
952 c734c0e9 2021-08-03 op printf("assertion failed: ");
953 c734c0e9 2021-08-03 op pp_op(op->v.assert);
954 c734c0e9 2021-08-03 op printf("\n");
955 333d8d7d 2021-08-04 op return EVAL_ERR;
959 c734c0e9 2021-08-03 op case OP_FUNCALL:
960 349fd94e 2021-08-05 op /* assume airity matches */
962 f9cd3e06 2021-08-04 op proc = op->v.funcall.proc;
963 9af2cde2 2021-08-04 op if (proc->nativefn != NULL) {
965 6eb34747 2021-08-05 op * Push arguments on the stack for builtin
966 6eb34747 2021-08-05 op * functions. Counting the height of the
967 6eb34747 2021-08-05 op * stack is done to compute the correct number
968 6eb34747 2021-08-05 op * in the vararg case. argc only counts the
969 6eb34747 2021-08-05 op * "syntactical" arguments, i.e. foo(x, ...)
970 6eb34747 2021-08-05 op * has argc == 2, but at runtime argc may be
971 6eb34747 2021-08-05 op * 1, 2 or a greater number!
975 219cbeaa 2021-08-04 op t = op->v.funcall.argv;
976 219cbeaa 2021-08-04 op if (t != NULL && (ret = eval(t)) != EVAL_OK)
978 6eb34747 2021-08-05 op i = stackh - i;
980 6eb34747 2021-08-05 op assert(i >= 0);
982 6eb34747 2021-08-05 op if ((ret = proc->nativefn(i))
986 91cfe5ca 2021-08-06 op if (proc->body == NULL) {
987 91cfe5ca 2021-08-06 op before_printing();
988 91cfe5ca 2021-08-06 op printf("warn: calling the empty proc `%s'\n",
995 219cbeaa 2021-08-04 op for (t = op->v.funcall.argv, i = 0;
997 219cbeaa 2021-08-04 op t = t->next, i++) {
999 6eb34747 2021-08-05 op * Push a pseudo variable `...' (and
1000 6eb34747 2021-08-05 op * don't evaluate it) in the vararg
1001 6eb34747 2021-08-05 op * case. A special case is when the
1002 6eb34747 2021-08-05 op * variable is itself `...'.
1004 6eb34747 2021-08-05 op if (proc->vararg && i == proc->minargs) {
1005 6eb34747 2021-08-05 op if (t->type != OP_REST)
1006 6eb34747 2021-08-05 op setvar_raw(xstrdup("..."), t);
1011 29c35556 2021-08-07 op * The arguments are a linked list of
1012 29c35556 2021-08-07 op * ops. Setvar will call eval that
1013 29c35556 2021-08-07 op * will evaluate *all* the arguments.
1014 29c35556 2021-08-07 op * The dance here that sets next to
1015 29c35556 2021-08-07 op * NULL and then restores it is to
1016 29c35556 2021-08-07 op * avoid this behaviour.
1018 29c35556 2021-08-07 op tnext = t->next;
1019 29c35556 2021-08-07 op t->next = NULL;
1020 29c35556 2021-08-07 op ret = setvar(proc->args[i], t);
1021 29c35556 2021-08-07 op t->next = tnext;
1023 29c35556 2021-08-07 op if (ret != EVAL_OK)
1027 9af2cde2 2021-08-04 op if ((ret = eval(proc->body)) != EVAL_OK)
1035 c734c0e9 2021-08-03 op case OP_LITERAL:
1036 c734c0e9 2021-08-03 op pushv(&op->v.literal);
1039 c734c0e9 2021-08-03 op case OP_VAR:
1040 f9cd3e06 2021-08-04 op if ((ret = getvar(op->v.var, &a)) != EVAL_OK)
1045 c734c0e9 2021-08-03 op case OP_CAST:
1046 da4de0c1 2021-08-04 op if ((ret = eval(op->v.cast.expr)) != EVAL_OK)
1049 da4de0c1 2021-08-04 op if ((ret = val_cast(&a, op->v.cast.totype)) != EVAL_OK)
1054 c734c0e9 2021-08-03 op case OP_CMP_EQ:
1055 333d8d7d 2021-08-04 op if ((ret = eval(op->v.cmp_eq.a)) != EVAL_OK)
1057 333d8d7d 2021-08-04 op if ((ret = eval(op->v.cmp_eq.b)) != EVAL_OK)
1062 c734c0e9 2021-08-03 op pushbool(val_eq(&a, &b));
1066 acf1403a 2021-08-05 op case OP_FACCESS:
1067 acf1403a 2021-08-05 op if ((ret = eval(op->v.faccess.expr)) != EVAL_OK)
1070 acf1403a 2021-08-05 op if ((ret = val_faccess(&a, op->v.faccess.field, &b))
1076 8250ab19 2021-08-07 op case OP_SFAIL:
1077 8250ab19 2021-08-07 op if ((ret = eval(op->v.sfail.expr)) == EVAL_OK) {
1078 8250ab19 2021-08-07 op before_printing();
1079 8250ab19 2021-08-07 op printf("expecting failure");
1080 8250ab19 2021-08-07 op if (op->v.sfail.msg != NULL)
1081 8250ab19 2021-08-07 op printf(" \"%s\"", op->v.sfail.msg);
1082 8250ab19 2021-08-07 op printf("\n");
1083 8250ab19 2021-08-07 op printf("expression: ");
1084 8250ab19 2021-08-07 op pp_op(op->v.sfail.expr);
1085 8250ab19 2021-08-07 op printf("\n");
1086 8250ab19 2021-08-07 op return EVAL_ERR;
1088 8250ab19 2021-08-07 op if (ret == EVAL_SKIP)
1092 a9e9e3f7 2021-08-07 op case OP_VARGS:
1093 a9e9e3f7 2021-08-07 op if ((ret = getvar_raw("...", &t)) == EVAL_OK) {
1094 a9e9e3f7 2021-08-07 op for (i = 0; t != NULL; t = t->next)
1102 f3289b7e 2021-08-06 op before_printing();
1103 e9f9c9b1 2021-08-04 op fprintf(stderr, "invalid op, aborting.\n");
1107 c734c0e9 2021-08-03 op if (op->next)
1108 c734c0e9 2021-08-03 op return eval(op->next);
1109 333d8d7d 2021-08-04 op return EVAL_OK;
1113 d9d02161 2021-08-04 op prepare_funcall(void)
1115 d9d02161 2021-08-04 op pushstack(&args);
1119 c734c0e9 2021-08-03 op push_arg(struct op *op)
1121 d9d02161 2021-08-04 op push(&args, op);
1125 d9d02161 2021-08-04 op op_funcall(struct proc *proc)
1127 d9d02161 2021-08-04 op struct op *op, *argv;
1130 d9d02161 2021-08-04 op argv = finalize(&args, &argc);
1132 c734c0e9 2021-08-03 op op = newop(OP_FUNCALL);
1133 c734c0e9 2021-08-03 op op->v.funcall.proc = proc;
1134 d9d02161 2021-08-04 op op->v.funcall.argv = argv;
1135 d9d02161 2021-08-04 op op->v.funcall.argc = argc;
1141 54736a95 2021-08-04 op add_builtin_proc(const char *name, int (*fn)(int), int argc, int vararg)
1143 c734c0e9 2021-08-03 op struct proc *proc;
1145 c734c0e9 2021-08-03 op proc = xcalloc(1, sizeof(*proc));
1146 c734c0e9 2021-08-03 op proc->name = xstrdup(name);
1147 c734c0e9 2021-08-03 op proc->nativefn = fn;
1148 126080b7 2021-08-04 op proc->minargs = argc;
1149 54736a95 2021-08-04 op proc->vararg = vararg;
1151 c734c0e9 2021-08-03 op TAILQ_INSERT_HEAD(&procs, proc, entry);
1155 d9d02161 2021-08-04 op prepare_proc(void)
1157 d9d02161 2021-08-04 op pushstack(&args);
1161 c734c0e9 2021-08-03 op proc_setup_body(void)
1163 d9d02161 2021-08-04 op struct opstack *argv;
1164 d9d02161 2021-08-04 op struct op *op;
1167 d9d02161 2021-08-04 op argv = peek(&args);
1168 d9d02161 2021-08-04 op for (i = 0, op = argv->base.next; op != NULL; i++) {
1170 0e62706a 2021-08-04 op * TODO: should free the whole list on error but..,
1171 0e62706a 2021-08-04 op * we're gonna exit real soon(tm)!
1173 0e62706a 2021-08-04 op if (op->type != OP_VAR && op->type != OP_REST)
1176 c8878182 2021-08-04 op op = op->next;
1179 d9d02161 2021-08-04 op assert(i == argv->counter);
1180 d9d02161 2021-08-04 op pushstack(&blocks);
1185 d9d02161 2021-08-04 op proc_done(char *name)
1187 d9d02161 2021-08-04 op struct proc *proc;
1188 d9d02161 2021-08-04 op struct op *op, *next, *argv, *body;
1189 d9d02161 2021-08-04 op int i, argc;
1191 d9d02161 2021-08-04 op argv = finalize(&args, &argc);
1192 d9d02161 2021-08-04 op body = finalize(&blocks, NULL);
1194 d9d02161 2021-08-04 op proc = xcalloc(1, sizeof(*proc));
1195 d9d02161 2021-08-04 op proc->name = name;
1196 d9d02161 2021-08-04 op proc->minargs = argc;
1198 b99bdaa1 2021-08-04 op for (i = 0, op = argv; op != NULL; ++i) {
1199 0e62706a 2021-08-04 op if (op->type == OP_REST) {
1200 0e62706a 2021-08-04 op proc->vararg = 1;
1201 0e62706a 2021-08-04 op proc->minargs = i;
1205 d9d02161 2021-08-04 op proc->args[i] = xstrdup(op->v.var);
1207 d9d02161 2021-08-04 op next = op->next;
1208 d9d02161 2021-08-04 op free_op(op);
1211 0e62706a 2021-08-04 op assert(i == argc || (proc->vararg && i == proc->minargs));
1213 d9d02161 2021-08-04 op proc->body = body;
1215 d9d02161 2021-08-04 op TAILQ_INSERT_HEAD(&procs, proc, entry);
1219 c734c0e9 2021-08-03 op block_push(struct op *op)
1221 d9d02161 2021-08-04 op push(&blocks, op);
1224 c734c0e9 2021-08-03 op struct proc *
1225 c734c0e9 2021-08-03 op proc_by_name(const char *name)
1227 c734c0e9 2021-08-03 op struct proc *p;
1229 c734c0e9 2021-08-03 op TAILQ_FOREACH(p, &procs, entry) {
1230 c734c0e9 2021-08-03 op if (!strcmp(p->name, name))
1234 c734c0e9 2021-08-03 op return NULL;
1238 d9d02161 2021-08-04 op prepare_test(void)
1240 d9d02161 2021-08-04 op pushstack(&blocks);
1244 0b453b63 2021-08-07 op test_done(int shouldfail, char *name, char *dir)
1246 d9d02161 2021-08-04 op struct test *test;
1248 d9d02161 2021-08-04 op test = xcalloc(1, sizeof(*test));
1249 0b453b63 2021-08-07 op test->shouldfail = shouldfail;
1250 d9d02161 2021-08-04 op test->name = name;
1251 d9d02161 2021-08-04 op test->dir = dir;
1252 d9d02161 2021-08-04 op test->body = finalize(&blocks, NULL);
1254 5e5c37b5 2021-08-04 op if (TAILQ_EMPTY(&tests))
1255 5e5c37b5 2021-08-04 op TAILQ_INSERT_HEAD(&tests, test, entry);
1257 5e5c37b5 2021-08-04 op TAILQ_INSERT_TAIL(&tests, test, entry);
1263 54736a95 2021-08-04 op builtin_print(int argc)
1265 54736a95 2021-08-04 op struct value v;
1268 f3289b7e 2021-08-06 op before_printing();
1270 54736a95 2021-08-04 op for (i = argc; i > 0; --i) {
1271 54736a95 2021-08-04 op peekn(i, &v);
1272 54736a95 2021-08-04 op if (v.type == V_STR)
1273 54736a95 2021-08-04 op printf("%s", v.v.str);
1276 54736a95 2021-08-04 op printf(" ");
1279 ce030684 2021-08-04 op printf("\n");
1281 54736a95 2021-08-04 op popvn(argc);
1283 dd1d2343 2021-08-05 op return EVAL_OK;
1287 dd1d2343 2021-08-05 op builtin_debug(int argc)
1290 dd1d2343 2021-08-05 op return builtin_print(argc);
1292 dd1d2343 2021-08-05 op popvn(argc);
1293 333d8d7d 2021-08-04 op return EVAL_OK;
1297 ce030684 2021-08-04 op builtin_skip(int argc)
1299 ce030684 2021-08-04 op return EVAL_SKIP;
1303 6019438a 2021-08-05 op builtin_iota(int argc)
1305 6019438a 2021-08-05 op struct value v;
1307 582b509f 2021-08-07 op v.type = V_U16;
1308 582b509f 2021-08-07 op if ((v.v.u16 = ++lasttag) == 255)
1309 582b509f 2021-08-07 op v.v.u16 = ++lasttag;
1312 6019438a 2021-08-05 op return EVAL_OK;
1316 9820edbf 2021-08-06 op builtin_send(int argc)
1318 9820edbf 2021-08-06 op struct ibuf *buf;
1319 9820edbf 2021-08-06 op struct value v;
1320 9820edbf 2021-08-06 op uint32_t len;
1321 9820edbf 2021-08-06 op uint16_t slen;
1324 370b4f2e 2021-08-07 op check_for_output();
1327 9820edbf 2021-08-06 op * Compute the length of the packet. 4 is for the initial
1328 9820edbf 2021-08-06 op * length field
1332 9820edbf 2021-08-06 op for (i = argc; i > 0; --i) {
1333 9820edbf 2021-08-06 op peekn(i, &v);
1334 9820edbf 2021-08-06 op switch (v.type) {
1336 9820edbf 2021-08-06 op len += 2; /* count */
1337 9820edbf 2021-08-06 op len += strlen(v.v.str);
1353 9820edbf 2021-08-06 op before_printing();
1354 9820edbf 2021-08-06 op printf("%s: can't serialize ", __func__);
1356 9820edbf 2021-08-06 op printf("\n");
1357 9820edbf 2021-08-06 op return EVAL_ERR;
1361 9820edbf 2021-08-06 op if (len > UINT16_MAX) {
1362 9820edbf 2021-08-06 op before_printing();
1363 9820edbf 2021-08-06 op printf("%s: message size too long: got %d when max is %d\n",
1364 9820edbf 2021-08-06 op __func__, len, UINT16_MAX);
1365 9820edbf 2021-08-06 op return EVAL_ERR;
1368 9820edbf 2021-08-06 op if ((buf = imsg_create(&ibuf, IMSG_BUF, 0, 0, len)) == NULL)
1369 9820edbf 2021-08-06 op fatal("imsg_create(%d)", len);
1371 34aba5f7 2021-08-06 op len = htole32(len);
1372 9820edbf 2021-08-06 op imsg_add(buf, &len, sizeof(len));
1374 9820edbf 2021-08-06 op for (i = argc; i > 0; --i) {
1375 9820edbf 2021-08-06 op peekn(i, &v);
1376 9820edbf 2021-08-06 op switch (v.type) {
1378 9820edbf 2021-08-06 op slen = strlen(v.v.str);
1379 9820edbf 2021-08-06 op slen = htole16(slen);
1380 9820edbf 2021-08-06 op imsg_add(buf, &slen, sizeof(slen));
1381 9820edbf 2021-08-06 op imsg_add(buf, v.v.str, strlen(v.v.str));
1385 9820edbf 2021-08-06 op imsg_add(buf, &v.v.u8, 1);
1389 9820edbf 2021-08-06 op v.v.u16 = htole16(v.v.u16);
1390 9820edbf 2021-08-06 op imsg_add(buf, &v.v.u16, 2);
1394 9820edbf 2021-08-06 op v.v.u32 = htole32(v.v.u32);
1395 9820edbf 2021-08-06 op imsg_add(buf, &v.v.u32, 4);
1400 9820edbf 2021-08-06 op imsg_close(&ibuf, buf);
1402 9820edbf 2021-08-06 op if (imsg_flush(&ibuf) == -1) {
1404 9820edbf 2021-08-06 op before_printing();
1405 9820edbf 2021-08-06 op printf("%s: imsg_flush failed: %s\n", __func__, strerror(i));
1406 9820edbf 2021-08-06 op return EVAL_ERR;
1409 370b4f2e 2021-08-07 op check_for_output();
1410 9820edbf 2021-08-06 op return EVAL_OK;
1414 9820edbf 2021-08-06 op builtin_recv(int argc)
1416 9820edbf 2021-08-06 op struct pollfd pfd;
1417 9820edbf 2021-08-06 op struct value v;
1418 9820edbf 2021-08-06 op struct imsg imsg;
1419 9820edbf 2021-08-06 op ssize_t n, datalen;
1422 9820edbf 2021-08-06 op if (lastmsg != NULL) {
1423 9820edbf 2021-08-06 op free(lastmsg);
1424 9820edbf 2021-08-06 op lastmsg = NULL;
1427 9820edbf 2021-08-06 op pfd.fd = ibuf.fd;
1428 9820edbf 2021-08-06 op pfd.events = POLLIN;
1429 9820edbf 2021-08-06 op if (poll(&pfd, 1, INFTIM) == -1) {
1430 9820edbf 2021-08-06 op serrno = errno;
1431 9820edbf 2021-08-06 op before_printing();
1432 9820edbf 2021-08-06 op printf("%s: poll failed: %s\n", __func__, strerror(serrno));
1433 9820edbf 2021-08-06 op return EVAL_ERR;
1437 9820edbf 2021-08-06 op if ((n = imsg_read(&ibuf)) == -1) {
1438 9820edbf 2021-08-06 op if (errno == EAGAIN)
1440 9820edbf 2021-08-06 op fatal("imsg_read");
1442 9820edbf 2021-08-06 op if (n == 0) {
1444 9820edbf 2021-08-06 op before_printing();
1445 9820edbf 2021-08-06 op printf("child disconnected\n");
1446 9820edbf 2021-08-06 op return EVAL_ERR;
1449 eed75362 2021-08-06 op nextmessage:
1450 370b4f2e 2021-08-07 op check_for_output();
1452 9820edbf 2021-08-06 op /* read only one message */
1453 9820edbf 2021-08-06 op if ((n = imsg_get(&ibuf, &imsg)) == -1)
1454 9820edbf 2021-08-06 op fatal("imsg_get");
1456 9820edbf 2021-08-06 op goto disconnect;
1458 9820edbf 2021-08-06 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1459 9820edbf 2021-08-06 op switch (imsg.hdr.type) {
1460 9820edbf 2021-08-06 op case IMSG_BUF:
1461 9820edbf 2021-08-06 op v.type = V_MSG;
1462 9820edbf 2021-08-06 op if ((v.v.msg.msg = malloc(datalen)) == NULL)
1463 9820edbf 2021-08-06 op fatal("malloc");
1464 9820edbf 2021-08-06 op memcpy(v.v.msg.msg, imsg.data, datalen);
1465 9820edbf 2021-08-06 op v.v.msg.len = datalen;
1467 9820edbf 2021-08-06 op imsg_free(&imsg);
1468 9820edbf 2021-08-06 op return EVAL_OK;
1470 9820edbf 2021-08-06 op case IMSG_CLOSE:
1471 9820edbf 2021-08-06 op before_printing();
1472 9820edbf 2021-08-06 op printf("subprocess closed the connection\n");
1473 9820edbf 2021-08-06 op imsg_free(&imsg);
1474 9820edbf 2021-08-06 op return EVAL_ERR;
1476 eed75362 2021-08-06 op case IMSG_MSIZE:
1477 eed75362 2021-08-06 op imsg_free(&imsg);
1478 eed75362 2021-08-06 op goto nextmessage;
1481 9820edbf 2021-08-06 op before_printing();
1482 9820edbf 2021-08-06 op printf("got unknown message from subprocess: %d\n",
1483 9820edbf 2021-08-06 op imsg.hdr.type);
1484 9820edbf 2021-08-06 op imsg_free(&imsg);
1485 9820edbf 2021-08-06 op return EVAL_ERR;
1489 9820edbf 2021-08-06 op static pid_t
1490 9820edbf 2021-08-06 op spawn_client_proc(void)
1492 9820edbf 2021-08-06 op const char *argv[4];
1493 370b4f2e 2021-08-07 op int p[2], out[2], argc = 0;
1496 370b4f2e 2021-08-07 op if (child_out != -1)
1497 370b4f2e 2021-08-07 op close(child_out);
1499 9820edbf 2021-08-06 op if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
1500 9820edbf 2021-08-06 op PF_UNSPEC, p) == -1)
1501 370b4f2e 2021-08-07 op fatal("socketpair");
1503 370b4f2e 2021-08-07 op if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
1504 370b4f2e 2021-08-07 op PF_UNSPEC, out) == -1)
1505 9820edbf 2021-08-06 op fatal("socketpair");
1507 9820edbf 2021-08-06 op switch (pid = fork()) {
1509 9820edbf 2021-08-06 op fatal("cannot fork");
1513 9820edbf 2021-08-06 op close(p[1]);
1514 370b4f2e 2021-08-07 op close(out[1]);
1515 370b4f2e 2021-08-07 op child_out = out[0];
1516 9820edbf 2021-08-06 op if (ibuf_inuse) {
1517 9820edbf 2021-08-06 op msgbuf_clear(&ibuf.w);
1518 9820edbf 2021-08-06 op close(ibuf.fd);
1520 9820edbf 2021-08-06 op imsg_init(&ibuf, p[0]);
1521 9820edbf 2021-08-06 op ibuf_inuse = 1;
1525 9820edbf 2021-08-06 op close(p[0]);
1526 370b4f2e 2021-08-07 op close(out[0]);
1528 370b4f2e 2021-08-07 op if (dup2(out[1], 1) == -1 ||
1529 370b4f2e 2021-08-07 op dup2(out[1], 2) == -1)
1530 370b4f2e 2021-08-07 op fatal("dup2");
1532 9820edbf 2021-08-06 op if (p[1] != 3) {
1533 9820edbf 2021-08-06 op if (dup2(p[1], 3) == -1)
1534 9820edbf 2021-08-06 op fatal("cannot setup imsg fd");
1535 9820edbf 2021-08-06 op } else if (fcntl(F_SETFD, 0) == -1)
1536 9820edbf 2021-08-06 op fatal("cannot setup imsg fd");
1538 9820edbf 2021-08-06 op argv[argc++] = argv0;
1539 9820edbf 2021-08-06 op argv[argc++] = "-Tc";
1542 9820edbf 2021-08-06 op argv[argc++] = "-v";
1545 9820edbf 2021-08-06 op argv[argc++] = NULL;
1547 9820edbf 2021-08-06 op execvp(argv0, (char *const *)argv);
1548 9820edbf 2021-08-06 op fatal("execvp");
1552 9820edbf 2021-08-06 op prepare_child_for_test(struct test *t)
1554 9820edbf 2021-08-06 op struct passwd *pw;
1555 9820edbf 2021-08-06 op struct stat sb;
1557 9820edbf 2021-08-06 op if (stat(t->dir, &sb) == -1)
1558 9820edbf 2021-08-06 op fatal("stat(\"%s\")", t->dir);
1560 9820edbf 2021-08-06 op if ((pw = getpwuid(sb.st_uid)) == NULL)
1561 9820edbf 2021-08-06 op fatal("getpwuid(%d)", sb.st_uid);
1563 9820edbf 2021-08-06 op imsg_compose(&ibuf, IMSG_AUTH, 0, 0, -1,
1564 9820edbf 2021-08-06 op pw->pw_name, strlen(pw->pw_name)+1);
1565 9820edbf 2021-08-06 op imsg_compose(&ibuf, IMSG_AUTH_DIR, 0, 0, -1,
1566 9820edbf 2021-08-06 op t->dir, strlen(t->dir)+1);
1568 9820edbf 2021-08-06 op if (imsg_flush(&ibuf) == -1)
1569 9820edbf 2021-08-06 op fatal("imsg_flush");
1573 c734c0e9 2021-08-03 op run_test(struct test *t)
1579 f3289b7e 2021-08-06 op before_printing();
1580 c734c0e9 2021-08-03 op puts("=====================");
1581 8e0ef4cb 2021-08-04 op pp_block(t->body);
1582 c734c0e9 2021-08-03 op puts("=====================");
1585 09e42a8d 2021-08-07 op if (stackh != 0)
1586 09e42a8d 2021-08-07 op popvn(stackh);
1588 91cfe5ca 2021-08-06 op if (t->body == NULL) {
1589 91cfe5ca 2021-08-06 op before_printing();
1590 91cfe5ca 2021-08-06 op printf("no instructions, skipping...\n");
1591 91cfe5ca 2021-08-06 op return EVAL_SKIP;
1594 9820edbf 2021-08-06 op pid = spawn_client_proc();
1595 9820edbf 2021-08-06 op prepare_child_for_test(t);
1596 9820edbf 2021-08-06 op ret = eval(t->body);
1598 eed75362 2021-08-06 op imsg_compose(&ibuf, IMSG_CONN_GONE, 0, 0, -1, NULL, 0);
1599 eed75362 2021-08-06 op imsg_flush(&ibuf);
1601 9820edbf 2021-08-06 op while (waitpid(pid, NULL, 0) != pid)
1604 370b4f2e 2021-08-07 op check_for_output();
1606 0b453b63 2021-08-07 op if (t->shouldfail) {
1607 0b453b63 2021-08-07 op if (ret == EVAL_OK) {
1608 0b453b63 2021-08-07 op before_printing();
1609 0b453b63 2021-08-07 op printf("test was expected to fail\n");
1610 0b453b63 2021-08-07 op return EVAL_ERR;
1611 0b453b63 2021-08-07 op } else if (ret == EVAL_ERR)
1612 0b453b63 2021-08-07 op return EVAL_OK;
1619 c734c0e9 2021-08-03 op main(int argc, char **argv)
1621 c734c0e9 2021-08-03 op struct test *t;
1622 f3289b7e 2021-08-06 op int ch, i, r, passed = 0, failed = 0, skipped = 0;
1623 9820edbf 2021-08-06 op int runclient = 0;
1624 32b8add4 2021-12-14 op const char *pat = NULL;
1625 32b8add4 2021-12-14 op regex_t reg;
1627 9820edbf 2021-08-06 op assert(argv0 = argv[0]);
1629 9820edbf 2021-08-06 op signal(SIGPIPE, SIG_IGN);
1631 c734c0e9 2021-08-03 op log_init(1, LOG_DAEMON);
1632 c734c0e9 2021-08-03 op log_setverbose(1);
1634 f9cd3e06 2021-08-04 op /* prepare the global env */
1637 54736a95 2021-08-04 op add_builtin_proc("print", builtin_print, 1, 1);
1638 dd1d2343 2021-08-05 op add_builtin_proc("debug", builtin_debug, 1, 1);
1639 54736a95 2021-08-04 op add_builtin_proc("skip", builtin_skip, 0, 0);
1640 6019438a 2021-08-05 op add_builtin_proc("iota", builtin_iota, 0, 0);
1641 9820edbf 2021-08-06 op add_builtin_proc("send", builtin_send, 2, 1);
1642 9820edbf 2021-08-06 op add_builtin_proc("recv", builtin_recv, 0, 0);
1644 32b8add4 2021-12-14 op while ((ch = getopt(argc, argv, "nT:vx:")) != -1) {
1645 dd1d2343 2021-08-05 op switch (ch) {
1647 4315b20a 2021-08-05 op syntaxcheck = 1;
1650 9820edbf 2021-08-06 op assert(*optarg == 'c');
1651 9820edbf 2021-08-06 op runclient = 1;
1657 32b8add4 2021-12-14 op pat = optarg;
1660 4315b20a 2021-08-05 op fprintf(stderr, "Usage: %s [-nv] [files...]\n",
1665 dd1d2343 2021-08-05 op argc -= optind;
1666 dd1d2343 2021-08-05 op argv += optind;
1668 9820edbf 2021-08-06 op if (runclient)
1669 9820edbf 2021-08-06 op client(1, debug);
1671 32b8add4 2021-12-14 op if (pat == NULL)
1674 32b8add4 2021-12-14 op if (regcomp(®, pat, REG_BASIC | REG_ICASE | REG_NOSUB) != 0)
1675 32b8add4 2021-12-14 op fatalx("invalid regexp: %s", pat);
1677 dd1d2343 2021-08-05 op for (i = 0; i < argc; ++i)
1678 c734c0e9 2021-08-03 op loadfile(argv[i]);
1680 4315b20a 2021-08-05 op if (syntaxcheck) {
1681 4315b20a 2021-08-05 op fprintf(stderr, "files OK\n");
1685 9820edbf 2021-08-06 op /* Check for root privileges. */
1686 9820edbf 2021-08-06 op if (geteuid())
1687 9820edbf 2021-08-06 op fatalx("need root privileges");
1690 c734c0e9 2021-08-03 op TAILQ_FOREACH(t, &tests, entry) {
1691 32b8add4 2021-12-14 op if (regexec(®, t->name, 0, NULL, 0) != 0)
1694 4e3f88dd 2021-12-13 op printf("===> [%d/%d] running test \"%s\"... ", i+1, ntests,
1696 e334da07 2021-08-04 op fflush(stdout);
1698 f3289b7e 2021-08-06 op filler = "\n";
1699 f3289b7e 2021-08-06 op r = run_test(t);
1700 f3289b7e 2021-08-06 op if (filler == NULL)
1701 f3289b7e 2021-08-06 op printf("=> test ");
1703 f3289b7e 2021-08-06 op switch (r) {
1704 333d8d7d 2021-08-04 op case EVAL_OK:
1705 d728769d 2021-08-07 op printf("passed\n");
1708 333d8d7d 2021-08-04 op case EVAL_ERR:
1710 f3289b7e 2021-08-06 op printf("failed\n");
1712 333d8d7d 2021-08-04 op case EVAL_SKIP:
1713 f3289b7e 2021-08-06 op printf("skipped\n");
1718 83117bfe 2021-08-07 op if (filler == NULL)
1719 83117bfe 2021-08-07 op printf("\n");
1723 f3289b7e 2021-08-06 op printf("\n");
1724 4ed3f1b8 2021-12-14 op printf("%d/%d passed (%d skipped and %d failed)\n",
1725 25b0f8d2 2021-08-07 op passed, i, skipped, failed);
1728 d41deb93 2021-08-07 op free(lastmsg);
1729 32b8add4 2021-12-14 op regfree(®);
1731 c734c0e9 2021-08-03 op return failed != 0;