riarexxlib
This library gives a program a public message port through which ARexx and other programs can send it commands, and lets the program send commands to the ports of other programs. It needs rexxsyslib.library, which comes with ARexx; without it, nothing is sent, and the commands that send return 0 or an empty string. All commands that work with the port are Amiga mode only.
The library has one port, not numbered objects. RXCreatePort makes
it and must come before most other commands; with runtime error
checking on, they stop with the error “No message port opened!”
without it. RXDeletePort removes the port again; the program does
that by itself when it ends.
Received messages are kept in a list of up to 31; further ones wait
in the port until the list has room. RXReadPort and RXWaitPort
take the next one from it and make it the current message, which
RXNumArgs, RXGetArg, RXNextArg and RXSendersName read and
RXReplyMsg answers. Every received message must be replied, or its
sender waits for ever.
RXSendCommand sends a command and waits for the reply and its result
string; RXRunCommand sends a command without waiting. Both send to
the port named in their call or, without a name, to the default port,
REXX unless RXDefaultPort sets another one. By default a command
is split into words, each sent as an argument of its own; text
between quote characters (" unless RXQuoteChar sets another one)
is one word. A receiving program that reads only the first argument
sees only the first word; RXTokenise Off sends the whole command as
one argument instead.
A program that answers commands until it gets QUIT:
If RXCreatePort("MYAPP")
Repeat
If RXWaitPort
cmd$=RXGetArg(1)
NPrint "Command: ",cmd$
RXReplyMsg 0
EndIf
Until UCase$(cmd$)="QUIT"
RXDeletePort
EndIfThe fixed bugs of the library are listed in Fixes in riarexxlib.
Based on the RI ARexx Lib guide by Red When Excited.
Library number 133.
Commands
RXCreatePort
Makes the program’s public message port with the name name$. Returns the port’s address, or 0 if the program has a port already, a public port of that name exists, or the port cannot be made.
Function, result .l. Help text: (name$)
Arguments: $
RXDefaultPort
Sets the port that RXRunCommand and RXSendCommand send to when they are given no port name. It is REXX at the start of every run.
Statement. Help text: portname$
Arguments: $
RXDeletePort
Removes the program’s message port. The current message and the messages that are still waiting are replied with the return code 5 first.
Statement. No help text.
No arguments.
Then the command waits up to 5 seconds for the replies to commands
sent with RXRunCommand or RXRunScript that are still with their
receivers, and replies messages that arrive meanwhile with the
return code 5; the end of the program does the same. If a reply is
still out after 5 seconds, the port is taken off the public list but
kept in memory, so that the late reply has somewhere to go.
RXExecuteScript
Runs the ARexx script scriptname$ through ARexx and waits until it has ended. Returns -1 when the script was sent, 0 if ARexx is not running; RXReturnCode gives the script’s return code.
Statement or function, result .l. Help text: scriptname$
Arguments: $
The message always goes to the port REXX, not to the default port.
Messages that arrive while the command waits are kept for
RXReadPort and RXWaitPort. A script that sends a command to the
program’s own port and waits for the reply cannot be run this way,
because both would wait for each other; use RXRunScript for it.
RXGetArg
The argument argnum, 1 to 16, of the current message. An empty string if there is no such argument or no current message.
Function, result $. Help text: argnum
Arguments: .l
RXMessageList
The address of the library’s list of received messages that have not yet become current, a list of message addresses ending with 0; 0 before RXCreatePort.
Function, result .l. No help text.
No arguments.
RXNextArg
The next argument of the current message, starting with the first. An empty string after the last one.
Function, result $. Help text: argnum
No arguments.
RXNumArgs
The number of arguments of the current message, 0 if there is no current message.
Function, result .l. No help text.
No arguments.
RXQuoteChar
Sets the quote character that groups words when commands are split, the first character of char$. It is " at the start; ' saves typing Chr$(34).
Statement. Help text: char$
Arguments: $
RXReadPort
Takes the messages that have arrived at the port and makes the next one the current message. Returns its address, or 0 if no message is waiting.
Function, result .l. No help text.
No arguments.
Replies to the program’s own messages are handled here too. Anything at the port that is neither a message nor a reply is replied at once, and the reading stops there.
The library’s list of received messages has room for 31. While it is full, further messages stay in the port, in the order they came, and are taken into the list as it gets room.
RXReplyMsg
Replies to the current message with the return code errcode, 0 for no error, and the optional result string result$. Nothing happens if there is no current message.
Statement. Help text: errcode[,result$]
2 forms:
.l.l, $
Read the arguments of a message before replying to it: after the
reply there is no current message, and RXGetArg, RXNextArg,
RXNumArgs and RXSendersName give 0 or an empty string until the
next RXReadPort or RXWaitPort.
The result string goes back only if the sender asked for one and
errcode is 0. The reply goes to the port named in the message; if
no such port exists any more, the message is not replied.
RXReturnCode
The return code of the reply to the last RXSendCommand or RXExecuteScript, 0 when it succeeded.
Function, result .l. No help text.
No arguments.
RXRunCommand
Sends command$ to the port portname$, or to the default port, and does not wait for the reply. Returns -1 when the command was sent, and 0 if the port is not found or the command cannot be sent.
Statement or function, result .l. Help text: [portname$,]command$
2 forms:
$$, $
The library frees the reply when it arrives, at the next
RXReadPort, RXWaitPort or RXSendCommand. Up to 31 commands
sent this way can wait for their replies; while 31 do, the command
sends nothing and returns 0.
RXRunScript
Runs the ARexx script scriptname$ through ARexx, without waiting. Returns -1 when the script was sent, 0 if ARexx is not running.
Statement or function, result .l. Help text: scriptname$
Arguments: $
The message always goes to the port REXX, not to the default port.
The notes of RXRunCommand apply too.
RXSendCommand
Sends command$ to the port portname$, or to the default port, waits for the reply and returns its result string. An empty string if the port is not found, the command failed or the reply has no result; RXReturnCode gives the return code.
Statement or function, result $. Help text: [portname$,]command$
2 forms:
$$, $
Messages that arrive while the command waits are kept for
RXReadPort and RXWaitPort.
RXSendersName
The name stored in the current message, which for messages sent by this library is the name of the sender’s port. An empty string if there is no current message.
Function, result $. No help text.
No arguments.
RXTokenise
With On, the default, commands are split into words, each sent as an argument of its own; with Off, a command is sent as one argument.
Statement. Help text: on|off
Arguments: .w
Spaces separate the words. Text between two quote characters is one word, and inside it a doubled quote character stands for one quote; the character right after the closing quote starts the next word. An empty quoted word is sent as an empty argument.
A message has at most 16 arguments: with more words, the 16th argument holds the rest of the command as it is, for the receiver to split. Commands and words can be of any length.
RXWaitPort
Like RXReadPort, but waits for a message if none is waiting. Returns the address of the new current message, or 0 if only replies to the program’s own messages arrived.
Function, result .l. No help text.
No arguments.