added ioRedirection/varibles
This commit is contained in:
commit
25cd3f8929
5
Makefile
5
Makefile
@ -4,6 +4,7 @@ all: compile link
|
||||
|
||||
compile:
|
||||
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/Pish.o src/Pish.c
|
||||
|
||||
@ -13,17 +14,17 @@ link:
|
||||
exec: compile link
|
||||
./bin/pish.out
|
||||
|
||||
debug: debugCompile debugLink
|
||||
debug: clean debugCompile debugLink
|
||||
|
||||
debugCompile:
|
||||
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/Common.o src/Common.c
|
||||
gcc $(INC) -g -c -o build/Pish.o src/Pish.c
|
||||
|
||||
debugLink:
|
||||
gcc -g -o bin/pish.out build/*.o
|
||||
|
||||
|
||||
clean:
|
||||
rm build/*.o
|
||||
rm bin/*.out
|
||||
|
@ -6,4 +6,6 @@ typedef struct CommandStruct {
|
||||
int argc;
|
||||
} CommandStruct;
|
||||
|
||||
void ResetCommandStruct(CommandStruct* command);
|
||||
|
||||
#endif
|
@ -3,6 +3,10 @@
|
||||
|
||||
#include "Common.h"
|
||||
|
||||
void IntegratedCheck(char* command);
|
||||
const char* GetHomeDir(void);
|
||||
void IntegratedCheck(CommandStruct* command);
|
||||
void GetInput(char* inputString);
|
||||
void ParseInput(char* inputString, CommandStruct* command);
|
||||
void ReadPishrc(CommandStruct* command, char* inputString);
|
||||
|
||||
#endif
|
@ -4,8 +4,5 @@
|
||||
#include "Common.h"
|
||||
|
||||
void Pish(void);
|
||||
void Execute(char* command);
|
||||
void GetInput(char* inputString);
|
||||
void ParseInput(char* inputString, CommandStruct* command);
|
||||
|
||||
#endif
|
||||
|
11
src/Common.c
Normal file
11
src/Common.c
Normal 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++)
|
||||
command->argv[i] = "";
|
||||
command->argc = 0;
|
||||
return;
|
||||
}
|
@ -1,14 +1,91 @@
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
#include <pwd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include "Integrated.h"
|
||||
|
||||
// Checks for commands that are built into Pish
|
||||
void IntegratedCheck(char* command)
|
||||
// Gets home directory location of user
|
||||
const char* GetHomeDir(void)
|
||||
{
|
||||
if (command == "exit")
|
||||
char* homeDir = (getpwuid(getuid()))->pw_dir;
|
||||
strcat(homeDir, "/.pishrc");
|
||||
return homeDir;
|
||||
}
|
||||
|
||||
|
||||
// Checks for commands that are built into Pish
|
||||
void IntegratedCheck(CommandStruct* command)
|
||||
{
|
||||
if (strcmp(command->argv[0], "exit") == 0)
|
||||
exit(0);
|
||||
// If there is an argument, change to argument location
|
||||
// Else, change to home directory
|
||||
if (command[0] == 'c' && command[1] == 'd')
|
||||
if (command->argv[0][0] == 'c' && command->argv[0][1] == 'd')
|
||||
;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// Prints a prompt and then reads a command from the terminal
|
||||
void GetInput(char* inputString)
|
||||
{
|
||||
char c;
|
||||
char* prompt = "pish%>";
|
||||
printf("%s ", prompt);
|
||||
// assert(scanf("%[^\n]s", inputString) == 1);
|
||||
scanf("%1000[^\n]s", inputString);
|
||||
while((c = getchar()) != '\n' && c != EOF)
|
||||
/* discard */ ;
|
||||
return;
|
||||
}
|
||||
|
||||
// Splits a string separated by spaces into an array of strings
|
||||
void ParseInput(char* inputString, CommandStruct* command)
|
||||
{
|
||||
//Parse command
|
||||
int* argc = &(command->argc);
|
||||
char* token = strtok(inputString, " ");
|
||||
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[*argc] = token;
|
||||
(*argc)++;
|
||||
}
|
||||
}
|
||||
|
||||
// Reads ~/.pishrc and runs each command in the file
|
||||
void ReadPishrc(CommandStruct* command, char* inputString)
|
||||
{
|
||||
char buffer[1];
|
||||
int forkID;
|
||||
int* argc = &(command->argc);
|
||||
int fd = open(GetHomeDir(), O_RDONLY | O_CREAT);
|
||||
assert(fd > -1);
|
||||
while (read(fd, buffer, 1) > 0)
|
||||
{
|
||||
if (buffer[0] == '\n')
|
||||
{
|
||||
ParseInput(inputString, command);
|
||||
forkID = fork();
|
||||
if (forkID == 0)
|
||||
execvp(command->argv[0], command->argv);
|
||||
else
|
||||
wait(&forkID);
|
||||
strcpy(inputString, "");
|
||||
ResetCommandStruct(command);
|
||||
continue;
|
||||
}
|
||||
strcat(inputString, buffer);
|
||||
}
|
||||
assert(close(fd) >= 0);
|
||||
}
|
||||
|
56
src/Pish.c
56
src/Pish.c
@ -1,3 +1,4 @@
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
@ -13,11 +14,14 @@ void Pish(void)
|
||||
CommandStruct* command = &commandObject;
|
||||
char inputString[1000]; // Max 1000 characters
|
||||
int forkPID;
|
||||
ReadPishrc(command, inputString);
|
||||
while (1)
|
||||
{
|
||||
ResetCommandStruct(command); // Clean command
|
||||
strcpy(inputString, ""); // Clean inputString
|
||||
GetInput(inputString);
|
||||
ParseInput(inputString, command);
|
||||
IntegratedCheck(command->argv[0]);
|
||||
IntegratedCheck(command);
|
||||
forkPID = fork();
|
||||
// Child
|
||||
if (forkPID == 0)
|
||||
@ -25,56 +29,14 @@ void Pish(void)
|
||||
// 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(inputString);
|
||||
// execve() is recommended, execvpe() may be better
|
||||
execvp(command->argv[0], command->argv);
|
||||
// exec does not return if it succeeds
|
||||
printf("ERROR: Could not execute %s\n", inputString);
|
||||
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;
|
||||
// This is the parent process; Wait for the child to finish
|
||||
wait(&forkPID);
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
void GetInput(char* inputString)
|
||||
{
|
||||
char prompt = '$';
|
||||
printf("%c ", prompt);
|
||||
scanf("%[^\n]", inputString);
|
||||
return;
|
||||
}
|
||||
|
||||
void ParseInput(char* inputString, CommandStruct* command)
|
||||
{
|
||||
//Parse command
|
||||
int* count = &(command->argc);
|
||||
char* token = strtok(inputString, " ");
|
||||
command->argv[*count] = token;
|
||||
(*count)++;
|
||||
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)++;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user