--
--	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 hostsp

#COMMENT "Host i/o library, V1.1, 16/08/90"
#COMMENT "(c) Copyright INMOS Limited 1989"
#COMMENT "KROC hostsp version 1996"
#INCLUDE "hostio.inc"
--{{{  sp.read
--* Read a limited number of bytes from a stream.
--
-- @param fs from server
-- @param ts to server
-- @param streamid the identification number of the open stream to be
--   read from
-- @param bytes.read if result is spr.ok then this gives the number of
--   bytes read from the file; these bytes will be [data FROM 0 FOR
--   bytes.read]; if bytes.read <> (SIZE data) then either an error
--   occurred or the end of the file was reached.  If result is not
--   spr.ok then bytes.read will be zero. 0 <= bytes.read <=
--   sp.max.readbuffer.size
-- @param data if result is spr.ok then this contains the bytes read
--   from the file; otherwise it is undefined.  The number of bytes
--   requested to be read is (SIZE data); the limit to this is
--   sp.max.readbuffer.size.
-- @param result equal to spr.bad.packet.size if too many bytes asked
--   for ie (SIZE data) > sp.max.readbuffer.size, otherwise it is equal
--   to spr.ok
PROC sp.read (CHAN OF SP fs, ts, VAL INT32 streamid,
              INT bytes.read, []BYTE data, BYTE result)
  --{{{  history
  -- Removed the IF on result = spr.ok because it always is
  -- spr.ok due to the stupid sp.read.tag action of the server;
  -- V1.1, SRH 16-Aug-90
  --}}}

  IF
    (SIZE data) > sp.max.readbuffer.size
      SEQ
        result     := spr.bad.packet.size
        bytes.read := 0
    TRUE
      [sp.max.packet.data.size]BYTE buffer :
      INT16 temp.INT16 :
      SEQ
        --{{{  pack the data into the buffer (7 bytes)
        buffer [0] := sp.read.tag
        
        VAL [4]BYTE b.streamid RETYPES streamid :
        [buffer FROM 1 FOR 4] := b.streamid
        
        temp.INT16 := INT16 (SIZE data)
        VAL [2]BYTE b.count RETYPES temp.INT16 :
        [buffer FROM 5 FOR 2] := b.count
        --}}}pack the data into the buffer (7 bytes)
        --{{{  send and receive the buffer
        ts ! 8 (INT16)  :: buffer  -- 8 is 7 rounded up to be even
        fs ? temp.INT16 :: buffer
        --}}}
        --{{{  unpack the data               (1, or 3 bytes plus data)
        result := buffer [0]  -- always 0 for sp.read.tag
        
        [2]BYTE b.temp RETYPES temp.INT16 :
        b.temp := [buffer FROM 1 FOR 2]
        bytes.read := INT temp.INT16
        
        [data FROM 0 FOR bytes.read] := [buffer FROM 3 FOR bytes.read]
        --}}}unpack the data               (1, or 3 bytes plus data)
:
--}}}sp.read
