confdata.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  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. if (*value == 'n') {
  445. value = "";
  446. }
  447. break;
  448. default:
  449. break;
  450. }
  451. fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, value);
  452. }
  453. static void
  454. kconfig_print_comment(FILE *fp, const char *value, void *arg)
  455. {
  456. const char *p = value;
  457. size_t l;
  458. for (;;) {
  459. l = strcspn(p, "\n");
  460. fprintf(fp, "#");
  461. if (l) {
  462. fprintf(fp, " ");
  463. xfwrite(p, l, 1, fp);
  464. p += l;
  465. }
  466. fprintf(fp, "\n");
  467. if (*p++ == '\0')
  468. break;
  469. }
  470. }
  471. static struct conf_printer kconfig_printer_cb =
  472. {
  473. .print_symbol = kconfig_print_symbol,
  474. .print_comment = kconfig_print_comment,
  475. };
  476. /*
  477. * Header printer
  478. *
  479. * This printer is used when generating the `include/generated/autoconf.h' file.
  480. */
  481. static void
  482. header_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
  483. {
  484. switch (sym->type) {
  485. case S_BOOLEAN:
  486. case S_TRISTATE: {
  487. const char *suffix = "";
  488. switch (*value) {
  489. case 'n':
  490. break;
  491. case 'm':
  492. suffix = "_MODULE";
  493. /* fall through */
  494. default:
  495. fprintf(fp, "#define %s%s%s 1\n",
  496. CONFIG_, sym->name, suffix);
  497. }
  498. break;
  499. }
  500. case S_HEX: {
  501. const char *prefix = "";
  502. if (value[0] != '0' || (value[1] != 'x' && value[1] != 'X'))
  503. prefix = "0x";
  504. fprintf(fp, "#define %s%s %s%s\n",
  505. CONFIG_, sym->name, prefix, value);
  506. break;
  507. }
  508. case S_STRING:
  509. case S_INT:
  510. fprintf(fp, "#define %s%s %s\n",
  511. CONFIG_, sym->name, value);
  512. break;
  513. default:
  514. break;
  515. }
  516. }
  517. static void
  518. header_print_comment(FILE *fp, const char *value, void *arg)
  519. {
  520. const char *p = value;
  521. size_t l;
  522. fprintf(fp, "/*\n");
  523. for (;;) {
  524. l = strcspn(p, "\n");
  525. fprintf(fp, " *");
  526. if (l) {
  527. fprintf(fp, " ");
  528. xfwrite(p, l, 1, fp);
  529. p += l;
  530. }
  531. fprintf(fp, "\n");
  532. if (*p++ == '\0')
  533. break;
  534. }
  535. fprintf(fp, " */\n");
  536. }
  537. static struct conf_printer header_printer_cb =
  538. {
  539. .print_symbol = header_print_symbol,
  540. .print_comment = header_print_comment,
  541. };
  542. /*
  543. * Tristate printer
  544. *
  545. * This printer is used when generating the `include/config/tristate.conf' file.
  546. */
  547. static void
  548. tristate_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
  549. {
  550. if (sym->type == S_TRISTATE && *value != 'n')
  551. fprintf(fp, "%s%s=%c\n", CONFIG_, sym->name, (char)toupper(*value));
  552. }
  553. static struct conf_printer tristate_printer_cb =
  554. {
  555. .print_symbol = tristate_print_symbol,
  556. .print_comment = kconfig_print_comment,
  557. };
  558. static void conf_write_symbol(FILE *fp, struct symbol *sym,
  559. struct conf_printer *printer, void *printer_arg)
  560. {
  561. const char *str;
  562. switch (sym->type) {
  563. case S_OTHER:
  564. case S_UNKNOWN:
  565. break;
  566. case S_STRING:
  567. str = sym_get_string_value(sym);
  568. str = sym_escape_string_value(str);
  569. printer->print_symbol(fp, sym, str, printer_arg);
  570. free((void *)str);
  571. break;
  572. default:
  573. str = sym_get_string_value(sym);
  574. printer->print_symbol(fp, sym, str, printer_arg);
  575. }
  576. }
  577. static void
  578. conf_write_heading(FILE *fp, struct conf_printer *printer, void *printer_arg)
  579. {
  580. char buf[256];
  581. snprintf(buf, sizeof(buf),
  582. "\n"
  583. "Automatically generated file; DO NOT EDIT.\n"
  584. "%s\n",
  585. rootmenu.prompt->text);
  586. printer->print_comment(fp, buf, printer_arg);
  587. }
  588. /*
  589. * Write out a minimal config.
  590. * All values that has default values are skipped as this is redundant.
  591. */
  592. int conf_write_defconfig(const char *filename)
  593. {
  594. struct symbol *sym;
  595. struct menu *menu;
  596. FILE *out;
  597. out = fopen(filename, "w");
  598. if (!out)
  599. return 1;
  600. sym_clear_all_valid();
  601. /* Traverse all menus to find all relevant symbols */
  602. menu = rootmenu.list;
  603. while (menu != NULL)
  604. {
  605. sym = menu->sym;
  606. if (sym == NULL) {
  607. if (!menu_is_visible(menu))
  608. goto next_menu;
  609. } else if (!sym_is_choice(sym)) {
  610. sym_calc_value(sym);
  611. if (!(sym->flags & SYMBOL_WRITE))
  612. goto next_menu;
  613. sym->flags &= ~SYMBOL_WRITE;
  614. /* If we cannot change the symbol - skip */
  615. if (!sym_is_changable(sym))
  616. goto next_menu;
  617. /* If symbol equals to default value - skip */
  618. if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
  619. goto next_menu;
  620. /*
  621. * If symbol is a choice value and equals to the
  622. * default for a choice - skip.
  623. * But only if value is bool and equal to "y" and
  624. * choice is not "optional".
  625. * (If choice is "optional" then all values can be "n")
  626. */
  627. if (sym_is_choice_value(sym)) {
  628. struct symbol *cs;
  629. struct symbol *ds;
  630. cs = prop_get_symbol(sym_get_choice_prop(sym));
  631. ds = sym_choice_default(cs);
  632. if (!sym_is_optional(cs) && sym == ds) {
  633. if ((sym->type == S_BOOLEAN) &&
  634. sym_get_tristate_value(sym) == yes)
  635. goto next_menu;
  636. }
  637. }
  638. conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
  639. }
  640. next_menu:
  641. if (menu->list != NULL) {
  642. menu = menu->list;
  643. }
  644. else if (menu->next != NULL) {
  645. menu = menu->next;
  646. } else {
  647. while ((menu = menu->parent)) {
  648. if (menu->next != NULL) {
  649. menu = menu->next;
  650. break;
  651. }
  652. }
  653. }
  654. }
  655. fclose(out);
  656. return 0;
  657. }
  658. int conf_write(const char *name)
  659. {
  660. FILE *out;
  661. struct symbol *sym;
  662. struct menu *menu;
  663. const char *basename;
  664. const char *str;
  665. char dirname[PATH_MAX+1], tmpname[PATH_MAX+1], newname[PATH_MAX+1];
  666. char *env;
  667. dirname[0] = 0;
  668. if (name && name[0]) {
  669. struct stat st;
  670. char *slash;
  671. if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
  672. strcpy(dirname, name);
  673. strcat(dirname, "/");
  674. basename = conf_get_configname();
  675. } else if ((slash = strrchr(name, '/'))) {
  676. int size = slash - name + 1;
  677. memcpy(dirname, name, size);
  678. dirname[size] = 0;
  679. if (slash[1])
  680. basename = slash + 1;
  681. else
  682. basename = conf_get_configname();
  683. } else
  684. basename = name;
  685. } else
  686. basename = conf_get_configname();
  687. sprintf(newname, "%s%s", dirname, basename);
  688. env = getenv("KCONFIG_OVERWRITECONFIG");
  689. if (!env || !*env) {
  690. sprintf(tmpname, "%s.tmpconfig.%d", newname, (int)getpid());
  691. out = fopen(tmpname, "w");
  692. } else {
  693. *tmpname = 0;
  694. out = fopen(newname, "w");
  695. }
  696. if (!out)
  697. return 1;
  698. conf_write_heading(out, &kconfig_printer_cb, NULL);
  699. if (!conf_get_changed())
  700. sym_clear_all_valid();
  701. menu = rootmenu.list;
  702. while (menu) {
  703. sym = menu->sym;
  704. if (!sym) {
  705. if (!menu_is_visible(menu))
  706. goto next;
  707. str = menu_get_prompt(menu);
  708. fprintf(out, "\n"
  709. "#\n"
  710. "# %s\n"
  711. "#\n", str);
  712. } else if (!(sym->flags & SYMBOL_CHOICE)) {
  713. sym_calc_value(sym);
  714. if (!(sym->flags & SYMBOL_WRITE))
  715. goto next;
  716. sym->flags &= ~SYMBOL_WRITE;
  717. conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
  718. }
  719. next:
  720. if (menu->list) {
  721. menu = menu->list;
  722. continue;
  723. }
  724. if (menu->next)
  725. menu = menu->next;
  726. else while ((menu = menu->parent)) {
  727. if (menu->next) {
  728. menu = menu->next;
  729. break;
  730. }
  731. }
  732. }
  733. fclose(out);
  734. if (*tmpname) {
  735. strcat(dirname, basename);
  736. strcat(dirname, ".old");
  737. rename(newname, 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];
  749. char *s, *d, c;
  750. struct symbol *sym;
  751. struct stat sb;
  752. int res, i, fd;
  753. name = conf_get_autoconfig_name();
  754. conf_read_simple(name, S_DEF_AUTO);
  755. sym_calc_value(modules_sym);
  756. if (chdir("include/config"))
  757. return 1;
  758. res = 0;
  759. for_all_symbols(i, sym) {
  760. sym_calc_value(sym);
  761. if ((sym->flags & SYMBOL_AUTO) || !sym->name)
  762. continue;
  763. if (sym->flags & SYMBOL_WRITE) {
  764. if (sym->flags & SYMBOL_DEF_AUTO) {
  765. /*
  766. * symbol has old and new value,
  767. * so compare them...
  768. */
  769. switch (sym->type) {
  770. case S_BOOLEAN:
  771. case S_TRISTATE:
  772. if (sym_get_tristate_value(sym) ==
  773. sym->def[S_DEF_AUTO].tri)
  774. continue;
  775. break;
  776. case S_STRING:
  777. case S_HEX:
  778. case S_INT:
  779. if (!strcmp(sym_get_string_value(sym),
  780. sym->def[S_DEF_AUTO].val))
  781. continue;
  782. break;
  783. default:
  784. break;
  785. }
  786. } else {
  787. /*
  788. * If there is no old value, only 'no' (unset)
  789. * is allowed as new value.
  790. */
  791. switch (sym->type) {
  792. case S_BOOLEAN:
  793. case S_TRISTATE:
  794. if (sym_get_tristate_value(sym) == no)
  795. continue;
  796. break;
  797. default:
  798. break;
  799. }
  800. }
  801. } else if (!(sym->flags & SYMBOL_DEF_AUTO))
  802. /* There is neither an old nor a new value. */
  803. continue;
  804. /* else
  805. * There is an old value, but no new value ('no' (unset)
  806. * isn't saved in auto.conf, so the old value is always
  807. * different from 'no').
  808. */
  809. /* Replace all '_' and append ".h" */
  810. s = sym->name;
  811. d = path;
  812. while ((c = *s++)) {
  813. c = tolower(c);
  814. *d++ = (c == '_') ? '/' : c;
  815. }
  816. strcpy(d, ".h");
  817. /* Assume directory path already exists. */
  818. fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  819. if (fd == -1) {
  820. if (errno != ENOENT) {
  821. res = 1;
  822. break;
  823. }
  824. /*
  825. * Create directory components,
  826. * unless they exist already.
  827. */
  828. d = path;
  829. while ((d = strchr(d, '/'))) {
  830. *d = 0;
  831. if (stat(path, &sb) && mkdir(path, 0755)) {
  832. res = 1;
  833. goto out;
  834. }
  835. *d++ = '/';
  836. }
  837. /* Try it again. */
  838. fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  839. if (fd == -1) {
  840. res = 1;
  841. break;
  842. }
  843. }
  844. close(fd);
  845. }
  846. out:
  847. if (chdir("../.."))
  848. return 1;
  849. return res;
  850. }
  851. int conf_write_autoconf(void)
  852. {
  853. struct symbol *sym;
  854. const char *name;
  855. FILE *out, *tristate, *out_h;
  856. int i;
  857. sym_clear_all_valid();
  858. file_write_dep("include/config/auto.conf.cmd");
  859. if (conf_split_config())
  860. return 1;
  861. out = fopen(".tmpconfig", "w");
  862. if (!out)
  863. return 1;
  864. tristate = fopen(".tmpconfig_tristate", "w");
  865. if (!tristate) {
  866. fclose(out);
  867. return 1;
  868. }
  869. out_h = fopen(".tmpconfig.h", "w");
  870. if (!out_h) {
  871. fclose(out);
  872. fclose(tristate);
  873. return 1;
  874. }
  875. conf_write_heading(out, &kconfig_printer_cb, NULL);
  876. conf_write_heading(tristate, &tristate_printer_cb, NULL);
  877. conf_write_heading(out_h, &header_printer_cb, NULL);
  878. /* write symbols to auto.conf, tristate and header files */
  879. for_all_symbols(i, sym) {
  880. if (!sym->name) continue;
  881. if ((sym->flags & SYMBOL_WRITE) ||
  882. /*
  883. * If the symbol is disabled by dependency we still want it in auto.conf
  884. * so that all possible variables are always defined.
  885. */
  886. (sym->dir_dep.expr != NULL && sym->dir_dep.tri == no)) {
  887. conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
  888. }
  889. if (sym->flags & SYMBOL_WRITE) {
  890. conf_write_symbol(tristate, sym, &tristate_printer_cb, NULL);
  891. conf_write_symbol(out_h, sym, &header_printer_cb, NULL);
  892. }
  893. }
  894. fclose(out);
  895. fclose(tristate);
  896. fclose(out_h);
  897. name = getenv("KCONFIG_AUTOHEADER");
  898. if (!name)
  899. name = "include/generated/autoconf.h";
  900. if (rename(".tmpconfig.h", name))
  901. return 1;
  902. name = getenv("KCONFIG_TRISTATE");
  903. if (!name)
  904. name = "include/config/tristate.conf";
  905. if (rename(".tmpconfig_tristate", name))
  906. return 1;
  907. name = conf_get_autoconfig_name();
  908. /*
  909. * This must be the last step, kbuild has a dependency on auto.conf
  910. * and this marks the successful completion of the previous steps.
  911. */
  912. if (rename(".tmpconfig", name))
  913. return 1;
  914. return 0;
  915. }
  916. static int sym_change_count;
  917. static void (*conf_changed_callback)(void);
  918. void sym_set_change_count(int count)
  919. {
  920. int _sym_change_count = sym_change_count;
  921. sym_change_count = count;
  922. if (conf_changed_callback &&
  923. (bool)_sym_change_count != (bool)count)
  924. conf_changed_callback();
  925. }
  926. void sym_add_change_count(int count)
  927. {
  928. sym_set_change_count(count + sym_change_count);
  929. }
  930. bool conf_get_changed(void)
  931. {
  932. return sym_change_count;
  933. }
  934. void conf_set_changed_callback(void (*fn)(void))
  935. {
  936. conf_changed_callback = fn;
  937. }
  938. static bool randomize_choice_values(struct symbol *csym)
  939. {
  940. struct property *prop;
  941. struct symbol *sym;
  942. struct expr *e;
  943. int cnt, def;
  944. /*
  945. * If choice is mod then we may have more items selected
  946. * and if no then no-one.
  947. * In both cases stop.
  948. */
  949. if (csym->curr.tri != yes)
  950. return false;
  951. prop = sym_get_choice_prop(csym);
  952. /* count entries in choice block */
  953. cnt = 0;
  954. expr_list_for_each_sym(prop->expr, e, sym)
  955. cnt++;
  956. /*
  957. * find a random value and set it to yes,
  958. * set the rest to no so we have only one set
  959. */
  960. def = (rand() % cnt);
  961. cnt = 0;
  962. expr_list_for_each_sym(prop->expr, e, sym) {
  963. if (def == cnt++) {
  964. sym->def[S_DEF_USER].tri = yes;
  965. csym->def[S_DEF_USER].val = sym;
  966. }
  967. else {
  968. sym->def[S_DEF_USER].tri = no;
  969. }
  970. sym->flags |= SYMBOL_DEF_USER;
  971. /* clear VALID to get value calculated */
  972. sym->flags &= ~SYMBOL_VALID;
  973. }
  974. csym->flags |= SYMBOL_DEF_USER;
  975. /* clear VALID to get value calculated */
  976. csym->flags &= ~(SYMBOL_VALID);
  977. return true;
  978. }
  979. void set_all_choice_values(struct symbol *csym)
  980. {
  981. struct property *prop;
  982. struct symbol *sym;
  983. struct expr *e;
  984. prop = sym_get_choice_prop(csym);
  985. /*
  986. * Set all non-assinged choice values to no
  987. */
  988. expr_list_for_each_sym(prop->expr, e, sym) {
  989. if (!sym_has_value(sym))
  990. sym->def[S_DEF_USER].tri = no;
  991. }
  992. csym->flags |= SYMBOL_DEF_USER;
  993. /* clear VALID to get value calculated */
  994. csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
  995. }
  996. bool conf_set_all_new_symbols(enum conf_def_mode mode)
  997. {
  998. struct symbol *sym, *csym;
  999. int i, cnt, pby, pty, ptm; /* pby: probability of boolean = y
  1000. * pty: probability of tristate = y
  1001. * ptm: probability of tristate = m
  1002. */
  1003. pby = 50; pty = ptm = 33; /* can't go as the default in switch-case
  1004. * below, otherwise gcc whines about
  1005. * -Wmaybe-uninitialized */
  1006. if (mode == def_random) {
  1007. int n, p[3];
  1008. char *env = getenv("KCONFIG_PROBABILITY");
  1009. n = 0;
  1010. while( env && *env ) {
  1011. char *endp;
  1012. int tmp = strtol( env, &endp, 10 );
  1013. if( tmp >= 0 && tmp <= 100 ) {
  1014. p[n++] = tmp;
  1015. } else {
  1016. errno = ERANGE;
  1017. perror( "KCONFIG_PROBABILITY" );
  1018. exit( 1 );
  1019. }
  1020. env = (*endp == ':') ? endp+1 : endp;
  1021. if( n >=3 ) {
  1022. break;
  1023. }
  1024. }
  1025. switch( n ) {
  1026. case 1:
  1027. pby = p[0]; ptm = pby/2; pty = pby-ptm;
  1028. break;
  1029. case 2:
  1030. pty = p[0]; ptm = p[1]; pby = pty + ptm;
  1031. break;
  1032. case 3:
  1033. pby = p[0]; pty = p[1]; ptm = p[2];
  1034. break;
  1035. }
  1036. if( pty+ptm > 100 ) {
  1037. errno = ERANGE;
  1038. perror( "KCONFIG_PROBABILITY" );
  1039. exit( 1 );
  1040. }
  1041. }
  1042. bool has_changed = false;
  1043. for_all_symbols(i, sym) {
  1044. if (sym_has_value(sym) || (sym->flags & SYMBOL_VALID))
  1045. continue;
  1046. switch (sym_get_type(sym)) {
  1047. case S_BOOLEAN:
  1048. case S_TRISTATE:
  1049. has_changed = true;
  1050. switch (mode) {
  1051. case def_yes:
  1052. sym->def[S_DEF_USER].tri = yes;
  1053. break;
  1054. case def_mod:
  1055. sym->def[S_DEF_USER].tri = mod;
  1056. break;
  1057. case def_no:
  1058. if (sym->flags & SYMBOL_ALLNOCONFIG_Y)
  1059. sym->def[S_DEF_USER].tri = yes;
  1060. else
  1061. sym->def[S_DEF_USER].tri = no;
  1062. break;
  1063. case def_random:
  1064. sym->def[S_DEF_USER].tri = no;
  1065. cnt = rand() % 100;
  1066. if (sym->type == S_TRISTATE) {
  1067. if (cnt < pty)
  1068. sym->def[S_DEF_USER].tri = yes;
  1069. else if (cnt < (pty+ptm))
  1070. sym->def[S_DEF_USER].tri = mod;
  1071. } else if (cnt < pby)
  1072. sym->def[S_DEF_USER].tri = yes;
  1073. break;
  1074. default:
  1075. continue;
  1076. }
  1077. if (!(sym_is_choice(sym) && mode == def_random))
  1078. sym->flags |= SYMBOL_DEF_USER;
  1079. break;
  1080. default:
  1081. break;
  1082. }
  1083. }
  1084. sym_clear_all_valid();
  1085. /*
  1086. * We have different type of choice blocks.
  1087. * If curr.tri equals to mod then we can select several
  1088. * choice symbols in one block.
  1089. * In this case we do nothing.
  1090. * If curr.tri equals yes then only one symbol can be
  1091. * selected in a choice block and we set it to yes,
  1092. * and the rest to no.
  1093. */
  1094. if (mode != def_random) {
  1095. for_all_symbols(i, csym) {
  1096. if ((sym_is_choice(csym) && !sym_has_value(csym)) ||
  1097. sym_is_choice_value(csym))
  1098. csym->flags |= SYMBOL_NEED_SET_CHOICE_VALUES;
  1099. }
  1100. }
  1101. for_all_symbols(i, csym) {
  1102. if (sym_has_value(csym) || !sym_is_choice(csym))
  1103. continue;
  1104. sym_calc_value(csym);
  1105. if (mode == def_random)
  1106. has_changed = randomize_choice_values(csym);
  1107. else {
  1108. set_all_choice_values(csym);
  1109. has_changed = true;
  1110. }
  1111. }
  1112. return has_changed;
  1113. }