confdata.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  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. dirname[0] = 0;
  698. if (name && name[0]) {
  699. char *slash;
  700. if (is_dir(name)) {
  701. strcpy(dirname, name);
  702. strcat(dirname, "/");
  703. basename = conf_get_configname();
  704. } else if ((slash = strrchr(name, '/'))) {
  705. int size = slash - name + 1;
  706. memcpy(dirname, name, size);
  707. dirname[size] = 0;
  708. if (slash[1])
  709. basename = slash + 1;
  710. else
  711. basename = conf_get_configname();
  712. } else
  713. basename = name;
  714. } else
  715. basename = conf_get_configname();
  716. sprintf(newname, "%s%s", dirname, basename);
  717. env = getenv("KCONFIG_OVERWRITECONFIG");
  718. if (!env || !*env) {
  719. sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
  720. out = fopen(tmpname, "w");
  721. } else {
  722. *tmpname = 0;
  723. out = fopen(newname, "w");
  724. }
  725. if (!out)
  726. return 1;
  727. conf_write_heading(out, &kconfig_printer_cb, NULL);
  728. if (!conf_get_changed())
  729. sym_clear_all_valid();
  730. menu = rootmenu.list;
  731. while (menu) {
  732. sym = menu->sym;
  733. if (!sym) {
  734. if (!menu_is_visible(menu))
  735. goto next;
  736. str = menu_get_prompt(menu);
  737. fprintf(out, "\n"
  738. "#\n"
  739. "# %s\n"
  740. "#\n", str);
  741. } else if (!(sym->flags & SYMBOL_CHOICE)) {
  742. sym_calc_value(sym);
  743. if (!(sym->flags & SYMBOL_WRITE))
  744. goto next;
  745. sym->flags &= ~SYMBOL_WRITE;
  746. conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
  747. }
  748. next:
  749. if (menu->list) {
  750. menu = menu->list;
  751. continue;
  752. }
  753. if (menu->next)
  754. menu = menu->next;
  755. else while ((menu = menu->parent)) {
  756. if (menu->next) {
  757. menu = menu->next;
  758. break;
  759. }
  760. }
  761. }
  762. fclose(out);
  763. if (*tmpname) {
  764. strcat(dirname, basename);
  765. strcat(dirname, ".old");
  766. rename(newname, dirname);
  767. if (rename(tmpname, newname))
  768. return 1;
  769. }
  770. conf_message("configuration written to %s", newname);
  771. sym_set_change_count(0);
  772. return 0;
  773. }
  774. /* write a dependency file as used by kbuild to track dependencies */
  775. static int conf_write_dep(const char *name)
  776. {
  777. struct file *file;
  778. FILE *out;
  779. if (!name)
  780. name = ".kconfig.d";
  781. out = fopen("..config.tmp", "w");
  782. if (!out)
  783. return 1;
  784. fprintf(out, "deps_config := \\\n");
  785. for (file = file_list; file; file = file->next) {
  786. if (file->next)
  787. fprintf(out, "\t%s \\\n", file->name);
  788. else
  789. fprintf(out, "\t%s\n", file->name);
  790. }
  791. fprintf(out, "\n%s: \\\n"
  792. "\t$(deps_config)\n\n", conf_get_autoconfig_name());
  793. env_write_dep(out, conf_get_autoconfig_name());
  794. fprintf(out, "\n$(deps_config): ;\n");
  795. fclose(out);
  796. if (make_parent_dir(name))
  797. return 1;
  798. rename("..config.tmp", name);
  799. return 0;
  800. }
  801. static int conf_split_config(void)
  802. {
  803. const char *name;
  804. char path[PATH_MAX+1];
  805. char *s, *d, c;
  806. struct symbol *sym;
  807. int res, i, fd;
  808. name = conf_get_autoconfig_name();
  809. conf_read_simple(name, S_DEF_AUTO);
  810. sym_calc_value(modules_sym);
  811. if (make_parent_dir("include/config/foo.h"))
  812. return 1;
  813. if (chdir("include/config"))
  814. return 1;
  815. res = 0;
  816. for_all_symbols(i, sym) {
  817. sym_calc_value(sym);
  818. if ((sym->flags & SYMBOL_NO_WRITE) || !sym->name)
  819. continue;
  820. if (sym->flags & SYMBOL_WRITE) {
  821. if (sym->flags & SYMBOL_DEF_AUTO) {
  822. /*
  823. * symbol has old and new value,
  824. * so compare them...
  825. */
  826. switch (sym->type) {
  827. case S_BOOLEAN:
  828. case S_TRISTATE:
  829. if (sym_get_tristate_value(sym) ==
  830. sym->def[S_DEF_AUTO].tri)
  831. continue;
  832. break;
  833. case S_STRING:
  834. case S_HEX:
  835. case S_INT:
  836. if (!strcmp(sym_get_string_value(sym),
  837. sym->def[S_DEF_AUTO].val))
  838. continue;
  839. break;
  840. default:
  841. break;
  842. }
  843. } else {
  844. /*
  845. * If there is no old value, only 'no' (unset)
  846. * is allowed as new value.
  847. */
  848. switch (sym->type) {
  849. case S_BOOLEAN:
  850. case S_TRISTATE:
  851. if (sym_get_tristate_value(sym) == no)
  852. continue;
  853. break;
  854. default:
  855. break;
  856. }
  857. }
  858. } else if (!(sym->flags & SYMBOL_DEF_AUTO))
  859. /* There is neither an old nor a new value. */
  860. continue;
  861. /* else
  862. * There is an old value, but no new value ('no' (unset)
  863. * isn't saved in auto.conf, so the old value is always
  864. * different from 'no').
  865. */
  866. /* Replace all '_' and append ".h" */
  867. s = sym->name;
  868. d = path;
  869. while ((c = *s++)) {
  870. c = tolower(c);
  871. *d++ = (c == '_') ? '/' : c;
  872. }
  873. strcpy(d, ".h");
  874. /* Assume directory path already exists. */
  875. fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  876. if (fd == -1) {
  877. if (errno != ENOENT) {
  878. res = 1;
  879. break;
  880. }
  881. if (make_parent_dir(path)) {
  882. res = 1;
  883. goto out;
  884. }
  885. /* Try it again. */
  886. fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  887. if (fd == -1) {
  888. res = 1;
  889. break;
  890. }
  891. }
  892. close(fd);
  893. }
  894. out:
  895. if (chdir("../.."))
  896. return 1;
  897. return res;
  898. }
  899. int conf_write_autoconf(int overwrite)
  900. {
  901. struct symbol *sym;
  902. const char *name;
  903. const char *autoconf_name = conf_get_autoconfig_name();
  904. FILE *out, *tristate, *out_h;
  905. int i;
  906. if (!overwrite && is_present(autoconf_name))
  907. return 0;
  908. sym_clear_all_valid();
  909. conf_write_dep("include/config/auto.conf.cmd");
  910. if (conf_split_config())
  911. return 1;
  912. out = fopen(".tmpconfig", "w");
  913. if (!out)
  914. return 1;
  915. tristate = fopen(".tmpconfig_tristate", "w");
  916. if (!tristate) {
  917. fclose(out);
  918. return 1;
  919. }
  920. out_h = fopen(".tmpconfig.h", "w");
  921. if (!out_h) {
  922. fclose(out);
  923. fclose(tristate);
  924. return 1;
  925. }
  926. conf_write_heading(out, &kconfig_printer_cb, NULL);
  927. conf_write_heading(tristate, &tristate_printer_cb, NULL);
  928. conf_write_heading(out_h, &header_printer_cb, NULL);
  929. for_all_symbols(i, sym) {
  930. sym_calc_value(sym);
  931. if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
  932. continue;
  933. /* write symbol to auto.conf, tristate and header files */
  934. conf_write_symbol(out, sym, &kconfig_printer_cb, (void *)1);
  935. conf_write_symbol(tristate, sym, &tristate_printer_cb, (void *)1);
  936. conf_write_symbol(out_h, sym, &header_printer_cb, NULL);
  937. }
  938. fclose(out);
  939. fclose(tristate);
  940. fclose(out_h);
  941. name = getenv("KCONFIG_AUTOHEADER");
  942. if (!name)
  943. name = "include/generated/autoconf.h";
  944. if (make_parent_dir(name))
  945. return 1;
  946. if (rename(".tmpconfig.h", name))
  947. return 1;
  948. name = getenv("KCONFIG_TRISTATE");
  949. if (!name)
  950. name = "include/config/tristate.conf";
  951. if (make_parent_dir(name))
  952. return 1;
  953. if (rename(".tmpconfig_tristate", name))
  954. return 1;
  955. if (make_parent_dir(autoconf_name))
  956. return 1;
  957. /*
  958. * This must be the last step, kbuild has a dependency on auto.conf
  959. * and this marks the successful completion of the previous steps.
  960. */
  961. if (rename(".tmpconfig", autoconf_name))
  962. return 1;
  963. return 0;
  964. }
  965. static int sym_change_count;
  966. static void (*conf_changed_callback)(void);
  967. void sym_set_change_count(int count)
  968. {
  969. int _sym_change_count = sym_change_count;
  970. sym_change_count = count;
  971. if (conf_changed_callback &&
  972. (bool)_sym_change_count != (bool)count)
  973. conf_changed_callback();
  974. }
  975. void sym_add_change_count(int count)
  976. {
  977. sym_set_change_count(count + sym_change_count);
  978. }
  979. bool conf_get_changed(void)
  980. {
  981. return sym_change_count;
  982. }
  983. void conf_set_changed_callback(void (*fn)(void))
  984. {
  985. conf_changed_callback = fn;
  986. }
  987. static bool randomize_choice_values(struct symbol *csym)
  988. {
  989. struct property *prop;
  990. struct symbol *sym;
  991. struct expr *e;
  992. int cnt, def;
  993. /*
  994. * If choice is mod then we may have more items selected
  995. * and if no then no-one.
  996. * In both cases stop.
  997. */
  998. if (csym->curr.tri != yes)
  999. return false;
  1000. prop = sym_get_choice_prop(csym);
  1001. /* count entries in choice block */
  1002. cnt = 0;
  1003. expr_list_for_each_sym(prop->expr, e, sym)
  1004. cnt++;
  1005. /*
  1006. * find a random value and set it to yes,
  1007. * set the rest to no so we have only one set
  1008. */
  1009. def = (rand() % cnt);
  1010. cnt = 0;
  1011. expr_list_for_each_sym(prop->expr, e, sym) {
  1012. if (def == cnt++) {
  1013. sym->def[S_DEF_USER].tri = yes;
  1014. csym->def[S_DEF_USER].val = sym;
  1015. }
  1016. else {
  1017. sym->def[S_DEF_USER].tri = no;
  1018. }
  1019. sym->flags |= SYMBOL_DEF_USER;
  1020. /* clear VALID to get value calculated */
  1021. sym->flags &= ~SYMBOL_VALID;
  1022. }
  1023. csym->flags |= SYMBOL_DEF_USER;
  1024. /* clear VALID to get value calculated */
  1025. csym->flags &= ~(SYMBOL_VALID);
  1026. return true;
  1027. }
  1028. void set_all_choice_values(struct symbol *csym)
  1029. {
  1030. struct property *prop;
  1031. struct symbol *sym;
  1032. struct expr *e;
  1033. prop = sym_get_choice_prop(csym);
  1034. /*
  1035. * Set all non-assinged choice values to no
  1036. */
  1037. expr_list_for_each_sym(prop->expr, e, sym) {
  1038. if (!sym_has_value(sym))
  1039. sym->def[S_DEF_USER].tri = no;
  1040. }
  1041. csym->flags |= SYMBOL_DEF_USER;
  1042. /* clear VALID to get value calculated */
  1043. csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
  1044. }
  1045. bool conf_set_all_new_symbols(enum conf_def_mode mode)
  1046. {
  1047. struct symbol *sym, *csym;
  1048. int i, cnt, pby, pty, ptm; /* pby: probability of bool = y
  1049. * pty: probability of tristate = y
  1050. * ptm: probability of tristate = m
  1051. */
  1052. pby = 50; pty = ptm = 33; /* can't go as the default in switch-case
  1053. * below, otherwise gcc whines about
  1054. * -Wmaybe-uninitialized */
  1055. if (mode == def_random) {
  1056. int n, p[3];
  1057. char *env = getenv("KCONFIG_PROBABILITY");
  1058. n = 0;
  1059. while( env && *env ) {
  1060. char *endp;
  1061. int tmp = strtol( env, &endp, 10 );
  1062. if( tmp >= 0 && tmp <= 100 ) {
  1063. p[n++] = tmp;
  1064. } else {
  1065. errno = ERANGE;
  1066. perror( "KCONFIG_PROBABILITY" );
  1067. exit( 1 );
  1068. }
  1069. env = (*endp == ':') ? endp+1 : endp;
  1070. if( n >=3 ) {
  1071. break;
  1072. }
  1073. }
  1074. switch( n ) {
  1075. case 1:
  1076. pby = p[0]; ptm = pby/2; pty = pby-ptm;
  1077. break;
  1078. case 2:
  1079. pty = p[0]; ptm = p[1]; pby = pty + ptm;
  1080. break;
  1081. case 3:
  1082. pby = p[0]; pty = p[1]; ptm = p[2];
  1083. break;
  1084. }
  1085. if( pty+ptm > 100 ) {
  1086. errno = ERANGE;
  1087. perror( "KCONFIG_PROBABILITY" );
  1088. exit( 1 );
  1089. }
  1090. }
  1091. bool has_changed = false;
  1092. for_all_symbols(i, sym) {
  1093. if (sym_has_value(sym) || (sym->flags & SYMBOL_VALID))
  1094. continue;
  1095. switch (sym_get_type(sym)) {
  1096. case S_BOOLEAN:
  1097. case S_TRISTATE:
  1098. has_changed = true;
  1099. switch (mode) {
  1100. case def_yes:
  1101. sym->def[S_DEF_USER].tri = yes;
  1102. break;
  1103. case def_mod:
  1104. sym->def[S_DEF_USER].tri = mod;
  1105. break;
  1106. case def_no:
  1107. if (sym->flags & SYMBOL_ALLNOCONFIG_Y)
  1108. sym->def[S_DEF_USER].tri = yes;
  1109. else
  1110. sym->def[S_DEF_USER].tri = no;
  1111. break;
  1112. case def_random:
  1113. sym->def[S_DEF_USER].tri = no;
  1114. cnt = rand() % 100;
  1115. if (sym->type == S_TRISTATE) {
  1116. if (cnt < pty)
  1117. sym->def[S_DEF_USER].tri = yes;
  1118. else if (cnt < (pty+ptm))
  1119. sym->def[S_DEF_USER].tri = mod;
  1120. } else if (cnt < pby)
  1121. sym->def[S_DEF_USER].tri = yes;
  1122. break;
  1123. default:
  1124. continue;
  1125. }
  1126. if (!(sym_is_choice(sym) && mode == def_random))
  1127. sym->flags |= SYMBOL_DEF_USER;
  1128. break;
  1129. default:
  1130. break;
  1131. }
  1132. }
  1133. sym_clear_all_valid();
  1134. /*
  1135. * We have different type of choice blocks.
  1136. * If curr.tri equals to mod then we can select several
  1137. * choice symbols in one block.
  1138. * In this case we do nothing.
  1139. * If curr.tri equals yes then only one symbol can be
  1140. * selected in a choice block and we set it to yes,
  1141. * and the rest to no.
  1142. */
  1143. if (mode != def_random) {
  1144. for_all_symbols(i, csym) {
  1145. if ((sym_is_choice(csym) && !sym_has_value(csym)) ||
  1146. sym_is_choice_value(csym))
  1147. csym->flags |= SYMBOL_NEED_SET_CHOICE_VALUES;
  1148. }
  1149. }
  1150. for_all_symbols(i, csym) {
  1151. if (sym_has_value(csym) || !sym_is_choice(csym))
  1152. continue;
  1153. sym_calc_value(csym);
  1154. if (mode == def_random)
  1155. has_changed = randomize_choice_values(csym);
  1156. else {
  1157. set_all_choice_values(csym);
  1158. has_changed = true;
  1159. }
  1160. }
  1161. return has_changed;
  1162. }