| 1 |
#include <iostream> |
|---|
| 2 |
#include <string> |
|---|
| 3 |
#include <cassert> |
|---|
| 4 |
#include <cstring> |
|---|
| 5 |
|
|---|
| 6 |
#include "llvm/System/Path.h" |
|---|
| 7 |
|
|---|
| 8 |
#include "libconfig.h++" |
|---|
| 9 |
|
|---|
| 10 |
#include "gen/configfile.h" |
|---|
| 11 |
|
|---|
| 12 |
#include "mars.h" |
|---|
| 13 |
|
|---|
| 14 |
namespace sys = llvm::sys; |
|---|
| 15 |
|
|---|
| 16 |
ConfigFile::ConfigFile() |
|---|
| 17 |
{ |
|---|
| 18 |
cfg = new libconfig::Config; |
|---|
| 19 |
} |
|---|
| 20 |
|
|---|
| 21 |
ConfigFile::~ConfigFile() |
|---|
| 22 |
{ |
|---|
| 23 |
delete cfg; |
|---|
| 24 |
} |
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
bool ConfigFile::locate(sys::Path& p, const char* argv0, void* mainAddr, const char* filename) |
|---|
| 28 |
{ |
|---|
| 29 |
// temporary configuration |
|---|
| 30 |
|
|---|
| 31 |
// try the current working dir |
|---|
| 32 |
p = sys::Path::GetCurrentDirectory(); |
|---|
| 33 |
p.appendComponent(filename); |
|---|
| 34 |
if (p.exists()) |
|---|
| 35 |
return true; |
|---|
| 36 |
|
|---|
| 37 |
// user configuration |
|---|
| 38 |
|
|---|
| 39 |
// try ~/.ldc |
|---|
| 40 |
p = sys::Path::GetUserHomeDirectory(); |
|---|
| 41 |
p.appendComponent(".ldc"); |
|---|
| 42 |
p.appendComponent(filename); |
|---|
| 43 |
if (p.exists()) |
|---|
| 44 |
return true; |
|---|
| 45 |
|
|---|
| 46 |
#if _WIN32 |
|---|
| 47 |
// try home dir |
|---|
| 48 |
p = sys::Path::GetUserHomeDirectory(); |
|---|
| 49 |
p.appendComponent(filename); |
|---|
| 50 |
if (p.exists()) |
|---|
| 51 |
return true; |
|---|
| 52 |
#endif |
|---|
| 53 |
|
|---|
| 54 |
// system configuration |
|---|
| 55 |
|
|---|
| 56 |
#if _WIN32 |
|---|
| 57 |
// try the install-prefix |
|---|
| 58 |
p = sys::Path(LDC_INSTALL_PREFIX); |
|---|
| 59 |
p.appendComponent(filename); |
|---|
| 60 |
if (p.exists()) |
|---|
| 61 |
return true; |
|---|
| 62 |
#else |
|---|
| 63 |
// try the install-prefix/etc |
|---|
| 64 |
p = sys::Path(LDC_INSTALL_PREFIX); |
|---|
| 65 |
p.appendComponent("etc"); |
|---|
| 66 |
p.appendComponent(filename); |
|---|
| 67 |
if (p.exists()) |
|---|
| 68 |
return true; |
|---|
| 69 |
|
|---|
| 70 |
// try the install-prefix/etc/ldc |
|---|
| 71 |
p = sys::Path(LDC_INSTALL_PREFIX); |
|---|
| 72 |
p.appendComponent("etc"); |
|---|
| 73 |
p.appendComponent("ldc"); |
|---|
| 74 |
p.appendComponent(filename); |
|---|
| 75 |
if (p.exists()) |
|---|
| 76 |
return true; |
|---|
| 77 |
|
|---|
| 78 |
// try /etc (absolute path) |
|---|
| 79 |
p = sys::Path("/etc"); |
|---|
| 80 |
p.appendComponent(filename); |
|---|
| 81 |
if (p.exists()) |
|---|
| 82 |
return true; |
|---|
| 83 |
|
|---|
| 84 |
// try /etc/ldc (absolute path) |
|---|
| 85 |
p = sys::Path("/etc/ldc"); |
|---|
| 86 |
p.appendComponent(filename); |
|---|
| 87 |
if (p.exists()) |
|---|
| 88 |
return true; |
|---|
| 89 |
#endif |
|---|
| 90 |
|
|---|
| 91 |
// try next to the executable |
|---|
| 92 |
p = sys::Path::GetMainExecutable(argv0, mainAddr); |
|---|
| 93 |
p.eraseComponent(); |
|---|
| 94 |
p.appendComponent(filename); |
|---|
| 95 |
if (p.exists()) |
|---|
| 96 |
return true; |
|---|
| 97 |
|
|---|
| 98 |
return false; |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
bool ConfigFile::read(const char* argv0, void* mainAddr, const char* filename) |
|---|
| 102 |
{ |
|---|
| 103 |
sys::Path p; |
|---|
| 104 |
if (!locate(p, argv0, mainAddr, filename)) |
|---|
| 105 |
{ |
|---|
| 106 |
// failed to find cfg, users still have the DFLAGS environment var |
|---|
| 107 |
std::cerr << "Error failed to locate the configuration file: " << filename << std::endl; |
|---|
| 108 |
return false; |
|---|
| 109 |
} |
|---|
| 110 |
|
|---|
| 111 |
// save config file path for -v output |
|---|
| 112 |
pathstr = p.str(); |
|---|
| 113 |
|
|---|
| 114 |
try |
|---|
| 115 |
{ |
|---|
| 116 |
// read the cfg |
|---|
| 117 |
cfg->readFile(p.c_str()); |
|---|
| 118 |
|
|---|
| 119 |
// make sure there's a default group |
|---|
| 120 |
if (!cfg->exists("default")) |
|---|
| 121 |
{ |
|---|
| 122 |
std::cerr << "no default settings in configuration file" << std::endl; |
|---|
| 123 |
return false; |
|---|
| 124 |
} |
|---|
| 125 |
libconfig::Setting& root = cfg->lookup("default"); |
|---|
| 126 |
if (!root.isGroup()) |
|---|
| 127 |
{ |
|---|
| 128 |
std::cerr << "default is not a group" << std::endl; |
|---|
| 129 |
return false; |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
// handle switches |
|---|
| 133 |
if (root.exists("switches")) |
|---|
| 134 |
{ |
|---|
| 135 |
std::string binpathkey = "%%ldcbinarypath%%"; |
|---|
| 136 |
|
|---|
| 137 |
std::string binpath = sys::Path::GetMainExecutable(argv0, mainAddr).getDirname(); |
|---|
| 138 |
|
|---|
| 139 |
libconfig::Setting& arr = cfg->lookup("default.switches"); |
|---|
| 140 |
int len = arr.getLength(); |
|---|
| 141 |
for (int i=0; i<len; i++) |
|---|
| 142 |
{ |
|---|
| 143 |
std::string v = arr[i]; |
|---|
| 144 |
|
|---|
| 145 |
// replace binpathkey with binpath |
|---|
| 146 |
size_t p; |
|---|
| 147 |
while (std::string::npos != (p = v.find(binpathkey))) |
|---|
| 148 |
v.replace(p, binpathkey.size(), binpath); |
|---|
| 149 |
|
|---|
| 150 |
switches.push_back(strdup(v.c_str())); |
|---|
| 151 |
} |
|---|
| 152 |
} |
|---|
| 153 |
|
|---|
| 154 |
} |
|---|
| 155 |
catch(libconfig::FileIOException& fioe) |
|---|
| 156 |
{ |
|---|
| 157 |
std::cerr << "Error reading configuration file: " << filename << std::endl; |
|---|
| 158 |
return false; |
|---|
| 159 |
} |
|---|
| 160 |
catch(libconfig::ParseException& pe) |
|---|
| 161 |
{ |
|---|
| 162 |
std::cerr << "Error parsing configuration file: " << filename |
|---|
| 163 |
<< "(" << pe.getLine() << "): " << pe.getError() << std::endl; |
|---|
| 164 |
return false; |
|---|
| 165 |
} |
|---|
| 166 |
catch(...) |
|---|
| 167 |
{ |
|---|
| 168 |
std::cerr << "Unknown exception caught!" << std::endl; |
|---|
| 169 |
return false; |
|---|
| 170 |
} |
|---|
| 171 |
|
|---|
| 172 |
return true; |
|---|
| 173 |
} |
|---|