public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Mike Frysinger (vapier)" <vapier@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] gentoo-projects commit in portage-utils: main.c
Date: Sun, 18 Dec 2011 03:04:07 +0000 (UTC)	[thread overview]
Message-ID: <20111218030407.755DF20034@flycatcher.gentoo.org> (raw)

vapier      11/12/18 03:04:07

  Modified:             main.c
  Log:
  expand nested variables when using known important ones #307625 by Wilke Schwiedop

Revision  Changes    Path
1.201                portage-utils/main.c

file : http://sources.gentoo.org/viewvc.cgi/gentoo-projects/portage-utils/main.c?rev=1.201&view=markup
plain: http://sources.gentoo.org/viewvc.cgi/gentoo-projects/portage-utils/main.c?rev=1.201&content-type=text/plain
diff : http://sources.gentoo.org/viewvc.cgi/gentoo-projects/portage-utils/main.c?r1=1.200&r2=1.201

Index: main.c
===================================================================
RCS file: /var/cvsroot/gentoo-projects/portage-utils/main.c,v
retrieving revision 1.200
retrieving revision 1.201
diff -u -r1.200 -r1.201
--- main.c	18 Dec 2011 01:17:14 -0000	1.200
+++ main.c	18 Dec 2011 03:04:07 -0000	1.201
@@ -1,7 +1,7 @@
 /*
  * Copyright 2005-2008 Gentoo Foundation
  * Distributed under the terms of the GNU General Public License v2
- * $Header: /var/cvsroot/gentoo-projects/portage-utils/main.c,v 1.200 2011/12/18 01:17:14 vapier Exp $
+ * $Header: /var/cvsroot/gentoo-projects/portage-utils/main.c,v 1.201 2011/12/18 03:04:07 vapier Exp $
  *
  * Copyright 2005-2008 Ned Ludd        - <solar@gentoo.org>
  * Copyright 2005-2008 Mike Frysinger  - <vapier@gentoo.org>
@@ -64,6 +64,7 @@
 static char *portvdb;
 const char portcachedir[] = "metadata/cache";
 static char *portroot;
+static char *eprefix;
 static char *config_protect, *config_protect_mask;
 
 static char *pkgdir;
@@ -501,6 +502,17 @@
 	const char *default_value;
 } env_vars;
 
+_q_static env_vars *get_portage_env_var(env_vars *vars, const char *name)
+{
+	size_t i;
+
+	for (i = 0; vars[i].name; ++i)
+		if (!strcmp(vars[i].name, name))
+			return &vars[i];
+
+	return NULL;
+}
+
 _q_static void set_portage_env_var(env_vars *var, const char *value)
 {
 	switch (var->type) {
@@ -673,6 +685,7 @@
 		_Q_EVS(ISTR, CONFIG_PROTECT_MASK, config_protect_mask, "")
 		_Q_EVB(BOOL, NOCOLOR,             nocolor,             0)
 		_Q_EVS(ISTR, FEATURES,            features,            "noman noinfo nodoc")
+		_Q_EVS(STR,  EPREFIX,             eprefix,             EPREFIX)
 		_Q_EVS(STR,  PORTDIR,             portdir,             EPREFIX "usr/portage")
 		_Q_EVS(STR,  PORTAGE_BINHOST,     binhost,             DEFAULT_PORTAGE_BINHOST)
 		_Q_EVS(STR,  PORTAGE_TMPDIR,      port_tmpdir,         EPREFIX "var/tmp/portage/")
@@ -709,7 +722,7 @@
 			set_portage_env_var(var, s);
 		if (getenv("DEBUG") IF_DEBUG(|| 1)) {
 			fprintf(stderr, "%s = ", var->name);
-			switch (vars_to_read[i].type) {
+			switch (var->type) {
 			case _Q_BOOL: fprintf(stderr, "%i\n", *var->value.b); break;
 			case _Q_STR:
 			case _Q_ISTR: fprintf(stderr, "%s\n", *var->value.s); break;
@@ -717,6 +730,70 @@
 		}
 	}
 
+	/* expand any nested variables e.g. PORTDIR=${EPREFIX}/usr/portage */
+	for (i = 0; vars_to_read[i].name; ++i) {
+		char *svar;
+
+		var = &vars_to_read[i];
+		if (var->type == _Q_BOOL)
+			continue;
+
+		while ((svar = strchr(*var->value.s, '$'))) {
+			env_vars *evar;
+			bool brace;
+			const char *sval;
+			size_t slen, pre_len, var_len, post_len;
+			char byte;
+
+			pre_len = svar - *var->value.s;
+
+			/* First skip the leading "${" */
+			s = ++svar;
+			brace = (*svar == '{');
+			if (brace)
+				s = ++svar;
+
+			/* Now skip the variable name itself */
+			while (isalnum(*svar) || *svar == '_')
+				++svar;
+
+			/* Finally skip the trailing "}" */
+			if (brace && *svar != '}') {
+				warn("invalid variable setting: %s\n", *var->value.s);
+				break;
+			}
+
+			var_len = svar - *var->value.s + 1;
+
+			byte = *svar;
+			*svar = '\0';
+			evar = get_portage_env_var(vars_to_read, s);
+			if (evar) {
+				sval = *evar->value.s;
+			} else {
+				sval = getenv(s);
+				if (!sval)
+					sval = "";
+			}
+			*svar = byte;
+			slen = strlen(sval);
+			post_len = strlen(svar + 1);
+			*var->value.s = xrealloc(*var->value.s, pre_len + MAX(var_len, slen) + post_len + 1);
+
+			/*
+			 * VAR=XxXxX	(slen = 5)
+			 * FOO${VAR}BAR
+			 * pre_len = 3
+			 * var_len = 6
+			 * post_len = 3
+			 */
+			memmove(*var->value.s + pre_len + slen,
+				*var->value.s + pre_len + var_len,
+				post_len + 1);
+			memcpy(*var->value.s + pre_len, sval, slen);
+		}
+	}
+
 	/* Make sure ROOT always ends in a slash */
 	var = &vars_to_read[0];
 	if ((*var->value.s)[var->value_len - 1] != '/') {






             reply	other threads:[~2011-12-18  3:04 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-12-18  3:04 Mike Frysinger (vapier) [this message]
  -- strict thread matches above, loose matches on Subject: below --
2013-10-31  2:40 [gentoo-commits] gentoo-projects commit in portage-utils: main.c Mike Frysinger (vapier)
2013-05-23  3:19 Mike Frysinger (vapier)
2013-04-03 18:39 Mike Frysinger (vapier)
2012-10-28 10:29 Mike Frysinger (vapier)
2012-10-28 10:11 Mike Frysinger (vapier)
2012-08-13 21:16 Robin H. Johnson (robbat2)
2012-08-04 20:24 Mike Frysinger (vapier)
2012-06-04 23:13 Mike Frysinger (vapier)
2012-05-19 13:20 Mike Frysinger (vapier)
2012-01-08 17:41 Ned Ludd (solar)
2011-12-22 20:26 Mike Frysinger (vapier)
2011-12-22 17:49 Mike Frysinger (vapier)
2011-12-22 17:11 Mike Frysinger (vapier)
2011-12-21  4:35 Mike Frysinger (vapier)
2011-12-19 20:09 Mike Frysinger (vapier)
2011-12-19  4:39 Mike Frysinger (vapier)
2011-12-19  4:37 Mike Frysinger (vapier)
2011-12-18 20:35 Mike Frysinger (vapier)
2011-12-18 20:21 Mike Frysinger (vapier)
2011-12-12 21:42 Fabian Groffen (grobian)
2011-12-09 16:50 Mike Frysinger (vapier)
2011-10-02 22:08 Mike Frysinger (vapier)
2011-03-17  3:04 Mike Frysinger (vapier)
2011-03-02  8:10 Mike Frysinger (vapier)
2011-03-02  7:55 Mike Frysinger (vapier)
2011-03-02  5:31 Mike Frysinger (vapier)
2011-03-02  2:40 Mike Frysinger (vapier)
2011-02-28 18:16 Mike Frysinger (vapier)
2010-12-19 13:15 Fabian Groffen (grobian)
2010-12-07  1:13 Mike Frysinger (vapier)
2010-12-04 12:37 Mike Frysinger (vapier)
2010-12-04 12:20 Mike Frysinger (vapier)
2010-12-04 11:15 Mike Frysinger (vapier)
2010-06-08  4:54 Mike Frysinger (vapier)
2010-01-17  0:25 Mike Frysinger (vapier)
2010-01-17  0:24 Mike Frysinger (vapier)
2010-01-13 18:23 Mike Frysinger (vapier)
2010-01-13  6:12 Ned Ludd (solar)
2009-09-08 16:49 Fabian Groffen (grobian)
2009-08-23  6:07 Ned Ludd (solar)
2009-03-15 10:03 Mike Frysinger (vapier)
2008-12-07  3:43 Ned Ludd (solar)
2008-09-26 16:42 Ned Ludd (solar)
2008-09-13 18:58 Fabian Groffen (grobian)
2008-05-11 17:25 Ned Ludd (solar)
2008-01-16  7:09 Mike Frysinger (vapier)
2008-01-15  8:03 Mike Frysinger (vapier)
2007-10-28 21:22 Ned Ludd (solar)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20111218030407.755DF20034@flycatcher.gentoo.org \
    --to=vapier@gentoo.org \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox