comp.lang.ada
 help / color / mirror / Atom feed
* Ada Equivalent of "system()" in C?
@ 1996-04-15  0:00 Dave Sparks
  1996-04-16  0:00 ` Samuel Tardieu
  1996-04-16  0:00 ` Robert Dewar
  0 siblings, 2 replies; 10+ messages in thread
From: Dave Sparks @ 1996-04-15  0:00 UTC (permalink / raw)


I'm new to Ada programming, having come from a C background.  Is
there an equivalent in Ada of the "system()" function in C that
allows you execute an external program?

For information, I'm using Gnat 3.01 for OS/2.

Thank you very much.

---
Dave Sparks -- dsparks@pobox.com




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

* Re: Ada Equivalent of "system()" in C?
  1996-04-15  0:00 Ada Equivalent of "system()" in C? Dave Sparks
  1996-04-16  0:00 ` Samuel Tardieu
@ 1996-04-16  0:00 ` Robert Dewar
  1996-04-17  0:00   ` Samuel Tardieu
  1996-04-17  0:00   ` Bob Kitzberger
  1 sibling, 2 replies; 10+ messages in thread
From: Robert Dewar @ 1996-04-16  0:00 UTC (permalink / raw)


Dave asks

"I'm new to Ada programming, having come from a C background.  Is
there an equivalent in Ada of the "system()" function in C that
allows you execute an external program?"

I don't have my C standard handy, but I am a little surprised if system()
is part of the C language, but either way, the answer is the same!

There is no function in Ada for this purpose, but you can call the
system function from Ada using pragma Interface with no problem.





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

* Re: Ada Equivalent of "system()" in C?
  1996-04-17  0:00   ` Bob Kitzberger
@ 1996-04-16  0:00     ` David Emery
  1996-04-17  0:00       ` Robert Dewar
                         ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: David Emery @ 1996-04-16  0:00 UTC (permalink / raw)


In article <4l1dbi$pho@rational.rational.com>, rlk@rational.com (Bob
Kitzberger) wrote:

> : I'm new to Ada programming, having come from a C background.  Is
> : there an equivalent in Ada of the "system()" function in C that
> : allows you execute an external program?
> 
> Take a look at the Posix Ada bindings -- likely provided by
> your compiler vendor.  The Posix Ada bindings are a standardized
> binding definition to Posix/Unix services.  Personally, I find
> the Posix bindings much nicer to use than using pragma Interface
> to the C library routines themselves.
> 
> --
> Bob Kitzberger        Rational Software Corporation       rlk@rational.com

The Unix function system() is not included in POSIX.1 or POSIX.5.  I 
believe that it is included in the POSIX.1a revision that is still in
development.

That being said, it's trivial to implement this via pragma interface.
The signature is, if I remember correctly (I'm at home and can't check
the manpage):
   int   system (const char * cmd);
In other words, this takes a single string parameter, the command to
execute, and returns an int value if the command was succesfully started.
Note that the return value is NOT the return value of the command, but
rather of the fork() that is used to set up execution of the command.

            dave




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

* Re: Ada Equivalent of "system()" in C?
  1996-04-15  0:00 Ada Equivalent of "system()" in C? Dave Sparks
@ 1996-04-16  0:00 ` Samuel Tardieu
  1996-04-16  0:00 ` Robert Dewar
  1 sibling, 0 replies; 10+ messages in thread
From: Samuel Tardieu @ 1996-04-16  0:00 UTC (permalink / raw)
  To: Dave Sparks

>>>>> "Dave" == Dave Sparks <dsparks@pobox.com> writes:

Dave> I'm new to Ada programming, having come from a C background.  Is
Dave> there an equivalent in Ada of the "system()" function in C that
Dave> allows you execute an external program?

Every C library functions may be accessed from an Ada program. All you
have to do is put a wrapper on it. Try something like that (untested):

+++ GNATCHOP FROM HERE +++
package System_Interface is
   function System (Command : String) return Integer;
end System_Interface;

with Interfaces.C.Strings; use Interfaces.C; use Interfaces.C.Strings;
package body System_Interface is
   
   function System (Command : String) return Integer is
      function C_System (Command : chars_ptr) return int;
      pragma import (C, C_System, "system");
      C_Command   : chars_ptr := New_String (Command);
      Return_Code : constant int := C_System (C_Command);
   begin
      Free (C_Command);
      return Integer (Return_Code);
   end System;

end System_Interface;
+++ GNATCHOP TO HERE +++

Then you may call:

   R_Code := System_Interface.System (My_Command);

Hope this helps.

 Sam
-- 
"La cervelle des petits enfants, ca doit avoir comme un petit gout de noisette"
                                                       Charles Baudelaire




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

* Re: Ada Equivalent of "system()" in C?
  1996-04-16  0:00 ` Robert Dewar
@ 1996-04-17  0:00   ` Samuel Tardieu
  1996-04-17  0:00   ` Bob Kitzberger
  1 sibling, 0 replies; 10+ messages in thread
From: Samuel Tardieu @ 1996-04-17  0:00 UTC (permalink / raw)
  To: David Emery

>>>>> "David" == David Emery <emery@grebyn.com> writes:

David> In other words, this takes a single string parameter, the
David> command to execute, and returns an int value if the command was
David> succesfully started. Note that the return value is NOT the
David> return value of the command, but rather of the fork() that is
David> used to set up execution of the command.

I think you're wrong; if I remember well, the return value of system()
is a value composed of the exit status of the shell command and the
result of wait() (which is used to wait for child termination), whith
a special case of -127 when the command couldn't be launched due to
the lack of /bin/sh.

  Sam
-- 
"La cervelle des petits enfants, ca doit avoir comme un petit gout de noisette"
                                                       Charles Baudelaire




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

* Re: Ada Equivalent of "system()" in C?
  1996-04-16  0:00     ` David Emery
@ 1996-04-17  0:00       ` Robert Dewar
  1996-04-22  0:00         ` Sandy Wise
  1996-04-17  0:00       ` Keith Thompson
  1996-04-19  0:00       ` Ron J Theriault
  2 siblings, 1 reply; 10+ messages in thread
From: Robert Dewar @ 1996-04-17  0:00 UTC (permalink / raw)


Dave Emery said

"The Unix function system() is not included in POSIX.1 or POSIX.5.  I
believe that it is included in the POSIX.1a revision that is still in
development."

However, it *is* in fact in the ANSI C standard, although the semantics
are pretty much entirely implementation defined.

In general the Ada standard library avoids functions that cannot be
well defined in a portable manner, although, to be honest, the command
line interface is pretty much in this category already.

Anyway, Peter's recipe for interfacing to C is certainly one approach.

If you are using GNAT, you could also use GNAT.OS_Lib.Spawn. Spawn is
a routine in the GNAT supplied predefined package GNAT.OS_Lib which has
a handful of useful operating systems interfaces (it is the interface
used by the GNAT compiler itself). Here is the documentatoin from the
spec:

   procedure Spawn
     (Program_Name : String;
      Args         : Argument_List;
      Success      : out Boolean);
   --  The first parameter of function Spawn is the full path name of the
   --  executable. The second parameter contains the arguments to be passed
   --  to the program. Success is false if the program could not be spawned
   --  or its execution completed unsuccessfully. Note that the caller will
   --  be blocked until the execution of the spawned program is complete.






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

* Re: Ada Equivalent of "system()" in C?
  1996-04-16  0:00 ` Robert Dewar
  1996-04-17  0:00   ` Samuel Tardieu
@ 1996-04-17  0:00   ` Bob Kitzberger
  1996-04-16  0:00     ` David Emery
  1 sibling, 1 reply; 10+ messages in thread
From: Bob Kitzberger @ 1996-04-17  0:00 UTC (permalink / raw)



: I'm new to Ada programming, having come from a C background.  Is
: there an equivalent in Ada of the "system()" function in C that
: allows you execute an external program?

Take a look at the Posix Ada bindings -- likely provided by
your compiler vendor.  The Posix Ada bindings are a standardized
binding definition to Posix/Unix services.  Personally, I find
the Posix bindings much nicer to use than using pragma Interface
to the C library routines themselves.

--
Bob Kitzberger	      Rational Software Corporation       rlk@rational.com




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

* Re: Ada Equivalent of "system()" in C?
  1996-04-16  0:00     ` David Emery
  1996-04-17  0:00       ` Robert Dewar
@ 1996-04-17  0:00       ` Keith Thompson
  1996-04-19  0:00       ` Ron J Theriault
  2 siblings, 0 replies; 10+ messages in thread
From: Keith Thompson @ 1996-04-17  0:00 UTC (permalink / raw)


In <emery-1604961854590001@line029.nwm.mindlink.net> emery@grebyn.com (David Emery) writes:
[...]
> Note that the return value is NOT the return value of the command, but
> rather of the fork() that is used to set up execution of the command.

That's not quite correct.  The system function returns -1 (and sets
errno) if the fork or exec fails.  Otherwise it returns the exit status
of the shell (sh) that it invokes to execute the command; the format of
this exit status is a bit tricky.  This information is from a SunOS 5.4
(Solaris 2.4) man page; see your local man page for details.

The above is Unix-specific.  The ISO C standard has very little to say
about the return value of system, or even its semantics.

-- 
Keith Thompson (The_Other_Keith) kst@thomsoft.com <*>
TeleSoft^H^H^H^H^H^H^H^H Alsys^H^H^H^H^H Thomson Software Products
10251 Vista Sorrento Parkway, Suite 300, San Diego, CA, USA, 92121-2718
This sig uses the word "Exon" in violation of the Communications Decency Act.




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

* Re: Ada Equivalent of "system()" in C?
  1996-04-16  0:00     ` David Emery
  1996-04-17  0:00       ` Robert Dewar
  1996-04-17  0:00       ` Keith Thompson
