confdata.c 19 KB

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