Skip to content

Commit

Permalink
Merge pull request #4679 from WalterBright/envsections
Browse files Browse the repository at this point in the history
fix order of evaluation of conf file
  • Loading branch information
andralex committed May 31, 2015
2 parents 78c9d39 + c9d5b51 commit b1a6f37
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 22 deletions.
67 changes: 60 additions & 7 deletions src/inifile.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "root.h"
#include "rmem.h"
#include "port.h"
#include "stringtable.h"

#define LOG 0

Expand Down Expand Up @@ -116,19 +117,72 @@ const char *findConfFile(const char *argv0, const char *inifile)
return filename;
}

/**********************************
* Read from environment, looking for cached value first.
*/

const char *readFromEnv(StringTable *environment, const char *name)
{
size_t len = strlen(name);
StringValue *sv = environment->lookup(name, len);
if (sv)
return (const char *)sv->ptrvalue; // get cached value

return getenv(name);
}

/*********************************
* Write to our copy of the environment, not the real environment
*/

static void writeToEnv(StringTable *environment, char *nameEqValue)
{
char *p = strchr(nameEqValue, '=');
assert(p);
StringValue *sv = environment->insert(nameEqValue, p - nameEqValue);
sv->ptrvalue = (void *)(p + 1);
}

/************************************
* Update real enviroment with our copy.
*/

static int envput(StringValue *sv)
{
const char *name = sv->toDchars();
size_t namelen = strlen(name);
const char *value = (const char *)sv->ptrvalue;
size_t valuelen = strlen(value);
char *s = (char *)malloc(namelen + 1 + valuelen + 1);
assert(s);
memcpy(s, name, namelen);
s[namelen] = '=';
memcpy(s + namelen + 1, value, valuelen);
s[namelen + 1 + valuelen] = 0;
//printf("envput('%s')\n", s);
putenv(s);

return 0; // do all of them
}

void updateRealEnvironment(StringTable *environment)
{
environment->apply(&envput);
}

