1
0
Fork 0

cosmetics: white space fix & one "var" added

This commit is contained in:
mfranz 2008-07-23 19:37:51 +00:00
parent de1aa4c56e
commit 9148a4b2eb

View file

@ -1,12 +1,14 @@
# Reads and returns a complete file as a string
var readfile = func(file) {
if((var st = stat(file)) == nil) die("Cannot stat file: " ~ 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;
}
# 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 ==
@ -19,13 +21,15 @@ var _gen_ifmt_test = func(ifmt) {
}
}
# 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])) { ... }
var ifmts = {dir:4, reg:8, lnk:10, sock:12, fifo:1, blk:6, chr:2};
foreach(fmt; keys(ifmts))
var ifmts = { dir:4, reg:8, lnk:10, sock:12, fifo:1, blk:6, chr:2 };
foreach (var fmt; keys(ifmts))
caller(0)[0]["is" ~ fmt] = _gen_ifmt_test(ifmts[fmt]);
# 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.
@ -38,18 +42,18 @@ foreach(fmt; keys(ifmts))
# io.load_nasal("/tmp/foo.nas", "test");
#
var load_nasal = func(file, module = nil) {
if(module == nil)
if (module == nil)
module = split(".", split("/", file)[-1])[0];
if(!contains(globals, module))
if (!contains(globals, module))
globals[module] = {};
var err = [];
printlog("info", "loading ", file, " into namespace ", module);
var code = call(func compile(readfile(file), file), nil, err);
if(size(err)) {
if (size(err)) {
(func nil)(); # needed to get correct caller results (?!?)
for(var i = 1; (var c = caller(i)) != nil; i += 1)
for (var i = 1; (var c = caller(i)) != nil; i += 1)
err ~= subvec(c, 2, 2);
debug.printerror(err);
return 0;
@ -59,6 +63,7 @@ var load_nasal = func(file, module = nil) {
return !size(err);
}
# 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
@ -77,9 +82,9 @@ var load_nasal = func(file, module = nil) {
#
var read_properties = func(path, target = nil) {
var args = props.Node.new({ filename: path });
if(target == nil) {
if (target == nil) {
var ret = args.getNode("data", 1);
} elsif(isa(target, props.Node)) {
} elsif (isa(target, props.Node)) {
args.getNode("targetnode", 1).setValue(target.getPath());
var ret = target;
} else {
@ -89,6 +94,7 @@ var read_properties = func(path, target = nil) {
return fgcommand("loadxml", args) ? ret : nil;
}
# Write XML file in FlightGear's native <PropertyList> format.
# Returns the filename on success or nil on error. If the source
# is a props.Node that refers to a node in the main tree, then
@ -109,10 +115,10 @@ var write_properties = func(path, prop) {
# default attributes of a new node plus the lowest unused bit
var attr = args.getAttribute() + args.getAttribute("last") * 2;
props.globals.setAttribute(attr);
if(isa(prop, props.Node)) {
for(var root = prop; (var p = root.getParent()) != nil;)
if (isa(prop, props.Node)) {
for (var root = prop; (var p = root.getParent()) != nil;)
root = p;
if(root.getAttribute() == attr)
if (root.getAttribute() == attr)
args.getNode("sourcenode", 1).setValue(prop.getPath());
else
props.copy(prop, args.getNode("data", 1), 1);
@ -122,6 +128,7 @@ var write_properties = func(path, prop) {
return fgcommand("savexml", args) ? path : nil;
}
# 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
@ -145,12 +152,12 @@ var readxml = func(path, prefix = "___") {
var tree = node; # prevent GC
var start = func(name, attr) {
var index = stack[-1][0];
if(!contains(index, name))
if (!contains(index, name))
index[name] = 0;
node = node.getChild(name, index[name], 1);
if(prefix != nil)
foreach(var n; keys(attr))
if (prefix != nil)
foreach (var n; keys(attr))
node.getNode(prefix ~ n, 1).setValue(attr[n]);
index[name] += 1;
@ -158,7 +165,7 @@ var readxml = func(path, prefix = "___") {
}
var end = func(name) {
var buf = pop(stack);
if(!size(buf[0]) and size(buf[1]))
if (!size(buf[0]) and size(buf[1]))
node.setValue(buf[1]);
node = node.getParent();
}
@ -166,6 +173,7 @@ var readxml = func(path, prefix = "___") {
return parsexml(path, start, end, data) == nil ? nil : tree;
}
# Writes a property tree as returned by readxml() to a file. Children
# with name starting with <prefix> are again turned into attributes of
# their parent. <node> must contain exactly one child, which will
@ -173,9 +181,9 @@ var readxml = func(path, prefix = "___") {
#
var writexml = func(path, node, indent = "\t", prefix = "___") {
var root = node.getChildren();
if(!size(root))
if (!size(root))
die("writexml(): tree doesn't have a root node");
if(substr(path, -4) != ".xml")
if (substr(path, -4) != ".xml")
path ~= ".xml";
var file = open(path, "w");
write(file, "<?xml version=\"1.0\"?>\n\n");
@ -183,19 +191,19 @@ var writexml = func(path, node, indent = "\t", prefix = "___") {
var name = n.getName();
var name_attr = name;
var children = [];
foreach(var c; n.getChildren()) {
foreach (var c; n.getChildren()) {
var a = c.getName();
if(substr(a, 0, size(prefix)) == prefix)
if (substr(a, 0, size(prefix)) == prefix)
name_attr ~= " " ~ substr(a, size(prefix)) ~ '="' ~ c.getValue() ~ '"';
else
append(children, c);
}
if(size(children)) {
if (size(children)) {
write(file, ind ~ "<" ~ name_attr ~ ">\n");
foreach(var c; children)
foreach (var c; children)
writenode(c, ind ~ indent);
write(file, ind ~ "</" ~ name ~ ">\n");
} elsif((var value = n.getValue()) != nil) {
} elsif ((var value = n.getValue()) != nil) {
write(file, ind ~ "<" ~ name_attr ~ ">" ~ value ~ "</" ~ name ~ ">\n");
} else {
write(file, ind ~ "<" ~ name_attr ~ "/>\n");
@ -203,10 +211,11 @@ var writexml = func(path, node, indent = "\t", prefix = "___") {
}
writenode(root[0]);
close(file);
if(size(root) != 1)
if (size(root) != 1)
die("writexml(): tree has more than one root node");
}
# Redefine io.open() such that files can only be opened under authorized directories.
#
_setlistener("/sim/signals/nasal-dir-initialized", func {
@ -220,28 +229,28 @@ _setlistener("/sim/signals/nasal-dir-initialized", func {
var write_rules = [];
var load_rules = func(path) {
if(stat(path) == nil)
if (stat(path) == nil)
return nil;
printlog("info", "using io.open() rules from ", path);
read_rules = [];
write_rules = [];
var file = open(path, "r");
for(var no = 1; (var line = readln(file)) != nil; no += 1) {
if(!size(line) or line[0] == `#`)
for (var no = 1; (var line = readln(file)) != nil; no += 1) {
if (!size(line) or line[0] == `#`)
continue;
var f = split(" ", line);
if(size(f) < 3 or f[0] != "READ" and f[0] != "WRITE" or f[1] != "DENY" and f[1] != "ALLOW") {
if (size(f) < 3 or f[0] != "READ" and f[0] != "WRITE" or f[1] != "DENY" and f[1] != "ALLOW") {
printlog("alert", "ERROR: invalid io.open() rule in ", path, ", line ", no, ": ", line);
read_rules = write_rules = [];
break;
}
var pattern = f[2];
foreach(var p; subvec(f, 3))
foreach (var p; subvec(f, 3))
pattern ~= " " ~ p;
if(substr(pattern, 0, 9) == "$FG_ROOT/")
if (substr(pattern, 0, 9) == "$FG_ROOT/")
pattern = root ~ "/" ~ substr(pattern, 9);
elsif(substr(pattern, 0, 9) == "$FG_HOME/")
elsif (substr(pattern, 0, 9) == "$FG_HOME/")
pattern = home ~ "/" ~ substr(pattern, 9);
append(f[0] == "READ" ? read_rules : write_rules, [pattern, f[1] == "ALLOW"]);
}
@ -252,14 +261,14 @@ _setlistener("/sim/signals/nasal-dir-initialized", func {
# catch exceptions so that a die() doesn't ruin everything
var rules_file = call(func load_rules(home ~ "/" ~ config)
or load_rules(root ~ "/" ~ config), nil, var err = []);
if(size(err)) {
if (size(err)) {
debug.printerror(err);
read_rules = write_rules = [];
}
read_rules = [["*/" ~ config, 0]] ~ read_rules;
write_rules = [["*/" ~ config, 0]] ~ write_rules;
if(__.log_level <= 3) {
if (__.log_level <= 3) {
print("io.open()/READ: ", debug.string(read_rules));
print("io.open()/WRITE: ", debug.string(write_rules));
}
@ -274,8 +283,8 @@ _setlistener("/sim/signals/nasal-dir-initialized", func {
var valid = func(path, rules) {
var fpath = fixpath(path);
foreach(var d; rules)
if(match(fpath, d[0]))
foreach (var d; rules)
if (match(fpath, d[0]))
return d[1] ? fpath : nil;
return nil;
}
@ -295,10 +304,10 @@ _setlistener("/sim/signals/nasal-dir-initialized", func {
var _open = io.open;
io.open = var io_open = func(path, mode = "rb") {
var rules = write_rules;
if(mode == "r" or mode == "rb" or mode == "br")
if (mode == "r" or mode == "rb" or mode == "br")
rules = read_rules;
if(var vpath = valid(path, rules))
if (var vpath = valid(path, rules))
return _open(vpath, mode);
die("io.open(): opening file '" ~ path ~ "' denied (unauthorized access)\n ");
@ -308,7 +317,7 @@ _setlistener("/sim/signals/nasal-dir-initialized", func {
var thislistener = caller(0)[1];
var _closure = globals.closure;
globals.closure = func(fn, level = 0) {
if(fn != thislistener and fn != io_open and fn != caller(0)[1]
if (fn != thislistener and fn != io_open and fn != caller(0)[1]
and fn != read_validator and fn != write_validator)
return _closure(fn, level);