| 48 | |
| 49 | === delete( arg:variable ) === |
| 50 | |
| 51 | This function deletes the element identified by the ”variable expression” (i.e. variable name with optional |
| 52 | subscript 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" = { |
| 56 | x = list("a", "b", "c"); |
| 57 | delete(x[1]); |
| 58 | return(x); |
| 59 | }; |
| 60 | }}} |
| 61 | |
| 62 | === error( message:string ) === |
| 63 | |
| 64 | This function prints the given message on stderr and aborts the compilation. |
| 65 | |
| 66 | {{{ |
| 67 | # user function requiring one long argument |
| 68 | define function foo = { |
| 69 | if (argc != 1) |
| 70 | error("foo(): wrong number of arguments: " + to_string(argc)); |
| 71 | if (!is_long(argv[0])) |
| 72 | error("foo(): argument is not a long"); |
| 73 | # normal processing... |
| 74 | }; |
| 75 | }}} |
| 76 | |
| 77 | === escape( arg:string ) : string === |
| 78 | |
| 79 | This function escape non alphanumeric characters in the given string so that it can be used inside paths, |
| 80 | for 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 | |
| 89 | This function checks if the ”variable expression” (see delete function) corresponds to an existing |
| 90 | element. |
| 91 | |
| 92 | === exists( path:string ) : boolean === |
| 93 | |
| 94 | This function checks if the path corresponds to an existing element. |
| 95 | |
| 96 | === exists( template:string ) : boolean === |
| 97 | |
| 98 | This function checks if the given template exists. |
| 99 | |
| 100 | === first( arg:resource, key:identifier, value:identifier ) : boolean === |
| 101 | |
| 102 | This function resets the iterator associated with arg so that it points to its first child element; if there is |
| 103 | one (i.e. if the resource is not empty), sets the variable key to the ”key” of this element (i.e. a long if arg |
| 104 | is a list or a string if it is a nlist) and the variable value to the child element itself and return true; if key |
| 105 | or 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 |
| 109 | numlist = list(1, 2, 4, 8); |
| 110 | sum = 0; |
| 111 | ok = first(numlist, k, v); |
| 112 | while (ok) { |
| 113 | sum = sum + v; |
| 114 | ok = next(numlist, k, v); |
| 115 | }; |
| 116 | # sum will be 15 |
| 117 | # put the list of all the keys of table inside keys |
| 118 | table = nlist("a", 1, "b", 2, "c", 3); |
| 119 | keys = list(); |
| 120 | ok = first(table, k, v); |
| 121 | while (ok) { |
| 122 | keys[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 | |
| 129 | This function searches for the given substring inside the given string and returns its position from the |
| 130 | beginning 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 | |
| 140 | This 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 |
| 142 | children 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 | |
| 155 | This function searches for the given property inside the given named list of properties and returns its |
| 156 | name or the empty string if not found; if the third argument is given, skips that many matching children; |
| 157 | it 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 | |
| 170 | This function searches for the given named list inside the given list of named lists and returns its position |
| 171 | or -1 if not found; the comparison is done by comparing all the children of sub, these children must all |
| 172 | be properties; if the third argument is given, starts only from that position; it is an error if sub and arg’s |
| 173 | children 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), |
| 178 | nlist("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 | |
| 184 | This function searches for the given named list inside the given named list of named lists and returns its |
| 185 | name or the empty string if not found; if the third argument is given, skips that many matching children; |
| 186 | it is an error if sub and arg’s children are not of the same type or if their common children don’t have the |
| 187 | same type. |
| 188 | |
| 189 | === is_boolean( arg:element ) : boolean === |
| 190 | |
| 191 | This function checks if the given argument is a boolean. |
| 192 | |
| 193 | === is_defined( arg:element ) : boolean === |
| 194 | |
| 195 | This function checks if the given argument is defined (i.e. is anything but undef or null). |
| 196 | |
| 197 | === is_double( arg:element ) : boolean === |
| 198 | |
| 199 | This function checks if the given argument is a double. |
| 200 | |
| 201 | === is_list( arg:element ) : boolean === |
| 202 | |
| 203 | This function checks if the given argument is a list. |
| 204 | |
| 205 | === is_long( arg:element ) : boolean === |
| 206 | |
| 207 | This function checks if the given argument is a long. |
| 208 | |
| 209 | === is_nlist( arg:element ) : boolean === |
| 210 | |
| 211 | This function checks if the given argument is a nlist. |
| 212 | |
| 213 | === is_null( arg:element ) : boolean === |
| 214 | |
| 215 | This function checks if the given argument has a null value (i.e. is anything but null). |
| 216 | |
| 217 | === is_property( arg:element ) : boolean === |
| 218 | |
| 219 | This function checks if the given argument is a property. |
| 220 | |
| 221 | === is_resource( arg:element ) : boolean === |
| 222 | |
| 223 | This function checks if the given argument is a resource. |
| 224 | |
| 225 | === is_string( arg:element ) : boolean === |
| 226 | |
| 227 | This function checks if the given argument is a string. |
| 228 | |
| 229 | === key( arg:nlist, index:long ) : string === |
| 230 | |
| 231 | This function returns the name of the child identified by its index, this can be used to iterate through all |
| 232 | the children of a named list. |
| 233 | |
| 234 | {{{ |
| 235 | "/table" = nlist("red", 0xf00, "green", 0x0f0, "blue", 0x00f); |
| 236 | "/keys" = { |
| 237 | tbl = value("/table"); |
| 238 | res = ""; |
| 239 | len = length(tbl); |
| 240 | idx = 0; |
| 241 | while (idx < len) { |
| 242 | res = res + key(tbl, idx) + " "; |
| 243 | idx = idx + 1; |
| 244 | }; |
| 245 | if (length(res) > 0) |
| 246 | splice(res, -1, 1); |
| 247 | return(res); |
| 248 | }; |
| 249 | # /keys will be the string "red green blue" |
| 250 | }}} |
| 251 | |
| 252 | === length( arg:string ) : long === |
| 253 | |
| 254 | This function returns the length of the given string. |
| 255 | |
| 256 | === length( arg:resource ) : long === |
| 257 | |
| 258 | This function returns the number of children of the given resource. |
| 259 | |
| 260 | === list( ... ) : list === |
| 261 | |
| 262 | This 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 | |
| 275 | This 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" |
| 279 | type device_t = string with match(self, ’ˆ(disk|cd|net)$’); |
| 280 | }}} |
| 281 | |
| 282 | === matches( arg:string, regexp:string ) : list === |
| 283 | This function matches the given string against the regular expression and returns the list of captured |
| 284 | substrings, the first one being the complete matched string (i.e. $& in Perl). |
| 285 | |
| 286 | {{{ |
| 287 | # IPv4 address in dotted number notation |
| 288 | type ipv4 = string with { |
| 289 | result = matches(self, ’ˆ(\d+)\.(\d+)\.(\d+)\.(\d+)$’); |
| 290 | if (length(result) == 0) |
| 291 | return("bad string"); |
| 292 | i = 1; |
| 293 | while (i <= 4) { |
| 294 | x = to_long(result[i]); |
| 295 | if (x > 255) |
| 296 | return("chunk " + to_string(i) + " too big: " + result[i]); |
| 297 | i = i + 1; |
| 298 | }; |
| 299 | return(true); |
| 300 | }; |
| 301 | }}} |
| 302 | |
| 303 | === merge( ... ) : resource === |
| 304 | |
| 305 | This function returns the resource which is the merge of its arguments which must be of the same type: |
| 306 | either 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 | |
| 317 | This function increments the iterator associated with arg so that it points to the next child element and |
| 318 | then behaves like first. |
| 319 | |
| 320 | === nlist( ... ) : nlist === |
| 321 | |
| 322 | This function returns a named list made of its arguments which must be pairs of key (i.e. child’s name, a |
| 323 | string) 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 | |
| 338 | This function interrupts the processing of the current DML block and returns from it with the given value, |
| 339 | this is often used in user defined functions. |
| 340 | |
| 341 | {{{ |
| 342 | function 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 | |
| 350 | This function deletes the substring identified by start and length (see substr’s documentation for de- |
| 351 | tails) 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 | |
| 361 | This function deletes the children of the given list identified by start and length (see substr’s docu- |
| 362 | mentation 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 | |
| 375 | This function returns the part of the given string characterised by its start position (starting from 0) and |
| 376 | its length (if omitted, returns everything to the end of the string); if start is negative, starts that far from |
| 377 | the 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 | |
| 390 | This function converts the given property into a boolean; 0 and the empty strings are considered as false, |
| 391 | everything else as true. |
| 392 | |
| 393 | === to_double( arg:property ) : double === |
| 394 | |
| 395 | This function converts the given property into a double; it is an error if the given string does not represent |
| 396 | a double. |
| 397 | |
| 398 | === to_long( arg:property ) : long === |
| 399 | |
| 400 | This function converts the given property into a long; it is an error if the given string does not represent |
| 401 | a long; if the argument is a double, it will be rounded to the nearest long. |
| 402 | |
| 403 | === to_string( arg:property ) : string === |
| 404 | |
| 405 | This function converts the given property into a string. |
| 406 | |
| 407 | === unescape( arg:string ) : string === |
| 408 | |
| 409 | This function replace escaped characters in the given string to get back the original string, this is the |
| 410 | inverse of the escape function. |
| 411 | |
| 412 | === value( path:string ) : element === |
| 413 | |
| 414 | This function returns the element identified by its path (which can be an external path), an error occurs if |
| 415 | there 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"), |
| 424 | list("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 | }}} |