This commit is contained in:
~d6 2023-12-13 12:20:05 -05:00
parent 214d504311
commit 05cf2c6ef3
4 changed files with 18 additions and 51 deletions

View File

@ -62,8 +62,6 @@ handle_sigchld(int sig)
UxnSubprocess *child = find_child_by_pid(pid);
if (child != NULL) {
child->running = WEXITSTATUS(wstatus) - 256;
if(CONSOLE_DEBUG)
fprintf(stderr, "handle_sigchld: %d -> child %d (%d) -> %d\n", sig, child->id, pid, child->running);
if(child_ptys(child->mode))
close(child->pty);
else {
@ -75,8 +73,6 @@ handle_sigchld(int sig)
return;
}
}
if(CONSOLE_DEBUG)
fprintf(stderr, "handle_sigchld: no child found", sig);
}
static void
@ -105,7 +101,6 @@ start_fork_pty(UxnSubprocess *child, int mode)
static void
start_fork_pipe(UxnSubprocess *child, int mode)
{
//printf("start_fork_pipe(..., %d)\n", mode);
if(child_reads(mode)) {
/* parent writes to child's stdin */
if(pipe(to_child_fd) == -1) {
@ -147,19 +142,9 @@ start_fork_pipe(UxnSubprocess *child, int mode)
child->fd_in = child_reads(mode) ? to_child_fd[1] : -1;
child->fd_out = child_writes(mode) ? from_child_fd[0] : -1;
} while (child->pid != pid);
//printf("child %d: pid=%d mode=%d running=%d in=%d out=%d\n", child->id, child->pid, child->mode, child->running, child->fd_in, child->fd_out);
if(CONSOLE_DEBUG)
fprintf(stderr, "child has pid %d\n", child->pid);
if(child_reads(mode)) close(to_child_fd[0]);
if(child_writes(mode)) close(from_child_fd[1]);
}
/* /\* TODO *\/ */
/* for(int n = 0; n < CONSOLE_MAX_CHILDREN; n++) { */
/* UxnSubprocess *child = &children[n]; */
/* printf("child: %d/%d (%d, %d)\n", n, child->id, child->fd_out, child->pid); */
/* } */
}
static void
@ -174,8 +159,6 @@ proc_put(Uxn *u, Uint8 *d)
{
Uint8 c = d[0xa];
int n = d[0xe] & 0x3;
if (CONSOLE_DEBUG)
fprintf(stderr, " fd %d: write %02x\n", children[n].fd_in, c);
write(children[n].fd_in, &c, 1);
fdatasync(children[n].fd_in);
}
@ -190,13 +173,8 @@ host_execute(Uxn *u, Uint8 *d)
UxnSubprocess *child = &children[opts & 0x3];
fork_args[2] = cmd;
if(CONSOLE_DEBUG)
fprintf(stderr, "running execute: %s (#%02x)\n", cmd, opts);
if(child->pid) {
if(CONSOLE_DEBUG)
fprintf(stderr, " killing previous child: %d\n", child->pid);
if(child->pid)
kill(child->pid, 9);
}
if(opts >= 0x80)
start_fork_pty(child, mode);
@ -208,8 +186,8 @@ static void
host_response(Uxn *u, char *s)
{
for(int i = 0;; i++)
console_input(u, 0x5, s[i], CONSOLE_HOST);
console_input(u, 0x5, '\0', CONSOLE_HOST_END);
console_input(u, s[i], CONSOLE_HOST);
console_input(u, '\0', CONSOLE_HOST_END);
}
static void
@ -291,20 +269,17 @@ console_monitor(Uxn *u)
fflush(stdout);
for(int n = 0; n < CONSOLE_MAX_CHILDREN; n++) {
UxnSubprocess *child = &children[n];
/* printf("child: %d/%d (%d, %d)\n", n, child->id, child->fd_out, child->pid); */
if(child->running < 0) {
/* printf("child ready for clean up: %d\n", n); */
/* fflush(stdout); */
Uint8 status = child->running + 256;
child->running = 0;
console_input(u, 0x4, status, CONSOLE_CHILD_EXIT | n);
console_input(u, status, CONSOLE_CHILD_EXIT | n);
}
}
}
// TODO ignore port
int
console_input(Uxn *u, int port, char c, int type)
console_input(Uxn *u, char c, int type)
{
Uint8 *d = &u->dev[0x10];
d[0x2] = c;
@ -317,8 +292,8 @@ console_listen(Uxn *u, int i, int argc, char **argv)
{
for(; i < argc; i++) {
char *p = argv[i];
while(*p) console_input(u, 0x2, *p++, CONSOLE_ARG);
console_input(u, 0x2, '\n', i == argc - 1 ? CONSOLE_ARG_END : CONSOLE_ARG_SPACER);
while(*p) console_input(u, *p++, CONSOLE_ARG);
console_input(u, '\n', i == argc - 1 ? CONSOLE_ARG_END : CONSOLE_ARG_SPACER);
}
}

View File

@ -11,8 +11,6 @@ WITH REGARD TO THIS SOFTWARE.
#define CONSOLE_VERSION 1
#define CONSOLE_DEBUG 0
#define CONSOLE_NONE 0x00
#define CONSOLE_STDIN 0x01
#define CONSOLE_ARG 0x02
@ -39,7 +37,7 @@ typedef struct UxnSubprocess {
void init_console(void);
UxnSubprocess *get_child(int n);
void console_monitor(Uxn *u);
int console_input(Uxn *u, int port, char c, int type);
int console_input(Uxn *u, char c, int type);
void console_listen(Uxn *u, int i, int argc, char **argv);
Uint8 console_dei(Uxn *u, Uint8 addr);
void console_deo(Uxn *u, Uint8 *d, Uint8 port);

View File

@ -152,16 +152,16 @@ toggle_scale(Uxn *u)
/* returns true if the fd ended (has been closed), false otherwise */
static int
handle_input(Uxn *u, struct pollfd pollfd, int port, char *coninp, int argdata, int argend)
handle_input(Uxn *u, struct pollfd pollfd, char *coninp, int argdata, int argend)
{
if((pollfd.revents & POLLIN) != 0) {
int n = read(pollfd.fd, coninp, CONINBUFSIZE - 1);
coninp[n] = 0;
if(n == 0)
console_input(u, port, 0, argend);
console_input(u, 0, argend);
else
for(int i = 0; i < n; i++)
console_input(u, port, coninp[i], argdata);
console_input(u, coninp[i], argdata);
return n == 0;
} else
return 0;
@ -294,11 +294,11 @@ emu_run(Uxn *u, char *rom)
}
/* read input from stdin */
handle_input(u, fds[2], 0x2, coninp, CONSOLE_STDIN, CONSOLE_STDIN_END);
handle_input(u, fds[2], coninp, CONSOLE_STDIN, CONSOLE_STDIN_END);
/* read input from child processes */
for(i = 0; i < 4; i++)
handle_input(u, fds[i + 3], 0x4, coninp, CONSOLE_CHILD_DATA | i, CONSOLE_CHILD_END | i);
handle_input(u, fds[i + 3], coninp, CONSOLE_CHILD_DATA | i, CONSOLE_CHILD_END | i);
/* check to see if any children exited */
console_monitor(u);

View File

@ -50,17 +50,11 @@ handle_input(Uxn *u, int fd, int port, int argdata, int argend)
{
char buf[32];
int n = read(fd, &buf, 32);
if(n == 0) {
if(CONSOLE_DEBUG)
fprintf(stderr, " fd %d: closed\n", fd);
console_input(u, port, 0x00, argend);
} else {
for(int i = 0; i < n; i++) {
if(CONSOLE_DEBUG)
fprintf(stderr, " fd %d: read %02x\n", fd, buf[i]);
console_input(u, port, buf[i], argdata);
}
}
if(n == 0)
console_input(u, 0x00, argend);
else
for(int i = 0; i < n; i++)
console_input(u, buf[i], argdata);
}
static int