Factor out usage_exit into into tools/common.h

This commit is contained in:
Rangi 2021-09-21 17:37:43 -04:00
parent 2691c9f5c8
commit be7a5e09b5
7 changed files with 48 additions and 58 deletions

View File

@ -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)

View File

@ -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;

View File

@ -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]);

View File

@ -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;

View File

@ -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;

View File

@ -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);

View File

@ -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];