Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2002-2005 Roman Zippel <zippel@linux-m68k.org> |
| 3 | * Copyright (C) 2002-2005 Sam Ravnborg <sam@ravnborg.org> |
| 4 | * |
| 5 | * Released under the terms of the GNU GPL v2.0. |
| 6 | */ |
| 7 | |
| 8 | #include <string.h> |
| 9 | #include "lkc.h" |
| 10 | |
| 11 | /* file already present in list? If not add it */ |
| 12 | struct file *file_lookup(const char *name) |
| 13 | { |
| 14 | struct file *file; |
Arnaud Lacombe | c7abe86 | 2010-09-04 16:11:26 -0400 | [diff] [blame] | 15 | const char *file_name = sym_expand_string_value(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | for (file = file_list; file; file = file->next) { |
Arnaud Lacombe | c7abe86 | 2010-09-04 16:11:26 -0400 | [diff] [blame] | 18 | if (!strcmp(name, file->name)) { |
| 19 | free((void *)file_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | return file; |
Arnaud Lacombe | c7abe86 | 2010-09-04 16:11:26 -0400 | [diff] [blame] | 21 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | file = malloc(sizeof(*file)); |
| 25 | memset(file, 0, sizeof(*file)); |
Arnaud Lacombe | c7abe86 | 2010-09-04 16:11:26 -0400 | [diff] [blame] | 26 | file->name = file_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | file->next = file_list; |
| 28 | file_list = file; |
| 29 | return file; |
| 30 | } |
| 31 | |
| 32 | /* write a dependency file as used by kbuild to track dependencies */ |
| 33 | int file_write_dep(const char *name) |
| 34 | { |
Roman Zippel | 9344908 | 2008-01-14 04:50:54 +0100 | [diff] [blame] | 35 | struct symbol *sym, *env_sym; |
| 36 | struct expr *e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | struct file *file; |
| 38 | FILE *out; |
| 39 | |
| 40 | if (!name) |
Sam Ravnborg | 752625c | 2005-12-26 23:34:03 +0100 | [diff] [blame] | 41 | name = ".kconfig.d"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | out = fopen("..config.tmp", "w"); |
| 43 | if (!out) |
| 44 | return 1; |
| 45 | fprintf(out, "deps_config := \\\n"); |
| 46 | for (file = file_list; file; file = file->next) { |
| 47 | if (file->next) |
| 48 | fprintf(out, "\t%s \\\n", file->name); |
| 49 | else |
| 50 | fprintf(out, "\t%s\n", file->name); |
| 51 | } |
Markus Heidelberg | 12122f6 | 2009-05-18 01:36:54 +0200 | [diff] [blame] | 52 | fprintf(out, "\n%s: \\\n" |
| 53 | "\t$(deps_config)\n\n", conf_get_autoconfig_name()); |
Roman Zippel | 9344908 | 2008-01-14 04:50:54 +0100 | [diff] [blame] | 54 | |
| 55 | expr_list_for_each_sym(sym_env_list, e, sym) { |
| 56 | struct property *prop; |
| 57 | const char *value; |
| 58 | |
| 59 | prop = sym_get_env_prop(sym); |
| 60 | env_sym = prop_get_symbol(prop); |
| 61 | if (!env_sym) |
| 62 | continue; |
| 63 | value = getenv(env_sym->name); |
| 64 | if (!value) |
| 65 | value = ""; |
| 66 | fprintf(out, "ifneq \"$(%s)\" \"%s\"\n", env_sym->name, value); |
Markus Heidelberg | 12122f6 | 2009-05-18 01:36:54 +0200 | [diff] [blame] | 67 | fprintf(out, "%s: FORCE\n", conf_get_autoconfig_name()); |
Roman Zippel | 9344908 | 2008-01-14 04:50:54 +0100 | [diff] [blame] | 68 | fprintf(out, "endif\n"); |
| 69 | } |
| 70 | |
| 71 | fprintf(out, "\n$(deps_config): ;\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | fclose(out); |
| 73 | rename("..config.tmp", name); |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | |
Thomas Weber | 31a2d31 | 2010-02-19 12:43:44 +0100 | [diff] [blame] | 78 | /* Allocate initial growable string */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | struct gstr str_new(void) |
| 80 | { |
| 81 | struct gstr gs; |
| 82 | gs.s = malloc(sizeof(char) * 64); |
Christophe Jaillet | 107f43a | 2008-05-18 23:10:24 +0200 | [diff] [blame] | 83 | gs.len = 64; |
Vadim Bendebury (вб) | da60fbb | 2009-12-20 00:29:49 -0800 | [diff] [blame] | 84 | gs.max_width = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | strcpy(gs.s, "\0"); |
| 86 | return gs; |
| 87 | } |
| 88 | |
| 89 | /* Allocate and assign growable string */ |
| 90 | struct gstr str_assign(const char *s) |
| 91 | { |
| 92 | struct gstr gs; |
| 93 | gs.s = strdup(s); |
| 94 | gs.len = strlen(s) + 1; |
Vadim Bendebury (вб) | da60fbb | 2009-12-20 00:29:49 -0800 | [diff] [blame] | 95 | gs.max_width = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | return gs; |
| 97 | } |
| 98 | |
| 99 | /* Free storage for growable string */ |
| 100 | void str_free(struct gstr *gs) |
| 101 | { |
| 102 | if (gs->s) |
| 103 | free(gs->s); |
| 104 | gs->s = NULL; |
| 105 | gs->len = 0; |
| 106 | } |
| 107 | |
| 108 | /* Append to growable string */ |
| 109 | void str_append(struct gstr *gs, const char *s) |
| 110 | { |
Sam Ravnborg | a67cb13 | 2007-09-19 21:23:09 +0200 | [diff] [blame] | 111 | size_t l; |
| 112 | if (s) { |
| 113 | l = strlen(gs->s) + strlen(s) + 1; |
| 114 | if (l > gs->len) { |
| 115 | gs->s = realloc(gs->s, l); |
| 116 | gs->len = l; |
| 117 | } |
| 118 | strcat(gs->s, s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /* Append printf formatted string to growable string */ |
| 123 | void str_printf(struct gstr *gs, const char *fmt, ...) |
| 124 | { |
| 125 | va_list ap; |
| 126 | char s[10000]; /* big enough... */ |
| 127 | va_start(ap, fmt); |
| 128 | vsnprintf(s, sizeof(s), fmt, ap); |
| 129 | str_append(gs, s); |
| 130 | va_end(ap); |
| 131 | } |
| 132 | |
Matt Mackall | 4a4efbd | 2006-01-03 13:27:11 +0100 | [diff] [blame] | 133 | /* Retrieve value of growable string */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | const char *str_get(struct gstr *gs) |
| 135 | { |
| 136 | return gs->s; |
| 137 | } |
| 138 | |