diff --git a/main.c b/main.c index c831c8d..d087175 100644 --- a/main.c +++ b/main.c @@ -337,15 +337,9 @@ static int add_fet_device(void *user_data, const struct fet_db_record *r) return vector_push(v, &r->name, 1); } -static int cmp_char_ptr(const void *a, const void *b) -{ - return strcmp(*(const char **)a, *(const char **)b); -} - static int list_devices(void) { struct vector v; - int i; vector_init(&v, sizeof(const char *)); if (fet_db_enum(add_fet_device, &v) < 0) { @@ -354,13 +348,10 @@ static int list_devices(void) return -1; } - qsort(v.ptr, v.size, v.elemsize, cmp_char_ptr); - printc("Devices supported by FET driver:\n"); - for (i = 0; i < v.size; i++) - printc(" %s\n", VECTOR_AT(v, i, const char *)); - + namelist_print(&v); vector_destroy(&v); + return 0; } diff --git a/output.c b/output.c index 3bb5ee9..c3baac4 100644 --- a/output.c +++ b/output.c @@ -17,6 +17,7 @@ */ #include +#include #include #include #include @@ -121,3 +122,53 @@ void capture_end(void) { capture_func = NULL; } + +/************************************************************************ + * Name lists + */ + +static int namelist_cmp(const void *a, const void *b) +{ + return strcasecmp(*(const char **)a, *(const char **)b); +} + +void namelist_print(struct vector *v) +{ + int i; + int max_len = 0; + int rows, cols; + + qsort(v->ptr, v->size, v->elemsize, namelist_cmp); + + for (i = 0; i < v->size; i++) { + const char *text = VECTOR_AT(*v, i, const char *); + int len = strlen(text); + + if (len > max_len) + max_len = len; + } + + max_len += 2; + cols = 72 / max_len; + rows = (v->size + cols - 1) / cols; + + for (i = 0; i < rows; i++) { + int j; + + printc(" "); + for (j = 0; j < cols; j++) { + int k = j * rows + i; + const char *text; + + if (k >= v->size) + break; + + text = VECTOR_AT(*v, k, const char *); + printc("%s", text); + for (k = strlen(text); k < max_len; k++) + printc(" "); + } + + printc("\n"); + } +} diff --git a/output.h b/output.h index fa2d1c9..ab1b33b 100644 --- a/output.h +++ b/output.h @@ -19,6 +19,8 @@ #ifndef OUTPUT_H_ #define OUTPUT_H_ +#include "vector.h" + /* Print output. ANSI colour codes may be embedded, and these will be * stripped on output if colour output is disabled. * @@ -43,4 +45,9 @@ typedef void (*capture_func_t)(void *user_data, const char *text); void capture_start(capture_func_t, void *user_data); void capture_end(void); +/* Name lists. This function is used for printing multi-column sorted + * lists of constant strings. Expected is a vector of const char *. + */ +void namelist_print(struct vector *v); + #endif diff --git a/stdcmd.c b/stdcmd.c index b00afd9..45a36f4 100644 --- a/stdcmd.c +++ b/stdcmd.c @@ -28,52 +28,6 @@ #include "reader.h" #include "expr.h" -static int namelist_cmp(const void *a, const void *b) -{ - return strcasecmp(*(const char **)a, *(const char **)b); -} - -static void namelist_print(struct vector *v) -{ - int i; - int max_len = 0; - int rows, cols; - - qsort(v->ptr, v->size, v->elemsize, namelist_cmp); - - for (i = 0; i < v->size; i++) { - const char *text = VECTOR_AT(*v, i, const char *); - int len = strlen(text); - - if (len > max_len) - max_len = len; - } - - max_len += 2; - cols = 72 / max_len; - rows = (v->size + cols - 1) / cols; - - for (i = 0; i < rows; i++) { - int j; - - printc(" "); - for (j = 0; j < cols; j++) { - int k = j * rows + i; - const char *text; - - if (k >= v->size) - break; - - text = VECTOR_AT(*v, k, const char *); - printc("%s", text); - for (k = strlen(text); k < max_len; k++) - printc(" "); - } - - printc("\n"); - } -} - static const char *type_text(opdb_type_t type) { switch (type) {