diff --git a/Makefile b/Makefile index 4993ac8..6d352fe 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,7 @@ all: compile link compile: gcc $(INC) -c -o build/main.o src/main.c + gcc $(INC) -c -o build/Common.o src/Common.c gcc $(INC) -c -o build/Integrated.o src/Integrated.c gcc $(INC) -c -o build/Pish.o src/Pish.c @@ -13,17 +14,17 @@ link: exec: compile link ./bin/pish.out -debug: debugCompile debugLink +debug: clean debugCompile debugLink debugCompile: gcc $(INC) -g -c -o build/main.o src/main.c gcc $(INC) -g -c -o build/Integrated.o src/Integrated.c + gcc $(INC) -g -c -o build/Common.o src/Common.c gcc $(INC) -g -c -o build/Pish.o src/Pish.c debugLink: gcc -g -o bin/pish.out build/*.o - clean: rm build/*.o rm bin/*.out diff --git a/include/Common.h b/include/Common.h index 7b217a3..25ac07a 100755 --- a/include/Common.h +++ b/include/Common.h @@ -6,4 +6,6 @@ typedef struct CommandStruct { int argc; } CommandStruct; +void ResetCommandStruct(CommandStruct* command); + #endif \ No newline at end of file diff --git a/include/Integrated.h b/include/Integrated.h index 0826d06..ac3a921 100755 --- a/include/Integrated.h +++ b/include/Integrated.h @@ -4,5 +4,6 @@ #include "Common.h" void IntegratedCheck(char* command); +void ReadPishrc(void); #endif \ No newline at end of file diff --git a/include/Pish.h b/include/Pish.h index 1f230e4..8868804 100644 --- a/include/Pish.h +++ b/include/Pish.h @@ -4,7 +4,6 @@ #include "Common.h" void Pish(void); -void Execute(char* command); void GetInput(char* inputString); void ParseInput(char* inputString, CommandStruct* command); diff --git a/src/Common.c b/src/Common.c new file mode 100644 index 0000000..e416eee --- /dev/null +++ b/src/Common.c @@ -0,0 +1,11 @@ +#include "Common.h" +#include +#include + +void ResetCommandStruct(CommandStruct* command) +{ + for (int i = 0; i < command->argc; i++) + memset(command->argv[i], 0, sizeof(command->argv[i])); + command->argc = 0; + return; +} diff --git a/src/Integrated.c b/src/Integrated.c index 00103c8..407f900 100755 --- a/src/Integrated.c +++ b/src/Integrated.c @@ -1,4 +1,12 @@ +#include +#include +#include #include +#include +#include +#include +#include +#include #include "Integrated.h" // Checks for commands that are built into Pish @@ -11,4 +19,27 @@ void IntegratedCheck(char* command) if (command[0] == 'c' && command[1] == 'd') ; return; +} + +// Reads ~/.pishrc and runs each command in the file +void ReadPishrc(void) +{ + char* homeDir = (getpwuid(getuid()))->pw_dir; + strcat(homeDir, "/.pishrc"); + int fd = open(homeDir, O_RDONLY | O_CREAT); + char commandString[1000] = ""; + char buffer; + assert(fd > -1); + while (read(fd, &buffer, 1) > 0) + { + strcat(commandString, &buffer); + if (buffer == '\n') + { + // TODO: Make each command run + printf("%s\n", commandString); + memset(commandString, 0, sizeof(commandString)); + continue; + } + } + assert(close(fd) >= 0); } \ No newline at end of file diff --git a/src/Pish.c b/src/Pish.c index 76f6191..3558c32 100644 --- a/src/Pish.c +++ b/src/Pish.c @@ -13,8 +13,10 @@ void Pish(void) CommandStruct* command = &commandObject; char inputString[1000]; // Max 1000 characters int forkPID; + ReadPishrc(); while (1) { + ResetCommandStruct(command); GetInput(inputString); ParseInput(inputString, command); IntegratedCheck(command->argv[0]); @@ -25,34 +27,23 @@ void Pish(void) // This is the child process // Setup the child's process environment here // E.g., where is standard I/O, how to handle signals? - // TODO: Adjust Execute() to handle CommandStruct - Execute(inputString); + // execve() is recommended, execvpe() may be better + execvp(command->argv[0], command->argv); // exec does not return if it succeeds printf("ERROR: Could not execute %s\n", inputString); exit(1); } // Parent - else - { - // This is the parent process; Wait for the child to finish - // Removed due to potentially useless - // int pid = forkPID; - // wait(&pid); - wait(&forkPID); - } + // This is the parent process; Wait for the child to finish + // 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) { @@ -62,6 +53,7 @@ void GetInput(char* inputString) return; } +// Splits a string separated by spaces into an array of strings void ParseInput(char* inputString, CommandStruct* command) { //Parse command