mirror of
https://github.com/systemd/systemd-stable.git
synced 2025-01-11 05:17:44 +03:00
Split out pretty-print.c and move pager.c and main-func.h to shared/
This is high-level functionality, and fits better in shared/ (which is for our executables), than in basic/ (which is also for libraries).
This commit is contained in:
parent
0166c42868
commit
294bf0c34a
@ -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"
|
||||
|
@ -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"
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
@ -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"
|
||||
|
@ -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"
|
||||
|
@ -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"
|
||||
|
||||
|
@ -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"
|
||||
|
@ -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"
|
||||
|
@ -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"
|
||||
|
@ -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 */
|
||||
|
@ -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"
|
||||
|
@ -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"
|
||||
|
||||
|
@ -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 {
|
||||
|
@ -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"
|
||||
|
@ -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"
|
||||
|
||||
|
@ -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"
|
||||
|
||||
|
@ -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"
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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"
|
||||
|
@ -19,12 +19,12 @@
|
||||
#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"
|
||||
|
@ -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;
|
||||
|
@ -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"
|
||||
|
@ -17,11 +17,11 @@
|
||||
#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"
|
||||
|
@ -16,10 +16,10 @@
|
||||
#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"
|
||||
|
||||
|
@ -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"
|
||||
|
@ -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;
|
||||
|
@ -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"
|
||||
|
@ -13,10 +13,10 @@
|
||||
#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;
|
||||
|
@ -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"
|
||||
|
@ -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"
|
||||
|
@ -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;
|
||||
|
@ -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"
|
||||
|
||||
|
@ -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"
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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"
|
||||
|
@ -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"
|
||||
|
@ -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"
|
||||
|
@ -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"
|
||||
|
@ -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) {
|
||||
|
@ -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
|
||||
|
233
src/shared/pretty-print.c
Normal file
233
src/shared/pretty-print.c
Normal file
@ -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);
|
||||
}
|
15
src/shared/pretty-print.h
Normal file
15
src/shared/pretty-print.h
Normal file
@ -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);
|
@ -18,11 +18,11 @@
|
||||
#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;
|
||||
|
@ -20,10 +20,10 @@
|
||||
#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)
|
||||
|
@ -17,10 +17,10 @@
|
||||
#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;
|
||||
|
@ -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"
|
||||
|
@ -14,12 +14,12 @@
|
||||
#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"
|
||||
|
@ -305,6 +305,10 @@ tests += [
|
||||
[],
|
||||
[]],
|
||||
|
||||
[['src/test/test-pretty-print.c'],
|
||||
[],
|
||||
[]],
|
||||
|
||||
[['src/test/test-uid-range.c'],
|
||||
[],
|
||||
[]],
|
||||
|
40
src/test/test-pretty-print.c
Normal file
40
src/test/test-pretty-print.c
Normal file
@ -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;
|
||||
}
|
@ -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"
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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"
|
||||
|
@ -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"
|
||||
|
@ -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"
|
||||
|
@ -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"
|
||||
|
@ -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"
|
||||
|
@ -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"
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user