Added comments and tips for code

This commit is contained in:
TriantaTV 2022-10-27 19:57:26 -05:00
parent 9f28625f1a
commit 72c42aa825
2 changed files with 132 additions and 113 deletions

View File

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

View File

@ -11,143 +11,161 @@
#include "Integrated.h" #include "Integrated.h"
// Gets home directory location of user // Gets home directory location of user
const char* GetHomeDir(void) const char *GetHomeDir(void)
{ {
char* homeDir = (getpwuid(getuid()))->pw_dir; char *homeDir = (getpwuid(getuid()))->pw_dir;
strcat(homeDir, "/.pishrc"); strcat(homeDir, "/.pishrc");
return homeDir; return homeDir;
} }
// Checks for commands that are built into Pish // Checks for commands that are built into Pish
void IntegratedCheck(CommandStruct* command) void IntegratedCheck(CommandStruct *command)
{ {
if (strcmp(command->argv[0], "exit") == 0) 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->argv[0][0] == 'c' && command->argv[0][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 // Prints a prompt and then reads a command from the terminal
void GetInput(char* inputString) void GetInput(char *inputString)
{ {
char c; char c;
char* prompt = "pish%>"; char *prompt = "pish%>";
printf("%s ", prompt); printf("%s ", prompt);
// assert(scanf("%[^\n]s", inputString) == 1); // assert(scanf("%[^\n]s", inputString) == 1);
scanf("%1000[^\n]s", inputString); scanf("%1000[^\n]s", inputString);
while((c = getchar()) != '\n' && c != EOF) while ((c = getchar()) != '\n' && c != EOF)
/* discard */ ; /* discard */;
return; return;
} }
// Splits a string separated by spaces into an array of strings // 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
int* argc = &(command->argc); int *argc = &(command->argc);
char* token = strtok(inputString, " "); char *token = strtok(inputString, " ");
command->argv[*argc] = token; command->argv[*argc] = token;
(*argc)++; (*argc)++;
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, " ");
command->argv[*argc] = token; command->argv[*argc] = token;
(*argc)++; (*argc)++;
} }
} }
// Reads ~/.pishrc and runs each command in the file // Reads ~/.pishrc and runs each command in the file
void ReadPishrc(CommandStruct* command, char* inputString) void ReadPishrc(CommandStruct *command, char *inputString)
{ {
char buffer[1]; char buffer[1];
int forkID; int forkID;
int* argc = &(command->argc); int *argc = &(command->argc);
int fd = open(GetHomeDir(), O_RDONLY | O_CREAT); int fd = open(GetHomeDir(), O_RDONLY | O_CREAT);
assert(fd > -1); assert(fd > -1);
while (read(fd, buffer, 1) > 0) while (read(fd, buffer, 1) > 0)
{ {
if (buffer[0] == '\n') if (buffer[0] == '\n')
{ {
ParseInput(inputString, command); ParseInput(inputString, command);
forkID = fork(); forkID = fork();
if (forkID == 0) if (forkID == 0)
execvp(command->argv[0], command->argv); execvp(command->argv[0], command->argv);
else else
wait(&forkID); wait(&forkID);
strcpy(inputString, ""); strcpy(inputString, "");
ResetCommandStruct(command); ResetCommandStruct(command);
continue; continue;
} }
strcat(inputString, buffer); strcat(inputString, buffer);
} }
assert(close(fd) >= 0); assert(close(fd) >= 0);
} }
// i/o redirection // i/o redirection
void ioRedirection(char* inputString, CommandStruct* command) // TODO: Work on one symbol at a time
// inputString may not be useful here, only *command
// Try to add a layer of abstraction where possible
// I.E. think about using function instead for checking
void ioRedirection(char *inputString, CommandStruct *command)
{ {
int newfd; int newfd;
// check command standard output // check command standard output
if (strchr(inputString, '>' )!= NULL) // TODO: Check command->argv[] for symbol instead
{ if (strchr(inputString, '>') != NULL)
{
// TODO: Throwing an error here and line 108 for using command
// Check if targeting a specific string in command->argv works
newfd = (open((command, O_CREAT |O_TRUNC | O_WRONLY, 0644)) < 0);
// No if statement for fail checking, brackets just exist here
{
// failed
perror(command);
exit(1);
}
newfd = open((command, O_CREAT |O_TRUNC | O_WRONLY, 0644)) <0) { // copies the new file descriptor
//failed dup2(newfd, 1);
perror(command); } // this is supposed to close if statement??
exit(1);
}
// copies the new file descriptor // check command standard input
dup2(newfd,1); // if (strchr(inputString, '<') != NULL)
}//this is supposed to close if statement?? // {
// newfd = open((command, O_CREAT |O_TRUNC | O_WRONLY, 0644)) < 0);
// // No if statement for fail checking, brackets just exist here
// {
// // failed
// perror(command);
// exit(1);
// }
// dup2(newfd, 0);
// }
// check command standard input // check pipe
if(strchr(inputString, '<') != NULL) // TODO: Check for pipe symbol
{ // Check in command->argv[] instead
newfd = open((command, O_CREAT |O_TRUNC | O_WRONLY, 0644)) <0) { // if (strchr(command, '|') != NULL)
//failed // {
perror(command); // ;
exit(1); // }
} // check append
dup2(newfd,0); // TODO: Implement append
} // if (strpbrk(command, ">>") != NULL)
// {
//check pipe // newfd = open((command, O_CREAT |O_TRUNC | O_WRONLY, 0644)) < 0);
if(strchr(command, '|') != NULL) // // No if statement for fail checking, brackets just exist here
{ // {
// // failed
} // perror(command);
//check append // exit(1);
if (strpbrk(command, ">>") != NULL) // }
newfd = open((command, O_CREAT |O_TRUNC | O_WRONLY, 0644)) <0) { // dup2(newfd, 0);
//failed // }
perror(command); // check pipe
exit(1); // TODO: Check pipe already exists above, maybe loop?
} // if (strchr(command, '|') != NULL)
dup2(newfd,0); // {
} // ;
// }
//check pipe // check append
if(strchr(command, '|') != NULL) // TODO: Check append already exists above, maybe loop?
{ // if (strpbrk(command, ">>") != NULL)
// {
} // ;
//check append // }
if (strpbrk(command, ">>") != NULL)
{
}
} }
//Environment varibles
void varibles(char str, int count)
{
char var[1000];
var[count]= str;
} // Environment varibles
// TODO: Add this function to CommandStruct
// Environment Variables can just be a part of the struct
// void varibles(char str, int count)
// {
// char var[1000];
// var[count] = str;
// }