]> git.ipfire.org Git - people/ric9/pakfire.git/commitdiff
pty: Add helper function to stream a buffer into stdin
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 8 Jan 2025 11:11:39 +0000 (11:11 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 8 Jan 2025 11:11:39 +0000 (11:11 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/pty.c
src/pakfire/pty.h

index 129f501a88f6cec5f05c6daf8f33bddbf448611a..cbc1ad23550b728c80c2b3bc18ffd844cd256d7e 100644 (file)
@@ -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;
+}
index 0a8856e89dd6d84def515e5d915e5bb669d66564..38bccb2956d4ebd6e54c077cf9a3dfe6d4781997 100644 (file)
@@ -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 */