confdata.c 26 KB

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