2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/socket.h>
49 static const char *argv0;
51 static uint8_t *lastmsg;
53 static struct imsgbuf ibuf;
54 static int ibuf_inuse;
55 static int child_out = -1;
57 static struct procs procs = TAILQ_HEAD_INITIALIZER(procs);
58 static struct tests tests = TAILQ_HEAD_INITIALIZER(tests);
62 static struct opstacks blocks = TAILQ_HEAD_INITIALIZER(blocks);
63 static struct opstacks args = TAILQ_HEAD_INITIALIZER(args);
65 #define STACK_HEIGHT 64
66 static struct value vstack[STACK_HEIGHT];
69 static struct envs envs = TAILQ_HEAD_INITIALIZER(envs);
71 static struct value v_false = {.type = V_NUM, .v = {.num = 0}};
72 static struct value v_true = {.type = V_NUM, .v = {.num = 1}};
74 static uint8_t lasttag;
77 static int syntaxcheck;
79 static const char *filler;
91 check_for_output(void)
93 static char buf[BUFSIZ];
99 if (poll(&pfd, 1, 0) == -1)
102 if (!(pfd.revents & POLLIN))
106 if ((r = read(child_out, buf, sizeof(buf))) == -1) {
114 fwrite(buf, 1, r, stdout);
119 peekn(int depth, struct value *v)
122 errx(1, "can't peek the stack at %d: underflow",
124 memcpy(v, &vstack[stackh - depth], sizeof(*v));
127 printf("peeking(%d) ", depth); pp_val(v); printf("\n");
132 popv(struct value *v)
135 errx(1, "can't pop the stack: underflow");
136 memcpy(v, &vstack[--stackh], sizeof(*v));
139 printf("popping "); pp_val(v); printf("\n");
153 pushv(struct value *v)
155 if (stackh == STACK_HEIGHT)
156 errx(1, "can't push the stack: overflow");
159 printf("pushing "); pp_val(v); printf("\n");
162 memcpy(&vstack[stackh++], v, sizeof(*v));
168 pushv(n ? &v_true : &v_false);
181 static inline struct opstack *
182 pushstack(struct opstacks *stack)
186 ops = xcalloc(1, sizeof(*ops));
187 TAILQ_INSERT_HEAD(stack, ops, entry);
191 static inline struct opstack *
192 peek(struct opstacks *stack)
194 if (TAILQ_EMPTY(stack))
195 errx(1, "%s: args underflow", __func__);
197 return TAILQ_FIRST(stack);
200 static inline struct op *
201 finalize(struct opstacks *stack, int *argc)
206 if (TAILQ_EMPTY(stack))
207 errx(1, "%s: args underflow", __func__);
210 TAILQ_REMOVE(&args, ops, entry);
214 *argc = ops->counter;
221 push(struct opstacks *stack, struct op *op)
226 if (ops->last == NULL) {
230 ops->last->next = op;
242 e = xcalloc(1, sizeof(*e));
243 TAILQ_INSERT_HEAD(&envs, e, entry);
246 static inline struct env *
249 assert(!TAILQ_EMPTY(&envs));
250 return TAILQ_FIRST(&envs);
257 struct binding *b, *tb;
260 TAILQ_REMOVE(&envs, e, entry);
262 TAILQ_FOREACH_SAFE(b, &e->bindings, entry, tb)
269 setvar(char *sym, struct op *op)
276 if ((ret = eval(op)) != EVAL_OK)
279 if (stackh != height + 1) {
281 printf("trying to assign to `%s' a void value: ", sym);
287 b = xcalloc(1, sizeof(*b));
291 e = TAILQ_FIRST(&envs);
292 TAILQ_INSERT_HEAD(&e->bindings, b, entry);
298 setvar_raw(char *sym, struct op *op)
303 b = xcalloc(1, sizeof(*b));
307 e = TAILQ_FIRST(&envs);
308 TAILQ_INSERT_HEAD(&e->bindings, b, entry);
312 getvar(const char *sym, struct value *v)
317 TAILQ_FOREACH(e, &envs, entry) {
318 TAILQ_FOREACH(b, &e->bindings, entry) {
319 if (!strcmp(sym, b->name)) {
320 memcpy(v, &b->val, sizeof(*v));
327 fprintf(stderr, "unbound variable %s\n", sym);
332 getvar_raw(const char *sym, struct op **raw)
337 TAILQ_FOREACH(e, &envs, entry) {
338 TAILQ_FOREACH(b, &e->bindings, entry) {
339 if (!strcmp(sym, b->name)) {
350 global_set(char *sym, struct op *op)
355 /* TODO: check for duplicates */
357 if (op->type != OP_LITERAL &&
358 (op->type == OP_CAST && op->v.cast.expr->type != OP_LITERAL))
361 b = xcalloc(1, sizeof(*b));
364 /* it's only a cast on a literal! */
365 if (op->type == OP_CAST) {
366 if (eval(op) != EVAL_OK) {
372 memcpy(&b->val, &op->v.literal, sizeof(b->val));
374 e = TAILQ_LAST(&envs, envs);
375 TAILQ_INSERT_HEAD(&e->bindings, b, entry);
385 op = xcalloc(1, sizeof(*op));
392 free_op_rec(struct op *op)
404 free_op(struct op *op)
415 free(op->v.assign.name);
416 free_op_rec(op->v.assign.expr);
419 free_op_rec(op->v.assert);
422 free_op_rec(op->v.funcall.argv);
428 free_op_rec(op->v.cast.expr);
432 free_op_rec(op->v.bin_cmp.a);
433 free_op_rec(op->v.bin_cmp.b);
436 free_op_rec(op->v.faccess.expr);
437 free(op->v.faccess.field);
440 free(op->v.sfail.msg);
441 free_op_rec(op->v.sfail.expr);
454 return newop(OP_REST);
458 op_assign(char *sym, struct op *expr)
462 op = newop(OP_ASSIGN);
463 op->v.assign.name = sym;
464 op->v.assign.expr = expr;
470 op_assert(struct op *expr)
474 op = newop(OP_ASSERT);
492 op_lit_str(char *str)
496 op = newop(OP_LITERAL);
497 op->v.literal.type = V_STR;
498 op->v.literal.v.str = str;
504 op_lit_num(uint64_t n)
508 op = newop(OP_LITERAL);
509 op->v.literal.type = V_NUM;
510 op->v.literal.v.num = n;
516 op_cmp_eq(struct op *a, struct op *b)
520 op = newop(OP_CMP_EQ);
528 op_cmp_leq(struct op *a, struct op *b)
532 op = newop(OP_CMP_LEQ);
540 op_cast(struct op *expr, int totype)
545 op->v.cast.expr = expr;
546 op->v.cast.totype = totype;
552 op_faccess(struct op *expr, char *field)
556 op = newop(OP_FACCESS);
557 op->v.faccess.expr = expr;
558 op->v.faccess.field = field;
564 op_sfail(struct op *expr, char *msg)
568 op = newop(OP_SFAIL);
569 op->v.sfail.expr = expr;
570 op->v.sfail.msg = msg;
580 op = newop(OP_VARGS);
586 ppf_val(FILE *f, struct value *val)
592 fprintf(f, "%s", val->v.str);
595 fprintf(f, "\"%s\"", val->v.str);
598 fprintf(f, "%"PRIi64, val->v.num);
601 fprintf(f, "%"PRIu8, val->v.u8);
604 fprintf(f, "%"PRIu16, val->v.u16);
607 fprintf(f, "%"PRIu32, val->v.u32);
611 for (i = 0; i < val->v.msg.len; ++i)
612 fprintf(f, "%x%s", val->v.msg.msg[i],
613 i == val->v.msg.len-1 ? "" : " ");
617 fprintf(f, "qids[n=%zu]", val->v.qidvec.len);
620 fprintf(f, "<unknown value>");
626 pp_val(struct value *val)
628 ppf_val(stdout, val);
632 val_type(struct value *v)
635 case V_SYM: return "symbol";
636 case V_STR: return "string";
637 case V_NUM: return "number";
638 case V_MSG: return "message";
639 case V_QID: return "qid";
640 case V_U8: return "u8";
641 case V_U16: return "u16";
642 case V_U32: return "u32";
643 default: return "unknown";
648 val_trueish(struct value *a)
656 val_isnum(struct value *a)
658 return a->type == V_NUM
665 val_tonum(struct value *a)
668 case V_NUM: return a->v.num;
669 case V_U8: return a->v.u8;
670 case V_U16: return a->v.u16;
671 case V_U32: return a->v.u32;
674 fprintf(stderr, "%s: given value is not a number\n", __func__);
680 val_eq(struct value *a, struct value *b)
682 if (val_isnum(a) && val_isnum(b))
683 return val_tonum(a) == val_tonum(b);
685 if (a->type != b->type)
691 return !strcmp(a->v.str, b->v.str);
698 val_leq(struct value *a, struct value *b)
700 if (val_isnum(a) && val_isnum(b))
701 return val_tonum(a) <= val_tonum(b);
705 static inline const char *
706 pp_totype(int totype)
709 * Not all of these are valid cast type thought, including
710 * every possibility only to aid debugging.
713 case V_STR: return "str";
714 case V_SYM: return "sym";
715 case V_NUM: return "num";
716 case V_QID: return "qid";
717 case V_U8: return "u8";
718 case V_U16: return "u16";
719 case V_U32: return "u32";
720 default: return "unknown";
725 val_cast(struct value *a, int totype)
729 #define NUMCAST(val, t, c, totype, max) do { \
732 fprintf(stderr, "can't cast %"PRIu64 \
733 " to %s\n", val, pp_totype(totype)); \
741 if (a->type == totype)
746 fprintf(stderr, "can't cast ");
748 fprintf(stderr, " to type %s\n", pp_totype(totype));
754 case V_U8: NUMCAST(v, u8, uint8_t, totype, UINT8_MAX);
755 case V_U16: NUMCAST(v, u16, uint16_t, totype, UINT16_MAX);
756 case V_U32: NUMCAST(v, u32, uint32_t, totype, UINT32_MAX);
759 fprintf(stderr, "can't cast %"PRIu64" to %s\n",
760 v, pp_totype(totype));
768 val_faccess(struct value *a, const char *field, struct value *ret)
774 #define MSGTYPE(m) *(m.msg + 4) /* skip the length */
778 /* TODO: add path. needs uint64_t values thought! */
779 if (!strcmp(field, "vers")) {
781 memcpy(&ret->v.u32, a->v.qid+1, 4);
783 } else if (!strcmp(field, "type")) {
785 ret->v.u8 = *a->v.qid;
791 mtype = MSGTYPE(a->v.msg);
792 if (!strcmp(field, "type")) {
794 ret->v.u8 = MSGTYPE(a->v.msg);
796 } else if (!strcmp(field, "tag")) {
798 memcpy(&ret->v.u16, &a->v.msg.msg[5], 2);
799 ret->v.u16 = le16toh(ret->v.u16);
801 } else if (!strcmp(field, "msize") && mtype == Rversion) {
803 memcpy(&ret->v.u32, &a->v.msg.msg[7], 4);
804 ret->v.u32 = le32toh(ret->v.u32);
806 } else if (!strcmp(field, "qid") && mtype == Rattach) {
808 memcpy(&ret->v.qid, &a->v.msg.msg[7], QIDSIZE);
810 } else if (!strcmp(field, "nwqid") && mtype == Rwalk) {
812 memcpy(&ret->v.u16, &a->v.msg.msg[7], 2);
813 ret->v.u16 = le16toh(ret->v.u16);
815 } else if (!strcmp(field, "wqid") && mtype == Rwalk) {
816 ret->type = V_QIDVEC;
817 ret->v.qidvec.start = &a->v.msg.msg[9];
818 memcpy(&len, &a->v.msg.msg[7], 2);
820 ret->v.qidvec.len = len;
826 len = strtonum(field, 0, MAXWELEM, &errstr);
827 if (errstr != NULL) {
829 printf("can't access qid #%s: %s\n", field, errstr);
833 if (len >= a->v.qidvec.len) {
835 printf("can't access qid #%d: out-of-bound "
836 "(max %zu)\n", len, a->v.qidvec.len);
841 memcpy(&ret->v.qid, a->v.qidvec.start + len * QIDSIZE,
851 printf("can't access field `%s' on type %s (", field, val_type(a));
869 printf("%s = ", op->v.assign.name);
870 pp_op(op->v.assign.expr);
877 printf("funcall %s(", op->v.funcall.proc->name);
878 for (aux = op->v.funcall.argv; aux != NULL; aux = aux->next) {
880 if (aux->next != NULL)
886 pp_val(&op->v.literal);
889 printf("%s", op->v.var);
892 pp_op(op->v.cast.expr);
894 switch (op->v.cast.totype) {
895 case V_U8: printf("u8"); break;
896 case V_U16: printf("u16"); break;
897 case V_U32: printf("u32"); break;
898 case V_STR: printf("str"); break;
899 default: printf("???"); break;
903 pp_op(op->v.bin_cmp.a);
905 pp_op(op->v.bin_cmp.b);
908 pp_op(op->v.bin_cmp.a);
910 pp_op(op->v.bin_cmp.b);
913 pp_op(op->v.faccess.expr);
914 printf(".%s", op->v.faccess.field);
917 printf("should-fail ");
918 pp_op(op->v.sfail.expr);
919 if (op->v.sfail.msg != NULL)
920 printf(": \"%s\"", op->v.sfail.msg);
926 printf(" ???[%d] ", op->type);
931 pp_block(struct op *op)
947 struct op *t, *tnext;
958 * Try to load the rest argument. Note that it can be
961 if ((ret = getvar_raw("...", &t)) == EVAL_OK)
962 if ((ret = eval(t)) != EVAL_OK)
967 ret = setvar(op->v.assign.name, op->v.assign.expr);
973 if ((ret = eval(op->v.assert)) != EVAL_OK)
976 if (!val_trueish(&a)) {
978 printf("assertion failed: ");
986 /* assume airity matches */
988 proc = op->v.funcall.proc;
989 if (proc->nativefn != NULL) {
991 * Push arguments on the stack for builtin
992 * functions. Counting the height of the
993 * stack is done to compute the correct number
994 * in the vararg case. argc only counts the
995 * "syntactical" arguments, i.e. foo(x, ...)
996 * has argc == 2, but at runtime argc may be
997 * 1, 2 or a greater number!
1001 t = op->v.funcall.argv;
1002 if (t != NULL && (ret = eval(t)) != EVAL_OK)
1008 if ((ret = proc->nativefn(i))
1012 if (proc->body == NULL) {
1014 printf("warn: calling the empty proc `%s'\n",
1021 for (t = op->v.funcall.argv, i = 0;
1025 * Push a pseudo variable `...' (and
1026 * don't evaluate it) in the vararg
1027 * case. A special case is when the
1028 * variable is itself `...'.
1030 if (proc->vararg && i == proc->minargs) {
1031 if (t->type != OP_REST)
1032 setvar_raw(xstrdup("..."), t);
1037 * The arguments are a linked list of
1038 * ops. Setvar will call eval that
1039 * will evaluate *all* the arguments.
1040 * The dance here that sets next to
1041 * NULL and then restores it is to
1042 * avoid this behaviour.
1046 ret = setvar(proc->args[i], t);
1053 if ((ret = eval(proc->body)) != EVAL_OK)
1062 pushv(&op->v.literal);
1066 if ((ret = getvar(op->v.var, &a)) != EVAL_OK)
1072 if ((ret = eval(op->v.cast.expr)) != EVAL_OK)
1075 if ((ret = val_cast(&a, op->v.cast.totype)) != EVAL_OK)
1081 if ((ret = eval(op->v.bin_cmp.a)) != EVAL_OK)
1083 if ((ret = eval(op->v.bin_cmp.b)) != EVAL_OK)
1088 pushbool(val_eq(&a, &b));
1092 if ((ret = eval(op->v.bin_cmp.a)) != EVAL_OK)
1094 if ((ret = eval(op->v.bin_cmp.b)) != EVAL_OK)
1099 pushbool(val_leq(&a, &b));
1103 if ((ret = eval(op->v.faccess.expr)) != EVAL_OK)
1106 if ((ret = val_faccess(&a, op->v.faccess.field, &b))
1113 if ((ret = eval(op->v.sfail.expr)) == EVAL_OK) {
1115 printf("expecting failure");
1116 if (op->v.sfail.msg != NULL)
1117 printf(" \"%s\"", op->v.sfail.msg);
1119 printf("expression: ");
1120 pp_op(op->v.sfail.expr);
1124 if (ret == EVAL_SKIP)
1129 if ((ret = getvar_raw("...", &t)) == EVAL_OK) {
1130 for (i = 0; t != NULL; t = t->next)
1139 fprintf(stderr, "invalid op, aborting.\n");
1144 return eval(op->next);
1149 prepare_funcall(void)
1155 push_arg(struct op *op)
1161 op_funcall(struct proc *proc)
1163 struct op *op, *argv;
1166 argv = finalize(&args, &argc);
1168 op = newop(OP_FUNCALL);
1169 op->v.funcall.proc = proc;
1170 op->v.funcall.argv = argv;
1171 op->v.funcall.argc = argc;
1177 add_builtin_proc(const char *name, int (*fn)(int), int argc, int vararg)
1181 proc = xcalloc(1, sizeof(*proc));
1182 proc->name = xstrdup(name);
1183 proc->nativefn = fn;
1184 proc->minargs = argc;
1185 proc->vararg = vararg;
1187 TAILQ_INSERT_HEAD(&procs, proc, entry);
1197 proc_setup_body(void)
1199 struct opstack *argv;
1204 for (i = 0, op = argv->base.next; op != NULL; i++) {
1206 * TODO: should free the whole list on error but..,
1207 * we're gonna exit real soon(tm)!
1209 if (op->type != OP_VAR && op->type != OP_REST)
1215 assert(i == argv->counter);
1221 proc_done(char *name)
1224 struct op *op, *next, *argv, *body;
1227 argv = finalize(&args, &argc);
1228 body = finalize(&blocks, NULL);
1230 proc = xcalloc(1, sizeof(*proc));
1232 proc->minargs = argc;
1234 for (i = 0, op = argv; op != NULL; ++i) {
1235 if (op->type == OP_REST) {
1241 proc->args[i] = xstrdup(op->v.var);
1247 assert(i == argc || (proc->vararg && i == proc->minargs));
1251 TAILQ_INSERT_HEAD(&procs, proc, entry);
1255 block_push(struct op *op)
1261 proc_by_name(const char *name)
1265 TAILQ_FOREACH(p, &procs, entry) {
1266 if (!strcmp(p->name, name))
1280 test_done(int shouldfail, char *name, char *dir)
1284 test = xcalloc(1, sizeof(*test));
1285 test->shouldfail = shouldfail;
1288 test->body = finalize(&blocks, NULL);
1290 if (TAILQ_EMPTY(&tests))
1291 TAILQ_INSERT_HEAD(&tests, test, entry);
1293 TAILQ_INSERT_TAIL(&tests, test, entry);
1299 builtin_print(int argc)
1306 for (i = argc; i > 0; --i) {
1308 if (v.type == V_STR)
1309 printf("%s", v.v.str);
1323 builtin_debug(int argc)
1326 return builtin_print(argc);
1333 builtin_skip(int argc)
1339 builtin_iota(int argc)
1344 if ((v.v.u16 = ++lasttag) == 255)
1345 v.v.u16 = ++lasttag;
1352 builtin_send(int argc)
1363 * Compute the length of the packet. 4 is for the initial
1368 for (i = argc; i > 0; --i) {
1372 len += 2; /* count */
1373 len += strlen(v.v.str);
1390 printf("%s: can't serialize ", __func__);
1397 if (len > UINT16_MAX) {
1399 printf("%s: message size too long: got %d when max is %d\n",
1400 __func__, len, UINT16_MAX);
1404 if ((buf = imsg_create(&ibuf, IMSG_BUF, 0, 0, len)) == NULL)
1405 fatal("imsg_create(%d)", len);
1408 imsg_add(buf, &len, sizeof(len));
1410 for (i = argc; i > 0; --i) {
1414 slen = strlen(v.v.str);
1415 slen = htole16(slen);
1416 imsg_add(buf, &slen, sizeof(slen));
1417 imsg_add(buf, v.v.str, strlen(v.v.str));
1421 imsg_add(buf, &v.v.u8, 1);
1425 v.v.u16 = htole16(v.v.u16);
1426 imsg_add(buf, &v.v.u16, 2);
1430 v.v.u32 = htole32(v.v.u32);
1431 imsg_add(buf, &v.v.u32, 4);
1436 imsg_close(&ibuf, buf);
1438 if (imsg_flush(&ibuf) == -1) {
1441 printf("%s: imsg_flush failed: %s\n", __func__, strerror(i));
1450 builtin_recv(int argc)
1458 if (lastmsg != NULL) {
1464 pfd.events = POLLIN;
1465 if (poll(&pfd, 1, INFTIM) == -1) {
1468 printf("%s: poll failed: %s\n", __func__, strerror(serrno));
1473 if ((n = imsg_read(&ibuf)) == -1) {
1474 if (errno == EAGAIN)
1481 printf("child disconnected\n");
1488 /* read only one message */
1489 if ((n = imsg_get(&ibuf, &imsg)) == -1)
1494 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1495 switch (imsg.hdr.type) {
1498 if ((v.v.msg.msg = malloc(datalen)) == NULL)
1500 memcpy(v.v.msg.msg, imsg.data, datalen);
1501 v.v.msg.len = datalen;
1508 printf("subprocess closed the connection\n");
1518 printf("got unknown message from subprocess: %d\n",
1526 spawn_client_proc(void)
1528 const char *argv[4];
1529 int p[2], out[2], argc = 0;
1532 if (child_out != -1)
1535 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
1536 PF_UNSPEC, p) == -1)
1537 fatal("socketpair");
1539 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
1540 PF_UNSPEC, out) == -1)
1541 fatal("socketpair");
1543 switch (pid = fork()) {
1545 fatal("cannot fork");
1553 msgbuf_clear(&ibuf.w);
1556 imsg_init(&ibuf, p[0]);
1564 if (dup2(out[1], 1) == -1 ||
1565 dup2(out[1], 2) == -1)
1569 if (dup2(p[1], 3) == -1)
1570 fatal("cannot setup imsg fd");
1571 } else if (fcntl(F_SETFD, 0) == -1)
1572 fatal("cannot setup imsg fd");
1574 argv[argc++] = argv0;
1575 argv[argc++] = "-Tc";
1578 argv[argc++] = "-v";
1581 argv[argc++] = NULL;
1583 execvp(argv0, (char *const *)argv);
1588 prepare_child_for_test(struct test *t)
1593 if (stat(t->dir, &sb) == -1)
1594 fatal("stat(\"%s\")", t->dir);
1596 if ((pw = getpwuid(sb.st_uid)) == NULL)
1597 fatal("getpwuid(%d)", sb.st_uid);
1599 imsg_compose(&ibuf, IMSG_AUTH, 0, 0, -1,
1600 pw->pw_name, strlen(pw->pw_name)+1);
1601 imsg_compose(&ibuf, IMSG_AUTH_DIR, 0, 0, -1,
1602 t->dir, strlen(t->dir)+1);
1604 if (imsg_flush(&ibuf) == -1)
1605 fatal("imsg_flush");
1609 run_test(struct test *t)
1616 puts("=====================");
1618 puts("=====================");
1624 if (t->body == NULL) {
1626 printf("no instructions, skipping...\n");
1630 pid = spawn_client_proc();
1631 prepare_child_for_test(t);
1632 ret = eval(t->body);
1634 imsg_compose(&ibuf, IMSG_CONN_GONE, 0, 0, -1, NULL, 0);
1637 while (waitpid(pid, NULL, 0) != pid)
1642 if (t->shouldfail) {
1643 if (ret == EVAL_OK) {
1645 printf("test was expected to fail\n");
1647 } else if (ret == EVAL_ERR)
1655 main(int argc, char **argv)
1658 int ch, i, r, passed = 0, failed = 0, skipped = 0;
1660 const char *pat = NULL;
1663 assert(argv0 = argv[0]);
1665 signal(SIGPIPE, SIG_IGN);
1667 log_init(1, LOG_DAEMON);
1670 /* prepare the global env */
1673 add_builtin_proc("print", builtin_print, 1, 1);
1674 add_builtin_proc("debug", builtin_debug, 1, 1);
1675 add_builtin_proc("skip", builtin_skip, 0, 0);
1676 add_builtin_proc("iota", builtin_iota, 0, 0);
1677 add_builtin_proc("send", builtin_send, 2, 1);
1678 add_builtin_proc("recv", builtin_recv, 0, 0);
1680 while ((ch = getopt(argc, argv, "nT:vx:")) != -1) {
1686 assert(*optarg == 'c');
1696 fprintf(stderr, "Usage: %s [-nv] [files...]\n",
1710 if (regcomp(®, pat, REG_BASIC | REG_ICASE | REG_NOSUB) != 0)
1711 fatalx("invalid regexp: %s", pat);
1713 for (i = 0; i < argc; ++i)
1717 fprintf(stderr, "files OK\n");
1721 /* Check for root privileges. */
1723 fatalx("need root privileges");
1726 TAILQ_FOREACH(t, &tests, entry) {
1727 if (regexec(®, t->name, 0, NULL, 0) != 0)
1730 printf("===> [%d/%d] running test \"%s\"... ", i+1, ntests,
1749 printf("skipped\n");
1760 printf("%d/%d passed (%d skipped and %d failed)\n",
1761 passed, i, skipped, failed);