confdata.c 14 KB

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