Phi: nicer scroll animation for METAR widget
[fg:fgdata.git] / Nasal / io.nas
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);
5     var sz = st[7];
6     var buf = bits.buf(sz);
7     read(open(file), buf, sz);
8     return buf;
9 }
10
11 # basename(<path>), dirname(<path>)
12 #
13 # Work like standard Unix commands: basename returns the file name from a given
14 # path, and dirname returns the directory part.
15
16 var basename = func(path) {
17     split("/", string.normpath(path))[-1];
18 };
19
20 var dirname =  func(path) {
21     path = string.normpath(path);
22     substr(path, 0, size(path) - size(basename(path)));
23 };
24
25 # include(<filename>)
26 #
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).
29 #
30 # Examples:
31 #
32 #     io.include("Aircraft/Generic/library.nas");
33 #     io.include("my_other_file.nas");
34
35 var include = func(file) {
36
37     file = string.normpath(file);
38     var clr = caller();
39     var (ns, fn, fl) = clr;
40
41     var local_file = dirname(fl) ~ file;
42     var path = (stat(local_file) != nil)? local_file : resolvepath(file);
43
44     if (path == "") die("File not found: ", file);
45
46     var module = "__" ~ path ~ "__";
47     if (contains(ns, module))
48         return;
49
50     var code = call(compile, [readfile(path), path], var err = []);
51     if (size(err)) {
52         if (find("Parse error:", err[0]) < 0)
53             die(err[0]);
54         else
55             die(sprintf("%s\n  in included file: %s", err[0], path));
56     }
57
58     ns[module] = "included";
59     call(bind(code, ns, fn), [], nil, ns);
60 }
61
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.
65 #
66 # Usage:   io.load_nasal(<filename> [, <modulename>]);
67 #
68 # Example:
69 #
70 #     io.load_nasal(getprop("/sim/fg-root") ~ "/Local/test.nas");
71 #     io.load_nasal("/tmp/foo.nas", "test");
72 #
73 var load_nasal = func(file, module = nil) {
74     if (module == nil)
75         module = split(".", split("/", file)[-1])[0];
76
77     printlog("info", "loading ", file, " into namespace ", module);
78
79     if (!contains(globals, module))
80         globals[module] = {};
81     elsif (typeof(globals[module]) != "hash")
82         die("io.load_nasal(): namespace '" ~ module ~ "' already in use, but not a hash");
83
84     var code = call(func compile(readfile(file), file), nil, var err = []);
85     if (size(err)) {
86         if (substr(err[0], 0, 12) == "Parse error:") { # hack around Nasal feature
87             var e = split(" at line ", err[0]);
88             if (size(e) == 2)
89                 err[0] = string.join("", [e[0], "\n  at ", file, ", line ", e[1], "\n "]);
90         }
91         for (var i = 1; (var c = caller(i)) != nil; i += 1)
92             err ~= subvec(c, 2, 2);
93         debug.printerror(err);
94         return 0;
95     }
96     call(bind(code, globals), nil, nil, globals[module], err);
97     debug.printerror(err);
98     return !size(err);
99 }
100
101
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.
107 #
108 # Usage:   io.read_properties(<filename> [, <props.Node or property-path>]);
109 #
110 # Examples:
111 #
112 #     var target = props.globals.getNode("/sim/model");
113 #     io.read_properties("/tmp/foo.xml", target);
114 #
115 #     var data = io.read_properties("/tmp/foo.xml", "/sim/model");
116 #     var data = io.read_properties("/tmp/foo.xml");
117 #
118 var read_properties = func(path, target = nil) {
119     var args = props.Node.new({ filename: path });
120     if (target == nil) {
121         var ret = args.getNode("data", 1);
122     } elsif (isa(target, props.Node)) {
123         args.getNode("targetnode", 1).setValue(target.getPath());
124         var ret = target;
125     } else {
126         args.getNode("targetnode", 1).setValue(target);
127         var ret = props.globals.getNode(target, 1);
128     }
129     return fgcommand("loadxml", args) ? ret : nil;
130 }
131
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.
139 #
140 # Usage:   io.read_airport_properties(<icao>, <filename> [, <props.Node or property-path>]);
141 #
142 # Examples:
143 #
144 #     var data = io.read_properties("KSFO", "rwyuse");
145 #
146 var read_airport_properties = func(icao, fname, target = nil) {
147     var args = props.Node.new({ filename: fname, icao:icao });
148     if (target == nil) {
149         var ret = args.getNode("data", 1);
150     } elsif (isa(target, props.Node)) {
151         args.getNode("targetnode", 1).setValue(target.getPath());
152         var ret = target;
153     } else {
154         args.getNode("targetnode", 1).setValue(target);
155         var ret = props.globals.getNode(target, 1);
156     }
157     return fgcommand("loadxml", args) ? ret : nil;
158 }
159
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.)
166 #
167 # Usage:   io.write_properties(<filename>, <props.Node or property-path>);
168 #
169 # Examples:
170 #
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");
174 #
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;)
182             root = p;
183         if (root.getAttribute() == attr)
184             args.getNode("sourcenode", 1).setValue(prop.getPath());
185         else
186             props.copy(prop, args.getNode("data", 1), 1);
187     } else {
188         args.getNode("sourcenode", 1).setValue(prop);
189     }
190     return fgcommand("savexml", args) ? path : nil;
191 }
192
193
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.
204
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.
210 #
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))
218             index[name] = 0;
219
220         node = node.getChild(name, index[name], 1);
221         if (prefix != nil)
222             foreach (var n; keys(attr))
223                 node.getNode(prefix ~ n, 1).setValue(attr[n]);
224
225         index[name] += 1;
226         append(stack, [{}, ""]);
227     }
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();
233     }
234     var data = func(d) stack[-1][1] ~= d;
235     return parsexml(path, start, end, data) == nil ? nil : tree;
236 }
237
238
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.
243 #
244 var writexml = func(path, node, indent = "\t", prefix = "___") {
245     var root = node.getChildren();
246     if (!size(root))
247         die("writexml(): tree doesn't have a root node");
248     if (substr(path, -4) != ".xml")
249         path ~= ".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;
255         var children = [];
256         foreach (var c; n.getChildren()) {
257             var a = c.getName();
258             if (substr(a, 0, size(prefix)) == prefix)
259                 name_attr ~= " " ~ substr(a, size(prefix)) ~ '="' ~  c.getValue() ~ '"';
260             else
261                 append(children, c);
262         }
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");
270         } else {
271             write(file, ind ~ "<" ~ name_attr ~ "/>\n");
272         }
273     }
274     writenode(root[0]);
275     close(file);
276     if (size(root) != 1)
277         die("writexml(): tree has more than one root node");
278 }
279
280
281 # Redefine io.open() such that files can only be opened under authorized directories.
282 #
283 _setlistener("/sim/signals/nasal-dir-initialized", func {
284     # read IO rules
285     var root = string.normpath(getprop("/sim/fg-root"));
286     var home = string.normpath(getprop("/sim/fg-home"));
287     var config = "Nasal/IOrules";
288
289     var rules_file = nil;
290     var read_rules = [];
291     var write_rules = [];
292
293     var load_rules = func(path) {
294         if (stat(path) == nil)
295             return nil;
296         printlog("info", "using io.open() rules from ", path);
297         read_rules = [];
298         write_rules = [];
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] == `#`)
302                 continue;
303
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 = [];
308                 break;
309             }
310             var pattern = f[2];
311             foreach (var p; subvec(f, 3))
312                 pattern ~= " " ~ p;
313             var rules = f[0] == "READ" ? read_rules : write_rules;
314             var allow = (f[1] == "ALLOW");
315
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);
323                 }
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);
331                 }
332             } else {
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);
337
338                 append(rules, [pattern, allow]);
339                 printlog("info", "IORules: appending ", pattern);
340             }
341         }
342         close(file);
343         return path;
344     }
345
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 = []);
349     if (size(err)) {
350         debug.printerror(err);
351         read_rules = write_rules = [];
352     }
353
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));
359     }
360
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;
366     var caller = caller;
367     var die = die;
368
369     # validators
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;
375         return nil;
376     }
377
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 ""]);
380
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);
385
386     # wrap removelistener
387     globals.removelistener = var remove_listener = (func {
388         var _removelistener = globals.removelistener;
389         func(n) {
390             if (n != rval and n != wval)
391                 return _removelistener(n);
392
393             die("removelistener(): removal of protected listener #'" ~ n ~ "' denied (unauthorized access)\n ");
394         }
395     })();
396
397     # wrap io.open()
398     io.open = var io_open = (func {
399         var _open = io.open;
400         func(path, mode = "rb") {
401             var rules = write_rules;
402             if (mode == "r" or mode == "rb" or mode == "br")
403                 rules = read_rules;
404
405             if (var vpath = valid(path, rules))
406                 return _open(vpath, mode);
407
408             die("io.open(): opening file '" ~ path ~ "' denied (unauthorized access)\n ");
409         }
410     })();
411
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);
422
423             die("closure(): query denied (unauthorized access)\n ");
424         }
425     })();
426 });