static VOID recursive_pipe(command_t* root, HANDLE in, HANDLE out) { BOOL bRes; HANDLE hRead, hWrite; SECURITY_ATTRIBUTES sa; ZeroMemory(&sa, sizeof(sa)); sa.nLength = sizeof(SECURITY_ATTRIBUTES); sa.bInheritHandle = TRUE; if (root->op == OP_PIPE) { bRes = CreatePipe(&hRead, &hWrite,&sa, 0); DIE(!bRes, "PIPE CRE"); recursive_pipe(root->cmd1, in, hWrite); CloseHandle(hWrite); recursive_pipe(root->cmd2, hRead, out); CloseHandle(hRead); root->aux = INVALID_HANDLE_VALUE; //TODO: return code of cmd1 or cmd2 } else if (root->op == OP_NONE) { root->aux = run_simple_command_nowait(root->scmd, in, out); } }
void exec_pipe(char ***env, t_tree *tree, t_fd *fd) { pid_t father; int tube[2]; while (tree->right && tree->right->type == 3) tree = tree->right; pipe(tube); father = fork(); if (father) { close(tube[1]); dup2(tube[0], 0); wait(NULL); } else { close(tube[0]); dup2(tube[1], 1); recursive_pipe(env, tree, fd); exit(2); } if (tree->right->type == 4) ft_operator_four(env, tree->right); else do_the_cmd(env, ft_strsplit(tree->right->name, ' '), fd); exit(2); }
static void recursive_pipe(char ***env, t_tree *tree, t_fd *fd) { pid_t father; int tube[2]; pipe(tube); father = fork(); if (father) { close(tube[1]); dup2(tube[0], 0); wait(NULL); } else { close(tube[0]); dup2(tube[1], 1); if (tree->father && tree->father->type == 3) recursive_pipe(env, tree->father, fd); exit(2); } if (tree->left->type == 4) ft_operator_four(env, tree->left); else do_the_cmd(env, ft_strsplit(tree->left->name, ' '), fd); }
////=========================================================================================== // executer les commandes en pipes void runPipe() { if (numcmd_pipe>0) { int pid=fork(); if (pid < 0) { printf("Error: Fork failed.n"); return 1; } if (pid == 0) { // child process recursive_pipe(0); } else { //parent (Shell) waitpid(pid, NULL, 0); } } }
////=========================================================================================== // executer les pipes recursivement void recursive_pipe(int i) { char line[55]; // if(!cmds_pipe[i]) strcpy(line,cmds_pipe[i]); char* elem[MAXELEMS]; decoupe_cmd(line,elem); printf("\n %d Execution de %s \n",i,line); fflush(stdout); int fd[2]; int pid; int Pipe; Pipe=pipe(fd) ; if (Pipe== -1) { printf("Error: Pipe failed.n"); return 1; } if (i!=(numcmd_pipe-1)) { pid=fork(); if (pid < 0) { printf("Error: Fork failed.n"); return 1; } if (pid == 0) { // child process close(fd[1]); dup2(fd[0], STDIN_FILENO); close(fd[0]); recursive_pipe(i+1); printf("Error: execvp failed.n"); return 1; } else { // parent process close(fd[0]); dup2(fd[1], 1); close(fd[1]); int erreur = execvp( elem[0], elem); printf("Error: execvp failed : (%s)\n",strerror(erreur)); } } else { close(fd[0]); execvp( elem[0], elem); printf("Error: execvp failed.n"); } }
static int run_command(command_t* root, BOOL wait) { int ret = 0; DWORD dwRes; switch(root->op) { case OP_NONE: return run_simple_command(root->scmd, wait); case OP_PIPE: recursive_pipe(root, INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE); dwRes = recursive_wait(root, wait); return dwRes; case OP_SEQUENTIAL: run_command(root->cmd1, TRUE); return run_command(root->cmd2, wait); case OP_CONDITIONAL_ZERO: ret = run_command(root->cmd1, TRUE); if (ret) return ret; return run_command(root->cmd2, wait); case OP_CONDITIONAL_NZERO: ret = run_command(root->cmd1, TRUE); if (!ret) return ret; return run_command(root->cmd2, wait); case OP_PARALLEL: //TODO: functie care face un thread in care ruleaza run_command(cmd1) run_command_in_parallel(root->cmd1); return run_command(root->cmd2, wait); default: printf("RUN COMMAND case not treated.\n"); return 0; } }