summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsijanec <anton@sijanec.eu>2021-01-19 20:23:45 +0100
committersijanec <anton@sijanec.eu>2021-01-19 20:23:45 +0100
commit59b099ee82477ffd8ad12948dd4601234563d0e4 (patch)
tree4cc421c4ac8a0c069beaca7079b3da75bd824b8f
parentadded an abstraction of accessing variables through file streams - possible to make functions (diff)
downloadbverbose-59b099ee82477ffd8ad12948dd4601234563d0e4.tar
bverbose-59b099ee82477ffd8ad12948dd4601234563d0e4.tar.gz
bverbose-59b099ee82477ffd8ad12948dd4601234563d0e4.tar.bz2
bverbose-59b099ee82477ffd8ad12948dd4601234563d0e4.tar.lz
bverbose-59b099ee82477ffd8ad12948dd4601234563d0e4.tar.xz
bverbose-59b099ee82477ffd8ad12948dd4601234563d0e4.tar.zst
bverbose-59b099ee82477ffd8ad12948dd4601234563d0e4.zip
-rwxr-xr-xbin/bvr-compose-htmlbin36672 -> 36712 bytes
-rwxr-xr-xbin/bvr-compose-singlebin36528 -> 36568 bytes
-rw-r--r--src/bvr.h10
-rw-r--r--src/bvrcommands.c11
-rw-r--r--src/bvrvar.c87
-rw-r--r--test/tape-test.bvr4
6 files changed, 75 insertions, 37 deletions
diff --git a/bin/bvr-compose-html b/bin/bvr-compose-html
index afd91ab..091dc4a 100755
--- a/bin/bvr-compose-html
+++ b/bin/bvr-compose-html
Binary files differ
diff --git a/bin/bvr-compose-single b/bin/bvr-compose-single
index 954c306..c68c60f 100755
--- a/bin/bvr-compose-single
+++ b/bin/bvr-compose-single
Binary files differ
diff --git a/src/bvr.h b/src/bvr.h
index abcd0cf..185b78f 100644
--- a/src/bvr.h
+++ b/src/bvr.h
@@ -24,12 +24,18 @@
#define THE_VOID "/dev/null"
#define BVR_INITIAL_VARIABLES_COUNT 128
-#define BVR_MAX_VARIABLE_SIZE 128
#define BVR_UNDEFINED "BVR_UNDEFINED"
#define BVR_ARRAY_INDEX_CHAR '['
#define BVR_ARRAY_AFTER_INDEX "]"
-char bvr_variables[BVR_INITIAL_VARIABLES_COUNT*2][BVR_MAX_VARIABLE_SIZE];
+struct bvr_variable {
+ char * k; /* ey */
+ char * v; /* alue */
+ size_t sv; /* _izeof_alue */
+ size_t sk; /* _izeof_key */
+};
+size_t bvr_variables_count = BVR_INITIAL_VARIABLES_COUNT;
+struct bvr_variable * bvr_variables;
int bvr_bvrvar_first_time_set = 1;
#define BVR_VER_MAJOR 0
diff --git a/src/bvrcommands.c b/src/bvrcommands.c
index a44954f..a1738dc 100644
--- a/src/bvrcommands.c
+++ b/src/bvrcommands.c
@@ -449,10 +449,13 @@ int bvr_handle_join(FILE * input, FILE * output) {
char * value = bvr_commands_get_value(input, chars_to_break_value);
char * stvar1 = bvr_var_get(item);
char * stvar2 = bvr_var_get(value);
- strncat(stvar1, stvar2, (BVR_MAX_VARIABLE_SIZE-strlen(stvar1))-1);
- return_value = bvr_var_set(item, stvar1);
+ char * temp = malloc(sizeof(char)*(strlen(stvar1)+strlen(stvar2)+1));
+ sprintf(temp, "%s%s", stvar1, stvar2);
+ return_value = bvr_var_set(item, temp);
free(item);
free(value);
+ free(temp);
+ temp = NULL;
item = NULL;
value = NULL;
return return_value;
@@ -517,12 +520,12 @@ int bvr_handle_explode(FILE * input, FILE * output) {
index = 0;
while ((token = strtok_r(rest, string2, &rest))) {
charpointer = strrchr(item, BVR_ARRAY_INDEX_CHAR);
- snprintf(charpointer+1, (BVR_MAX_VARIABLE_SIZE-(charpointer-string1))-4, "%d" BVR_ARRAY_AFTER_INDEX, index++);
+ sprintf(charpointer+1, "%d" BVR_ARRAY_AFTER_INDEX, index++); /* keys must always have 128 bytes more space allocated */
return_value = return_value != SUCCESS ? return_value : bvr_var_set(item, token); // če je bila prej napaka pač ne poskušamo več!
len++;
}
charpointer = strrchr(item, BVR_ARRAY_INDEX_CHAR);
- snprintf(charpointer+1, (BVR_MAX_VARIABLE_SIZE-(charpointer-string1))-4, "#" BVR_ARRAY_AFTER_INDEX, index++);
+ sprintf(charpointer+1, "#" BVR_ARRAY_AFTER_INDEX, index++);
snprintf(lenst, 69-1, "%lu", len);
return_value = return_value != SUCCESS ? return_value : bvr_var_set(item, lenst);
free(item);
diff --git a/src/bvrvar.c b/src/bvrvar.c
index 9e405ec..c5ead88 100644
--- a/src/bvrvar.c
+++ b/src/bvrvar.c
@@ -2,52 +2,77 @@
#include <bvr.h>
#include <string.h>
#include <stdlib.h>
+#define BVR_VAR_FIRST_TIME() \
+ if(bvr_bvrvar_first_time_set == 1) { \
+ bvr_variables = malloc(sizeof(struct bvr_variable)*bvr_variables_count); \
+ for(int i = 0; i < bvr_variables_count; i++) { \
+ bvr_variables[i].v = malloc(sizeof(char)*128); \
+ strcpy(bvr_variables[i].v, BVR_UNDEFINED); \
+ bvr_variables[i].sv = 128; \
+ bvr_variables[i].k = malloc(sizeof(char)*128); \
+ strcpy(bvr_variables[i].k, BVR_UNDEFINED); \
+ bvr_variables[i].sk = 128; \
+ } \
+ bvr_bvrvar_first_time_set = 0; \
+ }
char * bvr_var_get(char * item) {
- for(int i = 0; i < sizeof(bvr_variables)/sizeof(bvr_variables[0]); i=i+2) {
- // printf("%s, %s, %d, %d\n", bvr_variables[i], item, sizeof(bvr_variables)/sizeof(bvr_variables[0]), i);
- if(strcmp(bvr_variables[i], item) == 0) {
- return bvr_variables[i+1];
+ BVR_VAR_FIRST_TIME();
+ for(int i = 0; i < bvr_variables_count; i++) {
+ // printf("%s, %s, %d, %d\n", bvr_variables[i].v, item, bvr_variables_count, i);
+ if(strcmp(bvr_variables[i].k, item) == 0) {
+ return bvr_variables[i].v;
}
+ // fprintf(stderr, "wa\n");
}
return BVR_UNDEFINED;
}
int bvr_var_set(char * item, char * value) {
- if(bvr_bvrvar_first_time_set == 1) {
- for(int i = 0; i < (sizeof(bvr_variables)/sizeof(bvr_variables[0])); i=i+2) {
- // printf("loop here1\n");
- strlcpy(bvr_variables[i], BVR_UNDEFINED, sizeof(bvr_variables[i]));
- }
- bvr_bvrvar_first_time_set = 0;
- }
- if(strlen(value) >= BVR_MAX_VARIABLE_SIZE) { // >=, ker je še \0, ki ga strlen ne prišteje!
- value[BVR_MAX_VARIABLE_SIZE-1] = '\0';
- fprintf(stderr, "[bvrvar.c] bvr_set: value of variable %s too long, chopped to \"%s\"; increase BVR_MAX_VARIABLE_SIZE (%d). Returning FAILURE and setting anyways.\n",
- item, value, BVR_MAX_VARIABLE_SIZE);
- }
- for(int i = 0; i < (sizeof(bvr_variables)/sizeof(bvr_variables[0])); i=i+2) {
- // printf("loop here2\n");
- if(strcmp(bvr_variables[i], item) == 0) {
- strlcpy(bvr_variables[i+1], value, sizeof(bvr_variables[i+1]));
- return SUCCESS;
- }
- } // could already search for BVR_UNDEFINED here, but idc
- for(int i = 0; i < (sizeof(bvr_variables)/sizeof(bvr_variables[0])); i=i+2) {
+ BVR_VAR_FIRST_TIME();
+ int freevar = -69420;
+ for(int i = 0; i < bvr_variables_count; i++) {
// printf("loop here4\n");
- if(strcmp(bvr_variables[i], BVR_UNDEFINED) == 0) {
- strlcpy(bvr_variables[i], item, sizeof(bvr_variables[i]));
- strlcpy(bvr_variables[i+1], value, sizeof(bvr_variables[i+1]));
+ if (strcmp(bvr_variables[i].v, BVR_UNDEFINED) == 0) {
+ freevar = i;
+ }
+ if(strcmp(bvr_variables[i].k, item) == 0 || i+1 == bvr_variables_count) {
+ if (i+1 == bvr_variables_count && strcmp(bvr_variables[i].k, item) != 0) {
+ i = freevar;
+ if (i == -69420) {
+ fprintf(stderr, "[bvrvar.c] bvr_set: no more space on the variable stack for %s. Increase BVR_INITIAL_VARIABLES_COUNT (%d).\n", item, BVR_INITIAL_VARIABLES_COUNT);
+ return FAILURE;
+ }
+ }
+ if (bvr_variables[i].sk > strlen(item)) {
+ bvr_variables[i].sk = strlen(item)+128;
+ free(bvr_variables[i].k);
+ bvr_variables[i].k = malloc(sizeof(char)*bvr_variables[i].sk);
+ }
+ if (bvr_variables[i].sv > strlen(item)) {
+ bvr_variables[i].sv = strlen(value)+128;
+ free(bvr_variables[i].v);
+ bvr_variables[i].v = malloc(sizeof(char)*bvr_variables[i].sv);
+ }
+ strlcpy(bvr_variables[i].k, item, bvr_variables[i].sk);
+ strlcpy(bvr_variables[i].v, value, bvr_variables[i].sv);
+ // fprintf(stderr, "debug: %s\n", bvr_variables[i].v);
return SUCCESS;
}
}
- fprintf(stderr, "[bvrvar.c] bvr_set: no more space on the variable stack for %s. Increase BVR_INITIAL_VARIABLES_COUNT (%d).\n", item, BVR_INITIAL_VARIABLES_COUNT);
+ fprintf(stderr, "undefined condition in bvr_var_set.\n");
return FAILURE;
}
int bvr_var_mv(char * item, char * newname) {
- for(int i = 0; i < (sizeof(bvr_variables)/sizeof(bvr_variables[0])); i=i+2) {
- if(strcmp(bvr_variables[i], item) == 0) {
- strlcpy(bvr_variables[i], newname, sizeof(bvr_variables[i]));
+ BVR_VAR_FIRST_TIME();
+ for(int i = 0; i < bvr_variables_count; i=i+2) {
+ if(strcmp(bvr_variables[i].k, item) == 0) {
+ if (bvr_variables[i].sk > strlen(newname)) {
+ bvr_variables[i].sk = strlen(newname)+128;
+ free(bvr_variables[i].k);
+ bvr_variables[i].k = malloc(sizeof(char)*bvr_variables[i].sk);
+ }
+ strlcpy(bvr_variables[i].k, newname, bvr_variables[i].sk);
return SUCCESS;
}
}
diff --git a/test/tape-test.bvr b/test/tape-test.bvr
index 4d53690..52b8664 100644
--- a/test/tape-test.bvr
+++ b/test/tape-test.bvr
@@ -1,3 +1,4 @@
+yeet
<@?i assets/content/global.bvr @>
<@?s testing krneki@>
# <@?u -2 -1 ?g testing @>
@@ -14,3 +15,6 @@
<@?s z {@>
<@?s k }@>
<@?i bvr://v/function_that_prints_array_elements_backwards@>
+<@?m ?"a very long string should surpass 128 characters. this should trigger the automatic or, in other words, dynamic memory allocation that should extend the variable space to support such a long string. yup, that's enough of text." longstring@>
+<@?g longstring@>
+<@?g longstring@>