core/configmanager.lua
author Waqas Hussain <waqas20@gmail.com>
Wed, 05 Jan 2011 06:16:07 +0500
changeset 4001 2e8411f6cb14
parent 3929 7cb03d67101b
child 4358 86be454168fb
permissions -rw-r--r--
configmanager: Added rawget().
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 1504
diff changeset
     1
-- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2862
diff changeset
     2
-- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2862
diff changeset
     3
-- Copyright (C) 2008-2010 Waqas Hussain
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 466
diff changeset
     4
-- 
758
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 750
diff changeset
     5
-- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 750
diff changeset
     6
-- COPYING file in the source package for more information.
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 466
diff changeset
     7
--
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 466
diff changeset
     8
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
local _G = _G;
3905
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
    10
local setmetatable, loadfile, pcall, rawget, rawset, io, error, dofile, type, pairs, table =
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
    11
      setmetatable, loadfile, pcall, rawget, rawset, io, error, dofile, type, pairs, table;
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
    12
local format, math_max = string.format, math.max;
2861
1402615b66f8 configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
    13
3384
b7600dd7cd42 configmanager: Remove dependency on eventmanager, and global 'prosody' object
Matthew Wild <mwild1@gmail.com>
parents: 3012
diff changeset
    14
local fire_event = prosody and prosody.events.fire_event or function () end;
1000
a73715a9267f core.configmanager: Fire event when (re)loading config file
Matthew Wild <mwild1@gmail.com>
parents: 913
diff changeset
    15
3905
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
    16
local lfs = require "lfs";
3609
954b1159f2f3 prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents: 3573
diff changeset
    17
local path_sep = package.config:sub(1,1);
954b1159f2f3 prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents: 3573
diff changeset
    18
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
module "configmanager"
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
local parsers = {};
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    22
3573
f31fa6520a4b configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents: 3515
diff changeset
    23
local config_mt = { __index = function (t, k) return rawget(t, "*"); end};
f31fa6520a4b configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents: 3515
diff changeset
    24
local config = setmetatable({ ["*"] = { core = {} } }, config_mt);
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
-- When host not found, use global
3573
f31fa6520a4b configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents: 3515
diff changeset
    27
local host_mt = { };
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
-- When key not found in section, check key in global's section
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
function section_mt(section_name)
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
	return { __index = 	function (t, k)
3573
f31fa6520a4b configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents: 3515
diff changeset
    32
					local section = rawget(config["*"], section_name);
2974
f37dc6f038f3 configmanager: Fix some wacky indentation
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    33
					if not section then return nil; end
f37dc6f038f3 configmanager: Fix some wacky indentation
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    34
					return section[k];
f37dc6f038f3 configmanager: Fix some wacky indentation
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    35
				end
f37dc6f038f3 configmanager: Fix some wacky indentation
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    36
	};
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    37
end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    38
376
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
    39
function getconfig()
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
    40
	return config;
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
    41
end
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
    42
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    43
function get(host, section, key)
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    44
	local sec = config[host][section];
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    45
	if sec then
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    46
		return sec[key];
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    47
	end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    48
	return nil;
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    49
end
4001
2e8411f6cb14 configmanager: Added rawget().
Waqas Hussain <waqas20@gmail.com>
parents: 3929
diff changeset
    50
function _M.rawget(host, section, key)
2e8411f6cb14 configmanager: Added rawget().
Waqas Hussain <waqas20@gmail.com>
parents: 3929
diff changeset
    51
	local hostconfig = rawget(config, host);
2e8411f6cb14 configmanager: Added rawget().
Waqas Hussain <waqas20@gmail.com>
parents: 3929
diff changeset
    52
	if hostconfig then
2e8411f6cb14 configmanager: Added rawget().
Waqas Hussain <waqas20@gmail.com>
parents: 3929
diff changeset
    53
		local sectionconfig = rawget(hostconfig, section);
2e8411f6cb14 configmanager: Added rawget().
Waqas Hussain <waqas20@gmail.com>
parents: 3929
diff changeset
    54
		if sectionconfig then
