gpioinfo: improve output format
authorBartosz Golaszewski <bartekgola@gmail.com>
Mon, 9 Jan 2017 13:53:33 +0000 (14:53 +0100)
committerBartosz Golaszewski <bartekgola@gmail.com>
Mon, 9 Jan 2017 13:53:33 +0000 (14:53 +0100)
Improve the way the information is formatted. The code is still pretty
ugly though.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
gpioinfo.c
tools-common.c
tools-common.h

index 2b35a4a6d78243cf7cbfec4807e44c6a7285c0db..1c2e1dc1c7ba1ee0f35959ef5d66ec606ea05a4f 100644 (file)
@@ -13,7 +13,9 @@
 
 #include <stdio.h>
 #include <string.h>
+#include <stdarg.h>
 #include <getopt.h>
+#include <errno.h>
 
 #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");
 
index 01c381f851a3301dc843840d9f5ec8b12af431df..713a7036df862146f0bae569630fa28634697c78 100644 (file)
@@ -16,7 +16,6 @@
 #include <stdarg.h>
 
 #define NORETURN               __attribute__((noreturn))
-#define PRINTF(fmt, arg)       __attribute__((format(printf, fmt, arg)))
 
 static char *progname = "unknown";
 
index dfb379dd507494b2150aa23b4257432a35b9f49d..e31d43835291476c7b6f9bb7744d6a26d4b11099 100644 (file)
@@ -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);