kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andrew Jones <drjones@redhat.com>
To: kvm@vger.kernel.org
Cc: rkrcmar@redhat.com, pbonzini@redhat.com, lvivier@redhat.com,
	thuth@redhat.com
Subject: [PATCH kvm-unit-tests 10/15] lib/argv: introduce setup_env and getenv
Date: Fri, 13 Jan 2017 19:15:28 +0100	[thread overview]
Message-ID: <20170113181533.15145-11-drjones@redhat.com> (raw)
In-Reply-To: <20170113181533.15145-1-drjones@redhat.com>

Provide a function that imports an environ (a list of key=value
environment variables). The list may be delimited by either
'\0' or '\n'. If the list is delimited by '\n', then we assume
it's user input and do additional sanity checking, as well as
allow variables to be commented out with '#'. We also provide
getenv() to lookup the variables.

Signed-off-by: Andrew Jones <drjones@redhat.com>
---
 lib/libcflat.h |  1 +
 lib/argv.c     | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 lib/string.c   | 15 ++++++++++
 3 files changed, 105 insertions(+), 1 deletion(-)

diff --git a/lib/libcflat.h b/lib/libcflat.h
index e80fc5071f61..96a37926f302 100644
--- a/lib/libcflat.h
+++ b/lib/libcflat.h
@@ -85,6 +85,7 @@ extern void puts(const char *s);
 extern void exit(int code);
 extern void abort(void);
 extern long atol(const char *ptr);
+extern char *getenv(const char *name);
 
 extern int printf(const char *fmt, ...)
 					__attribute__((format(printf, 1, 2)));
diff --git a/lib/argv.c b/lib/argv.c
index fe8826255c18..a37fc8792a92 100644
--- a/lib/argv.c
+++ b/lib/argv.c
@@ -2,13 +2,18 @@
 #include "auxinfo.h"
 
 int __argc;
-char *__argv[100];
 char *__args;
+char *__argv[100];
+char *__environ[200];
+
+char **environ = __environ;
 
 static char args_copy[1000];
 static char *copy_ptr = args_copy;
 
 #define isblank(c) ((c) == ' ' || (c) == '\t')
+#define isalpha(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z') || (c) == '_')
+#define isalnum(c) (isalpha(c) || ((c) >= '0' && (c) <= '9'))
 
 static char *skip_blanks(char *p)
 {
@@ -51,3 +56,86 @@ void setup_args_progname(char *args)
 		__setup_args();
 	}
 }
+
+static char *env_eol(char *env)
+{
+	while (*env && *env != '\n')
+		++env;
+	return env;
+}
+
+static char *env_invalid_eol(char *env)
+{
+	char *eol = env_eol(env);
+	char eol_old = *eol;
+
+	*eol = '\0';
+	printf("Invalid environment variable: %s\n", env);
+	*eol = eol_old;
+	return eol;
+}
+
+static char *env_next(char *env)
+{
+	char *p;
+
+	if (!*env)
+		return env;
+
+	if (isalpha(*env)) {
+		bool invalid = false;
+
+		p = env + 1;
+		while (*p && *p != '=' && *p != '\n') {
+			if (!isalnum(*p))
+				invalid = true;
+			++p;
+		}
+
+		if (*p != '=')
+			invalid = true;
+
+		if (invalid) {
+			env = env_invalid_eol(env);
+			return *env ? env_next(env + 1) : env;
+		}
+		return env;
+	}
+
+	p = env;
+	while (isblank(*p))
+		++p;
+
+	if (*p == '\n')
+		return env_next(p + 1);
+
+	if (*p == '#')
+		env = env_eol(env);
+	else
+		env = env_invalid_eol(env);
+
+	return *env ? env_next(env + 1) : env;
+}
+
+void setup_env(char *env, int size)
+{
+	char *eof = env + size, *p = env;
+	bool newline = false;
+	int i = 0;
+
+	while (*p)
+		++p;
+	if (p == eof)
+		newline = true;
+
+	while (env < eof) {
+		if (newline)
+			env = env_next(env);
+		if (!*env || env >= eof)
+			break;
+		__environ[i++] = env;
+		while (env < eof && *env && !(newline && *env == '\n'))
+			++env;
+		*env++ = '\0';
+	}
+}
diff --git a/lib/string.c b/lib/string.c
index ee93e25e9821..833f22be48c5 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -158,3 +158,18 @@ long atol(const char *ptr)
 
     return acc;
 }
+
+extern char **environ;
+
+char *getenv(const char *name)
+{
+    char **envp = environ, *delim;
+
+    while (*envp) {
+        delim = strchr(*envp, '=');
+        if (delim && strncmp(name, *envp, delim - *envp) == 0)
+            return delim + 1;
+        ++envp;
+    }
+    return NULL;
+}
-- 
2.9.3


  parent reply	other threads:[~2017-01-13 18:15 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-13 18:15 [PATCH kvm-unit-tests 00/15] Introduce an errata framework Andrew Jones
2017-01-13 18:15 ` [PATCH kvm-unit-tests 01/15] devicetree: improve dt_get_bootargs interface Andrew Jones
2017-01-13 18:15 ` [PATCH kvm-unit-tests 02/15] devicetree: introduce dt_get_initrd Andrew Jones
2017-01-13 18:15 ` [PATCH kvm-unit-tests 03/15] arm/arm64: better document setup Andrew Jones
2017-01-13 18:15 ` [PATCH kvm-unit-tests 04/15] powerpc: " Andrew Jones
2017-01-13 18:15 ` [PATCH kvm-unit-tests 05/15] arm/arm64: import initrd Andrew Jones
2017-01-13 18:15 ` [PATCH kvm-unit-tests 06/15] powerpc: " Andrew Jones
2017-01-13 18:15 ` [PATCH kvm-unit-tests 07/15] x86_64: mbi-cmdline is a 4 byte addr Andrew Jones
2017-01-13 18:15 ` [PATCH kvm-unit-tests 08/15] x86: import initrd Andrew Jones
2017-01-13 18:15 ` [PATCH kvm-unit-tests 09/15] lib/argv: fix coding style Andrew Jones
2017-01-13 18:15 ` Andrew Jones [this message]
2017-01-13 18:15 ` [PATCH kvm-unit-tests 11/15] arm/arm64: enable environ Andrew Jones
2017-01-13 18:15 ` [PATCH kvm-unit-tests 12/15] powerpc: " Andrew Jones
2017-01-13 18:15 ` [PATCH kvm-unit-tests 13/15] x86: " Andrew Jones
2017-01-13 18:15 ` [PATCH kvm-unit-tests 14/15] README: reserve some environment variables Andrew Jones
2017-01-13 18:15 ` [PATCH kvm-unit-tests 15/15] Introduce lib/errata.h Andrew Jones
2017-03-03 14:40   ` Radim Krčmář

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=20170113181533.15145-11-drjones@redhat.com \
    --to=drjones@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=lvivier@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=rkrcmar@redhat.com \
    --cc=thuth@redhat.com \
    /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;
as well as URLs for NNTP newsgroup(s).