confdata.c 25 KB

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