From be7a5e09b5e54676a9c239682707331be1229828 Mon Sep 17 00:00:00 2001 From: Rangi Date: Tue, 21 Sep 2021 17:37:43 -0400 Subject: [PATCH] Factor out usage_exit into into tools/common.h --- tools/common.h | 14 +++++++++++++- tools/gfx.c | 18 +++++++----------- tools/png_dimensions.c | 10 ++++------ tools/pokemon_animation.c | 16 ++++++---------- tools/pokemon_animation_graphics.c | 16 ++++++---------- tools/scan_includes.c | 16 ++++++---------- tools/stadium.c | 16 ++++++---------- 7 files changed, 48 insertions(+), 58 deletions(-) diff --git a/tools/common.h b/tools/common.h index 2010e9e9d..506b483b9 100644 --- a/tools/common.h +++ b/tools/common.h @@ -11,7 +11,19 @@ #include #include -#define error_exit(...) exit((fprintf(stderr, __VA_ARGS__), 1)) +#ifndef PROGRAM_NAME +#error Define PROGRAM_NAME before including common.h! +#endif +#ifndef USAGE_OPTS +#error Define USAGE_OPTS before including common.h! +#endif + +#define error_exit(...) exit((fprintf(stderr, PROGRAM_NAME ": " __VA_ARGS__), 1)) + +void usage_exit(int status) { + fprintf(stderr, "Usage: " PROGRAM_NAME " " USAGE_OPTS "\n"); + exit(status); +} int getopt_long_index; #define getopt_long(argc, argv, optstring, longopts) getopt_long(argc, argv, optstring, longopts, &getopt_long_index) diff --git a/tools/gfx.c b/tools/gfx.c index b086238a9..2928637c0 100644 --- a/tools/gfx.c +++ b/tools/gfx.c @@ -1,8 +1,7 @@ -#include "common.h" +#define PROGRAM_NAME "gfx" +#define USAGE_OPTS "[-h|--help] [--trim-whitespace] [--remove-whitespace] [--interleave] [--remove-duplicates [--keep-whitespace]] [--remove-xflip] [--remove-yflip] [--preserve indexes] [-d|--depth depth] [-p|--png filename.png] [-o|--out outfile] infile" -void usage(void) { - fprintf(stderr, "Usage: gfx [-h|--help] [--trim-whitespace] [--remove-whitespace] [--interleave] [--remove-duplicates [--keep-whitespace]] [--remove-xflip] [--remove-yflip] [--preserve indexes] [-d|--depth depth] [-p|--png filename.png] [-o|--out outfile] infile\n"); -} +#include "common.h" struct Options { bool trim_whitespace; @@ -76,12 +75,10 @@ void parse_args(int argc, char *argv[]) { options.outfile = optarg; break; case 'h': - usage(); - exit(0); + usage_exit(0); break; default: - usage(); - exit(1); + usage_exit(1); } } } @@ -128,7 +125,7 @@ void trim_whitespace(struct Graphic *graphic) { } } -int get_tile_size() { +int get_tile_size(void) { return options.depth * (options.interleave ? 16 : 8); } @@ -270,8 +267,7 @@ int main(int argc, char *argv[]) { argc -= optind; argv += optind; if (argc < 1) { - usage(); - exit(1); + usage_exit(1); } struct Graphic graphic; diff --git a/tools/png_dimensions.c b/tools/png_dimensions.c index e043097ee..aea9c0d09 100644 --- a/tools/png_dimensions.c +++ b/tools/png_dimensions.c @@ -1,8 +1,7 @@ -#include "common.h" +#define PROGRAM_NAME "png_dimensions" +#define USAGE_OPTS "front.png front.dimensions" -void usage() { - fputs("Usage: png_dimensions front.png front.dimensions\n", stderr); -} +#include "common.h" uint8_t read_png_dimensions(const char *filename) { uint32_t width_px = read_png_width_verbose(filename); @@ -15,8 +14,7 @@ uint8_t read_png_dimensions(const char *filename) { int main(int argc, char *argv[]) { if (argc < 3) { - usage(); - exit(1); + usage_exit(1); } uint8_t output_byte = read_png_dimensions(argv[1]); diff --git a/tools/pokemon_animation.c b/tools/pokemon_animation.c index a854c8b4c..cb5766896 100644 --- a/tools/pokemon_animation.c +++ b/tools/pokemon_animation.c @@ -1,3 +1,6 @@ +#define PROGRAM_NAME "pokemon_animation" +#define USAGE_OPTS "[-h|--help] [-b|--bitmasks] [-f|--frames] front.animated.tilemap front.dimensions" + #include "common.h" struct Options { @@ -5,10 +8,6 @@ struct Options { bool use_frames; }; -void usage() { - fputs("Usage: pokemon_animation [-b|--bitmasks] [-f|--frames] front.animated.tilemap front.dimensions\n", stderr); -} - void parse_args(int argc, char *argv[], struct Options *options) { struct option long_options[] = { {"bitmasks", no_argument, 0, 'b'}, @@ -25,12 +24,10 @@ void parse_args(int argc, char *argv[], struct Options *options) { options->use_frames = true; break; case 'h': - usage(); - exit(0); + usage_exit(0); break; default: - usage(); - exit(1); + usage_exit(1); } } } @@ -175,8 +172,7 @@ int main(int argc, char *argv[]) { argc -= optind; argv += optind; if (argc < 2) { - usage(); - exit(1); + usage_exit(1); } int width; diff --git a/tools/pokemon_animation_graphics.c b/tools/pokemon_animation_graphics.c index 27573a516..c8c126475 100644 --- a/tools/pokemon_animation_graphics.c +++ b/tools/pokemon_animation_graphics.c @@ -1,3 +1,6 @@ +#define PROGRAM_NAME "pokemon_animation_graphics" +#define USAGE_OPTS "[-h|--help] [-o|--output front.animated.2bpp] [-t|--tilemap front.animated.tilemap] [--girafarig] front.2bpp front.dimensions" + #include "common.h" struct Options { @@ -6,10 +9,6 @@ struct Options { bool girafarig; }; -void usage() { - fputs("Usage: pokemon_animation_graphics [-h|--help] [-o|--output front.animated.2bpp] [-t|--tilemap front.animated.tilemap] [--girafarig] front.2bpp front.dimensions\n", stderr); -} - void parse_args(int argc, char *argv[], struct Options *options) { struct option long_options[] = { {"output", required_argument, 0, 'o'}, @@ -30,12 +29,10 @@ void parse_args(int argc, char *argv[], struct Options *options) { options->girafarig = true; break; case 'h': - usage(); - exit(0); + usage_exit(0); break; default: - usage(); - exit(1); + usage_exit(1); } } } @@ -154,8 +151,7 @@ int main(int argc, char *argv[]) { argc -= optind; argv += optind; if (argc < 2) { - usage(); - exit(1); + usage_exit(1); } int width; diff --git a/tools/scan_includes.c b/tools/scan_includes.c index ff8f93678..cb24be2d7 100644 --- a/tools/scan_includes.c +++ b/tools/scan_includes.c @@ -1,8 +1,7 @@ -#include "common.h" +#define PROGRAM_NAME "scan_includes" +#define USAGE_OPTS "[-h|--help] [-s|--strict] filename.asm" -void usage() { - fputs("Usage: scan_includes [-h|--help] [-s|--strict] filename.asm\n", stderr); -} +#include "common.h" void parse_args(int argc, char *argv[], bool *strict) { struct option long_options[] = { @@ -16,12 +15,10 @@ void parse_args(int argc, char *argv[], bool *strict) { *strict = true; break; case 'h': - usage(); - exit(0); + usage_exit(0); break; default: - usage(); - exit(1); + usage_exit(1); } } } @@ -95,8 +92,7 @@ int main(int argc, char *argv[]) { argc -= optind; argv += optind; if (argc < 1) { - usage(); - exit(1); + usage_exit(1); } scan_file(argv[0], strict); diff --git a/tools/stadium.c b/tools/stadium.c index 8353e90ec..8eeb0d922 100644 --- a/tools/stadium.c +++ b/tools/stadium.c @@ -1,11 +1,10 @@ +#define PROGRAM_NAME "stadium" +#define USAGE_OPTS "[-h|--help] [-b|--base us|eu|dbg] pokecrystal.gbc" + #include "common.h" enum Base { BASE_NONE, BASE_US, BASE_EU, BASE_DEBUG }; -void usage() { - fputs("Usage: stadium [-h|--help] [-b|--base us|eu|dbg] pokecrystal.gbc\n", stderr); -} - void parse_args(int argc, char *argv[], enum Base *base) { struct option long_options[] = { {"base", required_argument, 0, 'b'}, @@ -21,12 +20,10 @@ void parse_args(int argc, char *argv[], enum Base *base) { BASE_NONE; break; case 'h': - usage(); - exit(0); + usage_exit(0); break; default: - usage(); - exit(1); + usage_exit(1); } } } @@ -144,8 +141,7 @@ int main(int argc, char *argv[]) { argc -= optind; argv += optind; if (argc < 1) { - usage(); - exit(1); + usage_exit(1); } char *filename = argv[0];