comp.lang.ada
 help / color / mirror / Atom feed
* Re: "system" Call in GNAT?
  1999-04-05  0:00 "system" Call in GNAT? M. Smith
@ 1999-04-05  0:00 ` Jerry van Dijk
  1999-04-07  0:00 ` Robert Dewar
  1 sibling, 0 replies; 3+ messages in thread
From: Jerry van Dijk @ 1999-04-05  0:00 UTC (permalink / raw)


M. Smith (work@alphasoft-inc.com) wrote:

:     I was looking for a call in GNAT that would make invoke a command to the OS
: for me and  I found the function "system" and "execl".

Calling the OS is, of course, OS specific, so not addressed by Ada or an
Ada compiler (although a compiler may provided a vendor specific package
for it).

The system() call is the easiest to use, since you simply give it the command
to execute in a string, and of it goes.

However, the system() call is part of the C runtime library, not of the Win32
API, so you are making things more difficult than they need to be.

Here is example:

--  using the system call on Win32 to list a directory

with System;   --  to use the Address type

procedure List is

   --------------------------------------------------------------------
   procedure System_Call (S : in String) is

      --  The system() call is not part of the Win32 API, but of the C
      --  runtime library, which GNAT links in by default.

      --  Note that the system() call will go away and not return until
      --  its job is done.

      --  The return value of the system() call in the Win32 environment
      --  indicates the result of the system call, not the result of the
      --  command it tried to execute. Since system() is unlikely to
      --  fail I discard the return value here.

      procedure System_Call (Command : in System.Address);
      pragma Import (C, System_Call, "system");
      --  The system() call expects a C string pointer, which is the
      --  address of the first character in a character array terminated
      --  by a zero.

      --  Note the distingishing between the Ada name (System_Call) and
      --  the C name (system) which are 'linked' by the import pragma.

      Command : constant String := S & ASCII.NUL;
      --  Add a terminating zero to the Ada string to make it look like
      --  a C string.

   begin
      System_Call (Command'Address);
      --  Command'Address returns the address of the first character
   end System_Call;

-----------------------------------------------------------------------
begin
   System_Call ("dir");
end List;

--
-- Jerry van Dijk | Leiden, Holland
-- Team Ada       | jdijk@acm.org
-- see http://stad.dsl.nl/~jvandyk




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

* "system" Call in GNAT?
@ 1999-04-05  0:00 M. Smith
  1999-04-05  0:00 ` Jerry van Dijk
  1999-04-07  0:00 ` Robert Dewar
  0 siblings, 2 replies; 3+ messages in thread
From: M. Smith @ 1999-04-05  0:00 UTC (permalink / raw)
  To: Roga Danar

Hi All,

    I was looking for a call in GNAT that would make invoke a command to the OS
for me and  I found the function "system" and "execl".

    Unfortunatly, I have not been able to link these either of these calls with
my test driver.

    Am I missing something here?  I have been only working for a short time with
GNAT.

    My code and the error message follow.

-- *****************************************************************

My code is =>

with Win32;
with Win32.WinNT;
with Win32.crt.Process;

with Win32.Shellapi;
with Interfaces.C;
with Interfaces.C.Strings;

with Ada.Text_Io;

procedure Test_Command is

   use Ada;

   package C renames Interfaces.C;
   package C_Str renames C.Strings;

   Command_Str : C_Str.chars_ptr := C_Str.New_String("DIR T* ");
   Arg      : String := "/w";

   type int_ptr is access C.Int;

   args : int_ptr := new C.Int'(1);
   ppt_char : Win32.Shellapi.PPTCHAR := new Win32.PCHAR'(new C.CHAR'(c.nul));

   status : C.Int := 0;

begin

   ppt_char := Win32.Shellapi.CommandLineToArgvW (
               Win32.To_PSTR (Command_Str),
               args);
   text_io.put (C.To_Ada (ppt_char.all.all));

  text_io.put_line (C_Str.Value (Command_Str));

--  status := Win32.crt.Process.system (p1 => Win32.To_PCSTR (Command_Str));
  status := Win32.crt.Process.execl (cmdname => Win32.To_PCSTR (Command_Str));

end Test_Command;

--*******************************************************

The Error message is =>


gnatbind -aO./
-aOC:\usr\lib\gcc-lib\i586-pc-mingw32\2.7.2\adainclude;C:\My_Work;C:\My_Work\Win32Ada\src;C:\My_Work\Jeff\Communication;C:\My_Work\Jeff\Database;C:\My_Work\Jeff\Communication;C:\My_Work\Jeff\Database;C:\My_Work\Jeff\Executive;C:\My_Work\Jeff\Misc;C:\My_Work\database\odbc;C:\My_Work\database\sql;C:\USR\lib\gcc-lib\i586-pc-mingw32\2.7.2\adalib
-I- -x Test_Command.ali
gnatlink -mwindows -lodbc32 Test_Command.ali
./stdarg-inst.o(.text+0xb9):stdarg-inst.ad: undefined reference to `to_long'
./stdarg-inst.o(.text+0x69b):stdarg-inst.ad: undefined reference to `to_long'
./stdarg-inst.o(.text+0xf67):stdarg-inst.ad: undefined reference to `to_long'
./stdarg-inst.o(.text+0x1829):stdarg-inst.ad: undefined reference to `to_double'

./stdarg-inst.o(.text+0x1b15):stdarg-inst.ad: undefined reference to `to_double'

./win32-crt-process.o(.text+0x4dd):win32-crt-process.adb: undefined reference to
`do_varargs'
./win32-crt-process.o(.text+0x718):win32-crt-process.adb: undefined reference to
`do_varargs'
./win32-crt-process.o(.text+0x911):win32-crt-process.adb: undefined reference to
`do_varargs'
./win32-crt-process.o(.text+0xb4c):win32-crt-process.adb: undefined reference to
`do_varargs'
./win32-crt-process.o(.text+0xd66):win32-crt-process.adb: undefined reference to
`do_varargs'
./win32-crt-process.o(.text+0xfc1):win32-crt-process.adb: more undefined
references to `do_varargs' follow
gnatmake: *** link failed.
Process completed with exit code 2






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

* Re: "system" Call in GNAT?
  1999-04-05  0:00 "system" Call in GNAT? M. Smith
  1999-04-05  0:00 ` Jerry van Dijk
@ 1999-04-07  0:00 ` Robert Dewar
  1 sibling, 0 replies; 3+ messages in thread
From: Robert Dewar @ 1999-04-07  0:00 UTC (permalink / raw)


In article <3708E4C0.F13C2023@alphasoft-inc.com>,
  smithm-nospam@stelnj.com wrote:
> Hi All,
>
> I was looking for a call in GNAT that would make
> invoke a command to the OS
> for me and  I found the function "system" and "execl".

When using GNAT, do not forget that a critical part of
the documentation is the specs of the g.xxx units, found
in the g-xxx.ads files in gnatlib. These provide a lot of
useful functionality. In this particular case, look up
the section called Subprocesses in g-os_lib.ads, it may
well do what you want, without having to mess with low
level interface to C functions.

Robert Dewar
Ada Core Technologies

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    




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

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

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-04-05  0:00 "system" Call in GNAT? M. Smith
1999-04-05  0:00 ` Jerry van Dijk
1999-04-07  0:00 ` Robert Dewar

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