confdata.c 19 KB

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