--
--	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 streamio

#OPTION "E"  -- no compliler libs
#COMMENT "Stream i/o library, V1.1, 13/06/90"
#COMMENT "(c) Copyright INMOS Limited, 1988"
#INCLUDE "hostio.inc"
#INCLUDE "streamio.inc"
#USE "hostio.lib"
--* Write the data sent on the given screen stream to a file.
--
-- This routine terminates on receipt of st.endstream on scrn. What is
-- written to the file is the bytes that follow tags ( for those tags
-- that have them ), with the exception that the st.out.int tag has its
-- following INT32 truncated to a BYTE.
--
-- @param fs from server
-- @param ts to server
-- @param scrn for input
-- @param filename the name of the file to be write to.  The name of
--   the file must fit exactly into filename, i.e. there are (SIZE
--   filename) characters in the name of the file.  A directory
--   specification may form part of filename.  If a file of the same
--   name exists it is overwritten.
-- @param result spr.ok if the data sent on scrn was successfully
--   written to the file filename; otherwise it takes on a value
--   indicating what went wrong: spr.bad.packet.size   filename too
--   large (SIZE filename) > sp.max.openname.size spr.bad.name
--   null file name >=spr.operation.failed  see hostio.inc or iserver
--   documentation for further details
PROC so.scrstream.to.file (CHAN OF SP fs, ts, CHAN OF SS scrn,
                           VAL []BYTE filename, BYTE result)
  --{{{  history
  -- Inserted the tags st.help and st.initialise into the CASE
  -- input, for completeness.  New version is 1.1 ( old version
  -- was 0.01, 23-Dec-88 ); 13-June-90 SRH
  --}}}

  INT32 streamid :
  BOOL  running, opened :
  SEQ
    sp.open (fs, ts, filename, spt.text, spm.output, streamid, result)
    opened  := (result = spr.ok)
    running := TRUE
    WHILE running AND (result = spr.ok)
      SEQ
        scrn ? CASE
          INT32 len:
          [st.max.string.size]BYTE string :
          st.out.string; len::string
            INT len.written :
            SEQ
              sp.write (fs, ts, streamid, [string FROM 0 FOR INT len],
                        len.written, result)
          BYTE ch :
          st.out.byte; ch
            INT len.written :
            sp.write (fs, ts, streamid, [ch], len.written, result)
          BYTE ch:
          st.ins.char; ch
            INT len.written :
            sp.write (fs, ts, streamid, [ch], len.written, result)
          st.beep
            SKIP
          INT32 ch:
          st.out.int; ch
            INT len.written :
            sp.write (fs, ts, streamid, [BYTE ((INT ch) /\ #FF)], len.written, result)
          INT32 x, y :
          st.goto; x; y
            SKIP
          st.reset
            SKIP
          st.up
            SKIP
          st.down
            SKIP
          st.left
            SKIP
          st.right
            SKIP
          st.del.char
            SKIP
          st.clear.eol
            SKIP
          st.clear.eos
            SKIP
          st.ins.line
            SKIP
          st.del.line
            SKIP
          st.terminate
            SKIP
          st.help
            SKIP
          st.initialise
            SKIP
          st.key.raw
            SKIP
          st.key.cooked
            SKIP
          st.release
            SKIP
          st.claim
            SKIP
          st.endstream
            running := FALSE
          -- if st.spare is seen then something is wrong => STOP
    IF
      opened
        sp.close (fs, ts, streamid, result)
      TRUE
        SKIP
:
