summaryrefslogtreecommitdiffstats
path: root/src/bvrcommands.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/bvrcommands.c')
-rw-r--r--src/bvrcommands.c195
1 files changed, 110 insertions, 85 deletions
diff --git a/src/bvrcommands.c b/src/bvrcommands.c
index b040d46..3d54cd0 100644
--- a/src/bvrcommands.c
+++ b/src/bvrcommands.c
@@ -1,7 +1,12 @@
#pragma once
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <strlcat.c>
#include <bvr.h>
#include <tape.c>
#include <bvrvar.c>
+#include <inarray.c>
int bvr_commands_check_for_command(char * input_char, char ** value, int *i, FILE * input) {
if((*input_char) == LINE_COMMAND_CHAR) {
FILE * command_return;
@@ -11,12 +16,7 @@ int bvr_commands_check_for_command(char * input_char, char ** value, int *i, FIL
fprintf(stderr, "[bvrcommands.c] bvr_commands_check_for_command: command, passed as argument, didn't return success. Going on.\n");
}
fflush(command_return);
- // fprintf(stderr, "debug: \"%s\"\n", (*value)+((*i)-1));
- // fprintf(stderr, "debug: \"%s\"\n", (*value));
- // fprintf(stderr, "debug: \"%d\"\n", (*i));
- // fprintf(stderr, "debug: \"%ld\"\n", (buf_size));
(*i) = (*i)+buf_size;
- // fprintf(stderr, "debug: \"%d\"\n", (*i));
(*input_char) = CLOSING_COMMAND_TAG_CHAR_1; // da zaključimo loop (drugače ostane notri ?)
//\\ zelo slabo. znak, ki ga najde izveden ukaz, se izgubi. rešitev bi bila dobiti zadnji input char, ki triggera break
// prejšnjega ukaza, kar pa je nemogoče.
@@ -27,7 +27,6 @@ int bvr_commands_check_for_command(char * input_char, char ** value, int *i, FIL
}
return BVR_KEEPGOING;
}
-
char bvr_var_skip_separator_chars(FILE * input) {
char input_char = fgetc(input);
while(input_char == ' ' || input_char == CLOSING_COMMAND_TAG_CHAR_1 || input_char == ',' || input_char == ';' || input_char == EOF ||
@@ -36,68 +35,105 @@ char bvr_var_skip_separator_chars(FILE * input) {
}
return input_char;
}
-int bvr_handle_get(FILE * input, FILE * output) {
- char * item = (char*) malloc(BVR_MAX_VARIABLE_SIZE+1);
+char * bvr_commands_get_value(FILE * input, char * yeetus_chars) {
+ int value_size = BVR_VALUE_CHUNK_SIZE;
+ char * value = (char*) malloc(value_size);
char input_char = bvr_var_skip_separator_chars(input);
int i = 0;
- while(input_char != ' ' && input_char != CLOSING_COMMAND_TAG_CHAR_1 && input_char != ',' && input_char != ';' && input_char != EOF &&
- input_char != '\0' && input_char != '\n' && i < BVR_MAX_VARIABLE_SIZE) {
- if(bvr_commands_check_for_command(&input_char, &item, &i, input) == BVR_CONTINUE) {
+ while(1) {
+ // i == napisali smo že toliko znakov
+ if(i >= value_size) { // <-- todo: uncomment after done debugging
+ value = realloc(value, (value_size) * BVR_VALUE_CHUNK_SIZE);
+ value_size = value_size + BVR_VALUE_CHUNK_SIZE;
+ }
+ if(char_in_array(input_char, yeetus_chars)) {
+ value[i++] = '\0';
+ return value; // or yeet!
+ }
+ if(bvr_commands_check_for_command(&(input_char), &(value), &(i), input) == BVR_CONTINUE) {
continue;
}
- item[i++] = input_char;
+ (value)[(i)++] = input_char;
input_char = fgetc(input);
}
- item[i++] = '\0';
+ // we should not get here!
+}
+int bvr_handle_substring(FILE * input, FILE * output) { // acts like https://www.php.net/manual/en/function.substr.php
+ char chars_to_break_value[69] = ",; ";
+ strlcat(chars_to_break_value, BVR_CHARS_TO_BREAK_VALUE, sizeof(chars_to_break_value));
+ char * offset = bvr_commands_get_value(input, chars_to_break_value);
+ char * length = bvr_commands_get_value(input, chars_to_break_value);
+ char * string = bvr_commands_get_value(input, BVR_CHARS_TO_BREAK_VALUE);
+
+ int return_status = SUCCESS;
+ int real_offset = atoi(offset);
+
+ if(real_offset < 0) {
+ real_offset = strlen(string)-abs(real_offset);
+ }
+
+ if(strlen(string) < real_offset) { // catch errors
+ fprintf(stderr, "[bvrcommands.c] bvr_handle_substring: string lenght (%ld) is less than offset (%d). fuck you.\n",
+ strlen(string), real_offset);
+ fprintf(output, "\nbVerbose substring: string length of %ld is less than offset of %d.\n", strlen(string), real_offset);
+ return_status = FAILURE;
+ goto cleanup_and_return;
+ }
+ if(real_offset < 0) { // catch errors
+ fprintf(stderr, "[bvrcommands.c] bvr_handle_substring: supplied negative offset after calculation (%d) is negative. failing...\n",
+ real_offset);
+ fprintf(output, "\nbVerbose substring: supplied negative offset after calculation (%d) is negative.\n", real_offset);
+ return_status = FAILURE;
+ goto cleanup_and_return;
+ }
+
+ if(atoi(length) < 0) {
+ string[strlen(string)-abs(atoi(length))] = '\0';
+ fprintf(output, "%s", string+real_offset);
+ } else {
+ fprintf(output, "%.*s", atoi(length), string+real_offset);
+ }
+
+ cleanup_and_return:
+ fflush(output);
+ free(offset);
+ free(length);
+ free(string);
+ offset = NULL;
+ length = NULL;
+ string = NULL;
+ return return_status;
+}
+int bvr_handle_get(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));
+ char * item = bvr_commands_get_value(input, chars_to_break_value);
+
fprintf(output, "%s", bvr_var_get(item));
fflush(output);
+ free(item);
+ item = NULL;
return SUCCESS;
}
int bvr_handle_set(FILE * input, FILE * output) {
- char * item = (char *) malloc(BVR_MAX_VARIABLE_SIZE+1);
- char * value = (char *) malloc(BVR_MAX_VARIABLE_SIZE+1);
- char input_char = bvr_var_skip_separator_chars(input);
- int i = 0;
- while(input_char != ' ' && input_char != CLOSING_COMMAND_TAG_CHAR_1 && input_char != ',' && input_char != ';' && input_char != EOF &&
- input_char != '\0' && input_char != '\n' && i < BVR_MAX_VARIABLE_SIZE) {
- if(bvr_commands_check_for_command(&input_char, &item, &i, input) == BVR_CONTINUE) {
- continue;
- }
- item[i++] = input_char;
- input_char = fgetc(input);
- }
- item[i++] = '\0';
- i = 0;
- input_char = bvr_var_skip_separator_chars(input);
- while(input_char != CLOSING_COMMAND_TAG_CHAR_1 && input_char != ',' && input_char != ';' && input_char != EOF &&
- input_char != '\0' && i < BVR_MAX_VARIABLE_SIZE) {
- if(bvr_commands_check_for_command(&input_char, &value, &i, input) == BVR_CONTINUE) {
- // fprintf(stderr, "debug3: \"%s\"\n", value);
- continue;
- }
- value[i++] = input_char;
- input_char = fgetc(input);
- }
- value[i++] = '\0';
- // fprintf(stderr, "debug2: \"%s\"\n", value);
- // fprintf(stderr, "debug2: \"%d\"\n", i);
- return bvr_var_set(item, value);
+ char chars_to_break_value[69] = " ";
+ strlcat(chars_to_break_value, BVR_CHARS_TO_BREAK_VALUE, sizeof(chars_to_break_value));
+ char * item = bvr_commands_get_value(input, chars_to_break_value);
+ char * value = bvr_commands_get_value(input, BVR_CHARS_TO_BREAK_VALUE);
+ int return_value = bvr_var_set(item, value);
fflush(output);
- return SUCCESS;
+ free(item);
+ free(value);
+ item = NULL;
+ value = NULL;
+ return return_value;
}
int bvr_handle_include(FILE * input, FILE * output) {
- char * item = (char *) malloc(BVR_MAX_VARIABLE_SIZE+1);
- char input_char = bvr_var_skip_separator_chars(input);
- int i = 0;
- while(input_char != ' ' && input_char != CLOSING_COMMAND_TAG_CHAR_1 && input_char != ',' && input_char != ';' && input_char != EOF &&
- input_char != '\0' && input_char != '\n' && i < BVR_MAX_VARIABLE_SIZE) {
- if(bvr_commands_check_for_command(&input_char, &item, &i, input) == BVR_CONTINUE) {
- continue;
- }
- item[i++] = input_char;
- input_char = fgetc(input);
- }
- item[i++] = '\0';
+ 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);
@@ -106,8 +142,8 @@ int bvr_handle_include(FILE * input, FILE * output) {
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);
+ 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;
}
}
@@ -124,8 +160,8 @@ int bvr_handle_include(FILE * input, FILE * output) {
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);
+ 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;
}
break;
@@ -144,37 +180,26 @@ int bvr_handle_include(FILE * input, FILE * output) {
}
*singlepath = '\0';
}
- return bvr_compose_stream(stream, output);
+ int return_status = bvr_compose_stream(stream, output);
fflush(output);
+ free(item);
+ item = NULL;
+ return return_status;
+ fflush(output); // kja?
}
int bvr_handle_move(FILE * input, FILE * output) {
- char * item = (char *) malloc(BVR_MAX_VARIABLE_SIZE+1);
- char * value = (char *) malloc(BVR_MAX_VARIABLE_SIZE+1);
- char input_char = bvr_var_skip_separator_chars(input);
- int i = 0;
- while(input_char != ' ' && input_char != CLOSING_COMMAND_TAG_CHAR_1 && input_char != ',' && input_char != ';' && input_char != EOF &&
- input_char != '\0' && input_char != '\n' && i < BVR_MAX_VARIABLE_SIZE) {
- if(bvr_commands_check_for_command(&input_char, &item, &i, input) == BVR_CONTINUE) {
- continue;
- }
- item[i++] = input_char;
- input_char = fgetc(input);
- }
- item[i++] = '\0';
- i = 0;
- input_char = bvr_var_skip_separator_chars(input);
- while(input_char != ' ' && input_char != CLOSING_COMMAND_TAG_CHAR_1 && input_char != ',' && input_char != ';' && input_char != EOF &&
- input_char != '\0' && input_char != '\n' && i < BVR_MAX_VARIABLE_SIZE) {
- if(bvr_commands_check_for_command(&input_char, &value, &i, input) == BVR_CONTINUE) {
- continue;
- }
- value[i++] = input_char;
- input_char = fgetc(input);
- }
- value[i++] = '\0';
- return bvr_var_mv(item, value);
+ char chars_to_break_value[69] = " ";
+ strlcat(chars_to_break_value, BVR_CHARS_TO_BREAK_VALUE, sizeof(chars_to_break_value));
+ char * item = bvr_commands_get_value(input, chars_to_break_value);
+ char * value = bvr_commands_get_value(input, chars_to_break_value);
+
+ int return_value = bvr_var_mv(item, value);
+ free(item);
+ free(value);
+ value = NULL;
+ item = NULL;
fflush(output);
- return SUCCESS;
+ return return_value;
}
int bvr_handle_info(FILE * input, FILE * output) {