home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / vxtech07.zip / RXAWAR / FUNCS / C / LOWER.C next >
C/C++ Source or Header  |  1994-07-16  |  4KB  |  130 lines

  1. /*
  2.  * lower.c -- Defines a simple REXX function which will translate a string
  3.  *            into lowercase.
  4.  */
  5.  
  6. #define INCL_REXXSAA
  7.  
  8. #include <os2.h>
  9. #include <rexxsaa.h>
  10.  
  11. #include <ctype.h>
  12.  
  13. #include "lower.h"
  14.  
  15. /*
  16.  * Lower -- A REXX external function that converts its single argument
  17.  *          into lowercase.
  18.  */
  19.  
  20. RexxFunctionHandler Lower;
  21.  
  22. ULONG Lower( PUCHAR funcname, ULONG numargs, PRXSTRING args,
  23.              PSZ queuename, PRXSTRING result )
  24.   {
  25.     PVOID mem = NULL;
  26.     ULONG i;
  27.  
  28.     /* Get rid of compiler warnings... */
  29.  
  30.     funcname  = funcname;
  31.     queuename = queuename;
  32.  
  33.     /* Need exactly one argument... */
  34.  
  35.     if( numargs != 1 ) return( 40 );
  36.  
  37.     /* Allocate new buffer if necessary... don't use malloc or any
  38.        other library routine -- use DosAllocMem only */
  39.  
  40.     if( args[0].strlength > result->strlength ){
  41.         if( DosAllocMem( &mem, args[0].strlength,
  42.                          PAG_COMMIT | PAG_READ | PAG_WRITE ) != 0 ){
  43.             return( 40 );
  44.         }
  45.  
  46.         result->strptr = mem;
  47.     }
  48.  
  49.     /* Copy and convert... */
  50.  
  51.     for( i = 0; i < args[0].strlength; ++i ){
  52.         result->strptr[i] = tolower( args[0].strptr[i] );
  53.     }
  54.  
  55.     result->strlength = args[0].strlength;
  56.  
  57.     return( 0 );
  58.   }
  59.  
  60. /*
  61.  * Function registration... before starting any REXX macros, we have
  62.  * to register our external functions with the interpreter.  We just
  63.  * keep a table of functions and register each one in sequence.
  64.  */
  65.  
  66. typedef struct {
  67.     PSZ name;
  68.     PFN function;
  69. } rxfunc_entry, *rxfunc_entry_p;
  70.  
  71. static rxfunc_entry REXXFuncs[] =
  72.   {
  73.     { "LOWER", (PFN) Lower },
  74.     { NULL,    NULL  }
  75.   };
  76.  
  77. /*
  78.  * RegisterREXXFuncs -- Register each external REXX function in the
  79.  *                      table.  We use RexxRegisterFunctionExe to do
  80.  *                      the registration.
  81.  *
  82.  *  NOTE: RexxRegisterFunctionExe takes two parms, one is the name of
  83.  *        the function to register and the second is the address of the
  84.  *        function.  The function does not need to be exported.  After
  85.  *        registration the function is only available to the process
  86.  *        that registered it.
  87.  *
  88.  *        In contrast, RexxRegisterFunctionDll requires that the function
  89.  *        be exported from a DLL and is then made available to all REXX
  90.  *        programs in any process.
  91.  *
  92.  *        When the REXX interpreter looks for a function, it first looks
  93.  *        in the list of functions registered using RexxRegisterFunctionExe
  94.  *        before looking at those registered using RexxRegisterFunctionDll.
  95.  */
  96.  
  97. BOOL RegisterREXXFuncs( void )
  98.   {
  99.     rxfunc_entry_p ptr;
  100.  
  101.     for( ptr = REXXFuncs; ptr->name != NULL; ++ptr ){
  102.         RexxRegisterFunctionExe( ptr->name, ptr->function );
  103.     }
  104.  
  105.     return( TRUE );
  106.   }
  107.  
  108. /*
  109.  * DeregisterREXXFuncs -- Deregister each external REXX function.  Strictly
  110.  *                        speaking, this is not necessary since the
  111.  *                        registrations are freed once the process exits,
  112.  *                        but it's always good practice to clean up nicely.
  113.  *
  114.  *  NOTE: Never deregister external REXX functions that were registered
  115.  *        using RexxRegisterFunctionDll, as this swipes them out from
  116.  *        under the feet of ANY and ALL REXX programs, even those
  117.  *        currently running!  Deregistering functions that were
  118.  *        registered with RexxRegisterFunctionEXE is OK to do since they
  119.  *        were only registered as part of your process space.
  120.  */
  121.  
  122. void DeregisterREXXFuncs( void )
  123.   {
  124.     rxfunc_entry_p ptr;
  125.  
  126.     for( ptr = REXXFuncs; ptr->name != NULL; ++ptr ){
  127.         RexxDeregisterFunction( ptr->name );
  128.     }
  129.   }
  130.