1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-2007, 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 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. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Ada.Unchecked_Deallocation;
29 with System.Case_Util; use System.Case_Util;
31 with Osint; use Osint;
32 with Output; use Output;
34 with Snames; use Snames;
35 with Targparm; use Targparm;
37 package body Prj.Util is
39 procedure Free is new Ada.Unchecked_Deallocation
40 (Text_File_Data, Text_File);
46 procedure Close (File : in out Text_File) is
49 Prj.Com.Fail ("Close attempted on an invalid Text_File");
52 -- Close file, no need to test status, since this is a file that we
53 -- read, and the file was read successfully before we closed it.
63 function End_Of_File (File : Text_File) return Boolean is
66 Prj.Com.Fail ("End_Of_File attempted on an invalid Text_File");
69 return File.End_Of_File_Reached;
76 function Executable_Of
77 (Project : Project_Id;
78 In_Tree : Project_Tree_Ref;
79 Main : File_Name_Type;
81 Ada_Main : Boolean := True) return File_Name_Type
83 pragma Assert (Project /= No_Project);
85 The_Packages : constant Package_Id :=
86 In_Tree.Projects.Table (Project).Decl.Packages;
88 Builder_Package : constant Prj.Package_Id :=
90 (Name => Name_Builder,
91 In_Packages => The_Packages,
94 Executable : Variable_Value :=
96 (Name => Name_Id (Main),
98 Attribute_Or_Array_Name => Name_Executable,
99 In_Package => Builder_Package,
102 Executable_Suffix : Variable_Value := Nil_Variable_Value;
104 Body_Append : constant String := Get_Name_String
105 (In_Tree.Projects.Table
107 Naming.Ada_Body_Suffix);
109 Spec_Append : constant String := Get_Name_String
110 (In_Tree.Projects.Table
112 Naming.Ada_Spec_Suffix);
115 if Builder_Package /= No_Package then
116 Executable_Suffix := Prj.Util.Value_Of
117 (Variable_Name => Name_Executable_Suffix,
118 In_Variables => In_Tree.Packages.Table
119 (Builder_Package).Decl.Attributes,
122 if Executable = Nil_Variable_Value and Ada_Main then
123 Get_Name_String (Main);
125 -- Try as index the name minus the implementation suffix or minus
126 -- the specification suffix.
129 Name : constant String (1 .. Name_Len) :=
130 Name_Buffer (1 .. Name_Len);
131 Last : Positive := Name_Len;
133 Naming : constant Naming_Data :=
134 In_Tree.Projects.Table (Project).Naming;
136 Spec_Suffix : constant String :=
137 Get_Name_String (Naming.Ada_Spec_Suffix);
138 Body_Suffix : constant String :=
139 Get_Name_String (Naming.Ada_Body_Suffix);
141 Truncated : Boolean := False;
144 if Last > Body_Suffix'Length
145 and then Name (Last - Body_Suffix'Length + 1 .. Last) =
149 Last := Last - Body_Suffix'Length;
153 and then Last > Spec_Suffix'Length
154 and then Name (Last - Spec_Suffix'Length + 1 .. Last) =
158 Last := Last - Spec_Suffix'Length;
163 Name_Buffer (1 .. Name_Len) := Name (1 .. Last);
168 Attribute_Or_Array_Name => Name_Executable,
169 In_Package => Builder_Package,
175 -- If we have found an Executable attribute, return its value,
176 -- possibly suffixed by the executable suffix.
178 if Executable /= Nil_Variable_Value
179 and then Executable.Value /= Empty_Name
181 -- Get the executable name. If Executable_Suffix is defined,
182 -- make sure that it will be the extension of the executable.
185 Saved_EEOT : constant Name_Id := Executable_Extension_On_Target;
186 Result : File_Name_Type;
189 if Executable_Suffix /= Nil_Variable_Value
190 and then not Executable_Suffix.Default
192 Executable_Extension_On_Target := Executable_Suffix.Value;
195 Result := Executable_Name (File_Name_Type (Executable.Value));
196 Executable_Extension_On_Target := Saved_EEOT;
202 Get_Name_String (Main);
204 -- If there is a body suffix or a spec suffix, remove this suffix,
205 -- otherwise remove any suffix ('.' followed by other characters), if
208 if Ada_Main and then Name_Len > Body_Append'Length
209 and then Name_Buffer (Name_Len - Body_Append'Length + 1 .. Name_Len) =
212 -- Found the body termination, remove it
214 Name_Len := Name_Len - Body_Append'Length;
216 elsif Ada_Main and then Name_Len > Spec_Append'Length
217 and then Name_Buffer (Name_Len - Spec_Append'Length + 1 .. Name_Len) =
220 -- Found the spec termination, remove it
222 Name_Len := Name_Len - Spec_Append'Length;
225 -- Remove any suffix, if there is one
227 Get_Name_String (Strip_Suffix (Main));
230 if Executable_Suffix /= Nil_Variable_Value
231 and then not Executable_Suffix.Default
233 -- If attribute Executable_Suffix is specified, add this suffix
236 Suffix : constant String :=
237 Get_Name_String (Executable_Suffix.Value);
239 Name_Buffer (Name_Len + 1 .. Name_Len + Suffix'Length) := Suffix;
240 Name_Len := Name_Len + Suffix'Length;
245 -- Otherwise, add the standard suffix for the platform, if any
247 return Executable_Name (Name_Find);
270 if File.Cursor = File.Buffer_Len then
274 A => File.Buffer'Address,
275 N => File.Buffer'Length);
277 if File.Buffer_Len = 0 then
278 File.End_Of_File_Reached := True;
285 File.Cursor := File.Cursor + 1;
289 -- Start of processing for Get_Line
293 Prj.Com.Fail ("Get_Line attempted on an invalid Text_File");
296 Last := Line'First - 1;
298 if not File.End_Of_File_Reached then
300 C := File.Buffer (File.Cursor);
301 exit when C = ASCII.CR or else C = ASCII.LF;
306 if File.End_Of_File_Reached then
310 exit when Last = Line'Last;
313 if C = ASCII.CR or else C = ASCII.LF then
316 if File.End_Of_File_Reached then
322 and then File.Buffer (File.Cursor) = ASCII.LF
333 function Is_Valid (File : Text_File) return Boolean is
342 procedure Open (File : out Text_File; Name : String) is
343 FD : File_Descriptor;
344 File_Name : String (1 .. Name'Length + 1);
347 File_Name (1 .. Name'Length) := Name;
348 File_Name (File_Name'Last) := ASCII.NUL;
349 FD := Open_Read (Name => File_Name'Address,
350 Fmode => GNAT.OS_Lib.Text);
351 if FD = Invalid_FD then
354 File := new Text_File_Data;
358 A => File.Buffer'Address,
359 N => File.Buffer'Length);
361 if File.Buffer_Len = 0 then
362 File.End_Of_File_Reached := True;
374 (Variable : Variable_Value;
375 Default : String) return String
378 if Variable.Kind /= Single
379 or else Variable.Default
380 or else Variable.Value = No_Name
384 return Get_Name_String (Variable.Value);
390 In_Array : Array_Element_Id;
391 In_Tree : Project_Tree_Ref) return Name_Id
393 Current : Array_Element_Id := In_Array;
394 Element : Array_Element;
395 Real_Index : Name_Id := Index;
398 if Current = No_Array_Element then
402 Element := In_Tree.Array_Elements.Table (Current);
404 if not Element.Index_Case_Sensitive then
405 Get_Name_String (Index);
406 To_Lower (Name_Buffer (1 .. Name_Len));
407 Real_Index := Name_Find;
410 while Current /= No_Array_Element loop
411 Element := In_Tree.Array_Elements.Table (Current);
413 if Real_Index = Element.Index then
414 exit when Element.Value.Kind /= Single;
415 exit when Element.Value.Value = Empty_String;
416 return Element.Value.Value;
418 Current := Element.Next;
427 Src_Index : Int := 0;
428 In_Array : Array_Element_Id;
429 In_Tree : Project_Tree_Ref) return Variable_Value
431 Current : Array_Element_Id := In_Array;
432 Element : Array_Element;
433 Real_Index : Name_Id := Index;
436 if Current = No_Array_Element then
437 return Nil_Variable_Value;
440 Element := In_Tree.Array_Elements.Table (Current);
442 if not Element.Index_Case_Sensitive then
443 Get_Name_String (Index);
444 To_Lower (Name_Buffer (1 .. Name_Len));
445 Real_Index := Name_Find;
448 while Current /= No_Array_Element loop
449 Element := In_Tree.Array_Elements.Table (Current);
451 if Real_Index = Element.Index and then
452 Src_Index = Element.Src_Index
454 return Element.Value;
456 Current := Element.Next;
460 return Nil_Variable_Value;
466 Attribute_Or_Array_Name : Name_Id;
467 In_Package : Package_Id;
468 In_Tree : Project_Tree_Ref) return Variable_Value
470 The_Array : Array_Element_Id;
471 The_Attribute : Variable_Value := Nil_Variable_Value;
474 if In_Package /= No_Package then
476 -- First, look if there is an array element that fits
480 (Name => Attribute_Or_Array_Name,
481 In_Arrays => In_Tree.Packages.Table (In_Package).Decl.Arrays,
487 In_Array => The_Array,
490 -- If there is no array element, look for a variable
492 if The_Attribute = Nil_Variable_Value then
495 (Variable_Name => Attribute_Or_Array_Name,
496 In_Variables => In_Tree.Packages.Table
497 (In_Package).Decl.Attributes,
502 return The_Attribute;
508 In_Arrays : Array_Id;
509 In_Tree : Project_Tree_Ref) return Name_Id
511 Current : Array_Id := In_Arrays;
512 The_Array : Array_Data;
515 while Current /= No_Array loop
516 The_Array := In_Tree.Arrays.Table (Current);
517 if The_Array.Name = In_Array then
519 (Index, In_Array => The_Array.Value, In_Tree => In_Tree);
521 Current := The_Array.Next;
530 In_Arrays : Array_Id;
531 In_Tree : Project_Tree_Ref) return Array_Element_Id
533 Current : Array_Id := In_Arrays;
534 The_Array : Array_Data;
537 while Current /= No_Array loop
538 The_Array := In_Tree.Arrays.Table (Current);
540 if The_Array.Name = Name then
541 return The_Array.Value;
543 Current := The_Array.Next;
547 return No_Array_Element;
552 In_Packages : Package_Id;
553 In_Tree : Project_Tree_Ref) return Package_Id
555 Current : Package_Id := In_Packages;
556 The_Package : Package_Element;
559 while Current /= No_Package loop
560 The_Package := In_Tree.Packages.Table (Current);
561 exit when The_Package.Name /= No_Name
562 and then The_Package.Name = Name;
563 Current := The_Package.Next;
570 (Variable_Name : Name_Id;
571 In_Variables : Variable_Id;
572 In_Tree : Project_Tree_Ref) return Variable_Value
574 Current : Variable_Id := In_Variables;
575 The_Variable : Variable;
578 while Current /= No_Variable loop
580 In_Tree.Variable_Elements.Table (Current);
582 if Variable_Name = The_Variable.Name then
583 return The_Variable.Value;
585 Current := The_Variable.Next;
589 return Nil_Variable_Value;
598 Max_Length : Positive;
599 Separator : Character)
601 First : Positive := S'First;
602 Last : Natural := S'Last;
605 -- Nothing to do for empty strings
609 -- Start on a new line if current line is already longer than
612 if Positive (Column) >= Max_Length then
616 -- If length of remainder is longer than Max_Length, we need to
617 -- cut the remainder in several lines.
619 while Positive (Column) + S'Last - First > Max_Length loop
621 -- Try the maximum length possible
623 Last := First + Max_Length - Positive (Column);
625 -- Look for last Separator in the line
627 while Last >= First and then S (Last) /= Separator loop
631 -- If we do not find a separator, we output the maximum length
635 Last := First + Max_Length - Positive (Column);
638 Write_Line (S (First .. Last));
640 -- Set the beginning of the new remainder
645 -- What is left goes to the buffer, without EOL
647 Write_Str (S (First .. S'Last));