tools/patchelf: update to 0.18.0
[openwrt/staging/dedeckeh.git] / scripts / config / symbol.c
index b79d81ac137004de99506f954e75dcb72f0e5053..d97b56ab0ece7a0af72eb8019f3127bb1c3c19b7 100644 (file)
@@ -1,78 +1,43 @@
+// SPDX-License-Identifier: GPL-2.0-only
 /*
  * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
- * Released under the terms of the GNU GPL v2.0.
  */
 
+#include <sys/types.h>
 #include <ctype.h>
 #include <stdlib.h>
 #include <string.h>
 #include <regex.h>
-#include <sys/utsname.h>
 
-#define LKC_DIRECT_LINK
 #include "lkc.h"
 
 struct symbol symbol_yes = {
        .name = "y",
        .curr = { "y", yes },
-       .flags = SYMBOL_YES|SYMBOL_VALID,
-}, symbol_mod = {
+       .flags = SYMBOL_CONST|SYMBOL_VALID,
+};
+
+struct symbol symbol_mod = {
        .name = "m",
        .curr = { "m", mod },
-       .flags = SYMBOL_MOD|SYMBOL_VALID,
-}, symbol_no = {
+       .flags = SYMBOL_CONST|SYMBOL_VALID,
+};
+
+struct symbol symbol_no = {
        .name = "n",
        .curr = { "n", no },
-       .flags = SYMBOL_NO|SYMBOL_VALID,
-}, symbol_empty = {
+       .flags = SYMBOL_CONST|SYMBOL_VALID,
+};
+
+static struct symbol symbol_empty = {
        .name = "",
        .curr = { "", no },
        .flags = SYMBOL_VALID,
 };
 
-int sym_change_count;
 struct symbol *modules_sym;
-tristate modules_val;
-
-void sym_add_default(struct symbol *sym, const char *def)
-{
-       struct property *prop = prop_alloc(P_DEFAULT, sym);
-
-       prop->expr = expr_alloc_symbol(sym_lookup(def, 1));
-}
-
-void sym_init(void)
-{
-       struct symbol *sym;
-       struct utsname uts;
-       char *p;
-       static bool inited = false;
-
-       if (inited)
-               return;
-       inited = true;
-
-       uname(&uts);
-
-       sym = sym_lookup("ARCH", 0);
-       sym->type = S_STRING;
-       sym->flags |= SYMBOL_AUTO;
-       p = getenv("ARCH");
-       if (p)
-               sym_add_default(sym, p);
-
-       sym = sym_lookup("OPENWRTVERSION", 0);
-       sym->type = S_STRING;
-       sym->flags |= SYMBOL_AUTO;
-       p = getenv("OPENWRTVERSION");
-       if (p)
-               sym_add_default(sym, p);
-
-       sym = sym_lookup("UNAME_RELEASE", 0);
-       sym->type = S_STRING;
-       sym->flags |= SYMBOL_AUTO;
-       sym_add_default(sym, uts.release);
-}
+static tristate modules_val;
+int recursive_is_error;
 
 enum symbol_type sym_get_type(struct symbol *sym)
 {
@@ -81,11 +46,8 @@ enum symbol_type sym_get_type(struct symbol *sym)
        if (type == S_TRISTATE) {
                if (sym_is_choice_value(sym) && sym->visible == yes)
                        type = S_BOOLEAN;
-/* tristate always enabled */
-#if 0
                else if (modules_val == no)
                        type = S_BOOLEAN;
-#endif
        }
        return type;
 }
@@ -94,7 +56,7 @@ const char *sym_type_name(enum symbol_type type)
 {
        switch (type) {
        case S_BOOLEAN:
-               return "boolean";
+               return "bool";
        case S_TRISTATE:
                return "tristate";
        case S_INT:
@@ -105,8 +67,6 @@ const char *sym_type_name(enum symbol_type type)
                return "string";
        case S_UNKNOWN:
                return "unknown";
-       case S_OTHER:
-               break;
        }
        return "???";
 }
@@ -120,7 +80,7 @@ struct property *sym_get_choice_prop(struct symbol *sym)
        return NULL;
 }
 
-struct property *sym_get_default_prop(struct symbol *sym)
+static struct property *sym_get_default_prop(struct symbol *sym)
 {
        struct property *prop;
 
@@ -144,7 +104,7 @@ struct property *sym_get_range_prop(struct symbol *sym)
        return NULL;
 }
 
