changeset 2425:794ba17f0716

Make termhelper work when current working directory is not the one that contains blastem
author Michael Pavone <pavone@retrodev.com>
date Wed, 24 Jan 2024 20:18:17 -0800
parents 767ec72acca7
children cde4ea2b4929
files terminal.c util.c util.h
diffstat 3 files changed, 20 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/terminal.c	Wed Jan 24 18:51:44 2024 -0800
+++ b/terminal.c	Wed Jan 24 20:18:17 2024 -0800
@@ -26,6 +26,7 @@
 
 void init_terminal()
 {
+#ifndef IS_LIB
 	if (!init_done) {
 		if (!(isatty(STDIN_FILENO) && isatty(STDOUT_FILENO))) {
 #ifndef __APPLE__
@@ -50,11 +51,13 @@
 				warning("Failed to fork for terminal spawn");
 			} else if (!child) {
 				//child process, exec our terminal emulator
+				char *termhelper = bundled_file_path("termhelper");
 #ifdef __APPLE__
-				execlp("open", "open", "./termhelper", NULL);
+				execlp("open", "open", termhelper, NULL);
 #else
-				execlp(term, term, "-title", "BlastEm Debugger", "-e", "./termhelper", NULL);
+				execlp(term, term, "-title", "BlastEm Debugger", "-e", termhelper, NULL);
 #endif
+				free(termhelper);
 			} else {
 				//connect to the FIFOs, these will block so order is important
 				open(INPUT_PATH, O_RDONLY);
@@ -68,4 +71,5 @@
 
 		init_done = 1;
 	}
+#endif
 }
--- a/util.c	Wed Jan 24 18:51:44 2024 -0800
+++ b/util.c	Wed Jan 24 20:18:17 2024 -0800
@@ -1016,21 +1016,29 @@
 #else
 
 #ifndef IS_LIB
-char *read_bundled_file(char *name, uint32_t *sizeret)
+char *bundled_file_path(char *name)
 {
 #ifdef DATA_PATH
 	char *data_dir = DATA_PATH;
 #else
 	char *data_dir = get_exe_dir();
 	if (!data_dir) {
+		return NULL;
+	}
+#endif
+	char const *pieces[] = {data_dir, PATH_SEP, name};
+	return alloc_concat_m(3, pieces);
+}
+
+char *read_bundled_file(char *name, uint32_t *sizeret)
+{
+	char *path = bundled_file_path(name);
+	if (!path) {
 		if (sizeret) {
 			*sizeret = -1;
 		}
 		return NULL;
 	}
-#endif
-	char const *pieces[] = {data_dir, PATH_SEP, name};
-	char *path = alloc_concat_m(3, pieces);
 	FILE *f = fopen(path, "rb");
 	free(path);
 	if (!f) {
--- a/util.h	Wed Jan 24 18:51:44 2024 -0800
+++ b/util.h	Wed Jan 24 20:18:17 2024 -0800
@@ -66,6 +66,8 @@
 char const *get_config_dir();
 //Returns an appropriate path for saving non-config data like savestates
 char const *get_userdata_dir();
+//Returns the path of a file bundled with the executable
+char *bundled_file_path(char *name);
 //Reads a file bundled with the executable
 char *read_bundled_file(char *name, uint32_t *sizeret);
 //Retunrs an array of normal files and directories residing in a directory