]> git.ipfire.org Git - people/ms/strongswan.git/commitdiff
Add a utility function to resolve TTY color escape codes dynamically
authorMartin Willi <martin@revosec.ch>
Mon, 4 Mar 2013 14:04:56 +0000 (15:04 +0100)
committerMartin Willi <martin@revosec.ch>
Mon, 4 Mar 2013 14:04:56 +0000 (15:04 +0100)
src/libstrongswan/utils/utils.c
src/libstrongswan/utils/utils.h

index 0850df9bc80c699cd0d20855fda7b54fc7fddbda..2f38d8a9333104f322b9cdc43eeb35a1da0f3d27 100644 (file)
@@ -194,6 +194,69 @@ bool mkdir_p(const char *path, mode_t mode)
        return TRUE;
 }
 
+ENUM(tty_color_names, TTY_RESET, TTY_BG_DEF,
+       "\e[0m",
+       "\e[1m",
+       "\e[4m",
+       "\e[5m",
+       "\e[30m",
+       "\e[31m",
+       "\e[32m",
+       "\e[33m",
+       "\e[34m",
+       "\e[35m",
+       "\e[36m",
+       "\e[37m",
+       "\e[39m",
+       "\e[40m",
+       "\e[41m",
+       "\e[42m",
+       "\e[43m",
+       "\e[44m",
+       "\e[45m",
+       "\e[46m",
+       "\e[47m",
+       "\e[49m",
+);
+
+/**
+ * Get the escape string for a given TTY color, empty string on non-tty FILE
+ */
+char* tty_escape_get(int fd, tty_escape_t escape)
+{
+       if (!isatty(fd))
+       {
+               return "";
+       }
+       switch (escape)
+       {
+               case TTY_RESET:
+               case TTY_BOLD:
+               case TTY_UNDERLINE:
+               case TTY_BLINKING:
+               case TTY_FG_BLACK:
+               case TTY_FG_RED:
+               case TTY_FG_GREEN:
+               case TTY_FG_YELLOW:
+               case TTY_FG_BLUE:
+               case TTY_FG_MAGENTA:
+               case TTY_FG_CYAN:
+               case TTY_FG_WHITE:
+               case TTY_FG_DEF:
+               case TTY_BG_BLACK:
+               case TTY_BG_RED:
+               case TTY_BG_GREEN:
+               case TTY_BG_YELLOW:
+               case TTY_BG_BLUE:
+               case TTY_BG_MAGENTA:
+               case TTY_BG_CYAN:
+               case TTY_BG_WHITE:
+               case TTY_BG_DEF:
+                       return enum_to_name(tty_color_names, escape);
+               /* warn if a excape code is missing */
+       }
+       return "";
+}
 
 /**
  * The size of the thread-specific error buffer
index 6c24a261fafe0fb67045dc63d0c8d4084f38bec8..c66c665e08cac6873563b71b15d89323d60b9497 100644 (file)
@@ -314,6 +314,46 @@ enum status_t {
  */
 extern enum_name_t *status_names;
 
+typedef enum tty_escape_t tty_escape_t;
+
+/**
+ * Excape codes for tty colors
+ */
+enum tty_escape_t {
+       /** text properties */
+       TTY_RESET,
+       TTY_BOLD,
+       TTY_UNDERLINE,
+       TTY_BLINKING,
+
+       /** foreground colors */
+       TTY_FG_BLACK,
+       TTY_FG_RED,
+       TTY_FG_GREEN,
+       TTY_FG_YELLOW,
+       TTY_FG_BLUE,
+       TTY_FG_MAGENTA,
+       TTY_FG_CYAN,
+       TTY_FG_WHITE,
+       TTY_FG_DEF,
+
+       /** background colors */
+       TTY_BG_BLACK,
+       TTY_BG_RED,
+       TTY_BG_GREEN,
+       TTY_BG_YELLOW,
+       TTY_BG_BLUE,
+       TTY_BG_MAGENTA,
+       TTY_BG_CYAN,
+       TTY_BG_WHITE,
+       TTY_BG_DEF,
+};
+
+/**
+ * Get the escape string for a given TTY color, empty string on non-tty fd
+ */
+char* tty_escape_get(int fd, tty_escape_t escape);
+
 /**
  * deprecated pluto style return value:
  * error message, NULL for success