ref: be7a5e09b5e54676a9c239682707331be1229828
parent: 2691c9f5c8f866a2af802a119a565c743c05fceb
author: Rangi <remy.oukaour+rangi42@gmail.com>
date: Tue Sep 21 13:37:43 EDT 2021
Factor out usage_exit into into tools/common.h
--- a/tools/common.h
+++ b/tools/common.h
@@ -11,7 +11,19 @@
#include <unistd.h>
#include <getopt.h>
-#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)
--- a/tools/gfx.c
+++ b/tools/gfx.c
@@ -1,9 +1,8 @@
+#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"
+
#include "common.h"
-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");
-}
-
struct Options {
bool trim_whitespace;
bool remove_whitespace;
@@ -76,12 +75,10 @@
options.outfile = optarg;
break;
case 'h':
- usage();
- exit(0);
+ usage_exit(0);
break;
default:
- usage();
- exit(1);
+ usage_exit(1);
}
}
}
@@ -128,7 +125,7 @@
}
}
-int get_tile_size() {
+int get_tile_size(void) {
return options.depth * (options.interleave ? 16 : 8);
}
@@ -270,8 +267,7 @@
argc -= optind;
argv += optind;
if (argc < 1) {
- usage();
- exit(1);
+ usage_exit(1);
}
struct Graphic graphic;
--- a/tools/png_dimensions.c
+++ b/tools/png_dimensions.c
@@ -1,9 +1,8 @@
+#define PROGRAM_NAME "png_dimensions"
+#define USAGE_OPTS "front.png front.dimensions"
+
#include "common.h"
-void usage() {
- fputs("Usage: png_dimensions front.png front.dimensions\n", stderr);
-}
-
uint8_t read_png_dimensions(const char *filename) {
uint32_t width_px = read_png_width_verbose(filename);
if (width_px != 40 && width_px != 48 && width_px != 56) {
@@ -15,8 +14,7 @@
int main(int argc, char *argv[]) {
if (argc < 3) {
- usage();
- exit(1);
+ usage_exit(1);
}
uint8_t output_byte = read_png_dimensions(argv[1]);
--- 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 @@
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 @@
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 @@
argc -= optind;
argv += optind;
if (argc < 2) {
- usage();
- exit(1);
+ usage_exit(1);
}
int width;
--- 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 @@
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 @@
options->girafarig = true;
break;
case 'h':
- usage();
- exit(0);
+ usage_exit(0);
break;
default:
- usage();
- exit(1);
+ usage_exit(1);
}
}
}
@@ -154,8 +151,7 @@
argc -= optind;
argv += optind;
if (argc < 2) {
- usage();
- exit(1);
+ usage_exit(1);
}
int width;
--- a/tools/scan_includes.c
+++ b/tools/scan_includes.c
@@ -1,9 +1,8 @@
+#define PROGRAM_NAME "scan_includes"
+#define USAGE_OPTS "[-h|--help] [-s|--strict] filename.asm"
+
#include "common.h"
-void usage() {
- fputs("Usage: scan_includes [-h|--help] [-s|--strict] filename.asm\n", stderr);
-}
-
void parse_args(int argc, char *argv[], bool *strict) {
struct option long_options[] = {
{"strict", no_argument, 0, 's'},
@@ -16,12 +15,10 @@
*strict = true;
break;
case 'h':
- usage();
- exit(0);
+ usage_exit(0);
break;
default:
- usage();
- exit(1);
+ usage_exit(1);
}
}
}
@@ -95,8 +92,7 @@
argc -= optind;
argv += optind;
if (argc < 1) {
- usage();
- exit(1);
+ usage_exit(1);
}
scan_file(argv[0], strict);
--- 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 @@
BASE_NONE;
break;
case 'h':
- usage();
- exit(0);
+ usage_exit(0);
break;
default:
- usage();
- exit(1);
+ usage_exit(1);
}
}
}
@@ -144,8 +141,7 @@
argc -= optind;
argv += optind;
if (argc < 1) {
- usage();
- exit(1);
+ usage_exit(1);
}
char *filename = argv[0];