From a0da4ce1a3531e205e769fc64d327630259fdae1 Mon Sep 17 00:00:00 2001 From: VegOwOtenks Date: Sun, 22 Sep 2024 22:26:33 +0200 Subject: [PATCH] Subprocess with pipe capturing --- src/Subprocess/CMakeLists.txt | 1 + src/Subprocess/Subprocess.c | 109 ++++++++++++++++++++++++++++++++++ src/Subprocess/Subprocess.h | 43 ++++++++++++++ tests/CMakeLists.txt | 8 +++ tests/Subprocess.test.c | 31 ++++++++++ tests/regex.test.c | 33 ++++++++++ 6 files changed, 225 insertions(+) create mode 100644 src/Subprocess/CMakeLists.txt create mode 100644 src/Subprocess/Subprocess.c create mode 100644 src/Subprocess/Subprocess.h create mode 100644 tests/Subprocess.test.c diff --git a/src/Subprocess/CMakeLists.txt b/src/Subprocess/CMakeLists.txt new file mode 100644 index 0000000..0b775ae --- /dev/null +++ b/src/Subprocess/CMakeLists.txt @@ -0,0 +1 @@ +add_library(Subprocess STATIC Subprocess.c) diff --git a/src/Subprocess/Subprocess.c b/src/Subprocess/Subprocess.c new file mode 100644 index 0000000..d48c96f --- /dev/null +++ b/src/Subprocess/Subprocess.c @@ -0,0 +1,109 @@ +#include "Subprocess.h" +#include +#include +#include +#include + +typedef struct PipePair_s { + int read; + int write; +} PipePair; + +static int _PipePair_Create(PipePair* pair) +{ + int pipefds[2]; + int pipe_call = pipe(pipefds); + + if (pipe_call == 0) { + pair->read = pipefds[0]; + pair->write = pipefds[1]; + } + + return pipe_call; +} + +static void _PipePair_Destroy(PipePair* pair) +{ + close(pair->read); + close(pair->write); +} + +int Subprocess_Create(Subprocess* subprocess, char* file, char** argv) +{ + if (subprocess == NULL) { + return EDESTADDRREQ; + } + if (file == NULL) { + return EINVAL; + } + if (argv == NULL) { + return EINVAL; + } + if (subprocess->in.type != SUBPROCESSREDIRECTIONTYPE_PIPE) { + return ENOTSUP; + } + if (subprocess->out.type != SUBPROCESSREDIRECTIONTYPE_PIPE) { + return ENOTSUP; + } + if (subprocess->err.type != SUBPROCESSREDIRECTIONTYPE_PIPE) { + return ENOTSUP; + } + + // TODO: Implement FILE redirection + + // Create pipe pairs + PipePair in_pair, out_pair, err_pair; + if (_PipePair_Create(&in_pair)) { + return errno; + } + if (_PipePair_Create(&out_pair)) { + _PipePair_Destroy(&in_pair); + return errno; + } + if (_PipePair_Create(&err_pair)) { + _PipePair_Destroy(&in_pair); + _PipePair_Destroy(&out_pair); + return errno; + } + + pid_t fork_result = fork(); + if (fork_result == 0) { + // child + // TODO: Check errors + + close(in_pair.write); + close(out_pair.read); + close(err_pair.read); + dup2(in_pair.read, STDIN_FILENO); + dup2(out_pair.write, STDOUT_FILENO); + dup2(err_pair.write, STDERR_FILENO); + execvp(file, argv); + + } else if (fork_result != -1) { + // parent + + close(in_pair.read); + close(out_pair.write); + close(err_pair.write); + subprocess->in.get.pipefd = in_pair.write; + subprocess->out.get.pipefd = out_pair.read; + subprocess->err.get.pipefd = err_pair.read; + subprocess->child_pid = fork_result; + + } else { + // error + return errno; + } + + return EXIT_SUCCESS; +} + + +void Subprocess_Destroy(Subprocess* subprocess) +{ + waitpid(subprocess->child_pid, NULL, 0); + + if (subprocess->in .type == SUBPROCESSREDIRECTIONTYPE_PIPE) close(subprocess->in.get.pipefd); + if (subprocess->out.type == SUBPROCESSREDIRECTIONTYPE_PIPE) close(subprocess->out.get.pipefd); + if (subprocess->err.type == SUBPROCESSREDIRECTIONTYPE_PIPE) close(subprocess->err.get.pipefd); +} diff --git a/src/Subprocess/Subprocess.h b/src/Subprocess/Subprocess.h new file mode 100644 index 0000000..dac0c43 --- /dev/null +++ b/src/Subprocess/Subprocess.h @@ -0,0 +1,43 @@ +#ifndef UTILITIEC_SUBPROCESS_H +#define UTILITIEC_SUBPROCESS_H + +#include "../StringView/StringView.h" +#include + +// TODO: Implement FILE redirection +enum SubprocessRedirectionType { + SUBPROCESSREDIRECTIONTYPE_PIPE, + SUBPROCESSREDIRECTIONTYPE_FILE, +}; + +union SubprocessRedirectionContent { + int pipefd; + StringView file_name; +}; + +typedef struct SubprocessRedirection_s { + enum SubprocessRedirectionType type; + union SubprocessRedirectionContent get; +} SubprocessRedirection; + +typedef struct Subprocess_t { + pid_t child_pid; + SubprocessRedirection in; + SubprocessRedirection out; + SubprocessRedirection err; +} Subprocess; + + +#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) + + +int Subprocess_Create(Subprocess* subprocess, char* file, char** argv); +void Subprocess_Destroy(Subprocess* subprocess); + + +#else +// TODO: Implement for Windows with a windows testing machine +#error "This module is not implemented for your platform yet" +#endif // platform + +#endif // header diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 881b58c..95df5c2 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -44,3 +44,11 @@ target_link_libraries(BinaryTree-test pointers allocator-interface ) + +add_executable(Subprocess-test Subprocess.test.c) +target_link_libraries(Subprocess-test + Subprocess + threading + pointers + allocator-interface +) diff --git a/tests/Subprocess.test.c b/tests/Subprocess.test.c new file mode 100644 index 0000000..7f55360 --- /dev/null +++ b/tests/Subprocess.test.c @@ -0,0 +1,31 @@ +#include "../src/Subprocess/Subprocess.h" +#include +#include +#include +#include +#include + +char read_buffer[128]; + +void testEcho() +{ + Subprocess sub; + sub.in .type = SUBPROCESSREDIRECTIONTYPE_PIPE; + sub.out.type = SUBPROCESSREDIRECTIONTYPE_PIPE; + sub.err.type = SUBPROCESSREDIRECTIONTYPE_PIPE; + + char* argv[] = { "echo", "Hello World!", NULL }; + + assert(EXIT_SUCCESS == Subprocess_Create(&sub, "/usr/bin/echo", argv)); + ssize_t read_size = read(sub.out.get.pipefd, read_buffer, 128); + assert(read_size == 13); + assert(strcmp(read_buffer, "Hello World!\n") == 0); + assert(read(sub.out.get.pipefd, read_buffer, 128) == 0); + Subprocess_Destroy(&sub); +} + +int main() +{ + testEcho(); + return 0; +} diff --git a/tests/regex.test.c b/tests/regex.test.c index b4bf432..091d80a 100644 --- a/tests/regex.test.c +++ b/tests/regex.test.c @@ -79,6 +79,38 @@ void testGroup(void) assert(RegexMatch_HaveNumberedCapture(&match, 1)); assert(RegexMatch_HaveNumberedCapture(&match, 2)); + Regex_Destroy(®ex); + RegexMatch_Destroy(&match); + assert(allocator.reserved == 0); + Allocator_DestroySystemAllocator(&allocator); + return; +} + +void testOption(void) +{ + StringView regex_string = StringView_FromString("abc|def|"); + StringView match_string_0 = StringView_FromString("abc"); + StringView match_string_1 = StringView_FromString("def"); + StringView match_string_2 = StringView_FromString(""); + allocator_t allocator; + Allocator_CreateSystemAllocator(&allocator); + + Regex regex; + assert(Regex_Create(®ex, &allocator) == EXIT_SUCCESS); + + assert(Regex_Compile(®ex, regex_string) == EXIT_SUCCESS); + + RegexMatch match; + + assert(Regex_FirstMatch(®ex, match_string_0, &match) == EXIT_SUCCESS); + assert(StringView_Equal(match.match, match_string_0)); + + assert(Regex_FirstMatch(®ex, match_string_1, &match) == EXIT_SUCCESS); + assert(StringView_Equal(match.match, match_string_1)); + + assert(Regex_FirstMatch(®ex, match_string_2, &match) == EXIT_SUCCESS); + assert(StringView_Equal(match.match, match_string_2)); + Regex_Destroy(®ex); assert(allocator.reserved == 0); Allocator_DestroySystemAllocator(&allocator); @@ -118,6 +150,7 @@ int main() testLiteral(); testBackslash(); testGroup(); + testOption(); testQuantifier(); return 0; }