]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/colors: check for /etc/terminal-colors.d/[util].disable
authorOndrej Oprala <ooprala@redhat.com>
Thu, 23 Jan 2014 12:03:45 +0000 (13:03 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 11 Mar 2014 08:38:09 +0000 (09:38 +0100)
[kzak@redhat.com: - move paths to pathnames.h,
                  - use static path buffer]

Signed-off-by: Ondrej Oprala <ooprala@redhat.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
fdisks/fdisk.c
include/colors.h
include/pathnames.h
lib/colors.c
misc-utils/cal.c
sys-utils/dmesg.c
text-utils/hexdump.c

index 7afd6420bc85a49a1169db1d7999a44b81f20873..a5c1fe63431618339fd12ea65599415650788828 100644 (file)
@@ -397,7 +397,7 @@ enum {
 int main(int argc, char **argv)
 {
        int i, c, act = ACT_FDISK;
-       int colormode = UL_COLORMODE_AUTO;
+       int colormode = UL_COLORMODE_UNDEF;
        struct fdisk_context *cxt;
 
        setlocale(LC_ALL, "");
@@ -500,7 +500,7 @@ int main(int argc, char **argv)
                warnx(_("The device properties (sector size and geometry) should"
                        " be used with one specified device only."));
 
-       colors_init(colormode);
+       colors_init(colormode, "fdisk");
 
        switch (act) {
        case ACT_LIST:
index 16db01075dc3a93565523c66e0f0ad199e4cd07f..42881d5e38ac2e4a44e341b3b8276d1e95eccf77 100644 (file)
@@ -43,6 +43,7 @@ enum colortmode {
        UL_COLORMODE_AUTO = 0,
        UL_COLORMODE_NEVER,
        UL_COLORMODE_ALWAYS,
+       UL_COLORMODE_UNDEF,
 
        __UL_NCOLORMODES        /* last */
 };
@@ -50,8 +51,8 @@ enum colortmode {
 extern int colormode_from_string(const char *str);
 extern int colormode_or_err(const char *str, const char *errmsg);
 
-/* Initialize the global variable OUT_IS_TERM */
-extern int colors_init(int mode);
+/* Initialize the global variable UL_COLOR_TERM_OK */
+extern int colors_init(int mode, const char *util_name);
 
 /* Returns 1 or 0 */
 extern int colors_wanted(void);
index 2957dacb56a9f3f8de303681c2c06f2527a19bae..94cc412a7fc4d028222eaf8f9d1eefd1a223d3bd 100644 (file)
@@ -50,6 +50,9 @@
 #define _PATH_SECURE           "/etc/securesingle"
 #define _PATH_USERTTY           "/etc/usertty"
 
+#define _PATH_TERMCOLORS_DIR   "/etc/terminal-colors.d/"
+#define _PATH_TERMCOLORS_DISABLE _PATH_TERMCOLORS_DIR "disable"
+
 /* used in login-utils/shutdown.c */
 
 /* used in login-utils/setpwnam.h and login-utils/islocal.c */
index a197823b46c24f24bd5fbabc33fab030cd0a77e5..bf5fa855db359b4bf26ed2f7cecc8ce1d5e5341e 100644 (file)
@@ -6,14 +6,33 @@
  */
 #include <c.h>
 #include <assert.h>
+#include <sys/stat.h>
 
 #include "colors.h"
+#include "xalloc.h"
+#include "pathnames.h"
 
 static int ul_color_term_ok;
 
-int colors_init(int mode)
+int colors_init(int mode, const char *name)
 {
        switch (mode) {
+       case UL_COLORMODE_UNDEF:
+               if (access(_PATH_TERMCOLORS_DISABLE, F_OK) == 0) {
+                       ul_color_term_ok = 0;
+                       break;
+               } else {
+                       char path[PATH_MAX];
+
+                       snprintf(path, sizeof(path), "%s%s%s",
+                               _PATH_TERMCOLORS_DIR, name, ".disable");
+
+                       if (access(path, F_OK) == 0) {
+                               ul_color_term_ok = 0;
+                               break;
+                       }
+               }
+               /* fallthrough */
        case UL_COLORMODE_AUTO:
                ul_color_term_ok = isatty(STDOUT_FILENO);
                break;
@@ -50,7 +69,8 @@ int colormode_from_string(const char *str)
        static const char *modes[] = {
                [UL_COLORMODE_AUTO]   = "auto",
                [UL_COLORMODE_NEVER]  = "never",
-               [UL_COLORMODE_ALWAYS] = "always"
+               [UL_COLORMODE_ALWAYS] = "always",
+               [UL_COLORMODE_UNDEF] = ""
        };
 
        if (!str || !*str)
@@ -144,8 +164,9 @@ int main(int argc, char *argv[])
                }
        }
 
-       colors_init(mode);
-       color_enable(colorscheme_from_string(scheme));
+       colors_init(mode, program_invocation_short_name);
+       color_enable(UL_COLOR_RED);
+
        printf("Hello World!");
        color_disable();
        return EXIT_SUCCESS;
index 747694d751f3c8bbed7690618d6d329e10671cff..81375fe19b7e00111cdcdca93256e9df21fa3bab 100644 (file)
@@ -266,7 +266,7 @@ int main(int argc, char **argv)
        static struct cal_control ctl = {
                .weekstart = SUNDAY,
                .num_months = NUM_MONTHS,
-               .colormode = UL_COLORMODE_AUTO,
+               .colormode = UL_COLORMODE_UNDEF,
                .weektype = WEEK_NUM_DISABLED,
                .day_width = DAY_LEN,
                .gutter_width = 2,
@@ -469,7 +469,7 @@ int main(int argc, char **argv)
 
        headers_init(&ctl);
 
-       if (!colors_init(ctl.colormode)) {
+       if (!colors_init(ctl.colormode, "cal")) {
                ctl.req.day = 0;
                ctl.weektype &= ~WEEK_NUM_MASK;
        }
index 3ed0b1db7576d33a86139954f6696f7fb7a1c998..c1f84a5688760149e34522d843fb5f910a2952aa 100644 (file)
@@ -1187,7 +1187,7 @@ int main(int argc, char *argv[])
                .kmsg = -1,
                .time_fmt = DMESG_TIMEFTM_TIME,
        };
-       int colormode = UL_COLORMODE_NEVER;
+       int colormode = UL_COLORMODE_UNDEF;
        enum {
                OPT_TIME_FORMAT = CHAR_MAX + 1,
        };
@@ -1377,7 +1377,7 @@ int main(int argc, char *argv[])
                }
 
 
-       ctl.color = colors_init(colormode) ? 1 : 0;
+       ctl.color = colors_init(colormode, "dmesg") ? 1 : 0;
        if (ctl.follow)
                nopager = 1;
        ctl.pager = nopager ? 0 : ctl.pager;
index 19e67f46d46009c57d8b70234b8072f0c0eb17c1..ec9fecbec21c866fab75858da9085aea7d8821a3 100644 (file)
@@ -60,7 +60,7 @@ int
 parse_args(int argc, char **argv, struct hexdump *hex)
 {
        int ch;
-       int colormode = UL_COLORMODE_NEVER;
+       int colormode = UL_COLORMODE_UNDEF;
        char *hex_offt = "\"%07.7_Ax\n\"";
 
 
@@ -145,7 +145,7 @@ parse_args(int argc, char **argv, struct hexdump *hex)
                add_fmt(hex_offt, hex);
                add_fmt("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"", hex);
        }
-       colors_init (colormode);
+       colors_init (colormode, "hexdump");
        return optind;
 }