From 338b5535a9a4b2aa55c4d034479921f5aa77a560 Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Thu, 9 Jan 2014 15:22:43 +0100 Subject: [PATCH 1/3] ini: Fix parsing of variable with spaces before the = At least from what the source code suggests, a variable declaration in the config file as this should be supported: DFLAGS = -I/usr/include/d But in reality, if you live spaces like that, the variable value will get uppercased. This commit fixes this problem to properly parse the extra spaces. --- src/inifile.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/inifile.c b/src/inifile.c index f1c4c54eb122..d65c75836a85 100644 --- a/src/inifile.c +++ b/src/inifile.c @@ -285,7 +285,10 @@ const char *inifile(const char *argv0x, const char *inifilex, const char *envsec { if (islower((utf8_t)*p)) *p &= ~0x20; else if (isspace((utf8_t)*p)) + { memmove(p, p + 1, strlen(p)); + p--; + } else if (*p == '=') { p++; From f506a3f5cdc9342fd35e3dd452aa8bbf84bbde25 Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Thu, 9 Jan 2014 15:50:42 +0100 Subject: [PATCH 2/3] man: Mention variable names are converted to uppercase --- docs/man/man5/dmd.conf.5 | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/docs/man/man5/dmd.conf.5 b/docs/man/man5/dmd.conf.5 index 3dd6ca76aa5b..b0527d160f65 100644 --- a/docs/man/man5/dmd.conf.5 +++ b/docs/man/man5/dmd.conf.5 @@ -17,9 +17,10 @@ This is handy to make dmd independent of programs with conflicting use of environment variables. .SH SYNTAX -Environment variables follow the [Environment] section -heading, in name=value pairs. Comments are lines that start -with ;. +Environment variables follow the [Environment] section heading, in +name=value pairs. Comments are lines that start with ;. Variable names +are always converted to uppercase, so if you use name=value, actually +the variable with name NAME will be written. .PP .SH EXAMPLE From ba95719f3a05476b1ec71528b27b5de627f5a478 Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Thu, 9 Jan 2014 15:25:31 +0100 Subject: [PATCH 3/3] ini: Add support to define undefined variables Similarly to Make files, the syntax `VAR ?= value` is added to define variables only when they were previously undefined. In this case, if the environment variable `VAR` was undefined, then it will be defined with the value `value`, otherwise the previous content of `VAR` will be preserved. This is useful to define default flags in the config file that can be easily overridden by using environment variables. --- docs/man/man5/dmd.conf.5 | 9 ++++++--- src/inifile.c | 23 ++++++++++++++++++++--- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/docs/man/man5/dmd.conf.5 b/docs/man/man5/dmd.conf.5 index b0527d160f65..fe5bcaefea04 100644 --- a/docs/man/man5/dmd.conf.5 +++ b/docs/man/man5/dmd.conf.5 @@ -18,9 +18,12 @@ conflicting use of environment variables. .SH SYNTAX Environment variables follow the [Environment] section heading, in -name=value pairs. Comments are lines that start with ;. Variable names -are always converted to uppercase, so if you use name=value, actually -the variable with name NAME will be written. +name=value or name?=value pairs. Comments are lines that start with ;. +Variable names are always converted to uppercase, so if you use +name=value, actually the variable with name NAME will be written. If +the name?=value syntax is used, the variable with NAME will only be +written if it was defined before (in that case the original value is +preserved). .PP .SH EXAMPLE diff --git a/src/inifile.c b/src/inifile.c index d65c75836a85..2918a0571da0 100644 --- a/src/inifile.c +++ b/src/inifile.c @@ -289,6 +289,20 @@ const char *inifile(const char *argv0x, const char *inifilex, const char *envsec memmove(p, p + 1, strlen(p)); p--; } + else if (p[0] == '?' && p[1] == '=') + { + *p = '\0'; + if (getenv(p)) + { + pn = NULL; + break; + } + // remove the '?' and resume parsing starting from + // '=' again so the regular variable format is + // parsed + memmove(p, p + 1, strlen(p + 1) + 1); + p--; + } else if (*p == '=') { p++; @@ -298,11 +312,14 @@ const char *inifile(const char *argv0x, const char *inifilex, const char *envsec } } - putenv(strdup(pn)); + if (pn) + { + putenv(strdup(pn)); #if LOG - printf("\tputenv('%s')\n", pn); - //printf("getenv(\"TEST\") = '%s'\n",getenv("TEST")); + printf("\tputenv('%s')\n", pn); + //printf("getenv(\"TEST\") = '%s'\n",getenv("TEST")); #endif + } } break; }