/************************************************** ** Function: executeCommand ** Description: This method checks to see what command the user entered and runs it. ** Based on the foreground/background process specified it manages the process. ** Parameters: Command *cmd, commandt the user wants to execute. ** int prevCmdStatus, status returned by the previous command ** int *backStatus, pointer to an integer to store the background process return value. ** int *foreStatus, pointer to an integer to store the foreground process return value. ** Returns: None **************************************************/ int executeCommand(struct Command *cmd, int prevCmdStatus, int* backStatus, int* foreStatus) { if(strcmp("exit", cmd->cmd) == 0) { // Built in command exit. exitCommand(); } else if(strcmp("cd", cmd->cmd) == 0) { // Built in command cd. cdCommand(cmd->args[0]); // First arguement will be path. } else if(strcmp("status", cmd->cmd) == 0) { // Built in command status. statusCommand(prevCmdStatus); // Pass status pointer to status command method. } else if(cmd->backgroundProcess == 1) { // Background process. *backStatus = createBackProcess(cmd); return *backStatus; // Return how the process executed. } else if(cmd->backgroundProcess == 0){ // Foreground process. *foreStatus = createForeProcess(cmd); // -1 means error // printf("for: %d", *foreStatus); // fflush(stdout); // if (foreStatus == ENOENT) { // // } return *foreStatus; // Return how the process executed. } return 1; }
int main(int argc, char** argv){ //Setting the signal interrupt to its default function. signal(SIGINT, handler); //get the pid of the current process pid = getpid(); //Allocating space to store the previous commands. int numCmds = 0; char **cmds = (char **)malloc(1000 * sizeof(char *)); int printDollar = 1; char input[MAXLINE]; char** tokens; int notEOF = 1; int i; FILE* stream = stdin; while(notEOF) { if (printDollar == 1){ printf("$ "); // the prompt fflush(stdin); } char *in = fgets(input, MAXLINE, stream); //taking input one line at a time //Checking for EOF if (in == NULL){ if (DEBUG) printf("EOF found\n"); exit(0); } //add the command to the command list. cmds[numCmds] = (char *)malloc(sizeof(input)); strcpy(cmds[numCmds++], input); // Calling the tokenizer function on the input line tokens = tokenize(input); // check tokens and execute corresponding command if(tokens[0] == NULL){ printf(""); } else if( is_equal(tokens[0], "run\0") ) { runCommand(tokens[1]); } else if(is_equal(tokens[0], "cd\0")){ cdCommand(tokens[1]); } else if(is_equal(tokens[0], "cron\0")){ cronCommand(tokens[1]); } else if(is_equal(tokens[0], "parallel\0")){ parallelCommand(tokens); } else{ executeFile(tokens); } } printf("Print and deallocate %s\n", tokens[0]); // Freeing the allocated memory for(i=0;tokens[i]!=NULL;i++){ free(tokens[i]); } free(tokens); return 0; }
int main(int argc, char** argv){ //Setting the signal interrupt to its default function. signal(SIGINT, handler); signal(SIGCHLD, sigchldHandler); children = (pid_t *)malloc(1000*sizeof(pid_t)); children[0] = -1; //get the pid of the current process pid = getpid(); ready = 1; //Allocating space to store the previous commands. int numCmds = 0; char **cmds = (char **)malloc(1000 * sizeof(char *)); char input[MAXLINE]; char** tokens; char** command; int notEOF = 1; int i; //variables to store information about I/O redirection int inCounter=0 , outCounter=0, pipeCounter = 0, appendFlag=0; char * infilename; char * outfilename; FILE* stream = stdin; while(notEOF) { if (ready == 1){ printf("$ "); // the prompt fflush(stdin); } char *in = fgets(input, MAXLINE, stream); //taking input one line at a time //Checking for EOF if (in == NULL){ if (DEBUG) printf("EOF found\n"); exit(0); } //add the command to the command list. cmds[numCmds] = (char *)malloc(sizeof(input)); strcpy(cmds[numCmds++], input); // Calling the tokenizer function on the input line tokens = tokenize(input); command = (char **) malloc(MAXLINE*sizeof(char**)); //Check for input and output redirection int k=0; int flag = 1; int pipeFlag = 0; for(k=0;tokens[k]!=NULL;k++){ if( is_equal(tokens[k], "<\0")){ inCounter++; if(inCounter == 1){ infilename = tokens[k + 1]; } flag = 0; } if( is_equal(tokens[k], ">\0")){ outCounter++; if(outCounter == 1){ if(is_equal(tokens[k+1], ">\0")){ //if appending is done while output redirection appendFlag=1; outfilename=tokens[k + 2]; outCounter++; k++; } else outfilename = tokens[k + 1]; } flag = 0; } if(is_equal(tokens[k], "|\0")){ pipeFlag = 1; } if(flag == 1){ command[k] = tokens[k]; } } //if piping is used if(pipeFlag == 1){ pipeCommand(tokens); } else { if(inCounter > 1){ perror("More than one input redirections."); } else if(outCounter > 2 || (appendFlag == 0 && outCounter == 2)){ perror("More than one output redirections."); } else{ // check tokens and execute corresponding command if(tokens[0] == NULL){ printf(""); } else if( is_equal(tokens[0], "run\0") ) { runCommand(tokens[1]); } else if(is_equal(tokens[0], "cd\0")){ cdCommand(tokens[1]); } else if(is_equal(tokens[0], "cron\0")){ cronCommand(tokens[1]); } else if(is_equal(tokens[0], "parallel\0")){ parallelCommand(command); } else if(is_equal(tokens[0], "exit\0")){ exitCommand(); } else{ executeFile(command, inCounter, infilename, outCounter, outfilename, appendFlag); } } } inCounter = 0; outCounter = 0; appendFlag = 0; } printf("Print and deallocate %s\n", tokens[0]); // Freeing the allocated memory for(i=0;tokens[i]!=NULL;i++){ free(tokens[i]); } free(tokens); for(i=0;command[i]!=NULL;i++){ free(command[i]); } free(command); return 0; }
void shell_main(int argc, char *argv[]) { char *buf; char argumentList[100][256] = {0}; int argcount; int type; int i; buf = (char *)Malloc(sizeof(char) * 256); while(1) { memset(buf, 0, 256); for(i = 0; i < 100; i++) { memset(argumentList[i], 0, 256); } //先打印shell提示符, 提示符根据路径名的改变而改变 printShellPrompt(); //等待输入要执行的命令 //用input函数, input函数主要用于输入命令, 并且处理输入的命令 inputCommand(buf); argcount = explainCommand(buf, argumentList); char *argument[argcount + 1]; for(i = 0; i < argcount; i++) { argument[i] = (char *)argumentList[i]; } argument[argcount] = NULL; //判度输入的是不是退出命令 if(!strcmp(buf, "exit") || !strcmp(buf, "logout")) { break; } if(!strncmp(buf, "cd", 2)) { //进行cd命令的一系列处理 //argcount = explainCommand(buf, argumentList); cdCommand(argcount, argument); } else { type = judgeSymbolType(buf); if(type == HAVE_BACK) { backgroundSymbol(argument); } else if(type == HAVE_INPUT) { inputRedirectionSymbol(argument); } else if(type == HAVE_OUT) { outputRedirectionSymbol(argument); } else if(type == HAVE_PIPE) { pipeSymbol(argument, argcount); } else if(type == NO_SYMBOL) //如果没有以上这些符号,则当作普通命令执行 { noSymbol(argument); } } } //如果输入了exit或logout, 则释放内存, 并结束进程 if(buf) { free(buf); buf = NULL; } exit(0); }
/** * Start the Mish and runs it in a loop until CTRL + D is pressed and the * shell will exit. Collect the commands from the shells input and parses * them into a list of commands. The list is iterated through and the * commands are started with pipes and file redirections set between them. */ int main(void) { char input[MAXLINELEN]; char *internCommand; command comLine[MAXCOMMANDS]; command tempCom; int commandCount = 0; int i; int status; int fdRead[2]; int fdWrite[2]; if (signal(SIGINT, sig_handler) == SIG_ERR) fprintf(stderr, "\ncan't catch SIGINT\n"); do { i = 0; stopped =false; fprintf(stderr, "\e[1;34m\nmish%% \e[0m"); fflush(stderr); if (fgets(input, MAXLINELEN, stdin) == NULL) { running = 0; } commandCount = parse(input, comLine); if (commandCount > 0 && running == 1) { internCommand = intCommands(comLine); if (internCommand != NULL) { tempCom = comLine[i]; if (strcmp(internCommand, "cd") == 0) { cdCommand(&tempCom); } else if (strcmp(internCommand, "echo") == 0) { echoCommand(&tempCom); } } else { pipe(fdWrite); if(commandCount > 1 && !stopped) { // first fork in a chain of 2 or more pipe to stdout // from process makeFork(comLine[i], fdWrite, NULL); wait(&status); fdRead[0] = fdWrite[0]; fdRead[1] = fdWrite[1]; // Pipe stdin and stdout between processes for(i = 1 ; i < commandCount -1 ; i++) { if (!stopped) { pipe(fdWrite); makeFork(comLine[i], fdWrite, fdRead); close(fdRead[0]); close(fdRead[1]); wait(&status); fdRead[0] = fdWrite[0]; fdRead[1] = fdWrite[1]; } } // Fork and pipe to stdin of the last process if (!stopped) { makeFork(comLine[i], NULL, fdRead); close(fdRead[0]); close(fdRead[1]); wait(&status); } } else { // Run single command makeFork(comLine[i], NULL, NULL); wait(&status); } } } } while(running == 1); return 0; }