Skip to content

Commit

Permalink
Merge pull request #4668 from WalterBright/twicebaked
Browse files Browse the repository at this point in the history
Config file should be read once, not twice
  • Loading branch information
andralex authored and WalterBright committed May 20, 2015
1 parent 51185a8 commit 325c770
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
26 changes: 9 additions & 17 deletions src/inifile.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,41 +125,33 @@ const char *findConfFile(const char *argv0, const char *inifile)
* well as any entries in one of the specified section(s).
*
* Params:
* filename = path to config file
* path = what @P will expand to
* buffer[len] = contents of configuration file
* sections[] = section namesdimension of array of section names
*/
void parseConfFile(const char *filename, Strings *sections)
void parseConfFile(const char *path, size_t length, unsigned char *buffer, Strings *sections)
{
const char *path = FileName::path(filename); // need path for @P macro
#if LOG
printf("\tpath = '%s', filename = '%s'\n", path, filename);
#endif

File file(filename);

if (file.read())
return; // error reading file

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

OutBuffer buf;
bool eof = false;
for (size_t i = 0; i < file.len && !eof; i++)
for (size_t i = 0; i < length && !eof; i++)
{
Lstart:
size_t linestart = i;

for (; i < file.len; i++)
for (; i < length; i++)
{
switch (file.buffer[i])
switch (buffer[i])
{
case '\r':
break;

case '\n':
// Skip if it was preceded by '\r'
if (i && file.buffer[i - 1] == '\r')
if (i && buffer[i - 1] == '\r')
{
i++;
goto Lstart;
Expand All @@ -184,8 +176,8 @@ void parseConfFile(const char *filename, Strings *sections)

for (size_t k = 0; k < i - linestart; k++)
{
// The line is file.buffer[linestart..i]
char *line = (char *)&file.buffer[linestart];
// The line is buffer[linestart..i]
char *line = (char *)&buffer[linestart];
if (line[k] == '%')
{
for (size_t j = k + 1; j < i - linestart; j++)
Expand Down
16 changes: 13 additions & 3 deletions src/mars.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "root.h"
#include "async.h"
#include "target.h"
#include "file.h"
#include "filename.h"

#include "mars.h"
#include "module.h"
Expand Down Expand Up @@ -52,7 +54,7 @@ static const char* parse_arch_arg(Strings *args, const char* arch);
static const char* parse_conf_arg(Strings *args);

const char *findConfFile(const char *argv0, const char *inifile);
void parseConfFile(const char *filename, Strings *sections);
void parseConfFile(const char *path, size_t len, unsigned char *buffer, Strings *sections);


FILE *stdmsg;
Expand Down Expand Up @@ -574,13 +576,21 @@ int main(int iargc, const char *argv[])
}
}

// Read the configurarion file
File inifile(global.inifilename);
inifile.read();

/* Need path of configuration file, for use in expanding @P macro
*/
const char *inifilepath = FileName::path(global.inifilename);

Strings sections;

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

Strings dflags;
getenv_setargv("DFLAGS", &dflags);
Expand All @@ -594,7 +604,7 @@ int main(int iargc, const char *argv[])
char envsection[80];
sprintf(envsection, "Environment%s", arch);
sections.push(envsection);
parseConfFile(global.inifilename, &sections);
parseConfFile(inifilepath, inifile.len, inifile.buffer, &sections);

getenv_setargv("DFLAGS", &arguments);

Expand Down

0 comments on commit 325c770

Please sign in to comment.