SIMULA interface to a UNIX environment

Auteur: J.Vaucher
Version: 1.2
Date: 17 octobre 1997

Source Code: unix.src

This class provides an interface to features commonly used for systems programming on UNIX, in particular:

It also allows testing if SYSIN is connected to a file or to a terminal.

Using the UNIX class

To access the procedures described here, you must have an external class specification and compile the program with either the -diro option or the -simwin option.

Example: unix_test.sim

   begin    ! compile with "sim -diro unix_test" ;
      EXTERNAL class UNIX;
      Text T;
      Integer i;
      outtext("Program >" & arg(0) & "< called with ");
      outtext(" arguments"); outimage;
      outtext("Home directory = "); outtext(env("HOME"); outimage;
      outtext("Environment VARIABLES"); outimage;
      T :- envarg(0);
      for i := 0, i+1 while T =/= notext do
         outint(i,5); outtext(T); outimage;
         T :- envarg(i+1)

Accessing call line arguments

integer procedure Argc;
Like its C counterpart, it returns the number of arguments +1.
text procedure Arg (n); integer n;
returns text values of Argv[n] for n in [0:argc-1]
Note that Arg(0) returns the name used to invoke the programme.

Environment variables

text procedure Env(T); Text T;
Interface to the unix getenv function which return the value of a given variable. In my case: Env("USER") returns "vaucher". For a non-existent variable, notext is returned.
text procedure EnvArg (n); integer n;
returns the nth "VARIABLE=VALUE" string where n=0,1,.... Last value is NOTEXT and access to any string beyond the last will probably lead to error.

Password file

This file is a readable text file with one line per USER containing the folllowing fields:
where PASSWD is the encrypted password....off course. From a terminal, the file can be listed with "ypcat passwd". Access to this file can be used to expand file names with a tilde such as "~dift1010" into the full path /JSP/E2/usagers/dift1010

text procedure GetPWnam(login_name); Text login_name;
This returns the password entry for the USER with login_name.
text procedure Home_of(login_name); Text login_name;
returns the path to he HOME directory of the USER with login_name.

Interactive input

In many case, a program should act differently depending if the input is from a terminal or from a file. With interactive input (from a terminal), the program should prompt the user for the required data; whereas this kind of interaction is unecessary when connected to a file.

Boolean procedure tty_input;
returns TRUE if SYSIN is connected to a terminal. The procedure calls the UNIX function isatty(0)

Direct Memory Access

The following functions allow access to data structures, pointers and strings returned by C functions. The PEEK functions take an integer address and return (as integers) data of different sizes:
   Peek(adr)     ==>  returns a 32 bit word
   Peek16(adr)   ==>  returns a 16 bit halfword
   Peek8(adr)    ==>  returns an 8 bit character
integer procedure Peek(Adr); integer Adr;
integer procedure Peek16(Adr); integer Adr;
integer procedure Peek8(Adr); integer Adr;
Text procedure CtoSText(Adr); integer Adr;
takes the address of a zero terminated "C" string as a parameter and creates a Simula Text which is a copy of the "C" string

Source Code