diff options
author | Russ Allbery <rra@stanford.edu> | 2007-10-05 02:15:27 +0000 |
---|---|---|
committer | Russ Allbery <rra@stanford.edu> | 2007-10-05 02:15:27 +0000 |
commit | f21fba65f194ff26bf72e23f0db311314529720b (patch) | |
tree | ce454a33292244ad130962ab9da7f667fe57d1ec /client/remctl.c | |
parent | fe56c09a7feeb2d1c9cd699fda07e145c4c354a2 (diff) |
Refactor the remctl calls in the wallet client to share a common routine.
Diffstat (limited to 'client/remctl.c')
-rw-r--r-- | client/remctl.c | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/client/remctl.c b/client/remctl.c new file mode 100644 index 0000000..089d7f4 --- /dev/null +++ b/client/remctl.c @@ -0,0 +1,74 @@ +/* $Id$ +** +** remctl interface for the wallet client. +** +** Written by Russ Allbery <rra@stanford.edu> +** Copyright 2007 Board of Trustees, Leland Stanford Jr. University +** +** See README for licensing terms. +*/ + +#include <config.h> +#include <system.h> + +#include <remctl.h> + +#include <client/internal.h> +#include <util/util.h> + + +/* +** Given a remctl connection and a command, run the command. +** +** If data is non-NULL, save the output in it and return the length in +** length. Otherwise, send any output to stdout. Either way, send error +** output to stderr, and return the exit status (or 255 if there is an +** error). +*/ +int +run_command(struct remctl *r, const char **command, char **data, + size_t *length) +{ + struct remctl_output *output; + int status = 255; + + if (data != NULL) + *data = NULL; + if (length != NULL) + *length = 0; + if (!remctl_command(r, command)) { + warn("%s", remctl_error(r)); + return 255; + } + do { + output = remctl_output(r); + switch (output->type) { + case REMCTL_OUT_OUTPUT: + if (output->stream == 1) { + if (data != NULL) { + *data = xrealloc(*data, *length + output->length); + memcpy(*data + *length, output->data, output->length); + *length += output->length; + } else { + fwrite(output->data, 1, output->length, stdout); + } + } else { + fprintf(stderr, "wallet: "); + fwrite(output->data, 1, output->length, stderr); + } + break; + case REMCTL_OUT_STATUS: + status = output->status; + break; + case REMCTL_OUT_ERROR: + fprintf(stderr, "wallet: "); + fwrite(output->data, 1, output->length, stderr); + fputc('\n', stderr); + status = 255; + break; + case REMCTL_OUT_DONE: + break; + } + } while (output->type != REMCTL_OUT_DONE); + return status; +} |