>From f4f78466daffdefcb4ff9382b6ae56abf68be8d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Diego=20Aur=C3=A9lio=20Mesquita?= Date: Sat, 18 Aug 2018 19:36:12 -0300 Subject: [PATCH] filter: reshuffle some code around MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit So that the process that filters text also forks the process that outputs the data to be filtered. Some odditty in the way pipes were handled created problems after filtering some text. Now, we have a chain of processes instead of the previous behavior where nano main process created the other two. This fixes https://savannah.gnu.org/bugs/?54499. Signed-off-by: Marco Diego Aurélio Mesquita --- src/text.c | 71 ++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 37 insertions(+), 34 deletions(-) diff --git a/src/text.c b/src/text.c index b5269029..7ee1ad02 100644 --- a/src/text.c +++ b/src/text.c @@ -1127,6 +1127,7 @@ bool execute_command(const char *command) /* Original and temporary handlers for SIGINT. */ bool setup_failed = FALSE; /* Whether setting up the temporary SIGINT handler failed. */ + filestruct *was_cutbuffer = cutbuffer; /* Create a pipe to read the command's output from, and, if needed, * a pipe to feed the command's input through. */ @@ -1135,10 +1136,29 @@ bool execute_command(const char *command) return FALSE; } - /* Check which shell to use. If none is specified, use /bin/sh. */ - shellenv = getenv("SHELL"); - if (shellenv == NULL) - shellenv = (char *) "/bin/sh"; + /* If the command starts with "|", pipe buffer or region to the command. */ + if (should_pipe) { + cutbuffer = NULL; + +#ifdef ENABLE_MULTIBUFFER + if (ISSET(MULTIBUFFER)) { + switch_to_prev_buffer(); + if (openfile->mark) + do_cut_text(TRUE, TRUE, FALSE); + } else +#endif + { + add_undo(COUPLE_BEGIN); + openfile->undotop->strdata = mallocstrcpy(NULL, _("filtering")); + if (openfile->mark == NULL) { + openfile->current = openfile->fileage; + openfile->current_x = 0; + } + add_undo(CUT); + do_cut_text(FALSE, openfile->mark, openfile->mark == NULL); + update_undo(CUT); + } + } /* Fork a child process to run the command in. */ if ((pid_of_command = fork()) == 0) { @@ -1153,9 +1173,21 @@ bool execute_command(const char *command) * feeding pipe to the child's input stream. */ if (should_pipe) { dup2(to_fd[0], fileno(stdin)); + if (fork() == 0) { + close(to_fd[0]); + send_data(cutbuffer != NULL ? cutbuffer : openfile->fileage, to_fd[1]); + close(to_fd[1]); + exit(0); + } + close(to_fd[1]); } + /* Check which shell to use. If none is specified, use /bin/sh. */ + shellenv = getenv("SHELL"); + if (shellenv == NULL) + shellenv = (char *) "/bin/sh"; + /* Run the given command inside the preferred shell. */ execl(shellenv, tail(shellenv), "-c", should_pipe ? &command[1] : command, NULL); @@ -1169,40 +1201,11 @@ bool execute_command(const char *command) if (pid_of_command == -1) { statusbar(_("Could not fork")); close(from_fd[0]); + cutbuffer = was_cutbuffer; return FALSE; } - /* If the command starts with "|", pipe buffer or region to the command. */ if (should_pipe) { - filestruct *was_cutbuffer = cutbuffer; - cutbuffer = NULL; - -#ifdef ENABLE_MULTIBUFFER - if (ISSET(MULTIBUFFER)) { - switch_to_prev_buffer(); - if (openfile->mark) - do_cut_text(TRUE, TRUE, FALSE); - } else -#endif - { - add_undo(COUPLE_BEGIN); - openfile->undotop->strdata = mallocstrcpy(NULL, _("filtering")); - if (openfile->mark == NULL) { - openfile->current = openfile->fileage; - openfile->current_x = 0; - } - add_undo(CUT); - do_cut_text(FALSE, openfile->mark, openfile->mark == NULL); - update_undo(CUT); - } - - if (fork() == 0) { - close(to_fd[0]); - send_data(cutbuffer != NULL ? cutbuffer : openfile->fileage, to_fd[1]); - close(to_fd[1]); - exit(0); - } - close(to_fd[0]); close(to_fd[1]); -- 2.11.0