Large commit: Added reset function for CommandStruct, added reading from ~/.pishrc, and fixed running command in fork

This commit is contained in:
TriantaTV 2022-10-27 00:25:40 -05:00
parent 68e9985c18
commit 8a1ac21a5c
7 changed files with 58 additions and 21 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

@ -4,5 +4,6 @@
#include "Common.h" #include "Common.h"
void IntegratedCheck(char* command); void IntegratedCheck(char* command);
void ReadPishrc(void);
#endif #endif

View File

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

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++)
memset(command->argv[i], 0, sizeof(command->argv[i]));
command->argc = 0;
return;
}

View File

@ -1,4 +1,12 @@
#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 "Integrated.h" #include "Integrated.h"
// Checks for commands that are built into Pish // Checks for commands that are built into Pish
@ -11,4 +19,27 @@ void IntegratedCheck(char* command)
if (command[0] == 'c' && command[1] == 'd') if (command[0] == 'c' && command[1] == 'd')
; ;
return; 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);
} }

View File

@ -13,8 +13,10 @@ 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();
while (1) while (1)
{ {
ResetCommandStruct(command);
GetInput(inputString); GetInput(inputString);
ParseInput(inputString, command); ParseInput(inputString, command);
IntegratedCheck(command->argv[0]); IntegratedCheck(command->argv[0]);
@ -25,34 +27,23 @@ 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
{ // Removed due to potentially useless
// This is the parent process; Wait for the child to finish // int pid = forkPID;
// Removed due to potentially useless // wait(&pid);
// int pid = forkPID; wait(&forkPID);
// wait(&pid);
wait(&forkPID);
}
// TODO: Remove break when while loop doesn't break program // TODO: Remove break when while loop doesn't break program
break; 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 // Prints a prompt and then reads a command from the terminal
void GetInput(char* inputString) void GetInput(char* inputString)
{ {
@ -62,6 +53,7 @@ void GetInput(char* inputString)
return; return;
} }
// Splits a string separated by spaces into an array of strings
void ParseInput(char* inputString, CommandStruct* command) void ParseInput(char* inputString, CommandStruct* command)
{ {
//Parse command //Parse command