#include #include #include #include #include #include #include #include #include #include #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(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->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); } // 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; }