/** * Travers cmd descr tree using tokens. */ static void cmd_traverse( cmd_traverse_state_t state ) { cmd_descriptor_t* i = state->root; if (state->token == NULL) { return; } else if (state->root == NULL) { return; } while (*i != NULL) { if (strcmp((*i)->name, state->token->name) == 0) { state->root = (*i)->sub; state->cmd = *i; state->token = state->token->next; cmd_traverse(state); return; } i++; } state->root = NULL; }
/*ARGSUSED*/ static void * perprobe(tnfctl_handle_t *hndl, tnfctl_probe_t *probe_p) { process_args_t args; tnfctl_errcode_t err; args.probe_p = probe_p; args.hndl = hndl; err = cmd_traverse(percmd, &args); if (err) { (void) fprintf(stderr, gettext( "%s: error on new (dlopened) probe : %s\n"), g_argv[0], tnfctl_strerror(err)); } return (NULL); }
static void cmd_execute_slash(const char* s) { cmd_tokenized_node_t tokens = NULL; struct cmd_traverse_state state; tokens = state.token = cmd_tokenize(s, s + strlen(s)); state.root = cmds_root; state.cmd = NULL; cmd_traverse(&state); if (!state.cmd) { io_printfln("Unknown command: `%s'", state.token->name); goto ret; } if (state.cmd->handle) { state.cmd->handle(state.token); } else { io_printfln("Unknown command: %s", state.token->name); goto ret; } ret: cmd_tokenized_free(tokens); }
static void run() { cmd_t* cmd = cmd_create(".history", "~>"); cmd_register(cmd, "base conhash", test_base_conhash); cmd_register(cmd, "base bitset", test_base_bitset); cmd_register(cmd, "base heap", test_base_heap); cmd_register(cmd, "base rbtree", test_base_rbtree); cmd_register(cmd, "base rbuffer", test_base_rbuffer); cmd_register(cmd, "base slist", test_base_slist); cmd_register(cmd, "base skiplist", test_base_skiplist); cmd_register(cmd, "base skiplist find", test_base_skiplist_find); cmd_register(cmd, "base skiplist duplicate", test_base_skiplist_duplicate); cmd_register(cmd, "base timer", test_base_timer); cmd_register(cmd, "core atomic", test_core_atomic); #ifdef OS_LINUX // seems some memory error ... // cmd_register(cmd, "core coroutine", test_core_coroutine); #endif cmd_register(cmd, "core fsm", test_core_fsm); cmd_register(cmd, "core lock", test_core_lock); cmd_register(cmd, "core spin", test_core_spin); cmd_register(cmd, "core thread", test_core_thread); cmd_register(cmd, "logic dirty", test_logic_dirty); cmd_register(cmd, "logic task", test_logic_task); cmd_register(cmd, "mm slab", test_mm_slab); cmd_register(cmd, "mm shm", test_mm_shm); cmd_register(cmd, "net curl", test_net_curl); cmd_register(cmd, "net echo", test_net_echo); cmd_register(cmd, "util base64", test_util_base64); cmd_register(cmd, "util cjson text", test_util_cjson_text); cmd_register(cmd, "util cjson file", test_util_cjson_file); cmd_register(cmd, "util cjson create", test_util_cjson_create); cmd_register(cmd, "util dh", test_util_dh); cmd_register(cmd, "util dh perf", test_util_dh_perf); cmd_register(cmd, "util random", test_util_random); cmd_register(cmd, "util shuffle", test_util_shuffle); cmd_register(cmd, "util unicode", test_util_unicode); cmd_register(cmd, "util wscode", test_util_wscode); // unit test mode if (mode_all) { int res = cmd_traverse(cmd, NULL, traverse_callback); if (res < 0) { fprintf(stderr, "\033[%dm--> [RUN ALL TEST CASES FAILURE] <--\033[0m\n\n", COLOR_RED); cmd_release(cmd); exit(res); } } // interact mode if (mode_interact) { while (1) { char* line = cmd_readline(cmd); if (!line) { break; } int ret = cmd_handle(cmd, line); traverse_callback(line, ret); free(line); if (cmd_eof(cmd)) { if (cmd_closed(cmd)) { break; } } } } cmd_release(cmd); }