From e53135a7b9a67812f1c5518eeea3f3f476bd3905 Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Wed, 16 Jul 2014 23:47:47 -0700 Subject: [PATCH] Clean up command line args --- src/gba/gba-thread.c | 6 +++--- src/platform/commandline.c | 38 ++++++++++++++++++++------------------ src/platform/commandline.h | 9 +++++---- src/platform/sdl/gl-main.c | 7 +++---- src/platform/sdl/sw-main.c | 7 +++---- 5 files changed, 34 insertions(+), 33 deletions(-) diff --git a/src/gba/gba-thread.c b/src/gba/gba-thread.c index 2b0565ce1..002c428f0 100644 --- a/src/gba/gba-thread.c +++ b/src/gba/gba-thread.c @@ -169,10 +169,10 @@ static THREAD_ENTRY _GBAThreadRun(void* context) { } void GBAMapOptionsToContext(struct StartupOptions* opts, struct GBAThread* threadContext) { - threadContext->fd = VFileFromFD(opts->fd); + threadContext->fd = VFileOpen(opts->fname, O_RDONLY); threadContext->fname = opts->fname; - threadContext->biosFd = VFileFromFD(opts->biosFd); - threadContext->patchFd = VFileFromFD(opts->patchFd); + threadContext->biosFd = VFileOpen(opts->bios, O_RDONLY); + threadContext->patchFd = VFileOpen(opts->patch, O_RDONLY); threadContext->frameskip = opts->frameskip; threadContext->logLevel = opts->logLevel; threadContext->rewindBufferCapacity = opts->rewindBufferCapacity; diff --git a/src/platform/commandline.c b/src/platform/commandline.c index 4858fdd4b..ff7720be9 100644 --- a/src/platform/commandline.c +++ b/src/platform/commandline.c @@ -21,18 +21,16 @@ " -4 4x viewport\n" \ " -f Start full-screen" -static const char* _defaultFilename = "test.rom"; - static const struct option _options[] = { - { "bios", 1, 0, 'b' }, - { "patch", 1, 0, 'p' }, - { "frameskip", 1, 0, 's' }, + { "bios", required_argument, 0, 'b' }, + { "frameskip", required_argument, 0, 's' }, #ifdef USE_CLI_DEBUGGER - { "debug", 1, 0, 'd' }, + { "debug", no_argument, 0, 'd' }, #endif #ifdef USE_GDB_STUB - { "gdb", 1, 0, 'g' }, + { "gdb", no_argument, 0, 'g' }, #endif + { "patch", required_argument, 0, 'p' }, { 0, 0, 0, 0 } }; @@ -40,9 +38,6 @@ bool _parseGraphicsArg(struct SubParser* parser, int option, const char* arg); bool parseCommandArgs(struct StartupOptions* opts, int argc, char* const* argv, struct SubParser* subparser) { memset(opts, 0, sizeof(*opts)); - opts->fd = -1; - opts->biosFd = -1; - opts->patchFd = -1; int ch; char options[64] = @@ -61,7 +56,7 @@ bool parseCommandArgs(struct StartupOptions* opts, int argc, char* const* argv, while ((ch = getopt_long(argc, argv, options, _options, 0)) != -1) { switch (ch) { case 'b': - opts->biosFd = open(optarg, O_RDONLY); + opts->bios = strdup(optarg); break; #ifdef USE_CLI_DEBUGGER case 'd': @@ -83,7 +78,7 @@ bool parseCommandArgs(struct StartupOptions* opts, int argc, char* const* argv, opts->logLevel = atoi(optarg); break; case 'p': - opts->patchFd = open(optarg, O_RDONLY); + opts->patch = strdup(optarg); break; case 's': opts->frameskip = atoi(optarg); @@ -99,17 +94,24 @@ bool parseCommandArgs(struct StartupOptions* opts, int argc, char* const* argv, } argc -= optind; argv += optind; - if (argc == 1) { - opts->fname = argv[0]; - } else if (argc == 0) { - opts->fname = _defaultFilename; - } else { + if (argc != 1) { return false; } - opts->fd = open(opts->fname, O_RDONLY); + opts->fname = strdup(argv[0]); return true; } +void freeOptions(struct StartupOptions* opts) { + free(opts->fname); + opts->fname = 0; + + free(opts->bios); + opts->bios = 0; + + free(opts->patch); + opts->patch = 0; +} + void initParserForGraphics(struct SubParser* parser, struct GraphicsOpts* opts) { parser->usage = GRAPHICS_USAGE; parser->opts = opts; diff --git a/src/platform/commandline.h b/src/platform/commandline.h index 38945df74..18d611288 100644 --- a/src/platform/commandline.h +++ b/src/platform/commandline.h @@ -15,10 +15,9 @@ enum DebuggerType { }; struct StartupOptions { - int fd; - const char* fname; - int biosFd; - int patchFd; + char* fname; + char* bios; + char* patch; int logLevel; int frameskip; int rewindBufferCapacity; @@ -43,6 +42,8 @@ struct GraphicsOpts { }; bool parseCommandArgs(struct StartupOptions* opts, int argc, char* const* argv, struct SubParser* subparser); +void freeOptions(struct StartupOptions* opts); + void usage(const char* arg0, const char* extraOptions); void initParserForGraphics(struct SubParser* parser, struct GraphicsOpts* opts); diff --git a/src/platform/sdl/gl-main.c b/src/platform/sdl/gl-main.c index 0fe6461d3..acc5f4363 100644 --- a/src/platform/sdl/gl-main.c +++ b/src/platform/sdl/gl-main.c @@ -67,6 +67,7 @@ int main(int argc, char** argv) { initParserForGraphics(&subparser, &graphicsOpts); if (!parseCommandArgs(&opts, argc, argv, &subparser)) { usage(argv[0], subparser.usage); + freeOptions(&opts); return 1; } @@ -78,6 +79,7 @@ int main(int argc, char** argv) { #endif if (!_GBASDLInit(&renderer)) { + freeOptions(&opts); return 1; } @@ -99,10 +101,7 @@ int main(int argc, char** argv) { _GBASDLRunloop(&context, &renderer); GBAThreadJoin(&context); - close(opts.fd); - if (opts.biosFd >= 0) { - close(opts.biosFd); - } + freeOptions(&opts); free(context.debugger); _GBASDLDeinit(&renderer); diff --git a/src/platform/sdl/sw-main.c b/src/platform/sdl/sw-main.c index ff8bab550..c69907c7c 100644 --- a/src/platform/sdl/sw-main.c +++ b/src/platform/sdl/sw-main.c @@ -54,6 +54,7 @@ int main(int argc, char** argv) { initParserForGraphics(&subparser, &graphicsOpts); if (!parseCommandArgs(&opts, argc, argv, &subparser)) { usage(argv[0], subparser.usage); + freeOptions(&opts); return 1; } @@ -61,6 +62,7 @@ int main(int argc, char** argv) { renderer.viewportHeight = graphicsOpts.height; if (!_GBASDLInit(&renderer)) { + freeOptions(&opts); return 1; } @@ -129,11 +131,8 @@ int main(int argc, char** argv) { SDL_UnlockSurface(surface); #endif GBAThreadJoin(&context); - close(opts.fd); - if (opts.biosFd >= 0) { - close(opts.biosFd); - } free(context.debugger); + freeOptions(&opts); _GBASDLDeinit(&renderer);