blob: 5c95291013af6882867156e5193158123992c2fb [file] [log] [blame]
Eric Andersenc9f20d92002-12-05 08:41:41 +00001/*
2 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3 * Released under the terms of the GNU GPL v2.0.
Eric Andersenc9f20d92002-12-05 08:41:41 +00004 */
5
Eric Andersen72d8e442003-08-05 02:18:25 +00006#include <sys/stat.h>
Eric Andersenc9f20d92002-12-05 08:41:41 +00007#include <ctype.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11#include <unistd.h>
12
13#define LKC_DIRECT_LINK
14#include "lkc.h"
15
16const char conf_def_filename[] = ".config";
17
Eric Andersenfa7532b2003-06-12 22:09:08 +000018const char conf_defname[] = "sysdeps/linux/defconfig";
Eric Andersenc9f20d92002-12-05 08:41:41 +000019
20const char *conf_confnames[] = {
21 ".config",
22 conf_defname,
23 NULL,
24};
25
Rob Landley9a1e25d2005-12-19 16:30:33 +000026static char *conf_expand_value(const char *in)
Eric Andersenc9f20d92002-12-05 08:41:41 +000027{
28 struct symbol *sym;
Rob Landley9a1e25d2005-12-19 16:30:33 +000029 const char *src;
Eric Andersenc9f20d92002-12-05 08:41:41 +000030 static char res_value[SYMBOL_MAXLENGTH];
31 char *dst, name[SYMBOL_MAXLENGTH];
32
33 res_value[0] = 0;
34 dst = name;
35 while ((src = strchr(in, '$'))) {
36 strncat(res_value, in, src - in);
37 src++;
38 dst = name;
Eric Andersen72d8e442003-08-05 02:18:25 +000039 while (isalnum(*src) || *src == '_')
Eric Andersenc9f20d92002-12-05 08:41:41 +000040 *dst++ = *src++;
41 *dst = 0;
42 sym = sym_lookup(name, 0);
43 sym_calc_value(sym);
44 strcat(res_value, sym_get_string_value(sym));
45 in = src;
46 }
47 strcat(res_value, in);
48
49 return res_value;
50}
51
52char *conf_get_default_confname(void)
53{
Eric Andersen72d8e442003-08-05 02:18:25 +000054 struct stat buf;
Eric Andersen16f94a62004-01-16 12:49:06 +000055 static char fullname[PATH_MAX+1];
Eric Andersen72d8e442003-08-05 02:18:25 +000056 char *env, *name;
57
58 name = conf_expand_value(conf_defname);
59 env = getenv(SRCTREE);
60 if (env) {
61 sprintf(fullname, "%s/%s", env, name);
62 if (!stat(fullname, &buf))
63 return fullname;
64 }
65 return name;
Eric Andersenc9f20d92002-12-05 08:41:41 +000066}
67
68int conf_read(const char *name)
69{
70 FILE *in = NULL;
71 char line[1024];
72 char *p, *p2;
73 int lineno = 0;
74 struct symbol *sym;
75 struct property *prop;
76 struct expr *e;
77 int i;
78
79 if (name) {
Eric Andersen72d8e442003-08-05 02:18:25 +000080 in = zconf_fopen(name);
Eric Andersenc9f20d92002-12-05 08:41:41 +000081 } else {
82 const char **names = conf_confnames;
83 while ((name = *names++)) {
84 name = conf_expand_value(name);
Eric Andersen72d8e442003-08-05 02:18:25 +000085 in = zconf_fopen(name);
Eric Andersenc9f20d92002-12-05 08:41:41 +000086 if (in) {
87 printf("#\n"
88 "# using defaults found in %s\n"
89 "#\n", name);
90 break;
91 }
92 }
93 }
94
95 if (!in)
96 return 1;
97
98 for_all_symbols(i, sym) {
99 sym->flags |= SYMBOL_NEW | SYMBOL_CHANGED;
100 sym->flags &= ~SYMBOL_VALID;
101 switch (sym->type) {
102 case S_INT:
103 case S_HEX:
104 case S_STRING:
Eric Andersen72d8e442003-08-05 02:18:25 +0000105 if (sym->user.val)
106 free(sym->user.val);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000107 default:
Eric Andersen72d8e442003-08-05 02:18:25 +0000108 sym->user.val = NULL;
109 sym->user.tri = no;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000110 }
111 }
112
113 while (fgets(line, sizeof(line), in)) {
114 lineno++;
Eric Andersen72d8e442003-08-05 02:18:25 +0000115 sym = NULL;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000116 switch (line[0]) {
Eric Andersenc9f20d92002-12-05 08:41:41 +0000117 case '#':
Eric Andersen72d8e442003-08-05 02:18:25 +0000118 if (line[1]!=' ')
Eric Andersenc9f20d92002-12-05 08:41:41 +0000119 continue;
Eric Andersen72d8e442003-08-05 02:18:25 +0000120 p = strchr(line + 2, ' ');
Eric Andersenc9f20d92002-12-05 08:41:41 +0000121 if (!p)
122 continue;
123 *p++ = 0;
124 if (strncmp(p, "is not set", 10))
125 continue;
Eric Andersen72d8e442003-08-05 02:18:25 +0000126 sym = sym_find(line + 2);
127 if (!sym) {
128 fprintf(stderr, "%s:%d: trying to assign nonexistent symbol %s\n", name, lineno, line + 2);
129 break;
130 }
Eric Andersenc9f20d92002-12-05 08:41:41 +0000131 switch (sym->type) {
132 case S_BOOLEAN:
133 case S_TRISTATE:
Eric Andersen72d8e442003-08-05 02:18:25 +0000134 sym->user.tri = no;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000135 sym->flags &= ~SYMBOL_NEW;
136 break;
137 default:
138 ;
139 }
140 break;
Eric Andersen72d8e442003-08-05 02:18:25 +0000141
Eric Andersenc9f20d92002-12-05 08:41:41 +0000142 case 'A' ... 'Z':
143 p = strchr(line, '=');
144 if (!p)
145 continue;
146 *p++ = 0;
147 p2 = strchr(p, '\n');
148 if (p2)
149 *p2 = 0;
150 sym = sym_find(line);
151 if (!sym) {
152 fprintf(stderr, "%s:%d: trying to assign nonexistent symbol %s\n", name, lineno, line);
153 break;
154 }
155 switch (sym->type) {
Eric Andersen72d8e442003-08-05 02:18:25 +0000156 case S_TRISTATE:
Eric Andersenc9f20d92002-12-05 08:41:41 +0000157 if (p[0] == 'm') {
Eric Andersen72d8e442003-08-05 02:18:25 +0000158 sym->user.tri = mod;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000159 sym->flags &= ~SYMBOL_NEW;
160 break;
161 }
162 case S_BOOLEAN:
163 if (p[0] == 'y') {
Eric Andersen72d8e442003-08-05 02:18:25 +0000164 sym->user.tri = yes;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000165 sym->flags &= ~SYMBOL_NEW;
166 break;
167 }
168 if (p[0] == 'n') {
Eric Andersen72d8e442003-08-05 02:18:25 +0000169 sym->user.tri = no;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000170 sym->flags &= ~SYMBOL_NEW;
171 break;
172 }
Eric Andersen72d8e442003-08-05 02:18:25 +0000173 break;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000174 case S_STRING:
175 if (*p++ != '"')
176 break;
177 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
178 if (*p2 == '"') {
179 *p2 = 0;
180 break;
181 }
182 memmove(p2, p2 + 1, strlen(p2));
183 }
Eric Andersen72d8e442003-08-05 02:18:25 +0000184 if (!p2) {
185 fprintf(stderr, "%s:%d: invalid string found\n", name, lineno);
186 exit(1);
187 }
Eric Andersenc9f20d92002-12-05 08:41:41 +0000188 case S_INT:
189 case S_HEX:
190 if (sym_string_valid(sym, p)) {
Eric Andersen72d8e442003-08-05 02:18:25 +0000191 sym->user.val = strdup(p);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000192 sym->flags &= ~SYMBOL_NEW;
Eric Andersen72d8e442003-08-05 02:18:25 +0000193 } else {
194 fprintf(stderr, "%s:%d: symbol value '%s' invalid for %s\n", name, lineno, p, sym->name);
195 exit(1);
196 }
Eric Andersenc9f20d92002-12-05 08:41:41 +0000197 break;
198 default:
199 ;
200 }
Eric Andersen72d8e442003-08-05 02:18:25 +0000201 break;
202 case '\n':
Eric Andersenc9f20d92002-12-05 08:41:41 +0000203 break;
204 default:
205 continue;
206 }
Eric Andersen72d8e442003-08-05 02:18:25 +0000207 if (sym && sym_is_choice_value(sym)) {
208 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
209 switch (sym->user.tri) {
210 case no:
211 break;
212 case mod:
213 if (cs->user.tri == yes)
214 /* warn? */;
215 break;
216 case yes:
217 if (cs->user.tri != no)
218 /* warn? */;
219 cs->user.val = sym;
220 break;
221 }
222 cs->user.tri = E_OR(cs->user.tri, sym->user.tri);
223 cs->flags &= ~SYMBOL_NEW;
224 }
Eric Andersenc9f20d92002-12-05 08:41:41 +0000225 }
226 fclose(in);
227
Eric Andersen837f0582004-07-15 06:01:05 +0000228 if (modules_sym)
229 sym_calc_value(modules_sym);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000230 for_all_symbols(i, sym) {
Eric Andersen72d8e442003-08-05 02:18:25 +0000231 sym_calc_value(sym);
232 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
233 if (sym->visible == no)
234 sym->flags |= SYMBOL_NEW;
235 switch (sym->type) {
236 case S_STRING:
237 case S_INT:
238 case S_HEX:
239 if (!sym_string_within_range(sym, sym->user.val))
240 sym->flags |= SYMBOL_NEW;
241 default:
242 break;
243 }
244 }
Eric Andersenc9f20d92002-12-05 08:41:41 +0000245 if (!sym_is_choice(sym))
246 continue;
247 prop = sym_get_choice_prop(sym);
Eric Andersen72d8e442003-08-05 02:18:25 +0000248 for (e = prop->expr; e; e = e->left.expr)
249 if (e->right.sym->visible != no)
250 sym->flags |= e->right.sym->flags & SYMBOL_NEW;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000251 }
252
253 sym_change_count = 1;
254
255 return 0;
256}
257
Rob Landley2ee82722005-09-14 14:40:01 +0000258struct menu *next_menu(struct menu *menu)
259{
260 if (menu->list) return menu->list;
261 do {
262 if (menu->next) {
263 menu = menu->next;
264 break;
265 }
266 } while ((menu = menu->parent));
267
268 return menu;
269}
270
271#define SYMBOL_FORCEWRITE (1<<31)
272
Eric Andersenc9f20d92002-12-05 08:41:41 +0000273int conf_write(const char *name)
274{
275 FILE *out, *out_h;
276 struct symbol *sym;
277 struct menu *menu;
Eric Andersen72d8e442003-08-05 02:18:25 +0000278 const char *basename;
279 char dirname[128], tmpname[128], newname[128];
Eric Andersenc9f20d92002-12-05 08:41:41 +0000280 int type, l;
281 const char *str;
282
Eric Andersen72d8e442003-08-05 02:18:25 +0000283 dirname[0] = 0;
284 if (name && name[0]) {
Eric Andersen837f0582004-07-15 06:01:05 +0000285 struct stat st;
286 char *slash;
287
288 if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
289 strcpy(dirname, name);
290 strcat(dirname, "/");
291 basename = conf_def_filename;
292 } else if ((slash = strrchr(name, '/'))) {
Eric Andersen72d8e442003-08-05 02:18:25 +0000293 int size = slash - name + 1;
294 memcpy(dirname, name, size);
295 dirname[size] = 0;
296 if (slash[1])
297 basename = slash + 1;
298 else
299 basename = conf_def_filename;
300 } else
301 basename = name;
302 } else
303 basename = conf_def_filename;
304
Peter Kjellerstedtbae38db2005-04-19 09:55:06 +0000305 sprintf(newname, "%s.tmpconfig.%d", dirname, (int)getpid());
Eric Andersen72d8e442003-08-05 02:18:25 +0000306 out = fopen(newname, "w");
Eric Andersenc9f20d92002-12-05 08:41:41 +0000307 if (!out)
308 return 1;
Eric Andersen72d8e442003-08-05 02:18:25 +0000309 out_h = NULL;
310 if (!name) {
311 out_h = fopen(".tmpconfig.h", "w");
312 if (!out_h)
313 return 1;
314 }
Eric Andersenc9f20d92002-12-05 08:41:41 +0000315 fprintf(out, "#\n"
316 "# Automatically generated make config: don't edit\n"
317 "#\n");
Glenn L McGrath7c87b672004-01-13 10:57:32 +0000318 if (out_h) {
Eric Andersen72d8e442003-08-05 02:18:25 +0000319 fprintf(out_h, "/*\n"
320 " * Automatically generated header file: don't edit\n"
321 " */\n\n"
Eric Andersen72d8e442003-08-05 02:18:25 +0000322 "/* Version Number */\n"
323 "#define BB_VER \"%s\"\n"
Glenn L McGrath7c87b672004-01-13 10:57:32 +0000324 "#define BB_BT \"%s\"\n",
Eric Andersen72d8e442003-08-05 02:18:25 +0000325 getenv("VERSION"),
326 getenv("BUILDTIME"));
Glenn L McGrath7c87b672004-01-13 10:57:32 +0000327 if (getenv("EXTRA_VERSION"))
328 fprintf(out_h, "#define BB_EXTRA_VERSION \"%s\"\n",
329 getenv("EXTRA_VERSION"));
330 fprintf(out_h, "\n");
331 }
Eric Andersenc9f20d92002-12-05 08:41:41 +0000332
333 if (!sym_change_count)
334 sym_clear_all_valid();
335
Rob Landley2ee82722005-09-14 14:40:01 +0000336 /* Force write of all non-duplicate symbols. */
337
338 /* Write out everything by default. */
339 for(menu = rootmenu.list; menu; menu = next_menu(menu))
340 if (menu->sym) menu->sym->flags |= SYMBOL_FORCEWRITE;
341
Eric Andersenc9f20d92002-12-05 08:41:41 +0000342 menu = rootmenu.list;
343 while (menu) {
344 sym = menu->sym;
345 if (!sym) {
Rob Landley2ee82722005-09-14 14:40:01 +0000346 if (!menu_is_visible(menu))
347 goto next;
348 str = menu_get_prompt(menu);
349 fprintf(out, "\n"
350 "#\n"
351 "# %s\n"
352 "#\n", str);
353 if (out_h)
354 fprintf(out_h, "\n"
355 "/*\n"
356 " * %s\n"
357 " */\n", str);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000358 } else if (!(sym->flags & SYMBOL_CHOICE)) {
359 sym_calc_value(sym);
Rob Landley2ee82722005-09-14 14:40:01 +0000360 if (!(sym->flags & SYMBOL_FORCEWRITE))
361 goto next;
362
363 sym->flags &= ~SYMBOL_FORCEWRITE;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000364 type = sym->type;
365 if (type == S_TRISTATE) {
366 sym_calc_value(modules_sym);
Eric Andersen72d8e442003-08-05 02:18:25 +0000367 if (modules_sym->curr.tri == no)
Eric Andersenc9f20d92002-12-05 08:41:41 +0000368 type = S_BOOLEAN;
369 }
370 switch (type) {
371 case S_BOOLEAN:
372 case S_TRISTATE:
373 switch (sym_get_tristate_value(sym)) {
374 case no:
375 fprintf(out, "# %s is not set\n", sym->name);
Eric Andersen72d8e442003-08-05 02:18:25 +0000376 if (out_h)
377 fprintf(out_h, "#undef %s\n", sym->name);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000378 break;
Eric Andersen72d8e442003-08-05 02:18:25 +0000379 case mod:
Glenn L McGrath8ac86e32003-10-03 08:28:59 +0000380#if 0
Eric Andersenc9f20d92002-12-05 08:41:41 +0000381 fprintf(out, "%s=m\n", sym->name);
Eric Andersen72d8e442003-08-05 02:18:25 +0000382 if (out_h)
383 fprintf(out_h, "#define %s_MODULE 1\n", sym->name);
Eric Andersen72d8e442003-08-05 02:18:25 +0000384#endif
Glenn L McGrath8ac86e32003-10-03 08:28:59 +0000385 break;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000386 case yes:
387 fprintf(out, "%s=y\n", sym->name);
Eric Andersen72d8e442003-08-05 02:18:25 +0000388 if (out_h)
389 fprintf(out_h, "#define %s 1\n", sym->name);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000390 break;
391 }
392 break;
393 case S_STRING:
394 // fix me
395 str = sym_get_string_value(sym);
396 fprintf(out, "%s=\"", sym->name);
Eric Andersen72d8e442003-08-05 02:18:25 +0000397 if (out_h)
398 fprintf(out_h, "#define %s \"", sym->name);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000399 do {
400 l = strcspn(str, "\"\\");
401 if (l) {
402 fwrite(str, l, 1, out);
Eric Andersen72d8e442003-08-05 02:18:25 +0000403 if (out_h)
404 fwrite(str, l, 1, out_h);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000405 }
406 str += l;
407 while (*str == '\\' || *str == '"') {
408 fprintf(out, "\\%c", *str);
Eric Andersen72d8e442003-08-05 02:18:25 +0000409 if (out_h)
410 fprintf(out_h, "\\%c", *str);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000411 str++;
412 }
413 } while (*str);
414 fputs("\"\n", out);
Eric Andersen72d8e442003-08-05 02:18:25 +0000415 if (out_h)
416 fputs("\"\n", out_h);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000417 break;
418 case S_HEX:
419 str = sym_get_string_value(sym);
420 if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
Rob Landley8f991042005-09-05 11:04:30 +0000421 fprintf(out, "%s=%s\n", sym->name, *str ? str : "0");
Eric Andersen72d8e442003-08-05 02:18:25 +0000422 if (out_h)
423 fprintf(out_h, "#define %s 0x%s\n", sym->name, str);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000424 break;
425 }
426 case S_INT:
427 str = sym_get_string_value(sym);
Rob Landley8f991042005-09-05 11:04:30 +0000428 fprintf(out, "%s=%s\n", sym->name, *str ? str : "0");
Eric Andersen72d8e442003-08-05 02:18:25 +0000429 if (out_h)
430 fprintf(out_h, "#define %s %s\n", sym->name, str);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000431 break;
432 }
433 }
Rob Landley2ee82722005-09-14 14:40:01 +0000434next:
435 menu = next_menu(menu);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000436 }
437 fclose(out);
Eric Andersen72d8e442003-08-05 02:18:25 +0000438 if (out_h) {
439 fclose(out_h);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000440 rename(".tmpconfig.h", "include/config.h");
Eric Andersenc9f20d92002-12-05 08:41:41 +0000441 file_write_dep(NULL);
Eric Andersen72d8e442003-08-05 02:18:25 +0000442 }
443 if (!name || basename != conf_def_filename) {
444 if (!name)
445 name = conf_def_filename;
446 sprintf(tmpname, "%s.old", name);
447 rename(name, tmpname);
448 }
449 sprintf(tmpname, "%s%s", dirname, basename);
450 if (rename(newname, tmpname))
Eric Andersenc9f20d92002-12-05 08:41:41 +0000451 return 1;
452
453 sym_change_count = 0;
454
455 return 0;
456}