confdata.c 24 KB

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