From f657d87b997f5b599d74a8f2eb6fef8dd6381550 Mon Sep 17 00:00:00 2001
From: Roy Marples <roy@marples.name>
Date: Tue, 17 Apr 2007 09:32:18 +0000
Subject: Rationalise our colour usage a little.

---
 src/Makefile    |  2 +-
 src/einfo.h     | 30 +++++-------------
 src/libeinfo.c  | 95 ++++++++++++++++++++++++++++++++-------------------------
 src/rc-status.c | 18 +++++------
 src/rc.c        | 48 ++++++++++++++++-------------
 5 files changed, 96 insertions(+), 97 deletions(-)

(limited to 'src')

diff --git a/src/Makefile b/src/Makefile
index a2691bca..e255f193 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -38,7 +38,7 @@ SYS_WHITELIST = env_whitelist
 TARGET = $(LIB_TARGETS) $(BIN_TARGETS) $(SBIN_TARGETS) $(PRIV_BIN_TARGETS)
 
 RCLINKS = einfon einfo ewarnn ewarn eerrorn eerror ebegin eend ewend \
-	      eindent eoutdent eflush color_terminal \
+	      eindent eoutdent eflush eval_ecolors \
 	      veinfo vewarn vebegin veend vewend veindent veoutdent \
 	      service_starting service_inactive service_started \
 	      service_stopping service_stopped \
diff --git a/src/einfo.h b/src/einfo.h
index 29f831a2..a99af240 100644
--- a/src/einfo.h
+++ b/src/einfo.h
@@ -18,30 +18,14 @@
 
 typedef enum
 {
-	einfo_good,
-	einfo_warn,
-	einfo_bad,
-	einfo_hilite,
-	einfo_bracket,
-	einfo_normal
+	ecolor_good,
+	ecolor_warn,
+	ecolor_bad,
+	ecolor_hilite,
+	ecolor_bracket,
+	ecolor_normal
 } einfo_color_t;
 
-/* Colour codes used by the below functions. */
-#define EINFO_GOOD	"\033[32;01m"
-#define EINFO_WARN	"\033[33;01m"
-#define EINFO_BAD	"\033[31;01m"
-#define EINFO_HILITE	"\033[36;01m"
-#define EINFO_BRACKET	"\033[34;01m"
-#define EINFO_NORMAL	"\033[0m"
-
-/* Handy macros to easily use the above in a custom manner */
-#define PEINFO_GOOD	if (colour_terminal ()) printf (EINFO_GOOD)
-#define PEINFO_WARN	if (colour_terminal ()) printf (EINFO_WARN)
-#define PEINFO_BAD	if (colour_terminal ()) printf (EINFO_BAD)
-#define PEINFO_HILITE	if (colour_terminal ()) printf (EINFO_HILITE)
-#define PEINFO_BRACKET	if (colour_terminal ()) printf (EINFO_BRACKET)
-#define PEINFO_NORMAL	if (colour_terminal ()) printf (EINFO_NORMAL)
-
 /* We work out if the terminal supports colour or not through the use
    of the TERM env var. We cache the reslt in a static bool, so
    subsequent calls are very fast.
@@ -51,7 +35,7 @@ typedef enum
    in the future, but veinfo is used by shell scripts as they don't
    have the va_list concept
    */
-bool colour_terminal (void);
+const char *ecolor (einfo_color_t);
 int einfon (const char *fmt, ...) EINFO_PRINTF (1, 2);
 int ewarnn (const char *fmt, ...) EINFO_PRINTF (1, 2);
 int eerrorn (const char *fmt, ...) EINFO_PRINTF (1, 2);
diff --git a/src/libeinfo.c b/src/libeinfo.c
index a4f5825c..8c7671da 100644
--- a/src/libeinfo.c
+++ b/src/libeinfo.c
@@ -22,7 +22,7 @@
 #include "rc-misc.h"
 
 #include "hidden-visibility.h"
-hidden_proto(colour_terminal)
+hidden_proto(ecolor)
 hidden_proto(ebegin)
 hidden_proto(ebeginv)
 hidden_proto(ebracket)
@@ -48,26 +48,35 @@ hidden_proto(ewarnx)
 hidden_proto(ewend)
 hidden_proto(ewendv)
 
-/* Incase we cannot work out how many columns from ioctl, supply a default */
+    /* Incase we cannot work out how many columns from ioctl, supply a default */
 #define DEFAULT_COLS		 80
 
