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.
A simple example is
?- @ ls.
which 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'.
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 = ['by_unix.pl'], maplist( @@wc(-l), Files, Wcs ).
@author Nicos Angelopoulos @version 0.1.6 2013/12/26 @see http://stoics.org.uk/~nicos/sware/by_unix
For @cd( Arg )
see documentation of cd/2.
?- @ mkdir( -p, /tmp/test_by_unix ). ?- @ cd( /tmp/test_by_unix ). ?- @ touch( empty.pl ). ?- @ rm( -f, empty.pl ).
cd
, variable This is also bound to cd
. cd is handled separately as which cd
fails in bash, as does process_create(path(cd), ['/'], [] )
.expand_file_name( Atom, Serial )
.goal_expansion(_,_)
.?- by_unix_asert. ?- mkdir( -p, /tmp/test_by_unix ). ?- cd( /tmp/test_by_unix ). ?- touch( empty.pl ). ?- ls( -l ). ?- rm( -f, empty.pl ). ?- ls( -a ).
cd( New )
is called.
?- @ cd( pack ). true. ?- @ pwd. /usr/local/users/nicos/local/git/lib/swipl-7.1.4/pack true. ?- @ cd( @ '$HOME' ). true. ?- @ pwd. /home/nicos true.
The following predicates are exported, but not or incorrectly documented.