Yet another question on system calls(newbie question) 
Author Message
 Yet another question on system calls(newbie question)

Using the pragma Import method of calling non-Ada entities, am I only
allowed to define functions? How about procedures? What I want to do is
to create a text file, give this text file as a parameter to a C
program, and then wait until the C program finishes. By then it will
have produced another text file that I want to use.
Can I use a dummy result variable like the 'result' in the following
(from another mail):

procedure Command is

   function C_System(value : String) return integer;
   pragma Import(
         C, C_System, "system");

   result : integer;
begin
   result := C_System("pwd ");
   result := C_System(value => "acdsee D:\work\pictures\BP47.jpg ");
end Command;

/Torbj?rn Karfunkel



Mon, 27 Oct 2003 23:44:25 GMT  
 Yet another question on system calls(newbie question)

Quote:

> Using the pragma Import method of calling non-Ada entities, am I only
> allowed to define functions? How about procedures? What I want to do is
> to create a text file, give this text file as a parameter to a C
> program, and then wait until the C program finishes. By then it will
> have produced another text file that I want to use.
> Can I use a dummy result variable like the 'result' in the following
> (from another mail):

> procedure Command is

>    function C_System(value : String) return integer;
>    pragma Import(
>          C, C_System, "system");

>    result : integer;
> begin
>    result := C_System("pwd ");
>    result := C_System(value => "acdsee D:\work\pictures\BP47.jpg ");
> end Command;

Do you want to "use" the file with Ada, or with some other system
command?

Note that the "system" command in Unix can take a string containing
an entire pipeline, as in the following example:

   result := C_System("ps -ef | grep root");

You can also redirect the output of the command as follows:

   result := C_System("pwd > /tmp/here");

If you want to open a Unix pipe to a system call, and read the
result directly, then look at the example I posted on
www.adapower.com for using the pipe command.

Note that your last proposal, to call the system command with the
name of an image file, will not work. The system command only
executes files that have executable permissions set. This is not
normally true for image files.

Jim Rogers
Colorado Springs, Colorado USA



Tue, 28 Oct 2003 00:20:02 GMT  
 Yet another question on system calls(newbie question)

Quote:
>>Do you want to "use" the file with Ada, or with some other systemcommand?

Ada. I want to wait for the C program to produce the file, and then continue knowing that the file is there. I tried using GNAT.OS_Lib, but the C program starts and then nothing happenns.


Tue, 28 Oct 2003 03:40:38 GMT  
 Yet another question on system calls(newbie question)
[...]

Quote:
> Note that your last proposal, to call the system command with the
> name of an image file, will not work. The system command only
> executes files that have executable permissions set. This is not
> normally true for image files.

Take a closer look.  The call was
   result := C_System(value => "acdsee D:\work\pictures\BP47.jpg ");
The command is acdsee, presumably an image viewer.

BTW, why the trailing blank?

--

San Diego Supercomputer Center           <*>  <http://www.sdsc.edu/~kst>
Cxiuj via bazo apartenas ni.



Tue, 28 Oct 2003 05:03:01 GMT  
 Yet another question on system calls(newbie question)

Quote:

> Take a closer look.  The call was
>    result := C_System(value => "acdsee D:\work\pictures\BP47.jpg ");
> The command is acdsee, presumably an image viewer.

You are correct. My eyes completely skipped the blank.

One of the issues you may encounter when using the "system" command
is the PATH variable. Using a command such as "acdsee" may work
well on some systems, and not on others becuase of differences in
the way "system" evaluates your environment variables.

The safest solution, although non-portable due to use of different
directories, is to call all commands using a fully qualified
directory path.

Jim Rogers
Colorado Springs, Colorado USA



Tue, 28 Oct 2003 05:12:23 GMT  
 Yet another question on system calls(newbie question)

Quote:

> Using the pragma Import method of calling non-Ada entities, am I only
> allowed to define functions? How about procedures? What I want to do is
> to create a text file, give this text file as a parameter to a C
> program, and then wait until the C program finishes. By then it will
> have produced another text file that I want to use.
> Can I use a dummy result variable like the 'result' in the following
> (from another mail):

> procedure Command is

>    function C_System(value : String) return integer;
>    pragma Import(
>          C, C_System, "system");

>    result : integer;
> begin
>    result := C_System("pwd ");
>    result := C_System(value => "acdsee D:\work\pictures\BP47.jpg ");
> end Command;

> /Torbj?rn Karfunkel

It's been a while since I've done any of this stuff.

The C "system" function takes a C string, i.e., a pointer to the first
character of a string terminated with a null ('\0') character.  Is
pragma Import smart enough to do the necessary conversion, or do you
have to use Interfaces.C.Strings?  I would expect that at least you'd
have to append the null character explicitly.

--

San Diego Supercomputer Center           <*>  <http://www.sdsc.edu/~kst>
Cxiuj via bazo apartenas ni.



Tue, 28 Oct 2003 05:12:31 GMT  
 Yet another question on system calls(newbie question)
Thanks everybody for your help. I got it working now.
/Torbj?rn Karfunkel


Tue, 28 Oct 2003 15:18:10 GMT  
 Yet another question on system calls(newbie question)
While I'm not familiar with a C_System function, if is in written in C, you
most likely should be passing a C string:

    function C_System(value : Interfaces.C.Char_Array) return integer;
    pragma Import(
          C, C_System, "system");
    result : integer;
 begin
    result := C_System( Interfaces.C.To_C( "pwd " ) );
    result := C_System( Interfaces.C.To_C( "acdsee
D:\work\pictures\BP47.jpg" ) );
 end Command;

This turns the Ada string, which includes information about the extent of
the string, into a null terminated string.

Other answers to questions:
  You may import functions or procedures.
  Passing file handles from Ada to C may be tricky.  If you use the
File_Type defined in Ada's standard libraries, the definition is
implementation dependent.  You can certainly interface to C functions for
doing all I/O in Ada, but I don't know if you can mix Ada and C's native
file handling.

I hope this helps,

SteveD


Quote:
> Using the pragma Import method of calling non-Ada entities, am I only
> allowed to define functions? How about procedures? What I want to do is
> to create a text file, give this text file as a parameter to a C
> program, and then wait until the C program finishes. By then it will
> have produced another text file that I want to use.
> Can I use a dummy result variable like the 'result' in the following
> (from another mail):

> procedure Command is

>    function C_System(value : String) return integer;
>    pragma Import(
>          C, C_System, "system");

>    result : integer;
> begin
>    result := C_System("pwd ");
>    result := C_System(value => "acdsee D:\work\pictures\BP47.jpg ");
> end Command;

> /Torbj?rn Karfunkel



Wed, 29 Oct 2003 09:30:54 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. howto make system calls (newbie question)

2. Return value of system call (newbie question)

3. newbie question about pipes and system calls

4. (Yet another) newbie question

5. Yet another newbie question

6. Yet Another Newbie Haskell IO question

7. Yet Another Newbie Haskell IO question

8. Yet another newbie question...

9. -yet another (ruby) newbie question -string#concat

10. yet another newbie question!

11. Yet another Java/VRML newbie question

12. Yet Another Newbie Question: Stupid Clock tricks, Part II.

 

 
Powered by phpBB® Forum Software