1 # Reads and returns a complete file as a string
2 var readfile = func(file) {
3 if ((var st = stat(file)) == nil)
4 die("Cannot stat file: " ~ file);
6 var buf = bits.buf(sz);
7 read(open(file), buf, sz);
11 # basename(<path>), dirname(<path>)
13 # Work like standard Unix commands: basename returns the file name from a given
14 # path, and dirname returns the directory part.
16 var basename = func(path) {
17 split("/", string.normpath(path))[-1];
20 var dirname = func(path) {
21 path = string.normpath(path);
22 substr(path, 0, size(path) - size(basename(path)));
27 # Loads and executes a Nasal file in place. The file is searched for in the
28 # calling script directory and in standard FG directories (in that order).
32 # io.include("Aircraft/Generic/library.nas");
33 # io.include("my_other_file.nas");
35 var include = func(file) {
37 file = string.normpath(file);
39 var (ns, fn, fl) = clr;
41 var local_file = dirname(fl) ~ file;
42 var path = (stat(local_file) != nil)? local_file : resolvepath(file);
44 if (path == "") die("File not found: ", file);
46 var module = "__" ~ path ~ "__";
47 if (contains(ns, module))
50 var code = call(compile, [readfile(path), path], var err = []);
52 if (find("Parse error:", err[0]) < 0)
55 die(sprintf("%s\n in included file: %s", err[0], path));
58 ns[module] = "included";
59 call(bind(code, ns, fn), [], nil, ns);
62 # Loads Nasal file into namespace and executes it. The namespace
63 # (module name) is taken from the optional second argument, or
64 # derived from the Nasal file's name.
66 # Usage: io.load_nasal(<filename> [, <modulename>]);
70 # io.load_nasal(getprop("/sim/fg-root") ~ "/Local/test.nas");
71 # io.load_nasal("/tmp/foo.nas", "test");
73 var load_nasal = func(file, module = nil) {
75 module = split(".", split("/", file)[-1])[0];
77 printlog("info", "loading ", file, " into namespace ", module);
79 if (!contains(globals, module))
81 elsif (typeof(globals[module]) != "hash")
82 die("io.load_nasal(): namespace '" ~ module ~ "' already in use, but not a hash");
84 var code = call(func compile(readfile(file), file), nil, var err = []);
86 if (substr(err[0], 0, 12) == "Parse error:") { # hack around Nasal feature
87 var e = split(" at line ", err[0]);
89 err[0] = string.join("", [e[0], "\n at ", file, ", line ", e[1], "\n "]);
91 for (var i = 1; (var c = caller(i)) != nil; i += 1)
92 err ~= subvec(c, 2, 2);
93 debug.printerror(err);
96 call(bind(code, globals), nil, nil, globals[module], err);
97 debug.printerror(err);
102 # Load XML file in FlightGear's native <PropertyList> format.
103 # If the second, optional target parameter is set, then the properties
104 # are loaded to this node in the global property tree. Otherwise they
105 # are returned as a separate props.Node tree. Returns the data as a
106 # props.Node on success or nil on error.
108 # Usage: io.read_properties(<filename> [, <props.Node or property-path>]);
112 # var target = props.globals.getNode("/sim/model");
113 # io.read_properties("/tmp/foo.xml", target);
115 # var data = io.read_properties("/tmp/foo.xml", "/sim/model");
116 # var data = io.read_properties("/tmp/foo.xml");
118 var read_properties = func(path, target = nil) {
119 var args = props.Node.new({ filename: path });
121 var ret = args.getNode("data", 1);
122 } elsif (isa(target, props.Node)) {
123 args.getNode("targetnode", 1).setValue(target.getPath());
126 args.getNode("targetnode", 1).setValue(target);
127 var ret = props.globals.getNode(target, 1);
129 return fgcommand("loadxml", args) ? ret : nil;
132 # Load XML file in FlightGear's native <PropertyList> format.
133 # file will be located in the airport-scenery directories according to
134 # ICAO and filename, i,e in Airports/I/C/A/ICAO.filename.xml
135 # If the second, optional target parameter is set, then the properties
136 # are loaded to this node in the global property tree. Otherwise they
137 # are returned as a separate props.Node tree. Returns the data as a
138 # props.Node on success or nil on error.
140 # Usage: io.read_airport_properties(<icao>, <filename> [, <props.Node or property-path>]);
144 # var data = io.read_properties("KSFO", "rwyuse");
146 var read_airport_properties = func(icao, fname, target = nil) {
147 var args = props.Node.new({ filename: fname, icao:icao });
149 var ret = args.getNode("data", 1);
150 } elsif (isa(target, props.Node)) {
151 args.getNode("targetnode", 1).setValue(target.getPath());
154 args.getNode("targetnode", 1).setValue(target);
155 var ret = props.globals.getNode(target, 1);
157 return fgcommand("loadxml", args) ? ret : nil;
160 # Write XML file in FlightGear's native <PropertyList> format.
161 # Returns the filename on success or nil on error. If the source
162 # is a props.Node that refers to a node in the main tree, then
163 # the data are directly written from the tree, yielding a more
164 # accurate result. Otherwise the data need to be copied first,
165 # which may slightly change node types (FLOAT becomes DOUBLE etc.)
167 # Usage: io.write_properties(<filename>, <props.Node or property-path>);
171 # var data = props.Node.new({ a:1, b:2, c:{ d:3, e:4 } });
172 # io.write_properties("/tmp/foo.xml", data);
173 # io.write_properties("/tmp/foo.xml", "/sim/model");
175 var write_properties = func(path, prop) {
176 var args = props.Node.new({ filename: path });
177 # default attributes of a new node plus the lowest unused bit
178 var attr = args.getAttribute() + args.getAttribute("last") * 2;
179 props.globals.setAttribute(attr);
180 if (isa(prop, props.Node)) {
181 for (var root = prop; (var p = root.getParent()) != nil;)
183 if (root.getAttribute() == attr)
184 args.getNode("sourcenode", 1).setValue(prop.getPath());
186 props.copy(prop, args.getNode("data", 1), 1);
188 args.getNode("sourcenode", 1).setValue(prop);
190 return fgcommand("savexml", args) ? path : nil;
194 # The following two functions are for reading generic XML files into
195 # the property tree and for writing them from there to the disk. The
196 # built-in fgcommands (load, save, loadxml, savexml) are for FlightGear's
197 # own <PropertyList> XML files only, as they only handle a limited
198 # number of very specific attributes. The io.readxml() loader turns
199 # attributes into regular children with a configurable prefix prepended
200 # to their name, while io.writexml() turns such nodes back into
201 # attributes. The two functions have their own limitations, but can
202 # easily get extended to whichever needs. The underlying parsexml()
203 # command will handle any XML file.
205 # Reads an XML file from an absolute path and returns it as property
206 # tree. All nodes will be of type STRING. Data are only written to
207 # leafs. Attributes are written as regular nodes with the optional
208 # prefix prepended to the name. If the prefix is nil, then attributes
209 # are ignored. Returns nil on error.
211 var readxml = func(path, prefix = "___") {
212 var stack = [[{}, ""]];
213 var node = props.Node.new();
214 var tree = node; # prevent GC
215 var start = func(name, attr) {
216 var index = stack[-1][0];
217 if (!contains(index, name))
220 node = node.getChild(name, index[name], 1);
222 foreach (var n; keys(attr))
223 node.getNode(prefix ~ n, 1).setValue(attr[n]);
226 append(stack, [{}, ""]);
228 var end = func(name) {
229 var buf = pop(stack);
230 if (!size(buf[0]) and size(buf[1]))
231 node.setValue(buf[1]);
232 node = node.getParent();
234 var data = func(d) stack[-1][1] ~= d;
235 return parsexml(path, start, end, data) == nil ? nil : tree;
239 # Writes a property tree as returned by readxml() to a file. Children
240 # with name starting with <prefix> are again turned into attributes of
241 # their parent. <node> must contain exactly one child, which will
242 # become the XML file's outermost element.
244 var writexml = func(path, node, indent = "\t", prefix = "___") {
245 var root = node.getChildren();
247 die("writexml(): tree doesn't have a root node");
248 if (substr(path, -4) != ".xml")
250 var file = open(path, "w");
251 write(file, "<?xml version=\"1.0\"?>\n\n");
252 var writenode = func(n, ind = "") {
253 var name = n.getName();
254 var name_attr = name;
256 foreach (var c; n.getChildren()) {
258 if (substr(a, 0, size(prefix)) == prefix)
259 name_attr ~= " " ~ substr(a, size(prefix)) ~ '="' ~ c.getValue() ~ '"';
263 if (size(children)) {
264 write(file, ind ~ "<" ~ name_attr ~ ">\n");
265 foreach (var c; children)
266 writenode(c, ind ~ indent);
267 write(file, ind ~ "</" ~ name ~ ">\n");
268 } elsif ((var value = n.getValue()) != nil) {
269 write(file, ind ~ "<" ~ name_attr ~ ">" ~ value ~ "</" ~ name ~ ">\n");
271 write(file, ind ~ "<" ~ name_attr ~ "/>\n");
277 die("writexml(): tree has more than one root node");
281 # Redefine io.open() such that files can only be opened under authorized directories.
283 _setlistener("/sim/signals/nasal-dir-initialized", func {
285 var root = string.normpath(getprop("/sim/fg-root"));
286 var home = string.normpath(getprop("/sim/fg-home"));
287 var config = "Nasal/IOrules";
289 var rules_file = nil;
291 var write_rules = [];
293 var load_rules = func(path) {
294 if (stat(path) == nil)
296 printlog("info", "using io.open() rules from ", path);
299 var file = open(path, "r");
300 for (var no = 1; (var line = readln(file)) != nil; no += 1) {
301 if (!size(line) or line[0] == `#`)
304 var f = split(" ", line);
305 if (size(f) < 3 or f[0] != "READ" and f[0] != "WRITE" or f[1] != "DENY" and f[1] != "ALLOW") {
306 printlog("alert", "ERROR: invalid io.open() rule in ", path, ", line ", no, ": ", line);
307 read_rules = write_rules = [];
311 foreach (var p; subvec(f, 3))
313 var rules = f[0] == "READ" ? read_rules : write_rules;
314 var allow = (f[1] == "ALLOW");
316 if (substr(pattern, 0, 13) == "$FG_AIRCRAFT/") {
317 var p = substr(pattern, 13);
318 var sim = props.globals.getNode("/sim");
319 foreach (var c; sim.getChildren("fg-aircraft")) {
320 pattern = string.normpath(c.getValue()) ~ "/" ~ p;
321 append(rules, [pattern, allow]);
322 printlog("info", "IORules: appending ", pattern);
324 } elsif (substr(pattern, 0, 12) == "$FG_SCENERY/") {
325 var p = substr(pattern, 12);
326 var sim = props.globals.getNode("/sim");
327 foreach (var c; sim.getChildren("fg-scenery")) {
328 pattern = string.normpath(c.getValue()) ~ "/" ~ p;
329 append(rules, [pattern, allow]);
330 printlog("info", "IORules: appending ", pattern);
333 if (substr(pattern, 0, 9) == "$FG_ROOT/")
334 pattern = root ~ "/" ~ substr(pattern, 9);
335 elsif (substr(pattern, 0, 9) == "$FG_HOME/")
336 pattern = home ~ "/" ~ substr(pattern, 9);
338 append(rules, [pattern, allow]);
339 printlog("info", "IORules: appending ", pattern);
346 # catch exceptions so that a die() doesn't ruin everything
347 var rules_file = call(func load_rules(home ~ "/" ~ config)
348 or load_rules(root ~ "/" ~ config), nil, var err = []);
350 debug.printerror(err);
351 read_rules = write_rules = [];
354 read_rules = [["*/" ~ config, 0]] ~ read_rules;
355 write_rules = [["*/" ~ config, 0]] ~ write_rules;
356 if (__.log_level <= 3) {
357 print("IOrules/READ: ", debug.string(read_rules));
358 print("IOrules/WRITE: ", debug.string(write_rules));
361 # make safe, local copies
362 var setValue = props._setValue;
363 var getValue = props._getValue;
364 var normpath = string.normpath;
365 var match = string.match;
370 var valid = func(path, rules) {
371 var fpath = normpath(path);
372 foreach (var d; rules)
373 if (match(fpath, d[0]))
374 return d[1] ? fpath : nil;
378 var read_validator = func(n) setValue(n, [valid(getValue(n, []), read_rules) or ""]);
379 var write_validator = func(n) setValue(n, [valid(getValue(n, []), write_rules) or ""]);
381 # validation listeners for load[xml]/save[xml]/parsexml() (see utils.cxx:fgValidatePath)
382 var n = props.globals.getNode("/sim/paths/validate", 1).removeAllChildren();
383 var rval = _setlistener(n.getNode("read", 1)._g, read_validator);
384 var wval = _setlistener(n.getNode("write", 1)._g, write_validator);
386 # wrap removelistener
387 globals.removelistener = var remove_listener = (func {
388 var _removelistener = globals.removelistener;
390 if (n != rval and n != wval)
391 return _removelistener(n);
393 die("removelistener(): removal of protected listener #'" ~ n ~ "' denied (unauthorized access)\n ");
398 io.open = var io_open = (func {
400 func(path, mode = "rb") {
401 var rules = write_rules;
402 if (mode == "r" or mode == "rb" or mode == "br")
405 if (var vpath = valid(path, rules))
406 return _open(vpath, mode);
408 die("io.open(): opening file '" ~ path ~ "' denied (unauthorized access)\n ");
412 # wrap closure() to prevent tampering with security related functions
413 var thislistener = caller(0)[1];
414 globals.closure = (func {
415 var _closure = globals.closure;
416 func(fn, level = 0) {
417 var thisfunction = caller(0)[1];
418 if (fn != thislistener and fn != io_open and fn != thisfunction
419 and fn != read_validator and fn != write_validator
420 and fn != remove_listener)
421 return _closure(fn, level);
423 die("closure(): query denied (unauthorized access)\n ");