Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
0004  */
0005 
0006 #include <sys/types.h>
0007 #include <ctype.h>
0008 #include <stdlib.h>
0009 #include <string.h>
0010 #include <regex.h>
0011 
0012 #include "lkc.h"
0013 
0014 struct symbol symbol_yes = {
0015     .name = "y",
0016     .curr = { "y", yes },
0017     .flags = SYMBOL_CONST|SYMBOL_VALID,
0018 };
0019 
0020 struct symbol symbol_mod = {
0021     .name = "m",
0022     .curr = { "m", mod },
0023     .flags = SYMBOL_CONST|SYMBOL_VALID,
0024 };
0025 
0026 struct symbol symbol_no = {
0027     .name = "n",
0028     .curr = { "n", no },
0029     .flags = SYMBOL_CONST|SYMBOL_VALID,
0030 };
0031 
0032 static struct symbol symbol_empty = {
0033     .name = "",
0034     .curr = { "", no },
0035     .flags = SYMBOL_VALID,
0036 };
0037 
0038 struct symbol *modules_sym;
0039 static tristate modules_val;
0040 
0041 enum symbol_type sym_get_type(struct symbol *sym)
0042 {
0043     enum symbol_type type = sym->type;
0044 
0045     if (type == S_TRISTATE) {
0046         if (sym_is_choice_value(sym) && sym->visible == yes)
0047             type = S_BOOLEAN;
0048         else if (modules_val == no)
0049             type = S_BOOLEAN;
0050     }
0051     return type;
0052 }
0053 
0054 const char *sym_type_name(enum symbol_type type)
0055 {
0056     switch (type) {
0057     case S_BOOLEAN:
0058         return "bool";
0059     case S_TRISTATE:
0060         return "tristate";
0061     case S_INT:
0062         return "integer";
0063     case S_HEX:
0064         return "hex";
0065     case S_STRING:
0066         return "string";
0067     case S_UNKNOWN:
0068         return "unknown";
0069     }
0070     return "???";
0071 }
0072 
0073 struct property *sym_get_choice_prop(struct symbol *sym)
0074 {
0075     struct property *prop;
0076 
0077     for_all_choices(sym, prop)
0078         return prop;
0079     return NULL;
0080 }
0081 
0082 static struct property *sym_get_default_prop(struct symbol *sym)
0083 {
0084     struct property *prop;
0085 
0086     for_all_defaults(sym, prop) {
0087         prop->visible.tri = expr_calc_value(prop->visible.expr);
0088         if (prop->visible.tri != no)
0089             return prop;
0090     }
0091     return NULL;
0092 }
0093 
0094 struct property *sym_get_range_prop(struct symbol *sym)
0095 {
0096     struct property *prop;
0097 
0098     for_all_properties(sym, prop, P_RANGE) {
0099         prop->visible.tri = expr_calc_value(prop->visible.expr);
0100         if (prop->visible.tri != no)
0101             return prop;
0102     }
0103     return NULL;
0104 }
0105 
0106 static long long sym_get_range_val(struct symbol *sym, int base)
0107 {
0108     sym_calc_value(sym);
0109     switch (sym->type) {
0110     case S_INT:
0111         base = 10;
0112         break;
0113     case S_HEX:
0114         base = 16;
0115         break;
0116     default:
0117         break;
0118     }
0119     return strtoll(sym->curr.val, NULL, base);
0120 }
0121 
0122 static void sym_validate_range(struct symbol *sym)
0123 {
0124     struct property *prop;
0125     int base;
0126     long long val, val2;
0127     char str[64];
0128 
0129     switch (sym->type) {
0130     case S_INT:
0131         base = 10;
0132         break;
0133     case S_HEX:
0134         base = 16;
0135         break;
0136     default:
0137         return;
0138     }
0139     prop = sym_get_range_prop(sym);
0140     if (!prop)
0141         return;
0142     val = strtoll(sym->curr.val, NULL, base);
0143     val2 = sym_get_range_val(prop->expr->left.sym, base);
0144     if (val >= val2) {
0145         val2 = sym_get_range_val(prop->expr->right.sym, base);
0146         if (val <= val2)
0147             return;
0148     }
0149     if (sym->type == S_INT)
0150         sprintf(str, "%lld", val2);
0151     else
0152         sprintf(str, "0x%llx", val2);
0153     sym->curr.val = xstrdup(str);
0154 }
0155 
0156 static void sym_set_changed(struct symbol *sym)
0157 {
0158     struct property *prop;
0159 
0160     sym->flags |= SYMBOL_CHANGED;
0161     for (prop = sym->prop; prop; prop = prop->next) {
0162         if (prop->menu)
0163             prop->menu->flags |= MENU_CHANGED;
0164     }
0165 }
0166 
0167 static void sym_set_all_changed(void)
0168 {
0169     struct symbol *sym;
0170     int i;
0171 
0172     for_all_symbols(i, sym)
0173         sym_set_changed(sym);
0174 }
0175 
0176 static void sym_calc_visibility(struct symbol *sym)
0177 {
0178     struct property *prop;
0179     struct symbol *choice_sym = NULL;
0180     tristate tri;
0181 
0182     /* any prompt visible? */
0183     tri = no;
0184 
0185     if (sym_is_choice_value(sym))
0186         choice_sym = prop_get_symbol(sym_get_choice_prop(sym));
0187 
0188     for_all_prompts(sym, prop) {
0189         prop->visible.tri = expr_calc_value(prop->visible.expr);
0190         /*
0191          * Tristate choice_values with visibility 'mod' are
0192          * not visible if the corresponding choice's value is
0193          * 'yes'.
0194          */
0195         if (choice_sym && sym->type == S_TRISTATE &&
0196             prop->visible.tri == mod && choice_sym->curr.tri == yes)
0197             prop->visible.tri = no;
0198 
0199         tri = EXPR_OR(tri, prop->visible.tri);
0200     }
0201     if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
0202         tri = yes;
0203     if (sym->visible != tri) {
0204         sym->visible = tri;
0205         sym_set_changed(sym);
0206     }
0207     if (sym_is_choice_value(sym))
0208         return;
0209     /* defaulting to "yes" if no explicit "depends on" are given */
0210     tri = yes;
0211     if (sym->dir_dep.expr)
0212         tri = expr_calc_value(sym->dir_dep.expr);
0213     if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
0214         tri = yes;
0215     if (sym->dir_dep.tri != tri) {
0216         sym->dir_dep.tri = tri;
0217         sym_set_changed(sym);
0218     }
0219     tri = no;
0220     if (sym->rev_dep.expr)
0221         tri = expr_calc_value(sym->rev_dep.expr);
0222     if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
0223         tri = yes;
0224     if (sym->rev_dep.tri != tri) {
0225         sym->rev_dep.tri = tri;
0226         sym_set_changed(sym);
0227     }
0228     tri = no;
0229     if (sym->implied.expr)
0230         tri = expr_calc_value(sym->implied.expr);
0231     if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
0232         tri = yes;
0233     if (sym->implied.tri != tri) {
0234         sym->implied.tri = tri;
0235         sym_set_changed(sym);
0236     }
0237 }
0238 
0239 /*
0240  * Find the default symbol for a choice.
0241  * First try the default values for the choice symbol
0242  * Next locate the first visible choice value
0243  * Return NULL if none was found
0244  */
0245 struct symbol *sym_choice_default(struct symbol *sym)
0246 {
0247     struct symbol *def_sym;
0248     struct property *prop;
0249     struct expr *e;
0250 
0251     /* any of the defaults visible? */
0252     for_all_defaults(sym, prop) {
0253         prop->visible.tri = expr_calc_value(prop->visible.expr);
0254         if (prop->visible.tri == no)
0255             continue;
0256         def_sym = prop_get_symbol(prop);
0257         if (def_sym->visible != no)
0258             return def_sym;
0259     }
0260 
0261     /* just get the first visible value */
0262     prop = sym_get_choice_prop(sym);
0263     expr_list_for_each_sym(prop->expr, e, def_sym)
0264         if (def_sym->visible != no)
0265             return def_sym;
0266 
0267     /* failed to locate any defaults */
0268     return NULL;
0269 }
0270 
0271 static struct symbol *sym_calc_choice(struct symbol *sym)
0272 {
0273     struct symbol *def_sym;
0274     struct property *prop;
0275     struct expr *e;
0276     int flags;
0277 
0278     /* first calculate all choice values' visibilities */
0279     flags = sym->flags;
0280     prop = sym_get_choice_prop(sym);
0281     expr_list_for_each_sym(prop->expr, e, def_sym) {
0282         sym_calc_visibility(def_sym);
0283         if (def_sym->visible != no)
0284             flags &= def_sym->flags;
0285     }
0286 
0287     sym->flags &= flags | ~SYMBOL_DEF_USER;
0288 
0289     /* is the user choice visible? */
0290     def_sym = sym->def[S_DEF_USER].val;
0291     if (def_sym && def_sym->visible != no)
0292         return def_sym;
0293 
0294     def_sym = sym_choice_default(sym);
0295 
0296     if (def_sym == NULL)
0297         /* no choice? reset tristate value */
0298         sym->curr.tri = no;
0299 
0300     return def_sym;
0301 }
0302 
0303 static void sym_warn_unmet_dep(struct symbol *sym)
0304 {
0305     struct gstr gs = str_new();
0306 
0307     str_printf(&gs,
0308            "\nWARNING: unmet direct dependencies detected for %s\n",
0309            sym->name);
0310     str_printf(&gs,
0311            "  Depends on [%c]: ",
0312            sym->dir_dep.tri == mod ? 'm' : 'n');
0313     expr_gstr_print(sym->dir_dep.expr, &gs);
0314     str_printf(&gs, "\n");
0315 
0316     expr_gstr_print_revdep(sym->rev_dep.expr, &gs, yes,
0317                    "  Selected by [y]:\n");
0318     expr_gstr_print_revdep(sym->rev_dep.expr, &gs, mod,
0319                    "  Selected by [m]:\n");
0320 
0321     fputs(str_get(&gs), stderr);
0322 }
0323 
0324 void sym_calc_value(struct symbol *sym)
0325 {
0326     struct symbol_value newval, oldval;
0327     struct property *prop;
0328     struct expr *e;
0329 
0330     if (!sym)
0331         return;
0332 
0333     if (sym->flags & SYMBOL_VALID)
0334         return;
0335 
0336     if (sym_is_choice_value(sym) &&
0337         sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES) {
0338         sym->flags &= ~SYMBOL_NEED_SET_CHOICE_VALUES;
0339         prop = sym_get_choice_prop(sym);
0340         sym_calc_value(prop_get_symbol(prop));
0341     }
0342 
0343     sym->flags |= SYMBOL_VALID;
0344 
0345     oldval = sym->curr;
0346 
0347     switch (sym->type) {
0348     case S_INT:
0349     case S_HEX:
0350     case S_STRING:
0351         newval = symbol_empty.curr;
0352         break;
0353     case S_BOOLEAN:
0354     case S_TRISTATE:
0355         newval = symbol_no.curr;
0356         break;
0357     default:
0358         sym->curr.val = sym->name;
0359         sym->curr.tri = no;
0360         return;
0361     }
0362     sym->flags &= ~SYMBOL_WRITE;
0363 
0364     sym_calc_visibility(sym);
0365 
0366     if (sym->visible != no)
0367         sym->flags |= SYMBOL_WRITE;
0368 
0369     /* set default if recursively called */
0370     sym->curr = newval;
0371 
0372     switch (sym_get_type(sym)) {
0373     case S_BOOLEAN:
0374     case S_TRISTATE:
0375         if (sym_is_choice_value(sym) && sym->visible == yes) {
0376             prop = sym_get_choice_prop(sym);
0377             newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
0378         } else {
0379             if (sym->visible != no) {
0380                 /* if the symbol is visible use the user value
0381                  * if available, otherwise try the default value
0382                  */
0383                 if (sym_has_value(sym)) {
0384                     newval.tri = EXPR_AND(sym->def[S_DEF_USER].tri,
0385                                   sym->visible);
0386                     goto calc_newval;
0387                 }
0388             }
0389             if (sym->rev_dep.tri != no)
0390                 sym->flags |= SYMBOL_WRITE;
0391             if (!sym_is_choice(sym)) {
0392                 prop = sym_get_default_prop(sym);
0393                 if (prop) {
0394                     newval.tri = EXPR_AND(expr_calc_value(prop->expr),
0395                                   prop->visible.tri);
0396                     if (newval.tri != no)
0397                         sym->flags |= SYMBOL_WRITE;
0398                 }
0399                 if (sym->implied.tri != no) {
0400                     sym->flags |= SYMBOL_WRITE;
0401                     newval.tri = EXPR_OR(newval.tri, sym->implied.tri);
0402                     newval.tri = EXPR_AND(newval.tri,
0403                                   sym->dir_dep.tri);
0404                 }
0405             }
0406         calc_newval:
0407             if (sym->dir_dep.tri < sym->rev_dep.tri)
0408                 sym_warn_unmet_dep(sym);
0409             newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri);
0410         }
0411         if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
0412             newval.tri = yes;
0413         break;
0414     case S_STRING:
0415     case S_HEX:
0416     case S_INT:
0417         if (sym->visible != no && sym_has_value(sym)) {
0418             newval.val = sym->def[S_DEF_USER].val;
0419             break;
0420         }
0421         prop = sym_get_default_prop(sym);
0422         if (prop) {
0423             struct symbol *ds = prop_get_symbol(prop);
0424             if (ds) {
0425                 sym->flags |= SYMBOL_WRITE;
0426                 sym_calc_value(ds);
0427                 newval.val = ds->curr.val;
0428             }
0429         }
0430         break;
0431     default:
0432         ;
0433     }
0434 
0435     sym->curr = newval;
0436     if (sym_is_choice(sym) && newval.tri == yes)
0437         sym->curr.val = sym_calc_choice(sym);
0438     sym_validate_range(sym);
0439 
0440     if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
0441         sym_set_changed(sym);
0442         if (modules_sym == sym) {
0443             sym_set_all_changed();
0444             modules_val = modules_sym->curr.tri;
0445         }
0446     }
0447 
0448     if (sym_is_choice(sym)) {
0449         struct symbol *choice_sym;
0450 
0451         prop = sym_get_choice_prop(sym);
0452         expr_list_for_each_sym(prop->expr, e, choice_sym) {
0453             if ((sym->flags & SYMBOL_WRITE) &&
0454                 choice_sym->visible != no)
0455                 choice_sym->flags |= SYMBOL_WRITE;
0456             if (sym->flags & SYMBOL_CHANGED)
0457                 sym_set_changed(choice_sym);
0458         }
0459     }
0460 
0461     if (sym->flags & SYMBOL_NO_WRITE)
0462         sym->flags &= ~SYMBOL_WRITE;
0463 
0464     if (sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES)
0465         set_all_choice_values(sym);
0466 }
0467 
0468 void sym_clear_all_valid(void)
0469 {
0470     struct symbol *sym;
0471     int i;
0472 
0473     for_all_symbols(i, sym)
0474         sym->flags &= ~SYMBOL_VALID;
0475     conf_set_changed(true);
0476     sym_calc_value(modules_sym);
0477 }
0478 
0479 bool sym_tristate_within_range(struct symbol *sym, tristate val)
0480 {
0481     int type = sym_get_type(sym);
0482 
0483     if (sym->visible == no)
0484         return false;
0485 
0486     if (type != S_BOOLEAN && type != S_TRISTATE)
0487         return false;
0488 
0489     if (type == S_BOOLEAN && val == mod)
0490         return false;
0491     if (sym->visible <= sym->rev_dep.tri)
0492         return false;
0493     if (sym_is_choice_value(sym) && sym->visible == yes)
0494         return val == yes;
0495     return val >= sym->rev_dep.tri && val <= sym->visible;
0496 }
0497 
0498 bool sym_set_tristate_value(struct symbol *sym, tristate val)
0499 {
0500     tristate oldval = sym_get_tristate_value(sym);
0501 
0502     if (oldval != val && !sym_tristate_within_range(sym, val))
0503         return false;
0504 
0505     if (!(sym->flags & SYMBOL_DEF_USER)) {
0506         sym->flags |= SYMBOL_DEF_USER;
0507         sym_set_changed(sym);
0508     }
0509     /*
0510      * setting a choice value also resets the new flag of the choice
0511      * symbol and all other choice values.
0512      */
0513     if (sym_is_choice_value(sym) && val == yes) {
0514         struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
0515         struct property *prop;
0516         struct expr *e;
0517 
0518         cs->def[S_DEF_USER].val = sym;
0519         cs->flags |= SYMBOL_DEF_USER;
0520         prop = sym_get_choice_prop(cs);
0521         for (e = prop->expr; e; e = e->left.expr) {
0522             if (e->right.sym->visible != no)
0523                 e->right.sym->flags |= SYMBOL_DEF_USER;
0524         }
0525     }
0526 
0527     sym->def[S_DEF_USER].tri = val;
0528     if (oldval != val)
0529         sym_clear_all_valid();
0530 
0531     return true;
0532 }
0533 
0534 tristate sym_toggle_tristate_value(struct symbol *sym)
0535 {
0536     tristate oldval, newval;
0537 
0538     oldval = newval = sym_get_tristate_value(sym);
0539     do {
0540         switch (newval) {
0541         case no:
0542             newval = mod;
0543             break;
0544         case mod:
0545             newval = yes;
0546             break;
0547         case yes:
0548             newval = no;
0549             break;
0550         }
0551         if (sym_set_tristate_value(sym, newval))
0552             break;
0553     } while (oldval != newval);
0554     return newval;
0555 }
0556 
0557 bool sym_string_valid(struct symbol *sym, const char *str)
0558 {
0559     signed char ch;
0560 
0561     switch (sym->type) {
0562     case S_STRING:
0563         return true;
0564     case S_INT:
0565         ch = *str++;
0566         if (ch == '-')
0567             ch = *str++;
0568         if (!isdigit(ch))
0569             return false;
0570         if (ch == '0' && *str != 0)
0571             return false;
0572         while ((ch = *str++)) {
0573             if (!isdigit(ch))
0574                 return false;
0575         }
0576         return true;
0577     case S_HEX:
0578         if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
0579             str += 2;
0580         ch = *str++;
0581         do {
0582             if (!isxdigit(ch))
0583                 return false;
0584         } while ((ch = *str++));
0585         return true;
0586     case S_BOOLEAN:
0587     case S_TRISTATE:
0588         switch (str[0]) {
0589         case 'y': case 'Y':
0590         case 'm': case 'M':
0591         case 'n': case 'N':
0592             return true;
0593         }
0594         return false;
0595     default:
0596         return false;
0597     }
0598 }
0599 
0600 bool sym_string_within_range(struct symbol *sym, const char *str)
0601 {
0602     struct property *prop;
0603     long long val;
0604 
0605     switch (sym->type) {
0606     case S_STRING:
0607         return sym_string_valid(sym, str);
0608     case S_INT:
0609         if (!sym_string_valid(sym, str))
0610             return false;
0611         prop = sym_get_range_prop(sym);
0612         if (!prop)
0613             return true;
0614         val = strtoll(str, NULL, 10);
0615         return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
0616                val <= sym_get_range_val(prop->expr->right.sym, 10);
0617     case S_HEX:
0618         if (!sym_string_valid(sym, str))
0619             return false;
0620         prop = sym_get_range_prop(sym);
0621         if (!prop)
0622             return true;
0623         val = strtoll(str, NULL, 16);
0624         return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
0625                val <= sym_get_range_val(prop->expr->right.sym, 16);
0626     case S_BOOLEAN:
0627     case S_TRISTATE:
0628         switch (str[0]) {
0629         case 'y': case 'Y':
0630             return sym_tristate_within_range(sym, yes);
0631         case 'm': case 'M':
0632             return sym_tristate_within_range(sym, mod);
0633         case 'n': case 'N':
0634             return sym_tristate_within_range(sym, no);
0635         }
0636         return false;
0637     default:
0638         return false;
0639     }
0640 }
0641 
0642 bool sym_set_string_value(struct symbol *sym, const char *newval)
0643 {
0644     const char *oldval;
0645     char *val;
0646     int size;
0647 
0648     switch (sym->type) {
0649     case S_BOOLEAN:
0650     case S_TRISTATE:
0651         switch (newval[0]) {
0652         case 'y': case 'Y':
0653             return sym_set_tristate_value(sym, yes);
0654         case 'm': case 'M':
0655             return sym_set_tristate_value(sym, mod);
0656         case 'n': case 'N':
0657             return sym_set_tristate_value(sym, no);
0658         }
0659         return false;
0660     default:
0661         ;
0662     }
0663 
0664     if (!sym_string_within_range(sym, newval))
0665         return false;
0666 
0667     if (!(sym->flags & SYMBOL_DEF_USER)) {
0668         sym->flags |= SYMBOL_DEF_USER;
0669         sym_set_changed(sym);
0670     }
0671 
0672     oldval = sym->def[S_DEF_USER].val;
0673     size = strlen(newval) + 1;
0674     if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
0675         size += 2;
0676         sym->def[S_DEF_USER].val = val = xmalloc(size);
0677         *val++ = '0';
0678         *val++ = 'x';
0679     } else if (!oldval || strcmp(oldval, newval))
0680         sym->def[S_DEF_USER].val = val = xmalloc(size);
0681     else
0682         return true;
0683 
0684     strcpy(val, newval);
0685     free((void *)oldval);
0686     sym_clear_all_valid();
0687 
0688     return true;
0689 }
0690 
0691 /*
0692  * Find the default value associated to a symbol.
0693  * For tristate symbol handle the modules=n case
0694  * in which case "m" becomes "y".
0695  * If the symbol does not have any default then fallback
0696  * to the fixed default values.
0697  */
0698 const char *sym_get_string_default(struct symbol *sym)
0699 {
0700     struct property *prop;
0701     struct symbol *ds;
0702     const char *str;
0703     tristate val;
0704 
0705     sym_calc_visibility(sym);
0706     sym_calc_value(modules_sym);
0707     val = symbol_no.curr.tri;
0708     str = symbol_empty.curr.val;
0709 
0710     /* If symbol has a default value look it up */
0711     prop = sym_get_default_prop(sym);
0712     if (prop != NULL) {
0713         switch (sym->type) {
0714         case S_BOOLEAN:
0715         case S_TRISTATE:
0716             /* The visibility may limit the value from yes => mod */
0717             val = EXPR_AND(expr_calc_value(prop->expr), prop->visible.tri);
0718             break;
0719         default:
0720             /*
0721              * The following fails to handle the situation
0722              * where a default value is further limited by
0723              * the valid range.
0724              */
0725             ds = prop_get_symbol(prop);
0726             if (ds != NULL) {
0727                 sym_calc_value(ds);
0728                 str = (const char *)ds->curr.val;
0729             }
0730         }
0731     }
0732 
0733     /* Handle select statements */
0734     val = EXPR_OR(val, sym->rev_dep.tri);
0735 
0736     /* transpose mod to yes if modules are not enabled */
0737     if (val == mod)
0738         if (!sym_is_choice_value(sym) && modules_sym->curr.tri == no)
0739             val = yes;
0740 
0741     /* transpose mod to yes if type is bool */
0742     if (sym->type == S_BOOLEAN && val == mod)
0743         val = yes;
0744 
0745     /* adjust the default value if this symbol is implied by another */
0746     if (val < sym->implied.tri)
0747         val = sym->implied.tri;
0748 
0749     switch (sym->type) {
0750     case S_BOOLEAN:
0751     case S_TRISTATE:
0752         switch (val) {
0753         case no: return "n";
0754         case mod: return "m";
0755         case yes: return "y";
0756         }
0757     case S_INT:
0758     case S_HEX:
0759         return str;
0760     case S_STRING:
0761         return str;
0762     case S_UNKNOWN:
0763         break;
0764     }
0765     return "";
0766 }
0767 
0768 const char *sym_get_string_value(struct symbol *sym)
0769 {
0770     tristate val;
0771 
0772     switch (sym->type) {
0773     case S_BOOLEAN:
0774     case S_TRISTATE:
0775         val = sym_get_tristate_value(sym);
0776         switch (val) {
0777         case no:
0778             return "n";
0779         case mod:
0780             sym_calc_value(modules_sym);
0781             return (modules_sym->curr.tri == no) ? "n" : "m";
0782         case yes:
0783             return "y";
0784         }
0785         break;
0786     default:
0787         ;
0788     }
0789     return (const char *)sym->curr.val;
0790 }
0791 
0792 bool sym_is_changeable(struct symbol *sym)
0793 {
0794     return sym->visible > sym->rev_dep.tri;
0795 }
0796 
0797 static unsigned strhash(const char *s)
0798 {
0799     /* fnv32 hash */
0800     unsigned hash = 2166136261U;
0801     for (; *s; s++)
0802         hash = (hash ^ *s) * 0x01000193;
0803     return hash;
0804 }
0805 
0806 struct symbol *sym_lookup(const char *name, int flags)
0807 {
0808     struct symbol *symbol;
0809     char *new_name;
0810     int hash;
0811 
0812     if (name) {
0813         if (name[0] && !name[1]) {
0814             switch (name[0]) {
0815             case 'y': return &symbol_yes;
0816             case 'm': return &symbol_mod;
0817             case 'n': return &symbol_no;
0818             }
0819         }
0820         hash = strhash(name) % SYMBOL_HASHSIZE;
0821 
0822         for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
0823             if (symbol->name &&
0824                 !strcmp(symbol->name, name) &&
0825                 (flags ? symbol->flags & flags
0826                    : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE))))
0827                 return symbol;
0828         }
0829         new_name = xstrdup(name);
0830     } else {
0831         new_name = NULL;
0832         hash = 0;
0833     }
0834 
0835     symbol = xmalloc(sizeof(*symbol));
0836     memset(symbol, 0, sizeof(*symbol));
0837     symbol->name = new_name;
0838     symbol->type = S_UNKNOWN;
0839     symbol->flags = flags;
0840 
0841     symbol->next = symbol_hash[hash];
0842     symbol_hash[hash] = symbol;
0843 
0844     return symbol;
0845 }
0846 
0847 struct symbol *sym_find(const char *name)
0848 {
0849     struct symbol *symbol = NULL;
0850     int hash = 0;
0851 
0852     if (!name)
0853         return NULL;
0854 
0855     if (name[0] && !name[1]) {
0856         switch (name[0]) {
0857         case 'y': return &symbol_yes;
0858         case 'm': return &symbol_mod;
0859         case 'n': return &symbol_no;
0860         }
0861     }
0862     hash = strhash(name) % SYMBOL_HASHSIZE;
0863 
0864     for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
0865         if (symbol->name &&
0866             !strcmp(symbol->name, name) &&
0867             !(symbol->flags & SYMBOL_CONST))
0868                 break;
0869     }
0870 
0871     return symbol;
0872 }
0873 
0874 struct sym_match {
0875     struct symbol   *sym;
0876     off_t       so, eo;
0877 };
0878 
0879 /* Compare matched symbols as thus:
0880  * - first, symbols that match exactly
0881  * - then, alphabetical sort
0882  */
0883 static int sym_rel_comp(const void *sym1, const void *sym2)
0884 {
0885     const struct sym_match *s1 = sym1;
0886     const struct sym_match *s2 = sym2;
0887     int exact1, exact2;
0888 
0889     /* Exact match:
0890      * - if matched length on symbol s1 is the length of that symbol,
0891      *   then this symbol should come first;
0892      * - if matched length on symbol s2 is the length of that symbol,
0893      *   then this symbol should come first.
0894      * Note: since the search can be a regexp, both symbols may match
0895      * exactly; if this is the case, we can't decide which comes first,
0896      * and we fallback to sorting alphabetically.
0897      */
0898     exact1 = (s1->eo - s1->so) == strlen(s1->sym->name);
0899     exact2 = (s2->eo - s2->so) == strlen(s2->sym->name);
0900     if (exact1 && !exact2)
0901         return -1;
0902     if (!exact1 && exact2)
0903         return 1;
0904 
0905     /* As a fallback, sort symbols alphabetically */
0906     return strcmp(s1->sym->name, s2->sym->name);
0907 }
0908 
0909 struct symbol **sym_re_search(const char *pattern)
0910 {
0911     struct symbol *sym, **sym_arr = NULL;
0912     struct sym_match *sym_match_arr = NULL;
0913     int i, cnt, size;
0914     regex_t re;
0915     regmatch_t match[1];
0916 
0917     cnt = size = 0;
0918     /* Skip if empty */
0919     if (strlen(pattern) == 0)
0920         return NULL;
0921     if (regcomp(&re, pattern, REG_EXTENDED|REG_ICASE))
0922         return NULL;
0923 
0924     for_all_symbols(i, sym) {
0925         if (sym->flags & SYMBOL_CONST || !sym->name)
0926             continue;
0927         if (regexec(&re, sym->name, 1, match, 0))
0928             continue;
0929         if (cnt >= size) {
0930             void *tmp;
0931             size += 16;
0932             tmp = realloc(sym_match_arr, size * sizeof(struct sym_match));
0933             if (!tmp)
0934                 goto sym_re_search_free;
0935             sym_match_arr = tmp;
0936         }
0937         sym_calc_value(sym);
0938         /* As regexec returned 0, we know we have a match, so
0939          * we can use match[0].rm_[se]o without further checks
0940          */
0941         sym_match_arr[cnt].so = match[0].rm_so;
0942         sym_match_arr[cnt].eo = match[0].rm_eo;
0943         sym_match_arr[cnt++].sym = sym;
0944     }
0945     if (sym_match_arr) {
0946         qsort(sym_match_arr, cnt, sizeof(struct sym_match), sym_rel_comp);
0947         sym_arr = malloc((cnt+1) * sizeof(struct symbol *));
0948         if (!sym_arr)
0949             goto sym_re_search_free;
0950         for (i = 0; i < cnt; i++)
0951             sym_arr[i] = sym_match_arr[i].sym;
0952         sym_arr[cnt] = NULL;
0953     }
0954 sym_re_search_free:
0955     /* sym_match_arr can be NULL if no match, but free(NULL) is OK */
0956     free(sym_match_arr);
0957     regfree(&re);
0958 
0959     return sym_arr;
0960 }
0961 
0962 /*
0963  * When we check for recursive dependencies we use a stack to save
0964  * current state so we can print out relevant info to user.
0965  * The entries are located on the call stack so no need to free memory.
0966  * Note insert() remove() must always match to properly clear the stack.
0967  */
0968 static struct dep_stack {
0969     struct dep_stack *prev, *next;
0970     struct symbol *sym;
0971     struct property *prop;
0972     struct expr **expr;
0973 } *check_top;
0974 
0975 static void dep_stack_insert(struct dep_stack *stack, struct symbol *sym)
0976 {
0977     memset(stack, 0, sizeof(*stack));
0978     if (check_top)
0979         check_top->next = stack;
0980     stack->prev = check_top;
0981     stack->sym = sym;
0982     check_top = stack;
0983 }
0984 
0985 static void dep_stack_remove(void)
0986 {
0987     check_top = check_top->prev;
0988     if (check_top)
0989         check_top->next = NULL;
0990 }
0991 
0992 /*
0993  * Called when we have detected a recursive dependency.
0994  * check_top point to the top of the stact so we use
0995  * the ->prev pointer to locate the bottom of the stack.
0996  */
0997 static void sym_check_print_recursive(struct symbol *last_sym)
0998 {
0999     struct dep_stack *stack;
1000     struct symbol *sym, *next_sym;
1001     struct menu *menu = NULL;
1002     struct property *prop;
1003     struct dep_stack cv_stack;
1004 
1005     if (sym_is_choice_value(last_sym)) {
1006         dep_stack_insert(&cv_stack, last_sym);
1007         last_sym = prop_get_symbol(sym_get_choice_prop(last_sym));
1008     }
1009 
1010     for (stack = check_top; stack != NULL; stack = stack->prev)
1011         if (stack->sym == last_sym)
1012             break;
1013     if (!stack) {
1014         fprintf(stderr, "unexpected recursive dependency error\n");
1015         return;
1016     }
1017 
1018     for (; stack; stack = stack->next) {
1019         sym = stack->sym;
1020         next_sym = stack->next ? stack->next->sym : last_sym;
1021         prop = stack->prop;
1022         if (prop == NULL)
1023             prop = stack->sym->prop;
1024 
1025         /* for choice values find the menu entry (used below) */
1026         if (sym_is_choice(sym) || sym_is_choice_value(sym)) {
1027             for (prop = sym->prop; prop; prop = prop->next) {
1028                 menu = prop->menu;
1029                 if (prop->menu)
1030                     break;
1031             }
1032         }
1033         if (stack->sym == last_sym)
1034             fprintf(stderr, "%s:%d:error: recursive dependency detected!\n",
1035                 prop->file->name, prop->lineno);
1036 
1037         if (sym_is_choice(sym)) {
1038             fprintf(stderr, "%s:%d:\tchoice %s contains symbol %s\n",
1039                 menu->file->name, menu->lineno,
1040                 sym->name ? sym->name : "<choice>",
1041                 next_sym->name ? next_sym->name : "<choice>");
1042         } else if (sym_is_choice_value(sym)) {
1043             fprintf(stderr, "%s:%d:\tsymbol %s is part of choice %s\n",
1044                 menu->file->name, menu->lineno,
1045                 sym->name ? sym->name : "<choice>",
1046                 next_sym->name ? next_sym->name : "<choice>");
1047         } else if (stack->expr == &sym->dir_dep.expr) {
1048             fprintf(stderr, "%s:%d:\tsymbol %s depends on %s\n",
1049                 prop->file->name, prop->lineno,
1050                 sym->name ? sym->name : "<choice>",
1051                 next_sym->name ? next_sym->name : "<choice>");
1052         } else if (stack->expr == &sym->rev_dep.expr) {
1053             fprintf(stderr, "%s:%d:\tsymbol %s is selected by %s\n",
1054                 prop->file->name, prop->lineno,
1055                 sym->name ? sym->name : "<choice>",
1056                 next_sym->name ? next_sym->name : "<choice>");
1057         } else if (stack->expr == &sym->implied.expr) {
1058             fprintf(stderr, "%s:%d:\tsymbol %s is implied by %s\n",
1059                 prop->file->name, prop->lineno,
1060                 sym->name ? sym->name : "<choice>",
1061                 next_sym->name ? next_sym->name : "<choice>");
1062         } else if (stack->expr) {
1063             fprintf(stderr, "%s:%d:\tsymbol %s %s value contains %s\n",
1064                 prop->file->name, prop->lineno,
1065                 sym->name ? sym->name : "<choice>",
1066                 prop_get_type_name(prop->type),
1067                 next_sym->name ? next_sym->name : "<choice>");
1068         } else {
1069             fprintf(stderr, "%s:%d:\tsymbol %s %s is visible depending on %s\n",
1070                 prop->file->name, prop->lineno,
1071                 sym->name ? sym->name : "<choice>",
1072                 prop_get_type_name(prop->type),
1073                 next_sym->name ? next_sym->name : "<choice>");
1074         }
1075     }
1076 
1077     fprintf(stderr,
1078         "For a resolution refer to Documentation/kbuild/kconfig-language.rst\n"
1079         "subsection \"Kconfig recursive dependency limitations\"\n"
1080         "\n");
1081 
1082     if (check_top == &cv_stack)
1083         dep_stack_remove();
1084 }
1085 
1086 static struct symbol *sym_check_expr_deps(struct expr *e)
1087 {
1088     struct symbol *sym;
1089 
1090     if (!e)
1091         return NULL;
1092     switch (e->type) {
1093     case E_OR:
1094     case E_AND:
1095         sym = sym_check_expr_deps(e->left.expr);
1096         if (sym)
1097             return sym;
1098         return sym_check_expr_deps(e->right.expr);
1099     case E_NOT:
1100         return sym_check_expr_deps(e->left.expr);
1101     case E_EQUAL:
1102     case E_GEQ:
1103     case E_GTH:
1104     case E_LEQ:
1105     case E_LTH:
1106     case E_UNEQUAL:
1107         sym = sym_check_deps(e->left.sym);
1108         if (sym)
1109             return sym;
1110         return sym_check_deps(e->right.sym);
1111     case E_SYMBOL:
1112         return sym_check_deps(e->left.sym);
1113     default:
1114         break;
1115     }
1116     fprintf(stderr, "Oops! How to check %d?\n", e->type);
1117     return NULL;
1118 }
1119 
1120 /* return NULL when dependencies are OK */
1121 static struct symbol *sym_check_sym_deps(struct symbol *sym)
1122 {
1123     struct symbol *sym2;
1124     struct property *prop;
1125     struct dep_stack stack;
1126 
1127     dep_stack_insert(&stack, sym);
1128 
1129     stack.expr = &sym->dir_dep.expr;
1130     sym2 = sym_check_expr_deps(sym->dir_dep.expr);
1131     if (sym2)
1132         goto out;
1133 
1134     stack.expr = &sym->rev_dep.expr;
1135     sym2 = sym_check_expr_deps(sym->rev_dep.expr);
1136     if (sym2)
1137         goto out;
1138 
1139     stack.expr = &sym->implied.expr;
1140     sym2 = sym_check_expr_deps(sym->implied.expr);
1141     if (sym2)
1142         goto out;
1143 
1144     stack.expr = NULL;
1145 
1146     for (prop = sym->prop; prop; prop = prop->next) {
1147         if (prop->type == P_CHOICE || prop->type == P_SELECT ||
1148             prop->type == P_IMPLY)
1149             continue;
1150         stack.prop = prop;
1151         sym2 = sym_check_expr_deps(prop->visible.expr);
1152         if (sym2)
1153             break;
1154         if (prop->type != P_DEFAULT || sym_is_choice(sym))
1155             continue;
1156         stack.expr = &prop->expr;
1157         sym2 = sym_check_expr_deps(prop->expr);
1158         if (sym2)
1159             break;
1160         stack.expr = NULL;
1161     }
1162 
1163 out:
1164     dep_stack_remove();
1165 
1166     return sym2;
1167 }
1168 
1169 static struct symbol *sym_check_choice_deps(struct symbol *choice)
1170 {
1171     struct symbol *sym, *sym2;
1172     struct property *prop;
1173     struct expr *e;
1174     struct dep_stack stack;
1175 
1176     dep_stack_insert(&stack, choice);
1177 
1178     prop = sym_get_choice_prop(choice);
1179     expr_list_for_each_sym(prop->expr, e, sym)
1180         sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1181 
1182     choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1183     sym2 = sym_check_sym_deps(choice);
1184     choice->flags &= ~SYMBOL_CHECK;
1185     if (sym2)
1186         goto out;
1187 
1188     expr_list_for_each_sym(prop->expr, e, sym) {
1189         sym2 = sym_check_sym_deps(sym);
1190         if (sym2)
1191             break;
1192     }
1193 out:
1194     expr_list_for_each_sym(prop->expr, e, sym)
1195         sym->flags &= ~SYMBOL_CHECK;
1196 
1197     if (sym2 && sym_is_choice_value(sym2) &&
1198         prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
1199         sym2 = choice;
1200 
1201     dep_stack_remove();
1202 
1203     return sym2;
1204 }
1205 
1206 struct symbol *sym_check_deps(struct symbol *sym)
1207 {
1208     struct symbol *sym2;
1209     struct property *prop;
1210 
1211     if (sym->flags & SYMBOL_CHECK) {
1212         sym_check_print_recursive(sym);
1213         return sym;
1214     }
1215     if (sym->flags & SYMBOL_CHECKED)
1216         return NULL;
1217 
1218     if (sym_is_choice_value(sym)) {
1219         struct dep_stack stack;
1220 
1221         /* for choice groups start the check with main choice symbol */
1222         dep_stack_insert(&stack, sym);
1223         prop = sym_get_choice_prop(sym);
1224         sym2 = sym_check_deps(prop_get_symbol(prop));
1225         dep_stack_remove();
1226     } else if (sym_is_choice(sym)) {
1227         sym2 = sym_check_choice_deps(sym);
1228     } else {
1229         sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1230         sym2 = sym_check_sym_deps(sym);
1231         sym->flags &= ~SYMBOL_CHECK;
1232     }
1233 
1234     return sym2;
1235 }
1236 
1237 struct symbol *prop_get_symbol(struct property *prop)
1238 {
1239     if (prop->expr && (prop->expr->type == E_SYMBOL ||
1240                prop->expr->type == E_LIST))
1241         return prop->expr->left.sym;
1242     return NULL;
1243 }
1244 
1245 const char *prop_get_type_name(enum prop_type type)
1246 {
1247     switch (type) {
1248     case P_PROMPT:
1249         return "prompt";
1250     case P_COMMENT:
1251         return "comment";
1252     case P_MENU:
1253         return "menu";
1254     case P_DEFAULT:
1255         return "default";
1256     case P_CHOICE:
1257         return "choice";
1258     case P_SELECT:
1259         return "select";
1260     case P_IMPLY:
1261         return "imply";
1262     case P_RANGE:
1263         return "range";
1264     case P_SYMBOL:
1265         return "symbol";
1266     case P_UNKNOWN:
1267         break;
1268     }
1269     return "unknown";
1270 }