]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/colors: use libtinfo to check terminal capability
authorKarel Zak <kzak@redhat.com>
Fri, 27 Feb 2015 12:57:34 +0000 (13:57 +0100)
committerKarel Zak <kzak@redhat.com>
Fri, 27 Feb 2015 12:57:34 +0000 (13:57 +0100)
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>
configure.ac
disk-utils/Makemodule.am
lib/Makemodule.am
lib/colors.c
libsmartcols/src/Makemodule.am
misc-utils/Makemodule.am
sys-utils/Makemodule.am
text-utils/Makemodule.am

index 122b5d532c6e453d2b8a66afc9e7fe91a30e4454..f7c3f8e57a4afcbf3d0d79e29a5ea3c54c539df8 100644 (file)
@@ -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],
index b4606a14898ccb8fcc4a34324b00ebb9bee5a3a0..766b40bcc6584aaf0e7508194239990aac54ff6c 100644 (file)
@@ -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
index 565294e5c340d93573f54a45fcb9e57f7c6cd8fb..a33f0a089d3e772ac8789e897e3e6f917671148d 100644 (file)
@@ -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
index 2e5af48d9b1dee670b473f137c787f845008d39d..da5a3e1f41eb6fac55839eb5809e63ce57ff0bde 100644 (file)
 #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;
index c1facad0c502398a51b96aa0c53e442531e75dfd..49ffea65f2d5c46c3f2df3d7e0d8d3e8cff466e9 100644 (file)
@@ -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)
index 3c79bec530571bb1e027aff649bf7e0b5ffe6cff..f7485c9584c74c74b8ecce9bad69f02e8868381b 100644 (file)
@@ -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
index 23b8f5c0cfce1e6d64ebcdad4bead9b0c805d822..8e6108d07148e38ff6c9e9e9cb8bc8f468b37331 100644 (file)
@@ -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
index bd3d53f82278d9c1156636cd1bc4f0d2f22e1076..94c8c7e055c3fb08d7c888fcb6cf7707f8f4e83e 100644 (file)
@@ -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