confdata.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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[] = "/dev/null"; //bbox
  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 = NULL;
  62. char *env, *name;
  63. name = conf_expand_value(conf_defname);
  64. env = getenv(SRCTREE);
  65. if (env) {
  66. fullname = realloc(fullname, strlen(env) + strlen(name) + 2);
  67. sprintf(fullname, "%s/%s", env, name);
  68. if (!stat(fullname, &buf))
  69. return fullname;
  70. }
  71. return name;
  72. }
  73. int conf_read_simple(const char *name)
  74. {
  75. FILE *in = NULL;
  76. char line[1024];
  77. char *p, *p2;
  78. struct symbol *sym;
  79. int i;
  80. if (name) {
  81. in = zconf_fopen(name);
  82. } else {
  83. const char **names = conf_confnames;
  84. while ((name = *names++)) {
  85. name = conf_expand_value(name);
  86. in = zconf_fopen(name);
  87. if (in) {
  88. printf(_("#\n"
  89. "# using defaults found in %s\n"
  90. "#\n"), name);
  91. break;
  92. }
  93. }
  94. }
  95. if (!in)
  96. return 1;
  97. conf_filename = name;
  98. conf_lineno = 0;
  99. conf_warnings = 0;
  100. conf_unsaved = 0;
  101. for_all_symbols(i, sym) {
  102. sym->flags |= SYMBOL_NEW | SYMBOL_CHANGED;
  103. if (sym_is_choice(sym))
  104. sym->flags &= ~SYMBOL_NEW;
  105. sym->flags &= ~SYMBOL_VALID;
  106. switch (sym->type) {
  107. case S_INT:
  108. case S_HEX:
  109. case S_STRING:
  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. } else { /* bbox */
  379. strcpy(buf, "#define AUTOCONF_TIMESTAMP \"\"\n");
  380. }
  381. fprintf(out_h, "/*\n"
  382. " * Automatically generated C config: don't edit\n"
  383. " * Busybox version: %s\n"
  384. " */\n"
  385. "%s"
  386. "\n",
  387. sym_get_string_value(sym),
  388. buf);
  389. }
  390. if (!sym_change_count)
  391. sym_clear_all_valid();
  392. menu = rootmenu.list;
  393. while (menu) {
  394. sym = menu->sym;
  395. if (!sym) {
  396. if (!menu_is_visible(menu))
  397. goto next;
  398. str = menu_get_prompt(menu);
  399. fprintf(out, "\n"
  400. "#\n"
  401. "# %s\n"
  402. "#\n", str);
  403. if (out_h)
  404. fprintf(out_h, "\n"
  405. "/*\n"
  406. " * %s\n"
  407. " */\n", str);
  408. } else if (!(sym->flags & SYMBOL_CHOICE)) {
  409. sym_calc_value(sym);
  410. /* bbox: we want to see all syms
  411. if (!(sym->flags & SYMBOL_WRITE))
  412. goto next;
  413. */
  414. sym->flags &= ~SYMBOL_WRITE;
  415. type = sym->type;
  416. if (type == S_TRISTATE) {
  417. sym_calc_value(modules_sym);
  418. if (modules_sym->curr.tri == no)
  419. type = S_BOOLEAN;
  420. }
  421. switch (type) {
  422. case S_BOOLEAN:
  423. case S_TRISTATE:
  424. switch (sym_get_tristate_value(sym)) {
  425. case no:
  426. fprintf(out, "# CONFIG_%s is not set\n", sym->name);
  427. if (out_h) {
  428. fprintf(out_h, "#undef CONFIG_%s\n", sym->name);
  429. /* bbox */
  430. fprintf(out_h, "#define ENABLE_%s 0\n", sym->name);
  431. fprintf(out_h, "#define IF_%s(...)\n", sym->name);
  432. fprintf(out_h, "#define IF_NOT_%s(...) __VA_ARGS__\n", sym->name);
  433. }
  434. break;
  435. case mod:
  436. fprintf(out, "CONFIG_%s=m\n", sym->name);
  437. if (out_h)
  438. fprintf(out_h, "#define CONFIG_%s_MODULE 1\n", sym->name);
  439. break;
  440. case yes:
  441. fprintf(out, "CONFIG_%s=y\n", sym->name);
  442. if (out_h) {
  443. fprintf(out_h, "#define CONFIG_%s 1\n", sym->name);
  444. /* bbox */
  445. fprintf(out_h, "#define ENABLE_%s 1\n", sym->name);
  446. fprintf(out_h, "#ifdef MAKE_SUID\n");
  447. fprintf(out_h, "# define IF_%s(...) __VA_ARGS__ \"CONFIG_%s\"\n", sym->name, sym->name);
  448. fprintf(out_h, "#else\n");
  449. fprintf(out_h, "# define IF_%s(...) __VA_ARGS__\n", sym->name);
  450. fprintf(out_h, "#endif\n");
  451. fprintf(out_h, "#define IF_NOT_%s(...)\n", sym->name);
  452. }
  453. break;
  454. }
  455. break;
  456. case S_STRING:
  457. // fix me
  458. str = sym_get_string_value(sym);
  459. fprintf(out, "CONFIG_%s=\"", sym->name);
  460. if (out_h)
  461. fprintf(out_h, "#define CONFIG_%s \"", sym->name);
  462. do {
  463. l = strcspn(str, "\"\\");
  464. if (l) {
  465. fwrite(str, l, 1, out);
  466. if (out_h)
  467. fwrite(str, l, 1, out_h);
  468. }
  469. str += l;
  470. while (*str == '\\' || *str == '"') {
  471. fprintf(out, "\\%c", *str);
  472. if (out_h)
  473. fprintf(out_h, "\\%c", *str);
  474. str++;
  475. }
  476. } while (*str);
  477. fputs("\"\n", out);
  478. if (out_h) {
  479. fputs("\"\n", out_h);
  480. /* bbox */
  481. fprintf(out_h, "#define ENABLE_%s 1\n", sym->name);
  482. fprintf(out_h, "#ifdef MAKE_SUID\n");
  483. fprintf(out_h, "# define IF_%s(...) __VA_ARGS__ \"CONFIG_%s\"\n", sym->name, sym->name);
  484. fprintf(out_h, "#else\n");
  485. fprintf(out_h, "# define IF_%s(...) __VA_ARGS__\n", sym->name);
  486. fprintf(out_h, "#endif\n");
  487. fprintf(out_h, "#define IF_NOT_%s(...)\n", sym->name);
  488. }
  489. break;
  490. case S_HEX:
  491. str = sym_get_string_value(sym);
  492. if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
  493. fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
  494. if (out_h) {
  495. fprintf(out_h, "#define CONFIG_%s 0x%s\n", sym->name, str);
  496. /* bbox */
  497. fprintf(out_h, "#define ENABLE_%s 1\n", sym->name);
  498. fprintf(out_h, "#ifdef MAKE_SUID\n");
  499. fprintf(out_h, "# define IF_%s(...) __VA_ARGS__ \"CONFIG_%s\"\n", sym->name, sym->name);
  500. fprintf(out_h, "#else\n");
  501. fprintf(out_h, "# define IF_%s(...) __VA_ARGS__\n", sym->name);
  502. fprintf(out_h, "#endif\n");
  503. fprintf(out_h, "#define IF_NOT_%s(...)\n", sym->name);
  504. }
  505. break;
  506. }
  507. case S_INT:
  508. str = sym_get_string_value(sym);
  509. if (!str[0])
  510. str = "0";
  511. fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
  512. if (out_h) {
  513. fprintf(out_h, "#define CONFIG_%s %s\n", sym->name, str);
  514. /* bbox */
  515. fprintf(out_h, "#define ENABLE_%s 1\n", sym->name);
  516. fprintf(out_h, "#ifdef MAKE_SUID\n");
  517. fprintf(out_h, "# define IF_%s(...) __VA_ARGS__ \"CONFIG_%s\"\n", sym->name, sym->name);
  518. fprintf(out_h, "#else\n");
  519. fprintf(out_h, "# define IF_%s(...) __VA_ARGS__\n", sym->name);
  520. fprintf(out_h, "#endif\n");
  521. fprintf(out_h, "#define IF_NOT_%s(...)\n", sym->name);
  522. }
  523. break;
  524. }
  525. }
  526. next:
  527. if (menu->list) {
  528. menu = menu->list;
  529. continue;
  530. }
  531. if (menu->next)
  532. menu = menu->next;
  533. else while ((menu = menu->parent)) {
  534. if (menu->next) {
  535. menu = menu->next;
  536. break;
  537. }
  538. }
  539. }
  540. fclose(out);
  541. if (out_h) {
  542. fclose(out_h);
  543. rename(".tmpconfig.h", "include/autoconf.h");
  544. }
  545. if (!name || basename != conf_def_filename) {
  546. if (!name)
  547. name = conf_def_filename;
  548. sprintf(tmpname, "%s.old", name);
  549. rename(name, tmpname);
  550. }
  551. sprintf(tmpname, "%s%s", dirname, basename);
  552. if (rename(newname, tmpname))
  553. return 1;
  554. sym_change_count = 0;
  555. return 0;
  556. }