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

View File

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

View File

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

View File

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

View File

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