chiark / gitweb /
terminal-util: port some generic code over to rearrange_stdio()
authorLennart Poettering <lennart@poettering.net>
Wed, 28 Feb 2018 20:21:33 +0000 (21:21 +0100)
committerSven Eden <yamakuzure@gmx.net>
Wed, 30 May 2018 05:59:13 +0000 (07:59 +0200)
src/basic/fd-util.h
src/basic/terminal-util.c
src/basic/terminal-util.h

index f53839d4560198fc304cb5c2129725cb46e51069..7c39257cdc4560b1983e130ce352b53bf41bdd4f 100644 (file)
@@ -106,3 +106,7 @@ int acquire_data_fd(const void *data, size_t size, unsigned flags);
 int fd_move_above_stdio(int fd);
 
 int rearrange_stdio(int original_input_fd, int original_output_fd, int original_error_fd);
+
+static inline int make_null_stdio(void) {
+        return rearrange_stdio(-1, -1, -1);
+}
index 7c446d4aebea016acc0fa4fa30263504514ed0a4..ccebe805d3676482d43844df077380b795d47261 100644 (file)
@@ -636,9 +636,9 @@ int make_console_stdio(void) {
         if (r < 0)
                 log_warning_errno(r, "Failed to reset terminal, ignoring: %m");
 
-        r = make_stdio(fd);
+        r = rearrange_stdio(fd, fd, fd); /* This invalidates 'fd' both on success and on failure. */
         if (r < 0)
-                return log_error_errno(r, "Failed to duplicate terminal fd: %m");
+                return log_error_errno(r, "Failed to make terminal stdin/stdout/stderr: %m");
 
         reset_terminal_feature_caches();
 
@@ -918,40 +918,6 @@ bool on_tty(void) {
         return cached_on_tty;
 }
 
-int make_stdio(int fd) {
-        int r = 0;
-
-        assert(fd >= 0);
-
-        if (dup2(fd, STDIN_FILENO) < 0)
-                r = -errno;
-        if (dup2(fd, STDOUT_FILENO) < 0 && r >= 0)
-                r = -errno;
-        if (dup2(fd, STDERR_FILENO) < 0 && r >= 0)
-                r = -errno;
-
-        safe_close_above_stdio(fd);
-
-        /* Explicitly unset O_CLOEXEC, since if fd was < 3, then dup2() was a NOP and the bit hence possibly set. */
-        stdio_unset_cloexec();
-
-        return r;
-}
-
-int make_null_stdio(void) {
-        int null_fd, r;
-
-        null_fd = open("/dev/null", O_RDWR|O_NOCTTY|O_CLOEXEC);
-        if (null_fd < 0)
-                return -errno;
-
-        r = make_stdio(null_fd);
-
-        reset_terminal_feature_caches();
-
-        return r;
-}
-
 int getttyname_malloc(int fd, char **ret) {
         size_t l = 100;
         int r;
index cd93f4c4b22f25c9a401c7788bfa0c1e91136bd7..3c41a5b976b69ba4946e83082b79a9a24e083f1e 100644 (file)
 #include "macro.h"
 #include "time-util.h"
 
-/* Regular colors */
-#define ANSI_BLACK   "\x1B[0;30m"
-#define ANSI_RED     "\x1B[0;31m"
-#define ANSI_GREEN   "\x1B[0;32m"
-#define ANSI_YELLOW  "\x1B[0;33m"
-#define ANSI_BLUE    "\x1B[0;34m"
-#define ANSI_MAGENTA "\x1B[0;35m"
-#define ANSI_CYAN    "\x1B[0;36m"
-#define ANSI_WHITE   "\x1B[0;37m"
-
-/* Bold/highlighted */
-#define ANSI_HIGHLIGHT_BLACK   "\x1B[0;1;30m"
-#define ANSI_HIGHLIGHT_RED     "\x1B[0;1;31m"
-#define ANSI_HIGHLIGHT_GREEN   "\x1B[0;1;32m"
-#define ANSI_HIGHLIGHT_YELLOW  "\x1B[0;1;33m"
-#define ANSI_HIGHLIGHT_BLUE    "\x1B[0;1;34m"
-#define ANSI_HIGHLIGHT_MAGENTA "\x1B[0;1;35m"
-#define ANSI_HIGHLIGHT_CYAN    "\x1B[0;1;36m"
-#define ANSI_HIGHLIGHT_WHITE   "\x1B[0;1;37m"
-
-/* Underlined */
-#define ANSI_HIGHLIGHT_BLACK_UNDERLINE   "\x1B[0;1;4;30m"
-#define ANSI_HIGHLIGHT_RED_UNDERLINE     "\x1B[0;1;4;31m"
-#define ANSI_HIGHLIGHT_GREEN_UNDERLINE   "\x1B[0;1;4;32m"
-#define ANSI_HIGHLIGHT_YELLOW_UNDERLINE  "\x1B[0;1;4;33m"
-#define ANSI_HIGHLIGHT_BLUE_UNDERLINE    "\x1B[0;1;4;34m"
-#define ANSI_HIGHLIGHT_MAGENTA_UNDERLINE "\x1B[0;1;4;35m"
-#define ANSI_HIGHLIGHT_CYAN_UNDERLINE    "\x1B[0;1;4;36m"
-#define ANSI_HIGHLIGHT_WHITE_UNDERLINE   "\x1B[0;1;4;37m"
-
-/* Other ANSI codes */
+#define ANSI_RED "\x1B[0;31m"
+#define ANSI_GREEN "\x1B[0;32m"
 #define ANSI_UNDERLINE "\x1B[0;4m"
 #define ANSI_HIGHLIGHT "\x1B[0;1;39m"
+#define ANSI_HIGHLIGHT_RED "\x1B[0;1;31m"
+#define ANSI_HIGHLIGHT_GREEN "\x1B[0;1;32m"
+#define ANSI_HIGHLIGHT_YELLOW "\x1B[0;1;33m"
+#define ANSI_HIGHLIGHT_BLUE "\x1B[0;1;34m"
 #define ANSI_HIGHLIGHT_UNDERLINE "\x1B[0;1;4m"
-
-/* Reset/clear ANSI styles */
+#define ANSI_HIGHLIGHT_RED_UNDERLINE "\x1B[0;1;4;31m"
+#define ANSI_HIGHLIGHT_GREEN_UNDERLINE "\x1B[0;1;4;32m"
+#define ANSI_HIGHLIGHT_YELLOW_UNDERLINE "\x1B[0;1;4;33m"
+#define ANSI_HIGHLIGHT_BLUE_UNDERLINE "\x1B[0;1;4;34m"
 #define ANSI_NORMAL "\x1B[0m"
 
-/* Erase characters until the end of the line */
 #define ANSI_ERASE_TO_END_OF_LINE "\x1B[K"
 
 /* Set cursor to top left corner and clear screen */
@@ -124,8 +100,6 @@ int vtnr_from_tty(const char *tty);
 const char *default_term_for_tty(const char *tty);
 #endif // 0
 
-int make_stdio(int fd);
-int make_null_stdio(void);
 #if 0 /// UNNEEDED by elogind
 int make_console_stdio(void);
 #endif // 0