2e8411f6cb14 configmanager: Added rawget().
Waqas Hussain <waqas20@gmail.com>
parents: 3929
diff changeset
    55
			return rawget(sectionconfig, key);
2e8411f6cb14 configmanager: Added rawget().
Waqas Hussain <waqas20@gmail.com>
parents: 3929
diff changeset
    56
		end
2e8411f6cb14 configmanager: Added rawget().
Waqas Hussain <waqas20@gmail.com>
parents: 3929
diff changeset
    57
	end
2e8411f6cb14 configmanager: Added rawget().
Waqas Hussain <waqas20@gmail.com>
parents: 3929
diff changeset
    58
end
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    59
3573
f31fa6520a4b configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents: 3515
diff changeset
    60
local function set(config, host, section, key, value)
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    61
	if host and section and key then
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    62
		local hostconfig = rawget(config, host);
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    63
		if not hostconfig then
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    64
			hostconfig = rawset(config, host, setmetatable({}, host_mt))[host];
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    65
		end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    66
		if not rawget(hostconfig, section) then
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    67
			hostconfig[section] = setmetatable({}, section_mt(section));
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    68
		end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    69
		hostconfig[section][key] = value;
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    70
		return true;
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    71
	end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    72
	return false;
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    73
end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    74
3573
f31fa6520a4b configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents: 3515
diff changeset
    75
function _M.set(host, section, key, value)
f31fa6520a4b configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents: 3515
diff changeset
    76
	return set(config, host, section, key, value);
f31fa6520a4b configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents: 3515
diff changeset
    77
end
f31fa6520a4b configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents: 3515
diff changeset
    78
3609
954b1159f2f3 prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents: 3573
diff changeset
    79
-- Helper function to resolve relative paths (needed by config)
954b1159f2f3 prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents: 3573
diff changeset
    80
do
954b1159f2f3 prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents: 3573
diff changeset
    81
	local rel_path_start = ".."..path_sep;
954b1159f2f3 prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents: 3573
diff changeset
    82
	function resolve_relative_path(parent_path, path)
954b1159f2f3 prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents: 3573
diff changeset
    83
		if path then
3905
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
    84
			-- Some normalization
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
    85
			parent_path = parent_path:gsub("%"..path_sep.."+$", "");
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
    86
			path = path:gsub("^%.%"..path_sep.."+", "");
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
    87
			
3609
954b1159f2f3 prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents: 3573
diff changeset
    88
			local is_relative;
954b1159f2f3 prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents: 3573
diff changeset
    89
			if path_sep == "/" and path:sub(1,1) ~= "/" then
954b1159f2f3 prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents: 3573
diff changeset
    90
				is_relative = true;
954b1159f2f3 prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents: 3573
diff changeset
    91
			elseif path_sep == "\\" and (path:sub(1,1) ~= "/" and path:sub(2,3) ~= ":\\") then
954b1159f2f3 prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents: 3573
diff changeset
    92
				is_relative = true;
954b1159f2f3 prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents: 3573
diff changeset
    93
			end
954b1159f2f3 prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents: 3573
diff changeset
    94
			if is_relative then
954b1159f2f3 prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents: 3573
diff changeset
    95
				return parent_path..path_sep..path;
954b1159f2f3 prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents: 3573
diff changeset
    96
			end
954b1159f2f3 prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents: 3573
diff changeset
    97
		end
954b1159f2f3 prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents: 3573
diff changeset
    98
		return path;
954b1159f2f3 prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents: 3573
diff changeset
    99
	end	
954b1159f2f3 prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents: 3573
diff changeset
   100
end
954b1159f2f3 prosody, configmanager, certmanager: Relocate prosody.resolve_relative_path() to configmanager, and update certmanager (the only user of this function)
Matthew Wild <mwild1@gmail.com>
parents: 3573
diff changeset
   101
3905
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   102
-- Helper function to convert a glob to a Lua pattern
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   103
local function glob_to_pattern(glob)
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   104
	return "^"..glob:gsub("[%p*?]", function (c)
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   105
		if c == "*" then
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   106
			return ".*";
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   107
		elseif c == "?" then
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   108
			return ".";
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   109
		else
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   110
			return "%"..c;
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   111
		end
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   112
	end).."$";
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   113
end
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   114
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   115
function load(filename, format)
376
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
   116
	format = format or filename:match("%w+$");
