confdata.c 22 KB

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