Author Topic: NetRexx  (Read 568 times)

Jan-Erik Lärka

  • Global Moderator
  • Full Member
  • *****
  • Thank You
  • -Given: 5
  • -Receive: 11
  • Posts: 151
    • View Profile
NetRexx
« on: September 03, 2016, 06:55:20 pm »
I've tried to use NetRexx as it offer you to write
code in (some-kind-of-) Rexx that can be compiled
into JAVA byte code. It's apparently even possible
to use it to write apps for android...

I just had to rewrite the included script to better suit how
OpenJDK 6 in OS/2 work. Please do help me to verify
that the script behave well and handle various scenarios
properly.

You can even drop a folder and it should try to compile all
.nrx-files within (if the resulting command line length
doesn't exceed 1024 characters).

Place "NetRexxKing.png" and "NetRexxKing.ico" in the
NetRexx  folder. Place the script NRxC.cmd in the NetRexx \bin
- folder before you run it and let it create a program object
in the development folder or on the desktop.

Code: [Select]
/* Translate and compile a NetRexx program             */
/*                                                     */
/* use as:  NRxC hello [file2]...                      */
/*                                                     */
/*   which will use the NetRexx translator to          */
/*   translate hello.nrx to hello.java                 */
/*   then will use javac to compile hello.java         */
/*                                                     */
/* OPTIONS keywords may be added (with a -) before or  */
/* after the file specification, along with the extra  */
/* flags known to NetRexxC (such as -keep).   For      */
/* example:                                            */
/*                                                     */
/*    NRxC -keep -format -comments hello               */
/*                                                     */
/* Invoke with no parameters for a full list of flags. */
/*                                                     */
/* To run the class after compilation, specify -run as */
/* an option word.  Each file will be run in turn.     */
/*                                                     */
/*    NRxC -run hello                                  */
/*                                                     */
/* Multiple programs may be specified; in this case    */
/* they are all run (if requested) after all compiles. */
/*                                                     */
/* ----------                                          */
/* 1996.09.02 -- handle Warnings from NetRexxC (rc=1)  */
/* 1996.12.14 -- use COM.ibm.netrexx.process           */
/* 1998.05.25 -- pass NETREXX_JAVA setting to java.exe */
/* 2011.09.01 -- use org.netrexx.process               */
/* 2011.09.01 -- remove -xms4M                         */
/* 2016.08.30 -- Complete rewrite                      */

PARSE UPPER ARG uargs
PARSE ARG args

netrx.run = ( WORDPOS( '-RUN', uargs ) > 0 )
netrx.noc = ( WORDPOS( '-NOCOMPILE', uargs ) > 0 )
IF netrx.run THEN
DO
   PARSE VALUE uargs WITH pre'-RUN'post
   args = LEFT( args, LENGTH( pre ) )RIGHT( args, LENGTH( post ) )
END

PARSE SOURCE system . this
old_dir = DIRECTORY()

SELECT
   WHEN system = 'OS/2' THEN
   DO
      '@ECHO OFF'
      env = 'OS2ENVIRONMENT'

      /* Load RexxUtil Library */
      IF RxFuncQuery('SysLoadFuncs') THEN
      DO
         CALL RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
         CALL SysLoadFuncs
      END
     
      CALL SysGetEA this, 'JAVA_PATH', 'java_path'
      IF LENGTH( java_path ) < 2 THEN DO
         PARSE UPPER VALUE rxFind( 'JAVA.EXE', ' -version', 'VERSION "1.' ) WITH java_path'\BIN'
         IF LENGTH( java_path ) = 0 THEN
         DO
            CALL LINEOUT 'STDERR', 'JAVA engine not found'
            RETURN 1
         END
         CALL SysPutEA this, 'JAVA_PATH', java_path
      END
     
      CALL SysGetEA this, 'NETRX_PATH', 'netrx_path'
      IF LENGTH( netrx_path ) < 2 THEN DO
         PARSE UPPER VALUE rxFind( 'NetRexxC.jar' ) WITH netrx_path'\LIB'
         IF LENGTH( netrx_path ) = 0 THEN
         DO
            CALL LINEOUT 'STDERR', 'NetRexx not found'
            RETURN 2
         END
         CALL SysPutEA this, 'NETRX_PATH', netrx_path
      END
      objID = 'NRXC'
      netrx_name = 'NetRexxKing'
      IF wpsObjectExist( '<WP_'objID'>' ) = 0 THEN
      DO
         setupstring = 'OBJECTID=<WP_'objID'>;EXENAME='this';PARAMETERS=[Type "-run" (without quotes) to run compiled script] %**P\%**F;ASSOCFILTER=*.nrx;ICONFILE='netrx_path'\'netrx_name'.ico;'
         IF SysCreateObject( 'WPProgram', 'NetRexx Compiler / Run', '<WP_DEVFLDR>', setupstring, 'U' ) <> 1 THEN
            CALL SysCreateObject 'WPProgram', 'NetRexx Compiler / Run', '<WP_DESKTOP>', setupstring, 'U'
         CALL rxAddBigIcon netrx_path'\'netrx_name'.png', '<WP_'objID'>'
      END
      IF netrx.noc THEN
         CALL VALUE 'PATH', VALUE( 'PATH',, env )'.;'java_path'\bin;'java_path'\lib;'netrx_path'\runlib', env
      ELSE
         CALL VALUE 'PATH', VALUE( 'PATH',, env )'.;'java_path'\bin;'java_path'\lib;'netrx_path'\lib', env

      DO WHILE QUEUED() > 0
         PARSE PULL
      END
     
      /* Add option -norestart for OS/2s 1.0.2+ java.exe, for better display */
      old_trace = TRACE( 'O' )
      '@JAVA -version 2>&1|RXQUEUE'
      CALL TRACE old_trace
      PARSE PULL '1.' JVer ' '
      PARSE VALUE JVer WITH JVer'_'.
      javaopts = ''
      IF JVer >= 6.0 THEN
         CALL VALUE 'CLASSPATH', netrx_path'\lib\NetRexxF.jar;'netrx_path'\lib\NetRexxC.jar;', env
      ELSE IF JVer >= 0.2 THEN javaopts = '-norestart'
      ELSE javaopts = '-noasyncgc'
     
      /* Add any options from NETREXX_JAVA environment variable */
      javaopts = javaopts VALUE( 'NETREXX_JAVA',, env )
   END
   OTHERWISE
   /* Add any options from NETREXX_JAVA environment variable */
   javaopts = VALUE( 'NETREXX_JAVA',, 'ENVIRONMENT' )
END

CALL rxARGs

'@JAVA -version'
IF netrx.noc THEN
   '@JAVA ' javaopts '-jar 'netrx_path'\lib\NetRexxF.jar -exec' args
ELSE
DO
   CALL LINEOUT 'STDERR', 'Compiling...'
   '@JAVA ' javaopts 'org.netrexx.process.NetRexxC' STRIP( iargs filec )
END   

IF ( rc <= 1 & netrx.run ) | ( netrx.noc & rc <> 0 ) THEN
DO
   CALL rxARGs
   DO WHILE LENGTH( files ) > 0
      PARSE VALUE files WITH f_name'.class' files
      CALL LINEOUT 'STDERR', 'Running' f_name '...'
      '@JAVA 'f_name
      IF rc <> 0 THEN PARSE PULL
   END
END
CALL DIRECTORY old_dir
RETURN rc

rxARGs:
   inargs = args
   iargs = ''
   files = ''
   filec = ''
   DO FOREVER
      PARSE VAR inargs file inargs
      IF file = '' THEN LEAVE
      IF LEFT( file, 1 ) = '-' THEN
      DO
         iargs = iargs file
         ITERATE
      END
      dir = DIRECTORY( file )
      IF POS( '*', file ) > 0 | STREAM( file, 'C', 'QUERY EXISTS' ) <> '' | dir <> ''  THEN
      DO
         IF dir <> '' THEN
            file = file'\*.nrx'
         CALL SysFileTree file, 'filename.', 'FO'
         DO i = 1 TO filename.0
            filec = filec filename.i
            PARSE VALUE REVERSE( filename.i ) WITH ext'.'fname'\'path
            fname = REVERSE( fname )
            IF STREAM( fname'.class','C', 'QUERY EXISTS' ) <> '' THEN
            DO
               files = STRIP( files fname'.class' )
               CALL DIRECTORY path
            END
         END
      END
   END
RETURN

rxChk: PROCEDURE
   DO WHILE QUEUED() > 0; PARSE PULL; END;
   '@'||ARG(1)||ARG(2)||' 2>>&1 | RXQUEUE'
   DO WHILE QUEUED() > 0
      IF POS( TRANSLATE( ARG(3) ), TRANSLATE( LINEIN( 'QUEUE:' ) ) ) > 0 THEN
         IF LENGTH( ARG(4) ) > 0 THEN RETURN ARG(4)
   END
RETURN ''

rxFind: PROCEDURE EXPOSE !_msg_!.
   PARSE VALUE REVERSE( SysSearchPath( 'PATH', ARG(1) ) ) WITH .'\'fpath
   fpath = REVERSE( fpath )
   IF ARG() > 1 & LENGTH( fpath ) > 0 THEN
      fspec = rxChk( fpath'\'ARG(1), ARG(2), ARG(3), fpath )
   ELSE IF STREAM( fpath'\'ARG(1), 'C', 'QUERY EXISTS' ) <> '' THEN
      fspec = fpath
   ELSE fspec = ''
   IF LENGTH( fspec ) > 0 THEN RETURN fspec
   CALL LINEOUT 'STDERR', VALUE( 'PROGRAMS',,'OS2ENVIRONMENT' )
   CALL SysFileTree VALUE( 'PROGRAMS',,'OS2ENVIRONMENT' )||'\*'||ARG(1), 'file.', 'SFO'
   DO i = 1 TO file.0
      PARSE VALUE REVERSE( file.i ) WITH .'\'fspec
      IF ARG() > 1 THEN
         fspec = rxChk( file.i, ARG(2), ARG(3), REVERSE( fspec ) )
      ELSE
         fspec = REVERSE( fspec )
      IF LENGTH( fspec ) > 0 THEN RETURN fspec
   END
   drives = SysDriveMap( 'C:', 'USED' )
   DO j = 1 TO WORDS( drives )
      CALL LINEOUT 'STDERR', SUBWORD( drives, j, 1 )
      CALL SysFileTree SUBWORD( drives, j, 1 )||'\*'||ARG(1), 'file.', 'SFO'
      DO i = 1 TO file.0
         PARSE VALUE REVERSE( file.i ) WITH .'\'fspec
         IF ARG() > 1 THEN
            fspec = rxChk( file.i, ARG(2), ARG(3), REVERSE( fspec ) )
         ELSE
            fspec = REVERSE( fspec )
         IF LENGTH( fspec ) > 0 THEN RETURN fspec
      END
   END
RETURN ''
   
rxAddBigIcon: PROCEDURE EXPOSE !_msg_!. java_path
    crlf = D2C(13)D2C(10)
    bigicons_dir = VALUE( 'BIGICONS',, 'OS2ENVIRONMENT' )
    bigicon = FILESPEC( 'N', ARG(1) )
    objid = ARG(2)
    IF LENGTH( bigicons_dir ) > 0 THEN
    DO
        IF STREAM( ( bigicons_dir'\'bigicon ), 'C','QUERY EXISTS') = '' THEN
           IF SysCopyObject( ARG(1), bigicons_dir ) = 0 THEN
              RETURN 1
        IF STREAM( bigicons_dir'\bigicons.txt', 'C', 'QUERY EXIST' ) <> '' THEN
        DO
            f_size = STREAM( bigicons_dir'\bigicons.txt', 'C', 'QUERY SIZE' )
            IF f_size > 0 THEN
            DO
                input = CHARIN( bigicons_dir'\bigicons.txt', 1, f_size )
                CALL STREAM bigicons_dir'\bigicons.txt', 'C', 'CLOSE'
                PARSE UPPER VALUE input WITH pre'"'(bigicon)'"'post
                IF LENGTH( post ) > 0 THEN RETURN 2
                PARSE UPPER VALUE input WITH pre'"'(objid)'"'post
                IF LENGTH( post ) > 0 THEN RETURN 3
                PARSE VALUE input WITH pre'[OBJECTID]'mid(crlf)(crlf)'['post
                input = pre'[OBJECTID]'STRIP( STRIP( mid, 'T', D2C(10) ), 'T', D2C(13) )||crlf||LEFT( '"'objid'"', 28 )'"'bigicon'"'crlf||crlf'['post
                CALL SysFileDelete bigicons_dir'\bigicons.txt'
                CALL CHAROUT bigicons_dir'\bigicons.txt', input
                CALL STREAM bigicons_dir'\bigicons.txt', 'C', 'CLOSE'
            END
            ELSE RETURN 4
        END
    END
RETURN 0

/* Code borrowed from REXX Tips & Tricks v3.60 */
wpsObjectExist: PROCEDURE /* parameter: '<new_wps_object_id>', return: 1 exist, 0 doesn't exist, 43 error */
  PARSE ARG objID
  rc = 43               /* init return/error code 43 = routine not found */
  SIGNAL ON SYNTAX NAME ObjectExistError

  IF SUBSTR( objID, 2, 1 ) <> ":" THEN
      IF LEFT( objID, 1 ) <> "<" & RIGHT( objID, 1 ) <> ">" THEN
          objID = "<" || objID || ">"

  /* create the object and see if it fail */
  tempRC = SysCreateObject( "WPFolder", "TestObject", "<WP_NOWHERE>", "OBJECTID=" || objID || ";", "FAIL" )

  IF tempRC = 1 THEN
  DO
    CALL SysDestroyObject objID
    rc = 0
  END
  ELSE
    rc = 1

ObjectExistError:

RETURN rc
« Last Edit: September 03, 2016, 07:12:02 pm by Jan-Erik Lärka »