blob: 30517020d925aa6cdc7e66a5061fa908719ef978 [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
Rob Landley7a43bd02006-01-20 17:47:09 +000018const char conf_defname[] = "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:
Rob Landleye7c43b62006-03-01 16:39:45 +0000105 free(sym->user.val);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000106 default:
Eric Andersen72d8e442003-08-05 02:18:25 +0000107 sym->user.val = NULL;
108 sym->user.tri = no;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000109 }
110 }
111
112 while (fgets(line, sizeof(line), in)) {
113 lineno++;
Eric Andersen72d8e442003-08-05 02:18:25 +0000114 sym = NULL;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000115 switch (line[0]) {
Eric Andersenc9f20d92002-12-05 08:41:41 +0000116 case '#':
Eric Andersen72d8e442003-08-05 02:18:25 +0000117 if (line[1]!=' ')
Eric Andersenc9f20d92002-12-05 08:41:41 +0000118 continue;
Eric Andersen72d8e442003-08-05 02:18:25 +0000119 p = strchr(line + 2, ' ');
Eric Andersenc9f20d92002-12-05 08:41:41 +0000120 if (!p)
121 continue;
122 *p++ = 0;
123 if (strncmp(p, "is not set", 10))
124 continue;
Eric Andersen72d8e442003-08-05 02:18:25 +0000125 sym = sym_find(line + 2);
126 if (!sym) {
127 fprintf(stderr, "%s:%d: trying to assign nonexistent symbol %s\n", name, lineno, line + 2);
128 break;
129 }
Eric Andersenc9f20d92002-12-05 08:41:41 +0000130 switch (sym->type) {
131 case S_BOOLEAN:
132 case S_TRISTATE:
Eric Andersen72d8e442003-08-05 02:18:25 +0000133 sym->user.tri = no;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000134 sym->flags &= ~SYMBOL_NEW;
135 break;
136 default:
137 ;
138 }
139 break;
Eric Andersen72d8e442003-08-05 02:18:25 +0000140
Eric Andersenc9f20d92002-12-05 08:41:41 +0000141 case 'A' ... 'Z':
142 p = strchr(line, '=');
143 if (!p)
144 continue;
145 *p++ = 0;
146 p2 = strchr(p, '\n');
147 if (p2)
148 *p2 = 0;
149 sym = sym_find(line);
150 if (!sym) {
151 fprintf(stderr, "%s:%d: trying to assign nonexistent symbol %s\n", name, lineno, line);
152 break;
153 }
154 switch (sym->type) {
Eric Andersen72d8e442003-08-05 02:18:25 +0000155 case S_TRISTATE:
Eric Andersenc9f20d92002-12-05 08:41:41 +0000156 if (p[0] == 'm') {
Eric Andersen72d8e442003-08-05 02:18:25 +0000157 sym->user.tri = mod;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000158 sym->flags &= ~SYMBOL_NEW;
159 break;
160 }
161 case S_BOOLEAN:
162 if (p[0] == 'y') {
Eric Andersen72d8e442003-08-05 02:18:25 +0000163 sym->user.tri = yes;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000164 sym->flags &= ~SYMBOL_NEW;
165 break;
166 }
167 if (p[0] == 'n') {
Eric Andersen72d8e442003-08-05 02:18:25 +0000168 sym->user.tri = no;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000169 sym->flags &= ~SYMBOL_NEW;
170 break;
171 }
Eric Andersen72d8e442003-08-05 02:18:25 +0000172 break;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000173 case S_STRING:
174 if (*p++ != '"')
175 break;
176 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
177 if (*p2 == '"') {
178 *p2 = 0;
179 break;
180 }
181 memmove(p2, p2 + 1, strlen(p2));
182 }
Eric Andersen72d8e442003-08-05 02:18:25 +0000183 if (!p2) {
184 fprintf(stderr, "%s:%d: invalid string found\n", name, lineno);
185 exit(1);
186 }
Eric Andersenc9f20d92002-12-05 08:41:41 +0000187 case S_INT:
188 case S_HEX:
189 if (sym_string_valid(sym, p)) {
Eric Andersen72d8e442003-08-05 02:18:25 +0000190 sym->user.val = strdup(p);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000191 sym->flags &= ~SYMBOL_NEW;
Eric Andersen72d8e442003-08-05 02:18:25 +0000192 } else {
193 fprintf(stderr, "%s:%d: symbol value '%s' invalid for %s\n", name, lineno, p, sym->name);
194 exit(1);
195 }
Eric Andersenc9f20d92002-12-05 08:41:41 +0000196 break;
197 default:
198 ;
199 }
Eric Andersen72d8e442003-08-05 02:18:25 +0000200 break;
201 case '\n':
Eric Andersenc9f20d92002-12-05 08:41:41 +0000202 break;
203 default:
204 continue;
205 }
Eric Andersen72d8e442003-08-05 02:18:25 +0000206 if (sym && sym_is_choice_value(sym)) {
207 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
208 switch (sym->user.tri) {
209 case no:
210 break;
211 case mod:
212 if (cs->user.tri == yes)
213 /* warn? */;
214 break;
215 case yes:
216 if (cs->user.tri != no)
217 /* warn? */;
218 cs->user.val = sym;
219 break;
220 }
221 cs->user.tri = E_OR(cs->user.tri, sym->user.tri);
222 cs->flags &= ~SYMBOL_NEW;
223 }
Eric Andersenc9f20d92002-12-05 08:41:41 +0000224 }
225 fclose(in);
226
Eric Andersen837f0582004-07-15 06:01:05 +0000227 if (modules_sym)
228 sym_calc_value(modules_sym);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000229 for_all_symbols(i, sym) {
Eric Andersen72d8e442003-08-05 02:18:25 +0000230 sym_calc_value(sym);
231 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
232 if (sym->visible == no)
233 sym->flags |= SYMBOL_NEW;
234 switch (sym->type) {
235 case S_STRING:
236 case S_INT:
237 case S_HEX:
238 if (!sym_string_within_range(sym, sym->user.val))
239 sym->flags |= SYMBOL_NEW;
240 default:
241 break;
242 }
243 }
Eric Andersenc9f20d92002-12-05 08:41:41 +0000244 if (!sym_is_choice(sym))
245 continue;
246 prop = sym_get_choice_prop(sym);
Eric Andersen72d8e442003-08-05 02:18:25 +0000247 for (e = prop->expr; e; e = e->left.expr)
248 if (e->right.sym->visible != no)
249 sym->flags |= e->right.sym->flags & SYMBOL_NEW;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000250 }
251
252 sym_change_count = 1;
253
254 return 0;
255}
256
Rob Landley2ee82722005-09-14 14:40:01 +0000257struct menu *next_menu(struct menu *menu)
258{
259 if (menu->list) return menu->list;
260 do {
261 if (menu->next) {
262 menu = menu->next;
263 break;
264 }
265 } while ((menu = menu->parent));
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000266
Rob Landley2ee82722005-09-14 14:40:01 +0000267 return menu;
268}
269
270#define SYMBOL_FORCEWRITE (1<<31)
271
Eric Andersenc9f20d92002-12-05 08:41:41 +0000272int conf_write(const char *name)
273{
274 FILE *out, *out_h;
275 struct symbol *sym;
276 struct menu *menu;
Eric Andersen72d8e442003-08-05 02:18:25 +0000277 const char *basename;
278 char dirname[128], tmpname[128], newname[128];
Eric Andersenc9f20d92002-12-05 08:41:41 +0000279 int type, l;
280 const char *str;
"Vladimir N. Oleynik"cb9b1142006-02-16 14:28:17 +0000281
282 /* busybox`s code */
"Vladimir N. Oleynik"6732af22006-02-15 12:29:37 +0000283 const char *opt_name;
"Vladimir N. Oleynik"cb9b1142006-02-16 14:28:17 +0000284 int use_flg;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000285
Eric Andersen72d8e442003-08-05 02:18:25 +0000286 dirname[0] = 0;
287 if (name && name[0]) {
Eric Andersen837f0582004-07-15 06:01:05 +0000288 struct stat st;
289 char *slash;
290
291 if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
292 strcpy(dirname, name);
293 strcat(dirname, "/");
294 basename = conf_def_filename;
295 } else if ((slash = strrchr(name, '/'))) {
Eric Andersen72d8e442003-08-05 02:18:25 +0000296 int size = slash - name + 1;
297 memcpy(dirname, name, size);
298 dirname[size] = 0;
299 if (slash[1])
300 basename = slash + 1;
301 else
302 basename = conf_def_filename;
303 } else
304 basename = name;
305 } else
306 basename = conf_def_filename;
307
Peter Kjellerstedtbae38db2005-04-19 09:55:06 +0000308 sprintf(newname, "%s.tmpconfig.%d", dirname, (int)getpid());
Eric Andersen72d8e442003-08-05 02:18:25 +0000309 out = fopen(newname, "w");
Eric Andersenc9f20d92002-12-05 08:41:41 +0000310 if (!out)
311 return 1;
Eric Andersen72d8e442003-08-05 02:18:25 +0000312 out_h = NULL;
313 if (!name) {
314 out_h = fopen(".tmpconfig.h", "w");
315 if (!out_h)
316 return 1;
317 }
Eric Andersenc9f20d92002-12-05 08:41:41 +0000318 fprintf(out, "#\n"
319 "# Automatically generated make config: don't edit\n"
320 "#\n");
"Vladimir N. Oleynik"cb9b1142006-02-16 14:28:17 +0000321
322 /* busybox`s code */
Glenn L McGrath7c87b672004-01-13 10:57:32 +0000323 if (out_h) {
"Vladimir N. Oleynik"6732af22006-02-15 12:29:37 +0000324 fprintf(out_h, "#ifndef BB_CONFIG_H\n#define BB_CONFIG_H\n");
Eric Andersen72d8e442003-08-05 02:18:25 +0000325 fprintf(out_h, "/*\n"
326 " * Automatically generated header file: don't edit\n"
327 " */\n\n"
Eric Andersen72d8e442003-08-05 02:18:25 +0000328 "/* Version Number */\n"
329 "#define BB_VER \"%s\"\n"
Glenn L McGrath7c87b672004-01-13 10:57:32 +0000330 "#define BB_BT \"%s\"\n",
Eric Andersen72d8e442003-08-05 02:18:25 +0000331 getenv("VERSION"),
332 getenv("BUILDTIME"));
Glenn L McGrath7c87b672004-01-13 10:57:32 +0000333 if (getenv("EXTRA_VERSION"))
334 fprintf(out_h, "#define BB_EXTRA_VERSION \"%s\"\n",
335 getenv("EXTRA_VERSION"));
336 fprintf(out_h, "\n");
337 }
"Vladimir N. Oleynik"cb9b1142006-02-16 14:28:17 +0000338 /* end busybox`s code */
Eric Andersenc9f20d92002-12-05 08:41:41 +0000339
340 if (!sym_change_count)
341 sym_clear_all_valid();
342
Rob Landley2ee82722005-09-14 14:40:01 +0000343 /* Force write of all non-duplicate symbols. */
344
345 /* Write out everything by default. */
346 for(menu = rootmenu.list; menu; menu = next_menu(menu))
347 if (menu->sym) menu->sym->flags |= SYMBOL_FORCEWRITE;
348
Eric Andersenc9f20d92002-12-05 08:41:41 +0000349 menu = rootmenu.list;
350 while (menu) {
351 sym = menu->sym;
352 if (!sym) {
Rob Landley2ee82722005-09-14 14:40:01 +0000353 if (!menu_is_visible(menu))
354 goto next;
355 str = menu_get_prompt(menu);
356 fprintf(out, "\n"
357 "#\n"
358 "# %s\n"
359 "#\n", str);
360 if (out_h)
361 fprintf(out_h, "\n"
362 "/*\n"
363 " * %s\n"
364 " */\n", str);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000365 } else if (!(sym->flags & SYMBOL_CHOICE)) {
366 sym_calc_value(sym);
Rob Landley2ee82722005-09-14 14:40:01 +0000367 if (!(sym->flags & SYMBOL_FORCEWRITE))
368 goto next;
369
370 sym->flags &= ~SYMBOL_FORCEWRITE;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000371 type = sym->type;
372 if (type == S_TRISTATE) {
373 sym_calc_value(modules_sym);
Eric Andersen72d8e442003-08-05 02:18:25 +0000374 if (modules_sym->curr.tri == no)
Eric Andersenc9f20d92002-12-05 08:41:41 +0000375 type = S_BOOLEAN;
376 }
"Vladimir N. Oleynik"cb9b1142006-02-16 14:28:17 +0000377
378 /* busybox`s code */
"Vladimir N. Oleynik"6732af22006-02-15 12:29:37 +0000379 opt_name = strchr(sym->name, '_');
380 if(opt_name == NULL)
381 opt_name = sym->name;
382 else
383 opt_name++;
"Vladimir N. Oleynik"cb9b1142006-02-16 14:28:17 +0000384 use_flg = 1;
385 /* end busybox`s code */
386
Eric Andersenc9f20d92002-12-05 08:41:41 +0000387 switch (type) {
388 case S_BOOLEAN:
389 case S_TRISTATE:
390 switch (sym_get_tristate_value(sym)) {
391 case no:
392 fprintf(out, "# %s is not set\n", sym->name);
"Vladimir N. Oleynik"cb9b1142006-02-16 14:28:17 +0000393 if (out_h)
Eric Andersen72d8e442003-08-05 02:18:25 +0000394 fprintf(out_h, "#undef %s\n", sym->name);
"Vladimir N. Oleynik"cb9b1142006-02-16 14:28:17 +0000395 use_flg = 0; /* busybox`s code */
Eric Andersenc9f20d92002-12-05 08:41:41 +0000396 break;
Eric Andersen72d8e442003-08-05 02:18:25 +0000397 case mod:
"Vladimir N. Oleynik"cb9b1142006-02-16 14:28:17 +0000398#if 0 /* busybox`s code */
Eric Andersenc9f20d92002-12-05 08:41:41 +0000399 fprintf(out, "%s=m\n", sym->name);
Eric Andersen72d8e442003-08-05 02:18:25 +0000400 if (out_h)
401 fprintf(out_h, "#define %s_MODULE 1\n", sym->name);
"Vladimir N. Oleynik"cb9b1142006-02-16 14:28:17 +0000402#endif /* busybox`s code */
Glenn L McGrath8ac86e32003-10-03 08:28:59 +0000403 break;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000404 case yes:
405 fprintf(out, "%s=y\n", sym->name);
"Vladimir N. Oleynik"cb9b1142006-02-16 14:28:17 +0000406 if (out_h)
Eric Andersen72d8e442003-08-05 02:18:25 +0000407 fprintf(out_h, "#define %s 1\n", sym->name);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000408 break;
409 }
410 break;
411 case S_STRING:
412 // fix me
413 str = sym_get_string_value(sym);
414 fprintf(out, "%s=\"", sym->name);
Eric Andersen72d8e442003-08-05 02:18:25 +0000415 if (out_h)
416 fprintf(out_h, "#define %s \"", sym->name);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000417 do {
418 l = strcspn(str, "\"\\");
419 if (l) {
420 fwrite(str, l, 1, out);
Eric Andersen72d8e442003-08-05 02:18:25 +0000421 if (out_h)
422 fwrite(str, l, 1, out_h);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000423 }
424 str += l;
425 while (*str == '\\' || *str == '"') {
426 fprintf(out, "\\%c", *str);
Eric Andersen72d8e442003-08-05 02:18:25 +0000427 if (out_h)
428 fprintf(out_h, "\\%c", *str);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000429 str++;
430 }
431 } while (*str);
432 fputs("\"\n", out);
"Vladimir N. Oleynik"cb9b1142006-02-16 14:28:17 +0000433 if (out_h)
Eric Andersen72d8e442003-08-05 02:18:25 +0000434 fputs("\"\n", out_h);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000435 break;
436 case S_HEX:
437 str = sym_get_string_value(sym);
438 if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
Rob Landley8f991042005-09-05 11:04:30 +0000439 fprintf(out, "%s=%s\n", sym->name, *str ? str : "0");
"Vladimir N. Oleynik"cb9b1142006-02-16 14:28:17 +0000440 if (out_h)
Eric Andersen72d8e442003-08-05 02:18:25 +0000441 fprintf(out_h, "#define %s 0x%s\n", sym->name, str);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000442 break;
443 }
444 case S_INT:
445 str = sym_get_string_value(sym);
Rob Landley8f991042005-09-05 11:04:30 +0000446 fprintf(out, "%s=%s\n", sym->name, *str ? str : "0");
"Vladimir N. Oleynik"cb9b1142006-02-16 14:28:17 +0000447 if (out_h)
Eric Andersen72d8e442003-08-05 02:18:25 +0000448 fprintf(out_h, "#define %s %s\n", sym->name, str);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000449 break;
450 }
"Vladimir N. Oleynik"cb9b1142006-02-16 14:28:17 +0000451 /* busybox`s code */
452 if (out_h) {
453 fprintf(out_h, "#define ENABLE_%s %d\n", opt_name, use_flg);
454 fprintf(out_h, "#define USE_%s(...)%s\n", opt_name,
455 (use_flg ? " __VA_ARGS__" : ""));
Rob Landley0d8766a2006-02-20 23:05:06 +0000456 fprintf(out_h, "#define SKIP_%s(...)%s\n\n", opt_name,
"Vladimir N. Oleynik"cb9b1142006-02-16 14:28:17 +0000457 (use_flg ? "" : " __VA_ARGS__"));
458 }
459 /* end busybox`s code */
Eric Andersenc9f20d92002-12-05 08:41:41 +0000460 }
Rob Landley2ee82722005-09-14 14:40:01 +0000461next:
462 menu = next_menu(menu);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000463 }
464 fclose(out);
Eric Andersen72d8e442003-08-05 02:18:25 +0000465 if (out_h) {
"Vladimir N. Oleynik"cb9b1142006-02-16 14:28:17 +0000466 fprintf(out_h, "#endif /* BB_CONFIG_H */\n"); /* busybox`s code */
Eric Andersen72d8e442003-08-05 02:18:25 +0000467 fclose(out_h);
"Vladimir N. Oleynik"cb9b1142006-02-16 14:28:17 +0000468 rename(".tmpconfig.h", "include/bb_config.h"); /* busybox`s config name */
Eric Andersenc9f20d92002-12-05 08:41:41 +0000469 file_write_dep(NULL);
Eric Andersen72d8e442003-08-05 02:18:25 +0000470 }
471 if (!name || basename != conf_def_filename) {
472 if (!name)
473 name = conf_def_filename;
474 sprintf(tmpname, "%s.old", name);
475 rename(name, tmpname);
476 }
477 sprintf(tmpname, "%s%s", dirname, basename);
478 if (rename(newname, tmpname))
Eric Andersenc9f20d92002-12-05 08:41:41 +0000479 return 1;
480
481 sym_change_count = 0;
482
483 return 0;
484}