/* typepkt.rexx

   This is a simple host written in ARexx, an adaptation of SimpleHost.rexx,
   slightly spiced up by the addition of some calls to the support library
   TYPEPKT function. Try running it first as a command host:

      Shell> run rx TypePkt
      Shell> rx "address typepkt_host boguscommand
      Shell> rx "address typepkt_host quit

   Now run it again, but this time add it as a function host:

      Shell> run rx TypePkt
      Shell> RXLIB TYPEPKT_HOST 0

   Now it will respond (though not usefully) to function calls that are
   not picked up by the built-in library or anyone higher up on the Library
   List (to see who's on the list, just invoke RXLIB with no arguments).
   The response will show the contents of the message packets sent as a
   result of your function calls, e.g.:

      Shell> rx "say gizmo(3,11,4)
      Packet command field contains: $02020003
      Number of arguments          : 3
      Method of invocation         : Function
      Unknown Function: GIZMO

   When you've tried a few of these, you can quit (use QUIT as a function
   or as a command). The host will remove itself from the Library List
   before exiting.
*/

call AddRexxSupport('FAIL')

hname    = 'TYPEPKT_HOST'
shutdown = 0

if openport(hname) then do
   if open('cf','con:50/50/200/20/'hname) then do
      do until shutdown
         call waitpkt(hname)
         pkt   = getpkt(hname)
         rcode = 0

         if pkt ~= null() then do
            cmd = getarg(pkt)

            if typepkt(pkt,'f') then
               pt = 'Function'
            else if typepkt(pkt,'c') then
               pt = 'Command'
            else
               pt = 'Unknown'

            say "Packet command field contains: $"c2x(typepkt(pkt))
            say "Number of arguments          :" typepkt(pkt,'a')
            say "Method of invocation         :" pt

            if upper(cmd)='QUIT' then do
               if show('l','TYPEPKT_HOST') then do
                  call remlib('TYPEPKT_HOST')
                  say "Removed TYPEPKT_HOST from Library List."
                  end

               shutdown = 1
               end
            else do
               if pt='Unknown' then pt='Keyword'

               say 'Unknown' pt':' cmd
               rcode = 10
               end

            call reply(pkt,rcode)
            end

         end

      call close('cf')
      end

   call closeport(hname)
   end

exit


/* AddRexxSupport

   You can use this to check/add `rexxsupport.library' and verify its
   existence before trying to call functions it contains. That way the script
   won't bomb out ungracefully and unhelpfully with either `Function not
   found' or `Host environment not found'. If you call this with
   arg(1)=`FAIL', it will SAY a message and exit the script if the library is
   not available. Otherwise the availability of the library will be reflected
   in the boolean return value (1 = OK, 0 = no library).
*/

AddRexxSupport: procedure

   signal on syntax

   if ~show('l','rexxsupport.library') then
      call addlib('rexxsupport.library',0,-30)  /* add to library list */

   call null()                                  /* force lib to load   */
   return 1

   syntax:
      if arg(1)='FAIL' then do
         say "Library rexxsupport.library is unavailable."
         exit
         end
      else
         return 0
