blob: 12bb8ab67cd8849556e111918c9b26d68567bfd2 [file] [log] [blame]
Nicholas Flintham1e3d3112013-04-10 10:48:38 +01001/*
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 <stdarg.h>
9#include <stdlib.h>
10#include <string.h>
11#include "lkc.h"
12
13struct file *file_lookup(const char *name)
14{
15 struct file *file;
16 const char *file_name = sym_expand_string_value(name);
17
18 for (file = file_list; file; file = file->next) {
19 if (!strcmp(name, file->name)) {
20 free((void *)file_name);
21 return file;
22 }
23 }
24
25 file = malloc(sizeof(*file));
26 memset(file, 0, sizeof(*file));
27 file->name = file_name;
28 file->next = file_list;
29 file_list = file;
30 return file;
31}
32
33int file_write_dep(const char *name)
34{
35 struct symbol *sym, *env_sym;
36 struct expr *e;
37 struct file *file;
38 FILE *out;
39
40 if (!name)
41 name = ".kconfig.d";
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 }
52 fprintf(out, "\n%s: \\\n"
53 "\t$(deps_config)\n\n", conf_get_autoconfig_name());
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);
67 fprintf(out, "%s: FORCE\n", conf_get_autoconfig_name());
68 fprintf(out, "endif\n");
69 }
70
71 fprintf(out, "\n$(deps_config): ;\n");
72 fclose(out);
73 rename("..config.tmp", name);
74 return 0;
75}
76
77
78struct gstr str_new(void)
79{
80 struct gstr gs;
81 gs.s = malloc(sizeof(char) * 64);
82 gs.len = 64;
83 gs.max_width = 0;
84 strcpy(gs.s, "\0");
85 return gs;
86}
87
88struct gstr str_assign(const char *s)
89{
90 struct gstr gs;
91 gs.s = strdup(s);
92 gs.len = strlen(s) + 1;
93 gs.max_width = 0;
94 return gs;
95}
96
97void str_free(struct gstr *gs)
98{
99 if (gs->s)
100 free(gs->s);
101 gs->s = NULL;
102 gs->len = 0;
103}
104
105void str_append(struct gstr *gs, const char *s)
106{
107 size_t l;
108 if (s) {
109 l = strlen(gs->s) + strlen(s) + 1;
110 if (l > gs->len) {
111 gs->s = realloc(gs->s, l);
112 gs->len = l;
113 }
114 strcat(gs->s, s);
115 }
116}
117
118void str_printf(struct gstr *gs, const char *fmt, ...)
119{
120 va_list ap;
121 char s[10000];
122 va_start(ap, fmt);
123 vsnprintf(s, sizeof(s), fmt, ap);
124 str_append(gs, s);
125 va_end(ap);
126}
127
128const char *str_get(struct gstr *gs)
129{
130 return gs->s;
131}
132