--
--	Inmos Libraries
--	Copyright (C) 1988, 1989, 1990, 1991, 1993  Inmos Limited
--
--	This library is free software; you can redistribute it and/or
--	modify it under the terms of the GNU Lesser General Public
--	License as published by the Free Software Foundation; either
--	version 2 of the License, or (at your option) any later version.
--
--	This library is distributed in the hope that it will be useful,
--	but WITHOUT ANY WARRANTY; without even the implied warranty of
--	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
--	Lesser General Public License for more details.
--
--	You should have received a copy of the GNU Lesser General Public
--	License along with this library; if not, write to the Free Software
--	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
--

--** @module streamio

#OPTION "E"  -- no compliler libs
#COMMENT "Stream i/o library, V1.1, 14/06/90"
#COMMENT "(c) Copyright INMOS Limited, 1988"
#INCLUDE "streamio.inc"
--{{{  history
-- Introduced st.help and st.initialise into the CASE statements, for
-- they had been omitted.  Also added 4 more to tp in st.out.string case
-- of ss.scrstream.to.array.  New version is 1.1 ( old version was
-- 0.01, 23-Dec-88 ); 14-June-90 SRH
--}}}
--{{{  tt. tag values
VAL tt.reset      IS BYTE  0 :
VAL tt.up         IS BYTE  1 :
VAL tt.down       IS BYTE  2 :
VAL tt.left       IS BYTE  3 :
VAL tt.right      IS BYTE  4 :
VAL tt.goto       IS BYTE  5 :
VAL tt.ins.char   IS BYTE  6 :
VAL tt.del.char   IS BYTE  7 :
VAL tt.out.string IS BYTE  8 :
VAL tt.clear.eol  IS BYTE  9 :
VAL tt.clear.eos  IS BYTE 10 :
VAL tt.ins.line   IS BYTE 11 :
VAL tt.del.line   IS BYTE 12 :
VAL tt.beep       IS BYTE 13 :
-- nothing defined for BYTE 14
VAL tt.terminate  IS BYTE 15 :
VAL tt.help       IS BYTE 16 :
VAL tt.initialise IS BYTE 17 :
VAL tt.out.byte   IS BYTE 18 :
VAL tt.out.int    IS BYTE 19 :
VAL tt.key.raw    IS BYTE 20 :
VAL tt.key.cooked IS BYTE 21 :
VAL tt.release    IS BYTE 22 :
VAL tt.claim      IS BYTE 23 :
VAL tt.endstream  IS BYTE 24 :
--}}}
--{{{  to array
--* Put the data received on given screen stream channel into given
-- array.
--
-- This procedure terminates when st.endstream is read on channel scrn.
--  This terminating st.endstream is stored in buffer as tt.endstream.
--
-- @param scrn for input
-- @param buffer the array to which the data received on scrn is
--   placed; the data is stored in the form of TDS2 tt. tags followed
--   as necessary by further data bytes; if buffer overflows then this
--   procedure acts as an invalid process
PROC ss.scrstream.to.array (CHAN OF SS scrn, []BYTE buffer)
  BOOL going:
  INT tp:
  SEQ
    going := TRUE
    tp := 0
    WHILE going
      SEQ
        scrn ? CASE
          INT32 len:
          st.out.string; len::[buffer FROM tp+5 FOR (SIZE buffer) - (tp+5)]
            SEQ
              buffer [tp] := tt.out.string
              tp := tp + 1
              [4]BYTE lenb32 RETYPES len:
              [buffer FROM tp FOR 4] := lenb32
              tp := tp + ( (INT len) + 4 )  -- extra 4 added 14-June-90 SRH
          BYTE tt.char :
          st.out.byte; tt.char
            SEQ
              buffer[tp] := tt.out.byte
              buffer[tp+1] := tt.char
              tp := tp + 2
          BYTE tt.char :
          st.ins.char; tt.char
            SEQ
              buffer[tp] := tt.ins.char
              buffer[tp+1] := tt.char
              tp := tp + 2
          INT32 x :
          st.out.int; x
            SEQ
              buffer[tp] := tt.out.int
              tp := tp + 1
              [4]BYTE xb32 RETYPES x:
              [buffer FROM tp FOR 4] := xb32
              tp := tp + 4
          INT32 x,y:
          st.goto; x; y
            SEQ
              buffer[tp] := tt.goto
              tp := tp + 1
              [4]BYTE xb32 RETYPES x:
              [buffer FROM tp FOR 4] := xb32
              tp := tp + 4
              [4]BYTE yb32 RETYPES y:
              [buffer FROM tp FOR 4] := yb32
              tp := tp + 4
          st.reset
            SEQ
              buffer[tp] := tt.reset
              tp := tp + 1
          st.up
            SEQ
              buffer[tp] := tt.up
              tp := tp + 1
          st.down
            SEQ
              buffer[tp] := tt.down
              tp := tp + 1
          st.left
            SEQ
              buffer[tp] := tt.left
              tp := tp + 1
          st.right
            SEQ
              buffer[tp] := tt.right
              tp := tp + 1
          st.del.char
            SEQ
              buffer[tp] := tt.del.char
              tp := tp + 1
          st.clear.eol
            SEQ
              buffer[tp] := tt.clear.eol
              tp := tp + 1
          st.clear.eos
            SEQ
              buffer[tp] := tt.clear.eos
              tp := tp + 1
          st.ins.line
            SEQ
              buffer[tp] := tt.ins.line
              tp := tp + 1
          st.del.line
            SEQ
              buffer[tp] := tt.del.line
              tp := tp + 1
          st.beep
            SEQ
              buffer[tp] := tt.beep
              tp := tp + 1
          st.terminate
            SEQ
              buffer[tp] := tt.terminate
              tp := tp + 1
          st.help
            SEQ
              buffer[tp] := tt.help
              tp := tp + 1
          st.initialise
            SEQ
              buffer[tp] := tt.initialise
              tp := tp + 1
          st.key.raw
            SEQ
              buffer[tp] := tt.key.raw
              tp := tp + 1
          st.key.cooked
            SEQ
              buffer[tp] := tt.key.cooked
              tp := tp + 1
          st.release
            SEQ
              buffer[tp] := tt.release
              tp := tp + 1
          st.claim
            SEQ
              buffer[tp] := tt.claim
              tp := tp + 1
          st.endstream
            going := FALSE
          -- if st.spare is read then STOP because something cannot be right
    buffer[tp] := tt.endstream
:
--}}}
--{{{  from array
--* Output the data in the given array on the given screen stream
-- channel.
--
-- This procedure terminates when tt.endstream is found in buffer.
-- This tt.endstream is not sent out ( as st.endstream or anything else
-- ).
--
-- @param scrn for output
-- @param buffer the array from which the data is to be sent on scrn;
--   the data should be in the form of TDS2 tt. tags, followed as
--   necessary by further data bytes.
PROC ss.scrstream.from.array (CHAN OF SS scrn, VAL []BYTE buffer)
  BYTE tt.bcom :
  INT tp:
  SEQ
    tt.bcom := buffer[0]
    tp := 1
    WHILE tt.bcom <> tt.endstream
      SEQ
        CASE tt.bcom
          tt.out.string
            INT maxlen:
            INT32 len:
            SEQ
              [4]BYTE lenb32 RETYPES len:
              lenb32 := [buffer FROM tp FOR 4]
              tp := tp + 4
              maxlen := (SIZE buffer) - tp
              scrn ! st.out.string; len :: [buffer FROM tp FOR maxlen]
              tp := tp + (INT len)
          tt.out.byte
            SEQ
              scrn ! st.out.byte; buffer[tp]
              tp := tp + 1
          tt.ins.char
            SEQ
              scrn ! st.ins.char; buffer[tp]
              tp := tp + 1
          tt.out.int
            INT32 x :
            SEQ
              [4]BYTE bx RETYPES x:
              SEQ
                bx := [buffer FROM tp FOR 4]
                tp := tp + 4
              scrn ! st.out.int; x
          tt.goto
            INT32 x, y:
            SEQ
              [4]BYTE bx RETYPES x:
              SEQ
                bx := [buffer FROM tp FOR 4]
                tp := tp + 4
              [4]BYTE by RETYPES y:
              SEQ
                by := [buffer FROM tp FOR 4]
                tp := tp + 4
              scrn ! st.goto; x; y
          tt.reset
            scrn ! st.reset
          tt.up
            scrn ! st.up
          tt.down
            scrn ! st.down
          tt.left
            scrn ! st.left
          tt.right
            scrn ! st.right
          tt.del.char
            scrn ! st.del.char
          tt.clear.eol
            scrn ! st.clear.eol
          tt.clear.eos
            scrn ! st.clear.eos
          tt.ins.line
            scrn ! st.ins.line
          tt.del.line
            scrn ! st.del.line
          tt.beep
            scrn ! st.beep
          tt.terminate
            scrn ! st.terminate
          tt.help
            scrn ! st.help
          tt.initialise
            scrn ! st.initialise
          tt.key.raw
            scrn ! st.key.raw
          tt.key.cooked
            scrn ! st.key.cooked
          tt.release
            scrn ! st.release
          tt.claim
            scrn ! st.claim
        tt.bcom := buffer[tp]
        tp := tp + 1
        -- BYTE 14 ( tt.spare ) should not occur; hence, if it does, STOP
:
--}}}
