It is based on the libcurl demo from the gnucobol docs. Should be
able to build it with "make" , assuming you have gnucobol-devel, make
and libcurl installed.
I'm having a problem with libcurl's callback from libcurl into the
cobol CURLOPT_WRITEFUNCTION 'function when accessing the parameters.
All the parameters passed to curl-writeback seem to be causing an
"attempt to reference invalid memory address (signal)" error. when
they are attempted to be accessed.
My first thought was that this has something to do with the lifetime
of the items in the linkage section, but after trying many options I
didn't get any luck. I think (or assume) that libcurl would have
copied the parameters and that wasn't something that I would have to
look after.
What am I doing wrong ? Thanks in advance for any help you can
provide. I eventually want to end up using this for fetching json, as
a rest style client, if a project like this exists please let me know,
as I'd love to learn from it.
--
Wade Mealing
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I do have a basic example working, where I wrote my own callback ( https://github.com/wmealing/example-cobol-c-callback ) but I'd figure i'd ask before commiting too much time to debugging this if I'm doing something obviously wrong.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
So - your basic example does work as expected, but the more complex isn't, right?
Note - the code uploaded there seems to set a callback function that doesn't exist at all ?!?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
That looks fine for everything but the return value (int vs. 64bit type).
To at least better match the type use binary-long unsigned (no change in codegen for the prototype yet).
Checking further in the code we see that the program expects to have a COBOL caller and therefore gets the number of parameters from the last COBOL call - which obviously was less than you expect.
To work around that:
procedure division
+ with C linkage using
by value contents
by value element-size
by value element-count
by value memory-structure
returning real-size.
But as there a lot of warnings and the return doesn't match I'd personally suggest to have a minimal C wrapper that only takes the parameters, then executes a cob_call() to call COBOL. This has also the benefit that you can directly use curses types and defines in the C wrapper and use whatever you see most useful as types for COBOL.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello again,
I'm trying to use libcurl to fetch a page, I've setup my current
configuration here:
https://github.com/wmealing/ca/tree/main
It is based on the libcurl demo from the gnucobol docs. Should be
able to build it with "make" , assuming you have gnucobol-devel, make
and libcurl installed.
I'm having a problem with libcurl's callback from libcurl into the
cobol CURLOPT_WRITEFUNCTION 'function when accessing the parameters.
All the parameters passed to curl-writeback seem to be causing an
"attempt to reference invalid memory address (signal)" error. when
they are attempted to be accessed.
My first thought was that this has something to do with the lifetime
of the items in the linkage section, but after trying many options I
didn't get any luck. I think (or assume) that libcurl would have
copied the parameters and that wasn't something that I would have to
look after.
What am I doing wrong ? Thanks in advance for any help you can
provide. I eventually want to end up using this for fetching json, as
a rest style client, if a project like this exists please let me know,
as I'd love to learn from it.
--
Wade Mealing
I do have a basic example working, where I wrote my own callback ( https://github.com/wmealing/example-cobol-c-callback ) but I'd figure i'd ask before commiting too much time to debugging this if I'm doing something obviously wrong.
So - your basic example does work as expected, but the more complex isn't, right?
Note - the code uploaded there seems to set a callback function that doesn't exist at all ?!?
Correct.
Unless i'm misunderstanding how callbacks works, it sets the callback to https://github.com/wmealing/ca/blob/main/src/curl-write-callback.cbl
I will see checkpoint 1 and 2. The compute element on line 45 fails.
Hm, checking the callback function documentation at https://curl.se/libcurl/c/CURLOPT_WRITEFUNCTION.html the function looks right.
I'm not sure about the magic number for writefunc in the constants - ar you sure that is correct?
In any case - compiling your code results in :
so those message possibly mean "thin ice".
Nonetheless, checking the generated C source:
That looks fine for everything but the return value (int vs. 64bit type).
To at least better match the type use
binary-long unsigned
(no change in codegen for the prototype yet).Checking further in the code we see that the program expects to have a COBOL caller and therefore gets the number of parameters from the last COBOL call - which obviously was less than you expect.
To work around that:
But as there a lot of warnings and the return doesn't match I'd personally suggest to have a minimal C wrapper that only takes the parameters, then executes a
cob_call()
to call COBOL. This has also the benefit that you can directly use curses types and defines in the C wrapper and use whatever you see most useful as types for COBOL.Example run:
% ./ca
Starting..
CALLBACK HANDLE IS: 0x0000000000404844
MEMORY ADDRESS:0x0000000000408370
MEMORY SIZE: +0016777216
Running total: +0000000001
CHECKPOINT 1.
CHECKPOINT 2.
attempt to reference invalid memory address (signal)
I've modified the trivial example and it seems to have the same behavior. See https://github.com/wmealing/example-cobol-c-callback.git now.
After compiling with
--debug
, the runtime gave additional output:As far as I can tell libcurl is supposed to call this function with the relevant args. The function gets called, just not as I expect.
Last edit: Simon Sobisch 2023-06-30
I now have the trivial example working, I had to explicitly call out that the parameter being passed was 'by value'.
Same tests on libcurl/more complex version did not yield results, but I will continue to investigate.
Last edit: Simon Sobisch 2023-06-30
I'd like to know if the current example in the gnucobol programmers guide works works for anyone else, as its not working for me at the moment.
Which example in the PG do you refer to?
The example from https://gnucobol.sourceforge.io/faq/index.html "4.1.245 FUNCTION-ID" I for some reason thought I had seen it elsewhere too.
I was able to get my example code working, with the changes you suggested.