* pass a argument to spawn
@ 2016-01-12 1:17 comicfanzine
2016-01-12 7:05 ` Anh Vo
` (4 more replies)
0 siblings, 5 replies; 8+ messages in thread
From: comicfanzine @ 2016-01-12 1:17 UTC (permalink / raw)
Hi , i'm trying to do a simple " ls -a " , on Ubuntu .
I tried to associate a string value , because :
subtype Argument_List is String_List;
But it failed .
Anyway , i'm lost with all the types and subtypes in GNAT.OS_Lib and System.Strings .
So , i don't know how to proceed in the simplier way .
WITH GNAT.OS_Lib ; USE GNAT.OS_Lib ;
WITH Ada.Text_IO ; USE Ada.Text_IO ;
procedure console_Ada is
state_spawn : boolean ;
arg : Argument_List ( 1..1 ) ;
-- here the value must be "-a" .
begin
new_line ;
put_line ( " Print of your files :") ;
new_line ;
Spawn ( "/bin/ls" , arg , state_spawn );
-- /bin/ls -a
new_line ;
put ( " Execution of the program with Spawn ? : " & Boolean'Image( state_spawn ) ) ;
end console_Ada ;
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: pass a argument to spawn
2016-01-12 1:17 pass a argument to spawn comicfanzine
@ 2016-01-12 7:05 ` Anh Vo
2016-01-12 7:12 ` Per Sandberg
` (3 subsequent siblings)
4 siblings, 0 replies; 8+ messages in thread
From: Anh Vo @ 2016-01-12 7:05 UTC (permalink / raw)
On Monday, January 11, 2016 at 5:17:50 PM UTC-8, comicf...@gmail.com wrote:
> Hi , i'm trying to do a simple " ls -a " , on Ubuntu .
>
> I tried to associate a string value , because :
>
> subtype Argument_List is String_List;
>
> But it failed .
>
> Anyway , i'm lost with all the types and subtypes in GNAT.OS_Lib and System.Strings .
>
> So , i don't know how to proceed in the simplier way .
>
> WITH GNAT.OS_Lib ; USE GNAT.OS_Lib ;
> WITH Ada.Text_IO ; USE Ada.Text_IO ;
>
> procedure console_Ada is
>
> state_spawn : boolean ;
>
> arg : Argument_List ( 1..1 ) ;
> -- here the value must be "-a" .
>
> begin
>
> new_line ;
>
> put_line ( " Print of your files :") ;
>
> new_line ;
>
> Spawn ( "/bin/ls" , arg , state_spawn );
> -- /bin/ls -a
>
> new_line ;
>
> put ( " Execution of the program with Spawn ? : " & Boolean'Image( state_spawn ) ) ;
>
> end console_Ada ;
Why mess with low level while there is a simpler way. Here is a quick binding to the system call as shown below.
with Interfaces.C;
function Invoke_System (Name :string) return Integer is
C_Name : constant Interfaces.C.Char_Array (1 .. Name'Length + 1) :=
Interfaces.C.To_C(Name);
function C_system (Name : System.Address) return Interfaces.C.int;
pragma Import(C, C_system, "system");
begin
return integer (C_system(C_Name(1)'address));
end;
procedure Invoke_System (Name : String) is
Dummy : Integer;
begin
Dummy := Invoke_System(Name);
end Invoke_System;
--...
declare
Unix_Command : constant String := "ls -a";
begin
Invoke_System (Unix_Command);
end;
Anh Vo
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: pass a argument to spawn
2016-01-12 1:17 pass a argument to spawn comicfanzine
2016-01-12 7:05 ` Anh Vo
@ 2016-01-12 7:12 ` Per Sandberg
2016-01-12 13:48 ` comicfanzine
` (2 subsequent siblings)
4 siblings, 0 replies; 8+ messages in thread
From: Per Sandberg @ 2016-01-12 7:12 UTC (permalink / raw)
Well
An argument_list is an array of String_Access and needs to be initialize
as such.
-------------------------------------------------------------------------
with GNAT.OS_Lib; use GNAT.OS_Lib;
with Ada.Text_IO; use Ada.Text_IO;
procedure Console_Ada is
State_Spawn : Boolean;
Arg : constant Argument_List :=
(1 => new String'("-a"));
-- Named association is needed since there is only one argument.
-- This construct also leaks memory but that does not matter here
-- since the program terminate immediate.
begin
Put_Line ("Print of your files :");
Spawn ("/bin/ls", Arg, State_Spawn);
Put (" Execution of the program with Spawn ? : " & State_Spawn'Img);
end Console_Ada;
-------------------------------------------------------------------------
/May i also suggest that you look into the GNAT.Expect package.
/Per
Den 2016-01-12 kl. 02:17, skrev comicfanzine@gmail.com:
> Hi , i'm trying to do a simple " ls -a " , on Ubuntu .
>
> I tried to associate a string value , because :
>
> subtype Argument_List is String_List;
>
> But it failed .
>
> Anyway , i'm lost with all the types and subtypes in GNAT.OS_Lib and System.Strings .
>
> So , i don't know how to proceed in the simplier way .
>
> WITH GNAT.OS_Lib ; USE GNAT.OS_Lib ;
> WITH Ada.Text_IO ; USE Ada.Text_IO ;
>
> procedure console_Ada is
>
> state_spawn : boolean ;
>
> arg : Argument_List ( 1..1 ) ;
> -- here the value must be "-a" .
>
> begin
>
> new_line ;
>
> put_line ( " Print of your files :") ;
>
> new_line ;
>
> Spawn ( "/bin/ls" , arg , state_spawn );
> -- /bin/ls -a
>
> new_line ;
>
> put ( " Execution of the program with Spawn ? : " & Boolean'Image( state_spawn ) ) ;
>
> end console_Ada ;
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: pass a argument to spawn
2016-01-12 1:17 pass a argument to spawn comicfanzine
2016-01-12 7:05 ` Anh Vo
2016-01-12 7:12 ` Per Sandberg
@ 2016-01-12 13:48 ` comicfanzine
2016-01-12 16:10 ` Björn Lundin
2016-01-12 19:36 ` comicfanzine
2016-01-17 17:28 ` comicfanzine
4 siblings, 1 reply; 8+ messages in thread
From: comicfanzine @ 2016-01-12 13:48 UTC (permalink / raw)
If i understand :
subtype Argument_List is String_List;
which is simply :
type String_List is array (Positive range <>) of String_Access;
which is a access to the pointer :
type String_Access is access all String;
Now , in the syntax :
Arg : constant Argument_List := (1 => new String'("-a"));
If this is a call to the pointer value , why use : "new String'" ?
Normally when i use a String , i never use it like this .
----------------
Anh Vo = In your exemple you create your own function with Interfaces.C , that can be a better way to use a external program then Spawn , i keep it for later :)
----------------
Per Sandberg = The GNAT.Expect package is not helping .
It's pretty much , more complicated :/
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: pass a argument to spawn
2016-01-12 13:48 ` comicfanzine
@ 2016-01-12 16:10 ` Björn Lundin
0 siblings, 0 replies; 8+ messages in thread
From: Björn Lundin @ 2016-01-12 16:10 UTC (permalink / raw)
On 2016-01-12 14:48, comicfanzine@gmail.com wrote:
>
> Now , in the syntax :
> Arg : constant Argument_List := (1 => new String'("-a"));
>
>
> If this is a call to the pointer value , why use : "new String'" ?
new String'("-a");
allocates memory for a string, assigns it the value -a
and returns a pointer/access to it.
It is NOT a string, but access/pointer to it
new Integer'(5)
allocates memory for an integer value, assigns it the value 5
and returns a pointer/access to it.
> Normally when i use a String , i never use it like this .
it is not a String.
--
--
Björn
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: pass a argument to spawn
2016-01-12 1:17 pass a argument to spawn comicfanzine
` (2 preceding siblings ...)
2016-01-12 13:48 ` comicfanzine
@ 2016-01-12 19:36 ` comicfanzine
2016-01-12 20:58 ` Per Sandberg
2016-01-17 17:28 ` comicfanzine
4 siblings, 1 reply; 8+ messages in thread
From: comicfanzine @ 2016-01-12 19:36 UTC (permalink / raw)
Björn , i understand much better .
-----------------
I know how to use this procedure :
procedure Spawn
(Program_Name : String;
Args : Argument_List;
Output_File : String;
Success : out Boolean;
Return_Code : out Integer;
Err_To_Out : Boolean := True);
His body in the package body is not clear , by the way .
Anyway , i would like to use an array for storage , instead of : " Output_File : String; " .
So , maybe it would be simplier to create a spawn procedure with the desired content .
But , i don't know how to do that .
Informations about spawning in Ada are needed .
At this mmoment , that's the problem .
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: pass a argument to spawn
2016-01-12 19:36 ` comicfanzine
@ 2016-01-12 20:58 ` Per Sandberg
0 siblings, 0 replies; 8+ messages in thread
From: Per Sandberg @ 2016-01-12 20:58 UTC (permalink / raw)
Well
From your questions i expect that you want to achieve something like:
----------------------------------------------------------------
with GNAT.Expect;
with GNAT.String_Split;
with GNAT.OS_Lib;
with Ada.Text_IO; use Ada.Text_IO;
procedure Console_Ada is
Arg : constant GNAT.OS_Lib.Argument_List :=
(1 => new String'("-a"));
Status : aliased Integer := 0;
S : GNAT.String_Split.Slice_Set;
begin
Put_Line ("Print of your files :");
GNAT.String_Split.Create
(S => S,
From => GNAT.Expect.Get_Command_Output
(Command => "/bin/ls",
Arguments => Arg,
Input => "",
Status => Status'Access,
Err_To_Out => True),
Separators => ASCII.LF & ASCII.CR);
for i in 1 .. GNAT.String_Split.Slice_Count (S) loop
Put_Line (">> " & GNAT.String_Split.Slice (S, i));
end loop;
Put (" Execution of the program with Spawn ? : " & Status'Img);
end Console_Ada;
----------------------------------------------------------------
Den 2016-01-12 kl. 20:36, skrev comicfanzine@gmail.com:
> Björn , i understand much better .
> -----------------
>
> I know how to use this procedure :
>
> procedure Spawn
> (Program_Name : String;
> Args : Argument_List;
> Output_File : String;
> Success : out Boolean;
> Return_Code : out Integer;
> Err_To_Out : Boolean := True);
>
> His body in the package body is not clear , by the way .
>
> Anyway , i would like to use an array for storage , instead of : " Output_File : String; " .
>
> So , maybe it would be simplier to create a spawn procedure with the desired content .
>
> But , i don't know how to do that .
> Informations about spawning in Ada are needed .
>
> At this mmoment , that's the problem .
>
>
>
>
>
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: pass a argument to spawn
2016-01-12 1:17 pass a argument to spawn comicfanzine
` (3 preceding siblings ...)
2016-01-12 19:36 ` comicfanzine
@ 2016-01-17 17:28 ` comicfanzine
4 siblings, 0 replies; 8+ messages in thread
From: comicfanzine @ 2016-01-17 17:28 UTC (permalink / raw)
Per Sandberg = Yes that was it .
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2016-01-17 17:28 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-12 1:17 pass a argument to spawn comicfanzine
2016-01-12 7:05 ` Anh Vo
2016-01-12 7:12 ` Per Sandberg
2016-01-12 13:48 ` comicfanzine
2016-01-12 16:10 ` Björn Lundin
2016-01-12 19:36 ` comicfanzine
2016-01-12 20:58 ` Per Sandberg
2016-01-17 17:28 ` comicfanzine
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox