View source with formatted comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2010-2016, University of Amsterdam
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(sicstus_system,
   36	  [ environ/2,			% ?Name, ?Value
   37
   38	    exec/3,			% +Command, -Streams, -PID
   39	    wait/2,			% +PID, -Status
   40	    pid/1,			% -PID
   41
   42	    now/1,			% -TimeStamp
   43	    datime/1,			% -DaTime
   44	    datime/2,			% +TimeStamp, -DaTime
   45	    sleep/1,			% +Seconds
   46
   47	    shell/0,
   48	    shell/1,			% +Command
   49	    shell/2,			% +Command, -Status
   50
   51	    system/0,
   52	    system/1,			% +Command
   53	    system/2,			% +Command, -Status
   54
   55	    popen/3,			% +Command, +Mode, -Stream
   56
   57	    host_name/1,		% -HostName
   58
   59	    working_directory/2,	% -Old, +New
   60	    make_directory/1,		% +DirName
   61	    file_exists/1,		% +FileName
   62	    delete_file/1,		% +FileName
   63	    rename_file/2,		% +Old, +New
   64	    mktemp/2,			% +Template, -FileName
   65	    tmpnam/1			% -FileName
   66	  ]).   67:- use_module(library(process)).   68:- use_module(library(socket)).   69:- use_module(library(shell), [shell/0]).   70
   71:- multifile sicstus:rename_module/2.   72
   73sicstus:rename_module(system, sicstus_system).
   74
   75/** <module> SICStus 3-compatible library(system).
   76
   77@tbd	This library is incomplete.
   78	As of SICStus 3.12.11, the following predicates are missing:
   79
   80	* delete_file/2
   81	* directory_files/2
   82	* file_exists/2
   83	* file_property/2
   84	* host_id/1
   85	* kill/2
   86
   87@see	https://sicstus.sics.se/sicstus/docs/3.12.11/html/sicstus/System-Utilities.html
   88*/
   89
   90%%	environ(?Name, ?Value) is nondet.
   91%
   92%	True if Value is an atom associated with the environment variable
   93%	or system property Name.
   94%
   95%	@tbd	Mode -Name is not supported.
   96%
   97%		Because SWI-Prolog doesn't have an obvious equivalent to
   98%		SICStus system properties, this predicate currently
   99%		behaves as if no system properties are defined,
  100%		i. e. only environment variables are returned.
  101%
  102%	@compat sicstus
  103
  104environ(Name, Value) :-
  105	getenv(Name, Value).
  106
  107
  108		 /*******************************
  109		 *	      PROCESSES		*
  110		 *******************************/
  111
  112%%	exec(+Command, +Streams, -PID)
  113%
  114%	SICStus 3 compatible implementation of  exec/3   on  top  of the
  115%	SICStus 4 compatible process_create/3.
  116%
  117%	@bug	The SICStus version for Windows seems to hand Command
  118%		directly to CreateProcess(). We hand it to
  119%
  120%		  ==
  121%		  %COMSPEC% /s /c "Command"
  122%		  ==
  123%
  124%		In case of conflict, it is adviced to use
  125%		process_create/3
  126
  127exec(Command, Streams, PID) :-
  128	Streams = [In, Out, Error],
  129	shell(Shell, Command, Argv),
  130	process_create(Shell, Argv,
  131		       [ stdin(In),
  132			 stdout(Out),
  133			 stderr(Error),
  134			 process(PID)
  135		       ]).
  136
  137shell(Shell, Command, ['/s', '/c', Command]) :-
  138	current_prolog_flag(windows, true), !,
  139	getenv('COMSPEC', Shell).
  140shell('/bin/sh', Command, ['-c', Command]).
  141
  142%%	wait(+PID, -Status)
  143%
  144%	Wait for processes created using exec/3.
  145%
  146%	@see exec/3
  147
  148wait(PID, Status) :-
  149	process_wait(PID, Status).
  150
  151%%	pid(-PID)
  152%
  153%	Process ID of the current process.
  154%
  155%	@compat sicstus.
  156
  157pid(PID) :-
  158	current_prolog_flag(pid, PID).
  159
  160%%	now(-When) is det.
  161%
  162%	Unify when with the current time-stamp
  163%
  164%	@compat sicstus
  165
  166now(When) :-
  167	get_time(Now),
  168	When is integer(Now).
  169
  170%%	datime(+When, -Datime) is det.
  171%%	datime(-When, +Datime) is det.
  172%
  173%	True when Datime is a  datime/6   record  that reflects the time
  174%	stamp When.
  175%
  176%	@compat sicstus
  177
  178datime(When, datime(Year,Month,Day,Hour,Min,Sec)) :-
  179	var(When),
  180	!,
  181	date_time_stamp(date(Year,Month,Day,Hour,Min,Sec,_,_,_), When).
  182datime(When, datime(Year,Month,Day,Hour,Min,Sec)) :-
  183	stamp_date_time(When, date(Year,Month,Day,Hour,Min,SecF,_,_,_), local),
  184	Sec is integer(SecF).
  185
  186%%	datime(-Datime) is det.
  187%
  188%	Unifies Datime with the current  date   and  time  as a datime/6
  189%	record  of  the  form  datime(Year,Month,Day,Hour,Min,Sec).  All
  190%	fields are integers.
  191%
  192%	@compat sicstus
  193
  194datime(datime(Year,Month,Day,Hour,Min,Sec)) :-
  195	get_time(Now),
  196	stamp_date_time(Now, date(Year,Month,Day,Hour,Min,SecF,_,_,_), local),
  197	Sec is integer(SecF).
  198
  199
  200%%	system.
  201%%	system(+Command).
  202%%	system(+Command, -Status).
  203%
  204%	Synomyms for shell/0, shell/1 and shell/2.
  205%
  206%	@compat sicstus.
  207
  208system :- shell.
  209system(Command) :- shell(Command).
  210system(Command, Status) :- shell(Command, Status).
  211
  212%%	popen(+Command, +Mode, ?Stream)
  213%
  214%	@compat sicstus
  215
  216popen(Command, Mode, Stream) :-
  217	open(pipe(Command), Mode, Stream).
  218
  219%%	host_name(-HostName)
  220%
  221%	@compat sicstus
  222%	@see gethostname/1
  223
  224host_name(HostName) :-
  225	gethostname(HostName).
  226
  227
  228		 /*******************************
  229		 *	 FILE OPERATIONS	*
  230		 *******************************/
  231
  232%%	mktemp(+Template, -File) is det.
  233%
  234%	Interface to the Unix function.  This emulation uses
  235%	tmp_file/2 and ignores Template.
  236%
  237%	@compat sicstus
  238%	@deprecated This interface is a security-risc.  Use
  239%	tmp_file_stream/3.
  240
  241mktemp(_Template, File) :-
  242	tmp_file(mkstemp, File).
  243
  244%%	tmpnam(-FileName)
  245%
  246%	Interface to tmpnam(). This emulation uses tmp_file/2.
  247%
  248%	@compat sicstus
  249%	@deprecated This interface is a security-risc.  Use
  250%	tmp_file_stream/3.
  251
  252tmpnam(File) :-
  253	tmp_file(tmpnam, File).
  254
  255%%	file_exists(+FileName) is semidet.
  256%
  257%	True if a file named FileName exists.
  258%
  259%	@compat sicstus
  260
  261file_exists(FileName) :-
  262	exists_file(FileName)