confdata.c 23 KB

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