comp.lang.ada
 help / color / mirror / Atom feed
* Exec shell command with GNAT
@ 2003-01-13 16:58 JuK
  2003-01-13 17:59 ` David C. Hoos
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: JuK @ 2003-01-13 16:58 UTC (permalink / raw)


Hello 
I want to execute a shell command in my ADA application :

 function EXEC_SHELL_COMMAND (COMMAND : in STRING) return EXIT_STATUS ;


How Can I do that  ?

( SOLARIS with GNAT 3.14 )

Thank you



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

* Re: Exec shell command with GNAT
  2003-01-13 16:58 Exec shell command with GNAT JuK
@ 2003-01-13 17:59 ` David C. Hoos
  2003-01-14  8:58   ` Thomas Wolf
  2003-01-14 10:34   ` Lutz Donnerhacke
  2003-01-13 18:52 ` Per Sandbergs
  2003-01-13 19:01 ` Jeffrey Carter
  2 siblings, 2 replies; 8+ messages in thread
From: David C. Hoos @ 2003-01-13 17:59 UTC (permalink / raw)


Here's such a function I've been using for some years:
It should be portable across compilers and OSs, as long
as the C-runtime library has the "system" function.
------------------------------------------------------------------------
-- $Source: /usr/local/cvsroot/execute_shell_command.adb,v $
-- $Revision: 1.1 $
-- $Date: 1999/08/11 12:58:58 $
-- $Author: hoosd $
-- $State: Exp $
-- Revision History:
-- $Log: execute_shell_command.adb,v $
-- Revision 1.1  1999/08/11 12:58:58  hoosd
-- Initial checkin
--
--
-- Execute_Shell_Command
-- Purpose:
--   This library-level function is an Ada interface to the "system"
--   command of the C run-time library.
------------------------------------------------------------------------
with Ada.Characters.Latin_1;
with System;
function Execute_Shell_Command
  (The_Command : String) return Integer is

   function Execute
              (The_Command_Address : System.Address) return Integer;
   pragma Import (C, Execute, "system");
   The_Nul_Terminated_Command_String : constant String :=
     The_Command & Ada.Characters.Latin_1.Nul;
