Skip to content

Commit

Permalink
Config file should be read once, not twice
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright committed May 18, 2015
1 parent 484447b commit f492177
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
Expand Up @@ -122,41 +122,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 @@ -181,8 +173,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
Expand Up @@ -24,6 +24,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 @@ -59,7 +61,7 @@ int runLINK();
void deleteExeFile();
int runProgram();
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);

void genObjFile(Module *m, bool multiobj);
void genhelpers(Module *m, bool iscomdat);
Expand Down Expand Up @@ -399,13 +401,21 @@ int tryMain(size_t argc, const char *argv[])
#endif
}

// 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 @@ -419,7 +429,7 @@ int tryMain(size_t argc, 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 f492177

Please sign in to comment.