Skip to content

Commit

Permalink
Merge pull request #5017 from luismarques/fix_dmdconf_assert
Browse files Browse the repository at this point in the history
Fix dmdconf assert
  • Loading branch information
WalterBright committed Sep 13, 2015
2 parents 16969dc + 31b2a18 commit ad502a1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
43 changes: 31 additions & 12 deletions src/dinifile.d
Expand Up @@ -8,8 +8,18 @@

module ddmd.dinifile;

import core.stdc.ctype, core.stdc.stdlib, core.stdc.string, core.sys.posix.stdlib, core.sys.windows.windows;
import ddmd.globals, ddmd.root.file, ddmd.root.filename, ddmd.root.outbuffer, ddmd.root.port, ddmd.root.stringtable;
import core.stdc.ctype;
import core.stdc.stdlib;
import core.stdc.string;
import core.sys.posix.stdlib;
import core.sys.windows.windows;
import ddmd.errors;
import ddmd.globals;
import ddmd.root.file;
import ddmd.root.filename;
import ddmd.root.outbuffer;
import ddmd.root.port;
import ddmd.root.stringtable;

version (Windows) extern (C) int putenv(const char*);
private enum LOG = false;
Expand All @@ -23,7 +33,7 @@ private enum LOG = false;
* file path of the config file or NULL
* Note: this is a memory leak
*/
extern (C++) const(char)* findConfFile(const(char)* argv0, const(char)* inifile)
const(char)* findConfFile(const(char)* argv0, const(char)* inifile)
{
static if (LOG)
{
Expand Down Expand Up @@ -91,7 +101,7 @@ extern (C++) const(char)* findConfFile(const(char)* argv0, const(char)* inifile)
/**********************************
* Read from environment, looking for cached value first.
*/
extern (C++) const(char)* readFromEnv(StringTable* environment, const(char)* name)
const(char)* readFromEnv(StringTable* environment, const(char)* name)
{
size_t len = strlen(name);
StringValue* sv = environment.lookup(name, len);
Expand All @@ -103,18 +113,20 @@ extern (C++) const(char)* readFromEnv(StringTable* environment, const(char)* nam
/*********************************
* Write to our copy of the environment, not the real environment
*/
extern (C++) static void writeToEnv(StringTable* environment, char* nameEqValue)
private bool writeToEnv(StringTable* environment, char* nameEqValue)
{
char* p = strchr(nameEqValue, '=');
assert(p);
if (!p)
return false;
StringValue* sv = environment.update(nameEqValue, p - nameEqValue);
sv.ptrvalue = cast(void*)(p + 1);
return true;
}

/************************************
* Update real enviroment with our copy.
*/
extern (C++) static int envput(StringValue* sv)
extern (C++) private int envput(StringValue* sv)
{
const(char)* name = sv.toDchars();
size_t namelen = strlen(name);
Expand All @@ -131,7 +143,7 @@ extern (C++) static int envput(StringValue* sv)
return 0; // do all of them
}

extern (C++) void updateRealEnvironment(StringTable* environment)
void updateRealEnvironment(StringTable* environment)
{
environment.apply(&envput);
}
Expand All @@ -143,16 +155,18 @@ extern (C++) void updateRealEnvironment(StringTable* environment)
*
* Params:
* environment = our own cache of the program environment
* filename = name of the file being parsed
* path = what @P will expand to
* buffer[len] = contents of configuration file
* sections[] = section namesdimension of array of section names
* sections[] = section names
*/
extern (C++) void parseConfFile(StringTable* environment, const(char)* path, size_t length, ubyte* buffer, Strings* sections)
void parseConfFile(StringTable* environment, const(char)* filename, const(char)* path, size_t length, ubyte* buffer, Strings* sections)
{
// Parse into lines
bool envsection = true; // default is to read
OutBuffer buf;
bool eof = false;
int lineNum = 0;
for (size_t i = 0; i < length && !eof; i++)
{
Lstart:
Expand Down Expand Up @@ -180,6 +194,7 @@ extern (C++) void parseConfFile(StringTable* environment, const(char)* path, siz
}
break;
}
++lineNum;
buf.reset();
// First, expand the macros.
// Macros are bracketed by % characters.
Expand Down Expand Up @@ -306,7 +321,11 @@ extern (C++) void parseConfFile(StringTable* environment, const(char)* path, siz
}
if (pn)
{
writeToEnv(environment, strdup(pn));
if (!writeToEnv(environment, strdup(pn)))
{
error(Loc(filename, lineNum, 0), "Use 'NAME=value' syntax, not '%s'", pn);
fatal();
}
static if (LOG)
{
printf("\tputenv('%s')\n", pn);
Expand All @@ -322,7 +341,7 @@ extern (C++) void parseConfFile(StringTable* environment, const(char)* path, siz
/********************
* Skip spaces.
*/
extern (C++) char* skipspace(char* p)
private char* skipspace(char* p)
{
while (isspace(cast(char)*p))
p++;
Expand Down
4 changes: 2 additions & 2 deletions src/mars.d
Expand Up @@ -411,7 +411,7 @@ extern (C++) int tryMain(size_t argc, const(char)** argv)
* pick up any DFLAGS settings.
*/
sections.push("Environment");
parseConfFile(&environment, inifilepath, inifile.len, inifile.buffer, &sections);
parseConfFile(&environment, global.inifilename, inifilepath, inifile.len, inifile.buffer, &sections);
Strings dflags;
getenv_setargv(readFromEnv(&environment, "DFLAGS"), &dflags);
environment.reset(7); // erase cached environment updates
Expand All @@ -422,7 +422,7 @@ extern (C++) int tryMain(size_t argc, const(char)** argv)
char[80] envsection;
sprintf(envsection.ptr, "Environment%s", arch);
sections.push(envsection.ptr);
parseConfFile(&environment, inifilepath, inifile.len, inifile.buffer, &sections);
parseConfFile(&environment, global.inifilename, inifilepath, inifile.len, inifile.buffer, &sections);
getenv_setargv(readFromEnv(&environment, "DFLAGS"), &arguments);
updateRealEnvironment(&environment);
environment.reset(1); // don't need environment cache any more
Expand Down

0 comments on commit ad502a1

Please sign in to comment.