nuclear@6: #include nuclear@6: #include nuclear@6: #include nuclear@6: #include nuclear@6: #include "rbtree.h" nuclear@6: #include "sym.h" nuclear@6: nuclear@6: #define SEP " \t\r\n,\"\'" nuclear@6: nuclear@6: /* commands */ nuclear@6: enum { nuclear@6: CMD_CREATE, nuclear@6: CMD_DESTROY, nuclear@6: CMD_CLEAR, nuclear@6: CMD_COPY, nuclear@6: CMD_SIZE, nuclear@6: CMD_INSERT, nuclear@6: CMD_DELETE, nuclear@6: CMD_FIND, nuclear@6: CMD_PRINT, nuclear@6: nuclear@6: MAX_CMD nuclear@6: }; nuclear@6: nuclear@6: int runcmd(int cmd, int argc, char **argv); nuclear@6: nuclear@6: #define NUM_TREES 8 nuclear@6: struct rbtree *trees[NUM_TREES]; nuclear@6: nuclear@6: nuclear@6: int main(void) nuclear@6: { nuclear@6: char buf[512]; nuclear@6: nuclear@6: /* init symbol table */ nuclear@6: if(init_sym() == -1) { nuclear@6: return 1; nuclear@6: } nuclear@6: add_sym("create", CMD_CREATE, 0); nuclear@6: add_sym("destroy", CMD_DESTROY, 1); nuclear@6: add_sym("clear", CMD_CLEAR, 1); nuclear@6: add_sym("copy", CMD_COPY, 2); nuclear@6: add_sym("size", CMD_SIZE, 1); nuclear@6: add_sym("insert", CMD_INSERT, 2); nuclear@6: add_sym("delete", CMD_DELETE, 2); nuclear@6: add_sym("find", CMD_FIND, 2); nuclear@6: add_sym("print", CMD_PRINT, 1); nuclear@6: nuclear@6: for(;;) { nuclear@6: int argc = 0; nuclear@6: char *argv[8]; nuclear@6: int cmd_id, cmd_arity; nuclear@6: nuclear@6: printf("rbshell> "); nuclear@6: fflush(stdout); nuclear@6: nuclear@6: if(!fgets(buf, sizeof buf, stdin)) { nuclear@6: putchar('\n'); nuclear@6: break; nuclear@6: } nuclear@6: nuclear@6: if(!(argv[argc] = strtok(buf, SEP)) || argv[argc][0] == '#') { nuclear@6: return 0; /* empty / comment */ nuclear@6: } nuclear@6: argc++; nuclear@6: nuclear@6: if((cmd_id = sym_lookup(argv[0])) == -1) { nuclear@6: continue; nuclear@6: } nuclear@6: cmd_arity = sym_arity(argv[0]); nuclear@6: nuclear@6: while((argv[argc] = strtok(0, SEP))) { nuclear@6: argc++; nuclear@6: if(argc >= sizeof argv / sizeof *argv) { nuclear@6: break; nuclear@6: } nuclear@6: } nuclear@6: nuclear@6: if(argc - 1 != cmd_arity) { nuclear@6: fprintf(stderr, "too many (%d) arguments for %s, expected %d\n", argc - 1, argv[0], cmd_arity); nuclear@6: continue; nuclear@6: } nuclear@6: nuclear@6: runcmd(cmd_id, argc, argv); nuclear@6: } nuclear@6: nuclear@6: destroy_sym(); nuclear@6: return 0; nuclear@6: } nuclear@6: nuclear@6: #define CHECKINT(i) \ nuclear@6: if(!isdigit(argv[i][0])) { \ nuclear@6: fprintf(stderr, "expected number, got: %s\n", argv[i]); \ nuclear@6: return -1; \ nuclear@6: } nuclear@6: nuclear@6: #define CHECKVALID(idx) \ nuclear@6: if((idx) < 0 || (idx) >= NUM_TREES || !trees[idx]) { \ nuclear@6: fprintf(stderr, "invalid tree specified: %d\n", idx); \ nuclear@6: return -1; \ nuclear@6: } nuclear@6: nuclear@6: nuclear@6: int runcmd(int cmd, int argc, char **argv) nuclear@6: { nuclear@6: int i, idx, idx2, n; nuclear@6: struct rbnode *node; nuclear@6: nuclear@6: switch(cmd) { nuclear@6: case CMD_CREATE: nuclear@6: for(i=0; i %d\n", idx, idx2); nuclear@6: break; nuclear@6: nuclear@6: case CMD_SIZE: nuclear@6: CHECKINT(1); nuclear@6: idx = atoi(argv[1]); nuclear@6: CHECKVALID(idx); nuclear@6: nuclear@6: printf("tree size: %d\n", rb_size(trees[idx])); nuclear@6: break; nuclear@6: nuclear@6: case CMD_INSERT: nuclear@6: CHECKINT(1); nuclear@6: CHECKINT(2); nuclear@6: idx = atoi(argv[1]); nuclear@6: CHECKVALID(idx); nuclear@6: nuclear@6: n = atoi(argv[2]); nuclear@6: rb_inserti(trees[idx], n, 0); nuclear@6: printf("inserted: %d\n", n); nuclear@6: break; nuclear@6: nuclear@6: case CMD_DELETE: nuclear@6: case CMD_FIND: nuclear@6: CHECKINT(1); nuclear@6: CHECKINT(2); nuclear@6: idx = atoi(argv[1]); nuclear@6: CHECKVALID(idx); nuclear@6: n = atoi(argv[2]); nuclear@6: nuclear@6: if(!rb_findi(trees[idx], n)) { nuclear@6: fprintf(stderr, "%d not found\n", n); nuclear@6: } else { nuclear@6: if(cmd == CMD_DELETE) { nuclear@6: rb_deletei(trees[idx], n); nuclear@6: printf("deleted %d\n", n); nuclear@6: } else { nuclear@6: printf("found %d\n", n); nuclear@6: } nuclear@6: } nuclear@6: break; nuclear@6: nuclear@6: case CMD_PRINT: nuclear@6: CHECKINT(1); nuclear@6: idx = atoi(argv[1]); nuclear@6: CHECKVALID(idx); nuclear@6: nuclear@6: rb_begin(trees[idx]); nuclear@6: while((node = rb_next(trees[idx]))) { nuclear@6: printf("%d ", rb_node_keyi(node)); nuclear@6: } nuclear@6: putchar('\n'); nuclear@6: break; nuclear@6: nuclear@6: default: nuclear@6: /* can't happen */ nuclear@6: break; nuclear@6: } nuclear@6: return 0; nuclear@6: }