confdata.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 <unistd.h>
  11. #define LKC_DIRECT_LINK
  12. #include "lkc.h"
  13. const char conf_def_filename[] = ".config";
  14. const char conf_defname[] = "defconfig";
  15. const char *conf_confnames[] = {
  16. ".config",
  17. conf_defname,
  18. NULL,
  19. };
  20. static char *conf_expand_value(const char *in)
  21. {
  22. struct symbol *sym;
  23. const char *src;
  24. static char res_value[SYMBOL_MAXLENGTH];
  25. char *dst, name[SYMBOL_MAXLENGTH];
  26. res_value[0] = 0;
  27. dst = name;
  28. while ((src = strchr(in, '$'))) {
  29. strncat(res_value, in, src - in);
  30. src++;
  31. dst = name;
  32. while (isalnum(*src) || *src == '_')
  33. *dst++ = *src++;
  34. *dst = 0;
  35. sym = sym_lookup(name, 0);
  36. sym_calc_value(sym);
  37. strcat(res_value, sym_get_string_value(sym));
  38. in = src;
  39. }
  40. strcat(res_value, in);
  41. return res_value;
  42. }
  43. char *conf_get_default_confname(void)
  44. {
  45. struct stat buf;
  46. static char fullname[PATH_MAX+1];
  47. char *env, *name;
  48. name = conf_expand_value(conf_defname);
  49. env = getenv(SRCTREE);
  50. if (env) {
  51. sprintf(fullname, "%s/%s", env, name);
  52. if (!stat(fullname, &buf))
  53. return fullname;
  54. }
  55. return name;
  56. }
  57. int conf_read(const char *name)
  58. {
  59. FILE *in = NULL;
  60. char line[1024];
  61. char *p, *p2;
  62. int lineno = 0;
  63. struct symbol *sym;
  64. struct property *prop;
  65. struct expr *e;
  66. int i;
  67. if (name) {
  68. in = zconf_fopen(name);
  69. } else {
  70. const char **names = conf_confnames;
  71. while ((name = *names++)) {
  72. name = conf_expand_value(name);
  73. in = zconf_fopen(name);
  74. if (in) {
  75. printf("#\n"
  76. "# using defaults found in %s\n"
  77. "#\n", name);
  78. break;
  79. }
  80. }
  81. }
  82. if (!in)
  83. return 1;
  84. for_all_symbols(i, sym) {
  85. sym->flags |= SYMBOL_NEW | SYMBOL_CHANGED;
  86. sym->flags &= ~SYMBOL_VALID;
  87. switch (sym->type) {
  88. case S_INT:
  89. case S_HEX:
  90. case S_STRING:
  91. free(sym->user.val);
  92. default:
  93. sym->user.val = NULL;
  94. sym->user.tri = no;
  95. }
  96. }
  97. while (fgets(line, sizeof(line), in)) {
  98. lineno++;
  99. sym = NULL;
  100. switch (line[0]) {
  101. case '#':
  102. if (line[1]!=' ')
  103. continue;
  104. p = strchr(line + 2, ' ');
  105. if (!p)
  106. continue;
  107. *p++ = 0;
  108. if (strncmp(p, "is not set", 10))
  109. continue;
  110. sym = sym_find(line + 2);
  111. if (!sym) {
  112. fprintf(stderr, "%s:%d: trying to assign nonexistent symbol %s\n", name, lineno, line + 2);
  113. break;
  114. }
  115. switch (sym->type) {
  116. case S_BOOLEAN:
  117. case S_TRISTATE:
  118. sym->user.tri = no;
  119. sym->flags &= ~SYMBOL_NEW;
  120. break;
  121. default:
  122. ;
  123. }
  124. break;
  125. case 'A' ... 'Z':
  126. p = strchr(line, '=');
  127. if (!p)
  128. continue;
  129. *p++ = 0;
  130. p2 = strchr(p, '\n');
  131. if (p2)
  132. *p2 = 0;
  133. sym = sym_find(line);
  134. if (!sym) {
  135. fprintf(stderr, "%s:%d: trying to assign nonexistent symbol %s\n", name, lineno, line);
  136. break;
  137. }
  138. switch (sym->type) {
  139. case S_TRISTATE:
  140. if (p[0] == 'm') {
  141. sym->user.tri = mod;
  142. sym->flags &= ~SYMBOL_NEW;
  143. break;
  144. }
  145. case S_BOOLEAN:
  146. if (p[0] == 'y') {
  147. sym->user.tri = yes;
  148. sym->flags &= ~SYMBOL_NEW;
  149. break;
  150. }
  151. if (p[0] == 'n') {
  152. sym->user.tri = no;
  153. sym->flags &= ~SYMBOL_NEW;
  154. break;
  155. }
  156. break;
  157. case S_STRING:
  158. if (*p++ != '"')
  159. break;
  160. for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
  161. if (*p2 == '"') {
  162. *p2 = 0;
  163. break;
  164. }
  165. memmove(p2, p2 + 1, strlen(p2));
  166. }
  167. if (!p2) {
  168. fprintf(stderr, "%s:%d: invalid string found\n", name, lineno);
  169. exit(1);
  170. }
  171. case S_INT:
  172. case S_HEX:
  173. if (sym_string_valid(sym, p)) {
  174. sym->user.val = strdup(p);
  175. sym->flags &= ~SYMBOL_NEW;
  176. } else {
  177. fprintf(stderr, "%s:%d: symbol value '%s' invalid for %s\n", name, lineno, p, sym->name);
  178. exit(1);
  179. }
  180. break;
  181. default:
  182. ;
  183. }
  184. break;
  185. case '\n':
  186. break;
  187. default:
  188. continue;
  189. }
  190. if (sym && sym_is_choice_value(sym)) {
  191. struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
  192. switch (sym->user.tri) {
  193. case no:
  194. break;
  195. case mod:
  196. if (cs->user.tri == yes)
  197. /* warn? */;
  198. break;
  199. case yes:
  200. if (cs->user.tri != no)
  201. /* warn? */;
  202. cs->user.val = sym;
  203. break;
  204. }
  205. cs->user.tri = E_OR(cs->user.tri, sym->user.tri);
  206. cs->flags &= ~SYMBOL_NEW;
  207. }
  208. }
  209. fclose(in);
  210. if (modules_sym)
  211. sym_calc_value(modules_sym);
  212. for_all_symbols(i, sym) {
  213. sym_calc_value(sym);
  214. if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
  215. if (sym->visible == no)
  216. sym->flags |= SYMBOL_NEW;
  217. switch (sym->type) {
  218. case S_STRING:
  219. case S_INT:
  220. case S_HEX:
  221. if (!sym_string_within_range(sym, sym->user.val))
  222. sym->flags |= SYMBOL_NEW;
  223. default:
  224. break;
  225. }
  226. }
  227. if (!sym_is_choice(sym))
  228. continue;
  229. prop = sym_get_choice_prop(sym);
  230. for (e = prop->expr; e; e = e->left.expr)
  231. if (e->right.sym->visible != no)
  232. sym->flags |= e->right.sym->flags & SYMBOL_NEW;
  233. }
  234. sym_change_count = 1;
  235. return 0;
  236. }
  237. struct menu *next_menu(struct menu *menu)
  238. {
  239. if (menu->list) return menu->list;
  240. do {
  241. if (menu->next) {
  242. menu = menu->next;
  243. break;
  244. }
  245. } while ((menu = menu->parent));
  246. return menu;
  247. }
  248. #define SYMBOL_FORCEWRITE (1<<31)
  249. int conf_write(const char *name)
  250. {
  251. FILE *out, *out_h;
  252. struct symbol *sym;
  253. struct menu *menu;
  254. const char *basename;
  255. char dirname[128], tmpname[128], newname[128];
  256. int type, l;
  257. const char *str;
  258. /* busybox`s code */
  259. const char *opt_name;
  260. int use_flg;
  261. dirname[0] = 0;
  262. if (name && name[0]) {
  263. struct stat st;
  264. char *slash;
  265. if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
  266. strcpy(dirname, name);
  267. strcat(dirname, "/");
  268. basename = conf_def_filename;
  269. } else if ((slash = strrchr(name, '/'))) {
  270. int size = slash - name + 1;
  271. memcpy(dirname, name, size);
  272. dirname[size] = 0;
  273. if (slash[1])
  274. basename = slash + 1;
  275. else
  276. basename = conf_def_filename;
  277. } else
  278. basename = name;
  279. } else
  280. basename = conf_def_filename;
  281. sprintf(newname, "%s.tmpconfig.%d", dirname, (int)getpid());
  282. out = fopen(newname, "w");
  283. if (!out)
  284. return 1;
  285. out_h = NULL;
  286. if (!name) {
  287. out_h = fopen(".tmpconfig.h", "w");
  288. if (!out_h)
  289. return 1;
  290. }
  291. fprintf(out, "#\n"
  292. "# Automatically generated make config: don't edit\n"
  293. "#\n");
  294. /* busybox`s code */
  295. if (out_h) {
  296. fprintf(out_h, "#ifndef BB_CONFIG_H\n#define BB_CONFIG_H\n");
  297. fprintf(out_h, "/*\n"
  298. " * Automatically generated header file: don't edit\n"
  299. " */\n\n"
  300. "/* Version Number */\n"
  301. "#define BB_VER \"%s\"\n"
  302. "#define BB_BT \"%s\"\n",
  303. getenv("VERSION"),
  304. getenv("BUILDTIME"));
  305. if (getenv("EXTRA_VERSION"))
  306. fprintf(out_h, "#define BB_EXTRA_VERSION \"%s\"\n",
  307. getenv("EXTRA_VERSION"));
  308. fprintf(out_h, "\n");
  309. }
  310. /* end busybox`s code */
  311. if (!sym_change_count)
  312. sym_clear_all_valid();
  313. /* Force write of all non-duplicate symbols. */
  314. /* Write out everything by default. */
  315. for(menu = rootmenu.list; menu; menu = next_menu(menu))
  316. if (menu->sym) menu->sym->flags |= SYMBOL_FORCEWRITE;
  317. menu = rootmenu.list;
  318. while (menu) {
  319. sym = menu->sym;
  320. if (!sym) {
  321. if (!menu_is_visible(menu))
  322. goto next;
  323. str = menu_get_prompt(menu);
  324. fprintf(out, "\n"
  325. "#\n"
  326. "# %s\n"
  327. "#\n", str);
  328. if (out_h)
  329. fprintf(out_h, "\n"
  330. "/*\n"
  331. " * %s\n"
  332. " */\n", str);
  333. } else if (!(sym->flags & SYMBOL_CHOICE)) {
  334. sym_calc_value(sym);
  335. if (!(sym->flags & SYMBOL_FORCEWRITE))
  336. goto next;
  337. sym->flags &= ~SYMBOL_FORCEWRITE;
  338. type = sym->type;
  339. if (type == S_TRISTATE) {
  340. sym_calc_value(modules_sym);
  341. if (modules_sym->curr.tri == no)
  342. type = S_BOOLEAN;
  343. }
  344. /* busybox`s code */
  345. opt_name = strchr(sym->name, '_');
  346. if(opt_name == NULL)
  347. opt_name = sym->name;
  348. else
  349. opt_name++;
  350. use_flg = 1;
  351. /* end busybox`s code */
  352. switch (type) {
  353. case S_BOOLEAN:
  354. case S_TRISTATE:
  355. switch (sym_get_tristate_value(sym)) {
  356. case no:
  357. fprintf(out, "# %s is not set\n", sym->name);
  358. if (out_h)
  359. fprintf(out_h, "#undef %s\n", sym->name);
  360. use_flg = 0; /* busybox`s code */
  361. break;
  362. case mod:
  363. #if 0 /* busybox`s code */
  364. fprintf(out, "%s=m\n", sym->name);
  365. if (out_h)
  366. fprintf(out_h, "#define %s_MODULE 1\n", sym->name);
  367. #endif /* busybox`s code */
  368. break;
  369. case yes:
  370. fprintf(out, "%s=y\n", sym->name);
  371. if (out_h)
  372. fprintf(out_h, "#define %s 1\n", sym->name);
  373. break;
  374. }
  375. break;
  376. case S_STRING:
  377. // fix me
  378. str = sym_get_string_value(sym);
  379. fprintf(out, "%s=\"", sym->name);
  380. if (out_h)
  381. fprintf(out_h, "#define %s \"", sym->name);
  382. do {
  383. l = strcspn(str, "\"\\");
  384. if (l) {
  385. fwrite(str, l, 1, out);
  386. if (out_h)
  387. fwrite(str, l, 1, out_h);
  388. }
  389. str += l;
  390. while (*str == '\\' || *str == '"') {
  391. fprintf(out, "\\%c", *str);
  392. if (out_h)
  393. fprintf(out_h, "\\%c", *str);
  394. str++;
  395. }
  396. } while (*str);
  397. fputs("\"\n", out);
  398. if (out_h)
  399. fputs("\"\n", out_h);
  400. break;
  401. case S_HEX:
  402. str = sym_get_string_value(sym);
  403. if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
  404. fprintf(out, "%s=%s\n", sym->name, *str ? str : "0");
  405. if (out_h)
  406. fprintf(out_h, "#define %s 0x%s\n", sym->name, str);
  407. break;
  408. }
  409. case S_INT:
  410. str = sym_get_string_value(sym);
  411. fprintf(out, "%s=%s\n", sym->name, *str ? str : "0");
  412. if (out_h)
  413. fprintf(out_h, "#define %s %s\n", sym->name, str);
  414. break;
  415. }
  416. /* busybox`s code */
  417. if (out_h) {
  418. fprintf(out_h, "#define ENABLE_%s %d\n", opt_name, use_flg);
  419. fprintf(out_h, "#define USE_%s(...)%s\n", opt_name,
  420. (use_flg ? " __VA_ARGS__" : ""));
  421. fprintf(out_h, "#define SKIP_%s(...)%s\n\n", opt_name,
  422. (use_flg ? "" : " __VA_ARGS__"));
  423. }
  424. /* end busybox`s code */
  425. }
  426. next:
  427. menu = next_menu(menu);
  428. }
  429. fclose(out);
  430. if (out_h) {
  431. fprintf(out_h, "#endif /* BB_CONFIG_H */\n"); /* busybox`s code */
  432. fclose(out_h);
  433. rename(".tmpconfig.h", "include/bb_config.h"); /* busybox`s config name */
  434. file_write_dep(NULL);
  435. }
  436. if (!name || basename != conf_def_filename) {
  437. if (!name)
  438. name = conf_def_filename;
  439. sprintf(tmpname, "%s.old", name);
  440. rename(name, tmpname);
  441. }
  442. sprintf(tmpname, "%s%s", dirname, basename);
  443. if (rename(newname, tmpname))
  444. return 1;
  445. sym_change_count = 0;
  446. return 0;
  447. }