comp.lang.ada
 help / color / mirror / Atom feed
* C code to Ada
@ 2003-01-20 18:44 chris.danx
  2003-01-20 21:33 ` sk
                   ` (3 more replies)
  0 siblings, 4 replies; 23+ messages in thread
From: chris.danx @ 2003-01-20 18:44 UTC (permalink / raw)


Hi,

How do you map something like this to Ada (for a binding)?  It's 
probable that I'll only be working with GNAT on the Linux front (it's 
all there is), so a GNAT specific option although not ideal, is acceptable.


/* Open the shared object FILE and map it in; return a handle that can 
be passed to `dlsym' to get symbol values from it.  */

extern void *dlopen (__const char *__file, int __mode) __THROW;

I know rougly how to map the __file, and __mode params but am not sure 
how to deal with the void * or in this case the throw.  This is taken 
from dlfcn.h which is the header for dynamic library support in Linux. 
If anyone already has a (GMGPL) binding to this library and they'd like 
to share, please let me know, it'll save me making one.  In any event 
how do you translate this C code so it can be called from Ada?

After I get a wee thin binding working, I'm going to make it a little 
thicker and toss it into the open.


Cheers,
Chris
-- 
for personal replies change spamoff to chris




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: C code to Ada
  2003-01-20 18:44 C code to Ada chris.danx
@ 2003-01-20 21:33 ` sk
  2003-01-20 21:43   ` chris.danx
  2003-01-21  2:27   ` Jeffrey Carter
       [not found] ` <3E2C6B2B.5090600@noname.com>
                   ` (2 subsequent siblings)
  3 siblings, 2 replies; 23+ messages in thread
From: sk @ 2003-01-20 21:33 UTC (permalink / raw)
  To: comp.lang.ada mail to news gateway

Hi,

I had a binding (lost due to a misplaced "mkfs /dev/...")
but I am not sure where you are getting the "__THROW"
from.

When I was playing with shared-libraries etc (based on the
Linux-Documentation-Project Program-Library-HOWTO), I did
"man dlopen" and bound to the functions specified

 From "man dlopen" ...


#include <dlfcn.h>

void *dlopen(const char *filename, int flag);
const char *dlerror(void);
void *dlsym(void *handle, char *symbol);
int dlclose(void *handle);

So ...

     ICS => Interfaces.C.Strings;
     IC  => Interfaces.C;

     procedure Dlopen (
         Name : ICS.Chars_Ptr;  -- <= Assign with ICS.New_String
         Mode : IC.Int
     );

     function DlError return ICS.Chars_Ptr;
     -- Need to check man for the return parameter ...
     -- ... this also seems to be an example of the
     -- message being allocated in called function address
     -- space (havn't time to double check for now).

     procedure Dlsym (Handle : System.Address; Symbol : IC.Char_Array)

     function Dlclose return IC.Int;

Translating
     "(void)" typically translates to parameterless function/procedure 

     "void function-name" typically translates to an Ada procedure

The best source I found dealing with dl* is in the above mentioned
howto. With some care, the special library functions "init"
and "fini" can be nicely related to "adainit" and "adafinal"
needed to make a stand-alone shared/dynamic library all using
Ada and GNAT :-)

-- 
--
-- Merge vertically for real address
--
------------------------------------
-- s n p @ t . o
--  k i e k c c m
------------------------------------




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: C code to Ada
       [not found] ` <3E2C6B2B.5090600@noname.com>
@ 2003-01-20 21:39   ` sk
  0 siblings, 0 replies; 23+ messages in thread
From: sk @ 2003-01-20 21:39 UTC (permalink / raw)
  To: comp.lang.ada mail to news gateway

Oops,

 > procedure Dlsym (Handle : System.Address; Symbol : IC.Char_Array)

the "Symbol : IC.Char_Array" isn't going to work, that (very brief
scan) looks like it should be either "ICS.Chars_Ptr" or
"access IC.Char_Array"

And

"void *" often best translates to system.address if you don't
have a specific structure defined within Ada with an access
type which can corespond to a "C" pointer.

-- 
--
-- Merge vertically for real address
--
------------------------------------
-- s n p @ t . o
--  k i e k c c m
------------------------------------




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: C code to Ada
  2003-01-20 21:33 ` sk
@ 2003-01-20 21:43   ` chris.danx
  2003-01-20 22:56     ` James S. Rogers
  2003-01-20 23:32     ` sk
  2003-01-21  2:27   ` Jeffrey Carter
  1 sibling, 2 replies; 23+ messages in thread
From: chris.danx @ 2003-01-20 21:43 UTC (permalink / raw)


sk wrote:
> Hi,
> 
> I had a binding (lost due to a misplaced "mkfs /dev/...")

oops!

> but I am not sure where you are getting the "__THROW"
> from.

from /usr/include/dlfcn.h


> When I was playing with shared-libraries etc (based on the
> Linux-Documentation-Project Program-Library-HOWTO), I did
> "man dlopen" and bound to the functions specified
> 
>  From "man dlopen" ...

Cheers will look at that!


