next up previous contents index
Next: 1.7.1 Communication with Subprocesses Up: 1. Library Utilities Previous: 1.6 String Manipulation   Contents   Index

1.7 Script Writing Utilities

Prolog, (in particular XSB!) can be useful for writing scripts. Prolog's simple syntax and declarative semantics make it especially suitable for scripts that involve text processing. There are several ways to access script-writing commands from XSB. The first is to execute the command via the predicates shell/1 or shell/2. These predicates can execute any command but they do not provide streamability across UNIX and Windows commands, and they do not return any output of commands to Prolog. Special predicates are provided to handle cross-platform compatibility and to bring output into XSB.

Effort has been made to make the these thread-safe; however in Version 3.0, calls to the XSB script writing utilities go through a single mutex, and may cause contention if many threads seek to concurrently use sockets.

expand_filename(+FileName,-ExpandedName)
machine
Expands the file name passed as the first argument and binds the variable in the second argument to the expanded name. This includes (1) expanding Unix tildes, (2) prepending FileName to the current directory, and (3) ``rectifying'' the expanded file name. In rectification, the expanded file name is ``rectified'' so that multiple repeated slashes are replaced with a single slash, the intervening ``./'' are removed, and ``../'' are applied so that the preceding item in the path name is deleted. For instance, if the current directory is /home, then abc//cde/..///ff/./b will be converted into /home/abc/ff/b.

Under Windows, this predicates does rectification as described above, (using backslashes when appropriate), but it does not expand the tildes.

expand_filename_no_prepend(+FileName,-ExpandedName)
shell
This predicate behaves as expand_filename/2, but only expands tildes and does rectification. It does not prepend the current working directory to relative file names.

parse_filename(+FileName,-Dir,-Base,-Extension)
machine
This predicate parses file names by separating the directory part, the base name part, and file extension. If file extension is found, it is removed from the base name. Also, directory names are rectified and if a directory name starts with a tilde (in Unix), then it is expanded. Directory names always end with a slash or a backslash, as appropriate for the OS at hand.

For instance, $\sim$john///doe/dir1//../foo.bar will be parsed into: /home/john/doe/, foo, and bar (where we assume that /home/john is what $\sim$john expands into).

sleep(+Seconds)
shell
Put XSB to sleep for a given number of seconds.

Error Cases

sys_pid(-Pid)
shell
Get Id of the current process.

getenv(+VarName,-VarVal
machine
Unifies VarVal with the value of VarName in the current shell. If VarName is not an environment varible, the predicate fails.

Example:

:- import getenv/2 from machine.

yes
| ?- getenv('HOSTTYPE',F).

F = intel-pc

putenv(+String)
machine
If String is of the form VarName=Value, inserts or resets the environment variable VarName. If VarName does not exist, it is inserted with VarVal. If the VarName does exist, it is reset to VarVal. putenv/2 always succeeds.

Exceptions:

instantiation_error
String is not instantiated at the time of call.
type_error
VarName or VarVal is not an atom or a list of atoms.



Subsections
next up previous contents index
Next: 1.7.1 Communication with Subprocesses Up: 1. Library Utilities Previous: 1.6 String Manipulation   Contents   Index
Terrance Swift 2007-10-06