confdata.c 19 KB

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