From 034384622f8c3291e5d5f4dfb404db0073f2cc1a Mon Sep 17 00:00:00 2001 From: "Such Meme, Many Skill" Date: Tue, 31 Mar 2020 20:13:39 +0200 Subject: [PATCH] Add strings --- source/tegraexplorer/script/functions.c | 39 +++++++++- source/tegraexplorer/script/parser.c | 1 + source/tegraexplorer/script/variables.c | 98 ++++++++++++++++++++++++- source/tegraexplorer/script/variables.h | 6 +- 4 files changed, 140 insertions(+), 4 deletions(-) diff --git a/source/tegraexplorer/script/functions.c b/source/tegraexplorer/script/functions.c index e775517..a793d74 100644 --- a/source/tegraexplorer/script/functions.c +++ b/source/tegraexplorer/script/functions.c @@ -43,8 +43,23 @@ int parseJmpInput(char *in, u64 *out){ return -1; } +int parseStringInput(char *in, char **out){ + if (in[0] == '$'){ + if (str_str_find(in, out)) + return -1; + else + return 0; + } + else{ + *out = in; + return 0; + } +} + int part_printf(){ - gfx_printf(argv[0]); + char *toprint; + parseStringInput(argv[0], &toprint); + gfx_printf(toprint); gfx_printf("\n"); return 0; } @@ -121,6 +136,26 @@ int part_SetInt(){ return out; } +int part_SetString(){ + if (argv[1][0] != '$') + return -1; + str_str_add(argv[1], argv[0]); + return 0; +} + +int part_SetStringIndex(){ + int index; + char *out; + if (parseIntInput(argv[0], &index)) + return -1; + if (argv[1][0] != '$') + return -1; + if (str_str_index(index, &out)) + return -1; + str_str_add(argv[1], out); + return 0; +} + int part_goto(){ u64 target = 0; if (parseJmpInput(argv[0], &target)) @@ -137,6 +172,8 @@ str_fnc_struct functions[] = { {"check", part_Check, 3}, {"setInt", part_SetInt, 1}, {"goto", part_goto, 1}, + {"setString", part_SetString, 2}, + {"setStringIndex", part_SetStringIndex, 2}, {NULL, NULL, 0} }; diff --git a/source/tegraexplorer/script/parser.c b/source/tegraexplorer/script/parser.c index a900784..af0faa9 100644 --- a/source/tegraexplorer/script/parser.c +++ b/source/tegraexplorer/script/parser.c @@ -267,5 +267,6 @@ void tester(char *path){ f_close(&scriptin); str_int_clear(); str_jmp_clear(); + str_str_clear(); btn_wait(); } \ No newline at end of file diff --git a/source/tegraexplorer/script/variables.c b/source/tegraexplorer/script/variables.c index 86e250c..81fe63c 100644 --- a/source/tegraexplorer/script/variables.c +++ b/source/tegraexplorer/script/variables.c @@ -29,6 +29,7 @@ int str_int_add(char *key, int value){ keyvaluepair->key = key_local; keyvaluepair->value = value; keyvaluepair->next = NULL; + if (str_int_table == NULL){ str_int_table = keyvaluepair; } @@ -37,6 +38,8 @@ int str_int_add(char *key, int value){ temp = str_int_table; while (temp != NULL){ if (!strcmp(temp->key, key_local)){ + free(keyvaluepair); + free(key_local); temp->value = value; return 0; } @@ -93,13 +96,15 @@ int str_jmp_add(char *key, u64 value){ char *key_local; dict_str_loc *keyvaluepair; - utils_copystring(key, &key_local); //gfx_printf("Adding |%s|\n", key_local); - + + utils_copystring(key, &key_local); + keyvaluepair = calloc(1, sizeof(dict_str_loc)); keyvaluepair->key = key_local; keyvaluepair->value = value; keyvaluepair->next = NULL; + if (str_jmp_table == NULL){ str_jmp_table = keyvaluepair; } @@ -108,6 +113,9 @@ int str_jmp_add(char *key, u64 value){ temp = str_jmp_table; while (temp != NULL){ if (!strcmp(temp->key, key_local)){ + free(keyvaluepair); + free(key_local); + temp->value = value; return 0; } @@ -152,4 +160,90 @@ void str_jmp_clear(){ cur = next; } str_jmp_table = NULL; +} + +int str_str_add(char *key, char *value){ + char *key_local, *value_local; + dict_str_str *keyvaluepair; + //gfx_printf("Adding |%s|\n", key_local); + utils_copystring(value, &value_local); + utils_copystring(key, &key_local); + + keyvaluepair = calloc(1, sizeof(dict_str_str)); + keyvaluepair->key = key_local; + keyvaluepair->value = value_local; + keyvaluepair->next = NULL; + + if (str_str_table == NULL){ + str_str_table = keyvaluepair; + } + else { + dict_str_str *temp; + temp = str_str_table; + while (temp != NULL){ + if (!strcmp(temp->key, key_local)){ + free(keyvaluepair); + free(key_local); + + free(temp->value); + temp->value = value_local; + return 0; + } + + if (temp->next == NULL){ + temp->next = keyvaluepair; + return 0; + } + + temp = temp->next; + } + } + + return 0; +} + +int str_str_find(char *key, char **out){ + dict_str_str *temp; + temp = str_str_table; + + while (temp != NULL){ + if (!strcmp(temp->key, key)){ + *out = temp->value; + return 0; + } + temp = temp->next; + } + + return -1; +} + +int str_str_index(int index, char **out){ + dict_str_str *temp; + temp = str_str_table; + + for (int i = 0; i < (index - 1); i++){ + if (temp == NULL) + return -1; + temp = temp->next; + } + + if (temp == NULL) + return -1; + + *out = temp->value; + return 0; +} + +void str_str_clear(){ + dict_str_str *cur, *next; + cur = str_str_table; + + while (cur != NULL){ + next = cur->next; + free(cur->key); + free(cur->value); + free(cur); + cur = next; + } + str_str_table = NULL; } \ No newline at end of file diff --git a/source/tegraexplorer/script/variables.h b/source/tegraexplorer/script/variables.h index a7a7634..61c7612 100644 --- a/source/tegraexplorer/script/variables.h +++ b/source/tegraexplorer/script/variables.h @@ -25,4 +25,8 @@ void str_int_clear(); void str_int_printall(); int str_jmp_add(char *key, u64 value); int str_jmp_find(char *key, u64 *out); -void str_jmp_clear(); \ No newline at end of file +void str_jmp_clear(); +int str_str_add(char *key, char *value); +int str_str_find(char *key, char **out); +int str_str_index(int index, char **out); +void str_str_clear(); \ No newline at end of file