diff --git a/src/Pish.c b/src/Pish.c index b6fc767..76f6191 100644 --- a/src/Pish.c +++ b/src/Pish.c @@ -6,117 +6,75 @@ #include "Integrated.h" #include "Pish.h" -//checks if there is a special symbol for redirection -int redirectCheck(char* command) -{ - int check; - if(strchr(command, '|') != NULL){ - check = 1; - } - else if (strpbrk(command, ">>") != NULL){ - check = 2; - } - else if (strchr(command, '>') != NULL){ - check = 3; - } - else if (strchr(command, '<') != NULL){ - check = 4; - } - else{ - check = 0; -} -//i/o Redirection (void my need to be changed) -void ioRedirect(int sym, char* command) -{ - // |1, >>2, >3, <4 - if(sym == 1) - { - // | - } - else if(sym == 2) - { - // >> Append - // does not replace file contents just adds - } - else if(sym == 3) - { - // > Redirecting output - // create a file if doesnt exist - // replaces files contents - } - else - { - // < ?? - } -} - -// Prints a prompt and then reads a command from the terminal -char* getInput(void) -{ - char prompt = '$'; - char *commandLine; - - printf("%c ", prompt); - scanf("%[^\n]", commandLine); - - return commandLine; -} - -// Executes a command to the system -void exec(char* command) -{ - system(command); -} - // Main function for Pish program void Pish(void) { - char* command; - int retval; - int redirect; - + CommandStruct commandObject = {"", 0}; + CommandStruct* command = &commandObject; + char inputString[1000]; // Max 1000 characters + int forkPID; while (1) { - command = getInput(); - //Parse command - // Need to check for |,<,>,>> - //redirect = redirectCheck(command); - if (redirect > 0) - { - //ioRedirect(redirect, command); - } - else - { - char* token = strtok(command, " "); - while(token != NULL) - { - //This only holds 1 command at a time then it overrides - //Will need modified - token = strtok(NULL, " "); - } - { - - IntegratedCheck(command); - retval = fork(); + GetInput(inputString); + ParseInput(inputString, command); + IntegratedCheck(command->argv[0]); + forkPID = fork(); // Child - if (retval == 0) + if (forkPID == 0) { // This is the child process // Setup the child's process environment here // E.g., where is standard I/O, how to handle signals? - exec(command); + // TODO: Adjust Execute() to handle CommandStruct + Execute(inputString); // exec does not return if it succeeds - printf("ERROR: Could not execute %s\n", command); + printf("ERROR: Could not execute %s\n", inputString); exit(1); } // Parent else { // This is the parent process; Wait for the child to finish - int pid = retval; - wait(pid); + // Removed due to potentially useless + // int pid = forkPID; + // wait(&pid); + wait(&forkPID); } // TODO: Remove break when while loop doesn't break program break; } } + +// Executes a command to the system +void Execute(char* command) +{ + printf("%s\n", command); + system(command); + exit(0); +} + +// Prints a prompt and then reads a command from the terminal +void GetInput(char* inputString) +{ + char prompt = '$'; + printf("%c ", prompt); + scanf("%[^\n]", inputString); + return; +} + +void ParseInput(char* inputString, CommandStruct* command) +{ + //Parse command + int* count = &(command->argc); + char* token = strtok(inputString, " "); + command->argv[*count] = token; + (*count)++; + while (token != NULL) + { + //This only holds 1 command at a time then it overrides + //Will need modified + token = strtok(NULL, " "); + command->argv[*count] = token; + (*count)++; + } +}