File: util.lua

package info (click to toggle)
lua-gtk 0.9%2B20100528-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,176 kB
  • ctags: 1,934
  • sloc: ansic: 9,571; sh: 373; makefile: 241
file content (77 lines) | stat: -rwxr-xr-x 2,149 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#! /usr/bin/env lua
-- vim:sw=4:sts=4
-- Functions used from configure.lua, parse-xml.lua, make-link.lua,
-- make-xml.lua and others.
--
-- Exported symbols:
--  load_spec
--  load_config
--  function_flag_map
--

-- This map must match the defines for FLAG_xxx in include/common.h.  These
-- constants are available to library config files.
function_flag_map = {
    CONST_OBJECT = 1,		-- used
    NOT_NEW_OBJECT = 2,		-- used
    DONT_FREE = 4,		-- used
    INCREF = 8,			-- not used
    NOINCREF = 16,		-- used
    CHAR_PTR = 0x1000,		-- used indirectly
    CHAR_PTR_FORCE = 0x9000,	-- C headers have a const char*
    CONST_CHAR_PTR = 0x2000,	-- used indirectly
    CONST_CHAR_PTR_FORCE = 0xA000,  -- C headers have a non-const char*
}

-- Read a Lua file, no frills.
function load_config(fname, tbl)
    local chunk
    chunk = assert(loadfile(fname))
    tbl = tbl or {}

    -- catch accesses to undefined variables.
    setmetatable(tbl, { __index = function(tbl, key)
	error(string.format("%s: undefined variable: %s", fname, key))
    end
    })
   
    setfenv(chunk, tbl)
    chunk()
    setmetatable(tbl, nil)
    return tbl
end

-- read another modules's spec file and copy all settings into the current
-- spec.
local function include_spec(modname)
    local fname, spec, target

    fname = string.format("src/%s/spec.lua", modname)
    spec = load_spec(fname)
    target = getfenv(2)
    for k, v in pairs(spec) do
	target[k] = v
    end
end

-- dummy function to replace the "include_spec" when following includes is
-- not wanted.
local function ignore_spec(name)
end

---
-- Read a Lua configuration file.  In case of error, aborts the application.
--
-- @param fname  The path and name of the file to load
-- @param is_other  True when loading all other libraries; don't follow
--	include_spec statements.  Only "native" declarations are considered.
-- @return  A table with the variables defined in that file.
--
function load_spec(fname, is_other)
    local tbl = { include_spec=is_other and ignore_spec or include_spec }
    for k, v in pairs(function_flag_map) do
	tbl[k] = v
    end
    return load_config(fname, tbl)
end