-static int sym_get_range_val(struct symbol *sym, int base)
+static long long sym_get_range_val(struct symbol *sym, int base)
 {
        sym_calc_value(sym);
        switch (sym->type) {
@@ -157,13 +117,14 @@ static int sym_get_range_val(struct symbol *sym, int base)
        default:
                break;
        }
-       return strtol(sym->curr.val, NULL, base);
+       return strtoll(sym->curr.val, NULL, base);
 }
 
 static void sym_validate_range(struct symbol *sym)
 {
        struct property *prop;
-       int base, val, val2;
+       int base;
+       long long val, val2;
        char str[64];
 
        switch (sym->type) {
@@ -179,7 +140,7 @@ static void sym_validate_range(struct symbol *sym)
        prop = sym_get_range_prop(sym);
        if (!prop)
                return;
-       val = strtol(sym->curr.val, NULL, base);
+       val = strtoll(sym->curr.val, NULL, base);
        val2 = sym_get_range_val(prop->expr->left.sym, base);
        if (val >= val2) {
                val2 = sym_get_range_val(prop->expr->right.sym, base);
@@ -187,29 +148,58 @@ static void sym_validate_range(struct symbol *sym)
                        return;
        }
        if (sym->type == S_INT)
-               sprintf(str, "%d", val2);
+               sprintf(str, "%lld", val2);
        else
-               sprintf(str, "0x%x", val2);
-       sym->curr.val = strdup(str);
+               sprintf(str, "0x%llx", val2);
+       sym->curr.val = xstrdup(str);
+}
+
+static void sym_set_changed(struct symbol *sym)
+{
+       struct property *prop;
+
+       sym->flags |= SYMBOL_CHANGED;
+       for (prop = sym->prop; prop; prop = prop->next) {
+               if (prop->menu)
+                       prop->menu->flags |= MENU_CHANGED;
+       }
+}
+
+static void sym_set_all_changed(void)
+{
+       struct symbol *sym;
+       int i;
+
+       for_all_symbols(i, sym)
+               sym_set_changed(sym);
 }
 
 static void sym_calc_visibility(struct symbol *sym)
 {
        struct property *prop;
+       struct symbol *choice_sym = NULL;
        tristate tri;
 
        /* any prompt visible? */
        tri = no;
+
+       if (sym_is_choice_value(sym))
+               choice_sym = prop_get_symbol(sym_get_choice_prop(sym));
+
        for_all_prompts(sym, prop) {
                prop->visible.tri = expr_calc_value(prop->visible.expr);
-               tri = E_OR(tri, prop->visible.tri);
+               /*
+                * Tristate choice_values with visibility 'mod' are
+                * not visible if the corresponding choice's value is
+                * 'yes'.
+                */
+               if (choice_sym && sym->type == S_TRISTATE &&
+                   prop->visible.tri == mod && choice_sym->curr.tri == yes)
+                       prop->visible.tri = no;
+
+               tri = EXPR_OR(tri, prop->visible.tri);
        }
-/* tristate always enabled */
-#if 0
        if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
-#else
-       if (tri == mod && (sym->type != S_TRISTATE))
-#endif
                tri = yes;
        if (sym->visible != tri) {
                sym->visible = tri;
@@ -217,6 +207,16 @@ static void sym_calc_visibility(struct symbol *sym)
        }
        if (sym_is_choice_value(sym))
                return;
+       /* defaulting to "yes" if no explicit "depends on" are given */
+       tri = yes;
+       if (sym->dir_dep.expr)
+               tri = expr_calc_value(sym->dir_dep.expr);
+       if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
+               tri = yes;
+       if (sym->dir_dep.tri != tri) {
+               sym->dir_dep.tri = tri;
+               sym_set_changed(sym);
+       }
        tri = no;
        if (sym->rev_dep.expr)
                tri = expr_calc_value(sym->rev_dep.expr);
@@ -226,47 +226,81 @@ static void sym_calc_visibility(struct symbol *sym)
                sym->rev_dep.tri = tri;
                sym_set_changed(sym);
        }
+       tri = no;
+       if (sym->implied.expr)
+               tri = expr_calc_value(sym->implied.expr);
+       if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
+               tri = yes;
+       if (sym->implied.tri != tri) {
+               sym->implied.tri = tri;
+               sym_set_changed(sym);
+       }
 }
 
-static struct symbol *sym_calc_choice(struct symbol *sym)
+/*
+ * Find the default symbol for a choice.
+ * First try the default values for the choice symbol
+ * Next locate the first visible choice value
+ * Return NULL if none was found
+ */
+struct symbol *sym_choice_default(struct symbol *sym)
 {
        struct symbol *def_sym;
        struct property *prop;
        struct expr *e;
 
-       /* is the user choice visible? */
-       def_sym = sym->user.val;
-       if (def_sym) {
-               sym_calc_visibility(def_sym);
-               if (def_sym->visible != no)
-                       return def_sym;
-       }
-
        /* any of the defaults visible? */
        for_all_defaults(sym, prop) {
                prop->visible.tri = expr_calc_value(prop->visible.expr);
                if (prop->visible.tri == no)
                        continue;
                def_sym = prop_get_symbol(prop);
-               sym_calc_visibility(def_sym);
                if (def_sym->visible != no)
                        return def_sym;
        }
 
        /* just get the first visible value */
        prop = sym_get_choice_prop(sym);
-       for (e = prop->expr; e; e = e->left.expr) {
-               def_sym = e->right.sym;
-               sym_calc_visibility(def_sym);
+       expr_list_for_each_sym(prop->expr, e, def_sym)
                if (def_sym->visible != no)
                        return def_sym;
-       }
 
-       /* no choice? reset tristate value */
-       sym->curr.tri = no;
+       /* failed to locate any defaults */
        return NULL;
 }
 
