--
--	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, 20/11/90"
#COMMENT "(c) Copyright INMOS Limited 1989"
#COMMENT "KROC hostsp version 1996"
#INCLUDE "hostio.inc"
--{{{  history
-- V1.00, 10-Mar-89
-- In sp.gets, replaced use of temp.INT16 in channel input statement by dummy; 
-- V1.1, 20-Nov-90 SRH
--}}}
--{{{  sp.gets
--* Read a line from the specified input stream.
--
-- Characters are read until a newline sequence is found, the end of
-- the file is reached, or the number of characters is not less than
-- sp.max.readbuffer.size. If a newline sequence is found, it is not
-- included in data.
--
-- @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 this is the number of bytes
--   read; if result is spr.buffer.overflow then this will be (SIZE
--   data) and will be less than the actual number of bytes read from
--   the file; otherwise zero.
-- @param data an array of bytes holding line read if result is spr.ok;
--   or holding a truncation of the line if result is
--   spr.buffer.overflow.  It is undefined otherwise.  If result is
--   spr.ok or spr.buffer.overflow then the bytes will be [data FROM 0
--   FOR bytes.read].
-- @param result equal to spr.ok if line was successfully read;
--   otherwise takes on a value indicating what went wrong:
--   spr.bad.packet.size   data is too large, (SIZE data) >
--   sp.max.readbuffer.size spr.buffer.overflow   data not large enough
--   to hold the line, in which case the line is truncated to fit
--   >=spr.operation.failed  the read failed, either because the end of
--   the file has been reached or some error occurred - see hostio.inc
--   or iserver documentation for further details
PROC sp.gets (CHAN OF SP fs, ts, VAL INT32 streamid,
              INT bytes.read, []BYTE data, BYTE result)
  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.gets.tag
        
        VAL [4]BYTE b.streamid RETYPES streamid :
        [buffer FROM 1 FOR 4] := b.streamid
        
        temp.INT16 := INT16 sp.max.readbuffer.size
        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
        INT16 dummy :
        fs ? dummy :: buffer
        --}}}
        --{{{  unpack the data               (1, or 3 bytes plus data)
        result := buffer [0]
        
        IF
          result = spr.ok
            SEQ
              [2]BYTE b.temp RETYPES temp.INT16 :
              b.temp := [buffer FROM 1 FOR 2]
              bytes.read := INT temp.INT16
              IF
                bytes.read > (SIZE data)
                  SEQ
                    bytes.read := SIZE data
                    result     := spr.buffer.overflow
                TRUE
                  SKIP
              [data FROM 0 FOR bytes.read] := [buffer FROM 3 FOR bytes.read]
          TRUE
            bytes.read := 0
        --}}}unpack the data               (1, or 3 bytes plus data)
:
--}}}sp.gets
--{{{  so.gets
PROC so.gets (CHAN OF SP fs, ts, VAL INT32 streamid,
              INT bytes.read, []BYTE data, BYTE result)

  sp.gets (fs, ts, streamid, bytes.read, data, result)
:
--}}}so.gets
