all repos — mgba @ e53135a7b9a67812f1c5518eeea3f3f476bd3905

mGBA Game Boy Advance Emulator

Clean up command line args
Jeffrey Pfau jeffrey@endrift.com
Wed, 16 Jul 2014 23:47:47 -0700
commit

e53135a7b9a67812f1c5518eeea3f3f476bd3905

parent

6519fad6520853bba336d4858d7f7045efc59749

M src/gba/gba-thread.csrc/gba/gba-thread.c

@@ -169,10 +169,10 @@ return 0;

} 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;
M src/platform/commandline.csrc/platform/commandline.c

@@ -21,18 +21,16 @@ " -3 3x viewport\n" \

" -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 @@ }

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 @@ case 'l':

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,15 +94,22 @@ }

} 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) {
M src/platform/commandline.hsrc/platform/commandline.h

@@ -15,10 +15,9 @@ DEBUGGER_MAX

}; 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 @@ int height;

}; 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);
M src/platform/sdl/gl-main.csrc/platform/sdl/gl-main.c

@@ -67,6 +67,7 @@ struct GraphicsOpts graphicsOpts;

initParserForGraphics(&subparser, &graphicsOpts); if (!parseCommandArgs(&opts, argc, argv, &subparser)) { usage(argv[0], subparser.usage); + freeOptions(&opts); return 1; }

@@ -78,6 +79,7 @@ renderer.events.windowUpdated = 0;

#endif if (!_GBASDLInit(&renderer)) { + freeOptions(&opts); return 1; }

@@ -99,10 +101,7 @@

_GBASDLRunloop(&context, &renderer); GBAThreadJoin(&context); - close(opts.fd); - if (opts.biosFd >= 0) { - close(opts.biosFd); - } + freeOptions(&opts); free(context.debugger); _GBASDLDeinit(&renderer);
M src/platform/sdl/sw-main.csrc/platform/sdl/sw-main.c

@@ -54,6 +54,7 @@ struct GraphicsOpts graphicsOpts;

initParserForGraphics(&subparser, &graphicsOpts); if (!parseCommandArgs(&opts, argc, argv, &subparser)) { usage(argv[0], subparser.usage); + freeOptions(&opts); return 1; }

@@ -61,6 +62,7 @@ renderer.viewportWidth = graphicsOpts.width;

renderer.viewportHeight = graphicsOpts.height; if (!_GBASDLInit(&renderer)) { + freeOptions(&opts); return 1; }

@@ -129,11 +131,8 @@ #if !SDL_VERSION_ATLEAST(2, 0, 0)

SDL_UnlockSurface(surface); #endif GBAThreadJoin(&context); - close(opts.fd); - if (opts.biosFd >= 0) { - close(opts.biosFd); - } free(context.debugger); + freeOptions(&opts); _GBASDLDeinit(&renderer);