summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--libsmartcols/src/table_print.c17
-rw-r--r--sys-utils/losetup.85
-rw-r--r--sys-utils/losetup.c18
3 files changed, 32 insertions, 8 deletions
diff --git a/libsmartcols/src/table_print.c b/libsmartcols/src/table_print.c
index 37d75c962..731879cae 100644
--- a/libsmartcols/src/table_print.c
+++ b/libsmartcols/src/table_print.c
@@ -539,6 +539,7 @@ static int print_header(struct libscols_table *tb, struct libscols_buffer *buf)
if (scols_table_is_noheadings(tb) ||
scols_table_is_export(tb) ||
+ scols_table_is_json(tb) ||
list_empty(&tb->tb_lines))
return 0;
@@ -942,6 +943,12 @@ int scols_print_table(struct libscols_table *tb)
return -EINVAL;
DBG(TAB, ul_debugobj(tb, "printing"));
+
+ if (list_empty(&tb->tb_lines)) {
+ DBG(TAB, ul_debugobj(tb, "ignore -- epmty table"));
+ return 0;
+ }
+
if (!tb->symbols)
scols_table_set_symbols(tb, NULL); /* use default */
@@ -964,7 +971,7 @@ int scols_print_table(struct libscols_table *tb)
if (!buf)
return -ENOMEM;
- if (!(scols_table_is_raw(tb) || scols_table_is_export(tb))) {
+ if (tb->format == SCOLS_FMT_HUMAN) {
rc = recount_widths(tb, buf);
if (rc != 0)
goto done;
@@ -972,11 +979,9 @@ int scols_print_table(struct libscols_table *tb)
fput_table_open(tb);
- if (!scols_table_is_json(tb)) {
- rc = print_header(tb, buf);
- if (rc)
- goto done;
- }
+ rc = print_header(tb, buf);
+ if (rc)
+ goto done;
if (scols_table_is_tree(tb))
rc = print_tree(tb, buf);
diff --git a/sys-utils/losetup.8 b/sys-utils/losetup.8
index 6f18e540f..41661e553 100644
--- a/sys-utils/losetup.8
+++ b/sys-utils/losetup.8
@@ -100,11 +100,14 @@ Otherwise, just print its name.
Show the status of all loop devices associated with the given
.IR file .
.TP
+.IP "\fB\-J, \fB\-\-json\fP"
+Use JSON format for \fB\-\-list\fP output.
+.TP
.BR \-l , " \-\-list"
If a loop device or the \fB-a\fR option is specified, print the default columns
for either the specified loop device or all loop devices; the default is to
print info about all devices. See also \fB\-\-output\fP, \fB\-\-noheadings\fP
-and \fB\-\-raw\fP.
+\fB\-\-json\fP and \fB\-\-raw\fP.
.TP
.BR \-n , " \-\-noheadings"
Don't print headings for \fB\-\-list\fP output format.
diff --git a/sys-utils/losetup.c b/sys-utils/losetup.c
index 9201782ad..a68b22229 100644
--- a/sys-utils/losetup.c
+++ b/sys-utils/losetup.c
@@ -53,6 +53,7 @@ enum {
/* basic output flags */
static int no_headings;
static int raw;
+static int json;
struct colinfo {
const char *name;
@@ -302,8 +303,12 @@ static int show_table(struct loopdev_cxt *lc,
if (!(tb = scols_new_table()))
err(EXIT_FAILURE, _("failed to initialize output table"));
scols_table_enable_raw(tb, raw);
+ scols_table_enable_json(tb, json);
scols_table_enable_noheadings(tb, no_headings);
+ if (json)
+ scols_table_set_name(tb, "loopdevices");
+
for (i = 0; i < ncolumns; i++) {
struct colinfo *ci = get_column_info(i);
@@ -397,6 +402,7 @@ static void usage(FILE *out)
fputs(_(" -O, --output <cols> specify columns to output for --list\n"), out);
fputs(_(" -n, --noheadings don't print headings for --list output\n"), out);
fputs(_(" --raw use raw --list output format\n"), out);
+ fputs(_(" -J, --json use JSON --list output format\n"), out);
fputs(USAGE_SEPARATOR, out);
fputs(USAGE_HELP, out);
@@ -454,6 +460,7 @@ int main(int argc, char **argv)
{ "find", 0, 0, 'f' },
{ "help", 0, 0, 'h' },
{ "associated", 1, 0, 'j' },
+ { "json", 0, 0, 'J' },
{ "list", 0, 0, 'l' },
{ "noheadings", 0, 0, 'n' },
{ "offset", 1, 0, 'o' },
@@ -472,6 +479,7 @@ int main(int argc, char **argv)
{ 'D','a','c','d','f','j' },
{ 'D','c','d','f','l' },
{ 'D','c','d','f','O' },
+ { 'J',OPT_RAW },
{ 0 }
};
int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
@@ -484,7 +492,7 @@ int main(int argc, char **argv)
if (loopcxt_init(&lc, 0))
err(EXIT_FAILURE, _("failed to initialize loopcxt"));
- while ((c = getopt_long(argc, argv, "ac:d:Dfhj:lno:O:PrvV",
+ while ((c = getopt_long(argc, argv, "ac:d:Dfhj:Jlno:O:PrvV",
longopts, NULL)) != -1) {
err_exclusive_options(c, longopts, excl, excl_st);
@@ -519,6 +527,9 @@ int main(int argc, char **argv)
case 'h':
usage(stdout);
break;
+ case 'J':
+ json = 1;
+ break;
case 'j':
act = A_SHOW;
file = optarg;
@@ -566,6 +577,11 @@ int main(int argc, char **argv)
list = 1;
}
+ if (!act && argc == 2 && (raw || json)) {
+ act = A_SHOW;
+ list = 1;
+ }
+
/* default --list output columns */
if (list && !ncolumns) {
columns[ncolumns++] = COL_NAME;