> #include <dlfcn.h>
> 
> void *dlopen(const char *filename, int flag);
>
> So ...
> 
>     ICS => Interfaces.C.Strings;
>     IC  => Interfaces.C;
> 
>     procedure Dlopen (
>         Name : ICS.Chars_Ptr;  -- <= Assign with ICS.New_String
>         Mode : IC.Int
>     );


I don't understand this.  Why procedure?  The return type of dlopen is 
not void (a procedure), but void* (a pointer to an unknown object - 
unknown to the client because dereferencing directly on void* isn't 
allowed; you have to cast it before you can dereference it).  It's where 
you get the handle from and what's causing me the most problems.


>     procedure Dlsym (Handle : System.Address; Symbol : IC.Char_Array)
> 
>     function Dlclose return IC.Int;
> 
> Translating
>     "(void)" typically translates to parameterless function/procedure
>     "void function-name" typically translates to an Ada procedure

See above.


> The best source I found dealing with dl* is in the above mentioned
> howto. With some care, the special library functions "init"
> and "fini" can be nicely related to "adainit" and "adafinal"
> needed to make a stand-alone shared/dynamic library all using
> Ada and GNAT :-)

I don't understand but I'll look at this a little later.


Thanks,
Chris
-- 
for personal replies change spamoff to chris




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: C code to Ada
  2003-01-20 21:43   ` chris.danx
@ 2003-01-20 22:56     ` James S. Rogers
  2003-01-20 23:05       ` chris.danx
  2003-01-21  2:17       ` Jeffrey Carter
  2003-01-20 23:32     ` sk
  1 sibling, 2 replies; 23+ messages in thread
From: James S. Rogers @ 2003-01-20 22:56 UTC (permalink / raw)


I looked in the GNAT file i-cstrea.ads. It defines the following
(just an excerpt from the file)

package Interfaces.C_Streams is
   pragma Preelaborate;

   --  Note: the reason we do not use the types that are in Interfaces.C is
   --  that we want to avoid dragging in the code in this unit if possible.

   subtype chars is System.Address;
   --  Pointer to null-terminated array of characters

   subtype FILEs is System.Address;
   --  Corresponds to the C type FILE*

   subtype voids is System.Address;
   --  Corresponds to the C type void*

It appears that you can correctly use System.Address on to
bind to a C void*.

Jim Rogers
"chris.danx" <spamoff.danx@ntlworld.com> wrote in message
news:e2_W9.3074$Ts5.30248@newsfep4-win.server.ntli.net...
> sk wrote:
> > Hi,
> >
> > I had a binding (lost due to a misplaced "mkfs /dev/...")
>
> oops!
>
> > but I am not sure where you are getting the "__THROW"
> > from.
>
> from /usr/include/dlfcn.h
>
>
> > When I was playing with shared-libraries etc (based on the
> > Linux-Documentation-Project Program-Library-HOWTO), I did
> > "man dlopen" and bound to the functions specified
> >
> >  From "man dlopen" ...
>
> Cheers will look at that!
>
>
> > #include <dlfcn.h>
> >
> > void *dlopen(const char *filename, int flag);
> >
> > So ...
> >
> >     ICS => Interfaces.C.Strings;
> >     IC  => Interfaces.C;
> >
> >     procedure Dlopen (
> >         Name : ICS.Chars_Ptr;  -- <= Assign with ICS.New_String
> >         Mode : IC.Int
> >     );
>
>
> I don't understand this.  Why procedure?  The return type of dlopen is
> not void (a procedure), but void* (a pointer to an unknown object -
> unknown to the client because dereferencing directly on void* isn't
> allowed; you have to cast it before you can dereference it).  It's where
> you get the handle from and what's causing me the most problems.
>
>
> >     procedure Dlsym (Handle : System.Address; Symbol : IC.Char_Array)
> >
> >     function Dlclose return IC.Int;
> >
> > Translating
> >     "(void)" typically translates to parameterless function/procedure
> >     "void function-name" typically translates to an Ada procedure
>
> See above.
>
>
> > The best source I found dealing with dl* is in the above mentioned
> > howto. With some care, the special library functions "init"
> > and "fini" can be nicely related to "adainit" and "adafinal"
> > needed to make a stand-alone shared/dynamic library all using
> > Ada and GNAT :-)
>
> I don't understand but I'll look at this a little later.
>
>
> Thanks,
> Chris
> --
> for personal replies change spamoff to chris
>





^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: C code to Ada
  2003-01-20 22:56     ` James S. Rogers
@ 2003-01-20 23:05       ` chris.danx
  2003-01-21  2:17       ` Jeffrey Carter
  1 sibling, 0 replies; 23+ messages in thread
From: chris.danx @ 2003-01-20 23:05 UTC (permalink / raw)


James S. Rogers wrote:

> It appears that you can correctly use System.Address on to
> bind to a C void*.


Thanks Jim!




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: C code to Ada
  2003-01-20 21:43   ` chris.danx
  2003-01-20 22:56     ` James S. Rogers
@ 2003-01-20 23:32     ` sk
  2003-01-21  0:56       ` chris.danx
  1 sibling, 1 reply; 23+ messages in thread
