3 local setmetatable, loadfile, pcall, rawget, rawset, io =
4 setmetatable, loadfile, pcall, rawget, rawset, io;
9 local config = { ["*"] = { core = {} } };
11 local global_config = config["*"];
13 -- When host not found, use global
14 setmetatable(config, { __index = function () return global_config; end});
15 local host_mt = { __index = global_config };
17 -- When key not found in section, check key in global's section
18 function section_mt(section_name)
19 return { __index = function (t, k)
20 local section = rawget(global_config, section_name);
21 if not section then return nil; end
30 function get(host, section, key)
31 local sec = config[host][section];
38 function set(host, section, key, value)
39 if host and section and key then
40 local hostconfig = rawget(config, host);
41 if not hostconfig then
42 hostconfig = rawset(config, host, setmetatable({}, host_mt))[host];
44 if not rawget(hostconfig, section) then
45 hostconfig[section] = setmetatable({}, section_mt(section));
47 hostconfig[section][key] = value;
53 function load(filename, format)
54 format = format or filename:match("%w+$");
55 if parsers[format] and parsers[format].load then
56 local f = io.open(filename);
58 local ok, err = parsers[format].load(f:read("*a"));
64 return nil, "no parser specified";
66 return false, "no parser";
70 function save(filename, format)
73 function addparser(format, parser)
74 if format and parser then
75 parsers[format] = parser;
79 -- Built-in Lua parser
81 local loadstring, pcall, setmetatable = _G.loadstring, _G.pcall, _G.setmetatable;
82 local setfenv, rawget, tostring = _G.setfenv, _G.rawget, _G.tostring;
84 function parsers.lua.load(data)
86 env = setmetatable({ Host = true; host = true; }, { __index = function (t, k)
87 return rawget(_G, k) or
88 function (settings_table)
89 config[__currenthost or "*"][k] = settings_table;
92 __newindex = function (t, k, v)
93 set(env.__currenthost or "*", "core", k, v);
96 function env.Host(name)
97 rawset(env, "__currenthost", name);
98 set(name or "*", "core", "defined", true);
102 local chunk, err = loadstring(data);
110 local ok, err = pcall(chunk);