summaryrefslogtreecommitdiff
path: root/termhelper.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 /termhelper.c
parent25fc1e88deea8253d9d4b8084485e176eb69abd0 (diff)
Spawn a terminal for the debugger when needed if we are not already attached to one
Diffstat (limited to 'termhelper.c')
-rw-r--r--termhelper.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/termhelper.c b/termhelper.c
new file mode 100644
index 0000000..a5cf17c
--- /dev/null
+++ b/termhelper.c
@@ -0,0 +1,44 @@
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include "terminal.h"
+
+char buf[4096];
+
+void copy_data(int to, int from)
+{
+ ssize_t bytes = read(from, buf, sizeof(buf));
+ while (bytes > 0)
+ {
+ ssize_t written = write(to, buf, bytes);
+ if (written == -1) {
+ exit(1);
+ }
+ bytes -= written;
+ }
+}
+
+int main(int argc, char **argv)
+{
+ //these will block so order is important
+ int input_fd = open(INPUT_PATH, O_WRONLY);
+ int output_fd = open(OUTPUT_PATH, O_RDONLY);
+ fd_set read_fds;
+ FD_ZERO(&read_fds);
+ for (;;)
+ {
+ FD_SET(STDIN_FILENO, &read_fds);
+ FD_SET(output_fd, &read_fds);
+ select(output_fd+1, &read_fds, NULL, NULL, NULL);
+
+ if (FD_ISSET(STDIN_FILENO, &read_fds)) {
+ copy_data(input_fd, STDIN_FILENO);
+ }
+ if (FD_ISSET(output_fd, &read_fds)) {
+ copy_data(STDOUT_FILENO, output_fd);
+ }
+ }
+ return 0;
+}