chiark / gitweb /
Prep v239: terminal-util.[hc] - Mask new 'urlify' functions, we do not need them.
[elogind.git] / src / basic / terminal-util.c
index abd125f8f3df6a3a650288a25de9044edb86954e..f8623f9fdac777b1e1f57034c7821566af8a8b2f 100644 (file)
@@ -1,16 +1,11 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
-/***
-  This file is part of systemd.
-
-  Copyright 2010 Lennart Poettering
-***/
 
 #include <errno.h>
 #include <fcntl.h>
 #include <limits.h>
-//#include <linux/kd.h>
-//#include <linux/tiocl.h>
-//#include <linux/vt.h>
+#include <linux/kd.h>
+#include <linux/tiocl.h>
+#include <linux/vt.h>
 //#include <poll.h>
 //#include <signal.h>
 #include <stdarg.h>
@@ -29,6 +24,7 @@
 
 #include "alloc-util.h"
 //#include "copy.h"
+//#include "def.h"
 #include "env-util.h"
 #include "fd-util.h"
 #include "fileio.h"
@@ -896,8 +892,17 @@ void reset_terminal_feature_caches(void) {
 }
 
 bool on_tty(void) {
+
+        /* We check both stdout and stderr, so that situations where pipes on the shell are used are reliably
+         * recognized, regardless if only the output or the errors are piped to some place. Since on_tty() is generally
+         * used to default to a safer, non-interactive, non-color mode of operation it's probably good to be defensive
+         * here, and check for both. Note that we don't check for STDIN_FILENO, because it should fine to use fancy
+         * terminal functionality when outputting stuff, even if the input is piped to us. */
+
         if (cached_on_tty < 0)
-                cached_on_tty = isatty(STDOUT_FILENO) > 0;
+                cached_on_tty =
+                        isatty(STDOUT_FILENO) > 0 &&
+                        isatty(STDERR_FILENO) > 0;
 
         return cached_on_tty;
 }
@@ -1292,6 +1297,7 @@ int vt_reset_keyboard(int fd) {
         return 0;
 }
 
+#if 0 /// UNNEEDED by elogind
 static bool urlify_enabled(void) {
         static int cached_urlify_enabled = -1;
 
@@ -1384,29 +1390,52 @@ int terminal_urlify_path(const char *path, const char *text, char **ret) {
 }
 
 static int cat_file(const char *filename, bool newline) {
-        _cleanup_close_ int fd;
+        _cleanup_fclose_ FILE *f = NULL;
+        _cleanup_free_ char *urlified = NULL;
+        int r;
 
-        fd = open(filename, O_RDONLY|O_CLOEXEC|O_NOCTTY);
-        if (fd < 0)
+        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(),
-               filename,
+               urlified,
                ansi_normal());
         fflush(stdout);
 
-        return copy_bytes(fd, STDOUT_FILENO, (uint64_t) -1, 0);
+        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) {
+int cat_files(const char *file, char **dropins, CatFlags flags) {
         char **path;
         int r;
 
         if (file) {
                 r = cat_file(file, false);
-                if (r < 0)
+                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);
         }
 
@@ -1418,3 +1447,26 @@ int cat_files(const char *file, char **dropins) {
 
         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);
+}
+#endif // 0