summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKarel Zak2015-02-27 13:57:34 +0100
committerKarel Zak2015-02-27 13:57:34 +0100
commit4310faf9507b863adf0d735564f28db5c68ca600 (patch)
treee60620d3a69b9566e9e214cc316ccf2525bc116d
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>
-rw-r--r--configure.ac3
-rw-r--r--disk-utils/Makemodule.am6
-rw-r--r--lib/Makemodule.am14
-rw-r--r--lib/colors.c35
-rw-r--r--libsmartcols/src/Makemodule.am5
-rw-r--r--misc-utils/Makemodule.am2
-rw-r--r--sys-utils/Makemodule.am3
-rw-r--r--text-utils/Makemodule.am2
8 files changed, 57 insertions, 13 deletions
diff --git a/configure.ac b/configure.ac
index 122b5d532..f7c3f8e57 100644
--- a/configure.ac
+++ b/configure.ac
@@ -778,6 +778,9 @@ PKG_CHECK_MODULES(TINFO, [tinfo], [have_tinfo=yes], [
AC_SUBST([TINFO_LIBS])
AC_SUBST([TINFO_CFLAGS])
AM_CONDITIONAL([HAVE_TINFO], [test "x$have_tinfo" = xyes])
+AS_IF([test "x$have_tinfo" = xyes],
+ AC_DEFINE(HAVE_LIBTINFO, 1, [Define if libtinfo available.])
+])
AC_ARG_WITH([utempter],
diff --git a/disk-utils/Makemodule.am b/disk-utils/Makemodule.am
index b4606a148..766b40bcc 100644
--- a/disk-utils/Makemodule.am
+++ b/disk-utils/Makemodule.am
@@ -134,7 +134,7 @@ fdisk_SOURCES = \
disk-utils/fdisk-list.c \
disk-utils/fdisk-list.h
-fdisk_LDADD = $(LDADD) libcommon.la libfdisk.la libsmartcols.la
+fdisk_LDADD = $(LDADD) libcommon.la libfdisk.la libsmartcols.la libtcolors.la
fdisk_CFLAGS = $(AM_CFLAGS) -I$(ul_libfdisk_incdir) -I$(ul_libsmartcols_incdir)
if BUILD_LIBBLKID
@@ -166,7 +166,7 @@ sfdisk_SOURCES = \
disk-utils/fdisk-list.c \
disk-utils/fdisk-list.h
-sfdisk_LDADD = $(LDADD) libcommon.la libfdisk.la libsmartcols.la
+sfdisk_LDADD = $(LDADD) libcommon.la libfdisk.la libsmartcols.la libtcolors.la
sfdisk_CFLAGS = $(AM_CFLAGS) -I$(ul_libfdisk_incdir) -I$(ul_libsmartcols_incdir)
if BUILD_LIBUUID
@@ -193,7 +193,7 @@ if BUILD_CFDISK
sbin_PROGRAMS += cfdisk
dist_man_MANS += disk-utils/cfdisk.8
cfdisk_SOURCES = disk-utils/cfdisk.c
-cfdisk_LDADD = $(LDADD) libcommon.la libfdisk.la
+cfdisk_LDADD = $(LDADD) libcommon.la libfdisk.la libtcolors.la
cfdisk_CFLAGS = $(AM_CFLAGS) -I$(ul_libfdisk_incdir)
if BUILD_LIBUUID
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;
diff --git a/libsmartcols/src/Makemodule.am b/libsmartcols/src/Makemodule.am
index c1facad0c..49ffea65f 100644
--- a/libsmartcols/src/Makemodule.am
+++ b/libsmartcols/src/Makemodule.am
@@ -22,7 +22,7 @@ libsmartcols_la_SOURCES= \
nodist_libsmartcols_la_SOURCES = libsmartcols/src/smartcolsP.h
-libsmartcols_la_LIBADD = libcommon.la
+libsmartcols_la_LIBADD = libcommon.la libtcolors.la
libsmartcols_la_CFLAGS = \
$(SOLIB_CFLAGS) \
@@ -31,6 +31,7 @@ libsmartcols_la_CFLAGS = \
libsmartcols_la_DEPENDENCIES = \
libcommon.la \
+ libtcolors.la \
libsmartcols/src/libsmartcols.sym \
libsmartcols/src/libsmartcols.h.in
@@ -48,7 +49,7 @@ if BUILD_LIBSMARTCOLS_TESTS
check_PROGRAMS += test_smartcols
libsmartcols_tests_cflags = $(libsmartcols_la_CFLAGS)
-libsmartcols_tests_ldadd = libsmartcols.la libcommon.la
+libsmartcols_tests_ldadd = libsmartcols.la libcommon.la libtcolors.la
test_smartcols_SOURCES = libsmartcols/src/test.c
test_smartcols_CFLAGS = $(libsmartcols_tests_cflags)
diff --git a/misc-utils/Makemodule.am b/misc-utils/Makemodule.am
index 3c79bec53..f7485c958 100644
--- a/misc-utils/Makemodule.am
+++ b/misc-utils/Makemodule.am
@@ -11,7 +11,7 @@ cal_SOURCES += lib/langinfo.c
endif
cal_CFLAGS = $(AM_CFLAGS) $(NCURSES_CFLAGS)
-cal_LDADD = $(LDADD) libcommon.la $(NCURSES_LIBS) $(TINFO_LIBS)
+cal_LDADD = $(LDADD) libcommon.la libtcolors.la $(NCURSES_LIBS)
if HAVE_TERMCAP
cal_LDADD += -ltermcap
endif
diff --git a/sys-utils/Makemodule.am b/sys-utils/Makemodule.am
index 23b8f5c0c..8e6108d07 100644
--- a/sys-utils/Makemodule.am
+++ b/sys-utils/Makemodule.am
@@ -73,7 +73,8 @@ if BUILD_DMESG
bin_PROGRAMS += dmesg
dist_man_MANS += sys-utils/dmesg.1
dmesg_SOURCES = sys-utils/dmesg.c lib/monotonic.c
-dmesg_LDADD = $(LDADD) libcommon.la $(CLOCKGETTIME_LIBS)
+dmesg_LDADD = $(LDADD) libcommon.la libtcolors.la $(CLOCKGETTIME_LIBS)
+dmesg_CFLAGS = $(AM_CFLAGS)
endif
if BUILD_CTRLALTDEL
diff --git a/text-utils/Makemodule.am b/text-utils/Makemodule.am
index bd3d53f82..94c8c7e05 100644
--- a/text-utils/Makemodule.am
+++ b/text-utils/Makemodule.am
@@ -34,7 +34,7 @@ hexdump_SOURCES = \
text-utils/hexdump.c \
text-utils/hexdump.h \
text-utils/hexdump-parse.c
-hexdump_LDADD = $(LDADD) libcommon.la
+hexdump_LDADD = $(LDADD) libcommon.la libtcolors.la
endif
if BUILD_REV