2022-10-27 00:25:40 -05:00
|
|
|
#include <assert.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <pwd.h>
|
2022-10-19 21:44:28 -05:00
|
|
|
#include <stdlib.h>
|
2022-10-27 00:25:40 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2022-10-27 03:47:19 -05:00
|
|
|
#include <sys/wait.h>
|
2022-10-19 21:44:28 -05:00
|
|
|
#include "Integrated.h"
|
|
|
|
|
2022-10-27 03:47:19 -05:00
|
|
|
// Gets home directory location of user
|
|
|
|
const char* GetHomeDir(void)
|
|
|
|
{
|
|
|
|
char* homeDir = (getpwuid(getuid()))->pw_dir;
|
|
|
|
strcat(homeDir, "/.pishrc");
|
|
|
|
return homeDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-19 21:44:28 -05:00
|
|
|
// Checks for commands that are built into Pish
|
2022-10-27 04:37:16 -05:00
|
|
|
void IntegratedCheck(CommandStruct* command)
|
2022-10-19 21:44:28 -05:00
|
|
|
{
|
2022-10-27 04:37:16 -05:00
|
|
|
if (strcmp(command->argv[0], "exit") == 0)
|
2022-10-19 21:44:28 -05:00
|
|
|
exit(0);
|
|
|
|
// If there is an argument, change to argument location
|
|
|
|
// Else, change to home directory
|
2022-10-27 04:37:16 -05:00
|
|
|
if (command->argv[0][0] == 'c' && command->argv[0][1] == 'd')
|
2022-10-19 21:44:28 -05:00
|
|
|
;
|
|
|
|
return;
|
2022-10-27 00:25:40 -05:00
|
|
|
}
|
|
|
|
|
2022-10-27 05:38:12 -05:00
|
|
|
|
|
|
|
// 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)++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-27 00:25:40 -05:00
|
|
|
// Reads ~/.pishrc and runs each command in the file
|
2022-10-27 03:47:19 -05:00
|
|
|
void ReadPishrc(CommandStruct* command, char* inputString)
|
2022-10-27 00:25:40 -05:00
|
|
|
{
|
2022-10-27 04:37:16 -05:00
|
|
|
char buffer[1];
|
2022-10-27 03:47:19 -05:00
|
|
|
int forkID;
|
|
|
|
int* argc = &(command->argc);
|
|
|
|
int fd = open(GetHomeDir(), O_RDONLY | O_CREAT);
|
2022-10-27 00:25:40 -05:00
|
|
|
assert(fd > -1);
|
2022-10-27 04:37:16 -05:00
|
|
|
while (read(fd, buffer, 1) > 0)
|
2022-10-27 00:25:40 -05:00
|
|
|
{
|
2022-10-27 04:37:16 -05:00
|
|
|
if (buffer[0] == '\n')
|
2022-10-27 00:25:40 -05:00
|
|
|
{
|
2022-10-27 05:38:12 -05:00
|
|
|
ParseInput(inputString, command);
|
2022-10-27 03:47:19 -05:00
|
|
|
forkID = fork();
|
|
|
|
if (forkID == 0)
|
|
|
|
execvp(command->argv[0], command->argv);
|
2022-10-27 05:38:12 -05:00
|
|
|
else
|
|
|
|
wait(&forkID);
|
|
|
|
strcpy(inputString, "");
|
2022-10-27 03:47:19 -05:00
|
|
|
ResetCommandStruct(command);
|
2022-10-27 05:38:12 -05:00
|
|
|
continue;
|
2022-10-27 00:25:40 -05:00
|
|
|
}
|
2022-10-27 04:37:16 -05:00
|
|
|
strcat(inputString, buffer);
|
2022-10-27 00:25:40 -05:00
|
|
|
}
|
|
|
|
assert(close(fd) >= 0);
|
2022-10-27 05:38:12 -05:00
|
|
|
}
|
2022-10-27 19:17:35 -05:00
|
|
|
// i/o redirection
|
|
|
|
void ioRedirection(char* inputString, CommandStruct* command)
|
|
|
|
{
|
|
|
|
int newfd;
|
|
|
|
// check command standard output
|
|
|
|
if (strchr(inputString, '>' )!= NULL)
|
|
|
|
{
|
|
|
|
|
|
|
|
newfd = open((command, O_CREAT |O_TRUNC | O_WRONLY, 0644)) <0) {
|
|
|
|
//failed
|
|
|
|
perror(command);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// copies the new file descriptor
|
|
|
|
dup2(newfd,1);
|
|
|
|
}//this is supposed to close if statement??
|
|
|
|
|
|
|
|
// check command standard input
|
|
|
|
if(strchr(inputString, '<') != NULL)
|
|
|
|
{
|
|
|
|
newfd = open((command, O_CREAT |O_TRUNC | O_WRONLY, 0644)) <0) {
|
|
|
|
//failed
|
|
|
|
perror(command);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
dup2(newfd,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
//check pipe
|
|
|
|
if(strchr(command, '|') != NULL)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
//check append
|
|
|
|
if (strpbrk(command, ">>") != NULL)
|
|
|
|
newfd = open((command, O_CREAT |O_TRUNC | O_WRONLY, 0644)) <0) {
|
|
|
|
//failed
|
|
|
|
perror(command);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
dup2(newfd,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
//check pipe
|
|
|
|
if(strchr(command, '|') != NULL)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
//check append
|
|
|
|
if (strpbrk(command, ">>") != NULL)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//Environment varibles
|
|
|
|
void varibles(char str, int count)
|
|
|
|
{
|
|
|
|
char var[1000];
|
|
|
|
var[count]= str;
|
|
|
|
|
|
|
|
}
|