confdata.c 24 KB

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