home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ool.zip / OOL / source / xrexx.CPP < prev    next >
C/C++ Source or Header  |  1997-04-05  |  8KB  |  251 lines

  1. #include "xrexx.h"
  2. #include "xrexxex.h"
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. /*@ XRexxInterface :: XRexxInterface(char *n, PFN proc)
  7. @group constructors/destructors
  8. @remarks Creates an interface to REXX. Therefore a C-function of your application (EXE) will be registered.
  9. The function must have following format:<P>
  10. REXXINTERFACE Handler(PRXSTRING commandString, PUSHORT flags, PRXSTRING returnString)<P>
  11. Commands which are not known in a rexx-script running in this interface
  12. will be posted to the handler.
  13. @parameters char * name        the symbolic name of the interface<BR>
  14.               PFN proc            the adress of your function
  15. @exceptions    If the method fails an exception of the type XRexxException is thrown.
  16. */
  17. XRexxInterface :: XRexxInterface(char *n, PFN proc)
  18. {
  19.     ULONG user[2];
  20.  
  21.     user[0] = (ULONG) this;
  22.     name = n;
  23.     ULONG rc = RexxRegisterSubcomExe( (char*) name, proc, (PUCHAR) & user);
  24.  
  25.     if (rc)
  26.         OOLRexxThrow("cannot install XRexxInterfaceer", rc);
  27. }
  28.  
  29.  
  30. /*@ XRexxInterface :: XRexxInterface(char *n, char *dllName, char *proc)
  31. @group constructors/destructors
  32. @remarks Creates an interface to REXX. Therefore a C-function of you application (DLL) will be registered.
  33. The function must have following format:<P>
  34. REXXINTERFACE Handler(PRXSTRING commandString, PUSHORT flags, PRXSTRING returnString)<P>
  35. Commands which are not known in a rexx-script running in this interface
  36. will be posted to the handler.
  37. @parameters <t '°' c=2>
  38.                 °char * name        °the symbolic name of the interface
  39.               °char * dllName    °the name of the DLL
  40.               °char * proc        °the name of your function
  41.                 </t>
  42. @exceptions    If the method fails an exception of the type XRexxException is thrown.
  43. */
  44. XRexxInterface :: XRexxInterface(char *n, char *dllName, char *proc)
  45. {
  46.     ULONG user[2];
  47.  
  48.     user[0] = (ULONG) this;
  49.     name = n;
  50.     dll = dllName;
  51.     ULONG rc = RexxRegisterSubcomDll( (char*) name, dllName, proc, (PUCHAR) & user, RXSUBCOM_NONDROP);
  52.  
  53.     if (rc)
  54.         OOLRexxThrow("cannot install XRexxInterface", rc);
  55. }
  56.  
  57.  
  58. XRexxInterface :: ~XRexxInterface()
  59. {
  60.     ULONG rc = RexxDeregisterSubcom( (char*) name, (char*) dll);
  61.  
  62.     if (rc)
  63.         OOLRexxThrow("cannot remove XRexxInterface", rc);
  64. }
  65.  
  66.  
  67. /*@ XRexxInterface::GetHandler(char *handlerName, char *dllName)
  68. @group misc
  69. @remarks Returns a pointer to a XRexxInterface
  70. @parameters char * handlerName     the name of the rexx interface used in the constructor<BR>
  71.               char *    dllName            name of the DLL if used (default is NULL)
  72. @returns      XRexxInterface * pointer        pointer to an interface (NULL if no interface found)
  73. */
  74. XRexxInterface * XRexxInterface::GetHandler(char *handlerName, char *dllName)
  75. {
  76.     ULONG user[2];
  77.     unsigned short flag;
  78.  
  79.     if (RexxQuerySubcom(handlerName, dllName, &flag, (PUCHAR) & user) == 0)
  80.         return (XRexxInterface *) user[0];
  81.     else
  82.         return NULL;
  83. }
  84.  
  85.  
  86. /*@ XRexxInterface::Execute(char *commandFile, LONG type, XString * resultBuffer, SHORT * returnCode, XString * argList)
  87. @group misc
  88. @remarks Run a REXX-script
  89. @parameters <t '°' c=2>
  90.                 °char * commandFile        °name of a file which contains the script
  91.               °LONG type                    °type of command (see rexx program reference)
  92.               °XString * result        °buffer for result
  93.               °SHORT returnCode        °buffer for returncode
  94.               °XString * args            °string which contains the arguments for the script
  95.                 </t>
  96. @returns      LONG result                return-code from the operating system
  97. */
  98. LONG XRexxInterface::Execute(char *commandFile, LONG type, XString * resultBuffer, SHORT * returnCode, XString * argList)
  99. {
  100.     ULONG fi = 0, old = 0;
  101.  
  102.     argList->Strip();
  103.  
  104.     RXSTRING *args = NULL;
  105.     RXSTRING res;
  106.     SHORT argCount = 0;
  107.     char retBuffer[250];
  108.  
  109.     MAKERXSTRING(res, retBuffer, 250);
  110.  
  111.     if (type != RXCOMMAND)
  112.     {
  113.         *argList += " ";
  114.         while (argList->Find(fi, " ", fi))
  115.         {
  116.             XString buffer = argList->Mid(old, fi - old);
  117.  
  118.             argCount += 1;
  119.             args = (RXSTRING *) realloc(args, argCount * sizeof(RXSTRING));
  120.             MAKERXSTRING(args[argCount - 1], (char *) buffer, buffer.GetLength());
  121.             fi++;
  122.             old = fi;
  123.         }
  124.     }
  125.     else
  126.     {
  127.         argCount = 1;
  128.         args = (RXSTRING *) malloc(sizeof(RXSTRING));
  129.         MAKERXSTRING(args[argCount - 1], (char *) *argList, argList->GetLength());
  130.     }
  131.     LONG rc = RexxStart(argCount, args, commandFile, NULL, (char*) name, type, NULL, returnCode, &res);
  132.  
  133.     free(args);
  134.  
  135.     if (res.strlength)
  136.     {
  137.         memcpy(resultBuffer->GetBuffer(res.strlength + 1), res.strptr, res.strlength);
  138.         resultBuffer->ReleaseBuffer(res.strlength);
  139.     }
  140.  
  141.     return rc;
  142. }
  143.  
  144.  
  145. /*@ XRexxInterface::SetVar(char *name, char *value)
  146. @group variables
  147. @remarks Set a variable for a running rexx-script
  148. @parameters char * name        name of the variable<BR>
  149.               char *    value        value of the variable
  150. @returns      LONG result                return-code from the operating system
  151. */
  152. LONG XRexxInterface::SetVar(char *name, char *value)
  153. {
  154.     SHVBLOCK block;
  155.  
  156.     memset(&block, 0, sizeof(block));
  157.     block.shvcode = RXSHV_SET;
  158.  
  159.     MAKERXSTRING(block.shvname, name, strlen(name));
  160.     MAKERXSTRING(block.shvvalue, value, strlen(value));
  161.     block.shvvaluelen = strlen(value);
  162.     RexxVariablePool(&block);
  163.     return block.shvret;
  164. }
  165.  
  166.  
  167. /*@ XRexxInterface::GetVar(char *name, XString * value)
  168. @group variables
  169. @remarks Query a variable for a running rexx-script
  170. @parameters char * name        name of the variable<BR>
  171.               XString *    value        buffer for the value of the variable
  172. @returns      LONG result                return-code from the operating system
  173. */
  174. LONG XRexxInterface::GetVar(char *name, XString * value)
  175. {
  176.     SHVBLOCK block;
  177.     char buffer[250];
  178.  
  179.     memset(&block, 0, sizeof(block));
  180.     block.shvcode = RXSHV_FETCH;
  181.     MAKERXSTRING(block.shvname, name, strlen(name));
  182.     MAKERXSTRING(block.shvvalue, buffer, 250);
  183.     block.shvvaluelen = 250;
  184.     RexxVariablePool(&block);
  185. /*
  186.     if( block.shvret & RXSHV_OK)
  187.     {
  188. */
  189.     memcpy(value->GetBuffer(block.shvvalue.strlength + 1), block.shvvalue.strptr, block.shvvalue.strlength);
  190.     value->ReleaseBuffer(block.shvvalue.strlength);
  191. //    }
  192.     return block.shvret;
  193. }
  194.  
  195.  
  196.  
  197. /*@ XRexxInterface::DeregisterFunction(char *name)
  198. @group register/deregister functions
  199. @remarks Deregister a function
  200. @parameters char * name        symbolic name of the function
  201. @returns      LONG result                return-code from the operating system
  202. */
  203. LONG XRexxInterface::DeregisterFunction(char *name)
  204. {
  205.     return RexxDeregisterFunction(name);
  206. }
  207.  
  208.  
  209. /*@ XRexxInterface::IsFunctionRegistered(char *name)
  210. @group register/deregister functions
  211. @remarks Query if a function is registered
  212. @parameters char * name        symbolic name of the function
  213. @returns      BOOL result
  214. */
  215. BOOL XRexxInterface::IsFunctionRegistered(char *name)
  216. {
  217.     return (BOOL) RexxQueryFunction(name);
  218. }
  219.  
  220.  
  221. /*@ XRexxInterface::RegisterFunction(char *name, PFN entry)
  222. @remarks Register a C-function of your application (EXE) so that it can be called from a rexx-script. The
  223. function must have the following format:<P>
  224. REXXINTERFACE MyFunc( PUCHAR name, ULONG argc, PRXSTRING argv, PSZ queue, PRXSTRING ret)
  225. @parameters char * name        symbolic name of the function which must be used in the rexx-script<BR>
  226.               PFN proc            adress of the function
  227. @returns      LONG result                return-code from the operating system
  228. */
  229. LONG XRexxInterface::RegisterFunction(char *name, PFN entry)
  230. {
  231.     return RexxRegisterFunctionExe(name, entry);
  232. }
  233.  
  234.  
  235. /*@ XRexxInterface::RegisterFunction(char *name, char *dllName, char *routine)
  236. @group register/deregister functions
  237. @remarks Register a C-function of your application (DLL) so that it can be called from a rexx-script. The
  238. function must have the following format:<P>
  239. REXXINTERFACE MyFunc( PUCHAR name, ULONG argc, PRXSTRING argv, PSZ queue, PRXSTRING ret)
  240. @parameters <t '°' c=2>
  241.                 °char * name        °symbolic name of the function which must be used in the rexx-script
  242.               °char * dllName    °name of the dll
  243.               °char * proc        °real name of the function
  244.                 </t>
  245. @returns      LONG result                return-code from the operating system
  246. */
  247. LONG XRexxInterface::RegisterFunction(char *name, char *dllName, char *routine)
  248. {
  249.     return RexxRegisterFunctionDll(name, dllName, routine);
  250. }
  251.