17
17
18
18
with Ada.Command_Line ;
19
19
with Ada.Directories ;
20
+ with Ada.Streams ;
20
21
with Ada.Text_IO ;
21
22
with Ada.Strings.Unbounded ; use Ada.Strings.Unbounded;
22
23
with GNAT.OS_Lib ; use GNAT.OS_Lib;
23
24
24
25
with GNATCOLL.Utils ; use GNATCOLL.Utils;
25
26
with GNATCOLL.JSON ; use GNATCOLL.JSON;
26
27
28
+ with VSS.Stream_Element_Vectors ;
29
+ with VSS.Strings.Conversions ;
30
+ with VSS.Strings.Converters.Decoders ;
31
+
27
32
with Spawn.Processes.Monitor_Loop ;
28
33
29
34
package body Tester.Tests is
@@ -168,6 +173,13 @@ package body Tester.Tests is
168
173
function To_Program (Name : String) return String;
169
174
-- Take base name of the command and find it on PATH
170
175
176
+ procedure Print (V : VSS.Stream_Element_Vectors.Stream_Element_Vector);
177
+ -- Print V as string.
178
+
179
+ -- --------------
180
+ -- To_Program --
181
+ -- --------------
182
+
171
183
function To_Program (Name : String) return String is
172
184
Found : GNAT.OS_Lib.String_Access :=
173
185
GNAT.OS_Lib.Locate_Exec_On_Path (Name);
@@ -181,10 +193,20 @@ package body Tester.Tests is
181
193
Cmd : constant String := To_Program (GNATCOLL.JSON.Get (List, 1 ).Get);
182
194
Args : Spawn.String_Vectors.UTF_8_String_Vector;
183
195
184
- type Shell_Listener is new Spawn.Processes.Process_Listener with record
185
- Done : Boolean := False;
196
+ type Shell_Listener is limited new Spawn.Processes.Process_Listener with
197
+ record
198
+ Process : Spawn.Processes.Process;
199
+ Done : Boolean := False;
200
+ Stdout : VSS.Stream_Element_Vectors.Stream_Element_Vector;
201
+ Stderr : VSS.Stream_Element_Vectors.Stream_Element_Vector;
186
202
end record ;
187
203
204
+ overriding procedure Standard_Output_Available
205
+ (Self : in out Shell_Listener);
206
+
207
+ overriding procedure Standard_Error_Available
208
+ (Self : in out Shell_Listener);
209
+
188
210
overriding procedure Finished
189
211
(Self : in out Shell_Listener;
190
212
Exit_Code : Integer);
@@ -193,6 +215,10 @@ package body Tester.Tests is
193
215
(Self : in out Shell_Listener;
194
216
Process_Error : Integer);
195
217
218
+ -- ------------------
219
+ -- Error_Occurred --
220
+ -- ------------------
221
+
196
222
overriding procedure Error_Occurred
197
223
(Self : in out Shell_Listener;
198
224
Process_Error : Integer) is
@@ -210,6 +236,10 @@ package body Tester.Tests is
210
236
Self.Done := True;
211
237
end Error_Occurred ;
212
238
239
+ -- ------------
240
+ -- Finished --
241
+ -- ------------
242
+
213
243
overriding procedure Finished
214
244
(Self : in out Shell_Listener;
215
245
Exit_Code : Integer) is
@@ -230,23 +260,85 @@ package body Tester.Tests is
230
260
Self.Done := True;
231
261
end Finished ;
232
262
233
- Shell : Spawn.Processes.Process;
263
+ -- ---------
264
+ -- Print --
265
+ -- ---------
266
+
267
+ procedure Print (V : VSS.Stream_Element_Vectors.Stream_Element_Vector) is
268
+ use type Ada.Streams.Stream_Element_Count;
269
+ Decoder : VSS.Strings.Converters.Decoders.Virtual_String_Decoder;
270
+ Text : VSS.Strings.Virtual_String;
271
+ begin
272
+ if V.Length > 0 then
273
+ Decoder.Initialize (VSS.Strings.To_Virtual_String (" utf-8" ));
274
+ Text := Decoder.Decode (V);
275
+ Ada.Text_IO.Put_Line
276
+ (VSS.Strings.Conversions.To_UTF_8_String (Text));
277
+ end if ;
278
+ end Print ;
279
+
280
+ -- ----------------------------
281
+ -- Standard_Error_Available --
282
+ -- ----------------------------
283
+
284
+ overriding procedure Standard_Error_Available
285
+ (Self : in out Shell_Listener)
286
+ is
287
+ use type Ada.Streams.Stream_Element_Count;
288
+ Data : Ada.Streams.Stream_Element_Array (1 .. 512 );
289
+ Last : Ada.Streams.Stream_Element_Count;
290
+ begin
291
+ loop
292
+ Self.Process.Read_Standard_Error (Data, Last);
293
+
294
+ exit when Last < 1 ;
295
+
296
+ for X of Data (1 .. Last) loop
297
+ Self.Stderr.Append (X);
298
+ end loop ;
299
+ end loop ;
300
+ end Standard_Error_Available ;
301
+
302
+ -- -----------------------------
303
+ -- Standard_Output_Available --
304
+ -- -----------------------------
305
+
306
+ overriding procedure Standard_Output_Available
307
+ (Self : in out Shell_Listener)
308
+ is
309
+ use type Ada.Streams.Stream_Element_Count;
310
+ Data : Ada.Streams.Stream_Element_Array (1 .. 512 );
311
+ Last : Ada.Streams.Stream_Element_Count;
312
+ begin
313
+ loop
314
+ Self.Process.Read_Standard_Output (Data, Last);
315
+
316
+ exit when Last < 1 ;
317
+
318
+ for X of Data (1 .. Last) loop
319
+ Self.Stdout.Append (X);
320
+ end loop ;
321
+ end loop ;
322
+ end Standard_Output_Available ;
234
323
235
324
Listener : aliased Shell_Listener;
236
325
begin
237
326
for J in 2 .. GNATCOLL.JSON.Length (List) loop
238
327
Args.Append (GNATCOLL.JSON.Get (List, J).Get);
239
328
end loop ;
240
329
241
- Shell .Set_Listener (Listener'Unchecked_Access);
242
- Shell .Set_Program (Cmd);
243
- Shell .Set_Arguments (Args);
244
- Shell .Start;
330
+ Listener.Process .Set_Listener (Listener'Unchecked_Access);
331
+ Listener.Process .Set_Program (Cmd);
332
+ Listener.Process .Set_Arguments (Args);
333
+ Listener.Process .Start;
245
334
246
335
loop
247
336
Spawn.Processes.Monitor_Loop (Timeout => 10 );
248
337
exit when Listener.Done;
249
338
end loop ;
339
+
340
+ Print (Listener.Stdout);
341
+ Print (Listener.Stderr);
250
342
end Do_Shell ;
251
343
252
344
-- ------------
0 commit comments