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

Script Writing Utilities

Prolog, (or XSB) can be useful for writing scripts in a UNIX system. Prolog's simple syntax and declarative semantics make it especially suitable for scripts that involve text processing. Wherever noted, some of these functions are currently working under Unix only.

date(?Date)
scrptutl

Unifies Date to the current date, returned as a Prolog term, suitable for term comparison. Currently this only works under Unix, is slow, and should be rewritten in C using time() and localtime().

Example:

                > date 
                Thu Feb 20 08:46:08 EST 1997
                > xsb -i
               XSB Version 1.7
               [sequential, single word, optimal mode]
               | ?- [scrptutl].
               [scrptutl loaded]

               yes
               | ?- date(D).
               D = date(1997,1,20,8,47,41)

               yes

This predicate is obsolete and datime/1, defined in standard, should be used instead.

file_time(+FileName, -time(Time1,Time2))
file_io

Returns file's modification time. Because XSB steals 5 bits from each word, time must be returned as two words: Time1, representing the most significant digits, and Time2, representing the less significant digits.

file_size(+FileName, -Size)
file_io

Returns file size.

directory(+Path,?Directory)
directry

Unifies Directory with a list of files in the directory specified by path. Information about the files is similar to that obtained by ls -l, but transformed for ease of processing. This currently works for Unix only, is slow, and should be reimplemented in C using opendir() and readdir().

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 expanding Unix tildas, prepending the current directory, etc. In addition, 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 (using backslashes when appropriate), but it does not expand the tildas.

tilde_expand_filename(+FileName,-ExpandedName)
machine
Like expand_filename/2, but only expands tildas and does rectification. This does not prepend the current working directory to relative file names.

is_absolute_filename(+FileName)
machine
Succeeds, if file name is absolute; fails otherwise. This predicate works also under Windows, i.e., it recognizes drive letters, etc.

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).

file_to_list(IOport, List)
scrptutls
Read lines from an open I/O port. Return a list of terms, one per each line read. Each such term is a list of tokens on the corresponding line. Tokens are lists of characters separated by a space symbol (space, newline, return, tabs, formfeed). For instance, if IOport 10 is bound to a file
ads sdfdsfd ee
112 444
4555
then
| ?- file_to_list(10, L).  
L = [[ads,sdfdsfd,ee],[112,444],[4555]]
yes
Note: file_to_list/2 does not close the I/O port, so it is an application program responsibility.

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

cd(+Path)
shell
Change directory.

rename(+Old,-New)
shell
Rename file.

ls
shell
Does ls -F. Unix only.

rm(+Path)
shell
Remove file.

cwd(-Dir)
shell
Get current working directory.

edit(+Path)
shell
Edit file using your favorite editor (specified by the environment variable EDITOR. Unix only.

sys_exit(+ExitCode)
shell
Exit XSB with a given exit code.

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

In addition, the module file_io provides the following unified interface to the operations on files. All these calls succeed iff the corresponding system call succeeds.

path_sysop(isplain, +Path)
file_io
Succeeds, if Path is a plain file.
path_sysop(isdir, +Path)
file_io
Succeeds, if Path is a directory.
path_sysop(rename, +OldPath, +NewPath)
file_io
Renames OldPath into NewPath.
path_sysop(copy, +FromPath, +ToPath)
file_io
Copies FromPath into ToPath.
path_sysop(rm, +Path)
file_io
Removes the plain file Path.
path_sysop(unlink, +Path)
file_io
Same as rm.
path_sysop(link, +SrsPath, +DestPath)
file_io
Creates a hard link from SrsPath to DestPath. UNIX only.
path_sysop(cwd, -Path)
file_io
Binds Path to the current working directory.
path_sysop(chdir, +Path)
file_io
Changes the current working directory to Path.
path_sysop(mkdir, +Path)
file_io
Creates a new directory, Path.
path_sysop(rmdir, +Path)
file_io
Deletes the directory Path.
path_sysop(exists, +Path)
file_io
Succeeds if the file Path exists.
path_sysop(readable, +Path)
file_io
Succeeds if Path is a readable file.
path_sysop(writable, +Path)
file_io
Succeeds if Path is a writable file.
path_sysop(executable, +Path)
file_io
Succeeds if Path is an executable file.
path_sysop(modtime, +Path, -Time)
file_io
Returns a list that represents the last modification time of the file. Succeeds if file exists. In this case, Time is bound to a list [high,low] where low is the least significant 24 bits of the modification time and high is the most significant bits (25th) and up. Time represents the last modification time of the file. The actual value is thus $ \tt high*2^{24} + low$, which represents the number of seconds elapsed since 00:00:00 on January 1, 1970, Coordinated Universal Time (UTC).
path_sysop(newerthan, +Path1, +Path2)
file_io
Succeeds is the last modification time of Path1 is higher than that of Path2. Also succeeds if Path1 exists but Path2 does not.
path_sysop(size, +Path, -Size)
file_io
Returns a list that represents the byte size of Path. Succeeds if the file exists. In this case Size is bound to the list of the form [high,low] where low is the least significant 24 bits of the byte-size and high is the most significant bits (25th) and up. The actual value is thus $ \tt high*2^{24} + low$.
path_sysop(tmpfilename, -Name)
file_io
Returns the name of a new temporary file. This is useful when the application needs to open a completely new temporary file.
path_sysop(extension, +Name, -Ext)
file_io
Returns file name extension.
path_sysop(basename, +Name, -Base)
file_io
Returns the base name of the file name (i.e., the name sans the directory and the extension).
path_sysop(dirname, +Name, -Dir)
file_io
Returns the directory portion of the filename. The directory is slash or backslash terminated.
path_sysop(isabsolute, +Name)
file_io
Succeeds if Name is an absolute path name. File does not need to exist.
path_sysop(expand, +Name, -ExpandedName)
file_io
Binds ExpandedName to the expanded absolute path name of Name. The file does not need to exist. Duplicate slashes, references to the current and parent directories are factored out.


next up previous contents index
Next: Communication with Subprocesses Up: Library Utilities Previous: String Manipulation   Contents   Index
Luis Fernando P. de Castro 2003-06-27