confdata.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 signed char *in)
  21. {
  22. struct symbol *sym;
  23. const signed 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, (int)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\n"
  287. "/*\n"
  288. " * Version Number\n"
  289. " */\n"
  290. "#define __UCLIBC_MAJOR__ %s\n"
  291. "#define __UCLIBC_MINOR__ %s\n"
  292. "#define __UCLIBC_SUBLEVEL__ %s\n",
  293. getenv("MAJOR_VERSION"),
  294. getenv("MINOR_VERSION"),
  295. getenv("SUBLEVEL"));
  296. if (!sym_change_count)
  297. sym_clear_all_valid();
  298. menu = rootmenu.list;
  299. while (menu) {
  300. sym = menu->sym;
  301. if (!sym) {
  302. if (!menu_is_visible(menu))
  303. goto next;
  304. str = menu_get_prompt(menu);
  305. fprintf(out, "\n"
  306. "#\n"
  307. "# %s\n"
  308. "#\n", str);
  309. if (out_h)
  310. fprintf(out_h, "\n"
  311. "/*\n"
  312. " * %s\n"
  313. " */\n", str);
  314. } else if (!(sym->flags & SYMBOL_CHOICE)) {
  315. sym_calc_value(sym);
  316. if (!(sym->flags & SYMBOL_WRITE))
  317. goto next;
  318. sym->flags &= ~SYMBOL_WRITE;
  319. type = sym->type;
  320. if (type == S_TRISTATE) {
  321. sym_calc_value(modules_sym);
  322. if (modules_sym->curr.tri == no)
  323. type = S_BOOLEAN;
  324. }
  325. switch (type) {
  326. case S_BOOLEAN:
  327. case S_TRISTATE:
  328. switch (sym_get_tristate_value(sym)) {
  329. case no:
  330. fprintf(out, "# %s is not set\n", sym->name);
  331. if (out_h)
  332. fprintf(out_h, "#undef __%s__\n", sym->name);
  333. break;
  334. case mod:
  335. fprintf(out, "%s=m\n", sym->name);
  336. if (out_h)
  337. fprintf(out_h, "#define %s_MODULE 1\n", sym->name);
  338. break;
  339. case yes:
  340. fprintf(out, "%s=y\n", sym->name);
  341. if (out_h)
  342. fprintf(out_h, "#define __%s__ 1\n", sym->name);
  343. break;
  344. }
  345. break;
  346. case S_STRING:
  347. // fix me
  348. str = sym_get_string_value(sym);
  349. fprintf(out, "%s=\"", sym->name);
  350. if (out_h)
  351. fprintf(out_h, "#define __%s__ \"", sym->name);
  352. do {
  353. l = strcspn(str, "\"\\");
  354. if (l) {
  355. fwrite(str, l, 1, out);
  356. if (out_h)
  357. fwrite(str, l, 1, out_h);
  358. }
  359. str += l;
  360. while (*str == '\\' || *str == '"') {
  361. fprintf(out, "\\%c", *str);
  362. if (out_h)
  363. fprintf(out_h, "\\%c", *str);
  364. str++;
  365. }
  366. } while (*str);
  367. fputs("\"\n", out);
  368. if (out_h)
  369. fputs("\"\n", out_h);
  370. break;
  371. case S_HEX:
  372. str = sym_get_string_value(sym);
  373. if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
  374. fprintf(out, "%s=%s\n", sym->name, str);
  375. if (out_h)
  376. fprintf(out_h, "#define __%s__ 0x%s\n", sym->name, str);
  377. break;
  378. }
  379. case S_INT:
  380. str = sym_get_string_value(sym);
  381. fprintf(out, "%s=%s\n", sym->name, str);
  382. if (out_h)
  383. fprintf(out_h, "#define __%s__ %s\n", sym->name, str);
  384. break;
  385. }
  386. }
  387. next:
  388. if (menu->list) {
  389. menu = menu->list;
  390. continue;
  391. }
  392. if (menu->next)
  393. menu = menu->next;
  394. else while ((menu = menu->parent)) {
  395. if (menu->next) {
  396. menu = menu->next;
  397. break;
  398. }
  399. }
  400. }
  401. fclose(out);
  402. if (out_h) {
  403. fclose(out_h);
  404. rename(".tmpconfig.h", "include/bits/uClibc_config.h");
  405. file_write_dep(NULL);
  406. }
  407. if (!name || basename != conf_def_filename) {
  408. if (!name)
  409. name = conf_def_filename;
  410. sprintf(tmpname, "%s.old", name);
  411. rename(name, tmpname);
  412. }
  413. sprintf(tmpname, "%s%s", dirname, basename);
  414. if (rename(newname, tmpname))
  415. return 1;
  416. sym_change_count = 0;
  417. return 0;
  418. }