home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / vxtech07.zip / RXAWAR / VARPOOL / C / FUNC.C next >
C/C++ Source or Header  |  1994-08-05  |  5KB  |  174 lines

  1. /*
  2.  * func.c --
  3.  */
  4.  
  5. #define INCL_REXXSAA
  6.  
  7. #include <os2.h>
  8. #include <rexxsaa.h>
  9.  
  10. #include <stdio.h>
  11. #include <ctype.h>
  12.  
  13. #include "func.h"
  14. #include "varpool.h"
  15.  
  16. /*
  17.  * SetVar -- A REXX external function that sets a variable...
  18.  *
  19.  *       SETVAR( varname, value )
  20.  */
  21.  
  22. RexxFunctionHandler SetVar;
  23.  
  24. ULONG SetVar( PUCHAR funcname, ULONG numargs, PRXSTRING args,
  25.               PSZ queuename, PRXSTRING result )
  26.   {
  27.     LONG     ret;
  28.  
  29.     /* Get rid of compiler warnings... */
  30.  
  31.     funcname  = funcname;
  32.     queuename = queuename;
  33.  
  34.     /* Need exactly two arguments... */
  35.  
  36.     if( numargs != 2 ) return( 40 );
  37.  
  38.     ret = VarSetValue( args[0].strptr, &args[1] );
  39.  
  40.     result->strptr[0] = ( ( ret == RXSHV_OK || ret == RXSHV_NEWV ) ? '1' : '0' );
  41.     result->strlength = 1;
  42.  
  43.     return( 0 );
  44.   }
  45.  
  46. /*
  47.  * DumpVars -- A REXX external function that dumps all the currently
  48.  *             defined rexx variables.
  49.  */
  50.  
  51. RexxFunctionHandler DumpVars;
  52.  
  53. ULONG DumpVars( PUCHAR funcname, ULONG numargs, PRXSTRING args,
  54.                 PSZ queuename, PRXSTRING result )
  55.   {
  56.     RXSTRING name;
  57.     RXSTRING value;
  58.     ULONG    count = 0;
  59.  
  60.     /* Get rid of compiler warnings... */
  61.  
  62.     funcname  = funcname;
  63.     queuename = queuename;
  64.     args      = args;
  65.  
  66.     /* Need exactly no arguments... */
  67.  
  68.     if( numargs != 0 ) return( 40 );
  69.  
  70.     printf( "---- Dumping variable pool:\n\n" );
  71.  
  72.     /* Scan through the list and print each one... */
  73.  
  74.     while( 1 ){
  75.         name.strlength = value.strlength = 0;
  76.         name.strptr    = value.strptr    = NULL;
  77.  
  78.         VarGetNextValue( &name, &value );
  79.         if( !name.strptr ) break;
  80.  
  81.         if( value.strptr ){
  82.             printf( "%s: %s\n", name.strptr, value.strptr );
  83.  
  84.             DosFreeMem( value.strptr );
  85.         } else {
  86.             printf( "%s: (NULL)\n", name.strptr );
  87.         }
  88.  
  89.         DosFreeMem( name.strptr );
  90.  
  91.         ++count;
  92.     }
  93.  
  94.     printf( "\n---- End of variable pool\n" );
  95.  
  96.     /* Return # of variables that were dumped... */
  97.  
  98.     result->strlength = sprintf( result->strptr, "%d", count );
  99.  
  100.     return( 0 );
  101.   }
  102.  
  103. /*
  104.  * Function registration... before starting any REXX macros, we have
  105.  * to register our external functions with the interpreter.  We just
  106.  * keep a table of functions and register each one in sequence.
  107.  */
  108.  
  109. typedef struct {
  110.     PSZ name;
  111.     PFN function;
  112. } rxfunc_entry, *rxfunc_entry_p;
  113.  
  114. static rxfunc_entry REXXFuncs[] =
  115.   {
  116.     { "SETVAR",   (PFN) SetVar },
  117.     { "DUMPVARS", (PFN) DumpVars },
  118.     { NULL,             NULL  }
  119.   };
  120.  
  121. /*
  122.  * RegisterREXXFuncs -- Register each external REXX function in the
  123.  *                      table.  We use RexxRegisterFunctionExe to do
  124.  *                      the registration.
  125.  *
  126.  *  NOTE: RexxRegisterFunctionExe takes two parms, one is the name of
  127.  *        the function to register and the second is the address of the
  128.  *        function.  The function does not need to be exported.  After
  129.  *        registration the function is only available to the process
  130.  *        that registered it.
  131.  *
  132.  *        In contrast, RexxRegisterFunctionDll requires that the function
  133.  *        be exported from a DLL and is then made available to all REXX
  134.  *        programs in any process.
  135.  *
  136.  *        When the REXX interpreter looks for a function, it first looks
  137.  *        in the list of functions registered using RexxRegisterFunctionExe
  138.  *        before looking at those registered using RexxRegisterFunctionDll.
  139.  */
  140.  
  141. BOOL RegisterREXXFuncs( void )
  142.   {
  143.     rxfunc_entry_p ptr;
  144.  
  145.     for( ptr = REXXFuncs; ptr->name != NULL; ++ptr ){
  146.         RexxRegisterFunctionExe( ptr->name, ptr->function );
  147.     }
  148.  
  149.     return( TRUE );
  150.   }
  151.  
  152. /*
  153.  * DeregisterREXXFuncs -- Deregister each external REXX function.  Strictly
  154.  *                        speaking, this is not necessary since the
  155.  *                        registrations are freed once the process exits,
  156.  *                        but it's always good practice to clean up nicely.
  157.  *
  158.  *  NOTE: Never deregister external REXX functions that were registered
  159.  *        using RexxRegisterFunctionDll, as this swipes them out from
  160.  *        under the feet of ANY and ALL REXX programs, even those
  161.  *        currently running!  Deregistering functions that were
  162.  *        registered with RexxRegisterFunctionEXE is OK to do since they
  163.  *        were only registered as part of your process space.
  164.  */
  165.  
  166. void DeregisterREXXFuncs( void )
  167.   {
  168.     rxfunc_entry_p ptr;
  169.  
  170.     for( ptr = REXXFuncs; ptr->name != NULL; ++ptr ){
  171.         RexxDeregisterFunction( ptr->name );
  172.     }
  173.   }
  174.