#include #include #include #include #include #include #include "apostolofShellFunctions.h" const char semicolonDelimeter[2] = ";"; const char ampersandDelimeter[2] = "&"; void interactiveMode(){ char input[BUFFER_SIZE]; int quit_called = 0; while (1){ printf(GRN "\x1B[1mfanakis_8261> \x1B[0m" RESET); //Gets user input and checks for errors if (!fgets(input, BUFFER_SIZE, stdin) || feof(stdin) || ferror(stdin)){ continue; } //Checks for input buffer overflow if ((strlen(input) == BUFFER_SIZE - 1) && input[BUFFER_SIZE - 2] != '\n'){ printf("Input too long! Please confine input to 512 characters.\n"); //Clears garbage from stdin char discarded; while ((discarded = fgetc(stdin)) != '\n' && discarded != EOF); continue; } //Trims possible leading and trailing whitespace strcpy(input, trimWhitespaces(input)); if (strcmp(input, "\n") == 0 || input == 0){ continue; } //Parses semicolon-separated chunks first unconditionalRun(input, &quit_called); if (quit_called == QUIT_CALLED){ exit(EXIT_SUCCESS); } } } void batchFileMode(char *filename){ char line[BUFFER_SIZE]; int quit_called = 0; //Opens file FILE* file = fopen(filename, "r"); if (!file){ printf("Couldn't open file.\n"); exit(EXIT_FAILURE); } //Reads file one line at a time while (fgets(line, sizeof(line), file)) { strcpy(line, trimWhitespaces(line)); if (strcmp(line, "\n") == 0 || line == 0){ continue; } unconditionalRun(line, &quit_called); if (quit_called == QUIT_CALLED){ fclose(file); exit(EXIT_SUCCESS); } } fclose(file); return; } void unconditionalRun(char *input, int *quit_called){ char *rest, *token; //Parses chunks of input split by semicolons (";") //Gets first token token = strtok_r(input, semicolonDelimeter, &rest); while (token != NULL){ //Further parses and conditionally runs chunks conditionalRun(token, quit_called); //Parses ampersand-separated chunks and runs them successively if (*quit_called == QUIT_CALLED){ return; } /*else if ((*rest == 0)){ return 1; }*/ token = strtok_r(NULL, semicolonDelimeter, &rest); } } void conditionalRun(char *input, int *quit_called){ char *rest, *token; //Parses ampersand-separated chunks //Gets first token token = strtok_r(input, ampersandDelimeter, &rest); while (token != NULL){ char *command, **argv; int argc = 0; //Parses the command parseCommand(trimWhitespaces(token), &command, &argv, &argc); if (strcmp(command, "quit") == 0){ *quit_called = QUIT_CALLED; return; } //Executes the command if (execute(command, argv)){ //Exits on failure because this is a conditional run return; } //Cleans up space allocated free(command); for (int i=0; i string && isspace( (unsigned char) *end )){ end--; } //Writes new null terminator *(end+1) = 0; return string; }