confdata.c 25 KB

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