confdata.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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[] = "extra/Configs/Config.$TARGET_ARCH.default";
  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[4096+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. if (sym->user.val)
  92. free(sym->user.val);
  93. default:
  94. sym->user.val = NULL;
  95. sym->user.tri = no;
  96. }
  97. }
  98. while (fgets(line, sizeof(line), in)) {
  99. lineno++;
  100. sym = NULL;
  101. switch (line[0]) {
  102. case '#':
  103. if (line[1]!=' ')
  104. continue;
  105. p = strchr(line + 2, ' ');
  106. if (!p)
  107. continue;
  108. *p++ = 0;
  109. if (strncmp(p, "is not set", 10))
  110. continue;
  111. sym = sym_find(line + 2);
  112. if (!sym) {
  113. fprintf(stderr, "%s:%d: trying to assign nonexistent symbol %s\n", name, lineno, line + 2);
  114. break;
  115. }
  116. switch (sym->type) {
  117. case S_BOOLEAN:
  118. case S_TRISTATE:
  119. sym->user.tri = no;
  120. sym->flags &= ~SYMBOL_NEW;
  121. break;
  122. default:
  123. ;
  124. }
  125. break;
  126. case 'A' ... 'Z':
  127. p = strchr(line, '=');
  128. if (!p)
  129. continue;
  130. *p++ = 0;
  131. p2 = strchr(p, '\n');
  132. if (p2)
  133. *p2 = 0;
  134. sym = sym_find(line);
  135. if (!sym) {
  136. fprintf(stderr, "%s:%d: trying to assign nonexistent symbol %s\n", name, lineno, line);
  137. break;
  138. }
  139. switch (sym->type) {
  140. case S_TRISTATE:
  141. if (p[0] == 'm') {
  142. sym->user.tri = mod;
  143. sym->flags &= ~SYMBOL_NEW;
  144. break;
  145. }
  146. case S_BOOLEAN:
  147. if (p[0] == 'y') {
  148. sym->user.tri = yes;
  149. sym->flags &= ~SYMBOL_NEW;
  150. break;
  151. }
  152. if (p[0] == 'n') {
  153. sym->user.tri = no;
  154. sym->flags &= ~SYMBOL_NEW;
  155. break;
  156. }
  157. break;
  158. case S_STRING:
  159. if (*p++ != '"')
  160. break;
  161. for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
  162. if (*p2 == '"') {
  163. *p2 = 0;
  164. break;
  165. }
  166. memmove(p2, p2 + 1, strlen(p2));
  167. }
  168. if (!p2) {
  169. fprintf(stderr, "%s:%d: invalid string found\n", name, lineno);
  170. exit(1);
  171. }
  172. case S_INT:
  173. case S_HEX:
  174. if (sym_string_valid(sym, p)) {
  175. sym->user.val = strdup(p);
  176. sym->flags &= ~SYMBOL_NEW;
  177. } else {
  178. fprintf(stderr, "%s:%d: symbol value '%s' invalid for %s\n", name, lineno, p, sym->name);
  179. exit(1);
  180. }
  181. break;
  182. default:
  183. ;
  184. }
  185. break;
  186. case '\n':
  187. break;
  188. default:
  189. continue;
  190. }
  191. if (sym && sym_is_choice_value(sym)) {
  192. struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
  193. switch (sym->user.tri) {
  194. case no:
  195. break;
  196. case mod:
  197. if (cs->user.tri == yes)
  198. /* warn? */;
  199. break;
  200. case yes:
  201. if (cs->user.tri != no)
  202. /* warn? */;
  203. cs->user.val = sym;
  204. break;
  205. }
  206. cs->user.tri = E_OR(cs->user.tri, sym->user.tri);
  207. cs->flags &= ~SYMBOL_NEW;
  208. }
  209. }
  210. fclose(in);
  211. for_all_symbols(i, sym) {
  212. sym_calc_value(sym);
  213. if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
  214. if (sym->visible == no)
  215. sym->flags |= SYMBOL_NEW;
  216. switch (sym->type) {
  217. case S_STRING:
  218. case S_INT:
  219. case S_HEX:
  220. if (!sym_string_within_range(sym, sym->user.val))
  221. sym->flags |= SYMBOL_NEW;
  222. default:
  223. break;
  224. }
  225. }
  226. if (!sym_is_choice(sym))
  227. continue;
  228. prop = sym_get_choice_prop(sym);
  229. for (e = prop->expr; e; e = e->left.expr)
  230. if (e->right.sym->visible != no)
  231. sym->flags |= e->right.sym->flags & SYMBOL_NEW;
  232. }
  233. sym_change_count = 1;
  234. return 0;
  235. }
  236. int conf_write(const char *name)
  237. {
  238. FILE *out, *out_h;
  239. struct symbol *sym;
  240. struct menu *menu;
  241. const char *basename;
  242. char dirname[128], tmpname[128], newname[128];
  243. int type, l;
  244. const char *str;
  245. dirname[0] = 0;
  246. if (name && name[0]) {
  247. char *slash = strrchr(name, '/');
  248. if (slash) {
  249. int size = slash - name + 1;
  250. memcpy(dirname, name, size);
  251. dirname[size] = 0;
  252. if (slash[1])
  253. basename = slash + 1;
  254. else
  255. basename = conf_def_filename;
  256. } else
  257. basename = name;
  258. } else
  259. basename = conf_def_filename;
  260. sprintf(newname, "%s.tmpconfig.%d", dirname, getpid());
  261. out = fopen(newname, "w");
  262. if (!out)
  263. return 1;
  264. out_h = NULL;
  265. if (!name) {
  266. out_h = fopen(".tmpconfig.h", "w");
  267. if (!out_h)
  268. return 1;
  269. }
  270. fprintf(out, "#\n"
  271. "# Automatically generated make config: don't edit\n"
  272. "#\n");
  273. if (out_h)
  274. fprintf(out_h, "/*\n"
  275. " * Automatically generated C config: don't edit\n"
  276. " */\n"
  277. "#if !defined __FEATURES_H && !defined __need_uClibc_config_h\n"
  278. "#error Never include <bits/uClibc_config.h> directly; use <features.h> instead\n"
  279. "#endif\n"
  280. "#define AUTOCONF_INCLUDED\n\n"
  281. "/*\n"
  282. " * Version Number\n"
  283. " */\n"
  284. "#define __UCLIBC_MAJOR__ %s\n"
  285. "#define __UCLIBC_MINOR__ %s\n"
  286. "#define __UCLIBC_SUBLEVEL__ %s\n",
  287. getenv("MAJOR_VERSION"),
  288. getenv("MINOR_VERSION"),
  289. getenv("SUBLEVEL"));
  290. if (!sym_change_count)
  291. sym_clear_all_valid();
  292. menu = rootmenu.list;
  293. while (menu) {
  294. sym = menu->sym;
  295. if (!sym) {
  296. if (!menu_is_visible(menu))
  297. goto next;
  298. str = menu_get_prompt(menu);
  299. fprintf(out, "\n"
  300. "#\n"
  301. "# %s\n"
  302. "#\n", str);
  303. if (out_h)
  304. fprintf(out_h, "\n"
  305. "/*\n"
  306. " * %s\n"
  307. " */\n", str);
  308. } else if (!(sym->flags & SYMBOL_CHOICE)) {
  309. sym_calc_value(sym);
  310. if (!(sym->flags & SYMBOL_WRITE))
  311. goto next;
  312. sym->flags &= ~SYMBOL_WRITE;
  313. type = sym->type;
  314. if (type == S_TRISTATE) {
  315. sym_calc_value(modules_sym);
  316. if (modules_sym->curr.tri == no)
  317. type = S_BOOLEAN;
  318. }
  319. switch (type) {
  320. case S_BOOLEAN:
  321. case S_TRISTATE:
  322. switch (sym_get_tristate_value(sym)) {
  323. case no:
  324. fprintf(out, "# %s is not set\n", sym->name);
  325. if (out_h)
  326. fprintf(out_h, "#undef __%s__\n", sym->name);
  327. break;
  328. case mod:
  329. fprintf(out, "%s=m\n", sym->name);
  330. if (out_h)
  331. fprintf(out_h, "#define %s_MODULE 1\n", sym->name);
  332. break;
  333. case yes:
  334. fprintf(out, "%s=y\n", sym->name);
  335. if (out_h)
  336. fprintf(out_h, "#define __%s__ 1\n", sym->name);
  337. break;
  338. }
  339. break;
  340. case S_STRING:
  341. // fix me
  342. str = sym_get_string_value(sym);
  343. fprintf(out, "%s=\"", sym->name);
  344. if (out_h)
  345. fprintf(out_h, "#define __%s__ \"", sym->name);
  346. do {
  347. l = strcspn(str, "\"\\");
  348. if (l) {
  349. fwrite(str, l, 1, out);
  350. if (out_h)
  351. fwrite(str, l, 1, out_h);
  352. }
  353. str += l;
  354. while (*str == '\\' || *str == '"') {
  355. fprintf(out, "\\%c", *str);
  356. if (out_h)
  357. fprintf(out_h, "\\%c", *str);
  358. str++;
  359. }
  360. } while (*str);
  361. fputs("\"\n", out);
  362. if (out_h)
  363. fputs("\"\n", out_h);
  364. break;
  365. case S_HEX:
  366. str = sym_get_string_value(sym);
  367. if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
  368. fprintf(out, "%s=%s\n", sym->name, str);
  369. if (out_h)
  370. fprintf(out_h, "#define __%s__ 0x%s\n", sym->name, str);
  371. break;
  372. }
  373. case S_INT:
  374. str = sym_get_string_value(sym);
  375. fprintf(out, "%s=%s\n", sym->name, str);
  376. if (out_h)
  377. fprintf(out_h, "#define __%s__ %s\n", sym->name, str);
  378. break;
  379. }
  380. }
  381. next:
  382. if (menu->list) {
  383. menu = menu->list;
  384. continue;
  385. }
  386. if (menu->next)
  387. menu = menu->next;
  388. else while ((menu = menu->parent)) {
  389. if (menu->next) {
  390. menu = menu->next;
  391. break;
  392. }
  393. }
  394. }
  395. fclose(out);
  396. if (out_h) {
  397. fclose(out_h);
  398. rename(".tmpconfig.h", "include/bits/uClibc_config.h");
  399. file_write_dep(NULL);
  400. }
  401. if (!name || basename != conf_def_filename) {
  402. if (!name)
  403. name = conf_def_filename;
  404. sprintf(tmpname, "%s.old", name);
  405. rename(name, tmpname);
  406. }
  407. sprintf(tmpname, "%s%s", dirname, basename);
  408. if (rename(newname, tmpname))
  409. return 1;
  410. sym_change_count = 0;
  411. return 0;
  412. }