confdata.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
  3. * Released under the terms of the GNU GPL v2.0.
  4. *
  5. * Allow 'n' as a symbol value.
  6. * 2002-11-05 Petr Baudis <pasky@ucw.cz>
  7. */
  8. #include <ctype.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #define LKC_DIRECT_LINK
  14. #include "lkc.h"
  15. const char conf_def_filename[] = ".config";
  16. const char conf_defname[] = "extra/Configs/Config.$TARGET_ARCH.default";
  17. const char *conf_confnames[] = {
  18. ".config",
  19. conf_defname,
  20. NULL,
  21. };
  22. static char *conf_expand_value(const char *in)
  23. {
  24. struct symbol *sym;
  25. const char *src;
  26. static char res_value[SYMBOL_MAXLENGTH];
  27. char *dst, name[SYMBOL_MAXLENGTH];
  28. res_value[0] = 0;
  29. dst = name;
  30. while ((src = strchr(in, '$'))) {
  31. strncat(res_value, in, src - in);
  32. src++;
  33. dst = name;
  34. while (isalnum((int)*src) || *src == '_')
  35. *dst++ = *src++;
  36. *dst = 0;
  37. sym = sym_lookup(name, 0);
  38. sym_calc_value(sym);
  39. strcat(res_value, sym_get_string_value(sym));
  40. in = src;
  41. }
  42. strcat(res_value, in);
  43. return res_value;
  44. }
  45. char *conf_get_default_confname(void)
  46. {
  47. return conf_expand_value(conf_defname);
  48. }
  49. int conf_read(const char *name)
  50. {
  51. FILE *in = NULL;
  52. char line[1024];
  53. char *p, *p2;
  54. int lineno = 0;
  55. struct symbol *sym;
  56. struct property *prop;
  57. struct expr *e;
  58. int i;
  59. if (name) {
  60. in = fopen(name, "r");
  61. } else {
  62. const char **names = conf_confnames;
  63. while ((name = *names++)) {
  64. name = conf_expand_value(name);
  65. in = fopen(name, "r");
  66. if (in) {
  67. printf("#\n"
  68. "# using defaults found in %s\n"
  69. "#\n", name);
  70. break;
  71. }
  72. }
  73. }
  74. if (!in)
  75. return 1;
  76. for_all_symbols(i, sym) {
  77. sym->flags |= SYMBOL_NEW | SYMBOL_CHANGED;
  78. sym->flags &= ~SYMBOL_VALID;
  79. switch (sym->type) {
  80. case S_INT:
  81. case S_HEX:
  82. case S_STRING:
  83. if (S_VAL(sym->def))
  84. free(S_VAL(sym->def));
  85. default:
  86. S_VAL(sym->def) = NULL;
  87. S_TRI(sym->def) = no;
  88. ;
  89. }
  90. }
  91. while (fgets(line, sizeof(line), in)) {
  92. lineno++;
  93. switch (line[0]) {
  94. case '\n':
  95. break;
  96. case ' ':
  97. break;
  98. case '#':
  99. p = strchr(line, ' ');
  100. if (!p)
  101. continue;
  102. *p++ = 0;
  103. p = strchr(p, ' ');
  104. if (!p)
  105. continue;
  106. *p++ = 0;
  107. if (strncmp(p, "is not set", 10))
  108. continue;
  109. sym = sym_lookup(line+2, 0);
  110. switch (sym->type) {
  111. case S_BOOLEAN:
  112. case S_TRISTATE:
  113. sym->def = symbol_no.curr;
  114. sym->flags &= ~SYMBOL_NEW;
  115. break;
  116. default:
  117. ;
  118. }
  119. break;
  120. case 'A' ... 'Z':
  121. p = strchr(line, '=');
  122. if (!p)
  123. continue;
  124. *p++ = 0;
  125. p2 = strchr(p, '\n');
  126. if (p2)
  127. *p2 = 0;
  128. sym = sym_find(line);
  129. if (!sym) {
  130. fprintf(stderr, "%s:%d: trying to assign nonexistent symbol %s\n", name, lineno, line);
  131. break;
  132. }
  133. switch (sym->type) {
  134. case S_TRISTATE:
  135. if (p[0] == 'm') {
  136. S_TRI(sym->def) = mod;
  137. sym->flags &= ~SYMBOL_NEW;
  138. break;
  139. }
  140. case S_BOOLEAN:
  141. if (p[0] == 'y') {
  142. S_TRI(sym->def) = yes;
  143. sym->flags &= ~SYMBOL_NEW;
  144. break;
  145. }
  146. if (p[0] == 'n') {
  147. S_TRI(sym->def) = no;
  148. sym->flags &= ~SYMBOL_NEW;
  149. break;
  150. }
  151. break;
  152. case S_STRING:
  153. if (*p++ != '"')
  154. break;
  155. for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
  156. if (*p2 == '"') {
  157. *p2 = 0;
  158. break;
  159. }
  160. memmove(p2, p2 + 1, strlen(p2));
  161. }
  162. case S_INT:
  163. case S_HEX:
  164. if (sym_string_valid(sym, p)) {
  165. S_VAL(sym->def) = strdup(p);
  166. sym->flags &= ~SYMBOL_NEW;
  167. } else
  168. fprintf(stderr, "%s:%d:symbol value '%s' invalid for %s\n", name, lineno, p, sym->name);
  169. break;
  170. default:
  171. ;
  172. }
  173. if (sym_is_choice_value(sym)) {
  174. prop = sym_get_choice_prop(sym);
  175. switch (S_TRI(sym->def)) {
  176. case mod:
  177. if (S_TRI(prop->def->def) == yes)
  178. /* warn? */;
  179. break;
  180. case yes:
  181. if (S_TRI(prop->def->def) != no)
  182. /* warn? */;
  183. S_VAL(prop->def->def) = sym;
  184. break;
  185. case no:
  186. break;
  187. }
  188. S_TRI(prop->def->def) = S_TRI(sym->def);
  189. }
  190. break;
  191. default:
  192. continue;
  193. }
  194. }
  195. fclose(in);
  196. for_all_symbols(i, sym) {
  197. if (!sym_is_choice(sym))
  198. continue;
  199. prop = sym_get_choice_prop(sym);
  200. for (e = prop->dep; e; e = e->left.expr)
  201. sym->flags |= e->right.sym->flags & SYMBOL_NEW;
  202. sym->flags &= ~SYMBOL_NEW;
  203. }
  204. sym_change_count = 1;
  205. return 0;
  206. }
  207. int conf_write(const char *name)
  208. {
  209. FILE *out, *out_h;
  210. struct symbol *sym;
  211. struct menu *menu;
  212. char oldname[128];
  213. int type, l;
  214. const char *str;
  215. out = fopen(".tmpconfig", "w");
  216. if (!out)
  217. return 1;
  218. out_h = fopen(".tmpconfig.h", "w");
  219. if (!out_h)
  220. return 1;
  221. fprintf(out, "#\n"
  222. "# Automatically generated make config: don't edit\n"
  223. "#\n");
  224. fprintf(out_h, "/*\n"
  225. " * Automatically generated C config: don't edit\n"
  226. " */\n"
  227. "#if !defined __FEATURES_H && !defined __need_uClibc_config_h\n"
  228. "#error Never include <bits/uClibc_config.h> directly; use <features.h> instead.\n"
  229. "#endif\n"
  230. "#define AUTOCONF_INCLUDED\n\n"
  231. "/*\n"
  232. " * Version Number\n"
  233. " */\n"
  234. "#define __UCLIBC_MAJOR__ %s\n"
  235. "#define __UCLIBC_MINOR__ %s\n"
  236. "#define __UCLIBC_SUBLEVEL__ %s\n",
  237. getenv("MAJOR_VERSION"),
  238. getenv("MINOR_VERSION"),
  239. getenv("SUBLEVEL")
  240. );
  241. if (!sym_change_count)
  242. sym_clear_all_valid();
  243. menu = rootmenu.list;
  244. while (menu) {
  245. sym = menu->sym;
  246. if (!sym) {
  247. if (!menu_is_visible(menu))
  248. goto next;
  249. str = menu_get_prompt(menu);
  250. fprintf(out, "\n"
  251. "#\n"
  252. "# %s\n"
  253. "#\n", str);
  254. fprintf(out_h, "\n"
  255. "/*\n"
  256. " * %s\n"
  257. " */\n", str);
  258. } else if (!(sym->flags & SYMBOL_CHOICE)) {
  259. sym_calc_value(sym);
  260. if (!(sym->flags & SYMBOL_WRITE))
  261. goto next;
  262. sym->flags &= ~SYMBOL_WRITE;
  263. type = sym->type;
  264. if (type == S_TRISTATE) {
  265. sym_calc_value(modules_sym);
  266. if (S_TRI(modules_sym->curr) == no)
  267. type = S_BOOLEAN;
  268. }
  269. switch (type) {
  270. case S_BOOLEAN:
  271. case S_TRISTATE:
  272. switch (sym_get_tristate_value(sym)) {
  273. case no:
  274. fprintf(out, "# %s is not set\n", sym->name);
  275. fprintf(out_h, "#undef __%s__\n", sym->name);
  276. break;
  277. case mod:
  278. fprintf(out, "%s=m\n", sym->name);
  279. fprintf(out_h, "#define __%s__MODULE 1\n", sym->name);
  280. break;
  281. case yes:
  282. fprintf(out, "%s=y\n", sym->name);
  283. fprintf(out_h, "#define __%s__ 1\n", sym->name);
  284. break;
  285. }
  286. break;
  287. case S_STRING:
  288. // fix me
  289. str = sym_get_string_value(sym);
  290. fprintf(out, "%s=\"", sym->name);
  291. fprintf(out_h, "#define __%s__ \"", sym->name);
  292. do {
  293. l = strcspn(str, "\"\\");
  294. if (l) {
  295. fwrite(str, l, 1, out);
  296. fwrite(str, l, 1, out_h);
  297. }
  298. str += l;
  299. while (*str == '\\' || *str == '"') {
  300. fprintf(out, "\\%c", *str);
  301. fprintf(out_h, "\\%c", *str);
  302. str++;
  303. }
  304. } while (*str);
  305. fputs("\"\n", out);
  306. fputs("\"\n", out_h);
  307. break;
  308. case S_HEX:
  309. str = sym_get_string_value(sym);
  310. if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
  311. fprintf(out, "%s=%s\n", sym->name, str);
  312. fprintf(out_h, "#define __%s__ 0x%s\n", sym->name, str);
  313. break;
  314. }
  315. case S_INT:
  316. str = sym_get_string_value(sym);
  317. fprintf(out, "%s=%s\n", sym->name, str);
  318. fprintf(out_h, "#define __%s__ %s\n", sym->name, str);
  319. break;
  320. }
  321. }
  322. next:
  323. if (menu->list) {
  324. menu = menu->list;
  325. continue;
  326. }
  327. if (menu->next)
  328. menu = menu->next;
  329. else while ((menu = menu->parent)) {
  330. if (menu->next) {
  331. menu = menu->next;
  332. break;
  333. }
  334. }
  335. }
  336. fclose(out);
  337. fclose(out_h);
  338. if (!name) {
  339. rename(".tmpconfig.h", "include/bits/uClibc_config.h");
  340. name = conf_def_filename;
  341. file_write_dep(NULL);
  342. } else
  343. unlink(".tmpconfig.h");
  344. sprintf(oldname, "%s.old", name);
  345. rename(name, oldname);
  346. if (rename(".tmpconfig", name))
  347. return 1;
  348. sym_change_count = 0;
  349. return 0;
  350. }