]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
format-table: introduce table_isempty and use it where appropriate
authorMike Yuan <me@yhndnzj.com>
Mon, 25 Dec 2023 09:34:57 +0000 (17:34 +0800)
committerMike Yuan <me@yhndnzj.com>
Mon, 25 Dec 2023 09:47:18 +0000 (17:47 +0800)
15 files changed:
coccinelle/isempty.cocci
src/analyze/analyze-fdstore.c
src/analyze/analyze-plot.c
src/creds/creds.c
src/cryptenroll/cryptenroll-list.c
src/home/homectl.c
src/login/inhibit.c
src/login/loginctl.c
src/machine/machinectl.c
src/pcrlock/pcrlock.c
src/portable/portablectl.c
src/shared/format-table.h
src/shared/pkcs11-util.c
src/shared/tpm2-util.c
src/userdb/userdbctl.c

index cd912ff18f422151ad244aeebe8418fd0959ff28..20899708864997393998e26ee2f354c2a7cda95f 100644 (file)
@@ -132,3 +132,13 @@ expression s;
 - prioq_size(s) != 0
 + !prioq_isempty(s)
 )
+@@
+expression s;
+@@
+(
+- table_get_rows(s) <= 1
++ table_isempty(s)
+|
+- table_get_rows(s) > 1
++ !table_isempty(s)
+)
index e1f88c355252684c46cee01c542a087c0f82ce86..8ada6d4e73e1fafcb1ebed910111ca969a4eafd7 100644 (file)
@@ -81,7 +81,7 @@ static int dump_fdstore(sd_bus *bus, const char *arg) {
         if (r < 0)
                 return r;
 
-        if (FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF) && table_get_rows(table) <= 1)
+        if (FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF) && table_isempty(table))
                 log_info("No file descriptors in fdstore of '%s'.", unit);
         else {
                 r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, /* show_header= */true);
