From c29b82dfe97fe7ce6974e088b1b55826a8c49354 Mon Sep 17 00:00:00 2001 From: Daniel Beer Date: Tue, 17 Aug 2010 11:11:48 +1200 Subject: [PATCH] Added output capture callback. --- output.c | 16 ++++++++++++++++ output.h | 12 ++++++++++++ 2 files changed, 28 insertions(+) diff --git a/output.c b/output.c index d3de4ef..01babda 100644 --- a/output.c +++ b/output.c @@ -32,6 +32,9 @@ struct outbuf { static struct outbuf stdout_buf; static struct outbuf stderr_buf; +static capture_func_t capture_func; +static void *capture_data; + static int write_text(struct outbuf *out, const char *buf, FILE *fout) { int want_color = opdb_get_boolean("color"); @@ -47,6 +50,8 @@ static int write_text(struct outbuf *out, const char *buf, FILE *fout) if (*buf == '\n') { out->buf[out->len] = 0; fprintf(fout, "%s\n", out->buf); + if (capture_func) + capture_func(capture_data, out->buf); out->len = 0; } else if ((want_color || !out->in_code) && out->len + 1 < sizeof(out->buf)) { @@ -90,3 +95,14 @@ void pr_error(const char *prefix) { printc_err("%s: %s\n", prefix, strerror(errno)); } + +void capture_start(capture_func_t func, void *data) +{ + capture_func = func; + capture_data = data; +} + +void capture_end(void) +{ + capture_func = NULL; +} diff --git a/output.h b/output.h index e61d324..62c8dd9 100644 --- a/output.h +++ b/output.h @@ -30,4 +30,16 @@ int printc_err(const char *fmt, ...); void pr_error(const char *prefix); +/* Capture output. Capturing is started by calling capture_begin() with + * a callback function. The callback is invoked for each line of output + * printed to either stdout or stderr (output still goes to + * stdout/stderr as well). + * + * Capture is ended by calling capture_end(). + */ +typedef void (*capture_func_t)(void *user_data, const char *text); + +void capture_start(capture_func_t, void *user_data); +void capture_end(void); + #endif