28:- module(operating_system_support,
234:-
dynamic tmp_file_sequence_counter/1.
258 handle_system_internal(
Error, off, datime(
X)).
261 throw(error(instantiation_error,mktime(
V,
A))).
263 check_mktime_inp(
In, mktime(
In,
Out)),
264 In = datime(
Y,
Mo,
D,
H,
Mi,
S),
265 mktime(
Y,
Mo,
D,
H,
Mi,
S,
Out,
Error),
266 handle_system_internal(
Error, off, mktime(
In,
Out)).
268check_mktime_inp(
V,
Inp)
:- var(
V),
var,
269 throw(error(instantiation_error,
Inp)).
270check_mktime_inp(datime(
Y,
Mo,
D,
H,
Mi,
S),
Inp)
:- check_mktime_inp,
277check_mktime_inp(
T,
Inp)
:-
278 throw(error(domain_error(mktime,
T),
Inp)).
280check_int(
I,
_)
:- integer(
I),
integer.
281check_int(
I,
Inp)
:- var(
I),
282 throw(error(instantiation_error,
Inp)).
284 throw(error(type_error(integer,
I),
Inp)).
305 true_file_name(
IFile,
File),
306 process_delete_file_opts(
Opts,
Dir,
Recurse,
Ignore, delete_file(
File,
Opts)),
307 delete_file(
File,
Dir,
Recurse,
Ignore).
309process_delete_file_opts(
V,
_,
_,
_,
T)
:- var(
V),
var,
310 throw(error(instantiation_error,
T)).
311process_delete_file_opts([], off, off, off,
_)
:- process_delete_file_opts.
312process_delete_file_opts([
V|_],
_,
_,
_,
T)
:- var(
V),
var,
313 throw(error(instantiation_error,
T)).
314process_delete_file_opts([directory
|Opts], on,
Recurse,
Ignore,
T)
:- process_delete_file_opts,
315 process_delete_file_opts(
Opts,
_,
Recurse,
Ignore,
T).
316process_delete_file_opts([recursive
|Opts],
Dir, on,
Ignore,
T)
:- process_delete_file_opts,
317 process_delete_file_opts(
Opts,
Dir,
_,
Ignore,
T).
318process_delete_file_opts([ignore
|Opts],
Dir,
Recurse, on,
T)
:- process_delete_file_opts,
319 process_delete_file_opts(
Opts,
Dir,
Recurse,
_,
T).
320process_delete_file_opts(
Opts,
_,
_,
_,
T)
:-
321 throw(error(domain_error(delete_file_option,
Opts),
T)).
323delete_file(
IFile,
Dir,
Recurse,
Ignore)
:-
324 true_file_name(
IFile,
File),
325 file_property(
File,
Type,
_,
_,
_Permissions,
_,
Ignore),
326 delete_file(
Type,
File,
Dir,
Recurse,
Ignore).
328delete_file(
N,
File,
_Dir,
_Recurse,
Ignore)
:- number(
N),
number,
329 handle_system_internal(
N,
Ignore, delete_file(
File)).
330delete_file(directory,
File,
Dir,
Recurse,
Ignore)
:-
331 delete_directory(
Dir,
File,
Recurse,
Ignore),
delete_directory.
332delete_file(
_,
File,
_Dir,
_Recurse,
Ignore)
:-
333 unlink_file(
File,
Ignore).
335unlink_file(
IFile,
Ignore)
:-
336 true_file_name(
IFile,
File),
338 handle_system_internal(
N,
Ignore, delete_file(
File)).
340delete_directory(on,
File,
_Recurse,
Ignore)
:-
341 rm_directory(
File,
Ignore).
342delete_directory(off,
File,
Recurse,
Ignore)
:-
343 delete_directory(
Recurse,
File,
Ignore).
345rm_directory(
File,
Ignore)
:-
347 handle_system_internal(
Error,
Ignore, delete_file(
File)).
349delete_directory(on,
File,
Ignore)
:-
352 atom_concat(
File,
D,
FileP),
353 delete_dirfiles(
FileList,
FileP,
Ignore),
356delete_dirfiles([],
_,
_).
357delete_dirfiles([
'.'|Fs],
File,
Ignore)
:- delete_dirfiles,
358 delete_dirfiles(
Fs,
File,
Ignore).
359delete_dirfiles([
'..'|Fs],
File,
Ignore)
:- delete_dirfiles,
360 delete_dirfiles(
Fs,
File,
Ignore).
361delete_dirfiles([
F|Fs],
File,
Ignore)
:-
362 atom_concat(
File,
F,
TrueF),
363 delete_file(
TrueF, off, on,
Ignore),
364 delete_dirfiles(
Fs,
File,
Ignore).
366handle_system_internal(
Error,
_Ignore,
_G)
:- var(
Error),
var.
367handle_system_internal(
Error, off,
G)
:- atom(
Error),
atom,
368 throw(error(system_internal(
Error),
G)).
369handle_system_internal(
Error, off,
G)
:-
370 error_message(
Error,
Message),
371 throw(error(system_internal(
Message),
G)).
373handle_system_internal(
Error,
_Id,
_Ignore,
_G)
:- var(
Error),
var.
374handle_system_internal(
Error,
_SIG, off,
G)
:- integer(
Error),
integer,
375 error_message(
Error,
Message),
376 throw(error(system_internal(
Message),
G)).
377handle_system_internal(signal,
SIG, off,
G)
:- handle_system_internal,
378 throw(error(system_internal(child_signal(
SIG)),
G)).
379handle_system_internal(stopped,
SIG, off,
G)
:-
380 throw(error(system_internal(child_stopped(
SIG)),
G)).
383 true_file_name(
IFile,
File),
384 file_property(
File,
Type,
_Size,
_Date,
_Permissions,
_LinkName).
386 true_file_name(
IFile,
File),
387 file_property(
File,
_Type,
Size,
_Date,
_Permissions,
_LinkName).
389 true_file_name(
IFile,
File),
390 file_property(
File,
_Type,
_Size,
Date,
_Permissions,
_LinkName).
392 true_file_name(
IFile,
File),
393 file_property(
File,
_Type,
_Size,
_Date,
Permissions,
_LinkName).
395 true_file_name(
IFile,
File),
396 file_property(
File,
_Type,
_Size,
_Date,
_Permissions,
LinkName),
399file_property(
File,
Type,
Size,
Date,
Permissions,
LinkName)
:-
400 file_property(
File,
Type,
Size,
Date,
Permissions,
LinkName,
Error),
401 handle_system_internal(
Error, off, file_property(
File)).
422 (
p_environ(
_,
S)
-> environ_split(
S,
SNa,
SVal)
; environ_split,
environ_split ),
426 bound_environ(
Na,
Val).
428 throw(error(type_error(atom,
Na),environ(
Na,
Val))).
430bound_environ(
Na,
Val)
:- var(
Val),
var,
432bound_environ(
Na,
Val)
:- atom(
Val),
atom,
434bound_environ(
Na,
Val)
:-
435 throw(error(type_error(atom,
Val),environ(
Na,
Val))).
442environ_split([
61|SVal], [],
SVal)
:- environ_split.
443environ_split([
C|S],[
C|SNa],
SVal)
:-
444 environ_split(
S,
SNa,
SVal).
474exec(
Command, [
StdIn,
StdOut,
StdErr],
PID)
:-
475 G = exec(
Command, [
StdIn,
StdOut,
StdErr],
PID),
476 check_command_with_default_shell(
Command,
TrueCommand,
G),
477 process_inp_stream_for_exec(
StdIn,
In,
G, [],
L1),
478 process_out_stream_for_exec(
StdOut,
Out,
G,
L1,
L2),
479 process_err_stream_for_exec(
StdErr,
Err,
G,
L2,
L3),
480 (
exec_command(
TrueCommand,
In,
Out,
Err,
PID,
Error)
-> exec_command
; exec_command ),
481 close_temp_streams(
L3),
482 handle_system_internal(
Error, off,
G).
484process_inp_stream_for_exec(
Error,
_,
G,
L,
L)
:- var(
Error),
var,
485 close_temp_streams(
L),
486 throw(error(instantiation_error,
G)).
487process_inp_stream_for_exec(null, null,
_,
L,
L)
:- process_inp_stream_for_exec.
488process_inp_stream_for_exec(std,
0,
_,
L,
L)
:- process_inp_stream_for_exec.
489process_inp_stream_for_exec(pipe(
ForWriting),
ForReading,
_,
L, [
ForReading|L])
:- var(
ForWriting),
var,
490 open_pipe_stream(
ForReading,
ForWriting).
491process_inp_stream_for_exec(pipe(
Stream),
_,
_,
L,
L)
:- process_inp_stream_for_exec,
493process_inp_stream_for_exec(
Stream,
Stream,
_,
L,
L)
:-
497process_out_stream_for_exec(
Error,
_,
G,
L,
L)
:- var(
Error),
var,
498 close_temp_streams(
L),
499 throw(error(instantiation_error,
G)).
500process_out_stream_for_exec(null, null,
_,
L,
L)
:- process_out_stream_for_exec.
501process_out_stream_for_exec(std,
1,
_,
L,
L)
:- process_out_stream_for_exec.
502process_out_stream_for_exec(pipe(
ForReading),
ForWriting,
_,
L, [
ForWriting|L])
:- var(
ForReading),
var,
503 open_pipe_stream(
ForReading,
ForWriting).
504process_out_stream_for_exec(pipe(
Stream),
_,
_,
L,
L)
:- process_out_stream_for_exec,
506process_out_stream_for_exec(
Stream,
Stream,
_,
L,
L)
:-
509process_err_stream_for_exec(
Error,
_,
G,
L,
L)
:- var(
Error),
var,
510 close_temp_streams(
L),
511 throw(error(instantiation_error,
G)).
512process_err_stream_for_exec(null, null,
_,
L,
L)
:- process_err_stream_for_exec.
513process_err_stream_for_exec(std,
2,
_,
L,
L)
:- process_err_stream_for_exec.
514process_err_stream_for_exec(pipe(
ForReading),
ForWriting,
_,
L, [
ForWriting|L])
:- var(
ForReading),
var,
515 open_pipe_stream(
ForReading,
ForWriting).
516process_err_stream_for_exec(pipe(
Stream),
Stream,
_,
L,
L)
:- process_err_stream_for_exec,
518process_err_stream_for_exec(
Stream,
Stream,
_,
L,
L)
:-
521close_temp_streams([]).
522close_temp_streams([
S|Ss])
:-
524 close_temp_streams(
Ss).
548popen(
Command, read,
Stream)
:-
549 exec(
Command, [std,pipe(
Stream),std],
Stream).
550popen(
Command, write,
Stream)
:-
551 exec(
Command, [pipe(
Stream),std,std],
Stream).
553check_command_with_default_shell(
Com,
ComF,
G)
:-
554 check_command(
Com,
G),
555 os_command_postprocess(
Com,
ComF).
560os_command_postprocess(
Com,
ComF)
:- os_command_postprocess,
os_command_postprocess,
563 getenv(
'COMSPEC',
Shell0),
567os_command_postprocess(
Com,
Com).
569check_command(
Com,
G)
:- var(
Com),
var,
570 throw(error(instantiation_error,
G)).
571check_command(
Com,
_)
:- atom(
Com),
atom.
572check_command(
Com,
G)
:-
573 throw(error(type_error(atom,
Com),
G)).
575check_mode(
Mode,
_,
G)
:- var(
Mode),
var,
576 throw(error(instantiation_error,
G)).
577check_mode(read,
0,
_)
:- check_mode.
578check_mode(write,
1,
_)
:- check_mode.
579check_mode(
Mode,
G)
:-
580 throw(error(domain_error(io_mode,
Mode),
G)).
584 get_shell0(
FullCommand),
585 exec_command(
FullCommand,
0,
1,
2,
PID,
Error),
586 handle_system_internal(
Error, off,
G),
587 wait(
PID,
_Status,
Error,
Id),
588 handle_system_internal(
Error, got(
FullCommand,
Id), off,
G).
592 check_command(
Command,
G),
593 get_shell(
Shell,
Opt),
594 do_shell(
Shell,
Opt,
Command,
Status,
Error),
596 handle_system_internal(
Error, off,
G).
598shell(
Command,
Status)
:-
599 G = shell(
Command,
Status),
600 check_command(
Command,
G),
601 get_shell(
Shell,
Opt),
602 do_shell(
Shell,
Opt,
Command,
Status,
Error),
603 handle_system_internal(
Error, off,
G).
619 default_shell(
Command),
620 do_system(
Command,
_Status,
Error),
621 handle_system_internal(
Error, off, system).
623default_shell(
Shell)
:- default_shell,
default_shell,
624 getenv(
'COMSPEC',
Shell).
625default_shell(
'/bin/sh').
636 G = system(
Command,
Status),
637 check_command(
Command,
G),
638 do_system(
Command,
Status,
Error),
640 handle_system_internal(
Error, off,
G).
642wait(
PID,
STATUS)
:- var(
PID),
var,
643 throw(error(instantiation_error,wait(
PID,
STATUS))).
644wait(
PID,
STATUS)
:- integer(
PID),
integer,
645 plwait(
PID,
STATUS,
Error,
_Detail),
646 handle_system_internal(
Error, off, wait(
PID,
STATUS)).
648 throw(error(type_error(integer,
PID),wait(
PID,
STATUS))).
655 handle_system_internal(
Error, off, host_name(
X)).
659 handle_system_internal(
Error, off, host_id(
X)),
665 handle_system_internal(
Error, off, pid(
X)).
670 handle_system_internal(
Error, off, kill(
X,
Y)).
671kill(
X,
Y)
:- (
var(
X)
; var(
Y)), !,
672 throw(error(instantiation_error,kill(
X,
Y))).
673kill(
X,
Y)
:- integer(
X),
integer,
674 throw(error(type_error(integer,
Y),kill(
X,
Y))).
676 throw(error(type_error(integer,
X),kill(
X,
Y))).
679 throw(error(instantiation_error,mktemp(
X,
Y))).
683 handle_system_internal(
Error, off, mktemp(
X,
Y)).
685 throw(error(type_error(atom,
X),mktemp(
X,
Y))).
689 handle_system_internal(
Error, off, tmpnam(
X)).
695 handle_system_internal(
Error, off, tmpdir(
Dir)),
697 (
atom_concat(
_,
D,
Dir)
->
700 atom_concat(
Dir,
D,
TmpDir)
703path_separator(
'\\')
:-
704 path_separator,
path_separator.
720 rename_file(
F0,
F,
Error),
721 handle_system_internal(
Error, off, rename_file(
F0,
F)).
738:-
meta_predicate directory_map(+,
1,-),
761d_map([
'.'|Fs],
D,
P)
:-
764d_map([
'..'|Fs],
D,
P)
:-
767d_map([
F|Fs],
D,
P)
:-
773 catch( file_property(
File, type(directory) ),
_, fail ),
781absolute_file_name( -File:atom, +Path:atom, +Options:list)
catch( : Goal,+ Exception,+ Action)
stream_property( Stream, Prop )
working_directory( ?_CurDir_,? NextDir)
list_directory(+ Dir, -ListOfFiles)
load_foreign_files( Files, Libs, InitRoutine)
atom_codes(?Atom, ?Codes)
append(? List1,? List2,? List3)
datime(datime(- Year, - Month, - DayOfTheMonth, - Hour, - Minute, - Second)
delete_file(+ File,+ Opts)
directory_files(+ Dir,+ List)
directory_map(+ Dir, 1:P)
environ(? EnvVar,+ EnvValue)
exec(+ Command, StandardStreams, -PID)
file_property(+ File,? Property)
mktime(+_Datime_, - Seconds)
popen( +Command, +TYPE, -Stream)
read_link(+ SymbolicLink, -Link, -NewPath)
rename_file(+ OldFile,+ NewFile)
shell(+ Command,- Status)
tmp_file(+_Base_, - File)