From 68d9674ca3c4ebe3d5d05ca51f746913993cd5fb Mon Sep 17 00:00:00 2001 From: TuxSH Date: Tue, 23 May 2017 17:13:43 +0200 Subject: [PATCH] Refactor error --- source/main.c | 19 +++---------------- source/utils.c | 14 ++++++++++++-- source/utils.h | 2 +- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/source/main.c b/source/main.c index 38ef998..f04fd6f 100644 --- a/source/main.c +++ b/source/main.c @@ -31,7 +31,6 @@ #include "buttons.h" #include "pin.h" #include "crypto.h" -#include "fmt.h" #include "memory.h" #include "screen.h" @@ -47,7 +46,6 @@ void main(int argc, char **argv, u32 magicWord) { bool isSafeMode = false, isNoForceFlagSet = false; - char errbuf[46]; u32 emuHeader; FirmwareType firmType; FirmwareSource nandType; @@ -58,7 +56,6 @@ void main(int argc, char **argv, u32 magicWord) for(i = 0; i < 40 && argv[0][i] != 0; i++) //Copy and convert the path to UTF-16 launchedPath[i] = argv[0][i]; launchedPath[i] = 0; - break; } else if(magicWord == 0xBABE && argc == 2) //Firmlaunch { @@ -69,14 +66,9 @@ void main(int argc, char **argv, u32 magicWord) launchedPath[i] = 0; isFirmlaunch = true; - break; } else - { - sprintf(errbuf, "Unsupported launcher or entrypoint (magic = 0x%08x, argc = %d).", magicWord, argc); - error(errbuf); - break; - } + error("Unsupported launcher or entrypoint (magic = 0x%08x, argc = %d).", magicWord, argc); if(memcmp(launchedPath, u"sdmc", 8) == 0) { @@ -98,8 +90,7 @@ void main(int argc, char **argv, u32 magicWord) mountPoint[i] = (char)launchedPath[i]; mountPoint[i] = 0; - sprintf(errbuf, "Launched from an unsupported location: %s.", mountPoint); - error(errbuf); + error("Launched from an unsupported location: %s.", mountPoint); } //Attempt to read the configuration file @@ -303,11 +294,7 @@ boot: break; } - if(res != 0) - { - sprintf(errbuf, "Failed to apply %u FIRM patch(es).", res); - error(errbuf); - } + if(res != 0) error("Failed to apply %u FIRM patch(es).", res); if(!isFirmlaunch) deinitScreens(); launchFirm(0, NULL); diff --git a/source/utils.c b/source/utils.c index 7a283cc..978806f 100644 --- a/source/utils.c +++ b/source/utils.c @@ -30,6 +30,9 @@ #include "screen.h" #include "draw.h" #include "cache.h" +#include "fmt.h" + +#include static void startChrono(void) { @@ -104,14 +107,21 @@ void wait(u64 amount) while(chrono() < amount); } -void error(const char *message) +void error(const char *fmt, ...) { if(!isFirmlaunch) { + char buf[DRAW_MAX_FORMATTED_STRING_SIZE + 1]; + + va_list args; + va_start(args, fmt); + vsprintf(buf, fmt, args); + va_end(args); + initScreens(); drawString(true, 10, 10, COLOR_RED, "An error has occurred:"); - u32 posY = drawString(true, 10, 30, COLOR_WHITE, message); + u32 posY = drawString(true, 10, 30, COLOR_WHITE, buf); drawString(true, 10, posY + 2 * SPACING_Y, COLOR_WHITE, "Press any button to shutdown"); waitInput(false); diff --git a/source/utils.h b/source/utils.h index 96fe265..9533dfc 100644 --- a/source/utils.h +++ b/source/utils.h @@ -35,4 +35,4 @@ u32 waitInput(bool isMenu); void mcuPowerOff(void); void wait(u64 amount); -void error(const char *message); \ No newline at end of file +void error(const char *fmt, ...);