466
0ecfd89c2cc0 Fix for configmanager when config file can't be found
Matthew Wild <mwild1@gmail.com>
parents: 376
diff changeset
   117
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   118
	if parsers[format] and parsers[format].load then
466
0ecfd89c2cc0 Fix for configmanager when config file can't be found
Matthew Wild <mwild1@gmail.com>
parents: 376
diff changeset
   119
		local f, err = io.open(filename);
2552
8dda55217e83 configmanager: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2427
diff changeset
   120
		if f then
3613
f617718d2221 configmanager: Change parser API again to pass a config table to insert settings to. Fixes Include(). (Thanks Zash/answerman)
Matthew Wild <mwild1@gmail.com>
parents: 3610
diff changeset
   121
			local new_config = setmetatable({ ["*"] = { core = {} } }, config_mt);
f617718d2221 configmanager: Change parser API again to pass a config table to insert settings to. Fixes Include(). (Thanks Zash/answerman)
Matthew Wild <mwild1@gmail.com>
parents: 3610
diff changeset
   122
			local ok, err = parsers[format].load(f:read("*a"), filename, new_config);
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   123
			f:close();
3613
f617718d2221 configmanager: Change parser API again to pass a config table to insert settings to. Fixes Include(). (Thanks Zash/answerman)
Matthew Wild <mwild1@gmail.com>
parents: 3610
diff changeset
   124
			if ok then
3573
f31fa6520a4b configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents: 3515
diff changeset
   125
				config = new_config;
f31fa6520a4b configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents: 3515
diff changeset
   126
				fire_event("config-reloaded", {
f31fa6520a4b configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents: 3515
diff changeset
   127
					filename = filename,
f31fa6520a4b configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents: 3515
diff changeset
   128
					format = format,
f31fa6520a4b configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents: 3515
diff changeset
   129
					config = config
f31fa6520a4b configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents: 3515
diff changeset
   130
				});
1000
a73715a9267f core.configmanager: Fire event when (re)loading config file
Matthew Wild <mwild1@gmail.com>
parents: 913
diff changeset
   131
			end
3780
791aede977da configmanager: Switch back to returning 'ok' to signal config load success - fixes config errors not being displayed
Matthew Wild <mwild1@gmail.com>
parents: 3613
diff changeset
   132
			return ok, "parser", err;
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   133
		end
793
55add3b87c01 Report errors in the config file to the user
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
   134
		return f, "file", err;
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   135
	end
466
0ecfd89c2cc0 Fix for configmanager when config file can't be found
Matthew Wild <mwild1@gmail.com>
parents: 376
diff changeset
   136
376
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
   137
	if not format then
793
55add3b87c01 Report errors in the config file to the user
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
   138
		return nil, "file", "no parser specified";
376
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
   139
	else
793
55add3b87c01 Report errors in the config file to the user
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
   140
		return nil, "file", "no parser for "..(format);
376
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
   141
	end
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   142
end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   143
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   144
function save(filename, format)
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   145
end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   146
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   147
function addparser(format, parser)
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   148
	if format and parser then
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   149
		parsers[format] = parser;
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   150
	end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   151
end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   152
2427
343a9eb7540e configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   153
-- _M needed to avoid name clash with local 'parsers'
343a9eb7540e configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   154
function _M.parsers()
343a9eb7540e configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   155
	local p = {};
343a9eb7540e configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   156
	for format in pairs(parsers) do
343a9eb7540e configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   157
		table.insert(p, format);
343a9eb7540e configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   158
	end
343a9eb7540e configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   159
	return p;
343a9eb7540e configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   160
end
343a9eb7540e configmanager: Add parsers() method to return an array of supported config formats
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   161
376
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
   162
-- Built-in Lua parser
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   163
do
376
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
   164
	local loadstring, pcall, setmetatable = _G.loadstring, _G.pcall, _G.setmetatable;
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
   165
	local setfenv, rawget, tostring = _G.setfenv, _G.rawget, _G.tostring;
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   166
	parsers.lua = {};
