home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / SAAREXX.ZIP / XMPMAIN.C < prev    next >
Text File  |  1991-08-03  |  13KB  |  306 lines

  1. /*******************************************************************/
  2. /*                                                                 */
  3. /* Module Name:        XMPMAIN                                     */
  4. /*                                                                 */
  5. /* Description:        Example program to invoke REXX/2.           */
  6. /*                                                                 */
  7. /* Function:           Creates an example REXX subcommand          */
  8. /*                     environment and utilizes an external        */
  9. /*                     function package.                           */
  10. /*                                                                 */
  11. /* Notes:                                                          */
  12. /*                                                                 */
  13. /*******************************************************************/
  14. /*   Modifications:                                                */
  15. /*                                                                 */
  16. /*    Date    Who        Reason                                    */
  17. /*  --------  ---  ----------------------------------------------  */
  18. /*  08/01/91  WDA  Original issue.                                 */
  19. /*                                                                 */
  20. /*******************************************************************/
  21.  
  22. #define INCL_DOS
  23. #include    <os2.h>
  24. #include    <stdlib.h>
  25. #include    <stdio.h>
  26. #include    <string.h>
  27.  
  28. #define INCL_RXFUNC
  29. #define INCL_RXSYSEXIT
  30. #define INCL_RXSUBCOM
  31. #include    <rexxsaa.h>
  32.  
  33. #include    "xmpfxdll.h"
  34. #include    "xmpscdll.h"
  35.  
  36.  
  37. /*******************************************************************/
  38. /*                                                                 */
  39. /* Internal prototypes follow.                                     */
  40. /*                                                                 */
  41. /*******************************************************************/
  42.  
  43. static SHORT RegisterFuncs   (VOID);
  44. static SHORT DeRegisterFuncs (VOID);
  45. SHORT APIENTRY XmpSysExitRXFNC (SHORT, SHORT, RXFNCCAL_PARM *);
  46. SHORT APIENTRY XmpSysExitRXHLT (SHORT, SHORT, RXFNCCAL_PARM *);
  47. SHORT APIENTRY CMDSubcom (PRXSTRING, PUSHORT, PRXSTRING);
  48.  
  49.  
  50. /*******************************************************************/
  51. /*                                                                 */
  52. /* Global variables and defines follow.                            */
  53. /*                                                                 */
  54. /*******************************************************************/
  55.  
  56. static PSZ   szXmpVersion = "0.02";
  57. static PSZ   szCompDate   = __DATE__;
  58.  
  59. static RXSTRING  rxResult;
  60. static CHAR      cResultBuffer[250];
  61. static RXSYSEXIT rxSysExits[] = {{"XFNC", RXFNC},
  62.                                  {NULL,   RXENDLST}};
  63.  
  64. static SCBLOCK   XmpSCBLOCK = {NULL, "XMP", "", "", 0.,
  65.                             (PFN) XmpSubcom, 0, 0, 0, 0};
  66.  
  67. static SCBLOCK   SysExitSCBLOCK = {NULL, "XFNC", "", "", 0.,
  68.                             (PFN) XmpSysExitRXFNC, 0, 0, 0, 0};
  69.  
  70. CHAR ExecName [CCHMAXPATH];
  71.  
  72.  
  73. /*******************************************************************/
  74. /*                                                                 */
  75. /* Function:            main()                                     */
  76. /*                                                                 */
  77. /* Description:         Main entry point to XmpMain from OS/2.     */
  78. /*                                                                 */
  79. /* Input Parms:         Name of OS/2 file containg REXX procedure. */
  80. /*                                                                 */
  81. /* Returns:             Error code                                 */
  82. /*                                                                 */
  83. /* References:          REXXSAA()                                  */
  84. /*                                                                 */
  85. /* Notes:                                                          */
  86. /*                                                                 */
  87. /*   This routine takes 1 parameter.  The form of the call is:     */
  88. /*                                                                 */
  89. /*   XMPMAIN REXXcmd[.cmd]                                         */
  90. /*                                                                 */
  91. /*******************************************************************/
  92.  
  93. SHORT _cdecl main (
  94.          SHORT     argc,               /* Number of arguments.     */
  95.          PSZ       argv[]) {           /* Function arguments.      */
  96.  
  97.    SHORT    sErrorCode = 0;
  98.    SHORT    sRexxErrorCode;
  99.    RXSTRING rxsArg;
  100.  
  101.    /* give banner information */
  102.    printf ("\nXmpMain - (c) W. David Ashley, 1991."
  103.            " All rights reserved.\n");
  104.    printf ("Example Version %s %s.\n", szXmpVersion,
  105.            szCompDate);
  106.    printf ("Written by W. David Ashley.\n\n");
  107.  
  108.    /* arg count valid? */
  109.    if (argc == 3) {
  110.       /* set up REXX result buffer */
  111.       rxResult.strlength = 250;
  112.       rxResult.strptr = cResultBuffer;
  113.  
  114.       /* set up XmpMain arguments */
  115.       rxsArg.strptr = argv[2];
  116.       rxsArg.strlength = strlen (argv[2]);
  117.  
  118.       /* set up XmpMain REXX filename */
  119.       strcpy (ExecName, argv[1]);
  120.       if (strlen (ExecName) > 4) {
  121.          if (stricmp (ExecName + strlen (ExecName) - 4, ".cmd") != 0)
  122.             strcat (ExecName, ".cmd");
  123.          }
  124.  
  125.       /* register our default environment with REXX */
  126.       sErrorCode = RxSubcomRegister (&XmpSCBLOCK);
  127.       if (sErrorCode == RXSUBCOM_DUP) {
  128.          RxSubcomDrop ("XMP", "");
  129.          sErrorCode = RxSubcomRegister (&SysExitSCBLOCK);
  130.          if (sErrorCode == RXSUBCOM_NOEMEM ||
  131.           sErrorCode == RXSUBCOM_BADTYPE) {
  132.             sErrorCode = -48;
  133.             printf ("Failure in system service.\n");
  134.             goto wrapup;
  135.             }
  136.          }
  137.  
  138.       /* register our system exit handler with REXX */
  139.       sErrorCode = RxExitRegister (&SysExitSCBLOCK);
  140.       if (sErrorCode == RXSUBCOM_DUP) {
  141.          RxExitDrop ("XFNC", "");
  142.          sErrorCode = RxExitRegister (&SysExitSCBLOCK);
  143.          if (sErrorCode == RXSUBCOM_NOEMEM ||
  144.           sErrorCode == RXSUBCOM_BADTYPE) {
  145.             sErrorCode = -48;
  146.             printf ("Failure in system service.\n");
  147.             goto wrapup;
  148.             }
  149.          }
  150.  
  151.       /* register our external functions with REXX */
  152.       if (RegisterFuncs() != RXFUNC_OK) {
  153.          sErrorCode = -48;
  154.          printf ("Failure in system service.\n");
  155.          goto wrapup;
  156.          }
  157.  
  158.       /* call REXX */
  159.       sRexxErrorCode = REXXSAA (1, &rxsArg, ExecName, NULL, "XMP",
  160.                                 RXCOMMAND, rxSysExits, &sErrorCode,
  161.                                 &rxResult);
  162.       printf ("The return code from REXX was %d.\n", sRexxErrorCode);
  163.       if (sRexxErrorCode != 0)
  164.          sErrorCode = sRexxErrorCode;
  165.  
  166.       /* drop our external functions with REXX */
  167.       DeRegisterFuncs();
  168.  
  169.       /* drop our system exit handlers */
  170.       RxExitDrop ("XFNC", "");
  171.  
  172.       /* drop our subcommand environment handler */
  173.       RxSubcomDrop ("XMP", "");
  174.       }
  175.  
  176.    else {
  177.       printf ("XmpMain invocation incorrect. "
  178.               "One parameter (a file name) required.\n");
  179.       sErrorCode = -43;
  180.       }
  181.  
  182.    wrapup:
  183.    printf ("XmpMain execution completed.\n");
  184.  
  185.    return (sErrorCode);
  186.    }
  187.  
  188.  
  189. /*******************************************************************/
  190. /*                                                                 */
  191. /* Function:            RegisterFuncs()                            */
  192. /*                                                                 */
  193. /* Description:         Register all our functions with REXX.      */
  194. /*                                                                 */
  195. /* Input Parms:         None.                                      */
  196. /*                                                                 */
  197. /* Returns:             Error code                                 */
  198. /*                                                                 */
  199. /* References:          RxFunctionRegister()                       */
  200. /*                                                                 */
  201. /* Notes:                                                          */
  202. /*                                                                 */
  203. /*   This routine takes 0 parameters.  The form of the call is:    */
  204. /*                                                                 */
  205. /*   retc = RegisterFuncs();                                       */
  206. /*                                                                 */
  207. /*******************************************************************/
  208.  
  209. static SHORT RegisterFuncs (VOID) {
  210.  
  211.    SHORT retc;
  212.  
  213.    retc = RxFunctionRegister ("REXDIR", NULL, (PSZ) RexDir,
  214.                               RXFUNC_CALLENTRY);
  215.    if (retc != RXFUNC_OK)
  216.       return (retc);
  217.    return (RXFUNC_OK);
  218.    }
  219.  
  220.  
  221. /*******************************************************************/
  222. /*                                                                 */
  223. /* Function:            DeRegisterFuncs()                          */
  224. /*                                                                 */
  225. /* Description:         Deregister all our functions with REXX.    */
  226. /*                                                                 */
  227. /* Input Parms:         None.                                      */
  228. /*                                                                 */
  229. /* Returns:             Error code                                 */
  230. /*                                                                 */
  231. /* References:          RxFunctionDeregister()                     */
  232. /*                                                                 */
  233. /* Notes:                                                          */
  234. /*                                                                 */
  235. /*   This routine takes 0 parameters.  The form of the call is:    */
  236. /*                                                                 */
  237. /*   retc = DeRegisterFuncs();                                     */
  238. /*                                                                 */
  239. /*******************************************************************/
  240.  
  241. static SHORT DeRegisterFuncs (VOID) {
  242.  
  243.    SHORT retc = RXFUNC_OK;
  244.  
  245.    retc |= RxFunctionDeregister ("REXDIR");
  246.    return (retc);
  247.    }
  248.  
  249.  
  250. /*******************************************************************/
  251. /*                                                                 */
  252. /* Function:            XmpSysExitRXFNC()                          */
  253. /*                                                                 */
  254. /* Description:         System exit for REXX external function     */
  255. /*                      calls. This exit only checks to see if the */
  256. /*                      function parameters are correct. REXX will */
  257. /*                      actually attempt to execute the function   */
  258. /*                      only if this exit ok's the function parms. */
  259. /*                                                                 */
  260. /* Input Parms:         Function code                              */
  261. /*                      Subfunction code                           */
  262. /*                      Control block                              */
  263. /*                                                                 */
  264. /* Returns:             Error code 0 = function parameter error    */
  265. /*                                 1 = REXX should execute function*/
  266. /*                                                                 */
  267. /* References:          rx2i()                                     */
  268. /*                                                                 */
  269. /* Notes:                                                          */
  270. /*                                                                 */
  271. /*     None.                                                       */
  272. /*                                                                 */
  273. /*******************************************************************/
  274.  
  275. SHORT APIENTRY XmpSysExitRXFNC (
  276.          SHORT     func,               /* Function code            */
  277.          SHORT     subfunc,            /* Subfunction code         */
  278.          RXFNCCAL_PARM * rxfnccal) {   /* Parm control block       */
  279.  
  280.    SHORT sTemp;
  281.  
  282.    if (func == RXFNC) {
  283.       switch (subfunc) {
  284.          case (RXFNCCAL):
  285.             if (memicmp ("REXDIR", rxfnccal -> rxfnc_name,
  286.              rxfnccal -> rxfnc_namel) == 0) {
  287.                if (rxfnccal -> rxfnc_argc < 2 ||
  288.                 rxfnccal -> rxfnc_argc > 3) {
  289.                   rxfnccal -> rxfnc_flags.rxfferr = 1;
  290.                   return (0);
  291.                   }
  292.                }
  293.  
  294.             return (1);
  295.  
  296.          default:
  297.             break;
  298.          }
  299.       }
  300.  
  301.    /* If we get here either the function name was unknown or we */
  302.    /* did find it and successfully checked the function parms.  */
  303.    /* REXX can now attempt to find and execute the function.    */
  304.    return (1);
  305.    }
  306.