int parse_cmd(char* cmd, run_params* par, run_params* parent_par, int wait) { //printf("*** %s ***\n", cmd); int pos, i; int return_val = 0; char* cmd_itself = (char*)malloc(sizeof(char) * MAX_CMD_LEN); char* input = (char*)malloc(sizeof(char) * MAX_PATH_LEN); char* output = (char*)malloc(sizeof(char) * MAX_PATH_LEN); char* err_output = (char*)malloc(sizeof(char) * MAX_PATH_LEN); int out_append = 0; for (i = 0; i < MAX_PATH_LEN; i++) { input[i] = '\0'; output[i] = '\0'; err_output[i] = '\0'; } char** args = (char**)malloc(sizeof(char*) * MAX_ARGC); char* arg = (char*)malloc(sizeof(char) * MAX_PARAM_LEN); int argc = 0; pos = 0; int ret; ret = fill_string(cmd, &pos, cmd_itself); if (ret != 0) { return_val = 0; // empty command goto end; } while (1) { ret = fill_string(cmd, &pos, arg); if (ret != 0) break; ret = parse_redirects(cmd, &pos, arg, input, output, err_output, &out_append); if (ret == 0) { continue; } else if (ret == 1) { break; } else { if (argc >= MAX_ARGC) { return_val = 2; goto end; } args[argc] = arg; argc++; arg = (char*)malloc(sizeof(char) * MAX_PARAM_LEN); } } run_params* nParams = make_params(par, input, output, err_output, out_append, args, argc, cmd_itself); if (nParams == NULL) return 3; if (strcmp(cmd_itself, "exit") == 0) { if (par->secret_params == 2) { return 1; } if (par->secret_params == 1) { par->in->autoclose = 1; par->err->autoclose = 1; par->out->autoclose = 1; c_run((LPTHREAD_START_ROUTINE)(c_exit), nParams, wait); } } else if (strcmp(cmd_itself, "dir") == 0) { c_run((LPTHREAD_START_ROUTINE)(dir), nParams, wait); } else if (strcmp(cmd_itself, "echo") == 0) { c_run((LPTHREAD_START_ROUTINE)(echo), nParams, wait); } else if (strcmp(cmd_itself, "scan") == 0) { c_run((LPTHREAD_START_ROUTINE)(scan), nParams, wait); } else if (strcmp(cmd_itself, "rand") == 0) { c_run((LPTHREAD_START_ROUTINE)(random), nParams, wait); } else if (strcmp(cmd_itself, "cd") == 0) { cd(nParams, parent_par); } else if (strcmp(cmd_itself, "tree") == 0) { c_run((LPTHREAD_START_ROUTINE)(tree), nParams, wait); } else if (strcmp(cmd_itself, "pipe") == 0) { c_run((LPTHREAD_START_ROUTINE)(c_pipe), nParams, wait); } else if (strcmp(cmd_itself, "type") == 0) { c_run((LPTHREAD_START_ROUTINE)(type), nParams, wait); } else if (strcmp(cmd_itself, "info") == 0) { c_run((LPTHREAD_START_ROUTINE)(info), nParams, wait); } else if (strcmp(cmd_itself, "rm") == 0 || strcmp(cmd_itself, "rd") == 0) { c_run((LPTHREAD_START_ROUTINE)(rm), nParams, wait); } else if (strcmp(cmd_itself, "sort") == 0) { c_run((LPTHREAD_START_ROUTINE)(sort), nParams, wait); } else if (strcmp(cmd_itself, "mkdir") == 0 || strcmp(cmd_itself, "md") == 0) { c_run((LPTHREAD_START_ROUTINE)(mkdir), nParams, wait); } else if (strcmp(cmd_itself, "freq") == 0) { c_run((LPTHREAD_START_ROUTINE)(freq), nParams, wait); } else if (strcmp(cmd_itself, "wc") == 0) { c_run((LPTHREAD_START_ROUTINE)(wc), nParams, wait); } else if (strcmp(cmd_itself, "cmd") == 0) { nParams->secret_params = 2; c_run((LPTHREAD_START_ROUTINE)(c_cmd_run), nParams, wait); } else { c_run((LPTHREAD_START_ROUTINE)(c_non_existent), nParams, wait); } end: return return_val; }
int main() { srand((int)time(NULL)); node* root_dir = node_create("Computer", NULL, 1); node* root = node_create("C:", root_dir, 1); node* n1 = node_create("n1", root, 0); node* n2 = node_create("n2", root, 0); node* n3 = node_create("n3", root, 0); node* n4 = node_create("n4", root, 1); node* n5 = node_create("n5", root, 1); node* n5a = node_create("n5a", n5, 0); node* n5b = node_create("n5b", n5, 0); char* str = "n1 - aaaaaa\nbbbb\ncccc\nfskdhjs\ndsfj\niuyffj\n\n\nejrwehjr\n"; n1->content = (char*)malloc(sizeof(char) * strlen(str)+1); memcpy(n1->content, str, strlen(str)+1); n1->content_len = strlen(n1->content); str = ""; n2->content = (char*)malloc(sizeof(char) * strlen(str)+1); memcpy(n2->content, str, strlen(str)+1); n2->content_len = strlen(n2->content); str = "n3 - ,m,mkjhseur\nsjagfjhsdhj\n"; n3->content = (char*)malloc(sizeof(char) * strlen(str)+1); memcpy(n3->content, str, strlen(str)+1); n3->content_len = strlen(n3->content); str = "n4 - yighjgjhgfyufyt\n"; n4->content = (char*)malloc(sizeof(char) * strlen(str)+1); memcpy(n4->content, str, strlen(str) + 1); n4->content_len = strlen(n4->content); str = "n5a - 45455\n"; n5a->content = (char*)malloc(sizeof(char) * strlen(str)+1); memcpy(n5a->content, str, strlen(str) + 1); n5a->content_len = strlen(n5a->content); str = "n5b - 1010\n"; n5b->content = (char*)malloc(sizeof(char) * strlen(str)+1); memcpy(n5b->content, str, strlen(str) + 1); n5b->content_len = strlen(n5b->content); pipe_in* in_in = (pipe_in *)malloc(sizeof(pipe_in)); pipe_out* in_out = (pipe_out *)malloc(sizeof(pipe_out)); pipe_create(in_in, in_out, 0, 0, 1); pipe_in* out_in = (pipe_in *)malloc(sizeof(pipe_in)); pipe_out* out_out = (pipe_out *)malloc(sizeof(pipe_out)); pipe_create(out_in, out_out, 0, 0); pipe_in* err_in = (pipe_in *)malloc(sizeof(pipe_in)); pipe_out* err_out = (pipe_out *)malloc(sizeof(pipe_out)); pipe_create(err_in, err_out, 0, 0); HANDLE h_in = std_reader_run(in_in); HANDLE h_out = std_writter_run(out_out); HANDLE h_err = std_writter_run(err_out); run_params par; par.cmd_name = "cmd\0"; par.in = in_out; par.out = out_in; par.err = err_in; par.start_node = root; par.root_node = root; par.args = (char**)malloc(sizeof(char*) * 1); par.args[0] = "-main"; par.argc = 1; par.secret_params = 1; c_run( (LPTHREAD_START_ROUTINE) c_cmd_run, &par, 0); WaitForSingleObject(h_out, INFINITE); WaitForSingleObject(h_err, INFINITE); TerminateThread(h_in, 0); // needs to be terminated, otherwise would wait for input forever free(par.args); return 0; }
// - - -- - -- - - - - - -- --- -- - - - - - -- - - - - - - - - -- - - - - -- void _run(){ c_run(null); }