#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; return homeDir; } // Checks for commands that are built into Pish int 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') { 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 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) // Cleans input /* discard */; return; } // Splits a string separated by spaces into an array of strings void ParseInput(char *inputString, CommandStruct *command, char *symbol) { // Parse command int *argc = &(command->argc); char *token = strtok(inputString, symbol);//change 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, symbol);//change 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); char* pishLocation = GetHomeDir(); strcat(pishLocation, "/.pishrc"); int fd = open(pishLocation, 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); } // 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)--; } // Checking redirection // 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) { // check command standard output // 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; } void ioRedirection(CommandStruct* command) { for (int i = 0; i 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 } } void output(char *command){ int newfd; if (newfd = open(command, O_CREAT | O_TRUNC | O_WRONLY, 0644) < 0) { perror(command); exit(1); } dup2(newfd,1); } void input(char *command) { int newfd; if (newfd = open(command, O_RDONLY) < 0) { perror(command); exit(1); } dup2(newfd,0); } void ExecPipe() { // ; } void append(char *input, char *file) { FILE *fp; fp = fopen(file, "a+"); fputs(input, fp); fclose(fp); } // 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) // { // ; // } // 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; // }