+static struct symbol *sym_calc_choice(struct symbol *sym)
+{
+       struct symbol *def_sym;
+       struct property *prop;
+       struct expr *e;
+       int flags;
+
+       /* first calculate all choice values' visibilities */
+       flags = sym->flags;
+       prop = sym_get_choice_prop(sym);
+       expr_list_for_each_sym(prop->expr, e, def_sym) {
+               sym_calc_visibility(def_sym);
+               if (def_sym->visible != no)
+                       flags &= def_sym->flags;
+       }
+
+       sym->flags &= flags | ~SYMBOL_DEF_USER;
+
+       /* is the user choice visible? */
+       def_sym = sym->def[S_DEF_USER].val;
+       if (def_sym && def_sym->visible != no)
+               return def_sym;
+
+       def_sym = sym_choice_default(sym);
+
+       if (def_sym == NULL)
+               /* no choice? reset tristate value */
+               sym->curr.tri = no;
+
+       return def_sym;
+}
+
 void sym_calc_value(struct symbol *sym)
 {
        struct symbol_value newval, oldval;
@@ -278,6 +312,14 @@ void sym_calc_value(struct symbol *sym)
 
        if (sym->flags & SYMBOL_VALID)
                return;
+
+       if (sym_is_choice_value(sym) &&
+           sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES) {
+               sym->flags &= ~SYMBOL_NEED_SET_CHOICE_VALUES;
+               prop = sym_get_choice_prop(sym);
+               sym_calc_value(prop_get_symbol(prop));
+       }
+
        sym->flags |= SYMBOL_VALID;
 
        oldval = sym->curr;
@@ -297,11 +339,13 @@ void sym_calc_value(struct symbol *sym)
                sym->curr.tri = no;
                return;
        }
-       if (!sym_is_choice_value(sym))
-               sym->flags &= ~SYMBOL_WRITE;
+       sym->flags &= ~SYMBOL_WRITE;
 
        sym_calc_visibility(sym);
 
+       if (sym->visible != no)
+               sym->flags |= SYMBOL_WRITE;
+
        /* set default if recursively called */
        sym->curr = newval;
 
