//preprocess cmds by constroy void *handleComplexCmdStr(int begin,int end) { int i,j,cnt; ComplexCmd *cmd = (ComplexCmd*)malloc(sizeof(ComplexCmd)); cnt = 0; for (i = begin; i<end; ++i) { cnt += (inputBuff[i] == '|'); } cmd->num = ++cnt; cmd->cmds = (SimpleCmd**)malloc(cnt*sizeof(SimpleCmd*)); cnt = 0; for (i = begin, j=begin; i<end && j<end; ++j) { if (inputBuff[j] == '|') { cmd->cmds[cnt++] = handleSimpleCmdStr(i,j); i = j + 1; } } cmd->cmds[cnt++] = handleSimpleCmdStr(i,j); cmd->isBack = cmd->cmds[cnt-1]->isBack; for (i = 0; i<cmd->num; ++i) { if(strcmp(cmd->cmds[i]->args[0], "exit") == 0) exit(0);//exit cmd->cmds[i]->isBack = cmd->isBack; } return 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 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 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; // } }
/******************************************************* 命令解析 ********************************************************/ SimpleCmd* handleSimpleCmdStr(int begin, int end){ int i, j, k; int fileFinished; //记录命令是否解析完毕 int pipeFinished; char c, buff[10][40], inputFile[30], outputFile[30], *temp = NULL; SimpleCmd *cmd = (SimpleCmd*)malloc(sizeof(SimpleCmd)); //默认为非后台命令,输入输出重定向为null,管道指令为null cmd->isBack = 0; cmd->input = cmd->output = NULL; cmd->nextCmd=NULL; //初始化相应变量 for(i = begin; i<10; i++){ buff[i][0] = '\0'; } inputFile[0] = '\0'; outputFile[0] = '\0'; i = begin; //跳过空格等无用信息 while(i < end && (inputBuff[i] == ' ' || inputBuff[i] == '\t')){ i++; } k = 0; j = 0; fileFinished = 0; pipeFinished = 0; temp = buff[k]; //以下通过temp指针的移动实现对buff[i]的顺次赋值过程 while(i < end&&!pipeFinished){ /*根据命令字符的不同情况进行不同的处理*/ switch(inputBuff[i]){ case ' ': case '\t': //命令名及参数的结束标志 temp[j] = '\0'; j = 0; if(!fileFinished){ k++; temp = buff[k]; } break; case '<': //输入重定向标志 if(j != 0){ //此判断为防止命令直接挨着<符号导致判断为同一个参数,如果ls<sth temp[j] = '\0'; j = 0; if(!fileFinished){ k++; temp = buff[k]; } } temp = inputFile; fileFinished = 1; i++; break; case '>': //输出重定向标志 if(j != 0){ temp[j] = '\0'; j = 0; if(!fileFinished){ k++; temp = buff[k]; } } temp = outputFile; fileFinished = 1; i++; break; case '&': //后台运行标志 if(j != 0){ temp[j] = '\0'; j = 0; if(!fileFinished){ k++; temp = buff[k]; } } cmd->isBack = 1; fileFinished = 1; i++; break; case '|'://管道指令的标志 if(j!=0){ // printf("when '|',temp[j-1]=%c\n",temp[j-1]); // printf("inputFile[0]=%c\n",inputFile[0]); temp[j]='\0'; j=0; if(!fileFinished){ k++; temp=buff[k]; } } i++; // printf("-------------------pipe %d\n",i); cmd->nextCmd=handleSimpleCmdStr(i, end); // printf("%s->%s\n",buff[0],cmd->nextCmd->args[0]); fileFinished=1; pipeFinished=1; break; default: //默认则读入到temp指定的空间 // printf("inputBuff[i]=%c\n",inputBuff[i]); temp[j++] = inputBuff[i++]; continue; } //跳过空格等无用信息 while(i < end && (inputBuff[i] == ' ' || inputBuff[i] == '\t')){ i++; } } // printf("inputFile: %s\n",inputFile); if(inputBuff[end-1] != ' ' && inputBuff[end-1] != '\t' && inputBuff[end-1] != '&'&& !pipeFinished){ // printf("inpubBuff[end-1]=%c\n",inputBuff[end-1]); temp[j] = '\0'; if(!fileFinished){ k++; } } // printf("inputFile: %s\n",inputFile); //依次为命令名及其各个参数赋值 cmd->args = (char**)malloc(sizeof(char*) * (k + 1)); cmd->args[k] = NULL; for(i = 0; i<k; i++){ j = strlen(buff[i]); cmd->args[i] = (char*)malloc(sizeof(char) * (j + 1)); strcpy(cmd->args[i], buff[i]); } // printf("inputFile: %s\n",inputFile); //如果有输入重定向文件,则为命令的输入重定向变量赋值 if(strlen(inputFile) != 0){ j = strlen(inputFile); cmd->input = (char*)malloc(sizeof(char) * (j + 1)); strcpy(cmd->input, inputFile); } //如果有输出重定向文件,则为命令的输出重定向变量赋值 if(strlen(outputFile) != 0){ j = strlen(outputFile); cmd->output = (char*)malloc(sizeof(char) * (j + 1)); strcpy(cmd->output, outputFile); } #ifdef DEBUG printf("****\n"); printf("inputbuff= %s\n",inputBuff); printf("isBack: %d\n",cmd->isBack); for(i = 0; cmd->args[i] != NULL; i++){ printf("args[%d]: %s\n",i,cmd->args[i]); } printf("input: %s\n",cmd->input); printf("output: %s\n",cmd->output); if(cmd->nextCmd==NULL){ printf("nextCmd=NULL\n"); } else{ printf("nextCmd exists\n"); } printf("****\n"); #endif return cmd; }
/******************************************************* 命令执行接口 ********************************************************/ void execute(){ SimpleCmd *cmd = handleSimpleCmdStr(0, strlen(inputBuff)); execSimpleCmd(cmd); }