Quintus
Prolog Manual
The predicate described here, unix/1, provides the most commonly needed access to UNIX. This minor extension to the Prolog system should be sufficient for most of your needs. However, you can also extend the Prolog system with additional C code, including system calls, using the foreign function interface (see {manual(i-3)}). The reason for channeling all the interaction with the operating system through a single built-in predicate, rather than having separate predicates for each function, is simply to localize the system dependencies. Admittedly, this makes for more cumbersome commands, so you may wish to put some clauses such as these in your prolog.ini file:
cd :- unix(cd). cd(X) :- unix(cd(X)).
Initialization files are discussed in {manual(g-3)}.
unix(cd(+Path)) changes the working directory of Prolog (and of Emacs, if running under the editor interface) to that specified by Path, which should be an atom corresponding to a legal directory. If Path is not specified, unix(cd) changes the working directory of Prolog (and of Emacs if running under the editor interface) to your home directory. Note that the 'Escape-x cd Path' and 'Escape-x cd' commands under Emacs have the same effect as this, except that Emacs also provides filename completion.
To spawn a unix shell and execute a command use unix(shell(+Command)). The reference page for unix/1 contains examples. If Command is an atom, a new shell process is invoked, and Command is passed to it for execution as a shell command. The shell invoked depends on your SHELL environment variable. If the shell returns with a non-zero result (for example, because the command was not found), unix(shell(Command)) simply fails. Similarly, a new UNIX sh(1) process can be invoked by calling unix(system(+Command)).
unix(shell) starts up an interactive shell. The shell run depends on your SHELL environment variable. You can exit from the shell by typing ^d (or your end-of-file character) unless under Emacs, in which case you should type '^x ^d'. The Prolog idiom 'end_of_file.' will not work in this context. If 'ignoreeof' is set (for example, in your '.cshrc' file), ^d may not work (setting 'ignoreeof' turns off ^d). In this case, you may type 'exit' to the shell to kill it. The call to unix(shell) fails if a non-zero result is returned by the shell.
NOTE: Invoking the predicate unix(shell) when your SHELL environment variable is set to a non-standard shell (not /bin/csh or /bin/sh) may cause echoing problems under the Emacs interface due to the 'stty' settings of the non-standard shell. If a non-standard shell proves to be a problem, an alternative is to use either unix(shell(sh)) or unix(shell(csh)) to invoke the standard 'sh' or 'csh' shell, respectively.
To return the arguments that were typed on the UNIX command line following the command which invoked the current Prolog saved state (see {manual(g-3)}) use either of the following:
| ?- unix(argv(ArgList)). % mnuemonic; 'vector', 'value' | ?- unix(args(ArgList)). % mnuemonic; 'string'
The difference between using 'argv' and 'args' is evident when Prolog is invoked with numbers as arguments. The objects returned by unix(argv(_)) are Prolog objects; that is, if the command line argument is a number, then it will be returned as a number. Thus:
% prolog 1
.
.
.
| ?- unix(argv([1])).
yes
| ?- unix(argv(['1'])).
no
| ?- unix(args(['1'])).
yes
% prolog 1 6.999999
.
.
.
| ?- unix(argv(X)).
X = [1, 6.9999999E+00]
| ?- unix(args(X)).
X = ['1', '6.999999']
So if your program treats the command line argument as a number, use the form with 'argv', but if it is to be treated as a string, use 'args'. For example if the program is called with a number and performs some arithmetic operation on the argument then displays the result, use 'argv'. | ?- [user].
| runtime_entry(start):-
unix(argv([A])),
Y is A+1,
display(Y).
|
| ^D
% user compiled in module user, 0.083 sec 8 bytes
yes
| ?- runtime_entry(start).
46
yes
The command line arguments passed to Prolog can be accessed from C through QP_argv and QP_argc. These are similar to Unix argc and argv except that they only store Prolog's arguments. In <quintus/quintus.h> they are classed as
extern int QP_argc
extern char **QP_argv
contact: product
support sales information