OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / gcc / ada / g-enblsp.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT LIBRARY COMPONENTS                          --
4 --                                                                          --
5 --         G N A T . E X P E C T . N O N _ B L O C K I N G _ S P A W N      --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --                                                                          --
10 --              Copyright (C) 2002 Ada Core Technologies, Inc.              --
11 --                                                                          --
12 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
13 -- terms of the  GNU General Public License as published  by the Free Soft- --
14 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
15 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
18 -- for  more details.  You should have  received  a copy of the GNU General --
19 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
20 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
21 -- MA 02111-1307, USA.                                                      --
22 --                                                                          --
23 -- As a special exception,  if other files  instantiate  generics from this --
24 -- unit, or you link  this unit with other files  to produce an executable, --
25 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
26 -- covered  by the  GNU  General  Public  License.  This exception does not --
27 -- however invalidate  any other reasons why  the executable file  might be --
28 -- covered by the  GNU Public License.                                      --
29 --                                                                          --
30 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com).   --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 --  This is the default version. Used everywhere except VMS.
35
36 separate (GNAT.Expect)
37 procedure Non_Blocking_Spawn
38   (Descriptor  : out Process_Descriptor'Class;
39    Command     : String;
40    Args        : GNAT.OS_Lib.Argument_List;
41    Buffer_Size : Natural := 4096;
42    Err_To_Out  : Boolean := False)
43 is
44    function Fork return Process_Id;
45    pragma Import (C, Fork, "__gnat_expect_fork");
46    --  Starts a new process if possible.
47    --  See the Unix command fork for more information. On systems that
48    --  don't support this capability (Windows...), this command does
49    --  nothing, and Fork will return Null_Pid.
50
51    Pipe1, Pipe2, Pipe3 : aliased Pipe_Type;
52
53    Arg      : String_Access;
54    Arg_List : aliased array (1 .. Args'Length + 2) of System.Address;
55
56    Command_With_Path : String_Access;
57
58 begin
59    --  Create the rest of the pipes
60
61    Set_Up_Communications
62      (Descriptor, Err_To_Out, Pipe1'Access, Pipe2'Access, Pipe3'Access);
63
64    --  Fork a new process
65
66    Descriptor.Pid := Fork;
67
68    --  Are we now in the child (or, for Windows, still in the common
69    --  process).
70
71    if Descriptor.Pid = Null_Pid then
72
73       Command_With_Path := Locate_Exec_On_Path (Command);
74
75       --  Prepare an array of arguments to pass to C
76       Arg   := new String (1 .. Command_With_Path'Length + 1);
77       Arg (1 .. Command_With_Path'Length) := Command_With_Path.all;
78       Arg (Arg'Last)        := ASCII.Nul;
79       Arg_List (1)          := Arg.all'Address;
80
81       for J in Args'Range loop
82          Arg                     := new String (1 .. Args (J)'Length + 1);
83          Arg (1 .. Args (J)'Length)  := Args (J).all;
84          Arg (Arg'Last)              := ASCII.Nul;
85          Arg_List (J + 2 - Args'First) := Arg.all'Address;
86       end loop;
87
88       Arg_List (Arg_List'Last) := System.Null_Address;
89
90       --  This does not return on Unix systems
91
92       Set_Up_Child_Communications
93         (Descriptor, Pipe1, Pipe2, Pipe3, Command_With_Path.all,
94          Arg_List'Address);
95
96       Free (Command_With_Path);
97    end if;
98
99    --  Did we have an error when spawning the child ?
100
101    if Descriptor.Pid < Null_Pid then
102       null;
103    else
104       --  We are now in the parent process
105
106       Set_Up_Parent_Communications (Descriptor, Pipe1, Pipe2, Pipe3);
107    end if;
108
109    --  Create the buffer
110
111    Descriptor.Buffer_Size := Buffer_Size;
112
113    if Buffer_Size /= 0 then
114       Descriptor.Buffer := new String (1 .. Positive (Buffer_Size));
115    end if;
116 end Non_Blocking_Spawn;