@@ -311,22 +355,38 @@ void sym_calc_value(struct symbol *sym)
                if (sym_is_choice_value(sym) && sym->visible == yes) {
                        prop = sym_get_choice_prop(sym);
                        newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
-               } else if (E_OR(sym->visible, sym->rev_dep.tri) != no) {
-                       sym->flags |= SYMBOL_WRITE;
-                       if (sym_has_value(sym))
-                               newval.tri = sym->user.tri;
-                       else if (!sym_is_choice(sym)) {
-                               prop = sym_get_default_prop(sym);
-                               if (prop)
-                                       newval.tri = expr_calc_value(prop->expr);
+               } else {
+                       if (sym->visible != no) {
+                               /* if the symbol is visible use the user value
+                                * if available, otherwise try the default value
+                                */
+                               if (sym_has_value(sym)) {
+                                       newval.tri = EXPR_AND(sym->def[S_DEF_USER].tri,
+                                                             sym->visible);
+                                       goto calc_newval;
+                               }
                        }
-                       newval.tri = E_OR(E_AND(newval.tri, sym->visible), sym->rev_dep.tri);
-               } else if (!sym_is_choice(sym)) {
-                       prop = sym_get_default_prop(sym);
-                       if (prop) {
+                       if (sym->rev_dep.tri != no)
                                sym->flags |= SYMBOL_WRITE;
-                               newval.tri = expr_calc_value(prop->expr);
+                       if (!sym_is_choice(sym)) {
+                               prop = sym_get_default_prop(sym);
+                               if (prop) {
+                                       sym->flags |= SYMBOL_WRITE;
+                                       newval.tri = EXPR_AND(expr_calc_value(prop->expr),
+                                                             prop->visible.tri);
+                               }
+                               if (sym->implied.tri != no) {
+                                       sym->flags |= SYMBOL_WRITE;
+                                       newval.tri = EXPR_OR(newval.tri, sym->implied.tri);
+                                       newval.tri = EXPR_AND(newval.tri,
+                                                             sym->dir_dep.tri);
+                               }
                        }
+               calc_newval:
+                       if (sym->dir_dep.tri == no && sym->rev_dep.tri != no)
+                               newval.tri = no;
+                       else
+                               newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri);
                }
                if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
                        newval.tri = yes;
@@ -334,12 +394,9 @@ void sym_calc_value(struct symbol *sym)
        case S_STRING:
        case S_HEX:
        case S_INT:
-               if (sym->visible != no) {
-                       sym->flags |= SYMBOL_WRITE;
-                       if (sym_has_value(sym)) {
-                               newval.val = sym->user.val;
-                               break;
-                       }
+               if (sym->visible != no && sym_has_value(sym)) {
+                       newval.val = sym->def[S_DEF_USER].val;
+                       break;
                }
                prop = sym_get_default_prop(sym);
                if (prop) {
@@ -360,53 +417,43 @@ void sym_calc_value(struct symbol *sym)
                sym->curr.val = sym_calc_choice(sym);
        sym_validate_range(sym);
 
-       if (memcmp(&oldval, &sym->curr, sizeof(oldval)))
+       if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
                sym_set_changed(sym);
-
-       if (modules_sym == sym)
-               modules_val = modules_sym->curr.tri;
+               if (modules_sym == sym) {
+                       sym_set_all_changed();
+                       modules_val = modules_sym->curr.tri;
+               }
+       }
 
        if (sym_is_choice(sym)) {
-               int flags = sym->flags & (SYMBOL_CHANGED | SYMBOL_WRITE);
+               struct symbol *choice_sym;
+
                prop = sym_get_choice_prop(sym);
-               for (e = prop->expr; e; e = e->left.expr) {
-                       e->right.sym->flags |= flags;
-                       if (flags & SYMBOL_CHANGED)
-                               sym_set_changed(e->right.sym);
+               expr_list_for_each_sym(prop->expr, e, choice_sym) {
+                       if ((sym->flags & SYMBOL_WRITE) &&
+                           choice_sym->visible != no)
+                               choice_sym->flags |= SYMBOL_WRITE;
+                       if (sym->flags & SYMBOL_CHANGED)
+                               sym_set_changed(choice_sym);
                }
        }
-}
-
-void sym_clear_all_valid(void)
-{
-       struct symbol *sym;
-       int i;
-
-       for_all_symbols(i, sym)
-               sym->flags &= ~SYMBOL_VALID;
-       sym_change_count++;
-       if (modules_sym)
-               sym_calc_value(modules_sym);
-}
 
-void sym_set_changed(struct symbol *sym)
-{
-       struct property *prop;
+       if (sym->flags & SYMBOL_NO_WRITE)
+               sym->flags &= ~SYMBOL_WRITE;
 
-       sym->flags |= SYMBOL_CHANGED;
-       for (prop = sym->prop; prop; prop = prop->next) {
-               if (prop->menu)
-                       prop->menu->flags |= MENU_CHANGED;
-       }
+       if (sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES)
+               set_all_choice_values(sym);
 }
 
-void sym_set_all_changed(void)
+void sym_clear_all_valid(void)
 {
        struct symbol *sym;
        int i;
 
        for_all_symbols(i, sym)
-               sym_set_changed(sym);
+               sym->flags &= ~SYMBOL_VALID;
+       conf_set_changed(true);
+       sym_calc_value(modules_sym);
 }
 
 bool sym_tristate_within_range(struct symbol *sym, tristate val)
@@ -435,8 +482,8 @@ bool sym_set_tristate_value(struct symbol *sym, tristate val)
        if (oldval != val && !sym_tristate_within_range(sym, val))
                return false;
 
-       if (sym->flags & SYMBOL_NEW) {
-               sym->flags &= ~SYMBOL_NEW;
+       if (!(sym->flags & SYMBOL_DEF_USER)) {
+               sym->flags |= SYMBOL_DEF_USER;
                sym_set_changed(sym);
        }
        /*
@@ -448,21 +495,18 @@ bool sym_set_tristate_value(struct symbol *sym, tristate val)
                struct property *prop;
                struct expr *e;
 
-               cs->user.val = sym;
-               cs->flags &= ~SYMBOL_NEW;
+               cs->def[S_DEF_USER].val = sym;
+               cs->flags |= SYMBOL_DEF_USER;
                prop = sym_get_choice_prop(cs);
                for (e = prop->expr; e; e = e->left.expr) {
                        if (e->right.sym->visible != no)
-                               e->right.sym->flags &= ~SYMBOL_NEW;
+                               e->right.sym->flags |= SYMBOL_DEF_USER;
                }
        }
 
-       sym->user.tri = val;
-       if (oldval != val) {
+       sym->def[S_DEF_USER].tri = val;
+       if (oldval != val)
                sym_clear_all_valid();
-               if (sym == modules_sym)
-                       sym_set_all_changed();
-       }
 
        return true;
 }
@@ -536,7 +580,7 @@ bool sym_string_valid(struct symbol *sym, const char *str)
 bool sym_string_within_range(struct symbol *sym, const char *str)
 {
        struct property *prop;
-       int val;
+       long long val;
 
        switch (sym->type) {
        case S_STRING:
@@ -547,7 +591,7 @@ bool sym_string_within_range(struct symbol *sym, const char *str)
                prop = sym_get_range_prop(sym);
                if (!prop)
                        return true;
-               val = strtol(str, NULL, 10);
+               val = strtoll(str, NULL, 10);
                return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
                       val <= sym_get_range_val(prop->expr->right.sym, 10);
        case S_HEX:
@@ -556,7 +600,7 @@ bool sym_string_within_range(struct symbol *sym, const char *str)
                prop = sym_get_range_prop(sym);
                if (!prop)
                        return true;
-               val = strtol(str, NULL, 16);
+               val = strtoll(str, NULL, 16);
                return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
                       val <= sym_get_range_val(prop->expr->right.sym, 16);
        case S_BOOLEAN:
@@ -600,20 +644,20 @@ bool sym_set_string_value(struct symbol *sym, const char *newval)
        if (!sym_string_within_range(sym, newval))
                return false;
 
-       if (sym->flags & SYMBOL_NEW) {
-               sym->flags &= ~SYMBOL_NEW;
+       if (!(sym->flags & SYMBOL_DEF_USER)) {
+               sym->flags |= SYMBOL_DEF_USER;
                sym_set_changed(sym);
        }
 
-       oldval = sym->user.val;
+       oldval = sym->def[S_DEF_USER].val;
        size = strlen(newval) + 1;
        if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
                size += 2;
-               sym->user.val = val = malloc(size);
+               sym->def[S_DEF_USER].val = val = xmalloc(size);
                *val++ = '0';
                *val++ = 'x';
        } else if (!oldval || strcmp(oldval, newval))
-               sym->user.val = val = malloc(size);
+               sym->def[S_DEF_USER].val = val = xmalloc(size);
        else
                return true;
 
@@ -624,6 +668,83 @@ bool sym_set_string_value(struct symbol *sym, const char *newval)
        return true;
 }
 
+/*
+ * Find the default value associated to a symbol.
+ * For tristate symbol handle the modules=n case
+ * in which case "m" becomes "y".
+ * If the symbol does not have any default then fallback
+ * to the fixed default values.
+ */
+const char *sym_get_string_default(struct symbol *sym)
+{
+       struct property *prop;
+       struct symbol *ds;
+       const char *str;
+       tristate val;
+
+       sym_calc_visibility(sym);
+       sym_calc_value(modules_sym);
+       val = symbol_no.curr.tri;
+       str = symbol_empty.curr.val;
+
+       /* If symbol has a default value look it up */
+       prop = sym_get_default_prop(sym);
+       if (prop != NULL) {
+               switch (sym->type) {
+               case S_BOOLEAN:
+               case S_TRISTATE:
+                       /* The visibility may limit the value from yes => mod */
+                       val = EXPR_AND(expr_calc_value(prop->expr), prop->visible.tri);
+                       break;
+               default:
+                       /*
+                        * The following fails to handle the situation
+                        * where a default value is further limited by
+                        * the valid range.
+                        */
+                       ds = prop_get_symbol(prop);
+                       if (ds != NULL) {
+                               sym_calc_value(ds);
+                               str = (const char *)ds->curr.val;
+                       }
+               }
+       }
+
+       /* Handle select statements */
+       val = EXPR_OR(val, sym->rev_dep.tri);
+
+       /* transpose mod to yes if modules are not enabled */
+       if (val == mod)
+               if (!sym_is_choice_value(sym) && modules_sym->curr.tri == no)
+                       val = yes;
+
+       /* transpose mod to yes if type is bool */
+       if (sym->type == S_BOOLEAN && val == mod)
+               val = yes;
+
+       /* adjust the default value if this symbol is implied by another */
+       if (val < sym->implied.tri)
+               val = sym->implied.tri;
+
+       switch (sym->type) {
+       case S_BOOLEAN:
+       case S_TRISTATE:
+               switch (val) {
+               case no: return "n";
+               case mod: return "m";
+               case yes: return "y";
+               }
+       case S_INT:
+       case S_HEX:
+               return str;
+       case S_STRING:
+               return str;
+       case S_UNKNOWN:
+               break;
+       }
+       return "";
+}
+
 const char *sym_get_string_value(struct symbol *sym)
 {
        tristate val;
@@ -636,7 +757,8 @@ const char *sym_get_string_value(struct symbol *sym)
                case no:
                        return "n";
                case mod:
-                       return "m";
+                       sym_calc_value(modules_sym);
+                       return (modules_sym->curr.tri == no) ? "n" : "m";
                case yes:
                        return "y";
                }
@@ -647,17 +769,25 @@ const char *sym_get_string_value(struct symbol *sym)
        return (const char *)sym->curr.val;
 }
 
-bool sym_is_changable(struct symbol *sym)
+bool sym_is_changeable(struct symbol *sym)
 {
        return sym->visible > sym->rev_dep.tri;
 }
 
-struct symbol *sym_lookup(const char *name, int isconst)
+static unsigned strhash(const char *s)
+{
+       /* fnv32 hash */
+       unsigned hash = 2166136261U;
+       for (; *s; s++)
+               hash = (hash ^ *s) * 0x01000193;
+       return hash;
+}
+
+struct symbol *sym_lookup(const char *name, int flags)
 {
        struct symbol *symbol;
-       const char *ptr;
        char *new_name;
-       int hash = 0;
+       int hash;
 
        if (name) {
                if (name[0] && !name[1]) {
@@ -667,30 +797,26 @@ struct symbol *sym_lookup(const char *name, int isconst)
                        case 'n': return &symbol_no;
                        }
                }
-               for (ptr = name; *ptr; ptr++)
-                       hash += *ptr;
-               hash &= 0xff;
+               hash = strhash(name) % SYMBOL_HASHSIZE;
 
                for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
-                       if (!strcmp(symbol->name, name)) {
-                               if ((isconst && symbol->flags & SYMBOL_CONST) ||
-                                   (!isconst && !(symbol->flags & SYMBOL_CONST)))
-                                       return symbol;
-                       }
+                       if (symbol->name &&
+                           !strcmp(symbol->name, name) &&
+                           (flags ? symbol->flags & flags
+                                  : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE))))
+                               return symbol;
                }
