confdata.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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. if (memcmp(line + 2, "CONFIG_", 7))
  199. continue;
  200. p = strchr(line + 9, ' ');
  201. if (!p)
  202. continue;
  203. *p++ = 0;
  204. if (strncmp(p, "is not set", 10))
  205. continue;
  206. if (def == S_DEF_USER) {
  207. sym = sym_find(line + 9);
  208. if (!sym) {
  209. sym_add_change_count(1);
  210. break;
  211. }
  212. } else {
  213. sym = sym_lookup(line + 9, 0);
  214. if (sym->type == S_UNKNOWN)
  215. sym->type = S_BOOLEAN;
  216. }
  217. if (sym->flags & def_flags) {
  218. conf_warning("override: reassigning to symbol %s", sym->name);
  219. }
  220. switch (sym->type) {
  221. case S_BOOLEAN:
  222. case S_TRISTATE:
  223. sym->def[def].tri = no;
  224. sym->flags |= def_flags;
  225. break;
  226. default:
  227. ;
  228. }
  229. break;
  230. case 'C':
  231. if (memcmp(line, "CONFIG_", 7)) {
  232. conf_warning("unexpected data");
  233. continue;
  234. }
  235. p = strchr(line + 7, '=');
  236. if (!p)
  237. continue;
  238. *p++ = 0;
  239. p2 = strchr(p, '\n');
  240. if (p2) {
  241. *p2-- = 0;
  242. if (*p2 == '\r')
  243. *p2 = 0;
  244. }
  245. if (def == S_DEF_USER) {
  246. sym = sym_find(line + 7);
  247. if (!sym) {
  248. sym_add_change_count(1);
  249. break;
  250. }
  251. } else {
  252. sym = sym_lookup(line + 7, 0);
  253. if (sym->type == S_UNKNOWN)
  254. sym->type = S_OTHER;
  255. }
  256. if (sym->flags & def_flags) {
  257. conf_warning("override: reassigning to symbol %s", sym->name);
  258. }
  259. if (conf_set_sym_val(sym, def, def_flags, p))
  260. continue;
  261. break;
  262. case '\r':
  263. case '\n':
  264. break;
  265. default:
  266. conf_warning("unexpected data");
  267. continue;
  268. }
  269. if (sym && sym_is_choice_value(sym)) {
  270. struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
  271. switch (sym->def[def].tri) {
  272. case no:
  273. break;
  274. case mod:
  275. if (cs->def[def].tri == yes) {
  276. conf_warning("%s creates inconsistent choice state", sym->name);
  277. cs->flags &= ~def_flags;
  278. }
  279. break;
  280. case yes:
  281. if (cs->def[def].tri != no)
  282. conf_warning("override: %s changes choice state", sym->name);
  283. cs->def[def].val = sym;
  284. break;
  285. }
  286. cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
  287. }
  288. }
  289. fclose(in);
  290. if (modules_sym)
  291. sym_calc_value(modules_sym);
  292. return 0;
  293. }
  294. int conf_read(const char *name)
  295. {
  296. struct symbol *sym, *choice_sym;
  297. struct property *prop;
  298. struct expr *e;
  299. int i, flags;
  300. sym_set_change_count(0);
  301. if (conf_read_simple(name, S_DEF_USER))
  302. return 1;
  303. for_all_symbols(i, sym) {
  304. sym_calc_value(sym);
  305. if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
  306. goto sym_ok;
  307. if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
  308. /* check that calculated value agrees with saved value */
  309. switch (sym->type) {
  310. case S_BOOLEAN:
  311. case S_TRISTATE:
  312. if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
  313. break;
  314. if (!sym_is_choice(sym))
  315. goto sym_ok;
  316. default:
  317. if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
  318. goto sym_ok;
  319. break;
  320. }
  321. } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
  322. /* no previous value and not saved */
  323. goto sym_ok;
  324. conf_unsaved++;
  325. /* maybe print value in verbose mode... */
  326. sym_ok:
  327. if (!sym_is_choice(sym))
  328. continue;
  329. /* The choice symbol only has a set value (and thus is not new)
  330. * if all its visible childs have values.
  331. */
  332. prop = sym_get_choice_prop(sym);
  333. flags = sym->flags;
  334. expr_list_for_each_sym(prop->expr, e, choice_sym)
  335. if (choice_sym->visible != no)
  336. flags &= choice_sym->flags;
  337. sym->flags &= flags | ~SYMBOL_DEF_USER;
  338. }
  339. for_all_symbols(i, sym) {
  340. if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
  341. /* Reset values of generates values, so they'll appear
  342. * as new, if they should become visible, but that
  343. * doesn't quite work if the Kconfig and the saved
  344. * configuration disagree.
  345. */
  346. if (sym->visible == no && !conf_unsaved)
  347. sym->flags &= ~SYMBOL_DEF_USER;
  348. switch (sym->type) {
  349. case S_STRING:
  350. case S_INT:
  351. case S_HEX:
  352. /* Reset a string value if it's out of range */
  353. if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
  354. break;
  355. sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
  356. conf_unsaved++;
  357. break;
  358. default:
  359. break;
  360. }
  361. }
  362. }
  363. sym_add_change_count(conf_warnings || conf_unsaved);
  364. return 0;
  365. }
  366. int conf_write(const char *name)
  367. {
  368. FILE *out;
  369. struct symbol *sym;
  370. struct menu *menu;
  371. const char *basename;
  372. char dirname[128], tmpname[128], newname[128];
  373. int type, l;
  374. const char *str;
  375. time_t now;
  376. int use_timestamp = 1;
  377. char *env;
  378. dirname[0] = 0;
  379. if (name && name[0]) {
  380. struct stat st;
  381. char *slash;
  382. if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
  383. strcpy(dirname, name);
  384. strcat(dirname, "/");
  385. basename = conf_get_configname();
  386. } else if ((slash = strrchr(name, '/'))) {
  387. int size = slash - name + 1;
  388. memcpy(dirname, name, size);
  389. dirname[size] = 0;
  390. if (slash[1])
  391. basename = slash + 1;
  392. else
  393. basename = conf_get_configname();
  394. } else
  395. basename = name;
  396. } else
  397. basename = conf_get_configname();
  398. sprintf(newname, "%s%s", dirname, basename);
  399. env = getenv("KCONFIG_OVERWRITECONFIG");
  400. if (!env || !*env) {
  401. sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
  402. out = fopen(tmpname, "w");
  403. } else {
  404. *tmpname = 0;
  405. out = fopen(newname, "w");
  406. }
  407. if (!out)
  408. return 1;
  409. sym = sym_lookup("KERNELVERSION", 0);
  410. sym_calc_value(sym);
  411. time(&now);
  412. env = getenv("KCONFIG_NOTIMESTAMP");
  413. if (env && *env)
  414. use_timestamp = 0;
  415. fprintf(out, _("#\n"
  416. "# Automatically generated make config: don't edit\n"
  417. "# Linux kernel version: %s\n"
  418. "%s%s"
  419. "#\n"),
  420. sym_get_string_value(sym),
  421. use_timestamp ? "# " : "",
  422. use_timestamp ? ctime(&now) : "");
  423. if (!conf_get_changed())
  424. sym_clear_all_valid();
  425. menu = rootmenu.list;
  426. while (menu) {
  427. sym = menu->sym;
  428. if (!sym) {
  429. if (!menu_is_visible(menu))
  430. goto next;
  431. str = menu_get_prompt(menu);
  432. fprintf(out, "\n"
  433. "#\n"
  434. "# %s\n"
  435. "#\n", str);
  436. } else if (!(sym->flags & SYMBOL_CHOICE)) {
  437. sym_calc_value(sym);
  438. if (!(sym->flags & SYMBOL_WRITE))
  439. goto next;
  440. sym->flags &= ~SYMBOL_WRITE;
  441. type = sym->type;
  442. if (type == S_TRISTATE) {
  443. sym_calc_value(modules_sym);
  444. if (modules_sym->curr.tri == no)
  445. type = S_BOOLEAN;
  446. }
  447. switch (type) {
  448. case S_BOOLEAN:
  449. case S_TRISTATE:
  450. switch (sym_get_tristate_value(sym)) {
  451. case no:
  452. fprintf(out, "# CONFIG_%s is not set\n", sym->name);
  453. break;
  454. case mod:
  455. fprintf(out, "CONFIG_%s=m\n", sym->name);
  456. break;
  457. case yes:
  458. fprintf(out, "CONFIG_%s=y\n", sym->name);
  459. break;
  460. }
  461. break;
  462. case S_STRING:
  463. str = sym_get_string_value(sym);
  464. fprintf(out, "CONFIG_%s=\"", sym->name);
  465. while (1) {
  466. l = strcspn(str, "\"\\");
  467. if (l) {
  468. fwrite(str, l, 1, out);
  469. str += l;
  470. }
  471. if (!*str)
  472. break;
  473. fprintf(out, "\\%c", *str++);
  474. }
  475. fputs("\"\n", out);
  476. break;
  477. case S_HEX:
  478. str = sym_get_string_value(sym);
  479. if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
  480. fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
  481. break;
  482. }
  483. case S_INT:
  484. str = sym_get_string_value(sym);
  485. fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
  486. break;
  487. }
  488. }
  489. next:
  490. if (menu->list) {
  491. menu = menu->list;
  492. continue;
  493. }
  494. if (menu->next)
  495. menu = menu->next;
  496. else while ((menu = menu->parent)) {
  497. if (menu->next) {
  498. menu = menu->next;
  499. break;
  500. }
  501. }
  502. }
  503. fclose(out);
  504. if (*tmpname) {
  505. strcat(dirname, basename);
  506. strcat(dirname, ".old");
  507. rename(newname, dirname);
  508. if (rename(tmpname, newname))
  509. return 1;
  510. }
  511. printf(_("#\n"
  512. "# configuration written to %s\n"
  513. "#\n"), newname);
  514. sym_set_change_count(0);
  515. return 0;
  516. }
  517. int conf_split_config(void)
  518. {
  519. const char *name;
  520. char path[128];
  521. char *s, *d, c;
  522. struct symbol *sym;
  523. struct stat sb;
  524. int res, i, fd;
  525. name = conf_get_autoconfig_name();
  526. conf_read_simple(name, S_DEF_AUTO);
  527. if (chdir("include/config"))
  528. return 1;
  529. res = 0;
  530. for_all_symbols(i, sym) {
  531. sym_calc_value(sym);
  532. if ((sym->flags & SYMBOL_AUTO) || !sym->name)
  533. continue;
  534. if (sym->flags & SYMBOL_WRITE) {
  535. if (sym->flags & SYMBOL_DEF_AUTO) {
  536. /*
  537. * symbol has old and new value,
  538. * so compare them...
  539. */
  540. switch (sym->type) {
  541. case S_BOOLEAN:
  542. case S_TRISTATE:
  543. if (sym_get_tristate_value(sym) ==
  544. sym->def[S_DEF_AUTO].tri)
  545. continue;
  546. break;
  547. case S_STRING:
  548. case S_HEX:
  549. case S_INT:
  550. if (!strcmp(sym_get_string_value(sym),
  551. sym->def[S_DEF_AUTO].val))
  552. continue;
  553. break;
  554. default:
  555. break;
  556. }
  557. } else {
  558. /*
  559. * If there is no old value, only 'no' (unset)
  560. * is allowed as new value.
  561. */
  562. switch (sym->type) {
  563. case S_BOOLEAN:
  564. case S_TRISTATE:
  565. if (sym_get_tristate_value(sym) == no)
  566. continue;
  567. break;
  568. default:
  569. break;
  570. }
  571. }
  572. } else if (!(sym->flags & SYMBOL_DEF_AUTO))
  573. /* There is neither an old nor a new value. */
  574. continue;
  575. /* else
  576. * There is an old value, but no new value ('no' (unset)
  577. * isn't saved in auto.conf, so the old value is always
  578. * different from 'no').
  579. */
  580. /* Replace all '_' and append ".h" */
  581. s = sym->name;
  582. d = path;
  583. while ((c = *s++)) {
  584. c = tolower(c);
  585. *d++ = (c == '_') ? '/' : c;
  586. }
  587. strcpy(d, ".h");
  588. /* Assume directory path already exists. */
  589. fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  590. if (fd == -1) {
  591. if (errno != ENOENT) {
  592. res = 1;
  593. break;
  594. }
  595. /*
  596. * Create directory components,
  597. * unless they exist already.
  598. */
  599. d = path;
  600. while ((d = strchr(d, '/'))) {
  601. *d = 0;
  602. if (stat(path, &sb) && mkdir(path, 0755)) {
  603. res = 1;
  604. goto out;
  605. }
  606. *d++ = '/';
  607. }
  608. /* Try it again. */
  609. fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  610. if (fd == -1) {
  611. res = 1;
  612. break;
  613. }
  614. }
  615. close(fd);
  616. }
  617. out:
  618. if (chdir("../.."))
  619. return 1;
  620. return res;
  621. }
  622. int conf_write_autoconf(void)
  623. {
  624. struct symbol *sym;
  625. const char *str;
  626. const char *name;
  627. FILE *out, *out_h;
  628. time_t now;
  629. int i, l;
  630. sym_clear_all_valid();
  631. file_write_dep("include/config/auto.conf.cmd");
  632. if (conf_split_config())
  633. return 1;
  634. out = fopen(".tmpconfig", "w");
  635. if (!out)
  636. return 1;
  637. out_h = fopen(".tmpconfig.h", "w");
  638. if (!out_h) {
  639. fclose(out);
  640. return 1;
  641. }
  642. sym = sym_lookup("KERNELVERSION", 0);
  643. sym_calc_value(sym);
  644. time(&now);
  645. fprintf(out, "#\n"
  646. "# Automatically generated make config: don't edit\n"
  647. "# Linux kernel version: %s\n"
  648. "# %s"
  649. "#\n",
  650. sym_get_string_value(sym), ctime(&now));
  651. fprintf(out_h, "/*\n"
  652. " * Automatically generated C config: don't edit\n"
  653. " * Linux kernel version: %s\n"
  654. " * %s"
  655. " */\n"
  656. "#define AUTOCONF_INCLUDED\n",
  657. sym_get_string_value(sym), ctime(&now));
  658. for_all_symbols(i, sym) {
  659. sym_calc_value(sym);
  660. if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
  661. continue;
  662. switch (sym->type) {
  663. case S_BOOLEAN:
  664. case S_TRISTATE:
  665. switch (sym_get_tristate_value(sym)) {
  666. case no:
  667. break;
  668. case mod:
  669. fprintf(out, "CONFIG_%s=m\n", sym->name);
  670. fprintf(out_h, "#define CONFIG_%s_MODULE 1\n", sym->name);
  671. break;
  672. case yes:
  673. fprintf(out, "CONFIG_%s=y\n", sym->name);
  674. fprintf(out_h, "#define CONFIG_%s 1\n", sym->name);
  675. break;
  676. }
  677. break;
  678. case S_STRING:
  679. str = sym_get_string_value(sym);
  680. fprintf(out, "CONFIG_%s=\"", sym->name);
  681. fprintf(out_h, "#define CONFIG_%s \"", sym->name);
  682. while (1) {
  683. l = strcspn(str, "\"\\");
  684. if (l) {
  685. fwrite(str, l, 1, out);
  686. fwrite(str, l, 1, out_h);
  687. str += l;
  688. }
  689. if (!*str)
  690. break;
  691. fprintf(out, "\\%c", *str);
  692. fprintf(out_h, "\\%c", *str);
  693. str++;
  694. }
  695. fputs("\"\n", out);
  696. fputs("\"\n", out_h);
  697. break;
  698. case S_HEX:
  699. str = sym_get_string_value(sym);
  700. if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
  701. fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
  702. fprintf(out_h, "#define CONFIG_%s 0x%s\n", sym->name, str);
  703. break;
  704. }
  705. case S_INT:
  706. str = sym_get_string_value(sym);
  707. fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
  708. fprintf(out_h, "#define CONFIG_%s %s\n", sym->name, str);
  709. break;
  710. default:
  711. break;
  712. }
  713. }
  714. fclose(out);
  715. fclose(out_h);
  716. name = getenv("KCONFIG_AUTOHEADER");
  717. if (!name)
  718. name = "include/linux/autoconf.h";
  719. if (rename(".tmpconfig.h", name))
  720. return 1;
  721. name = conf_get_autoconfig_name();
  722. /*
  723. * This must be the last step, kbuild has a dependency on auto.conf
  724. * and this marks the successful completion of the previous steps.
  725. */
  726. if (rename(".tmpconfig", name))
  727. return 1;
  728. return 0;
  729. }
  730. static int sym_change_count;
  731. static void (*conf_changed_callback)(void);
  732. void sym_set_change_count(int count)
  733. {
  734. int _sym_change_count = sym_change_count;
  735. sym_change_count = count;
  736. if (conf_changed_callback &&
  737. (bool)_sym_change_count != (bool)count)
  738. conf_changed_callback();
  739. }
  740. void sym_add_change_count(int count)
  741. {
  742. sym_set_change_count(count + sym_change_count);
  743. }
  744. bool conf_get_changed(void)
  745. {
  746. return sym_change_count;
  747. }
  748. void conf_set_changed_callback(void (*fn)(void))
  749. {
  750. conf_changed_callback = fn;
  751. }
  752. void conf_set_all_new_symbols(enum conf_def_mode mode)
  753. {
  754. struct symbol *sym, *csym;
  755. struct property *prop;
  756. struct expr *e;
  757. int i, cnt, def;
  758. for_all_symbols(i, sym) {
  759. if (sym_has_value(sym))
  760. continue;
  761. switch (sym_get_type(sym)) {
  762. case S_BOOLEAN:
  763. case S_TRISTATE:
  764. switch (mode) {
  765. case def_yes:
  766. sym->def[S_DEF_USER].tri = yes;
  767. break;
  768. case def_mod:
  769. sym->def[S_DEF_USER].tri = mod;
  770. break;
  771. case def_no:
  772. sym->def[S_DEF_USER].tri = no;
  773. break;
  774. case def_random:
  775. sym->def[S_DEF_USER].tri = (tristate)(rand() % 3);
  776. break;
  777. default:
  778. continue;
  779. }
  780. if (!(sym_is_choice(sym) && mode == def_random))
  781. sym->flags |= SYMBOL_DEF_USER;
  782. break;
  783. default:
  784. break;
  785. }
  786. }
  787. sym_clear_all_valid();
  788. if (mode != def_random)
  789. return;
  790. /*
  791. * We have different type of choice blocks.
  792. * If curr.tri equal to mod then we can select several
  793. * choice symbols in one block.
  794. * In this case we do nothing.
  795. * If curr.tri equal yes then only one symbol can be
  796. * selected in a choice block and we set it to yes,
  797. * and the rest to no.
  798. */
  799. for_all_symbols(i, csym) {
  800. if (sym_has_value(csym) || !sym_is_choice(csym))
  801. continue;
  802. sym_calc_value(csym);
  803. if (csym->curr.tri != yes)
  804. continue;
  805. prop = sym_get_choice_prop(csym);
  806. /* count entries in choice block */
  807. cnt = 0;
  808. expr_list_for_each_sym(prop->expr, e, sym)
  809. cnt++;
  810. /*
  811. * find a random value and set it to yes,
  812. * set the rest to no so we have only one set
  813. */
  814. def = (rand() % cnt);
  815. cnt = 0;
  816. expr_list_for_each_sym(prop->expr, e, sym) {
  817. if (def == cnt++) {
  818. sym->def[S_DEF_USER].tri = yes;
  819. csym->def[S_DEF_USER].val = sym;
  820. }
  821. else {
  822. sym->def[S_DEF_USER].tri = no;
  823. }
  824. }
  825. csym->flags |= SYMBOL_DEF_USER;
  826. /* clear VALID to get value calculated */
  827. csym->flags &= ~(SYMBOL_VALID);
  828. }
  829. }