1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1998-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.Characters.Conversions; use Ada.Characters.Conversions;
28 with Ada.Command_Line; use Ada.Command_Line;
29 with Ada.Directories; use Ada.Directories;
30 with Ada.Streams.Stream_IO; use Ada.Streams;
31 with Ada.Text_IO; use Ada.Text_IO;
32 with System.CRTL; use System; use System.CRTL;
34 with GNAT.Command_Line; use GNAT.Command_Line;
35 with GNAT.OS_Lib; use GNAT.OS_Lib;
36 with GNAT.Heap_Sort_G;
40 with Switch; use Switch;
45 Config_File_Name : constant String_Access := new String'("gnat.adc");
46 -- The name of the file holding the GNAT configuration pragmas
48 Gcc : String_Access := new String'("gcc");
49 -- May be modified by switch --GCC=
51 Gcc_Set : Boolean := False;
52 -- True if a switch --GCC= is used
54 Gnat_Cmd : String_Access;
55 -- Command to execute the GNAT compiler
57 Gnat_Args : Argument_List_Access :=
62 new String'("-gnats"),
63 new String'("-gnatu"));
64 -- Arguments used in Gnat_Cmd call
66 EOF : constant Character := Character'Val (26);
67 -- Special character to signal end of file. Not required in input
68 -- files, but properly treated if present. Not generated in output
69 -- files except as a result of copying input file.
75 subtype File_Num is Natural;
76 subtype File_Offset is Natural;
78 type File_Entry is record
80 -- Name of chop file or directory
82 SR_Name : String_Access;
83 -- Null unless the chop file starts with a source reference pragma
84 -- in which case this field points to the file name from this pragma.
87 package File is new GNAT.Table
88 (Table_Component_Type => File_Entry,
89 Table_Index_Type => File_Num,
92 Table_Increment => 100);
94 Directory : String_Access;
95 -- Record name of directory, or a null string if no directory given
97 Compilation_Mode : Boolean := False;
98 Overwrite_Files : Boolean := False;
99 Preserve_Mode : Boolean := False;
100 Quiet_Mode : Boolean := False;
101 Source_References : Boolean := False;
102 Verbose_Mode : Boolean := False;
103 Exit_On_Error : Boolean := False;
106 Write_gnat_adc : Boolean := False;
107 -- Gets set true if we append to gnat.adc or create a new gnat.adc.
108 -- Used to inhibit complaint about no units generated.
114 type Line_Num is new Natural;
115 -- Line number (for source reference pragmas)
117 type Unit_Count_Type is new Integer;
118 subtype Unit_Num is Unit_Count_Type range 1 .. Unit_Count_Type'Last;
119 -- Used to refer to unit number in unit table
121 type SUnit_Num is new Integer;
122 -- Used to refer to entry in sorted units table. Note that entry
123 -- zero is only for use by Heapsort, and is not otherwise referenced.
125 type Unit_Kind is (Unit_Spec, Unit_Body, Config_Pragmas);
127 -- Structure to contain all necessary information for one unit.
128 -- Entries are also temporarily used to record config pragma sequences.
130 type Unit_Info is record
131 File_Name : String_Access;
132 -- File name from GNAT output line
134 Chop_File : File_Num;
135 -- File number in chop file sequence
137 Start_Line : Line_Num;
138 -- Line number from GNAT output line
140 Offset : File_Offset;
141 -- Offset name from GNAT output line
143 SR_Present : Boolean;
144 -- Set True if SR parameter present
146 Length : File_Offset;
147 -- A length of 0 means that the Unit is the last one in the file
150 -- Indicates kind of unit
152 Sorted_Index : SUnit_Num;
153 -- Index of unit in sorted unit list
155 Bufferg : String_Access;
156 -- Pointer to buffer containing configuration pragmas to be
157 -- prepended. Null if no pragmas to be prepended.
160 -- The following table stores the unit offset information
162 package Unit is new GNAT.Table
163 (Table_Component_Type => Unit_Info,
164 Table_Index_Type => Unit_Count_Type,
165 Table_Low_Bound => 1,
166 Table_Initial => 500,
167 Table_Increment => 100);
169 -- The following table is used as a sorted index to the Unit.Table.
170 -- The entries in Unit.Table are not moved, instead we just shuffle
171 -- the entries in Sorted_Units. Note that the zeroeth entry in this
172 -- table is used by GNAT.Heap_Sort_G.
174 package Sorted_Units is new GNAT.Table
175 (Table_Component_Type => Unit_Num,
176 Table_Index_Type => SUnit_Num,
177 Table_Low_Bound => 0,
178 Table_Initial => 500,
179 Table_Increment => 100);
181 function Is_Duplicated (U : SUnit_Num) return Boolean;
182 -- Returns true if U is duplicated by a later unit.
183 -- Note that this function returns false for the last entry.
185 procedure Sort_Units;
186 -- Sort units and set up sorted unit table
188 ----------------------
189 -- File_Descriptors --
190 ----------------------
192 function dup (handle : File_Descriptor) return File_Descriptor;
193 function dup2 (from, to : File_Descriptor) return File_Descriptor;
195 ---------------------
196 -- Local variables --
197 ---------------------
199 Warning_Count : Natural := 0;
200 -- Count of warnings issued so far
202 -----------------------
203 -- Local subprograms --
204 -----------------------
206 procedure Error_Msg (Message : String; Warning : Boolean := False);
207 -- Produce an error message on standard error output
209 procedure File_Time_Stamp (Name : C_File_Name; Time : OS_Time);
210 -- Given the name of a file or directory, Name, set the
211 -- time stamp. This function must be used for an unopened file.
213 function Files_Exist return Boolean;
214 -- Check Unit.Table for possible file names that already exist
215 -- in the file system. Returns true if files exist, False otherwise
217 function Get_Maximum_File_Name_Length return Integer;
218 pragma Import (C, Get_Maximum_File_Name_Length,
219 "__gnat_get_maximum_file_name_length");
220 -- Function to get maximum file name length for system
222 Maximum_File_Name_Length : constant Integer := Get_Maximum_File_Name_Length;
223 Maximum_File_Name_Length_String : constant String :=
225 (Maximum_File_Name_Length);
227 function Locate_Executable
228 (Program_Name : String;
229 Look_For_Prefix : Boolean := True) return String_Access;
230 -- Locate executable for given program name. This takes into account
231 -- the target-prefix of the current command, if Look_For_Prefix is True.
233 subtype EOL_Length is Natural range 0 .. 2;
234 -- Possible lengths of end of line sequence
236 type EOL_String (Len : EOL_Length := 0) is record
237 Str : String (1 .. Len);
241 (Source : not null access String;
242 Start : Positive) return EOL_String;
243 -- Return the line terminator used in the passed string
246 (Source : not null access String;
247 Ptr : in out Positive);
248 -- On return Source (Ptr) is the first character of the next line
249 -- or EOF. Source.all must be terminated by EOF.
251 function Parse_File (Num : File_Num) return Boolean;
252 -- Calls the GNAT compiler to parse the given source file and parses the
253 -- output using Parse_Offset_Info. Returns True if parse operation
254 -- completes, False if some system error (e.g. failure to read the
255 -- offset information) occurs.
257 procedure Parse_Offset_Info
258 (Chop_File : File_Num;
259 Source : not null access String);
260 -- Parses the output of the compiler indicating the offsets
261 -- and names of the compilation units in Chop_File.
263 procedure Parse_Token
264 (Source : not null access String;
265 Ptr : in out Positive;
266 Token_Ptr : out Positive);
267 -- Skips any separators and stores the start of the token in Token_Ptr.
268 -- Then stores the position of the next separator in Ptr.
269 -- On return Source (Token_Ptr .. Ptr - 1) is the token.
272 (FD : File_Descriptor;
273 Contents : out String_Access;
274 Success : out Boolean);
275 -- Reads file associated with FS into the newly allocated
277 -- [VMS] Success is true iff the number of bytes read is less than or
278 -- equal to the file size.
279 -- [Other] Success is true iff the number of bytes read is equal to
282 function Report_Duplicate_Units return Boolean;
283 -- Output messages about duplicate units in the input files in Unit.Table
284 -- Returns True if any duplicates found, Fals if no duplicates found.
286 function Scan_Arguments return Boolean;
287 -- Scan command line options and set global variables accordingly.
288 -- Also scan out file and directory arguments. Returns True if scan
289 -- was successful, and False if the scan fails for any reason.
292 -- Output message on standard output describing syntax of gnatchop command
294 procedure Warning_Msg (Message : String);
295 -- Output a warning message on standard error and update warning count
297 function Write_Chopped_Files (Input : File_Num) return Boolean;
298 -- Write all units that result from chopping the Input file
300 procedure Write_Config_File (Input : File_Num; U : Unit_Num);
301 -- Call to write configuration pragmas (append them to gnat.adc)
302 -- Input is the file number for the chop file and U identifies the
303 -- unit entry for the configuration pragmas.
305 function Get_Config_Pragmas
307 U : Unit_Num) return String_Access;
308 -- Call to read configuration pragmas from given unit entry, and
309 -- return a buffer containing the pragmas to be appended to
310 -- following units. Input is the file number for the chop file and
311 -- U identifies the unit entry for the configuration pragmas.
313 procedure Write_Source_Reference_Pragma
316 File : Stream_IO.File_Type;
318 Success : in out Boolean);
319 -- If Success is True on entry, writes a source reference pragma using
320 -- the chop file from Info, and the given line number. On return Success
321 -- indicates whether the write succeeded. If Success is False on entry,
322 -- or if the global flag Source_References is False, then the call to
323 -- Write_Source_Reference_Pragma has no effect. EOL indicates the end
324 -- of line sequence to be written at the end of the pragma.
327 (Source : not null access String;
330 Success : out Boolean);
331 -- Write one compilation unit of the source to file
337 function dup (handle : File_Descriptor) return File_Descriptor is
339 return File_Descriptor (System.CRTL.dup (int (handle)));
346 function dup2 (from, to : File_Descriptor) return File_Descriptor is
348 return File_Descriptor (System.CRTL.dup2 (int (from), int (to)));
355 procedure Error_Msg (Message : String; Warning : Boolean := False) is
357 Put_Line (Standard_Error, Message);
360 Set_Exit_Status (Failure);
362 if Exit_On_Error then
363 raise Types.Terminate_Program;
368 ---------------------
369 -- File_Time_Stamp --
370 ---------------------
372 procedure File_Time_Stamp (Name : C_File_Name; Time : OS_Time) is
373 procedure Set_File_Time (Name : C_File_Name; Time : OS_Time);
374 pragma Import (C, Set_File_Time, "__gnat_set_file_time_name");
377 Set_File_Time (Name, Time);
384 function Files_Exist return Boolean is
385 Exists : Boolean := False;
388 for SNum in 1 .. SUnit_Num (Unit.Last) loop
390 -- Only check and report for the last instance of duplicated files
392 if not Is_Duplicated (SNum) then
394 Info : constant Unit_Info :=
395 Unit.Table (Sorted_Units.Table (SNum));
398 if Is_Writable_File (Info.File_Name.all) then
399 if Hostparm.OpenVMS then
402 & " already exists, use /OVERWRITE to overwrite");
404 Error_Msg (Info.File_Name.all
405 & " already exists, use -w to overwrite");
417 ------------------------
418 -- Get_Config_Pragmas --
419 ------------------------
421 function Get_Config_Pragmas
426 Info : Unit_Info renames Unit.Table (U);
427 FD : File_Descriptor;
428 Name : aliased constant String :=
429 File.Table (Input).Name.all & ASCII.Nul;
430 Length : File_Offset;
431 Buffer : String_Access;
433 Result : String_Access;
436 FD := Open_Read (Name'Address, Binary);
438 if FD = Invalid_FD then
439 Error_Msg ("cannot open " & File.Table (Input).Name.all);
443 Read_File (FD, Buffer, Success);
445 -- A length of 0 indicates that the rest of the file belongs to
446 -- this unit. The actual length must be calculated now. Take into
447 -- account that the last character (EOF) must not be written.
449 if Info.Length = 0 then
450 Length := Buffer'Last - (Buffer'First + Info.Offset);
452 Length := Info.Length;
455 Result := new String'(Buffer (1 .. Length));
458 end Get_Config_Pragmas;
465 (Source : not null access String;
469 Ptr : Positive := Start;
474 -- Skip to end of line
476 while Source (Ptr) /= ASCII.CR and then
477 Source (Ptr) /= ASCII.LF and then
485 if Source (Ptr) /= EOF then
495 -- Recognize CR/LF or LF/CR combination
497 if (Source (Ptr + 1) = ASCII.CR or Source (Ptr + 1) = ASCII.LF)
498 and then Source (Ptr) /= Source (Ptr + 1)
503 return (Len => Last + 1 - First, Str => Source (First .. Last));
510 function Is_Duplicated (U : SUnit_Num) return Boolean is
512 return U < SUnit_Num (Unit.Last)
514 Unit.Table (Sorted_Units.Table (U)).File_Name.all =
515 Unit.Table (Sorted_Units.Table (U + 1)).File_Name.all;
518 -----------------------
519 -- Locate_Executable --
520 -----------------------
522 function Locate_Executable
523 (Program_Name : String;
524 Look_For_Prefix : Boolean := True) return String_Access
526 Current_Command : constant String := Normalize_Pathname (Command_Name);
527 End_Of_Prefix : Natural;
528 Start_Of_Prefix : Positive;
529 Result : String_Access;
532 Start_Of_Prefix := Current_Command'First;
533 End_Of_Prefix := Start_Of_Prefix - 1;
535 if Look_For_Prefix then
537 -- Find Start_Of_Prefix
539 for J in reverse Current_Command'Range loop
540 if Current_Command (J) = '/' or
541 Current_Command (J) = Directory_Separator or
542 Current_Command (J) = ':'
544 Start_Of_Prefix := J + 1;
549 -- Find End_Of_Prefix
551 for J in reverse Start_Of_Prefix .. Current_Command'Last loop
552 if Current_Command (J) = '-' then
560 Command : constant String :=
561 Current_Command (Start_Of_Prefix .. End_Of_Prefix) &
564 Result := Locate_Exec_On_Path (Command);
566 if Result = null then
568 (Command & ": installation problem, executable not found");
573 end Locate_Executable;
580 (Source : not null access String;
581 Ptr : in out Positive) is
583 -- Skip to end of line
585 while Source (Ptr) /= ASCII.CR and then Source (Ptr) /= ASCII.LF
586 and then Source (Ptr) /= EOF
591 if Source (Ptr) /= EOF then
592 Ptr := Ptr + 1; -- skip CR or LF
595 -- Skip past CR/LF or LF/CR combination
597 if (Source (Ptr) = ASCII.CR or Source (Ptr) = ASCII.LF)
598 and then Source (Ptr) /= Source (Ptr - 1)
608 function Parse_File (Num : File_Num) return Boolean is
609 Chop_Name : constant String_Access := File.Table (Num).Name;
610 Save_Stdout : constant File_Descriptor := dup (Standout);
611 Offset_Name : Temp_File_Name;
612 Offset_FD : File_Descriptor;
613 Buffer : String_Access;
618 -- Display copy of GNAT command if verbose mode
623 for J in 1 .. Gnat_Args'Length loop
625 Put (Gnat_Args (J).all);
629 Put_Line (Chop_Name.all);
632 -- Create temporary file
634 Create_Temp_File (Offset_FD, Offset_Name);
636 if Offset_FD = Invalid_FD then
637 Error_Msg ("gnatchop: cannot create temporary file");
642 -- Redirect Stdout to this temporary file in the Unix way
644 if dup2 (Offset_FD, Standout) = Invalid_FD then
645 Error_Msg ("gnatchop: cannot redirect stdout to temporary file");
651 -- Call Gnat on the source filename argument with special options
652 -- to generate offset information. If this special compilation completes
653 -- successfully then we can do the actual gnatchop operation.
655 Spawn (Gnat_Cmd.all, Gnat_Args.all & Chop_Name, Success);
658 Error_Msg (Chop_Name.all & ": parse errors detected");
659 Error_Msg (Chop_Name.all & ": chop may not be successful");
664 if dup2 (Save_Stdout, Standout) = Invalid_FD then
665 Error_Msg ("gnatchop: cannot restore stdout");
668 -- Reopen the file to start reading from the beginning
672 Offset_FD := Open_Read (Offset_Name'Address, Binary);
674 if Offset_FD = Invalid_FD then
675 Error_Msg ("gnatchop: cannot access offset info");
679 Read_File (Offset_FD, Buffer, Success);
682 Error_Msg ("gnatchop: error reading offset info");
686 Parse_Offset_Info (Num, Buffer);
689 -- Close and delete temporary file
692 Delete_File (Offset_Name'Address, Success);
697 when Failure | Types.Terminate_Program =>
699 Delete_File (Offset_Name'Address, Success);
704 -----------------------
705 -- Parse_Offset_Info --
706 -----------------------
708 procedure Parse_Offset_Info
709 (Chop_File : File_Num;
710 Source : not null access String)
712 First_Unit : constant Unit_Num := Unit.Last + 1;
713 Bufferg : String_Access := null;
714 Parse_Ptr : File_Offset := Source'First;
715 Token_Ptr : File_Offset;
718 function Match (Literal : String) return Boolean;
719 -- Checks if given string appears at the current Token_Ptr location
720 -- and if so, bumps Parse_Ptr past the token and returns True. If
721 -- the string is not present, sets Parse_Ptr to Token_Ptr and
728 function Match (Literal : String) return Boolean is
730 Parse_Token (Source, Parse_Ptr, Token_Ptr);
732 if Source'Last + 1 - Token_Ptr < Literal'Length
734 Source (Token_Ptr .. Token_Ptr + Literal'Length - 1) /= Literal
736 Parse_Ptr := Token_Ptr;
740 Parse_Ptr := Token_Ptr + Literal'Length;
744 -- Start of processing for Parse_Offset_Info
748 -- Set default values, should get changed for all
749 -- units/pragmas except for the last
751 Info.Chop_File := Chop_File;
754 -- Parse the current line of offset information into Info
755 -- and exit the loop if there are any errors or on EOF.
757 -- First case, parse a line in the following format:
759 -- Unit x (spec) line 7, file offset 142, [SR, ]file name x.ads
761 -- Note that the unit name can be an operator name in quotes.
762 -- This is of course illegal, but both GNAT and gnatchop handle
763 -- the case so that this error does not intefere with chopping.
765 -- The SR ir present indicates that a source reference pragma
766 -- was processed as part of this unit (and that therefore no
767 -- Source_Reference pragma should be generated.
769 if Match ("Unit") then
770 Parse_Token (Source, Parse_Ptr, Token_Ptr);
772 if Match ("(body)") then
773 Info.Kind := Unit_Body;
774 elsif Match ("(spec)") then
775 Info.Kind := Unit_Spec;
780 exit when not Match ("line");
781 Parse_Token (Source, Parse_Ptr, Token_Ptr);
782 Info.Start_Line := Line_Num'Value
783 (Source (Token_Ptr .. Parse_Ptr - 1));
785 exit when not Match ("file offset");
786 Parse_Token (Source, Parse_Ptr, Token_Ptr);
787 Info.Offset := File_Offset'Value
788 (Source (Token_Ptr .. Parse_Ptr - 1));
790 Info.SR_Present := Match ("SR, ");
792 exit when not Match ("file name");
793 Parse_Token (Source, Parse_Ptr, Token_Ptr);
794 Info.File_Name := new String'
795 (Directory.all & Source (Token_Ptr .. Parse_Ptr - 1));
796 Parse_EOL (Source, Parse_Ptr);
798 -- Second case, parse a line of the following form
800 -- Configuration pragmas at line 10, file offset 223
802 elsif Match ("Configuration pragmas at") then
803 Info.Kind := Config_Pragmas;
804 Info.File_Name := Config_File_Name;
806 exit when not Match ("line");
807 Parse_Token (Source, Parse_Ptr, Token_Ptr);
808 Info.Start_Line := Line_Num'Value
809 (Source (Token_Ptr .. Parse_Ptr - 1));
811 exit when not Match ("file offset");
812 Parse_Token (Source, Parse_Ptr, Token_Ptr);
813 Info.Offset := File_Offset'Value
814 (Source (Token_Ptr .. Parse_Ptr - 1));
816 Parse_EOL (Source, Parse_Ptr);
818 -- Third case, parse a line of the following form
820 -- Source_Reference pragma for file "filename"
822 -- This appears at the start of the file only, and indicates
823 -- the name to be used on any generated Source_Reference pragmas.
825 elsif Match ("Source_Reference pragma for file ") then
826 Parse_Token (Source, Parse_Ptr, Token_Ptr);
827 File.Table (Chop_File).SR_Name :=
828 new String'(Source (Token_Ptr + 1 .. Parse_Ptr - 2));
829 Parse_EOL (Source, Parse_Ptr);
832 -- Unrecognized keyword or end of file
838 -- Store the data in the Info record in the Unit.Table
841 Unit.Table (Unit.Last) := Info;
843 -- If this is not the first unit from the file, calculate
844 -- the length of the previous unit as difference of the offsets
846 if Unit.Last > First_Unit then
847 Unit.Table (Unit.Last - 1).Length :=
848 Info.Offset - Unit.Table (Unit.Last - 1).Offset;
851 -- If not in compilation mode combine current unit with any
852 -- preceding configuration pragmas.
854 if not Compilation_Mode
855 and then Unit.Last > First_Unit
856 and then Unit.Table (Unit.Last - 1).Kind = Config_Pragmas
858 Info.Start_Line := Unit.Table (Unit.Last - 1).Start_Line;
859 Info.Offset := Unit.Table (Unit.Last - 1).Offset;
861 -- Delete the configuration pragma entry
863 Unit.Table (Unit.Last - 1) := Info;
867 -- If in compilation mode, and previous entry is the initial
868 -- entry for the file and is for configuration pragmas, then
869 -- they are to be appended to every unit in the file.
872 and then Unit.Last = First_Unit + 1
873 and then Unit.Table (First_Unit).Kind = Config_Pragmas
877 (Unit.Table (Unit.Last - 1).Chop_File, First_Unit);
878 Unit.Table (Unit.Last - 1) := Info;
882 Unit.Table (Unit.Last).Bufferg := Bufferg;
884 -- If in compilation mode, and this is not the first item,
885 -- combine configuration pragmas with previous unit, which
886 -- will cause an error message to be generated when the unit
890 and then Unit.Last > First_Unit
891 and then Unit.Table (Unit.Last).Kind = Config_Pragmas
901 -- Find out if the loop was exited prematurely because of
902 -- an error or if the EOF marker was found.
904 if Source (Parse_Ptr) /= EOF then
906 (File.Table (Chop_File).Name.all & ": error parsing offset info");
910 -- Handle case of a chop file consisting only of config pragmas
912 if Unit.Last = First_Unit
913 and then Unit.Table (Unit.Last).Kind = Config_Pragmas
915 -- In compilation mode, we append such a file to gnat.adc
917 if Compilation_Mode then
918 Write_Config_File (Unit.Table (Unit.Last).Chop_File, First_Unit);
921 -- In default (non-compilation) mode, this is invalid
925 (File.Table (Chop_File).Name.all &
926 ": no units found (only pragmas)");
931 -- Handle case of a chop file ending with config pragmas. This can
932 -- happen only in default non-compilation mode, since in compilation
933 -- mode such configuration pragmas are part of the preceding unit.
934 -- We simply concatenate such pragmas to the previous file which
935 -- will cause a compilation error, which is appropriate.
937 if Unit.Last > First_Unit
938 and then Unit.Table (Unit.Last).Kind = Config_Pragmas
942 end Parse_Offset_Info;
948 procedure Parse_Token
949 (Source : not null access String;
950 Ptr : in out Positive;
951 Token_Ptr : out Positive)
953 In_Quotes : Boolean := False;
958 while Source (Ptr) = ' ' or Source (Ptr) = ',' loop
966 while (In_Quotes or else not (Source (Ptr) = ' ' or Source (Ptr) = ','))
967 and then Source (Ptr) >= ' '
969 if Source (Ptr) = '"' then
970 In_Quotes := not In_Quotes;
982 (FD : File_Descriptor;
983 Contents : out String_Access;
984 Success : out Boolean)
986 Length : constant File_Offset := File_Offset (File_Length (FD));
987 -- Include room for EOF char
988 Buffer : constant String_Access := new String (1 .. Length + 1);
991 Read_Ptr : File_Offset := 1;
996 This_Read := Read (FD,
997 A => Buffer (Read_Ptr)'Address,
998 N => Length + 1 - Read_Ptr);
999 Read_Ptr := Read_Ptr + Integer'Max (This_Read, 0);
1000 exit when This_Read <= 0;
1003 Buffer (Read_Ptr) := EOF;
1004 Contents := new String (1 .. Read_Ptr);
1005 Contents.all := Buffer (1 .. Read_Ptr);
1007 -- Things aren't simple on VMS due to the plethora of file types
1008 -- and organizations. It seems clear that there shouldn't be more
1009 -- bytes read than are contained in the file though.
1011 if Hostparm.OpenVMS then
1012 Success := Read_Ptr <= Length + 1;
1014 Success := Read_Ptr = Length + 1;
1018 ----------------------------
1019 -- Report_Duplicate_Units --
1020 ----------------------------
1022 function Report_Duplicate_Units return Boolean is
1026 Duplicates : Boolean := False;
1030 while US < SUnit_Num (Unit.Last) loop
1031 U := Sorted_Units.Table (US);
1033 if Is_Duplicated (US) then
1036 -- Move to last two versions of duplicated file to make it clearer
1037 -- to understand which file is retained in case of overwriting.
1039 while US + 1 < SUnit_Num (Unit.Last) loop
1040 exit when not Is_Duplicated (US + 1);
1044 U := Sorted_Units.Table (US);
1046 if Overwrite_Files then
1047 Warning_Msg (Unit.Table (U).File_Name.all
1048 & " is duplicated (all but last will be skipped)");
1050 elsif Unit.Table (U).Chop_File =
1051 Unit.Table (Sorted_Units.Table (US + 1)).Chop_File
1053 Error_Msg (Unit.Table (U).File_Name.all
1054 & " is duplicated in "
1055 & File.Table (Unit.Table (U).Chop_File).Name.all);
1058 Error_Msg (Unit.Table (U).File_Name.all
1060 & File.Table (Unit.Table (U).Chop_File).Name.all
1061 & " is duplicated in "
1064 (Sorted_Units.Table (US + 1)).Chop_File).Name.all);
1071 if Duplicates and not Overwrite_Files then
1072 if Hostparm.OpenVMS then
1074 ("use /OVERWRITE to overwrite files and keep last version");
1076 Put_Line ("use -w to overwrite files and keep last version");
1081 end Report_Duplicate_Units;
1083 --------------------
1084 -- Scan_Arguments --
1085 --------------------
1087 function Scan_Arguments return Boolean is
1088 Kset : Boolean := False;
1089 -- Set true if -k switch found
1092 Initialize_Option_Scan;
1094 -- Scan options first
1097 case Getopt ("c gnat? h k? p q r v w x -GCC=!") is
1102 Gcc := new String'(Parameter);
1106 Compilation_Mode := True;
1110 new Argument_List'(Gnat_Args.all &
1111 new String'("-gnat" & Parameter));
1115 raise Types.Terminate_Program;
1119 Param : String_Access := new String'(Parameter);
1122 if Param.all /= "" then
1123 for J in Param'Range loop
1124 if Param (J) not in '0' .. '9' then
1125 if Hostparm.OpenVMS then
1126 Error_Msg ("/FILE_NAME_MAX_LENGTH=nnn" &
1127 " requires numeric parameter");
1129 Error_Msg ("-k# requires numeric parameter");
1137 if Hostparm.OpenVMS then
1138 Param := new String'("39");
1140 Param := new String'("8");
1145 new Argument_List'(Gnat_Args.all &
1146 new String'("-gnatk" & Param.all));
1151 Preserve_Mode := True;
1157 Source_References := True;
1160 Verbose_Mode := True;
1161 Display_Version ("GNATCHOP", "1998");
1164 Overwrite_Files := True;
1167 Exit_On_Error := True;
1174 if not Kset and then Maximum_File_Name_Length > 0 then
1176 -- If this system has restricted filename lengths, tell gnat1
1177 -- about them, removing the leading blank from the image string.
1180 new Argument_List'(Gnat_Args.all
1181 & new String'("-gnatk"
1182 & Maximum_File_Name_Length_String
1183 (Maximum_File_Name_Length_String'First + 1
1184 .. Maximum_File_Name_Length_String'Last)));
1191 S : constant String := Get_Argument (Do_Expansion => True);
1195 File.Increment_Last;
1196 File.Table (File.Last).Name := new String'(S);
1197 File.Table (File.Last).SR_Name := null;
1201 -- Case of more than one file where last file is a directory
1204 and then Is_Directory (File.Table (File.Last).Name.all)
1206 Directory := File.Table (File.Last).Name;
1207 File.Decrement_Last;
1209 -- Make sure Directory is terminated with a directory separator,
1210 -- so we can generate the output by just appending a filename.
1212 if Directory (Directory'Last) /= Directory_Separator
1213 and then Directory (Directory'Last) /= '/'
1215 Directory := new String'(Directory.all & Directory_Separator);
1218 -- At least one filename must be given
1220 elsif File.Last = 0 then
1224 -- No directory given, set directory to null, so that we can just
1225 -- concatenate the directory name to the file name unconditionally.
1228 Directory := new String'("");
1231 -- Finally check all filename arguments
1233 for File_Num in 1 .. File.Last loop
1235 F : constant String := File.Table (File_Num).Name.all;
1239 if Is_Directory (F) then
1240 Error_Msg (F & " is a directory, cannot be chopped");
1243 elsif not Is_Regular_File (F) then
1244 Error_Msg (F & " not found");
1253 when Invalid_Switch =>
1254 Error_Msg ("invalid switch " & Full_Switch);
1257 when Invalid_Parameter =>
1258 if Hostparm.OpenVMS then
1259 Error_Msg ("/FILE_NAME_MAX_LENGTH=nnn qualifier" &
1260 " requires numeric parameter");
1262 Error_Msg ("-k switch requires numeric parameter");
1273 procedure Sort_Units is
1275 procedure Move (From : Natural; To : Natural);
1276 -- Procedure used to sort the unit list
1277 -- Unit.Table (To) := Unit_List (From); used by sort
1279 function Lt (Left, Right : Natural) return Boolean;
1280 -- Compares Left and Right units based on file name (first),
1281 -- Chop_File (second) and Offset (third). This ordering is
1282 -- important to keep the last version in case of duplicate files.
1284 package Unit_Sort is new GNAT.Heap_Sort_G (Move, Lt);
1285 -- Used for sorting on filename to detect duplicates
1291 function Lt (Left, Right : Natural) return Boolean is
1292 L : Unit_Info renames
1293 Unit.Table (Sorted_Units.Table (SUnit_Num (Left)));
1295 R : Unit_Info renames
1296 Unit.Table (Sorted_Units.Table (SUnit_Num (Right)));
1299 return L.File_Name.all < R.File_Name.all
1300 or else (L.File_Name.all = R.File_Name.all
1301 and then (L.Chop_File < R.Chop_File
1302 or else (L.Chop_File = R.Chop_File
1303 and then L.Offset < R.Offset)));
1310 procedure Move (From : Natural; To : Natural) is
1312 Sorted_Units.Table (SUnit_Num (To)) :=
1313 Sorted_Units.Table (SUnit_Num (From));
1316 -- Start of processing for Sort_Units
1319 Sorted_Units.Set_Last (SUnit_Num (Unit.Last));
1321 for J in 1 .. Unit.Last loop
1322 Sorted_Units.Table (SUnit_Num (J)) := J;
1325 -- Sort Unit.Table, using Sorted_Units.Table (0) as scratch
1327 Unit_Sort.Sort (Natural (Unit.Last));
1329 -- Set the Sorted_Index fields in the unit tables
1331 for J in 1 .. SUnit_Num (Unit.Last) loop
1332 Unit.Table (Sorted_Units.Table (J)).Sorted_Index := J;
1343 ("Usage: gnatchop [-c] [-h] [-k#] " &
1344 "[-r] [-p] [-q] [-v] [-w] [-x] [--GCC=xx] file [file ...] [dir]");
1348 (" -c compilation mode, configuration pragmas " &
1352 (" -gnatxxx passes the -gnatxxx switch to gnat parser");
1355 (" -h help: output this usage information");
1358 (" -k# krunch file names of generated files to " &
1359 "no more than # characters");
1362 (" -k krunch file names of generated files to " &
1363 "no more than 8 characters");
1366 (" -p preserve time stamp, output files will " &
1367 "have same stamp as input");
1370 (" -q quiet mode, no output of generated file " &
1374 (" -r generate Source_Reference pragmas refer" &
1375 "encing original source file");
1378 (" -v verbose mode, output version and generat" &
1382 (" -w overwrite existing filenames");
1385 (" -x exit on error");
1388 (" --GCC=xx specify the path of the gnat parser to be used");
1392 (" file... list of source files to be chopped");
1395 (" dir directory location for split files (defa" &
1396 "ult = current directory)");
1403 procedure Warning_Msg (Message : String) is
1405 Warning_Count := Warning_Count + 1;
1406 Put_Line (Standard_Error, "warning: " & Message);
1409 -------------------------
1410 -- Write_Chopped_Files --
1411 -------------------------
1413 function Write_Chopped_Files (Input : File_Num) return Boolean is
1414 Name : aliased constant String :=
1415 File.Table (Input).Name.all & ASCII.Nul;
1416 FD : File_Descriptor;
1417 Buffer : String_Access;
1422 FD := Open_Read (Name'Address, Binary);
1423 TS_Time := File_Time_Stamp (FD);
1425 if FD = Invalid_FD then
1426 Error_Msg ("cannot open " & File.Table (Input).Name.all);
1430 Read_File (FD, Buffer, Success);
1433 Error_Msg ("cannot read " & File.Table (Input).Name.all);
1438 if not Quiet_Mode then
1439 Put_Line ("splitting " & File.Table (Input).Name.all & " into:");
1442 -- Only chop those units that come from this file
1444 for Num in 1 .. Unit.Last loop
1445 if Unit.Table (Num).Chop_File = Input then
1446 Write_Unit (Buffer, Num, TS_Time, Success);
1447 exit when not Success;
1453 end Write_Chopped_Files;
1455 -----------------------
1456 -- Write_Config_File --
1457 -----------------------
1459 procedure Write_Config_File (Input : File_Num; U : Unit_Num) is
1460 FD : File_Descriptor;
1461 Name : aliased constant String := "gnat.adc" & ASCII.NUL;
1462 Buffer : String_Access;
1465 Buffera : String_Access;
1469 Write_gnat_adc := True;
1470 FD := Open_Read_Write (Name'Address, Binary);
1472 if FD = Invalid_FD then
1473 FD := Create_File (Name'Address, Binary);
1476 if not Quiet_Mode then
1477 Put_Line ("writing configuration pragmas from " &
1478 File.Table (Input).Name.all & " to gnat.adc");
1484 if not Quiet_Mode then
1486 ("appending configuration pragmas from " &
1487 File.Table (Input).Name.all & " to gnat.adc");
1491 Success := FD /= Invalid_FD;
1494 Error_Msg ("cannot create gnat.adc");
1498 -- In append mode, acquire existing gnat.adc file
1501 Read_File (FD, Buffera, Success);
1504 Error_Msg ("cannot read gnat.adc");
1508 -- Find location of EOF byte if any to exclude from append
1511 while Bufferl <= Buffera'Last
1512 and then Buffera (Bufferl) /= EOF
1514 Bufferl := Bufferl + 1;
1517 Bufferl := Bufferl - 1;
1520 -- Write existing gnat.adc to new gnat.adc file
1522 FD := Create_File (Name'Address, Binary);
1523 Success := Write (FD, Buffera (1)'Address, Bufferl) = Bufferl;
1526 Error_Msg ("error writing gnat.adc");
1531 Buffer := Get_Config_Pragmas (Input, U);
1533 if Buffer /= null then
1534 Success := Write (FD, Buffer.all'Address, Buffer'Length) =
1538 Error_Msg ("disk full writing gnat.adc");
1544 end Write_Config_File;
1546 -----------------------------------
1547 -- Write_Source_Reference_Pragma --
1548 -----------------------------------
1550 procedure Write_Source_Reference_Pragma
1553 File : Stream_IO.File_Type;
1555 Success : in out Boolean)
1557 FTE : File_Entry renames Gnatchop.File.Table (Info.Chop_File);
1558 Nam : String_Access;
1561 if Success and Source_References and not Info.SR_Present then
1562 if FTE.SR_Name /= null then
1569 Reference : String :=
1570 "pragma Source_Reference (000000, """
1571 & Nam.all & """);" & EOL.Str;
1573 Pos : Positive := Reference'First;
1574 Lin : Line_Num := Line;
1577 while Reference (Pos + 1) /= ',' loop
1581 while Reference (Pos) = '0' loop
1582 Reference (Pos) := Character'Val
1583 (Character'Pos ('0') + Lin mod 10);
1588 -- Assume there are enough zeroes for any program length
1590 pragma Assert (Lin = 0);
1593 String'Write (Stream_IO.Stream (File), Reference);
1601 end Write_Source_Reference_Pragma;
1607 procedure Write_Unit
1608 (Source : not null access String;
1611 Success : out Boolean)
1614 procedure OS_Filename
1616 W_Name : Wide_String;
1618 N_Length : access Natural;
1620 E_Length : access Natural);
1621 pragma Import (C, OS_Filename, "__gnat_os_filename");
1622 -- Returns in OS_Name the proper name for the OS when used with the
1623 -- returned Encoding value. For example on Windows this will return the
1624 -- UTF-8 encoded name into OS_Name and set Encoding to encoding=utf8
1625 -- (form parameter Stream_IO).
1626 -- Name is the filename and W_Name the same filename in Unicode 16 bits
1627 -- (this corresponds to Win32 Unicode ISO/IEC 10646). N_Length and
1628 -- E_Length are the length returned in OS_Name and Encoding
1631 Info : Unit_Info renames Unit.Table (Num);
1632 Name : aliased constant String := Info.File_Name.all & ASCII.NUL;
1633 W_Name : aliased constant Wide_String := To_Wide_String (Name);
1634 EOL : constant EOL_String :=
1635 Get_EOL (Source, Source'First + Info.Offset);
1637 OS_Name : aliased String (1 .. Name'Length * 2);
1638 O_Length : aliased Natural := OS_Name'Length;
1639 Encoding : aliased String (1 .. 64);
1640 E_Length : aliased Natural := Encoding'Length;
1642 Length : File_Offset;
1645 -- Skip duplicated files
1647 if Is_Duplicated (Info.Sorted_Index) then
1648 Put_Line (" " & Info.File_Name.all & " skipped");
1649 Success := Overwrite_Files;
1657 OS_Name'Address, O_Length'Access,
1658 Encoding'Address, E_Length'Access);
1661 E_Name : constant String := OS_Name (1 .. O_Length);
1662 C_Name : aliased constant String := E_Name & ASCII.Nul;
1663 OS_Encoding : constant String := Encoding (1 .. E_Length);
1664 File : Stream_IO.File_Type;
1667 if not Overwrite_Files and then Exists (E_Name) then
1668 raise Stream_IO.Name_Error;
1671 (File, Stream_IO.Out_File, E_Name, OS_Encoding);
1675 when Stream_IO.Name_Error | Stream_IO.Use_Error =>
1676 Error_Msg ("cannot create " & Info.File_Name.all);
1680 -- A length of 0 indicates that the rest of the file belongs to
1681 -- this unit. The actual length must be calculated now. Take into
1682 -- account that the last character (EOF) must not be written.
1684 if Info.Length = 0 then
1685 Length := Source'Last - (Source'First + Info.Offset);
1687 Length := Info.Length;
1690 -- Prepend configuration pragmas if necessary
1692 if Success and then Info.Bufferg /= null then
1693 Write_Source_Reference_Pragma (Info, 1, File, EOL, Success);
1695 String'Write (Stream_IO.Stream (File), Info.Bufferg.all);
1698 Write_Source_Reference_Pragma
1699 (Info, Info.Start_Line, File, EOL, Success);
1704 (Stream_IO.Stream (File),
1705 Source (Source'First + Info.Offset ..
1706 Source'First + Info.Offset + Length - 1));
1708 when Stream_IO.Use_Error | Stream_IO.Device_Error =>
1709 Error_Msg ("disk full writing " & Info.File_Name.all);
1714 if not Quiet_Mode then
1715 Put_Line (" " & Info.File_Name.all);
1718 Stream_IO.Close (File);
1720 if Preserve_Mode then
1721 File_Time_Stamp (C_Name'Address, TS_Time);
1726 -- Start of processing for gnatchop
1729 -- Add the directory where gnatchop is invoked in front of the
1730 -- path, if gnatchop is invoked with directory information.
1731 -- Only do this if the platform is not VMS, where the notion of path
1732 -- does not really exist.
1734 if not Hostparm.OpenVMS then
1736 Command : constant String := Command_Name;
1739 for Index in reverse Command'Range loop
1740 if Command (Index) = Directory_Separator then
1742 Absolute_Dir : constant String :=
1744 (Command (Command'First .. Index));
1746 PATH : constant String :=
1749 Getenv ("PATH").all;
1752 Setenv ("PATH", PATH);
1761 -- Process command line options and initialize global variables
1763 -- First, scan to detect --version and/or --help
1765 Check_Version_And_Help ("GNATCHOP", "1998", Usage'Unrestricted_Access);
1767 if not Scan_Arguments then
1768 Set_Exit_Status (Failure);
1772 -- Check presence of required executables
1774 Gnat_Cmd := Locate_Executable (Gcc.all, not Gcc_Set);
1776 if Gnat_Cmd = null then
1777 goto No_Files_Written;
1780 -- First parse all files and read offset information
1782 for Num in 1 .. File.Last loop
1783 if not Parse_File (Num) then
1784 goto No_Files_Written;
1788 -- Check if any units have been found (assumes non-empty Unit.Table)
1790 if Unit.Last = 0 then
1791 if not Write_gnat_adc then
1792 Error_Msg ("no compilation units found", Warning => True);
1795 goto No_Files_Written;
1800 -- Check if any duplicate files would be created. If so, emit
1801 -- a warning if Overwrite_Files is true, otherwise generate an error.
1803 if Report_Duplicate_Units and then not Overwrite_Files then
1804 goto No_Files_Written;
1807 -- Check if any files exist, if so do not write anything
1808 -- Because all files have been parsed and checked already,
1809 -- there won't be any duplicates
1811 if not Overwrite_Files and then Files_Exist then
1812 goto No_Files_Written;
1815 -- After this point, all source files are read in succession
1816 -- and chopped into their destination files.
1818 -- As the Source_File_Name pragmas are handled as logical file 0,
1821 for F in 1 .. File.Last loop
1822 if not Write_Chopped_Files (F) then
1823 Set_Exit_Status (Failure);
1828 if Warning_Count > 0 then
1830 Warnings_Msg : constant String := Warning_Count'Img & " warning(s)";
1832 Error_Msg (Warnings_Msg (2 .. Warnings_Msg'Last), Warning => True);
1838 <<No_Files_Written>>
1840 -- Special error exit for all situations where no files have
1843 if not Write_gnat_adc then
1844 Error_Msg ("no source files written", Warning => True);
1850 when Types.Terminate_Program =>