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>
48 static const char *argv0;
49 static const char *dir;
52 static uint8_t *lastmsg;
54 static struct imsgbuf ibuf;
55 static int ibuf_inuse;
56 static int child_out = -1;
58 static struct procs procs = TAILQ_HEAD_INITIALIZER(procs);
59 static struct tests tests = TAILQ_HEAD_INITIALIZER(tests);
63 static struct opstacks blocks = TAILQ_HEAD_INITIALIZER(blocks);
64 static struct opstacks args = TAILQ_HEAD_INITIALIZER(args);
66 #define STACK_HEIGHT 64
67 static struct value vstack[STACK_HEIGHT];
70 static struct envs envs = TAILQ_HEAD_INITIALIZER(envs);
72 static const struct value v_false = {.type = V_NUM, .v = {.num = 0}};
73 static const struct value v_true = {.type = V_NUM, .v = {.num = 1}};
75 static uint8_t lasttag;
78 static int syntaxcheck;
80 static const char *filler;
92 check_for_output(void)
94 static char buf[BUFSIZ];
100 if (poll(&pfd, 1, 0) == -1)
103 if (!(pfd.revents & POLLIN))
107 if ((r = read(child_out, buf, sizeof(buf))) == -1) {
115 fwrite(buf, 1, r, stdout);
120 peekn(int depth, struct value *v)
123 errx(1, "can't peek the stack at %d: underflow",
125 memcpy(v, &vstack[stackh - depth], sizeof(*v));
128 printf("peeking(%d) ", depth); pp_val(v); printf("\n");
133 popv(struct value *v)
136 errx(1, "can't pop the stack: underflow");
137 memcpy(v, &vstack[--stackh], sizeof(*v));
140 printf("popping "); pp_val(v); printf("\n");
154 pushv(const struct value *v)
156 if (stackh == STACK_HEIGHT)
157 errx(1, "can't push the stack: overflow");
160 printf("pushing "); pp_val(v); printf("\n");
163 memcpy(&vstack[stackh++], v, sizeof(*v));
169 pushv(n ? &v_true : &v_false);
182 static inline struct opstack *
183 pushstack(struct opstacks *stack)
187 ops = xcalloc(1, sizeof(*ops));
188 TAILQ_INSERT_HEAD(stack, ops, entry);
192 static inline struct opstack *
193 peek(struct opstacks *stack)
195 if (TAILQ_EMPTY(stack))
196 errx(1, "%s: args underflow", __func__);
198 return TAILQ_FIRST(stack);
201 static inline struct op *
202 finalize(struct opstacks *stack, int *argc)
207 if (TAILQ_EMPTY(stack))
208 errx(1, "%s: args underflow", __func__);
211 TAILQ_REMOVE(&args, ops, entry);
215 *argc = ops->counter;
222 push(struct opstacks *stack, struct op *op)
227 if (ops->last == NULL) {
231 ops->last->next = op;
243 e = xcalloc(1, sizeof(*e));
244 TAILQ_INSERT_HEAD(&envs, e, entry);
247 static inline struct env *
250 assert(!TAILQ_EMPTY(&envs));
251 return TAILQ_FIRST(&envs);
258 struct binding *b, *tb;
261 TAILQ_REMOVE(&envs, e, entry);
263 TAILQ_FOREACH_SAFE(b, &e->bindings, entry, tb)
270 setvar(char *sym, struct op *op)
277 if ((ret = eval(op)) != EVAL_OK)
280 if (stackh != height + 1) {
282 printf("trying to assign to `%s' a void value: ", sym);
288 b = xcalloc(1, sizeof(*b));
292 e = TAILQ_FIRST(&envs);
293 TAILQ_INSERT_HEAD(&e->bindings, b, entry);
299 setvar_raw(char *sym, struct op *op)
304 b = xcalloc(1, sizeof(*b));
308 e = TAILQ_FIRST(&envs);
309 TAILQ_INSERT_HEAD(&e->bindings, b, entry);
313 getvar(const char *sym, struct value *v)
318 TAILQ_FOREACH(e, &envs, entry) {
319 TAILQ_FOREACH(b, &e->bindings, entry) {
320 if (!strcmp(sym, b->name)) {
321 memcpy(v, &b->val, sizeof(*v));
328 fprintf(stderr, "unbound variable %s\n", sym);
333 getvar_raw(const char *sym, struct op **raw)
338 TAILQ_FOREACH(e, &envs, entry) {
339 TAILQ_FOREACH(b, &e->bindings, entry) {
340 if (!strcmp(sym, b->name)) {
351 global_set(char *sym, struct op *op)
356 /* TODO: check for duplicates */
358 if (op->type != OP_LITERAL &&
359 (op->type == OP_CAST && op->v.cast.expr->type != OP_LITERAL))
362 b = xcalloc(1, sizeof(*b));
365 /* it's only a cast on a literal! */
366 if (op->type == OP_CAST) {
367 if (eval(op) != EVAL_OK) {
373 memcpy(&b->val, &op->v.literal, sizeof(b->val));
375 e = TAILQ_LAST(&envs, envs);
376 TAILQ_INSERT_HEAD(&e->bindings, b, entry);
386 op = xcalloc(1, sizeof(*op));
393 free_op_rec(struct op *op)
405 free_op(struct op *op)
416 free(op->v.assign.name);
417 free_op_rec(op->v.assign.expr);
420 free_op_rec(op->v.assert);
423 free_op_rec(op->v.funcall.argv);
429 free_op_rec(op->v.cast.expr);
433 free_op_rec(op->v.bin_cmp.a);
434 free_op_rec(op->v.bin_cmp.b);
437 free_op_rec(op->v.faccess.expr);
438 free(op->v.faccess.field);
441 free(op->v.sfail.msg);
442 free_op_rec(op->v.sfail.expr);
455 return newop(OP_REST);
459 op_assign(char *sym, struct op *expr)
463 op = newop(OP_ASSIGN);
464 op->v.assign.name = sym;
465 op->v.assign.expr = expr;
471 op_assert(struct op *expr)
475 op = newop(OP_ASSERT);
493 op_lit_str(char *str)
497 op = newop(OP_LITERAL);
498 op->v.literal.type = V_STR;
499 op->v.literal.v.str = str;
505 op_lit_num(uint64_t n)
509 op = newop(OP_LITERAL);
510 op->v.literal.type = V_NUM;
511 op->v.literal.v.num = n;
517 op_cmp_eq(struct op *a, struct op *b)
521 op = newop(OP_CMP_EQ);
529 op_cmp_leq(struct op *a, struct op *b)
533 op = newop(OP_CMP_LEQ);
541 op_cast(struct op *expr, int totype)
546 op->v.cast.expr = expr;
547 op->v.cast.totype = totype;
553 op_faccess(struct op *expr, char *field)
557 op = newop(OP_FACCESS);
558 op->v.faccess.expr = expr;
559 op->v.faccess.field = field;
565 op_sfail(struct op *expr, char *msg)
569 op = newop(OP_SFAIL);
570 op->v.sfail.expr = expr;
571 op->v.sfail.msg = msg;
581 op = newop(OP_VARGS);
587 ppf_val(FILE *f, const struct value *val)
593 fprintf(f, "%s", val->v.str);
596 fprintf(f, "\"%s\"", val->v.str);
599 fprintf(f, "%"PRIi64, val->v.num);
602 fprintf(f, "%"PRIu8, val->v.u8);
605 fprintf(f, "%"PRIu16, val->v.u16);
608 fprintf(f, "%"PRIu32, val->v.u32);
612 for (i = 0; i < val->v.msg.len; ++i)
613 fprintf(f, "%x%s", val->v.msg.msg[i],
614 i == val->v.msg.len-1 ? "" : " ");
618 fprintf(f, "qids[n=%zu]", val->v.qidvec.len);
621 fprintf(f, "<unknown value>");
627 pp_val(const struct value *val)
629 ppf_val(stdout, val);
633 val_type(struct value *v)
636 case V_SYM: return "symbol";
637 case V_STR: return "string";
638 case V_NUM: return "number";
639 case V_MSG: return "message";
640 case V_QID: return "qid";
641 case V_U8: return "u8";
642 case V_U16: return "u16";
643 case V_U32: return "u32";
644 default: return "unknown";
649 val_trueish(struct value *a)
657 val_isnum(struct value *a)
659 return a->type == V_NUM
666 val_tonum(struct value *a)
669 case V_NUM: return a->v.num;
670 case V_U8: return a->v.u8;
671 case V_U16: return a->v.u16;
672 case V_U32: return a->v.u32;
675 fprintf(stderr, "%s: given value is not a number\n", __func__);
681 val_eq(struct value *a, struct value *b)
683 if (val_isnum(a) && val_isnum(b))
684 return val_tonum(a) == val_tonum(b);
686 if (a->type != b->type)
692 return !strcmp(a->v.str, b->v.str);
699 val_leq(struct value *a, struct value *b)
701 if (val_isnum(a) && val_isnum(b))
702 return val_tonum(a) <= val_tonum(b);
706 static inline const char *
707 pp_totype(int totype)
710 * Not all of these are valid cast type thought, including
711 * every possibility only to aid debugging.
714 case V_STR: return "str";
715 case V_SYM: return "sym";
716 case V_NUM: return "num";
717 case V_QID: return "qid";
718 case V_U8: return "u8";
719 case V_U16: return "u16";
720 case V_U32: return "u32";
721 default: return "unknown";
726 val_cast(struct value *a, int totype)
730 #define NUMCAST(val, t, c, totype, max) do { \
733 fprintf(stderr, "can't cast %"PRIu64 \
734 " to %s\n", val, pp_totype(totype)); \
742 if (a->type == totype)
747 fprintf(stderr, "can't cast ");
749 fprintf(stderr, " to type %s\n", pp_totype(totype));
755 case V_U8: NUMCAST(v, u8, uint8_t, totype, UINT8_MAX);
756 case V_U16: NUMCAST(v, u16, uint16_t, totype, UINT16_MAX);
757 case V_U32: NUMCAST(v, u32, uint32_t, totype, UINT32_MAX);
760 fprintf(stderr, "can't cast %"PRIu64" to %s\n",
761 v, pp_totype(totype));
769 val_faccess(struct value *a, const char *field, struct value *ret)
775 #define MSGTYPE(m) *(m.msg + 4) /* skip the length */
779 /* TODO: add path. needs uint64_t values thought! */
780 if (!strcmp(field, "vers")) {
782 memcpy(&ret->v.u32, a->v.qid+1, 4);
784 } else if (!strcmp(field, "type")) {
786 ret->v.u8 = *a->v.qid;
792 mtype = MSGTYPE(a->v.msg);
793 if (!strcmp(field, "type")) {
795 ret->v.u8 = MSGTYPE(a->v.msg);
797 } else if (!strcmp(field, "tag")) {
799 memcpy(&ret->v.u16, &a->v.msg.msg[5], 2);
800 ret->v.u16 = le16toh(ret->v.u16);
802 } else if (!strcmp(field, "msize") && mtype == Rversion) {
804 memcpy(&ret->v.u32, &a->v.msg.msg[7], 4);
805 ret->v.u32 = le32toh(ret->v.u32);
807 } else if (!strcmp(field, "qid") && mtype == Rattach) {
809 memcpy(&ret->v.qid, &a->v.msg.msg[7], QIDSIZE);
811 } else if (!strcmp(field, "nwqid") && mtype == Rwalk) {
813 memcpy(&ret->v.u16, &a->v.msg.msg[7], 2);
814 ret->v.u16 = le16toh(ret->v.u16);
816 } else if (!strcmp(field, "wqid") && mtype == Rwalk) {
817 ret->type = V_QIDVEC;
818 ret->v.qidvec.start = &a->v.msg.msg[9];
819 memcpy(&len, &a->v.msg.msg[7], 2);
821 ret->v.qidvec.len = len;
827 len = strtonum(field, 0, MAXWELEM, &errstr);
828 if (errstr != NULL) {
830 printf("can't access qid #%s: %s\n", field, errstr);
834 if (len >= a->v.qidvec.len) {
836 printf("can't access qid #%d: out-of-bound "
837 "(max %zu)\n", len, a->v.qidvec.len);
842 memcpy(&ret->v.qid, a->v.qidvec.start + len * QIDSIZE,
852 printf("can't access field `%s' on type %s (", field, val_type(a));
870 printf("%s = ", op->v.assign.name);
871 pp_op(op->v.assign.expr);
878 printf("funcall %s(", op->v.funcall.proc->name);
879 for (aux = op->v.funcall.argv; aux != NULL; aux = aux->next) {
881 if (aux->next != NULL)
887 pp_val(&op->v.literal);
890 printf("%s", op->v.var);
893 pp_op(op->v.cast.expr);
895 switch (op->v.cast.totype) {
896 case V_U8: printf("u8"); break;
897 case V_U16: printf("u16"); break;
898 case V_U32: printf("u32"); break;
899 case V_STR: printf("str"); break;
900 default: printf("???"); break;
904 pp_op(op->v.bin_cmp.a);
906 pp_op(op->v.bin_cmp.b);
909 pp_op(op->v.bin_cmp.a);
911 pp_op(op->v.bin_cmp.b);
914 pp_op(op->v.faccess.expr);
915 printf(".%s", op->v.faccess.field);
918 printf("should-fail ");
919 pp_op(op->v.sfail.expr);
920 if (op->v.sfail.msg != NULL)
921 printf(": \"%s\"", op->v.sfail.msg);
927 printf(" ???[%d] ", op->type);
932 pp_block(struct op *op)
948 struct op *t, *tnext;
959 * Try to load the rest argument. Note that it can be
962 if ((ret = getvar_raw("...", &t)) == EVAL_OK)
963 if ((ret = eval(t)) != EVAL_OK)
968 ret = setvar(op->v.assign.name, op->v.assign.expr);
974 if ((ret = eval(op->v.assert)) != EVAL_OK)
977 if (!val_trueish(&a)) {
979 printf("assertion failed: ");
987 /* assume airity matches */
989 proc = op->v.funcall.proc;
990 if (proc->nativefn != NULL) {
992 * Push arguments on the stack for builtin
993 * functions. Counting the height of the
994 * stack is done to compute the correct number
995 * in the vararg case. argc only counts the
996 * "syntactical" arguments, i.e. foo(x, ...)
997 * has argc == 2, but at runtime argc may be
998 * 1, 2 or a greater number!
1002 t = op->v.funcall.argv;
1003 if (t != NULL && (ret = eval(t)) != EVAL_OK)
1009 if ((ret = proc->nativefn(i))
1013 if (proc->body == NULL) {
1015 printf("warn: calling the empty proc `%s'\n",
1022 for (t = op->v.funcall.argv, i = 0;
1026 * Push a pseudo variable `...' (and
1027 * don't evaluate it) in the vararg
1028 * case. A special case is when the
1029 * variable is itself `...'.
1031 if (proc->vararg && i == proc->minargs) {
1032 if (t->type != OP_REST)
1033 setvar_raw(xstrdup("..."), t);
1038 * The arguments are a linked list of
1039 * ops. Setvar will call eval that
1040 * will evaluate *all* the arguments.
1041 * The dance here that sets next to
1042 * NULL and then restores it is to
1043 * avoid this behaviour.
1047 ret = setvar(proc->args[i], t);
1054 if ((ret = eval(proc->body)) != EVAL_OK)
1063 pushv(&op->v.literal);
1067 if ((ret = getvar(op->v.var, &a)) != EVAL_OK)
1073 if ((ret = eval(op->v.cast.expr)) != EVAL_OK)
1076 if ((ret = val_cast(&a, op->v.cast.totype)) != EVAL_OK)
1082 if ((ret = eval(op->v.bin_cmp.a)) != EVAL_OK)
1084 if ((ret = eval(op->v.bin_cmp.b)) != EVAL_OK)
1089 pushbool(val_eq(&a, &b));
1093 if ((ret = eval(op->v.bin_cmp.a)) != EVAL_OK)
1095 if ((ret = eval(op->v.bin_cmp.b)) != EVAL_OK)
1100 pushbool(val_leq(&a, &b));
1104 if ((ret = eval(op->v.faccess.expr)) != EVAL_OK)
1107 if ((ret = val_faccess(&a, op->v.faccess.field, &b))
1114 if ((ret = eval(op->v.sfail.expr)) == EVAL_OK) {
1116 printf("expecting failure");
1117 if (op->v.sfail.msg != NULL)
1118 printf(" \"%s\"", op->v.sfail.msg);
1120 printf("expression: ");
1121 pp_op(op->v.sfail.expr);
1125 if (ret == EVAL_SKIP)
1130 if ((ret = getvar_raw("...", &t)) == EVAL_OK) {
1131 for (i = 0; t != NULL; t = t->next)
1140 fprintf(stderr, "invalid op, aborting.\n");
1145 return eval(op->next);
1150 prepare_funcall(void)
1156 push_arg(struct op *op)
1162 op_funcall(struct proc *proc)
1164 struct op *op, *argv;
1167 argv = finalize(&args, &argc);
1169 op = newop(OP_FUNCALL);
1170 op->v.funcall.proc = proc;
1171 op->v.funcall.argv = argv;
1172 op->v.funcall.argc = argc;
1178 add_builtin_proc(const char *name, int (*fn)(int), int argc, int vararg)
1182 proc = xcalloc(1, sizeof(*proc));
1183 proc->name = xstrdup(name);
1184 proc->nativefn = fn;
1185 proc->minargs = argc;
1186 proc->vararg = vararg;
1188 TAILQ_INSERT_HEAD(&procs, proc, entry);
1198 proc_setup_body(void)
1200 struct opstack *argv;
1205 for (i = 0, op = argv->base.next; op != NULL; i++) {
1207 * TODO: should free the whole list on error but..,
1208 * we're gonna exit real soon(tm)!
1210 if (op->type != OP_VAR && op->type != OP_REST)
1216 assert(i == argv->counter);
1222 proc_done(char *name)
1225 struct op *op, *next, *argv, *body;
1228 argv = finalize(&args, &argc);
1229 body = finalize(&blocks, NULL);
1231 proc = xcalloc(1, sizeof(*proc));
1233 proc->minargs = argc;
1235 for (i = 0, op = argv; op != NULL; ++i) {
1236 if (op->type == OP_REST) {
1242 proc->args[i] = xstrdup(op->v.var);
1248 assert(i == argc || (proc->vararg && i == proc->minargs));
1252 TAILQ_INSERT_HEAD(&procs, proc, entry);
1256 block_push(struct op *op)
1262 proc_by_name(const char *name)
1266 TAILQ_FOREACH(p, &procs, entry) {
1267 if (!strcmp(p->name, name))
1281 test_done(int shouldfail, char *name)
1285 test = xcalloc(1, sizeof(*test));
1286 test->shouldfail = shouldfail;
1288 test->body = finalize(&blocks, NULL);
1290 TAILQ_INSERT_TAIL(&tests, test, entry);
1296 builtin_print(int argc)
1303 for (i = argc; i > 0; --i) {
1305 if (v.type == V_STR)
1306 printf("%s", v.v.str);
1320 builtin_debug(int argc)
1323 return builtin_print(argc);
1330 builtin_skip(int argc)
1336 builtin_iota(int argc)
1341 if ((v.v.u16 = ++lasttag) == 255)
1342 v.v.u16 = ++lasttag;
1349 builtin_send(int argc)
1360 * Compute the length of the packet. 4 is for the initial
1365 for (i = argc; i > 0; --i) {
1369 len += 2; /* count */
1370 len += strlen(v.v.str);
1387 printf("%s: can't serialize ", __func__);
1394 if (len > UINT16_MAX) {
1396 printf("%s: message size too long: got %d when max is %d\n",
1397 __func__, len, UINT16_MAX);
1401 if ((buf = imsg_create(&ibuf, IMSG_BUF, 0, 0, len)) == NULL)
1402 fatal("imsg_create(%d)", len);
1405 imsg_add(buf, &len, sizeof(len));
1407 for (i = argc; i > 0; --i) {
1411 slen = strlen(v.v.str);
1412 slen = htole16(slen);
1413 imsg_add(buf, &slen, sizeof(slen));
1414 imsg_add(buf, v.v.str, strlen(v.v.str));
1418 imsg_add(buf, &v.v.u8, 1);
1422 v.v.u16 = htole16(v.v.u16);
1423 imsg_add(buf, &v.v.u16, 2);
1427 v.v.u32 = htole32(v.v.u32);
1428 imsg_add(buf, &v.v.u32, 4);
1433 imsg_close(&ibuf, buf);
1435 if (imsg_flush(&ibuf) == -1) {
1438 printf("%s: imsg_flush failed: %s\n", __func__, strerror(i));
1447 builtin_recv(int argc)
1453 int serrno, want_more = 0;
1455 if (lastmsg != NULL) {
1461 pfd.events = POLLIN;
1463 if (poll(&pfd, 1, INFTIM) == -1) {
1466 printf("%s: poll failed: %s\n", __func__, strerror(serrno));
1470 if ((n = imsg_read(&ibuf)) == -1) {
1471 if (errno == EAGAIN)
1477 printf("child disconnected\n");
1484 if ((n = imsg_get(&ibuf, &imsg)) == -1)
1490 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1491 switch (imsg.hdr.type) {
1494 if ((v.v.msg.msg = malloc(datalen)) == NULL)
1496 memcpy(v.v.msg.msg, imsg.data, datalen);
1497 v.v.msg.len = datalen;
1504 printf("subprocess closed the connection\n");
1515 printf("got unknown message from subprocess: %d\n",
1525 fatalx("reached the end of %s\n", __func__);
1529 spawn_client_proc(void)
1531 const char *argv[4];
1532 int p[2], out[2], argc = 0;
1535 if (child_out != -1)
1538 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
1539 PF_UNSPEC, p) == -1)
1540 fatal("socketpair");
1542 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
1543 PF_UNSPEC, out) == -1)
1544 fatal("socketpair");
1546 switch (pid = fork()) {
1548 fatal("cannot fork");
1556 msgbuf_clear(&ibuf.w);
1559 imsg_init(&ibuf, p[0]);
1567 if (dup2(out[1], 1) == -1 ||
1568 dup2(out[1], 2) == -1)
1572 if (dup2(p[1], 3) == -1)
1573 fatal("cannot setup imsg fd");
1574 } else if (fcntl(F_SETFD, 0) == -1)
1575 fatal("cannot setup imsg fd");
1577 argv[argc++] = argv0;
1578 argv[argc++] = "-Tc";
1581 argv[argc++] = "-v";
1584 argv[argc++] = NULL;
1586 execvp(argv0, (char *const *)argv);
1591 prepare_child_for_test(struct test *t)
1594 struct kd_auth_proc rauth;
1596 if ((pw = getpwuid(uid)) == NULL)
1597 fatal("getpwuid(%d)", uid);
1599 memset(&rauth, 0, sizeof(rauth));
1600 strlcpy(rauth.uname, pw->pw_name, sizeof(rauth.uname));
1601 strlcpy(rauth.dir, dir, sizeof(rauth.dir));
1603 imsg_compose(&ibuf, IMSG_AUTH, 0, 0, -1,
1604 &rauth, sizeof(rauth));
1606 if (imsg_flush(&ibuf) == -1)
1607 fatal("imsg_flush");
1611 run_test(struct test *t)
1618 puts("=====================");
1620 puts("=====================");
1626 if (t->body == NULL) {
1628 printf("no instructions, skipping...\n");
1632 pid = spawn_client_proc();
1633 prepare_child_for_test(t);
1634 ret = eval(t->body);
1636 imsg_compose(&ibuf, IMSG_CONN_GONE, 0, 0, -1, NULL, 0);
1639 while (waitpid(pid, NULL, 0) != pid)
1644 if (t->shouldfail) {
1645 if (ret == EVAL_OK) {
1647 printf("test was expected to fail\n");
1649 } else if (ret == EVAL_ERR)
1657 main(int argc, char **argv)
1661 int ch, i, r, passed = 0, failed = 0, skipped = 0;
1663 const char *pat = NULL;
1666 if ((argv0 = argv[0]) == NULL)
1667 fatal("empty 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);