From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!feeder.eternal-september.org!news.fcku.it!peer01.fr7!news.highwinds-media.com!fx26.fr7.POSTED!not-for-mail Subject: Re: A suggestion about interfacing with C Newsgroups: comp.lang.ada References: <45cb9b19-b470-4649-843a-7b9f88411e6e@googlegroups.com> From: Per Sandberg User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.0 MIME-Version: 1.0 In-Reply-To: <45cb9b19-b470-4649-843a-7b9f88411e6e@googlegroups.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Message-ID: X-Complaints-To: abuse@usenet.se NNTP-Posting-Date: Mon, 02 May 2016 19:40:25 UTC Organization: usenet.se Date: Mon, 2 May 2016 21:40:24 +0200 X-Received-Bytes: 3672 X-Received-Body-CRC: 1314536679 Xref: news.eternal-september.org comp.lang.ada:30338 Date: 2016-05-02T21:40:24+02:00 List-Id: 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 >