added ioRedirection/varibles

This commit is contained in:
Samantha Boyer 2022-10-27 18:29:22 -05:00
commit 25cd3f8929
7 changed files with 112 additions and 58 deletions

View File

@ -4,6 +4,7 @@ all: compile link
compile: compile:
gcc $(INC) -c -o build/main.o src/main.c 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/Integrated.o src/Integrated.c
gcc $(INC) -c -o build/Pish.o src/Pish.c gcc $(INC) -c -o build/Pish.o src/Pish.c
@ -13,17 +14,17 @@ link:
exec: compile link exec: compile link
./bin/pish.out ./bin/pish.out
debug: debugCompile debugLink debug: clean debugCompile debugLink
debugCompile: debugCompile:
gcc $(INC) -g -c -o build/main.o src/main.c 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/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 gcc $(INC) -g -c -o build/Pish.o src/Pish.c
debugLink: debugLink:
gcc -g -o bin/pish.out build/*.o gcc -g -o bin/pish.out build/*.o
clean: clean:
rm build/*.o rm build/*.o
rm bin/*.out rm bin/*.out

View File

@ -6,4 +6,6 @@ typedef struct CommandStruct {
int argc; int argc;
} CommandStruct; } CommandStruct;
void ResetCommandStruct(CommandStruct* command);
#endif #endif

View File

@ -3,6 +3,10 @@
#include "Common.h" #include "Common.h"
void IntegratedCheck(char* command); const char* GetHomeDir(void);
void IntegratedCheck(CommandStruct* command);
void GetInput(char* inputString);
void ParseInput(char* inputString, CommandStruct* command);
void ReadPishrc(CommandStruct* command, char* inputString);
#endif #endif

View File

@ -4,8 +4,5 @@
#include "Common.h" #include "Common.h"
void Pish(void); void Pish(void);
void Execute(char* command);
void GetInput(char* inputString);
void ParseInput(char* inputString, CommandStruct* command);
#endif #endif

11
src/Common.c Normal file
View File

@ -0,0 +1,11 @@
#include "Common.h"
#include <string.h>
#include <stdio.h>
void ResetCommandStruct(CommandStruct* command)
{
for (int i = 0; i < command->argc; i++)
command->argv[i] = "";
command->argc = 0;
return;
}

View File

@ -1,14 +1,91 @@
#include <assert.h>
#include <fcntl.h>
#include <pwd.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "Integrated.h" #include "Integrated.h"
// Checks for commands that are built into Pish // Gets home directory location of user
void IntegratedCheck(char* command) const char* GetHomeDir(void)
{ {
if (command == "exit") char* homeDir = (getpwuid(getuid()))->pw_dir;
strcat(homeDir, "/.pishrc");
return homeDir;
}
// Checks for commands that are built into Pish
void IntegratedCheck(CommandStruct* command)
{
if (strcmp(command->argv[0], "exit") == 0)
exit(0); exit(0);
// If there is an argument, change to argument location // If there is an argument, change to argument location
// Else, change to home directory // Else, change to home directory
if (command[0] == 'c' && command[1] == 'd') if (command->argv[0][0] == 'c' && command->argv[0][1] == 'd')
; ;
return; return;
} }
// Prints a prompt and then reads a command from the terminal
void GetInput(char* inputString)
{
char c;
char* prompt = "pish%>";
printf("%s ", prompt);
// assert(scanf("%[^\n]s", inputString) == 1);
scanf("%1000[^\n]s", inputString);
while((c = getchar()) != '\n' && c != EOF)
/* discard */ ;
return;
}
// Splits a string separated by spaces into an array of strings
void ParseInput(char* inputString, CommandStruct* command)
{
//Parse command
int* argc = &(command->argc);
char* token = strtok(inputString, " ");
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[*argc] = token;
(*argc)++;
}
}
// Reads ~/.pishrc and runs each command in the file
void ReadPishrc(CommandStruct* command, char* inputString)
{
char buffer[1];
int forkID;
int* argc = &(command->argc);
int fd = open(GetHomeDir(), O_RDONLY | O_CREAT);
assert(fd > -1);
while (read(fd, buffer, 1) > 0)
{
if (buffer[0] == '\n')
{
ParseInput(inputString, command);
forkID = fork();
if (forkID == 0)
execvp(command->argv[0], command->argv);
else
wait(&forkID);
strcpy(inputString, "");
ResetCommandStruct(command);
continue;
}
strcat(inputString, buffer);
}
assert(close(fd) >= 0);
}

View File

@ -1,3 +1,4 @@
#include <assert.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
@ -13,11 +14,14 @@ void Pish(void)
CommandStruct* command = &commandObject; CommandStruct* command = &commandObject;
char inputString[1000]; // Max 1000 characters char inputString[1000]; // Max 1000 characters
int forkPID; int forkPID;
ReadPishrc(command, inputString);
while (1) while (1)
{ {
ResetCommandStruct(command); // Clean command
strcpy(inputString, ""); // Clean inputString
GetInput(inputString); GetInput(inputString);
ParseInput(inputString, command); ParseInput(inputString, command);
IntegratedCheck(command->argv[0]); IntegratedCheck(command);
forkPID = fork(); forkPID = fork();
// Child // Child
if (forkPID == 0) if (forkPID == 0)
@ -25,56 +29,14 @@ void Pish(void)
// This is the child process // This is the child process
// Setup the child's process environment here // Setup the child's process environment here
// E.g., where is standard I/O, how to handle signals? // E.g., where is standard I/O, how to handle signals?
// TODO: Adjust Execute() to handle CommandStruct // execve() is recommended, execvpe() may be better
Execute(inputString); execvp(command->argv[0], command->argv);
// exec does not return if it succeeds // exec does not return if it succeeds
printf("ERROR: Could not execute %s\n", inputString); printf("ERROR: Could not execute %s\n", inputString);
exit(1); exit(1);
} }
// Parent // Parent
else // This is the parent process; Wait for the child to finish
{ 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)
{
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)++;
}
}