@ 1996-04-19  0:00       ` Ron J Theriault
  2 siblings, 0 replies; 10+ messages in thread
From: Ron J Theriault @ 1996-04-19  0:00 UTC (permalink / raw)


In article <emery-1604961854590001@line029.nwm.mindlink.net>, emery@grebyn.com (David Emery) writes:
|> In article <4l1dbi$pho@rational.rational.com>, rlk@rational.com (Bob
|> Kitzberger) wrote:
|> 
|> > : I'm new to Ada programming, having come from a C background.  Is
|> > : there an equivalent in Ada of the "system()" function in C that
|> > : allows you execute an external program?
|> > 
|> > Take a look at the Posix Ada bindings -- likely provided by
|> > your compiler vendor.  ...
|> > --
|> > Bob Kitzberger        Rational Software Corporation       rlk@rational.com
|> 
|> The Unix function system() is not included in POSIX.1 or POSIX.5.  I 
|> believe that it is included in the POSIX.1a revision that is still in
|> development....
|>             dave

Posix 1003.5 defines the Ada 83 to POSIX API interface.
One would use: Posix_Process_Primitives.Start_Process, and
then wait for it to finish.  The executable to start, would be 
a shell of ones choosing, with the command string as an argument.

-- 
Ron Theriault              |   
CS Department              |   In a democracy, you only have to fool
Texas A&M Univ.            |   most of the people, most of the time.
ron@cs.tamu.edu            |   




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

* Re: Ada Equivalent of "system()" in C?
  1996-04-17  0:00       ` Robert Dewar
@ 1996-04-22  0:00         ` Sandy Wise
  0 siblings, 0 replies; 10+ messages in thread
From: Sandy Wise @ 1996-04-22  0:00 UTC (permalink / raw)


Here is a bit of POSIX.5 code that I use to model the C system() function.
I have used it successfully under VADS 6.2.3 on the DEC Alpha, and IBM
RS/6000...  Its semantics are close to those documented for the Digital
UNIX version of system().

  Posix_Process_Primitives.Open_Template ( ATemplate );
  Posix.Append ( Args, Posix.To_POSIX_String("-c") );
  Posix.Append ( Args, 
                 Posix.To_POSIX_String( Command ) );
  Posix_Process_Primitives.Start_Process_Search ( 
        PID, Posix.To_POSIX_String("/bin/sh"), ATemplate, Args ); 
  Posix_Process_Primitives.Wait_For_Child_Process ( AStatus, PID );
  Posix_Process_Primitives.Close_Template ( ATemplate );

-- 
Alexander Erskine Wise/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
/\/\/\/\/\/\/\/\/\/\/\/\/\/\ WISE@CS.UMASS.EDU /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
/\/\/\/\/\/\/\/\/\/\/\/\/\Laboratory for Advanced Software Engineering Research




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

end of thread, other threads:[~1996-04-22  0:00 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-04-15  0:00 Ada Equivalent of "system()" in C? Dave Sparks
1996-04-16  0:00 ` Samuel Tardieu
1996-04-16  0:00 ` Robert Dewar
1996-04-17  0:00   ` Samuel Tardieu
1996-04-17  0:00   ` Bob Kitzberger
1996-04-16  0:00     ` David Emery
1996-04-17  0:00       ` Robert Dewar
1996-04-22  0:00         ` Sandy Wise
1996-04-17  0:00       ` Keith Thompson
1996-04-19  0:00       ` Ron J Theriault

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