/******************************************************************************/ /* */ /* RVM.RexxUtil.cls - The REXXUTIL package of the RVM */ /* =============================================== */ /* */ /* ooRexx's REXXUTIL (RexxUtilCommon.cpp, SysRexxUtil.cpp) is a package of */ /* native routines that is always loaded (PackageManager's */ /* loadInternalPackage): its routines are found as external routines are, a */ /* step after the ::ROUTINEs of the program and those it requires, and a */ /* step before the programs in files (SystemInterpreter's */ /* invokeExternalFunction: callNativeRoutine). A call to one is a native */ /* routine's: its line in a traceback is 'Compiled routine "NAME"'; its */ /* arguments are checked as a native routine's (88.901, 88.922). */ /* */ /* RexxUtilRoutines is the table (the routines the RVM has: name, the */ /* arguments it requires and takes, its key; in RVM.BIFs.cls, for */ /* RXFUNCQUERY); RexxUtilNative runs them. A routine that must wait */ /* (SysSleep) gives an RVM.Wait: the machine waits, */ /* and then the routine's result is the activity's waitResult. */ /* */ /* This file is part of the RVM, the Rexx Virtual Machine */ /* [See https://rexx.epbcn.com/rexx-parser/] */ /* */ /* This file is derived from the ooRexx interpreter: the routines of */ /* REXXUTIL, SysStemSort among them (RexxUtilCommon.cpp, StemClass.cpp). It */ /* is therefore distributed under the Common Public License v1.0, not under */ /* the Apache License 2.0 that covers the rest of the RVM (see NOTICE). */ /* */ /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */ /* Copyright (c) 2005-2026 Rexx Language Association. All rights reserved. */ /* Copyright (c) 2026 Josep Maria Blasco */ /* */ /* This program and the accompanying materials are made available under the */ /* terms of the Common Public License v1.0 which accompanies this */ /* distribution. A copy is also available at the following address: */ /* https://www.oorexx.org/license.html */ /* */ /******************************************************************************/ -- The table of the routines (RexxUtilRoutines, RexxUtilRoutine) is in -- RVM.BIFs.cls, which RXFUNCQUERY needs it in. -- The arguments of a native routine (ooRexx's RexxNativeActivation): -- too many, 88.922; one required that is missing, 88.901. ::Routine CheckRoutineArgs Public Use Strict Arg r, args If r~hasIndex( 6 ) Then Return -- a signature: see NativeArgs n = args~last If n == .Nil Then n = 0 If n > r[3] Then Raise Syntax 88.922 Array( r[3] ) Do i = 1 To r[2] If \ args~hasIndex( i ) Then Raise Syntax 88.901 Array( i ) End -- Run routine 'r' (an entry of RexxUtilRoutines) with 'args'. ::Routine RexxUtilNative Public Use Strict Arg r, args, context world = context~world If r~hasIndex( 6 ) Then args = NativeArgs( r, args, context ) context~nativeArgs = .False -- (see RoutineArgs in RVM.Machine) Select Case r[4] -- SysSleep(delay): seconds, 0 to 2147483 (88.902, 88.907); 0. The -- activity waits (a timer: the clock of the run), and others run. When "SLEEP" Then Do -- (A RexxStringObject: NativeArgs gives its string.) secs = args[1] If \ DataType( secs, "N" ) Then Raise Syntax 88.902 Array( "delay", secs ) Numeric Digits 18 If secs < 0 | secs > 2147483 Then Raise Syntax 88.907 Array( "delay", "0", "2147483", secs ) ms = Trunc( secs * 1000000 ) / 1000 If ms == 0 Then Return "0" act = context~activity sem = .RVM.EventSem~new -- never posted: its timer ends it sem~sleep = .True -- (or Ctrl-C) r = sem~wait( act, ms, world~scheduler ) Return .RVM.Wait~new( sem ) End -- The files (RexxUtilCommon.cpp, SysRexxUtil.cpp for Unix): the name -- is qualified (RoutineQualifiedName); the host is ooRexx, whose -- REXXUTIL does the same on the same file system. When "FILEEXISTS" Then Return SysFileExists( .RVM.FileSystem~qualify( str( args[1] ) ) ) When "ISFILE" Then Return SysIsFile( .RVM.FileSystem~qualify( str( args[1] ) ) ) When "ISDIR" Then Return SysIsFileDirectory( .RVM.FileSystem~qualify( str( args[1] ) ) ) When "ISLINK" Then Return SysIsFileLink( .RVM.FileSystem~qualify( str( args[1] ) ) ) When "FILEDELETE" Then Return SysFileDelete( .RVM.FileSystem~qualify( str( args[1] ) ) ) When "RMDIR" Then Return SysRmDir( .RVM.FileSystem~qualify( str( args[1] ) ) ) When "MKDIR" Then Do name = .RVM.FileSystem~qualify( str( args[1] ) ) If \ args~hasIndex( 2 ) Then Return SysMkDir( name ) mode = str( args[2] ) Numeric Digits 20 ok = DataType( mode, "W" ) If ok Then ok = ( mode >= -2147483648 & mode <= 2147483647 ) If \ ok Then Raise Syntax 88.907 Array( 2, "-2147483648", "2147483647", mode ) Return SysMkDir( name, mode / 1 ) End -- The system (SysRexxUtil.cpp): the host's, the same uname. When "VERSION" Then Return SysVersion() When "LINVER" Then Return SysLinVer() When "WINVER" Then Return SysWinVer() -- The stems (RexxUtilCommon.cpp; StemClass::sort). When "STEMSORT" Then Return StemSort( args, context ) When "STEMDELETE" Then Return StemDelete( args ) When "STEMINSERT" Then Return StemInsert( args ) When "STEMCOPY" Then Return StemCopy( args ) -- SysUtilVersion: the version of ooRexx (the RVM's, the same: 5.3.0). When "UTILVERSION" Then Return "5.3.0" When "LOADFUNCS", "DROPFUNCS" Then Return 0 -- SysCls(): the host's, which runs "clear" (system(): its wait status), -- after what has been written so far. When "CLS" Then Do Call FlushStandard Return SysCls() End -- SysSetPriority([class], priority): the host's (Unix: setpriority of the -- process, nice -priority; the class is ignored): 0, or the errno. When "SETPRIORITY" Then Do If args~hasIndex( 1 ) Then Return SysSetPriority( args[1], args[2] ) Return SysSetPriority( , args[2] ) End -- SysGetKey([echo]): a key from the terminal, read with no line editing -- (the host's); "" when the standard input is not a terminal. When "GETKEY" Then Do echo = "ECHO" If args~hasIndex( 1 ) Then Do echo = Upper( args[1] ) If echo \== "ECHO", echo \== "NOECHO" Then Raise Syntax 40.920 Array( "SysGetKey", "echo", "'ECHO' or 'NOECHO'", args[1] ) End Call FlushStandard Return SysGetKey( echo ) End -- SysQueryProcess([info]): the host's, the same process (PID, PPID, -- PGID, PPRIO, PTIME, PMEM, PSWAPS, PRCVDSIG; the default is PID). Any -- other selector, "" included, is 40.1 (InvalidRoutine). When "QUERYPROCESS" Then Do If \ args~hasIndex( 1 ) Then Return SysQueryProcess() sel = str( args[1] ) If \ DataType( sel, "A" ) Then Raise Syntax 40.1 Array( "SYSQUERYPROCESS" ) -- (Windows's: PID TID PPRIO TPRIO PTIME TTIME.) If .RVM.FileSystem~windows Then known = "PID TID PPRIO TPRIO PTIME TTIME" Else known = "PID PPID PGID PPRIO PTIME PMEM PSWAPS PRCVDSIG" If WordPos( Upper( sel ), known ) == 0 Then Raise Syntax 40.1 Array( "SYSQUERYPROCESS" ) Return SysQueryProcess( sel ) End -- SysDumpVariables([file]): the caller's variables, appended to the file -- (or written to the standard output of the process). When "DUMPVARS" Then Return DumpVariables( args, context ) -- SysFormatMessage(message, substitutions): &1 to &9 (formatMessage). When "FORMATMESSAGE" Then Return FormatMessage( args[1], args[2] ) -- SysGetErrorText(errno): the host's strerror. When "ERRORTEXT" Then Return SysGetErrorText( args[1] ) -- The files, with results (RexxUtilCommon.cpp, SysRexxUtil.cpp): the -- result stem or array first (StemHandler), and then the host's own -- routine, which the RVM's host is (the same file system, the same -- process: its current directory, its environment). When "FILETREE" Then Do results = ResultStem( args[2], 2, context ) found = .Stem~new a = args~copy; a[2] = found Return HostResults( results, "SysFileTree", a, found ) End When "FILESEARCH" Then Do If args~hasIndex( 4 ) Then If Verify( Upper( args[4] ), "NIC" ) > 0 Then Raise Syntax 40.900 Array( - 'SysFileSearch options argument must be a combination of C, I, or N; found "'args[4]'"' ) results = ResultStem( args[3], 3, context ) found = .Stem~new a = args~copy; a[3] = found Return HostResults( results, "SysFileSearch", a, found ) End When "SEARCHPATH" Then Return HostRexxUtil( "SysSearchPath", args ) When "FILECOPY" Then Return HostRexxUtil( "SysFileCopy", args ) When "FILEMOVE" Then Return HostRexxUtil( "SysFileMove", args ) When "TEMPFILENAME" Then Return HostRexxUtil( "SysTempFileName", args ) When "GETFILEDATETIME" Then Return HostRexxUtil( "SysGetFileDateTime", args ) When "SETFILEDATETIME" Then Return HostRexxUtil( "SysSetFileDateTime", args ) -- SysGetMessage(n [,catalog] [,s1...]) and SysGetMessageX(set, n -- [,catalog] [,s1...]) (Unix): a message of the interpreter by its -- number (set 1 of "rexx.cat"), or of a message catalog; &1 to &9 are -- the substitutions. The host's. When "GETMESSAGE" Then Return HostRexxUtil( "SysGetMessage", args ) When "GETMESSAGEX" Then Return HostRexxUtil( "SysGetMessageX", args ) -- The macrospace (see MacroProgram). When "ADDMACRO" Then Do order = "B" If args~hasIndex( 3 ) Then order = MacroOrder( "SysAddRexxMacro", args[3] ) program = MacroCompile( args[2], context ) If program == .Nil Then Return 7 -- RXMACRO_SOURCE_NOT_FOUND .RVM.RexxUtil~macros[ Upper( args[1] ) ] = .Array~of( order, program ) Return 0 End When "DROPMACRO" Then Do If .RVM.RexxUtil~macros~remove( Upper( args[1] ) ) == .Nil Then Return 2 Return 0 -- (2: RXMACRO_NOT_FOUND) End When "QUERYMACRO" Then Do m = .RVM.RexxUtil~macros[ Upper( args[1] ) ] If m == .Nil Then Return "" Return m[1] End When "REORDERMACRO" Then Do order = MacroOrder( "SysReorderRexxMacro", args[2] ) m = .RVM.RexxUtil~macros[ Upper( args[1] ) ] If m == .Nil Then Return 2 m[1] = order Return 0 End When "CLEARMACROS" Then Do .RVM.RexxUtil~macros~empty Return 0 End When "SAVEMACROS" Then Return SaveMacros( args[1] ) When "LOADMACROS" Then Return LoadMacros( args[1] ) Otherwise Nop End Raise Syntax 93.900 Additional( "RVM: REXXUTIL routine" r[1] "is missing" ) /******************************************************************************/ /* The results of a routine (ooRexx's StemHandler): an Array (emptied, and */ /* the results appended), or a stem (a Stem, or the name of a stem variable */ /* of the caller: elements 1 to n, and n in element 0; the others stay). */ /* Anything else is 40.925. */ /******************************************************************************/ ::Routine ResultStem Use Strict Arg v, i, context If v~isA( .RVM.Obj ) Then If v~native~isA( .Array ) Then Do v~native~empty Return v~native End st = context~world~stemOf( v ) If st \== .Nil Then Return st st = ContextStem( str( v ), context ) If st == .Nil Then Raise Syntax 40.925 Array( i, "" ) Return st -- A routine of the host's REXXUTIL, called with these arguments (omitted -- ones omitted). ::Routine HostRexxUtil Use Strict Arg name, args r = .Routine~loadExternalRoutine( name, "LIBRARY rexxutil" name ) Return r~callWith( args ) -- A host's routine with results in 'found' (a Stem), which are then added -- to 'results'; the routine's result. When it fails, a stem's count is set -- all the same (0), as StemHandler's destructor does. ::Routine HostResults Use Strict Arg results, name, a, found Signal On Syntax Return AddResults( results, found, HostRexxUtil( name, a ) ) Syntax: If \ results~isA( .Array ) Then Call SetStemElement results, 0, 0 Raise Propagate -- The results in 'found.' (found.0 of them) added to 'results'; the result. ::Routine AddResults Use Strict Arg results, found., rc If results~isA( .Array ) Then Do i = 1 To found.0 results~append( found.i ) End Else Do Do i = 1 To found.0 Call SetStemElement results, i, found.i End Call SetStemElement results, 0, found.0 End Return rc /******************************************************************************/ /* The arguments of a routine with a signature (r[6]), as ooRexx's */ /* NativeActivation::processArguments converts them, in order: one that is */ /* missing and required, 88.901; each one there, to its type; then, if there */ /* are more than the signature takes, 88.922. The types: */ /* */ /* ANY RexxObjectPtr: as it is */ /* STR CSTRING, RexxStringObject: its string value (88.909) */ /* POSW positive_wholenumber_t (88.905) */ /* NNEGW nonnegative_wholenumber_t (88.904) */ /* INT32 int32_t (88.907) */ /* ARRAY RexxArrayObject: an Array, or what makes one (98.913) */ /* STEM RexxStemObject: a Stem, or the name of a stem variable of the */ /* caller (the period may be omitted); else 40.919 */ /* * ARGLIST: the rest of the arguments, as they are (never 88.922) */ /* */ /* The result: the arguments converted (omitted ones stay omitted). */ /******************************************************************************/ ::Routine NativeArgs Public Use Strict Arg r, args, context out = .Array~new n = args~last If n == .Nil Then n = 0 i = 0 Do word Over r[6]~makeArray( " " ) -- The rest (ARGLIST: the routine takes the whole argument list), as -- they are, and there are never too many. If word == "*" Then Do Do j = i + 1 To n If args~hasIndex( j ) Then out[j] = args[j] End Return out End i += 1 If \ args~hasIndex( i ) Then Do If Left( word, 1 ) == "r" Then Raise Syntax 88.901 Array( i ) Iterate End v = args[i] Select Case Substr( word, 2 ) When "ANY" Then out[i] = v When "STR" Then out[i] = MethodString( v, i, context~world ) When "POSW" Then out[i] = WholeArg( v, i, 1, "88.905" ) When "NNEGW" Then out[i] = WholeArg( v, i, 0, "88.904" ) When "INT32" Then Do w = .Nil If \ v~isA( .RVM.Obj ) Then w = nativeWhole( v ) ok = w \== .Nil If ok Then ok = ( w >= -2147483648 & w <= 2147483647 ) If \ ok Then Raise Syntax 88.907 Array( i, "-2147483648", "2147483647", ObjectText( v ) ) out[i] = w End When "ARRAY" Then out[i] = CallWithArgs( v ) When "STEM" Then out[i] = StemArg( v, i, context ) -- A double (the API's float, double): a number, or nan, +infinity, -- -infinity; else 88.921. When "FLOAT" Then Do s = .Nil If \ v~isA( .RVM.Obj ) Then s = str( v ) ok = .False If s \== .Nil Then ok = DataType( s, "N" ) | WordPos( s, "nan +infinity -infinity" ) > 0 If \ ok Then Raise Syntax 88.921 Array( i, ObjectText( v ) ) out[i] = s End End End If n > i Then Raise Syntax 88.922 Array( i ) Return out -- A whole number argument, 'min' (0 or 1) to 999999999999999999, the -- largest of ooRexx's whole numbers; else 'code'. ::Routine WholeArg Use Strict Arg v, i, min, code w = .Nil If \ v~isA( .RVM.Obj ) Then w = nativeWhole( v ) If w \== .Nil Then If w >= min Then Return w Raise Syntax (code) Array( i, ObjectText( v ) ) -- A stem argument: a Stem object, or the name of a stem variable of the -- caller (a period is added when it has none); else 40.919. ::Routine StemArg Use Strict Arg v, i, context st = context~world~stemOf( v ) If st \== .Nil Then Return st If v~isA( .RVM.Obj ) Then s = v~requestString Else s = str( v ) If s \== .Nil Then st = ContextStem( s, context ) If st == .Nil Then Raise Syntax 40.919 Array( i, ObjectText( v ) ) Return st -- The stem (an RVM.Stem) of the stem variable named 'name' in the caller -- (ooRexx's getContextStem: a period is added when it has none), made if it -- is not there; .Nil when the name is not that of a stem. ::Routine ContextStem Public Use Strict Arg name, context If Right( name, 1 ) \== "." Then name ||= "." ref = NameRef( name, context ) If ref[1] \== "STEM" Then Return .Nil Return context~stemObject( ref[2], 1 ) /******************************************************************************/ /* The elements of a stem as ooRexx's API sees them (GetStemArrayElement, */ /* SetStemArrayElement, DropStemArrayElement): an element is a tail with a */ /* value of its own (the default does not count). */ /******************************************************************************/ -- The value of a tail, or .Nil (not there, or dropped). ::Routine StemElement Public Use Strict Arg st, tail node = st~findEntry( tail ) If node == .Nil Then Return .Nil v = node~realVariable~value If v == .RVM.Dropped Then Return .Nil Return v ::Routine SetStemElement Use Strict Arg st, tail, v st~setTail( tail, v ) -- A tail that is there is dropped; one that is not stays so. ::Routine DropStemElement Use Strict Arg st, tail If st~findEntry( tail ) \== .Nil Then st~dropTail( tail ) -- The count of a stem array (its element 0): a whole number, or .Nil. ::Routine StemCount Use Strict Arg st v = StemElement( st, 0 ) If v == .Nil Then Return .Nil If v~isA( .RVM.Obj ) Then Return .Nil Return wholeNumber( v, 18 ) /******************************************************************************/ /* SysStemSort(stem [,order] [,type] [,first] [,last] [,firstcol] */ /* [,lastcol]): ooRexx's StemClass::sort, a stable merge sort of the string */ /* values of elements first to last, which then hold them in order. The stem */ /* is a Stem, the name of one, or a stem name with a tail ("s.foo": the */ /* elements are s.FOO.1, s.FOO.2...). */ /******************************************************************************/ ::Routine StemSort Use Strict Arg args, context v = args[1] st = context~world~stemOf( v ) prefix = "" If st == .Nil Then Do s = MethodString( v, 1, context~world ) st = ContextStem( s, context ) If st == .Nil Then Do -- A name with a tail ("a.1"): the stem, and the tail's prefix. If v~isA( .RVM.Obj ) Then Raise Syntax 40.919 Array( 1, ObjectText( v ) ) dot = Pos( ".", s ) If dot == 0 | dot == Length( s ) Then Raise Syntax 40.919 Array( 1, s ) prefix = Upper( Substr( s, dot + 1 ) ) If Right( prefix, 1 ) \== "." Then prefix ||= "." st = ContextStem( Left( s, dot ), context ) If st == .Nil Then Raise Syntax 40.919 Array( 1, s ) End End descending = .False If args~hasIndex( 2 ) Then Do Select Case Upper( Left( args[2], 1 ) ) When "A" Then descending = .False When "D" Then descending = .True Otherwise Raise Syntax 40.920 Array( "SysStemSort", "sort order", "'A' or 'D'", args[2] ) End End caseless = .False If args~hasIndex( 3 ) Then Do Select Case Upper( Left( args[3], 1 ) ) When "C" Then caseless = .False When "I" Then caseless = .True Otherwise Raise Syntax 40.920 Array( "SysStemSort", "sort type", "'C' or 'I'", args[3] ) End End max = 999999999999999999 first = 1; If args~hasIndex( 4 ) Then first = args[4] last = max; If args~hasIndex( 5 ) Then last = args[5] If last < first Then Raise Syntax 40.921 Array( "last", last, "first", first ) firstCol = 1; If args~hasIndex( 6 ) Then firstCol = args[6] lastCol = max; If args~hasIndex( 7 ) Then lastCol = args[7] If lastCol < firstCol Then Raise Syntax 40.921 Array( "last column", lastCol, "first column", firstCol ) -- The count: element 0, a whole number of up to 9 digits, not negative. c = StemElement( st, prefix"0" ) count = .Nil If c \== .Nil, \ c~isA( .RVM.Obj ) Then count = wholeNumber( c, 9 ) If count \== .Nil Then If count < 0 Then count = .Nil If count == .Nil Then Raise Syntax 40.922 If count == 0 Then Return 0 If last == max Then last = count If first > count | last > count Then Raise Syntax 40.923 Array( count ) nodes = .Array~new values = .Array~new Do i = first To last node = st~findEntry( prefix || i ) ev = .Nil If node \== .Nil Then ev = node~realVariable~value If ev == .Nil | ev == .RVM.Dropped Then Raise Syntax 40.924 Array( i ) nodes~append( node~realVariable ) values~append( str( ev ) ) End sorter = .RVM.StemSorter~new( descending, caseless, firstCol, lastCol, max ) sorted = sorter~sort( values ) Do j = 1 To nodes~items nodes[j]~value = sorted[j] End Return 0 -- The comparisons of StemClass::sort (the sortCompare and -- sortCaselessCompare of RexxString), and a stable merge sort. ::Class RVM.StemSorter ::Method init Expose descending caseless columns start length signed Use Strict Arg descending, caseless, firstCol, lastCol, max columns = \ ( firstCol == 1 & lastCol == max ) start = firstCol - 1 -- 0-based length = lastCol - firstCol + 1 -- A caseless comparison compares (signed) chars: bytes 80-FF sort first. signed = XRange( "80"x, "FF"x ) || XRange( "00"x, "7F"x ) ::Method sort Use Strict Arg values n = values~items If n < 2 Then Return values work = .Array~new( n ) self~mergeSort( values, work, 1, n ) Return values ::Method mergeSort Use Strict Arg a, work, lo, hi If hi - lo < 1 Then Return mid = ( lo + hi ) % 2 self~mergeSort( a, work, lo, mid ) self~mergeSort( a, work, mid + 1, hi ) i = lo; j = mid + 1; k = lo Do While i <= mid & j <= hi -- Stable: the left one first when they are equal. If self~compare( a[j], a[i] ) < 0 Then Do work[k] = a[j]; j += 1 End Else Do work[k] = a[i]; i += 1 End k += 1 End Do While i <= mid work[k] = a[i]; i += 1; k += 1 End Do While j <= hi work[k] = a[j]; j += 1; k += 1 End Do k = lo To hi a[k] = work[k] End ::Method compare Expose descending Use Strict Arg x, y r = self~compareAscending( x, y ) If descending Then Return -r Return r ::Method compareAscending Expose columns start length Use Strict Arg x, y lx = Length( x ); ly = Length( y ) If \ columns Then Do r = self~compareBytes( x, y ) If r \== 0 Then Return r Return Sign( lx - ly ) End If start < lx & start < ly Then Do -- (the C code compares a byte past the end of the shorter one: its NUL) sl = Min( lx, ly ) - start + 1 n = Min( length, sl ) r = self~compareBytes( Substr( x"00"x, start + 1, n ), Substr( y"00"x, start + 1, n ) ) If r == 0 & sl < length Then r = Sign( lx - ly ) Return r End Return Sign( lx - ly ) -- memcmp (unsigned bytes), or the caseless one (ASCII upper, signed chars), -- of two strings of the same length (the shorter one's prefix when not). ::Method compareBytes Expose caseless signed Use Strict Arg x, y n = Min( Length( x ), Length( y ) ) x = Left( x, n ); y = Left( y, n ) If x == y Then Return 0 If caseless Then Do x = Translate( UpperASCII( x ), signed, XRange( "00"x, "FF"x ) ) y = Translate( UpperASCII( y ), signed, XRange( "00"x, "FF"x ) ) If x == y Then Return 0 End If x << y Then Return -1 Return 1 ::Routine UpperASCII Use Strict Arg s Return Translate( s, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz" ) /* SysStemDelete(stem, start [,count]) */ ::Routine StemDelete Use Strict Arg args st = args[1] start = args[2] count = 1 If args~hasIndex( 3 ) Then count = args[3] items = StemCount( st ) If items == .Nil Then Raise Syntax 40.922 If items < 0 Then Raise Syntax 40.922 If start + count - 1 > items Then Raise Syntax 40.923 Array( items ) Do index = start While index + count <= items v = StemElement( st, index + count ) If v == .Nil Then Raise Syntax 40.924 Array( index ) Call SetStemElement st, index, v End Do index = items - count + 1 To items Call DropStemElement st, index End Call SetStemElement st, 0, items - count Return 0 /* SysStemInsert(stem, position, value) */ ::Routine StemInsert Use Strict Arg args st = args[1] position = args[2] count = StemCount( st ) If count == .Nil Then Raise Syntax 40.922 If position > count + 1 Then Raise Syntax 40.923 Array( count ) Do index = count By -1 While index >= position v = StemElement( st, index ) If v == .Nil Then Raise Syntax 40.924 Array( index ) Call SetStemElement st, index + 1, v End Call SetStemElement st, position, args[3] Call SetStemElement st, 0, count + 1 Return 0 /* SysStemCopy(from, to [,from] [,to] [,count] [,'I'|'O']) */ ::Routine StemCopy Use Strict Arg args fromStem = args[1] toStem = args[2] inserting = .False If args~hasIndex( 6 ) Then Do Select Case Upper( Left( args[6], 1 ) ) When "I" Then inserting = .True When "O" Then inserting = .False Otherwise Raise Syntax 40.920 Array( "SysStemCopy", "sort type", "'I' or 'O'", args[6] ) End End fromCount = StemCount( fromStem ) If fromCount == .Nil Then Raise Syntax 40.922 from = 1; If args~hasIndex( 3 ) Then from = args[3] to = 1; If args~hasIndex( 4 ) Then to = args[4] If args~hasIndex( 5 ) Then Do count = args[5] If count > fromCount - from + 1 | fromCount == 0 Then Raise Syntax 40.923 Array( fromCount ) End Else count = fromCount - from + 1 toCount = 0 If StemElement( toStem, 0 ) \== .Nil Then Do toCount = StemCount( toStem ) If toCount == .Nil Then Raise Syntax 40.922 End If to > toCount + 1 Then Raise Syntax 40.923 Array( toCount ) If inserting Then Do Do index = toCount By -1 While index >= to v = StemElement( toStem, index ) If v == .Nil Then Raise Syntax 40.924 Array( index ) Call SetStemElement toStem, index + count, v End toCount += count Call SetStemElement toStem, 0, toCount End Do index = 0 While index < count v = StemElement( fromStem, from + index ) If v == .Nil Then Raise Syntax 40.924 Array( index ) Call SetStemElement toStem, to + index, v End If to + count - 1 > toCount Then Call SetStemElement toStem, 0, to + count - 1 Return 0 /******************************************************************************/ /* SysDumpVariables([file]): "Name=N, Value='v'" for each variable of the */ /* caller (a stem: its value, then each of its elements, "Name=S.T", whose */ /* lines end in CR LF: the native's), in the order of a Directory of them */ /* (GetAllContextVariables, GetAllStemElements). Appended to the file (40.1 */ /* when it cannot be opened), or written to the standard output. */ /******************************************************************************/ -- What has been written to the standard output and error, out now (before -- the host runs a command that writes there too). ::Routine FlushStandard Signal On Any Name Done .Output~flush .Error~flush Done: Return ::Routine DumpVariables Use Strict Arg args, context frame = context~frame vars = .Directory~new Do name Over frame~variableNames vars[ frame~ownName( name ) ] = frame[ name ]~value End out = .MutableBuffer~new s = vars~supplier Do While s~available name = s~index v = s~item If v~isA( .RVM.Stem ) Then Do default = v~value out~append( "Name="name", Value='"DumpString( default )"'" || "0a"x ) tails = .Directory~new Do node Over v~nodes If node~value \== .RVM.Dropped Then tails[ node~name ] = node~value End t = tails~supplier Do While t~available out~append( "Name="name || t~index", Value='"DumpString( t~item )"'" || "0d0a"x ) t~next End End Else out~append( "Name="name", Value='"DumpString( v )"'" || "0a"x ) s~next End If \ args~hasIndex( 1 ) Then Do .stdout~charOut( out~string ) Return 0 End file = .Stream~new( .RVM.FileSystem~qualify( args[1] ) ) If file~open( "write append" ) \== "READY:" Then Raise Syntax 40.1 Array( "SysDumpVariables" ) file~charOut( out~string ) file~close Return 0 -- The string of a value, as ObjectToString (an object's STRING). ::Routine DumpString Use Strict Arg v Return str( v ) /******************************************************************************/ /* formatMessage (RexxUtilCommon.cpp): &1 to &9 are the substitutions (an */ /* omitted one, or one past the last, is ""); more than 9 is 40.1. A NUL in */ /* the message ends it (a C string). */ /******************************************************************************/ ::Routine FormatMessage Use Strict Arg message, subs n = subs~last If n == .Nil Then n = 0 If n > 9 Then Raise Syntax 40.1 Array( "SysFormatMessage" ) p = Pos( "00"x, message ) If p > 0 Then message = Left( message, p - 1 ) out = .MutableBuffer~new Do Forever p = Pos( "&", message ) If p == 0 Then Leave c = Substr( message, p + 1, 1 ) If c \== "", Verify( c, "123456789" ) == 0 Then Do out~append( Left( message, p - 1 ) ) If c <= n Then Do If subs~hasIndex( c ) Then Do v = str( subs[c] ) z = Pos( "00"x, v ) If z > 0 Then v = Left( v, z - 1 ) out~append( v ) End End message = Substr( message, p + 2 ) End Else Do out~append( Left( message, p ) ) message = Substr( message, p + 1 ) End End out~append( message ) Return out~string /******************************************************************************/ /* The macrospace (ooRexx's, kept by rxapi for every process of the system: */ /* the RVM's is the process's). A macro is a program by a name (any case), */ /* found before the routines of REXXUTIL ("B") or after the programs in files */ /* ("A"): MacroProgram, for the machine's CallRoutine and LoadExternal. The */ /* return codes are the API's: 0 done, 2 not found, 5 a file error, 6 not a */ /* macrospace, 7 no such program (or one that does not compile). A saved */ /* macrospace is the RVM's own ("RVM-MACROSPACE 1", then each macro: "MACRO */ /* order name-in-hex count" and its R-code lines); loading one replaces the */ /* macros of the same names. */ /******************************************************************************/ -- The program of macro 'name' when it is found in 'order' ("B" or "A"), or -- .Nil. ::Routine MacroProgram Public Use Strict Arg name, order m = .RVM.RexxUtil~macros[ Upper( name ) ] If m == .Nil Then Return .Nil If m[1] \== order Then Return .Nil Return m[2] -- 'B' or 'A' (any case); else 40.920. ::Routine MacroOrder Use Strict Arg function, option Select Case Upper( Left( option, 1 ) ) When "B" Then Return "B" When "A" Then Return "A" Otherwise Raise Syntax 40.920 Array( function, "order", "'A' or 'B'", option ) End -- The program in a file (a regular one) compiled, or .Nil. ::Routine MacroCompile Use Strict Arg file, context path = .RVM.FileSystem~qualify( file ) If path == "" Then Return .Nil If \ SysIsFile( path ) Then Return .Nil saved = context~interpretError Signal On Syntax Return .RVM.RexxUtil~loader~call( path, context ) Syntax: context~interpretError = saved Return .Nil ::Routine SaveMacros Use Strict Arg file macros = .RVM.RexxUtil~macros If macros~isEmpty Then Return 5 path = .RVM.FileSystem~qualify( file ) If path == "" Then Return 5 out = .Stream~new( path ) If out~open( "write replace" ) \== "READY:" Then Return 5 Do line Over MacroImage() out~lineOut( line ) End out~close Return 0 -- The lines of the image of the macrospace (see SaveMacros). ::Routine MacroImage macros = .RVM.RexxUtil~macros lines = .Array~of( "RVM-MACROSPACE 1" ) Do name Over macros~allIndexes~sort m = macros[ name ] rcode = m[2]~rcode lines~append( "MACRO" m[1] C2X( name ) rcode~items ) Do line Over rcode lines~append( line ) End End Return lines ::Routine LoadMacros Use Strict Arg file path = .RVM.FileSystem~qualify( file ) If path == "" Then Return 5 If \ SysIsFile( path ) Then Return 5 in = .Stream~new( path ) If in~open( "read" ) \== "READY:" Then Return 5 lines = in~arrayIn in~close loaded = MacrosOfImage( lines ) If loaded == .Nil Then Return 6 -- (a macro that is there is replaced, as ooRexx's) macros = .RVM.RexxUtil~macros Do name Over loaded~allIndexes macros[ name ] = loaded[ name ] End Return 0 -- The macros of an image (NAME -> (order, program)), or .Nil when it is not -- one. ::Routine MacrosOfImage Use Strict Arg lines If lines~items == 0 Then Return .Nil If lines[1] \== "RVM-MACROSPACE 1" Then Return .Nil loaded = .Directory~new i = 2 Do While i <= lines~items Parse Value lines[i] With word order hex n . If word \== "MACRO" | \ DataType( n, "W" ) | \ DataType( hex, "X" ) Then Return .Nil If i + n > lines~items Then Return .Nil Signal On Syntax program = .RVM.RexxUtil~reader~call( lines~section( i + 1, n ), X2C( hex ), .Nil ) Signal Off Syntax loaded[ X2C( hex ) ] = .Array~of( order, program ) i += n + 1 End Return loaded Syntax: Return .Nil /******************************************************************************/ /* RVM.SharedMacros -- the macrospace shared with what a command starts (as */ /* the queues are): ooRexx's is rxapi's, one for every process; the RVM's */ /* goes to the RVM that a command starts in a file (RVM_MACROSPACE: see */ /* RVM.CommandHost~run and RVM.SystemHost~shareQueues), and comes back if it */ /* changed there. Its text is the image of the macrospace (MacroImage); */ /* restore replaces the whole macrospace (.False: not an image). */ /******************************************************************************/ ::Class RVM.SharedMacros Public ::Method activate Class .RVM.CommandHost~macros = self ::Method snapshot Class Return MacroImage()~makeString( "L", "0a"x ) ::Method restore Class Use Strict Arg text loaded = MacrosOfImage( text~makeArray( "0a"x ) ) If loaded == .Nil Then Return .False macros = .RVM.RexxUtil~macros macros~empty Do name Over loaded~allIndexes macros[ name ] = loaded[ name ] End Return .True ::Requires "modules/rvm/RVM.Number.cls" ::Requires "modules/rvm/RVM.Objects.cls" ::Requires "modules/rvm/RVM.BIFs.cls"