-- Stream.mesa (last edited by: McJones on: August 13, 1980  5:07 PM)

DIRECTORY
  Environment USING [Block, Byte, Word];

Stream: DEFINITIONS =
  BEGIN
  -- Types
  Handle: TYPE = POINTER TO Object;
  Byte: TYPE = Environment.Byte;
  Word: TYPE = Environment.Word;
  SubSequenceType: TYPE = [0..256);
  Block: TYPE = Environment.Block;
  InputOptions: TYPE = RECORD [
    terminateOnEndPhysicalRecord, signalLongBlock, signalShortBlock,
      signalSSTChange, signalEndOfStream: BOOLEAN];
  defaultInputOptions: InputOptions = [FALSE, FALSE, FALSE, FALSE, FALSE];
  CompletionCode: TYPE = {normal, endRecord, sstChange, endOfStream};
  -- Operations 
  GetByte: PROCEDURE [sH: Handle] RETURNS [byte: Byte] = INLINE {
    RETURN[sH.getByte[sH]]};
  -- Get the next byte from the stream.

  GetChar: PROCEDURE [sH: Handle] RETURNS [char: CHARACTER] = INLINE {
    RETURN[LOOPHOLE[sH.getByte[sH]]]};
  -- Get the next character from the stream.

  GetWord: PROCEDURE [sH: Handle] RETURNS [word: Word] = INLINE {
    RETURN[sH.getWord[sH]]};
  -- Get the next two bytes from the stream, and return them in a word (first in left half).

  GetBlock: PROCEDURE [sH: Handle, block: Block]
    RETURNS [
      bytesTransferred: CARDINAL, why: CompletionCode, sst: SubSequenceType] =
    INLINE {[bytesTransferred, why, sst] ← sH.get[sH, block, sH.options]};
  -- Get the next zero or more bytes from the stream, moving them to the specified block of memory.

  SetInputOptions: PROCEDURE [sH: Handle, options: InputOptions] = INLINE {
    sH.options ← options};
  -- Set the input options of the stream.

  PutByte: PROCEDURE [sH: Handle, byte: Byte] = INLINE {sH.putByte[sH, byte]};
  -- Put a byte at the next available position in the stream.

  PutChar: PROCEDURE [sH: Handle, char: CHARACTER] = INLINE {
    sH.putByte[sH, LOOPHOLE[char]]};
  -- Put a character at the next available position in the stream.

  PutWord: PROCEDURE [sH: Handle, word: Word] = INLINE {sH.putWord[sH, word]};
  -- Put two bytes into the next available positions in the stream, left half of word first.

  PutBlock: PROCEDURE [
    sH: Handle, block: Block, endPhysicalRecord: BOOLEAN ← FALSE] = INLINE {
    sH.put[sH, block, endPhysicalRecord]};
  -- Put zero or more bytes into the next available positions in the stream, moving them from a block in memory.

  SendNow: PROCEDURE [sH: Handle] = INLINE {
    bl: Block = [NIL, 0, 0]; sH.put[sH, bl, TRUE]};
  -- Terminate the current physical record.

  SetSST: PROCEDURE [sH: Handle, sst: SubSequenceType] = INLINE {
    sH.setSST[sH, sst]};
  -- Change the current SubSequenceType.

  SendAttention: PROCEDURE [sH: Handle, byte: Byte] = INLINE {
    sH.sendAttention[sH, byte]};
  -- Send an attention down the stream.

  WaitForAttention: PROCEDURE [sH: Handle] RETURNS [Byte] = INLINE {
    RETURN[sH.waitAttention[sH]]};
  -- Wait for an attention to arrive.

  Delete: PROCEDURE [sH: Handle] = INLINE {sH.delete[sH]};
  -- Delete the stream object.
  -- Signals and errors

  EndOfStream: SIGNAL [nextIndex: CARDINAL];
  LongBlock: SIGNAL [nextIndex: CARDINAL];
  ShortBlock: ERROR;
  SSTChange: SIGNAL [sst: SubSequenceType, nextIndex: CARDINAL];
  TimeOut: SIGNAL [nextIndex: CARDINAL];
  -- Representation
  Object: TYPE = RECORD [
    options: InputOptions,
    getByte: GetByteProcedure,
    putByte: PutByteProcedure,
    getWord: GetWordProcedure,
    putWord: PutWordProcedure,
    get: GetProcedure,
    put: PutProcedure,
    setSST: SetSSTProcedure,
    sendAttention: SendAttentionProcedure,
    waitAttention: WaitAttentionProcedure,
    delete: DeleteProcedure];
  GetByteProcedure: TYPE = PROCEDURE [sH: Handle] RETURNS [byte: Byte];
  PutByteProcedure: TYPE = PROCEDURE [sH: Handle, byte: Byte];
  GetWordProcedure: TYPE = PROCEDURE [sH: Handle] RETURNS [word: Word];
  PutWordProcedure: TYPE = PROCEDURE [sH: Handle, word: Word];
  GetProcedure: TYPE = PROCEDURE [sH: Handle, block: Block, options: InputOptions]
    RETURNS [
      bytesTransferred: CARDINAL, why: CompletionCode, sst: SubSequenceType];
  PutProcedure: TYPE = PROCEDURE [
    sH: Handle, block: Block, endPhysicalRecord: BOOLEAN];
  SetSSTProcedure: TYPE = PROCEDURE [sH: Handle, sst: SubSequenceType];
  SendAttentionProcedure: TYPE = PROCEDURE [sH: Handle, byte: Byte];
  WaitAttentionProcedure: TYPE = PROCEDURE [sH: Handle] RETURNS [Byte];
  DeleteProcedure: TYPE = PROCEDURE [sH: Handle];
  -- The following are default values for stream object records 
  defaultObject: READONLY Object;
  -- = [
  -- options: defaultInputOptions, (see above)
  -- getByte: DefaultGetByte, (requires sH.get defined)
  -- putByte: DefaultPutByte, (requires sH.put defined)
  -- getWord: DefaultGetWord, (requires one of sH.getByte or sH.get defined)
  -- putWord: DefaultPutWord, (requires one of sH.putByte or sH.put defined)
  -- get: DefaultGet, (requires sH.getByte defined)
  -- put: DefaultPut, (requires sH.putByte defined)
  -- setSST: DefaultSetSST, (raises Runtime.UnboundProcedure)
  -- sendAttention: DefaultSendAttention, (raises Runtime.UnboundProcedure)
  -- waitAttention: DefaultWaitAttention, (raises Runtime.UnboundProcedure)
  -- delete: DefaultDelete];	(raises Runtime.UnboundProcedure)

  END.
