1
0

confdata.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. int output_mode = 0;
  14. const char conf_def_filename[] = ".config";
  15. const char conf_defname[] = "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[2048];
  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. dirname[0] = 0;
  249. if (name && name[0]) {
  250. struct stat st;
  251. char *slash;
  252. if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
  253. strcpy(dirname, name);
  254. strcat(dirname, "/");
  255. basename = conf_def_filename;
  256. } else if ((slash = strrchr(name, '/'))) {
  257. int size = slash - name + 1;
  258. memcpy(dirname, name, size);
  259. dirname[size] = 0;
  260. if (slash[1])
  261. basename = slash + 1;
  262. else
  263. basename = conf_def_filename;
  264. } else
  265. basename = name;
  266. } else
  267. basename = conf_def_filename;
  268. sprintf(newname, "%s.tmpconfig.%d", dirname, getpid());
  269. out = fopen(newname, "w");
  270. if (!out)
  271. return 1;
  272. out_h = NULL;
  273. if (!name) {
  274. out_h = fopen(".tmpconfig.h", "w");
  275. if (!out_h)
  276. return 1;
  277. }
  278. fprintf(out, "#\n"
  279. "# Automatically generated make config: don't edit\n"
  280. "#\n");
  281. if (out_h) {
  282. fprintf(out_h, "/*\n"
  283. " * Automatically generated header file: don't edit\n"
  284. " */\n\n"
  285. "#define AUTOCONF_INCLUDED\n\n"
  286. "/* Version Number */\n"
  287. "#define BB_VER \"%s\"\n"
  288. "#define BB_BT \"%s\"\n",
  289. getenv("VERSION"),
  290. getenv("BUILDTIME"));
  291. if (getenv("EXTRA_VERSION"))
  292. fprintf(out_h, "#define BB_EXTRA_VERSION \"%s\"\n",
  293. getenv("EXTRA_VERSION"));
  294. fprintf(out_h, "\n");
  295. }
  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) && !output_mode)
  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 0
  337. if (out_h)
  338. fprintf(out_h, "#define %s_MODULE 1\n", sym->name);
  339. #endif
  340. break;
  341. case yes:
  342. fprintf(out, "%s=y\n", sym->name);
  343. if (out_h)
  344. fprintf(out_h, "#define %s 1\n", sym->name);
  345. break;
  346. }
  347. break;
  348. case S_STRING:
  349. // fix me
  350. str = sym_get_string_value(sym);
  351. fprintf(out, "%s=\"", sym->name);
  352. if (out_h)
  353. fprintf(out_h, "#define %s \"", sym->name);
  354. do {
  355. l = strcspn(str, "\"\\");
  356. if (l) {
  357. fwrite(str, l, 1, out);
  358. if (out_h)
  359. fwrite(str, l, 1, out_h);
  360. }
  361. str += l;
  362. while (*str == '\\' || *str == '"') {
  363. fprintf(out, "\\%c", *str);
  364. if (out_h)
  365. fprintf(out_h, "\\%c", *str);
  366. str++;
  367. }
  368. } while (*str);
  369. fputs("\"\n", out);
  370. if (out_h)
  371. fputs("\"\n", out_h);
  372. break;
  373. case S_HEX:
  374. str = sym_get_string_value(sym);
  375. if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
  376. fprintf(out, "%s=%s\n", sym->name, str);
  377. if (out_h)
  378. fprintf(out_h, "#define %s 0x%s\n", sym->name, str);
  379. break;
  380. }
  381. case S_INT:
  382. str = sym_get_string_value(sym);
  383. fprintf(out, "%s=%s\n", sym->name, str);
  384. if (out_h)
  385. fprintf(out_h, "#define %s %s\n", sym->name, str);
  386. break;
  387. }
  388. }
  389. next:
  390. if (menu->list) {
  391. menu = menu->list;
  392. continue;
  393. }
  394. if (menu->next)
  395. menu = menu->next;
  396. else while ((menu = menu->parent)) {
  397. if (menu->next) {
  398. menu = menu->next;
  399. break;
  400. }
  401. }
  402. }
  403. fclose(out);
  404. if (out_h) {
  405. fclose(out_h);
  406. rename(".tmpconfig.h", "include/config.h");
  407. file_write_dep(NULL);
  408. }
  409. if (!name || basename != conf_def_filename) {
  410. if (!name)
  411. name = conf_def_filename;
  412. sprintf(tmpname, "%s.old", name);
  413. rename(name, tmpname);
  414. }
  415. sprintf(tmpname, "%s%s", dirname, basename);
  416. if (rename(newname, tmpname))
  417. return 1;
  418. sym_change_count = 0;
  419. return 0;
  420. }