2007-03-29 22:09:25 +00:00
|
|
|
# Reads and returns a complete file as a string
|
|
|
|
var readfile = func(file) {
|
2008-07-23 19:37:51 +00:00
|
|
|
if ((var st = stat(file)) == nil)
|
|
|
|
die("Cannot stat file: " ~ file);
|
2007-03-29 22:09:25 +00:00
|
|
|
var sz = st[7];
|
|
|
|
var buf = bits.buf(sz);
|
|
|
|
read(open(file), buf, sz);
|
|
|
|
return buf;
|
|
|
|
}
|
2007-06-13 16:46:41 +00:00
|
|
|
|
2008-07-23 19:37:51 +00:00
|
|
|
|
2007-06-13 16:46:41 +00:00
|
|
|
# Generates a stat() test routine that is passed the "mode" field
|
|
|
|
# (stat(...)[2]) from a stat() call (index 2), extracts the IFMT
|
|
|
|
# subfield and compares it with the given type, assumes S_IFMT ==
|
|
|
|
# 0xf000.
|
|
|
|
var _gen_ifmt_test = func(ifmt) {
|
|
|
|
func(stat_mode) {
|
2008-11-26 16:17:55 +00:00
|
|
|
var i = int(stat_mode / 0x1000);
|
|
|
|
return ifmt == i - int(i / 0x10) * 0x10;
|
2007-06-13 16:46:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-23 19:37:51 +00:00
|
|
|
|
2007-06-14 11:26:16 +00:00
|
|
|
# Generate file type test predicates isdir(), isreg(), islnk(), etc.
|
|
|
|
# Usage: var s = io.stat(filename); # nil -> doesn't exist, broken link
|
|
|
|
# if (s != nil and io.isdir(s[2])) { ... }
|
2008-07-23 19:37:51 +00:00
|
|
|
var ifmts = { dir:4, reg:8, lnk:10, sock:12, fifo:1, blk:6, chr:2 };
|
|
|
|
foreach (var fmt; keys(ifmts))
|
2007-06-13 16:46:41 +00:00
|
|
|
caller(0)[0]["is" ~ fmt] = _gen_ifmt_test(ifmts[fmt]);
|
|
|
|
|
2008-07-23 19:37:51 +00:00
|
|
|
|
2008-06-14 14:22:34 +00:00
|
|
|
# Loads Nasal file into namespace and executes it. The namespace
|
|
|
|
# (module name) is taken from the optional second argument, or
|
|
|
|
# derived from the Nasal file's name.
|
|
|
|
#
|
|
|
|
# Usage: io.load_nasal(<filename> [, <modulename>]);
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# io.load_nasal(getprop("/sim/fg-root") ~ "/Local/test.nas");
|
|
|
|
# io.load_nasal("/tmp/foo.nas", "test");
|
|
|
|
#
|
|
|
|
var load_nasal = func(file, module = nil) {
|
2008-07-23 19:37:51 +00:00
|
|
|
if (module == nil)
|
2008-06-14 14:22:34 +00:00
|
|
|
module = split(".", split("/", file)[-1])[0];
|
|
|
|
|
2008-07-30 20:34:52 +00:00
|
|
|
printlog("info", "loading ", file, " into namespace ", module);
|
|
|
|
|
2008-07-23 19:37:51 +00:00
|
|
|
if (!contains(globals, module))
|
2008-06-14 14:22:34 +00:00
|
|
|
globals[module] = {};
|
2008-07-30 20:34:52 +00:00
|
|
|
elsif (typeof(globals[module]) != "hash")
|
|
|
|
die("io.load_nasal(): namespace '" ~ module ~ "' already in use, but not a hash");
|
2008-06-14 14:22:34 +00:00
|
|
|
|
2008-10-06 23:21:54 +00:00
|
|
|
var code = call(func compile(readfile(file), file), nil, var err = []);
|
2008-07-23 19:37:51 +00:00
|
|
|
if (size(err)) {
|
2008-10-06 23:21:54 +00:00
|
|
|
(func nil)(); # FIXME hack around Nasal bug
|
|
|
|
|
|
|
|
if (substr(err[0], 0, 12) == "Parse error:") { # hack around Nasal feature
|
|
|
|
var e = split(" at line ", err[0]);
|
|
|
|
if (size(e) == 2)
|
|
|
|
err[0] = string.join("", [e[0], "\n at ", file, ", line ", e[1], "\n "]);
|
|
|
|
}
|
2008-07-23 19:37:51 +00:00
|
|
|
for (var i = 1; (var c = caller(i)) != nil; i += 1)
|
2008-07-11 15:37:27 +00:00
|
|
|
err ~= subvec(c, 2, 2);
|
|
|
|
debug.printerror(err);
|
|
|
|
return 0;
|
|
|
|
}
|
2008-06-14 14:22:34 +00:00
|
|
|
call(bind(code, globals), nil, nil, globals[module], err);
|
2008-06-14 14:25:34 +00:00
|
|
|
debug.printerror(err);
|
2008-07-03 16:23:13 +00:00
|
|
|
return !size(err);
|
2008-06-14 14:22:34 +00:00
|
|
|
}
|
|
|
|
|
2008-07-23 19:37:51 +00:00
|
|
|
|
2008-07-10 16:53:43 +00:00
|
|
|
# Load XML file in FlightGear's native <PropertyList> format.
|
|
|
|
# If the second, optional target parameter is set, then the properties
|
|
|
|
# are loaded to this node in the global property tree. Otherwise they
|
|
|
|
# are returned as a separate props.Node tree. Returns the data as a
|
|
|
|
# props.Node on success or nil on error.
|
|
|
|
#
|
|
|
|
# Usage: io.read_properties(<filename> [, <props.Node or property-path>]);
|
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
#
|
|
|
|
# var target = props.globals.getNode("/sim/model");
|
|
|
|
# io.read_properties("/tmp/foo.xml", target);
|
|
|
|
#
|
|
|
|
# var data = io.read_properties("/tmp/foo.xml", "/sim/model");
|
|
|
|
# var data = io.read_properties("/tmp/foo.xml");
|
|
|
|
#
|
|
|
|
var read_properties = func(path, target = nil) {
|
|
|
|
var args = props.Node.new({ filename: path });
|
2008-07-23 19:37:51 +00:00
|
|
|
if (target == nil) {
|
2008-07-10 16:53:43 +00:00
|
|
|
var ret = args.getNode("data", 1);
|
2008-07-23 19:37:51 +00:00
|
|
|
} elsif (isa(target, props.Node)) {
|
2008-07-10 16:53:43 +00:00
|
|
|
args.getNode("targetnode", 1).setValue(target.getPath());
|
|
|
|
var ret = target;
|
|
|
|
} else {
|
|
|
|
args.getNode("targetnode", 1).setValue(target);
|
|
|
|
var ret = props.globals.getNode(target, 1);
|
|
|
|
}
|
|
|
|
return fgcommand("loadxml", args) ? ret : nil;
|
|
|
|
}
|
|
|
|
|
2008-07-23 19:37:51 +00:00
|
|
|
|
2008-07-10 16:53:43 +00:00
|
|
|
# Write XML file in FlightGear's native <PropertyList> format.
|
|
|
|
# Returns the filename on success or nil on error. If the source
|
2008-07-11 09:37:33 +00:00
|
|
|
# is a props.Node that refers to a node in the main tree, then
|
|
|
|
# the data are directly written from the tree, yielding a more
|
|
|
|
# accurate result. Otherwise the data need to be copied first,
|
|
|
|
# which may slightly change node types (FLOAT becomes DOUBLE etc.)
|
2008-07-10 16:53:43 +00:00
|
|
|
#
|
|
|
|
# Usage: io.write_properties(<filename>, <props.Node or property-path>);
|
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
#
|
|
|
|
# var data = props.Node.new({ a:1, b:2, c:{ d:3, e:4 } });
|
|
|
|
# io.write_properties("/tmp/foo.xml", data);
|
|
|
|
# io.write_properties("/tmp/foo.xml", "/sim/model");
|
|
|
|
#
|
|
|
|
var write_properties = func(path, prop) {
|
|
|
|
var args = props.Node.new({ filename: path });
|
2008-07-11 09:37:33 +00:00
|
|
|
# default attributes of a new node plus the lowest unused bit
|
|
|
|
var attr = args.getAttribute() + args.getAttribute("last") * 2;
|
|
|
|
props.globals.setAttribute(attr);
|
2008-07-23 19:37:51 +00:00
|
|
|
if (isa(prop, props.Node)) {
|
|
|
|
for (var root = prop; (var p = root.getParent()) != nil;)
|
2008-07-10 16:53:43 +00:00
|
|
|
root = p;
|
2008-07-23 19:37:51 +00:00
|
|
|
if (root.getAttribute() == attr)
|
2008-07-10 16:53:43 +00:00
|
|
|
args.getNode("sourcenode", 1).setValue(prop.getPath());
|
|
|
|
else
|
|
|
|
props.copy(prop, args.getNode("data", 1), 1);
|
|
|
|
} else {
|
|
|
|
args.getNode("sourcenode", 1).setValue(prop);
|
|
|
|
}
|
|
|
|
return fgcommand("savexml", args) ? path : nil;
|
|
|
|
}
|
|
|
|
|
2008-07-23 19:37:51 +00:00
|
|
|
|
2007-07-01 15:05:16 +00:00
|
|
|
# The following two functions are for reading generic XML files into
|
|
|
|
# the property tree and for writing them from there to the disk. The
|
|
|
|
# built-in fgcommands (load, save, loadxml, savexml) are for FlightGear's
|
|
|
|
# own <PropertyList> XML files only, as they only handle a limited
|
|
|
|
# number of very specific attributes. The io.readxml() loader turns
|
|
|
|
# attributes into regular children with a configurable prefix prepended
|
|
|
|
# to their name, while io.writexml() turns such nodes back into
|
|
|
|
# attributes. The two functions have their own limitations, but can
|
|
|
|
# easily get extended to whichever needs. The underlying parsexml()
|
|
|
|
# command will handle any XML file.
|
|
|
|
|
2007-06-29 15:49:08 +00:00
|
|
|
# Reads an XML file from an absolute path and returns it as property
|
2007-07-01 15:05:16 +00:00
|
|
|
# tree. All nodes will be of type STRING. Data are only written to
|
|
|
|
# leafs. Attributes are written as regular nodes with the optional
|
|
|
|
# prefix prepended to the name. If the prefix is nil, then attributes
|
|
|
|
# are ignored. Returns nil on error.
|
2007-06-30 11:06:36 +00:00
|
|
|
#
|
2007-10-07 15:12:05 +00:00
|
|
|
var readxml = func(path, prefix = "___") {
|
2007-06-30 11:06:36 +00:00
|
|
|
var stack = [[{}, ""]];
|
2007-06-29 15:49:08 +00:00
|
|
|
var node = props.Node.new();
|
|
|
|
var tree = node; # prevent GC
|
|
|
|
var start = func(name, attr) {
|
2007-06-30 11:06:36 +00:00
|
|
|
var index = stack[-1][0];
|
2008-07-23 19:37:51 +00:00
|
|
|
if (!contains(index, name))
|
2007-06-30 11:06:36 +00:00
|
|
|
index[name] = 0;
|
|
|
|
|
|
|
|
node = node.getChild(name, index[name], 1);
|
2008-07-23 19:37:51 +00:00
|
|
|
if (prefix != nil)
|
|
|
|
foreach (var n; keys(attr))
|
2007-06-29 15:49:08 +00:00
|
|
|
node.getNode(prefix ~ n, 1).setValue(attr[n]);
|
2007-06-30 11:06:36 +00:00
|
|
|
|
|
|
|
index[name] += 1;
|
|
|
|
append(stack, [{}, ""]);
|
2007-06-29 15:49:08 +00:00
|
|
|
}
|
|
|
|
var end = func(name) {
|
|
|
|
var buf = pop(stack);
|
2008-07-23 19:37:51 +00:00
|
|
|
if (!size(buf[0]) and size(buf[1]))
|
2007-06-29 15:49:08 +00:00
|
|
|
node.setValue(buf[1]);
|
|
|
|
node = node.getParent();
|
|
|
|
}
|
2008-07-03 16:23:13 +00:00
|
|
|
var data = func(d) stack[-1][1] ~= d;
|
2007-10-07 15:12:05 +00:00
|
|
|
return parsexml(path, start, end, data) == nil ? nil : tree;
|
2007-06-29 15:49:08 +00:00
|
|
|
}
|
|
|
|
|
2008-07-23 19:37:51 +00:00
|
|
|
|
2007-07-01 18:54:32 +00:00
|
|
|
# Writes a property tree as returned by readxml() to a file. Children
|
2007-07-01 15:05:16 +00:00
|
|
|
# with name starting with <prefix> are again turned into attributes of
|
|
|
|
# their parent. <node> must contain exactly one child, which will
|
2007-07-01 18:54:32 +00:00
|
|
|
# become the XML file's outermost element.
|
2007-07-01 15:05:16 +00:00
|
|
|
#
|
2007-10-07 15:12:05 +00:00
|
|
|
var writexml = func(path, node, indent = "\t", prefix = "___") {
|
2007-07-01 15:05:16 +00:00
|
|
|
var root = node.getChildren();
|
2008-07-23 19:37:51 +00:00
|
|
|
if (!size(root))
|
2007-07-01 18:54:32 +00:00
|
|
|
die("writexml(): tree doesn't have a root node");
|
2008-07-23 19:37:51 +00:00
|
|
|
if (substr(path, -4) != ".xml")
|
2007-10-07 15:12:05 +00:00
|
|
|
path ~= ".xml";
|
|
|
|
var file = open(path, "w");
|
|
|
|
write(file, "<?xml version=\"1.0\"?>\n\n");
|
2007-07-01 15:05:16 +00:00
|
|
|
var writenode = func(n, ind = "") {
|
|
|
|
var name = n.getName();
|
|
|
|
var name_attr = name;
|
|
|
|
var children = [];
|
2008-07-23 19:37:51 +00:00
|
|
|
foreach (var c; n.getChildren()) {
|
2007-10-07 15:12:05 +00:00
|
|
|
var a = c.getName();
|
2008-07-23 19:37:51 +00:00
|
|
|
if (substr(a, 0, size(prefix)) == prefix)
|
2007-10-07 15:12:05 +00:00
|
|
|
name_attr ~= " " ~ substr(a, size(prefix)) ~ '="' ~ c.getValue() ~ '"';
|
2007-07-01 15:05:16 +00:00
|
|
|
else
|
|
|
|
append(children, c);
|
|
|
|
}
|
2008-07-23 19:37:51 +00:00
|
|
|
if (size(children)) {
|
2007-10-07 15:12:05 +00:00
|
|
|
write(file, ind ~ "<" ~ name_attr ~ ">\n");
|
2008-07-23 19:37:51 +00:00
|
|
|
foreach (var c; children)
|
2007-07-01 15:05:16 +00:00
|
|
|
writenode(c, ind ~ indent);
|
2007-10-07 15:12:05 +00:00
|
|
|
write(file, ind ~ "</" ~ name ~ ">\n");
|
2008-07-23 19:37:51 +00:00
|
|
|
} elsif ((var value = n.getValue()) != nil) {
|
2007-10-07 15:12:05 +00:00
|
|
|
write(file, ind ~ "<" ~ name_attr ~ ">" ~ value ~ "</" ~ name ~ ">\n");
|
2007-07-01 15:05:16 +00:00
|
|
|
} else {
|
2007-10-07 15:12:05 +00:00
|
|
|
write(file, ind ~ "<" ~ name_attr ~ "/>\n");
|
2007-07-01 15:05:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
writenode(root[0]);
|
2007-10-07 15:12:05 +00:00
|
|
|
close(file);
|
2008-07-23 19:37:51 +00:00
|
|
|
if (size(root) != 1)
|
2007-07-01 18:54:32 +00:00
|
|
|
die("writexml(): tree has more than one root node");
|
2007-07-01 15:05:16 +00:00
|
|
|
}
|
|
|
|
|
2008-07-23 19:37:51 +00:00
|
|
|
|
2008-06-16 07:16:10 +00:00
|
|
|
# Redefine io.open() such that files can only be opened under authorized directories.
|
2008-06-13 11:35:55 +00:00
|
|
|
#
|
2008-06-15 10:11:32 +00:00
|
|
|
_setlistener("/sim/signals/nasal-dir-initialized", func {
|
2008-07-23 15:42:16 +00:00
|
|
|
# read IO rules
|
2008-10-15 16:35:11 +00:00
|
|
|
var root = string.normpath(getprop("/sim/fg-root"));
|
|
|
|
var home = string.normpath(getprop("/sim/fg-home"));
|
2008-06-16 10:47:02 +00:00
|
|
|
var config = "Nasal/IOrules";
|
|
|
|
|
2008-07-03 16:23:13 +00:00
|
|
|
var rules_file = nil;
|
2008-06-16 10:47:02 +00:00
|
|
|
var read_rules = [];
|
|
|
|
var write_rules = [];
|
|
|
|
|
|
|
|
var load_rules = func(path) {
|
2008-07-23 19:37:51 +00:00
|
|
|
if (stat(path) == nil)
|
2008-07-03 16:23:13 +00:00
|
|
|
return nil;
|
2008-06-16 10:47:02 +00:00
|
|
|
printlog("info", "using io.open() rules from ", path);
|
|
|
|
read_rules = [];
|
|
|
|
write_rules = [];
|
|
|
|
var file = open(path, "r");
|
2008-07-23 19:37:51 +00:00
|
|
|
for (var no = 1; (var line = readln(file)) != nil; no += 1) {
|
|
|
|
if (!size(line) or line[0] == `#`)
|
2008-06-16 10:47:02 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
var f = split(" ", line);
|
2008-07-23 19:37:51 +00:00
|
|
|
if (size(f) < 3 or f[0] != "READ" and f[0] != "WRITE" or f[1] != "DENY" and f[1] != "ALLOW") {
|
2008-06-16 10:47:02 +00:00
|
|
|
printlog("alert", "ERROR: invalid io.open() rule in ", path, ", line ", no, ": ", line);
|
|
|
|
read_rules = write_rules = [];
|
2008-06-22 18:00:02 +00:00
|
|
|
break;
|
2008-06-16 10:47:02 +00:00
|
|
|
}
|
|
|
|
var pattern = f[2];
|
2008-07-23 19:37:51 +00:00
|
|
|
foreach (var p; subvec(f, 3))
|
2008-06-16 10:47:02 +00:00
|
|
|
pattern ~= " " ~ p;
|
2008-07-23 19:37:51 +00:00
|
|
|
if (substr(pattern, 0, 9) == "$FG_ROOT/")
|
2008-06-16 10:47:02 +00:00
|
|
|
pattern = root ~ "/" ~ substr(pattern, 9);
|
2008-07-23 19:37:51 +00:00
|
|
|
elsif (substr(pattern, 0, 9) == "$FG_HOME/")
|
2008-06-16 10:47:02 +00:00
|
|
|
pattern = home ~ "/" ~ substr(pattern, 9);
|
|
|
|
append(f[0] == "READ" ? read_rules : write_rules, [pattern, f[1] == "ALLOW"]);
|
|
|
|
}
|
|
|
|
close(file);
|
2008-07-03 16:23:13 +00:00
|
|
|
return path;
|
2008-06-16 10:47:02 +00:00
|
|
|
}
|
2008-06-16 06:32:32 +00:00
|
|
|
|
2008-06-16 11:19:29 +00:00
|
|
|
# catch exceptions so that a die() doesn't ruin everything
|
2008-07-03 16:23:13 +00:00
|
|
|
var rules_file = call(func load_rules(home ~ "/" ~ config)
|
|
|
|
or load_rules(root ~ "/" ~ config), nil, var err = []);
|
2008-07-23 19:37:51 +00:00
|
|
|
if (size(err)) {
|
2008-06-16 11:19:29 +00:00
|
|
|
debug.printerror(err);
|
|
|
|
read_rules = write_rules = [];
|
|
|
|
}
|
|
|
|
|
2008-06-16 10:47:02 +00:00
|
|
|
read_rules = [["*/" ~ config, 0]] ~ read_rules;
|
|
|
|
write_rules = [["*/" ~ config, 0]] ~ write_rules;
|
2008-07-23 19:37:51 +00:00
|
|
|
if (__.log_level <= 3) {
|
2008-08-10 17:53:41 +00:00
|
|
|
print("IOrules/READ: ", debug.string(read_rules));
|
|
|
|
print("IOrules/WRITE: ", debug.string(write_rules));
|
2008-06-16 10:47:02 +00:00
|
|
|
}
|
2008-06-13 11:35:55 +00:00
|
|
|
|
2008-07-23 15:42:16 +00:00
|
|
|
# make safe, local copies
|
|
|
|
var setValue = props._setValue;
|
|
|
|
var getValue = props._getValue;
|
2008-10-15 16:35:11 +00:00
|
|
|
var normpath = string.normpath;
|
2008-07-22 16:46:18 +00:00
|
|
|
var match = string.match;
|
2008-07-23 00:41:18 +00:00
|
|
|
var caller = caller;
|
2008-07-23 15:42:16 +00:00
|
|
|
var die = die;
|
2008-07-22 16:46:18 +00:00
|
|
|
|
2008-07-24 17:41:40 +00:00
|
|
|
# validators
|
2008-07-11 15:37:27 +00:00
|
|
|
var valid = func(path, rules) {
|
2008-10-15 16:35:11 +00:00
|
|
|
var fpath = normpath(path);
|
2008-07-23 19:37:51 +00:00
|
|
|
foreach (var d; rules)
|
|
|
|
if (match(fpath, d[0]))
|
2008-07-11 15:37:27 +00:00
|
|
|
return d[1] ? fpath : nil;
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2008-07-24 17:41:40 +00:00
|
|
|
var read_validator = func(n) setValue(n, [valid(getValue(n, []), read_rules) or ""]);
|
|
|
|
var write_validator = func(n) setValue(n, [valid(getValue(n, []), write_rules) or ""]);
|
|
|
|
|
2008-07-23 15:42:16 +00:00
|
|
|
# validation listeners for load[xml]/save[xml]/parsexml() (see utils.cxx:fgValidatePath)
|
2008-07-24 17:41:40 +00:00
|
|
|
var n = props.globals.getNode("/sim/paths/validate", 1).remove();
|
|
|
|
_setlistener(n.getNode("read", 1)._g, read_validator);
|
|
|
|
_setlistener(n.getNode("write", 1)._g, write_validator);
|
2008-07-23 15:42:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
# wrap io.open()
|
2008-11-26 16:17:55 +00:00
|
|
|
io.open = var io_open = (func {
|
|
|
|
var _open = io.open;
|
|
|
|
func(path, mode = "rb") {
|
|
|
|
var rules = write_rules;
|
|
|
|
if (mode == "r" or mode == "rb" or mode == "br")
|
|
|
|
rules = read_rules;
|
2008-06-16 07:16:10 +00:00
|
|
|
|
2008-11-26 16:17:55 +00:00
|
|
|
if (var vpath = valid(path, rules))
|
|
|
|
return _open(vpath, mode);
|
2008-06-16 07:16:10 +00:00
|
|
|
|
2008-11-26 16:17:55 +00:00
|
|
|
die("io.open(): opening file '" ~ path ~ "' denied (unauthorized access)\n ");
|
|
|
|
}
|
|
|
|
})();
|
2008-07-11 15:37:27 +00:00
|
|
|
|
2008-07-24 17:41:40 +00:00
|
|
|
# wrap closure() to prevent tampering with security related functions
|
2008-07-23 15:42:16 +00:00
|
|
|
var thislistener = caller(0)[1];
|
2008-11-26 16:17:55 +00:00
|
|
|
globals.closure = (func {
|
|
|
|
var _closure = globals.closure;
|
|
|
|
func(fn, level = 0) {
|
|
|
|
var thisfunction = caller(0)[1];
|
|
|
|
if (fn != thislistener and fn != io_open and fn != thisfunction
|
|
|
|
and fn != read_validator and fn != write_validator)
|
|
|
|
return _closure(fn, level);
|
|
|
|
|
|
|
|
die("closure(): query denied (unauthorized access)\n ");
|
|
|
|
}
|
|
|
|
})();
|
2008-06-13 11:35:55 +00:00
|
|
|
});
|
2007-06-29 15:49:08 +00:00
|
|
|
|