next up previous contents index
Next: 6.2.1 The path_sysop/2 interface Up: 6. Standard Predicates Previous: 6.1.6 Special I/O   Contents   Index

6.2 Interactions with the Operating System

XSB provides a number of facilities for interacting with the UNIX and Windows operating systems. This section describes basic facilities for invoking shell commands and file manipulation. Chapter 1 of Volume 2 discusses more advanced commands for process spawning and control, along with interprocess communication.

shell(+SystemCall)

Calls the operating system with the atom SystemCall as argument. It succeeds if SystemCall is executed successfully, otherwise it fails. As a notational convenience, the user can supply SystemCall in the form of a list (something currently not possible for shell/2).

For example, the call:

$\vert$ ?- shell('echo $HOME').

will output in the current output stream of XSB the name of the user's home directory; while the call:

$\vert$ ?- File = 'test.c', shell(['cc -c ', File]).

will call the C compiler to compile the file test.c.

Note that in UNIX systems, since shell/1 is executed by forking off a shell process, it cannot be used, for example, to change the working directory of the interpreter. For that reason the standard predicate cd/1 described below should be used.

shell(+SystemCall, -Result)

Calls the operating system with the atom SystemCall as argument and returns the result of the call in Result. In comparison with shell/1 this predicate always succeeds, even if the SystemCall cannot be successfully executed.

shell_to_list(+SystemCall,-Output,-Result)

Calls the operating system with the atom SystemCall as argument returns the result of the call in Result, and the output of SystemCall tokenized to Output. Output is a list of lists: each element of the outer list corresponds to a line of output from SystemCall, while each element of an inner list corresponds to a token in that line. shell_to_list/3 is thus a sort of Prolog analog of the shell command `SystemCall`.

In comparison with shell/1 this predicate always succeeds, even if the SystemCall cannot be successfully executed.

Example:

shell_to_list(sw_vers,F,G).

F = [[ProductName:,Mac,OS,X],[ProductVersion:,10.4.9],[BuildVersion:,8P2137]]
G = 0

datime(?Date)
standard

Unifies Date to the current date, returned as a Prolog term, suitable for term comparison. Note that datime/1 must be explicitly imported from the module standard.

Example:

                > date
                Mon Aug  9 16:19:44 EDT 2004
                > nxsb1
                XSB Version 2.6 (Duff) of June 24, 2003
                [i686-pc-cygwin; mode: optimal; engine: slg-wam; gc: indirection; scheduling: local]

                | ?- import datime/1 from standard

                yes
                | ?- datime(F).
                F = datime(2004,8,9,20,20,23)

                yes



Subsections
next up previous contents index
Next: 6.2.1 The path_sysop/2 interface Up: 6. Standard Predicates Previous: 6.1.6 Special I/O   Contents   Index
Terrance Swift 2007-10-05