Home Contents Index Summary Previous Next

4.35 Operating System Interaction

shell(+Command, -Status)
Execute Command on the operating system. Command is given to the Bourne shell (/bin/sh). Status is unified with the exit status of the command.

On Win32 systems, shell/[1,2] executes the command using the CreateProcess() API and waits for the command to terminate. If the command ends with a & sign, the command is handed to the WinExec() API, which does not wait for the new task to terminate. See also win_exec/2 and win_shell/2. Please note that the CreateProcess() API does not imply the Windows command interpreter (command.exe on Windows 95/98 and cmd.exe on Windows-NT) and therefore commands built-in to the command-interpreter can only be activated using the command interpreter. For example: 'command.exe /C copy file1.txt file2.txt'

shell(+Command)
Equivalent to `shell(Command, 0)'.

shell
Start an interactive Unix shell. Default is /bin/sh, the environment variable SHELL overrides this default. Not available for Win32 platforms.

win_exec(+Command, +Show)
Win32 systems only. Spawns a Windows task without waiting for its completion. Show is either iconic or normal and dictates the initial status of the window. The iconic option is notably handy to start (DDE) servers.

win_shell(+Operation, +File)
Win32 systems only. Opens the document File using the windows shell-rules for doing so. Operation is one of open, print or explore or another operation registered with the shell for the given document-type. On modern systems it is also possible to pass a URL as File, opening the URL in Windows default browser. This call interfaces to the Win32 API ShellExecute().

win_registry_get_value(+Key, +Name, -Value)
Win32 systems only. Fetches the value of a Win32 registry key. Key is an atom formed as a path-name describing the desired registry key. Name is the desired attribute name of the key. Value is unified with the value. If the value is of type DWORD, the value is returned as an integer. If the value is a string it is returned as a Prolog atom. Other types are currently not supported. The default `root' is HKEY_CURRENT_USER. Other roots can be specified explicitely as HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE or HKEY_USERS. The example below fetches the extension to use for Prolog files (see README.TXT on the Windows version):


?- win_registry_get_value('HKEY_LOCAL_MACHINE/Software/SWI/Prolog',
                          fileExtension,
                          Ext).

Ext = pl

getenv(+Name, -Value)
Get environment variable. Fails silently if the variable does not exist. Please note that environment variable names are case-sensitive on Unix systems and case-insensitive on Windows.

setenv(+Name, +Value)
Set environment variable. Name and Value should be instantiated to atoms or integers. The environment variable will be passed to shell/[0-2] and can be requested using getenv/2. They also influence expand_file_name/2.

unsetenv(+Name)
Remove environment variable from the environment.

unix(+Command)
This predicate comes from the Quintus compatibility library and provides a partial implementation thereof. It provides access to some operating system features and unlike the name suggests, is not operating system specific. Defined Command's are below.

system(+Command)
Equivalent to calling shell/1. Use for compatibility only.

shell(+Command)
Equivalent to calling shell/1. Use for compatibility only.

shell
Equivalent to calling shell/0. Use for compatibility only.

cd
Equivalent to calling working_directory/2 to the expansion (see expand_file_name/2) of ~. For compatibility only.

cd(+Directory)
Equivalent to calling working_directory/2. Use for compatibility only.

argv(-Argv)
Unify Argv with the list of commandline arguments provides to this Prolog run. Please note that Prolog system-arguments and application arguments are separated by --. Integer arguments are passed as Prolog integers, float arguments and Prolog floating point numbers and all other arguments as Prolog atoms. New applications should use the prolog-flag argv. See also prolog prolog-flag argv.

A stand-alone program could use the following skeleton to handle command-line arguments. See also section 2.10.2.4.


main :-
        unix(argv(Argv)),
        append(_PrologArgs, [--|AppArgs], Argv), !,
        main(AppArgs).

4.35.1 Dealing with time and date

There is no standard for time-representation in the Prolog community. SWI-Prolog represents it as a floating-point number using the same basic representation as the POSIX standard, seconds elapsed since the January 1970, 0 hours. This format is also used for predicates accessing time-information from files (see time_file/2).

get_time(-Time)
Return the number of seconds that elapsed since the epoch of the POSIX, tim representation: January 1970, 0 hours. Time is a floating point number. The granularity is system dependent.

convert_time(+Time, -Year, -Month, -Day, -Hour, -Minute, -Second, -MilliSeconds)
Convert a time stamp, provided by get_time/1, time_file/2, etc. Year is unified with the year, Month with the month number (January is 1), Day with the day of the month (starting with 1), Hour with the hour of the day (0--23), Minute with the minute (0--59). Second with the second (0--59) and MilliSecond with the milliseconds (0--999). Note that the latter might not be accurate or might always be 0, depending on the timing capabilities of the system. See also convert_time/2.

convert_time(+Time, -String)
Convert a time-stamp as obtained though get_time/1 into a textual representation using the C-library function ctime(). The value is returned as a SWI-Prolog string object (see section 4.23). See also convert_time/8.

4.35.2 Controlling the PLWIN.EXE console window

The Windows executable PLWIN.EXE console has a number of predicates to control the appearance of the console. Being totally non-portable, we do not advice using it for your own application, but use XPCE or another portable GUI platform instead. We give the predicates for reference here.

window_title(-Old, +New)
Unify Old with the title displayed in the console and change the title to New. (42)

win_window_pos(+ListOfOptions)
Interface to the MS-Windows SetWindowPos() function, controlling size, position and stacking order of the window. ListOfOptions is a list that may hold any number of the terms below.

size(W, H)
Change the size of the window. W and H are expressed in character-units.

position(X, Y)
Change the top-left corner of the window. The values are expressed in pixel units.

zorder(ZOrder)
Change the location in the window stacking order. Values are bottom, top, topmost and notopmost. Topmost windows are displayed above all other windows.

show(Bool)
If true, show the window, if false hide the window.

activate
If present, activate the window.

win_has_menu
Suceeds if win_insert_menu/2 and win_insert_menu_item/4 are present.

win_insert_menu(+Label, +Before)
Insert a new entry (pulldown) in the menu. If the menu already contains this entry, nothing is done. The Label is the label and using the Windows conventions, a letter prefixed with & is underlined and defines the associated accelerator key. Before is the label before which this one must be inserted. Using - adds the new entry at the end (right). For example, the call below adds a Application entry just before the Help menu.


win_insert_menu('&Application', '&Help')

win_insert_menu_item(+Pulldown, +Label, +Before, :Goal)
Add an item to the named Pulldown menu. Label and Before are handled as in win_insert_menu/2, but the label - inserts a separator. Goal is called if the user selects the item.