From: sk @ 2003-01-20 23:32 UTC (permalink / raw)
  To: comp.lang.ada mail to news gateway

Hi,

 > I don't understand this.  Why procedure?

I owe you more than one "oops", sorry. Very q&d at
the end of the day and I was trying to get somewhere
else.

Hopefully you will understand enough of what I was
intending and produce a public package ?

Your OP concerning GMGPL ... my understanding of
GPL would suggest that a binding to a GPL'ed "dlfcn.h"
would require it also to be GPL if you released it
publicly since the binding would be a derived work.

The GPL (and associates) can be tricky. Also
remember R.Dewar's concerns about using GPL code
without being sure that the original author has
the right to release as GPL.

-- 
--
-- Merge vertically for real address
--
------------------------------------
-- s n p @ t . o
--  k i e k c c m
------------------------------------




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: C code to Ada
  2003-01-20 23:32     ` sk
@ 2003-01-21  0:56       ` chris.danx
  2003-01-21  1:26         ` sk
  0 siblings, 1 reply; 23+ messages in thread
From: chris.danx @ 2003-01-21  0:56 UTC (permalink / raw)


sk wrote:
> Hi,
> 
> Very q&d at the end of the day and I was trying to get 
 > somewhere else.


q&d?


> Hopefully you will understand enough of what I was
> intending and produce a public package ?

Yes I think (or rather hope) so, thanks for the advice.


> Your OP concerning GMGPL ... my understanding of
> GPL would suggest that a binding to a GPL'ed "dlfcn.h"
> would require it also to be GPL if you released it
> publicly since the binding would be a derived work.

Is it GPL?  Fook!  No wait... <checks> it's LGPL'ed, so I can LGPL the 
binding.  I don't know for sure that I can then include the binding in 
my project and be ok with GMGPL'ing the rest of the code or not.  I am 
working on the premise that it is, if anyone knows different or knows 
someone who knows different, do tell.


> The GPL (and associates) can be tricky. 

Yeah, I don't really understand any of them all that well (I sometimes 
think that's the idea :) ).  This is what I've deduced and go by:-

GPL - all code and linked code must be Free Software

LGPL - the library is Free Software but code that links to it need not be

GMGPL - the code and any modifications shall remain Free Software, but 
linking with or using the code does not make client code Free Software 
(i.e. the client code can be proprietry or another non-free license).


 > Also
> remember R.Dewar's concerns about using GPL code
> without being sure that the original author has
> the right to release as GPL.

Who's R. Dewar again?  It's been so long ;)

Anyway, I was not aware of Dr Dewars concerns but I should have checked 
the libs - I took it for granted they'd be LGPL.  This is my first 
venture into the LGPL and GPL world, so I'm a bit confused about 
licensing and stuff (I usually stick to GMGPL'ed software which I prefer 
purely because I sort of understand the license).



Chris
-- 
for personal replies change spamoff to chris




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: C code to Ada
  2003-01-20 18:44 C code to Ada chris.danx
  2003-01-20 21:33 ` sk
       [not found] ` <3E2C6B2B.5090600@noname.com>
@ 2003-01-21  1:23 ` sk
  2003-01-25 21:28   ` chris.danx
  2003-01-23  8:00 ` sk
  3 siblings, 1 reply; 23+ messages in thread
From: sk @ 2003-01-21  1:23 UTC (permalink / raw)
  To: comp.lang.ada mail to news gateway

Hi yet again,

Since I messed it all up pretty bad, here you go ...
(feel free to clean it up, thicken it and release it
back to CLA, hint hint :-)

Please also remember that this is still q&d programming and is
not tested other than "Hey, it works !" My original binding
and everything else was a lot cleaner etc.

-----------------------------
-- dynamic_shared_library.ads
-----------------------------
package Dynamic_Shared_Library is

     function What_Does_It_All_Mean return Integer;
     pragma Export (C, What_Does_It_All_Mean, "wdiam");

end Dynamic_Shared_Library;

-----------------------------
-- dynamic_shared_library.adb
-----------------------------
package body Dynamic_Shared_Library is

     function What_Does_It_All_Mean return Integer is
     begin
         return 42;
     end What_Does_It_All_Mean;

end Dynamic_Shared_Library;

---------------------------------------
-- dynamic_linking_loading_inteface.ads
---------------------------------------
with Interfaces.C;
with Interfaces.C.Strings;
with System;

package Dynamic_Linking_Loading_Interface is
pragma Linker_Options ("-ldl");

     package IC              renames Interfaces.C;
     package ICS             renames Interfaces.C.Strings;

     type Handle is private;

     Empty_Handle : constant Handle;

     type Open_Flags is (Lazy, Now);
     -- Values from "/usr/include/bits/dlfcn.h"
     for Open_Flags use (
         Lazy    =>  1,
         Now     =>  2
     );

     function Dl_Open (
         Name    : ICS.Chars_Ptr;
         Flags   : Open_Flags
     ) return Handle;
     pragma Import (C, Dl_Open, "dlopen");

     function Dl_Error return ICS.Chars_Ptr;
     pragma Import (C, Dl_Error, "dlerror");

     function Dl_Sym (
         Handle  : Dynamic_Linking_Loading_Interface.Handle;
         Symbol  : ICS.Chars_Ptr
     ) return System.Address;
     pragma Import (C, Dl_Sym, "dlsym");

     function Dl_Close (
	Handle : Dynamic_Linking_Loading_Interface.Handle
     ) return IC.Int;
     pragma Import (C, Dl_Close, "dlclose");

