confdata.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  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. printf(_("#\n"
  500. "# configuration written to %s\n"
  501. "#\n"), newname);
  502. sym_set_change_count(0);
  503. return 0;
  504. }
  505. int conf_split_config(void)
  506. {
  507. const char *name;
  508. char path[128];
  509. char *s, *d, c;
  510. struct symbol *sym;
  511. struct stat sb;
  512. int res, i, fd;
  513. name = conf_get_autoconfig_name();
  514. conf_read_simple(name, S_DEF_AUTO);
  515. if (chdir("include/config"))
  516. return 1;
  517. res = 0;
  518. for_all_symbols(i, sym) {
  519. sym_calc_value(sym);
  520. if ((sym->flags & SYMBOL_AUTO) || !sym->name)
  521. continue;
  522. if (sym->flags & SYMBOL_WRITE) {
  523. if (sym->flags & SYMBOL_DEF_AUTO) {
  524. /*
  525. * symbol has old and new value,
  526. * so compare them...
  527. */
  528. switch (sym->type) {
  529. case S_BOOLEAN:
  530. case S_TRISTATE:
  531. if (sym_get_tristate_value(sym) ==
  532. sym->def[S_DEF_AUTO].tri)
  533. continue;
  534. break;
  535. case S_STRING:
  536. case S_HEX:
  537. case S_INT:
  538. if (!strcmp(sym_get_string_value(sym),
  539. sym->def[S_DEF_AUTO].val))
  540. continue;
  541. break;
  542. default:
  543. break;
  544. }
  545. } else {
  546. /*
  547. * If there is no old value, only 'no' (unset)
  548. * is allowed as new value.
  549. */
  550. switch (sym->type) {
  551. case S_BOOLEAN:
  552. case S_TRISTATE:
  553. if (sym_get_tristate_value(sym) == no)
  554. continue;
  555. break;
  556. default:
  557. break;
  558. }
  559. }
  560. } else if (!(sym->flags & SYMBOL_DEF_AUTO))
  561. /* There is neither an old nor a new value. */
  562. continue;
  563. /* else
  564. * There is an old value, but no new value ('no' (unset)
  565. * isn't saved in auto.conf, so the old value is always
  566. * different from 'no').
  567. */
  568. /* Replace all '_' and append ".h" */
  569. s = sym->name;
  570. d = path;
  571. while ((c = *s++)) {
  572. c = tolower(c);
  573. *d++ = (c == '_') ? '/' : c;
  574. }
  575. strcpy(d, ".h");
  576. /* Assume directory path already exists. */
  577. fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  578. if (fd == -1) {
  579. if (errno != ENOENT) {
  580. res = 1;
  581. break;
  582. }
  583. /*
  584. * Create directory components,
  585. * unless they exist already.
  586. */
  587. d = path;
  588. while ((d = strchr(d, '/'))) {
  589. *d = 0;
  590. if (stat(path, &sb) && mkdir(path, 0755)) {
  591. res = 1;
  592. goto out;
  593. }
  594. *d++ = '/';
  595. }
  596. /* Try it again. */
  597. fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  598. if (fd == -1) {
  599. res = 1;
  600. break;
  601. }
  602. }
  603. close(fd);
  604. }
  605. out:
  606. if (chdir("../.."))
  607. return 1;
  608. return res;
  609. }
  610. int conf_write_autoconf(void)
  611. {
  612. struct symbol *sym;
  613. const char *str;
  614. const char *name;
  615. FILE *out, *out_h;
  616. time_t now;
  617. int i, l;
  618. sym_clear_all_valid();
  619. file_write_dep("include/config/auto.conf.cmd");
  620. if (conf_split_config())
  621. return 1;
  622. out = fopen(".tmpconfig", "w");
  623. if (!out)
  624. return 1;
  625. out_h = fopen(".tmpconfig.h", "w");
  626. if (!out_h) {
  627. fclose(out);
  628. return 1;
  629. }
  630. time(&now);
  631. fprintf(out, "#\n"
  632. "# Automatically generated make config: don't edit\n"
  633. "# OpenADK\n"
  634. "# %s"
  635. "#\n",
  636. ctime(&now));
  637. fprintf(out_h, "/*\n"
  638. " * Automatically generated C config: don't edit\n"
  639. " * OpenADK\n"
  640. " * %s"
  641. " */\n"
  642. "#define AUTOCONF_INCLUDED\n",
  643. ctime(&now));
  644. for_all_symbols(i, sym) {
  645. sym_calc_value(sym);
  646. if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
  647. continue;
  648. switch (sym->type) {
  649. case S_BOOLEAN:
  650. case S_TRISTATE:
  651. switch (sym_get_tristate_value(sym)) {
  652. case no:
  653. break;
  654. case mod:
  655. fprintf(out, "%s=m\n", sym->name);
  656. fprintf(out_h, "#define %s_MODULE 1\n", sym->name);
  657. break;
  658. case yes:
  659. fprintf(out, "%s=y\n", sym->name);
  660. fprintf(out_h, "#define %s 1\n", sym->name);
  661. break;
  662. }
  663. break;
  664. case S_STRING:
  665. str = sym_get_string_value(sym);
  666. fprintf(out, "%s=\"", sym->name);
  667. fprintf(out_h, "#define %s \"", sym->name);
  668. while (1) {
  669. l = strcspn(str, "\"\\");
  670. if (l) {
  671. fwrite(str, l, 1, out);
  672. fwrite(str, l, 1, out_h);
  673. str += l;
  674. }
  675. if (!*str)
  676. break;
  677. fprintf(out, "\\%c", *str);
  678. fprintf(out_h, "\\%c", *str);
  679. str++;
  680. }
  681. fputs("\"\n", out);
  682. fputs("\"\n", out_h);
  683. break;
  684. case S_HEX:
  685. str = sym_get_string_value(sym);
  686. if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
  687. fprintf(out, "%s=%s\n", sym->name, str);
  688. fprintf(out_h, "#define %s 0x%s\n", sym->name, str);
  689. break;
  690. }
  691. case S_INT:
  692. str = sym_get_string_value(sym);
  693. fprintf(out, "%s=%s\n", sym->name, str);
  694. fprintf(out_h, "#define %s %s\n", sym->name, str);
  695. break;
  696. default:
  697. break;
  698. }
  699. }
  700. fclose(out);
  701. fclose(out_h);
  702. name = getenv("KCONFIG_AUTOHEADER");
  703. if (!name)
  704. name = "include/linux/autoconf.h";
  705. if (rename(".tmpconfig.h", name))
  706. return 1;
  707. name = conf_get_autoconfig_name();
  708. /*
  709. * This must be the last step, kbuild has a dependency on auto.conf
  710. * and this marks the successful completion of the previous steps.
  711. */
  712. if (rename(".tmpconfig", name))
  713. return 1;
  714. return 0;
  715. }
  716. static int sym_change_count;
  717. static void (*conf_changed_callback)(void);
  718. void sym_set_change_count(int count)
  719. {
  720. int _sym_change_count = sym_change_count;
  721. sym_change_count = count;
  722. if (conf_changed_callback &&
  723. (bool)_sym_change_count != (bool)count)
  724. conf_changed_callback();
  725. }
  726. void sym_add_change_count(int count)
  727. {
  728. sym_set_change_count(count + sym_change_count);
  729. }
  730. bool conf_get_changed(void)
  731. {
  732. return sym_change_count;
  733. }
  734. void conf_set_changed_callback(void (*fn)(void))
  735. {
  736. conf_changed_callback = fn;
  737. }
  738. void conf_set_all_new_symbols(enum conf_def_mode mode)
  739. {
  740. struct symbol *sym, *csym;
  741. struct property *prop;
  742. struct expr *e;
  743. int i, cnt, def;
  744. for_all_symbols(i, sym) {
  745. if (sym_has_value(sym))
  746. continue;
  747. switch (sym_get_type(sym)) {
  748. case S_BOOLEAN:
  749. case S_TRISTATE:
  750. switch (mode) {
  751. case def_yes:
  752. sym->def[S_DEF_USER].tri = yes;
  753. break;
  754. case def_mod:
  755. sym->def[S_DEF_USER].tri = mod;
  756. break;
  757. case def_no:
  758. sym->def[S_DEF_USER].tri = no;
  759. break;
  760. case def_random:
  761. sym->def[S_DEF_USER].tri = (tristate)(rand() % 3);
  762. break;
  763. default:
  764. continue;
  765. }
  766. if (!(sym_is_choice(sym) && mode == def_random))
  767. sym->flags |= SYMBOL_DEF_USER;
  768. break;
  769. default:
  770. break;
  771. }
  772. }
  773. sym_clear_all_valid();
  774. if (mode != def_random)
  775. return;
  776. /*
  777. * We have different type of choice blocks.
  778. * If curr.tri equal to mod then we can select several
  779. * choice symbols in one block.
  780. * In this case we do nothing.
  781. * If curr.tri equal yes then only one symbol can be
  782. * selected in a choice block and we set it to yes,
  783. * and the rest to no.
  784. */
  785. for_all_symbols(i, csym) {
  786. if (sym_has_value(csym) || !sym_is_choice(csym))
  787. continue;
  788. sym_calc_value(csym);
  789. if (csym->curr.tri != yes)
  790. continue;
  791. prop = sym_get_choice_prop(csym);
  792. /* count entries in choice block */
  793. cnt = 0;
  794. expr_list_for_each_sym(prop->expr, e, sym)
  795. cnt++;
  796. /*
  797. * find a random value and set it to yes,
  798. * set the rest to no so we have only one set
  799. */
  800. def = (rand() % cnt);
  801. cnt = 0;
  802. expr_list_for_each_sym(prop->expr, e, sym) {
  803. if (def == cnt++) {
  804. sym->def[S_DEF_USER].tri = yes;
  805. csym->def[S_DEF_USER].val = sym;
  806. }
  807. else {
  808. sym->def[S_DEF_USER].tri = no;
  809. }
  810. }
  811. csym->flags |= SYMBOL_DEF_USER;
  812. /* clear VALID to get value calculated */
  813. csym->flags &= ~(SYMBOL_VALID);
  814. }
  815. }