annotate terminal.c @ 2496:187bc857a76a default tip

Fix bug in MED mapper protection bit implementation
author Michael Pavone <pavone@retrodev.com>
date Sun, 28 Apr 2024 23:33:11 -0700
parents 794ba17f0716
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
794
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1 #include <sys/types.h>
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
2 #include <sys/stat.h>
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
3 #include <unistd.h>
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
4 #include <fcntl.h>
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
5 #include <stdlib.h>
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
6 #include <stdint.h>
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
7 #include <signal.h>
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
8 #include "util.h"
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
9 #include "terminal.h"
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
10
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
11 pid_t child;
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
12
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
13 void cleanup_terminal()
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
14 {
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
15 kill(child, SIGKILL);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
16 unlink(INPUT_PATH);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
17 unlink(OUTPUT_PATH);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
18 }
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
19
832
0433fdd9ba66 Added a command line option to force BlastEm to not open a new terminal even if it detects that stdin/out are not terminals
Michael Pavone <pavone@retrodev.com>
parents: 794
diff changeset
20 static char init_done;
0433fdd9ba66 Added a command line option to force BlastEm to not open a new terminal even if it detects that stdin/out are not terminals
Michael Pavone <pavone@retrodev.com>
parents: 794
diff changeset
21
0433fdd9ba66 Added a command line option to force BlastEm to not open a new terminal even if it detects that stdin/out are not terminals
Michael Pavone <pavone@retrodev.com>
parents: 794
diff changeset
22 void force_no_terminal()
0433fdd9ba66 Added a command line option to force BlastEm to not open a new terminal even if it detects that stdin/out are not terminals
Michael Pavone <pavone@retrodev.com>
parents: 794
diff changeset
23 {
0433fdd9ba66 Added a command line option to force BlastEm to not open a new terminal even if it detects that stdin/out are not terminals
Michael Pavone <pavone@retrodev.com>
parents: 794
diff changeset
24 init_done = 1;
0433fdd9ba66 Added a command line option to force BlastEm to not open a new terminal even if it detects that stdin/out are not terminals
Michael Pavone <pavone@retrodev.com>
parents: 794
diff changeset
25 }
0433fdd9ba66 Added a command line option to force BlastEm to not open a new terminal even if it detects that stdin/out are not terminals
Michael Pavone <pavone@retrodev.com>
parents: 794
diff changeset
26
794
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
27 void init_terminal()
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
28 {
2425
794ba17f0716 Make termhelper work when current working directory is not the one that contains blastem
Michael Pavone <pavone@retrodev.com>
parents: 832
diff changeset
29 #ifndef IS_LIB
794
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
30 if (!init_done) {
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
31 if (!(isatty(STDIN_FILENO) && isatty(STDOUT_FILENO))) {
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
32 #ifndef __APPLE__
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
33 //check to see if x-terminal-emulator exists, just use xterm if it doesn't
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
34 char *term = system("which x-terminal-emulator > /dev/null") ? "xterm" : "x-terminal-emulator";
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
35 #endif
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
36 //get rid of FIFO's if they already exist
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
37 unlink(INPUT_PATH);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
38 unlink(OUTPUT_PATH);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
39 //create FIFOs for talking to helper process in terminal app
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
40 mkfifo(INPUT_PATH, 0666);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
41 mkfifo(OUTPUT_PATH, 0666);
832
0433fdd9ba66 Added a command line option to force BlastEm to not open a new terminal even if it detects that stdin/out are not terminals
Michael Pavone <pavone@retrodev.com>
parents: 794
diff changeset
42
794
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
43 //close existing file descriptors
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
44 close(STDIN_FILENO);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
45 close(STDOUT_FILENO);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
46 close(STDERR_FILENO);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
47
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
48 child = fork();
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
49 if (child == -1) {
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
50 //error, oh well
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
51 warning("Failed to fork for terminal spawn");
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
52 } else if (!child) {
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
53 //child process, exec our terminal emulator
2425
794ba17f0716 Make termhelper work when current working directory is not the one that contains blastem
Michael Pavone <pavone@retrodev.com>
parents: 832
diff changeset
54 char *termhelper = bundled_file_path("termhelper");
794
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
55 #ifdef __APPLE__
2425
794ba17f0716 Make termhelper work when current working directory is not the one that contains blastem
Michael Pavone <pavone@retrodev.com>
parents: 832
diff changeset
56 execlp("open", "open", termhelper, NULL);
794
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
57 #else
2425
794ba17f0716 Make termhelper work when current working directory is not the one that contains blastem
Michael Pavone <pavone@retrodev.com>
parents: 832
diff changeset
58 execlp(term, term, "-title", "BlastEm Debugger", "-e", termhelper, NULL);
794
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
59 #endif
2425
794ba17f0716 Make termhelper work when current working directory is not the one that contains blastem
Michael Pavone <pavone@retrodev.com>
parents: 832
diff changeset
60 free(termhelper);
794
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
61 } else {
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
62 //connect to the FIFOs, these will block so order is important
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
63 open(INPUT_PATH, O_RDONLY);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
64 open(OUTPUT_PATH, O_WRONLY);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
65 atexit(cleanup_terminal);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
66 if (-1 == dup(STDOUT_FILENO)) {
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
67 fatal_error("failed to dup STDOUT to STDERR after terminal fork");
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
68 }
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
69 }
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
70 }
832
0433fdd9ba66 Added a command line option to force BlastEm to not open a new terminal even if it detects that stdin/out are not terminals
Michael Pavone <pavone@retrodev.com>
parents: 794
diff changeset
71
794
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
72 init_done = 1;
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
73 }
2425
794ba17f0716 Make termhelper work when current working directory is not the one that contains blastem
Michael Pavone <pavone@retrodev.com>
parents: 832
diff changeset
74 #endif
794
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
75 }