confdata.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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.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[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. 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. if (modules_sym)
  212. sym_calc_value(modules_sym);
  213. for_all_symbols(i, sym) {
  214. sym_calc_value(sym);
  215. if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
  216. if (sym->visible == no)
  217. sym->flags |= SYMBOL_NEW;
  218. switch (sym->type) {
  219. case S_STRING:
  220. case S_INT:
  221. case S_HEX:
  222. if (!sym_string_within_range(sym, sym->user.val))
  223. sym->flags |= SYMBOL_NEW;
  224. default:
  225. break;
  226. }
  227. }
  228. if (!sym_is_choice(sym))
  229. continue;
  230. prop = sym_get_choice_prop(sym);
  231. for (e = prop->expr; e; e = e->left.expr)
  232. if (e->right.sym->visible != no)
  233. sym->flags |= e->right.sym->flags & SYMBOL_NEW;
  234. }
  235. sym_change_count = 1;
  236. return 0;
  237. }
  238. int conf_write(const char *name)
  239. {
  240. FILE *out, *out_h;
  241. struct symbol *sym;
  242. struct menu *menu;
  243. const char *basename;
  244. char dirname[128], tmpname[128], newname[128];
  245. int type, l;
  246. const char *str;
  247. dirname[0] = 0;
  248. if (name && name[0]) {
  249. struct stat st;
  250. char *slash;
  251. if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
  252. strcpy(dirname, name);
  253. strcat(dirname, "/");
  254. basename = conf_def_filename;
  255. } else if ((slash = strrchr(name, '/'))) {
  256. int size = slash - name + 1;
  257. memcpy(dirname, name, size);
  258. dirname[size] = 0;
  259. if (slash[1])
  260. basename = slash + 1;
  261. else
  262. basename = conf_def_filename;
  263. } else
  264. basename = name;
  265. } else
  266. basename = conf_def_filename;
  267. sprintf(newname, "%s.tmpconfig.%d", dirname, getpid());
  268. out = fopen(newname, "w");
  269. if (!out)
  270. return 1;
  271. out_h = NULL;
  272. if (!name) {
  273. out_h = fopen(".tmpconfig.h", "w");
  274. if (!out_h)
  275. return 1;
  276. }
  277. fprintf(out, "#\n"
  278. "# Automatically generated make config: don't edit\n"
  279. "#\n");
  280. if (out_h)
  281. fprintf(out_h, "/*\n"
  282. " * Automatically generated C config: don't edit\n"
  283. " */\n"
  284. "#if !defined __FEATURES_H && !defined __need_uClibc_config_h\n"
  285. "#error Never include <bits/uClibc_config.h> directly; use <features.h> instead\n"
  286. "#endif\n"
  287. "#define AUTOCONF_INCLUDED\n\n"
  288. "/*\n"
  289. " * Version Number\n"
  290. " */\n"
  291. "#define __UCLIBC_MAJOR__ %s\n"
  292. "#define __UCLIBC_MINOR__ %s\n"
  293. "#define __UCLIBC_SUBLEVEL__ %s\n",
  294. getenv("MAJOR_VERSION"),
  295. getenv("MINOR_VERSION"),
  296. getenv("SUBLEVEL"));
  297. if (!sym_change_count)
  298. sym_clear_all_valid();
  299. menu = rootmenu.list;
  300. while (menu) {
  301. sym = menu->sym;
  302. if (!sym) {
  303. if (!menu_is_visible(menu))
  304. goto next;
  305. str = menu_get_prompt(menu);
  306. fprintf(out, "\n"
  307. "#\n"
  308. "# %s\n"
  309. "#\n", str);
  310. if (out_h)
  311. fprintf(out_h, "\n"
  312. "/*\n"
  313. " * %s\n"
  314. " */\n", str);
  315. } else if (!(sym->flags & SYMBOL_CHOICE)) {
  316. sym_calc_value(sym);
  317. if (!(sym->flags & SYMBOL_WRITE))
  318. goto next;
  319. sym->flags &= ~SYMBOL_WRITE;
  320. type = sym->type;
  321. if (type == S_TRISTATE) {
  322. sym_calc_value(modules_sym);
  323. if (modules_sym->curr.tri == no)
  324. type = S_BOOLEAN;
  325. }
  326. switch (type) {
  327. case S_BOOLEAN:
  328. case S_TRISTATE:
  329. switch (sym_get_tristate_value(sym)) {
  330. case no:
  331. fprintf(out, "# %s is not set\n", sym->name);
  332. if (out_h)
  333. fprintf(out_h, "#undef __%s__\n", sym->name);
  334. break;
  335. case mod:
  336. fprintf(out, "%s=m\n", sym->name);
  337. if (out_h)
  338. fprintf(out_h, "#define %s_MODULE 1\n", sym->name);
  339. break;
  340. case yes:
  341. fprintf(out, "%s=y\n", sym->name);
  342. if (out_h)
  343. fprintf(out_h, "#define __%s__ 1\n", sym->name);
  344. break;
  345. }
  346. break;
  347. case S_STRING:
  348. // fix me
  349. str = sym_get_string_value(sym);
  350. fprintf(out, "%s=\"", sym->name);
  351. if (out_h)
  352. fprintf(out_h, "#define __%s__ \"", sym->name);
  353. do {
  354. l = strcspn(str, "\"\\");
  355. if (l) {
  356. fwrite(str, l, 1, out);
  357. if (out_h)
  358. fwrite(str, l, 1, out_h);
  359. }
  360. str += l;
  361. while (*str == '\\' || *str == '"') {
  362. fprintf(out, "\\%c", *str);
  363. if (out_h)
  364. fprintf(out_h, "\\%c", *str);
  365. str++;
  366. }
  367. } while (*str);
  368. fputs("\"\n", out);
  369. if (out_h)
  370. fputs("\"\n", out_h);
  371. break;
  372. case S_HEX:
  373. str = sym_get_string_value(sym);
  374. if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
  375. fprintf(out, "%s=%s\n", sym->name, str);
  376. if (out_h)
  377. fprintf(out_h, "#define __%s__ 0x%s\n", sym->name, str);
  378. break;
  379. }
  380. case S_INT:
  381. str = sym_get_string_value(sym);
  382. fprintf(out, "%s=%s\n", sym->name, str);
  383. if (out_h)
  384. fprintf(out_h, "#define __%s__ %s\n", sym->name, str);
  385. break;
  386. }
  387. }
  388. next:
  389. if (menu->list) {
  390. menu = menu->list;
  391. continue;
  392. }
  393. if (menu->next)
  394. menu = menu->next;
  395. else while ((menu = menu->parent)) {
  396. if (menu->next) {
  397. menu = menu->next;
  398. break;
  399. }
  400. }
  401. }
  402. fclose(out);
  403. if (out_h) {
  404. fclose(out_h);
  405. rename(".tmpconfig.h", "include/bits/uClibc_config.h");
  406. file_write_dep(NULL);
  407. }
  408. if (!name || basename != conf_def_filename) {
  409. if (!name)
  410. name = conf_def_filename;
  411. sprintf(tmpname, "%s.old", name);
  412. rename(name, tmpname);
  413. }
  414. sprintf(tmpname, "%s%s", dirname, basename);
  415. if (rename(newname, tmpname))
  416. return 1;
  417. sym_change_count = 0;
  418. return 0;
  419. }