/*****************************
* Read and analyze .ini file.
* Write the entries into the process environment as
* Write the entries into environment as
* well as any entries in one of the specified section(s).
*
* Params:
* environment = our own cache of the program environment
* path = what @P will expand to
* buffer[len] = contents of configuration file
* sections[] = section namesdimension of array of section names
*/
void parseConfFile(const char *path, size_t length, unsigned char *buffer, Strings *sections)
void parseConfFile(StringTable *environment, const char *path, size_t length, unsigned char *buffer, Strings *sections)
{

// Parse into lines
bool envsection = true; // default is to read

Expand Down Expand Up @@ -198,7 +252,7 @@ void parseConfFile(const char *path, size_t length, unsigned char *buffer, Strin
memcpy(p, &line[k + 1], len2);
p[len2] = 0;
Port::strupr(p);
char *penv = getenv(p);
const char *penv = readFromEnv(environment, p);
if (penv)
buf.writestring(penv);
free(p);
Expand Down Expand Up @@ -280,7 +334,7 @@ void parseConfFile(const char *path, size_t length, unsigned char *buffer, Strin
else if (p[0] == '?' && p[1] == '=')
{
*p = '\0';
if (getenv(pn))
if (readFromEnv(environment, pn))
{
pn = NULL;
break;
Expand All @@ -302,7 +356,7 @@ void parseConfFile(const char *path, size_t length, unsigned char *buffer, Strin

if (pn)
{
putenv(strdup(pn));
writeToEnv(environment, strdup(pn));
#if LOG
printf("\tputenv('%s')\n", pn);
//printf("getenv(\"TEST\") = '%s'\n",getenv("TEST"));
Expand All @@ -312,7 +366,6 @@ void parseConfFile(const char *path, size_t length, unsigned char *buffer, Strin
break;
}
}
return;
}

/********************
Expand Down
6 changes: 6 additions & 0 deletions src/magicport.json
Original file line number Diff line number Diff line change
Expand Up @@ -1696,6 +1696,7 @@
"root.filename",
"root.outbuffer",
"root.port",
"root.stringtable",
"core.sys.windows.windows",
"core.sys.posix.stdlib"
],
Expand All @@ -1707,6 +1708,10 @@
"members" :
[
"function findConfFile",
"function readFromEnv",
"function writeToEnv",
"function envput",
"function updateRealEnvironment",
"function parseConfFile",
"function skipspace"
]
Expand Down Expand Up @@ -2509,6 +2514,7 @@
"root.outbuffer",
"root.rmem",
"root.response",
"root.stringtable",
"target",
"tokens",
"traits"
Expand Down
40 changes: 25 additions & 15 deletions src/mars.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "target.h"
#include "file.h"
#include "filename.h"
#include "stringtable.h"

#include "mars.h"
#include "module.h"
Expand All @@ -45,7 +46,7 @@ bool response_expand(Strings *arguments);


void browse(const char *url);
void getenv_setargv(const char *envvar, Strings *args);
void getenv_setargv(const char *envvalue, Strings *args);

void printCtfePerformanceStats();

Expand All @@ -60,8 +61,12 @@ void initTraitsStringTable();
int runLINK();
void deleteExeFile();
int runProgram();

// inifile.c
const char *findConfFile(const char *argv0, const char *inifile);
void parseConfFile(const char *path, size_t len, unsigned char *buffer, Strings *sections);
const char *readFromEnv(StringTable *environment, const char *name);
void updateRealEnvironment(StringTable *environment);
void parseConfFile(StringTable *environment, const char *path, size_t len, unsigned char *buffer, Strings *sections);

void genObjFile(Module *m, bool multiobj);
void genhelpers(Module *m, bool iscomdat);
Expand Down Expand Up @@ -411,27 +416,33 @@ int tryMain(size_t argc, const char *argv[])

Strings sections;

StringTable environment;
environment._init(7);

/* Read the [Environment] section, so we can later
* pick up any DFLAGS settings.
*/
sections.push("Environment");
parseConfFile(inifilepath, inifile.len, inifile.buffer, &sections);
parseConfFile(&environment, inifilepath, inifile.len, inifile.buffer, &sections);

Strings dflags;
getenv_setargv("DFLAGS", &dflags);
getenv_setargv(readFromEnv(&environment, "DFLAGS"), &dflags);
environment.reset(7); // erase cached environment updates

const char *arch = global.params.is64bit ? "64" : "32"; // use default
arch = parse_arch_arg(&arguments, arch);
arch = parse_arch_arg(&dflags, arch);
bool is64bit = arch[0] == '6';

sections.setDim(0);
char envsection[80];
sprintf(envsection, "Environment%s", arch);
sections.push(envsection);
parseConfFile(inifilepath, inifile.len, inifile.buffer, &sections);
parseConfFile(&environment, inifilepath, inifile.len, inifile.buffer, &sections);

getenv_setargv("DFLAGS", &arguments);
getenv_setargv(readFromEnv(&environment, "DFLAGS"), &arguments);

updateRealEnvironment(&environment);
environment.reset(1); // don't need environment cache any more

#if 0
for (size_t i = 0; i < arguments.dim; i++)
Expand Down Expand Up @@ -1757,24 +1768,23 @@ int main(int argc, const char *argv[])


/***********************************
* Parse and append contents of environment variable envvar
* to args[].
* Parse and append contents of command line string envvalue to args[].
* The string is separated into arguments, processing \ and ".
*/

void getenv_setargv(const char *envvar, Strings *args)
void getenv_setargv(const char *envvalue, Strings *args)
{
if (!envvalue)
return;

char *p;

int instring;
int slash;
char c;

char *env = getenv(envvar);
if (!env)
return;

env = mem.xstrdup(env); // create our own writable copy
char *env = mem.xstrdup(envvalue); // create our own writable copy
//printf("env = '%s'\n", env);

while (1)
{
Expand Down
23 changes: 23 additions & 0 deletions src/root/stringtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,26 @@ void StringTable::grow()
}
mem.xfree(otab);
}

/********************************
* Walk the contents of the string table,
* calling fp for each entry.
* Params:
* fp = function to call. Returns !=0 to stop
* Returns:
* last return value of fp call
*/
int StringTable::apply(int (*fp)(StringValue *))
{
for (size_t i = 0; i < tabledim; ++i)
{
StringEntry *se = &table[i];
if (!se->vptr) continue;
StringValue *sv = getValue(se->vptr);
int result = (*fp)(sv);
if (result)
return result;
}
return 0;
}

1 change: 1 addition & 0 deletions src/root/stringtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ struct StringTable
StringValue *lookup(const char *s, size_t len);
StringValue *insert(const char *s, size_t len);
StringValue *update(const char *s, size_t len);
int apply(int (*fp)(StringValue *));

private:
uint32_t allocValue(const char *p, size_t length);
Expand Down

0 comments on commit b1a6f37

Please sign in to comment.