From 7d16fc17c1dde35395455feaf4475f1300c16aa8 Mon Sep 17 00:00:00 2001 From: Alekmaul Date: Mon, 11 Nov 2024 05:59:50 +0100 Subject: [PATCH] feat: update output display --- tools/snestools/errors.c | 91 +++++++++++++++++++++++++++++++++++++ tools/snestools/errors.h | 12 +++++ tools/snestools/snestools.c | 68 +++++++++++++-------------- 3 files changed, 134 insertions(+), 37 deletions(-) create mode 100644 tools/snestools/errors.c create mode 100644 tools/snestools/errors.h diff --git a/tools/snestools/errors.c b/tools/snestools/errors.c new file mode 100644 index 000000000..0e568d67d --- /dev/null +++ b/tools/snestools/errors.c @@ -0,0 +1,91 @@ +/*--------------------------------------------------------------------------------- + + Copyright (C) 2012-2024 + Alekmaul + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any + damages arising from the use of this software. + + Permission is granted to anyone to use this software for any + purpose, including commercial applications, and to alter it and + redistribute it freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you + must not claim that you wrote the original software. If you use + this software in a product, an acknowledgment in the product + documentation would be appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and + must not be misrepresented as being the original software. + 3. This notice may not be removed or altered from any source + distribution. + + Header checker / modifier for snes. + Some parts are based on Snes mess driver. + +---------------------------------------------------------------------------------*/ +#include +#include +#include + +#include "errors.h" + +#define ERRORRED(STRING) "\x1B[31m" STRING "\033[0m" +#define ERRORPINK(STRING) "\x1B[35m" STRING "\033[0m" +#define ERRORBRIGHT(STRING) "\x1B[97m" STRING "\033[0m" + +//------------------------------------------------------------------------------------------------- +// Print an info message - output produced, that's all +void info (const char *format, ...) +{ + va_list ap; + + va_start (ap, format); + fprintf (stdout, "%s: ", ERRORBRIGHT("snestools")); + vfprintf (stdout, format, ap); + va_end (ap); + fputc ('\n', stdout); + fflush(stdout); +} + +//------------------------------------------------------------------------------------------------- +// Print a warning message - output produced, but there may be problems. +void warning (const char *format, ...) +{ + va_list ap; + + va_start (ap, format); + fprintf (stderr, "%s: " ERRORPINK("warning") ": ", ERRORBRIGHT("snestools")); + vfprintf (stderr, format, ap); + va_end (ap); + fputc ('\n', stderr); + fflush(stderr); +} + +//------------------------------------------------------------------------------------------------- +// Print an error message - output produced, but terminate execution is elsewhere. +void errorcontinue (const char *format, ...) +{ + va_list ap; + + va_start (ap, format); + fprintf (stderr, "%s: " ERRORRED("error") ": ", ERRORBRIGHT("snestools")); + vfprintf (stderr, format, ap); + va_end (ap); + fputc ('\n', stderr); + fflush(stderr); +} + +//------------------------------------------------------------------------------------------------- +// Fatal error - terminate execution immediately. Does not return. +void fatal (const char *format, ...) +{ + va_list ap; + + va_start (ap, format); + fprintf (stderr, "%s: " ERRORRED("fatal error") ": ", ERRORBRIGHT("snestools")); + vfprintf (stderr, format, ap); + va_end (ap); + fputc ('\n', stderr); + exit (EXIT_FAILURE); +} \ No newline at end of file diff --git a/tools/snestools/errors.h b/tools/snestools/errors.h new file mode 100644 index 000000000..b57484c37 --- /dev/null +++ b/tools/snestools/errors.h @@ -0,0 +1,12 @@ + +#ifndef _SNESTOOLS_ERRORS_H +#define _SNESTOOLS_ERRORS_H + +//------------------------------------------------------------------------------------------------- +extern void info (const char *format, ...); +extern void warning (const char *format, ...); +extern void fatal (const char *, ...); +extern void errorcontinue (const char *format, ...); + +#endif + diff --git a/tools/snestools/snestools.c b/tools/snestools/snestools.c index 35188f677..377df70e2 100644 --- a/tools/snestools/snestools.c +++ b/tools/snestools/snestools.c @@ -1,6 +1,6 @@ /*--------------------------------------------------------------------------------- - Copyright (C) 2012-2021 + Copyright (C) 2012-2024 Alekmaul This software is provided 'as-is', without any express or implied @@ -31,6 +31,8 @@ #include #include +#include "errors.h" + // DEFINES #define SNESTOOLSVERSION __BUILD_VERSION #define SNESTOOLSDATE __BUILD_DATE @@ -608,8 +610,8 @@ int show_header(char *filename, FILE *fp) // read the header if (fread(&snesheader, sizeof(snes_header), 1, fp) < 1) { - printf("\nsnestools: error 'Header (loRom) of file [%s] is not correct'\n", filename); - exit(1); + printf("snestools: error 'Header (loRom) of file [%s] is not correct'\n", filename); + exit(EXIT_FAILURE); } // a valid checksum is the biggest indicator of a valid header to check lo or Hi rom. @@ -624,15 +626,15 @@ int show_header(char *filename, FILE *fp) // read the header if (fread(&snesheader, sizeof(snes_header), 1, fp) < 1) { - printf("\nsnestools: error 'Header (hiRom) of file [%s] is not correct'\n", filename); - exit(1); + printf("snestools: error 'Header (hiRom) of file [%s] is not correct'\n", filename); + exit(EXIT_FAILURE); } if ((snesheader.checksum + snesheader.checksum_c) == 0xffff && (snesheader.checksum != 0) && (snesheader.checksum_c != 0)) valid_check = 1; if (!valid_check) { - printf("\nsnestools: error 'No valid Header (hiRom or loRom) for file [%s]'\n", filename); - exit(1); + printf("snestools: error 'No valid Header (hiRom or loRom) for file [%s]'\n", filename); + exit(EXIT_FAILURE); } } @@ -719,7 +721,7 @@ int skip_header(char *filename, FILE *fp) // Check for a header (512 bytes) if (fread(&header, sizeof(header), 1, fp) < 1) { - printf("\nsnestools: error 'File [%s] has no correct header'\n", filename); + errorcontinue("File [%s] has no correct header", filename); return 0; } @@ -727,28 +729,28 @@ int skip_header(char *filename, FILE *fp) { // Found an SWC identifier if (quietmode == 0) - printf("\nsnestools: 'File [%s] has header (SWC)'\n", filename); + info("File [%s] has header (SWC)", filename); rom_has_header = 1; } else if ((header[0] | (header[1] << 8)) == (((rom_size - 512) / 1024) / 8)) { // 2621440 // Some headers have the rom size at the start, if this matches with the actual rom size, we probably have a header if (quietmode == 0) - printf("\nsnestools: 'File [%s] has header (size)'\n", filename); + info("File [%s] has header (size)", filename); rom_has_header = 1; } else if ((rom_size % 0x8000) == 512) { // As a last check we'll see if there's exactly 512 bytes extra to this image. if (quietmode == 0) - printf("\nsnestools: 'File [%s] has header (extra)'\n", filename); + info("File [%s] has header (extra)", filename); rom_has_header = 1; } else { // No header found so go back to the start of the file if (quietmode == 0) - printf("\nsnestools: 'File [%s] has no header'\n", filename); + info("File [%s] has no header", filename); rom_has_header = 0; } @@ -772,8 +774,7 @@ int change_checksum(char *filename, FILE *fp) { if ((c = fgetc(fp)) == EOF) { - printf("\nsnestools: error 'Unexpected end of file [%s] af offset %x'\n", filename, i); - exit(1); + fatal("Unexpected end of file [%s] af offset %x", filename, i); } sum_crc += (unsigned char)c; } @@ -791,7 +792,7 @@ int change_checksum(char *filename, FILE *fp) } if (quietmode == 0) - printf("\nsnestools: 'Change Checksum to %04Xh and Checksum complement %04Xh...'\n", sum_crc, sum_crc ^ 0xffff); + info("Change Checksum to %04Xh and Checksum complement %04Xh...", sum_crc, sum_crc ^ 0xffff); // Seek back to the checksum entry. fseek(fp, addr, SEEK_SET); @@ -819,7 +820,7 @@ int change_title(char *filename, FILE *fp, char *title) int i, addr; if (quietmode == 0) - printf("\nsnestools: 'Change title to [%s]...'\n", title); + info("Change title to [%s]...", title); // Compute address of title entry. addr = 512 * rom_has_header + rom_is_lorom ? LOROM_HEADER + OFFSET_TITLE : HIROM_HEADER + OFFSET_TITLE; @@ -840,7 +841,7 @@ int change_country(char *filename, FILE *fp, char *country) int cntry = 0, addr; if (quietmode == 0) - printf("\nsnestools: 'Change country to [%s]...'\n", country); + info("Change country to [%s]...", country); // Compute address of country entry. addr = 512 * rom_has_header + rom_is_lorom ? LOROM_HEADER + OFFSET_COUNTRY : HIROM_HEADER + OFFSET_COUNTRY; @@ -868,7 +869,7 @@ int change_romsize(char *filename, FILE *fp, char *romsize) int romsiz = 0, addr; if (quietmode == 0) - printf("\nsnestools: 'Change rom size to [%s]...'\n", romsize); + info("Change rom size to [%s]...", romsize); // Compute address of country entry. addr = 512 * rom_has_header + rom_is_lorom ? LOROM_HEADER + OFFSET_ROMSIZE : HIROM_HEADER + OFFSET_ROMSIZE; @@ -898,7 +899,7 @@ int change_sram(char *filename, FILE *fp, char nosram) int addr; if (quietmode == 0) - printf("\nsnestools: 'Change sram to [%s]...'\n", nosram ? "NOSRAM" : "SRAM"); + info("Change sram to [%s]...", nosram ? "NOSRAM" : "SRAM"); // Compute address of title entry and Go to sram and change it addr = 512 * rom_has_header + rom_is_lorom ? LOROM_HEADER + OFFSET_SRAM : HIROM_HEADER + OFFSET_SRAM; @@ -918,12 +919,11 @@ int change_sram(char *filename, FILE *fp, char nosram) return -1; } -////////////////////////////////////////////////////////////////////////////// - +//------------------------------------------------------------------------------------------------- void PrintOptions(char *str) { - printf("\n\nUsage: snestools.exe [options] sfc/smc filename ..."); - printf("\n where filename is a SNES rom file"); + printf("Usage: snestools.exe [options] sfc/smc filename ...\n"); + printf(" where filename is a SNES rom file\n"); if (str[0] != 0) printf("\nsnestools: error 'The [%s] parameter is not recognized'", str); @@ -942,15 +942,14 @@ void PrintOptions(char *str) printf("\n"); } -////////////////////////////////////////////////////////////////////////////// +//------------------------------------------------------------------------------------------------- void PrintVersion(void) { printf("snestools (" SNESTOOLSDATE ") version " SNESTOOLSVERSION ""); - printf("\nCopyright (c) 2012-2021 Alekmaul\n"); + printf("\nCopyright (c) 2012-2024 Alekmaul"); } -/// M A I N //////////////////////////////////////////////////////////// - +//------------------------------------------------------------------------------------------------- int main(int argc, char **argv) { FILE *fp; @@ -1063,23 +1062,18 @@ int main(int argc, char **argv) // make sure options are valid if (filebase[0] == 0) { - printf("\nsnestools: error 'You must specify a snes filename'"); - PrintOptions(""); - exit(1); + fatal("You must specify a snes filename"); } if ((changetitle) && (strlen(programtitle) == 0)) { - printf("\nsnestools: error 'You must specify a game title to change it'"); - PrintOptions(""); - exit(1); + fatal("You must specify a game title to change it"); } // open the file fp = fopen(filebase, "rb+"); if (fp == NULL) { - printf("\nsnestools: error 'Can't open file [%s]'", filebase); - exit(1); + fatal("Can't open file [%s]", filebase); } // check for an header and skip it @@ -1144,7 +1138,7 @@ int main(int argc, char **argv) fclose(fp); if (quietmode == 0) - printf("\nsnestools: 'Done!'\n"); + info("Done!"); - return 0; + return (EXIT_SUCCESS); }