Refactor the ROM set command line options

This commit is contained in:
David Hrdlička
2022-04-08 01:33:49 +02:00
parent 3449a173ae
commit db2b868909
2 changed files with 38 additions and 54 deletions

View File

@@ -395,12 +395,12 @@ pc_log(const char *fmt, ...)
int int
pc_init(int argc, char *argv[]) pc_init(int argc, char *argv[])
{ {
char path[2048], path2[2048]; char *ppath = NULL, *rpath = NULL;
char *cfg = NULL, *p; char *cfg = NULL, *p;
#if !defined(__APPLE__) && !defined(_WIN32) #if !defined(__APPLE__) && !defined(_WIN32)
char *appimage; char *appimage;
#endif #endif
char temp[128]; char temp[2048];
struct tm *info; struct tm *info;
time_t now; time_t now;
int c, vmrp = 0; int c, vmrp = 0;
@@ -424,9 +424,6 @@ pc_init(int argc, char *argv[])
plat_getcwd(usr_path, sizeof(usr_path) - 1); plat_getcwd(usr_path, sizeof(usr_path) - 1);
plat_getcwd(rom_path, sizeof(rom_path) - 1); plat_getcwd(rom_path, sizeof(rom_path) - 1);
memset(path, 0x00, sizeof(path));
memset(path2, 0x00, sizeof(path));
for (c=1; c<argc; c++) { for (c=1; c<argc; c++) {
if (argv[c][0] != '-') break; if (argv[c][0] != '-') break;
@@ -449,7 +446,6 @@ usage:
printf("-H or --hwnd id,hwnd - sends back the main dialog's hwnd\n"); printf("-H or --hwnd id,hwnd - sends back the main dialog's hwnd\n");
#endif #endif
printf("-L or --logfile path - set 'path' to be the logfile\n"); printf("-L or --logfile path - set 'path' to be the logfile\n");
printf("-M or --vmrompath - ROM path is roms subdirectory inside the userfiles path\n");
printf("-N or --noconfirm - do not ask for confirmation on quit\n"); printf("-N or --noconfirm - do not ask for confirmation on quit\n");
printf("-O or --dumpcfg - dump config file after loading\n"); printf("-O or --dumpcfg - dump config file after loading\n");
printf("-P or --vmpath path - set 'path' to be root for vm\n"); printf("-P or --vmpath path - set 'path' to be root for vm\n");
@@ -491,13 +487,13 @@ usage:
!strcasecmp(argv[c], "-P")) { !strcasecmp(argv[c], "-P")) {
if ((c+1) == argc) goto usage; if ((c+1) == argc) goto usage;
strcpy(path, argv[++c]); ppath = argv[++c];
} else if (!strcasecmp(argv[c], "--rompath") || } else if (!strcasecmp(argv[c], "--rompath") ||
!strcasecmp(argv[c], "-R")) { !strcasecmp(argv[c], "-R")) {
if ((c+1) == argc) goto usage; if ((c+1) == argc) goto usage;
strcpy(path2, argv[++c]); rpath = argv[++c];
rom_add_path(path2); rom_add_path(rpath);
} else if (!strcasecmp(argv[c], "--config") || } else if (!strcasecmp(argv[c], "--config") ||
!strcasecmp(argv[c], "-C")) { !strcasecmp(argv[c], "-C")) {
if ((c+1) == argc) goto usage; if ((c+1) == argc) goto usage;
@@ -554,7 +550,7 @@ usage:
/* One argument (config file) allowed. */ /* One argument (config file) allowed. */
if (c < argc) { if (c < argc) {
if (lvmp) if (lvmp)
strcpy(path, argv[c++]); ppath = argv[c++];
else else
cfg = argv[c++]; cfg = argv[c++];
} }
@@ -570,21 +566,21 @@ usage:
* make sure that if that was a relative path, we * make sure that if that was a relative path, we
* make it absolute. * make it absolute.
*/ */
if (path[0] != '\0') { if (ppath != NULL) {
if (! plat_path_abs(path)) { if (! plat_path_abs(ppath)) {
/* /*
* This looks like a relative path. * This looks like a relative path.
* *
* Add it to the current working directory * Add it to the current working directory
* to convert it (back) to an absolute path. * to convert it (back) to an absolute path.
*/ */
strcat(usr_path, path); strcat(usr_path, ppath);
} else { } else {
/* /*
* The user-provided path seems like an * The user-provided path seems like an
* absolute path, so just use that. * absolute path, so just use that.
*/ */
strcpy(usr_path, path); strcpy(usr_path, ppath);
} }
/* If the specified path does not yet exist, /* If the specified path does not yet exist,
@@ -593,40 +589,24 @@ usage:
plat_dir_create(usr_path); plat_dir_create(usr_path);
} }
if (vmrp) { // Add the VM-local ROM path.
char vmrppath[1024] = { 0 }; plat_append_filename(temp, usr_path, "roms");
strcpy(vmrppath, usr_path); plat_path_slash(temp);
plat_path_slash(vmrppath); rom_add_path(temp);
strcat(vmrppath, "roms");
plat_path_slash(vmrppath);
rom_add_path(vmrppath);
if (path2[0] == '\0') {
strcpy(path2, vmrppath);
}
}
{ // Add the standard ROM path in the same directory as the executable.
char default_rom_path[1024] = { 0 };
#if !defined(_WIN32) && !defined(__APPLE__) #if !defined(_WIN32) && !defined(__APPLE__)
appimage = getenv("APPIMAGE"); appimage = getenv("APPIMAGE");
if (appimage && (appimage[0] != '\0')) { if (appimage && (appimage[0] != '\0')) {
plat_get_dirname(default_rom_path, appimage); plat_append_filename(temp, appimage, "roms");
plat_path_slash(default_rom_path); } else {
strcat(default_rom_path, "roms"); #endif
plat_path_slash(default_rom_path); plat_append_filename(temp, exe_path, "roms");
#if !defined(_WIN32) && !defined(__APPLE__)
} }
#endif #endif
if (default_rom_path[0] == '\0') {
plat_getcwd(default_rom_path, 1024); rom_add_path(temp);
plat_path_slash(default_rom_path);
snprintf(default_rom_path, 1024, "%s%s%c", default_rom_path, "roms", 0);
plat_path_slash(default_rom_path);
}
rom_add_path(default_rom_path);
if (path2[0] == '\0') {
strcpy(path2, default_rom_path);
}
}
plat_init_rom_paths(); plat_init_rom_paths();
@@ -636,21 +616,21 @@ usage:
* make sure that if that was a relative path, we * make sure that if that was a relative path, we
* make it absolute. * make it absolute.
*/ */
if (path2[0] != '\0') { if (rpath != NULL) {
if (! plat_path_abs(path2)) { if (! plat_path_abs(rpath)) {
/* /*
* This looks like a relative path. * This looks like a relative path.
* *
* Add it to the current working directory * Add it to the current working directory
* to convert it (back) to an absolute path. * to convert it (back) to an absolute path.
*/ */
strcat(rom_path, path2); strcat(rom_path, rpath);
} else { } else {
/* /*
* The user-provided path seems like an * The user-provided path seems like an
* absolute path, so just use that. * absolute path, so just use that.
*/ */
strcpy(rom_path, path2); strcpy(rom_path, rpath);
} }
/* If the specified path does not yet exist, /* If the specified path does not yet exist,

View File

@@ -62,14 +62,18 @@ rom_add_path(const char* path)
{ {
char cwd[1024] = { 0 }; char cwd[1024] = { 0 };
// Iterate to the end of the list.
rom_path_t* rom_path = &rom_paths; rom_path_t* rom_path = &rom_paths;
if (rom_paths.path[0] != '\0')
{
// Iterate to the end of the list.
while (rom_path->next != NULL) { while (rom_path->next != NULL) {
rom_path = rom_path->next; rom_path = rom_path->next;
} }
// Allocate the new entry. // Allocate the new entry.
rom_path = rom_path->next = calloc(1, sizeof(rom_path_t)); rom_path = rom_path->next = calloc(1, sizeof(rom_path_t));
}
// Save the path, turning it into absolute if needed. // Save the path, turning it into absolute if needed.
if (!plat_path_abs((char*) path)) { if (!plat_path_abs((char*) path)) {