-               new_name = strdup(name);
+               new_name = xstrdup(name);
        } else {
                new_name = NULL;
-               hash = 256;
+               hash = 0;
        }
 
-       symbol = malloc(sizeof(*symbol));
+       symbol = xmalloc(sizeof(*symbol));
        memset(symbol, 0, sizeof(*symbol));
        symbol->name = new_name;
        symbol->type = S_UNKNOWN;
-       symbol->flags = SYMBOL_NEW;
-       if (isconst)
-               symbol->flags |= SYMBOL_CONST;
+       symbol->flags = flags;
 
        symbol->next = symbol_hash[hash];
        symbol_hash[hash] = symbol;
@@ -701,7 +827,6 @@ struct symbol *sym_lookup(const char *name, int isconst)
 struct symbol *sym_find(const char *name)
 {
        struct symbol *symbol = NULL;
-       const char *ptr;
        int hash = 0;
 
        if (!name)
@@ -714,12 +839,11 @@ struct symbol *sym_find(const char *name)
                case 'n': return &symbol_no;
                }
        }
-       for (ptr = name; *ptr; ptr++)
-               hash += *ptr;
-       hash &= 0xff;
+       hash = strhash(name) % SYMBOL_HASHSIZE;
 
        for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
-               if (!strcmp(symbol->name, name) &&
+               if (symbol->name &&
+                   !strcmp(symbol->name, name) &&
                    !(symbol->flags & SYMBOL_CONST))
                                break;
        }