LOG
Time: March 17, 1978  11:39 AM   By: Lauer	Action: Created file
Time: April 6, 1978  3:24 PM	By: Lauer	Action: Updated to conform to revised Functional Specifications
Time: April 18, 1978  9:58 AM	By: Lauer	Action: Updated for compilation by Alpha release of Mesa 4.0 (LONG POINTER is allowed).
Time: May 5, 1978  2:19 PM	By: Lauer	Action: Added GetWord and PutWord
Time: June 22, 1978  8:57 AM	By: Lauer	Action: Added EndOfStream signal, input option, and completion code
Time: July 21, 1978  11:31 AM	By: Lauer	Action: Moved type Block from Stream to Environment; added GetChar and PutChar
Time: August 14, 1978  2:03 PM	By: Lauer	Action: Deleted DeleteModule (can be simulated by Runtime.UnNew[GlobalFrame[p]]; moved "DeleteModuleAfterReturn" to Runtime and renamed it SelfDestruct
Time: February 22, 1979  2:50 PM   By: Dalal	Action: Moved a large number of procedures from StreamImpl.mesa to this module as INLINEs; added the parameter sH to all the procedures of Stream.Object; added Delete
Time: March 13, 1979  10:17 AM   By: Dalal	Action: Modified SendAttention and WaitForAttention to take and return a byte of data respectively
Time: February 1, 1980  7:16 PM   By: McJones	Action: Added object procedures for get/put byte/word; new defaults
Time: August 13, 1980  5:07 PM   By: McJones	Action: Deleted defaults within definition of Object