[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Nano-devel] [PATCH 2/2] tweaks: reshuffle and rename some stuff in the
From: |
Benno Schulenberg |
Subject: |
[Nano-devel] [PATCH 2/2] tweaks: reshuffle and rename some stuff in the new piping code |
Date: |
Tue, 1 May 2018 10:59:10 +0200 |
[Still to be done: the undoing should be transformed into a single step.]
---
src/text.c | 71 +++++++++++++++++++++++++-----------------------------
1 file changed, 33 insertions(+), 38 deletions(-)
diff --git a/src/text.c b/src/text.c
index 91b91603..34136a1c 100644
--- a/src/text.c
+++ b/src/text.c
@@ -1079,51 +1079,43 @@ RETSIGTYPE cancel_command(int signal)
nperror("kill");
}
-/* Sends buffer pointed to by inptr to file descriptor fd. */
-void send_data(int fd, const filestruct *inptr)
+/* Send the buffer that starts at the given line to file descriptor fd. */
+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];
- /* The pipe through which text will be sent to an external
command. */
- const bool has_selection = openfile->mark;
+ int to_fd[2], from_fd[2];
+ /* The pipes through which text will written and read. */
FILE *stream;
+ const bool has_selection = (openfile->mark != NULL);
const char *shellenv;
struct sigaction oldaction, newaction;
/* Original and temporary handlers for SIGINT. */
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 there is
+ * a marked region, a pipe to feed the command's input through. */
+ if (pipe(from_fd) == -1 || (has_selection && pipe(to_fd) == -1)) {
statusbar(_("Could not create pipe"));
return FALSE;
}
- if (has_selection && 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)
@@ -1131,18 +1123,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 output
streams. */
+ dup2(from_fd[1], fileno(stdout));
+ dup2(from_fd[1], fileno(stderr));
- /* If the parent sends the selected text, connect the read end
of
- * the pipe to the child's input stream. */
+ /* If the parent sends selected text, connect the read end of
the
+ * feeding pipe to the child's input stream. */
if (has_selection) {
- 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. */
@@ -1153,22 +1145,25 @@ 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;
}
/* If text was selected, cut it and pipe it to the external command. */
if (has_selection) {
filestruct *was_cutbuffer = cutbuffer;
- close(out_fd[0]);
+
cutbuffer = NULL;
do_cut_text_void();
- send_data(out_fd[1], cutbuffer);
- close(out_fd[1]);
+
+ close(to_fd[0]);
+ send_data(cutbuffer, to_fd[1]);
+ close(to_fd[1]);
+
free_filestruct(cutbuffer);
cutbuffer = was_cutbuffer;
}
@@ -1189,7 +1184,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.17.0
- [Nano-devel] [PATCH 1/2] new feature: pipe selected text to external command when executing one, Benno Schulenberg, 2018/05/01
- [Nano-devel] [PATCH 2/2] tweaks: reshuffle and rename some stuff in the new piping code,
Benno Schulenberg <=
- Re: [Nano-devel] [PATCH 1/2] new feature: pipe selected text to external command when executing one, Marco Diego Aurélio Mesquita, 2018/05/10
- Re: [Nano-devel] [PATCH 1/2] new feature: pipe selected text to external command when executing one, Benno Schulenberg, 2018/05/11
- Re: [Nano-devel] [PATCH 1/2] new feature: pipe selected text to external command when executing one, Benno Schulenberg, 2018/05/11
- Re: [Nano-devel] [PATCH 1/2] new feature: pipe selected text to external command when executing one, Marco Diego Aurélio Mesquita, 2018/05/11
- Re: [Nano-devel] [PATCH 1/2] new feature: pipe selected text to external command when executing one, Marco Diego Aurélio Mesquita, 2018/05/11
- Re: [Nano-devel] [PATCH 1/2] new feature: pipe selected text to external command when executing one, Marco Diego Aurélio Mesquita, 2018/05/11
- Re: [Nano-devel] [PATCH 1/2] new feature: piping (selected) text to an external command, Benno Schulenberg, 2018/05/12
- Re: [Nano-devel] [PATCH 1/2] new feature: pipe selected text to external command when executing one, Benno Schulenberg, 2018/05/12