From 221522cf995cbcd39c956f66423a26bbccae8f72 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 16 Oct 2020 04:49:14 +0200 Subject: Matrix: stream download to rapidjson parser --- src/Program.c | 136 +++++++++++++++++++++++----------------------------------- 1 file changed, 53 insertions(+), 83 deletions(-) (limited to 'src/Program.c') diff --git a/src/Program.c b/src/Program.c index 2307798..fc80e5e 100644 --- a/src/Program.c +++ b/src/Program.c @@ -11,7 +11,7 @@ #define READ_END 0 #define WRITE_END 1 -int exec_program(const char **args, ProgramOutputCallback output_callback, void *userdata) { +int exec_program_pipe(const char **args, ReadProgram *read_program) { /* 1 arguments */ if(args[0] == NULL) return -1; @@ -49,58 +49,68 @@ int exec_program(const char **args, ProgramOutputCallback output_callback, void _exit(127); } else { /* parent */ close(fd[WRITE_END]); + read_program->pid = pid; + read_program->read_fd = fd[READ_END]; + return 0; + } +} - int result = 0; - int status; - - char buffer[4097]; +int exec_program(const char **args, ProgramOutputCallback output_callback, void *userdata) { + ReadProgram read_program; + int res = exec_program_pipe(args, &read_program); + if(res != 0) + return res; - for(;;) { - ssize_t bytes_read = read(fd[READ_END], buffer, sizeof(buffer) - 1); - if(bytes_read == 0) { - break; - } else if(bytes_read == -1) { - int err = errno; - fprintf(stderr, "Failed to read from pipe to program %s, error: %s\n", args[0], strerror(err)); - result = -err; - goto cleanup; - } + int result = 0; + int status; - buffer[bytes_read] = '\0'; - if(output_callback && output_callback(buffer, bytes_read, userdata) != 0) - break; - } + char buffer[4097]; - if(waitpid(pid, &status, 0) == -1) { - perror("waitpid failed"); - result = -5; + for(;;) { + ssize_t bytes_read = read(read_program.read_fd, buffer, sizeof(buffer) - 1); + if(bytes_read == 0) { + break; + } else if(bytes_read == -1) { + int err = errno; + fprintf(stderr, "Failed to read from pipe to program %s, error: %s\n", args[0], strerror(err)); + result = -err; goto cleanup; } - if(!WIFEXITED(status)) { - result = -4; - goto cleanup; - } + buffer[bytes_read] = '\0'; + if(output_callback && output_callback(buffer, bytes_read, userdata) != 0) + break; + } - int exit_status = WEXITSTATUS(status); - if(exit_status != 0) { - fprintf(stderr, "Failed to execute program ("); - const char **arg = args; - while(*arg) { - if(arg != args) - fputc(' ', stderr); - fprintf(stderr, "'%s'", *arg); - ++arg; - } - fprintf(stderr, "), exit status %d\n", exit_status); - result = -exit_status; - goto cleanup; - } + if(waitpid(read_program.pid, &status, 0) == -1) { + perror("waitpid failed"); + result = -5; + goto cleanup; + } - cleanup: - close(fd[READ_END]); - return result; + if(!WIFEXITED(status)) { + result = -4; + goto cleanup; + } + + int exit_status = WEXITSTATUS(status); + if(exit_status != 0) { + fprintf(stderr, "Failed to execute program ("); + const char **arg = args; + while(*arg) { + if(arg != args) + fputc(' ', stderr); + fprintf(stderr, "'%s'", *arg); + ++arg; + } + fprintf(stderr, "), exit status %d\n", exit_status); + result = -exit_status; + goto cleanup; } + + cleanup: + close(read_program.read_fd); + return result; } int wait_program(pid_t process_id) { @@ -186,43 +196,3 @@ int exec_program_async(const char **args, pid_t *result_process_id) { } return 0; } - -#if 0 -int program_pipe_write(ProgramPipe *self, const char *data, size_t size) { - ssize_t bytes_written = write(self->write_fd, data, size); - if(bytes_written == -1) { - int err = errno; - perror("Failed to write to pipe to program"); - return -err; - } - return 0; -} - -int program_pipe_read(ProgramPipe *self, ProgramOutputCallback output_callback, void *userdata) { - char buffer[2048]; - - for(;;) { - ssize_t bytes_read = read(self->read_fd, buffer, sizeof(buffer) - 1); - if(bytes_read == 0) { - break; - } else if(bytes_read == -1) { - int err = errno; - perror("Failed to read from pipe to program"); - return -err; - } - - buffer[bytes_read] = '\0'; - if(output_callback && output_callback(buffer, bytes_read, userdata) != 0) - break; - } - - return 0; -} - -void program_pipe_close(ProgramPipe *self) { - close(self->read_fd); - close(self->write_fd); - self->read_fd = -1; - self->write_fd = -1; -} -#endif -- cgit v1.2.3