@@ -727,44 +851,260 @@ struct symbol *sym_find(const char *name)
        return symbol;
 }
 
+const char *sym_escape_string_value(const char *in)
+{
+       const char *p;
+       size_t reslen;
+       char *res;
+       size_t l;
+
+       reslen = strlen(in) + strlen("\"\"") + 1;
+
+       p = in;
+       for (;;) {
+               l = strcspn(p, "\"\\");
+               p += l;
+
+               if (p[0] == '\0')
+                       break;
+
+               reslen++;
+               p++;
+       }
+
+       res = xmalloc(reslen);
+       res[0] = '\0';
+
+       strcat(res, "\"");
+
+       p = in;
+       for (;;) {
+               l = strcspn(p, "\"\\");
+               strncat(res, p, l);
+               p += l;
+
+               if (p[0] == '\0')
+                       break;
+
+               strcat(res, "\\");
+               strncat(res, p++, 1);
+       }
+
+       strcat(res, "\"");
+       return res;
+}
+
+struct sym_match {
+       struct symbol   *sym;
+       off_t           so, eo;
+};
+
+/* Compare matched symbols as thus:
+ * - first, symbols that match exactly
+ * - then, alphabetical sort
+ */
+static int sym_rel_comp(const void *sym1, const void *sym2)
+{
+       const struct sym_match *s1 = sym1;
+       const struct sym_match *s2 = sym2;
+       int exact1, exact2;
+
+       /* Exact match:
+        * - if matched length on symbol s1 is the length of that symbol,
+        *   then this symbol should come first;
+        * - if matched length on symbol s2 is the length of that symbol,
+        *   then this symbol should come first.
+        * Note: since the search can be a regexp, both symbols may match
+        * exactly; if this is the case, we can't decide which comes first,
+        * and we fallback to sorting alphabetically.
+        */
+       exact1 = (s1->eo - s1->so) == strlen(s1->sym->name);
+       exact2 = (s2->eo - s2->so) == strlen(s2->sym->name);
+       if (exact1 && !exact2)
+               return -1;
+       if (!exact1 && exact2)
+               return 1;
+
+       /* As a fallback, sort symbols alphabetically */
+       return strcmp(s1->sym->name, s2->sym->name);
+}
+
 struct symbol **sym_re_search(const char *pattern)
 {
        struct symbol *sym, **sym_arr = NULL;
+       struct sym_match *sym_match_arr = NULL;
        int i, cnt, size;
        regex_t re;
+       regmatch_t match[1];
 
        cnt = size = 0;
        /* Skip if empty */
        if (strlen(pattern) == 0)
                return NULL;
-       if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB|REG_ICASE))
+       if (regcomp(&re, pattern, REG_EXTENDED|REG_ICASE))
                return NULL;
 
        for_all_symbols(i, sym) {
                if (sym->flags & SYMBOL_CONST || !sym->name)
                        continue;
-               if (regexec(&re, sym->name, 0, NULL, 0))
+               if (regexec(&re, sym->name, 1, match, 0))
                        continue;
-               if (cnt + 1 >= size) {
-                       void *tmp = sym_arr;
+               if (cnt >= size) {
+                       void *tmp;
                        size += 16;
-                       sym_arr = realloc(sym_arr, size * sizeof(struct symbol *));
-                       if (!sym_arr) {
-                               free(tmp);
-                               return NULL;
-                       }
+                       tmp = realloc(sym_match_arr, size * sizeof(struct sym_match));
+                       if (!tmp)
+                               goto sym_re_search_free;
+                       sym_match_arr = tmp;
                }
-               sym_arr[cnt++] = sym;
+               sym_calc_value(sym);
+               /* As regexec returned 0, we know we have a match, so
+                * we can use match[0].rm_[se]o without further checks
+                */
+               sym_match_arr[cnt].so = match[0].rm_so;
+               sym_match_arr[cnt].eo = match[0].rm_eo;
+               sym_match_arr[cnt++].sym = sym;
        }
-       if (sym_arr)
+       if (sym_match_arr) {
+               qsort(sym_match_arr, cnt, sizeof(struct sym_match), sym_rel_comp);
+               sym_arr = malloc((cnt+1) * sizeof(struct symbol *));
+               if (!sym_arr)
+                       goto sym_re_search_free;
+               for (i = 0; i < cnt; i++)
+                       sym_arr[i] = sym_match_arr[i].sym;
                sym_arr[cnt] = NULL;
+       }
+sym_re_search_free:
+       /* sym_match_arr can be NULL if no match, but free(NULL) is OK */
+       free(sym_match_arr);
        regfree(&re);
 
        return sym_arr;
 }
 
+/*
+ * When we check for recursive dependencies we use a stack to save
+ * current state so we can print out relevant info to user.
+ * The entries are located on the call stack so no need to free memory.
+ * Note insert() remove() must always match to properly clear the stack.
+ */
+static struct dep_stack {
+       struct dep_stack *prev, *next;
+       struct symbol *sym;
+       struct property *prop;
+       struct expr **expr;
+} *check_top;
+
+static void dep_stack_insert(struct dep_stack *stack, struct symbol *sym)
+{
+       memset(stack, 0, sizeof(*stack));
+       if (check_top)
+               check_top->next = stack;
+       stack->prev = check_top;
+       stack->sym = sym;
+       check_top = stack;
+}
 
