home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / rxfiles.zip / rxshv.c < prev    next >
C/C++ Source or Header  |  1997-08-10  |  8KB  |  185 lines

  1. /* -------------------------------------------------------------------- *
  2.  *                                                                      *
  3.  * PC DOS 7 REXX Shared Variable Interface access example (C version):  *
  4.  *                                                                      *
  5.  * Make RXSHV.EXE from RXSHV.C, rename RXSHV.EXE to RXSHV.RX, put it in *
  6.  * a directory, where REXX will find it (e.g. PATH). In a REXX script   *
  7.  * call RXSHV to examine and modify SHV interactively.                  *
  8.  *                                                                      *
  9.  * -------------------------------------------------------------------- */
  10.  
  11. #include <string.h>
  12. #include <conio.h>
  13. #include <dos.h>
  14. #include <stdlib.h>
  15.  
  16. #define RXSHV_SET       0x00    /* set variable from given value        */
  17. #define RXSHV_FETCH     0x01    /* copy value of variable to buffer     */
  18. #define RXSHV_DROPV     0x02    /* drop variable name                   */
  19. #define RXSHV_SYSET     0x03    /* symbolic name set variable           */
  20. #define RXSHV_SYFET     0x04    /* symbolic name fetch variable         */
  21. #define RXSHV_SYDRO     0x05    /* symbolic name drop variable          */
  22. #define RXSHV_NEXTV     0x06    /* get next variable                    */
  23. #define RXSHV_PRIV      0x07    /* fetch private information            */
  24. #define RXSHV_EXIT      0x08    /* set exit code (not in personal REXX) */
  25.  
  26. #define RXSHV_OK        0x00    /* execution was ok                     */
  27. #define RXSHV_NEWV      0x01    /* variable did not exist               */
  28. #define RXSHV_LVAR      0x02    /* last variable transferred            */
  29. #define RXSHV_TRUNC     0x04    /* truncation occurred during "fetch"   */
  30. #define RXSHV_BADN      0x08    /* invalid variable name                */
  31. #define RXSHV_MEMFL     0x10    /* memory resources exhausted           */
  32. #define RXSHV_BADF      0x80    /* invalid function code (shvcode)      */
  33.  
  34. #ifndef MK_FP
  35. #define MK_FP( s, o ) (((_segment)(s)):>((void _based(void) *)(o)))
  36. #endif
  37.  
  38. #pragma pack( 1 )
  39.  
  40. struct RXSTR                            /****** REXX string format ******/
  41. {       unsigned int  StrLen;           /* string length                */
  42.         char _far *   StrPtr;           /* string pointer               */
  43. };
  44.  
  45. static struct RX_PSP                    /**** REXX PSP informations: ****/
  46. {       char               Fill1[ 84 ]; /*                              */
  47.         char               Rexx[ 4 ];   /* signature "REXX" at PSP:54h  */
  48.         void ( _far *RXcall )( struct SHVBLOCK _far * );        /* call */
  49.         char               Fill2[ 38 ]; /*                              */
  50.         unsigned int       RXargc;      /* Counter of REXX arguments    */
  51.         struct RXSTR _far *RXargv;      /* Pointer to REXX arg. strings */
  52.         char _far *        RXresult;    /* Pointer to RESULT, 256 bytes */
  53. #ifdef  __TINY__
  54. }      *RX_psp;
  55. #else
  56. } _far *RX_psp;
  57. #endif
  58.  
  59. struct SHVBLOCK                         /****** SHV request block: ******/
  60. {       struct SHVBLOCK _far *ShvNext;  /* ptr to next shvblock or NULL */
  61.         struct RXSTR          ShvNam;   /* variable name                */
  62.         struct RXSTR          ShvVal;   /* variable value               */
  63.         unsigned int          ShvNamL;  /* returned length of name      */
  64.         unsigned int          ShvValL;  /* returned length of value     */
  65.         unsigned char         ShvCode;  /* shv function code            */
  66.         unsigned char         ShvRet;   /* shv return code              */
  67. };
  68.  
  69. static struct SHVBLOCK sb;
  70.  
  71. static char shvcode[  4 ];
  72. static char shvnam[ 256 ];
  73. static char shvval[ 256 ];
  74. static char shvkeys[] = "SFDsfdnpx";
  75.  
  76. /* -------------------------------------------------------------------- */
  77.  
  78. #pragma intrinsic( memcmp, memcpy, memset, strcpy, strlen )
  79. #pragma check_stack( off )
  80.  
  81. void _cdecl _nullcheck( void ) {}       /* no 0-pointer assignment test */
  82. void _cdecl _setenvp( void )   {}       /* no operations on environment */
  83. void _cdecl _setargv( void )   {}       /* no argc and argv processing  */
  84.  
  85. /* -------------------------------------------------------------------- */
  86.  
  87. int cgetb( char *response, unsigned size )
  88. {
  89.         memset( response, 0, size );
  90.         response[ 0 ] = (unsigned char)( size - 2 );
  91.  
  92.         return strlen( strcpy( response, cgets( response )));
  93. }
  94. /* -------------------------------------------------------------------- */
  95.  
  96. int main( void )
  97. {
  98.         unsigned i, j;
  99.         char alarm = 0;
  100.  
  101. #ifdef  __TINY__
  102.         RX_psp = 0;
  103.  
  104.         if ( memcmp( RX_psp->Rexx, "REXX", 4 ))
  105. #else
  106.         RX_psp = MK_FP( _psp, 0 );
  107.  
  108.         if ( _fmemcmp( RX_psp->Rexx, (void _far *) "REXX", 4 ))
  109. #endif
  110.         {       cprintf( "REXX not found at %4.4X:0054\r\n", _psp );
  111.                 return 1;
  112.         }
  113.         /* ------------------------------------------------------------ */
  114.  
  115.         cprintf("Rexx Shared Variable Interface Test\r\n"
  116.                 "s symbolic set     S set   variable\r\n"
  117.                 "f symbolic fetch   F fetch variable\r\n"
  118.                 "d symbolic drop    D drop  variable\r\n"
  119.                 "n next  var.  (cycling through SHV)\r\n"
  120.                 "p fetch private info (e.g. VERSION)\r\n"
  121.                 "x set REXX exit code (doesn't work)\r\n" );
  122.  
  123.         for ( ;; )
  124.         {
  125.                 if ( alarm ) putch( '\a' );
  126.                 memset( &sb, 0, sizeof sb );
  127.  
  128.                 alarm = 1;
  129.                 cputs( "\r\nSHVCODE: " );
  130.                 if ( ! cgetb( shvcode, sizeof shvcode )) return 0;
  131.  
  132.                 for ( i = 0; i < sizeof shvkeys; ++i )
  133.                         if ( shvkeys[ i ] == *shvcode ) break;
  134.  
  135.                 if ( sizeof shvkeys <= i ) continue;
  136.                 sb.ShvCode = (unsigned char) i;
  137.                 alarm = 0;
  138.  
  139.                 if ( *shvcode != 'n' )
  140.                 {
  141.                         cputs( "\r\nSHVNAM:  " );
  142.                         sb.ShvNam.StrLen = cgetb( shvnam, sizeof shvnam );
  143.                 }
  144.                 else    sb.ShvNam.StrLen = sizeof shvnam;
  145.  
  146.                 if ( *shvcode == 'S' || *shvcode == 's'
  147.                 ||   *shvcode == 'x' )
  148.                 {
  149.                         cputs( "\r\nSHVVAL:  " );
  150.                         sb.ShvVal.StrLen = cgetb( shvval, sizeof shvval );
  151.                 }
  152.                 else    sb.ShvVal.StrLen = sizeof shvval;
  153.  
  154.                 sb.ShvNam.StrPtr = (char _far *) shvnam;
  155.                 sb.ShvVal.StrPtr = (char _far *) shvval;
  156.  
  157.                 RX_psp->RXcall( &sb );          /* REXX call Shv entry  */
  158.  
  159.                 cprintf("\r\nSHVRET:  %2.2X  (0 ok, 1 new, 2 last, "
  160.                         "4 trunc, 8 inv, 10 no mem, 80 bad)"
  161.                         "\r\nSHVNAML: %d   SHVVALL: %d\r\n",
  162.                         (unsigned) sb.ShvRet, sb.ShvNamL, sb.ShvValL );
  163.  
  164.                 if ( sb.ShvNamL )
  165.                 {       cputs( "SHVNAM:  " );
  166.  
  167.                         j = sb.ShvNamL;
  168.                         if ( sizeof shvnam <= j ) j = sizeof shvnam;
  169.  
  170.                         for( i = 0; i < j; ++i )
  171.                                 putch( shvnam[ i ] );
  172.                         cputs( "\r\n" );
  173.                 }
  174.  
  175.                 if ( sb.ShvValL )
  176.                 {       cputs( "SHVVAL:  " );
  177.  
  178.                         j = sb.ShvValL;
  179.                         if ( sizeof shvval <= j ) j = sizeof shvval;
  180.  
  181.                         for( i = 0; i < j; ++i )
  182.                                 putch( shvval[ i ] );
  183.                         cputs( "\r\n" );
  184. }       }       }
  185.