confdata.c 27 KB

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