OSDN Git Service

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