/******************************************************************************/ /* */ /* RVM.Commands.cls - Redirectors of commands (ADDRESS ... WITH) */ /* ============================================================== */ /* */ /* IssueCommand (in RVM.Machine) works out the redirections of a command */ /* (ADDRESS ... WITH) and builds, for its INPUT, OUTPUT and ERROR, a source */ /* or a target; those for a file and for a fixed list of lines are here, and */ /* those that need the objects of the program (a stem, a stream object, a */ /* collection) are in RVM.Machine. The launching itself is the command host's */ /* (RVM.Host.cls). See doc/13-io-and-commands.md. */ /* */ /* RVM.StreamInput INPUT STREAM name: the lines of the file */ /* RVM.LinesInput INPUT USING a value: a fixed list of lines */ /* RVM.StreamOutput OUTPUT/ERROR STREAM name: the file is replaced (the */ /* default) or appended to */ /* */ /* Each input line is followed by a line end. Output is split into lines at */ /* LF or CR LF (SplitOutput); a last line without a line end is kept; a CR */ /* that ends the output is dropped. A file read as input is split as reading */ /* it line by line splits it (SplitFileLines). */ /* */ /* This file is part of the RVM, the Rexx Virtual Machine */ /* [See https://rexx.epbcn.com/rexx-parser/] */ /* */ /* Copyright (c) 2026 Josep Maria Blasco */ /* */ /* License: Apache License 2.0 (https://www.apache.org/licenses/LICENSE-2.0) */ /* */ /******************************************************************************/ /******************************************************************************/ /* Line handling */ /******************************************************************************/ /* Split command output into lines: a line ends at LF or CR LF; a last line */ /* without a line end is kept, and a CR that ends the text is dropped. A CR */ /* not followed by LF is ordinary data. */ ::Routine SplitOutput Public Use Strict Arg text lines = .Array~new n = Length( text ) start = 1 i = 1 Do While i <= n c = SubStr( text, i, 1 ) If c == "0a"x Then Do lines~append( SubStr( text, start, i - start ) ) start = i + 1 End Else If c == "0d"x, i < n, SubStr( text, i + 1, 1 ) == "0a"x Then Do lines~append( SubStr( text, start, i - start ) ) i = i + 1 start = i + 1 End i = i + 1 End If start <= n Then Do last = SubStr( text, start ) If Right( last, 1 ) == "0d"x Then last = Left( last, Length(last) - 1 ) lines~append( last ) End Return lines /* Split a file's content into lines the way reading it line by line does: a */ /* line ends at LF, and a CR just before that LF is dropped; a last line */ /* without LF is kept as it is. */ ::Routine SplitFileLines Use Strict Arg text lines = .Array~new start = 1 Loop p = Pos( "0a"x, text, start ) If p == 0 Then Leave line = SubStr( text, start, p - start ) If Right( line, 1 ) == "0d"x Then line = Left( line, Length(line) - 1 ) lines~append( line ) start = p + 1 End If start <= Length( text ) Then lines~append( SubStr( text, start ) ) Return lines /* The input text for a list of lines: each line followed by a line end. */ ::Routine JoinLines Use Strict Arg lines, lineEnd text = "" Do line Over lines text = text || line || lineEnd End Return text /******************************************************************************/ /* Input sources. ~bytes(context, lineEnd) returns everything to feed. */ /******************************************************************************/ -- INPUT STREAM name : the lines of the file. ::Class RVM.StreamInput Public ::Method init Expose name Use Strict Arg name ::Method bytes Expose name Use Strict Arg context, lineEnd host = context~commandHost text = host~readFile( host~qualifyName( name ) ) Return JoinLines( SplitFileLines( text ), lineEnd ) -- INPUT USING value : a fixed list of lines. ::Class RVM.LinesInput Public ::Method init Expose lines Use Strict Arg lines ::Method bytes Expose lines Use Strict Arg context, lineEnd Return JoinLines( lines, lineEnd ) /******************************************************************************/ /* Output targets. ~open, then ~writeText (all the captured text, split into */ /* lines), then ~close. ~sameTarget tells whether OUTPUT and ERROR coincide. */ /******************************************************************************/ -- OUTPUT/ERROR STREAM name ::Class RVM.StreamOutput Public ::Attribute name Get ::Method init Expose name append stream Use Strict Arg name, append stream = .Nil ::Method open Expose name append stream Use Strict Arg context host = context~commandHost stream = host~openOutputFile( host~qualifyName( name ), append ) ::Method writeText Expose stream Use Strict Arg text, context Do line Over SplitOutput( text ) stream~lineOut( line ) End ::Method close Expose stream If stream \== .Nil Then stream~close ::Method sameTarget Expose name Use Strict Arg other, context If \ other~isA( .RVM.StreamOutput ) Then Return .False host = context~commandHost Return host~qualifyName( name ) == host~qualifyName( other~name ) ::Requires "parser.cls" ::Requires "RVM.Number.cls" ::Requires "RVM.Host.cls"