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