confdata.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2002 Roman Zippel <[email protected]>
  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. void conf_reset(int def)
  291. {
  292. struct symbol *sym;
  293. int i, def_flags;
  294. def_flags = SYMBOL_DEF << def;
  295. for_all_symbols(i, sym) {
  296. sym->flags |= SYMBOL_CHANGED;
  297. sym->flags &= ~(def_flags|SYMBOL_VALID);
  298. if (sym_is_choice(sym))
  299. sym->flags |= def_flags;
  300. switch (sym->type) {
  301. case S_INT:
  302. case S_HEX:
  303. case S_STRING:
  304. if (sym->def[def].val)
  305. free(sym->def[def].val);
  306. /* fall through */
  307. default:
  308. sym->def[def].val = NULL;
  309. sym->def[def].tri = no;
  310. }
  311. }
  312. }
  313. int conf_read_simple(const char *name, int def)
  314. {
  315. FILE *in = NULL;
  316. char *line = NULL;
  317. size_t line_asize = 0;
  318. char *p, *p2;
  319. struct symbol *sym;
  320. int def_flags;
  321. if (name) {
  322. in = zconf_fopen(name);
  323. } else {
  324. struct property *prop;
  325. name = conf_get_configname();
  326. in = zconf_fopen(name);
  327. if (in)
  328. goto load;
  329. sym_add_change_count(1);
  330. if (!sym_defconfig_list)
  331. return 1;
  332. for_all_defaults(sym_defconfig_list, prop) {
  333. if (expr_calc_value(prop->visible.expr) == no ||
  334. prop->expr->type != E_SYMBOL)
  335. continue;
  336. sym_calc_value(prop->expr->left.sym);
  337. name = sym_get_string_value(prop->expr->left.sym);
  338. in = zconf_fopen(name);
  339. if (in) {
  340. conf_message("using defaults found in %s",
  341. name);
  342. goto load;
  343. }
  344. }
  345. }
  346. if (!in)
  347. return 1;
  348. load:
  349. conf_filename = name;
  350. conf_lineno = 0;
  351. conf_warnings = 0;
  352. def_flags = SYMBOL_DEF << def;
  353. conf_reset(def);
  354. while (compat_getline(&line, &line_asize, in) != -1) {
  355. conf_lineno++;
  356. sym = NULL;
  357. if (line[0] == '#') {
  358. if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
  359. continue;
  360. p = strchr(line + 2 + strlen(CONFIG_), ' ');
  361. if (!p)
  362. continue;
  363. *p++ = 0;
  364. if (strncmp(p, "is not set", 10))
  365. continue;
  366. if (def == S_DEF_USER) {
  367. sym = sym_find(line + 2 + strlen(CONFIG_));
  368. if (!sym) {
  369. sym_add_change_count(1);
  370. continue;
  371. }
  372. } else {
  373. sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
  374. if (sym->type == S_UNKNOWN)
  375. sym->type = S_BOOLEAN;
  376. }
  377. switch (sym->type) {
  378. case S_BOOLEAN:
  379. case S_TRISTATE:
  380. sym->def[def].tri = no;
  381. sym->flags |= def_flags;
  382. break;
  383. default:
  384. ;
  385. }
  386. } else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
  387. p = strchr(line + strlen(CONFIG_), '=');
  388. if (!p)
  389. continue;
  390. *p++ = 0;
  391. p2 = strchr(p, '\n');
  392. if (p2) {
  393. *p2-- = 0;
  394. if (*p2 == '\r')
  395. *p2 = 0;
  396. }
  397. sym = sym_find(line + strlen(CONFIG_));
  398. if (!sym) {
  399. if (def == S_DEF_AUTO)
  400. /*
  401. * Reading from include/config/auto.conf
  402. * If CONFIG_FOO previously existed in
  403. * auto.conf but it is missing now,
  404. * include/config/foo.h must be touched.
  405. */
  406. conf_touch_dep(line + strlen(CONFIG_));
  407. else
  408. sym_add_change_count(1);
  409. continue;
  410. }
  411. if (conf_set_sym_val(sym, def, def_flags, p))
  412. continue;
  413. } else {
  414. if (line[0] != '\r' && line[0] != '\n')
  415. conf_warning("unexpected data: %.*s",
  416. (int)strcspn(line, "\r\n"), line);
  417. continue;
  418. }
  419. if (sym && sym_is_choice_value(sym)) {
  420. struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
  421. switch (sym->def[def].tri) {
  422. case no:
  423. break;
  424. case mod:
  425. if (cs->def[def].tri == yes) {
  426. conf_warning("%s creates inconsistent choice state", sym->name);
  427. cs->flags &= ~def_flags;
  428. }
  429. break;
  430. case yes:
  431. if (cs->def[def].tri != no)
  432. conf_warning("override: %s changes choice state", sym->name);
  433. cs->def[def].val = sym;
  434. break;
  435. }
  436. cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
  437. }
  438. }
  439. free(line);
  440. fclose(in);
  441. return 0;
  442. }
  443. int conf_read(const char *name)
  444. {
  445. struct symbol *sym;
  446. int conf_unsaved = 0;
  447. int i;
  448. sym_set_change_count(0);
  449. if (conf_read_simple(name, S_DEF_USER)) {
  450. sym_calc_value(modules_sym);
  451. return 1;
  452. }
  453. sym_calc_value(modules_sym);
  454. for_all_symbols(i, sym) {
  455. sym_calc_value(sym);
  456. if (sym_is_choice(sym) || (sym->flags & SYMBOL_NO_WRITE))
  457. continue;
  458. if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
  459. /* check that calculated value agrees with saved value */
  460. switch (sym->type) {
  461. case S_BOOLEAN:
  462. case S_TRISTATE:
  463. if (sym->def[S_DEF_USER].tri == sym_get_tristate_value(sym))
  464. continue;
  465. break;
  466. default:
  467. if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
  468. continue;
  469. break;
  470. }
  471. } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
  472. /* no previous value and not saved */
  473. continue;
  474. conf_unsaved++;
  475. /* maybe print value in verbose mode... */
  476. }
  477. for_all_symbols(i, sym) {
  478. if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
  479. /* Reset values of generates values, so they'll appear
  480. * as new, if they should become visible, but that
  481. * doesn't quite work if the Kconfig and the saved
  482. * configuration disagree.
  483. */
  484. if (sym->visible == no && !conf_unsaved)
  485. sym->flags &= ~SYMBOL_DEF_USER;
  486. switch (sym->type) {
  487. case S_STRING:
  488. case S_INT:
  489. case S_HEX:
  490. /* Reset a string value if it's out of range */
  491. if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
  492. break;
  493. sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
  494. conf_unsaved++;
  495. break;
  496. default:
  497. break;
  498. }
  499. }
  500. }
  501. sym_add_change_count(conf_warnings || conf_unsaved);
  502. return 0;
  503. }
  504. /*
  505. * Kconfig configuration printer
  506. *
  507. * This printer is used when generating the resulting configuration after
  508. * kconfig invocation and `defconfig' files. Unset symbol might be omitted by
  509. * passing a non-NULL argument to the printer.
  510. *
  511. */
  512. static void
  513. kconfig_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
  514. {
  515. switch (sym->type) {
  516. case S_BOOLEAN:
  517. case S_TRISTATE:
  518. if (*value == 'n') {
  519. bool skip_unset = (arg != NULL);
  520. if (!skip_unset)
  521. fprintf(fp, "# %s%s is not set\n",
  522. CONFIG_, sym->name);
  523. return;
  524. }
  525. break;
  526. default:
  527. break;
  528. }
  529. fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, value);
  530. }
  531. static void
  532. kconfig_print_comment(FILE *fp, const char *value, void *arg)
  533. {
  534. const char *p = value;
  535. size_t l;
  536. for (;;) {
  537. l = strcspn(p, "\n");
  538. fprintf(fp, "#");
  539. if (l) {
  540. fprintf(fp, " ");
  541. xfwrite(p, l, 1, fp);
  542. p += l;
  543. }
  544. fprintf(fp, "\n");
  545. if (*p++ == '\0')
  546. break;
  547. }
  548. }
  549. static struct conf_printer kconfig_printer_cb =
  550. {
  551. .print_symbol = kconfig_print_symbol,
  552. .print_comment = kconfig_print_comment,
  553. };
  554. /*
  555. * Header printer
  556. *
  557. * This printer is used when generating the `include/generated/autoconf.h' file.
  558. */
  559. static void
  560. header_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
  561. {
  562. switch (sym->type) {
  563. case S_BOOLEAN:
  564. case S_TRISTATE: {
  565. const char *suffix = "";
  566. switch (*value) {
  567. case 'n':
  568. break;
  569. case 'm':
  570. suffix = "_MODULE";
  571. /* fall through */
  572. default:
  573. fprintf(fp, "#define %s%s%s 1\n",
  574. CONFIG_, sym->name, suffix);
  575. }
  576. break;
  577. }
  578. case S_HEX: {
  579. const char *prefix = "";
  580. if (value[0] != '0' || (value[1] != 'x' && value[1] != 'X'))
  581. prefix = "0x";
  582. fprintf(fp, "#define %s%s %s%s\n",
  583. CONFIG_, sym->name, prefix, value);
  584. break;
  585. }
  586. case S_STRING:
  587. case S_INT:
  588. fprintf(fp, "#define %s%s %s\n",
  589. CONFIG_, sym->name, value);
  590. break;
  591. default:
  592. break;
  593. }
  594. }
  595. static void
  596. header_print_comment(FILE *fp, const char *value, void *arg)
  597. {
  598. const char *p = value;
  599. size_t l;
  600. fprintf(fp, "/*\n");
  601. for (;;) {
  602. l = strcspn(p, "\n");
  603. fprintf(fp, " *");
  604. if (l) {
  605. fprintf(fp, " ");
  606. xfwrite(p, l, 1, fp);
  607. p += l;
  608. }
  609. fprintf(fp, "\n");
  610. if (*p++ == '\0')
  611. break;
  612. }
  613. fprintf(fp, " */\n");
  614. }
  615. static struct conf_printer header_printer_cb =
  616. {
  617. .print_symbol = header_print_symbol,
  618. .print_comment = header_print_comment,
  619. };
  620. static void conf_write_symbol(FILE *fp, struct symbol *sym,
  621. struct conf_printer *printer, void *printer_arg)
  622. {
  623. const char *str;
  624. switch (sym->type) {
  625. case S_UNKNOWN:
  626. break;
  627. case S_STRING:
  628. str = sym_get_string_value(sym);
  629. str = sym_escape_string_value(str);
  630. printer->print_symbol(fp, sym, str, printer_arg);
  631. free((void *)str);
  632. break;
  633. default:
  634. str = sym_get_string_value(sym);
  635. printer->print_symbol(fp, sym, str, printer_arg);
  636. }
  637. }
  638. static void
  639. conf_write_heading(FILE *fp, struct conf_printer *printer, void *printer_arg)
  640. {
  641. char buf[256];
  642. snprintf(buf, sizeof(buf),
  643. "\n"
  644. "Automatically generated file; DO NOT EDIT.\n"
  645. "%s\n",
  646. rootmenu.prompt->text);
  647. printer->print_comment(fp, buf, printer_arg);
  648. }
  649. /*
  650. * Write out a minimal config.
  651. * All values that has default values are skipped as this is redundant.
  652. */
  653. int conf_write_defconfig(const char *filename)
  654. {
  655. struct symbol *sym;
  656. struct menu *menu;
  657. FILE *out;
  658. out = fopen(filename, "w");
  659. if (!out)
  660. return 1;
  661. sym_clear_all_valid();
  662. /* Traverse all menus to find all relevant symbols */
  663. menu = rootmenu.list;
  664. while (menu != NULL)
  665. {
  666. sym = menu->sym;
  667. if (sym == NULL) {
  668. if (!menu_is_visible(menu))
  669. goto next_menu;
  670. } else if (!sym_is_choice(sym)) {
  671. sym_calc_value(sym);
  672. if (!(sym->flags & SYMBOL_WRITE))
  673. goto next_menu;
  674. sym->flags &= ~SYMBOL_WRITE;
  675. /* If we cannot change the symbol - skip */
  676. if (!sym_is_changeable(sym))
  677. goto next_menu;
  678. /* If symbol equals to default value - skip */
  679. if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
  680. goto next_menu;
  681. /*
  682. * If symbol is a choice value and equals to the
  683. * default for a choice - skip.
  684. * But only if value is bool and equal to "y" and
  685. * choice is not "optional".
  686. * (If choice is "optional" then all values can be "n")
  687. */
  688. if (sym_is_choice_value(sym)) {
  689. struct symbol *cs;
  690. struct symbol *ds;
  691. cs = prop_get_symbol(sym_get_choice_prop(sym));
  692. ds = sym_choice_default(cs);
  693. if (!sym_is_optional(cs) && sym == ds) {
  694. if ((sym->type == S_BOOLEAN) &&
  695. sym_get_tristate_value(sym) == yes)
  696. goto next_menu;
  697. }
  698. }
  699. conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
  700. }
  701. next_menu:
  702. if (menu->list != NULL) {
  703. menu = menu->list;
  704. }
  705. else if (menu->next != NULL) {
  706. menu = menu->next;
  707. } else {
  708. while ((menu = menu->parent)) {
  709. if (menu->next != NULL) {
  710. menu = menu->next;
  711. break;
  712. }
  713. }
  714. }
  715. }
  716. fclose(out);
  717. return 0;
  718. }
  719. int conf_write(const char *name)
  720. {
  721. FILE *out;
  722. struct symbol *sym;
  723. struct menu *menu;
  724. const char *str;
  725. char tmpname[PATH_MAX + 1], oldname[PATH_MAX + 1];
  726. char *env;
  727. int i;
  728. bool need_newline = false;
  729. if (!name)
  730. name = conf_get_configname();
  731. if (!*name) {
  732. fprintf(stderr, "config name is empty\n");
  733. return -1;
  734. }
  735. if (is_dir(name)) {
  736. fprintf(stderr, "%s: Is a directory\n", name);
  737. return -1;
  738. }
  739. if (make_parent_dir(name))
  740. return -1;
  741. env = getenv("KCONFIG_OVERWRITECONFIG");
  742. if (env && *env) {
  743. *tmpname = 0;
  744. out = fopen(name, "w");
  745. } else {
  746. snprintf(tmpname, sizeof(tmpname), "%s.%d.tmp",
  747. name, (int)getpid());
  748. out = fopen(tmpname, "w");
  749. }
  750. if (!out)
  751. return 1;
  752. conf_write_heading(out, &kconfig_printer_cb, NULL);
  753. if (!conf_get_changed())
  754. sym_clear_all_valid();
  755. menu = rootmenu.list;
  756. while (menu) {
  757. sym = menu->sym;
  758. if (!sym) {
  759. if (!menu_is_visible(menu))
  760. goto next;
  761. str = menu_get_prompt(menu);
  762. fprintf(out, "\n"
  763. "#\n"
  764. "# %s\n"
  765. "#\n", str);
  766. need_newline = false;
  767. } else if (!(sym->flags & SYMBOL_CHOICE) &&
  768. !(sym->flags & SYMBOL_WRITTEN)) {
  769. sym_calc_value(sym);
  770. if (!(sym->flags & SYMBOL_WRITE))
  771. goto next;
  772. if (need_newline) {
  773. fprintf(out, "\n");
  774. need_newline = false;
  775. }
  776. sym->flags |= SYMBOL_WRITTEN;
  777. conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
  778. }
  779. next:
  780. if (menu->list) {
  781. menu = menu->list;
  782. continue;
  783. }
  784. if (menu->next)
  785. menu = menu->next;
  786. else while ((menu = menu->parent)) {
  787. if (!menu->sym && menu_is_visible(menu) &&
  788. menu != &rootmenu) {
  789. str = menu_get_prompt(menu);
  790. fprintf(out, "# end of %s\n", str);
  791. need_newline = true;
  792. }
  793. if (menu->next) {
  794. menu = menu->next;
  795. break;
  796. }
  797. }
  798. }
  799. fclose(out);
  800. for_all_symbols(i, sym)
  801. sym->flags &= ~SYMBOL_WRITTEN;
  802. if (*tmpname) {
  803. if (is_same(name, tmpname)) {
  804. conf_message("No change to %s", name);
  805. unlink(tmpname);
  806. sym_set_change_count(0);
  807. return 0;
  808. }
  809. snprintf(oldname, sizeof(oldname), "%s.old", name);
  810. rename(name, oldname);
  811. if (rename(tmpname, name))
  812. return 1;
  813. }
  814. conf_message("configuration written to %s", name);
  815. sym_set_change_count(0);
  816. return 0;
  817. }
  818. /* write a dependency file as used by kbuild to track dependencies */
  819. static int conf_write_dep(const char *name)
  820. {
  821. struct file *file;
  822. FILE *out;
  823. out = fopen("..config.tmp", "w");
  824. if (!out)
  825. return 1;
  826. fprintf(out, "deps_config := \\\n");
  827. for (file = file_list; file; file = file->next) {
  828. if (file->next)
  829. fprintf(out, "\t%s \\\n", file->name);
  830. else
  831. fprintf(out, "\t%s\n", file->name);
  832. }
  833. fprintf(out, "\n%s: \\\n"
  834. "\t$(deps_config)\n\n", conf_get_autoconfig_name());
  835. env_write_dep(out, conf_get_autoconfig_name());
  836. fprintf(out, "\n$(deps_config): ;\n");
  837. fclose(out);
  838. if (make_parent_dir(name))
  839. return 1;
  840. rename("..config.tmp", name);
  841. return 0;
  842. }
  843. static int conf_touch_deps(void)
  844. {
  845. const char *name;
  846. struct symbol *sym;
  847. int res, i;
  848. strcpy(depfile_path, "include/config/");
  849. depfile_prefix_len = strlen(depfile_path);
  850. name = conf_get_autoconfig_name();
  851. conf_read_simple(name, S_DEF_AUTO);
  852. sym_calc_value(modules_sym);
  853. for_all_symbols(i, sym) {
  854. sym_calc_value(sym);
  855. if ((sym->flags & SYMBOL_NO_WRITE) || !sym->name)
  856. continue;
  857. if (sym->flags & SYMBOL_WRITE) {
  858. if (sym->flags & SYMBOL_DEF_AUTO) {
  859. /*
  860. * symbol has old and new value,
  861. * so compare them...
  862. */
  863. switch (sym->type) {
  864. case S_BOOLEAN:
  865. case S_TRISTATE:
  866. if (sym_get_tristate_value(sym) ==
  867. sym->def[S_DEF_AUTO].tri)
  868. continue;
  869. break;
  870. case S_STRING:
  871. case S_HEX:
  872. case S_INT:
  873. if (!strcmp(sym_get_string_value(sym),
  874. sym->def[S_DEF_AUTO].val))
  875. continue;
  876. break;
  877. default:
  878. break;
  879. }
  880. } else {
  881. /*
  882. * If there is no old value, only 'no' (unset)
  883. * is allowed as new value.
  884. */
  885. switch (sym->type) {
  886. case S_BOOLEAN:
  887. case S_TRISTATE:
  888. if (sym_get_tristate_value(sym) == no)
  889. continue;
  890. break;
  891. default:
  892. break;
  893. }
  894. }
  895. } else if (!(sym->flags & SYMBOL_DEF_AUTO))
  896. /* There is neither an old nor a new value. */
  897. continue;
  898. /* else
  899. * There is an old value, but no new value ('no' (unset)
  900. * isn't saved in auto.conf, so the old value is always
  901. * different from 'no').
  902. */
  903. res = conf_touch_dep(sym->name);
  904. if (res)
  905. return res;
  906. }
  907. return 0;
  908. }
  909. int conf_write_autoconf(int overwrite)
  910. {
  911. struct symbol *sym;
  912. const char *name;
  913. const char *autoconf_name = conf_get_autoconfig_name();
  914. FILE *out, *out_h;
  915. int i;
  916. #ifndef OPENWRT_DOES_NOT_WANT_THIS
  917. return 0;
  918. #endif
  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. sym_clear_all_valid();
  1089. for_all_symbols(i, sym) {
  1090. if (sym_has_value(sym) || (sym->flags & SYMBOL_VALID))
  1091. continue;
  1092. switch (sym_get_type(sym)) {
  1093. case S_BOOLEAN:
  1094. case S_TRISTATE:
  1095. has_changed = true;
  1096. switch (mode) {
  1097. case def_yes:
  1098. sym->def[S_DEF_USER].tri = yes;
  1099. break;
  1100. case def_mod:
  1101. sym->def[S_DEF_USER].tri = mod;
  1102. break;
  1103. case def_no:
  1104. if (sym->flags & SYMBOL_ALLNOCONFIG_Y)
  1105. sym->def[S_DEF_USER].tri = yes;
  1106. else
  1107. sym->def[S_DEF_USER].tri = no;
  1108. break;
  1109. case def_random:
  1110. sym->def[S_DEF_USER].tri = no;
  1111. cnt = rand() % 100;
  1112. if (sym->type == S_TRISTATE) {
  1113. if (cnt < pty)
  1114. sym->def[S_DEF_USER].tri = yes;
  1115. else if (cnt < (pty+ptm))
  1116. sym->def[S_DEF_USER].tri = mod;
  1117. } else if (cnt < pby)
  1118. sym->def[S_DEF_USER].tri = yes;
  1119. break;
  1120. default:
  1121. continue;
  1122. }
  1123. if (!(sym_is_choice(sym) && mode == def_random))
  1124. sym->flags |= SYMBOL_DEF_USER;
  1125. break;
  1126. default:
  1127. break;
  1128. }
  1129. }
  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. sym_add_change_count(1);
  1170. }
  1171. }
  1172. }