confdata.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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((unsigned char)*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. char *source_date_epoch;
  321. struct tm *build_time;
  322. dirname[0] = 0;
  323. if (name && name[0]) {
  324. struct stat st;
  325. char *slash;
  326. if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
  327. strcpy(dirname, name);
  328. strcat(dirname, "/");
  329. basename = conf_def_filename;
  330. } else if ((slash = strrchr(name, '/'))) {
  331. int size = slash - name + 1;
  332. memcpy(dirname, name, size);
  333. dirname[size] = 0;
  334. if (slash[1])
  335. basename = slash + 1;
  336. else
  337. basename = conf_def_filename;
  338. } else
  339. basename = name;
  340. } else
  341. basename = conf_def_filename;
  342. sprintf(newname, "%s.tmpconfig.%d", dirname, (int)getpid());
  343. out = fopen(newname, "w");
  344. if (!out)
  345. return 1;
  346. out_h = NULL;
  347. if (!name) {
  348. out_h = fopen(".tmpconfig.h", "w");
  349. if (!out_h)
  350. return 1;
  351. file_write_dep(NULL);
  352. }
  353. sym = sym_lookup("KERNELVERSION", 0);
  354. sym_calc_value(sym);
  355. source_date_epoch = getenv("SOURCE_DATE_EPOCH");
  356. if (source_date_epoch && *source_date_epoch) {
  357. now = strtoull(source_date_epoch, NULL, 10);
  358. build_time = gmtime(&now);
  359. } else {
  360. time(&now);
  361. build_time = localtime(&now);
  362. }
  363. env = getenv("KCONFIG_NOTIMESTAMP");
  364. if (env && *env)
  365. use_timestamp = 0;
  366. fprintf(out, _("#\n"
  367. "# Automatically generated make config: don't edit\n"
  368. "# Busybox version: %s\n"
  369. "%s%s"
  370. "#\n"),
  371. sym_get_string_value(sym),
  372. use_timestamp ? "# " : "",
  373. use_timestamp ? ctime(&now) : "");
  374. if (out_h) {
  375. char buf[sizeof("#define AUTOCONF_TIMESTAMP "
  376. "\"YYYY-MM-DD HH:MM:SS some_timezone\"\n")];
  377. buf[0] = '\0';
  378. if (use_timestamp) {
  379. size_t ret = \
  380. strftime(buf, sizeof(buf), "#define AUTOCONF_TIMESTAMP "
  381. "\"%Y-%m-%d %H:%M:%S %Z\"\n", build_time);
  382. /* if user has Factory timezone or some other odd install, the
  383. * %Z above will overflow the string leaving us with undefined
  384. * results ... so let's try again without the timezone.
  385. */
  386. if (ret == 0)
  387. strftime(buf, sizeof(buf), "#define AUTOCONF_TIMESTAMP "
  388. "\"%Y-%m-%d %H:%M:%S\"\n", build_time);
  389. } else { /* bbox */
  390. strcpy(buf, "#define AUTOCONF_TIMESTAMP \"\"\n");
  391. }
  392. fprintf(out_h, "/*\n"
  393. " * Automatically generated C config: don't edit\n"
  394. " * Busybox version: %s\n"
  395. " */\n"
  396. "%s"
  397. "\n",
  398. sym_get_string_value(sym),
  399. buf);
  400. }
  401. if (!sym_change_count)
  402. sym_clear_all_valid();
  403. menu = rootmenu.list;
  404. while (menu) {
  405. sym = menu->sym;
  406. if (!sym) {
  407. if (!menu_is_visible(menu))
  408. goto next;
  409. str = menu_get_prompt(menu);
  410. fprintf(out, "\n"
  411. "#\n"
  412. "# %s\n"
  413. "#\n", str);
  414. if (out_h)
  415. fprintf(out_h, "\n"
  416. "/*\n"
  417. " * %s\n"
  418. " */\n", str);
  419. } else if (!(sym->flags & SYMBOL_CHOICE)) {
  420. sym_calc_value(sym);
  421. /* bbox: we want to see all syms
  422. if (!(sym->flags & SYMBOL_WRITE))
  423. goto next;
  424. */
  425. sym->flags &= ~SYMBOL_WRITE;
  426. type = sym->type;
  427. if (type == S_TRISTATE) {
  428. sym_calc_value(modules_sym);
  429. if (modules_sym->curr.tri == no)
  430. type = S_BOOLEAN;
  431. }
  432. switch (type) {
  433. case S_BOOLEAN:
  434. case S_TRISTATE:
  435. switch (sym_get_tristate_value(sym)) {
  436. case no:
  437. fprintf(out, "# CONFIG_%s is not set\n", sym->name);
  438. if (out_h) {
  439. fprintf(out_h, "#undef CONFIG_%s\n", sym->name);
  440. /* bbox */
  441. fprintf(out_h, "#define ENABLE_%s 0\n", sym->name);
  442. fprintf(out_h, "#define IF_%s(...)\n", sym->name);
  443. fprintf(out_h, "#define IF_NOT_%s(...) __VA_ARGS__\n", sym->name);
  444. }
  445. break;
  446. case mod:
  447. fprintf(out, "CONFIG_%s=m\n", sym->name);
  448. if (out_h)
  449. fprintf(out_h, "#define CONFIG_%s_MODULE 1\n", sym->name);
  450. break;
  451. case yes:
  452. fprintf(out, "CONFIG_%s=y\n", sym->name);
  453. if (out_h) {
  454. fprintf(out_h, "#define CONFIG_%s 1\n", sym->name);
  455. /* bbox */
  456. fprintf(out_h, "#define ENABLE_%s 1\n", sym->name);
  457. fprintf(out_h, "#ifdef MAKE_SUID\n");
  458. fprintf(out_h, "# define IF_%s(...) __VA_ARGS__ \"CONFIG_%s\"\n", sym->name, sym->name);
  459. fprintf(out_h, "#else\n");
  460. fprintf(out_h, "# define IF_%s(...) __VA_ARGS__\n", sym->name);
  461. fprintf(out_h, "#endif\n");
  462. fprintf(out_h, "#define IF_NOT_%s(...)\n", sym->name);
  463. }
  464. break;
  465. }
  466. break;
  467. case S_STRING:
  468. // fix me
  469. str = sym_get_string_value(sym);
  470. fprintf(out, "CONFIG_%s=\"", sym->name);
  471. if (out_h)
  472. fprintf(out_h, "#define CONFIG_%s \"", sym->name);
  473. do {
  474. l = strcspn(str, "\"\\");
  475. if (l) {
  476. fwrite(str, l, 1, out);
  477. if (out_h)
  478. fwrite(str, l, 1, out_h);
  479. }
  480. str += l;
  481. while (*str == '\\' || *str == '"') {
  482. fprintf(out, "\\%c", *str);
  483. if (out_h)
  484. fprintf(out_h, "\\%c", *str);
  485. str++;
  486. }
  487. } while (*str);
  488. fputs("\"\n", out);
  489. if (out_h) {
  490. fputs("\"\n", out_h);
  491. /* bbox */
  492. fprintf(out_h, "#define ENABLE_%s 1\n", sym->name);
  493. fprintf(out_h, "#ifdef MAKE_SUID\n");
  494. fprintf(out_h, "# define IF_%s(...) __VA_ARGS__ \"CONFIG_%s\"\n", sym->name, sym->name);
  495. fprintf(out_h, "#else\n");
  496. fprintf(out_h, "# define IF_%s(...) __VA_ARGS__\n", sym->name);
  497. fprintf(out_h, "#endif\n");
  498. fprintf(out_h, "#define IF_NOT_%s(...)\n", sym->name);
  499. }
  500. break;
  501. case S_HEX:
  502. str = sym_get_string_value(sym);
  503. if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
  504. fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
  505. if (out_h) {
  506. fprintf(out_h, "#define CONFIG_%s 0x%s\n", sym->name, str);
  507. /* bbox */
  508. fprintf(out_h, "#define ENABLE_%s 1\n", sym->name);
  509. fprintf(out_h, "#ifdef MAKE_SUID\n");
  510. fprintf(out_h, "# define IF_%s(...) __VA_ARGS__ \"CONFIG_%s\"\n", sym->name, sym->name);
  511. fprintf(out_h, "#else\n");
  512. fprintf(out_h, "# define IF_%s(...) __VA_ARGS__\n", sym->name);
  513. fprintf(out_h, "#endif\n");
  514. fprintf(out_h, "#define IF_NOT_%s(...)\n", sym->name);
  515. }
  516. break;
  517. }
  518. case S_INT:
  519. str = sym_get_string_value(sym);
  520. if (!str[0])
  521. str = "0";
  522. fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
  523. if (out_h) {
  524. fprintf(out_h, "#define CONFIG_%s %s\n", sym->name, str);
  525. /* bbox */
  526. fprintf(out_h, "#define ENABLE_%s 1\n", sym->name);
  527. fprintf(out_h, "#ifdef MAKE_SUID\n");
  528. fprintf(out_h, "# define IF_%s(...) __VA_ARGS__ \"CONFIG_%s\"\n", sym->name, sym->name);
  529. fprintf(out_h, "#else\n");
  530. fprintf(out_h, "# define IF_%s(...) __VA_ARGS__\n", sym->name);
  531. fprintf(out_h, "#endif\n");
  532. fprintf(out_h, "#define IF_NOT_%s(...)\n", sym->name);
  533. }
  534. break;
  535. }
  536. }
  537. next:
  538. if (menu->list) {
  539. menu = menu->list;
  540. continue;
  541. }
  542. if (menu->next)
  543. menu = menu->next;
  544. else while ((menu = menu->parent)) {
  545. if (menu->next) {
  546. menu = menu->next;
  547. break;
  548. }
  549. }
  550. }
  551. fclose(out);
  552. if (out_h) {
  553. fclose(out_h);
  554. rename(".tmpconfig.h", "include/autoconf.h");
  555. }
  556. if (!name || basename != conf_def_filename) {
  557. if (!name)
  558. name = conf_def_filename;
  559. sprintf(tmpname, "%s.old", name);
  560. rename(name, tmpname);
  561. }
  562. sprintf(tmpname, "%s%s", dirname, basename);
  563. if (rename(newname, tmpname))
  564. return 1;
  565. sym_change_count = 0;
  566. return 0;
  567. }