-struct symbol *sym_check_deps(struct symbol *sym);
+static void dep_stack_remove(void)
+{
+       check_top = check_top->prev;
+       if (check_top)
+               check_top->next = NULL;
+}
+
+/*
+ * Called when we have detected a recursive dependency.
+ * check_top point to the top of the stact so we use
+ * the ->prev pointer to locate the bottom of the stack.
+ */
+static void sym_check_print_recursive(struct symbol *last_sym)
+{
+       struct dep_stack *stack;
+       struct symbol *sym, *next_sym;
+       struct menu *menu = NULL;
+       struct property *prop;
+       struct dep_stack cv_stack;
+
+       if (sym_is_choice_value(last_sym)) {
+               dep_stack_insert(&cv_stack, last_sym);
+               last_sym = prop_get_symbol(sym_get_choice_prop(last_sym));
+       }
+
+       for (stack = check_top; stack != NULL; stack = stack->prev)
+               if (stack->sym == last_sym)
+                       break;
+       if (!stack) {
+               fprintf(stderr, "unexpected recursive dependency error\n");
+               return;
+       }
+
+       for (; stack; stack = stack->next) {
+               sym = stack->sym;
+               next_sym = stack->next ? stack->next->sym : last_sym;
+               prop = stack->prop;
+               if (prop == NULL)
+                       prop = stack->sym->prop;
+
+               /* for choice values find the menu entry (used below) */
+               if (sym_is_choice(sym) || sym_is_choice_value(sym)) {
+                       for (prop = sym->prop; prop; prop = prop->next) {
+                               menu = prop->menu;
+                               if (prop->menu)
+                                       break;
+                       }
+               }
+               if (stack->sym == last_sym)
+                       fprintf(stderr, "%s:%d:error: recursive dependency detected!\n",
+                               prop->file->name, prop->lineno);
+
+               if (sym_is_choice(sym)) {
+                       fprintf(stderr, "%s:%d:\tchoice %s contains symbol %s\n",
+                               menu->file->name, menu->lineno,
+                               sym->name ? sym->name : "<choice>",
+                               next_sym->name ? next_sym->name : "<choice>");
+               } else if (sym_is_choice_value(sym)) {
+                       fprintf(stderr, "%s:%d:\tsymbol %s is part of choice %s\n",
+                               menu->file->name, menu->lineno,
+                               sym->name ? sym->name : "<choice>",
+                               next_sym->name ? next_sym->name : "<choice>");
+               } else if (stack->expr == &sym->dir_dep.expr) {
+                       fprintf(stderr, "%s:%d:\tsymbol %s depends on %s\n",
+                               prop->file->name, prop->lineno,
+                               sym->name ? sym->name : "<choice>",
+                               next_sym->name ? next_sym->name : "<choice>");
+               } else if (stack->expr == &sym->rev_dep.expr) {
+                       fprintf(stderr, "%s:%d:\tsymbol %s is selected by %s\n",
+                               prop->file->name, prop->lineno,
+                               sym->name ? sym->name : "<choice>",
+                               next_sym->name ? next_sym->name : "<choice>");
+               } else if (stack->expr == &sym->implied.expr) {
+                       fprintf(stderr, "%s:%d:\tsymbol %s is implied by %s\n",
+                               prop->file->name, prop->lineno,
+                               sym->name ? sym->name : "<choice>",
+                               next_sym->name ? next_sym->name : "<choice>");
+               } else if (stack->expr) {
+                       fprintf(stderr, "%s:%d:\tsymbol %s %s value contains %s\n",
+                               prop->file->name, prop->lineno,
+                               sym->name ? sym->name : "<choice>",
+                               prop_get_type_name(prop->type),
+                               next_sym->name ? next_sym->name : "<choice>");
+               } else {
+                       fprintf(stderr, "%s:%d:\tsymbol %s %s is visible depending on %s\n",
+                               prop->file->name, prop->lineno,
+                               sym->name ? sym->name : "<choice>",
+                               prop_get_type_name(prop->type),
+                               next_sym->name ? next_sym->name : "<choice>");
+               }
+       }
+
+       fprintf(stderr,
+               "For a resolution refer to Documentation/kbuild/kconfig-language.rst\n"
+               "subsection \"Kconfig recursive dependency limitations\"\n"
+               "\n");
+
+       if (check_top == &cv_stack)
+               dep_stack_remove();
+}
 
 static struct symbol *sym_check_expr_deps(struct expr *e)
 {
@@ -782,6 +1122,10 @@ static struct symbol *sym_check_expr_deps(struct expr *e)
        case E_NOT:
                return sym_check_expr_deps(e->left.expr);
        case E_EQUAL:
+       case E_GEQ:
+       case E_GTH:
+       case E_LEQ:
+       case E_LTH:
        case E_UNEQUAL:
                sym = sym_check_deps(e->left.sym);
                if (sym)
@@ -792,77 +1136,134 @@ static struct symbol *sym_check_expr_deps(struct expr *e)
        default:
                break;
        }
-       printf("Oops! How to check %d?\n", e->type);
+       fprintf(stderr, "Oops! How to check %d?\n", e->type);
        return NULL;
 }
 