3905
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   167
	function parsers.lua.load(data, config_file, config)
376
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
   168
		local env;
750
fbfcf8c1c830 configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
   169
		-- The ' = true' are needed so as not to set off __newindex when we assign the functions below
2975
c1a2e210f47e configmanager: Fix very wacky indentation
Matthew Wild <mwild1@gmail.com>
parents: 2974
diff changeset
   170
		env = setmetatable({
3012
6d86e26f0923 Merge configmanager->trunk
Matthew Wild <mwild1@gmail.com>
parents: 2985 3011
diff changeset
   171
			Host = true, host = true, VirtualHost = true,
6d86e26f0923 Merge configmanager->trunk
Matthew Wild <mwild1@gmail.com>
parents: 2985 3011
diff changeset
   172
			Component = true, component = true,
3610
2084959d4096 configmanager: Update Include and RunScript directives to support paths relative to the (current!) config file
Matthew Wild <mwild1@gmail.com>
parents: 3609
diff changeset
   173
			Include = true, include = true, RunScript = true }, {
2975
c1a2e210f47e configmanager: Fix very wacky indentation
Matthew Wild <mwild1@gmail.com>
parents: 2974
diff changeset
   174
				__index = function (t, k)
c1a2e210f47e configmanager: Fix very wacky indentation
Matthew Wild <mwild1@gmail.com>
parents: 2974
diff changeset
   175
					return rawget(_G, k) or
c1a2e210f47e configmanager: Fix very wacky indentation
Matthew Wild <mwild1@gmail.com>
parents: 2974
diff changeset
   176
						function (settings_table)
c1a2e210f47e configmanager: Fix very wacky indentation
Matthew Wild <mwild1@gmail.com>
parents: 2974
diff changeset
   177
							config[__currenthost or "*"][k] = settings_table;
c1a2e210f47e configmanager: Fix very wacky indentation
Matthew Wild <mwild1@gmail.com>
parents: 2974
diff changeset
   178
						end;
c1a2e210f47e configmanager: Fix very wacky indentation
Matthew Wild <mwild1@gmail.com>
parents: 2974
diff changeset
   179
				end,
c1a2e210f47e configmanager: Fix very wacky indentation
Matthew Wild <mwild1@gmail.com>
parents: 2974
diff changeset
   180
				__newindex = function (t, k, v)
3573
f31fa6520a4b configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents: 3515
diff changeset
   181
					set(config, env.__currenthost or "*", "core", k, v);
2975
c1a2e210f47e configmanager: Fix very wacky indentation
Matthew Wild <mwild1@gmail.com>
parents: 2974
diff changeset
   182
				end
c1a2e210f47e configmanager: Fix very wacky indentation
Matthew Wild <mwild1@gmail.com>
parents: 2974
diff changeset
   183
		});
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   184
		
1615
0e3eacf135f2 configmanager: Default options appearing before Host "*" to global (fixes potential traceback)
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
   185
		rawset(env, "__currenthost", "*") -- Default is global
3011
1189a29cd846 configmanager: Add VirtualHost as an alias for Host (re-applied in trunk due to previous bad merge with 0.7)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   186
		function env.VirtualHost(name)
2861
1402615b66f8 configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   187
			if rawget(config, name) and rawget(config[name].core, "component_module") then
1402615b66f8 configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   188
				error(format("Host %q clashes with previously defined %s Component %q, for services use a sub-domain like conference.%s",
1402615b66f8 configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   189
					name, config[name].core.component_module:gsub("^%a+$", { component = "external", muc = "MUC"}), name, name), 0);
1402615b66f8 configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   190
			end
376
6d87944df37c New configmanager. Old-style config files still work, but will print a warning
Matthew Wild <mwild1@gmail.com>
parents: 371
diff changeset
   191
			rawset(env, "__currenthost", name);
750
fbfcf8c1c830 configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
   192
			-- Needs at least one setting to logically exist :)
3573
f31fa6520a4b configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents: 3515
diff changeset
   193
			set(config, name or "*", "core", "defined", true);
