From baa3b270ecf795cd851e15eae33d5d1ed8277868 Mon Sep 17 00:00:00 2001 From: Ondrej Oprala Date: Thu, 27 Mar 2014 14:58:51 +0100 Subject: Use libsmartcols in libfdisk and cfdisk Signed-off-by: Ondrej Oprala --- Makefile.am | 2 +- configure.ac | 3 ++- disk-utils/Makemodule.am | 5 +++++ disk-utils/cfdisk.c | 49 +++++++++++++++++++++++++++------------------- libfdisk/src/Makemodule.am | 10 ++++++++++ libfdisk/src/bsd.c | 17 ++++++++-------- libfdisk/src/dos.c | 21 ++++++++++---------- libfdisk/src/fdiskP.h | 4 ++-- libfdisk/src/gpt.c | 15 +++++++------- libfdisk/src/sgi.c | 18 +++++++++-------- libfdisk/src/sun.c | 18 +++++++++-------- libfdisk/src/table.c | 24 ++++++++++++----------- 12 files changed, 110 insertions(+), 76 deletions(-) diff --git a/Makefile.am b/Makefile.am index 2c19283d7..a20e65da3 100644 --- a/Makefile.am +++ b/Makefile.am @@ -79,8 +79,8 @@ include lib/Makemodule.am include libuuid/Makemodule.am include libblkid/Makemodule.am include libmount/Makemodule.am -include libfdisk/Makemodule.am include libsmartcols/Makemodule.am +include libfdisk/Makemodule.am include schedutils/Makemodule.am include text-utils/Makemodule.am diff --git a/configure.ac b/configure.ac index f1b4cae50..3d2c5d064 100644 --- a/configure.ac +++ b/configure.ac @@ -817,10 +817,11 @@ AC_DEFINE_UNQUOTED([LIBSMARTCOLS_VERSION], ["$LIBSMARTCOLS_VERSION"], [libsmartc dnl -dnl libfdisk is enabled all time if possible +dnl libfdisk is enabled at all times if possible dnl UL_BUILD_INIT([libfdisk], [check]) UL_REQUIRES_BUILD([libfdisk], [libuuid]) +UL_REQUIRES_BUILD([libfdisk], [libsmartcols]) AM_CONDITIONAL([BUILD_LIBFDISK], [test "x$build_libfdisk" = xyes]) UL_BUILD_INIT([fdisk], [check]) diff --git a/disk-utils/Makemodule.am b/disk-utils/Makemodule.am index dd9143931..8780df644 100644 --- a/disk-utils/Makemodule.am +++ b/disk-utils/Makemodule.am @@ -176,6 +176,11 @@ cfdisk_CFLAGS += -I$(ul_libblkid_incdir) cfdisk_LDADD += libblkid.la endif +if BUILD_LIBSMARTCOLS +cfdisk_CFLAGS += -I$(ul_libsmartcols_incdir) +cfdisk_LDADD += libsmartcols.la +endif + if HAVE_SLANG cfdisk_LDADD += -lslang else diff --git a/disk-utils/cfdisk.c b/disk-utils/cfdisk.c index bc8290d8b..030cc19bf 100644 --- a/disk-utils/cfdisk.c +++ b/disk-utils/cfdisk.c @@ -4,6 +4,7 @@ #include #include #include +#include #ifdef HAVE_SLANG_H # include @@ -166,11 +167,12 @@ static char *table_to_string(struct cfdisk *cf, struct fdisk_table *tb) struct fdisk_partition *pa; struct fdisk_label *lb; struct fdisk_iter *itr = NULL; - struct tt *tt = NULL; + struct libscols_table *table = NULL; + struct libscols_iter *s_itr = NULL; char *res = NULL; size_t i; int tree = 0; - struct tt_line *ln, *ln_cont = NULL; + struct libscols_line *ln, *ln_cont = NULL; DBG(FRONTEND, ul_debug("table: convert to string")); @@ -190,23 +192,26 @@ static char *table_to_string(struct cfdisk *cf, struct fdisk_table *tb) while (fdisk_table_next_partition(tb, itr, &pa) == 0) { if (fdisk_partition_is_nested(pa)) { DBG(FRONTEND, ul_debug("table: nested detected, using tree")); - tree = TT_FL_TREE; + tree = SCOLS_FL_TREE; break; } } - tt = tt_new_table(TT_FL_FREEDATA | TT_FL_MAX | tree); - if (!tt) + table = scols_new_table(NULL); + if (!table) goto done; + scols_table_set_max(table, 1); + scols_table_set_tree(table, tree); /* headers */ for (i = 0; i < cf->ncols; i++) { col = fdisk_label_get_column(lb, cf->cols[i]); if (col) { - int fl = col->tt_flags; + int fl = col->scols_flags; if (tree && col->id == FDISK_COL_DEVICE) - fl |= TT_FL_TREE; - tt_define_column(tt, col->name, col->width, fl); + fl |= SCOLS_FL_TREE; + if (!scols_table_new_column(table, col->name, col->width, fl)) + goto done; } } @@ -214,9 +219,9 @@ static char *table_to_string(struct cfdisk *cf, struct fdisk_table *tb) fdisk_reset_iter(itr, FDISK_ITER_FORWARD); while (fdisk_table_next_partition(tb, itr, &pa) == 0) { - struct tt_line *parent = fdisk_partition_is_nested(pa) ? ln_cont : NULL; + struct libscols_line *parent = fdisk_partition_is_nested(pa) ? ln_cont : NULL; - ln = tt_add_line(tt, parent); + ln = scols_table_new_line(table, parent); if (!ln) goto done; for (i = 0; i < cf->ncols; i++) { @@ -226,22 +231,22 @@ static char *table_to_string(struct cfdisk *cf, struct fdisk_table *tb) continue; if (fdisk_partition_to_string(pa, cf->cxt, col->id, &cdata)) continue; - tt_line_set_data(ln, i, cdata); + scols_line_set_data(ln, i, cdata); } if (tree && fdisk_partition_is_container(pa)) ln_cont = ln; - tt_line_set_userdata(ln, (void *) pa); + scols_line_set_userdata(ln, (void *) pa); fdisk_ref_partition(pa); } - if (tt_is_empty(tt)) + if (scols_table_is_empty(table)) goto done; - tt_set_termreduce(tt, ARROW_CURSOR_WIDTH); - tt_print_table_to_string(tt, &res); + scols_table_reduce_termwidth(table, ARROW_CURSOR_WIDTH); + scols_print_table_to_string(table, &res); - /* tt_* code might to reorder lines, let's reorder @tb according to the + /* scols_* code might reorder lines, let's reorder @tb according to the * final output (it's no problem because partitions are addressed by * parno stored within struct fdisk_partition) */ @@ -251,15 +256,19 @@ static char *table_to_string(struct cfdisk *cf, struct fdisk_table *tb) fdisk_table_remove_partition(tb, pa); /* add all in the right order */ - i = 0; - while (tt_get_output_line(tt, i++, &ln) == 0) { - struct fdisk_partition *pa = tt_line_get_userdata(ln); + s_itr = scols_new_iter(SCOLS_ITER_FORWARD); + if (!s_itr) + goto done; + + while (scols_table_next_line(table, s_itr, &ln) == 0) { + struct fdisk_partition *pa = scols_line_get_userdata(ln); fdisk_table_add_partition(tb, pa); fdisk_unref_partition(pa); } done: - tt_free_table(tt); + scols_unref_table(table); + scols_free_iter(s_itr); fdisk_free_iter(itr); return res; diff --git a/libfdisk/src/Makemodule.am b/libfdisk/src/Makemodule.am index d8cbffbe3..99bddb2da 100644 --- a/libfdisk/src/Makemodule.am +++ b/libfdisk/src/Makemodule.am @@ -49,6 +49,12 @@ libfdisk_la_DEPENDENCIES += libuuid.la libfdisk_la_CFLAGS += -I$(ul_libuuid_incdir) endif +if BUILD_LIBSMARTCOLS +libfdisk_la_LIBADD += libsmartcols.la +libfdisk_la_DEPENDENCIES += libsmartcols.la +libfdisk_la_CFLAGS += -I$(ul_libsmartcols_incdir) +endif + check_PROGRAMS += \ test_fdisk_ask \ test_fdisk_utils @@ -65,6 +71,10 @@ if BUILD_LIBUUID libfdisk_tests_ldflags += libuuid.la endif +if BUILD_LIBSMARTCOLS +libfdisk_tests_ldflags += libsmartcols.la +endif + test_fdisk_ask_SOURCES = libfdisk/src/ask.c test_fdisk_ask_CFLAGS = $(libfdisk_tests_cflags) test_fdisk_ask_LDFLAGS = $(libfdisk_tests_ldflags) diff --git a/libfdisk/src/bsd.c b/libfdisk/src/bsd.c index 4ece79fb2..a81afd2c3 100644 --- a/libfdisk/src/bsd.c +++ b/libfdisk/src/bsd.c @@ -15,6 +15,7 @@ #include #include #include +#include #include "nls.h" #include "blkdev.h" @@ -879,15 +880,15 @@ static const struct fdisk_label_operations bsd_operations = static const struct fdisk_column bsd_columns[] = { { FDISK_COL_DEVICE, N_("Slice"), 1, 0 }, - { FDISK_COL_START, N_("Start"), 5, TT_FL_RIGHT }, - { FDISK_COL_END, N_("End"), 5, TT_FL_RIGHT }, - { FDISK_COL_SECTORS, N_("Sectors"), 5, TT_FL_RIGHT }, - { FDISK_COL_CYLINDERS, N_("Cylinders"), 5, TT_FL_RIGHT }, - { FDISK_COL_SIZE, N_("Size"), 5, TT_FL_RIGHT }, + { FDISK_COL_START, N_("Start"), 5, SCOLS_FL_RIGHT }, + { FDISK_COL_END, N_("End"), 5, SCOLS_FL_RIGHT }, + { FDISK_COL_SECTORS, N_("Sectors"), 5, SCOLS_FL_RIGHT }, + { FDISK_COL_CYLINDERS, N_("Cylinders"), 5, SCOLS_FL_RIGHT }, + { FDISK_COL_SIZE, N_("Size"), 5, SCOLS_FL_RIGHT }, { FDISK_COL_TYPE, N_("Type"), 8, 0 }, - { FDISK_COL_FSIZE, N_("Fsize"), 5, TT_FL_RIGHT }, - { FDISK_COL_BSIZE, N_("Bsize"), 5, TT_FL_RIGHT }, - { FDISK_COL_CPG, N_("Cpg"), 5, TT_FL_RIGHT } + { FDISK_COL_FSIZE, N_("Fsize"), 5, SCOLS_FL_RIGHT }, + { FDISK_COL_BSIZE, N_("Bsize"), 5, SCOLS_FL_RIGHT }, + { FDISK_COL_CPG, N_("Cpg"), 5, SCOLS_FL_RIGHT } }; /* diff --git a/libfdisk/src/dos.c b/libfdisk/src/dos.c index 612f562a6..b1b23350f 100644 --- a/libfdisk/src/dos.c +++ b/libfdisk/src/dos.c @@ -15,6 +15,7 @@ #include "fdiskP.h" #include +#include #define MAXIMUM_PARTS 60 #define ACTIVE_FLAG 0x80 @@ -1914,18 +1915,18 @@ static const struct fdisk_column dos_columns[] = /* basic */ { FDISK_COL_DEVICE, N_("Device"), 10, 0 }, { FDISK_COL_BOOT, N_("Boot"), 1, 0 }, - { FDISK_COL_START, N_("Start"), 5, TT_FL_RIGHT }, - { FDISK_COL_END, N_("End"), 5, TT_FL_RIGHT }, - { FDISK_COL_SECTORS, N_("Sectors"), 5, TT_FL_RIGHT }, - { FDISK_COL_CYLINDERS, N_("Cylinders"), 5, TT_FL_RIGHT }, - { FDISK_COL_SIZE, N_("Size"), 5, TT_FL_RIGHT, FDISK_COLFL_EYECANDY }, - { FDISK_COL_TYPEID, N_("Id"), 2, TT_FL_RIGHT }, - { FDISK_COL_TYPE, N_("Type"), 0.1, TT_FL_TRUNC }, + { FDISK_COL_START, N_("Start"), 5, SCOLS_FL_RIGHT }, + { FDISK_COL_END, N_("End"), 5, SCOLS_FL_RIGHT }, + { FDISK_COL_SECTORS, N_("Sectors"), 5, SCOLS_FL_RIGHT }, + { FDISK_COL_CYLINDERS, N_("Cylinders"), 5, SCOLS_FL_RIGHT }, + { FDISK_COL_SIZE, N_("Size"), 5, SCOLS_FL_RIGHT, FDISK_COLFL_EYECANDY }, + { FDISK_COL_TYPEID, N_("Id"), 2, SCOLS_FL_RIGHT }, + { FDISK_COL_TYPE, N_("Type"), 0.1, SCOLS_FL_TRUNC }, /* expert mode */ - { FDISK_COL_SADDR, N_("Start-C/H/S"), 1, TT_FL_RIGHT, FDISK_COLFL_DETAIL }, - { FDISK_COL_EADDR, N_("End-C/H/S"), 1, TT_FL_RIGHT, FDISK_COLFL_DETAIL }, - { FDISK_COL_ATTR, N_("Attrs"), 2, TT_FL_RIGHT, FDISK_COLFL_DETAIL } + { FDISK_COL_SADDR, N_("Start-C/H/S"), 1, SCOLS_FL_RIGHT, FDISK_COLFL_DETAIL }, + { FDISK_COL_EADDR, N_("End-C/H/S"), 1, SCOLS_FL_RIGHT, FDISK_COLFL_DETAIL }, + { FDISK_COL_ATTR, N_("Attrs"), 2, SCOLS_FL_RIGHT, FDISK_COLFL_DETAIL } }; diff --git a/libfdisk/src/fdiskP.h b/libfdisk/src/fdiskP.h index cb3c2b27c..f966d0a68 100644 --- a/libfdisk/src/fdiskP.h +++ b/libfdisk/src/fdiskP.h @@ -17,12 +17,12 @@ #include #include + #include "c.h" #include "libfdisk.h" #include "nls.h" /* temporary before dialog API will be implamented */ #include "list.h" -#include "tt.h" #include "debug.h" #include #include @@ -233,7 +233,7 @@ struct fdisk_column { int id; /* FDISK_COL_* */ const char *name; /* column header */ double width; - int tt_flags; /* TT_FL_* */ + int scols_flags; /* SCOLS_FL_* */ int flags; /* FDISK_COLFL_* */ }; diff --git a/libfdisk/src/gpt.c b/libfdisk/src/gpt.c index 1f5c03b0d..aba33cd37 100644 --- a/libfdisk/src/gpt.c +++ b/libfdisk/src/gpt.c @@ -18,6 +18,7 @@ #include #include #include +#include #include "fdiskP.h" @@ -2349,16 +2350,16 @@ static const struct fdisk_column gpt_columns[] = { /* basic */ { FDISK_COL_DEVICE, N_("Device"), 10, 0 }, - { FDISK_COL_START, N_("Start"), 5, TT_FL_RIGHT }, - { FDISK_COL_END, N_("End"), 5, TT_FL_RIGHT }, - { FDISK_COL_SECTORS, N_("Sectors"), 5, TT_FL_RIGHT }, - { FDISK_COL_CYLINDERS, N_("Cylinders"), 5, TT_FL_RIGHT }, - { FDISK_COL_SIZE, N_("Size"), 5, TT_FL_RIGHT, FDISK_COLFL_EYECANDY }, - { FDISK_COL_TYPE, N_("Type"), 0.1, TT_FL_TRUNC, FDISK_COLFL_EYECANDY }, + { FDISK_COL_START, N_("Start"), 5, SCOLS_FL_RIGHT }, + { FDISK_COL_END, N_("End"), 5, SCOLS_FL_RIGHT }, + { FDISK_COL_SECTORS, N_("Sectors"), 5, SCOLS_FL_RIGHT }, + { FDISK_COL_CYLINDERS, N_("Cylinders"), 5, SCOLS_FL_RIGHT }, + { FDISK_COL_SIZE, N_("Size"), 5, SCOLS_FL_RIGHT, FDISK_COLFL_EYECANDY }, + { FDISK_COL_TYPE, N_("Type"), 0.1, SCOLS_FL_TRUNC, FDISK_COLFL_EYECANDY }, /* expert */ { FDISK_COL_TYPEID, N_("Type-UUID"), 36, 0, FDISK_COLFL_DETAIL }, { FDISK_COL_UUID, N_("UUID"), 36, 0, FDISK_COLFL_DETAIL }, - { FDISK_COL_NAME, N_("Name"), 0.2, TT_FL_TRUNC, FDISK_COLFL_DETAIL }, + { FDISK_COL_NAME, N_("Name"), 0.2, SCOLS_FL_TRUNC, FDISK_COLFL_DETAIL }, { FDISK_COL_ATTR, N_("Attrs"), 0, 0, FDISK_COLFL_DETAIL } }; diff --git a/libfdisk/src/sgi.c b/libfdisk/src/sgi.c index 8204ce9d5..cd12d8049 100644 --- a/libfdisk/src/sgi.c +++ b/libfdisk/src/sgi.c @@ -10,6 +10,8 @@ * Arnaldo Carvalho de Melo , Mar 1999, * Phillip Kesling , Mar 2003. */ + +#include #include "c.h" #include "nls.h" #include "all-io.h" @@ -1078,14 +1080,14 @@ static int sgi_toggle_partition_flag(struct fdisk_context *cxt, size_t i, unsign static const struct fdisk_column sgi_columns[] = { { FDISK_COL_DEVICE, N_("Device"), 10, 0 }, - { FDISK_COL_START, N_("Start"), 5, TT_FL_RIGHT }, - { FDISK_COL_END, N_("End"), 5, TT_FL_RIGHT }, - { FDISK_COL_SECTORS, N_("Sectors"), 5, TT_FL_RIGHT }, - { FDISK_COL_CYLINDERS, N_("Cylinders"), 5, TT_FL_RIGHT }, - { FDISK_COL_SIZE, N_("Size"), 5, TT_FL_RIGHT, FDISK_COLFL_EYECANDY }, - { FDISK_COL_TYPEID, N_("Id"), 2, TT_FL_RIGHT }, - { FDISK_COL_TYPE, N_("Type"), 0.1, TT_FL_TRUNC, FDISK_COLFL_EYECANDY }, - { FDISK_COL_ATTR, N_("Attrs"), 0, TT_FL_RIGHT } + { FDISK_COL_START, N_("Start"), 5, SCOLS_FL_RIGHT }, + { FDISK_COL_END, N_("End"), 5, SCOLS_FL_RIGHT }, + { FDISK_COL_SECTORS, N_("Sectors"), 5, SCOLS_FL_RIGHT }, + { FDISK_COL_CYLINDERS, N_("Cylinders"), 5, SCOLS_FL_RIGHT }, + { FDISK_COL_SIZE, N_("Size"), 5, SCOLS_FL_RIGHT, FDISK_COLFL_EYECANDY }, + { FDISK_COL_TYPEID, N_("Id"), 2, SCOLS_FL_RIGHT }, + { FDISK_COL_TYPE, N_("Type"), 0.1, SCOLS_FL_TRUNC, FDISK_COLFL_EYECANDY }, + { FDISK_COL_ATTR, N_("Attrs"), 0, SCOLS_FL_RIGHT } }; static const struct fdisk_label_operations sgi_operations = diff --git a/libfdisk/src/sun.c b/libfdisk/src/sun.c index 0b79c2a28..6d68c7aa9 100644 --- a/libfdisk/src/sun.c +++ b/libfdisk/src/sun.c @@ -12,6 +12,8 @@ #include /* write */ #include /* ioctl */ +#include + #include "nls.h" #include "blkdev.h" #include "bitops.h" @@ -987,14 +989,14 @@ static int sun_partition_is_used( static const struct fdisk_column sun_columns[] = { { FDISK_COL_DEVICE, N_("Device"), 10, 0 }, - { FDISK_COL_START, N_("Start"), 5, TT_FL_RIGHT }, - { FDISK_COL_END, N_("End"), 5, TT_FL_RIGHT }, - { FDISK_COL_SECTORS, N_("Sectors"), 5, TT_FL_RIGHT }, - { FDISK_COL_CYLINDERS, N_("Cylinders"), 5, TT_FL_RIGHT }, - { FDISK_COL_SIZE, N_("Size"), 5, TT_FL_RIGHT }, - { FDISK_COL_TYPEID, N_("Id"), 2, TT_FL_RIGHT }, - { FDISK_COL_TYPE, N_("Type"), 0.1, TT_FL_TRUNC }, - { FDISK_COL_ATTR, N_("Flags"), 0, TT_FL_RIGHT } + { FDISK_COL_START, N_("Start"), 5, SCOLS_FL_RIGHT }, + { FDISK_COL_END, N_("End"), 5, SCOLS_FL_RIGHT }, + { FDISK_COL_SECTORS, N_("Sectors"), 5, SCOLS_FL_RIGHT }, + { FDISK_COL_CYLINDERS, N_("Cylinders"), 5, SCOLS_FL_RIGHT }, + { FDISK_COL_SIZE, N_("Size"), 5, SCOLS_FL_RIGHT }, + { FDISK_COL_TYPEID, N_("Id"), 2, SCOLS_FL_RIGHT }, + { FDISK_COL_TYPE, N_("Type"), 0.1, SCOLS_FL_TRUNC }, + { FDISK_COL_ATTR, N_("Flags"), 0, SCOLS_FL_RIGHT } }; const struct fdisk_label_operations sun_operations = diff --git a/libfdisk/src/table.c b/libfdisk/src/table.c index 5365f2cca..99dd67c79 100644 --- a/libfdisk/src/table.c +++ b/libfdisk/src/table.c @@ -1,4 +1,5 @@ +#include #include "fdiskP.h" /** @@ -539,7 +540,7 @@ int fdisk_table_to_string(struct fdisk_table *tb, char **data) { int *org_cols = cols, rc = 0; - struct tt *tt = NULL; + struct libscols_table *table = NULL; const struct fdisk_column *col; struct fdisk_partition *pa = NULL; struct fdisk_iter itr; @@ -560,8 +561,8 @@ int fdisk_table_to_string(struct fdisk_table *tb, return rc; } - tt = tt_new_table(TT_FL_FREEDATA); - if (!tt) { + table = scols_new_table(NULL); + if (!table) { rc = -ENOMEM; goto done; } @@ -570,14 +571,15 @@ int fdisk_table_to_string(struct fdisk_table *tb, for (j = 0; j < ncols; j++) { col = fdisk_label_get_column(cxt->label, cols[j]); if (col) - tt_define_column(tt, col->name, col->width, col->tt_flags); + if (!scols_table_new_column(table, col->name, col->width, col->scols_flags)) + goto done; } fdisk_reset_iter(&itr, FDISK_ITER_FORWARD); - /* convert partition to string and add to tt */ + /* convert partition to string and add to table */ while (fdisk_table_next_partition(tb, &itr, &pa) == 0) { - struct tt_line *ln = tt_add_line(tt, NULL); + struct libscols_line *ln = scols_table_new_line(table, NULL); if (!ln) { rc = -ENOMEM; goto done; @@ -595,18 +597,18 @@ int fdisk_table_to_string(struct fdisk_table *tb, continue; if (fdisk_partition_to_string(pa, cxt, col->id, &cdata)) continue; - tt_line_set_data(ln, j, cdata); + scols_line_set_data(ln, j, cdata); } } rc = 0; - if (!tt_is_empty(tt)) - rc = tt_print_table_to_string(tt, data); + if (!scols_table_is_empty(table)) + rc = scols_print_table_to_string(table, data); else - DBG(TAB, ul_debugobj(tb, "tt empty")); + DBG(TAB, ul_debugobj(tb, "table empty")); done: if (org_cols != cols) free(cols); - tt_free_table(tt); + scols_unref_table(table); return rc; } -- cgit v1.2.3-55-g7522