confdata.c 27 KB

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