From 741c8e2e2ad42c4bbdf8f532d83eaf1fdcba5eff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20L=2E=20=C5=A0ijanec?= Date: Fri, 1 May 2020 21:51:26 +0200 Subject: command detection made possible --- src/tape.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 67 insertions(+), 15 deletions(-) (limited to 'src/tape.c') diff --git a/src/tape.c b/src/tape.c index 4cda521..846b96a 100644 --- a/src/tape.c +++ b/src/tape.c @@ -8,19 +8,25 @@ #include #include #include +#include #define BVR_MAX_VARIABLE_SIZE 128 #define BVR_INITIAL_VARIABLES_COUNT 128 #define SUCCESS 0 #define FAILURE -1 -#define COPY_BUFFER_SIZE 2 +#define COPY_BUFFER_SIZE 128 #define OPENING_COMMAND_TAG_LENGTH 2 #define OPENING_COMMAND_TAG_CHAR_1 '<' #define OPENING_COMMAND_TAG_CHAR_2 '@' #define CLOSING_COMMAND_TAG_CHAR_1 '@' #define CLOSING_COMMAND_TAG_CHAR_2 '>' +#define LINE_COMMENT_CHAR '#' +#define LINE_COMMAND_CHAR '?' +#define WAITING_FOR_COMMAND 8922 +#define READING_COMMAND 2343 +#define PROCESSING_COMMAND 346 char bvr_variables[BVR_INITIAL_VARIABLES_COUNT][BVR_MAX_VARIABLE_SIZE]; // char* bvr_variables[128] = malloc(BVR_INITIAL_VARIABLES_COUNT * BVR_MAX_VARIABLE_SIZE); -int bvr_variables_multiplier = 1; +// int bvr_variables_multiplier = 1; // int bvr_increase_variables() { // this can be costly https://stackoverflow.com/a/3827922 // char* more_variables = realloc(bvr_variables, ++bvr_variables_multiplier * BVR_INITIAL_VARIABLES_COUNT * sizeof(BVR_MAX_VARIABLE_SIZE)); @@ -32,22 +38,52 @@ int bvr_variables_multiplier = 1; // return SUCCESS; // } - -int bvr_command_processor(FILE * page_source_file, FILE * temp_output_file, char copy_buffer[]) { +int bvr_inline_command_processor(FILE * page_source_file, FILE * temp_output_file, char copy_buffer[]) { + int what_to_return = SUCCESS; for(int i = 1; i <= OPENING_COMMAND_TAG_LENGTH; i++) { copy_buffer[ftell(page_source_file)% COPY_BUFFER_SIZE] = fgetc(page_source_file); // remove opening command tag characters } - while(copy_buffer[(ftell(page_source_file)-1)% COPY_BUFFER_SIZE] != CLOSING_COMMAND_TAG_CHAR_1 || copy_buffer[ftell(page_source_file)% COPY_BUFFER_SIZE] != CLOSING_COMMAND_TAG_CHAR_2) { + int command_processor_status = WAITING_FOR_COMMAND; + int argument_length = 0; + while(copy_buffer[(ftell(page_source_file)-1)% COPY_BUFFER_SIZE] != CLOSING_COMMAND_TAG_CHAR_1 || + copy_buffer[ftell(page_source_file)% COPY_BUFFER_SIZE] != CLOSING_COMMAND_TAG_CHAR_2 || + command_processor_status == PROCESSING_COMMAND) { copy_buffer[(ftell(page_source_file))% COPY_BUFFER_SIZE] = fgetc(page_source_file); // advance file pointer to the end of command without rewriting. - fputc('a', temp_output_file); + // printf("received command char %c\n", copy_buffer[(ftell(page_source_file)-2)% COPY_BUFFER_SIZE]); + if(copy_buffer[(ftell(page_source_file)-2)% COPY_BUFFER_SIZE] == '?' && command_processor_status == WAITING_FOR_COMMAND) { + command_processor_status = READING_COMMAND; + continue; + } + if(command_processor_status == READING_COMMAND) { + if(++argument_length > COPY_BUFFER_SIZE) { + fprintf(temp_output_file, "\nbVerbose %d bytes till buffer overflow. Decrease argument length (%d), increase COPY_BUFFER_SIZE (%d) or take cover.\n", + COPY_BUFFER_SIZE-argument_length, argument_length, COPY_BUFFER_SIZE); + fprintf(stderr, "[tape.c] bvr_inline_command_processor: %d bytes till buffer overflow. Decrease argument length (%d), increase COPY_BUFFER_SIZE (%d) or take cover.\n", + COPY_BUFFER_SIZE-argument_length, argument_length, COPY_BUFFER_SIZE); + what_to_return = FAILURE; + } + if(copy_buffer[(ftell(page_source_file)-1)% COPY_BUFFER_SIZE] == CLOSING_COMMAND_TAG_CHAR_1 && + copy_buffer[ftell(page_source_file)% COPY_BUFFER_SIZE] == CLOSING_COMMAND_TAG_CHAR_2) { // end of arguments! + // switch (copy_buffer[(ftell(page_source_file)-(1+argument_length))]) { // switch command + // + // } + char argument_string[argument_length+1]; + strncpy(argument_string, copy_buffer+(ftell(page_source_file)-(argument_length-1)), argument_length); + argument_string[argument_length-2] = '\0'; + printf("end of command, command was %c, argument was \"%s\"\n", copy_buffer[(ftell(page_source_file)-(1+argument_length))], argument_string); + fprintf(temp_output_file, "command was %c, argument was \"%s\"\n", copy_buffer[(ftell(page_source_file)-(1+argument_length))], argument_string); + return SUCCESS; + } + } + // fputc('a', temp_output_file); if(copy_buffer[(ftell(page_source_file)-1)% COPY_BUFFER_SIZE] == EOF) { fprintf(temp_output_file, "\nbVerbose syntax error: EOF reached before closing command tag --- stopped with this bvr file\n"); - fprintf(stderr, "[tape.c] bvr_command_processor: syntax error: EOF reached before @> --- stopped with this bvr file\n"); + fprintf(stderr, "[tape.c] bvr_inline_command_processor: syntax error: EOF reached before @> --- stopped with this bvr file\n"); return FAILURE; } } copy_buffer[ftell(page_source_file)% COPY_BUFFER_SIZE] = fgetc(page_source_file); // remove closing command tag character - return SUCCESS; + return what_to_return; } int bvr_compose_page(char page_source_file_path[], int this_is_a_top_level_page, char * temp_output_path) { @@ -70,22 +106,38 @@ int bvr_compose_page(char page_source_file_path[], int this_is_a_top_level_page, while (1) { copy_buffer[ftell(page_source_file)% COPY_BUFFER_SIZE] = fgetc(page_source_file); if(copy_buffer[(ftell(page_source_file)-1)% COPY_BUFFER_SIZE] == OPENING_COMMAND_TAG_CHAR_1 && copy_buffer[ftell(page_source_file)% COPY_BUFFER_SIZE] == OPENING_COMMAND_TAG_CHAR_2) { - if(bvr_command_processor(page_source_file, temp_output_file, copy_buffer)) { - fprintf(temp_output_file, "\nbvr_command_processor returned an error status whilst composing %s\n", page_source_file_path); + if(bvr_inline_command_processor(page_source_file, temp_output_file, copy_buffer) == FAILURE) { + fprintf(temp_output_file, "\nbvr_inline_command_processor returned an error status whilst composing %s\n", page_source_file_path); + fprintf(stderr, "[tape.c] bvr_inline_command_processor returned an error status whilst composing %s\n", page_source_file_path); } - } else { - if(copy_buffer[ftell(page_source_file)% COPY_BUFFER_SIZE] == EOF) { - break; + continue; + } + if(copy_buffer[(ftell(page_source_file)-1)% COPY_BUFFER_SIZE] == '\n' && copy_buffer[ftell(page_source_file)% COPY_BUFFER_SIZE] == LINE_COMMENT_CHAR) { + copy_buffer[ftell(page_source_file)% COPY_BUFFER_SIZE] = fgetc(page_source_file); // idk, OB1 is a bitch + while(copy_buffer[(ftell(page_source_file)-1)% COPY_BUFFER_SIZE] != '\n') { // idk, OB1 is a bitch + copy_buffer[ftell(page_source_file)% COPY_BUFFER_SIZE] = fgetc(page_source_file); // idk, OB1 is a bitch } - fputc(copy_buffer[(ftell(page_source_file)-1)% COPY_BUFFER_SIZE], temp_output_file); + continue; + } + // if(copy_buffer[(ftell(page_source_file)-1)% COPY_BUFFER_SIZE] == '\n' && copy_buffer[ftell(page_source_file)% COPY_BUFFER_SIZE] == LINE_COMMAND_CHAR) { + // if(bvr_command_processor(page_source_file, temp_output_file, copy_buffer) == FAILURE) { + // fprintf(temp_output_file, "\nbvr_command_processor returned an error status whilst composing %s\n", page_source_file_path); + // fprintf(stderr, "[tape.c] bvr_command_processor returned an error status whilst composing %s\n", page_source_file_path); + // } + // continue; + // } + if(copy_buffer[ftell(page_source_file)% COPY_BUFFER_SIZE] == EOF) { + fputc('\n', temp_output_file); + break; } + fputc(copy_buffer[(ftell(page_source_file)-1)% COPY_BUFFER_SIZE], temp_output_file); } done_reading_write_file: if(this_is_a_top_level_page) { char folder_of_file[PATH_MAX]; - strcpy(folder_of_file, page_source_file_path); + strlcpy(folder_of_file, page_source_file_path, PATH_MAX); char * p; p = strrchr(folder_of_file, '/'); if (!p) { -- cgit v1.2.3