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

#COMMENT "Host i/o library, V1.0, 25/05/90"
#COMMENT "(c) Copyright INMOS Limited 1990"
#INCLUDE "hostio.inc"
#USE "solib.lib"
#USE "convert.lib"

--* Read a 32-bit integer in ( two's complement ) hexadecimal form from
-- the keyboard, and to echo it to standard out.
--
-- The hexadecimal number must begin with one of '#', '$' or '%'; a
-- sign is not allowed ( nor necessary since the most significant bit
-- indicates the sign ). A '#' and '$' mean exactly the same thing: the
-- following digits form a hex integer. A '%' means add the given hex
-- integer to MOSTNEG INT32 using modulo arithmetic to give the result.
-- Both upper and lower case hex digits are permissible. The number
-- typed at the keyboard must be terminated by pressing 'RETURN'.
--
-- @param fs from server
-- @param ts to server
-- @param n if error is FALSE, the value of the hexadecimal number
--   read; otherwise undefined
-- @param error FALSE if everything was in order; TRUE otherwise: 1) if
--   the integer overflows the INT32 range; 2) if there is a non-hex
--   character in the string read from the keyboard ( other than #, $,
--   or % in the first character position )
PROC so.read.echo.hex.int32 (CHAN OF SP fs, ts, INT32 n, BOOL error)
  [9]BYTE string : -- 9 = 8 + 1, the 1 being for the #, $ or %
  INT len :
  BYTE result :

  SEQ
    so.read.echo.line (fs, ts, len, string, result)
    IF
      (result = spr.ok) AND (len > 0) AND
      ((string[0] = '#') OR (string[0] = '$') OR (string[0] = '%'))
        SEQ
          --{{{  convert any lower case hex digits to upper case
          SEQ i = 1 FOR ( len - 1 ) -- assumes len > 0
            IF  -- assumes ASCII
              ( 'a' <= string[i] ) AND ( string[i] <= 'f' )
                string[i] := BYTE( (INT string[i]) -
                                   ( (INT 'a') - (INT 'A') ) )
              TRUE
                SKIP
          --}}}
          STRINGTOHEX32 (error, n, [string FROM 1 FOR len - 1])
          --{{{  deal with % if necessary
          IF
            string[0] = '%'
              n := n PLUS (MOSTNEG INT32)  -- can't overflow even if error
            TRUE  -- ( string[0] = '#' ) OR ( string[0] = '$' )
              SKIP
          --}}}
      TRUE
        error := TRUE
:
