diff options
author | Karel Zak | 2014-08-13 10:10:11 +0200 |
---|---|---|
committer | Karel Zak | 2014-08-13 10:10:11 +0200 |
commit | d44115f3b5866f76ceaefd0d145468e68d7e2688 (patch) | |
tree | d7d83433edd97bbefaf9539c10f95b42937b4145 /disk-utils | |
parent | lib/colors: add function to return color from scheme (diff) | |
download | kernel-qcow2-util-linux-d44115f3b5866f76ceaefd0d145468e68d7e2688.tar.gz kernel-qcow2-util-linux-d44115f3b5866f76ceaefd0d145468e68d7e2688.tar.xz kernel-qcow2-util-linux-d44115f3b5866f76ceaefd0d145468e68d7e2688.zip |
libfdisk: remove dependence on libsmartcols
It's application business to convert libfdisk_table to string.
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'disk-utils')
-rw-r--r-- | disk-utils/Makemodule.am | 5 | ||||
-rw-r--r-- | disk-utils/cfdisk.c | 20 | ||||
-rw-r--r-- | disk-utils/fdisk.c | 109 |
3 files changed, 103 insertions, 31 deletions
diff --git a/disk-utils/Makemodule.am b/disk-utils/Makemodule.am index 995e08552..04118c73b 100644 --- a/disk-utils/Makemodule.am +++ b/disk-utils/Makemodule.am @@ -142,6 +142,11 @@ fdisk_CFLAGS += -I$(ul_libuuid_incdir) fdisk_LDADD += libuuid.la endif +if BUILD_LIBSMARTCOLS +fdisk_CFLAGS += -I$(ul_libsmartcols_incdir) +fdisk_LDADD += libsmartcols.la +endif + if HAVE_STATIC_FDISK sbin_PROGRAMS += fdisk.static fdisk_static_SOURCES = $(fdisk_SOURCES) diff --git a/disk-utils/cfdisk.c b/disk-utils/cfdisk.c index eff793355..453eafab0 100644 --- a/disk-utils/cfdisk.c +++ b/disk-utils/cfdisk.c @@ -235,9 +235,6 @@ static int partition_from_scols(struct fdisk_table *tb, return 0; } -/* It would be possible to use fdisk_table_to_string(), but we want some - * extension to the output format, so let's do it without libfdisk - */ static char *table_to_string(struct cfdisk *cf, struct fdisk_table *tb) { const struct fdisk_column *col; @@ -283,10 +280,18 @@ static char *table_to_string(struct cfdisk *cf, struct fdisk_table *tb) for (i = 0; i < cf->ncols; i++) { col = fdisk_label_get_column(lb, cf->cols[i]); if (col) { - int fl = col->scols_flags; - if (tree && col->id == FDISK_COL_DEVICE) + int fl = 0; + + if (fdisk_column_is_number(col)) + fl |= SCOLS_FL_RIGHT; + if (fdisk_column_get_id(col) == FDISK_COL_TYPE) + fl |= SCOLS_FL_TRUNC; + if (tree && fdisk_column_get_id(col) == FDISK_COL_DEVICE) fl |= SCOLS_FL_TREE; - if (!scols_table_new_column(table, col->name, col->width, fl)) + + if (!scols_table_new_column(table, + fdisk_column_get_name(col), + fdisk_column_get_width(col), fl)) goto done; } } @@ -305,7 +310,8 @@ static char *table_to_string(struct cfdisk *cf, struct fdisk_table *tb) col = fdisk_label_get_column(lb, cf->cols[i]); if (!col) continue; - if (fdisk_partition_to_string(pa, cf->cxt, col->id, &cdata)) + if (fdisk_partition_to_string(pa, cf->cxt, + fdisk_column_get_id(col), &cdata)) continue; scols_line_refer_data(ln, i, cdata); } diff --git a/disk-utils/fdisk.c b/disk-utils/fdisk.c index fed7a5745..1e6b45d3c 100644 --- a/disk-utils/fdisk.c +++ b/disk-utils/fdisk.c @@ -21,6 +21,7 @@ #include <sys/time.h> #include <time.h> #include <limits.h> +#include <libsmartcols.h> #include "c.h" #include "xalloc.h" @@ -539,45 +540,105 @@ void list_disklabel(struct fdisk_context *cxt) { struct fdisk_table *tb = NULL; struct fdisk_partition *pa = NULL; - struct fdisk_iter *itr; - - char *str; + struct fdisk_iter *itr = NULL; + struct libscols_table *out = NULL; + const char *bold = NULL; + int *cols = NULL; + size_t ncols = 0, i; /* print label specific stuff by libfdisk FDISK_ASK_INFO API */ fdisk_list_disklabel(cxt); - /* print partitions */ - if (fdisk_get_partitions(cxt, &tb)) - return; - if (fdisk_table_to_string(tb, cxt, NULL, 0, &str) == 0) { - fputc('\n', stdout); - if (str) { - char *p = str; - char *next = strchr(str, '\n'); - if (next && colors_wanted()) { - *next = '\0'; - color_scheme_enable("header", UL_COLOR_BOLD); - fputs(p, stdout); - color_disable(); - fputc('\n', stdout); - p = ++next; - } - fputs(p, stdout); - free(str); + /* get partitions and generate output */ + if (fdisk_get_partitions(cxt, &tb) || fdisk_table_get_nents(tb) <= 0) + goto done; + + if (fdisk_get_columns(cxt, 0, &cols, &ncols)) + goto done; + + itr = fdisk_new_iter(FDISK_ITER_FORWARD); + if (!itr) { + fdisk_warn(cxt, _("faild to allocate iterator")); + goto done; + } + + out = scols_new_table(); + if (!out) { + fdisk_warn(cxt, _("faild to allocate output table")); + goto done; + } + + if (colors_wanted()) { + scols_table_enable_colors(out, 1); + bold = color_scheme_get_sequence("header", UL_COLOR_BOLD); + } + + /* define output table columns */ + for (i = 0; i < ncols; i++) { + int fl = 0; + struct libscols_column *co; + const struct fdisk_column *col = + fdisk_label_get_column(cxt->label, cols[i]); + if (!col) + goto done; + if (fdisk_column_is_number(col)) + fl |= SCOLS_FL_RIGHT; + if (fdisk_column_get_id(col) == FDISK_COL_TYPE) + fl |= SCOLS_FL_TRUNC; + + co = scols_table_new_column(out, + fdisk_column_get_name(col), + fdisk_column_get_width(col), fl); + if (!co) + goto done; + + /* set colum header color */ + if (bold) + scols_cell_set_color(scols_column_get_header(co), bold); + } + + /* fill-in output table */ + while (fdisk_table_next_partition(tb, itr, &pa) == 0) { + struct libscols_line *ln = scols_table_new_line(out, NULL); + + if (!ln) { + fdisk_warn(cxt, _("faild to allocate output line")); + goto done; + } + + for (i = 0; i < ncols; i++) { + char *data = NULL; + + const struct fdisk_column *col = + fdisk_label_get_column(cxt->label, cols[i]); + + if (fdisk_partition_to_string(pa, cxt, + fdisk_column_get_id(col), + &data)) + continue; + scols_line_refer_data(ln, i, data); } } - fputc('\n', stdout); + /* print */ + if (!scols_table_is_empty(out)) { + fputc('\n', stdout); + scols_print_table(out); + } - itr = fdisk_new_iter(FDISK_ITER_FORWARD); + fputc('\n', stdout); + + /* print warnings */ while (itr && fdisk_table_next_partition(tb, itr, &pa) == 0) fdisk_warn_alignment(cxt, fdisk_partition_get_start(pa), fdisk_partition_get_partno(pa) + 1); if (fdisk_table_wrong_order(tb)) fdisk_info(cxt, _("Partition table entries are not in disk order.")); - +done: + free(cols); + scols_unref_table(out); fdisk_unref_table(tb); fdisk_free_iter(itr); } |