Hi,

On some earlier release of jBase 5, I once had a need to "divert" a jBC
subroutine call to go via my own jBC subroutine with the same name that
then called the original (for which I didn't have source). I remember
creating a C function to achieve that. The C function was declared like
below. What I learned about the DP is that every subroutine is passed the
DP as the first argument so you do not need to initialise it again - just
pass it on.

I achieved my objective by compiling and cataloging a duplicate routine and
making sure that my duplicate routine was in a search path that was found
first before the existing routine. I would then call dllsym function below
passing it the actual shared library file name that contained the original
routine name (dll_name) and the name of the original subroutine (rtn_name)
and the arguments in VAR*, .... (Note that I had typedef int (*fptrXX) for
upto 20 arguments which was more than enough for all the subroutines I
wanted to debug).

dlopen/dlsym system calls were key functions in this piece of code. In my
case, the original jbase session was already started and from there, calls
could be exchanged between jBC and C any number of times by just passing
the DP along.

Hope it helps, Marco.

#include <stdlib.h>
#include <dlfcn.h>
#include <jsystem.h>

typedef int (*fptr)(struct jBASEDataAreas *dp, char *);
typedef int (*fptr1)(struct jBASEDataAreas *dp, char *, VAR *);
typedef int (*fptr2)(struct jBASEDataAreas *dp, char *, VAR *, VAR *);
typedef int (*fptr3)(struct jBASEDataAreas *dp, char *, VAR *, VAR *, VAR
*);
typedef int (*fptr4)(struct jBASEDataAreas *dp, char *, VAR *, VAR *, VAR
*, VAR *);

INT32 dllsym(struct jBASEDataAreas *dp, char *dll_name, char *rtn_name,
char *vflags, VAR **arg)
{
   void *handle;
   fptr fun_ptr;

   int numargs = strlen(vflags);

   handle = dlopen(dll_name, RTLD_LOCAL | RTLD_LAZY);
   if (!handle) {
      printf("dlopen('%s') failed!: %s\n ",dll_name, dlerror());
      return 1;
   }

   *(void **)(&fun_ptr) = dlsym(handle, rtn_name);
   if (fun_ptr == NULL) {
      printf("dlsym('%s', '%s') failed!: %s\n", dll_name, rtn_name,
dlerror());
      dlclose(handle);
      return 1;
   }
   switch(numargs) {
   case 0:
      (*fun_ptr)(dp, vflags);
      break;
   case 1:
      (*(fptr1)fun_ptr)(dp, vflags, arg[0]);
      break;
   case 2:
      (*(fptr2)fun_ptr)(dp, vflags, arg[0], arg[1]);
      break;
   case 3:
      (*(fptr3)fun_ptr)(dp, vflags, arg[0], arg[1], arg[2]);
      break;
   case 4:
      (*(fptr4)fun_ptr)(dp, vflags, arg[0], arg[1], arg[2], arg[3]);
      break;
   }
   dlclose(handle);
   return 0;
}


On Wed, 4 Mar 2020 at 17:05, Igor Osmolovskiy <igos...@gmail.com> wrote:

> Hello Jim,
>
> Thanks for your participation in the discussion :)
>
> > Are writing a pure C program and wish to call in to subroutines etc?
> Yes, that's the idea. Actually the final goal is wider, namely to write a
> Python extension in C that would call JBC functions. Basically I managed to
> do it but this excessive session is something would be good to get rid of.
>
> I've applied jbc -S myrtn.b for a simple code already which results in
> producing 2 files: myrtn.c and the myrtn.j, the latter one being included
> into the .c one via #include "myrtn.j".
> But in the .j file already we can see the lines like
>     #define DEF_SEL_VAR JLibReturnDefaultSelect(dp) /* Default select
> list for this program */
>     #define DEF_FILE_VAR JLibReturnDefaultFile(dp) /* Default file
> descriptor for this program */
>
> i.e. dp is supposed to be initialized already by the moment myrtn is
> called.
>
> I've managed to find the next dp related functions mentioned in some
> libraries
> jbase_getdp() - this works, i.e. initializes dp, but creates a new session
> jbase_getdp_nocreate() - not working if called alone. but it works and
> returns the dp1 value when called after jbase_getdp(), and dp1 = dp in
> this case
> jbase_getdpEX() - not sure what it should do
> jbase_setdpEX() - not sure what it should do
>
>
> Please see the sample piece of code
>
> #include <jsystem.h>
> extern "C" DPSTRUCT *jbase_getdp();
>
> ...
>
> PyMODINIT_FUNC
> jbc_oconv(PyObject*, PyObject* _args)
> {
> ...
> DPSTRUCT *dp = jbase_getdp();
> OCONV_BBBBB(result, tmp, VarHelper(arg1), VarHelper(arg2));  // this if
> defined in jsystem.h as   #define OCONV_BBBBB(Target, ConvReg, Source,
> Conversion) JLibGConvRun(*dp*, ConvReg, Target, Source, Conversion, 1)
> ...
> }
>
>
>
> I've tried to read the jmainfunction.obj  as well where some other jbase
> related functions are mentioned but could not figure out any useful one...
>
> Maybe there is something I am missing or some non-documented function?
>
> Would appreciate any hints! :)
> Thanks
> Igor
>
> среда, 4 марта 2020 г., 8:11:30 UTC+1 пользователь Jim Idle написал:
>>
>> Wow - it has been a long time since that code ;) However, what you ask is
>> possible I believe. It depends on your context. Are writing a pure C
>> program and wish to call in to subroutines etc?
>>
>> Probably the best place to start is to generate the C code for a very
>> small jBC program:
>>
>> lufc.b
>> CRT "Leeds United are Magic!"
>>
>> jbc -S lufc.b
>>
>> Then you will see the startup sequence for a standard executable and can
>> copy the code.
>>
>> However, my memory is vague, but I think that we had a C function you can
>> call to do what you are doing and that will return the dp for reuse. It
>> get's passed to subroutines for instance.
>>
>> Perhaps you can post the code you currently have.
>>
>> Jim
>>
>> On Wed, Mar 4, 2020 at 3:19 AM Igor Osmolovskiy <igo...@gmail.com> wrote:
>>
>>> Dear all
>>>
>>> I need the jBase DP (data pointer) to be initialized. I've actually
>>> managed to initialize it by calling the jbase_getdp(). But in both cases
>>> the new child session is created in jshell (and gets removed automatically
>>> after the code has been executed).
>>>
>>> So I am wondering: is there a possibility to retrieve and use the
>>> current session DP somehow? So that no child session would get created
>>>
>>> Any advice would be highly appreciated
>>>
>>> Thanks in advance
>>>
>>>
>>> PS: to the admin, it is actually a jBase related question rather than
>>> TAFC. Thanks
>>>
>>>
>>> Kind regards,
>>> Igor
>>>
>>> --
>>> --
>>> IMPORTANT: T24/Globus posts are no longer accepted on this forum.
>>>
>>> To post, send email to jb...@googlegroups.com
>>> To unsubscribe, send email to jbase-un...@googlegroups.com
>>> For more options, visit this group at
>>> http://groups.google.com/group/jBASE?hl=en
>>>
>>> ---
>>> You received this message because you are subscribed to the Google
>>> Groups "jBASE" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to jb...@googlegroups.com.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/jbase/73153581-25ae-493a-95f6-8afc5cdf7813%40googlegroups.com
>>> <https://groups.google.com/d/msgid/jbase/73153581-25ae-493a-95f6-8afc5cdf7813%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> --
> --
> IMPORTANT: T24/Globus posts are no longer accepted on this forum.
>
> To post, send email to jBASE@googlegroups.com
> To unsubscribe, send email to jbase-unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/jBASE?hl=en
>
> ---
> You received this message because you are subscribed to the Google Groups
> "jBASE" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to jbase+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/jbase/c010b5b5-199a-42b8-a7fa-2b351052c29f%40googlegroups.com
> <https://groups.google.com/d/msgid/jbase/c010b5b5-199a-42b8-a7fa-2b351052c29f%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
-- 
IMPORTANT: T24/Globus posts are no longer accepted on this forum.

To post, send email to jBASE@googlegroups.com
To unsubscribe, send email to jbase-unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/jBASE?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"jBASE" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jbase+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jbase/CAJeZcsYosNXdVjPPydqVfEB-WEJahNbcQ-N%3D9yHq35ch09h0%3DA%40mail.gmail.com.

Reply via email to