enum {
PRE_KEY,
KEY,
- PRE_EQUAL,
PRE_VALUE,
VALUE,
VALUE_ESCAPE,
switch (state) {
case PRE_KEY:
- if (startswith(p, "export "))
- p+=6;
- else if (strchr(COMMENTS, c))
+ if (strchr(COMMENTS, c))
state = COMMENT;
else if (!strchr(WHITESPACE, c)) {
state = KEY;
if (strchr(newline, c)) {
state = PRE_KEY;
n_key = 0;
- } else if (strchr(WHITESPACE, c))
- state = PRE_EQUAL;
- else if (c == '=')
+ } else if (c == '=')
state = PRE_VALUE;
else {
if (!greedy_realloc((void**) &key, &key_alloc, n_key+2)) {
break;
- case PRE_EQUAL:
- if (strchr(newline, c)) {
- state = PRE_KEY;
- n_key = 0;
- } else if (c == '=')
- state = PRE_VALUE;
- else if (!strchr(WHITESPACE, c)) {
- n_key = 0;
- state = COMMENT;
- }
-
- break;
-
case PRE_VALUE:
if (strchr(newline, c) || strchr(COMMENTS, c)) {
state = PRE_KEY;
if (value)
value[n_value] = 0;
+ /* strip trailing whitespace from key */
+ while(strchr(WHITESPACE, key[--n_key]))
+ key[n_key]=0;
+
r = push(key, value, userdata);
if (r < 0)
goto fail;
if (last_whitespace != (size_t) -1)
value[last_whitespace] = 0;
+ /* strip trailing whitespace from key */
+ while(strchr(WHITESPACE, key[--n_key]))
+ key[n_key]=0;
+
r = push(key, value, userdata);
if (r < 0)
goto fail;
if (value)
value[n_value] = 0;
+ /* strip trailing whitespace from key */
+ while(strchr(WHITESPACE, key[--n_key]))
+ key[n_key]=0;
+
r = push(key, value, userdata);
if (r < 0)
goto fail;
#include "util.h"
#include "fileio.h"
#include "strv.h"
+#include "env-util.h"
static void test_parse_env_file(void) {
char t[] = "/tmp/test-parse-env-file-XXXXXX";
int fd, r;
FILE *f;
- _cleanup_free_ char *one = NULL, *two = NULL, *three = NULL, *four = NULL, *five = NULL, *six = NULL, *seven = NULL, *eight = NULL, *nine = NULL;
+ _cleanup_free_ char *one = NULL, *two = NULL, *three = NULL, *four = NULL, *five = NULL,
+ *six = NULL, *seven = NULL, *eight = NULL, *nine = NULL, *ten = NULL;
_cleanup_strv_free_ char **a = NULL, **b = NULL;
char **i;
unsigned k;
"five = \'55\\\'55\' \"FIVE\" cinco \n"
"six = seis sechs\\\n"
" sis\n"
- "export seven=\"sevenval\"#comment\n"
+ "seven=\"sevenval\"#comment\n"
"eight=#comment\n"
- "nine=", f);
+ "export nine=nineval\n"
+ "ten=", f);
fflush(f);
fclose(f);
+ r = load_env_file(t, NULL, &a);
+ assert_se(r >= 0);
+
+ STRV_FOREACH(i, a)
+ log_info("Got: <%s>", *i);
+
+ assert_se(streq(a[0], "one=BAR"));
+ assert_se(streq(a[1], "two=bar"));
+ assert_se(streq(a[2], "three=333\nxxxx"));
+ assert_se(streq(a[3], "four=44\"44"));
+ assert_se(streq(a[4], "five=55\'55FIVEcinco"));
+ assert_se(streq(a[5], "six=seis sechs sis"));
+ assert_se(streq(a[6], "seven=sevenval"));
+ assert_se(streq(a[7], "eight="));
+ assert_se(streq(a[8], "export nine=nineval"));
+ assert_se(streq(a[9], "ten="));
+ assert_se(a[10] == NULL);
+
+ strv_env_clean_log(a, "/tmp/test-fileio");
+
+ r = write_env_file("/tmp/test-fileio", a);
+ assert_se(r >= 0);
+
+ r = load_env_file("/tmp/test-fileio", NULL, &b);
+ assert_se(r >= 0);
+
+ k = 0;
+ STRV_FOREACH(i, b) {
+ log_info("Got2: <%s>", *i);
+ assert_se(streq(*i, a[k++]));
+ }
+
r = parse_env_file(
t, NULL,
"one", &one,
"six", &six,
"seven", &seven,
"eight", &eight,
- "nine", &nine,
+ "export nine", &nine,
+ "ten", &ten,
NULL);
assert_se(r >= 0);
log_info("six=[%s]", strna(six));
log_info("seven=[%s]", strna(seven));
log_info("eight=[%s]", strna(eight));
- log_info("nine=[%s]", strna(nine));
+ log_info("export nine=[%s]", strna(nine));
+ log_info("ten=[%s]", strna(nine));
assert_se(streq(one, "BAR"));
assert_se(streq(two, "bar"));
assert_se(streq(six, "seis sechs sis"));
assert_se(streq(seven, "sevenval"));
assert_se(eight == NULL);
- assert_se(nine == NULL);
-
- r = load_env_file(t, NULL, &a);
- assert_se(r >= 0);
-
- STRV_FOREACH(i, a)
- log_info("Got: <%s>", *i);
-
- assert_se(streq(a[0], "one=BAR"));
- assert_se(streq(a[1], "two=bar"));
- assert_se(streq(a[2], "three=333\nxxxx"));
- assert_se(streq(a[3], "four=44\"44"));
- assert_se(streq(a[4], "five=55\'55FIVEcinco"));
- assert_se(streq(a[5], "six=seis sechs sis"));
- assert_se(streq(a[6], "seven=sevenval"));
- assert_se(streq(a[7], "eight="));
- assert_se(streq(a[8], "nine="));
- assert_se(a[9] == NULL);
-
- r = write_env_file("/tmp/test-fileio", a);
- assert_se(r >= 0);
-
- r = load_env_file("/tmp/test-fileio", NULL, &b);
- assert_se(r >= 0);
-
- k = 0;
- STRV_FOREACH(i, b) {
- log_info("Got2: <%s>", *i);
- assert_se(streq(*i, a[k++]));
- }
+ assert_se(streq(nine, "nineval"));
+ assert_se(ten == NULL);
unlink(t);
unlink("/tmp/test-fileio");