| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT LIBRARY COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- G N A T . E X P E C T -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2000-2025, 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 3, 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. -- |
| 17 |
-- -- |
| 18 |
-- As a special exception under Section 7 of GPL version 3, you are granted -- |
| 19 |
-- additional permissions described in the GCC Runtime Library Exception, -- |
| 20 |
-- version 3.1, as published by the Free Software Foundation. -- |
| 21 |
-- -- |
| 22 |
-- You should have received a copy of the GNU General Public License and -- |
| 23 |
-- a copy of the GCC Runtime Library Exception along with this program; -- |
| 24 |
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see -- |
| 25 |
-- <http://www.gnu.org/licenses/>. -- |
| 26 |
-- -- |
| 27 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 28 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 29 |
-- -- |
| 30 |
------------------------------------------------------------------------------ |
| 31 |
|
| 32 |
with System; use System; |
| 33 |
with System.OS_Constants; use System.OS_Constants; |
| 34 |
with Ada.Calendar; use Ada.Calendar; |
| 35 |
|
| 36 |
with GNAT.IO; use GNAT.IO; |
| 37 |
with GNAT.OS_Lib; use GNAT.OS_Lib; |
| 38 |
with GNAT.Regpat; use GNAT.Regpat; |
| 39 |
|
| 40 |
with Ada.Unchecked_Deallocation; |
| 41 |
|
| 42 |
package body GNAT.Expect is |
| 43 |
|
| 44 |
type Array_Of_Pd is array (Positive range <>) of Process_Descriptor_Access; |
| 45 |
|
| 46 |
Expect_Process_Died : constant Expect_Match := -100; |
| 47 |
Expect_Internal_Error : constant Expect_Match := -101; |
| 48 |
-- Additional possible outputs of Expect_Internal. These are not visible in |
| 49 |
-- the spec because the user will never see them. |
| 50 |
|
| 51 |
procedure Expect_Internal |
| 52 |
(Descriptors : in out Array_Of_Pd; |
| 53 |
Result : out Expect_Match; |
| 54 |
Timeout : Integer; |
| 55 |
Full_Buffer : Boolean); |
| 56 |
-- Internal function used to read from the process Descriptor. |
| 57 |
-- |
| 58 |
-- Several outputs are possible: |
| 59 |
-- Result=Expect_Timeout, if no output was available before the timeout |
| 60 |
-- expired. |
| 61 |
-- Result=Expect_Full_Buffer, if Full_Buffer is True and some characters |
| 62 |
-- had to be discarded from the internal buffer of Descriptor. |
| 63 |
-- Result=Express_Process_Died if one of the processes was terminated. |
| 64 |
-- That process's Input_Fd is set to Invalid_FD |
| 65 |
-- Result=Express_Internal_Error |
| 66 |
-- Result=<integer>, indicates how many characters were added to the |
| 67 |
-- internal buffer. These characters are from indexes |
| 68 |
-- Descriptor.Buffer_Index - Result + 1 .. Descriptor.Buffer_Index |
| 69 |
-- Process_Died is raised if the process is no longer valid. |
| 70 |
|
| 71 |
procedure Reinitialize_Buffer |
| 72 |
(Descriptor : in out Process_Descriptor'Class); |
| 73 |
-- Reinitialize the internal buffer. |
| 74 |
-- The buffer is deleted up to the end of the last match. |
| 75 |
|
| 76 |
procedure Free is new Ada.Unchecked_Deallocation |
| 77 |
(Pattern_Matcher, Pattern_Matcher_Access); |
| 78 |
|
| 79 |
procedure Free is new Ada.Unchecked_Deallocation |
| 80 |
(Filter_List_Elem, Filter_List); |
| 81 |
|
| 82 |
procedure Call_Filters |
| 83 |
(Pid : Process_Descriptor'Class; |
| 84 |
Str : String; |
| 85 |
Filter_On : Filter_Type); |
| 86 |
-- Call all the filters that have the appropriate type. |
| 87 |
-- This function does nothing if the filters are locked |
| 88 |
|
| 89 |
------------------------------ |
| 90 |
-- Target dependent section -- |
| 91 |
------------------------------ |
| 92 |
|
| 93 |
function Dup (Fd : File_Descriptor) return File_Descriptor; |
| 94 |
pragma Import (C, Dup); |
| 95 |
|
| 96 |
procedure Dup2 (Old_Fd, New_Fd : File_Descriptor); |
| 97 |
pragma Import (C, Dup2); |
| 98 |
|
| 99 |
procedure Kill (Pid : Process_Id; Sig_Num : Integer); |
| 100 |
pragma Import (C, Kill, "__gnat_kill"); |
| 101 |
-- if Close is set to 1 all OS resources used by the Pid must be freed |
| 102 |
|
| 103 |
function Create_Pipe (Pipe : not null access Pipe_Type) return Integer; |
| 104 |
pragma Import (C, Create_Pipe, "__gnat_pipe"); |
| 105 |
|
| 106 |
function Poll |
| 107 |
(Fds : System.Address; |
| 108 |
Num_Fds : Integer; |
| 109 |
Timeout : Integer; |
| 110 |
Dead_Process : access Integer; |
| 111 |
Is_Set : System.Address) return Integer; |
| 112 |
pragma Import (C, Poll, "__gnat_expect_poll"); |
| 113 |
-- Check whether there is any data waiting on the file descriptors |
| 114 |
-- Fds, and wait if there is none, at most Timeout milliseconds |
| 115 |
-- Returns -1 in case of error, 0 if the timeout expired before |
| 116 |
-- data became available. |
| 117 |
-- |
| 118 |
-- Is_Set is an array of the same size as FDs and elements are set to 1 if |
| 119 |
-- data is available for the corresponding File Descriptor, 0 otherwise. |
| 120 |
-- |
| 121 |
-- If a process dies, then Dead_Process is set to the index of the |
| 122 |
-- corresponding file descriptor. |
| 123 |
|
| 124 |
function Waitpid (Pid : Process_Id) return Integer; |
| 125 |
pragma Import (C, Waitpid, "__gnat_waitpid"); |
| 126 |
-- Wait for a specific process id, and return its exit code |
| 127 |
|
| 128 |
--------- |
| 129 |
-- "+" -- |
| 130 |
--------- |
| 131 |
|
| 132 |
function "+" (S : String) return GNAT.OS_Lib.String_Access is |
| 133 |
begin |
| 134 |
return new String'(S); |
| 135 |
end "+"; |
| 136 |
|
| 137 |
--------- |
| 138 |
-- "+" -- |
| 139 |
--------- |
| 140 |
|
| 141 |
function "+" |
| 142 |
(P : GNAT.Regpat.Pattern_Matcher) return Pattern_Matcher_Access |
| 143 |
is |
| 144 |
begin |
| 145 |
return new GNAT.Regpat.Pattern_Matcher'(P); |
| 146 |
end "+"; |
| 147 |
|
| 148 |
---------------- |
| 149 |
-- Add_Filter -- |
| 150 |
---------------- |
| 151 |
|
| 152 |
procedure Add_Filter |
| 153 |
(Descriptor : in out Process_Descriptor; |
| 154 |
Filter : Filter_Function; |
| 155 |
Filter_On : Filter_Type := Output; |
| 156 |
User_Data : System.Address := System.Null_Address; |
| 157 |
After : Boolean := False) |
| 158 |
is |
| 159 |
Current : Filter_List := Descriptor.Filters; |
| 160 |
|
| 161 |
begin |
| 162 |
if After then |
| 163 |
while Current /= null and then Current.Next /= null loop |
| 164 |
Current := Current.Next; |
| 165 |
end loop; |
| 166 |
|
| 167 |
if Current = null then |
| 168 |
Descriptor.Filters := |
| 169 |
new Filter_List_Elem' |
| 170 |
(Filter => Filter, Filter_On => Filter_On, |
| 171 |
User_Data => User_Data, Next => null); |
| 172 |
else |
| 173 |
Current.Next := |
| 174 |
new Filter_List_Elem' |
| 175 |
(Filter => Filter, Filter_On => Filter_On, |
| 176 |
User_Data => User_Data, Next => null); |
| 177 |
end if; |
| 178 |
|
| 179 |
else |
| 180 |
Descriptor.Filters := |
| 181 |
new Filter_List_Elem' |
| 182 |
(Filter => Filter, Filter_On => Filter_On, |
| 183 |
User_Data => User_Data, Next => Descriptor.Filters); |
| 184 |
end if; |
| 185 |
end Add_Filter; |
| 186 |
|
| 187 |
------------------ |
| 188 |
-- Call_Filters -- |
| 189 |
------------------ |
| 190 |
|
| 191 |
procedure Call_Filters |
| 192 |
(Pid : Process_Descriptor'Class; |
| 193 |
Str : String; |
| 194 |
Filter_On : Filter_Type) |
| 195 |
is |
| 196 |
Current_Filter : Filter_List; |
| 197 |
|
| 198 |
begin |
| 199 |
if Pid.Filters_Lock = 0 then |
| 200 |
Current_Filter := Pid.Filters; |
| 201 |
|
| 202 |
while Current_Filter /= null loop |
| 203 |
if Current_Filter.Filter_On = Filter_On then |
| 204 |
Current_Filter.Filter |
| 205 |
(Pid, Str, Current_Filter.User_Data); |
| 206 |
end if; |
| 207 |
|
| 208 |
Current_Filter := Current_Filter.Next; |
| 209 |
end loop; |
| 210 |
end if; |
| 211 |
end Call_Filters; |
| 212 |
|
| 213 |
----------- |
| 214 |
-- Close -- |
| 215 |
----------- |
| 216 |
|
| 217 |
procedure Close |
| 218 |
(Descriptor : in out Process_Descriptor; |
| 219 |
Status : out Integer) |
| 220 |
is |
| 221 |
Current_Filter : Filter_List; |
| 222 |
Next_Filter : Filter_List; |
| 223 |
|
| 224 |
begin |
| 225 |
if Descriptor.Pid > 0 then -- see comment in Send_Signal |
| 226 |
Kill (Descriptor.Pid, Sig_Num => 9); |
| 227 |
end if; |
| 228 |
|
| 229 |
Close_Input (Descriptor); |
| 230 |
|
| 231 |
if Descriptor.Error_Fd /= Descriptor.Output_Fd |
| 232 |
and then Descriptor.Error_Fd /= Invalid_FD |
| 233 |
then |
| 234 |
Close (Descriptor.Error_Fd); |
| 235 |
end if; |
| 236 |
|
| 237 |
if Descriptor.Output_Fd /= Invalid_FD then |
| 238 |
Close (Descriptor.Output_Fd); |
| 239 |
end if; |
| 240 |
|
| 241 |
GNAT.OS_Lib.Free (Descriptor.Buffer); |
| 242 |
Descriptor.Buffer_Size := 0; |
| 243 |
|
| 244 |
Current_Filter := Descriptor.Filters; |
| 245 |
|
| 246 |
while Current_Filter /= null loop |
| 247 |
Next_Filter := Current_Filter.Next; |
| 248 |
Free (Current_Filter); |
| 249 |
Current_Filter := Next_Filter; |
| 250 |
end loop; |
| 251 |
|
| 252 |
Descriptor.Filters := null; |
| 253 |
|
| 254 |
-- Check process id (see comment in Send_Signal) |
| 255 |
|
| 256 |
if Descriptor.Pid > 0 then |
| 257 |
Status := Waitpid (Descriptor.Pid); |
| 258 |
else |
| 259 |
raise Invalid_Process; |
| 260 |
end if; |
| 261 |
end Close; |
| 262 |
|
| 263 |
procedure Close (Descriptor : in out Process_Descriptor) is |
| 264 |
Status : Integer; |
| 265 |
begin |
| 266 |
Close (Descriptor, Status); |
| 267 |
end Close; |
| 268 |
|
| 269 |
----------------- |
| 270 |
-- Close_Input -- |
| 271 |
----------------- |
| 272 |
|
| 273 |
procedure Close_Input (Pid : in out Process_Descriptor) is |
| 274 |
begin |
| 275 |
if Pid.Input_Fd /= Invalid_FD then |
| 276 |
Close (Pid.Input_Fd); |
| 277 |
end if; |
| 278 |
|
| 279 |
if Pid.Output_Fd = Pid.Input_Fd then |
| 280 |
Pid.Output_Fd := Invalid_FD; |
| 281 |
end if; |
| 282 |
|
| 283 |
if Pid.Error_Fd = Pid.Input_Fd then |
| 284 |
Pid.Error_Fd := Invalid_FD; |
| 285 |
end if; |
| 286 |
|
| 287 |
Pid.Input_Fd := Invalid_FD; |
| 288 |
end Close_Input; |
| 289 |
|
| 290 |
------------ |
| 291 |
-- Expect -- |
| 292 |
------------ |
| 293 |
|
| 294 |
procedure Expect |
| 295 |
(Descriptor : in out Process_Descriptor; |
| 296 |
Result : out Expect_Match; |
| 297 |
Regexp : String; |
| 298 |
Timeout : Integer := 10_000; |
| 299 |
Full_Buffer : Boolean := False) |
| 300 |
is |
| 301 |
begin |
| 302 |
if Regexp = "" then |
| 303 |
Expect (Descriptor, Result, Never_Match, Timeout, Full_Buffer); |
| 304 |
else |
| 305 |
Expect (Descriptor, Result, Compile (Regexp), Timeout, Full_Buffer); |
| 306 |
end if; |
| 307 |
end Expect; |
| 308 |
|
| 309 |
procedure Expect |
| 310 |
(Descriptor : in out Process_Descriptor; |
| 311 |
Result : out Expect_Match; |
| 312 |
Regexp : String; |
| 313 |
Matched : out GNAT.Regpat.Match_Array; |
| 314 |
Timeout : Integer := 10_000; |
| 315 |
Full_Buffer : Boolean := False) |
| 316 |
is |
| 317 |
begin |
| 318 |
pragma Assert (Matched'First = 0); |
| 319 |
if Regexp = "" then |
| 320 |
Expect |
| 321 |
(Descriptor, Result, Never_Match, Matched, Timeout, Full_Buffer); |
| 322 |
else |
| 323 |
Expect |
| 324 |
(Descriptor, Result, Compile (Regexp), Matched, Timeout, |
| 325 |
Full_Buffer); |
| 326 |
end if; |
| 327 |
end Expect; |
| 328 |
|
| 329 |
procedure Expect |
| 330 |
(Descriptor : in out Process_Descriptor; |
| 331 |
Result : out Expect_Match; |
| 332 |
Regexp : GNAT.Regpat.Pattern_Matcher; |
| 333 |
Timeout : Integer := 10_000; |
| 334 |
Full_Buffer : Boolean := False) |
| 335 |
is |
| 336 |
Matched : GNAT.Regpat.Match_Array (0 .. 0); |
| 337 |
pragma Warnings (Off, Matched); |
| 338 |
begin |
| 339 |
Expect (Descriptor, Result, Regexp, Matched, Timeout, Full_Buffer); |
| 340 |
end Expect; |
| 341 |
|
| 342 |
procedure Expect |
| 343 |
(Descriptor : in out Process_Descriptor; |
| 344 |
Result : out Expect_Match; |
| 345 |
Regexp : GNAT.Regpat.Pattern_Matcher; |
| 346 |
Matched : out GNAT.Regpat.Match_Array; |
| 347 |
Timeout : Integer := 10_000; |
| 348 |
Full_Buffer : Boolean := False) |
| 349 |
is |
| 350 |
N : Expect_Match; |
| 351 |
Descriptors : Array_Of_Pd := [Descriptor'Unrestricted_Access]; |
| 352 |
Try_Until : constant Time := Clock + Duration (Timeout) / 1000.0; |
| 353 |
Timeout_Tmp : Integer := Timeout; |
| 354 |
|
| 355 |
begin |
| 356 |
pragma Assert (Matched'First = 0); |
| 357 |
Reinitialize_Buffer (Descriptor); |
| 358 |
|
| 359 |
loop |
| 360 |
-- First, test if what is already in the buffer matches (This is |
| 361 |
-- required if this package is used in multi-task mode, since one of |
| 362 |
-- the tasks might have added something in the buffer, and we don't |
| 363 |
-- want other tasks to wait for new input to be available before |
| 364 |
-- checking the regexps). |
| 365 |
|
| 366 |
Match |
| 367 |
(Regexp, Descriptor.Buffer (1 .. Descriptor.Buffer_Index), Matched); |
| 368 |
|
| 369 |
if Descriptor.Buffer_Index >= 1 and then Matched (0).First /= 0 then |
| 370 |
Result := 1; |
| 371 |
Descriptor.Last_Match_Start := Matched (0).First; |
| 372 |
Descriptor.Last_Match_End := Matched (0).Last; |
| 373 |
return; |
| 374 |
end if; |
| 375 |
|
| 376 |
-- Else try to read new input |
| 377 |
|
| 378 |
Expect_Internal (Descriptors, N, Timeout_Tmp, Full_Buffer); |
| 379 |
|
| 380 |
case N is |
| 381 |
when Expect_Internal_Error |
| 382 |
| Expect_Process_Died |
| 383 |
=> |
| 384 |
raise Process_Died; |
| 385 |
|
| 386 |
when Expect_Full_Buffer |
| 387 |
| Expect_Timeout |
| 388 |
=> |
| 389 |
Result := N; |
| 390 |
return; |
| 391 |
|
| 392 |
when others => |
| 393 |
null; -- See below |
| 394 |
end case; |
| 395 |
|
| 396 |
-- Calculate the timeout for the next turn |
| 397 |
|
| 398 |
-- Note that Timeout is, from the caller's perspective, the maximum |
| 399 |
-- time until a match, not the maximum time until some output is |
| 400 |
-- read, and thus cannot be reused as is for Expect_Internal. |
| 401 |
|
| 402 |
if Timeout /= -1 then |
| 403 |
Timeout_Tmp := Integer (Try_Until - Clock) * 1000; |
| 404 |
|
| 405 |
if Timeout_Tmp < 0 then |
| 406 |
Result := Expect_Timeout; |
| 407 |
exit; |
| 408 |
end if; |
| 409 |
end if; |
| 410 |
end loop; |
| 411 |
|
| 412 |
-- Even if we had the general timeout above, we have to test that the |
| 413 |
-- last test we read from the external process didn't match. |
| 414 |
|
| 415 |
Match |
| 416 |
(Regexp, Descriptor.Buffer (1 .. Descriptor.Buffer_Index), Matched); |
| 417 |
|
| 418 |
if Matched (0).First /= 0 then |
| 419 |
Result := 1; |
| 420 |
Descriptor.Last_Match_Start := Matched (0).First; |
| 421 |
Descriptor.Last_Match_End := Matched (0).Last; |
| 422 |
return; |
| 423 |
end if; |
| 424 |
end Expect; |
| 425 |
|
| 426 |
procedure Expect |
| 427 |
(Descriptor : in out Process_Descriptor; |
| 428 |
Result : out Expect_Match; |
| 429 |
Regexps : Regexp_Array; |
| 430 |
Timeout : Integer := 10_000; |
| 431 |
Full_Buffer : Boolean := False) |
| 432 |
is |
| 433 |
Patterns : Compiled_Regexp_Array (Regexps'Range); |
| 434 |
|
| 435 |
Matched : GNAT.Regpat.Match_Array (0 .. 0); |
| 436 |
pragma Warnings (Off, Matched); |
| 437 |
|
| 438 |
begin |
| 439 |
for J in Regexps'Range loop |
| 440 |
Patterns (J) := new Pattern_Matcher'(Compile (Regexps (J).all)); |
| 441 |
end loop; |
| 442 |
|
| 443 |
Expect (Descriptor, Result, Patterns, Matched, Timeout, Full_Buffer); |
| 444 |
|
| 445 |
for J in Regexps'Range loop |
| 446 |
Free (Patterns (J)); |
| 447 |
end loop; |
| 448 |
end Expect; |
| 449 |
|
| 450 |
procedure Expect |
| 451 |
(Descriptor : in out Process_Descriptor; |
| 452 |
Result : out Expect_Match; |
| 453 |
Regexps : Compiled_Regexp_Array; |
| 454 |
Timeout : Integer := 10_000; |
| 455 |
Full_Buffer : Boolean := False) |
| 456 |
is |
| 457 |
Matched : GNAT.Regpat.Match_Array (0 .. 0); |
| 458 |
pragma Warnings (Off, Matched); |
| 459 |
begin |
| 460 |
Expect (Descriptor, Result, Regexps, Matched, Timeout, Full_Buffer); |
| 461 |
end Expect; |
| 462 |
|
| 463 |
procedure Expect |
| 464 |
(Result : out Expect_Match; |
| 465 |
Regexps : Multiprocess_Regexp_Array; |
| 466 |
Timeout : Integer := 10_000; |
| 467 |
Full_Buffer : Boolean := False) |
| 468 |
is |
| 469 |
Matched : GNAT.Regpat.Match_Array (0 .. 0); |
| 470 |
pragma Warnings (Off, Matched); |
| 471 |
begin |
| 472 |
Expect (Result, Regexps, Matched, Timeout, Full_Buffer); |
| 473 |
end Expect; |
| 474 |
|
| 475 |
procedure Expect |
| 476 |
(Descriptor : in out Process_Descriptor; |
| 477 |
Result : out Expect_Match; |
| 478 |
Regexps : Regexp_Array; |
| 479 |
Matched : out GNAT.Regpat.Match_Array; |
| 480 |
Timeout : Integer := 10_000; |
| 481 |
Full_Buffer : Boolean := False) |
| 482 |
is |
| 483 |
Patterns : Compiled_Regexp_Array (Regexps'Range); |
| 484 |
|
| 485 |
begin |
| 486 |
pragma Assert (Matched'First = 0); |
| 487 |
|
| 488 |
for J in Regexps'Range loop |
| 489 |
Patterns (J) := new Pattern_Matcher'(Compile (Regexps (J).all)); |
| 490 |
end loop; |
| 491 |
|
| 492 |
Expect (Descriptor, Result, Patterns, Matched, Timeout, Full_Buffer); |
| 493 |
|
| 494 |
for J in Regexps'Range loop |
| 495 |
Free (Patterns (J)); |
| 496 |
end loop; |
| 497 |
end Expect; |
| 498 |
|
| 499 |
procedure Expect |
| 500 |
(Descriptor : in out Process_Descriptor; |
| 501 |
Result : out Expect_Match; |
| 502 |
Regexps : Compiled_Regexp_Array; |
| 503 |
Matched : out GNAT.Regpat.Match_Array; |
| 504 |
Timeout : Integer := 10_000; |
| 505 |
Full_Buffer : Boolean := False) |
| 506 |
is |
| 507 |
N : Expect_Match; |
| 508 |
Descriptors : Array_Of_Pd := [Descriptor'Unrestricted_Access]; |
| 509 |
|
| 510 |
begin |
| 511 |
pragma Assert (Matched'First = 0); |
| 512 |
|
| 513 |
Reinitialize_Buffer (Descriptor); |
| 514 |
|
| 515 |
loop |
| 516 |
-- First, test if what is already in the buffer matches (This is |
| 517 |
-- required if this package is used in multi-task mode, since one of |
| 518 |
-- the tasks might have added something in the buffer, and we don't |
| 519 |
-- want other tasks to wait for new input to be available before |
| 520 |
-- checking the regexps). |
| 521 |
|
| 522 |
if Descriptor.Buffer /= null then |
| 523 |
for J in Regexps'Range loop |
| 524 |
Match |
| 525 |
(Regexps (J).all, |
| 526 |
Descriptor.Buffer (1 .. Descriptor.Buffer_Index), |
| 527 |
Matched); |
| 528 |
|
| 529 |
if Matched (0) /= No_Match then |
| 530 |
Result := Expect_Match (J); |
| 531 |
Descriptor.Last_Match_Start := Matched (0).First; |
| 532 |
Descriptor.Last_Match_End := Matched (0).Last; |
| 533 |
return; |
| 534 |
end if; |
| 535 |
end loop; |
| 536 |
end if; |
| 537 |
|
| 538 |
Expect_Internal (Descriptors, N, Timeout, Full_Buffer); |
| 539 |
|
| 540 |
case N is |
| 541 |
when Expect_Internal_Error |
| 542 |
| Expect_Process_Died |
| 543 |
=> |
| 544 |
raise Process_Died; |
| 545 |
|
| 546 |
when Expect_Full_Buffer |
| 547 |
| Expect_Timeout |
| 548 |
=> |
| 549 |
Result := N; |
| 550 |
return; |
| 551 |
|
| 552 |
when others => |
| 553 |
null; -- Continue |
| 554 |
end case; |
| 555 |
end loop; |
| 556 |
end Expect; |
| 557 |
|
| 558 |
procedure Expect |
| 559 |
(Result : out Expect_Match; |
| 560 |
Regexps : Multiprocess_Regexp_Array; |
| 561 |
Matched : out GNAT.Regpat.Match_Array; |
| 562 |
Timeout : Integer := 10_000; |
| 563 |
Full_Buffer : Boolean := False) |
| 564 |
is |
| 565 |
N : Expect_Match; |
| 566 |
Descriptors : Array_Of_Pd (Regexps'Range); |
| 567 |
|
| 568 |
begin |
| 569 |
pragma Assert (Matched'First = 0); |
| 570 |
|
| 571 |
for J in Descriptors'Range loop |
| 572 |
Descriptors (J) := Regexps (J).Descriptor; |
| 573 |
|
| 574 |
if Descriptors (J) /= null then |
| 575 |
Reinitialize_Buffer (Regexps (J).Descriptor.all); |
| 576 |
end if; |
| 577 |
end loop; |
| 578 |
|
| 579 |
loop |
| 580 |
-- First, test if what is already in the buffer matches (This is |
| 581 |
-- required if this package is used in multi-task mode, since one of |
| 582 |
-- the tasks might have added something in the buffer, and we don't |
| 583 |
-- want other tasks to wait for new input to be available before |
| 584 |
-- checking the regexps). |
| 585 |
|
| 586 |
for J in Regexps'Range loop |
| 587 |
if Regexps (J).Regexp /= null |
| 588 |
and then Regexps (J).Descriptor /= null |
| 589 |
then |
| 590 |
Match (Regexps (J).Regexp.all, |
| 591 |
Regexps (J).Descriptor.Buffer |
| 592 |
(1 .. Regexps (J).Descriptor.Buffer_Index), |
| 593 |
Matched); |
| 594 |
|
| 595 |
if Matched (0) /= No_Match then |
| 596 |
Result := Expect_Match (J); |
| 597 |
Regexps (J).Descriptor.Last_Match_Start := Matched (0).First; |
| 598 |
Regexps (J).Descriptor.Last_Match_End := Matched (0).Last; |
| 599 |
return; |
| 600 |
end if; |
| 601 |
end if; |
| 602 |
end loop; |
| 603 |
|
| 604 |
Expect_Internal (Descriptors, N, Timeout, Full_Buffer); |
| 605 |
|
| 606 |
case N is |
| 607 |
when Expect_Internal_Error |
| 608 |
| Expect_Process_Died |
| 609 |
=> |
| 610 |
raise Process_Died; |
| 611 |
|
| 612 |
when Expect_Full_Buffer |
| 613 |
| Expect_Timeout |
| 614 |
=> |
| 615 |
Result := N; |
| 616 |
return; |
| 617 |
|
| 618 |
when others => |
| 619 |
null; -- Continue |
| 620 |
end case; |
| 621 |
end loop; |
| 622 |
end Expect; |
| 623 |
|
| 624 |
--------------------- |
| 625 |
-- Expect_Internal -- |
| 626 |
--------------------- |
| 627 |
|
| 628 |
procedure Expect_Internal |
| 629 |
(Descriptors : in out Array_Of_Pd; |
| 630 |
Result : out Expect_Match; |
| 631 |
Timeout : Integer; |
| 632 |
Full_Buffer : Boolean) |
| 633 |
is |
| 634 |
Num_Descriptors : Integer; |
| 635 |
Buffer_Size : Integer := 0; |
| 636 |
|
| 637 |
N : Integer; |
| 638 |
|
| 639 |
type File_Descriptor_Array is |
| 640 |
array (0 .. Descriptors'Length - 1) of File_Descriptor; |
| 641 |
Fds : aliased File_Descriptor_Array; |
| 642 |
Fds_Count : Natural := 0; |
| 643 |
|
| 644 |
Fds_To_Descriptor : array (Fds'Range) of Integer; |
| 645 |
-- Maps file descriptor entries from Fds to entries in Descriptors. |
| 646 |
-- They do not have the same index when entries in Descriptors are null. |
| 647 |
|
| 648 |
type Integer_Array is array (Fds'Range) of Integer; |
| 649 |
Is_Set : aliased Integer_Array; |
| 650 |
|
| 651 |
begin |
| 652 |
for J in Descriptors'Range loop |
| 653 |
if Descriptors (J) /= null |
| 654 |
and then Descriptors (J).Output_Fd /= Invalid_FD |
| 655 |
then |
| 656 |
Fds (Fds'First + Fds_Count) := Descriptors (J).Output_Fd; |
| 657 |
Fds_To_Descriptor (Fds'First + Fds_Count) := J; |
| 658 |
Fds_Count := Fds_Count + 1; |
| 659 |
|
| 660 |
if Descriptors (J).Buffer_Size = 0 then |
| 661 |
Buffer_Size := Integer'Max (Buffer_Size, 4096); |
| 662 |
else |
| 663 |
Buffer_Size := |
| 664 |
Integer'Max (Buffer_Size, Descriptors (J).Buffer_Size); |
| 665 |
end if; |
| 666 |
end if; |
| 667 |
end loop; |
| 668 |
|
| 669 |
if Fds_Count = 0 then |
| 670 |
-- There are no descriptors to monitor, it means that process died. |
| 671 |
|
| 672 |
Result := Expect_Process_Died; |
| 673 |
|
| 674 |
return; |
| 675 |
end if; |
| 676 |
|
| 677 |
declare |
| 678 |
Buffer : aliased String (1 .. Buffer_Size); |
| 679 |
-- Buffer used for input. This is allocated only once, not for |
| 680 |
-- every iteration of the loop |
| 681 |
|
| 682 |
D : aliased Integer; |
| 683 |
-- Index in Descriptors |
| 684 |
|
| 685 |
begin |
| 686 |
-- Loop until we match or we have a timeout |
| 687 |
|
| 688 |
loop |
| 689 |
-- Poll may be interrupted on Linux by a signal and need to be |
| 690 |
-- repeated. We don't want to check for errno = EINTER, so just |
| 691 |
-- attempt to call Poll a few times. |
| 692 |
|
| 693 |
for J in 1 .. 3 loop |
| 694 |
Num_Descriptors := |
| 695 |
Poll |
| 696 |
(Fds'Address, Fds_Count, Timeout, D'Access, Is_Set'Address); |
| 697 |
|
| 698 |
exit when Num_Descriptors /= -1; |
| 699 |
end loop; |
| 700 |
|
| 701 |
case Num_Descriptors is |
| 702 |
|
| 703 |
-- Error? |
| 704 |
|
| 705 |
when -1 => |
| 706 |
Result := Expect_Internal_Error; |
| 707 |
|
| 708 |
if D /= 0 then |
| 709 |
Close_Input (Descriptors (D).all); |
| 710 |
end if; |
| 711 |
|
| 712 |
return; |
| 713 |
|
| 714 |
-- Timeout? |
| 715 |
|
| 716 |
when 0 => |
| 717 |
Result := Expect_Timeout; |
| 718 |
return; |
| 719 |
|
| 720 |
-- Some input |
| 721 |
|
| 722 |
when others => |
| 723 |
for F in Fds'Range loop |
| 724 |
if Is_Set (F) = 1 then |
| 725 |
D := Fds_To_Descriptor (F); |
| 726 |
|
| 727 |
Buffer_Size := Descriptors (D).Buffer_Size; |
| 728 |
|
| 729 |
if Buffer_Size = 0 then |
| 730 |
Buffer_Size := 4096; |
| 731 |
end if; |
| 732 |
|
| 733 |
-- Read may be interrupted on Linux by a signal and |
| 734 |
-- need to be repeated. We don't want to check for |
| 735 |
-- errno = EINTER, so just attempt to read a few |
| 736 |
-- times. |
| 737 |
|
| 738 |
for J in 1 .. 3 loop |
| 739 |
N := Read (Descriptors (D).Output_Fd, |
| 740 |
Buffer'Address, Buffer_Size); |
| 741 |
|
| 742 |
exit when N > 0; |
| 743 |
end loop; |
| 744 |
|
| 745 |
-- Error or End of file |
| 746 |
|
| 747 |
if N <= 0 then |
| 748 |
Close_Input (Descriptors (D).all); |
| 749 |
Result := Expect_Process_Died; |
| 750 |
|
| 751 |
return; |
| 752 |
|
| 753 |
else |
| 754 |
-- If there is no limit to the buffer size |
| 755 |
|
| 756 |
if Descriptors (D).Buffer_Size = 0 then |
| 757 |
declare |
| 758 |
Tmp : String_Access := Descriptors (D).Buffer; |
| 759 |
|
| 760 |
begin |
| 761 |
if Tmp /= null then |
| 762 |
Descriptors (D).Buffer := |
| 763 |
new String (1 .. Tmp'Length + N); |
| 764 |
Descriptors (D).Buffer (1 .. Tmp'Length) := |
| 765 |
Tmp.all; |
| 766 |
Descriptors (D).Buffer |
| 767 |
(Tmp'Length + 1 .. Tmp'Length + N) := |
| 768 |
Buffer (1 .. N); |
| 769 |
Free (Tmp); |
| 770 |
Descriptors (D).Buffer_Index := |
| 771 |
Descriptors (D).Buffer'Last; |
| 772 |
|
| 773 |
else |
| 774 |
Descriptors (D).Buffer := |
| 775 |
new String (1 .. N); |
| 776 |
Descriptors (D).Buffer.all := |
| 777 |
Buffer (1 .. N); |
| 778 |
Descriptors (D).Buffer_Index := N; |
| 779 |
end if; |
| 780 |
end; |
| 781 |
|
| 782 |
else |
| 783 |
-- Add what we read to the buffer |
| 784 |
|
| 785 |
if Descriptors (D).Buffer_Index + N > |
| 786 |
Descriptors (D).Buffer_Size |
| 787 |
then |
| 788 |
-- If the user wants to know when we have |
| 789 |
-- read more than the buffer can contain. |
| 790 |
|
| 791 |
if Full_Buffer then |
| 792 |
Result := Expect_Full_Buffer; |
| 793 |
return; |
| 794 |
end if; |
| 795 |
|
| 796 |
-- Keep as much as possible from the buffer, |
| 797 |
-- and forget old characters. |
| 798 |
|
| 799 |
Descriptors (D).Buffer |
| 800 |
(1 .. Descriptors (D).Buffer_Size - N) := |
| 801 |
Descriptors (D).Buffer |
| 802 |
(N - Descriptors (D).Buffer_Size + |
| 803 |
Descriptors (D).Buffer_Index + 1 .. |
| 804 |
Descriptors (D).Buffer_Index); |
| 805 |
Descriptors (D).Buffer_Index := |
| 806 |
Descriptors (D).Buffer_Size - N; |
| 807 |
end if; |
| 808 |
|
| 809 |
-- Keep what we read in the buffer |
| 810 |
|
| 811 |
Descriptors (D).Buffer |
| 812 |
(Descriptors (D).Buffer_Index + 1 .. |
| 813 |
Descriptors (D).Buffer_Index + N) := |
| 814 |
Buffer (1 .. N); |
| 815 |
Descriptors (D).Buffer_Index := |
| 816 |
Descriptors (D).Buffer_Index + N; |
| 817 |
end if; |
| 818 |
|
| 819 |
-- Call each of the output filter with what we |
| 820 |
-- read. |
| 821 |
|
| 822 |
Call_Filters |
| 823 |
(Descriptors (D).all, Buffer (1 .. N), Output); |
| 824 |
|
| 825 |
Result := Expect_Match (D); |
| 826 |
return; |
| 827 |
end if; |
| 828 |
end if; |
| 829 |
end loop; |
| 830 |
end case; |
| 831 |
end loop; |
| 832 |
end; |
| 833 |
end Expect_Internal; |
| 834 |
|
| 835 |
---------------- |
| 836 |
-- Expect_Out -- |
| 837 |
---------------- |
| 838 |
|
| 839 |
function Expect_Out (Descriptor : Process_Descriptor) return String is |
| 840 |
begin |
| 841 |
return Descriptor.Buffer (1 .. Descriptor.Last_Match_End); |
| 842 |
end Expect_Out; |
| 843 |
|
| 844 |
---------------------- |
| 845 |
-- Expect_Out_Match -- |
| 846 |
---------------------- |
| 847 |
|
| 848 |
function Expect_Out_Match (Descriptor : Process_Descriptor) return String is |
| 849 |
begin |
| 850 |
return Descriptor.Buffer |
| 851 |
(Descriptor.Last_Match_Start .. Descriptor.Last_Match_End); |
| 852 |
end Expect_Out_Match; |
| 853 |
|
| 854 |
------------------------ |
| 855 |
-- First_Dead_Process -- |
| 856 |
------------------------ |
| 857 |
|
| 858 |
function First_Dead_Process |
| 859 |
(Regexp : Multiprocess_Regexp_Array) return Natural is |
| 860 |
begin |
| 861 |
for R in Regexp'Range loop |
| 862 |
if Regexp (R).Descriptor /= null |
| 863 |
and then Regexp (R).Descriptor.Input_Fd = GNAT.OS_Lib.Invalid_FD |
| 864 |
then |
| 865 |
return R; |
| 866 |
end if; |
| 867 |
end loop; |
| 868 |
|
| 869 |
return 0; |
| 870 |
end First_Dead_Process; |
| 871 |
|
| 872 |
----------- |
| 873 |
-- Flush -- |
| 874 |
----------- |
| 875 |
|
| 876 |
procedure Flush |
| 877 |
(Descriptor : in out Process_Descriptor; |
| 878 |
Timeout : Integer := 0) |
| 879 |
is |
| 880 |
Buffer_Size : constant Integer := 8192; |
| 881 |
Num_Descriptors : Integer; |
| 882 |
N : aliased Integer; |
| 883 |
Is_Set : aliased Integer; |
| 884 |
Buffer : aliased String (1 .. Buffer_Size); |
| 885 |
|
| 886 |
begin |
| 887 |
-- Empty the current buffer |
| 888 |
|
| 889 |
Descriptor.Last_Match_End := Descriptor.Buffer_Index; |
| 890 |
Reinitialize_Buffer (Descriptor); |
| 891 |
|
| 892 |
-- Read everything from the process to flush its output |
| 893 |
|
| 894 |
loop |
| 895 |
Num_Descriptors := |
| 896 |
Poll (Descriptor.Output_Fd'Address, |
| 897 |
1, |
| 898 |
Timeout, |
| 899 |
N'Access, |
| 900 |
Is_Set'Address); |
| 901 |
|
| 902 |
case Num_Descriptors is |
| 903 |
|
| 904 |
-- Error ? |
| 905 |
|
| 906 |
when -1 => |
| 907 |
raise Process_Died; |
| 908 |
|
| 909 |
-- Timeout => End of flush |
| 910 |
|
| 911 |
when 0 => |
| 912 |
return; |
| 913 |
|
| 914 |
-- Some input |
| 915 |
|
| 916 |
when others => |
| 917 |
if Is_Set = 1 then |
| 918 |
N := Read (Descriptor.Output_Fd, Buffer'Address, |
| 919 |
Buffer_Size); |
| 920 |
|
| 921 |
if N = -1 then |
| 922 |
raise Process_Died; |
| 923 |
elsif N = 0 then |
| 924 |
return; |
| 925 |
end if; |
| 926 |
end if; |
| 927 |
end case; |
| 928 |
end loop; |
| 929 |
end Flush; |
| 930 |
|
| 931 |
---------- |
| 932 |
-- Free -- |
| 933 |
---------- |
| 934 |
|
| 935 |
procedure Free (Regexp : in out Multiprocess_Regexp) is |
| 936 |
procedure Unchecked_Free is new Ada.Unchecked_Deallocation |
| 937 |
(Process_Descriptor'Class, Process_Descriptor_Access); |
| 938 |
begin |
| 939 |
Unchecked_Free (Regexp.Descriptor); |
| 940 |
Free (Regexp.Regexp); |
| 941 |
end Free; |
| 942 |
|
| 943 |
------------------------ |
| 944 |
-- Get_Command_Output -- |
| 945 |
------------------------ |
| 946 |
|
| 947 |
function Get_Command_Output |
| 948 |
(Command : String; |
| 949 |
Arguments : GNAT.OS_Lib.Argument_List; |
| 950 |
Input : String; |
| 951 |
Status : not null access Integer; |
| 952 |
Err_To_Out : Boolean := False) return String |
| 953 |
is |
| 954 |
Process : Process_Descriptor; |
| 955 |
|
| 956 |
Output : String_Access := new String (1 .. 1024); |
| 957 |
-- Buffer used to accumulate standard output from the launched |
| 958 |
-- command, expanded as necessary during execution. |
| 959 |
|
| 960 |
Last : Integer := 0; |
| 961 |
-- Index of the last used character within Output |
| 962 |
|
| 963 |
begin |
| 964 |
Non_Blocking_Spawn |
| 965 |
(Process, Command, Arguments, Err_To_Out => Err_To_Out, |
| 966 |
Buffer_Size => 0); |
| 967 |
|
| 968 |
if Input'Length > 0 then |
| 969 |
Send (Process, Input); |
| 970 |
end if; |
| 971 |
|
| 972 |
Close_Input (Process); |
| 973 |
|
| 974 |
declare |
| 975 |
Result : Expect_Match; |
| 976 |
|
| 977 |
begin |
| 978 |
-- This loop runs until the call to Expect raises Process_Died |
| 979 |
|
| 980 |
loop |
| 981 |
Expect (Process, Result, ".+", Timeout => -1); |
| 982 |
|
| 983 |
declare |
| 984 |
NOutput : String_Access; |
| 985 |
S : constant String := Expect_Out (Process); |
| 986 |
pragma Assert (S'Length > 0); |
| 987 |
|
| 988 |
begin |
| 989 |
-- Expand buffer if we need more space. Note here that we add |
| 990 |
-- S'Length to ensure that S will fit in the new buffer size. |
| 991 |
|
| 992 |
if Last + S'Length > Output'Last then |
| 993 |
NOutput := new String (1 .. 2 * Output'Last + S'Length); |
| 994 |
NOutput (Output'Range) := Output.all; |
| 995 |
Free (Output); |
| 996 |
|
| 997 |
-- Here if current buffer size is OK |
| 998 |
|
| 999 |
else |
| 1000 |
NOutput := Output; |
| 1001 |
end if; |
| 1002 |
|
| 1003 |
NOutput (Last + 1 .. Last + S'Length) := S; |
| 1004 |
Last := Last + S'Length; |
| 1005 |
Output := NOutput; |
| 1006 |
end; |
| 1007 |
end loop; |
| 1008 |
|
| 1009 |
exception |
| 1010 |
when Process_Died => |
| 1011 |
Close (Process, Status.all); |
| 1012 |
end; |
| 1013 |
|
| 1014 |
if Last = 0 then |
| 1015 |
Free (Output); |
| 1016 |
return ""; |
| 1017 |
end if; |
| 1018 |
|
| 1019 |
declare |
| 1020 |
S : constant String := Output (1 .. Last); |
| 1021 |
begin |
| 1022 |
Free (Output); |
| 1023 |
return S; |
| 1024 |
end; |
| 1025 |
end Get_Command_Output; |
| 1026 |
|
| 1027 |
------------------ |
| 1028 |
-- Get_Error_Fd -- |
| 1029 |
------------------ |
| 1030 |
|
| 1031 |
function Get_Error_Fd |
| 1032 |
(Descriptor : Process_Descriptor) return GNAT.OS_Lib.File_Descriptor |
| 1033 |
is |
| 1034 |
begin |
| 1035 |
return Descriptor.Error_Fd; |
| 1036 |
end Get_Error_Fd; |
| 1037 |
|
| 1038 |
------------------ |
| 1039 |
-- Get_Input_Fd -- |
| 1040 |
------------------ |
| 1041 |
|
| 1042 |
function Get_Input_Fd |
| 1043 |
(Descriptor : Process_Descriptor) return GNAT.OS_Lib.File_Descriptor |
| 1044 |
is |
| 1045 |
begin |
| 1046 |
return Descriptor.Input_Fd; |
| 1047 |
end Get_Input_Fd; |
| 1048 |
|
| 1049 |
------------------- |
| 1050 |
-- Get_Output_Fd -- |
| 1051 |
------------------- |
| 1052 |
|
| 1053 |
function Get_Output_Fd |
| 1054 |
(Descriptor : Process_Descriptor) return GNAT.OS_Lib.File_Descriptor |
| 1055 |
is |
| 1056 |
begin |
| 1057 |
return Descriptor.Output_Fd; |
| 1058 |
end Get_Output_Fd; |
| 1059 |
|
| 1060 |
------------- |
| 1061 |
-- Get_Pid -- |
| 1062 |
------------- |
| 1063 |
|
| 1064 |
function Get_Pid |
| 1065 |
(Descriptor : Process_Descriptor) return Process_Id |
| 1066 |
is |
| 1067 |
begin |
| 1068 |
return Descriptor.Pid; |
| 1069 |
end Get_Pid; |
| 1070 |
|
| 1071 |
----------------- |
| 1072 |
-- Has_Process -- |
| 1073 |
----------------- |
| 1074 |
|
| 1075 |
function Has_Process (Regexp : Multiprocess_Regexp_Array) return Boolean is |
| 1076 |
begin |
| 1077 |
return Regexp /= [Regexp'Range => (null, null)]; |
| 1078 |
end Has_Process; |
| 1079 |
|
| 1080 |
--------------- |
| 1081 |
-- Interrupt -- |
| 1082 |
--------------- |
| 1083 |
|
| 1084 |
procedure Interrupt (Descriptor : in out Process_Descriptor) is |
| 1085 |
SIGINT : constant := 2; |
| 1086 |
begin |
| 1087 |
Send_Signal (Descriptor, SIGINT); |
| 1088 |
end Interrupt; |
| 1089 |
|
| 1090 |
------------------ |
| 1091 |
-- Lock_Filters -- |
| 1092 |
------------------ |
| 1093 |
|
| 1094 |
procedure Lock_Filters (Descriptor : in out Process_Descriptor) is |
| 1095 |
begin |
| 1096 |
Descriptor.Filters_Lock := Descriptor.Filters_Lock + 1; |
| 1097 |
end Lock_Filters; |
| 1098 |
|
| 1099 |
------------------------ |
| 1100 |
-- Non_Blocking_Spawn -- |
| 1101 |
------------------------ |
| 1102 |
|
| 1103 |
procedure Non_Blocking_Spawn |
| 1104 |
(Descriptor : out Process_Descriptor'Class; |
| 1105 |
Command : String; |
| 1106 |
Args : GNAT.OS_Lib.Argument_List; |
| 1107 |
Buffer_Size : Natural := 4096; |
| 1108 |
Err_To_Out : Boolean := False) |
| 1109 |
is |
| 1110 |
function Fork return Process_Id; |
| 1111 |
pragma Import (C, Fork, "__gnat_expect_fork"); |
| 1112 |
-- Starts a new process if possible. See the Unix command fork for more |
| 1113 |
-- information. On systems that do not support this capability (such as |
| 1114 |
-- Windows...), this command does nothing, and Fork will return |
| 1115 |
-- Null_Pid. |
| 1116 |
|
| 1117 |
Pipe1, Pipe2, Pipe3 : aliased Pipe_Type; |
| 1118 |
|
| 1119 |
Arg : String_Access; |
| 1120 |
Arg_List : String_List (1 .. Args'Length + 2); |
| 1121 |
C_Arg_List : aliased array (1 .. Args'Length + 2) of System.Address; |
| 1122 |
|
| 1123 |
Command_With_Path : String_Access; |
| 1124 |
|
| 1125 |
begin |
| 1126 |
Command_With_Path := Locate_Exec_On_Path (Command); |
| 1127 |
|
| 1128 |
if Command_With_Path = null then |
| 1129 |
raise Invalid_Process; |
| 1130 |
end if; |
| 1131 |
|
| 1132 |
-- Create the rest of the pipes once we know we will be able to |
| 1133 |
-- execute the process. |
| 1134 |
|
| 1135 |
Set_Up_Communications |
| 1136 |
(Descriptor, Err_To_Out, Pipe1'Access, Pipe2'Access, Pipe3'Access); |
| 1137 |
|
| 1138 |
-- Fork a new process |
| 1139 |
|
| 1140 |
Descriptor.Pid := Fork; |
| 1141 |
|
| 1142 |
-- Are we now in the child (or, for Windows, still in the common |
| 1143 |
-- process). |
| 1144 |
|
| 1145 |
if Descriptor.Pid = Null_Pid then |
| 1146 |
-- Prepare an array of arguments to pass to C |
| 1147 |
|
| 1148 |
Arg := new String (1 .. Command_With_Path'Length + 1); |
| 1149 |
Arg (1 .. Command_With_Path'Length) := Command_With_Path.all; |
| 1150 |
Arg (Arg'Last) := ASCII.NUL; |
| 1151 |
Arg_List (1) := Arg; |
| 1152 |
|
| 1153 |
for J in Args'Range loop |
| 1154 |
Arg := new String (1 .. Args (J)'Length + 1); |
| 1155 |
Arg (1 .. Args (J)'Length) := Args (J).all; |
| 1156 |
Arg (Arg'Last) := ASCII.NUL; |
| 1157 |
Arg_List (J + 2 - Args'First) := Arg.all'Access; |
| 1158 |
end loop; |
| 1159 |
|
| 1160 |
Arg_List (Arg_List'Last) := null; |
| 1161 |
|
| 1162 |
-- Make sure all arguments are compatible with OS conventions |
| 1163 |
|
| 1164 |
Normalize_Arguments (Arg_List); |
| 1165 |
|
| 1166 |
-- Prepare low-level argument list from the normalized arguments |
| 1167 |
|
| 1168 |
for K in Arg_List'Range loop |
| 1169 |
C_Arg_List (K) := |
| 1170 |
(if Arg_List (K) /= null |
| 1171 |
then Arg_List (K).all'Address |
| 1172 |
else System.Null_Address); |
| 1173 |
end loop; |
| 1174 |
|
| 1175 |
-- This does not return on Unix systems |
| 1176 |
|
| 1177 |
Set_Up_Child_Communications |
| 1178 |
(Descriptor, Pipe1, Pipe2, Pipe3, Command_With_Path.all, |
| 1179 |
C_Arg_List'Address); |
| 1180 |
|
| 1181 |
-- On Windows systems we need to release memory taken for Arg_List |
| 1182 |
|
| 1183 |
for A of Arg_List loop |
| 1184 |
Free (A); |
| 1185 |
end loop; |
| 1186 |
end if; |
| 1187 |
|
| 1188 |
Free (Command_With_Path); |
| 1189 |
|
| 1190 |
-- Did we have an error when spawning the child ? |
| 1191 |
|
| 1192 |
if Descriptor.Pid < Null_Pid then |
| 1193 |
raise Invalid_Process; |
| 1194 |
else |
| 1195 |
-- We are now in the parent process |
| 1196 |
|
| 1197 |
Set_Up_Parent_Communications (Descriptor, Pipe1, Pipe2, Pipe3); |
| 1198 |
end if; |
| 1199 |
|
| 1200 |
-- Create the buffer |
| 1201 |
|
| 1202 |
Descriptor.Buffer_Size := Buffer_Size; |
| 1203 |
|
| 1204 |
if Buffer_Size /= 0 then |
| 1205 |
Descriptor.Buffer := new String (1 .. Positive (Buffer_Size)); |
| 1206 |
end if; |
| 1207 |
|
| 1208 |
-- Initialize the filters |
| 1209 |
|
| 1210 |
Descriptor.Filters := null; |
| 1211 |
end Non_Blocking_Spawn; |
| 1212 |
|
| 1213 |
------------------------- |
| 1214 |
-- Reinitialize_Buffer -- |
| 1215 |
------------------------- |
| 1216 |
|
| 1217 |
procedure Reinitialize_Buffer |
| 1218 |
(Descriptor : in out Process_Descriptor'Class) |
| 1219 |
is |
| 1220 |
begin |
| 1221 |
if Descriptor.Buffer_Size = 0 then |
| 1222 |
declare |
| 1223 |
Tmp : String_Access := Descriptor.Buffer; |
| 1224 |
|
| 1225 |
begin |
| 1226 |
Descriptor.Buffer := |
| 1227 |
new String |
| 1228 |
(1 .. Descriptor.Buffer_Index - Descriptor.Last_Match_End); |
| 1229 |
|
| 1230 |
if Tmp /= null then |
| 1231 |
Descriptor.Buffer.all := Tmp |
| 1232 |
(Descriptor.Last_Match_End + 1 .. Descriptor.Buffer_Index); |
| 1233 |
Free (Tmp); |
| 1234 |
end if; |
| 1235 |
end; |
| 1236 |
|
| 1237 |
Descriptor.Buffer_Index := Descriptor.Buffer'Last; |
| 1238 |
|
| 1239 |
else |
| 1240 |
Descriptor.Buffer |
| 1241 |
(1 .. Descriptor.Buffer_Index - Descriptor.Last_Match_End) := |
| 1242 |
Descriptor.Buffer |
| 1243 |
(Descriptor.Last_Match_End + 1 .. Descriptor.Buffer_Index); |
| 1244 |
|
| 1245 |
if Descriptor.Buffer_Index > Descriptor.Last_Match_End then |
| 1246 |
Descriptor.Buffer_Index := |
| 1247 |
Descriptor.Buffer_Index - Descriptor.Last_Match_End; |
| 1248 |
else |
| 1249 |
Descriptor.Buffer_Index := 0; |
| 1250 |
end if; |
| 1251 |
end if; |
| 1252 |
|
| 1253 |
Descriptor.Last_Match_Start := 0; |
| 1254 |
Descriptor.Last_Match_End := 0; |
| 1255 |
end Reinitialize_Buffer; |
| 1256 |
|
| 1257 |
------------------- |
| 1258 |
-- Remove_Filter -- |
| 1259 |
------------------- |
| 1260 |
|
| 1261 |
procedure Remove_Filter |
| 1262 |
(Descriptor : in out Process_Descriptor; |
| 1263 |
Filter : Filter_Function) |
| 1264 |
is |
| 1265 |
Previous : Filter_List := null; |
| 1266 |
Current : Filter_List := Descriptor.Filters; |
| 1267 |
|
| 1268 |
begin |
| 1269 |
while Current /= null loop |
| 1270 |
if Current.Filter = Filter then |
| 1271 |
if Previous = null then |
| 1272 |
Descriptor.Filters := Current.Next; |
| 1273 |
else |
| 1274 |
Previous.Next := Current.Next; |
| 1275 |
end if; |
| 1276 |
end if; |
| 1277 |
|
| 1278 |
Previous := Current; |
| 1279 |
Current := Current.Next; |
| 1280 |
end loop; |
| 1281 |
end Remove_Filter; |
| 1282 |
|
| 1283 |
---------- |
| 1284 |
-- Send -- |
| 1285 |
---------- |
| 1286 |
|
| 1287 |
procedure Send |
| 1288 |
(Descriptor : in out Process_Descriptor; |
| 1289 |
Str : String; |
| 1290 |
Add_LF : Boolean := True; |
| 1291 |
Empty_Buffer : Boolean := False) |
| 1292 |
is |
| 1293 |
Line_Feed : aliased constant String := [1 .. 1 => ASCII.LF]; |
| 1294 |
Descriptors : Array_Of_Pd := [Descriptor'Unrestricted_Access]; |
| 1295 |
|
| 1296 |
Result : Expect_Match; |
| 1297 |
Discard : Natural; |
| 1298 |
pragma Warnings (Off, Result); |
| 1299 |
pragma Warnings (Off, Discard); |
| 1300 |
|
| 1301 |
begin |
| 1302 |
if Empty_Buffer then |
| 1303 |
|
| 1304 |
-- Force a read on the process if there is anything waiting |
| 1305 |
|
| 1306 |
Expect_Internal |
| 1307 |
(Descriptors, Result, Timeout => 0, Full_Buffer => False); |
| 1308 |
|
| 1309 |
if Result = Expect_Internal_Error |
| 1310 |
or else Result = Expect_Process_Died |
| 1311 |
then |
| 1312 |
raise Process_Died; |
| 1313 |
end if; |
| 1314 |
|
| 1315 |
Descriptor.Last_Match_End := Descriptor.Buffer_Index; |
| 1316 |
|
| 1317 |
-- Empty the buffer |
| 1318 |
|
| 1319 |
Reinitialize_Buffer (Descriptor); |
| 1320 |
end if; |
| 1321 |
|
| 1322 |
Call_Filters (Descriptor, Str, Input); |
| 1323 |
Discard := |
| 1324 |
Write (Descriptor.Input_Fd, Str'Address, Str'Last - Str'First + 1); |
| 1325 |
|
| 1326 |
if Add_LF then |
| 1327 |
Call_Filters (Descriptor, Line_Feed, Input); |
| 1328 |
Discard := |
| 1329 |
Write (Descriptor.Input_Fd, Line_Feed'Address, 1); |
| 1330 |
end if; |
| 1331 |
end Send; |
| 1332 |
|
| 1333 |
----------------- |
| 1334 |
-- Send_Signal -- |
| 1335 |
----------------- |
| 1336 |
|
| 1337 |
procedure Send_Signal |
| 1338 |
(Descriptor : Process_Descriptor; |
| 1339 |
Signal : Integer) |
| 1340 |
is |
| 1341 |
begin |
| 1342 |
-- A nonpositive process id passed to kill has special meanings. For |
| 1343 |
-- example, -1 means kill all processes in sight, including self, in |
| 1344 |
-- POSIX and Windows (and something slightly different in Linux). See |
| 1345 |
-- man pages for details. In any case, we don't want to do that. Note |
| 1346 |
-- that Descriptor.Pid will be -1 if the process was not successfully |
| 1347 |
-- started; we don't want to kill ourself in that case. |
| 1348 |
|
| 1349 |
if Descriptor.Pid > 0 then |
| 1350 |
Kill (Descriptor.Pid, Signal); |
| 1351 |
-- ??? Need to check process status here |
| 1352 |
else |
| 1353 |
raise Invalid_Process; |
| 1354 |
end if; |
| 1355 |
end Send_Signal; |
| 1356 |
|
| 1357 |
--------------------------------- |
| 1358 |
-- Set_Up_Child_Communications -- |
| 1359 |
--------------------------------- |
| 1360 |
|
| 1361 |
procedure Set_Up_Child_Communications |
| 1362 |
(Pid : in out Process_Descriptor; |
| 1363 |
Pipe1 : in out Pipe_Type; |
| 1364 |
Pipe2 : in out Pipe_Type; |
| 1365 |
Pipe3 : in out Pipe_Type; |
| 1366 |
Cmd : String; |
| 1367 |
Args : System.Address) |
| 1368 |
is |
| 1369 |
pragma Warnings (Off, Pid); |
| 1370 |
pragma Warnings (Off, Pipe1); |
| 1371 |
pragma Warnings (Off, Pipe2); |
| 1372 |
pragma Warnings (Off, Pipe3); |
| 1373 |
|
| 1374 |
Input : File_Descriptor; |
| 1375 |
Output : File_Descriptor; |
| 1376 |
Error : File_Descriptor; |
| 1377 |
|
| 1378 |
No_Fork_On_Target : constant Boolean := Target_OS = Windows; |
| 1379 |
|
| 1380 |
begin |
| 1381 |
if No_Fork_On_Target then |
| 1382 |
|
| 1383 |
-- Since Windows does not have a separate fork/exec, we need to |
| 1384 |
-- perform the following actions: |
| 1385 |
|
| 1386 |
-- - save stdin, stdout, stderr |
| 1387 |
-- - replace them by our pipes |
| 1388 |
-- - create the child with process handle inheritance |
| 1389 |
-- - revert to the previous stdin, stdout and stderr. |
| 1390 |
|
| 1391 |
Input := Dup (GNAT.OS_Lib.Standin); |
| 1392 |
Output := Dup (GNAT.OS_Lib.Standout); |
| 1393 |
Error := Dup (GNAT.OS_Lib.Standerr); |
| 1394 |
end if; |
| 1395 |
|
| 1396 |
-- Since we are still called from the parent process, there is no way |
| 1397 |
-- currently we can cleanly close the unneeded ends of the pipes, but |
| 1398 |
-- this doesn't really matter. |
| 1399 |
|
| 1400 |
-- We could close Pipe1.Output, Pipe2.Input, Pipe3.Input |
| 1401 |
|
| 1402 |
Dup2 (Pipe1.Input, GNAT.OS_Lib.Standin); |
| 1403 |
Dup2 (Pipe2.Output, GNAT.OS_Lib.Standout); |
| 1404 |
Dup2 (Pipe3.Output, GNAT.OS_Lib.Standerr); |
| 1405 |
|
| 1406 |
Portable_Execvp (Pid.Pid'Access, Cmd & ASCII.NUL, Args); |
| 1407 |
|
| 1408 |
-- The following lines are only required for Windows systems and will |
| 1409 |
-- not be executed on Unix systems, but we use the same condition as |
| 1410 |
-- above to avoid warnings on uninitialized variables on Unix systems. |
| 1411 |
-- We are now in the parent process. |
| 1412 |
|
| 1413 |
if No_Fork_On_Target then |
| 1414 |
|
| 1415 |
-- Restore the old descriptors |
| 1416 |
|
| 1417 |
Dup2 (Input, GNAT.OS_Lib.Standin); |
| 1418 |
Dup2 (Output, GNAT.OS_Lib.Standout); |
| 1419 |
Dup2 (Error, GNAT.OS_Lib.Standerr); |
| 1420 |
Close (Input); |
| 1421 |
Close (Output); |
| 1422 |
Close (Error); |
| 1423 |
end if; |
| 1424 |
end Set_Up_Child_Communications; |
| 1425 |
|
| 1426 |
--------------------------- |
| 1427 |
-- Set_Up_Communications -- |
| 1428 |
--------------------------- |
| 1429 |
|
| 1430 |
procedure Set_Up_Communications |
| 1431 |
(Pid : in out Process_Descriptor; |
| 1432 |
Err_To_Out : Boolean; |
| 1433 |
Pipe1 : not null access Pipe_Type; |
| 1434 |
Pipe2 : not null access Pipe_Type; |
| 1435 |
Pipe3 : not null access Pipe_Type) |
| 1436 |
is |
| 1437 |
Status : Boolean; |
| 1438 |
pragma Warnings (Off, "modified by call, but value overwritten"); |
| 1439 |
|
| 1440 |
begin |
| 1441 |
-- Create the pipes |
| 1442 |
|
| 1443 |
if Create_Pipe (Pipe1) /= 0 then |
| 1444 |
return; |
| 1445 |
end if; |
| 1446 |
|
| 1447 |
if Create_Pipe (Pipe2) /= 0 then |
| 1448 |
Close (Pipe1.Input); |
| 1449 |
Close (Pipe1.Output); |
| 1450 |
return; |
| 1451 |
end if; |
| 1452 |
|
| 1453 |
-- Record the 'parent' end of the two pipes in Pid: |
| 1454 |
-- Child stdin is connected to the 'write' end of Pipe1; |
| 1455 |
-- Child stdout is connected to the 'read' end of Pipe2. |
| 1456 |
-- We do not want these descriptors to remain open in the child |
| 1457 |
-- process, so we mark them close-on-exec/non-inheritable. |
| 1458 |
|
| 1459 |
Pid.Input_Fd := Pipe1.Output; |
| 1460 |
Set_Close_On_Exec (Pipe1.Output, True, Status); |
| 1461 |
Pid.Output_Fd := Pipe2.Input; |
| 1462 |
Set_Close_On_Exec (Pipe2.Input, True, Status); |
| 1463 |
|
| 1464 |
if Err_To_Out then |
| 1465 |
|
| 1466 |
-- Reuse the standard output pipe for standard error |
| 1467 |
|
| 1468 |
Pipe3.all := Pipe2.all; |
| 1469 |
|
| 1470 |
else |
| 1471 |
-- Create a separate pipe for standard error |
| 1472 |
|
| 1473 |
if Create_Pipe (Pipe3) /= 0 then |
| 1474 |
Pipe3.all := Pipe2.all; |
| 1475 |
end if; |
| 1476 |
end if; |
| 1477 |
|
| 1478 |
-- As above, record the proper fd for the child's standard error stream |
| 1479 |
|
| 1480 |
Pid.Error_Fd := Pipe3.Input; |
| 1481 |
Set_Close_On_Exec (Pipe3.Input, True, Status); |
| 1482 |
end Set_Up_Communications; |
| 1483 |
|
| 1484 |
---------------------------------- |
| 1485 |
-- Set_Up_Parent_Communications -- |
| 1486 |
---------------------------------- |
| 1487 |
|
| 1488 |
procedure Set_Up_Parent_Communications |
| 1489 |
(Pid : in out Process_Descriptor; |
| 1490 |
Pipe1 : in out Pipe_Type; |
| 1491 |
Pipe2 : in out Pipe_Type; |
| 1492 |
Pipe3 : in out Pipe_Type) |
| 1493 |
is |
| 1494 |
pragma Warnings (Off, Pid); |
| 1495 |
pragma Warnings (Off, Pipe1); |
| 1496 |
pragma Warnings (Off, Pipe2); |
| 1497 |
pragma Warnings (Off, Pipe3); |
| 1498 |
|
| 1499 |
begin |
| 1500 |
Close (Pipe1.Input); |
| 1501 |
Close (Pipe2.Output); |
| 1502 |
|
| 1503 |
if Pipe3.Output /= Pipe2.Output then |
| 1504 |
Close (Pipe3.Output); |
| 1505 |
end if; |
| 1506 |
end Set_Up_Parent_Communications; |
| 1507 |
|
| 1508 |
------------------ |
| 1509 |
-- Trace_Filter -- |
| 1510 |
------------------ |
| 1511 |
|
| 1512 |
procedure Trace_Filter |
| 1513 |
(Descriptor : Process_Descriptor'Class; |
| 1514 |
Str : String; |
| 1515 |
User_Data : System.Address := System.Null_Address) |
| 1516 |
is |
| 1517 |
pragma Warnings (Off, Descriptor); |
| 1518 |
pragma Warnings (Off, User_Data); |
| 1519 |
begin |
| 1520 |
GNAT.IO.Put (Str); |
| 1521 |
end Trace_Filter; |
| 1522 |
|
| 1523 |
-------------------- |
| 1524 |
-- Unlock_Filters -- |
| 1525 |
-------------------- |
| 1526 |
|
| 1527 |
procedure Unlock_Filters (Descriptor : in out Process_Descriptor) is |
| 1528 |
begin |
| 1529 |
if Descriptor.Filters_Lock > 0 then |
| 1530 |
Descriptor.Filters_Lock := Descriptor.Filters_Lock - 1; |
| 1531 |
end if; |
| 1532 |
end Unlock_Filters; |
| 1533 |
|
| 1534 |
end GNAT.Expect; |