diff --git a/include/cli.hpp b/include/cli.hpp index f895a44e..7b3343a5 100644 --- a/include/cli.hpp +++ b/include/cli.hpp @@ -7,6 +7,7 @@ #include #include "extern/getopt.hpp" // option +#include "usage.hpp" void cli_ParseArgs( int argc, @@ -14,7 +15,7 @@ void cli_ParseArgs( char const *shortOpts, option const *longOpts, void (*parseArg)(int, char *), - void (*fatal)(char const *, ...) + Usage usage ); #endif // RGBDS_CLI_HPP diff --git a/src/asm/main.cpp b/src/asm/main.cpp index e11d765b..6eff7b47 100644 --- a/src/asm/main.cpp +++ b/src/asm/main.cpp @@ -513,7 +513,7 @@ int main(int argc, char *argv[]) { options.maxErrors = 100; // LCOV_EXCL_LINE } - cli_ParseArgs(argc, argv, optstring, longopts, parseArg, fatal); + cli_ParseArgs(argc, argv, optstring, longopts, parseArg, usage); if (!options.targetFileName && options.objectFileName) { options.targetFileName = options.objectFileName; diff --git a/src/cli.cpp b/src/cli.cpp index 2c8197e0..4922ff4c 100644 --- a/src/cli.cpp +++ b/src/cli.cpp @@ -2,26 +2,30 @@ #include #include +#include #include #include #include #include "extern/getopt.hpp" +#include "style.hpp" +#include "usage.hpp" #include "util.hpp" // isBlankSpace using namespace std::literals; // Turn an at-file's contents into an argv that `getopt` can handle, appending them to `argPool`. -static std::vector readAtFile( - std::string const &path, std::vector &argPool, void (*fatal)(char const *, ...) -) { +static std::vector + readAtFile(std::string const &path, std::vector &argPool, Usage usage) { std::vector argvOfs; std::filebuf file; if (!file.open(path, std::ios_base::in)) { - std::string msg = "Error reading at-file \""s + path + "\": " + strerror(errno); - fatal(msg.c_str()); - return argvOfs; // Since we can't mark the `fatal` function pointer as [[noreturn]] + style_Set(stderr, STYLE_RED, true); + fputs("FATAL: ", stderr); + style_Reset(stderr); + fprintf(stderr, "Failed to open at-file \"%s\": %s\n", path.c_str(), strerror(errno)); + usage.printAndExit(1); } for (;;) { @@ -71,7 +75,7 @@ void cli_ParseArgs( char const *shortOpts, option const *longOpts, void (*parseArg)(int, char *), - void (*fatal)(char const *, ...) + Usage usage ) { struct AtFileStackEntry { int parentInd; // Saved offset into parent argv @@ -112,7 +116,7 @@ void cli_ParseArgs( // It would be nice to compute the char pointers on the fly, but reallocs don't allow // that; so we must compute the offsets after the pool is fixed - std::vector offsets = readAtFile(&musl_optarg[1], argPool, fatal); + std::vector offsets = readAtFile(&musl_optarg[1], argPool, usage); stackEntry.argv.reserve(offsets.size() + 2); // Avoid a bunch of reallocs for (size_t ofs : offsets) { stackEntry.argv.push_back(&argPool.data()[ofs]); diff --git a/src/extern/getopt.cpp b/src/extern/getopt.cpp index d04ec5e3..74849170 100644 --- a/src/extern/getopt.cpp +++ b/src/extern/getopt.cpp @@ -11,15 +11,21 @@ #include #include +#include "style.hpp" + char *musl_optarg; int musl_optind = 1, musl_opterr = 1, musl_optopt; int musl_optreset = 0; static int musl_optpos; -static void musl_getopt_msg(char const *a, char const *b, char const *c, size_t l) { +static void musl_getopt_msg(char const *msg, char const *param, size_t len) { FILE *f = stderr; - if (fputs(a, f) >= 0 && fwrite(b, strlen(b), 1, f) && fwrite(c, 1, l, f) == l) { + style_Set(f, STYLE_RED, true); + fputs("error: ", f); + style_Reset(f); + + if (fwrite(msg, strlen(msg), 1, f) && fwrite(param, 1, len, f) == len) { putc('\n', f); } } @@ -90,7 +96,7 @@ static int getopt(int argc, char *argv[], char const *optstring) { if (d != c || c == ':') { musl_optopt = c; if (optstring[0] != ':' && musl_opterr) { - musl_getopt_msg(argv[0], ": unrecognized option: ", optchar, k); + musl_getopt_msg("unrecognized option: ", optchar, k); } return '?'; } @@ -106,7 +112,7 @@ static int getopt(int argc, char *argv[], char const *optstring) { return ':'; } if (musl_opterr) { - musl_getopt_msg(argv[0], ": option requires an argument: ", optchar, k); + musl_getopt_msg("option requires an argument: ", optchar, k); } return '?'; } @@ -228,8 +234,7 @@ static int musl_getopt_long_core( return '?'; } musl_getopt_msg( - argv[0], - ": option does not take an argument: ", + "option does not take an argument: ", longopts[i].name, strlen(longopts[i].name) ); @@ -247,10 +252,7 @@ static int musl_getopt_long_core( return '?'; } musl_getopt_msg( - argv[0], - ": option requires an argument: ", - longopts[i].name, - strlen(longopts[i].name) + "option requires an argument: ", longopts[i].name, strlen(longopts[i].name) ); return '?'; } @@ -269,8 +271,7 @@ static int musl_getopt_long_core( musl_optopt = 0; if (!colon && musl_opterr) { musl_getopt_msg( - argv[0], - cnt ? ": option is ambiguous: " : ": unrecognized option: ", + cnt ? "option is ambiguous: " : "unrecognized option: ", argv[musl_optind] + 2, strlen(argv[musl_optind] + 2) ); diff --git a/src/fix/main.cpp b/src/fix/main.cpp index 94ff78f3..bcfa325b 100644 --- a/src/fix/main.cpp +++ b/src/fix/main.cpp @@ -346,7 +346,7 @@ static void initLogo() { } int main(int argc, char *argv[]) { - cli_ParseArgs(argc, argv, optstring, longopts, parseArg, fatal); + cli_ParseArgs(argc, argv, optstring, longopts, parseArg, usage); if ((options.cartridgeType & 0xFF00) == TPP1 && !options.japanese) { warning( diff --git a/src/gfx/main.cpp b/src/gfx/main.cpp index 9b0dc945..257273ff 100644 --- a/src/gfx/main.cpp +++ b/src/gfx/main.cpp @@ -639,7 +639,7 @@ static void replaceExtension(std::string &path, char const *extension) { } int main(int argc, char *argv[]) { - cli_ParseArgs(argc, argv, optstring, longopts, parseArg, fatal); + cli_ParseArgs(argc, argv, optstring, longopts, parseArg, usage); if (options.nbColorsPerPal == 0) { options.nbColorsPerPal = 1u << options.bitDepth; diff --git a/src/link/main.cpp b/src/link/main.cpp index c71c1ed5..e0a76336 100644 --- a/src/link/main.cpp +++ b/src/link/main.cpp @@ -415,7 +415,7 @@ static void verboseOutputConfig() { // LCOV_EXCL_STOP int main(int argc, char *argv[]) { - cli_ParseArgs(argc, argv, optstring, longopts, parseArg, fatal); + cli_ParseArgs(argc, argv, optstring, longopts, parseArg, usage); verboseOutputConfig();