From a8420ec3f850d82779ba1554dd5f97d271417a03 Mon Sep 17 00:00:00 2001
From: TriantaTV <gregcraw2001@gmail.com>
Date: Thu, 27 Oct 2022 03:47:19 -0500
Subject: [PATCH] Added .pishrc and a function for resetting an object of the
 struct

---
 include/Integrated.h |  3 ++-
 src/Common.c         |  2 +-
 src/Integrated.c     | 39 +++++++++++++++++++++++++++++----------
 src/Pish.c           | 21 +++++++++++----------
 4 files changed, 43 insertions(+), 22 deletions(-)

diff --git a/include/Integrated.h b/include/Integrated.h
index ac3a921..8fa09a7 100755
--- a/include/Integrated.h
+++ b/include/Integrated.h
@@ -3,7 +3,8 @@
 
 #include "Common.h"
 
+const char* GetHomeDir(void);
 void IntegratedCheck(char* command);
-void ReadPishrc(void);
+void ReadPishrc(CommandStruct* command, char* inputString);
 
 #endif
\ No newline at end of file
diff --git a/src/Common.c b/src/Common.c
index e416eee..62393af 100644
--- a/src/Common.c
+++ b/src/Common.c
@@ -5,7 +5,7 @@
 void ResetCommandStruct(CommandStruct* command)
 {
     for (int i = 0; i < command->argc; i++)
-        memset(command->argv[i], 0, sizeof(command->argv[i]));
+        command->argv[i] = "";
     command->argc = 0;
     return;
 }
diff --git a/src/Integrated.c b/src/Integrated.c
index 407f900..3753902 100755
--- a/src/Integrated.c
+++ b/src/Integrated.c
@@ -7,8 +7,18 @@
 #include <unistd.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <sys/wait.h>
 #include "Integrated.h"
 
+// Gets home directory location of user
+const char* GetHomeDir(void)
+{
+    char* homeDir = (getpwuid(getuid()))->pw_dir;
+    strcat(homeDir, "/.pishrc");
+    return homeDir;
+}
+
+
 // Checks for commands that are built into Pish
 void IntegratedCheck(char* command)
 {
@@ -22,24 +32,33 @@ void IntegratedCheck(char* command)
 }
 
 // Reads ~/.pishrc and runs each command in the file
-void ReadPishrc(void)
+void ReadPishrc(CommandStruct* command, char* inputString)
 {
-    char* homeDir = (getpwuid(getuid()))->pw_dir;
-    strcat(homeDir, "/.pishrc");
-    int fd = open(homeDir, O_RDONLY | O_CREAT);
-    char commandString[1000] = "";
     char buffer;
+    int forkID;
+    int* argc = &(command->argc);
+    int fd = open(GetHomeDir(), O_RDONLY | O_CREAT);
     assert(fd > -1);
     while (read(fd, &buffer, 1) > 0)
     {
-        strcat(commandString, &buffer);
+        printf("%c", buffer);
+        if (buffer == ' ')
+        {
+            command->argv[*argc] = inputString;
+            strcpy(inputString, "");
+            *argc++;
+        }
         if (buffer == '\n')
         {
-            // TODO: Make each command run
-            printf("%s\n", commandString);
-            memset(commandString, 0, sizeof(commandString));
-            continue;
+            command->argv[*argc] = inputString;
+            strcpy(inputString, "");
+            forkID = fork();
+            if (forkID == 0)
+                execvp(command->argv[0], command->argv);
+            wait(&forkID);
+            ResetCommandStruct(command);
         }
+        strcat(inputString, &buffer);
     }
     assert(close(fd) >= 0);
 }
\ No newline at end of file
diff --git a/src/Pish.c b/src/Pish.c
index 3558c32..3874824 100644
--- a/src/Pish.c
+++ b/src/Pish.c
@@ -13,10 +13,11 @@ void Pish(void)
     CommandStruct* command = &commandObject;
     char inputString[1000]; // Max 1000 characters
     int forkPID;
-    ReadPishrc();
+    ReadPishrc(command, inputString);
     while (1)
     {
-        ResetCommandStruct(command);
+        ResetCommandStruct(command); // Clean command
+        memset(inputString, 0, sizeof(inputString)); // Clean inputString
         GetInput(inputString);
         ParseInput(inputString, command);
 	    IntegratedCheck(command->argv[0]);
@@ -40,15 +41,15 @@ void Pish(void)
         // wait(&pid);
         wait(&forkPID);
         // TODO: Remove break when while loop doesn't break program
-        break;
+        // break;
     }
 }
 
 // Prints a prompt and then reads a command from the terminal
 void GetInput(char* inputString)
 {
-    char prompt = '$';
-    printf("%c ", prompt);
+    char* prompt = "pish%>";
+    printf("%s ", prompt);
     scanf("%[^\n]", inputString);
     return;
 }
@@ -57,16 +58,16 @@ void GetInput(char* inputString)
 void ParseInput(char* inputString, CommandStruct* command)
 {
     //Parse command
-    int* count = &(command->argc);
+    int* argc = &(command->argc);
 	char* token = strtok(inputString, " ");
-    command->argv[*count] = token;
-    (*count)++;
+    command->argv[*argc] = token;
+    (*argc)++;
 	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)++;
+        command->argv[*argc] = token;
+        (*argc)++;
 	}
 }