From: Jason at zx2c4.com (Jason A. Donenfeld)
Subject: [PATCH 2/4] filter: wire up exec_filter to enable function
Date: Sat, 14 Oct 2017 16:17:48 +0200 [thread overview]
Message-ID: <20171014141750.14013-3-Jason@zx2c4.com> (raw)
In-Reply-To: <20171014141750.14013-1-Jason@zx2c4.com>
We also move pipe_fh to be local, since it's not needed any place else.
Signed-off-by: Jason A. Donenfeld <Jason at zx2c4.com>
---
cgit.h | 1 +
filter.c | 27 +++++++++++++++++++++------
2 files changed, 22 insertions(+), 6 deletions(-)
diff --git a/cgit.h b/cgit.h
index 8da69e7..f9949a7 100644
--- a/cgit.h
+++ b/cgit.h
@@ -72,6 +72,7 @@ struct cgit_exec_filter {
char *cmd;
char **argv;
int old_stdout;
+ int new_stdout;
int pid;
};
diff --git a/filter.c b/filter.c
index aa0027a..4deb4de 100644
--- a/filter.c
+++ b/filter.c
@@ -60,9 +60,8 @@ static int open_exec_filter(struct cgit_filter *base, va_list ap)
die_errno("Unable to exec subprocess %s", filter->cmd);
}
close(pipe_fh[0]);
- chk_non_negative(dup2(pipe_fh[1], STDOUT_FILENO),
- "Unable to use pipe as STDOUT");
- close(pipe_fh[1]);
+ filter->new_stdout = pipe_fh[1];
+ chk_non_negative(dup2(filter->new_stdout, STDOUT_FILENO), "Unable to assign new fd to STDOUT");
return 0;
}
@@ -71,9 +70,10 @@ static int close_exec_filter(struct cgit_filter *base)
struct cgit_exec_filter *filter = (struct cgit_exec_filter *)base;
int i, exit_status = 0;
- chk_non_negative(dup2(filter->old_stdout, STDOUT_FILENO),
- "Unable to restore STDOUT");
+ chk_non_negative(dup2(filter->old_stdout, STDOUT_FILENO), "Unable to assign old fd to STDOUT");
close(filter->old_stdout);
+ close(filter->new_stdout);
+
if (filter->pid < 0)
goto done;
waitpid(filter->pid, &exit_status, 0);
@@ -82,10 +82,24 @@ static int close_exec_filter(struct cgit_filter *base)
die("Subprocess %s exited abnormally", filter->cmd);
done:
+ close(filter->old_stdout);
+ close(filter->new_stdout);
for (i = 0; i < filter->base.argument_count; i++)
filter->argv[i + 1] = NULL;
- return WEXITSTATUS(exit_status);
+ return WIFEXITED(exit_status);
+}
+static int enable_exec_filter(struct cgit_filter *base, bool on)
+{
+ struct cgit_exec_filter *filter = (struct cgit_exec_filter *)base;
+ int ret;
+
+ if (on)
+ ret = dup2(filter->new_stdout, STDOUT_FILENO);
+ else
+ ret = dup2(filter->old_stdout, STDOUT_FILENO);
+ chk_non_negative(ret, "Unable to assign fd to STDOUT");
+ return 0;
}
static void fprintf_exec_filter(struct cgit_filter *base, FILE *f, const char *prefix)
@@ -128,6 +142,7 @@ void cgit_exec_filter_init(struct cgit_exec_filter *filter, char *cmd, char **ar
memset(filter, 0, sizeof(*filter));
filter->base.open = open_exec_filter;
filter->base.close = close_exec_filter;
+ filter->base.enable = enable_exec_filter;
filter->base.fprintf = fprintf_exec_filter;
filter->base.cleanup = cleanup_exec_filter;
filter->cmd = cmd;
--
2.14.2
next prev parent reply other threads:[~2017-10-14 14:17 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-10-14 14:17 [PATCH 0/4] adding color to ui-blame Jason
2017-10-14 14:17 ` [PATCH 1/4] filter: add generic scafolding for temporarily disabling filter Jason
2017-10-14 14:17 ` Jason [this message]
2017-10-14 14:17 ` [PATCH 3/4] filter: wire up lua_filter to enable function Jason
2017-10-14 14:17 ` [PATCH 4/4] ui-blame: put source lines through filter Jason
2017-10-14 16:14 ` [PATCH 0/4] adding color to ui-blame john
2017-10-15 16:47 ` Jason
2017-10-15 17:04 ` whydoubt
2017-10-15 17:11 ` Jason
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20171014141750.14013-3-Jason@zx2c4.com \
--to=cgit@lists.zx2c4.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).