home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sffdk10b.zip / sample / rexxcall / ple_rexx.c < prev    next >
C/C++ Source or Header  |  2001-03-05  |  8KB  |  321 lines

  1. /*
  2. ** Module   :PLE_REXX.C
  3. ** Abstract :FDK Rexx library
  4. **
  5. ** Copyright (C) Link Guard Solutions
  6. ** For conditions of distribution and use, see license in license.txt
  7. **
  8. ** Log: Fri  23/02/2001 Created
  9. **
  10. */
  11. #define INCL_REXXSAA
  12. #define INCL_DOS
  13. #define INCL_DOSERRORS
  14.  
  15. #include <sftypes.h>
  16.  
  17. #include <os2.h>
  18. #include <rexxsaa.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21.  
  22. #define EN_LOAD         1
  23. #define EN_CONFIG       2
  24. #define EN_WRITE        4
  25. #define EN_DONE         8
  26.  
  27. #define SECT_MAXNAME    80
  28.  
  29. #define SNAME_VAR       "script"
  30.  
  31. #define FNAME_QUERY     "QUERY"
  32. #define FNAME_LOAD      "LOAD"
  33. #define FNAME_CONFIG    "CONFIG"
  34. #define FNAME_WRITE     "W"
  35. #define FNAME_DONE      "DONE"
  36.  
  37. dword numinstances = 0;
  38. HMTX  mutex = -1L;
  39.  
  40. typedef struct
  41. {
  42.     dword enabled;
  43.     char  scriptname[CCHMAXPATH];
  44.     char  sectname[SECT_MAXNAME];
  45.     dword sectnamelen; //Cache value to produce RXSTRING faster
  46.  
  47. } scriptdata;
  48.  
  49. dword getenabled(scriptdata *d)
  50. {
  51.     if (d->enabled)
  52.         return d->enabled;
  53.  
  54.     if (!d->scriptname[0])
  55.         return 0;
  56.  
  57.     /* Try to load script */
  58.     {
  59.         RXSTRING retstr;
  60.         RXSTRING argv[2];
  61.  
  62.         LONG startrc = 0;
  63.         SHORT rexxrc = 0;
  64.  
  65.         char *retptr;
  66.  
  67.         retstr.strptr = 0;
  68.         retstr.strlength = 0;
  69.  
  70.         MAKERXSTRING(argv[0], FNAME_QUERY, sizeof(FNAME_QUERY) - 1);
  71.  
  72.         if (mutex != -1L)
  73.             startrc = DosRequestMutexSem(mutex, SEM_INDEFINITE_WAIT);
  74.  
  75.         if (startrc == NO_ERROR)
  76.         {
  77.             startrc = RexxStart(1,
  78.                                 argv,
  79.                                 d->scriptname,
  80.                                 0,
  81.                                 "CMD",
  82.                                 RXCOMMAND,
  83.                                 0,
  84.                                 &rexxrc,
  85.                                 &retstr);
  86.  
  87.             retptr = RXSTRPTR(retstr);
  88.  
  89.             if (retptr)
  90.             {
  91.                 if (!startrc)
  92.                     d->enabled = atol(retptr);
  93.  
  94.                 DosFreeMem(retptr);
  95.             }
  96.  
  97.             if (d->enabled & EN_LOAD)
  98.             {
  99.                 /* Call load */
  100.                 retstr.strptr = 0;
  101.                 retstr.strlength = 0;
  102.  
  103.                 rexxrc = 0;
  104.  
  105.                 MAKERXSTRING(argv[0], FNAME_LOAD, sizeof(FNAME_LOAD) - 1);
  106.                 MAKERXSTRING(argv[1], d->sectname, d->sectnamelen);
  107.  
  108.                 startrc = RexxStart(2,
  109.                                     argv,
  110.                                     d->scriptname,
  111.                                     0,
  112.                                     "CMD",
  113.                                     RXCOMMAND,
  114.                                     0,
  115.                                     &rexxrc,
  116.                                     &retstr);
  117.  
  118.                 retptr = RXSTRPTR(retstr);
  119.  
  120.                 if (retptr)
  121.                     DosFreeMem(retptr);
  122.  
  123.             }
  124.  
  125.             if (mutex != -1L)
  126.                 DosReleaseMutexSem(mutex);
  127.         }
  128.     }
  129.  
  130.     if (d->enabled == 0)
  131.     {
  132.         //This is WRONG script. Clear it's name
  133.         d->scriptname[0] = 0;
  134.     }
  135.  
  136.     return d->enabled;
  137. }
  138.  
  139. void APIENTRY Init11(char *name, dword *plidx)
  140. {
  141.     scriptdata *d = malloc(sizeof(scriptdata));
  142.     d->enabled = 0;
  143.     strncpy(d->sectname, name, SECT_MAXNAME);
  144.     d->sectname[SECT_MAXNAME - 1] = 0;
  145.     d->sectnamelen = strlen(d->sectname);
  146.     d->scriptname[0] = 0;
  147.     *plidx = (dword)d;
  148.  
  149.     if (!numinstances)
  150.     {
  151.         /* Create Mutex */
  152.         if (DosCreateMutexSem(0, &mutex, 0, FALSE))
  153.             mutex = -1L;
  154.     }
  155.     numinstances++;
  156. }
  157.  
  158. void APIENTRY Config11(dword plidx, char *var, char *val)
  159. {
  160.     scriptdata *d = (scriptdata *)plidx;
  161.  
  162.     if (!stricmp(var, SNAME_VAR))
  163.     {
  164.         /* New script */
  165.         d->enabled = 0;
  166.         strncpy(d->scriptname, val, CCHMAXPATH);
  167.         d->scriptname[CCHMAXPATH - 1] = 0;
  168.     }
  169.     if (getenabled(d) & EN_CONFIG)
  170.     {
  171.         /* Pass configuration to plugin */
  172.         RXSTRING retstr;
  173.         RXSTRING argv[4];
  174.  
  175.         LONG startrc = 0;
  176.         SHORT rexxrc = 0;
  177.  
  178.         char *retptr;
  179.  
  180.         retstr.strptr = 0;
  181.         retstr.strlength = 0;
  182.  
  183.         MAKERXSTRING(argv[0], FNAME_CONFIG, sizeof(FNAME_CONFIG) - 1);
  184.         MAKERXSTRING(argv[1], d->sectname, d->sectnamelen);
  185.         MAKERXSTRING(argv[2], var, strlen(var));
  186.         MAKERXSTRING(argv[3], val, strlen(val));
  187.  
  188.         if (mutex != -1L)
  189.             startrc = DosRequestMutexSem(mutex, SEM_INDEFINITE_WAIT);
  190.  
  191.         if (startrc == NO_ERROR)
  192.         {
  193.             startrc = RexxStart(4,
  194.                                 argv,
  195.                                 d->scriptname,
  196.                                 0,
  197.                                 "CMD",
  198.                                 RXCOMMAND,
  199.                                 0,
  200.                                 &rexxrc,
  201.                                 &retstr);
  202.  
  203.             retptr = RXSTRPTR(retstr);
  204.  
  205.             if (retptr)
  206.                 DosFreeMem(retptr);
  207.  
  208.             if (mutex != -1L)
  209.                 DosReleaseMutexSem(mutex);
  210.         }
  211.     }
  212. }
  213.  
  214. dword APIENTRY Write11(dword plidx, Bool source, dword proto, char *packet, dword psize, dword idx)
  215. {
  216.     scriptdata *d = (scriptdata *)plidx;
  217.     dword rc = 0;
  218.  
  219.     if (getenabled(d) & EN_WRITE)
  220.     {
  221.         /* Pass configuration to plugin */
  222.         RXSTRING retstr;
  223.         RXSTRING argv[6];
  224.         char     pstr[33];
  225.  
  226.         LONG startrc = 0;
  227.         SHORT rexxrc = 0;
  228.  
  229.         char *retptr;
  230.  
  231.         retstr.strptr = 0;
  232.         retstr.strlength = 0;
  233.  
  234.         MAKERXSTRING(argv[0], FNAME_WRITE, sizeof(FNAME_WRITE) - 1);
  235.         MAKERXSTRING(argv[1], d->sectname, d->sectnamelen);
  236.         MAKERXSTRING(argv[2], source ? "IN2" : "IN1", 3);
  237.         MAKERXSTRING(argv[3], &proto, 4);
  238.         MAKERXSTRING(argv[4], packet, psize);
  239.         MAKERXSTRING(argv[5], &idx, 4);
  240.  
  241.         if (mutex != -1L)
  242.             startrc = DosRequestMutexSem(mutex, SEM_INDEFINITE_WAIT);
  243.  
  244.         if (startrc == NO_ERROR)
  245.         {
  246.             startrc = RexxStart(6,
  247.                                 argv,
  248.                                 d->scriptname,
  249.                                 0,
  250.                                 "CMD",
  251.                                 RXCOMMAND,
  252.                                 0,
  253.                                 &rexxrc,
  254.                                 &retstr);
  255.  
  256.             retptr = RXSTRPTR(retstr);
  257.  
  258.             if (retptr)
  259.             {
  260.                 rc = atol(retptr);
  261.                 DosFreeMem(retptr);
  262.             }
  263.  
  264.             if (mutex != -1L)
  265.                 DosReleaseMutexSem(mutex);
  266.         }
  267.     }
  268.     return rc;
  269. }
  270.  
  271. void APIENTRY Done11(dword plidx)
  272. {
  273.     scriptdata *d = (scriptdata *)plidx;
  274.  
  275.     if (getenabled(d) & EN_DONE)
  276.     {
  277.         /* Call load */
  278.         RXSTRING retstr;
  279.         RXSTRING argv[2];
  280.  
  281.         LONG startrc = 0;
  282.         SHORT rexxrc = 0;
  283.  
  284.         char *retptr;
  285.  
  286.         retstr.strptr = 0;
  287.         retstr.strlength = 0;
  288.  
  289.         rexxrc = 0;
  290.  
  291.         MAKERXSTRING(argv[0], FNAME_DONE, sizeof(FNAME_DONE) - 1);
  292.         MAKERXSTRING(argv[1], d->sectname, d->sectnamelen);
  293.  
  294.         startrc = RexxStart(2,
  295.                             argv,
  296.                             d->scriptname,
  297.                             0,
  298.                             "CMD",
  299.                             RXCOMMAND,
  300.                             0,
  301.                             &rexxrc,
  302.                             &retstr);
  303.  
  304.         retptr = RXSTRPTR(retstr);
  305.  
  306.         if (retptr)
  307.             DosFreeMem(retptr);
  308.     }
  309.  
  310.     free(d);
  311.  
  312.     numinstances--;
  313.     if (numinstances == 0)
  314.     {
  315.         DosCloseMutexSem(mutex);
  316.         mutex = -1L;
  317.     }
  318. }
  319.  
  320.  
  321.