/************************************************** ** 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; }
void MainPanel::pressButtonSlot(int button) { switch(button) { case CommandButtons::BUTTON_VIEW: viewCommand(); break; case CommandButtons::BUTTON_EDIT: editCommand(); break; case CommandButtons::BUTTON_COPY: copyCommand(); break; case CommandButtons::BUTTON_MOVE: moveCommand(); break; case CommandButtons::BUTTON_MK_DIR: mkdirCommand(); break; case CommandButtons::BUTTON_DELETE: deleteCommand(); break; case CommandButtons::BUTTON_EXIT: exitCommand(); break; } }
/* internalCommandHandler * handles internal commands when given and passes it accordingly. * will return false if not an internal command */ static bool internalCommandHandler( struct commandType* command ) { // exit command if (strcmp((*command).args[0], "exit" ) == 0) { exitCommand(command); return true; } // history command if (strcmp((*command).args[0], "history" ) == 0) { historyCommand(command); return true; } // not an internal command return false; }
/************************************************************************************************ handleComands() Arguments: myUser - struct with 'my' information saAddr - IP address of the SA saPortChar - port of SA Description: The messages typed by the user in the terminal are handled in this function. The commands permitted are join, leave, find, connect, message, disconnect, exit, lst. *************************************************************************************************/ int handleComands(user myUser, char * saAddr, char * saPortChar){ char command[256]; char * parseCommand; int retval, saPort, i=0; saPort = atoi(saPortChar); /*retrieves comand and stores to string command*/ memset(command, '\0', 256); fgets(command, 256, stdin); for(i = 0; i < 256; i++){ if(command[i] == '\n'){ parseCommand = (char *) malloc((i+2)*sizeof(char)); break; } if(command[i] == ' '){ parseCommand = (char *) malloc((i+2)*sizeof(char)); break; } } if(parseCommand == NULL){ printf("\n>Malloc on ParseCommand not succecefull\n"); fflush(stdout); free(parseCommand); exit(EXIT_FAILURE); } memset(parseCommand, '\0', (i+2)); retval = sscanf(command, "%s", parseCommand); if(retval != 1){ printf("\n>Error parsing command, please retry\n"); free(parseCommand); return 1; } /*Switch Commands*/ /*Join*/ if(strcmp("join", parseCommand) == 0){ if(joinCommand(myUser,saAddr, saPort) < 0){ fprintf(stderr, "Join Error"); free(parseCommand); exit(EXIT_FAILURE); } printf("\n>You have started a session\n"); inSession = 1; } /*Leave*/ else if(strcmp(parseCommand, "leave") == 0){ if(leaveCommand(myUser, saAddr, saPort) < 0){ fprintf(stderr, "\n>Leave Error>"); free(parseCommand); return 0; } terminateLeave(); inSession = 0; } else if(strcmp(parseCommand, "find") == 0){ if(findCommand(command, saAddr, saPort) < 0){ fprintf(stderr, "\n>Leave Error\n"); free(parseCommand); return 0; } } else if(strcmp(parseCommand, "connect") == 0){ if(connectCommand(command, saAddr, saPort) <= 0){ fprintf(stderr, "\n>connect Error\n"); free(parseCommand); return 0; } } else if(strcmp(parseCommand, "message") == 0){ if(chatCommand(command) <= 0){ fprintf(stderr, "\n>message Error\n"); free(parseCommand); return 0; } } else if(strcmp(parseCommand, "disconnect") == 0){ if(disconnectCommand() <= 0){ fprintf(stderr, "\n>disconnect Error\n"); free(parseCommand); return 0; } } /*Exit*/ else if(strcmp(parseCommand, "exit") == 0){ if(exitCommand() <= 0){ fprintf(stderr, "\n>Exit Error>"); free(parseCommand); return 0; } } else if(strcmp(parseCommand, "lst") == 0){ if(lst() < 0){ fprintf(stderr, "\n>lst Error>"); free(parseCommand); return 0; } } free(parseCommand); return 1; }
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; }
int commonInterface::runCommand(string command) { char commandStr[MAX_COMMAND_LEN]; memset(commandStr,0, MAX_COMMAND_LEN); sscanf(command.c_str(), "%s", commandStr); if(strlen(commandStr)<=0) return ERROR; string upperCaseCommand = commandStr; for(int i=0;i<upperCaseCommand.length();i++) { upperCaseCommand[i] = toupper(upperCaseCommand[i]); } if(strcmp(upperCaseCommand.c_str(), "CREATOR") == 0) { mWindow->displayCreator(); } else if(strcmp(upperCaseCommand.c_str(), "HELP") == 0) { mWindow->displayHelpMenu(); } else if(strcmp(upperCaseCommand.c_str(), "MYIP") == 0) { mWindow->displayMyIP(mMyIP); } else if(strcmp(upperCaseCommand.c_str(), "MYPORT") == 0) { mWindow->displayMyPort(mPort); } else if(strcmp(upperCaseCommand.c_str(), "REGISTER") == 0) { if(mMode == SERVER) { cout<<"\n Register command not available in server mode"<<endl; return true; } registerCommand(command); } else if(strcmp(upperCaseCommand.c_str(), "CONNECT") == 0) { connectCommand(command); return 1; } else if(strcmp(upperCaseCommand.c_str(), "LIST") == 0) { listCommand(); } else if(strcmp(upperCaseCommand.c_str(), "TERMINATE") == 0) { terminateCommand(command); } else if(strcmp(upperCaseCommand.c_str(), "EXIT") == 0) { exitCommand(); return EXIT_PROGRAM; } else if(strcmp(upperCaseCommand.c_str(), "UPLOAD") == 0) { uploadCommand(command); } else if(strcmp(upperCaseCommand.c_str(), "DOWNLOAD") == 0) { downloadCommand(command); } else if(strcmp(upperCaseCommand.c_str(), "STATISTICS") == 0) { statCommand(); } else { //Wrong command enter cout<<"\n Command not found. Please note the commands are case sensetive\n"; return ERROR; } }