summaryrefslogtreecommitdiff
path: root/terminal.c
diff options
context:
space:
mode:
authorMichael Pavone <pavone@retrodev.com>2015-07-26 01:11:04 -0700
committerMichael Pavone <pavone@retrodev.com>2015-07-26 01:11:04 -0700
commitbee8b42029900ca034675c54d98813a61ca4407a (patch)
treeee5d02129ed6f07607bb262b3960e99f3bc379e0 /terminal.c
parent25fc1e88deea8253d9d4b8084485e176eb69abd0 (diff)
Spawn a terminal for the debugger when needed if we are not already attached to one
Diffstat (limited to 'terminal.c')
-rw-r--r--terminal.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/terminal.c b/terminal.c
new file mode 100644
index 0000000..0e952f2
--- /dev/null
+++ b/terminal.c
@@ -0,0 +1,65 @@
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <signal.h>
+#include "util.h"
+#include "terminal.h"
+
+pid_t child;
+
+void cleanup_terminal()
+{
+ kill(child, SIGKILL);
+ unlink(INPUT_PATH);
+ unlink(OUTPUT_PATH);
+}
+
+void init_terminal()
+{
+ static char init_done;
+ if (!init_done) {
+ if (!(isatty(STDIN_FILENO) && isatty(STDOUT_FILENO))) {
+#ifndef __APPLE__
+ //check to see if x-terminal-emulator exists, just use xterm if it doesn't
+ char *term = system("which x-terminal-emulator > /dev/null") ? "xterm" : "x-terminal-emulator";
+#endif
+ //get rid of FIFO's if they already exist
+ unlink(INPUT_PATH);
+ unlink(OUTPUT_PATH);
+ //create FIFOs for talking to helper process in terminal app
+ mkfifo(INPUT_PATH, 0666);
+ mkfifo(OUTPUT_PATH, 0666);
+
+ //close existing file descriptors
+ close(STDIN_FILENO);
+ close(STDOUT_FILENO);
+ close(STDERR_FILENO);
+
+ child = fork();
+ if (child == -1) {
+ //error, oh well
+ warning("Failed to fork for terminal spawn");
+ } else if (!child) {
+ //child process, exec our terminal emulator
+#ifdef __APPLE__
+ execlp("open", "open", "./termhelper", NULL);
+#else
+ execlp(term, term, "-title", "BlastEm Debugger", "-e", "./termhelper", NULL);
+#endif
+ } else {
+ //connect to the FIFOs, these will block so order is important
+ open(INPUT_PATH, O_RDONLY);
+ open(OUTPUT_PATH, O_WRONLY);
+ atexit(cleanup_terminal);
+ if (-1 == dup(STDOUT_FILENO)) {
+ fatal_error("failed to dup STDOUT to STDERR after terminal fork");
+ }
+ }
+ }
+
+ init_done = 1;
+ }
+}