]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Split out pretty-print.c and move pager.c and main-func.h to shared/
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 20 Nov 2018 14:42:57 +0000 (15:42 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 20 Nov 2018 17:40:02 +0000 (18:40 +0100)
This is high-level functionality, and fits better in shared/ (which is for
our executables), than in basic/ (which is also for libraries).

67 files changed:
src/activate/activate.c
src/analyze/analyze.c
src/ask-password/ask-password.c
src/basic/conf-files.c
src/basic/conf-files.h
src/basic/meson.build
src/basic/terminal-util.c
src/basic/terminal-util.h
src/binfmt/binfmt.c
src/boot/bootctl.c
src/busctl/busctl.c
src/cgls/cgls.c
src/cgtop/cgtop.c
src/core/main.c
src/coredump/coredumpctl.c
src/cryptsetup/cryptsetup.c
src/delta/delta.c
src/detect-virt/detect-virt.c
src/escape/escape.c
src/firstboot/firstboot.c
src/hostname/hostnamectl.c
src/hwdb/hwdb.c
src/id128/id128.c
src/journal-remote/journal-gatewayd.c
src/journal-remote/journal-remote-main.c
src/journal-remote/journal-upload.c
src/journal/cat.c
src/journal/journalctl.c
src/locale/localectl.c
src/login/inhibit.c
src/login/loginctl.c
src/machine-id-setup/machine-id-setup-main.c
src/machine/machinectl.c
src/modules-load/modules-load.c
src/mount/mount-tool.c
src/network/networkctl.c
src/network/wait-online/wait-online.c
src/notify/notify.c
src/nspawn/nspawn.c
src/partition/growfs.c
src/path/path.c
src/portable/portablectl.c
src/resolve/resolvconf-compat.c
src/resolve/resolvectl.c
src/run/run.c
src/shared/id128-print.c
src/shared/main-func.h [moved from src/basic/main-func.h with 100% similarity]
src/shared/meson.build
src/shared/pager.c [moved from src/basic/pager.c with 100% similarity]
src/shared/pager.h [moved from src/basic/pager.h with 100% similarity]
src/shared/pretty-print.c [new file with mode: 0644]
src/shared/pretty-print.h [new file with mode: 0644]
src/sleep/sleep.c
src/socket-proxy/socket-proxyd.c
src/sysctl/sysctl.c
src/systemctl/systemctl.c
src/sysusers/sysusers.c
src/test/meson.build
src/test/test-pretty-print.c [new file with mode: 0644]
src/test/test-strip-tab-ansi.c
src/test/test-terminal-util.c
src/timedate/timedatectl.c
src/tmpfiles/tmpfiles.c
src/tty-ask-password-agent/tty-ask-password-agent.c
src/udev/udevadm.c
src/udev/udevd.c
src/veritysetup/veritysetup.c

index 25e2ec5aa7bc1e26c6ee0e4af52c35a81c571fda..4bc133a639fab1cab14e81243d7af9e1e5a79694 100644 (file)
@@ -14,6 +14,7 @@
 #include "fd-util.h"
 #include "log.h"
 #include "macro.h"
+#include "pretty-print.h"
 #include "process-util.h"
 #include "signal-util.h"
 #include "socket-util.h"
index 959c078c5c05a89a27dae48d1ee40de6bcfefc1f..5e0e03f53d9c11fb5230cec658ddd61441de404c 100644 (file)
@@ -30,8 +30,9 @@
 #include "pager.h"
 #include "parse-util.h"
 #include "path-util.h"
+#include "pretty-print.h"
 #if HAVE_SECCOMP
-#include "seccomp-util.h"
+#  include "seccomp-util.h"
 #endif
 #include "special.h"
 #include "strv.h"
index 571148f99b689e622fb80fd69b558f980ac670c0..530dc1ee6ef21ede89fba383d42d8b88513a9597 100644 (file)
@@ -10,8 +10,8 @@
 #include "log.h"
 #include "macro.h"
 #include "main-func.h"
+#include "pretty-print.h"
 #include "strv.h"
-#include "terminal-util.h"
 
 static const char *arg_icon = NULL;
 static const char *arg_id = NULL;
index d03366077d7c7b95b6660593ee8d2e03ed1d91a3..7b44ae277d7e88a11a1f825627282d125268d349 100644 (file)
@@ -328,36 +328,3 @@ int conf_files_list_with_replacement(
                 *replace_file = TAKE_PTR(p);
         return 0;
 }
-
-int conf_files_cat(const char *root, const char *name) {
-        _cleanup_strv_free_ char **dirs = NULL, **files = NULL;
-        _cleanup_free_ char *path = NULL;
-        const char *dir;
-        char **t;
-        int r;
-
-        NULSTR_FOREACH(dir, CONF_PATHS_NULSTR("")) {
-                assert(endswith(dir, "/"));
-                r = strv_extendf(&dirs, "%s%s.d", dir, name);
-                if (r < 0)
-                        return log_error_errno(r, "Failed to build directory list: %m");
-        }
-
-        r = conf_files_list_strv(&files, ".conf", root, 0, (const char* const*) dirs);
-        if (r < 0)
-                return log_error_errno(r, "Failed to query file list: %m");
-
-        path = path_join(root, "/etc", name);
-        if (!path)
-                return log_oom();
-
-        if (DEBUG_LOGGING) {
-                log_debug("Looking for configuration in:");
-                log_debug("   %s", path);
-                STRV_FOREACH(t, dirs)
-                        log_debug("   %s/*.conf", *t);
-        }
-
-        /* show */
-        return cat_files(path, files, CAT_FLAGS_MAIN_FILE_OPTIONAL);
-}
index 81ebcd46b6ef3d0f5485124c3d0b91366fce884b..f31f17de9d6017bfd291ba6bbaee60828681175f 100644 (file)
@@ -22,4 +22,3 @@ int conf_files_list_with_replacement(
                 const char *replacement,
                 char ***files,
                 char **replace_file);
-int conf_files_cat(const char *root, const char *name);
index abd54db8cc30aa53e6f54c2749be03a31f67c540..0b27ffda7d94f0993a60a7a0a3ff8d5dbe6bc595 100644 (file)
@@ -99,8 +99,6 @@ basic_sources = files('''
         nss-util.h
         ordered-set.c
         ordered-set.h
-        pager.c
-        pager.h
         parse-util.c
         parse-util.h
         path-util.c
index 41789598f7c680bc56b6026468e602699f0e643b..2c7b4508ce9ff9298dcf9b8817fccf82b5e3a8c4 100644 (file)
@@ -32,7 +32,6 @@
 #include "io-util.h"
 #include "log.h"
 #include "macro.h"
-#include "pager.h"
 #include "parse-util.h"
 #include "path-util.h"
 #include "proc-cmdline.h"
@@ -1272,184 +1271,3 @@ int vt_reset_keyboard(int fd) {
 
         return 0;
 }
-
-static bool urlify_enabled(void) {
-        static int cached_urlify_enabled = -1;
-
-        /* Unfortunately 'less' doesn't support links like this yet 😭, hence let's disable this as long as there's a
-         * pager in effect. Let's drop this check as soon as less got fixed a and enough time passed so that it's safe
-         * to assume that a link-enabled 'less' version has hit most installations. */
-
-        if (cached_urlify_enabled < 0) {
-                int val;
-
-                val = getenv_bool("SYSTEMD_URLIFY");
-                if (val >= 0)
-                        cached_urlify_enabled = val;
-                else
-                        cached_urlify_enabled = colors_enabled() && !pager_have();
-        }
-
-        return cached_urlify_enabled;
-}
-
-int terminal_urlify(const char *url, const char *text, char **ret) {
-        char *n;
-
-        assert(url);
-
-        /* Takes an URL and a pretty string and formats it as clickable link for the terminal. See
-         * https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda for details. */
-
-        if (isempty(text))
-                text = url;
-
-        if (urlify_enabled())
-                n = strjoin("\x1B]8;;", url, "\a", text, "\x1B]8;;\a");
-        else
-                n = strdup(text);
-        if (!n)
-                return -ENOMEM;
-
-        *ret = n;
-        return 0;
-}
-
-int terminal_urlify_path(const char *path, const char *text, char **ret) {
-        _cleanup_free_ char *absolute = NULL;
-        struct utsname u;
-        const char *url;
-        int r;
-
-        assert(path);
-
-        /* Much like terminal_urlify() above, but takes a file system path as input
-         * and turns it into a proper file:// URL first. */
-
-        if (isempty(path))
-                return -EINVAL;
-
-        if (isempty(text))
-                text = path;
-
-        if (!urlify_enabled()) {
-                char *n;
-
-                n = strdup(text);
-                if (!n)
-                        return -ENOMEM;
-
-                *ret = n;
-                return 0;
-        }
-
-        if (uname(&u) < 0)
-                return -errno;
-
-        if (!path_is_absolute(path)) {
-                r = path_make_absolute_cwd(path, &absolute);
-                if (r < 0)
-                        return r;
-
-                path = absolute;
-        }
-
-        /* As suggested by https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda, let's include the local
-         * hostname here. Note that we don't use gethostname_malloc() or gethostname_strict() since we are interested
-         * in the raw string the kernel has set, whatever it may be, under the assumption that terminals are not overly
-         * careful with validating the strings either. */
-
-        url = strjoina("file://", u.nodename, path);
-
-        return terminal_urlify(url, text, ret);
-}
-
-int terminal_urlify_man(const char *page, const char *section, char **ret) {
-        const char *url, *text;
-
-        url = strjoina("man:", page, "(", section, ")");
-        text = strjoina(page, "(", section, ") man page");
-
-        return terminal_urlify(url, text, ret);
-}
-
-static int cat_file(const char *filename, bool newline) {
-        _cleanup_fclose_ FILE *f = NULL;
-        _cleanup_free_ char *urlified = NULL;
-        int r;
-
-        f = fopen(filename, "re");
-        if (!f)
-                return -errno;
-
-        r = terminal_urlify_path(filename, NULL, &urlified);
-        if (r < 0)
-                return r;
-
-        printf("%s%s# %s%s\n",
-               newline ? "\n" : "",
-               ansi_highlight_blue(),
-               urlified,
-               ansi_normal());
-        fflush(stdout);
-
-        for (;;) {
-                _cleanup_free_ char *line = NULL;
-
-                r = read_line(f, LONG_LINE_MAX, &line);
-                if (r < 0)
-                        return log_error_errno(r, "Failed to read \"%s\": %m", filename);
-                if (r == 0)
-                        break;
-
-                puts(line);
-        }
-
-        return 0;
-}
-
-int cat_files(const char *file, char **dropins, CatFlags flags) {
-        char **path;
-        int r;
-
-        if (file) {
-                r = cat_file(file, false);
-                if (r == -ENOENT && (flags & CAT_FLAGS_MAIN_FILE_OPTIONAL))
-                        printf("%s# config file %s not found%s\n",
-                               ansi_highlight_magenta(),
-                               file,
-                               ansi_normal());
-                else if (r < 0)
-                        return log_warning_errno(r, "Failed to cat %s: %m", file);
-        }
-
-        STRV_FOREACH(path, dropins) {
-                r = cat_file(*path, file || path != dropins);
-                if (r < 0)
-                        return log_warning_errno(r, "Failed to cat %s: %m", *path);
-        }
-
-        return 0;
-}
-
-void print_separator(void) {
-
-        /* Outputs a separator line that resolves to whitespace when copied from the terminal. We do that by outputting
-         * one line filled with spaces with ANSI underline set, followed by a second (empty) line. */
-
-        if (underline_enabled()) {
-                size_t i, c;
-
-                c = columns();
-
-                flockfile(stdout);
-                fputs_unlocked(ANSI_UNDERLINE, stdout);
-
-                for (i = 0; i < c; i++)
-                        fputc_unlocked(' ', stdout);
-
-                fputs_unlocked(ANSI_NORMAL "\n\n", stdout);
-                funlockfile(stdout);
-        } else
-                fputs("\n\n", stdout);
-}
index 436951be98de1d7fec2a0bd79ab58003f4ad1f94..2d64afaee6143395bbdfb880cb421ffaf3b08826 100644 (file)
@@ -154,15 +154,3 @@ int open_terminal_in_namespace(pid_t pid, const char *name, int mode);
 
 int vt_default_utf8(void);
 int vt_reset_keyboard(int fd);
-
-int terminal_urlify(const char *url, const char *text, char **ret);
-int terminal_urlify_path(const char *path, const char *text, char **ret);
-int terminal_urlify_man(const char *page, const char *section, char **ret);
-
-typedef enum CatFlags {
-        CAT_FLAGS_MAIN_FILE_OPTIONAL = 1 << 0,
-} CatFlags;
-
-int cat_files(const char *file, char **dropins, CatFlags flags);
-
-void print_separator(void);
index b6f53a9bb893bfcc475489fbdcb8e057459ac12a..c3219d08738694b96e177fcbe5b906d22099a935 100644 (file)
@@ -17,9 +17,9 @@
 #include "main-func.h"
 #include "pager.h"
 #include "path-util.h"
+#include "pretty-print.h"
 #include "string-util.h"
 #include "strv.h"
-#include "terminal-util.h"
 #include "util.h"
 
 static bool arg_cat_config = false;
index 59241e5ee43cb93125197913484fc0c3108dfdcd..3ff3b3ffa380daf2238485d609536e91d9630e08 100644 (file)
@@ -32,6 +32,7 @@
 #include "main-func.h"
 #include "pager.h"
 #include "parse-util.h"
+#include "pretty-print.h"
 #include "rm-rf.h"
 #include "stat-util.h"
 #include "string-util.h"
index 0a325f963861936371545c3ea61a83e190dcb598..5ad2a7d12f9642b3c62a11a4ce47c4b11cc8edb4 100644 (file)
@@ -22,6 +22,7 @@
 #include "pager.h"
 #include "parse-util.h"
 #include "path-util.h"
+#include "pretty-print.h"
 #include "set.h"
 #include "strv.h"
 #include "terminal-util.h"
index b135dd6d519e2799d8e721666f1883c4def42253..3cdadc8fad5fa741815de4260b9c9bb49e1b185c 100644 (file)
@@ -18,8 +18,8 @@
 #include "output-mode.h"
 #include "pager.h"
 #include "path-util.h"
+#include "pretty-print.h"
 #include "strv.h"
-#include "terminal-util.h"
 #include "unit-name.h"
 #include "util.h"
 
index cad274a6334b812a56a872fef1cc4dbd46cd4897..562e6ece5b8e3c09464576ff3cb30a7215a42ae4 100644 (file)
@@ -22,6 +22,7 @@
 #include "main-func.h"
 #include "parse-util.h"
 #include "path-util.h"
+#include "pretty-print.h"
 #include "process-util.h"
 #include "procfs-util.h"
 #include "stdio-util.h"
index 70227da4b77441d0edb8b4ba5500967710b33a7d..f65206625d815636bd9e834b20a4c527721aceb0 100644 (file)
@@ -58,6 +58,7 @@
 #include "pager.h"
 #include "parse-util.h"
 #include "path-util.h"
+#include "pretty-print.h"
 #include "proc-cmdline.h"
 #include "process-util.h"
 #include "raw-clone.h"
index 9323e9d028c2f9632b0aefd2b62bfd4994de74c4..9f867bb1e73ac13d975d7cb97e68b69130512a28 100644 (file)
@@ -27,6 +27,7 @@
 #include "pager.h"
 #include "parse-util.h"
 #include "path-util.h"
+#include "pretty-print.h"
 #include "process-util.h"
 #include "rlimit-util.h"
 #include "sigbus.h"
index e1ceae06fa712bb9f3f2f7e9c8f1d3d853ebd186..8afff739907fdff6ed1924554619e378199b4403 100644 (file)
@@ -20,7 +20,7 @@
 #include "path-util.h"
 #include "string-util.h"
 #include "strv.h"
-#include "terminal-util.h"
+#include "pretty-print.h"
 #include "util.h"
 
 /* internal helper */
index a2aa28c75bc050a422df893dee5d90dab57ee7c9..f3a23f95d449c47dc744c1300181d510ef505c14 100644 (file)
@@ -17,6 +17,7 @@
 #include "pager.h"
 #include "parse-util.h"
 #include "path-util.h"
+#include "pretty-print.h"
 #include "process-util.h"
 #include "signal-util.h"
 #include "stat-util.h"
index f1259fdcd595e1cd4a23b4d4befc52cd9c155cad..1812218dafb70c346c8f9955059eede0d93ab3a4 100644 (file)
@@ -7,8 +7,8 @@
 
 #include "alloc-util.h"
 #include "main-func.h"
+#include "pretty-print.h"
 #include "string-table.h"
-#include "terminal-util.h"
 #include "util.h"
 #include "virt.h"
 
index b03c9e661d780f3f1c29f64471a9e3326da58af3..2b3cff0f3d7397db478041a4a1836643554813bd 100644 (file)
@@ -7,9 +7,9 @@
 #include "alloc-util.h"
 #include "log.h"
 #include "main-func.h"
+#include "pretty-print.h"
 #include "string-util.h"
 #include "strv.h"
-#include "terminal-util.h"
 #include "unit-name.h"
 
 static enum {
index 5b08f34fecf7f15f694ee303daa64311e65d9ed9..27dd494980ba08888b95aef14de6ae9e65c281e5 100644 (file)
@@ -32,6 +32,7 @@
 #include "os-util.h"
 #include "parse-util.h"
 #include "path-util.h"
+#include "pretty-print.h"
 #include "proc-cmdline.h"
 #include "random-util.h"
 #include "string-util.h"
index 0d40d17a556a2fbebc49f3511668b046e250e4f7..7f9bb49e0c900a2267794902c20903916acdcfd0 100644 (file)
@@ -15,8 +15,8 @@
 #include "bus-util.h"
 #include "hostname-util.h"
 #include "main-func.h"
+#include "pretty-print.h"
 #include "spawn-polkit-agent.h"
-#include "terminal-util.h"
 #include "util.h"
 #include "verbs.h"
 
index 89e313854282acd5e549233e0832ca15d053096f..b069a3eb8ae290dd94cac79fa85e16843cb73709 100644 (file)
@@ -7,8 +7,8 @@
 #include "alloc-util.h"
 #include "hwdb-util.h"
 #include "main-func.h"
+#include "pretty-print.h"
 #include "selinux-util.h"
-#include "terminal-util.h"
 #include "util.h"
 #include "verbs.h"
 
index cbd7464d2c97e05d495d9d864263ddc5ebf112d2..3c40dd6e5d1ac6c5ac06446840bc346b7148a507 100644 (file)
@@ -6,7 +6,7 @@
 #include "alloc-util.h"
 #include "id128-print.h"
 #include "main-func.h"
-#include "terminal-util.h"
+#include "pretty-print.h"
 #include "util.h"
 #include "verbs.h"
 
index 71553083d9db59fb99a0f021c72a2050477208e0..b39c7e33881ce65778c472018df70d005ad331fe 100644 (file)
@@ -21,8 +21,8 @@
 #include "microhttpd-util.h"
 #include "os-util.h"
 #include "parse-util.h"
+#include "pretty-print.h"
 #include "sigbus.h"
-#include "terminal-util.h"
 #include "util.h"
 
 #define JOURNAL_WAIT_TIMEOUT (10*USEC_PER_SEC)
index 930871b13b613b542fc0fa751ba7bfb151880938..af927046c6a347eb485795fc18a76d5159425b7c 100644 (file)
@@ -11,6 +11,7 @@
 #include "fileio.h"
 #include "journal-remote-write.h"
 #include "journal-remote.h"
+#include "pretty-print.h"
 #include "process-util.h"
 #include "rlimit-util.h"
 #include "signal-util.h"
@@ -18,7 +19,6 @@
 #include "stat-util.h"
 #include "string-table.h"
 #include "strv.h"
-#include "terminal-util.h"
 
 #define PRIV_KEY_FILE CERTIFICATE_ROOT "/private/journal-remote.pem"
 #define CERT_FILE     CERTIFICATE_ROOT "/certs/journal-remote.pem"
index b91d520cf0dafe837fcbd4868aff54043d6973eb..b9f98cda0aea2a4d6596f7fd420286225eea43d4 100644 (file)
 #include "log.h"
 #include "mkdir.h"
 #include "parse-util.h"
+#include "pretty-print.h"
 #include "process-util.h"
 #include "rlimit-util.h"
 #include "sigbus.h"
 #include "signal-util.h"
 #include "string-util.h"
-#include "terminal-util.h"
 #include "util.h"
 
 #define PRIV_KEY_FILE CERTIFICATE_ROOT "/private/journal-upload.pem"
index 3198d3261b465f8dffcada706d3d0fcdfc0f08bb..570515783aabd3998e83a7d0016bffd90ba16956 100644 (file)
@@ -13,9 +13,9 @@
 #include "fd-util.h"
 #include "main-func.h"
 #include "parse-util.h"
+#include "pretty-print.h"
 #include "string-util.h"
 #include "syslog-util.h"
-#include "terminal-util.h"
 #include "util.h"
 
 static const char *arg_identifier = NULL;
index e5a5acde8d93e2a7204580c1a3f98b190e6d907c..a2c812ba5704cc06c8d2db646c692272c238d7bc 100644 (file)
@@ -54,6 +54,7 @@
 #include "pager.h"
 #include "parse-util.h"
 #include "path-util.h"
+#include "pretty-print.h"
 #include "rlimit-util.h"
 #include "set.h"
 #include "sigbus.h"
index af1d27f9c7b32004bf5b435384020bfda8244148..9d76518bf13878e8ff94506cf1c11c2187ca09ed 100644 (file)
 #include "locale-util.h"
 #include "main-func.h"
 #include "pager.h"
+#include "pretty-print.h"
 #include "proc-cmdline.h"
 #include "set.h"
 #include "spawn-polkit-agent.h"
 #include "strv.h"
-#include "terminal-util.h"
 #include "util.h"
 #include "verbs.h"
 #include "virt.h"
index 7a63eb6efcb8424d1f5e62c88c1b4611032b421c..332cfdbba888ae0fb6f97bc9fb1c595aed2367b9 100644 (file)
 #include "format-util.h"
 #include "main-func.h"
 #include "pager.h"
+#include "pretty-print.h"
 #include "process-util.h"
 #include "signal-util.h"
 #include "strv.h"
-#include "terminal-util.h"
 #include "user-util.h"
 #include "util.h"
 
index 1f05753dff86a1d5e5dc79bd1b677fca39dc562b..427bb431cfc070c1b3b2e5d5028e1fc0b23e5a49 100644 (file)
@@ -21,6 +21,7 @@
 #include "main-func.h"
 #include "pager.h"
 #include "parse-util.h"
+#include "pretty-print.h"
 #include "process-util.h"
 #include "rlimit-util.h"
 #include "sigbus.h"
index c6a77cc90121fca677b2da318d08d119ec76ebc6..bfadd5881d7a9e257d4eb40fe57809446aaee0d1 100644 (file)
@@ -11,7 +11,7 @@
 #include "machine-id-setup.h"
 #include "main-func.h"
 #include "path-util.h"
-#include "terminal-util.h"
+#include "pretty-print.h"
 #include "util.h"
 
 static char *arg_root = NULL;
index 5dd46fc4a7a46ead2672d11e464c8d6ba0f0eb99..241e175ee6377ea0f42a6c95b2b4686864af64ec 100644 (file)
@@ -36,6 +36,7 @@
 #include "pager.h"
 #include "parse-util.h"
 #include "path-util.h"
+#include "pretty-print.h"
 #include "process-util.h"
 #include "ptyfwd.h"
 #include "rlimit-util.h"
index 0f7f4c384e1508a8d0c8150155326f2303070705..acb39516eba64e5a2a0bea8e321b314e25ceb4f7 100644 (file)
 #include "fileio.h"
 #include "log.h"
 #include "module-util.h"
+#include "pretty-print.h"
 #include "proc-cmdline.h"
 #include "string-util.h"
 #include "strv.h"
-#include "terminal-util.h"
 #include "util.h"
 
 static char **arg_proc_cmdline_modules = NULL;
index 62b910b48738c7ee61a6a722cea97d496ae92418..ce9473d60dc67b36561d9585fe44c4ef5775bb2a 100644 (file)
@@ -19,6 +19,7 @@
 #include "pager.h"
 #include "parse-util.h"
 #include "path-util.h"
+#include "pretty-print.h"
 #include "spawn-polkit-agent.h"
 #include "stat-util.h"
 #include "strv.h"
index 5fd67ad48460f1c47378a8df59bacd49b2652721..76629aa5075400df57693372f0e069d702f0112b 100644 (file)
@@ -24,6 +24,7 @@
 #include "netlink-util.h"
 #include "pager.h"
 #include "parse-util.h"
+#include "pretty-print.h"
 #include "socket-util.h"
 #include "sparse-endian.h"
 #include "stdio-util.h"
index 2d7459332672951b97dc5dae3daf1a775197d43a..7f30862bbfc79134f4c1e1b17c227ce6744625f8 100644 (file)
@@ -5,9 +5,9 @@
 #include "sd-daemon.h"
 
 #include "manager.h"
+#include "pretty-print.h"
 #include "signal-util.h"
 #include "strv.h"
-#include "terminal-util.h"
 
 static bool arg_quiet = false;
 static usec_t arg_timeout = 120 * USEC_PER_SEC;
index fdf4598941c8a5b67e5056bbdce8efff9a6f1b8b..07ad97a490d31678e7251f8a47dcbb5e084daabf 100644 (file)
@@ -14,9 +14,9 @@
 #include "log.h"
 #include "main-func.h"
 #include "parse-util.h"
+#include "pretty-print.h"
 #include "string-util.h"
 #include "strv.h"
-#include "terminal-util.h"
 #include "user-util.h"
 #include "util.h"
 
index cd986379b6a474e7be1067d8ed3accf7133266c5..3369dd18c9fc50042c50852aacbf07049db2f73a 100644 (file)
@@ -76,6 +76,7 @@
 #include "pager.h"
 #include "parse-util.h"
 #include "path-util.h"
+#include "pretty-print.h"
 #include "process-util.h"
 #include "ptyfwd.h"
 #include "random-util.h"
index 673b651da61b66afd79172eec2f36fbd39df1533..c089830b0d8a7f2951a3a93168f629131e038018 100644 (file)
@@ -22,8 +22,8 @@
 #include "mount-util.h"
 #include "parse-util.h"
 #include "path-util.h"
+#include "pretty-print.h"
 #include "strv.h"
-#include "terminal-util.h"
 
 static const char *arg_target = NULL;
 static bool arg_dry_run = false;
index 4a2e2ca3f61f4ad4c00e115ad92641c09881a9bf..2ac5f002a3fd684c056ca3a007ec97ab37ccca8b 100644 (file)
@@ -11,8 +11,8 @@
 #include "log.h"
 #include "macro.h"
 #include "main-func.h"
+#include "pretty-print.h"
 #include "string-util.h"
-#include "terminal-util.h"
 #include "util.h"
 
 static const char *arg_suffix = NULL;
index af2f84a84cc80d8ea3f379493400ee6ddb88c8af..beed2a8c1c9e05a22cd6ae1dc50abe865ae77a70 100644 (file)
@@ -20,6 +20,7 @@
 #include "pager.h"
 #include "parse-util.h"
 #include "path-util.h"
+#include "pretty-print.h"
 #include "spawn-polkit-agent.h"
 #include "string-util.h"
 #include "strv.h"
index 013b215a808e400e395b6428fc70673ed3282409..340767634fad8165ac10f832fd242e46921fc589 100644 (file)
@@ -9,6 +9,7 @@
 #include "extract-word.h"
 #include "fileio.h"
 #include "parse-util.h"
+#include "pretty-print.h"
 #include "resolvconf-compat.h"
 #include "resolvectl.h"
 #include "resolved-def.h"
index 44fa8ee62ca59a16977bcf38a764bfad7f1eba1a..d4a4b3f40eded6bbfdf74d09de305e79c41e0e95 100644 (file)
@@ -18,6 +18,7 @@
 #include "netlink-util.h"
 #include "pager.h"
 #include "parse-util.h"
+#include "pretty-print.h"
 #include "resolvconf-compat.h"
 #include "resolvectl.h"
 #include "resolved-def.h"
index 697b517c65d7815b35abca16415fdfddb6256b6c..dbd437593e7091acc4a8f64a2a80ec45d07d9bc2 100644 (file)
@@ -16,6 +16,7 @@
 #include "format-util.h"
 #include "parse-util.h"
 #include "path-util.h"
+#include "pretty-print.h"
 #include "process-util.h"
 #include "ptyfwd.h"
 #include "signal-util.h"
index eea3ee5ca8a0011a221f678507197967d836eb67..1b20b8f6445cdca2e13657181f0df9dde6c89bc9 100644 (file)
@@ -7,6 +7,7 @@
 #include "alloc-util.h"
 #include "id128-print.h"
 #include "log.h"
+#include "pretty-print.h"
 #include "terminal-util.h"
 
 int id128_pretty_print(sd_id128_t id, bool pretty) {
similarity index 100%
rename from src/basic/main-func.h
rename to src/shared/main-func.h
index f7f63a54042cae6561e193d4b03aabbb86862b3b..f7c2c7f560c13f075ac12c78d8fb293c23d07cd7 100644 (file)
@@ -96,6 +96,7 @@ shared_sources = files('''
         machine-image.h
         machine-pool.c
         machine-pool.h
+        main-func.h
         module-util.h
         nsflags.c
         nsflags.h
@@ -103,8 +104,12 @@ shared_sources = files('''
         os-util.h
         output-mode.c
         output-mode.h
+        pager.c
+        pager.h
         path-lookup.c
         path-lookup.h
+        pretty-print.c
+        pretty-print.h
         ptyfwd.c
         ptyfwd.h
         reboot-util.c
similarity index 100%
rename from src/basic/pager.c
rename to src/shared/pager.c
similarity index 100%
rename from src/basic/pager.h
rename to src/shared/pager.h
diff --git a/src/shared/pretty-print.c b/src/shared/pretty-print.c
new file mode 100644 (file)
index 0000000..1086018
--- /dev/null
@@ -0,0 +1,233 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+
+#include <sys/utsname.h>
+#include <errno.h>
+#include <stdio.h>
+
+#include "alloc-util.h"
+#include "conf-files.h"
+#include "def.h"
+#include "env-util.h"
+#include "fd-util.h"
+#include "fileio.h"
+#include "pager.h"
+#include "path-util.h"
+#include "pretty-print.h"
+#include "string-util.h"
+#include "strv.h"
+#include "terminal-util.h"
+#include "util.h"
+
+static bool urlify_enabled(void) {
+        static int cached_urlify_enabled = -1;
+
+        /* Unfortunately 'less' doesn't support links like this yet 😭, hence let's disable this as long as there's a
+         * pager in effect. Let's drop this check as soon as less got fixed a and enough time passed so that it's safe
+         * to assume that a link-enabled 'less' version has hit most installations. */
+
+        if (cached_urlify_enabled < 0) {
+                int val;
+
+                val = getenv_bool("SYSTEMD_URLIFY");
+                if (val >= 0)
+                        cached_urlify_enabled = val;
+                else
+                        cached_urlify_enabled = colors_enabled() && !pager_have();
+        }
+
+        return cached_urlify_enabled;
+}
+
+int terminal_urlify(const char *url, const char *text, char **ret) {
+        char *n;
+
+        assert(url);
+
+        /* Takes an URL and a pretty string and formats it as clickable link for the terminal. See
+         * https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda for details. */
+
+        if (isempty(text))
+                text = url;
+
+        if (urlify_enabled())
+                n = strjoin("\x1B]8;;", url, "\a", text, "\x1B]8;;\a");
+        else
+                n = strdup(text);
+        if (!n)
+                return -ENOMEM;
+
+        *ret = n;
+        return 0;
+}
+
+int terminal_urlify_path(const char *path, const char *text, char **ret) {
+        _cleanup_free_ char *absolute = NULL;
+        struct utsname u;
+        const char *url;
+        int r;
+
+        assert(path);
+
+        /* Much like terminal_urlify() above, but takes a file system path as input
+         * and turns it into a proper file:// URL first. */
+
+        if (isempty(path))
+                return -EINVAL;
+
+        if (isempty(text))
+                text = path;
+
+        if (!urlify_enabled()) {
+                char *n;
+
+                n = strdup(text);
+                if (!n)
+                        return -ENOMEM;
+
+                *ret = n;
+                return 0;
+        }
+
+        if (uname(&u) < 0)
+                return -errno;
+
+        if (!path_is_absolute(path)) {
+                r = path_make_absolute_cwd(path, &absolute);
+                if (r < 0)
+                        return r;
+
+                path = absolute;
+        }
+
+        /* As suggested by https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda, let's include the local
+         * hostname here. Note that we don't use gethostname_malloc() or gethostname_strict() since we are interested
+         * in the raw string the kernel has set, whatever it may be, under the assumption that terminals are not overly
+         * careful with validating the strings either. */
+
+        url = strjoina("file://", u.nodename, path);
+
+        return terminal_urlify(url, text, ret);
+}
+
+int terminal_urlify_man(const char *page, const char *section, char **ret) {
+        const char *url, *text;
+
+        url = strjoina("man:", page, "(", section, ")");
+        text = strjoina(page, "(", section, ") man page");
+
+        return terminal_urlify(url, text, ret);
+}
+
+static int cat_file(const char *filename, bool newline) {
+        _cleanup_fclose_ FILE *f = NULL;
+        _cleanup_free_ char *urlified = NULL;
+        int r;
+
+        f = fopen(filename, "re");
+        if (!f)
+                return -errno;
+
+        r = terminal_urlify_path(filename, NULL, &urlified);
+        if (r < 0)
+                return r;
+
+        printf("%s%s# %s%s\n",
+               newline ? "\n" : "",
+               ansi_highlight_blue(),
+               urlified,
+               ansi_normal());
+        fflush(stdout);
+
+        for (;;) {
+                _cleanup_free_ char *line = NULL;
+
+                r = read_line(f, LONG_LINE_MAX, &line);
+                if (r < 0)
+                        return log_error_errno(r, "Failed to read \"%s\": %m", filename);
+                if (r == 0)
+                        break;
+
+                puts(line);
+        }
+
+        return 0;
+}
+
+int cat_files(const char *file, char **dropins, CatFlags flags) {
+        char **path;
+        int r;
+
+        if (file) {
+                r = cat_file(file, false);
+                if (r == -ENOENT && (flags & CAT_FLAGS_MAIN_FILE_OPTIONAL))
+                        printf("%s# config file %s not found%s\n",
+                               ansi_highlight_magenta(),
+                               file,
+                               ansi_normal());
+                else if (r < 0)
+                        return log_warning_errno(r, "Failed to cat %s: %m", file);
+        }
+
+        STRV_FOREACH(path, dropins) {
+                r = cat_file(*path, file || path != dropins);
+                if (r < 0)
+                        return log_warning_errno(r, "Failed to cat %s: %m", *path);
+        }
+
+        return 0;
+}
+
+void print_separator(void) {
+
+        /* Outputs a separator line that resolves to whitespace when copied from the terminal. We do that by outputting
+         * one line filled with spaces with ANSI underline set, followed by a second (empty) line. */
+
+        if (underline_enabled()) {
+                size_t i, c;
+
+                c = columns();
+
+                flockfile(stdout);
+                fputs_unlocked(ANSI_UNDERLINE, stdout);
+
+                for (i = 0; i < c; i++)
+                        fputc_unlocked(' ', stdout);
+
+                fputs_unlocked(ANSI_NORMAL "\n\n", stdout);
+                funlockfile(stdout);
+        } else
+                fputs("\n\n", stdout);
+}
+
+int conf_files_cat(const char *root, const char *name) {
+        _cleanup_strv_free_ char **dirs = NULL, **files = NULL;
+        _cleanup_free_ char *path = NULL;
+        const char *dir;
+        char **t;
+        int r;
+
+        NULSTR_FOREACH(dir, CONF_PATHS_NULSTR("")) {
+                assert(endswith(dir, "/"));
+                r = strv_extendf(&dirs, "%s%s.d", dir, name);
+                if (r < 0)
+                        return log_error_errno(r, "Failed to build directory list: %m");
+        }
+
+        r = conf_files_list_strv(&files, ".conf", root, 0, (const char* const*) dirs);
+        if (r < 0)
+                return log_error_errno(r, "Failed to query file list: %m");
+
+        path = path_join(root, "/etc", name);
+        if (!path)
+                return log_oom();
+
+        if (DEBUG_LOGGING) {
+                log_debug("Looking for configuration in:");
+                log_debug("   %s", path);
+                STRV_FOREACH(t, dirs)
+                        log_debug("   %s/*.conf", *t);
+        }
+
+        /* show */
+        return cat_files(path, files, CAT_FLAGS_MAIN_FILE_OPTIONAL);
+}
diff --git a/src/shared/pretty-print.h b/src/shared/pretty-print.h
new file mode 100644 (file)
index 0000000..cf9a70d
--- /dev/null
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+#pragma once
+
+void print_separator(void);
+
+int terminal_urlify(const char *url, const char *text, char **ret);
+int terminal_urlify_path(const char *path, const char *text, char **ret);
+int terminal_urlify_man(const char *page, const char *section, char **ret);
+
+typedef enum CatFlags {
+        CAT_FLAGS_MAIN_FILE_OPTIONAL = 1 << 0,
+} CatFlags;
+
+int cat_files(const char *file, char **dropins, CatFlags flags);
+int conf_files_cat(const char *root, const char *name);
index f86d4c628f17243ffbbd7962cebe1336b1ddcecb..9e4831e35f3cb20d49bcbc8d6909a339b90ab492 100644 (file)
 #include "log.h"
 #include "main-func.h"
 #include "parse-util.h"
+#include "pretty-print.h"
 #include "sleep-config.h"
 #include "stdio-util.h"
 #include "string-util.h"
 #include "strv.h"
-#include "terminal-util.h"
 #include "util.h"
 
 static char* arg_verb = NULL;
index 227378edaaa041b1f27e2f1a71bc3a0243ec1a20..36ba5422eddcb4328283a29bd806039c92fdb57f 100644 (file)
 #include "log.h"
 #include "parse-util.h"
 #include "path-util.h"
+#include "pretty-print.h"
 #include "set.h"
 #include "socket-util.h"
 #include "string-util.h"
-#include "terminal-util.h"
 #include "util.h"
 
 #define BUFFER_SIZE (256 * 1024)
index d04a2cff155d8f44e706721b6715135713d9805a..adfb54675645e17df5e78715bd2958baadbe61fc 100644 (file)
 #include "main-func.h"
 #include "pager.h"
 #include "path-util.h"
+#include "pretty-print.h"
 #include "string-util.h"
 #include "strv.h"
 #include "sysctl-util.h"
-#include "terminal-util.h"
 #include "util.h"
 
 static char **arg_prefixes = NULL;
index 304dd1bbfb6441f1cf66677f6636afe52c357052..a54d5abebd624fe0710feac33330025921e131af 100644 (file)
@@ -56,6 +56,7 @@
 #include "parse-util.h"
 #include "path-lookup.h"
 #include "path-util.h"
+#include "pretty-print.h"
 #include "proc-cmdline.h"
 #include "process-util.h"
 #include "reboot-util.h"
index dfaff663892a345add0a708cea2efc1b6fc201d3..a348ec497aeca33ba16176fecf07fdf59d4a2183 100644 (file)
 #include "hashmap.h"
 #include "pager.h"
 #include "path-util.h"
+#include "pretty-print.h"
 #include "selinux-util.h"
 #include "smack-util.h"
 #include "specifier.h"
 #include "string-util.h"
 #include "strv.h"
-#include "terminal-util.h"
 #include "uid-range.h"
 #include "user-util.h"
 #include "utf8.h"
index 24c52a35790a83fdbb4dc189cdcb18e702470c53..ade905733ed910b3266629ccbf79f7c79000cc23 100644 (file)
@@ -305,6 +305,10 @@ tests += [
          [],
          []],
 
+        [['src/test/test-pretty-print.c'],
+         [],
+         []],
+
         [['src/test/test-uid-range.c'],
          [],
          []],
diff --git a/src/test/test-pretty-print.c b/src/test/test-pretty-print.c
new file mode 100644 (file)
index 0000000..53ec512
--- /dev/null
@@ -0,0 +1,40 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+
+#include <stdio.h>
+
+#include "alloc-util.h"
+#include "macro.h"
+#include "pretty-print.h"
+#include "strv.h"
+#include "tests.h"
+
+static void test_terminal_urlify(void) {
+        _cleanup_free_ char *formatted = NULL;
+
+        assert_se(terminal_urlify("https://www.freedesktop.org/wiki/Software/systemd/", "systemd homepage", &formatted) >= 0);
+        printf("Hey, considere visiting the %s right now! It is very good!\n", formatted);
+
+        formatted = mfree(formatted);
+
+        assert_se(terminal_urlify_path("/etc/fstab", "this link to your /etc/fstab", &formatted) >= 0);
+        printf("Or click on %s to have a look at it!\n", formatted);
+}
+
+static void test_cat_files(void) {
+        assert_se(cat_files("/no/such/file", NULL, 0) == -ENOENT);
+        assert_se(cat_files("/no/such/file", NULL, CAT_FLAGS_MAIN_FILE_OPTIONAL) == 0);
+
+        if (access("/etc/fstab", R_OK) >= 0)
+                assert_se(cat_files("/etc/fstab", STRV_MAKE("/etc/fstab", "/etc/fstab"), 0) == 0);
+}
+
+int main(int argc, char *argv[]) {
+        test_setup_logging(LOG_INFO);
+
+        test_terminal_urlify();
+        test_cat_files();
+
+        print_separator();
+
+        return 0;
+}
index 362f862221ef0916f400ce7541e86a1c07684cbe..fae384ef9b611354245e4b9ed9e82af43658d6f4 100644 (file)
@@ -3,6 +3,7 @@
 #include <stdio.h>
 
 #include "alloc-util.h"
+#include "pretty-print.h"
 #include "string-util.h"
 #include "terminal-util.h"
 #include "util.h"
index fbe8133b10a65ed9ec8479614c310e301c3f4cb7..7edf7681c25f7db8b252c687da6ff1c4d33d760c 100644 (file)
@@ -6,7 +6,7 @@
 #include "alloc-util.h"
 #include "fd-util.h"
 #include "fileio.h"
-#include "log.h"
+#include "tests.h"
 #include "macro.h"
 #include "strv.h"
 #include "terminal-util.h"
@@ -55,36 +55,11 @@ static void test_read_one_char(void) {
         unlink(name);
 }
 
-static void test_terminal_urlify(void) {
-        _cleanup_free_ char *formatted = NULL;
-
-        assert_se(terminal_urlify("https://www.freedesktop.org/wiki/Software/systemd/", "systemd homepage", &formatted) >= 0);
-        printf("Hey, considere visiting the %s right now! It is very good!\n", formatted);
-
-        formatted = mfree(formatted);
-
-        assert_se(terminal_urlify_path("/etc/fstab", "this link to your /etc/fstab", &formatted) >= 0);
-        printf("Or click on %s to have a look at it!\n", formatted);
-}
-
-static void test_cat_files(void) {
-        assert_se(cat_files("/no/such/file", NULL, 0) == -ENOENT);
-        assert_se(cat_files("/no/such/file", NULL, CAT_FLAGS_MAIN_FILE_OPTIONAL) == 0);
-
-        if (access("/etc/fstab", R_OK) >= 0)
-                assert_se(cat_files("/etc/fstab", STRV_MAKE("/etc/fstab", "/etc/fstab"), 0) == 0);
-}
-
 int main(int argc, char *argv[]) {
-        log_parse_environment();
-        log_open();
+        test_setup_logging(LOG_INFO);
 
         test_default_term_for_tty();
         test_read_one_char();
-        test_terminal_urlify();
-        test_cat_files();
-
-        print_separator();
 
         return 0;
 }
index c6d9a23bcfaf41656e047ae7af0e93bef83e6d1a..46712b71fc2afeec2a5b2883d6f563fad1ff7c37 100644 (file)
@@ -14,6 +14,7 @@
 #include "main-func.h"
 #include "pager.h"
 #include "parse-util.h"
+#include "pretty-print.h"
 #include "spawn-polkit-agent.h"
 #include "sparse-endian.h"
 #include "string-table.h"
index 9de9498d48615329b7001a31db7d4eb0b7c3a4d4..253d3af15af4459347ef820d258ff56d814114e0 100644 (file)
@@ -46,6 +46,7 @@
 #include "parse-util.h"
 #include "path-lookup.h"
 #include "path-util.h"
+#include "pretty-print.h"
 #include "rm-rf.h"
 #include "selinux-util.h"
 #include "set.h"
@@ -55,7 +56,6 @@
 #include "string-table.h"
 #include "string-util.h"
 #include "strv.h"
-#include "terminal-util.h"
 #include "umask-util.h"
 #include "user-util.h"
 #include "util.h"
index 0c966af499fb9fe7fb264b8a1feb3eb77ca3ccae..c147493bf884ab19d5df24bf2b1285b470596f1b 100644 (file)
@@ -33,6 +33,7 @@
 #include "main-func.h"
 #include "mkdir.h"
 #include "path-util.h"
+#include "pretty-print.h"
 #include "process-util.h"
 #include "signal-util.h"
 #include "socket-util.h"
index 8ed679698aa0b5e0b4d90eeab7933e4e58214547..1e1a92e138ce4c7298ea917162c80c8d038f448a 100644 (file)
@@ -6,9 +6,9 @@
 #include <stdio.h>
 
 #include "alloc-util.h"
+#include "pretty-print.h"
 #include "selinux-util.h"
 #include "string-util.h"
-#include "terminal-util.h"
 #include "udevadm.h"
 #include "udev-util.h"
 #include "verbs.h"
index 516453877fc0eddf806de1d511a50f9fc513aa59..c1c266e8c67f38bb764148c1f9432b5b6398e957 100644 (file)
@@ -49,6 +49,7 @@
 #include "mkdir.h"
 #include "netlink-util.h"
 #include "parse-util.h"
+#include "pretty-print.h"
 #include "proc-cmdline.h"
 #include "process-util.h"
 #include "selinux-util.h"
@@ -57,7 +58,6 @@
 #include "string-util.h"
 #include "strxcpyx.h"
 #include "syslog-util.h"
-#include "terminal-util.h"
 #include "udev-builtin.h"
 #include "udev-ctrl.h"
 #include "udev-util.h"
index a10baf2bfd2dac3723f06f38f299d86289470cbc..dbfd361d87bc37083de0f3f90dc3f88eb2c11b9e 100644 (file)
@@ -8,6 +8,7 @@
 #include "crypt-util.h"
 #include "hexdecoct.h"
 #include "log.h"
+#include "pretty-print.h"
 #include "string-util.h"
 #include "terminal-util.h"