From: Martin Ling Date: Wed, 4 Dec 2013 10:53:51 +0000 (+0000) Subject: Add sr_scpi_send_variadic() function. X-Git-Tag: libsigrok-0.3.0~490 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=87c410830d9967d9eea73ae18bef12e668b94e92;p=libsigrok.git Add sr_scpi_send_variadic() function. --- diff --git a/hardware/common/scpi.c b/hardware/common/scpi.c index 10218ba9..4992b736 100644 --- a/hardware/common/scpi.c +++ b/hardware/common/scpi.c @@ -22,7 +22,6 @@ #include #include -#include /* Message logging helpers with subsystem-specific prefix string. */ #define LOG_PREFIX "scpi: " @@ -127,22 +126,40 @@ SR_PRIV int sr_scpi_source_remove(struct sr_scpi_dev_inst *scpi) SR_PRIV int sr_scpi_send(struct sr_scpi_dev_inst *scpi, const char *format, ...) { - va_list args1, args2; + va_list args; + int ret; + + va_start(args, format); + ret = sr_scpi_send_variadic(scpi, format, args); + va_end(args); + + return ret; +} + +/** + * Send a SCPI command with a variadic argument list. + * + * @param scpi Previously initialized SCPI device structure. + * @param format Format string. + * @param args Argument list. + * + * @return SR_OK on success, SR_ERR on failure. + */ +SR_PRIV int sr_scpi_send_variadic(struct sr_scpi_dev_inst *scpi, + const char *format, va_list args) +{ + va_list args_copy; char *buf; int len, ret; - /* Copy arguments since we need to make two variadic calls. */ - va_start(args1, format); - va_copy(args2, args1); - /* Get length of buffer required. */ - len = vsnprintf(NULL, 0, format, args1); - va_end(args1); + va_copy(args_copy, args); + len = vsnprintf(NULL, 0, format, args_copy); + va_end(args_copy); /* Allocate buffer and write out command. */ buf = g_malloc(len + 1); - vsprintf(buf, format, args2); - va_end(args2); + vsprintf(buf, format, args); /* Send command. */ ret = scpi->send(scpi->priv, buf); diff --git a/libsigrok-internal.h b/libsigrok-internal.h index 0e8cdecb..adc48c49 100644 --- a/libsigrok-internal.h +++ b/libsigrok-internal.h @@ -302,6 +302,8 @@ SR_PRIV int sr_scpi_source_add(struct sr_scpi_dev_inst *scpi, int events, SR_PRIV int sr_scpi_source_remove(struct sr_scpi_dev_inst *scpi); SR_PRIV int sr_scpi_send(struct sr_scpi_dev_inst *scpi, const char *format, ...); +SR_PRIV int sr_scpi_send_variadic(struct sr_scpi_dev_inst *scpi, + const char *format, va_list args); SR_PRIV int sr_scpi_receive(struct sr_scpi_dev_inst *scpi, char **scpi_response); SR_PRIV int sr_scpi_read(struct sr_scpi_dev_inst *scpi, char *buf, int maxlen);