confdata.c 26 KB

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