1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283 | -- --
-- package GLib.Spawn_Alt Copyright (c) Dmitry A. Kazakov --
-- Interface Luebeck --
-- Spring, 2009 --
-- --
-- Last revision : 21:51 06 Jun 2019 --
-- --
-- This library is free software; you can redistribute it and/or --
-- modify it under the terms of the GNU General Public License as --
-- published by the Free Software Foundation; either version 2 of --
-- the License, or (at your option) any later version. This library --
-- is distributed in the hope that it will be useful, but WITHOUT --
-- ANY WARRANTY; without even the implied warranty of --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU --
-- General Public License for more details. You should have --
-- received a copy of the GNU General Public License along with --
-- this library; if not, write to the Free Software Foundation, --
-- Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. --
-- --
-- As a special exception, if other files instantiate generics from --
-- this unit, or you link this unit with other files to produce an --
-- executable, this unit does not by itself cause the resulting --
-- executable to be covered by the GNU General Public License. This --
-- exception does not however invalidate any other reasons why the --
-- executable file might be covered by the GNU Public License. --
--____________________________________________________________________--
with Interfaces.C; use Interfaces.C;
with Interfaces.C.Strings; use Interfaces.C.Strings;
with Glib.Error; use Glib.Error;
with Gtk.Enums; use Gtk.Enums.String_List;
with System; use System;
with Interfaces.C.Pointers;
package GLib.Spawn_Alt is
type GSpawnFlags is new GUInt;
Spawn_Leave_Descriptors_Open : constant GSpawnFlags := 2**0;
Spawn_Do_Not_Reap_Child : constant GSpawnFlags := 2**1;
Spawn_Search_Path : constant GSpawnFlags := 2**2;
Spawn_Stdout_To_Dev_Null : constant GSpawnFlags := 2**3;
Spawn_Stderr_To_Dev_Null : constant GSpawnFlags := 2**4;
Spawn_Child_Inherits_Stdin : constant GSpawnFlags := 2**5;
Spawn_File_And_ArgV_Zero : constant GSpawnFlags := 2**6;
type GPID is new Interfaces.C.Int;
--
-- Child_Setup -- A procedure used to set up the child
--
type GSpawnChildSetupFunc is access procedure (Data : Address);
pragma Convention (C, GSpawnChildSetupFunc);
--
-- Child_Watch_Func -- A procedure to be called upon child completion
--
type Child_Watch_Func is access procedure
( PID : GPid;
Status : GInt;
Data : System.Address
);
pragma Convention (C, Child_Watch_Func);
type Chars_Ptr_Array is
array (Positive range <>) of aliased Chars_Ptr;
package Chars_Ptr_Lists is
new Interfaces.C.Pointers
( Index => Positive,
Element => Chars_Ptr,
Element_Array => Chars_Ptr_Array,
Default_Terminator => Null_Ptr
);
--
-- Async_Result -- Result of a synchronous spawn
--
-- Running - True if the process has been spawned
-- Pipelined - True if pipes were created
--
type Async_Result
( Running : Boolean := False;
Pipelined : Boolean := False
) is record
case Running is
when True =>
PID : GPID;
case Pipelined is
when True =>
Standard_Input : GInt;
Standard_Output : GInt;
Standard_Error : GInt;
when False =>
null;
end case;
when False =>
Error : GError;
end case;
end record;
--
-- Sync_Result -- Result of a synchronous spawn
--
-- Executed - True if the process was spawned
--
type Sync_Result (Executed : Boolean := False) is record
case Executed is
when True =>
Exit_Status : GInt;
Standard_Output : Chars_Ptr;
Standard_Error : Chars_Ptr;
when False =>
Error : GError;
end case;
end record;
--
-- Async -- Spawns a process asynchronously
--
-- [ Working_Directory ] - The working directory name
-- ArgV - Arguments list (null terminated)
-- EnvP - Environment (null terminated)
-- Flags - Spawn flags
-- Child_Setup - Setup data object
--
-- Upon process invokation Setup procedure is called on Child_Setup
-- object. Child_Setup and user_data are a function and user data. On
-- POSIX platforms, the function is called in the child after GLib has
-- performed all the setup it plans to perform (including creating
-- pipes, closing file descriptors, etc.) but before calling exec().
-- That is, Child_Setup is called just before calling exec() in the
-- child. Obviously actions taken in this function will only affect the
-- child, not the parent. EnvP can be null when current environment has
-- to be used.
--
-- Returns :
--
-- Spawning result
--
function Async
( Working_Directory : UTF8_String;
ArgV : Chars_Ptr_Lists.Pointer;
EnvP : Chars_Ptr_Lists.Pointer := null;
Flags : GSpawnFlags;
Child_Setup : GSpawnChildSetupFunc := null;
Data : Address := Null_Address
) return Async_Result;
function Async
( ArgV : Chars_Ptr_Lists.Pointer;
EnvP : Chars_Ptr_Lists.Pointer := null;
Flags : GSpawnFlags;
Child_Setup : GSpawnChildSetupFunc := null;
Data : Address := Null_Address
) return Async_Result;
--
-- Async_With_Pipes -- Spawns a process asynchronously
--
-- [ Working_Directory ] - The working directory name
-- ArgV - Arguments list (null terminated)
-- [ EnvP ] - Environment (null terminated)
-- Flags - Spawn flags
-- Child_Setup - Setup procedure
-- User_Data - Data for the setup procedure
--
-- Upon process invokation Setup procedure is called on Child_Setup
-- object. Child_Setup and user_data are a function and user data. On
-- POSIX platforms, the function is called in the child after GLib has
-- performed all the setup it plans to perform (including creating
-- pipes, closing file descriptors, etc.) but before calling exec().
-- That is, Child_Setup is called just before calling exec() in the
-- child. Obviously actions taken in this function will only affect the
-- child, not the parent. EnvP can be null when current environment has
-- to be used.
--
-- Returns :
--
-- Spawning result
--
function Async_With_Pipes
( Working_Directory : UTF8_String;
ArgV : Chars_Ptr_Lists.Pointer;
EnvP : Chars_Ptr_Lists.Pointer := null;
Flags : GSpawnFlags;
Child_Setup : GSpawnChildSetupFunc := null;
User_Data : Address := Null_Address
) return Async_Result;
function Async_With_Pipes
( ArgV : Chars_Ptr_Lists.Pointer;
EnvP : Chars_Ptr_Lists.Pointer := null;
Flags : GSpawnFlags;
Child_Setup : GSpawnChildSetupFunc := null;
User_Data : Address := Null_Address
) return Async_Result;
--
-- Child_Watch_Add -- Child process completion callback
--
-- PID - Process ID
-- Func - To call
-- Data - User data to pass
--
procedure Child_Watch_Add
( PID : GPid;
Func : Child_Watch_Func;
Data : Address
);
--
-- Close_PID -- Process identifier
--
-- PID - Process ID
--
-- On some platforms, notably Windows, the GPid type represents a
-- resource which must be closed to prevent resource leaking.
-- g_spawn_close_pid() is provided for this purpose. It should be used
-- on all platforms, even though it doesn't do anything under UNIX.
--
procedure Close_PID (PID : GPID);
--
-- Command_Line_Async -- Spawns a process asynchronously
--
-- Command_Line - The command line
--
-- When the result is not null, it has to be freed using Error_Free.
--
-- Returns :
--
-- null on success, error otherwise.
--
function Command_Line_Async
( Command_Line : UTF8_String
) return GError;
--
-- Command_Line_Sync -- Spawns a process synchronously
--
-- Command_Line - The command line
--
-- Returns :
--
-- Execution result
--
function Command_Line_Sync
( Command_Line : UTF8_String
) return Sync_Result;
--
-- Sync -- Spawns a process synchronously
--
-- [ Working_Directory ] - The working directory name
-- ArgV - Arguments list (null terminated)
-- [ EnvP ] - Environment (null terminated)
-- Flags - Spawn flags
-- Child_Setup - Setup procedure
-- User_Data - Data for the setup procedure
--
-- Upon process invokation Setup procedure is called on Child_Setup
-- object. Child_Setup and user_data are a function and user data. On
-- POSIX platforms, the function is called in the child after GLib has
-- performed all the setup it plans to perform (including creating
-- pipes, closing file descriptors, etc.) but before calling exec().
-- That is, Child_Setup is called just before calling exec() in the
-- child. Obviously actions taken in this function will only affect the
-- child, not the parent. EnvP can be null when current environment has
-- to be used.
--
-- Returns :
--
-- Execution result
--
function Sync
( Working_Directory : UTF8_String;
ArgV : Chars_Ptr_Lists.Pointer;
EnvP : Chars_Ptr_Lists.Pointer := null;
Flags : GSpawnFlags;
Child_Setup : GSpawnChildSetupFunc := null;
User_Data : Address := Null_Address
) return Sync_Result;
function Sync
( ArgV : Chars_Ptr_Lists.Pointer;
EnvP : Chars_Ptr_Lists.Pointer := null;
Flags : GSpawnFlags;
Child_Setup : GSpawnChildSetupFunc := null;
User_Data : Address := Null_Address
) return Sync_Result;
private
pragma Import (C, Child_Watch_Add, "g_child_watch_add");
pragma Import (C, Close_PID, "g_spawn_close_pid");
end GLib.Spawn_Alt;
|