comp.lang.ada
 help / color / mirror / Atom feed
From: Dale Stanbrough <dale@goanna.cs.rmit.edu.au>
Subject: Re: Interfacing Ada to Unix/execl var. arg. list function?
Date: 19 Oct 1994 03:30:11 GMT
Date: 1994-10-19T03:30:11+00:00	[thread overview]
Message-ID: <3823s3$ioq@goanna.cs.rmit.oz.au> (raw)
In-Reply-To: 37uf6e$7ik@theopolis.orl.mmc.com

In article <37uf6e$7ik@theopolis.orl.mmc.com> Bob Gilbert,
rgilbert@orl.mmc.com writes:
>  function C_Excel(Path     : System.ADDRESS;
>                   Arg_List : ARG_LIST := Null_Arg_List) return
Interfaces.c.int is
>
>    type ADDRESS_ARRAY is array (NATURAL range Arg_List'range) of
System.ADDRESS;
>    Arg_Address_List : ADDRESS_ARRAY;
>
>  begin
>    for i in Arg_List'range loop          -- Build Arg_Address_List for C call
>      case Arg_List is
>          Arg_List(i) := Arg_List(i).Arg_1'address;
>        when Arg_2 =>
>          Arg_List(i) := Arg_List(i).Arg_2'address;
>         .
>         .
>        when Arg_n =>
>          Arg_List(i) := Arg_List(i).Arg_n'address;
>        when Null_Arg => null;
>      end case;
>    end loop;
>      .
>      .

The main problem lies in here - how do you assemble the various
arguments into a call to a pragma import'ed C routine?

The only solution appears to be to direct pass parameters via 
aggregates (as your solution suggests) and then call an equivalent
non-variable argument list function (e.g. execl - execv).

The code below works, but I am unsure of whether it is memory safe,
and whether all memory gets reclaimed at the end of the function.


function execv( path    :string;
                argv    :string_array := null_arg_list) return integer is

        C_path  :constant interfaces.c.char_array(1..path'length+1)
                                := interfaces.c.to_C(path);
        type address_array is array(1..argv'length + 1) of system.address;

        C_argv  :address_array;
        index   :integer;

        ------------------------------------------------------------
        function C_execv(       path    :system.address;
                                C_arg_list:system.address)
                                        return interfaces.c.int;
        pragma import(C, C_execv, "execv");
        ------------------------------------------------------------
begin
        -- set up the array of pointers to the strings
        index := 0;
        for i in argv'range loop
                index := index + 1;
                -- can Ada release the memory pointed returned by 
                -- interfaces.c.to_c after the loop (leaving the pointer
                -- pointing at nothing), or will it wait until the end of
                -- the function, or will it never free up the memory?

                C_argv(index) := interfaces.c.to_c(argv(i).all)(0)'address;
        end loop;

        -- append C style null to the end of the array of addresses
        -- (presumes that system.null_address = C style null).

        C_argv(C_argv'last) := system.null_address;

        -- pass the address of the first element of each
        -- parameter, as C expects.

        return integer(C_execv( C_path(1)'address,
                                C_argv(1)'address));
end execv;


Thanks,

Dale

-------------------------------------------------------------
Dale Stanbrough, RMIT, Melbourne, Australia, dale@rmit.edu.au
GNU Ada 94 (GNAT) => the best $0 you'll ever spend.
Available for DOS, Linux, OS/2, Sun Sparc, Sun Solaris, ...
Coming to a GNU supported platform near you soon...



  reply	other threads:[~1994-10-19  3:30 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1994-10-13  7:44 Interfacing Ada to Unix/execl var. arg. list function? Dale Stanbrough
1994-10-13  9:21 ` David Emery
1994-10-13 12:22 ` Robert Dewar
1994-10-13 17:37   ` Mark A Biggar
1994-10-13 18:46     ` Robert Dewar
1994-10-14 13:55   ` Norman H. Cohen
1994-10-13 13:31 ` Nicolas Courtel
1994-10-13 19:28 ` Tucker Taft
1994-10-14 13:55 ` Interfacing Ada to Unix/execl var ncohen
1994-10-17 18:18 ` Interfacing Ada to Unix/execl var. arg. li Bob Gilbert
1994-10-19  3:30   ` Dale Stanbrough [this message]
1994-10-20 12:42     ` Interfacing Ada to Unix/execl var. arg Bob Gilbert
1994-10-20  4:18   ` Interfacing Ada to Unix/execl var. arg. li S M Ryan
replies disabled

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