--
--	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, 11/05/90"
#COMMENT "(c) Copyright INMOS Limited 1989"
#COMMENT "KROC simplified version 1995"

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

--{{{  OC.fopen
VAL unix.max.openname.size IS (sp.max.openname.size/4) + 1:
#PRAGMA EXTERNAL "PROC C.fopen (INT32 handle, VAL[unix.max.openname.size]INT name, VAL[1]INT mode)  = 0 "
-- #PRAGMA EXTERNAL "PROC bytes.to.chars (VAL []BYTE from, []INT to) = 4"
PROC OC.fopen (INT32 handle, VAL[]BYTE name, VAL[2]BYTE mode)
  [unix.max.openname.size]INT unixname:
  [1]INT unixmode:
  SEQ
    bytes.to.chars (name, unixname)
    bytes.to.chars (mode, unixmode)
    C.fopen (handle, unixname, unixmode)
:
--}}}  
--{{{  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 fname 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 fname, VAL BYTE type, mode,
              INT32 streamid, BYTE result)
  IF
    --{{{  simple error cases
    (SIZE fname) > sp.max.openname.size
      result := spr.bad.packet.size
    (SIZE fname) = 0
      result := spr.bad.name
    (type <> spt.text) AND (type <> spt.binary)
      result := spr.bad.type
    (mode < spm.input) OR (mode > spm.append.update)
      result := spr.bad.mode
    --}}}  
    TRUE
      SEQ
        --{{{  Call Unix
        VAL modeUnix IS [["r ", "w ", "a ", "r+", "w+", "a+"],
                         ["r ", "w ", "a ", "r+", "w+", "a+"]]:
        SEQ
          OC.fopen (streamid, fname, modeUnix [(INT type)-1][(INT mode)-1])
          IF
            streamid <> Unix.NULL
              result := spr.ok
            TRUE
              result := spr.operation.failed
        --}}}  
:
--}}}  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
