confdata.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 <ctype.h>
  6. #include <limits.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. char conf_filename[PATH_MAX+1];
  15. const char conf_defname[] = "extra/Configs/Config.$TARGET_ARCH.default";
  16. const char *conf_confnames[] = {
  17. ".config",
  18. conf_defname,
  19. NULL,
  20. };
  21. static char *conf_expand_value(const char *in)
  22. {
  23. struct symbol *sym;
  24. const char *src;
  25. static char res_value[SYMBOL_MAXLENGTH];
  26. char *dst, name[SYMBOL_MAXLENGTH];
  27. res_value[0] = 0;
  28. dst = name;
  29. while ((src = strchr(in, '$'))) {
  30. strncat(res_value, in, src - in);
  31. src++;
  32. dst = name;
  33. while (isalnum(*src) || *src == '_')
  34. *dst++ = *src++;
  35. *dst = 0;
  36. sym = sym_lookup(name, 0);
  37. sym_calc_value(sym);
  38. strcat(res_value, sym_get_string_value(sym));
  39. in = src;
  40. }
  41. strcat(res_value, in);
  42. return res_value;
  43. }
  44. char *conf_get_default_confname(void)
  45. {
  46. return conf_expand_value(conf_defname);
  47. }
  48. int conf_read(const char *name)
  49. {
  50. FILE *in = NULL;
  51. char line[128];
  52. char *p, *p2;
  53. int lineno = 0;
  54. struct symbol *sym;
  55. struct property *prop;
  56. struct expr *e;
  57. int i;
  58. if (name) {
  59. in = fopen(name, "r");
  60. if (in)
  61. strcpy(conf_filename, name);
  62. } else {
  63. const char **names = conf_confnames;
  64. while ((name = *names++)) {
  65. name = conf_expand_value(name);
  66. in = fopen(name, "r");
  67. if (in) {
  68. printf("#\n"
  69. "# using defaults found in %s\n"
  70. "#\n", name);
  71. break;
  72. }
  73. }
  74. }
  75. if (!in)
  76. return 1;
  77. for_all_symbols(i, sym) {
  78. sym->flags |= SYMBOL_NEW;
  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. S_VAL(sym->def) = NULL;
  86. }
  87. default:
  88. ;
  89. }
  90. }
  91. while (fgets(line, 128, 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_BOOLEAN:
  135. sym->def = symbol_yes.curr;
  136. sym->flags &= ~SYMBOL_NEW;
  137. break;
  138. case S_TRISTATE:
  139. if (p[0] == 'm')
  140. sym->def = symbol_mod.curr;
  141. else
  142. sym->def = symbol_yes.curr;
  143. sym->flags &= ~SYMBOL_NEW;
  144. break;
  145. case S_STRING:
  146. if (*p++ != '"')
  147. break;
  148. for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
  149. if (*p2 == '"') {
  150. *p2 = 0;
  151. break;
  152. }
  153. memmove(p2, p2 + 1, strlen(p2));
  154. }
  155. case S_INT:
  156. case S_HEX:
  157. if (sym_string_valid(sym, p)) {
  158. S_VAL(sym->def) = strdup(p);
  159. sym->flags &= ~SYMBOL_NEW;
  160. } else
  161. fprintf(stderr, "%s:%d:symbol value '%s' invalid for %s\n", name, lineno, p, sym->name);
  162. break;
  163. default:
  164. ;
  165. }
  166. if (sym_is_choice_value(sym)) {
  167. prop = sym_get_choice_prop(sym);
  168. switch (S_TRI(sym->def)) {
  169. case mod:
  170. if (S_TRI(prop->def->def) == yes)
  171. /* warn? */;
  172. break;
  173. case yes:
  174. if (S_TRI(prop->def->def) != no)
  175. /* warn? */;
  176. S_VAL(prop->def->def) = sym;
  177. break;
  178. case no:
  179. break;
  180. }
  181. S_TRI(prop->def->def) = S_TRI(sym->def);
  182. }
  183. break;
  184. default:
  185. continue;
  186. }
  187. }
  188. fclose(in);
  189. for_all_symbols(i, sym) {
  190. if (!sym_is_choice(sym))
  191. continue;
  192. prop = sym_get_choice_prop(sym);
  193. for (e = prop->dep; e; e = e->left.expr)
  194. sym->flags |= e->right.sym->flags & SYMBOL_NEW;
  195. sym->flags &= ~SYMBOL_NEW;
  196. }
  197. sym_change_count = 1;
  198. return 0;
  199. }
  200. int conf_write(const char *name)
  201. {
  202. FILE *out, *out_h;
  203. struct symbol *sym;
  204. struct menu *menu;
  205. char oldname[128];
  206. int type, l;
  207. const char *str;
  208. out = fopen(".tmpconfig", "w");
  209. if (!out)
  210. return 1;
  211. out_h = fopen(".tmpconfig.h", "w");
  212. if (!out_h)
  213. return 1;
  214. fprintf(out, "#\n"
  215. "# Automatically generated make config: don't edit\n"
  216. "#\n");
  217. fprintf(out_h, "/*\n"
  218. " * Automatically generated C config: don't edit\n"
  219. " */\n"
  220. "#if !defined __FEATURES_H && !defined __need_uClibc_config_h\n"
  221. "#error Never include <bits/uClibc_config.h> directly; use <features.h> instead.\n"
  222. "#endif\n"
  223. "#define AUTOCONF_INCLUDED\n\n"
  224. "/*\n"
  225. " * Version Number\n"
  226. " */\n"
  227. "#define __UCLIBC_MAJOR__ %s\n"
  228. "#define __UCLIBC_MINOR__ %s\n"
  229. "#define __UCLIBC_SUBLEVEL__ %s\n",
  230. getenv("MAJOR_VERSION"),
  231. getenv("MINOR_VERSION"),
  232. getenv("SUBLEVEL")
  233. );
  234. if (!sym_change_count)
  235. sym_clear_all_valid();
  236. menu = rootmenu.list;
  237. while (menu) {
  238. sym = menu->sym;
  239. if (!sym) {
  240. if (!menu_is_visible(menu))
  241. goto next;
  242. str = menu_get_prompt(menu);
  243. fprintf(out, "\n"
  244. "#\n"
  245. "# %s\n"
  246. "#\n", str);
  247. fprintf(out_h, "\n"
  248. "/*\n"
  249. " * %s\n"
  250. " */\n", str);
  251. } else if (!(sym->flags & SYMBOL_CHOICE)) {
  252. sym_calc_value(sym);
  253. if (!(sym->flags & SYMBOL_WRITE))
  254. goto next;
  255. sym->flags &= ~SYMBOL_WRITE;
  256. type = sym->type;
  257. if (type == S_TRISTATE) {
  258. sym_calc_value(modules_sym);
  259. if (S_TRI(modules_sym->curr) == no)
  260. type = S_BOOLEAN;
  261. }
  262. switch (type) {
  263. case S_BOOLEAN:
  264. case S_TRISTATE:
  265. switch (sym_get_tristate_value(sym)) {
  266. case no:
  267. fprintf(out, "# %s is not set\n", sym->name);
  268. fprintf(out_h, "#undef __%s__\n", sym->name);
  269. break;
  270. case mod:
  271. fprintf(out, "%s=m\n", sym->name);
  272. fprintf(out_h, "#define __%s__MODULE 1\n", sym->name);
  273. break;
  274. case yes:
  275. fprintf(out, "%s=y\n", sym->name);
  276. fprintf(out_h, "#define __%s__ 1\n", sym->name);
  277. break;
  278. }
  279. break;
  280. case S_STRING:
  281. // fix me
  282. str = sym_get_string_value(sym);
  283. fprintf(out, "%s=\"", sym->name);
  284. fprintf(out_h, "#define __%s__ \"", sym->name);
  285. do {
  286. l = strcspn(str, "\"\\");
  287. if (l) {
  288. fwrite(str, l, 1, out);
  289. fwrite(str, l, 1, out_h);
  290. }
  291. str += l;
  292. while (*str == '\\' || *str == '"') {
  293. fprintf(out, "\\%c", *str);
  294. fprintf(out_h, "\\%c", *str);
  295. str++;
  296. }
  297. } while (*str);
  298. fputs("\"\n", out);
  299. fputs("\"\n", out_h);
  300. break;
  301. case S_HEX:
  302. str = sym_get_string_value(sym);
  303. if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
  304. fprintf(out, "%s=%s\n", sym->name, str);
  305. fprintf(out_h, "#define __%s__ 0x%s\n", sym->name, str);
  306. break;
  307. }
  308. case S_INT:
  309. str = sym_get_string_value(sym);
  310. fprintf(out, "%s=%s\n", sym->name, str);
  311. fprintf(out_h, "#define __%s__ %s\n", sym->name, str);
  312. break;
  313. }
  314. }
  315. next:
  316. if (menu->list) {
  317. menu = menu->list;
  318. continue;
  319. }
  320. if (menu->next)
  321. menu = menu->next;
  322. else while ((menu = menu->parent)) {
  323. if (menu->next) {
  324. menu = menu->next;
  325. break;
  326. }
  327. }
  328. }
  329. fclose(out);
  330. fclose(out_h);
  331. if (!name) {
  332. rename(".tmpconfig.h", "include/bits/uClibc_config.h");
  333. name = conf_def_filename;
  334. file_write_dep(NULL);
  335. } else
  336. unlink(".tmpconfig.h");
  337. sprintf(oldname, "%s.old", name);
  338. rename(name, oldname);
  339. if (rename(".tmpconfig", name))
  340. return 1;
  341. strcpy(conf_filename, name);
  342. sym_change_count = 0;
  343. return 0;
  344. }