confdata.c 9.6 KB

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