OSDN Git Service

2009-11-30 Sergey Rybin <rybin@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / g-enblsp-vms-alpha.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER 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) 2005-2009, AdaCore                      --
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,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, 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 was originally developed  by the GNAT team at  New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 --  This package provides a target dependent non-blocking spawn function
35 --  for use by the VMS GNAT.Expect package (g-expect-vms.adb). This package
36 --  should not be directly with'ed by an application program.
37
38 --  This version is for Alpha/VMS
39
40 separate (GNAT.Expect)
41 procedure Non_Blocking_Spawn
42   (Descriptor  : out Process_Descriptor'Class;
43    Command     : String;
44    Args        : GNAT.OS_Lib.Argument_List;
45    Buffer_Size : Natural := 4096;
46    Err_To_Out  : Boolean := False)
47 is
48    function Alloc_Vfork_Blocks return Integer;
49    pragma Import (C, Alloc_Vfork_Blocks, "decc$$alloc_vfork_blocks");
50
51    function Get_Vfork_Jmpbuf return System.Address;
52    pragma Import (C, Get_Vfork_Jmpbuf, "decc$$get_vfork_jmpbuf");
53
54    function Get_Current_Invo_Context
55      (Addr : System.Address) return Process_Id;
56    pragma Import (C, Get_Current_Invo_Context,
57      "LIB$GET_CURRENT_INVO_CONTEXT");
58
59    Pipe1, Pipe2, Pipe3 : aliased Pipe_Type;
60
61    Arg      : String_Access;
62    Arg_List : aliased array (1 .. Args'Length + 2) of System.Address;
63
64    Command_With_Path : String_Access;
65
66 begin
67    --  Create the rest of the pipes
68
69    Set_Up_Communications
70      (Descriptor, Err_To_Out, Pipe1'Access, Pipe2'Access, Pipe3'Access);
71
72    Command_With_Path := Locate_Exec_On_Path (Command);
73
74    if Command_With_Path = null then
75       raise Invalid_Process;
76    end if;
77
78    --  Fork a new process (it is not possible to do this in a subprogram)
79
80    Descriptor.Pid :=
81      (if Alloc_Vfork_Blocks >= 0
82       then Get_Current_Invo_Context (Get_Vfork_Jmpbuf) else -1);
83
84    --  Are we now in the child
85
86    if Descriptor.Pid = Null_Pid then
87
88       --  Prepare an array of arguments to pass to C
89
90       Arg   := new String (1 .. Command_With_Path'Length + 1);
91       Arg (1 .. Command_With_Path'Length) := Command_With_Path.all;
92       Arg (Arg'Last)        := ASCII.NUL;
93       Arg_List (1)          := Arg.all'Address;
94
95       for J in Args'Range loop
96          Arg                     := new String (1 .. Args (J)'Length + 1);
97          Arg (1 .. Args (J)'Length)  := Args (J).all;
98          Arg (Arg'Last)              := ASCII.NUL;
99          Arg_List (J + 2 - Args'First) := Arg.all'Address;
100       end loop;
101
102       Arg_List (Arg_List'Last) := System.Null_Address;
103
104       --  This does not return on Unix systems
105
106       Set_Up_Child_Communications
107         (Descriptor, Pipe1, Pipe2, Pipe3, Command_With_Path.all,
108          Arg_List'Address);
109    end if;
110
111    Free (Command_With_Path);
112
113    --  Did we have an error when spawning the child ?
114
115    if Descriptor.Pid < Null_Pid then
116       raise Invalid_Process;
117    else
118       --  We are now in the parent process
119
120       Set_Up_Parent_Communications (Descriptor, Pipe1, Pipe2, Pipe3);
121    end if;
122
123    --  Create the buffer
124
125    Descriptor.Buffer_Size := Buffer_Size;
126
127    if Buffer_Size /= 0 then
128       Descriptor.Buffer := new String (1 .. Positive (Buffer_Size));
129    end if;
130 end Non_Blocking_Spawn;