3515
bb494c3aa364 configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents: 3384
diff changeset
   194
			return function (config_options)
bb494c3aa364 configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents: 3384
diff changeset
   195
				rawset(env, "__currenthost", "*"); -- Return to global scope
bb494c3aa364 configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents: 3384
diff changeset
   196
				for option_name, option_value in pairs(config_options) do
3573
f31fa6520a4b configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents: 3515
diff changeset
   197
					set(config, name or "*", "core", option_name, option_value);
3515
bb494c3aa364 configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents: 3384
diff changeset
   198
				end
bb494c3aa364 configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents: 3384
diff changeset
   199
			end;
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   200
		end
3011
1189a29cd846 configmanager: Add VirtualHost as an alias for Host (re-applied in trunk due to previous bad merge with 0.7)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   201
		env.Host, env.host = env.VirtualHost, env.VirtualHost;
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   202
		
750
fbfcf8c1c830 configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
   203
		function env.Component(name)
2861
1402615b66f8 configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   204
			if rawget(config, name) and rawget(config[name].core, "defined") and not rawget(config[name].core, "component_module") then
1402615b66f8 configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   205
				error(format("Component %q clashes with previously defined Host %q, for services use a sub-domain like conference.%s",
1402615b66f8 configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   206
					name, name, name), 0);
1402615b66f8 configmanager: Error when a component and host clash hostnames
Matthew Wild <mwild1@gmail.com>
parents: 1777
diff changeset
   207
			end
3573
f31fa6520a4b configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents: 3515
diff changeset
   208
			set(config, name, "core", "component_module", "component");
913
3e2dac84017d core.configmanager: Make components use 'component' module by default if none specified
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
   209
			-- Don't load the global modules by default
3573
f31fa6520a4b configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents: 3515
diff changeset
   210
			set(config, name, "core", "load_global_modules", false);
913
3e2dac84017d core.configmanager: Make components use 'component' module by default if none specified
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
   211
			rawset(env, "__currenthost", name);
3515
bb494c3aa364 configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents: 3384
diff changeset
   212
			local function handle_config_options(config_options)
bb494c3aa364 configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents: 3384
diff changeset
   213
				rawset(env, "__currenthost", "*"); -- Return to global scope
bb494c3aa364 configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents: 3384
diff changeset
   214
				for option_name, option_value in pairs(config_options) do
3573
f31fa6520a4b configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents: 3515
diff changeset
   215
					set(config, name or "*", "core", option_name, option_value);
3515
bb494c3aa364 configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents: 3384
diff changeset
   216
				end
bb494c3aa364 configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents: 3384
diff changeset
   217
			end
913
3e2dac84017d core.configmanager: Make components use 'component' module by default if none specified
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
   218
	
750
fbfcf8c1c830 configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
   219
			return function (module)
857
49298263f241 core.configmanager: Small fix to check validity of Component definitions
Matthew Wild <mwild1@gmail.com>
parents: 795
diff changeset
   220
					if type(module) == "string" then
3573
f31fa6520a4b configmanager: Atomic reloads, and some other internal changes to achieve this
Matthew Wild <mwild1@gmail.com>
parents: 3515
diff changeset
   221
						set(config, name, "core", "component_module", module);
3515
bb494c3aa364 configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents: 3384
diff changeset
   222
						return handle_config_options;
857
49298263f241 core.configmanager: Small fix to check validity of Component definitions
Matthew Wild <mwild1@gmail.com>
parents: 795
diff changeset
   223
					end
3515
bb494c3aa364 configmanager: Allow VirtualHost/Component definitions to be followed by a table of config options
Matthew Wild <mwild1@gmail.com>
parents: 3384
diff changeset
   224
					return handle_config_options(module);
750
fbfcf8c1c830 configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
   225
				end
fbfcf8c1c830 configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
   226
		end
fbfcf8c1c830 configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
   227
		env.component = env.Component;
fbfcf8c1c830 configmanager: Add support for defining components
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
   228
		
