TextBinCIO and Uni* Definition Modules of ModulAware's PDP-11/RT-11 Modula-2 Compiler Kit

DEFINITION MODULE TextBinCIO;
(* 
  Channel oriented text (ASCII) and binary I/O:
  binary: Sequential input/output stream supporting any type or structure.
  NOT binary: Sequential input/output streams supporting 
    UnivTypes.ReadProc and UnivTypes.WriteProc. 

  Example: UseTex.MOD

  Imported modules: Streams (imports Storage), FilePool, Files, TTIO, ASCII,
    Exceptions, SystemTypes. 

  Started: Aug-1986, derived from module UnivCIO (Mar-1983) and extended
    to support binary files also by R. Singer.
*)
  IMPORT Files, Streams; FROM SYSTEM IMPORT WORD;

  CONST CTMAX = 15;
    TT = CTMAX; 
    (* TT is a predefined text CHANNEL for TTIO;
       no call of procedure Channel (...)  necessary,
       reading & writing allowed, no Break necessary (ignored) *)

  TYPE FileName = Files.FileName;

  CRANGE = [0..CTMAX];
  CHANNEL = CRANGE;
  CTYPE = RECORD
      s: Streams.STREAM;
      f: Files.FILE;
      reply: INTEGER;
      fn: FileName;
      new: BOOLEAN;
      binary: BOOLEAN;
    END;

  VAR CTA: ARRAY CRANGE OF CTYPE;

  PROCEDURE Channel (dfn: FileName; NewFileOrDevice, Binary: BOOLEAN;
    VAR c: CHANNEL; VAR ChannelOK: BOOLEAN);

    (* Connect an Binary or ASCII stream with dfn (device, filename, extension)
       to channel c;
       IF NewFileOrDevice THEN Create dfn; now WRITING
         to the channel c is allowed;
       ELSE Lookup; now READING from channel c is allowed;
       END;
    *)

  PROCEDURE Break (c: CHANNEL) ;
  (* print rest of internal operating system buffer;
     ignored on file structured devices (disks) and on channel TT *)

  PROCEDURE DeChannel (VAR c: CHANNEL; endWrite, close: BOOLEAN);
  (* close channel c, write out buffer (endWrite) and close associated file *)

  PROCEDURE EOC (c: CHANNEL): BOOLEAN;
  (* end of channel (for input); EOC(TT) is always FALSE *)

  (* calls of SetGet, Get, Put, SetPut, Read and Write are only allowed
     if the channel is NOT binary, otherwise an I/O-error will be raised *)
  PROCEDURE SetGet (c: CHANNEL); (* channel to be used in Get *)
  PROCEDURE Get (VAR ch: CHAR);
  (* get a char from channel predefined by SetGet; is of type ReadProc *)

  PROCEDURE SetPut (c: CHANNEL); (* channel to be used in Put *)
  PROCEDURE Put (ch: CHAR); 
  (* put ch to the channel predefined by SetPut; is of type WriteProc *)

  PROCEDURE NoEcho (ch: CHAR); (* doesn't do anything; is of type WriteProc *)

  PROCEDURE ReadSetStatus (VAR GetC, PutC: CHANNEL);
  (* get channels predefined by SetGet and SetPut *)
  PROCEDURE RestoreSetStatus (GetC, PutC: CHANNEL);
  (* restore channels predefined to the values obtained by ReadSetStatus *)

  (* use Read and Write, if you don't like to switch between 
     different channels: *)
  PROCEDURE Read (c: CHANNEL; VAR ch: CHAR); (* means SetGet(c); Get(ch) *)
  PROCEDURE Write (c: CHANNEL; ch: CHAR); (* means SetPut(c); Put(ch) *)

  (* calls of BinGet, BinPut, BinRead, BinWrite are only allowed
     if the channel is binary, otherwise an I/O-error will be raised *)
  PROCEDURE BinGet (c: CHANNEL; VAR t: WORD);
  PROCEDURE BinPut (c: CHANNEL; t: WORD);
  PROCEDURE BinRead (c: CHANNEL; VAR t: ARRAY OF WORD);
  PROCEDURE BinWrite (c: CHANNEL; VAR t: ARRAY OF WORD);

  (* use Reset, GetPosition and SetPosition for positioning the channel: *)
  PROCEDURE Reset (c: CHANNEL; endWrite: BOOLEAN);
  (* set c to start position of file/channel and write out buffer (endWrite) *)

  PROCEDURE GetPosition (c: CHANNEL; VAR highpos, lowpos: CARDINAL);
  (* see Streams.GetPos, ignored on channel TT *)

  PROCEDURE SetPosition (c: CHANNEL; highpos, lowpos: CARDINAL);
  (* see Streams.SetPos, ignored on channel TT, 
     not allowed on a write only device.
     highpos and lowpos must be obtained with GetPosition.
  *)
END TextBinCIO.


DEFINITION MODULE UniLInput; FROM UnivTypes IMPORT ReadProc, WriteProc; PROCEDURE ReadLong (Read: ReadProc; Write: WriteProc; VAR x: LONGINT; VAR isNumber: BOOLEAN; VAR termChar: CHAR); (* Read longinteger number in free format using < Read > and echo using < Write > isNumber = valid number was read and MIN (LONGINT) <= x <= MAX (LONGINT); valid number = { " " } [sign] {digit} termChar. digit = "0" | ... | "9". sign = "-" | "+". termChar = " " | "," | "< endOfLine >" | "< ESC >". *) END UniLInput.
DEFINITION MODULE UniLOutput; FROM UnivTypes IMPORT ReadProc, WriteProc; PROCEDURE WriteLong (Write: WriteProc; x: LONGINT; width: CARDINAL); END UniLOutput.
DEFINITION MODULE UnivCardInput; FROM UnivTypes IMPORT ReadProc, WriteProc; PROCEDURE ReadCard (Read: ReadProc; Write: WriteProc; VAR x: CARDINAL; VAR IsNum: BOOLEAN; VAR nextChar: CHAR); (* Read a decimal cardinal number. Echo using Write. NOT IsNum = an illegal character was Read. Leading blanks are ignored. The terminator is assigned to nextChar. Terminators are: ',' | ' ' | < ESC > | < LF > | < CR >. The Terminator nextChar is not echoed. No tests for overflow are made. *) END UnivCardInput.
DEFINITION MODULE UnivDate; FROM Clock IMPORT Time; FROM UnivTypes IMPORT WriteProc; PROCEDURE WriteDate (Write: WriteProc; t: Time); (* Write the date in the form "dd-Mmm-jj" with day roll over for RT11SJ (if hours > 24) *) PROCEDURE WriteTime (Write: WriteProc; t: Time); (* Write the time in the form "xx:xx:xx.xx" (hours:min:sec.(sec/100)) *) END UnivDate.
DEFINITION MODULE UnivDInput; FROM UnivTypes IMPORT ReadProc, WriteProc; PROCEDURE ReadLongReal (Read: ReadProc; Write: WriteProc; VAR r: LONGREAL; VAR isNumber: BOOLEAN; VAR termChar: CHAR); (* Read longreal number in free format using < Read > and echo using < Write > isNumber = valid number was read and ABS (r) <= MAX (LONGREAL); valid number = { " " } [sign] [ {digit} [ "." [ {digit} ] [ exp [sign] {digit} ] ] ] termChar. digit = "0" | ... | "9". sign = "-" | "+". exp = "E" | "e" | "D" | "d". termChar = " " | "," | "< endOfLine >" | "< ESC >". *) END UnivDInput.
DEFINITION MODULE UnivDOutput; FROM UnivTypes IMPORT WriteProc; PROCEDURE WriteLongReal (Write: WriteProc; r: LONGREAL; width, fractionlength: CARDINAL); (* if (width = 0) & (fractionlength = 0) then E (D)-format *) END UnivDOutput.
DEFINITION MODULE UnivDSReal; (* First version: 01-May-81. Modula-2 REAL*4 conversion support String to Real conversion (* in free format *) used in UnivDInput. *) PROCEDURE StringLongReal (str: ARRAY OF CHAR; VAR err:BOOLEAN): LONGREAL; (* Encodes str to LONGREAL. err:=TRUE <---> str is not filled with valid digits or ABS(real number) > MAXREAL; str = [sign] [ {digit} [ "." [ {digit} ] [ exp [sign] {digit} ] ] ]. { non digit } digit = "1" | ... | "9". sign = "-" | "+". exp = "E" | "e". *) END UnivDSReal.
DEFINITION MODULE UnivEdit; (* started: 1983, revisions: Dez-1986 (from module Retype) Displays a (possibly empty) line 's' and allows single line editing of this line (TIRF). Could be used for simple screen edit operations within an application program. Example: UseEdi.MOD *) FROM UnivTypes IMPORT ReadProc, WriteProc, BoolProc; PROCEDURE Retype(Read: ReadProc; Write: WriteProc; VAR s: ARRAY OF CHAR; UpperCase: BOOLEAN; Terminate: BoolProc; VAR termChar: CHAR); (* edit line s with TIRF *) END UnivEdit.
DEFINITION MODULE UnivFileName; (* SEK 4. 3. 1980, Derived from FileNames 4-Feb-83, Reading via ReadString.ReadString (TIRF), Univ* concept. Apr. 1984 *) FROM Files IMPORT FileName; FROM UnivTypes IMPORT ReadProc, WriteProc; PROCEDURE ReadFileName(ReadChar: ReadProc; WriteChar: WriteProc; VAR fn: FileName; default: FileName; VAR TypedFields: BITSET; VAR nextChar: CHAR); (* 0 IN TypedFields <=> ~,devicename was typed 1 IN TypedFields <=> ~,filename was typed 2 IN TypedFields <=> ~,extention was typed Other bits in TypedFields are reserved for ReadFileName; nextChar is the termination character *) END UnivFileName.
DEFINITION MODULE UnivInput; (* Jun-82 (TTInOut). Feb-83 (Proc variables like EDISON 1981) Jan-1983 (seperate module input/output) Implementation derived from InOut (N. Wirth (ETH Zurich) 19-Feb-80). *) FROM UnivTypes IMPORT ReadProc, WriteProc; PROCEDURE ReadInt(Read: ReadProc; Write: WriteProc; VAR x: INTEGER; VAR IsNum: BOOLEAN; VAR nextChar: CHAR); (* Read a decimal integer. Echo using Write. NOT IsNum = (a sign without any digit occurs) OR (an illegal character was Read). Leading blanks are ignored. The terminator is assigned to nextChar. Terminators are: ',' | ' ' | < ESC > | < LF > | < CR >. The Terminator nextChar is not echoed. No tests for overflow are made. *) END UnivInput.
DEFINITION MODULE UnivOutput; (* Jun-82 (TTInOut). Feb-83 (Proc variables like EDISON 1981) Jan-1983 (seperate module input/output) Implementation derived from InOut (N. Wirth (ETH Zurich) 19-Feb-80). *) FROM SYSTEM IMPORT WORD; FROM UnivTypes IMPORT WriteProc; PROCEDURE WriteInt (Write: WriteProc; x: INTEGER; n: CARDINAL); PROCEDURE WriteCard (Write: WriteProc; x, n: CARDINAL); PROCEDURE WriteOct (Write: WriteProc; w: WORD; n: CARDINAL); (* PROCEDURE WriteHex (Write: WriteProc; w: WORD; n: CARDINAL); *) PROCEDURE Writeln (Write: WriteProc) ; PROCEDURE WriteString (Write: WriteProc; s: ARRAY OF CHAR); PROCEDURE WriteStringPart (Write: WriteProc; s: ARRAY OF CHAR; Length: CARDINAL); END UnivOutput.
DEFINITION MODULE UnivRDate; FROM Clock IMPORT Time; FROM UnivTypes IMPORT WriteProc, ReadProc; PROCEDURE ReadDate (Read: ReadProc; Write: WriteProc; VAR Date: Time; VAR ok: BOOLEAN; VAR termch: CHAR); (* input format: "dd-mmm-jj" or "dd:mm:jj" *) END UnivRDate.
DEFINITION MODULE UnivRInput ; FROM UnivTypes IMPORT ReadProc, WriteProc; PROCEDURE ReadReal(Read: ReadProc; Write: WriteProc; VAR r:REAL; VAR isnum:BOOLEAN; VAR TermChar: CHAR); (* Read real number in free format using Read and echo using Write. isNumber = validRealNumber and ABS(r) < MAX (REAL); validRealNumber = { " " } [sign] [ {digit} [ "." [ {digit} ] [ exp [sign] {digit} ] ] ] { termChar }. digit = "0" | ... | "9". sign = "-" | "+". exp = "E" | "e". termChar = " " | "," | "" | "endOfLine". *) END UnivRInput.
DEFINITION MODULE UnivROutput; FROM UnivTypes IMPORT WriteProc; PROCEDURE WriteReal (Write: WriteProc; r: REAL; width, fractionlength: CARDINAL); (* E-Format is used if width=0 and fractionlength=0 *) END UnivROutput.
DEFINITION MODULE UnivSReal; (* This module converts a string to REAL (in free format); it is used in UnivRInput only. First version: 01-May-81. *) PROCEDURE StringReal (str: ARRAY OF CHAR; VAR err:BOOLEAN): REAL; (* Encodes str to REAL. err:=TRUE < --- > str is not filled with valid digits or ABS(real number) > MAXREAL; str = [sign] [ {digit} [ "." [ {digit} ] [ exp [sign] {digit} ] ] ]. { non digit } digit = "0" | ... | "9". sign = "-" | "+". exp = "E" | "e". *) END UnivSReal.
DEFINITION MODULE UnivString; (* Central input module for strings and numbers, used for - integer, cardinal input (UnivInput, UnivCardInput), - real input (UnivRInput), - long integers (UniLInput), - long real input (UnivDInput), - file names (UnivFileName), and - date (UnivRDate), and - module Options (substituted by ReadOp.(MOD, LNK)) Works also in single character input mode, from command file and from NewStreams/Streams: - eats up the line feed (LF) if the previous char is a CR - eats up NUL (0C) (e.g. for TTIO.SetMode(1,bool);) Revisions: Feb-82(ModuleIO), 02-Nov-82, Jan-83: read/write independent; May-83: Video Terminal Independent RETYPING Feature (TIRF); Jun-83: TermNumber: terminate on wrong characters Apr-1984: same as module ReadString Apr-1985: eat up leading blanks if ~+TermNumber~- is used with ReadString. DEL now deletes to the left, if cursor at rightmost position. *) FROM UnivTypes IMPORT ReadProc, WriteProc, BoolProc; PROCEDURE ReadString (Read: ReadProc; Echo: WriteProc; VAR s: ARRAY OF CHAR; UpperCase: BOOLEAN; Terminate: BoolProc; VAR TerminationChar: CHAR); (* Read string "s" using "Read" for input and "Write" for the echo. For standard input substitute "TTIO.Read" and "TTIO.Write". The input characters are stored in "s" and echoed [in UpperCase]. _Retyping (TIRF)_: Type any character and use < Backspace > or < BS > to move left, < TAB > or < HT > to move right 5 characters, < DEL > or < RUBOUT > to delete the char at cursor position. < DEL > or < RUBOUT > to delete the char to the left, if cursor is at the end of the string. All other characters < " " are ignored. The terminator typed at any position terminates the retyping mode. "Terminate" (BoolProc) is user defineable and gives TRUE if a termination char is read. It is called for every char "Read". If the standard terminator is ok for your application, then substitute "TermString" or "TermNumber" for the parameter "Terminate". If Terminate (" ") then leading blanks are ignored. IF the number of characters typed is greater than HIGH(s) THEN the rest is ignored until Terminate. The "TermationChar" is not echoed. IF TTIO.SetMode(do not wait or busy read mode) is enabled, then REPEAT Read(ch) UNTIL ch <> NUL; is used. *) PROCEDURE TermString (ch: CHAR): BOOLEAN; (* Standard terminator: < CR > or < LF > or < ESC >. Leading blanks aren't ignored *) PROCEDURE TermNumber (ch: CHAR): BOOLEAN; (* Standard terminator: TermString or " " or ",". Leading blanks are ignored *) END UnivString.
DEFINITION MODULE UnivTypes; (* Revision: Jun-83, to avoid circular references. Definition only. DO NOT RECOMPILE ! *) TYPE ReadProc = PROCEDURE (VAR CHAR); WriteProc = PROCEDURE (CHAR); BoolProc = PROCEDURE (CHAR): BOOLEAN; END UnivTypes.
DEFINITION MODULE UnivWriteFileName; FROM UnivTypes IMPORT WriteProc; FROM Files IMPORT FileName; PROCEDURE WriteFileName (Write: WriteProc; fn: FileName; VAR fnl: CARDINAL); (* writes "DEV:FILENA.EXT". fnl is the number of chars written *) END UnivWriteFileName.