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 <stdarg.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <limits.h>
12 #include <libgen.h>
13 #include "lkc.h"
14 
15 /* file already present in list? If not add it */
file_lookup(const char * name,bool relative)16 struct file *file_lookup(const char *name, bool relative)
17 {
18 	struct file *file;
19 	char fullname[PATH_MAX + 1] = { 0 };
20 
21 	if (relative) {
22 		char *last_bslash = strrchr(zconf_curname(), '\\');
23 		char *last_fslash = strrchr(zconf_curname(), '/');
24 		char *last_slash = last_bslash ? last_bslash : last_fslash;
25 		strncpy(fullname, zconf_curname(), last_slash - zconf_curname());
26 		strcat(fullname, last_bslash ? "\\" : "/");
27 		strcat(fullname, name);
28 	} else {
29 		sprintf(fullname, "%s", name);
30 	}
31 
32 	const char *file_name = sym_expand_string_value(fullname);
33 
34 	for (file = file_list; file; file = file->next) {
35 		if (!strcmp(name, file->name)) {
36 			free((void *)file_name);
37 			return file;
38 		}
39 	}
40 
41 	file = xmalloc(sizeof(*file));
42 	memset(file, 0, sizeof(*file));
43 	file->name = file_name;
44 	file->next = file_list;
45 	file_list = file;
46 	return file;
47 }
48 
49 /* write a dependency file as used by kbuild to track dependencies */
file_write_dep(const char * name)50 int file_write_dep(const char *name)
51 {
52 	struct symbol *sym, *env_sym;
53 	struct expr *e;
54 	struct file *file;
55 	FILE *out;
56 
57 	if (!name)
58 		name = ".kconfig.d";
59 	out = fopen("..config.tmp", "w");
60 	if (!out)
61 		return 1;
62 	fprintf(out, "deps_config := \\\n");
63 	for (file = file_list; file; file = file->next) {
64 		if (file->next)
65 			fprintf(out, "\t%s \\\n", file->name);
66 		else
67 			fprintf(out, "\t%s\n", file->name);
68 	}
69 	fprintf(out, "\n%s: \\\n"
70 		     "\t$(deps_config)\n\n", conf_get_autoconfig_name());
71 
72 	expr_list_for_each_sym(sym_env_list, e, sym) {
73 		struct property *prop;
74 		const char *value;
75 
76 		prop = sym_get_env_prop(sym);
77 		env_sym = prop_get_symbol(prop);
78 		if (!env_sym)
79 			continue;
80 		value = getenv(env_sym->name);
81 		if (!value)
82 			value = "";
83 		fprintf(out, "ifneq \"$(%s)\" \"%s\"\n", env_sym->name, value);
84 		fprintf(out, "%s: FORCE\n", conf_get_autoconfig_name());
85 		fprintf(out, "endif\n");
86 	}
87 
88 	fprintf(out, "\n$(deps_config): ;\n");
89 	fclose(out);
90 	rename("..config.tmp", name);
91 	return 0;
92 }
93 
94 
95 /* Allocate initial growable string */
str_new(void)96 struct gstr str_new(void)
97 {
98 	struct gstr gs;
99 	gs.s = xmalloc(sizeof(char) * 64);
100 	gs.len = 64;
101 	gs.max_width = 0;
102 	strcpy(gs.s, "\0");
103 	return gs;
104 }
105 
106 /* Free storage for growable string */
str_free(struct gstr * gs)107 void str_free(struct gstr *gs)
108 {
109 	if (gs->s)
110 		free(gs->s);
111 	gs->s = NULL;
112 	gs->len = 0;
113 }
114 
115 /* Append to growable string */
str_append(struct gstr * gs,const char * s)116 void str_append(struct gstr *gs, const char *s)
117 {
118 	size_t l;
119 	if (s) {
120 		l = strlen(gs->s) + strlen(s) + 1;
121 		if (l > gs->len) {
122 			gs->s   = realloc(gs->s, l);
123 			gs->len = l;
124 		}
125 		strcat(gs->s, s);
126 	}
127 }
128 
129 /* Append printf formatted string to growable string */
str_printf(struct gstr * gs,const char * fmt,...)130 void str_printf(struct gstr *gs, const char *fmt, ...)
131 {
132 	va_list ap;
133 	char s[10000]; /* big enough... */
134 	va_start(ap, fmt);
135 	vsnprintf(s, sizeof(s), fmt, ap);
136 	str_append(gs, s);
137 	va_end(ap);
138 }
139 
140 /* Retrieve value of growable string */
str_get(struct gstr * gs)141 const char *str_get(struct gstr *gs)
142 {
143 	return gs->s;
144 }
145 
xmalloc(size_t size)146 void *xmalloc(size_t size)
147 {
148 	void *p = malloc(size);
149 	if (p)
150 		return p;
151 	fprintf(stderr, "Out of memory.\n");
152 	exit(1);
153 }
154 
xcalloc(size_t nmemb,size_t size)155 void *xcalloc(size_t nmemb, size_t size)
156 {
157 	void *p = calloc(nmemb, size);
158 	if (p)
159 		return p;
160 	fprintf(stderr, "Out of memory.\n");
161 	exit(1);
162 }
163