2007-03-29 22:09:25 +00:00
|
|
|
# Reads and returns a complete file as a string
|
|
|
|
var readfile = func(file) {
|
|
|
|
if((var st = stat(file)) == nil) die("Cannot stat file: " ~ file);
|
|
|
|
var sz = st[7];
|
|
|
|
var buf = bits.buf(sz);
|
|
|
|
read(open(file), buf, sz);
|
|
|
|
return buf;
|
|
|
|
}
|
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) {
|
|
|
|
var buf = bits.buf(2);
|
|
|
|
bits.setfld(buf, 0, 16, stat_mode);
|
|
|
|
return ifmt == bits.fld(buf, 12, 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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])) { ... }
|
2007-06-13 16:46:41 +00:00
|
|
|
var ifmts = {dir:4, reg:8, lnk:10, sock:12, fifo:1, blk:6, chr:2};
|
|
|
|
foreach(fmt; keys(ifmts))
|
|
|
|
caller(0)[0]["is" ~ fmt] = _gen_ifmt_test(ifmts[fmt]);
|
|
|
|
|
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) {
|
|
|
|
if(module == nil)
|
|
|
|
module = split(".", split("/", file)[-1])[0];
|
|
|
|
|
|
|
|
if(!contains(globals, module))
|
|
|
|
globals[module] = {};
|
|
|
|
|
|
|
|
var err = [];
|
|
|
|
printlog("info", "loading ", file, " into namespace ", module);
|
2008-06-14 14:25:34 +00:00
|
|
|
var code = call(func compile(readfile(file), file), nil, err);
|
2008-06-14 14:22:34 +00:00
|
|
|
if(size(err))
|
|
|
|
return print(file ~ ": " ~ err[0]);
|
|
|
|
|
|
|
|
call(bind(code, globals), nil, nil, globals[module], err);
|
2008-06-14 14:25:34 +00:00
|
|
|
debug.printerror(err);
|
|
|
|
return size(err);
|
2008-06-14 14:22:34 +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];
|
2007-07-01 15:05:16 +00:00
|
|
|
if(!contains(index, name))
|
2007-06-30 11:06:36 +00:00
|
|
|
index[name] = 0;
|
|
|
|
|
|
|
|
node = node.getChild(name, index[name], 1);
|
2007-06-29 15:49:08 +00:00
|
|
|
if(prefix != nil)
|
|
|
|
foreach(var n; keys(attr))
|
|
|
|
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);
|
2007-06-30 11:06:36 +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();
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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();
|
2007-07-01 18:54:32 +00:00
|
|
|
if(!size(root))
|
|
|
|
die("writexml(): tree doesn't have a root node");
|
2007-10-07 15:12:05 +00:00
|
|
|
if(substr(path, -4) != ".xml")
|
|
|
|
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 = [];
|
|
|
|
foreach(var c; n.getChildren()) {
|
2007-10-07 15:12:05 +00:00
|
|
|
var a = c.getName();
|
|
|
|
if(substr(a, 0, size(prefix)) == prefix)
|
|
|
|
name_attr ~= " " ~ substr(a, size(prefix)) ~ '="' ~ c.getValue() ~ '"';
|
2007-07-01 15:05:16 +00:00
|
|
|
else
|
|
|
|
append(children, c);
|
|
|
|
}
|
|
|
|
if(size(children)) {
|
2007-10-07 15:12:05 +00:00
|
|
|
write(file, ind ~ "<" ~ name_attr ~ ">\n");
|
2007-07-01 15:05:16 +00:00
|
|
|
foreach(var c; children)
|
|
|
|
writenode(c, ind ~ indent);
|
2007-10-07 15:12:05 +00:00
|
|
|
write(file, ind ~ "</" ~ name ~ ">\n");
|
2007-07-01 15:05:16 +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);
|
2007-07-01 18:54:32 +00:00
|
|
|
if(size(root) != 1)
|
|
|
|
die("writexml(): tree has more than one root node");
|
2007-07-01 15:05:16 +00:00
|
|
|
}
|
|
|
|
|
2008-06-13 11:35:55 +00:00
|
|
|
# Redefine io.open() such that files can only be written under authorized directories.
|
|
|
|
#
|
|
|
|
setlistener("/sim/signals/nasal-dir-initialized", func {
|
2008-06-13 12:09:45 +00:00
|
|
|
var _open = open;
|
2008-06-13 11:35:55 +00:00
|
|
|
var writable_dirs = [
|
2008-06-13 13:01:38 +00:00
|
|
|
# "", # any
|
|
|
|
# string.fixpath(getprop("/sim/fg-root")) ~ "/Scenery/",
|
|
|
|
string.fixpath(getprop("/sim/fg-home")) ~ "/",
|
2008-06-13 11:57:20 +00:00
|
|
|
"/tmp/", "/var/tmp/",
|
2008-06-13 11:35:55 +00:00
|
|
|
"[A-Za-z]:TMP/", "[A-Za-z]:TEMP/",
|
|
|
|
"[A-Za-z]:/TMP/", "[A-Za-z]:/TEMP/",
|
|
|
|
];
|
|
|
|
|
|
|
|
open = func(path, mode = "rb") {
|
2008-06-13 19:14:48 +00:00
|
|
|
if(mode == "r" or mode == "rb" or mode == "br")
|
2008-06-13 11:35:55 +00:00
|
|
|
return _open(path, mode);
|
|
|
|
|
2008-06-13 13:01:38 +00:00
|
|
|
var fpath = string.fixpath(path);
|
2008-06-13 19:14:48 +00:00
|
|
|
foreach(var p; writable_dirs)
|
|
|
|
if(string.match(fpath, p ~ '*'))
|
2008-06-13 11:35:55 +00:00
|
|
|
return _open(fpath, mode);
|
|
|
|
|
2008-06-13 19:19:54 +00:00
|
|
|
die("io.open(): writing to file '" ~ path ~ "' denied (unauthorized directory)\n ");
|
2008-06-13 11:35:55 +00:00
|
|
|
}
|
|
|
|
});
|
2007-06-29 15:49:08 +00:00
|
|
|
|