confdata.c 18 KB

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