summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorKarel Zak2015-02-27 13:57:34 +0100
committerKarel Zak2015-02-27 13:57:34 +0100
commit4310faf9507b863adf0d735564f28db5c68ca600 (patch)
treee60620d3a69b9566e9e214cc316ccf2525bc116d /lib
parentMerge branch 'fixes' of https://github.com/rudimeier/util-linux (diff)
downloadkernel-qcow2-util-linux-4310faf9507b863adf0d735564f28db5c68ca600.tar.gz
kernel-qcow2-util-linux-4310faf9507b863adf0d735564f28db5c68ca600.tar.xz
kernel-qcow2-util-linux-4310faf9507b863adf0d735564f28db5c68ca600.zip
lib/colors: use libtinfo to check terminal capability
The current implementation assumes that all terminals supports colors and users are forcet to use terminal-colors.d/ to disable colors for some terminals. This patch checks for maximal supported colors for the current terminal and colors are automatically disabled for terminals like vt100. The patch moves lib/colors.c from libcommon.la to libtcolors.la to avoid collisions with another utils. Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/Makemodule.am14
-rw-r--r--lib/colors.c35
2 files changed, 44 insertions, 5 deletions
diff --git a/lib/Makemodule.am b/lib/Makemodule.am
index 565294e5c..a33f0a089 100644
--- a/lib/Makemodule.am
+++ b/lib/Makemodule.am
@@ -5,7 +5,6 @@ libcommon_la_SOURCES = \
lib/at.c \
lib/blkdev.c \
lib/canonicalize.c \
- lib/colors.c \
lib/crc32.c \
lib/crc64.c \
lib/env.c \
@@ -41,8 +40,15 @@ if HAVE_CPU_SET_T
libcommon_la_SOURCES += lib/cpuset.c
endif
+
+noinst_LTLIBRARIES += libtcolors.la
+libtcolors_la_CFLAGS = $(AM_CFLAGS) $(TINFO_CFLAGS)
+libtcolors_la_LIBADD = $(TINFO_LIBS)
+libtcolors_la_SOURCES = lib/colors.c include/colors.h
+
dist_man_MANS += lib/terminal-colors.d.5
+
check_PROGRAMS += \
test_at \
test_blkdev \
@@ -56,6 +62,9 @@ check_PROGRAMS += \
test_strutils \
test_ttyutils
+
+
+
if LINUX
if HAVE_CPU_SET_T
check_PROGRAMS += test_cpuset
@@ -87,7 +96,8 @@ test_strutils_SOURCES = lib/strutils.c
test_strutils_CFLAGS = -DTEST_PROGRAM
test_colors_SOURCES = lib/colors.c
-test_colors_CFLAGS = -DTEST_PROGRAM
+test_colors_CFLAGS = -DTEST_PROGRAM $(TINFO_CFLAGS)
+test_colors_LDADD = $(LDADD) $(TINFO_LIBS)
test_randutils_SOURCES = lib/randutils.c
test_randutils_CFLAGS = -DTEST_PROGRAM
diff --git a/lib/colors.c b/lib/colors.c
index 2e5af48d9..da5a3e1f4 100644
--- a/lib/colors.c
+++ b/lib/colors.c
@@ -10,6 +10,10 @@
#include <sys/types.h>
#include <dirent.h>
#include <ctype.h>
+#ifdef HAVE_LIBTINFO
+# include <curses.h>
+# include <term.h>
+#endif
#include "c.h"
#include "colors.h"
@@ -664,6 +668,31 @@ static void termcolors_init_debug(void)
__UL_INIT_DEBUG(termcolors, TERMCOLORS_DEBUG_, 0, TERMINAL_COLORS_DEBUG);
}
+static int colors_terminal_is_ready(void)
+{
+ int ncolors = -1;
+
+ if (isatty(STDOUT_FILENO) != 1)
+ goto none;
+
+#ifdef HAVE_LIBTINFO
+ {
+ int ret;
+
+ if (setupterm(NULL, STDOUT_FILENO, &ret) != OK || ret != 1)
+ goto none;
+ ncolors = tigetnum("colors");
+ if (ncolors <= 2)
+ goto none;
+ }
+#endif
+ DBG(CONF, ul_debug("terminal is ready (supports %d colors)", ncolors));
+ return 1;
+none:
+ DBG(CONF, ul_debug("terminal is NOT ready"));
+ return 0;
+}
+
/**
* colors_init:
* @mode: UL_COLORMODE_*
@@ -676,7 +705,7 @@ static void termcolors_init_debug(void)
*/
int colors_init(int mode, const char *name)
{
- int atty = -1;
+ int ready = -1;
struct ul_color_ctl *cc = &ul_colors;
cc->utilname = name;
@@ -684,7 +713,7 @@ int colors_init(int mode, const char *name)
termcolors_init_debug();
- if (mode == UL_COLORMODE_UNDEF && (atty = isatty(STDOUT_FILENO))) {
+ if (mode == UL_COLORMODE_UNDEF && (ready = colors_terminal_is_ready())) {
int rc = colors_read_configuration(cc);
if (rc)
cc->mode = UL_COLORMODE_DEFAULT;
@@ -703,7 +732,7 @@ int colors_init(int mode, const char *name)
switch (cc->mode) {
case UL_COLORMODE_AUTO:
- cc->has_colors = atty == -1 ? isatty(STDOUT_FILENO) : atty;
+ cc->has_colors = ready == -1 ? colors_terminal_is_ready() : ready;
break;
case UL_COLORMODE_ALWAYS:
cc->has_colors = 1;