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"
|
#include "Common.h"
|
||||||
|
|
||||||
|
const char* GetHomeDir(void);
|
||||||
void IntegratedCheck(char* command);
|
void IntegratedCheck(char* command);
|
||||||
void ReadPishrc(void);
|
void ReadPishrc(CommandStruct* command, char* inputString);
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -5,7 +5,7 @@
|
|||||||
void ResetCommandStruct(CommandStruct* command)
|
void ResetCommandStruct(CommandStruct* command)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < command->argc; i++)
|
for (int i = 0; i < command->argc; i++)
|
||||||
memset(command->argv[i], 0, sizeof(command->argv[i]));
|
command->argv[i] = "";
|
||||||
command->argc = 0;
|
command->argc = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -7,8 +7,18 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
#include "Integrated.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
|
// Checks for commands that are built into Pish
|
||||||
void IntegratedCheck(char* command)
|
void IntegratedCheck(char* command)
|
||||||
{
|
{
|
||||||
@ -22,24 +32,33 @@ void IntegratedCheck(char* command)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Reads ~/.pishrc and runs each command in the file
|
// 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;
|
char buffer;
|
||||||
|
int forkID;
|
||||||
|
int* argc = &(command->argc);
|
||||||
|
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)
|
||||||
{
|
{
|
||||||
strcat(commandString, &buffer);
|
printf("%c", buffer);
|
||||||
|
if (buffer == ' ')
|
||||||
|
{
|
||||||
|
command->argv[*argc] = inputString;
|
||||||
|
strcpy(inputString, "");
|
||||||
|
*argc++;
|
||||||
|
}
|
||||||
if (buffer == '\n')
|
if (buffer == '\n')
|
||||||
{
|
{
|
||||||
// TODO: Make each command run
|
command->argv[*argc] = inputString;
|
||||||
printf("%s\n", commandString);
|
strcpy(inputString, "");
|
||||||
memset(commandString, 0, sizeof(commandString));
|
forkID = fork();
|
||||||
continue;
|
if (forkID == 0)
|
||||||
|
execvp(command->argv[0], command->argv);
|
||||||
|
wait(&forkID);
|
||||||
|
ResetCommandStruct(command);
|
||||||
}
|
}
|
||||||
|
strcat(inputString, &buffer);
|
||||||
}
|
}
|
||||||
assert(close(fd) >= 0);
|
assert(close(fd) >= 0);
|
||||||
}
|
}
|
21
src/Pish.c
21
src/Pish.c
@ -13,10 +13,11 @@ 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();
|
ReadPishrc(command, inputString);
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
ResetCommandStruct(command);
|
ResetCommandStruct(command); // Clean command
|
||||||
|
memset(inputString, 0, sizeof(inputString)); // Clean inputString
|
||||||
GetInput(inputString);
|
GetInput(inputString);
|
||||||
ParseInput(inputString, command);
|
ParseInput(inputString, command);
|
||||||
IntegratedCheck(command->argv[0]);
|
IntegratedCheck(command->argv[0]);
|
||||||
@ -40,15 +41,15 @@ void Pish(void)
|
|||||||
// wait(&pid);
|
// wait(&pid);
|
||||||
wait(&forkPID);
|
wait(&forkPID);
|
||||||
// TODO: Remove break when while loop doesn't break program
|
// TODO: Remove break when while loop doesn't break program
|
||||||
break;
|
// break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 prompt = '$';
|
char* prompt = "pish%>";
|
||||||
printf("%c ", prompt);
|
printf("%s ", prompt);
|
||||||
scanf("%[^\n]", inputString);
|
scanf("%[^\n]", inputString);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -57,16 +58,16 @@ void GetInput(char* inputString)
|
|||||||
void ParseInput(char* inputString, CommandStruct* command)
|
void ParseInput(char* inputString, CommandStruct* command)
|
||||||
{
|
{
|
||||||
//Parse command
|
//Parse command
|
||||||
int* count = &(command->argc);
|
int* argc = &(command->argc);
|
||||||
char* token = strtok(inputString, " ");
|
char* token = strtok(inputString, " ");
|
||||||
command->argv[*count] = token;
|
command->argv[*argc] = token;
|
||||||
(*count)++;
|
(*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[*count] = token;
|
command->argv[*argc] = token;
|
||||||
(*count)++;
|
(*argc)++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user