Skip to content

Commit

Permalink
Merge pull request #4646 from WalterBright/EnvSections
Browse files Browse the repository at this point in the history
generalize parseConfFile() so it can handle multiple sections
  • Loading branch information
yebblies committed May 14, 2015
2 parents 9d5cba7 + 706c2c4 commit 36ba62d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 18 deletions.
51 changes: 36 additions & 15 deletions src/inifile.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Some portions copyright (c) 1994-1995 by Symantec
* Copyright (c) 1999-2013 by Digital Mars
* Copyright (c) 1999-2015 by Digital Mars
* All Rights Reserved
* http://www.digitalmars.com
* Written by Walter Bright
Expand Down Expand Up @@ -117,13 +117,15 @@ const char *findConfFile(const char *argv0, const char *inifile)
}

/*****************************
* Read and analyze .ini file, i.e. write the entries of the specified section
* into the process environment
* Input:
* filename path to config file
* envsectionname name of the section to process
* Read and analyze .ini file.
* Write the entries into the process environment as
* well as any entries in one of the specified section(s).
*
* Params:
* filename = path to config file
* sections[] = section namesdimension of array of section names
*/
void parseConfFile(const char *filename, const char *envsectionname)
void parseConfFile(const char *filename, Strings *sections)
{
const char *path = FileName::path(filename); // need path for @P macro
#if LOG
Expand All @@ -136,8 +138,8 @@ void parseConfFile(const char *filename, const char *envsectionname)
return; // error reading file

// Parse into lines
bool envsection = true;
size_t envsectionnamelen = strlen(envsectionname);
bool envsection = true; // default is to read

OutBuffer buf;
bool eof = false;
for (size_t i = 0; i < file.len && !eof; i++)
Expand Down Expand Up @@ -238,14 +240,33 @@ void parseConfFile(const char *filename, const char *envsectionname)
char *pn;
for (pn = p; isalnum((utf8_t)*pn); pn++)
;
if (pn - p == envsectionnamelen &&
Port::memicmp(p, envsectionname, envsectionnamelen) == 0 &&
*skipspace(pn) == ']')

if (*skipspace(pn) != ']')
{
envsection = true;
}
else
// malformed [sectionname], so just say we're not in a section
envsection = false;
break;
}

/* Seach sectionnamev[] for p..pn and set envsection to true if it's there
*/
for (size_t j = 0; 1; ++j)
{
if (j == sections->dim)
{
// Didn't find it
envsection = false;
break;
}
const char *sectionname = (*sections)[j];
size_t len = strlen(sectionname);
if (pn - p == len &&
Port::memicmp(p, sectionname, len) == 0)
{
envsection = true;
break;
}
}
break;

default:
Expand Down
15 changes: 12 additions & 3 deletions src/mars.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ int runLINK();
void deleteExeFile();
int runProgram();
const char *findConfFile(const char *argv0, const char *inifile);
void parseConfFile(const char *filename, const char* envsectionname);
void parseConfFile(const char *filename, Strings *sections);

void genObjFile(Module *m, bool multiobj);
void genhelpers(Module *m, bool iscomdat);
Expand Down Expand Up @@ -391,7 +391,14 @@ int tryMain(size_t argc, const char *argv[])
#error "fix this"
#endif
}
parseConfFile(global.inifilename, "Environment");

Strings sections;

/* Read the [Environment] section, so we can later
* pick up any DFLAGS settings.
*/
sections.push("Environment");
parseConfFile(global.inifilename, &sections);

size_t dflags_argc = 0;
const char** dflags_argv = NULL;
Expand All @@ -402,9 +409,11 @@ int tryMain(size_t argc, const char *argv[])
arch = parse_arch_arg(dflags_argc, dflags_argv, arch);
bool is64bit = arch[0] == '6';

sections.setDim(0);
char envsection[80];
sprintf(envsection, "Environment%s", arch);
parseConfFile(global.inifilename, envsection);
sections.push(envsection);
parseConfFile(global.inifilename, &sections);

getenv_setargv("DFLAGS", &argc, &argv);

Expand Down

0 comments on commit 36ba62d

Please sign in to comment.