confdata.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  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. const char *suffix = "";
  446. switch (sym->type) {
  447. case S_BOOLEAN:
  448. case S_TRISTATE:
  449. switch (*value) {
  450. case 'n':
  451. return;
  452. case 'm':
  453. suffix = "_MODULE";
  454. /* FALLTHROUGH */
  455. default:
  456. value = "1";
  457. }
  458. break;
  459. default:
  460. break;
  461. }
  462. fprintf(fp, "#define %s%s%s %s\n",
  463. CONFIG_, sym->name, suffix, value);
  464. }
  465. static void
  466. header_print_comment(FILE *fp, const char *value, void *arg)
  467. {
  468. const char *p = value;
  469. size_t l;
  470. fprintf(fp, "/*\n");
  471. for (;;) {
  472. l = strcspn(p, "\n");
  473. fprintf(fp, " *");
  474. if (l) {
  475. fprintf(fp, " ");
  476. fwrite(p, l, 1, fp);
  477. p += l;
  478. }
  479. fprintf(fp, "\n");
  480. if (*p++ == '\0')
  481. break;
  482. }
  483. fprintf(fp, " */\n");
  484. }
  485. static struct conf_printer header_printer_cb =
  486. {
  487. .print_symbol = header_print_symbol,
  488. .print_comment = header_print_comment,
  489. };
  490. /*
  491. * Function-style header printer
  492. *
  493. * This printer is used to generate the config_is_xxx() function-style macros
  494. * in `include/generated/autoconf.h'
  495. */
  496. static void
  497. header_function_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
  498. {
  499. int val = 0;
  500. char c;
  501. char *tmp, *d;
  502. switch (sym->type) {
  503. case S_BOOLEAN:
  504. case S_TRISTATE:
  505. break;
  506. default:
  507. return;
  508. }
  509. if (*value == 'm')
  510. val = 2;
  511. else if (*value == 'y')
  512. val = 1;
  513. d = strdup(CONFIG_);
  514. tmp = d;
  515. while ((c = *d)) {
  516. *d = tolower(c);
  517. d++;
  518. }
  519. fprintf(fp, "#define %sis_", tmp);
  520. free(tmp);
  521. d = strdup(sym->name);
  522. tmp = d;
  523. while ((c = *d)) {
  524. *d = tolower(c);
  525. d++;
  526. }
  527. fprintf(fp, "%s%s() %d\n", tmp, (val > 1) ? "_module" : "",
  528. val ? 1 : 0);
  529. free(tmp);
  530. }
  531. static struct conf_printer header_function_printer_cb =
  532. {
  533. .print_symbol = header_function_print_symbol,
  534. };
  535. /*
  536. * Tristate printer
  537. *
  538. * This printer is used when generating the `include/config/tristate.conf' file.
  539. */
  540. static void
  541. tristate_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
  542. {
  543. if (sym->type == S_TRISTATE && *value != 'n')
  544. fprintf(fp, "%s%s=%c\n", CONFIG_, sym->name, (char)toupper(*value));
  545. }
  546. static struct conf_printer tristate_printer_cb =
  547. {
  548. .print_symbol = tristate_print_symbol,
  549. .print_comment = kconfig_print_comment,
  550. };
  551. static void conf_write_symbol(FILE *fp, struct symbol *sym,
  552. struct conf_printer *printer, void *printer_arg)
  553. {
  554. const char *str;
  555. switch (sym->type) {
  556. case S_OTHER:
  557. case S_UNKNOWN:
  558. break;
  559. case S_STRING:
  560. str = sym_get_string_value(sym);
  561. str = sym_escape_string_value(str);
  562. printer->print_symbol(fp, sym, str, printer_arg);
  563. free((void *)str);
  564. break;
  565. default:
  566. str = sym_get_string_value(sym);
  567. printer->print_symbol(fp, sym, str, printer_arg);
  568. }
  569. }
  570. static void
  571. conf_write_heading(FILE *fp, struct conf_printer *printer, void *printer_arg)
  572. {
  573. char buf[256];
  574. snprintf(buf, sizeof(buf),
  575. "\n"
  576. "Automatically generated file; DO NOT EDIT.\n"
  577. "%s\n",
  578. rootmenu.prompt->text);
  579. printer->print_comment(fp, buf, printer_arg);
  580. }
  581. /*
  582. * Write out a minimal config.
  583. * All values that has default values are skipped as this is redundant.
  584. */
  585. int conf_write_defconfig(const char *filename)
  586. {
  587. struct symbol *sym;
  588. struct menu *menu;
  589. FILE *out;
  590. out = fopen(filename, "w");
  591. if (!out)
  592. return 1;
  593. sym_clear_all_valid();
  594. /* Traverse all menus to find all relevant symbols */
  595. menu = rootmenu.list;
  596. while (menu != NULL)
  597. {
  598. sym = menu->sym;
  599. if (sym == NULL) {
  600. if (!menu_is_visible(menu))
  601. goto next_menu;
  602. } else if (!sym_is_choice(sym)) {
  603. sym_calc_value(sym);
  604. if (!(sym->flags & SYMBOL_WRITE))
  605. goto next_menu;
  606. sym->flags &= ~SYMBOL_WRITE;
  607. /* If we cannot change the symbol - skip */
  608. if (!sym_is_changable(sym))
  609. goto next_menu;
  610. /* If symbol equals to default value - skip */
  611. if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
  612. goto next_menu;
  613. /*
  614. * If symbol is a choice value and equals to the
  615. * default for a choice - skip.
  616. * But only if value is bool and equal to "y" and
  617. * choice is not "optional".
  618. * (If choice is "optional" then all values can be "n")
  619. */
  620. if (sym_is_choice_value(sym)) {
  621. struct symbol *cs;
  622. struct symbol *ds;
  623. cs = prop_get_symbol(sym_get_choice_prop(sym));
  624. ds = sym_choice_default(cs);
  625. if (!sym_is_optional(cs) && sym == ds) {
  626. if ((sym->type == S_BOOLEAN) &&
  627. sym_get_tristate_value(sym) == yes)
  628. goto next_menu;
  629. }
  630. }
  631. conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
  632. }
  633. next_menu:
  634. if (menu->list != NULL) {
  635. menu = menu->list;
  636. }
  637. else if (menu->next != NULL) {
  638. menu = menu->next;
  639. } else {
  640. while ((menu = menu->parent)) {
  641. if (menu->next != NULL) {
  642. menu = menu->next;
  643. break;
  644. }
  645. }
  646. }
  647. }
  648. fclose(out);
  649. return 0;
  650. }
  651. int conf_write(const char *name)
  652. {
  653. FILE *out;
  654. struct symbol *sym;
  655. struct menu *menu;
  656. const char *basename;
  657. const char *str;
  658. char dirname[PATH_MAX+1], tmpname[PATH_MAX+1], newname[PATH_MAX+1];
  659. char *env;
  660. dirname[0] = 0;
  661. if (name && name[0]) {
  662. struct stat st;
  663. char *slash;
  664. if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
  665. strcpy(dirname, name);
  666. strcat(dirname, "/");
  667. basename = conf_get_configname();
  668. } else if ((slash = strrchr(name, '/'))) {
  669. int size = slash - name + 1;
  670. memcpy(dirname, name, size);
  671. dirname[size] = 0;
  672. if (slash[1])
  673. basename = slash + 1;
  674. else
  675. basename = conf_get_configname();
  676. } else
  677. basename = name;
  678. } else
  679. basename = conf_get_configname();
  680. sprintf(newname, "%s%s", dirname, basename);
  681. env = getenv("KCONFIG_OVERWRITECONFIG");
  682. if (!env || !*env) {
  683. sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
  684. out = fopen(tmpname, "w");
  685. } else {
  686. *tmpname = 0;
  687. out = fopen(newname, "w");
  688. }
  689. if (!out)
  690. return 1;
  691. conf_write_heading(out, &kconfig_printer_cb, NULL);
  692. if (!conf_get_changed())
  693. sym_clear_all_valid();
  694. menu = rootmenu.list;
  695. while (menu) {
  696. sym = menu->sym;
  697. if (!sym) {
  698. if (!menu_is_visible(menu))
  699. goto next;
  700. str = menu_get_prompt(menu);
  701. fprintf(out, "\n"
  702. "#\n"
  703. "# %s\n"
  704. "#\n", str);
  705. } else if (!(sym->flags & SYMBOL_CHOICE)) {
  706. sym_calc_value(sym);
  707. if (!(sym->flags & SYMBOL_WRITE))
  708. goto next;
  709. sym->flags &= ~SYMBOL_WRITE;
  710. conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
  711. }
  712. next:
  713. if (menu->list) {
  714. menu = menu->list;
  715. continue;
  716. }
  717. if (menu->next)
  718. menu = menu->next;
  719. else while ((menu = menu->parent)) {
  720. if (menu->next) {
  721. menu = menu->next;
  722. break;
  723. }
  724. }
  725. }
  726. fclose(out);
  727. if (*tmpname) {
  728. strcat(dirname, basename);
  729. strcat(dirname, ".old");
  730. rename(newname, dirname);
  731. if (rename(tmpname, newname))
  732. return 1;
  733. }
  734. conf_message(_("configuration written to %s"), newname);
  735. sym_set_change_count(0);
  736. return 0;
  737. }
  738. static int conf_split_config(void)
  739. {
  740. const char *name;
  741. char path[PATH_MAX+1];
  742. char *s, *d, c;
  743. struct symbol *sym;
  744. struct stat sb;
  745. int res, i, fd;
  746. name = conf_get_autoconfig_name();
  747. conf_read_simple(name, S_DEF_AUTO);
  748. if (chdir("include/config"))
  749. return 1;
  750. res = 0;
  751. for_all_symbols(i, sym) {
  752. sym_calc_value(sym);
  753. if ((sym->flags & SYMBOL_AUTO) || !sym->name)
  754. continue;
  755. if (sym->flags & SYMBOL_WRITE) {
  756. if (sym->flags & SYMBOL_DEF_AUTO) {
  757. /*
  758. * symbol has old and new value,
  759. * so compare them...
  760. */
  761. switch (sym->type) {
  762. case S_BOOLEAN:
  763. case S_TRISTATE:
  764. if (sym_get_tristate_value(sym) ==
  765. sym->def[S_DEF_AUTO].tri)
  766. continue;
  767. break;
  768. case S_STRING:
  769. case S_HEX:
  770. case S_INT:
  771. if (!strcmp(sym_get_string_value(sym),
  772. sym->def[S_DEF_AUTO].val))
  773. continue;
  774. break;
  775. default:
  776. break;
  777. }
  778. } else {
  779. /*
  780. * If there is no old value, only 'no' (unset)
  781. * is allowed as new value.
  782. */
  783. switch (sym->type) {
  784. case S_BOOLEAN:
  785. case S_TRISTATE:
  786. if (sym_get_tristate_value(sym) == no)
  787. continue;
  788. break;
  789. default:
  790. break;
  791. }
  792. }
  793. } else if (!(sym->flags & SYMBOL_DEF_AUTO))
  794. /* There is neither an old nor a new value. */
  795. continue;
  796. /* else
  797. * There is an old value, but no new value ('no' (unset)
  798. * isn't saved in auto.conf, so the old value is always
  799. * different from 'no').
  800. */
  801. /* Replace all '_' and append ".h" */
  802. s = sym->name;
  803. d = path;
  804. while ((c = *s++)) {
  805. c = tolower(c);
  806. *d++ = (c == '_') ? '/' : c;
  807. }
  808. strcpy(d, ".h");
  809. /* Assume directory path already exists. */
  810. fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  811. if (fd == -1) {
  812. if (errno != ENOENT) {
  813. res = 1;
  814. break;
  815. }
  816. /*
  817. * Create directory components,
  818. * unless they exist already.
  819. */
  820. d = path;
  821. while ((d = strchr(d, '/'))) {
  822. *d = 0;
  823. if (stat(path, &sb) && mkdir(path, 0755)) {
  824. res = 1;
  825. goto out;
  826. }
  827. *d++ = '/';
  828. }
  829. /* Try it again. */
  830. fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  831. if (fd == -1) {
  832. res = 1;
  833. break;
  834. }
  835. }
  836. close(fd);
  837. }
  838. out:
  839. if (chdir("../.."))
  840. return 1;
  841. return res;
  842. }
  843. int conf_write_autoconf(void)
  844. {
  845. struct symbol *sym;
  846. const char *name;
  847. FILE *out, *tristate, *out_h;
  848. int i;
  849. sym_clear_all_valid();
  850. file_write_dep("include/config/auto.conf.cmd");
  851. if (conf_split_config())
  852. return 1;
  853. out = fopen(".tmpconfig", "w");
  854. if (!out)
  855. return 1;
  856. tristate = fopen(".tmpconfig_tristate", "w");
  857. if (!tristate) {
  858. fclose(out);
  859. return 1;
  860. }
  861. out_h = fopen(".tmpconfig.h", "w");
  862. if (!out_h) {
  863. fclose(out);
  864. fclose(tristate);
  865. return 1;
  866. }
  867. conf_write_heading(out, &kconfig_printer_cb, NULL);
  868. conf_write_heading(tristate, &tristate_printer_cb, NULL);
  869. conf_write_heading(out_h, &header_printer_cb, NULL);
  870. for_all_symbols(i, sym) {
  871. sym_calc_value(sym);
  872. if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
  873. continue;
  874. /* write symbol to auto.conf, tristate and header files */
  875. conf_write_symbol(out, sym, &kconfig_printer_cb, (void *)1);
  876. conf_write_symbol(tristate, sym, &tristate_printer_cb, (void *)1);
  877. conf_write_symbol(out_h, sym, &header_printer_cb, NULL);
  878. conf_write_symbol(out_h, sym, &header_function_printer_cb, NULL);
  879. }
  880. fclose(out);
  881. fclose(tristate);
  882. fclose(out_h);
  883. name = getenv("KCONFIG_AUTOHEADER");
  884. if (!name)
  885. name = "include/generated/autoconf.h";
  886. if (rename(".tmpconfig.h", name))
  887. return 1;
  888. name = getenv("KCONFIG_TRISTATE");
  889. if (!name)
  890. name = "include/config/tristate.conf";
  891. if (rename(".tmpconfig_tristate", name))
  892. return 1;
  893. name = conf_get_autoconfig_name();
  894. /*
  895. * This must be the last step, kbuild has a dependency on auto.conf
  896. * and this marks the successful completion of the previous steps.
  897. */
  898. if (rename(".tmpconfig", name))
  899. return 1;
  900. return 0;
  901. }
  902. static int sym_change_count;
  903. static void (*conf_changed_callback)(void);
  904. void sym_set_change_count(int count)
  905. {
  906. int _sym_change_count = sym_change_count;
  907. sym_change_count = count;
  908. if (conf_changed_callback &&
  909. (bool)_sym_change_count != (bool)count)
  910. conf_changed_callback();
  911. }
  912. void sym_add_change_count(int count)
  913. {
  914. sym_set_change_count(count + sym_change_count);
  915. }
  916. bool conf_get_changed(void)
  917. {
  918. return sym_change_count;
  919. }
  920. void conf_set_changed_callback(void (*fn)(void))
  921. {
  922. conf_changed_callback = fn;
  923. }
  924. static void randomize_choice_values(struct symbol *csym)
  925. {
  926. struct property *prop;
  927. struct symbol *sym;
  928. struct expr *e;
  929. int cnt, def;
  930. /*
  931. * If choice is mod then we may have more items selected
  932. * and if no then no-one.
  933. * In both cases stop.
  934. */
  935. if (csym->curr.tri != yes)
  936. return;
  937. prop = sym_get_choice_prop(csym);
  938. /* count entries in choice block */
  939. cnt = 0;
  940. expr_list_for_each_sym(prop->expr, e, sym)
  941. cnt++;
  942. /*
  943. * find a random value and set it to yes,
  944. * set the rest to no so we have only one set
  945. */
  946. def = (rand() % cnt);
  947. cnt = 0;
  948. expr_list_for_each_sym(prop->expr, e, sym) {
  949. if (def == cnt++) {
  950. sym->def[S_DEF_USER].tri = yes;
  951. csym->def[S_DEF_USER].val = sym;
  952. }
  953. else {
  954. sym->def[S_DEF_USER].tri = no;
  955. }
  956. }
  957. csym->flags |= SYMBOL_DEF_USER;
  958. /* clear VALID to get value calculated */
  959. csym->flags &= ~(SYMBOL_VALID);
  960. }
  961. static void set_all_choice_values(struct symbol *csym)
  962. {
  963. struct property *prop;
  964. struct symbol *sym;
  965. struct expr *e;
  966. prop = sym_get_choice_prop(csym);
  967. /*
  968. * Set all non-assinged choice values to no
  969. */
  970. expr_list_for_each_sym(prop->expr, e, sym) {
  971. if (!sym_has_value(sym))
  972. sym->def[S_DEF_USER].tri = no;
  973. }
  974. csym->flags |= SYMBOL_DEF_USER;
  975. /* clear VALID to get value calculated */
  976. csym->flags &= ~(SYMBOL_VALID);
  977. }
  978. void conf_set_all_new_symbols(enum conf_def_mode mode)
  979. {
  980. struct symbol *sym, *csym;
  981. int i, cnt;
  982. for_all_symbols(i, sym) {
  983. if (sym_has_value(sym))
  984. continue;
  985. switch (sym_get_type(sym)) {
  986. case S_BOOLEAN:
  987. case S_TRISTATE:
  988. switch (mode) {
  989. case def_yes:
  990. sym->def[S_DEF_USER].tri = yes;
  991. break;
  992. case def_mod:
  993. sym->def[S_DEF_USER].tri = mod;
  994. break;
  995. case def_no:
  996. sym->def[S_DEF_USER].tri = no;
  997. break;
  998. case def_random:
  999. cnt = sym_get_type(sym) == S_TRISTATE ? 3 : 2;
  1000. sym->def[S_DEF_USER].tri = (tristate)(rand() % cnt);
  1001. break;
  1002. default:
  1003. continue;
  1004. }
  1005. if (!(sym_is_choice(sym) && mode == def_random))
  1006. sym->flags |= SYMBOL_DEF_USER;
  1007. break;
  1008. default:
  1009. break;
  1010. }
  1011. }
  1012. sym_clear_all_valid();
  1013. /*
  1014. * We have different type of choice blocks.
  1015. * If curr.tri equals to mod then we can select several
  1016. * choice symbols in one block.
  1017. * In this case we do nothing.
  1018. * If curr.tri equals yes then only one symbol can be
  1019. * selected in a choice block and we set it to yes,
  1020. * and the rest to no.
  1021. */
  1022. for_all_symbols(i, csym) {
  1023. if (sym_has_value(csym) || !sym_is_choice(csym))
  1024. continue;
  1025. sym_calc_value(csym);
  1026. if (mode == def_random)
  1027. randomize_choice_values(csym);
  1028. else
  1029. set_all_choice_values(csym);
  1030. }
  1031. }