comp.lang.ada
 help / color / mirror / Atom feed
From: Martin Krischik <krischik@users.sourceforge.net>
Subject: Re: Newbie GNAT question
Date: Wed, 29 Oct 2003 17:18:30 +0100
Date: 2003-10-29T17:18:30+01:00	[thread overview]
Message-ID: <2769004.oqcxehEVWl@linux1.krischik.com> (raw)
In-Reply-To: mailman.236.1067350162.25614.comp.lang.ada@ada-france.org

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




  reply	other threads:[~2003-10-29 16:18 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
     [not found]     ` <3F9E7848.1080304@myob.com>
2003-10-28 14:40       ` sk
replies disabled

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