Gentoo Archives: gentoo-commits

From: "Mike Frysinger (vapier)" <vapier@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] gentoo-projects commit in portage-utils: main.c main.h
Date: Mon, 21 Feb 2011 21:53:06
Message-Id: 20110221215255.7FC3320054@flycatcher.gentoo.org
1 vapier 11/02/21 21:52:55
2
3 Modified: main.c main.h
4 Log:
5 rework env var setup so that the values are allowed to grow dynamically without len restriction, and mark CONFIG_PROTECT/CONFIG_PROTECT_MASK as incremental
6
7 Revision Changes Path
8 1.183 portage-utils/main.c
9
10 file : http://sources.gentoo.org/viewvc.cgi/gentoo-projects/portage-utils/main.c?rev=1.183&view=markup
11 plain: http://sources.gentoo.org/viewvc.cgi/gentoo-projects/portage-utils/main.c?rev=1.183&content-type=text/plain
12 diff : http://sources.gentoo.org/viewvc.cgi/gentoo-projects/portage-utils/main.c?r1=1.182&r2=1.183
13
14 Index: main.c
15 ===================================================================
16 RCS file: /var/cvsroot/gentoo-projects/portage-utils/main.c,v
17 retrieving revision 1.182
18 retrieving revision 1.183
19 diff -u -r1.182 -r1.183
20 --- main.c 21 Feb 2011 07:33:21 -0000 1.182
21 +++ main.c 21 Feb 2011 21:52:55 -0000 1.183
22 @@ -1,7 +1,7 @@
23 /*
24 * Copyright 2005-2008 Gentoo Foundation
25 * Distributed under the terms of the GNU General Public License v2
26 - * $Header: /var/cvsroot/gentoo-projects/portage-utils/main.c,v 1.182 2011/02/21 07:33:21 vapier Exp $
27 + * $Header: /var/cvsroot/gentoo-projects/portage-utils/main.c,v 1.183 2011/02/21 21:52:55 vapier Exp $
28 *
29 * Copyright 2005-2008 Ned Ludd - <solar@g.o>
30 * Copyright 2005-2008 Mike Frysinger - <vapier@g.o>
31 @@ -54,20 +54,20 @@
32 char pretend = 0;
33 char reinitialize = 0;
34 char reinitialize_metacache = 0;
35 -char portdir[_Q_PATH_MAX] = EPREFIX "/usr/portage";
36 -char portarch[20] = "";
37 -char portvdb[_Q_PATH_MAX] = "var/db/pkg";
38 +static char *portdir;
39 +static char *portarch;
40 +static char *portvdb;
41 const char portcachedir[] = "metadata/cache";
42 -char portroot[_Q_PATH_MAX] = "/";
43 -char config_protect[_Q_PATH_MAX] = EPREFIX "/etc/";
44 +static char *portroot;
45 +static char *config_protect, *config_protect_mask;
46
47 -char pkgdir[512] = EPREFIX "/usr/portage/packages/";
48 -char port_tmpdir[512] = EPREFIX "/var/tmp/portage/";
49 +static char *pkgdir;
50 +static char *port_tmpdir;
51
52 -char binhost[1024] = PORTAGE_BINHOST;
53 -char features[2048] = "noman noinfo nodoc";
54 -char accept_license[512] = "*";
55 -char install_mask[BUFSIZ] = "";
56 +static char *binhost;
57 +static char *features;
58 +static char *accept_license;
59 +static char *install_mask;
60
61 const char err_noapplet[] = "Sorry this applet was disabled at compile time";
62
63 @@ -336,12 +336,9 @@
64 void freeargv(int, char **);
65 void freeargv(int argc, char **argv)
66 {
67 - int i;
68 - if (argc > 0) {
69 - for (i = 0; i < argc; i++)
70 - free(argv[i]);
71 - free(argv);
72 - }
73 + while (argc--)
74 + free(argv[argc]);
75 + free(argv);
76 }
77
78 void makeargv(char *string, int *argc, char ***argv);
79 @@ -464,19 +461,20 @@
80 return &e;
81 }
82
83 -static char *strincr_var(const char *name, const char *s, char *value, const size_t value_len)
84 +static void strincr_var(const char *name, const char *s, char **value, size_t *value_len)
85 {
86 - char buf[BUFSIZ];
87 - char *p;
88 -
89 - if ((strlen(value) + 1 + strlen(s)) >= value_len)
90 - errf("%s will exceed max length value of %zi with a size of %zi", name, value_len, (strlen(value) + 1 + strlen(s)));
91 + size_t len;
92 + char *buf, *p, *nv;
93
94 - strncat(value, " ", value_len);
95 - strncat(value, s, value_len);
96 + len = strlen(s);
97 + *value = xrealloc(*value, *value_len + len + 2);
98 + nv = &(*value)[*value_len];
99 + if (*value_len)
100 + *nv++ = ' ';
101 + memcpy(nv, s, len + 1);
102
103 - while ((p = strstr(value, "-*")) != NULL)
104 - memset(value, ' ', (strlen(value)-strlen(p))+2);
105 + while ((p = strstr(nv, "-*")) != NULL)
106 + memset(*value, ' ', p - *value);
107
108 /* This function is mainly used by the startup code for parsing
109 make.conf and stacking variables remove.
110 @@ -489,20 +487,23 @@
111 wont work:
112 FEATURES="${OTHERVAR} foo"
113 FEATURES="-nls nls -nls"
114 + FEATURES="nls nls nls"
115 */
116
117 - snprintf(buf, sizeof(buf), "${%s}", name);
118 - if ((p = strstr(value, buf)) != NULL)
119 - memset(p, ' ', strlen(name)+3);
120 -
121 - snprintf(buf, sizeof(buf), "$%s", name);
122 - if ((p = strstr(value, buf)) != NULL)
123 - memset(p, ' ', strlen(name)+1);
124 + len = strlen(name);
125 + buf = alloca(len + 3 + 1);
126
127 - remove_extra_space(value);
128 - /* we should sort here */
129 + sprintf(buf, "${%s}", name);
130 + if ((p = strstr(nv, buf)) != NULL)
131 + memset(p, ' ', len + 3);
132 +
133 + sprintf(buf, "$%s", name);
134 + if ((p = strstr(nv, buf)) != NULL)
135 + memset(p, ' ', len + 1);
136
137 - return value;
138 + remove_extra_space(*value);
139 + *value_len = strlen(*value);
140 + /* we should sort here */
141 }
142
143 typedef enum { _Q_BOOL, _Q_STR, _Q_ISTR } var_types;
144 @@ -510,21 +511,32 @@
145 const char *name;
146 const size_t name_len;
147 const var_types type;
148 - char *value;
149 - const size_t value_len;
150 + union {
151 + char **s;
152 + bool *b;
153 + } value;
154 + size_t value_len;
155 + const char *default_value;
156 } env_vars;
157
158 -static void set_portage_env_var(const env_vars var, const char *value)
159 +static void set_portage_env_var(env_vars *var, const char *value)
160 {
161 - switch (var.type) {
162 - case _Q_BOOL: *var.value = 1; break;
163 - case _Q_STR: strncpy(var.value, value, var.value_len); break;
164 - case _Q_ISTR: strincr_var(var.name, value, var.value, var.value_len); break;
165 + switch (var->type) {
166 + case _Q_BOOL:
167 + *var->value.b = 1;
168 + break;
169 + case _Q_STR:
170 + free(*var->value.s);
171 + *var->value.s = xstrdup(value);
172 + break;
173 + case _Q_ISTR:
174 + strincr_var(var->name, value, var->value.s, &var->value_len);
175 + break;
176 }
177 }
178
179 /* Helper to read a portage env file (e.g. make.conf) */
180 -static void read_portage_env_file(const char *file, const env_vars vars[])
181 +static void read_portage_env_file(const char *file, env_vars vars[])
182 {
183 size_t i, buflen, line;
184 FILE *fp;
185 @@ -586,7 +598,7 @@
186 }
187 }
188
189 - set_portage_env_var(vars[i], s);
190 + set_portage_env_var(&vars[i], s);
191 }
192 }
193
194 @@ -595,7 +607,7 @@
195 }
196
197 /* Helper to recursively read stacked make.defaults in profiles */
198 -static void read_portage_profile(const char *profile, const env_vars vars[])
199 +static void read_portage_profile(const char *profile, env_vars vars[])
200 {
201 size_t profile_len, sub_len;
202 char profile_file[_Q_PATH_MAX], *sub_file;
203 @@ -639,31 +651,43 @@
204 EPREFIX "/etc/make.conf",
205 EPREFIX "/etc/portage/make.conf",
206 };
207 - char nocolor = 0;
208 - const env_vars vars_to_read[] = {
209 - {"ACCEPT_LICENSE", 14, _Q_STR, accept_license, sizeof(accept_license)},
210 - {"INSTALL_MASK", 12, _Q_ISTR, install_mask, sizeof(install_mask)},
211 - {"ARCH", 4, _Q_STR, portarch, sizeof(portarch)},
212 - {"CONFIG_PROTECT", 14, _Q_STR, config_protect, sizeof(config_protect)},
213 - {"NOCOLOR", 7, _Q_BOOL, &nocolor, 1},
214 - {"FEATURES", 8, _Q_ISTR, features, sizeof(features)},
215 - {"PORTDIR", 7, _Q_STR, portdir, sizeof(portdir)},
216 - {"PORTAGE_BINHOST", 15, _Q_STR, binhost, sizeof(binhost)},
217 - {"PORTAGE_TMPDIR", 14, _Q_STR, port_tmpdir, sizeof(port_tmpdir)},
218 - {"PKGDIR", 6, _Q_STR, pkgdir, sizeof(pkgdir)},
219 - {"ROOT", 4, _Q_STR, portroot, sizeof(portroot)},
220 - { },
221 + bool nocolor = 0;
222 +
223 + env_vars vars_to_read[] = {
224 +#define _Q_EV(t, V, set, lset, d) \
225 + { \
226 + .name = #V, \
227 + .name_len = strlen(#V), \
228 + .type = _Q_##t, \
229 + set, \
230 + lset, \
231 + .default_value = d, \
232 + },
233 +#define _Q_EVS(t, V, v, d) _Q_EV(t, V, .value.s = &v, .value_len = strlen(d), d)
234 +#define _Q_EVB(t, V, v, d) _Q_EV(t, V, .value.b = &v, .value_len = 0, d)
235 +
236 + _Q_EVS(STR, ACCEPT_LICENSE, accept_license, "")
237 + _Q_EVS(ISTR, INSTALL_MASK, install_mask, "")
238 + _Q_EVS(STR, ARCH, portarch, "")
239 + _Q_EVS(ISTR, CONFIG_PROTECT, config_protect, EPREFIX "/etc")
240 + _Q_EVS(ISTR, CONFIG_PROTECT_MASK, config_protect_mask, "")
241 + _Q_EVB(BOOL, NOCOLOR, nocolor, 0)
242 + _Q_EVS(ISTR, FEATURES, features, "noman noinfo nodoc")
243 + _Q_EVS(STR, PORTDIR, portdir, EPREFIX "/usr/portage")
244 + _Q_EVS(STR, PORTAGE_BINHOST, binhost, DEFAULT_PORTAGE_BINHOST)
245 + _Q_EVS(STR, PORTAGE_TMPDIR, port_tmpdir, EPREFIX "/var/tmp/portage/")
246 + _Q_EVS(STR, PKGDIR, pkgdir, EPREFIX "/usr/portage/packages/")
247 + _Q_EVS(STR, ROOT, portroot, "/")
248 + _Q_EVS(STR, Q_VDB, portvdb, EPREFIX "/var/db/pkg")
249 + { }
250 +
251 +#undef _Q_EV
252 };
253
254 - s = getenv("Q_VDB"); /* #257251 */
255 - if (s) {
256 - strncpy(portvdb, s, sizeof(portvdb));
257 - portvdb[sizeof(portvdb) - 1] = '\0';
258 - } else if ((i = strlen(EPREFIX)) > 1) {
259 - memmove(portvdb + i, portvdb, strlen(portvdb));
260 - memcpy(portvdb, EPREFIX + 1, i - 1);
261 - portvdb[i - 1] = '/';
262 - }
263 + /* initialize all the strings with their default value */
264 + for (i = 0; vars_to_read[i].name; ++i)
265 + if (vars_to_read[i].type != _Q_BOOL)
266 + *vars_to_read[i].value.s = xstrdup(vars_to_read[i].default_value);
267
268 if ((s = strchr(portroot, '/')) != NULL)
269 if (strlen(s) != 1)
270 @@ -681,16 +705,17 @@
271 for (i = 0; vars_to_read[i].name; ++i) {
272 s = getenv(vars_to_read[i].name);
273 if (s != NULL)
274 - set_portage_env_var(vars_to_read[i], s);
275 + set_portage_env_var(&vars_to_read[i], s);
276 if (getenv("DEBUG") IF_DEBUG(|| 1)) {
277 fprintf(stderr, "%s = ", vars_to_read[i].name);
278 switch (vars_to_read[i].type) {
279 - case _Q_BOOL: fprintf(stderr, "%i\n", *vars_to_read[i].value); break;
280 + case _Q_BOOL: fprintf(stderr, "%i\n", *vars_to_read[i].value.b); break;
281 case _Q_STR:
282 - case _Q_ISTR: fprintf(stderr, "%s\n", vars_to_read[i].value); break;
283 + case _Q_ISTR: fprintf(stderr, "%s\n", *vars_to_read[i].value.s); break;
284 }
285 }
286 }
287 +
288 if ((s = strchr(portroot, '/')) != NULL)
289 if (strlen(s) != 1)
290 strncat(portroot, "/", sizeof(portroot));
291
292
293
294 1.7 portage-utils/main.h
295
296 file : http://sources.gentoo.org/viewvc.cgi/gentoo-projects/portage-utils/main.h?rev=1.7&view=markup
297 plain: http://sources.gentoo.org/viewvc.cgi/gentoo-projects/portage-utils/main.h?rev=1.7&content-type=text/plain
298 diff : http://sources.gentoo.org/viewvc.cgi/gentoo-projects/portage-utils/main.h?r1=1.6&r2=1.7
299
300 Index: main.h
301 ===================================================================
302 RCS file: /var/cvsroot/gentoo-projects/portage-utils/main.h,v
303 retrieving revision 1.6
304 retrieving revision 1.7
305 diff -u -r1.6 -r1.7
306 --- main.h 21 Feb 2011 07:33:21 -0000 1.6
307 +++ main.h 21 Feb 2011 21:52:55 -0000 1.7
308 @@ -1,7 +1,7 @@
309 /*
310 * Copyright 2005-2010 Gentoo Foundation
311 * Distributed under the terms of the GNU General Public License v2
312 - * $Header: /var/cvsroot/gentoo-projects/portage-utils/main.h,v 1.6 2011/02/21 07:33:21 vapier Exp $
313 + * $Header: /var/cvsroot/gentoo-projects/portage-utils/main.h,v 1.7 2011/02/21 21:52:55 vapier Exp $
314 *
315 * Copyright 2005-2010 Ned Ludd - <solar@g.o>
316 * Copyright 2005-2010 Mike Frysinger - <vapier@g.o>
317 @@ -24,26 +24,26 @@
318 #define URL "http://tinderbox.dev.gentoo.org"
319 # ifdef __i386__
320 # ifdef __UCLIBC__
321 -# define PORTAGE_BINHOST URL "/uclibc/i386"
322 +# define DEFAULT_PORTAGE_BINHOST URL "/uclibc/i386"
323 # else
324 # ifdef __SSP__
325 -# define PORTAGE_BINHOST URL "/hardened/x86"
326 +# define DEFAULT_PORTAGE_BINHOST URL "/hardened/x86"
327 # else
328 -# define PORTAGE_BINHOST URL "/default-linux/x86/All"
329 +# define DEFAULT_PORTAGE_BINHOST URL "/default-linux/x86/All"
330 # endif
331 # endif
332 # if defined(__powerpc__) && defined(__SSP__)
333 # if !defined(__UCLIBC__)
334 -# define PORTAGE_BINHOST URL "/hardened/ppc"
335 +# define DEFAULT_PORTAGE_BINHOST URL "/hardened/ppc"
336 # else
337 -# define PORTAGE_BINHOST URL "/uclibc/ppc"
338 +# define DEFAULT_PORTAGE_BINHOST URL "/uclibc/ppc"
339 # endif
340 # endif
341 # endif
342 #endif
343
344 -#ifndef PORTAGE_BINHOST
345 -# define PORTAGE_BINHOST ""
346 +#ifndef DEFAULT_PORTAGE_BINHOST
347 +# define DEFAULT_PORTAGE_BINHOST ""
348 #endif
349
350 #define qfprintf(stream, fmt, args...) do { if (!quiet) fprintf(stream, _( fmt ), ## args); } while (0)