Big commit: Cleaned formatting of Pish, added CommandStruct and Common.h for it

This commit is contained in:
TriantaTV 2022-10-25 01:09:00 -05:00
parent c21e2fb7f6
commit 52aeeb02e5
5 changed files with 69 additions and 47 deletions

View File

@ -1,14 +1,15 @@
INC := -I include INC := -I include
all: main exec all: compile link
main: 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/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
exec: link:
gcc -o bin/pish.out build/*.o gcc -o bin/pish.out build/*.o
clean: clean:
rm build/* rm build/*.o
rm bin/*.out

9
include/Common.h Executable file
View File

@ -0,0 +1,9 @@
#ifndef COMMON_H
#define COMMON_H
typedef struct CommandStruct {
char** argv;
int argc;
} CommandStruct;
#endif

View File

@ -1,6 +1,8 @@
#ifndef INTEGRATED_H #ifndef INTEGRATED_H
#define INTEGRATED_H #define INTEGRATED_H
#include "Common.h"
void IntegratedCheck(char* command); void IntegratedCheck(char* command);
#endif #endif

View File

@ -1,8 +1,11 @@
#ifndef PISH_H #ifndef PISH_H
#define PISH_H #define PISH_H
char* getcmd(void); #include "Common.h"
void exec(char* command);
void Pish(void); void Pish(void);
void Execute(char* command);
char* GetInput(void);
void ParseInput(char* inputString, CommandStruct command);
#endif #endif

View File

@ -6,8 +6,51 @@
#include "Integrated.h" #include "Integrated.h"
#include "Pish.h" #include "Pish.h"
// Main function for Pish program
void Pish(void)
{
CommandStruct command;
command.argc = 0;
int forkPID;
while (1)
{
ParseInput(GetInput(), command);
IntegratedCheck(command.argv[0]);
forkPID = fork();
// Child
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?
// TODO: Adjust Execute() to handle CommandStruct
Execute(command.argv[0]);
// exec does not return if it succeeds
printf("ERROR: Could not execute %s\n", command.argv[0]);
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);
}
// TODO: Remove break when while loop doesn't break program
break;
}
}
// Executes a command to the system
void Execute(char* command)
{
system(command);
}
// Prints a prompt and then reads a command from the terminal // Prints a prompt and then reads a command from the terminal
char* getInput(void) char* GetInput(void)
{ {
char prompt = '$'; char prompt = '$';
char *commandLine; char *commandLine;
@ -18,50 +61,14 @@ char* getInput(void)
return commandLine; return commandLine;
} }
// Executes a command to the system void ParseInput(char* inputString, CommandStruct command)
void exec(char* command)
{ {
system(command);
}
// Main function for Pish program
void Pish(void)
{
char* command;
int retval;
while (1)
{
command = getInput();
//Parse command //Parse command
char* token = strtok(command, " "); char* token = strtok(inputString, " ");
while(token != NULL) while(token != NULL)
{ {
//This only holds 1 command at a time then it overrides //This only holds 1 command at a time then it overrides
//Will need modified //Will need modified
token = strtok(NULL, " "); token = strtok(NULL, " ");
} }
IntegratedCheck(command);
retval = fork();
// Child
if (retval == 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);
// exec does not return if it succeeds
printf("ERROR: Could not execute %s\n", command);
exit(1);
}
// Parent
else
{
// This is the parent process; Wait for the child to finish
int pid = retval;
wait(pid);
}
// TODO: Remove break when while loop doesn't break program
break;
}
} }