comp.lang.ada
 help / color / mirror / Atom feed
From: rgilbert@orl.mmc.com (Bob Gilbert)
Subject: Re: Interfacing Ada to Unix/execl var. arg. li
Date: 17 Oct 1994 18:18:54 GMT
Date: 1994-10-17T18:18:54+00:00	[thread overview]
Message-ID: <37uf6e$7ik@theopolis.orl.mmc.com> (raw)
In-Reply-To: 37ioh5$h9v@goanna.cs.rmit.oz.au

In article h9v@goanna.cs.rmit.oz.au, Dale Stanbrough <dale@goanna.cs.rmit.edu.au> () writes:
->I am currently trying to develop a simple Ada/Unix binding (not Posix!)
->that will allow students to use the packages with a minimum of fuss.
->(i.e. reading the Unix man page/Unix programming text would give them
->a fair idea of how to use the Ada version).
->
->I've got stuck on how to implement Ada calls to the corresponding
->variable parameter list C functions, specifically execl.
->
->Also the solutions I have written currently look clunky. I was wondering if 
->there was a better way to implement these features.
->
->
->Problem #3.
->-----------
->How to specify and pass in a variable list of pointers to a C program.
->(Yes, I know we've been down this path before, but generally w.r.t.
->printf, which has varying types of parameters, as well as number).
->
->-----------------------------------------------------------
->-- Calling profile for execl in C.
->--
->--      int execl(path, arg0 [ , arg1,...  , argn ] (char *)0)
->--      char *path, *arg0, *arg1, ..., *argn;
->--
->
->function execl( path            :string;
->                arg_list        :string_array) return integer is
->
->        ---------------------------------------------
->        function C_execl( path      :system.address;
->                          C_arg_list:               --?? What type here?
->                          return interfaces.c.int;
->        pragma import(C, C_execl, "execl");
->
->begin
->      -- how to do this?
->end;

I don't know all (any of) the details involved with C_execl but...

If it is desired for the arg_list to contain a list of values 
of possibly different types, and you would like to protect the
procedure/function from invalid (wrong type) arguments (maybe
better than just passing an array of addresses to who knows what),
and you don't want to have to overload the function with all the
possible argument lengths, then how about something like:

  -- enumerate all possible argument types
  type ARG is (Arg_1, Arg_2, Arg_3, ... Arg_n, Null_Arg);

  type ARG_RECORD is (Arg_Name : ARG := Null_Arg) is
    record
      case Arg_Name is
        when Arg _1 =>
          Arg_1 : ARG_1_TYPE;
        when Arg_2 =>
          Arg_2 : ARG_2_TYPE;
         .
         .
        when Arg_n =>
          Arg_n : ARG_N_TYPE;
        when Null_Arg => null;
    end record;

  type ARG_LIST is array (NATURAL range <>) of ARG_RECORD;

  Null_Arg_List : constant ARG_LIST(1..0);

  ---------------------------------------------------------
  -- The following procedure (or function) can be invoked as:
  --
  --  C_Excel(My_Path, ARG_LIST'((Arg_1, Data_1),
  --                             (Arg_3, Data_2),
  --                             ...
  --                             (Arg_n, Data_n)));
  ---------------------------------------------------------
  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;
      .
      .
      .
    return {whatever};
  end C_Excel;

pragma (Disclaimer_On);

  Hope I didn't make too many errors in the above example,
  I didn't get a chance to see if it actually compiles.
  If their are any problems that I didn't account for (like
  maybe the Arg_Address_List does not have a valid lifetime
  or something), or if I'm just completely off base, I'm sure 
  others will correct me.

-Bob




  parent reply	other threads:[~1994-10-17 18:18 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 ` Bob Gilbert [this message]
1994-10-19  3:30   ` Interfacing Ada to Unix/execl var. arg. list function? Dale Stanbrough
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