From 7e879bd678fa1ac8b1b0bd4e7b9c845b0e967da2 Mon Sep 17 00:00:00 2001 From: Sascha Hauer Date: Tue, 20 Feb 2018 08:20:20 +0100 Subject: console: move global.allow_color handling to C code We have global.allow_color, but this is limited to the environment only. Move creation and handling of this variable to C code so that we can add support for colored output to commands/console controlled by the same variable. Signed-off-by: Sascha Hauer --- common/Kconfig | 8 ++++++++ common/console_common.c | 17 +++++++++++++++-- defaultenv/defaultenv-2-base/bin/init | 3 --- defaultenv/defaultenv-2-base/data/ansi-colors | 2 +- defaultenv/defaultenv-2-base/init/ps1 | 2 +- include/console.h | 2 ++ 6 files changed, 27 insertions(+), 7 deletions(-) diff --git a/common/Kconfig b/common/Kconfig index a21f3e51b2..25de2485cd 100644 --- a/common/Kconfig +++ b/common/Kconfig @@ -751,6 +751,14 @@ config CONSOLE_ACTIVATE_NONE endchoice +config CONSOLE_ALLOW_COLOR + prompt "Allow colored console output during boot" + bool + help + If enabled, colored output is allowed during boot. This is the + compile time default for colored console output. After boot it + can be controlled using global.allow_color. + config PBL_CONSOLE depends on PBL_IMAGE depends on !CONSOLE_NONE diff --git a/common/console_common.c b/common/console_common.c index d051458de4..0202345a62 100644 --- a/common/console_common.c +++ b/common/console_common.c @@ -145,15 +145,28 @@ int dev_printf(int level, const struct device_d *dev, const char *format, ...) return ret; } -static int loglevel_init(void) +#ifdef CONFIG_CONSOLE_ALLOW_COLOR +static unsigned int __console_allow_color = 1; +#else +static unsigned int __console_allow_color = 0; +#endif + +bool console_allow_color(void) +{ + return __console_allow_color; +} + +static int console_common_init(void) { if (IS_ENABLED(CONFIG_LOGBUF)) globalvar_add_simple_int("log_max_messages", &barebox_log_max_messages, "%d"); + globalvar_add_simple_bool("allow_color", &__console_allow_color); + return globalvar_add_simple_int("loglevel", &barebox_loglevel, "%d"); } -device_initcall(loglevel_init); +device_initcall(console_common_init); void log_print(unsigned flags) { diff --git a/defaultenv/defaultenv-2-base/bin/init b/defaultenv/defaultenv-2-base/bin/init index 7af3c7d95c..6f3a34dbac 100644 --- a/defaultenv/defaultenv-2-base/bin/init +++ b/defaultenv/defaultenv-2-base/bin/init @@ -6,7 +6,6 @@ global hostname global user global autoboot_timeout global boot.default -global allow_color global linux.bootargs.base global linux.bootargs.console #linux.bootargs.dyn.* will be cleared at the beginning of boot @@ -20,8 +19,6 @@ magicvar -a global.user "username (used in network filenames)" [ -z "${global.autoboot_timeout}" ] && global.autoboot_timeout=3 magicvar -a global.autoboot_timeout "timeout in seconds before automatic booting" [ -z "${global.boot.default}" ] && global.boot.default=net -[ -z "${global.allow_color}" ] && global.allow_color=true -magicvar -a global.allow_color "Allow color on the console (boolean)" [ -z "${global.editcmd}" ] && global.editcmd=sedit [ -e /env/config-board ] && /env/config-board diff --git a/defaultenv/defaultenv-2-base/data/ansi-colors b/defaultenv/defaultenv-2-base/data/ansi-colors index 636532979a..c61cae24e8 100644 --- a/defaultenv/defaultenv-2-base/data/ansi-colors +++ b/defaultenv/defaultenv-2-base/data/ansi-colors @@ -1,6 +1,6 @@ #!/bin/sh -if [ ${global.allow_color} != "true" ]; then +if [ ${global.allow_color} != "1" ]; then exit fi diff --git a/defaultenv/defaultenv-2-base/init/ps1 b/defaultenv/defaultenv-2-base/init/ps1 index 02d7b4b780..bbb544338c 100644 --- a/defaultenv/defaultenv-2-base/init/ps1 +++ b/defaultenv/defaultenv-2-base/init/ps1 @@ -1,6 +1,6 @@ #!/bin/sh -if [ ${global.allow_color} = "true" ]; then +if [ ${global.allow_color} = "1" ]; then export PS1="\e[1;32mbarebox@\e[1;36m\h:\w\e[0m " else export PS1="barebox@\h:\w " diff --git a/include/console.h b/include/console.h index 724168e07c..a8b2663a4c 100644 --- a/include/console.h +++ b/include/console.h @@ -94,4 +94,6 @@ void pbl_set_putc(void (*putcf)(void *ctx, int c), void *ctx); static inline void pbl_set_putc(void (*putcf)(void *ctx, int c), void *ctx) {} #endif +bool console_allow_color(void); + #endif -- cgit v1.2.3