confdata.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  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 = malloc(strlen(basename) + 4 + 1);
  734. strcpy(dirname, basename);
  735. strcat(dirname, ".old");
  736. rename(newname, dirname);
  737. free(dirname);
  738. if (rename(tmpname, newname))
  739. return 1;
  740. }
  741. conf_message(_("configuration written to %s"), newname);
  742. sym_set_change_count(0);
  743. return 0;
  744. }
  745. static int conf_split_config(void)
  746. {
  747. const char *name;
  748. char path[PATH_MAX+1], opwd[PATH_MAX+1];
  749. char *s, *d, c;
  750. struct symbol *sym;
  751. struct stat sb;
  752. int res, i, fd;
  753. if (getcwd(opwd, sizeof(opwd)) == NULL)
  754. return 1;
  755. name = conf_get_autoconfig_name();
  756. conf_read_simple(name, S_DEF_AUTO);
  757. strcpy(path, name);
  758. dir_name(path);
  759. if (chdir(path))
  760. return 1;
  761. res = 0;
  762. for_all_symbols(i, sym) {
  763. sym_calc_value(sym);
  764. if ((sym->flags & SYMBOL_AUTO) || !sym->name)
  765. continue;
  766. if (sym->flags & SYMBOL_WRITE) {
  767. if (sym->flags & SYMBOL_DEF_AUTO) {
  768. /*
  769. * symbol has old and new value,
  770. * so compare them...
  771. */
  772. switch (sym->type) {
  773. case S_BOOLEAN:
  774. case S_TRISTATE:
  775. if (sym_get_tristate_value(sym) ==
  776. sym->def[S_DEF_AUTO].tri)
  777. continue;
  778. break;
  779. case S_STRING:
  780. case S_HEX:
  781. case S_INT:
  782. if (!strcmp(sym_get_string_value(sym),
  783. sym->def[S_DEF_AUTO].val))
  784. continue;
  785. break;
  786. default:
  787. break;
  788. }
  789. } else {
  790. /*
  791. * If there is no old value, only 'no' (unset)
  792. * is allowed as new value.
  793. */
  794. switch (sym->type) {
  795. case S_BOOLEAN:
  796. case S_TRISTATE:
  797. if (sym_get_tristate_value(sym) == no)
  798. continue;
  799. break;
  800. default:
  801. break;
  802. }
  803. }
  804. } else if (!(sym->flags & SYMBOL_DEF_AUTO))
  805. /* There is neither an old nor a new value. */
  806. continue;
  807. /* else
  808. * There is an old value, but no new value ('no' (unset)
  809. * isn't saved in auto.conf, so the old value is always
  810. * different from 'no').
  811. */
  812. /* Replace all '_' and append ".h" */
  813. s = sym->name;
  814. d = path;
  815. while ((c = *s++)) {
  816. c = tolower(c);
  817. *d++ = (c == '_') ? '/' : c;
  818. }
  819. strcpy(d, ".h");
  820. /* Assume directory path already exists. */
  821. fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  822. if (fd == -1) {
  823. if (errno != ENOENT) {
  824. res = 1;
  825. break;
  826. }
  827. /*
  828. * Create directory components,
  829. * unless they exist already.
  830. */
  831. d = path;
  832. while ((d = strchr(d, '/'))) {
  833. *d = 0;
  834. if (stat(path, &sb) && mkdir(path, 0755)) {
  835. res = 1;
  836. goto out;
  837. }
  838. *d++ = '/';
  839. }
  840. /* Try it again. */
  841. fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  842. if (fd == -1) {
  843. res = 1;
  844. break;
  845. }
  846. }
  847. close(fd);
  848. }
  849. out:
  850. if (chdir(opwd))
  851. return 1;
  852. return res;
  853. }
  854. int conf_write_autoconf(void)
  855. {
  856. struct symbol *sym;
  857. const char *name;
  858. char cfg_fname[PATH_MAX+1], tristate_fname[PATH_MAX+1],
  859. cfgh_fname[PATH_MAX+1];
  860. char *dirname;
  861. FILE *out, *tristate, *out_h;
  862. int i;
  863. sym_clear_all_valid();
  864. sprintf(cfg_fname, "%s.cmd", conf_get_autoconfig_name());
  865. file_write_dep(cfg_fname);
  866. if (conf_split_config())
  867. return 1;
  868. dirname = dir_name(strdup(conf_get_configname()));
  869. sprintf(cfg_fname, "%s.tmpconfig", dirname);
  870. sprintf(tristate_fname, "%s.tmpconfig_tristate", dirname);
  871. sprintf(cfgh_fname, "%s.tmpconfig.h", dirname);
  872. free(dirname);
  873. out = fopen(cfg_fname, "w");
  874. if (!out)
  875. return 1;
  876. tristate = fopen(tristate_fname, "w");
  877. if (!tristate) {
  878. fclose(out);
  879. return 1;
  880. }
  881. out_h = fopen(cfgh_fname, "w");
  882. if (!out_h) {
  883. fclose(out);
  884. fclose(tristate);
  885. return 1;
  886. }
  887. conf_write_heading(out, &kconfig_printer_cb, NULL);
  888. conf_write_heading(tristate, &tristate_printer_cb, NULL);
  889. conf_write_heading(out_h, &header_printer_cb, NULL);
  890. for_all_symbols(i, sym) {
  891. sym_calc_value(sym);
  892. if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
  893. continue;
  894. /* write symbol to auto.conf, tristate and header files */
  895. conf_write_symbol(out, sym, &kconfig_printer_cb, (void *)1);
  896. conf_write_symbol(tristate, sym, &tristate_printer_cb, (void *)1);
  897. conf_write_symbol(out_h, sym, &header_printer_cb, NULL);
  898. }
  899. fclose(out);
  900. fclose(tristate);
  901. fclose(out_h);
  902. name = getenv("KCONFIG_AUTOHEADER");
  903. if (!name)
  904. name = "include/generated/autoconf.h";
  905. if (rename(cfgh_fname, name))
  906. return 1;
  907. name = getenv("KCONFIG_TRISTATE");
  908. if (!name)
  909. name = "include/config/tristate.conf";
  910. if (rename(tristate_fname, name))
  911. return 1;
  912. name = conf_get_autoconfig_name();
  913. /*
  914. * This must be the last step, kbuild has a dependency on auto.conf
  915. * and this marks the successful completion of the previous steps.
  916. */
  917. if (rename(cfg_fname, name))
  918. return 1;
  919. return 0;
  920. }
  921. static int sym_change_count;
  922. static void (*conf_changed_callback)(void);
  923. void sym_set_change_count(int count)
  924. {
  925. int _sym_change_count = sym_change_count;
  926. sym_change_count = count;
  927. if (conf_changed_callback &&
  928. (bool)_sym_change_count != (bool)count)
  929. conf_changed_callback();
  930. }
  931. void sym_add_change_count(int count)
  932. {
  933. sym_set_change_count(count + sym_change_count);
  934. }
  935. bool conf_get_changed(void)
  936. {
  937. return sym_change_count;
  938. }
  939. void conf_set_changed_callback(void (*fn)(void))
  940. {
  941. conf_changed_callback = fn;
  942. }
  943. static bool randomize_choice_values(struct symbol *csym)
  944. {
  945. struct property *prop;
  946. struct symbol *sym;
  947. struct expr *e;
  948. int cnt, def;
  949. /*
  950. * If choice is mod then we may have more items selected
  951. * and if no then no-one.
  952. * In both cases stop.
  953. */
  954. if (csym->curr.tri != yes)
  955. return false;
  956. prop = sym_get_choice_prop(csym);
  957. /* count entries in choice block */
  958. cnt = 0;
  959. expr_list_for_each_sym(prop->expr, e, sym)
  960. cnt++;
  961. /*
  962. * find a random value and set it to yes,
  963. * set the rest to no so we have only one set
  964. */
  965. def = (rand() % cnt);
  966. cnt = 0;
  967. expr_list_for_each_sym(prop->expr, e, sym) {
  968. if (def == cnt++) {
  969. sym->def[S_DEF_USER].tri = yes;
  970. csym->def[S_DEF_USER].val = sym;
  971. }
  972. else {
  973. sym->def[S_DEF_USER].tri = no;
  974. }
  975. sym->flags |= SYMBOL_DEF_USER;
  976. /* clear VALID to get value calculated */
  977. sym->flags &= ~SYMBOL_VALID;
  978. }
  979. csym->flags |= SYMBOL_DEF_USER;
  980. /* clear VALID to get value calculated */
  981. csym->flags &= ~(SYMBOL_VALID);
  982. return true;
  983. }
  984. void set_all_choice_values(struct symbol *csym)
  985. {
  986. struct property *prop;
  987. struct symbol *sym;
  988. struct expr *e;
  989. prop = sym_get_choice_prop(csym);
  990. /*
  991. * Set all non-assinged choice values to no
  992. */
  993. expr_list_for_each_sym(prop->expr, e, sym) {
  994. if (!sym_has_value(sym))
  995. sym->def[S_DEF_USER].tri = no;
  996. }
  997. csym->flags |= SYMBOL_DEF_USER;
  998. /* clear VALID to get value calculated */
  999. csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
  1000. }
  1001. bool conf_set_all_new_symbols(enum conf_def_mode mode)
  1002. {
  1003. struct symbol *sym, *csym;
  1004. int i, cnt, pby, pty, ptm; /* pby: probability of boolean = y
  1005. * pty: probability of tristate = y
  1006. * ptm: probability of tristate = m
  1007. */
  1008. pby = 50; pty = ptm = 33; /* can't go as the default in switch-case
  1009. * below, otherwise gcc whines about
  1010. * -Wmaybe-uninitialized */
  1011. if (mode == def_random) {
  1012. int n, p[3];
  1013. char *env = getenv("KCONFIG_PROBABILITY");
  1014. n = 0;
  1015. while( env && *env ) {
  1016. char *endp;
  1017. int tmp = strtol( env, &endp, 10 );
  1018. if( tmp >= 0 && tmp <= 100 ) {
  1019. p[n++] = tmp;
  1020. } else {
  1021. errno = ERANGE;
  1022. perror( "KCONFIG_PROBABILITY" );
  1023. exit( 1 );
  1024. }
  1025. env = (*endp == ':') ? endp+1 : endp;
  1026. if( n >=3 ) {
  1027. break;
  1028. }
  1029. }
  1030. switch( n ) {
  1031. case 1:
  1032. pby = p[0]; ptm = pby/2; pty = pby-ptm;
  1033. break;
  1034. case 2:
  1035. pty = p[0]; ptm = p[1]; pby = pty + ptm;
  1036. break;
  1037. case 3:
  1038. pby = p[0]; pty = p[1]; ptm = p[2];
  1039. break;
  1040. }
  1041. if( pty+ptm > 100 ) {
  1042. errno = ERANGE;
  1043. perror( "KCONFIG_PROBABILITY" );
  1044. exit( 1 );
  1045. }
  1046. }
  1047. bool has_changed = false;
  1048. for_all_symbols(i, sym) {
  1049. if (sym_has_value(sym) || (sym->flags & SYMBOL_VALID))
  1050. continue;
  1051. switch (sym_get_type(sym)) {
  1052. case S_BOOLEAN:
  1053. case S_TRISTATE:
  1054. has_changed = true;
  1055. switch (mode) {
  1056. case def_yes:
  1057. sym->def[S_DEF_USER].tri = yes;
  1058. break;
  1059. case def_mod:
  1060. sym->def[S_DEF_USER].tri = mod;
  1061. break;
  1062. case def_no:
  1063. sym->def[S_DEF_USER].tri = no;
  1064. break;
  1065. case def_random:
  1066. sym->def[S_DEF_USER].tri = no;
  1067. cnt = rand() % 100;
  1068. if (sym->type == S_TRISTATE) {
  1069. if (cnt < pty)
  1070. sym->def[S_DEF_USER].tri = yes;
  1071. else if (cnt < (pty+ptm))
  1072. sym->def[S_DEF_USER].tri = mod;
  1073. } else if (cnt < pby)
  1074. sym->def[S_DEF_USER].tri = yes;
  1075. break;
  1076. default:
  1077. continue;
  1078. }
  1079. if (!(sym_is_choice(sym) && mode == def_random))
  1080. sym->flags |= SYMBOL_DEF_USER;
  1081. break;
  1082. default:
  1083. break;
  1084. }
  1085. }
  1086. sym_clear_all_valid();
  1087. /*
  1088. * We have different type of choice blocks.
  1089. * If curr.tri equals to mod then we can select several
  1090. * choice symbols in one block.
  1091. * In this case we do nothing.
  1092. * If curr.tri equals yes then only one symbol can be
  1093. * selected in a choice block and we set it to yes,
  1094. * and the rest to no.
  1095. */
  1096. if (mode != def_random) {
  1097. for_all_symbols(i, csym) {
  1098. if ((sym_is_choice(csym) && !sym_has_value(csym)) ||
  1099. sym_is_choice_value(csym))
  1100. csym->flags |= SYMBOL_NEED_SET_CHOICE_VALUES;
  1101. }
  1102. }
  1103. for_all_symbols(i, csym) {
  1104. if (sym_has_value(csym) || !sym_is_choice(csym))
  1105. continue;
  1106. sym_calc_value(csym);
  1107. if (mode == def_random)
  1108. has_changed = randomize_choice_values(csym);
  1109. else {
  1110. set_all_choice_values(csym);
  1111. has_changed = true;
  1112. }
  1113. }
  1114. return has_changed;
  1115. }