home *** CD-ROM | disk | FTP | other *** search
/ The Developer Connection…ice Driver Kit for OS/2 3 / DEV3-D1.ISO / devtools / os2tk21j / rexx / samples / rxmacdll / macro.c__ / macro.c
Encoding:
C/C++ Source or Header  |  1993-03-12  |  13.4 KB  |  235 lines

  1. /*static char *SCCSID = "@(#)macro.c    6.1 91/09/20";*/
  2.  
  3. /*********************************************************************/
  4.  
  5. /*                                                                   */
  6.  
  7. /*  File Name:          MACRO.C                                      */
  8.  
  9. /*                                                                   */
  10.  
  11. /*  Description:        Provides access to the macrospace interface  */
  12.  
  13. /*                      functions for REXXSAA from REXX programs     */
  14.  
  15. /*                      via the Available Function Table, which is   */
  16.  
  17. /*                      composed of external functions for the REXX  */
  18.  
  19. /*                      interpreter written in languages other than  */
  20.  
  21. /*                      REXX.                                        */
  22.  
  23. /*                                                                   */
  24.  
  25. /*  Entry Points:       MacroLoad  - load a file or library          */
  26.  
  27. /*                      MacroSave  - save macrospace to a library    */
  28.  
  29. /*                      MacroErase - clear the macrospace            */
  30.  
  31. /*                      MacroQuery - check for a function            */
  32.  
  33. /*                                                                   */
  34.  
  35. /*  Notes:              Each of the entry points to this library     */
  36.  
  37. /*                      conforms to the external function interface  */
  38.  
  39. /*                      format for REXXSAA, i.e., each function has  */
  40.  
  41. /*                      five (5) parameters: the name the function   */
  42.  
  43. /*                      was called as, the argument count, the list  */
  44.  
  45. /*                      of argument strings, the current active      */
  46.  
  47. /*                      external data queue, and the address of the  */
  48.  
  49. /*                      RXSTRING structure to contain any return     */
  50.  
  51. /*                      information.                                 */
  52.  
  53. /*                                                                   */
  54.  
  55. /*                      For each function, the returned value is set */
  56.  
  57. /*                      to the return code from the macrospace API   */
  58.  
  59. /*                      function call which it performs; therefore,  */
  60.  
  61. /*                      to properly use these functions a program    */
  62.  
  63. /*                      should compare the returned value with '0'   */
  64.  
  65. /*                      to check for successful completion.          */
  66.  
  67. /*                                                                   */
  68.  
  69. /*  Return Codes:        0 == Function completed without error       */
  70.  
  71. /*                      -1 == Illegal call to function               */
  72.  
  73. /*                                                                   */
  74.  
  75. /*  Usage Notes:        Since each function has its own entry point  */
  76.  
  77. /*                      in the library, they can be registered in    */
  78.  
  79. /*                      the Available Function Table by any name     */
  80.  
  81. /*                      which is convenient for a program.  The      */
  82.  
  83. /*                      function name parameter passed to each       */
  84.  
  85. /*                      routine by the interpreter is ignored.       */
  86.  
  87. /*                                                                   */
  88.  
  89. /*********************************************************************/
  90.  
  91.  
  92.  
  93. #define INCL_RXMACRO              /* include macrospace info         */
  94.  
  95. #define INCL_RXFUNC               /* include external function  info */
  96.  
  97. #include <rexxsaa.h>              /* REXXSAA header information      */
  98.  
  99. #include <string.h>
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109. /*********************************************************************/
  110.  
  111. /* MacroLoad                                                         */
  112.  
  113. /* This function will do one of two functions:                       */
  114.  
  115. /*   When called with one argument:                                  */
  116.  
  117. /*     The argument must be the name of a file created by            */
  118.  
  119. /*     RexxSaveMacroSpace on the current release of OS/2.            */
  120.  
  121. /*     All macros saved in the file will be loaded.                  */
  122.  
  123. /*                                                                   */
  124.  
  125. /*   When called with three arguments:                               */
  126.  
  127. /*     The first argument must be the name of a REXX program file.   */
  128.  
  129. /*     The second must be "BEFORE" or "AFTER" (upper/lower case does */
  130.  
  131. /*       not matter)                                                 */
  132.  
  133. /*     The third is the name that the REXX program will be called by.*/
  134.  
  135. /*                                                                   */
  136.  
  137. /* If called with a different number of arguments, it will raise an  */
  138.  
  139. /*   error.                                                          */
  140.  
  141. /*                                                                   */
  142.  
  143. /* The return value of the function is the return code from          */
  144.  
  145. /*   calling the RexxLoadMacroSpace or RexxAddMacro API.             */
  146.  
  147. /*                                                                   */
  148.  
  149. /*********************************************************************/
  150.  
  151.  
  152.  
  153. RexxFunctionHandler MacroLoad;
  154.  
  155. RexxFunctionHandler MacroSave;
  156.  
  157. RexxFunctionHandler MacroErase;
  158.  
  159. RexxFunctionHandler MacroQuery;
  160.  
  161.  
  162.  
  163. /*$PE*/
  164.  
  165. /*$FD.MacroLoad*/
  166.  
  167.  
  168.  
  169. ULONG APIENTRY MacroLoad(
  170.  
  171.    PUCHAR    func,
  172.  
  173.    ULONG     ac,
  174.  
  175.    PRXSTRING av,
  176.  
  177.    PSZ       que,
  178.  
  179.    PRXSTRING ret)
  180.  
  181.   {
  182.  
  183.   APIRET rc = -1;                      /* return code from function  */
  184.  
  185.   ULONG  srch_ord = -1;                /* search order position flag */
  186.  
  187.                                        /*                            */
  188.  
  189.   if (ac == 1) {                       /* 1 arg == library load      */
  190.  
  191.     rc = RexxLoadMacroSpace(0L,        /* set return code from call  */
  192.  
  193.                      (PSZ *)0,         /*   to RexxLoadMacroSpace    */
  194.  
  195.                      av[0].strptr);    /*                            */
  196.  
  197.     sprintf(ret->strptr, "%d", (int)rc);
  198.  
  199.     ret->strlength=strlen(ret->strptr);/*                            */
  200.  
  201.     rc = 0;                            /* no errors in call          */
  202.  
  203.     }                                  /*                            */
  204.  
  205.  
  206.  
  207.   else if (ac == 3) {                  /* 3 args == function load    */
  208.  
  209.     if (!stricmp(av[1].strptr,"BEFORE"))   /* set 'before' search    */
  210.  
  211.       srch_ord = RXMACRO_SEARCH_BEFORE;/*                            */
  212.  
  213.     else if(!stricmp(av[1].strptr,"AFTER"))/* set 'after' search     */
  214.  
  215.       srch_ord = RXMACRO_SEARCH_AFTER; /*                            */
  216.  
  217.  
  218.  
  219.     rc = RexxAddMacro(av[2].strptr,    /* set return code from call  */
  220.  
  221.                        av[0].strptr,   /*   to RexxAddMacro          */
  222.  
  223.                        srch_ord),      /*                            */
  224.  
  225.     sprintf(ret->strptr, "%d", (int)rc);
  226.  
  227.     ret->strlength=strlen(ret->strptr);/*                            */
  228.  
  229.     rc = 0;                            /* no errors in call          */
  230.  
  231.     }                                  /*                            */
  232.  
  233.  
  234.  
  235.   /* We usually return 0 to indicate our processing was successful   */
  236.  
  237.   /* and we have returned a valid result.  If we return non-zero,    */
  238.  
  239.   /* then REXX will raise error 40, "invalid call to routine".       */
  240.  
  241.   /* For all the functions in this program, we only raise an         */
  242.  
  243.   /* error if we are called with an incorrect number of arguments.   */
  244.  
  245.   return rc;                           /*                            */
  246.  
  247.   }                                    /*                            */
  248.  
  249.  
  250.  
  251.  
  252.  
  253. /*********************************************************************/
  254.  
  255. /* MacroSave                                                         */
  256.  
  257. /* This function must be called with one argument.                   */
  258.  
  259. /*   The argument must be a valid OS/2 file name.                    */
  260.  
  261. /*   This function will call RexxSaveMacroSpace to create a file     */
  262.  
  263. /*   containing all the REXX programs currently loaded in the        */
  264.  
  265. /*   macrospace.  NOTE:  This file may have to be re-created after   */
  266.  
  267. /*   applying service or upgrading to a new release of OS/2.         */
  268.  
  269. /*                                                                   */
  270.  
  271. /* If called with a different number of arguments, it will raise an  */
  272.  
  273. /*   error.                                                          */
  274.  
  275. /*                                                                   */
  276.  
  277. /* The return value of the function is the return code from          */
  278.  
  279. /*   calling the RexxSaveMacroSpace API.                             */
  280.  
  281. /*                                                                   */
  282.  
  283. /*********************************************************************/
  284.  
  285. /*$PE*/
  286.  
  287. /*$FD.MacroSave*/
  288.  
  289. ULONG APIENTRY MacroSave(
  290.  
  291.    PUCHAR    func,
  292.  
  293.    ULONG     ac,
  294.  
  295.    PRXSTRING av,
  296.  
  297.    PSZ       que,
  298.  
  299.    PRXSTRING ret)
  300.  
  301.   {
  302.  
  303.   APIRET rc = -1;                      /* return code from function  */
  304.  
  305.                                        /*                            */
  306.  
  307.   if (ac == 1) {                       /* need only one arg          */
  308.  
  309.     rc = RexxSaveMacroSpace(0,         /* set return code from call  */
  310.  
  311.                      (PSZ *)0,         /*   to RexxSaveMacroSpace    */
  312.  
  313.                      av[0].strptr);    /*                            */
  314.  
  315.     sprintf(ret->strptr, "%d",(int)rc);/*                            */
  316.  
  317.     ret->strlength=strlen(ret->strptr);/*                            */
  318.  
  319.     rc = 0;                            /* no errors in call          */
  320.  
  321.     }                                  /*                            */
  322.  
  323.   return rc;                           /*                            */
  324.  
  325.   }                                    /*                            */
  326.  
  327.  
  328.  
  329.  
  330.  
  331. /*********************************************************************/
  332.  
  333. /* MacroErase                                                        */
  334.  
  335. /* This function must be called with no arguments.                   */
  336.  
  337. /*   This function will call RexxClearMacroSpace to remove all REXX  */
  338.  
  339. /*   macros from the macrospace.                                     */
  340.  
  341. /*                                                                   */
  342.  
  343. /* If called with any arguments, it will raise an error              */
  344.  
  345. /*                                                                   */
  346.  
  347. /* The return value of the function is the return code from          */
  348.  
  349. /*   calling the RexxSaveMacroSpace API.                             */
  350.  
  351. /*                                                                   */
  352.  
  353. /*********************************************************************/
  354.  
  355. /*$PE*/
  356.  
  357. /*$FD.MacroErase*/
  358.  
  359. ULONG APIENTRY MacroErase(
  360.  
  361.    PUCHAR    func,
  362.  
  363.    ULONG     ac,
  364.  
  365.    PRXSTRING av,
  366.  
  367.    PSZ       que,
  368.  
  369.    PRXSTRING ret)
  370.  
  371.   {
  372.  
  373.   APIRET rc = -1;                      /* return code from function  */
  374.  
  375.                                        /*                            */
  376.  
  377.   if (ac == 0) {                       /* no args allowed in call    */
  378.  
  379.     rc = RexxClearMacroSpace();        /* set return code from call  */
  380.  
  381.     sprintf(ret->strptr, "%d", (int)rc);
  382.  
  383.     rc = 0;                            /* no errors in call          */
  384.  
  385.     }                                  /*                            */
  386.  
  387.   return rc;                           /*                            */
  388.  
  389.   }                                    /*                            */
  390.  
  391.  
  392.  
  393.  
  394.  
  395. /*********************************************************************/
  396.  
  397. /* MacroQuery                                                        */
  398.  
  399. /* This function must be called with one argument.                   */
  400.  
  401. /*   The argument gives the name of a macro that may be in the       */
  402.  
  403. /*   macrospace.  This function will call RexxQueryMacro             */
  404.  
  405. /*   to find out if the macro is present.  This function returns the */
  406.  
  407. /*   return code of the RexxQueryMacro API.  This function does not  */
  408.  
  409. /*   return the search order flag.                                   */
  410.  
  411. /*                                                                   */
  412.  
  413. /* If called with a different number of arguments, it will raise an  */
  414.  
  415. /*   error.                                                          */
  416.  
  417. /*                                                                   */
  418.  
  419. /* The return value of the function is the return code from          */
  420.  
  421. /*   calling the RexxQueryMacro API.                                 */
  422.  
  423. /*                                                                   */
  424.  
  425. /*********************************************************************/
  426.  
  427. /*$PE*/
  428.  
  429. /*$FD.MacroQuery*/
  430.  
  431. ULONG APIENTRY MacroQuery(
  432.  
  433.    PUCHAR    func,
  434.  
  435.    ULONG     ac,
  436.  
  437.    PRXSTRING av,
  438.  
  439.    PSZ       que,
  440.  
  441.    PRXSTRING ret)
  442.  
  443.   {
  444.  
  445.   APIRET rc = -1;                      /* return code from function  */
  446.  
  447.   USHORT pos = 0;                      /* return of search position  */
  448.  
  449.                                        /*                            */
  450.  
  451.   if (ac == 1) {                       /* need only one arg          */
  452.  
  453.     rc = RexxQueryMacro(av[0].strptr,&pos);/* set return code from   */
  454.  
  455.                                        /*   call to RexxQueryMacro   */
  456.  
  457.     sprintf(ret->strptr, "%d", (int) rc);
  458.  
  459.     ret->strlength=strlen(ret->strptr);/*                            */
  460.  
  461.     rc = 0;                            /* no errors in call          */
  462.  
  463.     }                                  /*                            */
  464.  
  465.   return rc;                           /*                            */
  466.  
  467.   }                                    /*                            */
  468.  
  469.