/** rexx:breakthings.rexx
*
*  This program demonstrates what happens if you first add
*  our almost function host, rexx:testhost.rexx with a lower
*  priority than rexxmathlib.library, and then what happens
*  if you change things so that TESTPORT is added to the library
*  list with a higher priority than rexxmathlib.library.
*  In the first case, since TESTPORT gets all functions only
*  after everyone has rejected them, calls like sin(2) succeed.
*  In the second case no call ever gets to rexxmathlib.library
*  because one has to do more than reply to the RexxMessage
*  in order for the REXX host process to keep looking for a match
*
**/
   /*
   *  Make sure all libraries are present
   */

   addrexxlib

   /*
   *  First open a requester telling the user what is about to
   *  happen.  The first two arguments to the request function
   *  are the x and y coordinates of the upper left hand corner
   *  of the window.  The next argument is the message to be
   *  displayed.  Multi-line messages can be composed by using
   *  a \ to indicate a carriage return. I tend to compose the
   *  message first and then call the function
   */
      message =          "This is a demo to show how things break when\"
      message = message||"you add a broken function host or non-existent\"
      message = message||"library to ARexx's library list\\"
      message = message||"             Should I continue?               "
   /*
   *  Put up a simple requester with an OKAY or ABORT button
   *  the call is
   *
   *       request(xpos,ypos,message,,okay text,abort text)
   *
   *  The two ,, together are to indicate that this is not a
   *  string requester.  Clicking on the gadget that says Go On
   *  returns OKAY, clicking on Abort returns nothing
   */
      x = request(0,0,message,," Go On "," Abort ")
      if x ~="OKAY" then exit 0
   /*
   *  Start rexx:testhost running in a shell
   *  Send the command to AREXX so it will run asynchronously
   */
      address AREXX "work:articles/testhost.rexx"
   /*
   *  Now check to see that TESTPORT has been opened, give it some
   *  time.  If it doesn't open then exit and use postmsg() to
   *  inform the user what happened
   */
      do i = 1 to 2
         if ~showlist('p','TESTPORT') then call delay 20
         else leave i
      end

      if i = 2 & ~showlist('p',TESTPORT) then do
         call postmsg(10,10,"Couldn't start rexx:testhost.rexx")
         call delay 80
         call postmsg()
         exit 2
      end
   /*
   *  Add TESTPORT at a lower priority than rexxarplib,rexxmathlib,etc.
   *
   */
      call addlib(TESTPORT,-1)
   /*
   *  First show that a call to postnote works just fine
   *  by executing the call to postnote()
   */
      message =          "TESTPORT has been added to the library list  \"
      message = message||"at priority -1.  We can now do               \\"
      message = message||"             call postnote()                 \\"
      message = message||"and a window should open, hang around, and   \"
      message = message||"then close.\"

      x = request(0,0,message,," Show Me "," Abort ")
      if x ~= OKAY then exit 0

      call postnote()

      message = "Well, that worked okay, now how about calling a math\"
      message = message||"function from rexxmathlib.library"

      call delay 30
      x = request(0,0,message,," Do It "," Abort ")
      if x ~= OKAY then exit 0
   /*
   *  Get a list of all entries on the library list and reformat
   *  them into a column. Define nl to be the console
   *  linefeed character to make things easy.
   */
      nl = "0a"x
      libcol = ""

      liblist = show('l')
      do until liblist = ""
         parse var liblist nextentry liblist
         libcol = libcol"     "||nextentry||"0a"x
      end
   /*
   *  Call sin(3) which is supplied by rexxmathlib.library
   */
      sin3 = sin(3)
   /*
   *  format the message to appear in the window
   */
      message = "ARexx's current library list contains"nl"the following"nl||nl
      message = message||libcol||nl||nl
      message = message||"So a call to rexxmathlib should work fine."nl||nl
      message = message||"The result of typing"||nl||nl
      message = message||"     "||"say sin(3)"||nl
      message = message||"   > "sin3"0a"x
   /*
   *  Use a console window for output of the information
   *  post a requester above it to ask for directions.  Also,
   *  since nothing happens until the user clicks on something
   *  this provides a pause in the program.
   */
      call open(DEMOWINDOW,"con:0/65/400/160/Demo")
      call writech(DEMOWINDOW,message)

      message =          " So far so good.  The only thing that remains\"
      message = message||" is to see how things break.   Want to go on?"
      x = request(0,0,message,," Continue "," Quit ")

      call close(DEMOWINDOW)

      if x ~= OKAY then exit 0

   /*
   *  User clicked on Continue so now let us remove rexxmathlib and
   *  TESTPORT from the library list and add it back at a lower priority
   *  than TESTPORT
   */
      call remlib('rexxmathlib.library')
      call addlib('rexxmathlib.library',-2,-30,0)
   /*
   *  Now show the reader what happens when you try and call sin(3)
   *  in this situation
   *  Since the call will break with an error return from function
   *  which is an ARexx SYNTAX error we have to trap the error and
   *  handle the condition ourselves, or our demo will stop
   */
      libcol = ""

      liblist = show('l')

      do until liblist = ""
         parse var liblist nextentry liblist
         libcol = libcol"     "||nextentry||"0a"x
      end

      SIGNAL ON SYNTAX

      sin3 = sin(3)

CONTINUE:

      message =          "Well, this time the library list looks like "nl||nl
      message = message||libcol||nl||nl
      message = message||"and so now the line                         "nl
      message = message||"        say sin(3)                          "nl||nl
      message = message||"produces an error message                   "nl
      message = message||"  > "errormess

      call open(DEMOWINDOW,"con:0/48/383/160/Demo")
      call writech(DEMOWINDOW,message)

      x = request(0,0,"Well that's it. Click on the close gadget  \to exit the program ")
   /*
   *  Time to clean things up and put them back the way they were
   */
      call close(DEMOWINDOW)
      call closetestport()
      call remlib(TESTPORT)
      call remlib('rexxmathlib.library')
      call addlib('rexxmathlib.library',0,-30,0)

exit 0
SYNTAX:
   errormess = errortext(rc);
   signal CONTINUE:
return