--
--	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.1, 22/05/90"
#COMMENT "(c) Copyright INMOS Limited 1989, 1990"
#INCLUDE "hostio.inc"
#USE "solib.lib"
#USE "convert.lib"

--* Read an integer in either ( two's complement ) hexadecimal form or
-- in decimal 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 INT using modulo arithmetic to give the result.
-- Both upper and lower case hex digits are permissible. A sign is
-- allowed in the first character position if it is a decimal integer
-- that is entered. 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 integer read; otherwise
--   undefined
-- @param error FALSE if everything was in order; TRUE otherwise: 1)
--   if the integer overflows the INT range; 2a) if it is a hex
--   integer, if there is a non-hex character in the string read from
--   the keyboard ( other than #, $, or % in the first character
--   position ) 2b) if it is a decimal integer, if there is a
--   non-numeric character in the string read from the keyboard ( other
--   than a + or - in the first character position )
PROC so.read.echo.any.int (CHAN OF SP fs, ts, INT n, BOOL error)
  --{{{  history
  -- V1.1  TS/0052 Allowed lower case hex characters SRH 22-May-90
  --}}}

  [15]BYTE string :
  INT  len :
  BYTE result :

  SEQ
    so.read.echo.line (fs, ts, len, string, result)
    IF
      (result = spr.ok) AND (len > 0)
        IF
          (string[0] = '#') OR (string[0] = '$') OR (string[0] = '%')
            --{{{  hex
            SEQ
              --{{{  convert any lower case hex digits to upper case SRH 22/05/90
              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
              --}}}
              STRINGTOHEX (error, n, [string FROM 1 FOR len - 1])
              IF
                string[0] = '%'
                  n := n PLUS (MOSTNEG INT)  -- can't overflow even if error
                TRUE
                  SKIP
            --}}}
          TRUE
            STRINGTOINT (error, n, [string FROM 0 FOR len])
      TRUE
        error := TRUE
:
