--
--	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, 11/05/90"
#COMMENT "(c) Copyright INMOS Limited 1989"
#COMMENT "KROC hostsp version 1996"
#INCLUDE "hostio.inc"
--{{{  sp.open
--* Open a file.
--
-- No path environment variable is used to locate the file; what is in
-- name is all that is used ( cf. so.popen.read )
--
-- @param fs from server
-- @param ts to server
-- @param name the name of the file to be opened.  The name of the file
--   must fit exactly into name, i.e. there are (SIZE name) characters
--   in the name of the file.  A directory specification may form part
--   of name.
-- @param type either spt.binary, for a file containing raw bytes only
--   or spt.text, for a file stored as text records separated by
--   newlines
-- @param mode one of spm.input            open existing file for
--   reading spm.output           open new file, or truncate existing
--   one, for writing spm.append           open new file, or append to
--   existing one, for writing spm.existing.update  open existing file
--   for update ( reading and writing ), starting at the beginning of
--   the file spm.new.update       open new file, or truncate existing
--   one, for update spm.append.update    open new file, or append to
--   existing one, for update When a file is opened for update then the
--   resulting stream may be used for input and output.
-- @param streamid if result is spr.ok, the identification number of
--   the stream associated with the file that was opened; undefined
--   otherwise
-- @param result spr.ok if the file was successfully opened; otherwise
--   it takes on a value indicating what went wrong:
--   spr.bad.packet.size   name too large: (SIZE name) >
--   sp.max.openname.size spr.bad.name          null file name
--   spr.bad.type          invalid type spr.bad.mode          invalid
--   mode >=spr.operation.failed  the open failed - see hostio.inc or
--   iserver documentation for further details
PROC sp.open (CHAN OF SP fs, ts, VAL []BYTE name, VAL BYTE type, mode,
              INT32 streamid, BYTE result)
  IF
    --{{{  simple error cases
    (SIZE name) > sp.max.openname.size
      result := spr.bad.packet.size
    (SIZE name) = 0
      result := spr.bad.name
    (type <> spt.text) AND (type <> spt.binary)
      result := spr.bad.type
    (mode <> spm.input) AND (mode <> spm.output) AND (mode <> spm.append) AND
    (mode <> spm.existing.update) AND (mode <> spm.new.update) AND
    (mode <> spm.append.update)
      result := spr.bad.mode
    --}}}
    TRUE
      [sp.max.packet.data.size]BYTE buffer :
      INT16 temp.INT16 :
      SEQ
        --{{{  pack up the data into the buffer (5 bytes plus data)
        buffer [0] := sp.open.tag
        
        temp.INT16 := INT16 (SIZE name)
        VAL [2]BYTE b.temp RETYPES temp.INT16 :
        [buffer FROM 1 FOR 2] := b.temp
        
        [buffer FROM 3 FOR SIZE name] := name
        buffer [(SIZE name) + 3] := type
        buffer [(SIZE name) + 4] := mode
        --}}}pack up the data into the buffer (5 bytes plus data)
        --{{{  send and receive the buffer
        SEQ
          INT len :
          SEQ
            -- Size of buffer is (SIZE name) + 5.
            -- Add 1, then mask off bottom bit, to ensure it is even
            len := ((SIZE name) + 6) /\ ((-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.temp RETYPES streamid :
        b.temp := [buffer FROM 1 FOR 4]
        --}}}unpack the data                  (1 or 5 bytes)
:
--}}}sp.open
--{{{  so.open
PROC so.open (CHAN OF SP fs, ts, VAL []BYTE name, VAL BYTE type, mode,
              INT32 streamid, BYTE result)

  sp.open (fs, ts, name, type, mode, streamid, result)
:
--}}}so.open
