1
0

confdata.c 25 KB

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