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