System and environment

 

Presentation

Running a UNIX or a Windows process, getting information on environment variables are necessary services for interacting properly with the current operating system.

 

 

System environment variables

Dos and Windows use the notion of a system variable, which is close to the notion of the UNIX environment variable.  It is possible to take the value of these variables from J.  For everything linked to internal Objecteering parameterization, we strongly recommend the use of "MDAC parameters".

 

getEnvironmentInfo(in String info,

 out String result);

Returns the value of the "info" environment variable in "result".  If "info" does not exist, then "result" is an empty string.  An undefined value or a value defined as empty produces the same result (result = "")

 

Example:   To retrieve the content of the PATH variable:

String path;

   getEnvironmentInfo ("PATH", path);

 

 

String getObjingPath ()

Returns the "OBJING_PATH" environment variable, which specifies where Objecteering is located.

 

 

Running a process

boolean Object:checkedSpawn (in String command_i,

inout String result_o)

This operation spawns a command and returns results.  It is simpler but provides less control than the following commands.

 

Example: checkedSpawn ("ps", result);

boolean Object:spawnProcess(in String commandLine,

in boolean withConsole,

in boolean withShell,

in boolean wait,

out int cmdStatus)

Creates a process which executes "commandLine". "withShell" indicates whether or not the command should be launched in a shell (sh for Unix, command.com for Win95 and cmd for NT).  "wait" indicates whether or not you should wait for the end of the process.  "withConsole" is only used when console Windows (NT/95) programs are launched, and indicates whether or not the MS/DOS console should be made to appear.  "cmdStatus" indicates the outgoing status of the command, and is only valid if "wait" is true.  This method returns a status which indicates whether or not it was possible to launch the command.

 

 

boolean Object:spawnOutputProcess

(in string commandLine,

in boolean withConsole,

in boolean withShell,

out int cmdStatus,

out String output)

Create a process which executes "commandLine". "withShell" indicates whether or not the command should be launched in a shell (sh for Unix, command.com for Win95 and cmd for NT).  "withConsole" is only used when console Windows (NT/95) programs are launched, and indicates whether or not the MS/DOS console should be made to appear.  "cmdStatus" indicates the outgoing status of the command. "output" contains the set of standard and error outgoings of the command.  This method returns a status which indicates whether or not it was possible to launch the command.

 

Notes on the two above calls     

In Windows, when the "withConsole" parameter is equal to "false", the child process with the SW_HIDE attribute is launched.  Consequently, when the program launched is a Windows application (msdev, for example) it is obligatory to set this parameter to "true", otherwise the application will never appear.

 

"commandLine" accepts blanks in file names if inverted commas (" ") are placed around the file names.

 

When the "withShell" parameter is equal to "true", it is possible to put any metacharacter (*, >, etc) known to the shell into "commandLine".  The metacharacter obviously depends on the platform shell (be careful of Unix/Windows, Windows/NT and even Windows 95 portability).

 

 

Getting the day's date

String Object:getCurrentTime ()

This returns the current date.

 

String Object getFormatedTime (in String pFormat)

This returns the date in the format specified by the pFormat string.  This carries out the equivalent of the "strftime" system call.

For example, the result of the following example:

String pFormat = "%d / %m / %y"

String ITime = getFormatedTime(pFormat);

StdOut.write (Itime);

 

is the current date, for example, 23 / 11 / 06.

 

The formatting codes for pFormat are presented in the following table.

 

The code ...

represents ...

%a

the abbreviated weekday name.

%A

the full weekday name.

%b

the abbreviated month name.

%B

the full month name.

%c

the date and time representation appropriate to the local time-zone.

%d

the day of the month as a decimal number (01-31).

%H

the hour in 24-hour format (00-23).

%I

the hour in 12-hour format (01-12).

%j

the day of the year as a decimal number (001-366).

%m

the month as a decimal number (01-12).

%M

the minute as a decimal number (00-59).

%p

the current local time-zone's a.m./p.m. indicator for a 12-hour clock.

%S

the second as a decimal number (00-59).

%U

the week of the year as a decimal number, with Sunday as the first day of the week (00-53).

%w

the weekday as a decimal number (0-6, where Sunday is 0).

%W

the week of the year as a decimal number, with Monday as the first day of the week (00-53).

%x

the representation of the date for the current local time-zone.

%X

the representation of the time for the current local time-zone.

%y

the year without the century, as a decimal number (00-99).

%Y

the year with the century, as a decimal number.

%Z

the name or abbreviation of the time-zone (no characters if the time-zone is unknown).

%%

the percent sign.

 

 

As in the print function, the # flag can prefix any formatting code.  In this case, the meaning of the format code is changed as follows:

 

The format code ...

for the ... platform

means ...

%#a

%#A

%#b

%#B

%#p

%#X

%#z

%#Z

%#%

Windows

that the # flag is ignored.

%#c

Windows

that long date and time representation, appropriate to the current local time-zone, is used (for example, "Thursday, November 26, 2006, 11:30:15").

%#x

Windows

that long date representation, appropriate to the current local time-zone, is used (for example, "Thursday, November 26, 2006, 11:30:15").

%#d

%#H

%#I

%#j

%#m

%#M

%#S

%#U

%#w

%#W

%#y

%#Y

Windows

that any leading zeros are removed.

%C

UNIX

that long date and time representation, appropriate to the current local time-zone, is used (for example, "Thursday, November 26, 2006, 11:30:15").