private

     type Handle is new System.Address;

     Empty_Handle    : constant Handle := Handle(System.Null_Address);

end Dynamic_Linking_Loading_Interface;

---------------
-- lib_test.adb
---------------
with Ada.Text_Io;
with Ada.Unchecked_Conversion;
with Dynamic_Linking_Loading_Interface;
with Gnat.Directory_Operations;
with Interfaces.C;
with Interfaces.C.Strings;
with System;

procedure Lib_Test is

     -- References :
     --
     --     "Gnat Users Guide : Creating an Ada Library"
     --
     --     "Linux Documentation Project - Program Library HOWTO"
     --
     -- This reference is the basis for the whole example and library
     -- packages.

     package DLLI        renames Dynamic_Linking_Loading_Interface;
     package IC          renames Interfaces.C;
     package ICS         renames Interfaces.C.Strings;
     package GDIR        renames Gnat.Directory_Operations;
     package TIO         renames Ada.Text_Io;

     type Called_Function is access function return Integer;
     type Called_Procedure is access procedure;

     Library_Function    : Called_Function;

     Ada_Library_Init    : Called_Procedure;
     Ada_Library_Final   : Called_Procedure;

     function Handle_To_Called_Function is
	new Ada.Unchecked_Conversion (
  	       Source  => System.Address,
	       Target  => Called_Function
     );

     function Handle_To_Called_Procedure is
	new Ada.Unchecked_Conversion (
  	       Source  => System.Address,
                Target  => Called_Procedure
     );

     Handle  : DLLI.Handle;
     use type DLLI.Handle;

     Errors  : Boolean := False;

     Library_Error   : ICS.Chars_Ptr;
     use type ICS.Chars_Ptr;

     Full_Library_Name : constant String := (
         GDIR.Get_Current_Dir & "libmy_lib.so"
     );

begin
     Handle := DLLI.Dl_Open (
	ICS.New_String(Full_Library_Name),
	DLLI.Lazy
     );


     if Handle = DLLI.Empty_Handle then
         TIO.Put_Line (
		"Couldn't open library """ &
		Full_Library_Name & """"
         );
         TIO.Put_Line ("ERROR => " & ICS.Value (DLLI.Dl_Error));
         Errors := True;
     end if;

     -- ADAINIT
     if not Errors then

         Ada_Library_Init := Handle_To_Called_Procedure (
             DLLI.DL_Sym (Handle, ICS.New_String("adainit"))
         );

         Library_Error := DLLI.DL_Error;

         if Library_Error = ICS.Null_Ptr then

             -- "adainit"
             Ada_Library_Init.All;

         else
             TIO.Put_Line ("Couldn't call libraries ""adainit""");
             TIO.Put_Line ("ERROR => " & ICS.Value (Library_Error));
             Errors := True;

         end if;

     end if;

     -- LIBRARY CALL
     if not Errors then

         Library_Function := Handle_To_Called_Function (
             DLLI.DL_Sym (Handle, ICS.New_String("wdiam"))
         );

         Library_Error := DLLI.DL_Error;

         if Library_Error = ICS.Null_Ptr then

             declare
                 Result : Integer;
             begin
                 Result := Library_Function.All;

                 TIO.Put_Line (
		    "What does it all mean ? " &
                     Integer'Image(Result)
                 );
             end;

         else
             TIO.Put_Line (
                 "Error finding the requested library symbol."
             );
             TIO.Put_Line ("ERROR => " & ICS.Value (Library_Error));
             Errors := True;
         end if;

     end if;

     -- ADAFINAL
     if not Errors then

         Ada_Library_Final := Handle_To_Called_Procedure (
             DLLI.DL_Sym (Handle, ICS.New_String("adafinal"))
         );

         Library_Error := DLLI.DL_Error;

         if Library_Error = ICS.Null_Ptr then

             -- "adafinal"
             Ada_Library_Final.All;

         else
             TIO.Put_Line ("Couldn't call libraries ""adainit""");
             TIO.Put_Line ("ERROR => " & ICS.Value (Library_Error));
             Errors := True;
         end if;

     end if;

     if not Errors then
         declare
             Throwaway   : IC.Int;
         begin
             Throwaway := DLLI.DL_Close (Handle);
         end;
     end if;


end Lib_Test;

Putting the pieces together ...

-- make of dynamic-library example
$ gcc -c dynamic_shared_library.adb
$ gnatbind -n dynamic_shared_library
$ gcc -c b~dynamic_shared_library.adb
$ gcc -shared -o libmy_lib.so \
     dynamic_shared_library.o \
     b~dynamic_shared_library.o \
     /usr/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/libgnat.a

     -- Note: Linking against the static gnat-library

-- To illustrate a "read-only" library ...
$ rm *.o
$ chmod -w *.ali

-- Example
$ gnatmake lib_test



-- 
--
-- Merge vertically for real address
--
------------------------------------
-- s n p @ t . o
--  k i e k c c m
------------------------------------




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: C code to Ada
  2003-01-21  0:56       ` chris.danx
@ 2003-01-21  1:26         ` sk
  2003-01-21 15:49           ` Frank J. Lhota
  2003-01-22  1:05           ` chris.danx
  0 siblings, 2 replies; 23+ messages in thread
From: sk @ 2003-01-21  1:26 UTC (permalink / raw)
  To: comp.lang.ada mail to news gateway

 > q&d?

Quick-and-Dirty

-- 
--
-- Merge vertically for real address
--
------------------------------------
-- s n p @ t . o
--  k i e k c c m
------------------------------------




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: C code to Ada
  2003-01-20 22:56     ` James S. Rogers
  2003-01-20 23:05       ` chris.danx
@ 2003-01-21  2:17       ` Jeffrey Carter
  1 sibling, 0 replies; 23+ messages in thread
From: Jeffrey Carter @ 2003-01-21  2:17 UTC (permalink / raw)


James S. Rogers wrote:
> I looked in the GNAT file i-cstrea.ads. It defines the following
> (just an excerpt from the file)
> 
>    subtype voids is System.Address;
>    --  Corresponds to the C type void*
> 
> It appears that you can correctly use System.Address on to
> bind to a C void*.

In C, "pointer" and "address" are used interchangeably, so you can 
generally use System.Address for any C pointer. You can also do 
something like

type Void_Ptr is access all Integer;
pragma Convention (C, Void_Ptr);

and use Void_Ptr for void*. Integer is just a placeholder; it doesn't 
matter what the type designates, since you're never going to dereference 
values of the type, just get them from and pass them to C functions 
(sort of like private types). This has the advantage that Void_Ptr has 
the form of a C pointer even if System.Address doesn't.

If you like things a little thicker, you can define

    type Handle is private;
private
    type Handle is access all Integer;
    pragma Convention (C, Handle);

and use Handle for void*.

-- 
Jeff Carter
"Nobody expects the Spanish Inquisition!"
Monty Python's Flying Circus




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: C code to Ada
  2003-01-20 21:33 ` sk
  2003-01-20 21:43   ` chris.danx
@ 2003-01-21  2:27   ` Jeffrey Carter
  2003-01-21  2:55     ` sk
  2003-01-21  6:18     ` Simon Wright
  1 sibling, 2 replies; 23+ messages in thread
From: Jeffrey Carter @ 2003-01-21  2:27 UTC (permalink / raw)


sk wrote:
> 
> When I was playing with shared-libraries etc (based on the
> Linux-Documentation-Project Program-Library-HOWTO), I did
> "man dlopen" and bound to the functions specified
> 
>  From "man dlopen" ...
> 
> 
> #include <dlfcn.h>
> 
> void *dlopen(const char *filename, int flag);
> const char *dlerror(void);
> void *dlsym(void *handle, char *symbol);
> int dlclose(void *handle);
> 
> So ...
> 
>     ICS => Interfaces.C.Strings;
>     IC  => Interfaces.C;
> 
>     procedure Dlopen (
>         Name : ICS.Chars_Ptr;  -- <= Assign with ICS.New_String
>         Mode : IC.Int
>     );

Looks more like

type Handle is private;

function Dlopen (Name : IC.Char_Array; Mode : IC.Int) return Handle;

> 
>     function DlError return ICS.Chars_Ptr;
>     -- Need to check man for the return parameter ...
>     -- ... this also seems to be an example of the
>     -- message being allocated in called function address
>     -- space (havn't time to double check for now).

This looks OK.

> 
>     procedure Dlsym (Handle : System.Address; Symbol : IC.Char_Array)

Is Symbol an output from this function? Maybe

function Dlsym (File : Handle; Symbol : ICS.Chars_Ptr) return Handle;

Not sure what the return value is.

> 
>     function Dlclose return IC.Int;

function Dlclose (File : Handle) return IC.Int;

private

type Handle is access all Integer;
pragma Convention (C, Handle);

-- pragma Import (C ... for all the operations.

-- 
Jeff Carter
"Nobody expects the Spanish Inquisition!"
Monty Python's Flying Circus




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: C code to Ada
  2003-01-21  2:27   ` Jeffrey Carter
@ 2003-01-21  2:55     ` sk
  2003-01-21 19:33       ` Jeffrey Carter
  2003-01-21  6:18     ` Simon Wright
  1 sibling, 1 reply; 23+ messages in thread
From: sk @ 2003-01-21  2:55 UTC (permalink / raw)
  To: comp.lang.ada mail to news gateway

Hi,

jrcarter@acm.org
 > sk wrote:
 >> When I was playing with shared-libraries etc (based on the
 >
 > <snip>

It looks like messages are arriving in unexpected sequences.

Hopefully the full example I posted gets to you and it atones
for the terrible mistakes I made.

jrcarter@acm.org
 > private
 >
 > type Handle is access all Integer;
 > pragma Convention (C, Handle);

and from the previous message

 > In C, "pointer" and "address" are used interchangeably, ...
 > <snip>

 >   type Handle is private;
 > private
 >    type Handle is access all Integer;
 >    pragma Convention (C, Handle);
 >
 > and use Handle for void*.

This tip is definitely going into my personal collection of
notes in the "Binding for Dummies" section :-)


-- 
--
-- Merge vertically for real address
--
------------------------------------
-- s n p @ t . o
--  k i e k c c m
------------------------------------




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: C code to Ada
  2003-01-21  2:27   ` Jeffrey Carter
  2003-01-21  2:55     ` sk
@ 2003-01-21  6:18     ` Simon Wright
  1 sibling, 0 replies; 23+ messages in thread
From: Simon Wright @ 2003-01-21  6:18 UTC (permalink / raw)


Jeffrey Carter <jrcarter@acm.org> writes:

> function Dlsym (File : Handle; Symbol : ICS.Chars_Ptr) return Handle;

The function return corresponding to a subprogram is an
access-to-subprogram (probably with convention C -- certainly a thin
pointer). I would have thought you'd need several overloadings, which
would probably have to be supplied by the user:

  type p is procedure (x : integer);
  pragma convention (c, p);

  type f is function return integer;
  pragma convention (c, f);

  type ip is access all integer;
  pragma convention (c, ip);        --  OTT for plain integers, I guess

  function dlsym (in_library : handle; named : ics.chars_ptr) return p; 
  function dlsym (in_library : handle; named : ics.chars_ptr) return f; 
  function dlsym (in_library : handle; named : ics.chars_ptr) return ip; 
  pragma import (c, dlsym, "dlsym");



^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: C code to Ada
  2003-01-21  1:26         ` sk
@ 2003-01-21 15:49           ` Frank J. Lhota
  2003-01-22  1:05           ` chris.danx
  1 sibling, 0 replies; 23+ messages in thread
From: Frank J. Lhota @ 2003-01-21 15:49 UTC (permalink / raw)


"sk" <sk@noname.com> wrote in message
news:mailman.6.1043112199.4961.comp.lang.ada@ada.eu.org...
> > q&d?
>
> Quick-and-Dirty

... and the problem with doing anything "quick and dirty" is that you often
fail to be quick, but succeed to be dirty!





^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: C code to Ada
  2003-01-21  2:55     ` sk
@ 2003-01-21 19:33       ` Jeffrey Carter
  0 siblings, 0 replies; 23+ messages in thread
From: Jeffrey Carter @ 2003-01-21 19:33 UTC (permalink / raw)


sk wrote:
> It looks like messages are arriving in unexpected sequences.
> 
> Hopefully the full example I posted gets to you and it atones
> for the terrible mistakes I made.

"You" refers to me here. I was aware of your later messages correcting 
some of your errors, but I hope I also contributed some additional 
assistance on parameter types and modes.

> jrcarter@acm.org
>  >   type Handle is private;
>  > private
>  >    type Handle is access all Integer;
>  >    pragma Convention (C, Handle);
>  >
>  > and use Handle for void*.
> 
> This tip is definitely going into my personal collection of
> notes in the "Binding for Dummies" section :-)

Personally I think "dummies" should be doing bindings :) I'm glad you 
find this useful.

-- 
Jeff Carter
"That was the most fun I've ever had without laughing."
Annie Hall




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: C code to Ada
  2003-01-21  1:26         ` sk
  2003-01-21 15:49           ` Frank J. Lhota
@ 2003-01-22  1:05           ` chris.danx
  1 sibling, 0 replies; 23+ messages in thread
From: chris.danx @ 2003-01-22  1:05 UTC (permalink / raw)


Hi sk,

I can't decipher your email address, can you email me so I can send you 
(a link to) the sources of what I've done when I actually get round to 
doing something?  (muggins here kept prodding his group to do some 
requirements for our project but no-one would listen, saying they're not 
essential since it's not a programming task - it's a multimedia thing. 
Muggins started on the requirements two days ago and low and behold our 
supervisor asked how our requirements spec were taking shape today (4 
weeks after we'd met him), so muggins was right and as a prize for his 
mystical insight has to write up the requirements spec - which is 
difficult for muggins because he's used to software development tasks 
and the methods he has learned are not really applicable to Multimed  :( 
  I hate being right!)


My email is chris.danx "with abode" ntlworld.com

-- 
for personal replies change spamoff to chris




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: C code to Ada
  2003-01-20 18:44 C code to Ada chris.danx
                   ` (2 preceding siblings ...)
  2003-01-21  1:23 ` sk
@ 2003-01-23  8:00 ` sk
  2003-01-24 18:03   ` chris.danx
  3 siblings, 1 reply; 23+ messages in thread
From: sk @ 2003-01-23  8:00 UTC (permalink / raw)
  To: comp.lang.ada mail to news gateway

Hi,

 > Personally I think "dummies" should be doing bindings

I am trying to give up being clever, I always end up with
messes like my original post and, as noted by another
poster, the quick part takes longer than doing it the
dummy way :-)

However, at 2am in the morning, waiting for gcc to
compile, I cannot resist ...

For the entity with the usenetonym of "chris.danx" :

     Whoops    : exception;

     function "+"(A, B : Character) return Character is
         type Byte is mod 2**8;
     begin
         return Character'Val (
             Byte(Character'Pos(A)) +
             Byte(Character'Pos(B)) -
             Byte(Character'Pos(' '))
         );
     end "+";

     function "+"(S1, S2 : String) return String is
        Result  : String (1 .. S1'Length) := (Others => ' ');
     begin
         if S1'Length /= S2'Length then
             raise Whoops;
         end if;

         for C in Result'Range loop
             Result (C) := S1(C) + S2(C);
         end loop;

         return Result;
     end "+";

... and of course when I stumble across this code two
months from now, I will be totally lost wondering why
on earth I wanted to add two characters together and
subtract a space ? I dare not speculate at the results
other than for decoding my e-mail address :-)

(chris.danx : I e-mailed you, don't know if I got *your*
address correct).

-- 
--
-- Merge vertically for real address
--
------------------------------------
-- s n p @ t . o
--  k i e k c c m
------------------------------------




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: C code to Ada
  2003-01-23  8:00 ` sk
@ 2003-01-24 18:03   ` chris.danx
  0 siblings, 0 replies; 23+ messages in thread
From: chris.danx @ 2003-01-24 18:03 UTC (permalink / raw)


sk wrote:

> (chris.danx : I e-mailed you, don't know if I got *your*
> address correct).

You did, I just haven't had a mo' to get back to you.

I've been thinking about the binding and am going to do something later...


Chris





^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: C code to Ada
  2003-01-21  1:23 ` sk
@ 2003-01-25 21:28   ` chris.danx
  2003-01-26  2:16     ` sk
                       ` (2 more replies)
  0 siblings, 3 replies; 23+ messages in thread
From: chris.danx @ 2003-01-25 21:28 UTC (permalink / raw)


sk wrote:
> Hi yet again,
> 
> Since I messed it all up pretty bad, here you go ...
> (feel free to clean it up, thicken it and release it
> back to CLA, hint hint :-)


ok I've gotten to the point where I've tidied it up, added separate 
handle types for libraries and code within the library, and added a few 
modes that where missing (I am waiting to finish the adainit & adafinal 
calls and write an example before I email you sk).  Now I'm in the 
process of writing nice tidy calls to adainit & adafinal - code that 
will be called over and over - and am a bit confused.

Should I use ada.unchecked_conversion (which works in the example) or 
system.address_to_access_conversion?  (or something else entirely?).

I was going to use an address_to_access_conversion but wouldn't that 
give an access to an access to a procedure for the following type?

type Procedure_Access is access procedure;


The package instantiation would be

package X
    is new System.Address_To_Access_Conversion (Procedure_Access);


What's the best way to do the conversion so the procedure can be called? 
  In the meantime I will stick with Unchecked_Conversion, which works 
*here* even if it's a bit risky.


Chris




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: C code to Ada
  2003-01-25 21:28   ` chris.danx
@ 2003-01-26  2:16     ` sk
  2003-01-26  3:04     ` sk
       [not found]     ` <3E33502D.7030503@noname.com>
  2 siblings, 0 replies; 23+ messages in thread
From: sk @ 2003-01-26  2:16 UTC (permalink / raw)
  To: comp.lang.ada mail to news gateway

 > I am waiting to finish the adainit & adafinal calls and
 > write an example before I email you sk).  Now I'm in the
 > process of writing nice tidy calls to adainit & adafinal

adainit and final are part of the Gnat building process
and are automatically bound and linked by gnatmake (or
gcc -> gnatbind -> gnatlink).

The "-n" option tells the gnat binder to make them
available to the "outside" world.

They are still only accessible as "system addresses"
from the "dlsym" call (I suppose you could wrap
that but I am not sure of the necessity).

The thing that you need to look at for adainit
and adafinal is closely associating them with
the "init" and "fini" functions which the
dynamic/linker/loader automatically calls upon
the loading and unloading of a dynamic library.

See the library howto. I have not gone this far,
but I was planning to explore the possibilities

 > system.address_to_access_conversion?
Looks like a good idea but I haven't explored it.
The name itself suggests that it is ideal.

 > Unchecked_Conversion, which works *here* even
 > if it's a bit risky.

There are times when the use of Unchecked_Conversion
is dubious, there are times when it isn't.

Others will probably tell you differently, but
if the "dlsym" returns system.addresses (or void *)
to everything that is available (variables, functions
or procedures), then Unchecked_Conversion is probably
one of the best solutions.

If you look at a "Simon Wright" post in this thread,
you will see that the permutations of trying to
provide a binding for every possiblity is unmanageable.

-- 
--
-- Merge vertically for real address
--
------------------------------------
-- s n p @ t . o
--  k i e k c c m
------------------------------------




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: C code to Ada
  2003-01-25 21:28   ` chris.danx
  2003-01-26  2:16     ` sk
@ 2003-01-26  3:04     ` sk
       [not found]     ` <3E33502D.7030503@noname.com>
  2 siblings, 0 replies; 23+ messages in thread
From: sk @ 2003-01-26  3:04 UTC (permalink / raw)
  To: comp.lang.ada mail to news gateway

(I don't think the first message got to cla, so
sorry if this repeats)

 > I am waiting to finish the adainit & adafinal calls and
 > write an example before I email you sk).  Now I'm in the
 > process of writing nice tidy calls to adainit & adafinal

adainit and final are part of the Gnat building process
and are automatically bound and linked by gnatmake (or
gcc -> gnatbind -> gnatlink).

The "-n" option tells the gnat binder to make them
available to the "outside" world.

They are still only accessible as "system addresses"
from the "dlsym" call (I suppose you could wrap
that but I am not sure of the necessity).

The thing that you need to look at for adainit
and adafinal is closely associating them with
the "init" and "fini" functions which the
dynamic/linker/loader automatically calls upon
the loading and unloading of a dynamic library.

See the library howto. I have not gone this far,
but I was planning to explore the possibilities

 > system.address_to_access_conversion?
Looks like a good idea but I haven't explored it.
The name itself suggests that it is ideal.

 > Unchecked_Conversion, which works *here* even
 > if it's a bit risky.

There are times when the use of Unchecked_Conversion
is dubious, there are times when it isn't.

Others will probably tell you differently, but
if the "dlsym" returns system.addresses (or void *)
to everything that is available (variables, functions
or procedures), then Unchecked_Conversion is probably
one of the better (at least cleaner) solutions.

If you look at a "Simon Wright" post in this thread,
you will see that the permutations of trying to
provide a binding for every possiblity is unmanageable.

-- 
--
-- Merge vertically for real address
--
------------------------------------
-- s n p @ t . o
--  k i e k c c m
------------------------------------




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: C code to Ada
       [not found]     ` <3E33502D.7030503@noname.com>
@ 2003-01-26  5:37       ` sk
  0 siblings, 0 replies; 23+ messages in thread
From: sk @ 2003-01-26  5:37 UTC (permalink / raw)
  To: comp.lang.ada mail to news gateway

I didn't explain very well and rereading leaves me confused so,

AM := Ada main
CM := "C" main
ADL := Ada Dynamic Library
CDL := C Dynamic Library
GCC := gcc
DLLI := Dynamic Linking Loading Interface

AM -- part of the gnatmake creates adainit, adafinal

       if it wants to dynamically load, it

           "with Dynamic_Linking_Loading_Interface"

       and uses its functionality.

CM -- needs to call adainit and adafinal if it calls an ADL, but
       not a CDL.

ADL -- part of the gnatmake creates an externally callable adainit,
        adafinal for the *library* (gnatbind -n)

CDL -- no intrinsic adainit adafinal

GCC -- when building dynamic libraries recognizes or provides
        "init" and "fini" (cannot remember which).

DLLI -- when loading an ADL, needs to consider the adainit,adafinal
        for the library.

        when loading a CDL, adainit and adafinal are not necessarily
        present.

The Ada DLLI has its own adainit and adafinal which are incorporated
into the Ada mains adainit and adafinal. These adainit and adafinal
routines should not be wrapped and made available to the calling
routines, gnatmake does this and they become part of the Ada main
adainti and adafinal.

Overall, wrapping "adainit" and "adafinal" is not part of the
DLLI. Whether the functions need wrapping is entirely dependent
upon the library being loaded, not the DLLI.

Also, for DLLI, the "init" and "fini" are red-herrings (not deliberate)
these are only issues for building a dynamic-library, not the loading
of them thru DLLI.

Hopefully this is better

-- 
--
-- Merge vertically for real address
--
------------------------------------
-- s n p @ t . o
--  k i e k c c m
------------------------------------




^ permalink raw reply	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2003-01-26  5:37 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-01-20 18:44 C code to Ada chris.danx
2003-01-20 21:33 ` sk
2003-01-20 21:43   ` chris.danx
2003-01-20 22:56     ` James S. Rogers
2003-01-20 23:05       ` chris.danx
2003-01-21  2:17       ` Jeffrey Carter
2003-01-20 23:32     ` sk
2003-01-21  0:56       ` chris.danx
2003-01-21  1:26         ` sk
2003-01-21 15:49           ` Frank J. Lhota
2003-01-22  1:05           ` chris.danx
2003-01-21  2:27   ` Jeffrey Carter
2003-01-21  2:55     ` sk
2003-01-21 19:33       ` Jeffrey Carter
2003-01-21  6:18     ` Simon Wright
     [not found] ` <3E2C6B2B.5090600@noname.com>
2003-01-20 21:39   ` sk
2003-01-21  1:23 ` sk
2003-01-25 21:28   ` chris.danx
2003-01-26  2:16     ` sk
2003-01-26  3:04     ` sk
     [not found]     ` <3E33502D.7030503@noname.com>
2003-01-26  5:37       ` sk
2003-01-23  8:00 ` sk
2003-01-24 18:03   ` chris.danx

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox