comp.lang.ada
 help / color / mirror / Atom feed
* Newbie GNAT question
@ 2003-10-24 20:18 David Gressett
  2003-10-24 21:41 ` David Gressett
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: David Gressett @ 2003-10-24 20:18 UTC (permalink / raw)


I am trying to use the GNAT.OS_Lib package so that I can construct an
argument list and then spawn another program using that argument list.
The argument list will contain filenames which the spawned program
will open.

I'm being defeated by Ada's strong typing. Is there any example code
for this on the Web somewhere, or could somebody produce a quick
example of how to do it?



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

* Re: Newbie GNAT question
  2003-10-24 20:18 Newbie GNAT question David Gressett
@ 2003-10-24 21:41 ` David Gressett
  2003-10-25  2:41 ` Gautier Write-only
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 9+ messages in thread
From: David Gressett @ 2003-10-24 21:41 UTC (permalink / raw)


On Fri, 24 Oct 2003 15:18:59 -0500, David Gressett
<gresset1@airmail.spam> wrote:

>I am trying to use the GNAT.OS_Lib package so that I can construct an
>argument list and then spawn another program using that argument list.
>The argument list will contain filenames which the spawned program
>will open.
>
>I'm being defeated by Ada's strong typing. Is there any example code
>for this on the Web somewhere, or could somebody produce a quick
>example of how to do it?

Never mind --- I was using Access where I should have used
Unrestricted_Access.



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

* Re: Newbie GNAT question
  2003-10-24 20:18 Newbie GNAT question David Gressett
  2003-10-24 21:41 ` David Gressett
@ 2003-10-25  2:41 ` Gautier Write-only
  2003-10-26 12:22 ` Martin Krischik
  2003-10-26 15:09 ` sk
  3 siblings, 0 replies; 9+ messages in thread
From: Gautier Write-only @ 2003-10-25  2:41 UTC (permalink / raw)


David Gressett:

> I am trying to use the GNAT.OS_Lib package so that I can construct an
> argument list and then spawn another program using that argument list.
> The argument list will contain filenames which the spawned program
> will open.

An inspiration:

  procedure EXEC(name: String; param:String:="") is
    num_params : integer := 1;
  begin
    for i in param'range loop
      if param(i) = ' ' then
        num_params := num_params + 1; 
      end if;
    end loop;
    DECLARE
      a_param_list : Argument_List(1..num_params);
      last_start   : integer := param'first;
      counter      : integer := 1;
      ok           : Boolean;
    BEGIN
      for i in param'range loop
        if param(i) = ' ' then
          a_param_list(counter) := new String'(param(last_start..i-1));
          last_start := i+1;
          counter := counter + 1;
        end if;
      end loop;
      a_param_list(num_params) := 
        new String'(param(last_start..param'last));
      Spawn(name, a_param_list, ok);
      if not ok then
        raise_exception(Exec_failed'identity, name & " " & param);
      end if;
    END; -- DECLARE
  end EXEC;

> I'm being defeated by Ada's strong typing. Is there any example code
> for this on the Web somewhere, or could somebody produce a quick
> example of how to do it?

(about Unrestricted_Access): do you need
'Access or 'Unrestricted_Access in your code around spawn ?
________________________________________________________
Gautier  --  http://www.mysunrise.ch/users/gdm/gsoft.htm

NB: For a direct answer, e-mail address on the Web site!



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

* Re: Newbie GNAT question
  2003-10-24 20:18 Newbie GNAT question David Gressett
  2003-10-24 21:41 ` David Gressett
  2003-10-25  2:41 ` Gautier Write-only
@ 2003-10-26 12:22 ` Martin Krischik
  2003-10-26 15:09 ` sk
  3 siblings, 0 replies; 9+ messages in thread
From: Martin Krischik @ 2003-10-26 12:22 UTC (permalink / raw)


David Gressett wrote:

> I am trying to use the GNAT.OS_Lib package so that I can construct an
> argument list and then spawn another program using that argument list.
> The argument list will contain filenames which the spawned program
> will open.
> 
> I'm being defeated by Ada's strong typing. Is there any example code
> for this on the Web somewhere, or could somebody produce a quick
> example of how to do it?

http://adacl.sourceforge.net/html/______Include__AdaCL-OS-Command__adb.htm

With Regards

Martin.
-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com




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

* Re: Newbie GNAT question
  2003-10-24 20:18 Newbie GNAT question David Gressett
                   ` (2 preceding siblings ...)
  2003-10-26 12:22 ` Martin Krischik
@ 2003-10-26 15:09 ` sk
  2003-10-27 15:53   ` Martin Krischik
  3 siblings, 1 reply; 9+ messages in thread
From: sk @ 2003-10-26 15:09 UTC (permalink / raw)
  To: comp.lang.ada

David Gressett <gresset1@airmail.spam>:
 > ... I can construct an argument list ...

Try the

     Gnat.Os_Lib.Arguement_String_To_List (...)

function.

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




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

* Re: Newbie GNAT question
  2003-10-26 15:09 ` sk
@ 2003-10-27 15:53   ` Martin Krischik
  2003-10-28 14:08     ` sk
       [not found]     ` <3F9E7848.1080304@myob.com>
  0 siblings, 2 replies; 9+ messages in thread
From: Martin Krischik @ 2003-10-27 15:53 UTC (permalink / raw)


sk wrote:

> David Gressett <gresset1@airmail.spam>:
>  > ... I can construct an argument list ...
> 
> Try the
> 
>      Gnat.Os_Lib.Arguement_String_To_List (...)
> 
> function.

However there is not Free_Arguement_List (...) in Gnat.Os_Lib. But don't
despair there is one in AdaCL.OS.Command.

With Regards

Martin
-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com




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

* Re: Newbie GNAT question
  2003-10-27 15:53   ` Martin Krischik
@ 2003-10-28 14:08     ` sk
  2003-10-29 16:18       ` Martin Krischik
       [not found]     ` <3F9E7848.1080304@myob.com>
  1 sibling, 1 reply; 9+ messages in thread
From: sk @ 2003-10-28 14:08 UTC (permalink / raw)
  To: comp.lang.ada

krischik@users.sourceforge.net:
 > However there is not Free_Arguement_List (...) in
 > Gnat.Os_Lib. But don't despair there is one in
 > AdaCL.OS.Command.

Do you need one ?

declare
     Cmd  : constant String := "/bin/ls";
     Args : constant String := Gnat.Os_Lib.Arguement_String_To_List ("-al");
     Success : Boolean := False;
begin
     Gnat.Os_Lib.Spawn (Cmd, Args, Success);

     -- Check success and continue ...

end;

OR

    ....
    Gnat.Os_Lib.Spawn (
        Command => "/bin/ls",
        Args => Gnat.Os_Lib.Argument_String_To_List ("-al"),
        Success => Success
    );

I believe that there is no need for a "Free" in either case :-)


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




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

* Re: Newbie GNAT question
       [not found]     ` <3F9E7848.1080304@myob.com>
@ 2003-10-28 14:40       ` sk
  0 siblings, 0 replies; 9+ messages in thread
From: sk @ 2003-10-28 14:40 UTC (permalink / raw)
  To: comp.lang.ada

Oops ... the ghost of RD got me so I had to fix it :-)

---------------------------------------------------------------

with Gnat.Os_Lib;

procedure GOSL_Test is

     Ok : Boolean := False;

begin

     -- Method One.
     declare
         Cmd  : constant String := "/bin/ls";
         Args : constant Gnat.Os_Lib.Argument_List := (
             Gnat.Os_Lib.Argument_String_To_List ("-al").All
         );
         Success : Boolean := False;
     begin
         Gnat.Os_Lib.Spawn (Cmd, Args, Success);

         -- Check success and continue ...
     end;

     -- Method Two.
     Gnat.Os_Lib.Spawn (
         Program_Name => "/bin/ls",
         Args         => Gnat.Os_Lib.Argument_String_To_List("-al").all,
         Success      => Ok
     );

end GOSL_Test;

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




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

* Re: Newbie GNAT question
  2003-10-28 14:08     ` sk
