command_t makeCommand(command_t command, char **tokenPTR, command_type type, char *input, char *output) { command = makeBaseCommand(command, NULL, NULL); command->type = type; if (tokenPTR) { command->u.command[0] = makeSimpleCommand(NULL, tokenPTR, input, output); } return command; }
/* FIXME: Define the type 'struct command_stream' here. This should complete the incomplete type declaration in command.h. */ command_stream_t make_command_stream(int(*get_next_byte) (void *), void *get_next_byte_argument) { /* FIXME: Replace this with your implementation. You may need to add auxiliary functions and otherwise modify the source code. You can also use external functions defined in the GNU C Library. */ if (DEBUG) printf("291 Begin make_command_stream\n"); initStream(); size_t sizeofBuffer = 1024; char* buffer = (char*)checked_malloc(sizeofBuffer); char curr; size_t filled = 0; while ((curr = get_next_byte(get_next_byte_argument)) != EOF) { buffer[filled] = curr; filled++; if (filled == sizeofBuffer) { sizeofBuffer *= 2; buffer = (char*)checked_grow_alloc(buffer, &sizeofBuffer); } } //***For the parser: //Word int bufferWordSize = 10; int currWord = 0; //char ** wordElement; char** wordElement = checked_malloc(sizeof(char*)); wordElement[currWord] = (char*)checked_malloc(bufferWordSize); int usedWord = 0; //Input int bufferInputSize = 10; char* inputElement = (char*)checked_malloc(bufferInputSize); int usedInput = 0; //Output int bufferOutputSize = 10; char* outputElement = (char*)checked_malloc(bufferOutputSize); int usedOutput = 0; //***Initialize operator and command stacks initStacks(); if (DEBUG) printf("333 Buffer created, initStacks()\n"); int i = 0; while (i < (int)filled) { if (DEBUG) printf("on buffer %d\n", i); //***When you want to add a character*************************// int op = -1; int openCount = 0; int closedCount = 0; if (buffer[i] == '`') error(1, 0, "Line %d: %c", __LINE__, buffer[i]); if (buffer[i] == '(') { openCount = 1; closedCount = 0; int x = i; while (x < (int) filled) { x++; if (buffer[x] == '(') openCount++; if (buffer[x] == ')') closedCount++; if (closedCount == openCount) break; } if (closedCount != openCount) error(1, 0, "Line %d: Expected ')' for end of subshell", __LINE__); } if(buffer[i] == ')') { if(openCount == 0) error(1, 0, "Line %d: Expected '(' before ')'", __LINE__); } if(buffer[i] = '(') { op = numberOfOper(&buffer[i]); processOperator(op); } //Case of ' ' while (buffer[i] == ' ' && usedWord == 0) i++; if (buffer[i] == ' ' && usedWord != 0) { if (usedWord >= bufferWordSize) { bufferWordSize += 10; wordElement[currWord] = (char*)checked_realloc(wordElement[currWord], bufferWordSize); } //i++; wordElement[currWord][usedWord] = '\0'; while (buffer[i + 1] == ' ') i++; usedWord = 0; bufferWordSize = 10; currWord++; wordElement[currWord] = (char*)checked_malloc(bufferWordSize); //wordElement[currWord][usedWord] = buffer[i]; //usedWord++; } //Case of carrots //WHAT IF a<b>c>d<a....?! You're making a simple command out of a<b>c which then // must also be the word of >d<a //Case of '<' first else if (buffer[i] == '<') { int k = i; while (buffer[k-1] == ' ') { k--; if(buffer[k-1] == '\n') error(1, 0, "Line %d: Operator and word on diff lines", __LINE__); } if (wordElement[0][0] == '\0') error(1, 0, "Line %d: No command given to '<'", __LINE__); i++; while (buffer[i] != '<' && buffer[i] != '>' && buffer[i] != '&' && buffer[i] != '|' && buffer[i] != '(' && buffer[i] != ')' && buffer[i] != ';' && buffer[i] != '\n') { if (i == filled) { if (DEBUG) printf("378 Complete command, free buffer bc EOF\n"); currWord++; wordElement[currWord] = '\0'; if (usedInput == 0) inputElement = NULL; if (usedOutput == 0) outputElement = NULL; makeSimpleCommand(wordElement, inputElement, outputElement); completeCommand(); //free(buffer); return headStream; } if (buffer[i] == ' ') i++; else { if (usedInput >= bufferInputSize) { bufferInputSize += 10; inputElement = (char*)checked_realloc(inputElement, bufferInputSize); } inputElement[usedInput] = buffer[i]; usedInput++; i++; } } if (usedInput >= bufferInputSize) { bufferInputSize += 10; inputElement = (char*)checked_realloc(inputElement, bufferInputSize); } if (usedInput == 0) error(1, 0, "Line %d: No input after '<'", __LINE__); inputElement[usedInput] = '\0'; usedInput++; if (buffer[i] == '>') { i++; while (buffer[i] != '<' && buffer[i] != '>' && buffer[i] != '&' && buffer[i] != '|' && buffer[i] != '(' && buffer[i] != ')' && buffer[i] != ';' && buffer[i] != '\n') { if (i == filled) { if (DEBUG) printf("413 Complete command, free buffer at EOF\n"); currWord++; wordElement[currWord] = '\0'; if (usedInput == 0) inputElement = NULL; if (usedOutput == 0) outputElement = NULL; makeSimpleCommand(wordElement, inputElement, outputElement); completeCommand(); //free(buffer); return headStream; } if (buffer[i] == ' ') i++; else { if (usedOutput >= bufferOutputSize) { bufferOutputSize += 10; outputElement = (char*)checked_realloc(outputElement, bufferOutputSize); } outputElement[usedOutput] = buffer[i]; usedOutput++; i++; } } if (usedOutput >= bufferOutputSize) { bufferOutputSize += 10; outputElement = (char*)checked_realloc(outputElement, bufferOutputSize); } if (usedOutput == 0) error(1, 0, "Line %d: No input after '<'", __LINE__); outputElement[usedOutput] = '\0'; usedOutput++; //i--; //Check logic } i--; } /////CHECK FOR EXTRA i++!!!!! //Case of '>' first else if (buffer[i] == '>') { int k = i; while (buffer[k-1] == ' ') { k--; if(buffer[k-1] == '\n') error(1, 0, "Line %d: Operator and word on diff lines", __LINE__); } if (wordElement[0][0] == '\0') error(1, 0, "Line %d: No command given to '<'", __LINE__); i++; while (buffer[i] != '<' && buffer[i] != '>' && buffer[i] != '&' && buffer[i] != '|' && buffer[i] != '(' && buffer[i] != ')' && buffer[i] != ';' && buffer[i] != '\n') { if (i == filled) { if (DEBUG) printf("471 Complete Command, free buffer at EOF"); currWord++; wordElement[currWord] = '\0'; if (usedInput == 0) inputElement = NULL; if (usedOutput == 0) outputElement = NULL; makeSimpleCommand(wordElement, inputElement, outputElement); completeCommand(); //free(buffer); return headStream; } if (buffer[i] == ' ') i++; else { if (usedOutput >= bufferOutputSize) { bufferOutputSize += 10; outputElement = (char*)checked_realloc(outputElement, bufferOutputSize); } outputElement[usedOutput] = buffer[i]; usedOutput++; i++; } } if (usedOutput >= bufferOutputSize) { bufferOutputSize += 10; outputElement = (char*)checked_realloc(outputElement, bufferOutputSize); } if (usedOutput == 0) error(1, 0, "Line %d: No input after '<'", __LINE__); outputElement[usedOutput] = '\0'; usedOutput++; if (buffer[i] == '<') { i++; while (buffer[i] != '<' && buffer[i] != '>' && buffer[i] != '&' && buffer[i] != '|' && buffer[i] != '(' && buffer[i] != ')' && buffer[i] != ';' && buffer[i] != '\n') { if (i == filled) { if (DEBUG) printf("505 Complete Command, free buffer at EOF"); currWord++; wordElement[currWord] = '\0'; if (usedInput == 0) inputElement = NULL; if (usedOutput == 0) outputElement = NULL; makeSimpleCommand(wordElement, inputElement, outputElement); completeCommand(); //free(buffer); return headStream; } if (buffer[i] == ' ') i++; else { if (usedInput >= bufferInputSize) { bufferInputSize += 10; inputElement = (char*)checked_realloc(inputElement, bufferInputSize); } inputElement[usedInput] = buffer[i]; usedInput++; i++; } } if (usedInput >= bufferInputSize) { bufferInputSize += 10; inputElement = (char*)checked_realloc(inputElement, bufferInputSize); } if (usedInput == 0) error(1, 0, "Line %d: No input after '<'", __LINE__); inputElement[usedInput] = '\0'; usedInput++; } wordElement[currWord + 1] = '\0'; /*if (usedInput == 0) inputElement = NULL; if (usedOutput == 0) outputElement = NULL; if(DEBUG) printf("makeSimpleCommand %s\n", wordElement[0]); makeSimpleCommand(wordElement, inputElement, outputElement); bufferWordSize = 10; currWord = 0; usedWord = 0; usedInput = 0; usedOutput = 0; wordElement = (char**)checked_malloc(sizeof(char*)); wordElement[currWord] = (char*)checked_malloc(bufferWordSize); inputElement = (char*)checked_malloc(bufferInputSize); outputElement = (char*)checked_malloc(bufferOutputSize);*/ i--; } //Operators //After every operator is encountered, use makeSimpleCommand to push the command on the stack //Case of '|' else if (buffer[i] == '|') { int k = i; while (buffer[k-1] == ' ') { k--; if(buffer[k-1] == '\n') error(1, 0, "Line %d: Operator and word on diff lines", __LINE__); } if (buffer[i+1] == '|' && operatorStack == NULL) error(1, 0, "Line %d: Missing pipe for '||'", __LINE__); if (wordElement[0][0] == '\0') error(1, 0, "Line %d: Nothing for '|'", __LINE__); //if (commandStack == NULL) // error(1, 0, "Line %d: Nothing to run '|' on"); if (buffer[i + 1] == '|' && buffer[i+2] == '|') error(1, 0, "Line %d: Invalid Command, too many '|'", i); op = numberOfOper(&buffer[i]); //error(1, 0, "Line %d: Nothing to pipe", __LINE__); if (buffer[i-1] != ' ') currWord++; wordElement[currWord] = '\0'; if (usedInput == 0) inputElement = NULL; if (usedOutput == 0) outputElement = NULL; if (DEBUG) printf(" 566 makeSimpleCommand %s\n", wordElement[0]); makeSimpleCommand(wordElement, inputElement, outputElement); bufferWordSize = 10; currWord = 0; usedWord = 0; usedInput = 0; usedOutput = 0; wordElement = (char**)checked_malloc(sizeof(char*)); wordElement[currWord] = (char*)checked_malloc(bufferWordSize); inputElement = (char*)checked_malloc(bufferInputSize); outputElement = (char*)checked_malloc(bufferOutputSize); if (DEBUG) printf("577 Process operator %d\n", op); processOperator(op); if (op == 3) i++; if (buffer[i + 1] == ' ') i++; //i++; } //Case of '&' else if (buffer[i] == '&') { int k = i; while (buffer[k-1] == ' ') { k--; if(buffer[k-1] == '\n') error(1, 0, "Line %d: Operator and word on diff lines", __LINE__); } //if (buffer[i] == '&' && operatorStack == NULL) // error(1, 0, "Line %d: Missing pipe for '&&'", __LINE__); if (wordElement[0][0] == '\0') error(1, 0, "Line %d: Nothing for '|'", __LINE__); if (buffer[i + 1] == '&' && buffer[i+2] == '&') error(1, 0, "Line %d: Invalid Command, too many '&'", i); op = numberOfOper(&buffer[i]); if (buffer[i-1] != ' ') currWord++; wordElement[currWord] = '\0'; if (usedInput == 0) inputElement = NULL; if (usedOutput == 0) outputElement = NULL; if (DEBUG) printf("592 makeSimpleCommand %s\n", wordElement[0]); makeSimpleCommand(wordElement, inputElement, outputElement); bufferWordSize = 10; usedWord = 0; currWord = 0; usedInput = 0; usedOutput = 0; wordElement = (char**)checked_malloc(sizeof(char*)); wordElement[currWord] = (char*)checked_malloc(bufferWordSize); inputElement = (char*)checked_malloc(bufferInputSize); outputElement = (char*)checked_malloc(bufferOutputSize); processOperator(op); i++; if (buffer[i + 1] == ' ') i++; } //Case of ';' else if (buffer[i] == ';') { int k = i; while (buffer[k-1] == ' ') { k--; if(buffer[k-1] == '\n') error(1, 0, "Line %d: Operator and word on diff lines", __LINE__); } if(wordElement[0][0] == '\0') error(1, 0, "Line %d: Nothing before sequence", i); op = numberOfOper(&buffer[i]); currWord++; wordElement[currWord] = '\0'; if (usedInput == 0) inputElement = NULL; if (usedOutput == 0) outputElement = NULL; if (DEBUG) printf("617 makeSimpleCommand %s\n", wordElement[0]); if ((currWord = 0) || (currWord == 1)) error(1, 0, "Line %d: Nothing to run ';' on", i); makeSimpleCommand(wordElement, inputElement, outputElement); bufferWordSize = 10; usedWord = 0; currWord = 0; usedInput = 0; usedOutput = 0; wordElement = (char**)checked_malloc(sizeof(char*)); wordElement[currWord] = (char*)checked_malloc(bufferWordSize); inputElement = (char*)checked_malloc(bufferInputSize); outputElement = (char*)checked_malloc(bufferOutputSize); processOperator(op); //i++; } //Case of '\n' else if (buffer[i] == '\n' && i != 0) { /*int scChecker = i; while (buffer[i + 1] == ' ') { scChecker++; if (buffer[scChecker + 1] == ';') error(1, 0, "Line %d: Newline followed by ';'"); }*/ if (buffer[i+1] == ';') error(1, 0, "Line %d: Newline followed by ';'", i); if ((i + 1) == (int)filled) { currWord++; wordElement[currWord] = '\0'; if (usedInput == 0) inputElement = NULL; if (usedOutput == 0) outputElement = NULL; if (DEBUG) printf("654 makeSimpleCommand %s\n", wordElement[0]); makeSimpleCommand(wordElement, inputElement, outputElement); bufferWordSize = 10; currWord = 0; usedWord = 0; usedInput = 0; usedOutput = 0; wordElement = (char**)checked_malloc(sizeof(char*)); wordElement[currWord] = (char*)checked_malloc(bufferWordSize); inputElement = (char*)checked_malloc(bufferInputSize); outputElement = (char*)checked_malloc(bufferOutputSize); completeCommand(); free(buffer); return headStream; } char lastC; int back = 1; while (buffer[i - back] == ' ' && i - back >= 0) { //lastC = buffer[i - back]; back++; } lastC = buffer[i - back]; if (buffer[i + 1] == '\n') { while (buffer[i + 1] == '\n') i++; if (lastC != '|' && lastC != '&') { currWord++; wordElement[currWord] = '\0'; if (usedInput == 0) inputElement = NULL; if (usedOutput == 0) outputElement = NULL; if (DEBUG) printf("654 makeSimpleCommand %s\n", wordElement[0]); makeSimpleCommand(wordElement, inputElement, outputElement); bufferWordSize = 10; currWord = 0; usedWord = 0; usedInput = 0; usedOutput = 0; wordElement = (char**)checked_malloc(sizeof(char*)); wordElement[currWord] = (char*)checked_malloc(bufferWordSize); inputElement = (char*)checked_malloc(bufferInputSize); outputElement = (char*)checked_malloc(bufferOutputSize); completeCommand(); } } else { if (lastC == '|' || lastC == '&' || lastC == '<' || lastC || '>') { while (buffer[i + 1] == '\n') i++;//////SPIT ERROR? } else { char swap = ';'; op = numberOfOper(&swap); wordElement[currWord + 1] = '\0'; if (usedInput == 0) inputElement = NULL; if (usedOutput == 0) outputElement = NULL; if (DEBUG) printf(" 679 makeSimpleCommand %s\n", wordElement[0]); makeSimpleCommand(wordElement, inputElement, outputElement); bufferWordSize = 10; currWord = 0; usedWord = 0; usedInput = 0; usedOutput = 0; wordElement = (char**)checked_malloc(sizeof(char*)); wordElement[currWord] = (char*)checked_malloc(bufferWordSize); inputElement = (char*)checked_malloc(bufferInputSize); outputElement = (char*)checked_malloc(bufferOutputSize); processOperator(op); } } } //Case of # (comment) else if (buffer[i] == '#') { if (DEBUG) printf("698 Got a comment!\n"); while (buffer[i] != '\n') i++; } //Else else { if (buffer[i] == '\'') { if (buffer[i + 1] == 'E' && buffer[i + 2] == 'O' && buffer[i + 3] == 'F') break; } //if (buffer[i] != ' ') wordElement[currWord][usedWord] = buffer[i]; usedWord++; if (i + 1 == filled) { currWord++; wordElement[currWord] = '\0'; if (usedInput == 0) inputElement = NULL; if (usedOutput == 0) outputElement = NULL; makeSimpleCommand(wordElement, inputElement, outputElement); completeCommand(); } } i++; } free(buffer); return headStream; }
command_t makeCommandStreamUtil(int (*get_next_byte) (void *), void *get_next_byte_argument, STATE *state) { char **tokenPTR = checked_malloc(sizeof(char**)); char *token = NULL; int len = 0; TOKENTYPE type; command_t command = NULL; char *input = NULL, *output = NULL; type = readNextToken(tokenPTR, &len, get_next_byte, get_next_byte_argument); if (type == NOT_DEFINED) { free(tokenPTR); return NULL; } else if (type == O_PAR) { token = "("; } else { token = *tokenPTR; } command = AllocateCommand(); if (!command) { return NULL; } if (!strncmp(token, "then", 4)) { if (!(pop() == IF)) { printErr(); } push(THEN); *state = THEN; goto ret_null; } else if (!strncmp(token, "done", 4)) { if (!(pop() == DO)) { printErr(); } *state = DONE; CScount--; goto ret_null; } else if (!strncmp(token, "do", 4)) { STATE tmp = pop(); if (!((tmp == WHILE) || (tmp == UNTIL))) { printErr(); } push(DO); *state = DO; goto ret_null; } else if (!strncmp(token, "else", 4)) { if (!(pop() == THEN)) { printErr(); } push(ELSE); *state = ELSE; goto ret_null; } else if (!strncmp(token, "fi", 4)) { STATE tmp = pop(); if (!((tmp == THEN) || (tmp == ELSE))) { printErr(); } CScount--; *state = FI; goto ret_null; } else if (!strncmp(token, ")", 1)) { CScount--; *state = CLOSE_PAR; goto ret_null; } else if (!strncmp(token, "if", 2)) { push(IF); CScount++; command = makeCommand(command, NULL, IF_COMMAND, input, output); free(tokenPTR); command->u.command[0] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); while (*state != THEN) { if (!makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state)) { type = NOT_DEFINED; break; } } if (type == NOT_DEFINED && *state != THEN) { return NULL; } command->u.command[1] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); if (*state != ELSE && *state != FI) { // HANDLE error; ; } else if (*state == ELSE || (*state == FI && CScount)) { command->u.command[2] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); } else { command->u.command[2] = NULL; } } else if (!strncmp(token, "while", 5)) { push(WHILE); (CScount)++; command = makeCommand(command, NULL, WHILE_COMMAND, input, output); free(tokenPTR); command->u.command[0] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); if (*state != DO) { // Handle Error ; } command->u.command[1] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); if (*state != DONE) { // HANDLE error; ; } else if (*state == DONE) { if (checkRedirection(get_next_byte, get_next_byte_argument)) { fillRedirectionOperands(&command->input, &command->output, get_next_byte, get_next_byte_argument); } command->u.command[2] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); /* if (command->u.command[2]) { */ /* command_t newCommand = makeCommand(NULL, NULL, SEQUENCE_COMMAND, */ /* NULL, NULL); */ /* newCommand->u.command[0] = command->u.command[1]; */ /* newCommand->u.command[1] = command->u.command[2]; */ /* command->u.command[1] = newCommand; */ /* command->u.command[2] = NULL; */ /* } */ } else { command->u.command[2] = NULL; } } else if (!strncmp(token, "until", 5)) { push(UNTIL); (CScount)++; command = makeCommand(command, NULL, UNTIL_COMMAND, input, output); free(tokenPTR); command->u.command[0] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); if (*state != DO) { // Handle Error ; } command->u.command[1] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); if (*state != DONE) { // HANDLE error; ; } else if (*state == DONE) { if (checkRedirection(get_next_byte, get_next_byte_argument)) { fillRedirectionOperands(&command->input, &command->output, get_next_byte, get_next_byte_argument); } command->u.command[2] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); /* if (command->u.command[2]) { */ /* command_t newCommand = makeCommand(NULL, NULL, SEQUENCE_COMMAND, */ /* NULL, NULL); */ /* newCommand->u.command[0] = command->u.command[1]; */ /* newCommand->u.command[1] = command->u.command[2]; */ /* command->u.command[1] = newCommand; */ /* command->u.command[2] = NULL; */ /* } */ } else { command->u.command[2] = NULL; } } else if (!strncmp(token, "(", 1)) { CScount++; command = makeCommand(command, NULL, SUBSHELL_COMMAND, input, output); free(tokenPTR); command->u.command[0] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); if (*state != CLOSE_PAR) { // Handle Error } else if (*state == CLOSE_PAR && CScount) { command->u.command[0] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); } } else { // SIMPLE_COMMAND while (1) { STATE prevState = *state; if (isKeyWordUpdate(token, state) && (prevState == COMMAND)) { removeWhiteSpace(token); command = makeSimpleCommand(command, tokenPTR, input, output); break; } if (type == REDIRECTION1 || type == REDIRECTION2) { type = fillRedirectionOperands(&input, &output, get_next_byte, get_next_byte_argument); // command = makeSimpleCommand(command, tokenPTR, input, output); // break; // type = readNextToken(tokenPTR, &len, get_next_byte, get_next_byte_argument); } else if (type == SPACE) { appendChar(token, ' '); type = readNextToken(tokenPTR, &len, get_next_byte, get_next_byte_argument); } else if (type == NEWLINE && !CScount) { command = makeSimpleCommand(command, tokenPTR, input, output); break; } else if (type == PIPE || type == SEMICOLON || type == NEWLINE) { removeWhiteSpace(token); if (((type == PIPE) || (type == SEMICOLON)) && !strlen(token)) { printErr(); } command = makeCommand(command, tokenPTR, type == PIPE ? PIPE_COMMAND : SEQUENCE_COMMAND, input, output); command->u.command[1] = makeCommandStreamUtil(get_next_byte, get_next_byte_argument, state); if (!command->u.command[1]) { command = convertToSimple(command); } break; } *state = COMMAND; } } return command; ret_null: free(command); free(tokenPTR); return NULL; }