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