Clean up command line args

This commit is contained in:
Jeffrey Pfau 2014-07-16 23:47:47 -07:00
parent 6519fad652
commit e53135a7b9
5 changed files with 34 additions and 33 deletions

View File

@ -169,10 +169,10 @@ static THREAD_ENTRY _GBAThreadRun(void* context) {
} }
void GBAMapOptionsToContext(struct StartupOptions* opts, struct GBAThread* threadContext) { 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->fname = opts->fname;
threadContext->biosFd = VFileFromFD(opts->biosFd); threadContext->biosFd = VFileOpen(opts->bios, O_RDONLY);
threadContext->patchFd = VFileFromFD(opts->patchFd); threadContext->patchFd = VFileOpen(opts->patch, O_RDONLY);
threadContext->frameskip = opts->frameskip; threadContext->frameskip = opts->frameskip;
threadContext->logLevel = opts->logLevel; threadContext->logLevel = opts->logLevel;
threadContext->rewindBufferCapacity = opts->rewindBufferCapacity; threadContext->rewindBufferCapacity = opts->rewindBufferCapacity;

View File

@ -21,18 +21,16 @@
" -4 4x viewport\n" \ " -4 4x viewport\n" \
" -f Start full-screen" " -f Start full-screen"
static const char* _defaultFilename = "test.rom";
static const struct option _options[] = { static const struct option _options[] = {
{ "bios", 1, 0, 'b' }, { "bios", required_argument, 0, 'b' },
{ "patch", 1, 0, 'p' }, { "frameskip", required_argument, 0, 's' },
{ "frameskip", 1, 0, 's' },
#ifdef USE_CLI_DEBUGGER #ifdef USE_CLI_DEBUGGER
{ "debug", 1, 0, 'd' }, { "debug", no_argument, 0, 'd' },
#endif #endif
#ifdef USE_GDB_STUB #ifdef USE_GDB_STUB
{ "gdb", 1, 0, 'g' }, { "gdb", no_argument, 0, 'g' },
#endif #endif
{ "patch", required_argument, 0, 'p' },
{ 0, 0, 0, 0 } { 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) { bool parseCommandArgs(struct StartupOptions* opts, int argc, char* const* argv, struct SubParser* subparser) {
memset(opts, 0, sizeof(*opts)); memset(opts, 0, sizeof(*opts));
opts->fd = -1;
opts->biosFd = -1;
opts->patchFd = -1;
int ch; int ch;
char options[64] = 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) { while ((ch = getopt_long(argc, argv, options, _options, 0)) != -1) {
switch (ch) { switch (ch) {
case 'b': case 'b':
opts->biosFd = open(optarg, O_RDONLY); opts->bios = strdup(optarg);
break; break;
#ifdef USE_CLI_DEBUGGER #ifdef USE_CLI_DEBUGGER
case 'd': case 'd':
@ -83,7 +78,7 @@ bool parseCommandArgs(struct StartupOptions* opts, int argc, char* const* argv,
opts->logLevel = atoi(optarg); opts->logLevel = atoi(optarg);
break; break;
case 'p': case 'p':
opts->patchFd = open(optarg, O_RDONLY); opts->patch = strdup(optarg);
break; break;
case 's': case 's':
opts->frameskip = atoi(optarg); opts->frameskip = atoi(optarg);
@ -99,17 +94,24 @@ bool parseCommandArgs(struct StartupOptions* opts, int argc, char* const* argv,
} }
argc -= optind; argc -= optind;
argv += optind; argv += optind;
if (argc == 1) { if (argc != 1) {
opts->fname = argv[0];
} else if (argc == 0) {
opts->fname = _defaultFilename;
} else {
return false; return false;
} }
opts->fd = open(opts->fname, O_RDONLY); opts->fname = strdup(argv[0]);
return true; 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) { void initParserForGraphics(struct SubParser* parser, struct GraphicsOpts* opts) {
parser->usage = GRAPHICS_USAGE; parser->usage = GRAPHICS_USAGE;
parser->opts = opts; parser->opts = opts;

View File

@ -15,10 +15,9 @@ enum DebuggerType {
}; };
struct StartupOptions { struct StartupOptions {
int fd; char* fname;
const char* fname; char* bios;
int biosFd; char* patch;
int patchFd;
int logLevel; int logLevel;
int frameskip; int frameskip;
int rewindBufferCapacity; int rewindBufferCapacity;
@ -43,6 +42,8 @@ struct GraphicsOpts {
}; };
bool parseCommandArgs(struct StartupOptions* opts, int argc, char* const* argv, struct SubParser* subparser); 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 usage(const char* arg0, const char* extraOptions);
void initParserForGraphics(struct SubParser* parser, struct GraphicsOpts* opts); void initParserForGraphics(struct SubParser* parser, struct GraphicsOpts* opts);

View File

@ -67,6 +67,7 @@ int main(int argc, char** argv) {
initParserForGraphics(&subparser, &graphicsOpts); initParserForGraphics(&subparser, &graphicsOpts);
if (!parseCommandArgs(&opts, argc, argv, &subparser)) { if (!parseCommandArgs(&opts, argc, argv, &subparser)) {
usage(argv[0], subparser.usage); usage(argv[0], subparser.usage);
freeOptions(&opts);
return 1; return 1;
} }
@ -78,6 +79,7 @@ int main(int argc, char** argv) {
#endif #endif
if (!_GBASDLInit(&renderer)) { if (!_GBASDLInit(&renderer)) {
freeOptions(&opts);
return 1; return 1;
} }
@ -99,10 +101,7 @@ int main(int argc, char** argv) {
_GBASDLRunloop(&context, &renderer); _GBASDLRunloop(&context, &renderer);
GBAThreadJoin(&context); GBAThreadJoin(&context);
close(opts.fd); freeOptions(&opts);
if (opts.biosFd >= 0) {
close(opts.biosFd);
}
free(context.debugger); free(context.debugger);
_GBASDLDeinit(&renderer); _GBASDLDeinit(&renderer);

View File

@ -54,6 +54,7 @@ int main(int argc, char** argv) {
initParserForGraphics(&subparser, &graphicsOpts); initParserForGraphics(&subparser, &graphicsOpts);
if (!parseCommandArgs(&opts, argc, argv, &subparser)) { if (!parseCommandArgs(&opts, argc, argv, &subparser)) {
usage(argv[0], subparser.usage); usage(argv[0], subparser.usage);
freeOptions(&opts);
return 1; return 1;
} }
@ -61,6 +62,7 @@ int main(int argc, char** argv) {
renderer.viewportHeight = graphicsOpts.height; renderer.viewportHeight = graphicsOpts.height;
if (!_GBASDLInit(&renderer)) { if (!_GBASDLInit(&renderer)) {
freeOptions(&opts);
return 1; return 1;
} }
@ -129,11 +131,8 @@ int main(int argc, char** argv) {
SDL_UnlockSurface(surface); SDL_UnlockSurface(surface);
#endif #endif
GBAThreadJoin(&context); GBAThreadJoin(&context);
close(opts.fd);
if (opts.biosFd >= 0) {
close(opts.biosFd);
}
free(context.debugger); free(context.debugger);
freeOptions(&opts);
_GBASDLDeinit(&renderer); _GBASDLDeinit(&renderer);