--
--	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.00, 10/03/89"
#COMMENT "(c) Copyright INMOS Limited 1989"
#COMMENT "KROC hostsp version 1996"
#INCLUDE "hostio.inc"
--{{{  sp.system
--* Execute a command on the host system.
--
-- @param fs from server
-- @param ts to server
-- @param command the command to be executed, which may be null
-- @param status if command is not null and result is spr.ok then this
--   is the return value of the command, which is host dependent
-- @param result equal to spr.ok if a host command processor exists;
--   otherwise: spr.bad.packet.size   command too large, (SIZE command)
--   > sp.max.systemcommand.size >=spr.operation.failed  the operation
--   failed - see hostio.inc or iserver documentation for further
--   details
PROC sp.system (CHAN OF SP fs, ts, VAL []BYTE command,
                INT32 status, BYTE result)
  IF
    (SIZE command) > sp.max.systemcommand.size
      result := spr.bad.packet.size
    TRUE
      [sp.max.packet.data.size]BYTE buffer :
      INT16 temp.INT16 :
      SEQ
        --{{{  pack up the data into the buffer (3 bytes plus data)
        buffer [0] := sp.system.tag
        
        temp.INT16 := INT16 (SIZE command)
        VAL [2]BYTE b.temp RETYPES temp.INT16 :
        [buffer FROM 1 FOR 2] := b.temp
        
        [buffer FROM 3 FOR SIZE command] := command
        --}}}pack up the data into the buffer (3 bytes plus data)
        --{{{  send and receive the buffer
        SEQ
          INT len :
          SEQ
            -- Size of buffer is (SIZE command) + 3.
            -- Add 1, then mask off bottom bit, to ensure it is even
            len := ((SIZE command) + 4) /\ ((-1) << 1)
            IF
              len < sp.min.packet.data.size
                temp.INT16 := INT16 sp.min.packet.data.size
              TRUE
                temp.INT16 := INT16 len
        
          ts ! temp.INT16 :: buffer
          fs ? temp.INT16 :: buffer
        --}}}send and receive the buffer
        --{{{  unpack the data                  (1 or 5 bytes)
        result := buffer [0]
        
        [4]BYTE b.status RETYPES status :
        b.status := [buffer FROM 1 FOR 4]
        --}}}unpack the data                  (1 or 5 bytes)
:
--}}}sp.system
--{{{  so.system
PROC so.system (CHAN OF SP fs, ts, VAL []BYTE command,
                INT32 status, BYTE result)

  sp.system (fs, ts, command, status, result)
:
--}}}so.system
