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>
51 static const char *argv0;
52 static const char *dir;
55 static uint8_t *lastmsg;
57 static struct imsgbuf ibuf;
58 static int ibuf_inuse;
59 static int child_out = -1;
61 static struct procs procs = TAILQ_HEAD_INITIALIZER(procs);
62 static struct tests tests = TAILQ_HEAD_INITIALIZER(tests);
66 static struct opstacks blocks = TAILQ_HEAD_INITIALIZER(blocks);
67 static struct opstacks args = TAILQ_HEAD_INITIALIZER(args);
69 #define STACK_HEIGHT 64
70 static struct value vstack[STACK_HEIGHT];
73 static struct envs envs = TAILQ_HEAD_INITIALIZER(envs);
75 static struct value v_false = {.type = V_NUM, .v = {.num = 0}};
76 static struct value v_true = {.type = V_NUM, .v = {.num = 1}};
78 static uint8_t lasttag;
81 static int syntaxcheck;
83 static const char *filler;
95 check_for_output(void)
97 static char buf[BUFSIZ];
103 if (poll(&pfd, 1, 0) == -1)
106 if (!(pfd.revents & POLLIN))
110 if ((r = read(child_out, buf, sizeof(buf))) == -1) {
118 fwrite(buf, 1, r, stdout);
123 peekn(int depth, struct value *v)
126 errx(1, "can't peek the stack at %d: underflow",
128 memcpy(v, &vstack[stackh - depth], sizeof(*v));
131 printf("peeking(%d) ", depth); pp_val(v); printf("\n");
136 popv(struct value *v)
139 errx(1, "can't pop the stack: underflow");
140 memcpy(v, &vstack[--stackh], sizeof(*v));
143 printf("popping "); pp_val(v); printf("\n");
157 pushv(struct value *v)
159 if (stackh == STACK_HEIGHT)
160 errx(1, "can't push the stack: overflow");
163 printf("pushing "); pp_val(v); printf("\n");
166 memcpy(&vstack[stackh++], v, sizeof(*v));
172 pushv(n ? &v_true : &v_false);
185 static inline struct opstack *
186 pushstack(struct opstacks *stack)
190 ops = xcalloc(1, sizeof(*ops));
191 TAILQ_INSERT_HEAD(stack, ops, entry);
195 static inline struct opstack *
196 peek(struct opstacks *stack)
198 if (TAILQ_EMPTY(stack))
199 errx(1, "%s: args underflow", __func__);
201 return TAILQ_FIRST(stack);
204 static inline struct op *
205 finalize(struct opstacks *stack, int *argc)
210 if (TAILQ_EMPTY(stack))
211 errx(1, "%s: args underflow", __func__);
214 TAILQ_REMOVE(&args, ops, entry);
218 *argc = ops->counter;
225 push(struct opstacks *stack, struct op *op)
230 if (ops->last == NULL) {
234 ops->last->next = op;
246 e = xcalloc(1, sizeof(*e));
247 TAILQ_INSERT_HEAD(&envs, e, entry);
250 static inline struct env *
253 assert(!TAILQ_EMPTY(&envs));
254 return TAILQ_FIRST(&envs);
261 struct binding *b, *tb;
264 TAILQ_REMOVE(&envs, e, entry);
266 TAILQ_FOREACH_SAFE(b, &e->bindings, entry, tb)
273 setvar(char *sym, struct op *op)
280 if ((ret = eval(op)) != EVAL_OK)
283 if (stackh != height + 1) {
285 printf("trying to assign to `%s' a void value: ", sym);
291 b = xcalloc(1, sizeof(*b));
295 e = TAILQ_FIRST(&envs);
296 TAILQ_INSERT_HEAD(&e->bindings, b, entry);
302 setvar_raw(char *sym, struct op *op)
307 b = xcalloc(1, sizeof(*b));
311 e = TAILQ_FIRST(&envs);
312 TAILQ_INSERT_HEAD(&e->bindings, b, entry);
316 getvar(const char *sym, struct value *v)
321 TAILQ_FOREACH(e, &envs, entry) {
322 TAILQ_FOREACH(b, &e->bindings, entry) {
323 if (!strcmp(sym, b->name)) {
324 memcpy(v, &b->val, sizeof(*v));
331 fprintf(stderr, "unbound variable %s\n", sym);
336 getvar_raw(const char *sym, struct op **raw)
341 TAILQ_FOREACH(e, &envs, entry) {
342 TAILQ_FOREACH(b, &e->bindings, entry) {
343 if (!strcmp(sym, b->name)) {
354 global_set(char *sym, struct op *op)
359 /* TODO: check for duplicates */
361 if (op->type != OP_LITERAL &&
362 (op->type == OP_CAST && op->v.cast.expr->type != OP_LITERAL))
365 b = xcalloc(1, sizeof(*b));
368 /* it's only a cast on a literal! */
369 if (op->type == OP_CAST) {
370 if (eval(op) != EVAL_OK) {
376 memcpy(&b->val, &op->v.literal, sizeof(b->val));
378 e = TAILQ_LAST(&envs, envs);
379 TAILQ_INSERT_HEAD(&e->bindings, b, entry);
389 op = xcalloc(1, sizeof(*op));
396 free_op_rec(struct op *op)
408 free_op(struct op *op)
419 free(op->v.assign.name);
420 free_op_rec(op->v.assign.expr);
423 free_op_rec(op->v.assert);
426 free_op_rec(op->v.funcall.argv);
432 free_op_rec(op->v.cast.expr);
436 free_op_rec(op->v.bin_cmp.a);
437 free_op_rec(op->v.bin_cmp.b);
440 free_op_rec(op->v.faccess.expr);
441 free(op->v.faccess.field);
444 free(op->v.sfail.msg);
445 free_op_rec(op->v.sfail.expr);
458 return newop(OP_REST);
462 op_assign(char *sym, struct op *expr)
466 op = newop(OP_ASSIGN);
467 op->v.assign.name = sym;
468 op->v.assign.expr = expr;
474 op_assert(struct op *expr)
478 op = newop(OP_ASSERT);
496 op_lit_str(char *str)
500 op = newop(OP_LITERAL);
501 op->v.literal.type = V_STR;
502 op->v.literal.v.str = str;
508 op_lit_num(uint64_t n)
512 op = newop(OP_LITERAL);
513 op->v.literal.type = V_NUM;
514 op->v.literal.v.num = n;
520 op_cmp_eq(struct op *a, struct op *b)
524 op = newop(OP_CMP_EQ);
532 op_cmp_leq(struct op *a, struct op *b)
536 op = newop(OP_CMP_LEQ);
544 op_cast(struct op *expr, int totype)
549 op->v.cast.expr = expr;
550 op->v.cast.totype = totype;
556 op_faccess(struct op *expr, char *field)
560 op = newop(OP_FACCESS);
561 op->v.faccess.expr = expr;
562 op->v.faccess.field = field;
568 op_sfail(struct op *expr, char *msg)
572 op = newop(OP_SFAIL);
573 op->v.sfail.expr = expr;
574 op->v.sfail.msg = msg;
584 op = newop(OP_VARGS);
590 ppf_val(FILE *f, struct value *val)
596 fprintf(f, "%s", val->v.str);
599 fprintf(f, "\"%s\"", val->v.str);
602 fprintf(f, "%"PRIi64, val->v.num);
605 fprintf(f, "%"PRIu8, val->v.u8);
608 fprintf(f, "%"PRIu16, val->v.u16);
611 fprintf(f, "%"PRIu32, val->v.u32);
615 for (i = 0; i < val->v.msg.len; ++i)
616 fprintf(f, "%x%s", val->v.msg.msg[i],
617 i == val->v.msg.len-1 ? "" : " ");
621 fprintf(f, "qids[n=%zu]", val->v.qidvec.len);
624 fprintf(f, "<unknown value>");
630 pp_val(struct value *val)
632 ppf_val(stdout, val);
636 val_type(struct value *v)
639 case V_SYM: return "symbol";
640 case V_STR: return "string";
641 case V_NUM: return "number";
642 case V_MSG: return "message";
643 case V_QID: return "qid";
644 case V_U8: return "u8";
645 case V_U16: return "u16";
646 case V_U32: return "u32";
647 default: return "unknown";
652 val_trueish(struct value *a)
660 val_isnum(struct value *a)
662 return a->type == V_NUM
669 val_tonum(struct value *a)
672 case V_NUM: return a->v.num;
673 case V_U8: return a->v.u8;
674 case V_U16: return a->v.u16;
675 case V_U32: return a->v.u32;
678 fprintf(stderr, "%s: given value is not a number\n", __func__);
684 val_eq(struct value *a, struct value *b)
686 if (val_isnum(a) && val_isnum(b))
687 return val_tonum(a) == val_tonum(b);
689 if (a->type != b->type)
695 return !strcmp(a->v.str, b->v.str);
702 val_leq(struct value *a, struct value *b)
704 if (val_isnum(a) && val_isnum(b))
705 return val_tonum(a) <= val_tonum(b);
709 static inline const char *
710 pp_totype(int totype)
713 * Not all of these are valid cast type thought, including
714 * every possibility only to aid debugging.
717 case V_STR: return "str";
718 case V_SYM: return "sym";
719 case V_NUM: return "num";
720 case V_QID: return "qid";
721 case V_U8: return "u8";
722 case V_U16: return "u16";
723 case V_U32: return "u32";
724 default: return "unknown";
729 val_cast(struct value *a, int totype)
733 #define NUMCAST(val, t, c, totype, max) do { \
736 fprintf(stderr, "can't cast %"PRIu64 \
737 " to %s\n", val, pp_totype(totype)); \
745 if (a->type == totype)
750 fprintf(stderr, "can't cast ");
752 fprintf(stderr, " to type %s\n", pp_totype(totype));
758 case V_U8: NUMCAST(v, u8, uint8_t, totype, UINT8_MAX);
759 case V_U16: NUMCAST(v, u16, uint16_t, totype, UINT16_MAX);
760 case V_U32: NUMCAST(v, u32, uint32_t, totype, UINT32_MAX);
763 fprintf(stderr, "can't cast %"PRIu64" to %s\n",
764 v, pp_totype(totype));
772 val_faccess(struct value *a, const char *field, struct value *ret)
778 #define MSGTYPE(m) *(m.msg + 4) /* skip the length */
782 /* TODO: add path. needs uint64_t values thought! */
783 if (!strcmp(field, "vers")) {
785 memcpy(&ret->v.u32, a->v.qid+1, 4);
787 } else if (!strcmp(field, "type")) {
789 ret->v.u8 = *a->v.qid;
795 mtype = MSGTYPE(a->v.msg);
796 if (!strcmp(field, "type")) {
798 ret->v.u8 = MSGTYPE(a->v.msg);
800 } else if (!strcmp(field, "tag")) {
802 memcpy(&ret->v.u16, &a->v.msg.msg[5], 2);
803 ret->v.u16 = le16toh(ret->v.u16);
805 } else if (!strcmp(field, "msize") && mtype == Rversion) {
807 memcpy(&ret->v.u32, &a->v.msg.msg[7], 4);
808 ret->v.u32 = le32toh(ret->v.u32);
810 } else if (!strcmp(field, "qid") && mtype == Rattach) {
812 memcpy(&ret->v.qid, &a->v.msg.msg[7], QIDSIZE);
814 } else if (!strcmp(field, "nwqid") && mtype == Rwalk) {
816 memcpy(&ret->v.u16, &a->v.msg.msg[7], 2);
817 ret->v.u16 = le16toh(ret->v.u16);
819 } else if (!strcmp(field, "wqid") && mtype == Rwalk) {
820 ret->type = V_QIDVEC;
821 ret->v.qidvec.start = &a->v.msg.msg[9];
822 memcpy(&len, &a->v.msg.msg[7], 2);
824 ret->v.qidvec.len = len;
830 len = strtonum(field, 0, MAXWELEM, &errstr);
831 if (errstr != NULL) {
833 printf("can't access qid #%s: %s\n", field, errstr);
837 if (len >= a->v.qidvec.len) {
839 printf("can't access qid #%d: out-of-bound "
840 "(max %zu)\n", len, a->v.qidvec.len);
845 memcpy(&ret->v.qid, a->v.qidvec.start + len * QIDSIZE,
855 printf("can't access field `%s' on type %s (", field, val_type(a));
873 printf("%s = ", op->v.assign.name);
874 pp_op(op->v.assign.expr);
881 printf("funcall %s(", op->v.funcall.proc->name);
882 for (aux = op->v.funcall.argv; aux != NULL; aux = aux->next) {
884 if (aux->next != NULL)
890 pp_val(&op->v.literal);
893 printf("%s", op->v.var);
896 pp_op(op->v.cast.expr);
898 switch (op->v.cast.totype) {
899 case V_U8: printf("u8"); break;
900 case V_U16: printf("u16"); break;
901 case V_U32: printf("u32"); break;
902 case V_STR: printf("str"); break;
903 default: printf("???"); break;
907 pp_op(op->v.bin_cmp.a);
909 pp_op(op->v.bin_cmp.b);
912 pp_op(op->v.bin_cmp.a);
914 pp_op(op->v.bin_cmp.b);
917 pp_op(op->v.faccess.expr);
918 printf(".%s", op->v.faccess.field);
921 printf("should-fail ");
922 pp_op(op->v.sfail.expr);
923 if (op->v.sfail.msg != NULL)
924 printf(": \"%s\"", op->v.sfail.msg);
930 printf(" ???[%d] ", op->type);
935 pp_block(struct op *op)
951 struct op *t, *tnext;
962 * Try to load the rest argument. Note that it can be
965 if ((ret = getvar_raw("...", &t)) == EVAL_OK)
966 if ((ret = eval(t)) != EVAL_OK)
971 ret = setvar(op->v.assign.name, op->v.assign.expr);
977 if ((ret = eval(op->v.assert)) != EVAL_OK)
980 if (!val_trueish(&a)) {
982 printf("assertion failed: ");
990 /* assume airity matches */
992 proc = op->v.funcall.proc;
993 if (proc->nativefn != NULL) {
995 * Push arguments on the stack for builtin
996 * functions. Counting the height of the
997 * stack is done to compute the correct number
998 * in the vararg case. argc only counts the
999 * "syntactical" arguments, i.e. foo(x, ...)
1000 * has argc == 2, but at runtime argc may be
1001 * 1, 2 or a greater number!
1005 t = op->v.funcall.argv;
1006 if (t != NULL && (ret = eval(t)) != EVAL_OK)
1012 if ((ret = proc->nativefn(i))
1016 if (proc->body == NULL) {
1018 printf("warn: calling the empty proc `%s'\n",
1025 for (t = op->v.funcall.argv, i = 0;
1029 * Push a pseudo variable `...' (and
1030 * don't evaluate it) in the vararg
1031 * case. A special case is when the
1032 * variable is itself `...'.
1034 if (proc->vararg && i == proc->minargs) {
1035 if (t->type != OP_REST)
1036 setvar_raw(xstrdup("..."), t);
1041 * The arguments are a linked list of
1042 * ops. Setvar will call eval that
1043 * will evaluate *all* the arguments.
1044 * The dance here that sets next to
1045 * NULL and then restores it is to
1046 * avoid this behaviour.
1050 ret = setvar(proc->args[i], t);
1057 if ((ret = eval(proc->body)) != EVAL_OK)
1066 pushv(&op->v.literal);
1070 if ((ret = getvar(op->v.var, &a)) != EVAL_OK)
1076 if ((ret = eval(op->v.cast.expr)) != EVAL_OK)
1079 if ((ret = val_cast(&a, op->v.cast.totype)) != EVAL_OK)
1085 if ((ret = eval(op->v.bin_cmp.a)) != EVAL_OK)
1087 if ((ret = eval(op->v.bin_cmp.b)) != EVAL_OK)
1092 pushbool(val_eq(&a, &b));
1096 if ((ret = eval(op->v.bin_cmp.a)) != EVAL_OK)
1098 if ((ret = eval(op->v.bin_cmp.b)) != EVAL_OK)
1103 pushbool(val_leq(&a, &b));
1107 if ((ret = eval(op->v.faccess.expr)) != EVAL_OK)
1110 if ((ret = val_faccess(&a, op->v.faccess.field, &b))
1117 if ((ret = eval(op->v.sfail.expr)) == EVAL_OK) {
1119 printf("expecting failure");
1120 if (op->v.sfail.msg != NULL)
1121 printf(" \"%s\"", op->v.sfail.msg);
1123 printf("expression: ");
1124 pp_op(op->v.sfail.expr);
1128 if (ret == EVAL_SKIP)
1133 if ((ret = getvar_raw("...", &t)) == EVAL_OK) {
1134 for (i = 0; t != NULL; t = t->next)
1143 fprintf(stderr, "invalid op, aborting.\n");
1148 return eval(op->next);
1153 prepare_funcall(void)
1159 push_arg(struct op *op)
1165 op_funcall(struct proc *proc)
1167 struct op *op, *argv;
1170 argv = finalize(&args, &argc);
1172 op = newop(OP_FUNCALL);
1173 op->v.funcall.proc = proc;
1174 op->v.funcall.argv = argv;
1175 op->v.funcall.argc = argc;
1181 add_builtin_proc(const char *name, int (*fn)(int), int argc, int vararg)
1185 proc = xcalloc(1, sizeof(*proc));
1186 proc->name = xstrdup(name);
1187 proc->nativefn = fn;
1188 proc->minargs = argc;
1189 proc->vararg = vararg;
1191 TAILQ_INSERT_HEAD(&procs, proc, entry);
1201 proc_setup_body(void)
1203 struct opstack *argv;
1208 for (i = 0, op = argv->base.next; op != NULL; i++) {
1210 * TODO: should free the whole list on error but..,
1211 * we're gonna exit real soon(tm)!
1213 if (op->type != OP_VAR && op->type != OP_REST)
1219 assert(i == argv->counter);
1225 proc_done(char *name)
1228 struct op *op, *next, *argv, *body;
1231 argv = finalize(&args, &argc);
1232 body = finalize(&blocks, NULL);
1234 proc = xcalloc(1, sizeof(*proc));
1236 proc->minargs = argc;
1238 for (i = 0, op = argv; op != NULL; ++i) {
1239 if (op->type == OP_REST) {
1245 proc->args[i] = xstrdup(op->v.var);
1251 assert(i == argc || (proc->vararg && i == proc->minargs));
1255 TAILQ_INSERT_HEAD(&procs, proc, entry);
1259 block_push(struct op *op)
1265 proc_by_name(const char *name)
1269 TAILQ_FOREACH(p, &procs, entry) {
1270 if (!strcmp(p->name, name))
1284 test_done(int shouldfail, char *name)
1288 test = xcalloc(1, sizeof(*test));
1289 test->shouldfail = shouldfail;
1291 test->body = finalize(&blocks, NULL);
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)
1591 struct kd_auth_proc rauth;
1593 if ((pw = getpwuid(uid)) == NULL)
1594 fatal("getpwuid(%d)", uid);
1596 memset(&rauth, 0, sizeof(rauth));
1597 strlcpy(rauth.uname, pw->pw_name, sizeof(rauth.uname));
1598 strlcpy(rauth.dir, dir, sizeof(rauth.dir));
1600 imsg_compose(&ibuf, IMSG_AUTH, 0, 0, -1,
1601 &rauth, sizeof(rauth));
1603 if (imsg_flush(&ibuf) == -1)
1604 fatal("imsg_flush");
1608 run_test(struct test *t)
1615 puts("=====================");
1617 puts("=====================");
1623 if (t->body == NULL) {
1625 printf("no instructions, skipping...\n");
1629 pid = spawn_client_proc();
1630 prepare_child_for_test(t);
1631 ret = eval(t->body);
1633 imsg_compose(&ibuf, IMSG_CONN_GONE, 0, 0, -1, NULL, 0);
1636 while (waitpid(pid, NULL, 0) != pid)
1641 if (t->shouldfail) {
1642 if (ret == EVAL_OK) {
1644 printf("test was expected to fail\n");
1646 } else if (ret == EVAL_ERR)
1654 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:r:vx:")) != -1) {
1686 assert(*optarg == 'c');
1699 fprintf(stderr, "Usage: %s [-nv] [files...]\n",
1711 fatal("missing root test dir");
1713 if (stat(dir, &sb) == -1)
1714 fatal("stat(\"%s\")", dir);
1720 if (regcomp(®, pat, REG_ICASE | REG_NOSUB) != 0)
1721 fatalx("invalid regexp: %s", pat);
1723 for (i = 0; i < argc; ++i)
1727 fprintf(stderr, "files OK\n");
1731 /* Check for root privileges. */
1733 fatalx("need root privileges");
1736 TAILQ_FOREACH(t, &tests, entry) {
1737 if (regexec(®, t->name, 0, NULL, 0) != 0)
1740 printf("===> [%d/%d] running test \"%s\"... ", i+1, ntests,
1759 printf("skipped\n");
1770 printf("%d/%d passed (%d skipped and %d failed)\n",
1771 passed, i, skipped, failed);