@ 2003-10-29 16:18       ` Martin Krischik
  0 siblings, 0 replies; 9+ messages in thread
From: Martin Krischik @ 2003-10-29 16:18 UTC (permalink / raw)


sk wrote:

> krischik@users.sourceforge.net:
>  > However there is not Free_Arguement_List (...) in
>  > Gnat.Os_Lib. But don't despair there is one in
>  > AdaCL.OS.Command.
> 
> Do you need one ?
> 
> declare
>      Cmd  : constant String := "/bin/ls";
>      Args : constant String := Gnat.Os_Lib.Arguement_String_To_List
>      ("-al"); Success : Boolean := False;
> begin
>      Gnat.Os_Lib.Spawn (Cmd, Args, Success);
> 
>      -- Check success and continue ...
> 
> end;
> 
> OR
> 
>     ....
>     Gnat.Os_Lib.Spawn (
>         Command => "/bin/ls",
>         Args => Gnat.Os_Lib.Argument_String_To_List ("-al"),
>         Success => Success
>     );
> 
> I believe that there is no need for a "Free" in either case :-)

If you like memory leaks or have a special GNAT with full gargage collection
then not.

For anybody else, look closer: Args is an Argument_List:

procedure Spawn
     (Program_Name : String;
      Args         : Argument_List;
      Success      : out Boolean)

An Argument_List is an of access to string:

type String_Access is access all String;
subtype Argument_List is String_List;

And unlike common belive Gnat.Os_Lib.Spawn will not free the Args ot any of
the strings contained:

   begin
      --  Copy arguments into a local structure

      for K in N_Args'Range loop
         N_Args (K) := new String'(Args (K).all);
      end loop;

      --  Normalize those arguments

      Normalize_Arguments (N_Args);

      --  Call spawn using the normalized arguments

      Spawn (N_Args);

      --  Free arguments list

      for K in N_Args'Range loop
         Free (N_Args (K));
      end loop;
   end Spawn_Internal;

It makes copy for  Normalize_Arguments and frees that copy but not the
original. BTW:  it can't since it is "access _all_ String;" - you can
create the Args with strings from the stack.

You can of course use AdaCL.OS.Command:

http://adacl.sourceforge.net/html/______Include__AdaCL-OS-Command__ads.htm

Which does not leak memory and who's  Argument_String_To_List is lot more
powerfull:

    --
    --  Convert a String into an argument list. Unlike the
    --  version of GNAT.OS this version strips the double
    --  quotes. You will need this if you want to execute
    --  command with spaces inside a filename.
    --
    --  Also you can freely choose the Seperator, Quotation
    --  and escape character. This makes it easier to compose
    --  commandlines where you need the characters usualy used
    --  for this purpouse.
    --
    function Argument_String_To_List (
        --  Argument string. Arguments with spaces should be
        --  quoted.
        Arg_String : in String;
        --  Character seperating Arguments. Other options
        --  which might be used here are:
        --
        --  Ada.Characters.Latin_1.NUL
        --  Ada.Characters.Latin_1.LF
        Seperator : in Character := Ada.Characters.Latin_1.Space;
        --  Character to enclose Arguments. This overwrites
        --  the seperator character. Other options
        --  which might be used here are:
        --
        --  Ada.Characters.Latin_1.Apostrophe
        Quotation : in Character := Ada.Characters.Latin_1.Quotation;
        --  Character to remove special meaning of next
        --  character. Other options which might be used here are:
        --
        --  Ada.Characters.Latin_1.ESC
        Escape : in Character := Ada.Characters.Latin_1.Reverse_Solidus)
    return
        GNAT.OS_Lib.Argument_List_Access;

With Regards

Martin

-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com




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

end of thread, other threads:[~2003-10-29 16:18 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-10-24 20:18 Newbie GNAT question David Gressett
2003-10-24 21:41 ` David Gressett
2003-10-25  2:41 ` Gautier Write-only
2003-10-26 12:22 ` Martin Krischik
2003-10-26 15:09 ` sk
2003-10-27 15:53   ` Martin Krischik
2003-10-28 14:08     ` sk
2003-10-29 16:18       ` Martin Krischik
     [not found]     ` <3F9E7848.1080304@myob.com>
2003-10-28 14:40       ` sk

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