1
0

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