sbtools: rework the color hack and add switch to disable color output

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@30880 a1c6a512-1295-4272-9138-f99709370657
diff --git a/utils/sbtools/misc.c b/utils/sbtools/misc.c
index 3993495..4eeda4e 100644
--- a/utils/sbtools/misc.c
+++ b/utils/sbtools/misc.c
@@ -211,3 +211,24 @@
     if(newline)
         printf("\n");
 }
+
+char OFF[] = { 0x1b, 0x5b, 0x31, 0x3b, '0', '0', 0x6d, '\0' };
+
+char GREY[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '0', 0x6d, '\0' };
+char RED[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '1', 0x6d, '\0' };
+char GREEN[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '2', 0x6d, '\0' };
+char YELLOW[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '3', 0x6d, '\0' };
+char BLUE[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '4', 0x6d, '\0' };
+
+static bool g_color_enable = true;
+
+void enable_color(bool enable)
+{
+    g_color_enable = enable;
+}
+
+void color(color_t c)
+{
+    if(g_color_enable)
+        printf("%s", (char *)c);
+}
diff --git a/utils/sbtools/misc.h b/utils/sbtools/misc.h
index cc0a3fb..a685816 100644
--- a/utils/sbtools/misc.h
+++ b/utils/sbtools/misc.h
@@ -48,4 +48,10 @@
 void add_keys_from_file(const char *key_file);
 void print_key(struct crypto_key_t *key, bool newline);
 
+typedef char color_t[];
+
+extern color_t OFF, GREY, RED, GREEN, YELLOW, BLUE;
+void color(color_t c);
+void enable_color(bool enable);
+
 #endif /* __MISC_H__ */
diff --git a/utils/sbtools/sbtoelf.c b/utils/sbtools/sbtoelf.c
index 47aebe7..24417dc 100644
--- a/utils/sbtools/sbtoelf.c
+++ b/utils/sbtools/sbtoelf.c
@@ -42,22 +42,6 @@
 #include "sb.h"
 #include "misc.h"
 
-#if 1 /* ANSI colors */
-
-#	define color(a) printf("%s",a)
-char OFF[] 		= { 0x1b, 0x5b, 0x31, 0x3b, '0', '0', 0x6d, '\0' };
-
-char GREY[] 	= { 0x1b, 0x5b, 0x31, 0x3b, '3', '0', 0x6d, '\0' };
-char RED[] 		= { 0x1b, 0x5b, 0x31, 0x3b, '3', '1', 0x6d, '\0' };
-char GREEN[] 	= { 0x1b, 0x5b, 0x31, 0x3b, '3', '2', 0x6d, '\0' };
-char YELLOW[] 	= { 0x1b, 0x5b, 0x31, 0x3b, '3', '3', 0x6d, '\0' };
-char BLUE[] 	= { 0x1b, 0x5b, 0x31, 0x3b, '3', '4', 0x6d, '\0' };
-
-#else
-	/* disable colors */
-#	define color(a)
-#endif
-
 /* all blocks are sized as a multiple of 0x1ff */
 #define PAD_TO_BOUNDARY(x) (((x) + 0x1ff) & ~0x1ff)
 
@@ -723,7 +707,8 @@
     printf("  -k <file>\tAdd key file\n");
     printf("  -z\t\tAdd zero key\n");
     printf("  -r\t\tUse raw command mode\n");
-    printf("  --add-key <key>\tAdd single key (hex or usbotp)\n");
+    printf("  -a/--add-key <key>\tAdd single key (hex or usbotp)\n");
+    printf("  -n/--no-color\tDisable output colors\n");
     exit(1);
 }
 
@@ -742,16 +727,20 @@
             {"help", no_argument, 0, '?'},
             {"debug", no_argument, 0, 'd'},
             {"add-key", required_argument, 0, 'a'},
+            {"no-color", no_argument, 0, 'n'},
             {0, 0, 0, 0}
         };
 
-        int c = getopt_long(argc, argv, "?do:k:zra:", long_options, NULL);
+        int c = getopt_long(argc, argv, "?do:k:zra:n", long_options, NULL);
         if(c == -1)
             break;
         switch(c)
         {
             case -1:
                 break;
+            case 'n':
+                enable_color(false);
+                break;
             case 'd':
                 g_debug = true;
                 break;