void execMoreCmd(SimpleCmd *cmd, SimpleCmd *cmd2){ pid_t pid[2]; int pipe_fd[2]; if(pipe(pipe_fd) < 0){ printf("pipe error"); return; } if((pid[0] = fork()) < 0){ perror("fork failed"); return; } if(!pid[0]) // child process { close(pipe_fd[0]); dup2(pipe_fd[1], 1); close(pipe_fd[1]); execSimpleCmd(cmd); exit(0); } if(pid[0]) // father process { waitpid(pid[0], NULL, 0); // printf("first father %s\n", cmd->args[0]); if((pid[1] = fork()) < 0) { perror("fork failed"); return; } if(!pid[1]) // child process { close(pipe_fd[1]); dup2(pipe_fd[0], 0); close(pipe_fd[0]); if(cmd2->nextCmd != NULL) execMoreCmd(cmd2, cmd2->nextCmd); else execSimpleCmd(cmd2); exit(0); } // father process // printf("second father %s\n", cmd->args[0]); close(pipe_fd[0]); close(pipe_fd[1]); waitpid(pid[1], NULL, 0); } }
/******************************************************* 命令执行接口 ********************************************************/ void execute(){ SimpleCmd *cmd; #ifdef DEBUG SimpleCmd *pcmd; #endif if(checkwildcard()==1){ create_shell(); initBashCmd(); execBashCmd(); return ; } cmd = handleSimpleCmdStr(0, strlen(inputBuff)); if(cmd->nextCmd!=NULL){ #ifdef DEBUG pcmd=cmd; do{ printf("%s\n",pcmd->args[0]); pcmd=pcmd->nextCmd; }while(pcmd!=NULL); #endif if(execPipeCmd(cmd,cmd->nextCmd)){ printf("pipe error\n"); } free(cmd->nextCmd); } else{ execSimpleCmd(cmd); } }
/******************************************************* 命令执行接口 ********************************************************/ void execute(){ SimpleCmd *cmd = handleSimpleCmdStr(0, strlen(inputBuff)); if(cmd->ifpipeline==0) execSimpleCmd(cmd); while(cmd->ifpipeline==1) { SimpleCmd *cmd2 = handleSimpleCmdStr(0, strlen(inputBuff)); pipecmd(cmd,cmd2); free(cmd); cmd=cmd2; } }
void handleCmdStr(int begin, int end){ int i, j, k, ordernum = 0; SimpleCmd *cmd0 = NULL, *cmd1 = NULL, *cmd = NULL; for(i = j = begin; i < end; i ++) { if(inputBuff[i] == '|' || i == end-1) { if(i == end-1) { cmd1 = handleSimpleCmdStr(j, i+1); } else { cmd1 = handleSimpleCmdStr(j, i); } if(cmd == NULL) cmd = cmd0 = cmd1; else { cmd0->nextCmd = cmd1; cmd0 = cmd1; } j = i + 1; } } if(cmd->nextCmd != NULL) execMoreCmd(cmd, cmd->nextCmd); else execSimpleCmd(cmd); // execSimpleCmd(cmd); // while(cmd != NULL) // { // printf("%s exec %s ==> %s\n", cmd->args[0], cmd->input, cmd->output); // execSimpleCmd(cmd); // cmd = cmd->nextCmd; // } }
//execute a set of CMDs by constroy void executeComplexCmd(ComplexCmd *cmd) { int i; pid_t pid; int pfd[2][2]={{0,1},{0,1}}; cidCnt = 0; for (i = 0; i<cmd->num; ++i) { if (i == cmd->num - 1) { pfd[1][1] = 1; } else { if (pipe(pfd[1]) == -1) { perror("pipe failed"); exit(errno); } } pid = fork(); if (pid < 0) { perror("fork failed"); exit(errno); } else if (pid) { fgCid[cidCnt++] = pid; if (pfd[0][0] != 0) close(pfd[0][0]); if (pfd[1][1] != 1) close(pfd[1][1]); pfd[0][0]=pfd[1][0]; } else { dup2(pfd[0][0],0); dup2(pfd[1][1],1); execSimpleCmd(cmd->cmds[i]); close(pfd[0][0]); close(pfd[1][1]); exit(0); } } if (pfd[0][0] != 0) close(pfd[0][0]); //wait for all child processes to exit while ((pid=wait(NULL))>0); //free cmds[] for (i = 0; i<cmd->num; ++i) free(cmd->cmds[i]); }
//执行管道指令 int execPipeCmd(SimpleCmd *cmd1,SimpleCmd *cmd2){ int status; int pid[2]; int pipe_fd[2]; //创建管道 if(pipe(pipe_fd)<0){ perror("pipe failed"); return -1; } //为cmd1创建进程 if((pid[0]=fork())<0){ perror("fork failed"); return -1; } if(!pid[0]){ /*子进程*/ /*将管道的写描述符复制给标准输出,然后关闭*/ close(pipe_fd[0]); dup2(pipe_fd[1],1); close(pipe_fd[1]); /*执行cmd1*/ execSimpleCmd(cmd1); exit(0); } if(pid[0]){ /*父进程*/ waitpid(pid[0],&status,0); /*为cmd2创建子进程*/ if((pid[1]=fork())<0){ perror("fork failed"); return -1; } if(!pid[1]){ /*子进程*/ /*将管道的读描述符复制给标准输入*/ close(pipe_fd[1]); dup2(pipe_fd[0],0); close(pipe_fd[0]); /*执行cmd2*/ // printf("execute cmd2\n"); if(cmd2->nextCmd!=NULL){ // printf("pipe2 ok\n"); if(execPipeCmd(cmd2,cmd2->nextCmd)){ printf("pipe error\n"); } free(cmd2->nextCmd); } else{ execSimpleCmd(cmd2); } exit(0); } close(pipe_fd[0]); close(pipe_fd[1]); waitpid(pid[1],&status,0); } return 0; }
/******************************************************* 命令执行接口 ********************************************************/ void execute(){ SimpleCmd *cmd = handleSimpleCmdStr(0, strlen(inputBuff)); execSimpleCmd(cmd); }