confdata.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. /*
  2. * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
  3. * Released under the terms of the GNU GPL v2.0.
  4. */
  5. #include <sys/stat.h>
  6. #include <ctype.h>
  7. #include <fcntl.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <time.h>
  12. #include <unistd.h>
  13. #define LKC_DIRECT_LINK
  14. #include "lkc.h"
  15. static void conf_warning(const char *fmt, ...)
  16. __attribute__ ((format (printf, 1, 2)));
  17. static const char *conf_filename;
  18. static int conf_lineno, conf_warnings, conf_unsaved;
  19. const char conf_defname[] = "arch/$ARCH/defconfig";
  20. static void conf_warning(const char *fmt, ...)
  21. {
  22. va_list ap;
  23. va_start(ap, fmt);
  24. fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
  25. vfprintf(stderr, fmt, ap);
  26. fprintf(stderr, "\n");
  27. va_end(ap);
  28. conf_warnings++;
  29. }
  30. const char *conf_get_configname(void)
  31. {
  32. char *name = getenv("KCONFIG_CONFIG");
  33. return name ? name : ".config";
  34. }
  35. const char *conf_get_autoconfig_name(void)
  36. {
  37. char *name = getenv("KCONFIG_AUTOCONFIG");
  38. return name ? name : "include/config/auto.conf";
  39. }
  40. static char *conf_expand_value(const char *in)
  41. {
  42. struct symbol *sym;
  43. const char *src;
  44. static char res_value[SYMBOL_MAXLENGTH];
  45. char *dst, name[SYMBOL_MAXLENGTH];
  46. res_value[0] = 0;
  47. dst = name;
  48. while ((src = strchr(in, '$'))) {
  49. strncat(res_value, in, src - in);
  50. src++;
  51. dst = name;
  52. while (isalnum(*src) || *src == '_')
  53. *dst++ = *src++;
  54. *dst = 0;
  55. sym = sym_lookup(name, 0);
  56. sym_calc_value(sym);
  57. strcat(res_value, sym_get_string_value(sym));
  58. in = src;
  59. }
  60. strcat(res_value, in);
  61. return res_value;
  62. }
  63. char *conf_get_default_confname(void)
  64. {
  65. struct stat buf;
  66. static char fullname[PATH_MAX+1];
  67. char *env, *name;
  68. name = conf_expand_value(conf_defname);
  69. env = getenv(SRCTREE);
  70. if (env) {
  71. sprintf(fullname, "%s/%s", env, name);
  72. if (!stat(fullname, &buf))
  73. return fullname;
  74. }
  75. return name;
  76. }
  77. static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
  78. {
  79. char *p2;
  80. switch (sym->type) {
  81. case S_TRISTATE:
  82. if (p[0] == 'm') {
  83. sym->def[def].tri = mod;
  84. sym->flags |= def_flags;
  85. break;
  86. }
  87. case S_BOOLEAN:
  88. if (p[0] == 'y') {
  89. sym->def[def].tri = yes;
  90. sym->flags |= def_flags;
  91. break;
  92. }
  93. if (p[0] == 'n') {
  94. sym->def[def].tri = no;
  95. sym->flags |= def_flags;
  96. break;
  97. }
  98. conf_warning("symbol value '%s' invalid for %s", p, sym->name);
  99. break;
  100. case S_OTHER:
  101. if (*p != '"') {
  102. for (p2 = p; *p2 && !isspace(*p2); p2++)
  103. ;
  104. sym->type = S_STRING;
  105. goto done;
  106. }
  107. case S_STRING:
  108. if (*p++ != '"')
  109. break;
  110. for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
  111. if (*p2 == '"') {
  112. *p2 = 0;
  113. break;
  114. }
  115. memmove(p2, p2 + 1, strlen(p2));
  116. }
  117. if (!p2) {
  118. conf_warning("invalid string found");
  119. return 1;
  120. }
  121. case S_INT:
  122. case S_HEX:
  123. done:
  124. if (sym_string_valid(sym, p)) {
  125. sym->def[def].val = strdup(p);
  126. sym->flags |= def_flags;
  127. } else {
  128. conf_warning("symbol value '%s' invalid for %s", p, sym->name);
  129. return 1;
  130. }
  131. break;
  132. default:
  133. ;
  134. }
  135. return 0;
  136. }
  137. int conf_read_simple(const char *name, int def)
  138. {
  139. FILE *in = NULL;
  140. char line[1024];
  141. char *p, *p2;
  142. struct symbol *sym;
  143. int i, def_flags;
  144. if (name) {
  145. in = zconf_fopen(name);
  146. } else {
  147. struct property *prop;
  148. name = conf_get_configname();
  149. in = zconf_fopen(name);
  150. if (in)
  151. goto load;
  152. sym_add_change_count(1);
  153. if (!sym_defconfig_list)
  154. return 1;
  155. for_all_defaults(sym_defconfig_list, prop) {
  156. if (expr_calc_value(prop->visible.expr) == no ||
  157. prop->expr->type != E_SYMBOL)
  158. continue;
  159. name = conf_expand_value(prop->expr->left.sym->name);
  160. in = zconf_fopen(name);
  161. if (in) {
  162. printf(_("#\n"
  163. "# using defaults found in %s\n"
  164. "#\n"), name);
  165. goto load;
  166. }
  167. }
  168. }
  169. if (!in)
  170. return 1;
  171. load:
  172. conf_filename = name;
  173. conf_lineno = 0;
  174. conf_warnings = 0;
  175. conf_unsaved = 0;
  176. def_flags = SYMBOL_DEF << def;
  177. for_all_symbols(i, sym) {
  178. sym->flags |= SYMBOL_CHANGED;
  179. sym->flags &= ~(def_flags|SYMBOL_VALID);
  180. if (sym_is_choice(sym))
  181. sym->flags |= def_flags;
  182. switch (sym->type) {
  183. case S_INT:
  184. case S_HEX:
  185. case S_STRING:
  186. if (sym->def[def].val)
  187. free(sym->def[def].val);
  188. default:
  189. sym->def[def].val = NULL;
  190. sym->def[def].tri = no;
  191. }
  192. }
  193. while (fgets(line, sizeof(line), in)) {
  194. conf_lineno++;
  195. sym = NULL;
  196. switch (line[0]) {
  197. case '#':
  198. p = strchr(line + 2, ' ');
  199. if (!p)
  200. continue;
  201. *p++ = 0;
  202. if (strncmp(p, "is not set", 10))
  203. continue;
  204. if (def == S_DEF_USER) {
  205. sym = sym_find(line + 2);
  206. if (!sym) {
  207. sym_add_change_count(1);
  208. break;
  209. }
  210. } else {
  211. sym = sym_lookup(line + 2, 0);
  212. if (sym->type == S_UNKNOWN)
  213. sym->type = S_BOOLEAN;
  214. }
  215. if (sym->flags & def_flags) {
  216. conf_warning("override: reassigning to symbol %s", sym->name);
  217. }
  218. switch (sym->type) {
  219. case S_BOOLEAN:
  220. case S_TRISTATE:
  221. sym->def[def].tri = no;
  222. sym->flags |= def_flags;
  223. break;
  224. default:
  225. ;
  226. }
  227. break;
  228. default:
  229. p = strchr(line, '=');
  230. if (!p)
  231. continue;
  232. *p++ = 0;
  233. p2 = strchr(p, '\n');
  234. if (p2) {
  235. *p2-- = 0;
  236. if (*p2 == '\r')
  237. *p2 = 0;
  238. }
  239. if (def == S_DEF_USER) {
  240. sym = sym_find(line);
  241. if (!sym) {
  242. sym_add_change_count(1);
  243. break;
  244. }
  245. } else {
  246. sym = sym_lookup(line, 0);
  247. if (sym->type == S_UNKNOWN)
  248. sym->type = S_OTHER;
  249. }
  250. if (sym->flags & def_flags) {
  251. conf_warning("override: reassigning to symbol %s", sym->name);
  252. }
  253. if (conf_set_sym_val(sym, def, def_flags, p))
  254. continue;
  255. break;
  256. case '\r':
  257. case '\n':
  258. break;
  259. }
  260. if (sym && sym_is_choice_value(sym)) {
  261. struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
  262. switch (sym->def[def].tri) {
  263. case no:
  264. break;
  265. case mod:
  266. if (cs->def[def].tri == yes) {
  267. conf_warning("%s creates inconsistent choice state", sym->name);
  268. cs->flags &= ~def_flags;
  269. }
  270. break;
  271. case yes:
  272. if (cs->def[def].tri != no)
  273. conf_warning("override: %s changes choice state", sym->name);
  274. cs->def[def].val = sym;
  275. break;
  276. }
  277. cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
  278. }
  279. }
  280. fclose(in);
  281. if (modules_sym)
  282. sym_calc_value(modules_sym);
  283. return 0;
  284. }
  285. int conf_read(const char *name)
  286. {
  287. struct symbol *sym, *choice_sym;
  288. struct property *prop;
  289. struct expr *e;
  290. int i, flags;
  291. sym_set_change_count(0);
  292. if (conf_read_simple(name, S_DEF_USER))
  293. return 1;
  294. for_all_symbols(i, sym) {
  295. sym_calc_value(sym);
  296. if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
  297. goto sym_ok;
  298. if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
  299. /* check that calculated value agrees with saved value */
  300. switch (sym->type) {
  301. case S_BOOLEAN:
  302. case S_TRISTATE:
  303. if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
  304. break;
  305. if (!sym_is_choice(sym))
  306. goto sym_ok;
  307. default:
  308. if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
  309. goto sym_ok;
  310. break;
  311. }
  312. } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
  313. /* no previous value and not saved */
  314. goto sym_ok;
  315. conf_unsaved++;
  316. /* maybe print value in verbose mode... */
  317. sym_ok:
  318. if (!sym_is_choice(sym))
  319. continue;
  320. /* The choice symbol only has a set value (and thus is not new)
  321. * if all its visible childs have values.
  322. */
  323. prop = sym_get_choice_prop(sym);
  324. flags = sym->flags;
  325. expr_list_for_each_sym(prop->expr, e, choice_sym)
  326. if (choice_sym->visible != no)
  327. flags &= choice_sym->flags;
  328. sym->flags &= flags | ~SYMBOL_DEF_USER;
  329. }
  330. for_all_symbols(i, sym) {
  331. if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
  332. /* Reset values of generates values, so they'll appear
  333. * as new, if they should become visible, but that
  334. * doesn't quite work if the Kconfig and the saved
  335. * configuration disagree.
  336. */
  337. if (sym->visible == no && !conf_unsaved)
  338. sym->flags &= ~SYMBOL_DEF_USER;
  339. switch (sym->type) {
  340. case S_STRING:
  341. case S_INT:
  342. case S_HEX:
  343. /* Reset a string value if it's out of range */
  344. if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
  345. break;
  346. sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
  347. conf_unsaved++;
  348. break;
  349. default:
  350. break;
  351. }
  352. }
  353. }
  354. sym_add_change_count(conf_warnings || conf_unsaved);
  355. return 0;
  356. }
  357. int conf_write(const char *name)
  358. {
  359. FILE *out;
  360. struct symbol *sym;
  361. struct menu *menu;
  362. const char *basename;
  363. char dirname[128], tmpname[128], newname[128];
  364. int type, l;
  365. const char *str;
  366. time_t now;
  367. int use_timestamp = 1;
  368. char *env;
  369. dirname[0] = 0;
  370. if (name && name[0]) {
  371. struct stat st;
  372. char *slash;
  373. if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
  374. strcpy(dirname, name);
  375. strcat(dirname, "/");
  376. basename = conf_get_configname();
  377. } else if ((slash = strrchr(name, '/'))) {
  378. int size = slash - name + 1;
  379. memcpy(dirname, name, size);
  380. dirname[size] = 0;
  381. if (slash[1])
  382. basename = slash + 1;
  383. else
  384. basename = conf_get_configname();
  385. } else
  386. basename = name;
  387. } else
  388. basename = conf_get_configname();
  389. sprintf(newname, "%s%s", dirname, basename);
  390. env = getenv("KCONFIG_OVERWRITECONFIG");
  391. if (!env || !*env) {
  392. sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
  393. out = fopen(tmpname, "w");
  394. } else {
  395. *tmpname = 0;
  396. out = fopen(newname, "w");
  397. }
  398. if (!out)
  399. return 1;
  400. time(&now);
  401. env = getenv("KCONFIG_NOTIMESTAMP");
  402. if (env && *env)
  403. use_timestamp = 0;
  404. fprintf(out, _("#\n"
  405. "# Automatically generated make config: don't edit\n"
  406. "# OpenADK\n"
  407. "%s%s"
  408. "#\n"),
  409. use_timestamp ? "# " : "",
  410. use_timestamp ? ctime(&now) : "");
  411. if (!conf_get_changed())
  412. sym_clear_all_valid();
  413. menu = rootmenu.list;
  414. while (menu) {
  415. sym = menu->sym;
  416. if (!sym) {
  417. if (!menu_is_visible(menu))
  418. goto next;
  419. str = menu_get_prompt(menu);
  420. fprintf(out, "\n"
  421. "#\n"
  422. "# %s\n"
  423. "#\n", str);
  424. } else if (!(sym->flags & SYMBOL_CHOICE)) {
  425. sym_calc_value(sym);
  426. if (!(sym->flags & SYMBOL_WRITE))
  427. goto next;
  428. sym->flags &= ~SYMBOL_WRITE;
  429. type = sym->type;
  430. if (type == S_TRISTATE) {
  431. sym_calc_value(modules_sym);
  432. if (modules_sym->curr.tri == no)
  433. type = S_BOOLEAN;
  434. }
  435. switch (type) {
  436. case S_BOOLEAN:
  437. case S_TRISTATE:
  438. switch (sym_get_tristate_value(sym)) {
  439. case no:
  440. fprintf(out, "# %s is not set\n", sym->name);
  441. break;
  442. case mod:
  443. fprintf(out, "%s=m\n", sym->name);
  444. break;
  445. case yes:
  446. fprintf(out, "%s=y\n", sym->name);
  447. break;
  448. }
  449. break;
  450. case S_STRING:
  451. str = sym_get_string_value(sym);
  452. fprintf(out, "%s=\"", sym->name);
  453. while (1) {
  454. l = strcspn(str, "\"\\");
  455. if (l) {
  456. fwrite(str, l, 1, out);
  457. str += l;
  458. }
  459. if (!*str)
  460. break;
  461. fprintf(out, "\\%c", *str++);
  462. }
  463. fputs("\"\n", out);
  464. break;
  465. case S_HEX:
  466. str = sym_get_string_value(sym);
  467. if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
  468. fprintf(out, "%s=%s\n", sym->name, str);
  469. break;
  470. }
  471. case S_INT:
  472. str = sym_get_string_value(sym);
  473. fprintf(out, "%s=%s\n", sym->name, str);
  474. break;
  475. }
  476. }
  477. next:
  478. if (menu->list) {
  479. menu = menu->list;
  480. continue;
  481. }
  482. if (menu->next)
  483. menu = menu->next;
  484. else while ((menu = menu->parent)) {
  485. if (menu->next) {
  486. menu = menu->next;
  487. break;
  488. }
  489. }
  490. }
  491. fclose(out);
  492. if (*tmpname) {
  493. strcat(dirname, basename);
  494. strcat(dirname, ".old");
  495. rename(newname, dirname);
  496. if (rename(tmpname, newname))
  497. return 1;
  498. }
  499. sym_set_change_count(0);
  500. return 0;
  501. }
  502. int conf_split_config(void)
  503. {
  504. const char *name;
  505. char path[128];
  506. char *s, *d, c;
  507. struct symbol *sym;
  508. struct stat sb;
  509. int res, i, fd;
  510. name = conf_get_autoconfig_name();
  511. conf_read_simple(name, S_DEF_AUTO);
  512. if (chdir("include/config"))
  513. return 1;
  514. res = 0;
  515. for_all_symbols(i, sym) {
  516. sym_calc_value(sym);
  517. if ((sym->flags & SYMBOL_AUTO) || !sym->name)
  518. continue;
  519. if (sym->flags & SYMBOL_WRITE) {
  520. if (sym->flags & SYMBOL_DEF_AUTO) {
  521. /*
  522. * symbol has old and new value,
  523. * so compare them...
  524. */
  525. switch (sym->type) {
  526. case S_BOOLEAN:
  527. case S_TRISTATE:
  528. if (sym_get_tristate_value(sym) ==
  529. sym->def[S_DEF_AUTO].tri)
  530. continue;
  531. break;
  532. case S_STRING:
  533. case S_HEX:
  534. case S_INT:
  535. if (!strcmp(sym_get_string_value(sym),
  536. sym->def[S_DEF_AUTO].val))
  537. continue;
  538. break;
  539. default:
  540. break;
  541. }
  542. } else {
  543. /*
  544. * If there is no old value, only 'no' (unset)
  545. * is allowed as new value.
  546. */
  547. switch (sym->type) {
  548. case S_BOOLEAN:
  549. case S_TRISTATE:
  550. if (sym_get_tristate_value(sym) == no)
  551. continue;
  552. break;
  553. default:
  554. break;
  555. }
  556. }
  557. } else if (!(sym->flags & SYMBOL_DEF_AUTO))
  558. /* There is neither an old nor a new value. */
  559. continue;
  560. /* else
  561. * There is an old value, but no new value ('no' (unset)
  562. * isn't saved in auto.conf, so the old value is always
  563. * different from 'no').
  564. */
  565. /* Replace all '_' and append ".h" */
  566. s = sym->name;
  567. d = path;
  568. while ((c = *s++)) {
  569. c = tolower(c);
  570. *d++ = (c == '_') ? '/' : c;
  571. }
  572. strcpy(d, ".h");
  573. /* Assume directory path already exists. */
  574. fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  575. if (fd == -1) {
  576. if (errno != ENOENT) {
  577. res = 1;
  578. break;
  579. }
  580. /*
  581. * Create directory components,
  582. * unless they exist already.
  583. */
  584. d = path;
  585. while ((d = strchr(d, '/'))) {
  586. *d = 0;
  587. if (stat(path, &sb) && mkdir(path, 0755)) {
  588. res = 1;
  589. goto out;
  590. }
  591. *d++ = '/';
  592. }
  593. /* Try it again. */
  594. fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  595. if (fd == -1) {
  596. res = 1;
  597. break;
  598. }
  599. }
  600. close(fd);
  601. }
  602. out:
  603. if (chdir("../.."))
  604. return 1;
  605. return res;
  606. }
  607. int conf_write_autoconf(void)
  608. {
  609. struct symbol *sym;
  610. const char *str;
  611. const char *name;
  612. FILE *out, *out_h;
  613. time_t now;
  614. int i, l;
  615. sym_clear_all_valid();
  616. file_write_dep("include/config/auto.conf.cmd");
  617. if (conf_split_config())
  618. return 1;
  619. out = fopen(".tmpconfig", "w");
  620. if (!out)
  621. return 1;
  622. out_h = fopen(".tmpconfig.h", "w");
  623. if (!out_h) {
  624. fclose(out);
  625. return 1;
  626. }
  627. time(&now);
  628. fprintf(out, "#\n"
  629. "# Automatically generated make config: don't edit\n"
  630. "# OpenADK\n"
  631. "# %s"
  632. "#\n",
  633. ctime(&now));
  634. fprintf(out_h, "/*\n"
  635. " * Automatically generated C config: don't edit\n"
  636. " * OpenADK\n"
  637. " * %s"
  638. " */\n"
  639. "#define AUTOCONF_INCLUDED\n",
  640. ctime(&now));
  641. for_all_symbols(i, sym) {
  642. sym_calc_value(sym);
  643. if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
  644. continue;
  645. switch (sym->type) {
  646. case S_BOOLEAN:
  647. case S_TRISTATE:
  648. switch (sym_get_tristate_value(sym)) {
  649. case no:
  650. break;
  651. case mod:
  652. fprintf(out, "%s=m\n", sym->name);
  653. fprintf(out_h, "#define %s_MODULE 1\n", sym->name);
  654. break;
  655. case yes:
  656. fprintf(out, "%s=y\n", sym->name);
  657. fprintf(out_h, "#define %s 1\n", sym->name);
  658. break;
  659. }
  660. break;
  661. case S_STRING:
  662. str = sym_get_string_value(sym);
  663. fprintf(out, "%s=\"", sym->name);
  664. fprintf(out_h, "#define %s \"", sym->name);
  665. while (1) {
  666. l = strcspn(str, "\"\\");
  667. if (l) {
  668. fwrite(str, l, 1, out);
  669. fwrite(str, l, 1, out_h);
  670. str += l;
  671. }
  672. if (!*str)
  673. break;
  674. fprintf(out, "\\%c", *str);
  675. fprintf(out_h, "\\%c", *str);
  676. str++;
  677. }
  678. fputs("\"\n", out);
  679. fputs("\"\n", out_h);
  680. break;
  681. case S_HEX:
  682. str = sym_get_string_value(sym);
  683. if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
  684. fprintf(out, "%s=%s\n", sym->name, str);
  685. fprintf(out_h, "#define %s 0x%s\n", sym->name, str);
  686. break;
  687. }
  688. case S_INT:
  689. str = sym_get_string_value(sym);
  690. fprintf(out, "%s=%s\n", sym->name, str);
  691. fprintf(out_h, "#define %s %s\n", sym->name, str);
  692. break;
  693. default:
  694. break;
  695. }
  696. }
  697. fclose(out);
  698. fclose(out_h);
  699. name = getenv("KCONFIG_AUTOHEADER");
  700. if (!name)
  701. name = "include/linux/autoconf.h";
  702. if (rename(".tmpconfig.h", name))
  703. return 1;
  704. name = conf_get_autoconfig_name();
  705. /*
  706. * This must be the last step, kbuild has a dependency on auto.conf
  707. * and this marks the successful completion of the previous steps.
  708. */
  709. if (rename(".tmpconfig", name))
  710. return 1;
  711. return 0;
  712. }
  713. static int sym_change_count;
  714. static void (*conf_changed_callback)(void);
  715. void sym_set_change_count(int count)
  716. {
  717. int _sym_change_count = sym_change_count;
  718. sym_change_count = count;
  719. if (conf_changed_callback &&
  720. (bool)_sym_change_count != (bool)count)
  721. conf_changed_callback();
  722. }
  723. void sym_add_change_count(int count)
  724. {
  725. sym_set_change_count(count + sym_change_count);
  726. }
  727. bool conf_get_changed(void)
  728. {
  729. return sym_change_count;
  730. }
  731. void conf_set_changed_callback(void (*fn)(void))
  732. {
  733. conf_changed_callback = fn;
  734. }
  735. void conf_set_all_new_symbols(enum conf_def_mode mode)
  736. {
  737. struct symbol *sym, *csym;
  738. struct property *prop;
  739. struct expr *e;
  740. int i, cnt, def;
  741. for_all_symbols(i, sym) {
  742. if (sym_has_value(sym))
  743. continue;
  744. switch (sym_get_type(sym)) {
  745. case S_BOOLEAN:
  746. case S_TRISTATE:
  747. switch (mode) {
  748. case def_yes:
  749. sym->def[S_DEF_USER].tri = yes;
  750. break;
  751. case def_mod:
  752. sym->def[S_DEF_USER].tri = mod;
  753. break;
  754. case def_no:
  755. sym->def[S_DEF_USER].tri = no;
  756. break;
  757. case def_random:
  758. sym->def[S_DEF_USER].tri = (tristate)(rand() % 3);
  759. break;
  760. default:
  761. continue;
  762. }
  763. if (!(sym_is_choice(sym) && mode == def_random))
  764. sym->flags |= SYMBOL_DEF_USER;
  765. break;
  766. default:
  767. break;
  768. }
  769. }
  770. sym_clear_all_valid();
  771. if (mode != def_random)
  772. return;
  773. /*
  774. * We have different type of choice blocks.
  775. * If curr.tri equal to mod then we can select several
  776. * choice symbols in one block.
  777. * In this case we do nothing.
  778. * If curr.tri equal yes then only one symbol can be
  779. * selected in a choice block and we set it to yes,
  780. * and the rest to no.
  781. */
  782. for_all_symbols(i, csym) {
  783. if (sym_has_value(csym) || !sym_is_choice(csym))
  784. continue;
  785. sym_calc_value(csym);
  786. if (csym->curr.tri != yes)
  787. continue;
  788. prop = sym_get_choice_prop(csym);
  789. /* count entries in choice block */
  790. cnt = 0;
  791. expr_list_for_each_sym(prop->expr, e, sym)
  792. cnt++;
  793. /*
  794. * find a random value and set it to yes,
  795. * set the rest to no so we have only one set
  796. */
  797. def = (rand() % cnt);
  798. cnt = 0;
  799. expr_list_for_each_sym(prop->expr, e, sym) {
  800. if (def == cnt++) {
  801. sym->def[S_DEF_USER].tri = yes;
  802. csym->def[S_DEF_USER].val = sym;
  803. }
  804. else {
  805. sym->def[S_DEF_USER].tri = no;
  806. }
  807. }
  808. csym->flags |= SYMBOL_DEF_USER;
  809. /* clear VALID to get value calculated */
  810. csym->flags &= ~(SYMBOL_VALID);
  811. }
  812. }