[INHERIT ('SYS$LIBRARY:STARLET.PEN')]
MODULE Hex_Utilities_2(OUTPUT);

{ Module containing more various utility routines for the HEX program. }

(***************** Declare External Variables and Constants *******************)

%INCLUDE 'HEX$DIRECTORY:HEXGLOB.INC/NOLIST'

(********************* External RTL Routine Declarations **********************)

%INCLUDE 'HEX$DIRECTORY:LIB.INC/NOLIST'

(******************* External Homemade Routine Declarations *******************)

[EXTERNAL] FUNCTION Hex_to_Dec(Hex_String: String_80): UNSIGNED; EXTERN;

(******************************************************************************)
(******************************************************************************)
(******************************************************************************)

[GLOBAL] FUNCTION Set_With: INTEGER;

{ This procedure sets the With_Param variable for various commands. }

TYPE
	String_Body		= PACKED ARRAY [1..80] OF CHAR;

VAR
	Token_Address		: ^String_Body;
	Hex_String		: String_80;
	Addr			: UNSIGNED;
	Ch			: CHAR;
	String			: String_80;

BEGIN

  CASE (Tparse_Block.TPA$L_PARAM::INTEGER) OF

    1:	BEGIN	{ hex number }
  	  Token_Address::UNSIGNED := Tparse_Block.TPA$L_TOKENPTR;
	  Hex_String := SUBSTR(Token_Address^,1,Tparse_Block.TPA$L_TOKENCNT);
	  Addr := Hex_to_Dec(Hex_String);
	  IF (Addr <= 255) THEN
	    BEGIN
	      Ch := CHR(Addr);
	      With_Param := ORD(Ch);
	      Set_With := SS$_NORMAL;
	    END
	  ELSE
	    BEGIN
	      LIB$SIGNAL(hex_valrngerr,1,%STDESCR SUBSTR(Hex_String,1,Hex_String.LENGTH));
	      Set_With := Error_Signalled;
	    END;
	END;

    2:	BEGIN	{ ^# }
	  With_Param := 127;
	  Set_With := SS$_NORMAL;
	END;

    3:  BEGIN	{ ^c }
	  IF ((Tparse_Block.TPA$B_CHAR < 64) OR (Tparse_Block.TPA$B_CHAR > 95)) THEN
	    BEGIN
	      String := '^' + CHR(Tparse_Block.TPA$B_CHAR);
	      LIB$SIGNAL(hex_illegcontcode,1,%STDESCR SUBSTR(String,1,2));
	      Set_With := Error_Signalled;
	    END
	  ELSE
	    BEGIN
	      With_Param := Tparse_Block.TPA$B_CHAR - 64;
	      Set_With := SS$_NORMAL;
	    END;
	END;

    4:  BEGIN	{ 'c }
	  Tparse_Block.TPA$V_BLANKS := FALSE;
	  IF ((Tparse_Block.TPA$B_CHAR < 32) OR (Tparse_Block.TPA$B_CHAR > 126)) THEN
	    BEGIN
	      String := '''' + CHR(Tparse_Block.TPA$B_CHAR);
	      LIB$SIGNAL(hex_illegprntchr,1,%STDESCR SUBSTR(String,1,2));
	      Set_With := Error_Signalled;
	    END
	  ELSE
	    BEGIN
	      With_Param := Tparse_Block.TPA$B_CHAR;
	      Set_With := SS$_NORMAL;
	    END;
	END;

    5:	BEGIN	{ ~# }
	  With_Param := 255;
	  Set_With := SS$_NORMAL;
	END;

    6:  BEGIN	{ ~c }
	  IF ((Tparse_Block.TPA$B_CHAR < 64) OR (Tparse_Block.TPA$B_CHAR > 95)) THEN
	    BEGIN
	      String := '^' + CHR(Tparse_Block.TPA$B_CHAR);
	      LIB$SIGNAL(hex_illegcontcode,1,%STDESCR SUBSTR(String,1,2));
	      Set_With := Error_Signalled;
	    END
	  ELSE
	    BEGIN
	      With_Param := Tparse_Block.TPA$B_CHAR + 64;
	      Set_With := SS$_NORMAL;
	    END;
	END;

    7:  BEGIN	{ -c }
	  Tparse_Block.TPA$V_BLANKS := FALSE;
	  IF ((Tparse_Block.TPA$B_CHAR < 32) OR (Tparse_Block.TPA$B_CHAR > 126)) THEN
	    BEGIN
	      String := '''' + CHR(Tparse_Block.TPA$B_CHAR);
	      LIB$SIGNAL(hex_illegprntchr,1,%STDESCR SUBSTR(String,1,2));
	      Set_With := Error_Signalled;
	    END
	  ELSE
	    BEGIN
	      With_Param := Tparse_Block.TPA$B_CHAR + 128;
	      Set_With := SS$_NORMAL;
	    END;
	END;

  END; { case statement }

END;

(******************************************************************************)

END. { module set_with }
