Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org> |
| 3 | * Released under the terms of the GNU GPL v2.0. |
| 4 | */ |
| 5 | |
| 6 | #include <sys/stat.h> |
| 7 | #include <ctype.h> |
Roman Zippel | 2e3646e | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 8 | #include <fcntl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | #include <time.h> |
| 13 | #include <unistd.h> |
| 14 | |
| 15 | #define LKC_DIRECT_LINK |
| 16 | #include "lkc.h" |
| 17 | |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 18 | static void conf_warning(const char *fmt, ...) |
| 19 | __attribute__ ((format (printf, 1, 2))); |
| 20 | |
| 21 | static const char *conf_filename; |
| 22 | static int conf_lineno, conf_warnings, conf_unsaved; |
| 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | const char conf_defname[] = "arch/$ARCH/defconfig"; |
| 25 | |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 26 | static void conf_warning(const char *fmt, ...) |
| 27 | { |
| 28 | va_list ap; |
| 29 | va_start(ap, fmt); |
| 30 | fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno); |
| 31 | vfprintf(stderr, fmt, ap); |
| 32 | fprintf(stderr, "\n"); |
| 33 | va_end(ap); |
| 34 | conf_warnings++; |
| 35 | } |
| 36 | |
Roman Zippel | 14cdd3c | 2006-06-08 22:12:51 -0700 | [diff] [blame^] | 37 | const char *conf_get_configname(void) |
| 38 | { |
| 39 | char *name = getenv("KCONFIG_CONFIG"); |
| 40 | |
| 41 | return name ? name : ".config"; |
| 42 | } |
| 43 | |
J.A. Magallon | 48b9d03 | 2005-06-25 14:59:22 -0700 | [diff] [blame] | 44 | static char *conf_expand_value(const char *in) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | { |
| 46 | struct symbol *sym; |
J.A. Magallon | 48b9d03 | 2005-06-25 14:59:22 -0700 | [diff] [blame] | 47 | const char *src; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | static char res_value[SYMBOL_MAXLENGTH]; |
| 49 | char *dst, name[SYMBOL_MAXLENGTH]; |
| 50 | |
| 51 | res_value[0] = 0; |
| 52 | dst = name; |
| 53 | while ((src = strchr(in, '$'))) { |
| 54 | strncat(res_value, in, src - in); |
| 55 | src++; |
| 56 | dst = name; |
| 57 | while (isalnum(*src) || *src == '_') |
| 58 | *dst++ = *src++; |
| 59 | *dst = 0; |
| 60 | sym = sym_lookup(name, 0); |
| 61 | sym_calc_value(sym); |
| 62 | strcat(res_value, sym_get_string_value(sym)); |
| 63 | in = src; |
| 64 | } |
| 65 | strcat(res_value, in); |
| 66 | |
| 67 | return res_value; |
| 68 | } |
| 69 | |
| 70 | char *conf_get_default_confname(void) |
| 71 | { |
| 72 | struct stat buf; |
| 73 | static char fullname[PATH_MAX+1]; |
| 74 | char *env, *name; |
| 75 | |
| 76 | name = conf_expand_value(conf_defname); |
| 77 | env = getenv(SRCTREE); |
| 78 | if (env) { |
| 79 | sprintf(fullname, "%s/%s", env, name); |
| 80 | if (!stat(fullname, &buf)) |
| 81 | return fullname; |
| 82 | } |
| 83 | return name; |
| 84 | } |
| 85 | |
Roman Zippel | 669bfad | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 86 | int conf_read_simple(const char *name, int def) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | { |
| 88 | FILE *in = NULL; |
| 89 | char line[1024]; |
| 90 | char *p, *p2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | struct symbol *sym; |
Roman Zippel | 669bfad | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 92 | int i, def_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
| 94 | if (name) { |
| 95 | in = zconf_fopen(name); |
| 96 | } else { |
Roman Zippel | face437 | 2006-06-08 22:12:45 -0700 | [diff] [blame] | 97 | struct property *prop; |
| 98 | |
Roman Zippel | 14cdd3c | 2006-06-08 22:12:51 -0700 | [diff] [blame^] | 99 | name = conf_get_configname(); |
Roman Zippel | ddc97ca | 2006-06-08 22:12:38 -0700 | [diff] [blame] | 100 | in = zconf_fopen(name); |
| 101 | if (in) |
| 102 | goto load; |
| 103 | sym_change_count++; |
Roman Zippel | face437 | 2006-06-08 22:12:45 -0700 | [diff] [blame] | 104 | if (!sym_defconfig_list) |
| 105 | return 1; |
| 106 | |
| 107 | for_all_defaults(sym_defconfig_list, prop) { |
| 108 | if (expr_calc_value(prop->visible.expr) == no || |
| 109 | prop->expr->type != E_SYMBOL) |
| 110 | continue; |
| 111 | name = conf_expand_value(prop->expr->left.sym->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | in = zconf_fopen(name); |
| 113 | if (in) { |
Arnaldo Carvalho de Melo | 3b9fa09 | 2005-05-05 15:09:46 -0700 | [diff] [blame] | 114 | printf(_("#\n" |
Roman Zippel | ddc97ca | 2006-06-08 22:12:38 -0700 | [diff] [blame] | 115 | "# using defaults found in %s\n" |
| 116 | "#\n"), name); |
| 117 | goto load; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | if (!in) |
| 122 | return 1; |
| 123 | |
Roman Zippel | ddc97ca | 2006-06-08 22:12:38 -0700 | [diff] [blame] | 124 | load: |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 125 | conf_filename = name; |
| 126 | conf_lineno = 0; |
| 127 | conf_warnings = 0; |
| 128 | conf_unsaved = 0; |
| 129 | |
Roman Zippel | 669bfad | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 130 | def_flags = SYMBOL_DEF << def; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | for_all_symbols(i, sym) { |
Roman Zippel | 669bfad | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 132 | sym->flags |= SYMBOL_CHANGED; |
| 133 | sym->flags &= ~(def_flags|SYMBOL_VALID); |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 134 | if (sym_is_choice(sym)) |
Roman Zippel | 669bfad | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 135 | sym->flags |= def_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | switch (sym->type) { |
| 137 | case S_INT: |
| 138 | case S_HEX: |
| 139 | case S_STRING: |
Roman Zippel | 669bfad | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 140 | if (sym->def[def].val) |
| 141 | free(sym->def[def].val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | default: |
Roman Zippel | 669bfad | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 143 | sym->def[def].val = NULL; |
| 144 | sym->def[def].tri = no; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | while (fgets(line, sizeof(line), in)) { |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 149 | conf_lineno++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | sym = NULL; |
| 151 | switch (line[0]) { |
| 152 | case '#': |
| 153 | if (memcmp(line + 2, "CONFIG_", 7)) |
| 154 | continue; |
| 155 | p = strchr(line + 9, ' '); |
| 156 | if (!p) |
| 157 | continue; |
| 158 | *p++ = 0; |
| 159 | if (strncmp(p, "is not set", 10)) |
| 160 | continue; |
Roman Zippel | 669bfad | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 161 | if (def == S_DEF_USER) { |
| 162 | sym = sym_find(line + 9); |
| 163 | if (!sym) { |
| 164 | conf_warning("trying to assign nonexistent symbol %s", line + 9); |
| 165 | break; |
| 166 | } |
| 167 | } else { |
| 168 | sym = sym_lookup(line + 9, 0); |
| 169 | if (sym->type == S_UNKNOWN) |
| 170 | sym->type = S_BOOLEAN; |
| 171 | } |
| 172 | if (sym->flags & def_flags) { |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 173 | conf_warning("trying to reassign symbol %s", sym->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | break; |
| 175 | } |
| 176 | switch (sym->type) { |
| 177 | case S_BOOLEAN: |
| 178 | case S_TRISTATE: |
Roman Zippel | 669bfad | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 179 | sym->def[def].tri = no; |
| 180 | sym->flags |= def_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | break; |
| 182 | default: |
| 183 | ; |
| 184 | } |
| 185 | break; |
| 186 | case 'C': |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 187 | if (memcmp(line, "CONFIG_", 7)) { |
| 188 | conf_warning("unexpected data"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | continue; |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 190 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | p = strchr(line + 7, '='); |
| 192 | if (!p) |
| 193 | continue; |
| 194 | *p++ = 0; |
| 195 | p2 = strchr(p, '\n'); |
| 196 | if (p2) |
| 197 | *p2 = 0; |
Roman Zippel | 669bfad | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 198 | if (def == S_DEF_USER) { |
| 199 | sym = sym_find(line + 7); |
| 200 | if (!sym) { |
| 201 | conf_warning("trying to assign nonexistent symbol %s", line + 7); |
| 202 | break; |
| 203 | } |
| 204 | } else { |
| 205 | sym = sym_lookup(line + 7, 0); |
| 206 | if (sym->type == S_UNKNOWN) |
| 207 | sym->type = S_OTHER; |
| 208 | } |
| 209 | if (sym->flags & def_flags) { |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 210 | conf_warning("trying to reassign symbol %s", sym->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | break; |
| 212 | } |
| 213 | switch (sym->type) { |
| 214 | case S_TRISTATE: |
| 215 | if (p[0] == 'm') { |
Roman Zippel | 669bfad | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 216 | sym->def[def].tri = mod; |
| 217 | sym->flags |= def_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | break; |
| 219 | } |
| 220 | case S_BOOLEAN: |
| 221 | if (p[0] == 'y') { |
Roman Zippel | 669bfad | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 222 | sym->def[def].tri = yes; |
| 223 | sym->flags |= def_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | break; |
| 225 | } |
| 226 | if (p[0] == 'n') { |
Roman Zippel | 669bfad | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 227 | sym->def[def].tri = no; |
| 228 | sym->flags |= def_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | break; |
| 230 | } |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 231 | conf_warning("symbol value '%s' invalid for %s", p, sym->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | break; |
Roman Zippel | 669bfad | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 233 | case S_OTHER: |
| 234 | if (*p != '"') { |
| 235 | for (p2 = p; *p2 && !isspace(*p2); p2++) |
| 236 | ; |
| 237 | sym->type = S_STRING; |
| 238 | goto done; |
| 239 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | case S_STRING: |
| 241 | if (*p++ != '"') |
| 242 | break; |
| 243 | for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) { |
| 244 | if (*p2 == '"') { |
| 245 | *p2 = 0; |
| 246 | break; |
| 247 | } |
| 248 | memmove(p2, p2 + 1, strlen(p2)); |
| 249 | } |
| 250 | if (!p2) { |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 251 | conf_warning("invalid string found"); |
| 252 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | } |
| 254 | case S_INT: |
| 255 | case S_HEX: |
Roman Zippel | 669bfad | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 256 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | if (sym_string_valid(sym, p)) { |
Roman Zippel | 669bfad | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 258 | sym->def[def].val = strdup(p); |
| 259 | sym->flags |= def_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | } else { |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 261 | conf_warning("symbol value '%s' invalid for %s", p, sym->name); |
| 262 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | } |
| 264 | break; |
| 265 | default: |
| 266 | ; |
| 267 | } |
| 268 | break; |
| 269 | case '\n': |
| 270 | break; |
| 271 | default: |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 272 | conf_warning("unexpected data"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | continue; |
| 274 | } |
| 275 | if (sym && sym_is_choice_value(sym)) { |
| 276 | struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym)); |
Roman Zippel | 669bfad | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 277 | switch (sym->def[def].tri) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | case no: |
| 279 | break; |
| 280 | case mod: |
Roman Zippel | 669bfad | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 281 | if (cs->def[def].tri == yes) { |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 282 | conf_warning("%s creates inconsistent choice state", sym->name); |
Roman Zippel | 669bfad | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 283 | cs->flags &= ~def_flags; |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 284 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | break; |
| 286 | case yes: |
Roman Zippel | 669bfad | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 287 | if (cs->def[def].tri != no) { |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 288 | conf_warning("%s creates inconsistent choice state", sym->name); |
Roman Zippel | 669bfad | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 289 | cs->flags &= ~def_flags; |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 290 | } else |
Roman Zippel | 669bfad | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 291 | cs->def[def].val = sym; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | break; |
| 293 | } |
Roman Zippel | 669bfad | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 294 | cs->def[def].tri = E_OR(cs->def[def].tri, sym->def[def].tri); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | fclose(in); |
| 298 | |
| 299 | if (modules_sym) |
| 300 | sym_calc_value(modules_sym); |
Roman Zippel | 9038916 | 2005-11-08 21:34:49 -0800 | [diff] [blame] | 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | int conf_read(const char *name) |
| 305 | { |
| 306 | struct symbol *sym; |
| 307 | struct property *prop; |
| 308 | struct expr *e; |
Roman Zippel | 669bfad | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 309 | int i, flags; |
Roman Zippel | 9038916 | 2005-11-08 21:34:49 -0800 | [diff] [blame] | 310 | |
Roman Zippel | ddc97ca | 2006-06-08 22:12:38 -0700 | [diff] [blame] | 311 | sym_change_count = 0; |
| 312 | |
Roman Zippel | 669bfad | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 313 | if (conf_read_simple(name, S_DEF_USER)) |
Roman Zippel | 9038916 | 2005-11-08 21:34:49 -0800 | [diff] [blame] | 314 | return 1; |
| 315 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | for_all_symbols(i, sym) { |
| 317 | sym_calc_value(sym); |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 318 | if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO)) |
| 319 | goto sym_ok; |
| 320 | if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) { |
| 321 | /* check that calculated value agrees with saved value */ |
| 322 | switch (sym->type) { |
| 323 | case S_BOOLEAN: |
| 324 | case S_TRISTATE: |
Roman Zippel | 0c1822e | 2006-06-08 22:12:41 -0700 | [diff] [blame] | 325 | if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym)) |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 326 | break; |
| 327 | if (!sym_is_choice(sym)) |
| 328 | goto sym_ok; |
| 329 | default: |
Roman Zippel | 0c1822e | 2006-06-08 22:12:41 -0700 | [diff] [blame] | 330 | if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val)) |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 331 | goto sym_ok; |
| 332 | break; |
| 333 | } |
| 334 | } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE)) |
| 335 | /* no previous value and not saved */ |
| 336 | goto sym_ok; |
| 337 | conf_unsaved++; |
| 338 | /* maybe print value in verbose mode... */ |
| 339 | sym_ok: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | if (sym_has_value(sym) && !sym_is_choice_value(sym)) { |
| 341 | if (sym->visible == no) |
Roman Zippel | 669bfad | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 342 | sym->flags &= ~SYMBOL_DEF_USER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | switch (sym->type) { |
| 344 | case S_STRING: |
| 345 | case S_INT: |
| 346 | case S_HEX: |
Roman Zippel | 669bfad | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 347 | if (!sym_string_within_range(sym, sym->def[S_DEF_USER].val)) |
| 348 | sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | default: |
| 350 | break; |
| 351 | } |
| 352 | } |
| 353 | if (!sym_is_choice(sym)) |
| 354 | continue; |
| 355 | prop = sym_get_choice_prop(sym); |
Roman Zippel | 669bfad | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 356 | flags = sym->flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | for (e = prop->expr; e; e = e->left.expr) |
| 358 | if (e->right.sym->visible != no) |
Roman Zippel | 669bfad | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 359 | flags &= e->right.sym->flags; |
| 360 | sym->flags |= flags & SYMBOL_DEF_USER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Roman Zippel | ddc97ca | 2006-06-08 22:12:38 -0700 | [diff] [blame] | 363 | sym_change_count += conf_warnings || conf_unsaved; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | int conf_write(const char *name) |
| 369 | { |
Roman Zippel | c955cca | 2006-06-08 22:12:39 -0700 | [diff] [blame] | 370 | FILE *out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | struct symbol *sym; |
| 372 | struct menu *menu; |
| 373 | const char *basename; |
| 374 | char dirname[128], tmpname[128], newname[128]; |
| 375 | int type, l; |
| 376 | const char *str; |
| 377 | time_t now; |
| 378 | int use_timestamp = 1; |
| 379 | char *env; |
| 380 | |
| 381 | dirname[0] = 0; |
| 382 | if (name && name[0]) { |
| 383 | struct stat st; |
| 384 | char *slash; |
| 385 | |
| 386 | if (!stat(name, &st) && S_ISDIR(st.st_mode)) { |
| 387 | strcpy(dirname, name); |
| 388 | strcat(dirname, "/"); |
Roman Zippel | 14cdd3c | 2006-06-08 22:12:51 -0700 | [diff] [blame^] | 389 | basename = conf_get_configname(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | } else if ((slash = strrchr(name, '/'))) { |
| 391 | int size = slash - name + 1; |
| 392 | memcpy(dirname, name, size); |
| 393 | dirname[size] = 0; |
| 394 | if (slash[1]) |
| 395 | basename = slash + 1; |
| 396 | else |
Roman Zippel | 14cdd3c | 2006-06-08 22:12:51 -0700 | [diff] [blame^] | 397 | basename = conf_get_configname(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | } else |
| 399 | basename = name; |
| 400 | } else |
Roman Zippel | 14cdd3c | 2006-06-08 22:12:51 -0700 | [diff] [blame^] | 401 | basename = conf_get_configname(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | |
Roman Zippel | 14cdd3c | 2006-06-08 22:12:51 -0700 | [diff] [blame^] | 403 | sprintf(newname, "%s%s", dirname, basename); |
| 404 | env = getenv("KCONFIG_OVERWRITECONFIG"); |
| 405 | if (!env || !*env) { |
| 406 | sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid()); |
| 407 | out = fopen(tmpname, "w"); |
| 408 | } else { |
| 409 | *tmpname = 0; |
| 410 | out = fopen(newname, "w"); |
| 411 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | if (!out) |
| 413 | return 1; |
Roman Zippel | 14cdd3c | 2006-06-08 22:12:51 -0700 | [diff] [blame^] | 414 | |
Sam Ravnborg | 2244cbd | 2006-01-16 12:12:12 +0100 | [diff] [blame] | 415 | sym = sym_lookup("KERNELVERSION", 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | sym_calc_value(sym); |
| 417 | time(&now); |
| 418 | env = getenv("KCONFIG_NOTIMESTAMP"); |
| 419 | if (env && *env) |
| 420 | use_timestamp = 0; |
| 421 | |
Arnaldo Carvalho de Melo | 3b9fa09 | 2005-05-05 15:09:46 -0700 | [diff] [blame] | 422 | fprintf(out, _("#\n" |
| 423 | "# Automatically generated make config: don't edit\n" |
| 424 | "# Linux kernel version: %s\n" |
| 425 | "%s%s" |
| 426 | "#\n"), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | sym_get_string_value(sym), |
| 428 | use_timestamp ? "# " : "", |
| 429 | use_timestamp ? ctime(&now) : ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
| 431 | if (!sym_change_count) |
| 432 | sym_clear_all_valid(); |
| 433 | |
| 434 | menu = rootmenu.list; |
| 435 | while (menu) { |
| 436 | sym = menu->sym; |
| 437 | if (!sym) { |
| 438 | if (!menu_is_visible(menu)) |
| 439 | goto next; |
| 440 | str = menu_get_prompt(menu); |
| 441 | fprintf(out, "\n" |
| 442 | "#\n" |
| 443 | "# %s\n" |
| 444 | "#\n", str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | } else if (!(sym->flags & SYMBOL_CHOICE)) { |
| 446 | sym_calc_value(sym); |
| 447 | if (!(sym->flags & SYMBOL_WRITE)) |
| 448 | goto next; |
| 449 | sym->flags &= ~SYMBOL_WRITE; |
| 450 | type = sym->type; |
| 451 | if (type == S_TRISTATE) { |
| 452 | sym_calc_value(modules_sym); |
| 453 | if (modules_sym->curr.tri == no) |
| 454 | type = S_BOOLEAN; |
| 455 | } |
| 456 | switch (type) { |
| 457 | case S_BOOLEAN: |
| 458 | case S_TRISTATE: |
| 459 | switch (sym_get_tristate_value(sym)) { |
| 460 | case no: |
| 461 | fprintf(out, "# CONFIG_%s is not set\n", sym->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | break; |
| 463 | case mod: |
| 464 | fprintf(out, "CONFIG_%s=m\n", sym->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | break; |
| 466 | case yes: |
| 467 | fprintf(out, "CONFIG_%s=y\n", sym->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | break; |
| 469 | } |
| 470 | break; |
| 471 | case S_STRING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | str = sym_get_string_value(sym); |
| 473 | fprintf(out, "CONFIG_%s=\"", sym->name); |
Roman Zippel | c955cca | 2006-06-08 22:12:39 -0700 | [diff] [blame] | 474 | while (1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | l = strcspn(str, "\"\\"); |
| 476 | if (l) { |
| 477 | fwrite(str, l, 1, out); |
Roman Zippel | c955cca | 2006-06-08 22:12:39 -0700 | [diff] [blame] | 478 | str += l; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | } |
Roman Zippel | c955cca | 2006-06-08 22:12:39 -0700 | [diff] [blame] | 480 | if (!*str) |
| 481 | break; |
| 482 | fprintf(out, "\\%c", *str++); |
| 483 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | fputs("\"\n", out); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | break; |
| 486 | case S_HEX: |
| 487 | str = sym_get_string_value(sym); |
| 488 | if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) { |
| 489 | fprintf(out, "CONFIG_%s=%s\n", sym->name, str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | break; |
| 491 | } |
| 492 | case S_INT: |
| 493 | str = sym_get_string_value(sym); |
| 494 | fprintf(out, "CONFIG_%s=%s\n", sym->name, str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | break; |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | next: |
| 500 | if (menu->list) { |
| 501 | menu = menu->list; |
| 502 | continue; |
| 503 | } |
| 504 | if (menu->next) |
| 505 | menu = menu->next; |
| 506 | else while ((menu = menu->parent)) { |
| 507 | if (menu->next) { |
| 508 | menu = menu->next; |
| 509 | break; |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | fclose(out); |
Roman Zippel | 14cdd3c | 2006-06-08 22:12:51 -0700 | [diff] [blame^] | 514 | |
| 515 | if (*tmpname) { |
| 516 | strcat(dirname, name ? name : conf_get_configname()); |
| 517 | strcat(dirname, ".old"); |
| 518 | rename(newname, dirname); |
| 519 | if (rename(tmpname, newname)) |
| 520 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
Roman Zippel | ddc97ca | 2006-06-08 22:12:38 -0700 | [diff] [blame] | 523 | printf(_("#\n" |
| 524 | "# configuration written to %s\n" |
Roman Zippel | 14cdd3c | 2006-06-08 22:12:51 -0700 | [diff] [blame^] | 525 | "#\n"), newname); |
Roman Zippel | ddc97ca | 2006-06-08 22:12:38 -0700 | [diff] [blame] | 526 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | sym_change_count = 0; |
| 528 | |
| 529 | return 0; |
| 530 | } |
Roman Zippel | c955cca | 2006-06-08 22:12:39 -0700 | [diff] [blame] | 531 | |
Roman Zippel | 2e3646e | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 532 | int conf_split_config(void) |
| 533 | { |
| 534 | char *name, path[128]; |
| 535 | char *s, *d, c; |
| 536 | struct symbol *sym; |
| 537 | struct stat sb; |
| 538 | int res, i, fd; |
| 539 | |
| 540 | name = getenv("KCONFIG_AUTOCONFIG"); |
| 541 | if (!name) |
| 542 | name = "include/config/auto.conf"; |
| 543 | conf_read_simple(name, S_DEF_AUTO); |
| 544 | |
| 545 | if (chdir("include/config")) |
| 546 | return 1; |
| 547 | |
| 548 | res = 0; |
| 549 | for_all_symbols(i, sym) { |
| 550 | sym_calc_value(sym); |
| 551 | if ((sym->flags & SYMBOL_AUTO) || !sym->name) |
| 552 | continue; |
| 553 | if (sym->flags & SYMBOL_WRITE) { |
| 554 | if (sym->flags & SYMBOL_DEF_AUTO) { |
| 555 | /* |
| 556 | * symbol has old and new value, |
| 557 | * so compare them... |
| 558 | */ |
| 559 | switch (sym->type) { |
| 560 | case S_BOOLEAN: |
| 561 | case S_TRISTATE: |
| 562 | if (sym_get_tristate_value(sym) == |
| 563 | sym->def[S_DEF_AUTO].tri) |
| 564 | continue; |
| 565 | break; |
| 566 | case S_STRING: |
| 567 | case S_HEX: |
| 568 | case S_INT: |
| 569 | if (!strcmp(sym_get_string_value(sym), |
| 570 | sym->def[S_DEF_AUTO].val)) |
| 571 | continue; |
| 572 | break; |
| 573 | default: |
| 574 | break; |
| 575 | } |
| 576 | } else { |
| 577 | /* |
| 578 | * If there is no old value, only 'no' (unset) |
| 579 | * is allowed as new value. |
| 580 | */ |
| 581 | switch (sym->type) { |
| 582 | case S_BOOLEAN: |
| 583 | case S_TRISTATE: |
| 584 | if (sym_get_tristate_value(sym) == no) |
| 585 | continue; |
| 586 | break; |
| 587 | default: |
| 588 | break; |
| 589 | } |
| 590 | } |
| 591 | } else if (!(sym->flags & SYMBOL_DEF_AUTO)) |
| 592 | /* There is neither an old nor a new value. */ |
| 593 | continue; |
| 594 | /* else |
| 595 | * There is an old value, but no new value ('no' (unset) |
| 596 | * isn't saved in auto.conf, so the old value is always |
| 597 | * different from 'no'). |
| 598 | */ |
| 599 | |
| 600 | /* Replace all '_' and append ".h" */ |
| 601 | s = sym->name; |
| 602 | d = path; |
| 603 | while ((c = *s++)) { |
| 604 | c = tolower(c); |
| 605 | *d++ = (c == '_') ? '/' : c; |
| 606 | } |
| 607 | strcpy(d, ".h"); |
| 608 | |
| 609 | /* Assume directory path already exists. */ |
| 610 | fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 611 | if (fd == -1) { |
| 612 | if (errno != ENOENT) { |
| 613 | res = 1; |
| 614 | break; |
| 615 | } |
| 616 | /* |
| 617 | * Create directory components, |
| 618 | * unless they exist already. |
| 619 | */ |
| 620 | d = path; |
| 621 | while ((d = strchr(d, '/'))) { |
| 622 | *d = 0; |
| 623 | if (stat(path, &sb) && mkdir(path, 0755)) { |
| 624 | res = 1; |
| 625 | goto out; |
| 626 | } |
| 627 | *d++ = '/'; |
| 628 | } |
| 629 | /* Try it again. */ |
| 630 | fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 631 | if (fd == -1) { |
| 632 | res = 1; |
| 633 | break; |
| 634 | } |
| 635 | } |
| 636 | close(fd); |
| 637 | } |
| 638 | out: |
| 639 | if (chdir("../..")) |
| 640 | return 1; |
| 641 | |
| 642 | return res; |
| 643 | } |
| 644 | |
Roman Zippel | c955cca | 2006-06-08 22:12:39 -0700 | [diff] [blame] | 645 | int conf_write_autoconf(void) |
| 646 | { |
| 647 | struct symbol *sym; |
| 648 | const char *str; |
| 649 | char *name; |
| 650 | FILE *out, *out_h; |
| 651 | time_t now; |
| 652 | int i, l; |
| 653 | |
Roman Zippel | 2e3646e | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 654 | sym_clear_all_valid(); |
| 655 | |
Roman Zippel | c955cca | 2006-06-08 22:12:39 -0700 | [diff] [blame] | 656 | file_write_dep("include/config/auto.conf.cmd"); |
| 657 | |
Roman Zippel | 2e3646e | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 658 | if (conf_split_config()) |
| 659 | return 1; |
| 660 | |
Roman Zippel | c955cca | 2006-06-08 22:12:39 -0700 | [diff] [blame] | 661 | out = fopen(".tmpconfig", "w"); |
| 662 | if (!out) |
| 663 | return 1; |
| 664 | |
| 665 | out_h = fopen(".tmpconfig.h", "w"); |
| 666 | if (!out_h) { |
| 667 | fclose(out); |
| 668 | return 1; |
| 669 | } |
| 670 | |
| 671 | sym = sym_lookup("KERNELVERSION", 0); |
| 672 | sym_calc_value(sym); |
| 673 | time(&now); |
| 674 | fprintf(out, "#\n" |
| 675 | "# Automatically generated make config: don't edit\n" |
| 676 | "# Linux kernel version: %s\n" |
| 677 | "# %s" |
| 678 | "#\n", |
| 679 | sym_get_string_value(sym), ctime(&now)); |
| 680 | fprintf(out_h, "/*\n" |
| 681 | " * Automatically generated C config: don't edit\n" |
| 682 | " * Linux kernel version: %s\n" |
| 683 | " * %s" |
| 684 | " */\n" |
| 685 | "#define AUTOCONF_INCLUDED\n", |
| 686 | sym_get_string_value(sym), ctime(&now)); |
| 687 | |
Roman Zippel | c955cca | 2006-06-08 22:12:39 -0700 | [diff] [blame] | 688 | for_all_symbols(i, sym) { |
| 689 | sym_calc_value(sym); |
| 690 | if (!(sym->flags & SYMBOL_WRITE) || !sym->name) |
| 691 | continue; |
| 692 | switch (sym->type) { |
| 693 | case S_BOOLEAN: |
| 694 | case S_TRISTATE: |
| 695 | switch (sym_get_tristate_value(sym)) { |
| 696 | case no: |
| 697 | break; |
| 698 | case mod: |
| 699 | fprintf(out, "CONFIG_%s=m\n", sym->name); |
| 700 | fprintf(out_h, "#define CONFIG_%s_MODULE 1\n", sym->name); |
| 701 | break; |
| 702 | case yes: |
| 703 | fprintf(out, "CONFIG_%s=y\n", sym->name); |
| 704 | fprintf(out_h, "#define CONFIG_%s 1\n", sym->name); |
| 705 | break; |
| 706 | } |
| 707 | break; |
| 708 | case S_STRING: |
| 709 | str = sym_get_string_value(sym); |
| 710 | fprintf(out, "CONFIG_%s=\"", sym->name); |
| 711 | fprintf(out_h, "#define CONFIG_%s \"", sym->name); |
| 712 | while (1) { |
| 713 | l = strcspn(str, "\"\\"); |
| 714 | if (l) { |
| 715 | fwrite(str, l, 1, out); |
| 716 | fwrite(str, l, 1, out_h); |
| 717 | str += l; |
| 718 | } |
| 719 | if (!*str) |
| 720 | break; |
| 721 | fprintf(out, "\\%c", *str); |
| 722 | fprintf(out_h, "\\%c", *str); |
| 723 | str++; |
| 724 | } |
| 725 | fputs("\"\n", out); |
| 726 | fputs("\"\n", out_h); |
| 727 | break; |
| 728 | case S_HEX: |
| 729 | str = sym_get_string_value(sym); |
| 730 | if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) { |
| 731 | fprintf(out, "CONFIG_%s=%s\n", sym->name, str); |
| 732 | fprintf(out_h, "#define CONFIG_%s 0x%s\n", sym->name, str); |
| 733 | break; |
| 734 | } |
| 735 | case S_INT: |
| 736 | str = sym_get_string_value(sym); |
| 737 | fprintf(out, "CONFIG_%s=%s\n", sym->name, str); |
| 738 | fprintf(out_h, "#define CONFIG_%s %s\n", sym->name, str); |
| 739 | break; |
| 740 | default: |
| 741 | break; |
| 742 | } |
| 743 | } |
| 744 | fclose(out); |
| 745 | fclose(out_h); |
| 746 | |
| 747 | name = getenv("KCONFIG_AUTOHEADER"); |
| 748 | if (!name) |
| 749 | name = "include/linux/autoconf.h"; |
| 750 | if (rename(".tmpconfig.h", name)) |
| 751 | return 1; |
| 752 | name = getenv("KCONFIG_AUTOCONFIG"); |
| 753 | if (!name) |
| 754 | name = "include/config/auto.conf"; |
| 755 | /* |
| 756 | * This must be the last step, kbuild has a dependency on auto.conf |
| 757 | * and this marks the successful completion of the previous steps. |
| 758 | */ |
| 759 | if (rename(".tmpconfig", name)) |
| 760 | return 1; |
| 761 | |
| 762 | return 0; |
| 763 | } |