From: Michael Tremer Date: Wed, 8 Jan 2025 11:11:39 +0000 (+0000) Subject: pty: Add helper function to stream a buffer into stdin X-Git-Tag: 0.9.30~504 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bf8a7998255184d0e0198295045c9b00e5c11bb1;p=pakfire.git pty: Add helper function to stream a buffer into stdin Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/pty.c b/src/pakfire/pty.c index 129f501a8..cbc1ad235 100644 --- a/src/pakfire/pty.c +++ b/src/pakfire/pty.c @@ -1331,3 +1331,29 @@ void pakfire_pty_set_stdout_callback(struct pakfire_pty* pty, // We are now ready to write pty->stdout.io |= PAKFIRE_PTY_READY_TO_WRITE|PAKFIRE_PTY_MAP_CRNL; } + +ssize_t pakfire_pty_send_buffer(struct pakfire_ctx* ctx, + void* data, char* buffer, size_t length) { + struct pakfire_pty_buffer* input = data; + + // Check input + if (!input) + return -EINVAL; + + // If there is nothing left to send we are done + if (!input->length) + return 0; + + // Cap length if we have less data to send + if (input->length < length) + length = input->length; + + // Copy the data + memcpy(buffer, input->data, length); + + // Advance the buffer + input->data += length; + input->length -= length; + + return length; +} diff --git a/src/pakfire/pty.h b/src/pakfire/pty.h index 0a8856e89..38bccb295 100644 --- a/src/pakfire/pty.h +++ b/src/pakfire/pty.h @@ -58,4 +58,12 @@ void pakfire_pty_set_stdin_callback(struct pakfire_pty* pty, void pakfire_pty_set_stdout_callback(struct pakfire_pty* pty, pakfire_pty_stdout_callback callback, void* data); +struct pakfire_pty_buffer { + const char* data; + size_t length; +}; + +ssize_t pakfire_pty_send_buffer(struct pakfire_ctx* ctx, + void* data, char* buffer, size_t length); + #endif /* PAKFIRE_PTY_H */