3
0

confdata.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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];
  313. char tmpname[256];
  314. char newname[256];
  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. "# Busybox 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. size_t ret = \
  371. strftime(buf, sizeof(buf), "#define AUTOCONF_TIMESTAMP "
  372. "\"%Y-%m-%d %H:%M:%S %Z\"\n", localtime(&now));
  373. /* if user has Factory timezone or some other odd install, the
  374. * %Z above will overflow the string leaving us with undefined
  375. * results ... so let's try again without the timezone.
  376. */
  377. if (ret == 0)
  378. strftime(buf, sizeof(buf), "#define AUTOCONF_TIMESTAMP "
  379. "\"%Y-%m-%d %H:%M:%S\"\n", localtime(&now));
  380. } else { /* bbox */
  381. strcpy(buf, "#define AUTOCONF_TIMESTAMP \"\"\n");
  382. }
  383. fprintf(out_h, "/*\n"
  384. " * Automatically generated C config: don't edit\n"
  385. " * Busybox version: %s\n"
  386. " */\n"
  387. "%s"
  388. "\n",
  389. sym_get_string_value(sym),
  390. buf);
  391. }
  392. if (!sym_change_count)
  393. sym_clear_all_valid();
  394. menu = rootmenu.list;
  395. while (menu) {
  396. sym = menu->sym;
  397. if (!sym) {
  398. if (!menu_is_visible(menu))
  399. goto next;
  400. str = menu_get_prompt(menu);
  401. fprintf(out, "\n"
  402. "#\n"
  403. "# %s\n"
  404. "#\n", str);
  405. if (out_h)
  406. fprintf(out_h, "\n"
  407. "/*\n"
  408. " * %s\n"
  409. " */\n", str);
  410. } else if (!(sym->flags & SYMBOL_CHOICE)) {
  411. sym_calc_value(sym);
  412. /* bbox: we want to see all syms
  413. if (!(sym->flags & SYMBOL_WRITE))
  414. goto next;
  415. */
  416. sym->flags &= ~SYMBOL_WRITE;
  417. type = sym->type;
  418. if (type == S_TRISTATE) {
  419. sym_calc_value(modules_sym);
  420. if (modules_sym->curr.tri == no)
  421. type = S_BOOLEAN;
  422. }
  423. switch (type) {
  424. case S_BOOLEAN:
  425. case S_TRISTATE:
  426. switch (sym_get_tristate_value(sym)) {
  427. case no:
  428. fprintf(out, "# CONFIG_%s is not set\n", sym->name);
  429. if (out_h) {
  430. fprintf(out_h, "#undef CONFIG_%s\n", sym->name);
  431. /* bbox */
  432. fprintf(out_h, "#define ENABLE_%s 0\n", sym->name);
  433. fprintf(out_h, "#define IF_%s(...)\n", sym->name);
  434. fprintf(out_h, "#define IF_NOT_%s(...) __VA_ARGS__\n", sym->name);
  435. }
  436. break;
  437. case mod:
  438. fprintf(out, "CONFIG_%s=m\n", sym->name);
  439. if (out_h)
  440. fprintf(out_h, "#define CONFIG_%s_MODULE 1\n", sym->name);
  441. break;
  442. case yes:
  443. fprintf(out, "CONFIG_%s=y\n", sym->name);
  444. if (out_h) {
  445. fprintf(out_h, "#define CONFIG_%s 1\n", sym->name);
  446. /* bbox */
  447. fprintf(out_h, "#define ENABLE_%s 1\n", sym->name);
  448. fprintf(out_h, "#ifdef MAKE_SUID\n");
  449. fprintf(out_h, "# define IF_%s(...) __VA_ARGS__ \"CONFIG_%s\"\n", sym->name, sym->name);
  450. fprintf(out_h, "#else\n");
  451. fprintf(out_h, "# define IF_%s(...) __VA_ARGS__\n", sym->name);
  452. fprintf(out_h, "#endif\n");
  453. fprintf(out_h, "#define IF_NOT_%s(...)\n", sym->name);
  454. }
  455. break;
  456. }
  457. break;
  458. case S_STRING:
  459. // fix me
  460. str = sym_get_string_value(sym);
  461. fprintf(out, "CONFIG_%s=\"", sym->name);
  462. if (out_h)
  463. fprintf(out_h, "#define CONFIG_%s \"", sym->name);
  464. do {
  465. l = strcspn(str, "\"\\");
  466. if (l) {
  467. fwrite(str, l, 1, out);
  468. if (out_h)
  469. fwrite(str, l, 1, out_h);
  470. }
  471. str += l;
  472. while (*str == '\\' || *str == '"') {
  473. fprintf(out, "\\%c", *str);
  474. if (out_h)
  475. fprintf(out_h, "\\%c", *str);
  476. str++;
  477. }
  478. } while (*str);
  479. fputs("\"\n", out);
  480. if (out_h) {
  481. fputs("\"\n", out_h);
  482. /* bbox */
  483. fprintf(out_h, "#define ENABLE_%s 1\n", sym->name);
  484. fprintf(out_h, "#ifdef MAKE_SUID\n");
  485. fprintf(out_h, "# define IF_%s(...) __VA_ARGS__ \"CONFIG_%s\"\n", sym->name, sym->name);
  486. fprintf(out_h, "#else\n");
  487. fprintf(out_h, "# define IF_%s(...) __VA_ARGS__\n", sym->name);
  488. fprintf(out_h, "#endif\n");
  489. fprintf(out_h, "#define IF_NOT_%s(...)\n", sym->name);
  490. }
  491. break;
  492. case S_HEX:
  493. str = sym_get_string_value(sym);
  494. if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
  495. fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
  496. if (out_h) {
  497. fprintf(out_h, "#define CONFIG_%s 0x%s\n", sym->name, str);
  498. /* bbox */
  499. fprintf(out_h, "#define ENABLE_%s 1\n", sym->name);
  500. fprintf(out_h, "#ifdef MAKE_SUID\n");
  501. fprintf(out_h, "# define IF_%s(...) __VA_ARGS__ \"CONFIG_%s\"\n", sym->name, sym->name);
  502. fprintf(out_h, "#else\n");
  503. fprintf(out_h, "# define IF_%s(...) __VA_ARGS__\n", sym->name);
  504. fprintf(out_h, "#endif\n");
  505. fprintf(out_h, "#define IF_NOT_%s(...)\n", sym->name);
  506. }
  507. break;
  508. }
  509. case S_INT:
  510. str = sym_get_string_value(sym);
  511. if (!str[0])
  512. str = "0";
  513. fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
  514. if (out_h) {
  515. fprintf(out_h, "#define CONFIG_%s %s\n", sym->name, str);
  516. /* bbox */
  517. fprintf(out_h, "#define ENABLE_%s 1\n", sym->name);
  518. fprintf(out_h, "#ifdef MAKE_SUID\n");
  519. fprintf(out_h, "# define IF_%s(...) __VA_ARGS__ \"CONFIG_%s\"\n", sym->name, sym->name);
  520. fprintf(out_h, "#else\n");
  521. fprintf(out_h, "# define IF_%s(...) __VA_ARGS__\n", sym->name);
  522. fprintf(out_h, "#endif\n");
  523. fprintf(out_h, "#define IF_NOT_%s(...)\n", sym->name);
  524. }
  525. break;
  526. }
  527. }
  528. next:
  529. if (menu->list) {
  530. menu = menu->list;
  531. continue;
  532. }
  533. if (menu->next)
  534. menu = menu->next;
  535. else while ((menu = menu->parent)) {
  536. if (menu->next) {
  537. menu = menu->next;
  538. break;
  539. }
  540. }
  541. }
  542. fclose(out);
  543. if (out_h) {
  544. fclose(out_h);
  545. rename(".tmpconfig.h", "include/autoconf.h");
  546. }
  547. if (!name || basename != conf_def_filename) {
  548. if (!name)
  549. name = conf_def_filename;
  550. sprintf(tmpname, "%s.old", name);
  551. rename(name, tmpname);
  552. }
  553. sprintf(tmpname, "%s%s", dirname, basename);
  554. if (rename(newname, tmpname))
  555. return 1;
  556. sym_change_count = 0;
  557. return 0;
  558. }