An elegance layer to calling unix commands.
This library provides primitives that allow programmers and users to embed calls to process_create/3. The aim is to keep application code clear and succinct. This is achieved by (a) reducing the call to process_create/3 to its essential constituents and (b) allowing for term structures in the arguments of the call.
The library uses two local flags if they are defined.
current_prolog_flag( by_unix_shell, Shell ). current_prolog_flag( by_unix_shell_com_arg, CmA ).
Shell should be a Unix shell such as tcsh and CmA argument should be the shell's way of saying that what ever follows is for shell to execute. When Shell is present it is used to start the commands; this allows for including user personalisations via their usual shell. When CmA is missing, -c is used (set it to '' for none at all).
On loading, the file ~/.
pl/by_unix.pl will be consulted if it exists.
You can add user preferences there, such as
:- set_prolog_flag( by_unix_shell, tcsh ).
The simplest example of how to use the library is:
?- @ ls.
This lists all files by translating ls to
process_create( path(ls), ,  ).
To list all details (long list):
?- @ ls(-l).
which demonstrates translation of terms in arguments.
By_unix looks at the arguments of the terms right of an @ to decide which ones are options (3rd argument of process_create/3) assuming the rest to be arguments to the call (2nd argument of process_create/3). Command arguments can be terms which will be serialised, so that a/b becomes 'a/b'. The argument * is special and it is expanded fed into expand_file_name/2.
With SWI 7 you can now have '.' in atoms, making interactions with the OS even smoother.
?- @ mkdir( -p, /tmp/test_by_unix ). ?- @ cd( /tmp/test_by_unix ). ?- @ touch( empty.pl ). ?- @ rm( -f, empty.pl ). ?- @ cd( pack(by_unix) ). ?- Wc @@ wc( -l, pack.pl ). Wc = ['10 pack.pl']. ?- @ cd( @ '$HOME' ). ?- [Pwd] @@ pwd. Pwd = '/home/nicos'. ?- Pass @@ bash( -c, 'read -s -p password: pass; echo $pass' ), nl. % Sending, name: bash, args: [-c,read -s -p password: pass; echo $pass], opts:[stdout(pipe(_G1360))]. password: Pass = [word].
The main objective of by_unix is achieved by what has been described so far. We have found that more than 90 percent of the its uses are to produce elegant Goals that are clear to read and construct their arguments within Prolog. We provide some more features, which are described in what follows, but they should be seen as marginal.
Changing directory is not supported via cd in process_create as
which cd fails to return
an executable in bash. That is,
?- process_create( path(cd), ['/'],  ). ERROR: source_sink `path(cd)' does not exist
As oppose to:
?- [library(by_unix)]. ?- @ cd( / ). true. ?- @ ls. bin dev home initrd.img.old lib64 media opt root sbin srv tmp var boot etc initrd.img lib lost+found mnt proc run selinux sys usr vmlinuz true. ?- @ cd( /home/nicos ). ?- @ cd( pack(by_unix) ).
which/2 provides a locator for executables. by_unix_term_to_serial/2 serialises Prolog terms to process_create/3 atoms, and by_unix_assert/0 allows for doing away with the @.
As process_create/3 quotes special characters, for instance
?- process_create( path(ls), ['*'],  ). /bin/ls: cannot access *: No such file or directory ERROR: Process "/bin/ls": exit status: 2
By_unix allows for in-argument file name expansions via expand_file_name/2.
ls( -l, @('*.pl') ).
@@/2 provides dual functionality: either picking up output lines from the calling command or for maplist/2 applications. See @@/2 and @@/3.
A lines example
?- @ cd( @ '$HOME' ). ?- Pwd @@ pwd. Pwd = ['/home/nicos'].
A maplist example
?- @ cd( pack(by_unix) ). ?- Files = ['prolog/by_unix.pl'], maplist( @@(wc(-l)), Files, Wcs ).
As of version 0.1.7 (a) failure to locate a suitable executable results to an informational message, (b) non-zero exit on the process_create/3 does no longer produce an error, but an informational message followed by failure.
?- @what(else). % No 'what' executable could be found. false. ?- @ ls(who). /bin/ls: cannot access 'who': No such file or directory % Failed shell call: ls with args: [who]. false.
The following predicates are exported, but not or incorrectly documented.