From 4f9791a36caf27f9adb5a6d6490a703e408ca3dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 12 Sep 2023 16:44:52 +0200 Subject: [PATCH 1/6] export: use highlighting in --help --- src/import/export.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/import/export.c b/src/import/export.c index 84609cbe12..7e941a29af 100644 --- a/src/import/export.c +++ b/src/import/export.c @@ -18,6 +18,7 @@ #include "main-func.h" #include "signal-util.h" #include "string-util.h" +#include "terminal-util.h" #include "verbs.h" static ImportCompressType arg_compress = IMPORT_COMPRESS_UNKNOWN; @@ -201,16 +202,20 @@ static int export_raw(int argc, char *argv[], void *userdata) { } static int help(int argc, char *argv[], void *userdata) { - - printf("%s [OPTIONS...] {COMMAND} ...\n\n" - "Export container or virtual machine images.\n\n" + printf("%1$s [OPTIONS...] {COMMAND} ...\n" + "\n%4$sExport container or virtual machine images.%5$s\n" + "\n%2$sCommands:%3$s\n" + " tar NAME [FILE] Export a TAR image\n" + " raw NAME [FILE] Export a RAW image\n" + "\n%2$sOptions:%3$s\n" " -h --help Show this help\n" " --version Show package version\n" - " --format=FORMAT Select format\n\n" - "Commands:\n" - " tar NAME [FILE] Export a TAR image\n" - " raw NAME [FILE] Export a RAW image\n", - program_invocation_short_name); + " --format=FORMAT Select format\n\n", + program_invocation_short_name, + ansi_underline(), + ansi_normal(), + ansi_highlight(), + ansi_normal()); return 0; } From eb98074b5cc45253dfa1c207f57f1dc3766687fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 12 Sep 2023 16:45:22 +0200 Subject: [PATCH 2/6] bootctl: use pager in help The output is more than one page, let's enable the pager. --- src/boot/bootctl.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/boot/bootctl.c b/src/boot/bootctl.c index 1bc4aaf3ba..168c843d18 100644 --- a/src/boot/bootctl.c +++ b/src/boot/bootctl.c @@ -132,6 +132,8 @@ static int help(int argc, char *argv[], void *userdata) { _cleanup_free_ char *link = NULL; int r; + pager_open(arg_pager_flags); + r = terminal_urlify_man("bootctl", "1", &link); if (r < 0) return log_oom(); From 1535b52f1a3dc677a7f0a88467619fcc3020a856 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 14 Sep 2023 12:42:50 +0200 Subject: [PATCH 3/6] path: modernize, return first error --- src/path/path.c | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/src/path/path.c b/src/path/path.c index 3e022a467a..4f9ab61fbd 100644 --- a/src/path/path.c +++ b/src/path/path.c @@ -102,10 +102,9 @@ static const char* const path_table[_SD_PATH_MAX] = { }; static int list_homes(void) { - uint64_t i = 0; int r = 0; - for (i = 0; i < ELEMENTSOF(path_table); i++) { + for (size_t i = 0; i < ELEMENTSOF(path_table); i++) { _cleanup_free_ char *p = NULL; int q; @@ -114,7 +113,7 @@ static int list_homes(void) { log_full_errno(q == -ENXIO ? LOG_DEBUG : LOG_ERR, q, "Failed to query %s: %m", path_table[i]); if (q != -ENXIO) - r = q; + RET_GATHER(r, q); continue; } @@ -125,10 +124,9 @@ static int list_homes(void) { } static int print_home(const char *n) { - uint64_t i = 0; int r; - for (i = 0; i < ELEMENTSOF(path_table); i++) { + for (size_t i = 0; i < ELEMENTSOF(path_table); i++) if (streq(path_table[i], n)) { _cleanup_free_ char *p = NULL; @@ -139,7 +137,6 @@ static int print_home(const char *n) { printf("%s\n", p); return 0; } - } return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Path %s not known.", n); @@ -218,18 +215,13 @@ static int run(int argc, char* argv[]) { if (r <= 0) return r; - if (argc > optind) { - int i, q; + if (argc > optind) + for (int i = optind; i < argc; i++) + RET_GATHER(r, print_home(argv[i])); + else + r = list_homes(); - for (i = optind; i < argc; i++) { - q = print_home(argv[i]); - if (q < 0) - r = q; - } - - return r; - } else - return list_homes(); + return r; } DEFINE_MAIN_FUNCTION(run); From c19c5aa5b33c72d051e4920994aa8fc257eac3ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 14 Sep 2023 12:45:43 +0200 Subject: [PATCH 4/6] path: rename functions When this was originally added in 9a00f57a5ba7ed431e6bac8d8b36518708503b4e, the lookup function was called sd_path_home. But it was generalized a long time ago. --- src/path/path.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/path/path.c b/src/path/path.c index 4f9ab61fbd..a02047d110 100644 --- a/src/path/path.c +++ b/src/path/path.c @@ -101,7 +101,7 @@ static const char* const path_table[_SD_PATH_MAX] = { [SD_PATH_SYSTEMD_SEARCH_USER_ENVIRONMENT_GENERATOR] = "systemd-search-user-environment-generator", }; -static int list_homes(void) { +static int list_paths(void) { int r = 0; for (size_t i = 0; i < ELEMENTSOF(path_table); i++) { @@ -123,7 +123,7 @@ static int list_homes(void) { return r; } -static int print_home(const char *n) { +static int print_path(const char *n) { int r; for (size_t i = 0; i < ELEMENTSOF(path_table); i++) @@ -217,9 +217,9 @@ static int run(int argc, char* argv[]) { if (argc > optind) for (int i = optind; i < argc; i++) - RET_GATHER(r, print_home(argv[i])); + RET_GATHER(r, print_path(argv[i])); else - r = list_homes(); + r = list_paths(); return r; } From e441d1d46e8cd640ffac9d9231e5b16852fd9c28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 14 Sep 2023 22:30:14 +0200 Subject: [PATCH 5/6] path: add --no-pager option, enable pager by default When called with no argument, to list all known values, it is likely that it's used by somebody to look at all the whole list. The output is more than a page, so let's enable the pager. --- src/path/path.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/path/path.c b/src/path/path.c index a02047d110..9845425d01 100644 --- a/src/path/path.c +++ b/src/path/path.c @@ -12,10 +12,12 @@ #include "log.h" #include "macro.h" #include "main-func.h" +#include "pager.h" #include "pretty-print.h" #include "string-util.h" static const char *arg_suffix = NULL; +PagerFlags arg_pager_flags = 0; static const char* const path_table[_SD_PATH_MAX] = { [SD_PATH_TEMPORARY] = "temporary", @@ -104,6 +106,8 @@ static const char* const path_table[_SD_PATH_MAX] = { static int list_paths(void) { int r = 0; + pager_open(arg_pager_flags); + for (size_t i = 0; i < ELEMENTSOF(path_table); i++) { _cleanup_free_ char *p = NULL; int q; @@ -155,6 +159,7 @@ static int help(void) { " -h --help Show this help\n" " --version Show package version\n" " --suffix=SUFFIX Suffix to append to paths\n" + " --no-pager Do not pipe output into a pager\n" "\nSee the %s for details.\n", program_invocation_short_name, link); @@ -166,12 +171,14 @@ static int parse_argv(int argc, char *argv[]) { enum { ARG_VERSION = 0x100, ARG_SUFFIX, + ARG_NO_PAGER, }; static const struct option options[] = { { "help", no_argument, NULL, 'h' }, { "version", no_argument, NULL, ARG_VERSION }, { "suffix", required_argument, NULL, ARG_SUFFIX }, + { "no-pager", no_argument, NULL, ARG_NO_PAGER }, {} }; @@ -194,6 +201,10 @@ static int parse_argv(int argc, char *argv[]) { arg_suffix = optarg; break; + case ARG_NO_PAGER: + arg_pager_flags |= PAGER_DISABLE; + break; + case '?': return -EINVAL; From 87cc99332e2d30cda84790371b65dfc96e14fc87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 14 Sep 2023 22:28:23 +0200 Subject: [PATCH 6/6] man: add versioned version of --no-pager, use for systemd-path --- man/standard-options.xml | 14 +++++++++++++- man/systemd-path.xml | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/man/standard-options.xml b/man/standard-options.xml index e72442e5a7..75e91862f4 100644 --- a/man/standard-options.xml +++ b/man/standard-options.xml @@ -3,7 +3,9 @@ "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"> - + + @@ -29,6 +31,16 @@ + + + + + Do not pipe output into a pager. + + + + + diff --git a/man/systemd-path.xml b/man/systemd-path.xml index 81ce31d608..b1d566e4bf 100644 --- a/man/systemd-path.xml +++ b/man/systemd-path.xml @@ -59,6 +59,7 @@ +