public inbox for linux-kbuild@vger.kernel.org
 help / color / mirror / Atom feed
From: Sam Ravnborg <sam@ravnborg.org>
To: lkml <linux-kernel@vger.kernel.org>,
	kbuild <linux-kbuild@vger.kernel.org>
Cc: "Sally, Gene" <Gene.Sally@timesys.com>,
	gene.sally@timesys.com, Sam Ravnborg <sam@ravnborg.org>
Subject: [PATCH 7/8] kbuild: gen_init_cpio expands shell variables in file names
Date: Mon, 24 Nov 2008 21:54:54 +0100	[thread overview]
Message-ID: <1227560095-32597-7-git-send-email-sam@ravnborg.org> (raw)
In-Reply-To: <20081124205150.GA32497@uranus.ravnborg.org>

From: Sally, Gene <Gene.Sally@timesys.com>

Modify gen_init_cpio so that lines that specify files can contain
what looks like a shell variable that's expanded during processing.

For example:

   file /sbin/kinit ${RFS_BASE}/usr/src/klibc/kinit/kinit 0755 0 0

given RFS_BASE is "/some/directory" in the environment

would be expanded to

   file /sbin/kinit /some/directory/usr/src/klibc/kinit/kinit 0755 0 0

If several environment variables appear in a line, they are all expanded
with processing happening from left to right.
Undefined variables expand to a null string.
Syntax errors stop processing, letting the existing error handling
show the user offending line.

This patch helps embedded folks who frequently create several
RFS directories and then switch between them as they're tuning
an initramfs.

Signed-off-by: gene.sally@timesys.com
Signed-off-by: Sam Ravnborg <sam@ravnborg.org>
---
 usr/gen_init_cpio.c |   28 +++++++++++++++++++++++++++-
 1 files changed, 27 insertions(+), 1 deletions(-)

diff --git a/usr/gen_init_cpio.c b/usr/gen_init_cpio.c
index 7abc07f..f1d3fe3 100644
--- a/usr/gen_init_cpio.c
+++ b/usr/gen_init_cpio.c
@@ -370,6 +370,30 @@ error:
 	return rc;
 }
 
+static char *cpio_replace_env(char *new_location)
+{
+       char expanded[PATH_MAX + 1];
+       char env_var[PATH_MAX + 1];
+       char *start;
+       char *end;
+
+       for (start = NULL; (start = strstr(new_location, "${")); ) {
+               end = strchr(start, '}');
+               if (start < end) {
+                       *env_var = *expanded = '\0';
+                       strncat(env_var, start + 2, end - start - 2);
+                       strncat(expanded, new_location, start - new_location);
+                       strncat(expanded, getenv(env_var), PATH_MAX);
+                       strncat(expanded, end + 1, PATH_MAX);
+                       strncpy(new_location, expanded, PATH_MAX);
+               } else
+                       break;
+       }
+
+       return new_location;
+}
+
+
 static int cpio_mkfile_line(const char *line)
 {
 	char name[PATH_MAX + 1];
@@ -415,7 +439,8 @@ static int cpio_mkfile_line(const char *line)
 	} else {
 		dname = name;
 	}
-	rc = cpio_mkfile(dname, location, mode, uid, gid, nlinks);
+	rc = cpio_mkfile(dname, cpio_replace_env(location),
+	                 mode, uid, gid, nlinks);
  fail:
 	if (dname_len) free(dname);
 	return rc;
@@ -439,6 +464,7 @@ void usage(const char *prog)
 		"\n"
 		"<name>       name of the file/dir/nod/etc in the archive\n"
 		"<location>   location of the file in the current filesystem\n"
+		"             expands shell variables quoted with ${}\n"
 		"<target>     link target\n"
 		"<mode>       mode/permissions of the file\n"
 		"<uid>        user id (0=root)\n"
-- 
1.5.6.GIT


  parent reply	other threads:[~2008-11-24 20:53 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-11-24 20:51 [GIT] kbuild patches Sam Ravnborg
2008-11-24 20:54 ` [PATCH 1/8] kbuild: expand -I in KBUILD_CPPFLAGS Sam Ravnborg
2008-11-24 20:54 ` [PATCH 2/8] kbuild: kill output in silent mode of mkcompile_h Sam Ravnborg
2008-11-24 20:54 ` [PATCH 3/8] kbuild: introduce $(kecho) convenience echo Sam Ravnborg
2008-11-24 20:54 ` [PATCH 4/8] kbuild: use KECHO " Sam Ravnborg
2008-11-24 20:54 ` [PATCH 5/8] kbuild: teach mkmakfile to be silent Sam Ravnborg
2008-11-24 20:54 ` [PATCH 6/8] remove bashisms from scripts/extract-ikconfig Sam Ravnborg
2008-11-24 20:54 ` Sam Ravnborg [this message]
2008-11-24 20:54 ` [PATCH 8/8] kbuild: move tags support to a shell script Sam Ravnborg
2008-11-24 22:07   ` Ian Campbell
2008-11-24 23:16     ` Jeff Dike
2008-11-25  9:46     ` Sam Ravnborg

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=1227560095-32597-7-git-send-email-sam@ravnborg.org \
    --to=sam@ravnborg.org \
    --cc=Gene.Sally@timesys.com \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.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