Added .pishrc and a function for resetting an object of the struct
This commit is contained in:
parent
8a1ac21a5c
commit
a8420ec3f8
@ -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
|
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
21
src/Pish.c
21
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)++;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user