begin
   return Execute (The_Nul_Terminated_Command_String'Address);
end Execute_Shell_Command;

----- Original Message ----- 
From: "JuK" <calvinix@caramail.com>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: Monday, January 13, 2003 10:58 AM
Subject: Exec shell command with GNAT


> Hello 
> I want to execute a shell command in my ADA application :
> 
>  function EXEC_SHELL_COMMAND (COMMAND : in STRING) return EXIT_STATUS ;
> 
> 
> How Can I do that  ?
> 
> ( SOLARIS with GNAT 3.14 )
> 
> Thank you
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
> 



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

* Re: Exec shell command with GNAT
  2003-01-13 16:58 Exec shell command with GNAT JuK
  2003-01-13 17:59 ` David C. Hoos
@ 2003-01-13 18:52 ` Per Sandbergs
  2003-01-13 19:01 ` Jeffrey Carter
  2 siblings, 0 replies; 8+ messages in thread
From: Per Sandbergs @ 2003-01-13 18:52 UTC (permalink / raw)


As far as i remember the
 package GNAT.OS_Lib did contain a procedure spawn at that point in time,
but ther was also th emore elaborate
package GNAT.Expect that contains functions for managing subprocesses.

/Per


"JuK" <calvinix@caramail.com> wrote in message
news:baf7991b.0301130858.215e70b7@posting.google.com...
> Hello
> I want to execute a shell command in my ADA application :
>
>  function EXEC_SHELL_COMMAND (COMMAND : in STRING) return EXIT_STATUS ;
>
>
> How Can I do that  ?
>
> ( SOLARIS with GNAT 3.14 )
>
> Thank you





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

* Re: Exec shell command with GNAT
  2003-01-13 16:58 Exec shell command with GNAT JuK
  2003-01-13 17:59 ` David C. Hoos
  2003-01-13 18:52 ` Per Sandbergs
@ 2003-01-13 19:01 ` Jeffrey Carter
  2 siblings, 0 replies; 8+ messages in thread
From: Jeffrey Carter @ 2003-01-13 19:01 UTC (permalink / raw)


JuK wrote:
> Hello 
> I want to execute a shell command in my ADA application :
> 
>  function EXEC_SHELL_COMMAND (COMMAND : in STRING) return EXIT_STATUS ;
> 
> 
> How Can I do that  ?
> 
> ( SOLARIS with GNAT 3.14 )

If a GNAT-dependent solution is acceptable, look at GNAT.OS_Lib. You can 
use Spawn to run a shell. On UNIX and Windows systems you can import the 
C function "system" to do this.

-- 
Jeff Carter
"This school was here before you came,
and it'll be here before you go."
Horse Feathers




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

* Re: Exec shell command with GNAT
  2003-01-13 17:59 ` David C. Hoos
@ 2003-01-14  8:58   ` Thomas Wolf
  2003-01-14 12:15     ` David C. Hoos, Sr.
  2003-01-14 15:41     ` Adrian Knoth
  2003-01-14 10:34   ` Lutz Donnerhacke
  1 sibling, 2 replies; 8+ messages in thread
From: Thomas Wolf @ 2003-01-14  8:58 UTC (permalink / raw)


david.c.hoos.sr@ada95.com wrote:
> with Ada.Characters.Latin_1;
> with System;
> function Execute_Shell_Command
>   (The_Command : String) return Integer is
> 
>    function Execute
>               (The_Command_Address : System.Address) return Integer;
>    pragma Import (C, Execute, "system");
>    The_Nul_Terminated_Command_String : constant String :=
>      The_Command & Ada.Characters.Latin_1.Nul;
> begin
>    return Execute (The_Nul_Terminated_Command_String'Address);
> end Execute_Shell_Command;

Yes, that's it. My own version looks similar:

   with Interfaces.C;

   function Execute (Command : in String) return Integer
   is

     function Run (Command : in Interfaces.C.char_array) 
        return Interfaces.C.int;
     pragma Import (C, Run, "system");

   begin --  Execute
      return Integer (Run (Interfaces.C.To_C (Item => Command)));
   end Execute;

No fiddling around with addresses needed, and the declaration of "Run"
doesn't depend on Interfaces.C.int and Integer being the same. Note
that the Ada standard guarantees that an in-parameter of an array of
some element_type is passed in a way compatible with an "element_type *"
in C. (I.e., the address of the first element is passed.)

-- 
-----------------------------------------------------------------
Thomas Wolf                          e-mail: t_wolf@angelfire.com




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

* Re: Exec shell command with GNAT
  2003-01-13 17:59 ` David C. Hoos
  2003-01-14  8:58   ` Thomas Wolf
@ 2003-01-14 10:34   ` Lutz Donnerhacke
  1 sibling, 0 replies; 8+ messages in thread
From: Lutz Donnerhacke @ 2003-01-14 10:34 UTC (permalink / raw)


* David C. Hoos wrote:
> Here's such a function I've been using for some years:
> It should be portable across compilers and OSs, as long
> as the C-runtime library has the "system" function.

Don't do that. It's not theadsafe. Use GNAT.OS_Lib.Spawn or something similar.



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

* Re: Exec shell command with GNAT
  2003-01-14  8:58   ` Thomas Wolf
@ 2003-01-14 12:15     ` David C. Hoos, Sr.
  2003-01-14 15:41     ` Adrian Knoth
  1 sibling, 0 replies; 8+ messages in thread
From: David C. Hoos, Sr. @ 2003-01-14 12:15 UTC (permalink / raw)



----- Original Message ----- 
From: "Thomas Wolf" <t_wolf@angelfire.com>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: January 14, 2003 2:58 AM
Subject: Re: Exec shell command with GNAT


> david.c.hoos.sr@ada95.com wrote:
> > with Ada.Characters.Latin_1;
> > with System;
> > function Execute_Shell_Command
> >   (The_Command : String) return Integer is
> > 
> >    function Execute
> >               (The_Command_Address : System.Address) return Integer;
> >    pragma Import (C, Execute, "system");
> >    The_Nul_Terminated_Command_String : constant String :=
> >      The_Command & Ada.Characters.Latin_1.Nul;
> > begin
> >    return Execute (The_Nul_Terminated_Command_String'Address);
> > end Execute_Shell_Command;
> 
> Yes, that's it. My own version looks similar:
> 
>    with Interfaces.C;
> 
>    function Execute (Command : in String) return Integer
>    is
> 
>      function Run (Command : in Interfaces.C.char_array) 
>         return Interfaces.C.int;
>      pragma Import (C, Run, "system");
> 
>    begin --  Execute
>       return Integer (Run (Interfaces.C.To_C (Item => Command)));
>    end Execute;
> 
> No fiddling around with addresses needed, and the declaration of "Run"
> doesn't depend on Interfaces.C.int and Integer being the same. Note
> that the Ada standard guarantees that an in-parameter of an array of
> some element_type is passed in a way compatible with an "element_type *"
> in C. (I.e., the address of the first element is passed.)

I agree.  I failed to mention that my function was originally written
before Ada95 existed.  It's a case of "if it's not broken, don't 'fix'
it."

> 
> -- 
> -----------------------------------------------------------------
> Thomas Wolf                          e-mail: t_wolf@angelfire.com
> 
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
> 
>




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

* Re: Exec shell command with GNAT
  2003-01-14  8:58   ` Thomas Wolf
  2003-01-14 12:15     ` David C. Hoos, Sr.
@ 2003-01-14 15:41     ` Adrian Knoth
  1 sibling, 0 replies; 8+ messages in thread
From: Adrian Knoth @ 2003-01-14 15:41 UTC (permalink / raw)


Thomas Wolf <t_wolf@angelfire.com> wrote:

> Yes, that's it. My own version looks similar:
>    function Execute (Command : in String) return Integer
>    is
>      function Run (Command : in Interfaces.C.char_array) 
>         return Interfaces.C.int;
>      pragma Import (C, Run, "system");
>    begin --  Execute
>       return Integer (Run (Interfaces.C.To_C (Item => Command)));
>    end Execute;
 
> No fiddling around with addresses needed, 

There is even less fiddling:

   procedure my_C_system (command : in String) is

      function Internal (command : in String) return Integer;
      pragma Import (C, Internal, "system");

      result : Integer;
   begin
      result := Internal (command & ASCII.Nul);
   end my_C_system;


-- 
mail: adi@thur.de  	http://adi.thur.de	PGP: v2-key via keyserver

Windows 98? Warum? Ich hab' das alte noch nicht zu Ende gespielt.



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

end of thread, other threads:[~2003-01-14 15:41 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-01-13 16:58 Exec shell command with GNAT JuK
2003-01-13 17:59 ` David C. Hoos
2003-01-14  8:58   ` Thomas Wolf
2003-01-14 12:15     ` David C. Hoos, Sr.
2003-01-14 15:41     ` Adrian Knoth
2003-01-14 10:34   ` Lutz Donnerhacke
2003-01-13 18:52 ` Per Sandbergs
2003-01-13 19:01 ` Jeffrey Carter

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