pish/src/Integrated.c

277 lines
6.9 KiB
C
Raw Normal View History

#include <assert.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include "Integrated.h"
// Checks for commands that are built into Pish
int IntegratedCheck(CommandStruct *command)
{
2022-10-27 19:57:26 -05:00
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->argv[0][0] == 'c' && command->argv[0][1] == 'd')
{
if (command->argv[1] != NULL)
chdir(command->argv[1]);
if (command->argv[1] == NULL)
chdir(GetHomeDir());
return 1;
}
return 0;
}
// Prints a prompt and then reads a command from the terminal
2022-10-27 19:57:26 -05:00
void GetInput(char *inputString)
{
2022-10-27 19:57:26 -05:00
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) // Cleans input
2022-10-27 19:57:26 -05:00
/* discard */;
return;
}
// Splits a string separated by spaces into an array of strings
2022-10-28 23:00:52 -05:00
void ParseInput(char *inputString, CommandStruct *command, char *symbol)
{
2022-10-27 19:57:26 -05:00
// Parse command
int *argc = &(command->argc);
2022-10-28 23:00:52 -05:00
char *token = strtok(inputString, symbol);//change
2022-10-27 19:57:26 -05:00
command->argv[*argc] = token;
(*argc)++;
while (token != NULL)
2022-10-27 19:57:26 -05:00
{
// This only holds 1 command at a time then it overrides
// Will need modified
2022-10-28 23:00:52 -05:00
token = strtok(NULL, symbol);//change
2022-10-27 19:57:26 -05:00
command->argv[*argc] = token;
(*argc)++;
}
}
// Run child process
void RunChild(CommandStruct* commandChild, int* pipefd)
{
close(pipefd[0]);
dup2(pipefd[1], 1);
close(pipefd[1]);
// This is the child process
// Setup the child's process environment here
// E.g., where is standard I/O, how to handle signals?
// execve() is recommended, execvpe() may be better
execvp(commandChild->argv[0], commandChild->argv);
// exec does not return if it succeeds
printf("ERROR: Could not execute %s\n", commandChild->argv[0]);
exit(1);
}
2022-10-27 19:57:26 -05:00
// Splits a string separated by spaces into an array of strings
void SplitInput(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)++;
}
(*argc)--;
}
// Splits a command array into left and right
// void SplitPipe(CommandStruct* commandLeft, CommandStruct* commandRight);
int CheckSymbol(CommandStruct* command, char symbol, int start)
{
for (int i = start; i < command->argc; i++)
if (command->argv[i][0] == symbol)
return i;
return -1;
}
2022-10-28 23:00:52 -05:00
// Checking redirection
2022-10-27 19:57:26 -05:00
// 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 CheckRedirection(CommandStruct* command)
{
2022-10-28 23:00:52 -05:00
// check command standard output
2022-10-27 19:57:26 -05:00
// TODO: Check command->argv[] for symbol instead
for (int i = 0; i < command->argc; i++)
{
if ((command->argv[i][0] == '>') && (command->argv[i][1] == '>'))
printf(">> was found.\n");
// ParseInput(inputString, command, ">>");
if (command->argv[i][0] == '<')
printf("< was found.\n");
// ParseInput(inputString, command, "<");
if (command->argv[i][0] == '>')
printf("> was found.\n");
// ParseInput(inputString, command, ">");
if (command->argv[i][0] == '|')
printf("| was found.\n");
// ParseInput(inputString, command, "|");
// else
// ParseInput(inputString, command, " ");
}
return;
2022-10-28 23:00:52 -05:00
}
void ioRedirection(CommandStruct* command)
2022-10-28 23:00:52 -05:00
{
for (int i = 0; i <sizeof(command); i++)
{
if (strpbrk(command->argv[i], ">>")!= NULL)//append
{
append(command->argv[i-1], command->argv[i+1]);
}
else if (strchr(command->argv[i], '<')!= NULL)//input
{
input(command->argv[i+1]);
}
else if (strchr(command->argv[i],'|')!= NULL)//pipe
{
//ExecPipe();
}
else if (strchr(command->argv[i], '>')!= NULL)//output
{
output(command->argv[i+1]);
}
else{}
//exit
}
2022-10-27 22:45:40 -05:00
}
2022-10-27 23:11:40 -05:00
2022-10-27 22:45:40 -05:00
void output(char *command){
int newfd;
if (newfd = open(command, O_CREAT | O_TRUNC | O_WRONLY, 0644) < 0)
2022-10-27 22:45:40 -05:00
{
perror(command);
exit(1);
}
dup2(newfd,1);
}
2022-10-27 23:11:40 -05:00
2022-10-27 22:45:40 -05:00
void input(char *command)
{
int newfd;
if (newfd = open(command, O_RDONLY) < 0)
{
perror(command);
exit(1);
}
dup2(newfd,0);
2022-10-27 22:45:40 -05:00
}
2022-10-27 23:11:40 -05:00
int ExecPipe(CommandStruct* commandParent, CommandStruct* commandChild, int* pipefd)
2022-10-27 23:11:40 -05:00
{
char buffer[1024];
int forkPID;
int pipeLocation = 0;
int startArgc = 0;
int endArgc = commandParent->argc;
while (pipeLocation >= 0)
{
if (pipeLocation > 0)
startArgc = pipeLocation + 1;
pipeLocation = CheckSymbol(commandParent, '|', startArgc);
if (pipeLocation >= 0)
endArgc = pipeLocation - 1;
if (pipeLocation < 0)
endArgc = commandParent->argc;
CopyCommandStruct(commandParent, commandChild, startArgc, endArgc);
pipe(pipefd);
forkPID = fork();
if (forkPID == 0)
return forkPID;
if (forkPID != 0)
wait(&forkPID);
close(pipefd[1]);
while (read(pipefd[0], buffer, sizeof(buffer)) != 0)
printf("%s", buffer);
ResetCommandStruct(commandChild); // Clean command
}
return 1;
2022-10-27 23:11:40 -05:00
}
void append(char* input, char* fileName)
2022-10-27 22:45:40 -05:00
{
char buffer[4];
int fd = open(fileName, O_APPEND | O_CREAT);
// fp = fopen(file, "a+");
// fputs(input, fp);
// fclose(fp);
2022-10-27 22:45:40 -05:00
}
2022-10-27 19:57:26 -05:00
// check command standard input
// if (strchr(inputString, '<') != NULL)
// {
// 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 pipe
// TODO: Check for pipe symbol
// Check in command->argv[] instead
// if (strchr(command, '|') != NULL)
// {
// ;
// }
// check append
// TODO: Implement append
// if (strpbrk(command, ">>") != NULL)
// {
// 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 pipe
// TODO: Check pipe already exists above, maybe loop?
// if (strchr(command, '|') != NULL)
// {
// ;
// }
// check append
// TODO: Check append already exists above, maybe loop?
// if (strpbrk(command, ">>") != NULL)
// {
// ;
// }
2022-10-27 19:57:26 -05:00
// 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;
// }