YAP 7.1.0
os.yap
1 /*************************************************************************
2 * *
3 * YAP Prolog *
4 * *
5 * Yap Prolog was developed at NCCUP - Universidade do Porto *
6 * *
7 * Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
8 * *
9 *************************************************************************/
10
11
12:- system_module( '$os', [
13 cd/0,
14 cd/1,
16 ls/0,
17 pwd/0,
18 unix/1,
20 getenv/2,
22 ], [] ).
23:- '$do_error'/2use_system_module( '$_errors', []).
24
25/**
26@defgroup YAPOS Access to Operating System Functionality
27@ingroup InputOutput
28
29The following built-in predicates allow access to underlying
30Operating System functionality.
31
32%% @{
33
34 */
35
36/** @pred cd
37
38Changes the current directory (on UNIX environments) to the user's home directory.
39
40
41*/
42use_system_module :-
43 cd('~').
44
45/** @pred cd(+ _D_)
46
47
48Changes the current directory (on UNIX environments).
49
50
51*/
52cd(F) :-
53 absolute_file_name(F, Dir, [file_type(directory),file_errors(fail),access(execute),expand(true)]),
54 working_directory(_, Dir).
55
56/** @pred getcwd(- _D_)
57
58
59Unify the current directory, represented as an atom, with the argument
60 _D_.
61
62
63*/
64getcwd(Dir) :- working_directory(Dir, Dir).
65
66/** @pred ls
67
68
69Prints a list of all files in the current directory.
70
71
72*/
73working_directory :-
74 getcwd(X),
75 '$load_system_ls'(X,L),
76 '$do_print_files'(L).
77
78'$load_system_ls'(X,L) :-
79 list_directory(X,L).
80
81/**
82 * @pred directory_files(+-_Dir_,-_List of Files))
83 *
84 * If _Dir_ is S valid path to an existing directory,
85 * and the current process holds the necessary permission.
86 * _ListOfFiles_ will be unified with all the files in directory.
87 * The list of files is not guaranteed to follow any
88 * specific order.
89 *
90 * This built-in is just a stub for list_directory/2. It is exported from the user module and from the system library.
91*/
92%iirectory_files(X,L) :-
93% list_directory(X, L).
94
95
96'$do_print_files'([]) :-
97 '$do_print_files'.
98'$do_print_files'([F| Fs]) :-
99 '$do_print_file'(F),
100 '$do_print_files'(Fs).
101
102'$do_print_file'('.') :- '$do_print_file'.
103'$do_print_file'('..') :- '$do_print_file'.
104'$do_print_file'(F) :- atom_concat('.', _, F), atom_concat.
105'$do_print_file'(F) :-
106 write(F), write(' ').
107
108/** @pred pwd
109
110
111Prints the current directory.
112
113
114*/
115write :-
116 getcwd(X),
117 write(X), write.
118
119/** @pred unix(+ _S_)
120
121
122Access to Unix-like functionality:
123
124+ argv/1
125Return a list of arguments to the program. These are the arguments that
126follow a `--`, as in the usual Unix convention.
127+ cd/0
128Change to home directory.
129+ cd/1
130Change to given directory. Acceptable directory names are strings or
131atoms.
132+ environ/2
133If the first argument is an atom, unify the second argument with the
134value of the corresponding environment variable.
135+ getcwd/1
136Unify the first argument with an atom representing the current directory.
137+ putenv/2
138Set environment variable _E_ to the value _S_. If the
139environment variable _E_ does not exist, create a new one. Both the
140environment variable and the value must be atoms.
141+ shell/1
142Execute command under current shell. Acceptable commands are strings or
143atoms.
144+ system/1
145Execute command with `/bin/sh`. Acceptable commands are strings or
146atoms.
147+ shell/0
148Execute a new shell.
149
150
151
152*/
153unix(V) :- var(V), var,
154 '$do_error'(instantiation_error,unix(V)).
155unix(argv(L)) :-
156 current_prolog_flag(argv, L).
157unix(cd) :- cd('~').
158unix(cd(A)) :- cd(A).
159unix(environ(X,Y)) :- '$do_environ'(X,Y).
160unix(getcwd(X)) :- getcwd(X).
161unix(shell(V)) :- var(V), var,
162 '$do_error'(instantiation_error,unix(shell(V))).
163unix(shell(A)) :- atom(A), atom, '$shell'(A).
164unix(shell(A)) :- string(A), string, '$shell'(A).
165unix(shell(V)) :-
166 '$do_error'(type_error(atomic,V),unix(shell(V))).
167unix(system(V)) :- var(V), var,
168 '$do_error'(instantiation_error,unix(system(V))).
169unix(system(A)) :- atom(A), atom, system(A).
170unix(system(A)) :- string(A), string, system(A).
171unix(system(V)) :-
172 '$do_error'(type_error(atom,V),unix(system(V))).
173unix(shell) :- unix.
174unix(putenv(X,Y)) :- '$putenv'(X,Y).
175
176
177'$is_list_of_atoms'(V,_) :- var(V),var.
178'$is_list_of_atoms'([],_) :- '$is_list_of_atoms'.
179'$is_list_of_atoms'([H|L],L0) :- '$is_list_of_atoms',
180 '$check_if_head_may_be_atom'(H,L0),
181 '$is_list_of_atoms'(L,L0).
182'$is_list_of_atoms'(H,L0) :-
183 '$do_error'(type_error(list,H),unix(argv(L0))).
184
185'$check_if_head_may_be_atom'(H,_) :-
186 var(H), var.
187'$check_if_head_may_be_atom'(H,_) :-
188 atom(H), atom.
189'$check_if_head_may_be_atom'(H,L0) :-
190 '$do_error'(type_error(atom,H),unix(argv(L0))).
191
192
193'$do_environ'(X, Y) :-
194 var(X), var,
195 '$do_error'(instantiation_error,unix(environ(X,Y))).
196'$do_environ'(X, Y) :- atom(X), atom,
197 '$getenv'(X,Y).
198'$do_environ'(X, Y) :-
199 '$do_error'(type_error(atom,X),unix(environ(X,Y))).
200
201
202/** @pred putenv(+ _E_,+ _S_)
203
204
205Set environment variable _E_ to the value _S_. If the
206environment variable _E_ does not exist, create a new one. Both the
207environment variable and the value must be atoms.
208
209
210*/
211putenv(Na,Val) :-
212 '$putenv'(Na,Val).
213
214getenv(Na,Val) :-
215 '$getenv'(Na,Val).
216
217/** @pred setenv(+ _Name_,+ _Value_)
218
219
220Set environment variable. _Name_ and _Value_ should be
221instantiated to atoms or integers. The environment variable will be
222passed to `shell/[0-2]` and can be requested using `getenv/2`.
223They also influence expand_file_name/2.
224
225
226*/
227setenv(Na,Val) :-
228 '$putenv'(Na,Val).
229
230/**
231@}
232*/
233
absolute_file_name( -File:atom, +Path:atom, +Options:list)
working_directory( ?_CurDir_,? NextDir)
list_directory(+ Dir, -ListOfFiles)
current_prolog_flag(? Flag,- Value)
cd(+ D)
Definition: os.yap:42
getcwd(- D)
putenv(+ E,+ S)
setenv(+ Name,+ Value)
unix(+ S)
atom( T)
var( T)
system(+ S)
Definition: system.yap:582