1
0

confdata.c 23 KB

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