confdata.c 13 KB

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