From 69c8904b2d9a325ab2a8f71c28cbaa34bb830887 Mon Sep 17 00:00:00 2001 From: sijanec Date: Tue, 19 Jan 2021 15:26:05 +0100 Subject: added an abstraction of accessing variables through file streams - possible to make functions --- src/bvrcommands.c | 100 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 63 insertions(+), 37 deletions(-) (limited to 'src/bvrcommands.c') diff --git a/src/bvrcommands.c b/src/bvrcommands.c index b7d14e3..a44954f 100644 --- a/src/bvrcommands.c +++ b/src/bvrcommands.c @@ -180,56 +180,82 @@ int bvr_handle_set(FILE * input, FILE * output) { int bvr_handle_include(FILE * input, FILE * output) { char chars_to_break_value[69] = " "; strlcat(chars_to_break_value, BVR_CHARS_TO_BREAK_VALUE, sizeof(chars_to_break_value)); - // fprintf(stderr, "debug: \"%s\"\n", chars_to_break_value); char * item = bvr_commands_get_value(input, chars_to_break_value); - - FILE * stream = fopen(item, "r"); - char notgoodatnamingvariables[PATH_MAX]; - char * path = bvr_var_get(BVR_INCLUDE_PATH_VAR_NAME); - if(stream == NULL) { - strcpy(notgoodatnamingvariables, item); - strcat(notgoodatnamingvariables, BVR_COMMAND_FILE_EXT); - stream = fopen(notgoodatnamingvariables, "r"); - if(strcmp(path, BVR_UNDEFINED) == 0 && stream == NULL) { - fprintf(output, "\nbVerbose include error. File \"%s\" not found. Path is undefined.\n", item); - fprintf(stderr, "[bvrcommands.c] bvr_handle_include: File \"%s\" not found. Path is undefined.\n", item); - return FAILURE; + FILE * stream; + char * cp; + int return_status = SUCCESS; + // fprintf(stderr, "debug: %s\n", item); + if (strncmp(item, "bvr://", strlen("bvr://")) == 0) { + switch (item[6]) { + case 'v': /* ar */ + cp = strchr(item+6, '/'); + if (cp == NULL) + goto sike_this_actually_is_a_file; + cp = bvr_var_get(cp+1); + if (cp == NULL || 0 == strncmp(cp, "BVR_UNDEFINED", strlen("BVR_UNDEFINED"))) + goto sike_this_actually_is_a_file; + stream = fmemopen(cp, strlen(cp), "r"); + if (stream == NULL) + goto sike_this_actually_is_a_file; + break; + default: + goto sike_this_actually_is_a_file; + break; /* unreachable "code" */ } - } - char * singlepath; - while(stream == NULL) { - singlepath = strrchr(path, BVR_PATH_SEPARATOR); - if(singlepath == NULL) { - strcpy(notgoodatnamingvariables, path); - strcat(notgoodatnamingvariables, item); - stream = fopen(notgoodatnamingvariables, "r"); // ob1 fuckery - if(stream == NULL) { + } else { + sike_this_actually_is_a_file: + stream = fopen(item, "r"); + char notgoodatnamingvariables[PATH_MAX]; + char * path = bvr_var_get(BVR_INCLUDE_PATH_VAR_NAME); + if(stream == NULL) { + strcpy(notgoodatnamingvariables, item); + strcat(notgoodatnamingvariables, BVR_COMMAND_FILE_EXT); + stream = fopen(notgoodatnamingvariables, "r"); + if(strcmp(path, BVR_UNDEFINED) == 0 && stream == NULL) { + fprintf(output, "\nbVerbose include error. File \"%s\" not found. Path is undefined.\n", item); + fprintf(stderr, "[bvrcommands.c] bvr_handle_include: File \"%s\" not found. Path is undefined.\n", item); + return_status = FAILURE; + goto returncleanly; + } + } + char * singlepath; + while(stream == NULL) { + singlepath = strrchr(path, BVR_PATH_SEPARATOR); + if(singlepath == NULL) { strcpy(notgoodatnamingvariables, path); strcat(notgoodatnamingvariables, item); - strcat(notgoodatnamingvariables, BVR_COMMAND_FILE_EXT); stream = fopen(notgoodatnamingvariables, "r"); // ob1 fuckery if(stream == NULL) { - fprintf(output, "\nbVerbose include error. File \"%s\" not found.\n", item); - fprintf(stderr, "[bvrcommands.c] bvr_handle_include: File \"%s\" not found.\n", item); - return FAILURE; + strcpy(notgoodatnamingvariables, path); + strcat(notgoodatnamingvariables, item); + strcat(notgoodatnamingvariables, BVR_COMMAND_FILE_EXT); + stream = fopen(notgoodatnamingvariables, "r"); // ob1 fuckery + if(stream == NULL) { + fprintf(output, "\nbVerbose include error. File \"%s\" not found.\n", item); + fprintf(stderr, "[bvrcommands.c] bvr_handle_include: File \"%s\" not found.\n", item); + return_status = FAILURE; + goto returncleanly; + } + break; } break; } - break; - } - strcpy(notgoodatnamingvariables, singlepath); - strcat(notgoodatnamingvariables, item); - stream = fopen(notgoodatnamingvariables+1, "r"); // ob1 fuckery - if(stream == NULL) { - strcat(notgoodatnamingvariables, BVR_COMMAND_FILE_EXT); + strcpy(notgoodatnamingvariables, singlepath); + strcat(notgoodatnamingvariables, item); stream = fopen(notgoodatnamingvariables+1, "r"); // ob1 fuckery - if(stream != NULL) { - break; + if(stream == NULL) { + strcat(notgoodatnamingvariables, BVR_COMMAND_FILE_EXT); + stream = fopen(notgoodatnamingvariables+1, "r"); // ob1 fuckery + if(stream != NULL) { + break; + } } + *singlepath = '\0'; } - *singlepath = '\0'; } - int return_status = bvr_compose_stream(stream, output); + return_status = bvr_compose_stream(stream, output); + fclose(stream); + returncleanly: fflush(output); free(item); item = NULL; -- cgit v1.2.3