2 * Copyright (c) 2021, 2022 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;
50 static const char *dir;
53 static uint8_t *lastmsg;
55 static struct imsgbuf ibuf;
56 static int ibuf_inuse;
57 static int child_out = -1;
59 static struct procs procs = TAILQ_HEAD_INITIALIZER(procs);
60 static struct tests tests = TAILQ_HEAD_INITIALIZER(tests);
64 static struct opstacks blocks = TAILQ_HEAD_INITIALIZER(blocks);
65 static struct opstacks args = TAILQ_HEAD_INITIALIZER(args);
67 #define STACK_HEIGHT 64
68 static struct value vstack[STACK_HEIGHT];
71 static struct envs envs = TAILQ_HEAD_INITIALIZER(envs);
73 static const struct value v_false = {.type = V_NUM, .v = {.num = 0}};
74 static const struct value v_true = {.type = V_NUM, .v = {.num = 1}};
76 static uint8_t lasttag;
79 static int syntaxcheck;
81 static const char *filler;
93 check_for_output(void)
95 static char buf[BUFSIZ];
101 if (poll(&pfd, 1, 0) == -1)
104 if (!(pfd.revents & POLLIN))
108 if ((r = read(child_out, buf, sizeof(buf))) == -1) {
116 fwrite(buf, 1, r, stdout);
121 peekn(int depth, struct value *v)
124 errx(1, "can't peek the stack at %d: underflow",
126 memcpy(v, &vstack[stackh - depth], sizeof(*v));
129 printf("peeking(%d) ", depth); pp_val(v); printf("\n");
134 popv(struct value *v)
137 errx(1, "can't pop the stack: underflow");
138 memcpy(v, &vstack[--stackh], sizeof(*v));
141 printf("popping "); pp_val(v); printf("\n");
155 pushv(const struct value *v)
157 if (stackh == STACK_HEIGHT)
158 errx(1, "can't push the stack: overflow");
161 printf("pushing "); pp_val(v); printf("\n");
164 memcpy(&vstack[stackh++], v, sizeof(*v));
170 pushv(n ? &v_true : &v_false);
183 static inline struct opstack *
184 pushstack(struct opstacks *stack)
188 ops = xcalloc(1, sizeof(*ops));
189 TAILQ_INSERT_HEAD(stack, ops, entry);
193 static inline struct opstack *
194 peek(struct opstacks *stack)
196 if (TAILQ_EMPTY(stack))
197 errx(1, "%s: args underflow", __func__);
199 return TAILQ_FIRST(stack);
202 static inline struct op *
203 finalize(struct opstacks *stack, int *argc)
208 if (TAILQ_EMPTY(stack))
209 errx(1, "%s: args underflow", __func__);
212 TAILQ_REMOVE(&args, ops, entry);
216 *argc = ops->counter;
223 push(struct opstacks *stack, struct op *op)
228 if (ops->last == NULL) {
232 ops->last->next = op;
244 e = xcalloc(1, sizeof(*e));
245 TAILQ_INSERT_HEAD(&envs, e, entry);
248 static inline struct env *
251 assert(!TAILQ_EMPTY(&envs));
252 return TAILQ_FIRST(&envs);
259 struct binding *b, *tb;
262 TAILQ_REMOVE(&envs, e, entry);
264 TAILQ_FOREACH_SAFE(b, &e->bindings, entry, tb)
271 setvar(char *sym, struct op *op)
278 if ((ret = eval(op)) != EVAL_OK)
281 if (stackh != height + 1) {
283 printf("trying to assign to `%s' a void value: ", sym);
289 b = xcalloc(1, sizeof(*b));
293 e = TAILQ_FIRST(&envs);
294 TAILQ_INSERT_HEAD(&e->bindings, b, entry);
300 setvar_raw(char *sym, struct op *op)
305 b = xcalloc(1, sizeof(*b));
309 e = TAILQ_FIRST(&envs);
310 TAILQ_INSERT_HEAD(&e->bindings, b, entry);
314 getvar(const char *sym, struct value *v)
319 TAILQ_FOREACH(e, &envs, entry) {
320 TAILQ_FOREACH(b, &e->bindings, entry) {
321 if (!strcmp(sym, b->name)) {
322 memcpy(v, &b->val, sizeof(*v));
329 fprintf(stderr, "unbound variable %s\n", sym);
334 getvar_raw(const char *sym, struct op **raw)
339 TAILQ_FOREACH(e, &envs, entry) {
340 TAILQ_FOREACH(b, &e->bindings, entry) {
341 if (!strcmp(sym, b->name)) {
352 global_set(char *sym, struct op *op)
357 /* TODO: check for duplicates */
359 if (op->type != OP_LITERAL &&
360 (op->type == OP_CAST && op->v.cast.expr->type != OP_LITERAL))
363 b = xcalloc(1, sizeof(*b));
366 /* it's only a cast on a literal! */
367 if (op->type == OP_CAST) {
368 if (eval(op) != EVAL_OK) {
374 memcpy(&b->val, &op->v.literal, sizeof(b->val));
376 e = TAILQ_LAST(&envs, envs);
377 TAILQ_INSERT_HEAD(&e->bindings, b, entry);
387 op = xcalloc(1, sizeof(*op));
394 free_op_rec(struct op *op)
406 free_op(struct op *op)
417 free(op->v.assign.name);
418 free_op_rec(op->v.assign.expr);
421 free_op_rec(op->v.assert);
424 free_op_rec(op->v.funcall.argv);
430 free_op_rec(op->v.cast.expr);
434 free_op_rec(op->v.bin_cmp.a);
435 free_op_rec(op->v.bin_cmp.b);
438 free_op_rec(op->v.faccess.expr);
439 free(op->v.faccess.field);
442 free(op->v.sfail.msg);
443 free_op_rec(op->v.sfail.expr);
456 return newop(OP_REST);
460 op_assign(char *sym, struct op *expr)
464 op = newop(OP_ASSIGN);
465 op->v.assign.name = sym;
466 op->v.assign.expr = expr;
472 op_assert(struct op *expr)
476 op = newop(OP_ASSERT);
494 op_lit_str(char *str)
498 op = newop(OP_LITERAL);
499 op->v.literal.type = V_STR;
500 op->v.literal.v.str = str;
506 op_lit_num(uint64_t n)
510 op = newop(OP_LITERAL);
511 op->v.literal.type = V_NUM;
512 op->v.literal.v.num = n;
518 op_cmp_eq(struct op *a, struct op *b)
522 op = newop(OP_CMP_EQ);
530 op_cmp_leq(struct op *a, struct op *b)
534 op = newop(OP_CMP_LEQ);
542 op_cast(struct op *expr, int totype)
547 op->v.cast.expr = expr;
548 op->v.cast.totype = totype;
554 op_faccess(struct op *expr, char *field)
558 op = newop(OP_FACCESS);
559 op->v.faccess.expr = expr;
560 op->v.faccess.field = field;
566 op_sfail(struct op *expr, char *msg)
570 op = newop(OP_SFAIL);
571 op->v.sfail.expr = expr;
572 op->v.sfail.msg = msg;
582 op = newop(OP_VARGS);
588 ppf_val(FILE *f, const struct value *val)
594 fprintf(f, "%s", val->v.str);
597 fprintf(f, "\"%s\"", val->v.str);
600 fprintf(f, "%"PRIi64, val->v.num);
603 fprintf(f, "%"PRIu8, val->v.u8);
606 fprintf(f, "%"PRIu16, val->v.u16);
609 fprintf(f, "%"PRIu32, val->v.u32);
613 for (i = 0; i < val->v.msg.len; ++i)
614 fprintf(f, "%x%s", val->v.msg.msg[i],
615 i == val->v.msg.len-1 ? "" : " ");
619 fprintf(f, "qids[n=%zu]", val->v.qidvec.len);
622 fprintf(f, "<unknown value>");
628 pp_val(const struct value *val)
630 ppf_val(stdout, val);
634 val_type(struct value *v)
637 case V_SYM: return "symbol";
638 case V_STR: return "string";
639 case V_NUM: return "number";
640 case V_MSG: return "message";
641 case V_QID: return "qid";
642 case V_U8: return "u8";
643 case V_U16: return "u16";
644 case V_U32: return "u32";
645 default: return "unknown";
650 val_trueish(struct value *a)
658 val_isnum(struct value *a)
660 return a->type == V_NUM
667 val_tonum(struct value *a)
670 case V_NUM: return a->v.num;
671 case V_U8: return a->v.u8;
672 case V_U16: return a->v.u16;
673 case V_U32: return a->v.u32;
676 fprintf(stderr, "%s: given value is not a number\n", __func__);
682 val_eq(struct value *a, struct value *b)
684 if (val_isnum(a) && val_isnum(b))
685 return val_tonum(a) == val_tonum(b);
687 if (a->type != b->type)
693 return !strcmp(a->v.str, b->v.str);
700 val_leq(struct value *a, struct value *b)
702 if (val_isnum(a) && val_isnum(b))
703 return val_tonum(a) <= val_tonum(b);
707 static inline const char *
708 pp_totype(int totype)
711 * Not all of these are valid cast type thought, including
712 * every possibility only to aid debugging.
715 case V_STR: return "str";
716 case V_SYM: return "sym";
717 case V_NUM: return "num";
718 case V_QID: return "qid";
719 case V_U8: return "u8";
720 case V_U16: return "u16";
721 case V_U32: return "u32";
722 default: return "unknown";
727 val_cast(struct value *a, int totype)
731 #define NUMCAST(val, t, c, totype, max) do { \
734 fprintf(stderr, "can't cast %"PRIu64 \
735 " to %s\n", val, pp_totype(totype)); \
743 if (a->type == totype)
748 fprintf(stderr, "can't cast ");
750 fprintf(stderr, " to type %s\n", pp_totype(totype));
756 case V_U8: NUMCAST(v, u8, uint8_t, totype, UINT8_MAX);
757 case V_U16: NUMCAST(v, u16, uint16_t, totype, UINT16_MAX);
758 case V_U32: NUMCAST(v, u32, uint32_t, totype, UINT32_MAX);
761 fprintf(stderr, "can't cast %"PRIu64" to %s\n",
762 v, pp_totype(totype));
770 val_faccess(struct value *a, const char *field, struct value *ret)
776 #define MSGTYPE(m) *(m.msg + 4) /* skip the length */
780 /* TODO: add path. needs uint64_t values thought! */
781 if (!strcmp(field, "vers")) {
783 memcpy(&ret->v.u32, a->v.qid+1, 4);
785 } else if (!strcmp(field, "type")) {
787 ret->v.u8 = *a->v.qid;
793 mtype = MSGTYPE(a->v.msg);
794 if (!strcmp(field, "type")) {
796 ret->v.u8 = MSGTYPE(a->v.msg);
798 } else if (!strcmp(field, "tag")) {
800 memcpy(&ret->v.u16, &a->v.msg.msg[5], 2);
801 ret->v.u16 = le16toh(ret->v.u16);
803 } else if (!strcmp(field, "msize") && mtype == Rversion) {
805 memcpy(&ret->v.u32, &a->v.msg.msg[7], 4);
806 ret->v.u32 = le32toh(ret->v.u32);
808 } else if (!strcmp(field, "qid") && mtype == Rattach) {
810 memcpy(&ret->v.qid, &a->v.msg.msg[7], QIDSIZE);
812 } else if (!strcmp(field, "nwqid") && mtype == Rwalk) {
814 memcpy(&ret->v.u16, &a->v.msg.msg[7], 2);
815 ret->v.u16 = le16toh(ret->v.u16);
817 } else if (!strcmp(field, "wqid") && mtype == Rwalk) {
818 ret->type = V_QIDVEC;
819 ret->v.qidvec.start = &a->v.msg.msg[9];
820 memcpy(&len, &a->v.msg.msg[7], 2);
822 ret->v.qidvec.len = len;
828 len = strtonum(field, 0, MAXWELEM, &errstr);
829 if (errstr != NULL) {
831 printf("can't access qid #%s: %s\n", field, errstr);
835 if (len >= a->v.qidvec.len) {
837 printf("can't access qid #%d: out-of-bound "
838 "(max %zu)\n", len, a->v.qidvec.len);
843 memcpy(&ret->v.qid, a->v.qidvec.start + len * QIDSIZE,
853 printf("can't access field `%s' on type %s (", field, val_type(a));
871 printf("%s = ", op->v.assign.name);
872 pp_op(op->v.assign.expr);
879 printf("funcall %s(", op->v.funcall.proc->name);
880 for (aux = op->v.funcall.argv; aux != NULL; aux = aux->next) {
882 if (aux->next != NULL)
888 pp_val(&op->v.literal);
891 printf("%s", op->v.var);
894 pp_op(op->v.cast.expr);
896 switch (op->v.cast.totype) {
897 case V_U8: printf("u8"); break;
898 case V_U16: printf("u16"); break;
899 case V_U32: printf("u32"); break;
900 case V_STR: printf("str"); break;
901 default: printf("???"); break;
905 pp_op(op->v.bin_cmp.a);
907 pp_op(op->v.bin_cmp.b);
910 pp_op(op->v.bin_cmp.a);
912 pp_op(op->v.bin_cmp.b);
915 pp_op(op->v.faccess.expr);
916 printf(".%s", op->v.faccess.field);
919 printf("should-fail ");
920 pp_op(op->v.sfail.expr);
921 if (op->v.sfail.msg != NULL)
922 printf(": \"%s\"", op->v.sfail.msg);
928 printf(" ???[%d] ", op->type);
933 pp_block(struct op *op)
949 struct op *t, *tnext;
960 * Try to load the rest argument. Note that it can be
963 if ((ret = getvar_raw("...", &t)) == EVAL_OK)
964 if ((ret = eval(t)) != EVAL_OK)
969 ret = setvar(op->v.assign.name, op->v.assign.expr);
975 if ((ret = eval(op->v.assert)) != EVAL_OK)
978 if (!val_trueish(&a)) {
980 printf("assertion failed: ");
988 /* assume airity matches */
990 proc = op->v.funcall.proc;
991 if (proc->nativefn != NULL) {
993 * Push arguments on the stack for builtin
994 * functions. Counting the height of the
995 * stack is done to compute the correct number
996 * in the vararg case. argc only counts the
997 * "syntactical" arguments, i.e. foo(x, ...)
998 * has argc == 2, but at runtime argc may be
999 * 1, 2 or a greater number!
1003 t = op->v.funcall.argv;
1004 if (t != NULL && (ret = eval(t)) != EVAL_OK)
1010 if ((ret = proc->nativefn(i))
1014 if (proc->body == NULL) {
1016 printf("warn: calling the empty proc `%s'\n",
1023 for (t = op->v.funcall.argv, i = 0;
1027 * Push a pseudo variable `...' (and
1028 * don't evaluate it) in the vararg
1029 * case. A special case is when the
1030 * variable is itself `...'.
1032 if (proc->vararg && i == proc->minargs) {
1033 if (t->type != OP_REST)
1034 setvar_raw(xstrdup("..."), t);
1039 * The arguments are a linked list of
1040 * ops. Setvar will call eval that
1041 * will evaluate *all* the arguments.
1042 * The dance here that sets next to
1043 * NULL and then restores it is to
1044 * avoid this behaviour.
1048 ret = setvar(proc->args[i], t);
1055 if ((ret = eval(proc->body)) != EVAL_OK)
1064 pushv(&op->v.literal);
1068 if ((ret = getvar(op->v.var, &a)) != EVAL_OK)
1074 if ((ret = eval(op->v.cast.expr)) != EVAL_OK)
1077 if ((ret = val_cast(&a, op->v.cast.totype)) != EVAL_OK)
1083 if ((ret = eval(op->v.bin_cmp.a)) != EVAL_OK)
1085 if ((ret = eval(op->v.bin_cmp.b)) != EVAL_OK)
1090 pushbool(val_eq(&a, &b));
1094 if ((ret = eval(op->v.bin_cmp.a)) != EVAL_OK)
1096 if ((ret = eval(op->v.bin_cmp.b)) != EVAL_OK)
1101 pushbool(val_leq(&a, &b));
1105 if ((ret = eval(op->v.faccess.expr)) != EVAL_OK)
1108 if ((ret = val_faccess(&a, op->v.faccess.field, &b))
1115 if ((ret = eval(op->v.sfail.expr)) == EVAL_OK) {
1117 printf("expecting failure");
1118 if (op->v.sfail.msg != NULL)
1119 printf(" \"%s\"", op->v.sfail.msg);
1121 printf("expression: ");
1122 pp_op(op->v.sfail.expr);
1126 if (ret == EVAL_SKIP)
1131 if ((ret = getvar_raw("...", &t)) == EVAL_OK) {
1132 for (i = 0; t != NULL; t = t->next)
1141 fprintf(stderr, "invalid op, aborting.\n");
1146 return eval(op->next);
1151 prepare_funcall(void)
1157 push_arg(struct op *op)
1163 op_funcall(struct proc *proc)
1165 struct op *op, *argv;
1168 argv = finalize(&args, &argc);
1170 op = newop(OP_FUNCALL);
1171 op->v.funcall.proc = proc;
1172 op->v.funcall.argv = argv;
1173 op->v.funcall.argc = argc;
1179 add_builtin_proc(const char *name, int (*fn)(int), int argc, int vararg)
1183 proc = xcalloc(1, sizeof(*proc));
1184 proc->name = xstrdup(name);
1185 proc->nativefn = fn;
1186 proc->minargs = argc;
1187 proc->vararg = vararg;
1189 TAILQ_INSERT_HEAD(&procs, proc, entry);
1199 proc_setup_body(void)
1201 struct opstack *argv;
1206 for (i = 0, op = argv->base.next; op != NULL; i++) {
1208 * TODO: should free the whole list on error but..,
1209 * we're gonna exit real soon(tm)!
1211 if (op->type != OP_VAR && op->type != OP_REST)
1217 assert(i == argv->counter);
1223 proc_done(char *name)
1226 struct op *op, *next, *argv, *body;
1229 argv = finalize(&args, &argc);
1230 body = finalize(&blocks, NULL);
1232 proc = xcalloc(1, sizeof(*proc));
1234 proc->minargs = argc;
1236 for (i = 0, op = argv; op != NULL; ++i) {
1237 if (op->type == OP_REST) {
1243 proc->args[i] = xstrdup(op->v.var);
1249 assert(i == argc || (proc->vararg && i == proc->minargs));
1253 TAILQ_INSERT_HEAD(&procs, proc, entry);
1257 block_push(struct op *op)
1263 proc_by_name(const char *name)
1267 TAILQ_FOREACH(p, &procs, entry) {
1268 if (!strcmp(p->name, name))
1282 test_done(int shouldfail, char *name)
1286 test = xcalloc(1, sizeof(*test));
1287 test->shouldfail = shouldfail;
1289 test->body = finalize(&blocks, NULL);
1291 TAILQ_INSERT_TAIL(&tests, test, entry);
1297 builtin_print(int argc)
1304 for (i = argc; i > 0; --i) {
1306 if (v.type == V_STR)
1307 printf("%s", v.v.str);
1321 builtin_debug(int argc)
1324 return builtin_print(argc);
1331 builtin_skip(int argc)
1337 builtin_iota(int argc)
1342 if ((v.v.u16 = ++lasttag) == 255)
1343 v.v.u16 = ++lasttag;
1350 builtin_send(int argc)
1361 * Compute the length of the packet. 4 is for the initial
1366 for (i = argc; i > 0; --i) {
1370 len += 2; /* count */
1371 len += strlen(v.v.str);
1388 printf("%s: can't serialize ", __func__);
1395 if (len > UINT16_MAX) {
1397 printf("%s: message size too long: got %d when max is %d\n",
1398 __func__, len, UINT16_MAX);
1402 if ((buf = imsg_create(&ibuf, IMSG_BUF, 0, 0, len)) == NULL)
1403 fatal("imsg_create(%d)", len);
1406 imsg_add(buf, &len, sizeof(len));
1408 for (i = argc; i > 0; --i) {
1412 slen = strlen(v.v.str);
1413 slen = htole16(slen);
1414 imsg_add(buf, &slen, sizeof(slen));
1415 imsg_add(buf, v.v.str, strlen(v.v.str));
1419 imsg_add(buf, &v.v.u8, 1);
1423 v.v.u16 = htole16(v.v.u16);
1424 imsg_add(buf, &v.v.u16, 2);
1428 v.v.u32 = htole32(v.v.u32);
1429 imsg_add(buf, &v.v.u32, 4);
1434 imsg_close(&ibuf, buf);
1436 if (imsg_flush(&ibuf) == -1) {
1439 printf("%s: imsg_flush failed: %s\n", __func__, strerror(i));
1448 builtin_recv(int argc)
1454 int serrno, want_more = 0;
1456 if (lastmsg != NULL) {
1462 pfd.events = POLLIN;
1464 if (poll(&pfd, 1, INFTIM) == -1) {
1467 printf("%s: poll failed: %s\n", __func__, strerror(serrno));
1471 if ((n = imsg_read(&ibuf)) == -1) {
1472 if (errno == EAGAIN)
1478 printf("child disconnected\n");
1485 if ((n = imsg_get(&ibuf, &imsg)) == -1)
1491 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1492 switch (imsg.hdr.type) {
1495 if ((v.v.msg.msg = malloc(datalen)) == NULL)
1497 memcpy(v.v.msg.msg, imsg.data, datalen);
1498 v.v.msg.len = datalen;
1505 printf("subprocess closed the connection\n");
1516 printf("got unknown message from subprocess: %d\n",
1526 fatalx("reached the end of %s\n", __func__);
1530 spawn_client_proc(void)
1532 const char *argv[4];
1533 int p[2], out[2], argc = 0;
1536 if (child_out != -1)
1539 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
1540 PF_UNSPEC, p) == -1)
1541 fatal("socketpair");
1543 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
1544 PF_UNSPEC, out) == -1)
1545 fatal("socketpair");
1547 switch (pid = fork()) {
1549 fatal("cannot fork");
1557 msgbuf_clear(&ibuf.w);
1560 imsg_init(&ibuf, p[0]);
1568 if (dup2(out[1], 1) == -1 ||
1569 dup2(out[1], 2) == -1)
1573 if (dup2(p[1], 3) == -1)
1574 fatal("cannot setup imsg fd");
1575 } else if (fcntl(F_SETFD, 0) == -1)
1576 fatal("cannot setup imsg fd");
1578 argv[argc++] = argv0;
1579 argv[argc++] = "-Tc";
1582 argv[argc++] = "-v";
1585 argv[argc++] = NULL;
1587 execvp(argv0, (char *const *)argv);
1592 prepare_child_for_test(struct test *t)
1595 struct kd_auth_proc rauth;
1597 if ((pw = getpwuid(uid)) == NULL)
1598 fatal("getpwuid(%d)", uid);
1600 memset(&rauth, 0, sizeof(rauth));
1601 strlcpy(rauth.uname, pw->pw_name, sizeof(rauth.uname));
1602 strlcpy(rauth.dir, dir, sizeof(rauth.dir));
1604 imsg_compose(&ibuf, IMSG_AUTH, 0, 0, -1,
1605 &rauth, sizeof(rauth));
1607 if (imsg_flush(&ibuf) == -1)
1608 fatal("imsg_flush");
1612 run_test(struct test *t)
1619 puts("=====================");
1621 puts("=====================");
1627 if (t->body == NULL) {
1629 printf("no instructions, skipping...\n");
1633 pid = spawn_client_proc();
1634 prepare_child_for_test(t);
1635 ret = eval(t->body);
1637 imsg_compose(&ibuf, IMSG_CONN_GONE, 0, 0, -1, NULL, 0);
1640 while (waitpid(pid, NULL, 0) != pid)
1645 if (t->shouldfail) {
1646 if (ret == EVAL_OK) {
1648 printf("test was expected to fail\n");
1650 } else if (ret == EVAL_ERR)
1658 main(int argc, char **argv)
1662 int ch, i, r, passed = 0, failed = 0, skipped = 0;
1664 const char *pat = NULL;
1667 assert(argv0 = argv[0]);
1669 signal(SIGPIPE, SIG_IGN);
1671 log_init(1, LOG_DAEMON);
1674 /* prepare the global env */
1677 add_builtin_proc("print", builtin_print, 1, 1);
1678 add_builtin_proc("debug", builtin_debug, 1, 1);
1679 add_builtin_proc("skip", builtin_skip, 0, 0);
1680 add_builtin_proc("iota", builtin_iota, 0, 0);
1681 add_builtin_proc("send", builtin_send, 2, 1);
1682 add_builtin_proc("recv", builtin_recv, 0, 0);
1684 while ((ch = getopt(argc, argv, "nT:r:vx:")) != -1) {
1690 assert(*optarg == 'c');
1703 fprintf(stderr, "Usage: %s [-nv] [files...]\n",
1715 fatal("missing root test dir");
1717 if (stat(dir, &sb) == -1)
1718 fatal("stat(\"%s\")", dir);
1724 if (regcomp(®, pat, REG_ICASE | REG_NOSUB) != 0)
1725 fatalx("invalid regexp: %s", pat);
1727 for (i = 0; i < argc; ++i)
1731 fprintf(stderr, "files OK\n");
1735 /* Check for root privileges. */
1737 fatalx("need root privileges");
1740 TAILQ_FOREACH(t, &tests, entry) {
1741 if (regexec(®, t->name, 0, NULL, 0) != 0)
1744 printf("===> [%d/%d] running test \"%s\"... ", i+1, ntests,
1763 printf("skipped\n");
1774 printf("%d/%d passed (%d skipped and %d failed)\n",
1775 passed, i, skipped, failed);