confdata.c 26 KB

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