confdata.c 24 KB

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