confdata.c 24 KB

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