confdata.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  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. /* Last char has to be a '"' */
  166. if (p[strlen(p) - 1] != '"') {
  167. if (def != S_DEF_AUTO)
  168. conf_warning("invalid string found");
  169. return 1;
  170. }
  171. /* Overwrite '"' with \0 for string termination */
  172. p[strlen(p) - 1] = 0;
  173. /* fall through */
  174. case S_INT:
  175. case S_HEX:
  176. done:
  177. if (sym_string_valid(sym, p)) {
  178. sym->def[def].val = xstrdup(p);
  179. sym->flags |= def_flags;
  180. } else {
  181. if (def != S_DEF_AUTO)
  182. conf_warning("symbol value '%s' invalid for %s",
  183. p, sym->name);
  184. return 1;
  185. }
  186. break;
  187. default:
  188. ;
  189. }
  190. return 0;
  191. }
  192. #define LINE_GROWTH 16
  193. static int add_byte(int c, char **lineptr, size_t slen, size_t *n)
  194. {
  195. char *nline;
  196. size_t new_size = slen + 1;
  197. if (new_size > *n) {
  198. new_size += LINE_GROWTH - 1;
  199. new_size *= 2;
  200. nline = xrealloc(*lineptr, new_size);
  201. if (!nline)
  202. return -1;
  203. *lineptr = nline;
  204. *n = new_size;
  205. }
  206. (*lineptr)[slen] = c;
  207. return 0;
  208. }
  209. static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream)
  210. {
  211. char *line = *lineptr;
  212. size_t slen = 0;
  213. for (;;) {
  214. int c = getc(stream);
  215. switch (c) {
  216. case '\n':
  217. if (add_byte(c, &line, slen, n) < 0)
  218. goto e_out;
  219. slen++;
  220. /* fall through */
  221. case EOF:
  222. if (add_byte('\0', &line, slen, n) < 0)
  223. goto e_out;
  224. *lineptr = line;
  225. if (slen == 0)
  226. return -1;
  227. return slen;
  228. default:
  229. if (add_byte(c, &line, slen, n) < 0)
  230. goto e_out;
  231. slen++;
  232. }
  233. }
  234. e_out:
  235. line[slen-1] = '\0';
  236. *lineptr = line;
  237. return -1;
  238. }
  239. int conf_read_simple(const char *name, int def)
  240. {
  241. FILE *in = NULL;
  242. char *line = NULL;
  243. size_t line_asize = 0;
  244. char *p, *p2;
  245. struct symbol *sym;
  246. int i, def_flags;
  247. if (name) {
  248. in = zconf_fopen(name);
  249. } else {
  250. struct property *prop;
  251. name = conf_get_configname();
  252. in = zconf_fopen(name);
  253. if (in)
  254. goto load;
  255. sym_add_change_count(1);
  256. if (!sym_defconfig_list)
  257. return 1;
  258. for_all_defaults(sym_defconfig_list, prop) {
  259. if (expr_calc_value(prop->visible.expr) == no ||
  260. prop->expr->type != E_SYMBOL)
  261. continue;
  262. sym_calc_value(prop->expr->left.sym);
  263. name = sym_get_string_value(prop->expr->left.sym);
  264. in = zconf_fopen(name);
  265. if (in) {
  266. conf_message("using defaults found in %s",
  267. name);
  268. goto load;
  269. }
  270. }
  271. }
  272. if (!in)
  273. return 1;
  274. load:
  275. conf_filename = name;
  276. conf_lineno = 0;
  277. conf_warnings = 0;
  278. def_flags = SYMBOL_DEF << def;
  279. for_all_symbols(i, sym) {
  280. sym->flags |= SYMBOL_CHANGED;
  281. sym->flags &= ~(def_flags|SYMBOL_VALID);
  282. if (sym_is_choice(sym))
  283. sym->flags |= def_flags;
  284. switch (sym->type) {
  285. case S_INT:
  286. case S_HEX:
  287. case S_STRING:
  288. if (sym->def[def].val)
  289. free(sym->def[def].val);
  290. /* fall through */
  291. default:
  292. sym->def[def].val = NULL;
  293. sym->def[def].tri = no;
  294. }
  295. }
  296. while (compat_getline(&line, &line_asize, in) != -1) {
  297. conf_lineno++;
  298. sym = NULL;
  299. if (line[0] == '#') {
  300. if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
  301. continue;
  302. p = strchr(line + 2 + strlen(CONFIG_), ' ');
  303. if (!p)
  304. continue;
  305. *p++ = 0;
  306. if (strncmp(p, "is not set", 10))
  307. continue;
  308. if (def == S_DEF_USER) {
  309. sym = sym_find(line + 2 + strlen(CONFIG_));
  310. if (!sym) {
  311. sym_add_change_count(1);
  312. goto setsym;
  313. }
  314. } else {
  315. sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
  316. if (sym->type == S_UNKNOWN)
  317. sym->type = S_BOOLEAN;
  318. }
  319. if (sym->flags & def_flags) {
  320. conf_warning("override: reassigning to symbol %s", sym->name);
  321. }
  322. switch (sym->type) {
  323. case S_BOOLEAN:
  324. case S_TRISTATE:
  325. sym->def[def].tri = no;
  326. sym->flags |= def_flags;
  327. break;
  328. default:
  329. ;
  330. }
  331. } else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
  332. p = strchr(line + strlen(CONFIG_), '=');
  333. if (!p)
  334. continue;
  335. *p++ = 0;
  336. p2 = strchr(p, '\n');
  337. if (p2) {
  338. *p2-- = 0;
  339. if (*p2 == '\r')
  340. *p2 = 0;
  341. }
  342. if (def == S_DEF_USER) {
  343. sym = sym_find(line + strlen(CONFIG_));
  344. if (!sym) {
  345. sym_add_change_count(1);
  346. goto setsym;
  347. }
  348. } else {
  349. sym = sym_lookup(line + strlen(CONFIG_), 0);
  350. if (sym->type == S_UNKNOWN)
  351. sym->type = S_OTHER;
  352. }
  353. if (sym->flags & def_flags) {
  354. conf_warning("override: reassigning to symbol %s", sym->name);
  355. }
  356. if (conf_set_sym_val(sym, def, def_flags, p))
  357. continue;
  358. } else {
  359. if (line[0] != '\r' && line[0] != '\n')
  360. conf_warning("unexpected data: %.*s",
  361. (int)strcspn(line, "\r\n"), line);
  362. continue;
  363. }
  364. setsym:
  365. if (sym && sym_is_choice_value(sym)) {
  366. struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
  367. switch (sym->def[def].tri) {
  368. case no:
  369. break;
  370. case mod:
  371. if (cs->def[def].tri == yes) {
  372. conf_warning("%s creates inconsistent choice state", sym->name);
  373. cs->flags &= ~def_flags;
  374. }
  375. break;
  376. case yes:
  377. if (cs->def[def].tri != no)
  378. conf_warning("override: %s changes choice state", sym->name);
  379. cs->def[def].val = sym;
  380. break;
  381. }
  382. cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
  383. }
  384. }
  385. free(line);
  386. fclose(in);
  387. return 0;
  388. }
  389. int conf_read(const char *name)
  390. {
  391. struct symbol *sym;
  392. int conf_unsaved = 0;
  393. int i;
  394. sym_set_change_count(0);
  395. if (conf_read_simple(name, S_DEF_USER)) {
  396. sym_calc_value(modules_sym);
  397. return 1;
  398. }
  399. sym_calc_value(modules_sym);
  400. for_all_symbols(i, sym) {
  401. sym_calc_value(sym);
  402. if (sym_is_choice(sym) || (sym->flags & SYMBOL_NO_WRITE))
  403. continue;
  404. if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
  405. /* check that calculated value agrees with saved value */
  406. switch (sym->type) {
  407. case S_BOOLEAN:
  408. case S_TRISTATE:
  409. if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
  410. break;
  411. if (!sym_is_choice(sym))
  412. continue;
  413. /* fall through */
  414. default:
  415. if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
  416. continue;
  417. break;
  418. }
  419. } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
  420. /* no previous value and not saved */
  421. continue;
  422. conf_unsaved++;
  423. /* maybe print value in verbose mode... */
  424. }
  425. for_all_symbols(i, sym) {
  426. if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
  427. /* Reset values of generates values, so they'll appear
  428. * as new, if they should become visible, but that
  429. * doesn't quite work if the Kconfig and the saved
  430. * configuration disagree.
  431. */
  432. if (sym->visible == no && !conf_unsaved)
  433. sym->flags &= ~SYMBOL_DEF_USER;
  434. switch (sym->type) {
  435. case S_STRING:
  436. case S_INT:
  437. case S_HEX:
  438. /* Reset a string value if it's out of range */
  439. if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
  440. break;
  441. sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
  442. conf_unsaved++;
  443. break;
  444. default:
  445. break;
  446. }
  447. }
  448. }
  449. sym_add_change_count(conf_warnings || conf_unsaved);
  450. return 0;
  451. }
  452. /*
  453. * Kconfig configuration printer
  454. *
  455. * This printer is used when generating the resulting configuration after
  456. * kconfig invocation and `defconfig' files. Unset symbol might be omitted by
  457. * passing a non-NULL argument to the printer.
  458. *
  459. */
  460. static void
  461. kconfig_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
  462. {
  463. switch (sym->type) {
  464. case S_BOOLEAN:
  465. case S_TRISTATE:
  466. if (*value == 'n') {
  467. bool skip_unset = (arg != NULL);
  468. if (!skip_unset)
  469. fprintf(fp, "# %s%s is not set\n",
  470. CONFIG_, sym->name);
  471. return;
  472. }
  473. break;
  474. default:
  475. break;
  476. }
  477. fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, value);
  478. }
  479. static void
  480. kconfig_print_comment(FILE *fp, const char *value, void *arg)
  481. {
  482. const char *p = value;
  483. size_t l;
  484. for (;;) {
  485. l = strcspn(p, "\n");
  486. fprintf(fp, "#");
  487. if (l) {
  488. fprintf(fp, " ");
  489. xfwrite(p, l, 1, fp);
  490. p += l;
  491. }
  492. fprintf(fp, "\n");
  493. if (*p++ == '\0')
  494. break;
  495. }
  496. }
  497. static struct conf_printer kconfig_printer_cb =
  498. {
  499. .print_symbol = kconfig_print_symbol,
  500. .print_comment = kconfig_print_comment,
  501. };
  502. /*
  503. * Header printer
  504. *
  505. * This printer is used when generating the `include/generated/autoconf.h' file.
  506. */
  507. static void
  508. header_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
  509. {
  510. switch (sym->type) {
  511. case S_BOOLEAN:
  512. case S_TRISTATE: {
  513. const char *suffix = "";
  514. switch (*value) {
  515. case 'n':
  516. break;
  517. case 'm':
  518. suffix = "_MODULE";
  519. /* fall through */
  520. default:
  521. fprintf(fp, "#define %s%s%s 1\n",
  522. CONFIG_, sym->name, suffix);
  523. }
  524. break;
  525. }
  526. case S_HEX: {
  527. const char *prefix = "";
  528. if (value[0] != '0' || (value[1] != 'x' && value[1] != 'X'))
  529. prefix = "0x";
  530. fprintf(fp, "#define %s%s %s%s\n",
  531. CONFIG_, sym->name, prefix, value);
  532. break;
  533. }
  534. case S_STRING:
  535. case S_INT:
  536. fprintf(fp, "#define %s%s %s\n",
  537. CONFIG_, sym->name, value);
  538. break;
  539. default:
  540. break;
  541. }
  542. }
  543. static void
  544. header_print_comment(FILE *fp, const char *value, void *arg)
  545. {
  546. const char *p = value;
  547. size_t l;
  548. fprintf(fp, "/*\n");
  549. for (;;) {
  550. l = strcspn(p, "\n");
  551. fprintf(fp, " *");
  552. if (l) {
  553. fprintf(fp, " ");
  554. xfwrite(p, l, 1, fp);
  555. p += l;
  556. }
  557. fprintf(fp, "\n");
  558. if (*p++ == '\0')
  559. break;
  560. }
  561. fprintf(fp, " */\n");
  562. }
  563. static struct conf_printer header_printer_cb =
  564. {
  565. .print_symbol = header_print_symbol,
  566. .print_comment = header_print_comment,
  567. };
  568. /*
  569. * Tristate printer
  570. *
  571. * This printer is used when generating the `include/config/tristate.conf' file.
  572. */
  573. static void
  574. tristate_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
  575. {
  576. if (sym->type == S_TRISTATE && *value != 'n')
  577. fprintf(fp, "%s%s=%c\n", CONFIG_, sym->name, (char)toupper(*value));
  578. }
  579. static struct conf_printer tristate_printer_cb =
  580. {
  581. .print_symbol = tristate_print_symbol,
  582. .print_comment = kconfig_print_comment,
  583. };
  584. static void conf_write_symbol(FILE *fp, struct symbol *sym,
  585. struct conf_printer *printer, void *printer_arg)
  586. {
  587. const char *str;
  588. char *str2;
  589. switch (sym->type) {
  590. case S_OTHER:
  591. case S_UNKNOWN:
  592. break;
  593. case S_STRING:
  594. str = sym_get_string_value(sym);
  595. str2 = xmalloc(strlen(str) + 3);
  596. sprintf(str2, "\"%s\"", str);
  597. printer->print_symbol(fp, sym, str2, printer_arg);
  598. free((void *)str2);
  599. break;
  600. default:
  601. str = sym_get_string_value(sym);
  602. printer->print_symbol(fp, sym, str, printer_arg);
  603. }
  604. }
  605. static void
  606. conf_write_heading(FILE *fp, struct conf_printer *printer, void *printer_arg)
  607. {
  608. char buf[256];
  609. snprintf(buf, sizeof(buf),
  610. "\n"
  611. "Automatically generated file; DO NOT EDIT.\n"
  612. "%s\n",
  613. rootmenu.prompt->text);
  614. printer->print_comment(fp, buf, printer_arg);
  615. }
  616. /*
  617. * Write out a minimal config.
  618. * All values that has default values are skipped as this is redundant.
  619. */
  620. int conf_write_defconfig(const char *filename)
  621. {
  622. struct symbol *sym;
  623. struct menu *menu;
  624. FILE *out;
  625. out = fopen(filename, "w");
  626. if (!out)
  627. return 1;
  628. sym_clear_all_valid();
  629. /* Traverse all menus to find all relevant symbols */
  630. menu = rootmenu.list;
  631. while (menu != NULL)
  632. {
  633. sym = menu->sym;
  634. if (sym == NULL) {
  635. if (!menu_is_visible(menu))
  636. goto next_menu;
  637. } else if (!sym_is_choice(sym)) {
  638. sym_calc_value(sym);
  639. if (!(sym->flags & SYMBOL_WRITE))
  640. goto next_menu;
  641. sym->flags &= ~SYMBOL_WRITE;
  642. /* If we cannot change the symbol - skip */
  643. if (!sym_is_changable(sym))
  644. goto next_menu;
  645. /* If symbol equals to default value - skip */
  646. if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
  647. goto next_menu;
  648. /*
  649. * If symbol is a choice value and equals to the
  650. * default for a choice - skip.
  651. * But only if value is bool and equal to "y" and
  652. * choice is not "optional".
  653. * (If choice is "optional" then all values can be "n")
  654. */
  655. if (sym_is_choice_value(sym)) {
  656. struct symbol *cs;
  657. struct symbol *ds;
  658. cs = prop_get_symbol(sym_get_choice_prop(sym));
  659. ds = sym_choice_default(cs);
  660. if (!sym_is_optional(cs) && sym == ds) {
  661. if ((sym->type == S_BOOLEAN) &&
  662. sym_get_tristate_value(sym) == yes)
  663. goto next_menu;
  664. }
  665. }
  666. conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
  667. }
  668. next_menu:
  669. if (menu->list != NULL) {
  670. menu = menu->list;
  671. }
  672. else if (menu->next != NULL) {
  673. menu = menu->next;
  674. } else {
  675. while ((menu = menu->parent)) {
  676. if (menu->next != NULL) {
  677. menu = menu->next;
  678. break;
  679. }
  680. }
  681. }
  682. }
  683. fclose(out);
  684. return 0;
  685. }
  686. int conf_write(const char *name)
  687. {
  688. FILE *out;
  689. struct symbol *sym;
  690. struct menu *menu;
  691. const char *basename;
  692. const char *str;
  693. char dirname[PATH_MAX+1], tmpname[PATH_MAX+22], newname[PATH_MAX+8];
  694. char *env;
  695. dirname[0] = 0;
  696. if (name && name[0]) {
  697. char *slash;
  698. if (is_dir(name)) {
  699. strcpy(dirname, name);
  700. strcat(dirname, "/");
  701. basename = conf_get_configname();
  702. } else if ((slash = strrchr(name, '/'))) {
  703. int size = slash - name + 1;
  704. memcpy(dirname, name, size);
  705. dirname[size] = 0;
  706. if (slash[1])
  707. basename = slash + 1;
  708. else
  709. basename = conf_get_configname();
  710. } else
  711. basename = name;
  712. } else
  713. basename = conf_get_configname();
  714. sprintf(newname, "%s%s", dirname, basename);
  715. env = getenv("KCONFIG_OVERWRITECONFIG");
  716. if (!env || !*env) {
  717. sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
  718. out = fopen(tmpname, "w");
  719. } else {
  720. *tmpname = 0;
  721. out = fopen(newname, "w");
  722. }
  723. if (!out)
  724. return 1;
  725. conf_write_heading(out, &kconfig_printer_cb, NULL);
  726. if (!conf_get_changed())
  727. sym_clear_all_valid();
  728. menu = rootmenu.list;
  729. while (menu) {
  730. sym = menu->sym;
  731. if (!sym) {
  732. if (!menu_is_visible(menu))
  733. goto next;
  734. str = menu_get_prompt(menu);
  735. fprintf(out, "\n"
  736. "#\n"
  737. "# %s\n"
  738. "#\n", str);
  739. } else if (!(sym->flags & SYMBOL_CHOICE)) {
  740. sym_calc_value(sym);
  741. if (!(sym->flags & SYMBOL_WRITE))
  742. goto next;
  743. sym->flags &= ~SYMBOL_WRITE;
  744. conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
  745. }
  746. next:
  747. if (menu->list) {
  748. menu = menu->list;
  749. continue;
  750. }
  751. if (menu->next)
  752. menu = menu->next;
  753. else while ((menu = menu->parent)) {
  754. if (menu->next) {
  755. menu = menu->next;
  756. break;
  757. }
  758. }
  759. }
  760. fclose(out);
  761. if (*tmpname) {
  762. strcat(dirname, basename);
  763. strcat(dirname, ".old");
  764. rename(newname, dirname);
  765. if (rename(tmpname, newname))
  766. return 1;
  767. }
  768. conf_message("configuration written to %s", newname);
  769. sym_set_change_count(0);
  770. return 0;
  771. }
  772. /* write a dependency file as used by kbuild to track dependencies */
  773. static int conf_write_dep(const char *name)
  774. {
  775. struct file *file;
  776. FILE *out;
  777. if (!name)
  778. name = ".kconfig.d";
  779. out = fopen("..config.tmp", "w");
  780. if (!out)
  781. return 1;
  782. fprintf(out, "deps_config := \\\n");
  783. for (file = file_list; file; file = file->next) {
  784. if (file->next)
  785. fprintf(out, "\t%s \\\n", file->name);
  786. else
  787. fprintf(out, "\t%s\n", file->name);
  788. }
  789. fprintf(out, "\n%s: \\\n"
  790. "\t$(deps_config)\n\n", conf_get_autoconfig_name());
  791. env_write_dep(out, conf_get_autoconfig_name());
  792. fprintf(out, "\n$(deps_config): ;\n");
  793. fclose(out);
  794. if (make_parent_dir(name))
  795. return 1;
  796. rename("..config.tmp", name);
  797. return 0;
  798. }
  799. static int conf_split_config(void)
  800. {
  801. const char *name;
  802. char path[PATH_MAX+1];
  803. char *s, *d, c;
  804. struct symbol *sym;
  805. int res, i, fd;
  806. name = conf_get_autoconfig_name();
  807. conf_read_simple(name, S_DEF_AUTO);
  808. sym_calc_value(modules_sym);
  809. if (make_parent_dir("include/config/foo.h"))
  810. return 1;
  811. if (chdir("include/config"))
  812. return 1;
  813. res = 0;
  814. for_all_symbols(i, sym) {
  815. sym_calc_value(sym);
  816. if ((sym->flags & SYMBOL_NO_WRITE) || !sym->name)
  817. continue;
  818. if (sym->flags & SYMBOL_WRITE) {
  819. if (sym->flags & SYMBOL_DEF_AUTO) {
  820. /*
  821. * symbol has old and new value,
  822. * so compare them...
  823. */
  824. switch (sym->type) {
  825. case S_BOOLEAN:
  826. case S_TRISTATE:
  827. if (sym_get_tristate_value(sym) ==
  828. sym->def[S_DEF_AUTO].tri)
  829. continue;
  830. break;
  831. case S_STRING:
  832. case S_HEX:
  833. case S_INT:
  834. if (!strcmp(sym_get_string_value(sym),
  835. sym->def[S_DEF_AUTO].val))
  836. continue;
  837. break;
  838. default:
  839. break;
  840. }
  841. } else {
  842. /*
  843. * If there is no old value, only 'no' (unset)
  844. * is allowed as new value.
  845. */
  846. switch (sym->type) {
  847. case S_BOOLEAN:
  848. case S_TRISTATE:
  849. if (sym_get_tristate_value(sym) == no)
  850. continue;
  851. break;
  852. default:
  853. break;
  854. }
  855. }
  856. } else if (!(sym->flags & SYMBOL_DEF_AUTO))
  857. /* There is neither an old nor a new value. */
  858. continue;
  859. /* else
  860. * There is an old value, but no new value ('no' (unset)
  861. * isn't saved in auto.conf, so the old value is always
  862. * different from 'no').
  863. */
  864. /* Replace all '_' and append ".h" */
  865. s = sym->name;
  866. d = path;
  867. while ((c = *s++)) {
  868. c = tolower(c);
  869. *d++ = (c == '_') ? '/' : c;
  870. }
  871. strcpy(d, ".h");
  872. /* Assume directory path already exists. */
  873. fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  874. if (fd == -1) {
  875. if (errno != ENOENT) {
  876. res = 1;
  877. break;
  878. }
  879. if (make_parent_dir(path)) {
  880. res = 1;
  881. goto out;
  882. }
  883. /* Try it again. */
  884. fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  885. if (fd == -1) {
  886. res = 1;
  887. break;
  888. }
  889. }
  890. close(fd);
  891. }
  892. out:
  893. if (chdir("../.."))
  894. return 1;
  895. return res;
  896. }
  897. int conf_write_autoconf(void)
  898. {
  899. struct symbol *sym;
  900. const char *name;
  901. FILE *out, *tristate, *out_h;
  902. int i;
  903. sym_clear_all_valid();
  904. conf_write_dep("include/config/auto.conf.cmd");
  905. if (conf_split_config())
  906. return 1;
  907. out = fopen(".tmpconfig", "w");
  908. if (!out)
  909. return 1;
  910. tristate = fopen(".tmpconfig_tristate", "w");
  911. if (!tristate) {
  912. fclose(out);
  913. return 1;
  914. }
  915. out_h = fopen(".tmpconfig.h", "w");
  916. if (!out_h) {
  917. fclose(out);
  918. fclose(tristate);
  919. return 1;
  920. }
  921. conf_write_heading(out, &kconfig_printer_cb, NULL);
  922. conf_write_heading(tristate, &tristate_printer_cb, NULL);
  923. conf_write_heading(out_h, &header_printer_cb, NULL);
  924. for_all_symbols(i, sym) {
  925. sym_calc_value(sym);
  926. if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
  927. continue;
  928. /* write symbol to auto.conf, tristate and header files */
  929. conf_write_symbol(out, sym, &kconfig_printer_cb, (void *)1);
  930. conf_write_symbol(tristate, sym, &tristate_printer_cb, (void *)1);
  931. conf_write_symbol(out_h, sym, &header_printer_cb, NULL);
  932. }
  933. fclose(out);
  934. fclose(tristate);
  935. fclose(out_h);
  936. name = getenv("KCONFIG_AUTOHEADER");
  937. if (!name)
  938. name = "include/generated/autoconf.h";
  939. if (make_parent_dir(name))
  940. return 1;
  941. if (rename(".tmpconfig.h", name))
  942. return 1;
  943. name = getenv("KCONFIG_TRISTATE");
  944. if (!name)
  945. name = "include/config/tristate.conf";
  946. if (make_parent_dir(name))
  947. return 1;
  948. if (rename(".tmpconfig_tristate", name))
  949. return 1;
  950. name = conf_get_autoconfig_name();
  951. if (make_parent_dir(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", 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. }