From 249f0b09d1e4e5e701c2b41a474969501aa85db8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Diego=20Aur=C3=A9lio=20Mesquita?= Date: Thu, 10 May 2018 22:52:29 -0300 Subject: [PATCH 3/3] Reshuffle some code and improve some variable names. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marco Diego Aurélio Mesquita --- src/text.c | 66 ++++++++++++++++++++++++++++---------------------------------- 1 file changed, 30 insertions(+), 36 deletions(-) diff --git a/src/text.c b/src/text.c index c9f3236c..ebea0bbe 100644 --- a/src/text.c +++ b/src/text.c @@ -1080,31 +1080,27 @@ RETSIGTYPE cancel_command(int signal) } /* Sends buffer pointed to by inptr to file descriptor fd. */ -void send_data(int fd, const filestruct *inptr) +void send_data(const filestruct *line, int fd) { - FILE *fp = fdopen(fd, "w"); + FILE *tube = fdopen(fd, "w"); - if (fp == NULL) + if (tube == NULL) return; - while (inptr != NULL) { - /* If last line is empty, we should not send it. */ - if(inptr->next == NULL && inptr->data[0] == '\0') - break; - - fprintf(fp, "%s%s", inptr->data, inptr->next == NULL ? "" : "\n"); - inptr = inptr->next; + /* Send each line, except a final empty line. */ + while (line != NULL && (line->next != NULL || line->data[0] != '\0')) { + fprintf(tube, "%s%s", line->data, line->next == NULL ? "" : "\n"); + line = line->next; } - fclose(fp); + fclose(tube); } /* Execute command in a shell. Return TRUE on success. */ bool execute_command(const char *command) { - int fd[2]; - int out_fd[2]; - /* Pipe through which text will be sent for external command. */ + int from_fd[2], to_fd[2]; + /* The pipes through which text will written and read. */ const bool has_selection = ISSET(MULTIBUFFER) ? openfile->prev->mark : openfile->mark; const bool should_pipe = (command[0] == '|'); FILE *stream; @@ -1114,17 +1110,13 @@ bool execute_command(const char *command) bool setup_failed = FALSE; /* Whether setting up the temporary SIGINT handler failed. */ - /* Create a pipe to read the command's output from. */ - if (pipe(fd) == -1) { + /* Create a pipe to read the command's output from, and, if needed, + * a pipe to feed the command's input through. */ + if (pipe(from_fd) == -1 || (should_pipe && pipe(to_fd) == -1)) { statusbar(_("Could not create pipe")); return FALSE; } - if (should_pipe && pipe(out_fd)) { - statusbar(_("Could not create outgoing pipe")); - return FALSE; - } - /* Check which shell to use. If none is specified, use /bin/sh. */ shellenv = getenv("SHELL"); if (shellenv == NULL) @@ -1132,16 +1124,18 @@ bool execute_command(const char *command) /* Fork a child process to run the command in. */ if ((pid = fork()) == 0) { - /* Child: close the unused read end of the pipe. */ - close(fd[0]); + /* Child: close the unused read end of the output pipe. */ + close(from_fd[0]); - /* Connect the write end of the pipe to the process' output streams. */ - dup2(fd[1], fileno(stdout)); - dup2(fd[1], fileno(stderr)); + /* Connect the write end of the output pipe to the process' output streams. */ + dup2(from_fd[1], fileno(stdout)); + dup2(from_fd[1], fileno(stderr)); + /* If the parent sends text, connect the read end of the + * feeding pipe to the child's input stream. */ if (should_pipe) { - close(out_fd[1]); - dup2(out_fd[0], fileno(stdin)); + dup2(to_fd[0], fileno(stdin)); + close(to_fd[1]); } /* Run the given command inside the preferred shell. */ @@ -1152,11 +1146,11 @@ bool execute_command(const char *command) } /* Parent: close the unused write end of the pipe. */ - close(fd[1]); + close(from_fd[1]); if (pid == -1) { statusbar(_("Could not fork")); - close(fd[0]); + close(from_fd[0]); return FALSE; } @@ -1166,7 +1160,6 @@ bool execute_command(const char *command) switch_to_prev_buffer(); filestruct *was_cutbuffer = cutbuffer; - close(out_fd[0]); cutbuffer = NULL; if(has_selection) { @@ -1180,12 +1173,13 @@ bool execute_command(const char *command) } if(fork() == 0) { - send_data(out_fd[1], has_selection ? cutbuffer : openfile->fileage); - close(out_fd[1]); + close(to_fd[0]); + send_data(has_selection ? cutbuffer : openfile->fileage, to_fd[1]); + close(to_fd[1]); exit(0); } else { - close(out_fd[0]); - close(out_fd[1]); + close(to_fd[0]); + close(to_fd[1]); } free_filestruct(cutbuffer); @@ -1214,7 +1208,7 @@ bool execute_command(const char *command) } } - stream = fdopen(fd[0], "rb"); + stream = fdopen(from_fd[0], "rb"); if (stream == NULL) statusline(ALERT, _("Failed to open pipe: %s"), strerror(errno)); else -- 2.11.0