3905
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   229
		function env.Include(file, wildcard)
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   230
			if file:match("[*?]") then
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   231
				local path_pos, glob = file:match("()([^"..path_sep.."]+)$");
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   232
				local path = file:sub(1, math_max(path_pos-2,0));
3929
7cb03d67101b configmanager: Filenames without a path are also relative to the config file path, not the current working directory
Matthew Wild <mwild1@gmail.com>
parents: 3905
diff changeset
   233
				local config_path = config_file:gsub("[^"..path_sep.."]+$", "");
3905
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   234
				if #path > 0 then
3929
7cb03d67101b configmanager: Filenames without a path are also relative to the config file path, not the current working directory
Matthew Wild <mwild1@gmail.com>
parents: 3905
diff changeset
   235
					path = resolve_relative_path(config_path, path);
3905
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   236
				else
3929
7cb03d67101b configmanager: Filenames without a path are also relative to the config file path, not the current working directory
Matthew Wild <mwild1@gmail.com>
parents: 3905
diff changeset
   237
					path = config_path;
3905
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   238
				end
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   239
				local patt = glob_to_pattern(glob);
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   240
				for f in lfs.dir(path) do
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   241
					if f:sub(1,1) ~= "." and f:match(patt) then
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   242
						env.Include(path..path_sep..f);
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   243
					end
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   244
				end
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   245
			else
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   246
				local f, err = io.open(file);
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   247
				if f then
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   248
					local data = f:read("*a");
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   249
					local file = resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file);
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   250
					local ret, err = parsers.lua.load(data, file, config);
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   251
					if not ret then error(err:gsub("%[string.-%]", file), 0); end
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   252
				end
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   253
				if not f then error("Error loading included "..file..": "..err, 0); end
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   254
				return f, err;
794
912dc389935a Add Include command to include extra configuration files from the main one
Matthew Wild <mwild1@gmail.com>
parents: 793
diff changeset
   255
			end
912dc389935a Add Include command to include extra configuration files from the main one
Matthew Wild <mwild1@gmail.com>
parents: 793
diff changeset
   256
		end
912dc389935a Add Include command to include extra configuration files from the main one
Matthew Wild <mwild1@gmail.com>
parents: 793
diff changeset
   257
		env.include = env.Include;
912dc389935a Add Include command to include extra configuration files from the main one
Matthew Wild <mwild1@gmail.com>
parents: 793
diff changeset
   258
		
3610
2084959d4096 configmanager: Update Include and RunScript directives to support paths relative to the (current!) config file
Matthew Wild <mwild1@gmail.com>
parents: 3609
diff changeset
   259
		function env.RunScript(file)
3905
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   260
			return dofile(resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file));
3610
2084959d4096 configmanager: Update Include and RunScript directives to support paths relative to the (current!) config file
Matthew Wild <mwild1@gmail.com>
parents: 3609
diff changeset
   261
		end
2084959d4096 configmanager: Update Include and RunScript directives to support paths relative to the (current!) config file
Matthew Wild <mwild1@gmail.com>
parents: 3609
diff changeset
   262
		
3905
9222dad9e1e8 configmanager: Support for wildcards in Include directives
Matthew Wild <mwild1@gmail.com>
parents: 3780
diff changeset
   263
		local chunk, err = loadstring(data, "@"..config_file);
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   264
		
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   265
		if not chunk then
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   266
			return nil, err;
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   267
		end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   268
		
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   269
		setfenv(chunk, env);
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   270
		
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   271
		local ok, err = pcall(chunk);
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   272
		
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   273
		if not ok then
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   274
			return nil, err;
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   275
		end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   276
		
3613
f617718d2221 configmanager: Change parser API again to pass a config table to insert settings to. Fixes Include(). (Thanks Zash/answerman)
Matthew Wild <mwild1@gmail.com>
parents: 3610
diff changeset
   277
		return true;
371
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   278
	end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   279
	
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   280
end
0dc5819660e8 Import initial configmanager, not sure if it works yet, but it does pass the unit tests ;)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   281
466
0ecfd89c2cc0 Fix for configmanager when config file can't be found
Matthew Wild <mwild1@gmail.com>
parents: 376
diff changeset
   282
return _M;