From: Bartosz Golaszewski Date: Mon, 9 Jan 2017 13:53:33 +0000 (+0100) Subject: gpioinfo: improve output format X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=9d21200a0695731b7afb8c6dfb4d75e52ee25b96;p=qemu-gpiodev%2Flibgpiod.git gpioinfo: improve output format Improve the way the information is formatted. The code is still pretty ugly though. Signed-off-by: Bartosz Golaszewski --- diff --git a/gpioinfo.c b/gpioinfo.c index 2b35a4a..1c2e1dc 100644 --- a/gpioinfo.c +++ b/gpioinfo.c @@ -13,7 +13,9 @@ #include #include +#include #include +#include #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x))) @@ -52,42 +54,76 @@ static void print_help(void) printf(" -h, --help:\t\tdisplay this message and exit\n"); } +static PRINTF(3, 4) void prinfo(bool *overflow, + unsigned int pref_len, const char *fmt, ...) +{ + char *buf, *buffmt = NULL; + size_t len; + va_list va; + int status; + + va_start(va, fmt); + status = vasprintf(&buf, fmt, va); + va_end(va); + if (status < 0) + die("vasprintf: %s\n", strerror(errno)); + + len = strlen(buf) - 1; + + if (len >= pref_len || *overflow) { + *overflow = true; + printf("%s", buf); + } else { + status = asprintf(&buffmt, "%%%us", pref_len); + if (status < 0) + die("asprintf: %s\n", strerror(errno)); + + printf(buffmt, buf); + } + + free(buf); + if (fmt) + free(buffmt); +} + static void list_lines(struct gpiod_chip *chip) { + bool flag_printed, overflow; int direction, active_state; struct gpiod_line_iter iter; const char *name, *consumer; struct gpiod_line *line; - bool flag_printed; - unsigned int i; + unsigned int i, offset; printf("%s - %u lines:\n", gpiod_chip_name(chip), gpiod_chip_num_lines(chip)); gpiod_line_iter_init(&iter, chip); gpiod_foreach_line(&iter, line) { + offset = gpiod_line_offset(line); name = gpiod_line_name(line); consumer = gpiod_line_consumer(line); direction = gpiod_line_direction(line); active_state = gpiod_line_active_state(line); - printf("\tline %2u: ", gpiod_line_offset(line)); + overflow = false; + + printf("\tline "); + prinfo(&overflow, 2, "%u", offset); + printf(": "); - if (name) - printf("\"%s\"", name); - else - printf("unnamed"); + name ? prinfo(&overflow, 12, "\"%s\"", name) + : prinfo(&overflow, 12, "unnamed"); printf(" "); - if (consumer) - printf("\"%s\"", consumer); - else - printf("unused"); + consumer ? prinfo(&overflow, 12, "\"%s\"", consumer) + : prinfo(&overflow, 12, "unused"); printf(" "); - printf("%s ", direction == GPIOD_DIRECTION_INPUT + prinfo(&overflow, 8, "%s ", direction == GPIOD_DIRECTION_INPUT ? "input" : "output"); - printf("%s ", active_state == GPIOD_ACTIVE_STATE_LOW + prinfo(&overflow, 12, "%s ", + active_state == GPIOD_ACTIVE_STATE_LOW ? "active-low" : "active-high"); diff --git a/tools-common.c b/tools-common.c index 01c381f..713a703 100644 --- a/tools-common.c +++ b/tools-common.c @@ -16,7 +16,6 @@ #include #define NORETURN __attribute__((noreturn)) -#define PRINTF(fmt, arg) __attribute__((format(printf, fmt, arg))) static char *progname = "unknown"; diff --git a/tools-common.h b/tools-common.h index dfb379d..e31d438 100644 --- a/tools-common.h +++ b/tools-common.h @@ -18,7 +18,8 @@ * common code. */ -#define UNUSED __attribute__((unused)) +#define UNUSED __attribute__((unused)) +#define PRINTF(fmt, arg) __attribute__((format(printf, fmt, arg))) void set_progname(char *name); const char * get_progname(void);