Blame view

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