Skip to content

Commit

Permalink
Factor out usage_exit into into tools/common.h
Browse files Browse the repository at this point in the history
  • Loading branch information
Rangi42 committed Sep 21, 2021
1 parent 2691c9f commit be7a5e0
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 58 deletions.
14 changes: 13 additions & 1 deletion tools/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 7 additions & 11 deletions tools/gfx.c
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand Down
10 changes: 4 additions & 6 deletions tools/png_dimensions.c
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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]);
Expand Down
16 changes: 6 additions & 10 deletions tools/pokemon_animation.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#define PROGRAM_NAME "pokemon_animation"
#define USAGE_OPTS "[-h|--help] [-b|--bitmasks] [-f|--frames] front.animated.tilemap front.dimensions"

#include "common.h"

struct Options {
bool use_bitmasks;
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'},
Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -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;
Expand Down
16 changes: 6 additions & 10 deletions tools/pokemon_animation_graphics.c
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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'},
Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -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;
Expand Down
16 changes: 6 additions & 10 deletions tools/scan_includes.c
Original file line number Diff line number Diff line change
@@ -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[] = {
Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -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);
Expand Down
16 changes: 6 additions & 10 deletions tools/stadium.c
Original file line number Diff line number Diff line change
@@ -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'},
Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -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];
Expand Down

0 comments on commit be7a5e0

Please sign in to comment.