From 4d17423ed184ad0f0343ca62d0b101a7a042941b Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 15 Jul 2020 11:33:37 +0200 Subject: term: add term_scrollback_to_text() and term_view_to_text() These functions extract the current view, or the entire scrollback as an UTF-8 encoded byte buffer. --- terminal.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'terminal.c') diff --git a/terminal.c b/terminal.c index 40bfbc90..d237de97 100644 --- a/terminal.c +++ b/terminal.c @@ -22,6 +22,7 @@ #include "async.h" #include "config.h" +#include "extract.h" #include "grid.h" #include "quirks.h" #include "reaper.h" @@ -2472,3 +2473,60 @@ term_surface_kind(const struct terminal *term, const struct wl_surface *surface) else return TERM_SURF_NONE; } + +static bool +rows_to_text(const struct terminal *term, int start, int end, + char **text, size_t *len) +{ + struct extraction_context *ctx = extract_begin(SELECTION_NONE); + if (ctx == NULL) + return false; + + for (size_t r = start; + r != ((end + 1) & (term->grid->num_rows - 1)); + r = (r + 1) & (term->grid->num_rows - 1)) + { + const struct row *row = term->grid->rows[r]; + assert(row != NULL); + + for (int c = 0; c < term->cols; c++) + if (!extract_one(term, row, &row->cells[c], c, ctx)) + goto out; + } + +out: + return extract_finish(ctx, text, len); +} + +bool +term_scrollback_to_text(const struct terminal *term, char **text, size_t *len) +{ + int start = term->grid->offset + term->rows; + int end = term->grid->offset + term->rows - 1; + + /* If scrollback isn't full yet, this may be NULL, so scan forward + * until we find the first non-NULL row */ + while (term->grid->rows[start] == NULL) { + start++; + start &= term->grid->num_rows - 1; + } + + if (end < 0) + end += term->grid->num_rows; + + while (term->grid->rows[end] == NULL) { + end--; + if (end < 0) + end += term->grid->num_rows; + } + + return rows_to_text(term, start, end, text, len); +} + +bool +term_view_to_text(const struct terminal *term, char **text, size_t *len) +{ + int start = grid_row_absolute_in_view(term->grid, 0); + int end = grid_row_absolute_in_view(term->grid, term->rows - 1); + return rows_to_text(term, start, end, text, len); +} -- cgit v1.3 From 9cdccdd2ac5140a5a265926501a919a3b86f8d84 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 15 Jul 2020 12:39:10 +0200 Subject: term: break out fork+exec functionality to a separate file --- meson.build | 1 + spawn.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ spawn.h | 6 ++++++ terminal.c | 56 +++-------------------------------------------- 4 files changed, 82 insertions(+), 53 deletions(-) create mode 100644 spawn.c create mode 100644 spawn.h (limited to 'terminal.c') diff --git a/meson.build b/meson.build index a64f239f..a7db2105 100644 --- a/meson.build +++ b/meson.build @@ -123,6 +123,7 @@ executable( 'sixel.c', 'sixel.h', 'sixel-hls.c', 'sixel-hls.h', 'slave.c', 'slave.h', + 'spawn.c', 'spawn.h', 'terminal.c', 'terminal.h', 'tokenize.c', 'tokenize.h', 'vt.c', 'vt.h', diff --git a/spawn.c b/spawn.c new file mode 100644 index 00000000..94bbb928 --- /dev/null +++ b/spawn.c @@ -0,0 +1,72 @@ +#include "spawn.h" + +#include +#include +#include + +#include +#include +#include + +#define LOG_MODULE "spawn" +#define LOG_ENABLE_DBG 0 +#include "log.h" + +bool +spawn(struct reaper *reaper, const char *cwd, char *const argv[]) +{ + int pipe_fds[2] = {-1, -1}; + if (pipe2(pipe_fds, O_CLOEXEC) < 0) { + LOG_ERRNO("failed to create pipe"); + goto err; + } + + pid_t pid = fork(); + if (pid < 0) { + LOG_ERRNO("failed to fork"); + goto err; + } + + if (pid == 0) { + /* Child */ + close(pipe_fds[0]); + if ((cwd != NULL && chdir(cwd) < 0) || + //execlp(term->foot_exe, term->foot_exe, NULL) < 0) + execvp(argv[0], argv) < 0) + { + (void)!write(pipe_fds[1], &errno, sizeof(errno)); + _exit(errno); + } + assert(false); + _exit(errno); + } + + /* Parent */ + close(pipe_fds[1]); + + int _errno; + static_assert(sizeof(_errno) == sizeof(errno), "errno size mismatch"); + + ssize_t ret = read(pipe_fds[0], &_errno, sizeof(_errno)); + close(pipe_fds[0]); + + if (ret == 0) { + reaper_add(reaper, pid); + return true; + } else if (ret < 0) { + LOG_ERRNO("failed to read from pipe"); + return false; + } else { + LOG_ERRNO_P("%s: failed to spawn", _errno, argv[0]); + errno = _errno; + waitpid(pid, NULL, 0); + return false; + } + +err: + if (pipe_fds[0] != -1) + close(pipe_fds[0]); + if (pipe_fds[1] != -1) + close(pipe_fds[1]); + return false; +} diff --git a/spawn.h b/spawn.h new file mode 100644 index 00000000..129cadc2 --- /dev/null +++ b/spawn.h @@ -0,0 +1,6 @@ +#pragma once + +#include +#include "reaper.h" + +bool spawn(struct reaper *reaper, const char *cwd, char *const argv[]); diff --git a/terminal.c b/terminal.c index d237de97..53640baa 100644 --- a/terminal.c +++ b/terminal.c @@ -30,6 +30,7 @@ #include "selection.h" #include "sixel.h" #include "slave.h" +#include "spawn.h" #include "util.h" #include "vt.h" @@ -2246,59 +2247,8 @@ term_flash(struct terminal *term, unsigned duration_ms) bool term_spawn_new(const struct terminal *term) { - int pipe_fds[2] = {-1, -1}; - if (pipe2(pipe_fds, O_CLOEXEC) < 0) { - LOG_ERRNO("failed to create pipe"); - goto err; - } - - pid_t pid = fork(); - if (pid < 0) { - LOG_ERRNO("failed to fork new terminal"); - goto err; - } - - if (pid == 0) { - /* Child */ - close(pipe_fds[0]); - if (chdir(term->cwd) < 0 || - execlp(term->foot_exe, term->foot_exe, NULL) < 0) - { - (void)!write(pipe_fds[1], &errno, sizeof(errno)); - _exit(errno); - } - assert(false); - _exit(errno); - } - - /* Parent */ - close(pipe_fds[1]); - - int _errno; - static_assert(sizeof(_errno) == sizeof(errno), "errno size mismatch"); - - ssize_t ret = read(pipe_fds[0], &_errno, sizeof(_errno)); - close(pipe_fds[0]); - - if (ret == 0) { - reaper_add(term->reaper, pid); - return true; - } else if (ret < 0) { - LOG_ERRNO("failed to read from pipe"); - return false; - } else { - LOG_ERRNO_P("%s: failed to spawn new terminal", _errno, term->foot_exe); - errno = _errno; - waitpid(pid, NULL, 0); - return false; - } - -err: - if (pipe_fds[0] != -1) - close(pipe_fds[0]); - if (pipe_fds[1] != -1) - close(pipe_fds[1]); - return false; + return spawn( + term->reaper, term->cwd, (char *const []){term->foot_exe, NULL}); } void -- cgit v1.3 From 69d9ff3f2573dc46d558a5134a93165a7ec212d0 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 15 Jul 2020 13:33:56 +0200 Subject: spawn: add optional stdin/stdout/stderr redirection FDs If not -1, spawn() will redirect the child's stdin/stdout/stderr to these FDs. --- spawn.c | 10 +++++++--- spawn.h | 3 ++- terminal.c | 3 ++- 3 files changed, 11 insertions(+), 5 deletions(-) (limited to 'terminal.c') diff --git a/spawn.c b/spawn.c index 94bbb928..b268bfe5 100644 --- a/spawn.c +++ b/spawn.c @@ -13,7 +13,8 @@ #include "log.h" bool -spawn(struct reaper *reaper, const char *cwd, char *const argv[]) +spawn(struct reaper *reaper, const char *cwd, char *const argv[], + int stdin_fd, int stdout_fd, int stderr_fd) { int pipe_fds[2] = {-1, -1}; if (pipe2(pipe_fds, O_CLOEXEC) < 0) { @@ -30,8 +31,11 @@ spawn(struct reaper *reaper, const char *cwd, char *const argv[]) if (pid == 0) { /* Child */ close(pipe_fds[0]); - if ((cwd != NULL && chdir(cwd) < 0) || - //execlp(term->foot_exe, term->foot_exe, NULL) < 0) + + if ((stdin_fd >= 0 && (dup2(stdin_fd, STDIN_FILENO) < 0 || close(stdin_fd) < 0)) || + (stdout_fd >= 0 && (dup2(stdout_fd, STDOUT_FILENO) < 0 || close(stdout_fd) < 0)) || + (stderr_fd >= 0 && (dup2(stderr_fd, STDERR_FILENO) < 0 || close(stderr_fd) < 0)) || + (cwd != NULL && chdir(cwd) < 0) || execvp(argv[0], argv) < 0) { (void)!write(pipe_fds[1], &errno, sizeof(errno)); diff --git a/spawn.h b/spawn.h index 129cadc2..2ab645a8 100644 --- a/spawn.h +++ b/spawn.h @@ -3,4 +3,5 @@ #include #include "reaper.h" -bool spawn(struct reaper *reaper, const char *cwd, char *const argv[]); +bool spawn(struct reaper *reaper, const char *cwd, char *const argv[], + int stdin_fd, int stdout_fd, int stderr_fd); diff --git a/terminal.c b/terminal.c index 53640baa..c720aea4 100644 --- a/terminal.c +++ b/terminal.c @@ -2248,7 +2248,8 @@ bool term_spawn_new(const struct terminal *term) { return spawn( - term->reaper, term->cwd, (char *const []){term->foot_exe, NULL}); + term->reaper, term->cwd, (char *const []){term->foot_exe, NULL}, + -1, -1, -1); } void -- cgit v1.3