confdata.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. // SPDX-License-Identifier: GPL-2.0
  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 <stdbool.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <time.h>
  18. #include <unistd.h>
  19. #include <xalloc.h>
  20. #include "internal.h"
  21. #include "lkc.h"
  22. struct gstr autoconf_cmd;
  23. /* return true if 'path' exists, false otherwise */
  24. static bool is_present(const char *path)
  25. {
  26. struct stat st;
  27. return !stat(path, &st);
  28. }
  29. /* return true if 'path' exists and it is a directory, false otherwise */
  30. static bool is_dir(const char *path)
  31. {
  32. struct stat st;
  33. if (stat(path, &st))
  34. return false;
  35. return S_ISDIR(st.st_mode);
  36. }
  37. /* return true if the given two files are the same, false otherwise */
  38. static bool is_same(const char *file1, const char *file2)
  39. {
  40. int fd1, fd2;
  41. struct stat st1, st2;
  42. void *map1, *map2;
  43. bool ret = false;
  44. fd1 = open(file1, O_RDONLY);
  45. if (fd1 < 0)
  46. return ret;
  47. fd2 = open(file2, O_RDONLY);
  48. if (fd2 < 0)
  49. goto close1;
  50. ret = fstat(fd1, &st1);
  51. if (ret)
  52. goto close2;
  53. ret = fstat(fd2, &st2);
  54. if (ret)
  55. goto close2;
  56. if (st1.st_size != st2.st_size)
  57. goto close2;
  58. map1 = mmap(NULL, st1.st_size, PROT_READ, MAP_PRIVATE, fd1, 0);
  59. if (map1 == MAP_FAILED)
  60. goto close2;
  61. map2 = mmap(NULL, st2.st_size, PROT_READ, MAP_PRIVATE, fd2, 0);
  62. if (map2 == MAP_FAILED)
  63. goto close2;
  64. if (memcmp(map1, map2, st1.st_size))
  65. goto close2;
  66. ret = true;
  67. close2:
  68. close(fd2);
  69. close1:
  70. close(fd1);
  71. return ret;
  72. }
  73. /*
  74. * Create the parent directory of the given path.
  75. *
  76. * For example, if 'include/config/auto.conf' is given, create 'include/config'.
  77. */
  78. static int make_parent_dir(const char *path)
  79. {
  80. char tmp[PATH_MAX + 1];
  81. char *p;
  82. strncpy(tmp, path, sizeof(tmp));
  83. tmp[sizeof(tmp) - 1] = 0;
  84. /* Remove the base name. Just return if nothing is left */
  85. p = strrchr(tmp, '/');
  86. if (!p)
  87. return 0;
  88. *(p + 1) = 0;
  89. /* Just in case it is an absolute path */
  90. p = tmp;
  91. while (*p == '/')
  92. p++;
  93. while ((p = strchr(p, '/'))) {
  94. *p = 0;
  95. /* skip if the directory exists */
  96. if (!is_dir(tmp) && mkdir(tmp, 0755))
  97. return -1;
  98. *p = '/';
  99. while (*p == '/')
  100. p++;
  101. }
  102. return 0;
  103. }
  104. static char depfile_path[PATH_MAX];
  105. static size_t depfile_prefix_len;
  106. /* touch depfile for symbol 'name' */
  107. static int conf_touch_dep(const char *name)
  108. {
  109. int fd;
  110. /* check overflow: prefix + name + '\0' must fit in buffer. */
  111. if (depfile_prefix_len + strlen(name) + 1 > sizeof(depfile_path))
  112. return -1;
  113. strcpy(depfile_path + depfile_prefix_len, name);
  114. fd = open(depfile_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  115. if (fd == -1)
  116. return -1;
  117. close(fd);
  118. return 0;
  119. }
  120. static void conf_warning(const char *fmt, ...)
  121. __attribute__ ((format (printf, 1, 2)));
  122. static void conf_message(const char *fmt, ...)
  123. __attribute__ ((format (printf, 1, 2)));
  124. static const char *conf_filename;
  125. static int conf_lineno, conf_warnings;
  126. bool conf_errors(void)
  127. {
  128. if (conf_warnings)
  129. return getenv("KCONFIG_WERROR");
  130. return false;
  131. }
  132. static void conf_warning(const char *fmt, ...)
  133. {
  134. va_list ap;
  135. va_start(ap, fmt);
  136. fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
  137. vfprintf(stderr, fmt, ap);
  138. fprintf(stderr, "\n");
  139. va_end(ap);
  140. conf_warnings++;
  141. }
  142. static void conf_default_message_callback(const char *s)
  143. {
  144. printf("#\n# ");
  145. printf("%s", s);
  146. printf("\n#\n");
  147. }
  148. static void (*conf_message_callback)(const char *s) =
  149. conf_default_message_callback;
  150. void conf_set_message_callback(void (*fn)(const char *s))
  151. {
  152. conf_message_callback = fn;
  153. }
  154. static void conf_message(const char *fmt, ...)
  155. {
  156. va_list ap;
  157. char buf[4096];
  158. if (!conf_message_callback)
  159. return;
  160. va_start(ap, fmt);
  161. vsnprintf(buf, sizeof(buf), fmt, ap);
  162. conf_message_callback(buf);
  163. va_end(ap);
  164. }
  165. const char *conf_get_configname(void)
  166. {
  167. char *name = getenv("KCONFIG_CONFIG");
  168. return name ? name : ".config";
  169. }
  170. static const char *conf_get_autoconfig_name(void)
  171. {
  172. char *name = getenv("KCONFIG_AUTOCONFIG");
  173. return name ? name : "include/config/auto.conf";
  174. }
  175. static const char *conf_get_autoheader_name(void)
  176. {
  177. char *name = getenv("KCONFIG_AUTOHEADER");
  178. return name ? name : "include/generated/autoconf.h";
  179. }
  180. static const char *conf_get_rustccfg_name(void)
  181. {
  182. char *name = getenv("KCONFIG_RUSTCCFG");
  183. return name ? name : "include/generated/rustc_cfg";
  184. }
  185. static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
  186. {
  187. char *p2;
  188. switch (sym->type) {
  189. case S_TRISTATE:
  190. if (p[0] == 'm') {
  191. sym->def[def].tri = mod;
  192. sym->flags |= def_flags;
  193. break;
  194. }
  195. /* fall through */
  196. case S_BOOLEAN:
  197. if (p[0] == 'y') {
  198. sym->def[def].tri = yes;
  199. sym->flags |= def_flags;
  200. break;
  201. }
  202. if (p[0] == 'n') {
  203. sym->def[def].tri = no;
  204. sym->flags |= def_flags;
  205. break;
  206. }
  207. if (def != S_DEF_AUTO)
  208. conf_warning("symbol value '%s' invalid for %s",
  209. p, sym->name);
  210. return 1;
  211. case S_STRING:
  212. /* No escaping for S_DEF_AUTO (include/config/auto.conf) */
  213. if (def != S_DEF_AUTO) {
  214. if (*p++ != '"')
  215. break;
  216. for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
  217. if (*p2 == '"') {
  218. *p2 = 0;
  219. break;
  220. }
  221. memmove(p2, p2 + 1, strlen(p2));
  222. }
  223. if (!p2) {
  224. conf_warning("invalid string found");
  225. return 1;
  226. }
  227. }
  228. /* fall through */
  229. case S_INT:
  230. case S_HEX:
  231. if (sym_string_valid(sym, p)) {
  232. sym->def[def].val = xstrdup(p);
  233. sym->flags |= def_flags;
  234. } else {
  235. if (def != S_DEF_AUTO)
  236. conf_warning("symbol value '%s' invalid for %s",
  237. p, sym->name);
  238. return 1;
  239. }
  240. break;
  241. default:
  242. ;
  243. }
  244. return 0;
  245. }
  246. /* like getline(), but the newline character is stripped away */
  247. static ssize_t getline_stripped(char **lineptr, size_t *n, FILE *stream)
  248. {
  249. ssize_t len;
  250. len = getline(lineptr, n, stream);
  251. if (len > 0 && (*lineptr)[len - 1] == '\n') {
  252. len--;
  253. (*lineptr)[len] = '\0';
  254. if (len > 0 && (*lineptr)[len - 1] == '\r') {
  255. len--;
  256. (*lineptr)[len] = '\0';
  257. }
  258. }
  259. return len;
  260. }
  261. int conf_read_simple(const char *name, int def)
  262. {
  263. FILE *in = NULL;
  264. char *line = NULL;
  265. size_t line_asize = 0;
  266. char *p, *val;
  267. struct symbol *sym;
  268. int def_flags;
  269. const char *warn_unknown, *sym_name;
  270. warn_unknown = getenv("KCONFIG_WARN_UNKNOWN_SYMBOLS");
  271. if (name) {
  272. in = zconf_fopen(name);
  273. } else {
  274. char *env;
  275. name = conf_get_configname();
  276. in = zconf_fopen(name);
  277. if (in)
  278. goto load;
  279. conf_set_changed(true);
  280. env = getenv("KCONFIG_DEFCONFIG_LIST");
  281. if (!env)
  282. return 1;
  283. while (1) {
  284. bool is_last;
  285. while (isspace(*env))
  286. env++;
  287. if (!*env)
  288. break;
  289. p = env;
  290. while (*p && !isspace(*p))
  291. p++;
  292. is_last = (*p == '\0');
  293. *p = '\0';
  294. name = env;
  295. in = zconf_fopen(name);
  296. if (in) {
  297. conf_message("using defaults found in %s",
  298. name);
  299. goto load;
  300. }
  301. if (is_last)
  302. break;
  303. env = p + 1;
  304. }
  305. }
  306. if (!in)
  307. return 1;
  308. load:
  309. conf_filename = name;
  310. conf_lineno = 0;
  311. conf_warnings = 0;
  312. def_flags = SYMBOL_DEF << def;
  313. for_all_symbols(sym) {
  314. sym->flags &= ~def_flags;
  315. switch (sym->type) {
  316. case S_INT:
  317. case S_HEX:
  318. case S_STRING:
  319. free(sym->def[def].val);
  320. /* fall through */
  321. default:
  322. sym->def[def].val = NULL;
  323. sym->def[def].tri = no;
  324. }
  325. }
  326. if (def == S_DEF_USER) {
  327. for_all_symbols(sym)
  328. sym->flags &= ~SYMBOL_VALID;
  329. expr_invalidate_all();
  330. }
  331. while (getline_stripped(&line, &line_asize, in) != -1) {
  332. struct menu *choice;
  333. conf_lineno++;
  334. if (!line[0]) /* blank line */
  335. continue;
  336. if (line[0] == '#') {
  337. if (line[1] != ' ')
  338. continue;
  339. p = line + 2;
  340. if (memcmp(p, CONFIG_, strlen(CONFIG_)))
  341. continue;
  342. sym_name = p + strlen(CONFIG_);
  343. p = strchr(sym_name, ' ');
  344. if (!p)
  345. continue;
  346. *p++ = 0;
  347. if (strcmp(p, "is not set"))
  348. continue;
  349. val = "n";
  350. } else {
  351. if (memcmp(line, CONFIG_, strlen(CONFIG_))) {
  352. conf_warning("unexpected data: %s", line);
  353. continue;
  354. }
  355. sym_name = line + strlen(CONFIG_);
  356. p = strchr(sym_name, '=');
  357. if (!p) {
  358. conf_warning("unexpected data: %s", line);
  359. continue;
  360. }
  361. *p = 0;
  362. val = p + 1;
  363. }
  364. sym = sym_find(sym_name);
  365. if (!sym) {
  366. if (def == S_DEF_AUTO) {
  367. /*
  368. * Reading from include/config/auto.conf.
  369. * If CONFIG_FOO previously existed in auto.conf
  370. * but it is missing now, include/config/FOO
  371. * must be touched.
  372. */
  373. conf_touch_dep(sym_name);
  374. } else {
  375. if (warn_unknown)
  376. conf_warning("unknown symbol: %s", sym_name);
  377. conf_set_changed(true);
  378. }
  379. continue;
  380. }
  381. if (sym->flags & def_flags)
  382. conf_warning("override: reassigning to symbol %s", sym->name);
  383. if (conf_set_sym_val(sym, def, def_flags, val))
  384. continue;
  385. if (def != S_DEF_USER)
  386. continue;
  387. /*
  388. * If this is a choice member, give it the highest priority.
  389. * If conflicting CONFIG options are given from an input file,
  390. * the last one wins.
  391. */
  392. choice = sym_get_choice_menu(sym);
  393. if (choice)
  394. list_move(&sym->choice_link, &choice->choice_members);
  395. }
  396. free(line);
  397. fclose(in);
  398. return 0;
  399. }
  400. int conf_read(const char *name)
  401. {
  402. struct symbol *sym;
  403. conf_set_changed(false);
  404. if (conf_read_simple(name, S_DEF_USER)) {
  405. sym_calc_value(modules_sym);
  406. return 1;
  407. }
  408. sym_calc_value(modules_sym);
  409. for_all_symbols(sym) {
  410. sym_calc_value(sym);
  411. if (sym_is_choice(sym))
  412. continue;
  413. if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
  414. /* check that calculated value agrees with saved value */
  415. switch (sym->type) {
  416. case S_BOOLEAN:
  417. case S_TRISTATE:
  418. if (sym->def[S_DEF_USER].tri == sym_get_tristate_value(sym))
  419. continue;
  420. break;
  421. default:
  422. if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
  423. continue;
  424. break;
  425. }
  426. } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
  427. /* no previous value and not saved */
  428. continue;
  429. conf_set_changed(true);
  430. /* maybe print value in verbose mode... */
  431. }
  432. if (conf_warnings)
  433. conf_set_changed(true);
  434. return 0;
  435. }
  436. struct comment_style {
  437. const char *decoration;
  438. const char *prefix;
  439. const char *postfix;
  440. };
  441. static const struct comment_style comment_style_pound = {
  442. .decoration = "#",
  443. .prefix = "#",
  444. .postfix = "#",
  445. };
  446. static const struct comment_style comment_style_c = {
  447. .decoration = " *",
  448. .prefix = "/*",
  449. .postfix = " */",
  450. };
  451. static void conf_write_heading(FILE *fp, const struct comment_style *cs)
  452. {
  453. if (!cs)
  454. return;
  455. fprintf(fp, "%s\n", cs->prefix);
  456. fprintf(fp, "%s Automatically generated file; DO NOT EDIT.\n",
  457. cs->decoration);
  458. fprintf(fp, "%s %s\n", cs->decoration, rootmenu.prompt->text);
  459. fprintf(fp, "%s\n", cs->postfix);
  460. }
  461. /* The returned pointer must be freed on the caller side */
  462. static char *escape_string_value(const char *in)
  463. {
  464. const char *p;
  465. char *out;
  466. size_t len;
  467. len = strlen(in) + strlen("\"\"") + 1;
  468. p = in;
  469. while (1) {
  470. p += strcspn(p, "\"\\");
  471. if (p[0] == '\0')
  472. break;
  473. len++;
  474. p++;
  475. }
  476. out = xmalloc(len);
  477. out[0] = '\0';
  478. strcat(out, "\"");
  479. p = in;
  480. while (1) {
  481. len = strcspn(p, "\"\\");
  482. strncat(out, p, len);
  483. p += len;
  484. if (p[0] == '\0')
  485. break;
  486. strcat(out, "\\");
  487. strncat(out, p++, 1);
  488. }
  489. strcat(out, "\"");
  490. return out;
  491. }
  492. enum output_n { OUTPUT_N, OUTPUT_N_AS_UNSET, OUTPUT_N_NONE };
  493. static void __print_symbol(FILE *fp, struct symbol *sym, enum output_n output_n,
  494. bool escape_string)
  495. {
  496. const char *val;
  497. char *escaped = NULL;
  498. if (sym->type == S_UNKNOWN)
  499. return;
  500. val = sym_get_string_value(sym);
  501. if ((sym->type == S_BOOLEAN || sym->type == S_TRISTATE) &&
  502. output_n != OUTPUT_N && *val == 'n') {
  503. if (output_n == OUTPUT_N_AS_UNSET)
  504. fprintf(fp, "# %s%s is not set\n", CONFIG_, sym->name);
  505. return;
  506. }
  507. if (sym->type == S_STRING && escape_string) {
  508. escaped = escape_string_value(val);
  509. val = escaped;
  510. }
  511. fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, val);
  512. free(escaped);
  513. }
  514. static void print_symbol_for_dotconfig(FILE *fp, struct symbol *sym)
  515. {
  516. __print_symbol(fp, sym, OUTPUT_N_AS_UNSET, true);
  517. }
  518. static void print_symbol_for_autoconf(FILE *fp, struct symbol *sym)
  519. {
  520. __print_symbol(fp, sym, OUTPUT_N_NONE, false);
  521. }
  522. void print_symbol_for_listconfig(struct symbol *sym)
  523. {
  524. __print_symbol(stdout, sym, OUTPUT_N, true);
  525. }
  526. static void print_symbol_for_c(FILE *fp, struct symbol *sym)
  527. {
  528. const char *val;
  529. const char *sym_suffix = "";
  530. const char *val_prefix = "";
  531. char *escaped = NULL;
  532. if (sym->type == S_UNKNOWN)
  533. return;
  534. val = sym_get_string_value(sym);
  535. switch (sym->type) {
  536. case S_BOOLEAN:
  537. case S_TRISTATE:
  538. switch (*val) {
  539. case 'n':
  540. return;
  541. case 'm':
  542. sym_suffix = "_MODULE";
  543. /* fall through */
  544. default:
  545. val = "1";
  546. }
  547. break;
  548. case S_HEX:
  549. if (val[0] != '0' || (val[1] != 'x' && val[1] != 'X'))
  550. val_prefix = "0x";
  551. break;
  552. case S_STRING:
  553. escaped = escape_string_value(val);
  554. val = escaped;
  555. default:
  556. break;
  557. }
  558. fprintf(fp, "#define %s%s%s %s%s\n", CONFIG_, sym->name, sym_suffix,
  559. val_prefix, val);
  560. free(escaped);
  561. }
  562. static void print_symbol_for_rustccfg(FILE *fp, struct symbol *sym)
  563. {
  564. const char *val;
  565. const char *val_prefix = "";
  566. char *val_prefixed = NULL;
  567. size_t val_prefixed_len;
  568. char *escaped = NULL;
  569. if (sym->type == S_UNKNOWN)
  570. return;
  571. val = sym_get_string_value(sym);
  572. switch (sym->type) {
  573. case S_BOOLEAN:
  574. case S_TRISTATE:
  575. /*
  576. * We do not care about disabled ones, i.e. no need for
  577. * what otherwise are "comments" in other printers.
  578. */
  579. if (*val == 'n')
  580. return;
  581. /*
  582. * To have similar functionality to the C macro `IS_ENABLED()`
  583. * we provide an empty `--cfg CONFIG_X` here in both `y`
  584. * and `m` cases.
  585. *
  586. * Then, the common `fprintf()` below will also give us
  587. * a `--cfg CONFIG_X="y"` or `--cfg CONFIG_X="m"`, which can
  588. * be used as the equivalent of `IS_BUILTIN()`/`IS_MODULE()`.
  589. */
  590. fprintf(fp, "--cfg=%s%s\n", CONFIG_, sym->name);
  591. break;
  592. case S_HEX:
  593. if (val[0] != '0' || (val[1] != 'x' && val[1] != 'X'))
  594. val_prefix = "0x";
  595. break;
  596. default:
  597. break;
  598. }
  599. if (strlen(val_prefix) > 0) {
  600. val_prefixed_len = strlen(val) + strlen(val_prefix) + 1;
  601. val_prefixed = xmalloc(val_prefixed_len);
  602. snprintf(val_prefixed, val_prefixed_len, "%s%s", val_prefix, val);
  603. val = val_prefixed;
  604. }
  605. /* All values get escaped: the `--cfg` option only takes strings */
  606. escaped = escape_string_value(val);
  607. val = escaped;
  608. fprintf(fp, "--cfg=%s%s=%s\n", CONFIG_, sym->name, val);
  609. free(escaped);
  610. free(val_prefixed);
  611. }
  612. /*
  613. * Write out a minimal config.
  614. * All values that has default values are skipped as this is redundant.
  615. */
  616. int conf_write_defconfig(const char *filename)
  617. {
  618. struct symbol *sym;
  619. struct menu *menu;
  620. FILE *out;
  621. out = fopen(filename, "w");
  622. if (!out)
  623. return 1;
  624. sym_clear_all_valid();
  625. menu_for_each_entry(menu) {
  626. struct menu *choice;
  627. sym = menu->sym;
  628. if (!sym || sym_is_choice(sym))
  629. continue;
  630. sym_calc_value(sym);
  631. if (!(sym->flags & SYMBOL_WRITE))
  632. continue;
  633. sym->flags &= ~SYMBOL_WRITE;
  634. /* Skip unchangeable symbols */
  635. if (!sym_is_changeable(sym))
  636. continue;
  637. /* Skip symbols that are equal to the default */
  638. if (!strcmp(sym_get_string_value(sym), sym_get_string_default(sym)))
  639. continue;
  640. /* Skip choice values that are equal to the default */
  641. choice = sym_get_choice_menu(sym);
  642. if (choice) {
  643. struct symbol *ds;
  644. ds = sym_choice_default(choice);
  645. if (sym == ds && sym_get_tristate_value(sym) == yes)
  646. continue;
  647. }
  648. print_symbol_for_dotconfig(out, sym);
  649. }
  650. fclose(out);
  651. return 0;
  652. }
  653. int conf_write(const char *name)
  654. {
  655. FILE *out;
  656. struct symbol *sym;
  657. struct menu *menu;
  658. const char *str;
  659. char tmpname[PATH_MAX + 1], oldname[PATH_MAX + 1];
  660. char *env;
  661. bool need_newline = false;
  662. if (!name)
  663. name = conf_get_configname();
  664. if (!*name) {
  665. fprintf(stderr, "config name is empty\n");
  666. return -1;
  667. }
  668. if (is_dir(name)) {
  669. fprintf(stderr, "%s: Is a directory\n", name);
  670. return -1;
  671. }
  672. if (make_parent_dir(name))
  673. return -1;
  674. env = getenv("KCONFIG_OVERWRITECONFIG");
  675. if (env && *env) {
  676. *tmpname = 0;
  677. out = fopen(name, "w");
  678. } else {
  679. snprintf(tmpname, sizeof(tmpname), "%s.%d.tmp",
  680. name, (int)getpid());
  681. out = fopen(tmpname, "w");
  682. }
  683. if (!out)
  684. return 1;
  685. conf_write_heading(out, &comment_style_pound);
  686. if (!conf_get_changed())
  687. sym_clear_all_valid();
  688. menu = rootmenu.list;
  689. while (menu) {
  690. sym = menu->sym;
  691. if (!sym) {
  692. if (!menu_is_visible(menu))
  693. goto next;
  694. str = menu_get_prompt(menu);
  695. fprintf(out, "\n"
  696. "#\n"
  697. "# %s\n"
  698. "#\n", str);
  699. need_newline = false;
  700. } else if (!sym_is_choice(sym) &&
  701. !(sym->flags & SYMBOL_WRITTEN)) {
  702. sym_calc_value(sym);
  703. if (!(sym->flags & SYMBOL_WRITE))
  704. goto next;
  705. if (need_newline) {
  706. fprintf(out, "\n");
  707. need_newline = false;
  708. }
  709. sym->flags |= SYMBOL_WRITTEN;
  710. print_symbol_for_dotconfig(out, sym);
  711. }
  712. next:
  713. if (menu->list) {
  714. menu = menu->list;
  715. continue;
  716. }
  717. end_check:
  718. if (!menu->sym && menu_is_visible(menu) && menu != &rootmenu &&
  719. menu->prompt->type == P_MENU) {
  720. fprintf(out, "# end of %s\n", menu_get_prompt(menu));
  721. need_newline = true;
  722. }
  723. if (menu->next) {
  724. menu = menu->next;
  725. } else {
  726. menu = menu->parent;
  727. if (menu)
  728. goto end_check;
  729. }
  730. }
  731. fclose(out);
  732. for_all_symbols(sym)
  733. sym->flags &= ~SYMBOL_WRITTEN;
  734. if (*tmpname) {
  735. if (is_same(name, tmpname)) {
  736. conf_message("No change to %s", name);
  737. unlink(tmpname);
  738. conf_set_changed(false);
  739. return 0;
  740. }
  741. snprintf(oldname, sizeof(oldname), "%s.old", name);
  742. rename(name, oldname);
  743. if (rename(tmpname, name))
  744. return 1;
  745. }
  746. conf_message("configuration written to %s", name);
  747. conf_set_changed(false);
  748. return 0;
  749. }
  750. /* write a dependency file as used by kbuild to track dependencies */
  751. static int conf_write_autoconf_cmd(const char *autoconf_name)
  752. {
  753. char name[PATH_MAX], tmp[PATH_MAX];
  754. FILE *out;
  755. int ret;
  756. ret = snprintf(name, sizeof(name), "%s.cmd", autoconf_name);
  757. if (ret >= sizeof(name)) /* check truncation */
  758. return -1;
  759. if (make_parent_dir(name))
  760. return -1;
  761. ret = snprintf(tmp, sizeof(tmp), "%s.cmd.tmp", autoconf_name);
  762. if (ret >= sizeof(tmp)) /* check truncation */
  763. return -1;
  764. out = fopen(tmp, "w");
  765. if (!out) {
  766. perror("fopen");
  767. return -1;
  768. }
  769. fprintf(out, "autoconfig := %s\n", autoconf_name);
  770. fputs(str_get(&autoconf_cmd), out);
  771. fflush(out);
  772. ret = ferror(out); /* error check for all fprintf() calls */
  773. fclose(out);
  774. if (ret)
  775. return -1;
  776. if (rename(tmp, name)) {
  777. perror("rename");
  778. return -1;
  779. }
  780. return 0;
  781. }
  782. static int conf_touch_deps(void)
  783. {
  784. const char *name, *tmp;
  785. struct symbol *sym;
  786. int res;
  787. name = conf_get_autoconfig_name();
  788. tmp = strrchr(name, '/');
  789. depfile_prefix_len = tmp ? tmp - name + 1 : 0;
  790. if (depfile_prefix_len + 1 > sizeof(depfile_path))
  791. return -1;
  792. strncpy(depfile_path, name, depfile_prefix_len);
  793. depfile_path[depfile_prefix_len] = 0;
  794. conf_read_simple(name, S_DEF_AUTO);
  795. for_all_symbols(sym) {
  796. if (sym_is_choice(sym))
  797. continue;
  798. if (sym->flags & SYMBOL_WRITE) {
  799. if (sym->flags & SYMBOL_DEF_AUTO) {
  800. /*
  801. * symbol has old and new value,
  802. * so compare them...
  803. */
  804. switch (sym->type) {
  805. case S_BOOLEAN:
  806. case S_TRISTATE:
  807. if (sym_get_tristate_value(sym) ==
  808. sym->def[S_DEF_AUTO].tri)
  809. continue;
  810. break;
  811. case S_STRING:
  812. case S_HEX:
  813. case S_INT:
  814. if (!strcmp(sym_get_string_value(sym),
  815. sym->def[S_DEF_AUTO].val))
  816. continue;
  817. break;
  818. default:
  819. break;
  820. }
  821. } else {
  822. /*
  823. * If there is no old value, only 'no' (unset)
  824. * is allowed as new value.
  825. */
  826. switch (sym->type) {
  827. case S_BOOLEAN:
  828. case S_TRISTATE:
  829. if (sym_get_tristate_value(sym) == no)
  830. continue;
  831. break;
  832. default:
  833. break;
  834. }
  835. }
  836. } else if (!(sym->flags & SYMBOL_DEF_AUTO))
  837. /* There is neither an old nor a new value. */
  838. continue;
  839. /* else
  840. * There is an old value, but no new value ('no' (unset)
  841. * isn't saved in auto.conf, so the old value is always
  842. * different from 'no').
  843. */
  844. res = conf_touch_dep(sym->name);
  845. if (res)
  846. return res;
  847. }
  848. return 0;
  849. }
  850. static int __conf_write_autoconf(const char *filename,
  851. void (*print_symbol)(FILE *, struct symbol *),
  852. const struct comment_style *comment_style)
  853. {
  854. char tmp[PATH_MAX];
  855. FILE *file;
  856. struct symbol *sym;
  857. int ret;
  858. if (make_parent_dir(filename))
  859. return -1;
  860. ret = snprintf(tmp, sizeof(tmp), "%s.tmp", filename);
  861. if (ret >= sizeof(tmp)) /* check truncation */
  862. return -1;
  863. file = fopen(tmp, "w");
  864. if (!file) {
  865. perror("fopen");
  866. return -1;
  867. }
  868. conf_write_heading(file, comment_style);
  869. for_all_symbols(sym)
  870. if ((sym->flags & SYMBOL_WRITE) && sym->name)
  871. print_symbol(file, sym);
  872. fflush(file);
  873. /* check possible errors in conf_write_heading() and print_symbol() */
  874. ret = ferror(file);
  875. fclose(file);
  876. if (ret)
  877. return -1;
  878. if (rename(tmp, filename)) {
  879. perror("rename");
  880. return -1;
  881. }
  882. return 0;
  883. }
  884. int conf_write_autoconf(int overwrite)
  885. {
  886. struct symbol *sym;
  887. const char *autoconf_name = conf_get_autoconfig_name();
  888. int ret;
  889. if (!overwrite && is_present(autoconf_name))
  890. return 0;
  891. ret = conf_write_autoconf_cmd(autoconf_name);
  892. if (ret)
  893. return -1;
  894. for_all_symbols(sym)
  895. sym_calc_value(sym);
  896. if (conf_touch_deps())
  897. return 1;
  898. ret = __conf_write_autoconf(conf_get_autoheader_name(),
  899. print_symbol_for_c,
  900. &comment_style_c);
  901. if (ret)
  902. return ret;
  903. ret = __conf_write_autoconf(conf_get_rustccfg_name(),
  904. print_symbol_for_rustccfg,
  905. NULL);
  906. if (ret)
  907. return ret;
  908. /*
  909. * Create include/config/auto.conf. This must be the last step because
  910. * Kbuild has a dependency on auto.conf and this marks the successful
  911. * completion of the previous steps.
  912. */
  913. ret = __conf_write_autoconf(conf_get_autoconfig_name(),
  914. print_symbol_for_autoconf,
  915. &comment_style_pound);
  916. if (ret)
  917. return ret;
  918. return 0;
  919. }
  920. static bool conf_changed;
  921. static void (*conf_changed_callback)(bool);
  922. void conf_set_changed(bool val)
  923. {
  924. if (conf_changed_callback && conf_changed != val)
  925. conf_changed_callback(val);
  926. conf_changed = val;
  927. }
  928. bool conf_get_changed(void)
  929. {
  930. return conf_changed;
  931. }
  932. void conf_set_changed_callback(void (*fn)(bool))
  933. {
  934. conf_changed_callback = fn;
  935. }