]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
env: Add regex support to env_attrs
authorJoe Hershberger <joe.hershberger@ni.com>
Wed, 20 May 2015 19:27:20 +0000 (14:27 -0500)
committerTom Rini <trini@konsulko.com>
Thu, 21 May 2015 13:13:19 +0000 (09:13 -0400)
Allow the features that use env_attrs to specify regexs for the name

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
README
common/env_attr.c
include/env_callback.h

diff --git a/README b/README
index a96dd428cbbe5644514469d2fa9dd76812ff4ec6..119bbc994d22db4dc544f16544be622d55d5d2eb 100644 (file)
--- a/README
+++ b/README
@@ -4142,6 +4142,10 @@ Configuration Settings:
                list, simply add an entry for the same variable name to the
                ".flags" variable.
 
+       If CONFIG_REGEX is defined, the variable_name above is evaluated as a
+       regular expression. This allows multiple variables to define the same
+       flags without explicitly listing them for each variable.
+
 - CONFIG_ENV_ACCESS_IGNORE_FORCE
        If defined, don't allow the -f switch to env set override variable
        access flags.
@@ -5540,6 +5544,10 @@ override any association in the static list. You can define
 CONFIG_ENV_CALLBACK_LIST_DEFAULT to a list (string) to define the
 ".callbacks" environment variable in the default or embedded environment.
 
+If CONFIG_REGEX is defined, the variable_name above is evaluated as a
+regular expression. This allows multiple variables to be connected to
+the same callback without explicitly listing them all out.
+
 
 Command Line Parsing:
 =====================
index b9de16f73e75187b6b909eecdf7f4d9367ece057..5bfe5e3a89f02f83d74bfc957f99c97cce82d498 100644 (file)
@@ -11,6 +11,7 @@
 #include <linux/linux_string.h>
 #else
 #include <common.h>
+#include <slre.h>
 #endif
 
 #include <env_attr.h>
@@ -109,6 +110,89 @@ int env_attr_walk(const char *attr_list,
        return 0;
 }
 
+#if defined(CONFIG_REGEX)
+struct regex_callback_priv {
+       const char *searched_for;
+       char *regex;
+       char *attributes;
+};
+
+static int regex_callback(const char *name, const char *attributes, void *priv)
+{
+       int retval = 0;
+       struct regex_callback_priv *cbp = (struct regex_callback_priv *)priv;
+       struct slre slre;
+       char regex[strlen(name) + 3];
+
+       /* Require the whole string to be described by the regex */
+       sprintf(regex, "^%s$", name);
+       if (slre_compile(&slre, regex)) {
+               struct cap caps[slre.num_caps + 2];
+
+               if (slre_match(&slre, cbp->searched_for,
+                              strlen(cbp->searched_for), caps)) {
+                       free(cbp->regex);
+                       cbp->regex = malloc(strlen(regex) + 1);
+                       if (cbp->regex) {
+                               strcpy(cbp->regex, regex);
+                       } else {
+                               retval = -ENOMEM;
+                               goto done;
+                       }
+
+                       free(cbp->attributes);
+                       cbp->attributes = malloc(strlen(attributes) + 1);
+                       if (cbp->attributes) {
+                               strcpy(cbp->attributes, attributes);
+                       } else {
+                               retval = -ENOMEM;
+                               free(cbp->regex);
+                               cbp->regex = NULL;
+                               goto done;
+                       }
+               }
+       } else {
+               printf("Error compiling regex: %s\n", slre.err_str);
+               retval = EINVAL;
+       }
+done:
+       return retval;
+}
+
+/*
+ * Retrieve the attributes string associated with a single name in the list
+ * There is no protection on attributes being too small for the value
+ */
+int env_attr_lookup(const char *attr_list, const char *name, char *attributes)
+{
+       if (!attributes)
+               /* bad parameter */
+               return -EINVAL;
+       if (!attr_list)
+               /* list not found */
+               return -EINVAL;
+
+       struct regex_callback_priv priv;
+       int retval;
+
+       priv.searched_for = name;
+       priv.regex = NULL;
+       priv.attributes = NULL;
+       retval = env_attr_walk(attr_list, regex_callback, &priv);
+       if (retval)
+               return retval; /* error */
+
+       if (priv.regex) {
+               strcpy(attributes, priv.attributes);
+               free(priv.attributes);
+               free(priv.regex);
+               /* success */
+               return 0;
+       }
+       return -ENOENT; /* not found in list */
+}
+#else
+
 /*
  * Search for the last exactly matching name in an attribute list
  */
@@ -219,3 +303,4 @@ int env_attr_lookup(const char *attr_list, const char *name, char *attributes)
        /* not found in list */
        return -ENOENT;
 }
+#endif
index ab4e115fb0678b280dfe3ce04bdafa4ed4c7f302..3de1093ff17889c342f9085eda8ff2d60abd2734 100644 (file)
 #define SPLASHIMAGE_CALLBACK
 #endif
 
+#ifdef CONFIG_REGEX
+#define ENV_DOT_ESCAPE "\\"
+#else
+#define ENV_DOT_ESCAPE
+#endif
+
 /*
  * This list of callback bindings is static, but may be overridden by defining
  * a new association in the ".callbacks" environment variable.
  */
-#define ENV_CALLBACK_LIST_STATIC ENV_CALLBACK_VAR ":callbacks," \
-       ENV_FLAGS_VAR ":flags," \
+#define ENV_CALLBACK_LIST_STATIC ENV_DOT_ESCAPE ENV_CALLBACK_VAR ":callbacks," \
+       ENV_DOT_ESCAPE ENV_FLAGS_VAR ":flags," \
        "baudrate:baudrate," \
        "bootfile:bootfile," \
        "loadaddr:loadaddr," \