1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- S Y S T E M . R E S P O N S E _ F I L E --
9 -- Copyright (C) 2007-2024, Free Software Foundation, Inc. --
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. --
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. --
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/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 with Ada
.Unchecked_Deallocation
;
34 with System
.OS_Lib
; use System
.OS_Lib
;
36 package body System
.Response_File
is
39 type File_Ptr
is access File_Rec
;
40 type File_Rec
is record
45 -- To build a stack of response file names
47 procedure Free
is new Ada
.Unchecked_Deallocation
(File_Rec
, File_Ptr
);
49 type Argument_List_Access
is access Argument_List
;
50 procedure Free
is new Ada
.Unchecked_Deallocation
51 (Argument_List
, Argument_List_Access
);
52 -- Free only the allocated Argument_List, not allocated String components
58 function Arguments_From
59 (Response_File_Name
: String;
60 Recursive
: Boolean := False;
61 Ignore_Non_Existing_Files
: Boolean := False)
64 First_File
: File_Ptr
:= null;
65 Last_File
: File_Ptr
:= null;
66 -- The stack of response files
68 Arguments
: Argument_List_Access
:= new Argument_List
(1 .. 4);
69 Last_Arg
: Natural := 0;
71 procedure Add_Argument
(Arg
: String);
72 -- Add argument Arg to argument list Arguments, increasing Arguments
75 procedure Recurse
(File_Name
: String);
76 -- Get the arguments from the file and call itself recursively if one of
77 -- the arguments starts with character '@'.
83 procedure Add_Argument
(Arg
: String) is
85 if Last_Arg
= Arguments
'Last then
87 New_Arguments
: constant Argument_List_Access
:=
88 new Argument_List
(1 .. Arguments
'Last * 2);
90 New_Arguments
(Arguments
'Range) := Arguments
.all;
91 Arguments
.all := (others => null);
93 Arguments
:= New_Arguments
;
97 Last_Arg
:= Last_Arg
+ 1;
98 Arguments
(Last_Arg
) := new String'(Arg);
105 procedure Recurse (File_Name : String) is
106 -- Open the response file. If not found, fail or report a warning,
107 -- depending on the value of Ignore_Non_Existing_Files.
109 FD : constant File_Descriptor := Open_Read (File_Name, Text);
111 Buffer_Size : constant := 1500;
112 Buffer : String (1 .. Buffer_Size);
114 Buffer_Length : Natural;
116 Buffer_Cursor : Natural;
118 End_Of_File_Reached : Boolean;
120 Line : String (1 .. Max_Line_Length + 1);
123 First_Char : Positive;
124 -- Index of the first character of an argument in Line
127 -- Index of the last character of an argument in Line
130 -- True when inside a quoted string
134 function End_Of_File return Boolean;
135 -- True when the end of the response file has been reached
137 procedure Get_Buffer;
138 -- Read one buffer from the response file
141 -- Get one line from the response file
147 function End_Of_File return Boolean is
149 return End_Of_File_Reached and then Buffer_Cursor > Buffer_Length;
156 procedure Get_Buffer is
158 Buffer_Length := Read (FD, Buffer (1)'Address, Buffer'Length);
159 End_Of_File_Reached := Buffer_Length < Buffer'Length;
167 procedure Get_Line is
178 Ch := Buffer (Buffer_Cursor);
180 exit when Ch = ASCII.CR or else
181 Ch = ASCII.LF or else
187 if Last = Line'Last then
191 Buffer_Cursor := Buffer_Cursor + 1;
193 if Buffer_Cursor > Buffer_Length then
203 Ch := Buffer (Buffer_Cursor);
205 exit when Ch /= ASCII.HT and then
206 Ch /= ASCII.LF and then
209 Buffer_Cursor := Buffer_Cursor + 1;
211 if Buffer_Cursor > Buffer_Length then
221 -- Start of processing for Recurse
226 if FD = Invalid_FD then
227 if Ignore_Non_Existing_Files then
230 raise File_Does_Not_Exist;
234 -- Put the response file name on the stack
236 if First_File = null then
239 (Name
=> new String'(File_Name),
242 Last_File := First_File;
246 Current : File_Ptr := First_File;
250 if Current.Name.all = File_Name then
251 raise Circularity_Detected;
254 Current := Current.Next;
255 exit when Current = null;
260 (Name
=> new String'(File_Name),
263 Last_File := Last_File.Next;
267 End_Of_File_Reached := False;
270 -- Read the response file line by line
273 while not End_Of_File loop
276 if Last = Line'Last then
282 -- Get each argument on the line
286 -- First, skip any white space
288 while First_Char <= Last loop
289 exit when Line (First_Char) /= ' ' and then
290 Line (First_Char) /= ASCII.HT;
291 First_Char := First_Char + 1;
294 exit Arg_Loop when First_Char > Last;
296 Last_Char := First_Char;
299 -- Get the character one by one
302 while Last_Char <= Last loop
304 -- Inside a string, check only for '"'
307 if Line (Last_Char) = '"' then
311 Line (Last_Char .. Last - 1) :=
312 Line (Last_Char + 1 .. Last);
315 -- End of string is end of argument
317 if Last_Char > Last or else
318 Line (Last_Char) = ' ' or else
319 Line (Last_Char) = ASCII.HT
323 Last_Char := Last_Char - 1;
327 -- If there are two consecutive '"', the quoted
328 -- string is not closed
330 In_String := Line (Last_Char) = '"';
333 Last_Char := Last_Char + 1;
338 Last_Char := Last_Char + 1;
341 elsif Last_Char = Last then
343 -- An opening '"' at the end of the line is an error
345 if Line (Last) = '"' then
346 raise No_Closing_Quote;
349 -- The argument ends with the line
354 elsif Line (Last_Char) = '"' then
356 -- Entering a quoted string: remove the '"'
359 Line (Last_Char .. Last - 1) :=
360 Line (Last_Char + 1 .. Last);
364 -- Outside quoted strings, white space ends the argument
367 when Line (Last_Char + 1) = ' ' or else
368 Line (Last_Char + 1) = ASCII.HT;
370 Last_Char := Last_Char + 1;
372 end loop Character_Loop;
374 -- It is an error to not close a quoted string before the end
378 raise No_Closing_Quote;
381 -- Add the argument to the list
384 Arg : String (1 .. Last_Char - First_Char + 1);
386 Arg := Line (First_Char .. Last_Char);
390 -- Next argument, if line is not finished
392 First_Char := Last_Char + 1;
398 -- If Recursive is True, check for any argument starting with '@'
402 while Arg <= Last_Arg loop
404 if Arguments (Arg)'Length > 0 and then
405 Arguments (Arg) (1) = '@'
407 -- Ignore argument '@' with no file name
409 if Arguments (Arg)'Length = 1 then
410 Arguments (Arg .. Last_Arg - 1) :=
411 Arguments (Arg + 1 .. Last_Arg);
412 Last_Arg := Last_Arg - 1;
415 -- Save the current arguments and get those in the new
419 Inc_File_Name : constant String :=
420 Arguments (Arg) (2 .. Arguments (Arg)'Last);
421 Current_Arguments : constant Argument_List :=
422 Arguments (1 .. Last_Arg);
424 Recurse (Inc_File_Name);
426 -- Insert the new arguments where the new response
427 -- file was imported.
430 New_Arguments : constant Argument_List :=
431 Arguments (1 .. Last_Arg);
432 New_Last_Arg : constant Positive :=
433 Current_Arguments'Length +
434 New_Arguments'Length - 1;
437 -- Grow Arguments if it is not large enough
439 if Arguments'Last < New_Last_Arg then
440 Last_Arg := Arguments'Last;
443 while Last_Arg < New_Last_Arg loop
444 Last_Arg := Last_Arg * 2;
447 Arguments := new Argument_List (1 .. Last_Arg);
450 Last_Arg := New_Last_Arg;
452 Arguments (1 .. Last_Arg) :=
453 Current_Arguments (1 .. Arg - 1) &
456 (Arg + 1 .. Current_Arguments'Last);
458 Arg := Arg + New_Arguments'Length;
469 -- Remove the response file name from the stack
471 if First_File = Last_File then
472 System.Strings.Free (First_File.Name);
478 System.Strings.Free (Last_File.Name);
479 Last_File := Last_File.Prev;
480 Free (Last_File.Next);
490 -- Start of processing for Arguments_From
493 -- The job is done by procedure Recurse
495 Recurse (Response_File_Name);
497 -- Free Arguments before returning the result
500 Result : constant Argument_List := Arguments (1 .. Last_Arg);
509 -- When an exception occurs, deallocate everything
513 while First_File /= null loop
514 Last_File := First_File.Next;
515 System.Strings.Free (First_File.Name);
517 First_File := Last_File;
523 end System.Response_File;