--
--	Inmos Libraries
--	Copyright (C) 1988, 1989, 1990, 1991, 1993  Inmos Limited
--      Modifications Copyright (C) 2004  Fred Barnes
--
--	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 hostio

#COMMENT "Host i/o library, V1.1, 20/11/90"
#COMMENT "(c) Copyright INMOS Limited 1989"
#COMMENT "KROC simplified version 1995"

#INCLUDE "hostio.inc"
#INCLUDE "unixerrs.inc"
#USE "conv"

--{{{  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
--}}}  
--{{{  OC.fgets
VAL unix.max.readbuffer.size IS (sp.max.readbuffer.size/4) + 1:
#PRAGMA EXTERNAL "PROC C.fgets (INT result, VAL INT32 handle, [unix.max.readbuffer.size]INT buffer, VAL INT bytes.to.read, INT bytes.read)  = 0 "
PROC OC.fgets (INT result, VAL INT32 handle, []BYTE buffer, INT bytes.read)
  [unix.max.readbuffer.size]INT unixbuffer:
  SEQ
    C.fgets (result, handle, unixbuffer, SIZE buffer, bytes.read)
    IF
      (result = 0) AND (bytes.read > 0)
        chars.to.bytes (unixbuffer, [buffer FOR bytes.read])
      TRUE
        SKIP
:
--}}}  
--{{{  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)
  INT res:
  SEQ
    --{{{  Call Unix
    OC.fgets (res, streamid, data, bytes.read)
    IF
      res = 0
        result := spr.ok
      res = Unix.ACCESS.DENIED
        result := spr.bad.stream.use
      res = Unix.INVALID.HANDLE
        result := spr.invalid.streamid
      res = Unix.BUFFER.OVERFLOW
        result := spr.buffer.overflow
      TRUE
        result := spr.operation.failed
    --}}}  
:
--}}}  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