-#define OK			"ok"
-#define NOT_OK			"!!"
+#define OK					"ok"
+#define NOT_OK				"!!"
 
 #define CHECK_VERBOSE		if (! is_env ("RC_VERBOSE", "yes")) return 0
 
-/* Number of spaces for an indent */
+    /* Number of spaces for an indent */
 #define INDENT_WIDTH		2
 
-/* How wide can the indent go? */
-#define INDENT_MAX		40
+    /* How wide can the indent go? */
+#define INDENT_MAX			40
 
 #define EBUFFER_LOCK		RC_SVCDIR "ebuffer/.lock"
 
+/* Default colours */
+#define ECOLOR_GOOD      "\033[32;01m"
+#define ECOLOR_WARN      "\033[33;01m"
+#define ECOLOR_BAD       "\033[31;01m"
+#define ECOLOR_HILITE    "\033[36;01m"
+#define ECOLOR_BRACKET   "\033[34;01m"
+#define ECOLOR_NORMAL    "\033[0m"
+#define ECOLOR_FLUSH     "\033[K"
+
 /* A cheat sheet of colour capable terminals
    This is taken from DIR_COLORS from GNU coreutils
    We embed it here as we shouldn't depend on coreutils */
-static const char *colour_terms[] = {
+static const char *color_terms[] = {
 	"Eterm",
 	"ansi",
 	"color-xterm",
@@ -123,7 +132,7 @@ static bool is_env (const char *var, const char *val)
 	return (strcasecmp (v, val) ? false : true);
 }
 
-bool colour_terminal (void)
+static bool colour_terminal (void)
 {
 	static int in_colour = -1;
 	int i = 0;
@@ -139,8 +148,8 @@ bool colour_terminal (void)
 	if (! term)
 		return (true);
 
-	while (colour_terms[i]) {
-		if (strcmp (colour_terms[i], term) == 0) {
+	while (color_terms[i]) {
+		if (strcmp (color_terms[i], term) == 0) {
 			in_colour = 1;
 			return (true);
 		}
@@ -150,7 +159,6 @@ bool colour_terminal (void)
 	in_colour = 0;
 	return (false);
 }
-hidden_def(colour_terminal)
 
 static int get_term_columns (void)
 {
@@ -397,43 +405,44 @@ static int _eindent (FILE *stream)
 	return (fprintf (stream, "%s", indent));
 }
 
-static const char *ecolor (einfo_color_t color) {
+const char *ecolor (einfo_color_t color) {
 	const char *col = NULL;
+
+	if (! colour_terminal ())
+		return ("");
 	
 	switch (color) {
-		case einfo_good:
-			if (! (col = getenv ("EINFO_GOOD")))
-				col = EINFO_GOOD;
+		case ecolor_good:
+			if (! (col = getenv ("ECOLOR_GOOD")))
+				col = ECOLOR_GOOD;
 			break;
-		case einfo_warn:
-			if (! (col = getenv ("EINFO_WARN")))
-				col = EINFO_WARN;
+		case ecolor_warn:
+			if (! (col = getenv ("ECOLOR_WARN")))
+				col = ECOLOR_WARN;
 			break;
-		case einfo_bad:
-			if (! (col = getenv ("EINFO_BAD")))
-				col = EINFO_BAD;
+		case ecolor_bad:
+			if (! (col = getenv ("ECOLOR_BAD")))
+				col = ECOLOR_BAD;
 			break;
-		case einfo_hilite:
-			if (! (col = getenv ("EINFO_HILITE")))
-				col = EINFO_HILITE;
+		case ecolor_hilite:
+			if (! (col = getenv ("ECOLOR_HILITE")))
+				col = ECOLOR_HILITE;
 			break;
-		case einfo_bracket:
-			if (! (col = getenv ("EINFO_BRACKET")))
-				col = EINFO_BRACKET;
+		case ecolor_bracket:
+			if (! (col = getenv ("ECOLOR_BRACKET")))
+				col = ECOLOR_BRACKET;
 			break;
-		case einfo_normal:
-			col = EINFO_NORMAL;
+		case ecolor_normal:
+			col = ECOLOR_NORMAL;
 			break;
 	}
 
 	return (col);
 }
+hidden_def(ecolor)
 
 #define EINFOVN(_file, _color) \
-	if (colour_terminal ()) \
-fprintf (_file, " %s*%s ", ecolor (_color), ecolor (einfo_normal)); \
-else \
-fprintf (_file, " * "); \
+fprintf (_file, " %s*%s ", ecolor (_color), ecolor (ecolor_normal)); \
 retval += _eindent (_file); \
 { \
 	va_list _ap; \
@@ -442,13 +451,13 @@ retval += _eindent (_file); \
 	va_end (_ap); \
 } \
 if (colour_terminal ()) \
-fprintf (_file, "\033[K");
+fprintf (_file, ECOLOR_FLUSH);
 
 static int _einfovn (const char *fmt, va_list ap)
 {
 	int retval = 0;
 
-	EINFOVN (stdout, einfo_good);
+	EINFOVN (stdout, ecolor_good);
 	return (retval);
 }
 
@@ -456,7 +465,7 @@ static int _ewarnvn (const char *fmt, va_list ap)
 {
 	int retval = 0;
 
-	EINFOVN (stdout, einfo_warn);
+	EINFOVN (stdout, ecolor_warn);
 	return (retval);
 }
 
@@ -464,7 +473,7 @@ static int _eerrorvn (const char *fmt, va_list ap)
 {
 	int retval = 0;
 
-	EINFOVN (stderr, einfo_bad);
+	EINFOVN (stderr, ecolor_bad);
 	return (retval);
 }
 
@@ -637,15 +646,15 @@ static void _eend (int col, einfo_color_t color, const char *msg)
 	if (! msg)
 		return;
 
-	if (color == einfo_bad)
+	if (color == ecolor_bad)
 		fp = stderr;
 
 	cols = get_term_columns () - (strlen (msg) + 6);
 
 	if (cols > 0 && colour_terminal ()) {
 		fprintf (fp, "\033[A\033[%dC %s[ %s%s %s]%s\n", cols,
-				 ecolor (einfo_bracket), ecolor (color), msg,
-				 ecolor (einfo_bracket), ecolor (einfo_normal));
+				 ecolor (ecolor_bracket), ecolor (color), msg,
+				 ecolor (ecolor_bracket), ecolor (ecolor_normal));
 	} else {
 		for (i = -1; i < cols - col; i++)
 			fprintf (fp, " ");
@@ -682,7 +691,9 @@ static int _do_eend (const char *cmd, int retval, const char *fmt, va_list ap)
 			fprintf (fp, "\n");
 	}
 
-	_eend (col, retval == 0 ? einfo_good : einfo_bad, retval == 0 ? OK : NOT_OK);
+	_eend (col,
+		   retval == 0 ? ecolor_good : ecolor_bad,
+		   retval == 0 ? OK : NOT_OK);
 	return (retval);
 }
 
diff --git a/src/rc-status.c b/src/rc-status.c
index 76dedce9..2cf4ed0c 100644
--- a/src/rc-status.c
+++ b/src/rc-status.c
@@ -18,34 +18,34 @@
 
 static void print_level (char *level)
 {
-	printf ("Runlevel: ");
-	PEINFO_HILITE;
-	printf ("%s\n", level);
-	PEINFO_NORMAL;
+	printf ("Runlevel: %s%s%s\n",
+			ecolor (ecolor_hilite),
+			level,
+			ecolor (ecolor_normal));
 }
 
 static void print_service (char *service)
 {
 	char status[10];
 	int cols =  printf (" %s\n", service);
-	einfo_color_t color = einfo_bad;
+	einfo_color_t color = ecolor_bad;
 
 	if (rc_service_state (service, rc_service_stopping))
 		snprintf (status, sizeof (status),   "stopping ");
 	else if (rc_service_state (service, rc_service_starting)) {
 		snprintf (status, sizeof (status), "starting ");
-		color = einfo_warn;
+		color = ecolor_warn;
 	} else if (rc_service_state (service, rc_service_inactive)) {
 		snprintf (status, sizeof (status), "inactive ");
-		color = einfo_warn;
+		color = ecolor_warn;
 	} else if (geteuid () == 0 && rc_service_state (service, rc_service_crashed))
 		snprintf (status, sizeof (status),   " crashed ");
 	else if (rc_service_state (service, rc_service_started)) {
 		snprintf (status, sizeof (status), " started ");
-		color = einfo_good;
+		color = ecolor_good;
 	} else if (rc_service_state (service, rc_service_scheduled)) {
 		snprintf (status, sizeof (status), "scheduled");
-		color = einfo_warn;
+		color = ecolor_warn;
 	} else
 		snprintf (status, sizeof (status),   " stopped ");
 	ebracket (cols, color, status);
diff --git a/src/rc.c b/src/rc.c
index b5465d67..4a583ad8 100644
--- a/src/rc.c
+++ b/src/rc.c
@@ -33,17 +33,17 @@
 #include "rc-plugin.h"
 #include "strlist.h"
 
-#define INITSH 			RC_LIBDIR "sh/init.sh"
-#define HALTSH			RC_INITDIR "halt.sh"
+#define INITSH 					RC_LIBDIR "sh/init.sh"
+#define HALTSH					RC_INITDIR "halt.sh"
 
-#define RC_SVCDIR_STARTING	RC_SVCDIR "starting/"
-#define RC_SVCDIR_INACTIVE	RC_SVCDIR "inactive/"
-#define RC_SVCDIR_STARTED	RC_SVCDIR "started/"
+#define RC_SVCDIR_STARTING		RC_SVCDIR "starting/"
+#define RC_SVCDIR_INACTIVE		RC_SVCDIR "inactive/"
+#define RC_SVCDIR_STARTED		RC_SVCDIR "started/"
 #define RC_SVCDIR_COLDPLUGGED	RC_SVCDIR "coldplugged/"
 
-#define INTERACTIVE		RC_SVCDIR "interactive"
+#define INTERACTIVE				RC_SVCDIR "interactive"
 
-#define DEVBOOT			"/dev/.rcboot"
+#define DEVBOOT					"/dev/.rcboot"
 
 /* Cleanup anything in main */
 #define CHAR_FREE(_item) if (_item) { \
@@ -114,6 +114,17 @@ static int do_e (int argc, char **argv)
 	char *p;
 	char *fmt = NULL;
 
+	if (strcmp (applet, "eval_ecolors") == 0) {
+		printf ("GOOD='%s'\nWARN='%s'\nBAD='%s'\nHILITE='%s'\nBRACKET='%s'\nNORMAL='%s'\n",
+				ecolor (ecolor_good),
+				ecolor (ecolor_warn),
+				ecolor (ecolor_bad),
+				ecolor (ecolor_hilite),
+				ecolor (ecolor_bracket),
+				ecolor (ecolor_normal));
+		exit (EXIT_SUCCESS);
+	}
+
 	if (strcmp (applet, "eend") == 0 ||
 		strcmp (applet, "ewend") == 0 ||
 		strcmp (applet, "veend") == 0 ||
@@ -497,8 +508,6 @@ int main (int argc, char **argv)
 		exit (rc_runlevel_starting () ? 0 : 1);
 	else if (strcmp (applet, "is_runlevel_stop") == 0)
 		exit (rc_runlevel_stopping () ? 0 : 1);
-	else if (strcmp (applet, "color_terminal") == 0)
-		exit (colour_terminal () ? 0 : 1);
 
 	if (strcmp (applet, "rc" ) != 0)
 		eerrorx ("%s: unknown applet", applet);
@@ -578,19 +587,14 @@ int main (int argc, char **argv)
 				uname (&uts);
 
 				printf ("\n");
-				PEINFO_GOOD;
-				printf ("   Gentoo/%s; ", uts.sysname);
-				PEINFO_BRACKET;
-				printf ("http://www.gentoo.org/");
-				PEINFO_NORMAL;
-				printf ("\n   Copyright 1999-2007 Gentoo Foundation; "
-						"Distributed under the GPLv2\n\n");
-
-				printf ("Press ");
-				PEINFO_GOOD;
-				printf ("I");
-				PEINFO_NORMAL;
-				printf (" to enter interactive boot mode\n\n");
+				printf ("   %sGentoo/%s; %shttp://www.gentoo.org/%s"
+						"\n   Copyright 1999-2007 Gentoo Foundation; "
+						"Distributed under the GPLv2\n\n",
+						ecolor (ecolor_good), uts.sysname, ecolor (ecolor_bracket),
+						ecolor (ecolor_normal));
+
+				printf ("Press %sI%s to enter interactive boot mode\n\n",
+						ecolor (ecolor_good), ecolor (ecolor_normal));
 
 				setenv ("RC_SOFTLEVEL", newlevel, 1);
 				rc_plugin_run (rc_hook_runlevel_start_in, newlevel);
-- 
cgit v1.2.3