From 30afcddd3a2565f8651a50653e1178eadfbb8dde Mon Sep 17 00:00:00 2001 From: "Such Meme, Many Skill" Date: Tue, 31 Mar 2020 15:34:19 +0200 Subject: [PATCH] more functions, cleanup and simple error checking --- source/tegraexplorer/common/common.h | 3 +- source/tegraexplorer/common/strings.c | 3 +- source/tegraexplorer/gfx/gfxutils.c | 2 +- source/tegraexplorer/script/functions.c | 75 ++++++++++++++-- source/tegraexplorer/script/parser.c | 114 +++++------------------- 5 files changed, 96 insertions(+), 101 deletions(-) diff --git a/source/tegraexplorer/common/common.h b/source/tegraexplorer/common/common.h index 31c7b61..d163e58 100644 --- a/source/tegraexplorer/common/common.h +++ b/source/tegraexplorer/common/common.h @@ -20,7 +20,8 @@ enum utils_err_codes_te_call { ERR_EMMC_READ_FAILED, ERR_EMMC_WRITE_FAILED, ERR_FILE_TOO_BIG_FOR_DEST, - ERR_SD_EJECTED + ERR_SD_EJECTED, + ERR_PARSE_FAIL }; extern const char *utils_err_codes_te[]; diff --git a/source/tegraexplorer/common/strings.c b/source/tegraexplorer/common/strings.c index 6271bf7..5d25aee 100644 --- a/source/tegraexplorer/common/strings.c +++ b/source/tegraexplorer/common/strings.c @@ -49,7 +49,8 @@ const char *utils_err_codes_te[] = { // these start at 50 "EMMC READ FAILED", "EMMC WRITE FAILED", "FILE TOO BIG FOR DEST", - "SD EJECTED" + "SD EJECTED", + "PARSING FAILED" }; const char *pkg2names[] = { diff --git a/source/tegraexplorer/gfx/gfxutils.c b/source/tegraexplorer/gfx/gfxutils.c index 144b126..4c60510 100644 --- a/source/tegraexplorer/gfx/gfxutils.c +++ b/source/tegraexplorer/gfx/gfxutils.c @@ -54,7 +54,7 @@ int gfx_errDisplay(char *src_func, int err, int loc){ if (err < 15) gfx_printf("Desc: %s\n", utils_err_codes[err]); - else if (err >= ERR_SAME_LOC && err <= ERR_SD_EJECTED) + else if (err >= ERR_SAME_LOC && err <= ERR_PARSE_FAIL) gfx_printf("Desc: %s\n", utils_err_codes_te[err - 50]); if (loc) diff --git a/source/tegraexplorer/script/functions.c b/source/tegraexplorer/script/functions.c index fb8515b..1ec84b9 100644 --- a/source/tegraexplorer/script/functions.c +++ b/source/tegraexplorer/script/functions.c @@ -21,20 +21,35 @@ extern FIL scriptin; extern char **argv; extern u32 argc; +int parseIntInput(char *in, int *out){ + if (in[0] == '@'){ + if (str_int_find(argv[0], out)) + return -1; + } + else + *out = atoi(in); + + return 0; +} + int part_printf(){ gfx_printf(argv[0]); gfx_printf("\n"); return 0; } +int part_print_int(){ + int toprint; + if (parseIntInput(argv[0], &toprint)) + return -1; + gfx_printf("%s: %d\n", argv[0], toprint); + return 0; +} + int part_if(){ int condition; - if (argv[0][0] == '@'){ - if (str_int_find(argv[0], &condition)) - return -10; - } - else - condition = atoi(argv[0]); + if (parseIntInput(argv[0], &condition)) + return -1; getfollowingchar('{'); @@ -46,9 +61,55 @@ int part_if(){ } } +int part_Math(){ + int left, right; + if (parseIntInput(argv[0], &left)) + return -1; + if (parseIntInput(argv[2], &right)) + return -1; + + switch (argv[1][0]){ + case '+': + return left + right; + case '-': + return left - right; + case '*': + return left * right; + case '/': + return left * right; + } + return 0; +} + +int part_Check(){ + int left, right; + if (parseIntInput(argv[0], &left)) + return -1; + if (parseIntInput(argv[2], &right)) + return -1; + + if (!strcmp(argv[1], "==")) + return (left == right); + else if (!strcmp(argv[1], "!=")) + return (left != right); + else if (!strcmp(argv[1], ">=")) + return (left >= right); + else if (!strcmp(argv[1], "<=")) + return (left <= right); + else if (!strcmp(argv[1], ">")) + return (left > right); + else if (!strcmp(argv[1], "<")) + return (left < right); + else + return -1; +} + str_fnc_struct functions[] = { {"printf", part_printf, 1}, + {"print_int", part_print_int, 1}, {"if", part_if, 1}, + {"math", part_Math, 3}, + {"check", part_Check, 3}, {NULL, NULL, 0} }; @@ -59,6 +120,8 @@ int run_function(char *func_name, int *out){ return -2; *out = functions[i].value(); + if (*out < 0) + return -1; return 0; } } diff --git a/source/tegraexplorer/script/parser.c b/source/tegraexplorer/script/parser.c index 863d740..d6aa5dc 100644 --- a/source/tegraexplorer/script/parser.c +++ b/source/tegraexplorer/script/parser.c @@ -111,36 +111,8 @@ char *makestr(u32 size, char ignore){ return str; } -/* -char *makestrstrip(u32 size){ - char *str; - int count = 0; - str = calloc(size + 1, sizeof(char)); - for (u32 i = 0; i < size;){ - if (currentchar == '"'){ - getnextchar(); - i++; - while (getnextchar() != '"') - i++; - } - else if (currentchar == ' '){ - while (getnextchar() == ' ') - i++; - } - else{ - getnextchar(); - i++; - } - - str[count++] = currentchar; - } - - return str; -} -*/ - -char *readtilchar(char end, char ignore){ // this will strip spaces unless it's enclosed in a "" +char *readtilchar(char end, char ignore){ FSIZE_t offset, size; offset = f_tell(&scriptin); @@ -161,52 +133,17 @@ void functionparser(){ if (funcbuff != NULL) free(funcbuff); - //gfx_printf("getting func %c\n", currentchar); funcbuff = readtilchar('(', ' '); - /*calloc(20, sizeof(char)); - for (int i = 0; i < 19 && currentchar != '(' && currentchar != ' '; i++){ - funcbuff[i] = currentchar; - getnextchar(); - } - */ getfollowingchar('('); getnextchar(); - //gfx_printf("getting arg len %c\n", currentchar); - - /* - fileoffset = f_tell(&scriptin); - getfollowingchar(')'); - argsize = f_tell(&scriptin) - fileoffset; - f_lseek(&scriptin, fileoffset - 1); - - getnextchar(); - - gfx_printf("getting args %c\n", currentchar); - - unsplitargs = calloc(argsize + 1, sizeof(char)); - for (int i = 0; i < argsize; i++){ - unsplitargs[i] = currentchar; - getnextchar(); - } - */ unsplitargs = readtilchar(')', 0); - //gfx_printf("parsing args %c\n", currentchar); argc = splitargs(unsplitargs); getnextchar(); getnextchar(); - /* - gfx_printf("\n\nFunc: %s\n", funcbuff, currentchar); - gfx_printf("Split: %s\n", unsplitargs); - - for (int i = 0; i < argc; i++) - gfx_printf("%d | %s\n", i, argv[i]); - */ - - //gfx_printf("\ncurrent char: %c", currentchar); free(unsplitargs); } @@ -215,24 +152,6 @@ char *gettargetvar(){ FSIZE_t fileoffset; u32 varsize = 0; - /* - fileoffset = f_tell(&scriptin); - getfollowingchar('='); - varsize = f_tell(&scriptin) - fileoffset; - f_lseek(&scriptin, fileoffset - 1); - - variable = calloc(varsize + 1, sizeof(char)); - - getnextchar(); - for (int i = 0; i < varsize; i++){ - if (currentchar == ' ') - break; - - variable[i] = currentchar; - getnextchar(); - } - */ - variable = readtilchar('=', ' '); getfollowingchar('='); @@ -263,17 +182,19 @@ void mainparser(){ } functionparser(); - /* - if (variable != NULL) - gfx_printf("target: %s\n", variable); - */ - //gfx_printf("Func: %s\n", funcbuff); res = run_function(funcbuff, &out); - str_int_add("@RESULT", res); + if (res < 0){ + printerrors = true; + gfx_errDisplay("mainparser", ERR_PARSE_FAIL, 0); + forceExit = true; + } + else { + str_int_add("@RESULT", out); - if (variable != NULL) - str_int_add(variable, res); + if (variable != NULL) + str_int_add(variable, out); + } //gfx_printf("\nGoing to next func %c\n", currentchar); free(variable); @@ -297,7 +218,7 @@ void skipbrackets(){ void tester(char *path){ int res; - + forceExit = false; gfx_clearscreen(); res = f_open(&scriptin, path, FA_READ | FA_OPEN_EXISTING); @@ -315,10 +236,19 @@ void tester(char *path){ printerrors = false; - while (!f_eof(&scriptin)){ + while (!f_eof(&scriptin) && !forceExit){ mainparser(); } + if (funcbuff != NULL) + free(funcbuff); + + if (argv != NULL) { + for (int i = 0; argv[i] != NULL; i++) + free(argv[i]); + free(argv); + } + printerrors = true; //str_int_printall();