OVS_COLORS environment variable is parsed to extract user-defined preferences regarding colors (this is used to set up a color theme, not to replace the `--color` option for activating color output).
The string should be of a format similar to LS_COLORS or GREP_COLORS, with available colors being as follows: * ac: action field * dr: drop keyword * le: learn keyword * pm: parameters receiving attributes * pr: keyword having parenthesis * sp: some special keywords * vl: lone values with no parameter name For color whose idendifier does not appear in the string, the default hardcoded value is used instead. As an example, setting OVS_COLORS to the following string is equivalent to using the default values: OVS_COLORS="ac:01;31:dr=34:le=31:pm=36:pr=35:sp=33:vl=32" Signed-off-by: Quentin Monnet <quentin.mon...@6wind.com> --- lib/automake.mk | 2 + lib/colors.c | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/colors.h | 36 ++++++++++++++ utilities/ovs-ofctl.c | 4 ++ 4 files changed, 174 insertions(+) create mode 100644 lib/colors.c create mode 100644 lib/colors.h diff --git a/lib/automake.mk b/lib/automake.mk index 27a16691ebda..1eeb5e48b19a 100644 --- a/lib/automake.mk +++ b/lib/automake.mk @@ -40,6 +40,8 @@ lib_libopenvswitch_la_SOURCES = \ lib/classifier-private.h \ lib/cmap.c \ lib/cmap.h \ + lib/colors.c \ + lib/colors.h \ lib/command-line.c \ lib/command-line.h \ lib/compiler.h \ diff --git a/lib/colors.c b/lib/colors.c new file mode 100644 index 000000000000..28548d5836bc --- /dev/null +++ b/lib/colors.c @@ -0,0 +1,132 @@ +/* + * Copyright (c) 2016 6WIND S.A. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* Handle color setup for output. + * See dynamic-strings.{c,h} for the functions that are responsible for + * printing the color markers. + */ + +#include <config.h> + +#include "colors.h" + +#include <stdlib.h> +#include <string.h> + +#include "util.h" + +/* Returns a pointer to the variable containing a given color */ +static const char **get_color(const char * name); + +/* Extract user-defined colors from OVS_COLORS environment variable */ +static void colors_parse_from_env(void); + +struct color_struct { + const char *name; + const char **var_ptr; +}; + +/* Color IDs to use in OVS_COLORS environment variable to use custem colors */ +static const struct color_struct color_identification[] = +{ + { "ac", &actions_color}, + { "dr", &drop_color}, + { "le", &learn_color}, + { "pm", ¶m_color}, + { "pr", &paren_color}, + { "sp", &special_color}, + { "vl", &value_color}, + { NULL, NULL} +}; + +static const char ** +get_color(const char *name) +{ + const struct color_struct *color; + for (color = color_identification; color->name; color++) { + if (!strcmp(color->name, name)) { + return color->var_ptr; + } + } + return NULL; +} + +void +colors_init(void) +{ + /* Color codes; variables are declared in header file */ + actions_color = "01;31"; /* bold red */ + drop_color = "34"; /* blue */ + learn_color = "31"; /* red */ + param_color = "36"; /* cyan */ + paren_color = "35"; /* magenta */ + special_color = "33"; /* yellow */ + value_color = "32"; /* green */ + + /* Overwrite with user-defined color markers */ + colors_parse_from_env(); +} + +/* Colorized output: get user-defined colors from OVS_COLORS environment + * variable. This must be a string of the form: + * ac=01;31:r=34:le=:pm=02;32:pr=01;30 + * (see color_identification[] above for all color names) + * If a color is missing from this string, default value is used instead. + * If a color name is assigned an empty or incorrect value (i.e. something + * containing characters other than decimals and ';'), fields using this color + * will not be highlighted. + * Unknown color names are ignored so as to ensure forward compatibility. + */ +static void +colors_parse_from_env(void) +{ + const char *color_str; + char *s; + char *token; + + color_str = getenv("OVS_COLORS"); + if (color_str == NULL || *color_str == '\0') + { + return; + } + s = strdup(color_str); + + /* Loop on tokens: they are separated by columns ':' */ + for (token = strsep(&s, ":"); + token != NULL; + token = strsep(&s, ":")) { + char *ptr; + char *name = strsep(&token, "="); + for (ptr = token; ptr != NULL && *ptr != '\0'; ptr++) { + /* We accept only decimals and ';' for color marker */ + if (*ptr == ';' || (*ptr >= '0' && *ptr <= '9')) { + continue; + } + name = NULL; + break; + } + if (name != NULL) { + /* We found a name and marker contains only decimals and ';' + * Try to get a pointer to associated color variable + */ + const char **color_var_ptr = get_color(name); + /* If we know that color, update its value */ + if (color_var_ptr != NULL) { + *color_var_ptr = token; + } + } + } +} diff --git a/lib/colors.h b/lib/colors.h new file mode 100644 index 000000000000..4489690c4235 --- /dev/null +++ b/lib/colors.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2016 6WIND S.A. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef COLORS_H +#define COLORS_H 1 + +/* Variables containing color codes. */ +const char *actions_color; +const char *drop_color; +const char *learn_color; +const char *param_color; +const char *paren_color; +const char *special_color; +const char *value_color; + +/* Select Graphic Rendition (SGR, "\33[...m") strings. + * Also Erase in Line (EL) to Right ("\33[K"). */ +static const char *sgr_start = "\33[%sm\33[K"; +static const char *sgr_end = "\33[m\33[K"; + +void colors_init(void); + +#endif /* colors.h */ diff --git a/utilities/ovs-ofctl.c b/utilities/ovs-ofctl.c index a7dea78bcd3a..791c51f7e231 100644 --- a/utilities/ovs-ofctl.c +++ b/utilities/ovs-ofctl.c @@ -33,6 +33,7 @@ #include "classifier.h" #include "command-line.h" #include "daemon.h" +#include "colors.h" #include "compiler.h" #include "dirs.h" #include "dynamic-string.h" @@ -368,6 +369,9 @@ parse_options(int argc, char *argv[]) if (color_option == 2) { color_option = isatty(STDOUT_FILENO) && t && strcmp(t, "dumb") != 0; } + if (color_option != 0) { + colors_init(); + } } static void -- 1.9.1 _______________________________________________ dev mailing list dev@openvswitch.org http://openvswitch.org/mailman/listinfo/dev