Changes between Version 4 and Version 5 of Doc/panc/dml/functions


Ignore:
Timestamp:
Mar 3, 2007, 4:27:40 PM (17 years ago)
Author:
/O=GRID-FR/C=FR/O=CNRS/OU=LAL/CN=Charles Loomis
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Doc/panc/dml/functions

    v4 v5  
    3131"options" = list("noauto", "owner", "ro");
    3232# our first mount entry is the CD coming from hdc
     33"/system/mounts/0" = create("mount_cdrom", "device", "hdc");
     34# this is exactly equivalent to the following two lines
     35"/system/mounts/0" = create("mount_cdrom");
     36"/system/mounts/0/device" = "hdc";
    3337}}}
    3438
     
    4246debug("x is positive: " + to_string(x));
    4347}}}
     48
     49=== delete( arg:variable ) ===
     50
     51This function deletes the element identified by the ”variable expression” (i.e. variable name with optional
     52subscript such as foo or foo[123] or foo[123][”abc”]. . . ).
     53{{{
     54# the following will put the list ("a", "c") at path "/x"
     55"/x" = {
     56x = list("a", "b", "c");
     57delete(x[1]);
     58return(x);
     59};
     60}}}
     61
     62=== error( message:string ) ===
     63
     64This function prints the given message on stderr and aborts the compilation.
     65
     66{{{
     67# user function requiring one long argument
     68define function foo = {
     69if (argc != 1)
     70error("foo(): wrong number of arguments: " + to_string(argc));
     71if (!is_long(argv[0]))
     72error("foo(): argument is not a long");
     73# normal processing...
     74};
     75}}}
     76
     77=== escape( arg:string ) : string ===
     78
     79This function escape non alphanumeric characters in the given string so that it can be used inside paths,
     80for instance as an named list key.
     81
     82{{{
     83"/test" = escape("1+1");
     84# will be the string "1_2b1"
     85}}}
     86
     87=== exists( arg:variable ) : boolean ===
     88
     89This function checks if the ”variable expression” (see delete function) corresponds to an existing
     90element.
     91
     92=== exists( path:string ) : boolean ===
     93
     94This function checks if the path corresponds to an existing element.
     95
     96=== exists( template:string ) : boolean ===
     97
     98This function checks if the given template exists.
     99
     100=== first( arg:resource, key:identifier, value:identifier ) : boolean ===
     101
     102This function resets the iterator associated with arg so that it points to its first child element; if there is
     103one (i.e. if the resource is not empty), sets the variable key to the ”key” of this element (i.e. a long if arg
     104is a list or a string if it is a nlist) and the variable value to the child element itself and return true; if key
     105or value is undef, don’t assign it; if there is no such child element, simply return false.
     106
     107{{{
     108# compute the sum of the elements inside numlist
     109numlist = list(1, 2, 4, 8);
     110sum = 0;
     111ok = first(numlist, k, v);
     112while (ok) {
     113sum = sum + v;
     114ok = next(numlist, k, v);
     115};
     116# sum will be 15
     117# put the list of all the keys of table inside keys
     118table = nlist("a", 1, "b", 2, "c", 3);
     119keys = list();
     120ok = first(table, k, v);
     121while (ok) {
     122keys[length(keys)] = k;ok = next(table, k, v);
     123};
     124# keys will be ("a", "b", "c")
     125}}}
     126
     127=== index ( sub:string, arg:string, [ start:long ] ) : long ===
     128
     129This function searches for the given substring inside the given string and returns its position from the
     130beginning of the string or -1 if not found; if the third argument is given, starts only from that position.
     131
     132{{{
     133"/s1" = index("foo", "abcfoodefoobar"); # 3
     134"/s2" = index("f0o", "abcfoodefoobar"); # -1
     135"/s3" = index("foo", "abcfoodefoobar", 4); # 8
     136}}}
     137
     138=== index ( sub:property, arg:list, [ start:long ] ) : long ===
     139
     140This function searches for the given property inside the given list of properties and returns its position or
     141-1 if not found; if the third argument is given, starts only from that position; it is an error if sub and arg’s
     142children are not of the same type.
     143
     144{{{
     145# search in a list of strings
     146"/l1" = index("foo", list("Foo", "FOO", "foo", "bar"));
     147# will be 2
     148# search in a list of longs
     149"/l2" = index(1, list(3, 1, 4, 1, 6), 2);
     150# will be 3
     151}}}
     152
     153=== index ( sub:property, arg:nlist, [ start:long ] ) : string ===
     154
     155This function searches for the given property inside the given named list of properties and returns its
     156name or the empty string if not found; if the third argument is given, skips that many matching children;
     157it is an error if sub and arg’s children are not of the same type.
     158
     159{{{
     160# simple color table
     161"/table" = nlist("red", 0xf00, "green", 0x0f0, "blue", 0x00f);
     162"/name1" = index(0x0f0, value("/table"));
     163# will be the string "green"
     164"/name2" = index(0x0f0, value("/table"), 1);
     165# will be the string ""
     166}}}
     167
     168=== index ( sub:nlist, arg:list, [ start:long ] ) : long ===
     169
     170This function searches for the given named list inside the given list of named lists and returns its position
     171or -1 if not found; the comparison is done by comparing all the children of sub, these children must all
     172be properties; if the third argument is given, starts only from that position; it is an error if sub and arg’s
     173children are not of the same type or if their common children don’t have the same type.
     174
     175{{{
     176# search a record in a list of records
     177"/l1" = index(nlist("key", "foo"), list(nlist("key", "bar", "val", 101),
     178nlist("key", "foo", "val", 102)));
     179# will be 1 (i.e. the second nlist)
     180}}}
     181
     182=== index ( sub:nlist, arg:nlist, [ start:long ] ) : string ===
     183
     184This function searches for the given named list inside the given named list of named lists and returns its
     185name or the empty string if not found; if the third argument is given, skips that many matching children;
     186it is an error if sub and arg’s children are not of the same type or if their common children don’t have the
     187same type.
     188
     189=== is_boolean( arg:element ) : boolean ===
     190
     191This function checks if the given argument is a boolean.
     192
     193=== is_defined( arg:element ) : boolean ===
     194
     195This function checks if the given argument is defined (i.e. is anything but undef or null).
     196
     197=== is_double( arg:element ) : boolean ===
     198
     199This function checks if the given argument is a double.
     200
     201=== is_list( arg:element ) : boolean ===
     202
     203This function checks if the given argument is a list.
     204
     205=== is_long( arg:element ) : boolean ===
     206
     207This function checks if the given argument is a long.
     208
     209=== is_nlist( arg:element ) : boolean ===
     210
     211This function checks if the given argument is a nlist.
     212
     213=== is_null( arg:element ) : boolean ===
     214
     215This function checks if the given argument has a null value (i.e. is anything but null).
     216
     217=== is_property( arg:element ) : boolean ===
     218
     219This function checks if the given argument is a property.
     220
     221=== is_resource( arg:element ) : boolean ===
     222
     223This function checks if the given argument is a resource.
     224
     225=== is_string( arg:element ) : boolean ===
     226
     227This function checks if the given argument is a string.
     228
     229=== key( arg:nlist, index:long ) : string ===
     230
     231This function returns the name of the child identified by its index, this can be used to iterate through all
     232the children of a named list.
     233
     234{{{
     235"/table" = nlist("red", 0xf00, "green", 0x0f0, "blue", 0x00f);
     236"/keys" = {
     237tbl = value("/table");
     238res = "";
     239len = length(tbl);
     240idx = 0;
     241while (idx < len) {
     242res = res + key(tbl, idx) + " ";
     243idx = idx + 1;
     244};
     245if (length(res) > 0)
     246splice(res, -1, 1);
     247return(res);
     248};
     249# /keys will be the string "red green blue"
     250}}}
     251
     252=== length( arg:string ) : long ===
     253
     254This function returns the length of the given string.
     255
     256=== length( arg:resource ) : long ===
     257
     258This function returns the number of children of the given resource.
     259
     260=== list( ... ) : list ===
     261
     262This function returns a list made of its arguments.
     263
     264{{{
     265# the list of mount entries is empty
     266"/system/mounts = list();
     267# we define two DNS servers
     268"/system/dns/servers" = list("137.138.16.5", "137.138.17.5");
     269# this machine has only one CPU
     270"/hardware/cpus" = list(create("cpu_intel_p3_850"));
     271}}}
     272
     273=== match( arg:string, regexp:string ) : boolean ===
     274
     275This function checks if the given string matches the regular expression.
     276
     277{{{
     278# device_t is a string that can only be "disk", "cd" or "net"
     279type device_t = string with match(self, ’ˆ(disk|cd|net)$’);
     280}}}
     281
     282=== matches( arg:string, regexp:string ) : list ===
     283This function matches the given string against the regular expression and returns the list of captured
     284substrings, the first one being the complete matched string (i.e. $& in Perl).
     285
     286{{{
     287# IPv4 address in dotted number notation
     288type ipv4 = string with {
     289result = matches(self, ’ˆ(\d+)\.(\d+)\.(\d+)\.(\d+)$’);
     290if (length(result) == 0)
     291return("bad string");
     292i = 1;
     293while (i <= 4) {
     294x = to_long(result[i]);
     295if (x > 255)
     296return("chunk " + to_string(i) + " too big: " + result[i]);
     297i = i + 1;
     298};
     299return(true);
     300};
     301}}}
     302
     303=== merge( ... ) : resource ===
     304
     305This function returns the resource which is the merge of its arguments which must be of the same type:
     306either all lists or all named lists; if several named lists have children of the same name, an error occurs.
     307
     308{{{
     309"/x" = list("a", "b", "c");
     310"/y" = list("d", "e");
     311"/z" = merge (value("/x"), value("/y"));
     312# /z will contain the list "a", "b", "c", "d", "e"
     313}}}
     314
     315=== next( arg:resource, key:identifier, value:identifier ) : boolean ===
     316
     317This function increments the iterator associated with arg so that it points to the next child element and
     318then behaves like first.
     319
     320=== nlist( ... ) : nlist ===
     321
     322This function returns a named list made of its arguments which must be pairs of key (i.e. child’s name, a
     323string) and value (i.e. child’s value, an element).
     324
     325{{{
     326# hda1 is our root filesystem
     327"/system/mounts/0" = nlist("type", "ext2", "path", "/", "device", "hda1");
     328# hda2 is our /var filesystem
     329"/system/mounts/1" = nlist(
     330"type", "ext2",
     331"path", "/var",
     332"device", "hda2",
     333);
     334}}}
     335
     336=== return( arg:element ) : element ===
     337
     338This function interrupts the processing of the current DML block and returns from it with the given value,
     339this is often used in user defined functions.
     340
     341{{{
     342function facto = {
     343  if (argv[0] < 2) return(1);
     344  return(argv[0] * facto(argv[0] - 1));
     345};
     346}}}
     347
     348=== splice ( arg:string, start:long, length:long, [ new:string ] ) : string ===
     349
     350This function deletes the substring identified by start and length (see substr’s documentation for de-
     351tails) and, if a fourth argument is given, replaces it with new.
     352
     353{{{
     354"/s1" = splice("abcde", 2, 0, "12"); # ab12cde
     355"/s2" = splice("abcde", -2, 1); # abce
     356"/s3" = splice("abcde", 2, 2, "XXX"); # abXXXe
     357}}}
     358
     359=== splice ( arg:list, start:long, length:long, [ new:list] ) : list ===
     360
     361This function deletes the children of the given list identified by start and length (see substr’s docu-
     362mentation for details) and, if a fourth argument is given, replaces them with the contents of new.
     363
     364{{{
     365"/l1" = splice(list("a","b","c","d","e"), 2, 0, list(1,2));
     366# will be the list "a", "b", 1, 2, "c", "d", "e"
     367"/l2" = splice(list("a","b","c","d","e"), -2, 1);
     368# will be the list "a", "b", "c", "e"
     369"/l3" = splice(list("a","b","c","d","e"), 2, 2, list("XXX"));
     370# will be the list "a", "b", "XXX", "e"
     371}}}
     372
     373=== substr ( arg:string, start:long, [ length:long ] ) : string ===
     374
     375This function returns the part of the given string characterised by its start position (starting from 0) and
     376its length (if omitted, returns everything to the end of the string); if start is negative, starts that far from
     377the end of the string; if length is negative, leaves that many characters off the end of the string.
     378
     379{{{
     380"/s1" = substr("abcdef", 2); # cdef
     381"/s2" = substr("abcdef", 1, 1); # b
     382"/s3" = substr("abcdef", 1, -1); # bcde
     383"/s4" = substr("abcdef", -4); # cdef
     384"/s5" = substr("abcdef", -4, 1); # c
     385"/s6" = substr("abcdef", -4, -1); # cde
     386}}}
     387
     388=== to_boolean( arg:property ) : boolean ===
     389
     390This function converts the given property into a boolean; 0 and the empty strings are considered as false,
     391everything else as true.
     392
     393=== to_double( arg:property ) : double ===
     394
     395This function converts the given property into a double; it is an error if the given string does not represent
     396a double.
     397
     398=== to_long( arg:property ) : long ===
     399
     400This function converts the given property into a long; it is an error if the given string does not represent
     401a long; if the argument is a double, it will be rounded to the nearest long.
     402
     403=== to_string( arg:property ) : string ===
     404
     405This function converts the given property into a string.
     406
     407=== unescape( arg:string ) : string ===
     408
     409This function replace escaped characters in the given string to get back the original string, this is the
     410inverse of the escape function.
     411
     412=== value( path:string ) : element ===
     413
     414This function returns the element identified by its path (which can be an external path), an error occurs if
     415there is no such element.
     416
     417{{{
     418"/x" = 100;
     419"/y" = 2 * value("/x");
     420# /y will be 200
     421# we add one DNS server to the current list
     422# (we need to use list() because merge() requires lists)
     423"/system/dns/servers" = merge(value("/system/dns/servers"),
     424list("137.138.16.5"));
     425# the RAM of this machine is the same as the machine foo
     426"/hardware/memory/size" = value("//foo/hardware/memory/size");
     427}}}