index 81fc25b31e0370d492ea81290d1ecb6836faf8b6..ccd552a781648346591aca0e67a4ae0c9f2642ee 100644 (file)
@@ -402,7 +402,7 @@ static int show_table(Table *table, const char *word) {
         assert(table);
         assert(word);
 
-        if (table_get_rows(table) > 1) {
+        if (!table_isempty(table)) {
                 table_set_header(table, arg_legend);
 
                 if (!FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF))
@@ -414,10 +414,10 @@ static int show_table(Table *table, const char *word) {
         }
 
         if (arg_legend) {
-                if (table_get_rows(table) > 1)
-                        printf("\n%zu %s listed.\n", table_get_rows(table) - 1, word);
-                else
+                if (table_isempty(table))
                         printf("No %s.\n", word);
+                else
+                        printf("\n%zu %s listed.\n", table_get_rows(table) - 1, word);
         }
 
         return 0;
index b53e8d3b3654ac6442d99ca2dfa61c05863d9288..ed39ffe51e138f46f99cec4e507bef7b50431e2b 100644 (file)
@@ -232,7 +232,7 @@ static int verb_list(int argc, char **argv, void *userdata) {
                 return log_error_errno(SYNTHETIC_ERRNO(ENXIO), "No credentials passed. (i.e. $CREDENTIALS_DIRECTORY not set.)");
         }
 
-        if ((arg_json_format_flags & JSON_FORMAT_OFF) && table_get_rows(t) <= 1) {
+        if (FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF) && table_isempty(t)) {
                 log_info("No credentials");
                 return 0;
         }
index d21df7123b0c852f3b5b5f3146cef7fa4690fac6..00a1a8e6378a16d06c9cb246845a59723ea15115 100644 (file)
@@ -114,7 +114,7 @@ int list_enrolled(struct crypt_device *cd) {
                         return table_log_add_error(r);
         }
 
-        if (table_get_rows(t) <= 1) {
+        if (table_isempty(t)) {
                 log_info("No slots found.");
                 return 0;
         }
index f2fe90c75b0412c63c5500dcf3a062930e73c8b8..5a2eb8f31b590dc52dc71626e730776ba666cd92 100644 (file)
@@ -189,7 +189,7 @@ static int list_homes(int argc, char *argv[], void *userdata) {
         if (r < 0)
                 return bus_log_parse_error(r);
 
-        if (table_get_rows(table) > 1 || !FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) {
+        if (!table_isempty(table) || !FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) {
                 r = table_set_sort(table, (size_t) 0);
                 if (r < 0)
                         return table_log_sort_error(r);
@@ -199,11 +199,11 @@ static int list_homes(int argc, char *argv[], void *userdata) {
                         return r;
         }
 
-        if (arg_legend && (arg_json_format_flags & JSON_FORMAT_OFF)) {
-                if (table_get_rows(table) > 1)
-                        printf("\n%zu home areas listed.\n", table_get_rows(table) - 1);
-                else
+        if (arg_legend && !FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) {
+                if (table_isempty(table))
                         printf("No home areas.\n");
+                else
+                        printf("\n%zu home areas listed.\n", table_get_rows(table) - 1);
         }
 
         return 0;
index ad73c4bde331924abe4e837b264f13f41d039070..8c65e1582a43d8d957fd8997b08da77e9d22f646 100644 (file)
@@ -111,7 +111,7 @@ static int print_inhibitors(sd_bus *bus) {
         if (r < 0)
                 return bus_log_parse_error(r);
 
-        if (table_get_rows(table) > 1) {
+        if (!table_isempty(table)) {
                 r = table_set_sort(table, (size_t) 1, (size_t) 0, (size_t) 5, (size_t) 6);
                 if (r < 0)
                         return table_log_sort_error(r);
@@ -124,10 +124,10 @@ static int print_inhibitors(sd_bus *bus) {
         }
 
         if (arg_legend) {
-                if (table_get_rows(table) > 1)
-                        printf("\n%zu inhibitors listed.\n", table_get_rows(table) - 1);
-                else
+                if (table_isempty(table))
                         printf("No inhibitors.\n");
+                else
+                        printf("\n%zu inhibitors listed.\n", table_get_rows(table) - 1);
         }
 
         return 0;
index 7fc6efc9da9a3e54bbec1e411d85705e0679b0d7..9b76a63990859d008d21915cb479d654ba1f1e56 100644 (file)
@@ -119,7 +119,7 @@ static int show_table(Table *table, const char *word) {
         assert(table);
         assert(word);
 
-        if (table_get_rows(table) > 1 || OUTPUT_MODE_IS_JSON(arg_output)) {
+        if (!table_isempty(table) || OUTPUT_MODE_IS_JSON(arg_output)) {
                 r = table_set_sort(table, (size_t) 0);
                 if (r < 0)
                         return table_log_sort_error(r);
@@ -135,10 +135,10 @@ static int show_table(Table *table, const char *word) {
         }
 
         if (arg_legend) {
-                if (table_get_rows(table) > 1)
-                        printf("\n%zu %s listed.\n", table_get_rows(table) - 1, word);
-                else
+                if (table_isempty(table))
                         printf("No %s.\n", word);
+                else
+                        printf("\n%zu %s listed.\n", table_get_rows(table) - 1, word);
         }
 
         return 0;
index 3eadb5f4e7721167960f4768dcd7611330e044c6..aa81cf071fc0e8899ce902624b7b13740f2d5235 100644 (file)
@@ -235,7 +235,7 @@ static int show_table(Table *table, const char *word) {
         assert(table);
         assert(word);
 
-        if (table_get_rows(table) > 1 || OUTPUT_MODE_IS_JSON(arg_output)) {
+        if (!table_isempty(table) || OUTPUT_MODE_IS_JSON(arg_output)) {
                 r = table_set_sort(table, (size_t) 0);
                 if (r < 0)
                         return table_log_sort_error(r);
@@ -251,10 +251,10 @@ static int show_table(Table *table, const char *word) {
         }
 
         if (arg_legend) {
-                if (table_get_rows(table) > 1)
-                        printf("\n%zu %s listed.\n", table_get_rows(table) - 1, word);
-                else
+                if (table_isempty(table))
                         printf("No %s.\n", word);
+                else
+                        printf("\n%zu %s listed.\n", table_get_rows(table) - 1, word);
         }
 
         return 0;
index 23bdab0befe66e331e857a0a7424d4c999452b8e..d1bab5f59f867045e4346fc30ee08a03a7ad9fa4 100644 (file)
@@ -2570,17 +2570,17 @@ static int verb_list_components(int argc, char *argv[], void *userdata) {
                 }
         }
 
-        if (table_get_rows(table) > 1 || !FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) {
+        if (!table_isempty(table) || !FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) {
                 r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, /* show_header= */ true);
                 if (r < 0)
                         return log_error_errno(r, "Failed to output table: %m");
         }
 
         if (FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) {
-                if (table_get_rows(table) > 1)
-                        printf("\n%zu components listed.\n", table_get_rows(table) - 1);
-                else
+                if (table_isempty(table))
                         printf("No components defined.\n");
+                else
+                        printf("\n%zu components listed.\n", table_get_rows(table) - 1);
         }
 
         return 0;
index 532e8d93455b62540255939fa980f1e2fbecbb49..cf3122e29c56bede8c3df1d68bc71edfc79ed273 100644 (file)
@@ -1030,7 +1030,7 @@ static int list_images(int argc, char *argv[], void *userdata) {
         if (r < 0)
                 return bus_log_parse_error(r);
 
-        if (table_get_rows(table) > 1) {
+        if (!table_isempty(table)) {
                 r = table_set_sort(table, (size_t) 0);
                 if (r < 0)
                         return table_log_sort_error(r);
@@ -1043,10 +1043,10 @@ static int list_images(int argc, char *argv[], void *userdata) {
         }
 
         if (arg_legend) {
-                if (table_get_rows(table) > 1)
-                        printf("\n%zu images listed.\n", table_get_rows(table) - 1);
-                else
+                if (table_isempty(table))
                         printf("No images.\n");
+                else
+                        printf("\n%zu images listed.\n", table_get_rows(table) - 1);
         }
 
         return 0;
index 0368447638c2aaad3b653367cdd604d579abbfee..36e1f94ca0b953cfa92bc4d8e6d29da4641c3097 100644 (file)
@@ -144,6 +144,12 @@ static inline TableCell* TABLE_HEADER_CELL(size_t i) {
 }
 
 size_t table_get_rows(Table *t);
+static inline bool table_isempty(Table *t) {
+        if (!t)
+                return true;
+
+        return table_get_rows(t) <= 1;
+}
 size_t table_get_columns(Table *t);
 
 size_t table_get_current_column(Table *t);
index c3d97b80f9e4c30e870d53a0056a56f83eb14732..edf4fb01b925e13652ded04273a425093ce8bc31 100644 (file)
@@ -1530,7 +1530,7 @@ int pkcs11_list_tokens(void) {
         if (r < 0 && r != -EAGAIN)
                 return r;
 
-        if (table_get_rows(t) <= 1) {
+        if (table_isempty(t)) {
                 log_info("No suitable PKCS#11 tokens found.");
                 return 0;
         }
index 5e07b88a895cfdaf0c6e68b1cd679e4d3b9dd564..f2bdae2becac1fb9c3dae0374183f62066e49698 100644 (file)
@@ -5965,7 +5965,7 @@ int tpm2_list_devices(void) {
                 }
         }
 
-        if (table_get_rows(t) <= 1) {
+        if (table_isempty(t)) {
                 log_info("No suitable TPM2 devices found.");
                 return 0;
         }
index a7db3fb0d52168ddcb19c552d034336322933553..9ab5d42c4b86f87b9e1eb3f3aab3a1961686e349 100644 (file)
@@ -440,7 +440,7 @@ static int display_user(int argc, char *argv[], void *userdata) {
                 if (uid_map_lines < 0)
                         return uid_map_lines;
 
-                if (table_get_rows(table) > 1) {
+                if (!table_isempty(table)) {
                         r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
                         if (r < 0)
                                 return table_log_print_error(r);
@@ -743,7 +743,7 @@ static int display_group(int argc, char *argv[], void *userdata) {
                 if (gid_map_lines < 0)
                         return gid_map_lines;
 
-                if (table_get_rows(table) > 1) {
+                if (!table_isempty(table)) {
                         r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
                         if (r < 0)
                                 return table_log_print_error(r);
@@ -891,17 +891,17 @@ static int display_memberships(int argc, char *argv[], void *userdata) {
         }
 
         if (table) {
-                if (table_get_rows(table) > 1) {
+                if (!table_isempty(table)) {
                         r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
                         if (r < 0)
                                 return table_log_print_error(r);
                 }
 
                 if (arg_legend) {
-                        if (table_get_rows(table) > 1)
-                                printf("\n%zu memberships listed.\n", table_get_rows(table) - 1);
-                        else
+                        if (table_isempty(table))
                                 printf("No memberships.\n");
+                        else
+                                printf("\n%zu memberships listed.\n", table_get_rows(table) - 1);
                 }
         }
 
@@ -956,17 +956,17 @@ static int display_services(int argc, char *argv[], void *userdata) {
                         return table_log_add_error(r);
         }
 
-        if (table_get_rows(t) > 1) {
+        if (!table_isempty(t)) {
                 r = table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend);
                 if (r < 0)
                         return table_log_print_error(r);
         }
 
         if (arg_legend && arg_output != OUTPUT_JSON) {
-                if (table_get_rows(t) > 1)
-                        printf("\n%zu services listed.\n", table_get_rows(t) - 1);
-                else
+                if (table_isempty(t))
                         printf("No services.\n");
+                else
+                        printf("\n%zu services listed.\n", table_get_rows(t) - 1);
         }
 
         return 0;