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