--
--	interface_out.occ -- output interface to the external environment
--	Copyright (C) 2000 Fred Barnes
--      Separated from CCSP and updated, April 2006 Fred Barnes <frmb@kent.ac.uk>
--
--	This program is free software; you can redistribute it and/or modify
--	it under the terms of the GNU General Public License as published by
--	the Free Software Foundation; either version 2 of the License, or
--	(at your option) any later version.
--
--	This program 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 General Public License for more details.
--
--	You should have received a copy of the GNU General Public License
--	along with this program; if not, write to the Free Software
--	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
--

#OPTION "V"  -- no vectorspace please

#PRAGMA EXTERNAL "PROC C.write.screen (VAL []BYTE buffer) = 0"
#PRAGMA EXTERNAL "PROC C.write.error (VAL BYTE ch) = 0"

VAL kroc.out.buffer.size IS 64:

PROC kroc.screen.process (CHAN OF BYTE in)
  [kroc.out.buffer.size]BYTE out.buffer:
  INT out.len:
  SEQ
    out.len := 0
    WHILE TRUE
      BOOL do.flush:
      SEQ
        in ? out.buffer[out.len]
        -- need to flush buffer ?
        do.flush := FALSE
        CASE out.buffer[out.len]
          #FF
            do.flush := TRUE
          '*n', '*c'
            do.flush, out.len := TRUE, (out.len + 1)
          ELSE
            out.len := (out.len + 1)
        -- buffer full ?
        IF
          out.len = (kroc.out.buffer.size - 2)
            do.flush := TRUE
          TRUE
            SKIP
        -- flush ?
        IF
          do.flush
            SEQ
              out.buffer[out.len] := #00
              #PRAGMA DEFINED out.buffer
              C.write.screen (out.buffer)
              out.len := 0
          TRUE
            SKIP
:

PROC kroc.error.process (CHAN OF BYTE in)
  WHILE TRUE
    BYTE ch:
    SEQ
      in ? ch
      C.write.error (ch)
:

