comp.lang.ada
 help / color / mirror / Atom feed
From: Per Sandberg <per.s.sandberg@bahnhof.se>
Subject: Re: A suggestion about interfacing with C
Date: Mon, 2 May 2016 21:40:24 +0200
Date: 2016-05-02T21:40:24+02:00	[thread overview]
Message-ID: <J2OVy.79363$XE7.79168@fx26.fr7> (raw)
In-Reply-To: <45cb9b19-b470-4649-843a-7b9f88411e6e@googlegroups.com>

Well
I would have done it in two steps
1) generate a full low-level interface using -fdump-ada-spec
2) Do the high level interface using the generated specs in the private 
part.
I usually use a script like the following:
##################################################################################
#!/bin/bash
mkdir -p .gen
rm -rf src/gen
mkdir -p src/gen

(cd /usr/include/ ; find libnl3/ -type f -name "*.h")  | \
		grep -v -e qdisc/hfsc.h  \
				-e netlink/hashtable.h \
				-e route/link/info-api.h \
				-e route/link/sit.h \
				-e route/tc-api.h \
				-e route/link/api.h \
				-e route/link/ip6tnl.h \
				-e netlink/cache-api.h | sed -e "s-^-#include <-" -e "s-\$->-" | 
while read i ; do
	echo $i >.gen/gen.cpp
	(cd .gen; gcc -I /usr/include/libnl3/ -c -fdump-ada-spec gen.cpp)
done

# patch up all standard stuff (size_t, int32, uint32, ...)
# Get rid of dependencies to bits_*.h ctype.h ans alike.
sed -f sed sed/all.sed .gen/*.ads -i

# And file specific patches.
for i in sed/*.sed ; do
	name=$(basename $i .sed).ads
	echo .gen/$name
	if [[ -e ${name} ]] ; then
		sed -f $i -i ${name}
	fi
done

cp .gen/libnl3_* src/gen
##################################################################################

Den 2016-05-02 kl. 18:45, skrev mockturtle:
> Dear all,
> I need to interface with a C library (libnl, for using Netlink in Linux).  The library provides a "socket" represented by a struct nl_sock, but the user (the library user, that is, myself) sees only a pointer to nl_sock, since the library provides constructors, destructors and everything is needed to manipulate the socket. In a sense, the user can consider the pointer just as an "opaque handler" rather than a pointer. (I must say that my first impression of the API is definitively positive).
>
> In Ada I would like to provide a slightly thick package that hides the C type behind a, say, Netlink_Socket type that could be a record holding the pointer to nl_sock. Note that since libnl uses said pointer as an opaque handle, I just need to store the "bitstring" representing its value, in order to pass it to the C library.
>
> I was wandering about the best way to implement the Ada "view"  of the pointer to nl_sock.
>
> * I could use Interfaces.C.Pointers, but it seems me an overkill since I do not need to do pointer arithmetic.  Moreover, I would need to define a record equivalento to struct nl_sock (although maybe in my case a null record could suffice)
>
> * I could convert it in C to a char* and then using Interfaces.C.Strings.  (A bit dirty, but not too much...)
>
> * I could convert it to an integer... (no, definitively too dirty)
>
> * others?
>
> What would you suggest?
>
> Keep in mind that the software will run on Linux only (Netlink is specific to Linux, as long as I know) and at 99.99% only on x86 and similar.
>
> Thank you in advance for your help
>
> Riccardo
>

  parent reply	other threads:[~2016-05-02 19:40 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-02 16:45 A suggestion about interfacing with C mockturtle
2016-05-02 18:46 ` Simon Wright
2016-05-02 19:07 ` Jeffrey R. Carter
2016-05-02 19:40 ` Per Sandberg [this message]
2016-05-02 20:14 ` Maciej Sobczak
2016-05-03 21:53 ` mockturtle
replies disabled

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