// IO Flags
enum pakfire_pty_io {
- PAKFIRE_PTY_READY_TO_READ = (1 << 0),
- PAKFIRE_PTY_READY_TO_WRITE = (1 << 1),
- PAKFIRE_PTY_HANGUP = (1 << 2),
- PAKFIRE_PTY_EOF = (1 << 3),
- PAKFIRE_PTY_MAP_CRNL = (1 << 4),
+ PAKFIRE_PTY_CONNECT = (1 << 0),
+ PAKFIRE_PTY_READY_TO_READ = (1 << 1),
+ PAKFIRE_PTY_READY_TO_WRITE = (1 << 2),
+ PAKFIRE_PTY_HANGUP = (1 << 3),
+ PAKFIRE_PTY_EOF = (1 << 4),
+ PAKFIRE_PTY_MAP_CRNL = (1 << 5),
} io;
// Event Source
pty->state = PAKFIRE_PTY_STATE_FORWARDING;
// Connect to standard input
- if (pty->flags & PAKFIRE_PTY_CONNECT_STDIN) {
+ if (pakfire_pty_has_flag(pty, PAKFIRE_PTY_INTERACTIVE)) {
pty->stdin.fd = pakfire_pty_reopen(pty, STDIN_FILENO, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
if (pty->stdin.fd < 0) {
DEBUG(pty->ctx, "Could not re-open standard input: %s. Ignoring.\n", strerror(-pty->stdin.fd));
pty->stdout.close_fd = 1;
// Connect to standard output
- } else if (pty->flags & PAKFIRE_PTY_CONNECT_STDOUT) {
+ } else if (pakfire_pty_has_flag(pty, PAKFIRE_PTY_INTERACTIVE)) {
pty->stdout.fd = pakfire_pty_reopen(pty, STDOUT_FILENO, O_WRONLY|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
if (pty->stdout.fd < 0) {
DEBUG(pty->ctx, "Could not re-open standard output: %s. Ignoring.\n", strerror(-pty->stdout.fd));
// Store the flags
p->flags = flags;
+ // Connect stdout if we want to capture the output
if (pakfire_pty_has_flag(p, PAKFIRE_PTY_CAPTURE_OUTPUT))
- p->flags |= PAKFIRE_PTY_CONNECT_STDOUT | PAKFIRE_PTY_CONNECT_STDERR;
+ p->stdout.io |= PAKFIRE_PTY_CONNECT;
+
+ // Connect everything when we are in interactive mode
+ else if (pakfire_pty_has_flag(p, PAKFIRE_PTY_INTERACTIVE)) {
+ p->stdin.io |= PAKFIRE_PTY_CONNECT;
+ p->stdout.io |= PAKFIRE_PTY_CONNECT;
+ }
// Initialize the master file descriptor
p->master.fd = -EBADF;
DEBUG(pty->ctx, "Opened a new terminal %d\n", fd);
// Connect the new terminal to standard input
- if (pakfire_pty_has_flag(pty, PAKFIRE_PTY_CONNECT_STDIN)) {
+ if (pty->stdin.io & PAKFIRE_PTY_CONNECT) {
r = dup2(fd, STDIN_FILENO);
if (r < 0) {
ERROR(pty->ctx, "Failed to open standard input: %s\n", strerror(errno));
}
// Connect the new terminal to standard output
- if (pakfire_pty_has_flag(pty, PAKFIRE_PTY_CONNECT_STDOUT)) {
+ if (pty->stdout.io & PAKFIRE_PTY_CONNECT) {
r = dup2(fd, STDOUT_FILENO);
if (r < 0) {
ERROR(pty->ctx, "Failed to open standard output: %s\n", strerror(errno));
goto ERROR;
}
- // Otherwise we connect standard output to /dev/null
- } else {
- r = pakfire_pty_connect_null(pty, STDOUT_FILENO);
- if (r < 0)
- goto ERROR;
- }
-
- // Connect the new terminal to standard error
- if (pakfire_pty_has_flag(pty, PAKFIRE_PTY_CONNECT_STDERR)) {
+ // Connect stderr, too
r = dup2(fd, STDERR_FILENO);
if (r < 0) {
ERROR(pty->ctx, "Failed to open standard error: %s\n", strerror(errno));
goto ERROR;
}
- // Otherwise we connect standard error to /dev/null
+ // Otherwise we connect standard output to /dev/null
} else {
+ r = pakfire_pty_connect_null(pty, STDOUT_FILENO);
+ if (r < 0)
+ goto ERROR;
+
+ // Also redirect stderr
r = pakfire_pty_connect_null(pty, STDERR_FILENO);
if (r < 0)
goto ERROR;
pty->stdin.callbacks.data = data;
// We are now ready to read
- pty->stdin.io |= PAKFIRE_PTY_READY_TO_READ;
+ pty->stdin.io |=
+ PAKFIRE_PTY_CONNECT |
+ PAKFIRE_PTY_READY_TO_READ;
}
void pakfire_pty_set_stdout_callback(struct pakfire_pty* pty,
pty->stdout.callbacks.data = data;
// We are now ready to write
- pty->stdout.io |= PAKFIRE_PTY_READY_TO_WRITE|PAKFIRE_PTY_MAP_CRNL;
-
- // Actually connect standard output and error
- pty->flags |= PAKFIRE_PTY_CONNECT_STDOUT | PAKFIRE_PTY_CONNECT_STDERR;
+ pty->stdout.io |=
+ PAKFIRE_PTY_CONNECT |
+ PAKFIRE_PTY_READY_TO_WRITE |
+ PAKFIRE_PTY_MAP_CRNL;
}
ssize_t pakfire_pty_send_buffer(struct pakfire_ctx* ctx,