confdata.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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 <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <time.h>
  11. #include <unistd.h>
  12. #define LKC_DIRECT_LINK
  13. #include "lkc.h"
  14. static void conf_warning(const char *fmt, ...)
  15. __attribute__ ((format (printf, 1, 2)));
  16. static const char *conf_filename;
  17. static int conf_lineno, conf_warnings, conf_unsaved;
  18. const char conf_def_filename[] = ".config";
  19. const char conf_defname[] = "scripts/defconfig";
  20. const char *conf_confnames[] = {
  21. ".config",
  22. "/lib/modules/$UNAME_RELEASE/.config",
  23. "/etc/kernel-config",
  24. "/boot/config-$UNAME_RELEASE",
  25. conf_defname,
  26. NULL,
  27. };
  28. static void conf_warning(const char *fmt, ...)
  29. {
  30. va_list ap;
  31. va_start(ap, fmt);
  32. fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
  33. vfprintf(stderr, fmt, ap);
  34. fprintf(stderr, "\n");
  35. va_end(ap);
  36. conf_warnings++;
  37. }
  38. static char *conf_expand_value(const char *in)
  39. {
  40. struct symbol *sym;
  41. const char *src;
  42. static char res_value[SYMBOL_MAXLENGTH];
  43. char *dst, name[SYMBOL_MAXLENGTH];
  44. res_value[0] = 0;
  45. dst = name;
  46. while ((src = strchr(in, '$'))) {
  47. strncat(res_value, in, src - in);
  48. src++;
  49. dst = name;
  50. while (isalnum(*src) || *src == '_')
  51. *dst++ = *src++;
  52. *dst = 0;
  53. sym = sym_lookup(name, 0);
  54. sym_calc_value(sym);
  55. strcat(res_value, sym_get_string_value(sym));
  56. in = src;
  57. }
  58. strcat(res_value, in);
  59. return res_value;
  60. }
  61. char *conf_get_default_confname(void)
  62. {
  63. struct stat buf;
  64. static char fullname[PATH_MAX+1];
  65. char *env, *name;
  66. name = conf_expand_value(conf_defname);
  67. env = getenv(SRCTREE);
  68. if (env) {
  69. sprintf(fullname, "%s/%s", env, name);
  70. if (!stat(fullname, &buf))
  71. return fullname;
  72. }
  73. return name;
  74. }
  75. int conf_read_simple(const char *name)
  76. {
  77. FILE *in = NULL;
  78. char line[1024];
  79. char *p, *p2;
  80. struct symbol *sym;
  81. int i;
  82. if (name) {
  83. in = zconf_fopen(name);
  84. } else {
  85. const char **names = conf_confnames;
  86. while ((name = *names++)) {
  87. name = conf_expand_value(name);
  88. in = zconf_fopen(name);
  89. if (in) {
  90. printf(_("#\n"
  91. "# using defaults found in %s\n"
  92. "#\n"), name);
  93. break;
  94. }
  95. }
  96. }
  97. if (!in)
  98. return 1;
  99. conf_filename = name;
  100. conf_lineno = 0;
  101. conf_warnings = 0;
  102. conf_unsaved = 0;
  103. for_all_symbols(i, sym) {
  104. sym->flags |= SYMBOL_NEW | SYMBOL_CHANGED;
  105. if (sym_is_choice(sym))
  106. sym->flags &= ~SYMBOL_NEW;
  107. sym->flags &= ~SYMBOL_VALID;
  108. switch (sym->type) {
  109. case S_INT:
  110. case S_HEX:
  111. case S_STRING:
  112. if (sym->user.val)
  113. free(sym->user.val);
  114. default:
  115. sym->user.val = NULL;
  116. sym->user.tri = no;
  117. }
  118. }
  119. while (fgets(line, sizeof(line), in)) {
  120. conf_lineno++;
  121. sym = NULL;
  122. switch (line[0]) {
  123. case '#':
  124. if (memcmp(line + 2, "CONFIG_", 7))
  125. continue;
  126. p = strchr(line + 9, ' ');
  127. if (!p)
  128. continue;
  129. *p++ = 0;
  130. if (strncmp(p, "is not set", 10))
  131. continue;
  132. sym = sym_find(line + 9);
  133. if (!sym) {
  134. conf_warning("trying to assign nonexistent symbol %s", line + 9);
  135. break;
  136. } else if (!(sym->flags & SYMBOL_NEW)) {
  137. conf_warning("trying to reassign symbol %s", sym->name);
  138. break;
  139. }
  140. switch (sym->type) {
  141. case S_BOOLEAN:
  142. case S_TRISTATE:
  143. sym->user.tri = no;
  144. sym->flags &= ~SYMBOL_NEW;
  145. break;
  146. default:
  147. ;
  148. }
  149. break;
  150. case 'C':
  151. if (memcmp(line, "CONFIG_", 7)) {
  152. conf_warning("unexpected data");
  153. continue;
  154. }
  155. p = strchr(line + 7, '=');
  156. if (!p)
  157. continue;
  158. *p++ = 0;
  159. p2 = strchr(p, '\n');
  160. if (p2)
  161. *p2 = 0;
  162. sym = sym_find(line + 7);
  163. if (!sym) {
  164. conf_warning("trying to assign nonexistent symbol %s", line + 7);
  165. break;
  166. } else if (!(sym->flags & SYMBOL_NEW)) {
  167. conf_warning("trying to reassign symbol %s", sym->name);
  168. break;
  169. }
  170. switch (sym->type) {
  171. case S_TRISTATE:
  172. if (p[0] == 'm') {
  173. sym->user.tri = mod;
  174. sym->flags &= ~SYMBOL_NEW;
  175. break;
  176. }
  177. case S_BOOLEAN:
  178. if (p[0] == 'y') {
  179. sym->user.tri = yes;
  180. sym->flags &= ~SYMBOL_NEW;
  181. break;
  182. }
  183. if (p[0] == 'n') {
  184. sym->user.tri = no;
  185. sym->flags &= ~SYMBOL_NEW;
  186. break;
  187. }
  188. conf_warning("symbol value '%s' invalid for %s", p, sym->name);
  189. break;
  190. case S_STRING:
  191. if (*p++ != '"')
  192. break;
  193. for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
  194. if (*p2 == '"') {
  195. *p2 = 0;
  196. break;
  197. }
  198. memmove(p2, p2 + 1, strlen(p2));
  199. }
  200. if (!p2) {
  201. conf_warning("invalid string found");
  202. continue;
  203. }
  204. case S_INT:
  205. case S_HEX:
  206. if (sym_string_valid(sym, p)) {
  207. sym->user.val = strdup(p);
  208. sym->flags &= ~SYMBOL_NEW;
  209. } else {
  210. conf_warning("symbol value '%s' invalid for %s", p, sym->name);
  211. continue;
  212. }
  213. break;
  214. default:
  215. ;
  216. }
  217. break;
  218. case '\n':
  219. break;
  220. default:
  221. conf_warning("unexpected data");
  222. continue;
  223. }
  224. if (sym && sym_is_choice_value(sym)) {
  225. struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
  226. switch (sym->user.tri) {
  227. case no:
  228. break;
  229. case mod:
  230. if (cs->user.tri == yes) {
  231. conf_warning("%s creates inconsistent choice state", sym->name);
  232. cs->flags |= SYMBOL_NEW;
  233. }
  234. break;
  235. case yes:
  236. if (cs->user.tri != no) {
  237. conf_warning("%s creates inconsistent choice state", sym->name);
  238. cs->flags |= SYMBOL_NEW;
  239. } else
  240. cs->user.val = sym;
  241. break;
  242. }
  243. cs->user.tri = E_OR(cs->user.tri, sym->user.tri);
  244. }
  245. }
  246. fclose(in);
  247. if (modules_sym)
  248. sym_calc_value(modules_sym);
  249. return 0;
  250. }
  251. int conf_read(const char *name)
  252. {
  253. struct symbol *sym;
  254. struct property *prop;
  255. struct expr *e;
  256. int i;
  257. if (conf_read_simple(name))
  258. return 1;
  259. for_all_symbols(i, sym) {
  260. sym_calc_value(sym);
  261. if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
  262. goto sym_ok;
  263. if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
  264. /* check that calculated value agrees with saved value */
  265. switch (sym->type) {
  266. case S_BOOLEAN:
  267. case S_TRISTATE:
  268. if (sym->user.tri != sym_get_tristate_value(sym))
  269. break;
  270. if (!sym_is_choice(sym))
  271. goto sym_ok;
  272. default:
  273. if (!strcmp(sym->curr.val, sym->user.val))
  274. goto sym_ok;
  275. break;
  276. }
  277. } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
  278. /* no previous value and not saved */
  279. goto sym_ok;
  280. conf_unsaved++;
  281. /* maybe print value in verbose mode... */
  282. sym_ok:
  283. if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
  284. if (sym->visible == no)
  285. sym->flags |= SYMBOL_NEW;
  286. switch (sym->type) {
  287. case S_STRING:
  288. case S_INT:
  289. case S_HEX:
  290. if (!sym_string_within_range(sym, sym->user.val)) {
  291. sym->flags |= SYMBOL_NEW;
  292. sym->flags &= ~SYMBOL_VALID;
  293. }
  294. default:
  295. break;
  296. }
  297. }
  298. if (!sym_is_choice(sym))
  299. continue;
  300. prop = sym_get_choice_prop(sym);
  301. for (e = prop->expr; e; e = e->left.expr)
  302. if (e->right.sym->visible != no)
  303. sym->flags |= e->right.sym->flags & SYMBOL_NEW;
  304. }
  305. sym_change_count = conf_warnings || conf_unsaved;
  306. return 0;
  307. }
  308. int conf_write(const char *name)
  309. {
  310. FILE *out, *out_h;
  311. struct symbol *sym;
  312. struct menu *menu;
  313. const char *basename;
  314. char dirname[128], tmpname[128], newname[128];
  315. int type, l;
  316. const char *str;
  317. time_t now;
  318. int use_timestamp = 1;
  319. char *env;
  320. dirname[0] = 0;
  321. if (name && name[0]) {
  322. struct stat st;
  323. char *slash;
  324. if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
  325. strcpy(dirname, name);
  326. strcat(dirname, "/");
  327. basename = conf_def_filename;
  328. } else if ((slash = strrchr(name, '/'))) {
  329. int size = slash - name + 1;
  330. memcpy(dirname, name, size);
  331. dirname[size] = 0;
  332. if (slash[1])
  333. basename = slash + 1;
  334. else
  335. basename = conf_def_filename;
  336. } else
  337. basename = name;
  338. } else
  339. basename = conf_def_filename;
  340. sprintf(newname, "%s.tmpconfig.%d", dirname, (int)getpid());
  341. out = fopen(newname, "w");
  342. if (!out)
  343. return 1;
  344. out_h = NULL;
  345. if (!name) {
  346. out_h = fopen(".tmpconfig.h", "w");
  347. if (!out_h)
  348. return 1;
  349. file_write_dep(NULL);
  350. }
  351. sym = sym_lookup("KERNELVERSION", 0);
  352. sym_calc_value(sym);
  353. time(&now);
  354. env = getenv("KCONFIG_NOTIMESTAMP");
  355. if (env && *env)
  356. use_timestamp = 0;
  357. fprintf(out, _("#\n"
  358. "# Automatically generated make config: don't edit\n"
  359. "# Linux kernel version: %s\n"
  360. "%s%s"
  361. "#\n"),
  362. sym_get_string_value(sym),
  363. use_timestamp ? "# " : "",
  364. use_timestamp ? ctime(&now) : "");
  365. if (out_h) {
  366. char buf[sizeof("#define AUTOCONF_TIMESTAMP "
  367. "\"YYYY-MM-DD HH:MM:SS some_timezone\"\n")];
  368. buf[0] = '\0';
  369. if (use_timestamp)
  370. strftime(buf, sizeof(buf), "#define AUTOCONF_TIMESTAMP "
  371. "\"%Y-%m-%d %H:%M:%S %Z\"\n", localtime(&now));
  372. fprintf(out_h, "/*\n"
  373. " * Automatically generated C config: don't edit\n"
  374. " * Linux kernel version: %s\n"
  375. " */\n"
  376. "%s"
  377. "#define AUTOCONF_INCLUDED\n",
  378. sym_get_string_value(sym),
  379. buf);
  380. }
  381. if (!sym_change_count)
  382. sym_clear_all_valid();
  383. menu = rootmenu.list;
  384. while (menu) {
  385. sym = menu->sym;
  386. if (!sym) {
  387. if (!menu_is_visible(menu))
  388. goto next;
  389. str = menu_get_prompt(menu);
  390. fprintf(out, "\n"
  391. "#\n"
  392. "# %s\n"
  393. "#\n", str);
  394. if (out_h)
  395. fprintf(out_h, "\n"
  396. "/*\n"
  397. " * %s\n"
  398. " */\n", str);
  399. } else if (!(sym->flags & SYMBOL_CHOICE)) {
  400. sym_calc_value(sym);
  401. /* bbox: we want to all syms
  402. if (!(sym->flags & SYMBOL_WRITE))
  403. goto next;
  404. */
  405. sym->flags &= ~SYMBOL_WRITE;
  406. type = sym->type;
  407. if (type == S_TRISTATE) {
  408. sym_calc_value(modules_sym);
  409. if (modules_sym->curr.tri == no)
  410. type = S_BOOLEAN;
  411. }
  412. switch (type) {
  413. case S_BOOLEAN:
  414. case S_TRISTATE:
  415. switch (sym_get_tristate_value(sym)) {
  416. case no:
  417. fprintf(out, "# CONFIG_%s is not set\n", sym->name);
  418. if (out_h) {
  419. fprintf(out_h, "#undef CONFIG_%s\n", sym->name);
  420. /* bbox */
  421. fprintf(out_h, "#define ENABLE_%s 0\n", sym->name);
  422. fprintf(out_h, "#define USE_%s(...)\n", sym->name);
  423. fprintf(out_h, "#define SKIP_%s(...) __VA_ARGS__\n", sym->name);
  424. }
  425. break;
  426. case mod:
  427. fprintf(out, "CONFIG_%s=m\n", sym->name);
  428. if (out_h)
  429. fprintf(out_h, "#define CONFIG_%s_MODULE 1\n", sym->name);
  430. break;
  431. case yes:
  432. fprintf(out, "CONFIG_%s=y\n", sym->name);
  433. if (out_h) {
  434. fprintf(out_h, "#define CONFIG_%s 1\n", sym->name);
  435. /* bbox */
  436. fprintf(out_h, "#define ENABLE_%s 1\n", sym->name);
  437. fprintf(out_h, "#define USE_%s(...) __VA_ARGS__\n", sym->name);
  438. fprintf(out_h, "#define SKIP_%s(...)\n", sym->name);
  439. }
  440. break;
  441. }
  442. break;
  443. case S_STRING:
  444. // fix me
  445. str = sym_get_string_value(sym);
  446. fprintf(out, "CONFIG_%s=\"", sym->name);
  447. if (out_h)
  448. fprintf(out_h, "#define CONFIG_%s \"", sym->name);
  449. do {
  450. l = strcspn(str, "\"\\");
  451. if (l) {
  452. fwrite(str, l, 1, out);
  453. if (out_h)
  454. fwrite(str, l, 1, out_h);
  455. }
  456. str += l;
  457. while (*str == '\\' || *str == '"') {
  458. fprintf(out, "\\%c", *str);
  459. if (out_h)
  460. fprintf(out_h, "\\%c", *str);
  461. str++;
  462. }
  463. } while (*str);
  464. fputs("\"\n", out);
  465. if (out_h) {
  466. fputs("\"\n", out_h);
  467. /* bbox */
  468. fprintf(out_h, "#define ENABLE_%s 1\n", sym->name);
  469. fprintf(out_h, "#define USE_%s(...) __VA_ARGS__\n", sym->name);
  470. fprintf(out_h, "#define SKIP_%s(...)\n", sym->name);
  471. }
  472. break;
  473. case S_HEX:
  474. str = sym_get_string_value(sym);
  475. if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
  476. fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
  477. if (out_h) {
  478. fprintf(out_h, "#define CONFIG_%s 0x%s\n", sym->name, str);
  479. /* bbox */
  480. fprintf(out_h, "#define ENABLE_%s 1\n", sym->name);
  481. fprintf(out_h, "#define USE_%s(...) __VA_ARGS__\n", sym->name);
  482. fprintf(out_h, "#define SKIP_%s(...)\n", sym->name);
  483. }
  484. break;
  485. }
  486. case S_INT:
  487. str = sym_get_string_value(sym);
  488. fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
  489. if (out_h) {
  490. fprintf(out_h, "#define CONFIG_%s %s\n", sym->name, str);
  491. /* bbox */
  492. fprintf(out_h, "#define ENABLE_%s 1\n", sym->name);
  493. fprintf(out_h, "#define USE_%s(...) __VA_ARGS__\n", sym->name);
  494. fprintf(out_h, "#define SKIP_%s(...)\n", sym->name);
  495. }
  496. break;
  497. }
  498. }
  499. next:
  500. if (menu->list) {
  501. menu = menu->list;
  502. continue;
  503. }
  504. if (menu->next)
  505. menu = menu->next;
  506. else while ((menu = menu->parent)) {
  507. if (menu->next) {
  508. menu = menu->next;
  509. break;
  510. }
  511. }
  512. }
  513. fclose(out);
  514. if (out_h) {
  515. fclose(out_h);
  516. rename(".tmpconfig.h", "include/autoconf.h");
  517. }
  518. if (!name || basename != conf_def_filename) {
  519. if (!name)
  520. name = conf_def_filename;
  521. sprintf(tmpname, "%s.old", name);
  522. rename(name, tmpname);
  523. }
  524. sprintf(tmpname, "%s%s", dirname, basename);
  525. if (rename(newname, tmpname))
  526. return 1;
  527. sym_change_count = 0;
  528. return 0;
  529. }