-struct symbol *sym_check_deps(struct symbol *sym)
+/* return NULL when dependencies are OK */
+static struct symbol *sym_check_sym_deps(struct symbol *sym)
 {
        struct symbol *sym2;
        struct property *prop;
+       struct dep_stack stack;
 
-       if (sym->flags & SYMBOL_CHECK) {
-               printf("Warning! Found recursive dependency: %s", sym->name);
-               return sym;
-       }
-       if (sym->flags & SYMBOL_CHECKED)
-               return NULL;
+       dep_stack_insert(&stack, sym);
+
+       stack.expr = &sym->dir_dep.expr;
+       sym2 = sym_check_expr_deps(sym->dir_dep.expr);
+       if (sym2)
+               goto out;
 
-       sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
+       stack.expr = &sym->rev_dep.expr;
        sym2 = sym_check_expr_deps(sym->rev_dep.expr);
        if (sym2)
                goto out;
 
+       stack.expr = &sym->implied.expr;
+       sym2 = sym_check_expr_deps(sym->implied.expr);
+       if (sym2)
+               goto out;
+
+       stack.expr = NULL;
+
        for (prop = sym->prop; prop; prop = prop->next) {
-               if (prop->type == P_CHOICE || prop->type == P_SELECT)
+               if (prop->type == P_CHOICE || prop->type == P_SELECT ||
+                   prop->type == P_IMPLY)
                        continue;
+               stack.prop = prop;
                sym2 = sym_check_expr_deps(prop->visible.expr);
                if (sym2)
-                       goto out;
+                       break;
                if (prop->type != P_DEFAULT || sym_is_choice(sym))
                        continue;
+               stack.expr = &prop->expr;
                sym2 = sym_check_expr_deps(prop->expr);
                if (sym2)
-                       goto out;
+                       break;
+               stack.expr = NULL;
        }
+
 out:
-       if (sym2) {
-               printf(" %s", sym->name);
-               if (sym2 == sym) {
-                       printf("\n");
-                       sym2 = NULL;
-               }
+       dep_stack_remove();
+
+       return sym2;
+}
+
+static struct symbol *sym_check_choice_deps(struct symbol *choice)
+{
+       struct symbol *sym, *sym2;
+       struct property *prop;
+       struct expr *e;
+       struct dep_stack stack;
+
+       dep_stack_insert(&stack, choice);
+
+       prop = sym_get_choice_prop(choice);
+       expr_list_for_each_sym(prop->expr, e, sym)
+               sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
+
+       choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
+       sym2 = sym_check_sym_deps(choice);
+       choice->flags &= ~SYMBOL_CHECK;
+       if (sym2)
+               goto out;
+
+       expr_list_for_each_sym(prop->expr, e, sym) {
+               sym2 = sym_check_sym_deps(sym);
+               if (sym2)
+                       break;
        }
-       sym->flags &= ~SYMBOL_CHECK;
+out:
+       expr_list_for_each_sym(prop->expr, e, sym)
+               sym->flags &= ~SYMBOL_CHECK;
+
+       if (sym2 && sym_is_choice_value(sym2) &&
+           prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
+               sym2 = choice;
+
+       dep_stack_remove();
+
        return sym2;
 }
 
-struct property *prop_alloc(enum prop_type type, struct symbol *sym)
+struct symbol *sym_check_deps(struct symbol *sym)
 {
+       struct symbol *sym2;
        struct property *prop;
-       struct property **propp;
 
-       prop = malloc(sizeof(*prop));
-       memset(prop, 0, sizeof(*prop));
-       prop->type = type;
-       prop->sym = sym;
-       prop->file = current_file;
-       prop->lineno = zconf_lineno();
+       if (sym->flags & SYMBOL_CHECK) {
+               sym_check_print_recursive(sym);
+               return sym;
+       }
+       if (sym->flags & SYMBOL_CHECKED)
+               return NULL;
+
+       if (sym_is_choice_value(sym)) {
+               struct dep_stack stack;
 
-       /* append property to the prop list of symbol */
-       if (sym) {
-               for (propp = &sym->prop; *propp; propp = &(*propp)->next)
-                       ;
-               *propp = prop;
+               /* for choice groups start the check with main choice symbol */
+               dep_stack_insert(&stack, sym);
+               prop = sym_get_choice_prop(sym);
+               sym2 = sym_check_deps(prop_get_symbol(prop));
+               dep_stack_remove();
+       } else if (sym_is_choice(sym)) {
+               sym2 = sym_check_choice_deps(sym);
+       } else {
+               sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
+               sym2 = sym_check_sym_deps(sym);
+               sym->flags &= ~SYMBOL_CHECK;
        }
 
-       return prop;
+       if (!recursive_is_error && sym2 && sym2 == sym)
+               sym2 = NULL;
+
+       return sym2;
 }
 
 struct symbol *prop_get_symbol(struct property *prop)
 {
        if (prop->expr && (prop->expr->type == E_SYMBOL ||
-                          prop->expr->type == E_CHOICE))
+                          prop->expr->type == E_LIST))
                return prop->expr->left.sym;
        return NULL;
 }
@@ -882,8 +1283,14 @@ const char *prop_get_type_name(enum prop_type type)
                return "choice";
        case P_SELECT:
                return "select";
+       case P_IMPLY:
+               return "imply";
        case P_RANGE:
                return "range";
+       case P_SYMBOL:
+               return "symbol";
+       case P_RESET:
+               return "reset";
        case P_UNKNOWN:
                break;
        }