PR target/16201
[official-gcc.git] / gcc / ada / gnatchop.adb
blob0c5a25227af824d78bd9260594142989382573c9
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- G N A T C H O P --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1998-2004 Ada Core Technologies, Inc. --
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 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 with Ada.Command_Line; use Ada.Command_Line;
28 with Ada.Text_IO; use Ada.Text_IO;
30 with GNAT.Command_Line; use GNAT.Command_Line;
31 with GNAT.OS_Lib; use GNAT.OS_Lib;
32 with GNAT.Heap_Sort_G;
33 with GNAT.Table;
35 with Gnatvsn;
36 with Hostparm;
38 procedure Gnatchop is
40 Terminate_Program : exception;
41 -- Used to terminate execution immediately
43 Config_File_Name : constant String_Access := new String'("gnat.adc");
44 -- The name of the file holding the GNAT configuration pragmas
46 Gcc : String_Access := new String'("gcc");
47 -- May be modified by switch --GCC=
49 Gcc_Set : Boolean := False;
50 -- True if a switch --GCC= is used
52 Gnat_Cmd : String_Access;
53 -- Command to execute the GNAT compiler
55 Gnat_Args : Argument_List_Access :=
56 new Argument_List'
57 (new String'("-c"),
58 new String'("-x"),
59 new String'("ada"),
60 new String'("-gnats"),
61 new String'("-gnatu"));
62 -- Arguments used in Gnat_Cmd call
64 EOF : constant Character := Character'Val (26);
65 -- Special character to signal end of file. Not required in input
66 -- files, but properly treated if present. Not generated in output
67 -- files except as a result of copying input file.
69 --------------------
70 -- File arguments --
71 --------------------
73 subtype File_Num is Natural;
74 subtype File_Offset is Natural;
76 type File_Entry is record
77 Name : String_Access;
78 -- Name of chop file or directory
80 SR_Name : String_Access;
81 -- Null unless the chop file starts with a source reference pragma
82 -- in which case this field points to the file name from this pragma.
83 end record;
85 package File is new GNAT.Table
86 (Table_Component_Type => File_Entry,
87 Table_Index_Type => File_Num,
88 Table_Low_Bound => 1,
89 Table_Initial => 100,
90 Table_Increment => 100);
92 Directory : String_Access;
93 -- Record name of directory, or a null string if no directory given
95 Compilation_Mode : Boolean := False;
96 Overwrite_Files : Boolean := False;
97 Preserve_Mode : Boolean := False;
98 Quiet_Mode : Boolean := False;
99 Source_References : Boolean := False;
100 Verbose_Mode : Boolean := False;
101 Exit_On_Error : Boolean := False;
102 -- Global options
104 Write_gnat_adc : Boolean := False;
105 -- Gets set true if we append to gnat.adc or create a new gnat.adc.
106 -- Used to inhibit complaint about no units generated.
108 ---------------
109 -- Unit list --
110 ---------------
112 type Line_Num is new Natural;
113 -- Line number (for source reference pragmas)
115 type Unit_Count_Type is new Integer;
116 subtype Unit_Num is Unit_Count_Type range 1 .. Unit_Count_Type'Last;
117 -- Used to refer to unit number in unit table
119 type SUnit_Num is new Integer;
120 -- Used to refer to entry in sorted units table. Note that entry
121 -- zero is only for use by Heapsort, and is not otherwise referenced.
123 type Unit_Kind is (Unit_Spec, Unit_Body, Config_Pragmas);
125 -- Structure to contain all necessary information for one unit.
126 -- Entries are also temporarily used to record config pragma sequences.
128 type Unit_Info is record
129 File_Name : String_Access;
130 -- File name from GNAT output line
132 Chop_File : File_Num;
133 -- File number in chop file sequence
135 Start_Line : Line_Num;
136 -- Line number from GNAT output line
138 Offset : File_Offset;
139 -- Offset name from GNAT output line
141 SR_Present : Boolean;
142 -- Set True if SR parameter present
144 Length : File_Offset;
145 -- A length of 0 means that the Unit is the last one in the file
147 Kind : Unit_Kind;
148 -- Indicates kind of unit
150 Sorted_Index : SUnit_Num;
151 -- Index of unit in sorted unit list
153 Bufferg : String_Access;
154 -- Pointer to buffer containing configuration pragmas to be
155 -- prepended. Null if no pragmas to be prepended.
157 end record;
159 -- The following table stores the unit offset information
161 package Unit is new GNAT.Table
162 (Table_Component_Type => Unit_Info,
163 Table_Index_Type => Unit_Count_Type,
164 Table_Low_Bound => 1,
165 Table_Initial => 500,
166 Table_Increment => 100);
168 -- The following table is used as a sorted index to the Unit.Table.
169 -- The entries in Unit.Table are not moved, instead we just shuffle
170 -- the entries in Sorted_Units. Note that the zeroeth entry in this
171 -- table is used by GNAT.Heap_Sort_G.
173 package Sorted_Units is new GNAT.Table
174 (Table_Component_Type => Unit_Num,
175 Table_Index_Type => SUnit_Num,
176 Table_Low_Bound => 0,
177 Table_Initial => 500,
178 Table_Increment => 100);
180 function Is_Duplicated (U : SUnit_Num) return Boolean;
181 -- Returns true if U is duplicated by a later unit.
182 -- Note that this function returns false for the last entry.
184 procedure Sort_Units;
185 -- Sort units and set up sorted unit table.
187 ----------------------
188 -- File_Descriptors --
189 ----------------------
191 function dup (handle : File_Descriptor) return File_Descriptor;
192 function dup2 (from, to : File_Descriptor) return File_Descriptor;
193 -- File descriptor based functions needed for redirecting stdin/stdout
195 pragma Import (C, dup, "dup");
196 pragma Import (C, dup2, "dup2");
198 ---------------------
199 -- Local variables --
200 ---------------------
202 Warning_Count : Natural := 0;
203 -- Count of warnings issued so far
205 -----------------------
206 -- Local subprograms --
207 -----------------------
209 procedure Error_Msg (Message : String; Warning : Boolean := False);
210 -- Produce an error message on standard error output
212 procedure File_Time_Stamp (Name : C_File_Name; Time : OS_Time);
213 -- Given the name of a file or directory, Name, set the
214 -- time stamp. This function must be used for an unopened file.
216 function Files_Exist return Boolean;
217 -- Check Unit.Table for possible file names that already exist
218 -- in the file system. Returns true if files exist, False otherwise
220 function Get_Maximum_File_Name_Length return Integer;
221 pragma Import (C, Get_Maximum_File_Name_Length,
222 "__gnat_get_maximum_file_name_length");
223 -- Function to get maximum file name length for system
225 Maximum_File_Name_Length : constant Integer := Get_Maximum_File_Name_Length;
226 Maximum_File_Name_Length_String : constant String :=
227 Integer'Image
228 (Maximum_File_Name_Length);
230 function Locate_Executable
231 (Program_Name : String;
232 Look_For_Prefix : Boolean := True)
233 return String_Access;
234 -- Locate executable for given program name. This takes into account
235 -- the target-prefix of the current command, if Look_For_Prefix is True.
237 subtype EOL_Length is Natural range 0 .. 2;
238 -- Possible lengths of end of line sequence
240 type EOL_String (Len : EOL_Length := 0) is record
241 Str : String (1 .. Len);
242 end record;
244 function Get_EOL
245 (Source : access String;
246 Start : Positive)
247 return EOL_String;
248 -- Return the line terminator used in the passed string
250 procedure Parse_EOL (Source : access String; Ptr : in out Positive);
251 -- On return Source (Ptr) is the first character of the next line
252 -- or EOF. Source.all must be terminated by EOF.
254 function Parse_File (Num : File_Num) return Boolean;
255 -- Calls the GNAT compiler to parse the given source file and parses the
256 -- output using Parse_Offset_Info. Returns True if parse operation
257 -- completes, False if some system error (e.g. failure to read the
258 -- offset information) occurs.
260 procedure Parse_Offset_Info (Chop_File : File_Num; Source : access String);
261 -- Parses the output of the compiler indicating the offsets
262 -- and names of the compilation units in Chop_File.
264 procedure Parse_Token
265 (Source : access String;
266 Ptr : in out Positive;
267 Token_Ptr : out Positive);
268 -- Skips any separators and stores the start of the token in Token_Ptr.
269 -- Then stores the position of the next separator in Ptr.
270 -- On return Source (Token_Ptr .. Ptr - 1) is the token.
272 procedure Read_File
273 (FD : File_Descriptor;
274 Contents : out String_Access;
275 Success : out Boolean);
276 -- Reads file associated with FS into the newly allocated
277 -- string Contents.
278 -- [VMS] Success is true iff the number of bytes read is less than or
279 -- equal to the file size.
280 -- [Other] Success is true iff the number of bytes read is equal to
281 -- the file size.
283 function Report_Duplicate_Units return Boolean;
284 -- Output messages about duplicate units in the input files in Unit.Table
285 -- Returns True if any duplicates found, Fals if no duplicates found.
287 function Scan_Arguments return Boolean;
288 -- Scan command line options and set global variables accordingly.
289 -- Also scan out file and directory arguments. Returns True if scan
290 -- was successful, and False if the scan fails for any reason.
292 procedure Usage;
293 -- Output message on standard output describing syntax of gnatchop command
295 procedure Warning_Msg (Message : String);
296 -- Output a warning message on standard error and update warning count
298 function Write_Chopped_Files (Input : File_Num) return Boolean;
299 -- Write all units that result from chopping the Input file
301 procedure Write_Config_File (Input : File_Num; U : Unit_Num);
302 -- Call to write configuration pragmas (append them to gnat.adc)
303 -- Input is the file number for the chop file and U identifies the
304 -- unit entry for the configuration pragmas.
306 function Get_Config_Pragmas
307 (Input : File_Num;
308 U : Unit_Num)
309 return String_Access;
310 -- Call to read configuration pragmas from given unit entry, and
311 -- return a buffer containing the pragmas to be appended to
312 -- following units. Input is the file number for the chop file and
313 -- U identifies the unit entry for the configuration pragmas.
315 procedure Write_Source_Reference_Pragma
316 (Info : Unit_Info;
317 Line : Line_Num;
318 FD : File_Descriptor;
319 EOL : EOL_String;
320 Success : in out Boolean);
321 -- If Success is True on entry, writes a source reference pragma using
322 -- the chop file from Info, and the given line number. On return Success
323 -- indicates whether the write succeeded. If Success is False on entry,
324 -- or if the global flag Source_References is False, then the call to
325 -- Write_Source_Reference_Pragma has no effect. EOL indicates the end
326 -- of line sequence to be written at the end of the pragma.
328 procedure Write_Unit
329 (Source : access String;
330 Num : Unit_Num;
331 TS_Time : OS_Time;
332 Success : out Boolean);
333 -- Write one compilation unit of the source to file
335 ---------------
336 -- Error_Msg --
337 ---------------
339 procedure Error_Msg (Message : String; Warning : Boolean := False) is
340 begin
341 Put_Line (Standard_Error, Message);
343 if not Warning then
344 Set_Exit_Status (Failure);
346 if Exit_On_Error then
347 raise Terminate_Program;
348 end if;
349 end if;
350 end Error_Msg;
352 ---------------------
353 -- File_Time_Stamp --
354 ---------------------
356 procedure File_Time_Stamp (Name : C_File_Name; Time : OS_Time) is
357 procedure Set_File_Time (Name : C_File_Name; Time : OS_Time);
358 pragma Import (C, Set_File_Time, "__gnat_set_file_time_name");
360 begin
361 Set_File_Time (Name, Time);
362 end File_Time_Stamp;
364 -----------------
365 -- Files_Exist --
366 -----------------
368 function Files_Exist return Boolean is
369 Exists : Boolean := False;
371 begin
372 for SNum in 1 .. SUnit_Num (Unit.Last) loop
374 -- Only check and report for the last instance of duplicated files
376 if not Is_Duplicated (SNum) then
377 declare
378 Info : constant Unit_Info :=
379 Unit.Table (Sorted_Units.Table (SNum));
381 begin
382 if Is_Writable_File (Info.File_Name.all) then
383 if Hostparm.OpenVMS then
384 Error_Msg
385 (Info.File_Name.all
386 & " already exists, use /OVERWRITE to overwrite");
387 else
388 Error_Msg (Info.File_Name.all
389 & " already exists, use -w to overwrite");
390 end if;
392 Exists := True;
393 end if;
394 end;
395 end if;
396 end loop;
398 return Exists;
399 end Files_Exist;
401 ------------------------
402 -- Get_Config_Pragmas --
403 ------------------------
405 function Get_Config_Pragmas
406 (Input : File_Num;
407 U : Unit_Num)
408 return String_Access
410 Info : Unit_Info renames Unit.Table (U);
411 FD : File_Descriptor;
412 Name : aliased constant String :=
413 File.Table (Input).Name.all & ASCII.Nul;
414 Length : File_Offset;
415 Buffer : String_Access;
416 Success : Boolean;
417 Result : String_Access;
419 begin
420 FD := Open_Read (Name'Address, Binary);
422 if FD = Invalid_FD then
423 Error_Msg ("cannot open " & File.Table (Input).Name.all);
424 return null;
425 end if;
427 Read_File (FD, Buffer, Success);
429 -- A length of 0 indicates that the rest of the file belongs to
430 -- this unit. The actual length must be calculated now. Take into
431 -- account that the last character (EOF) must not be written.
433 if Info.Length = 0 then
434 Length := Buffer'Last - (Buffer'First + Info.Offset);
435 else
436 Length := Info.Length;
437 end if;
439 Result := new String'(Buffer (1 .. Length));
440 Close (FD);
441 return Result;
442 end Get_Config_Pragmas;
444 -------------
445 -- Get_EOL --
446 -------------
448 function Get_EOL
449 (Source : access String;
450 Start : Positive)
451 return EOL_String
453 Ptr : Positive := Start;
454 First : Positive;
455 Last : Natural;
457 begin
458 -- Skip to end of line
460 while Source (Ptr) /= ASCII.CR and then
461 Source (Ptr) /= ASCII.LF and then
462 Source (Ptr) /= EOF
463 loop
464 Ptr := Ptr + 1;
465 end loop;
467 Last := Ptr;
469 if Source (Ptr) /= EOF then
471 -- Found CR or LF
473 First := Ptr;
475 else
476 First := Ptr + 1;
477 end if;
479 -- Recognize CR/LF or LF/CR combination
481 if (Source (Ptr + 1) = ASCII.CR or Source (Ptr + 1) = ASCII.LF)
482 and then Source (Ptr) /= Source (Ptr + 1)
483 then
484 Last := First + 1;
485 end if;
487 return (Len => Last + 1 - First, Str => Source (First .. Last));
488 end Get_EOL;
490 -------------------
491 -- Is_Duplicated --
492 -------------------
494 function Is_Duplicated (U : SUnit_Num) return Boolean is
495 begin
496 return U < SUnit_Num (Unit.Last)
497 and then
498 Unit.Table (Sorted_Units.Table (U)).File_Name.all =
499 Unit.Table (Sorted_Units.Table (U + 1)).File_Name.all;
500 end Is_Duplicated;
502 -----------------------
503 -- Locate_Executable --
504 -----------------------
506 function Locate_Executable
507 (Program_Name : String;
508 Look_For_Prefix : Boolean := True)
509 return String_Access
511 Current_Command : constant String := Command_Name;
512 End_Of_Prefix : Natural := Current_Command'First - 1;
513 Start_Of_Prefix : Positive := Current_Command'First;
514 Result : String_Access;
516 begin
518 if Look_For_Prefix then
519 -- Find Start_Of_Prefix
521 for J in reverse Current_Command'Range loop
522 if Current_Command (J) = '/' or
523 Current_Command (J) = Directory_Separator or
524 Current_Command (J) = ':'
525 then
526 Start_Of_Prefix := J + 1;
527 exit;
528 end if;
529 end loop;
531 -- Find End_Of_Prefix
533 End_Of_Prefix := Start_Of_Prefix - 1;
535 for J in reverse Start_Of_Prefix .. Current_Command'Last loop
536 if Current_Command (J) = '-' then
537 End_Of_Prefix := J;
538 exit;
539 end if;
540 end loop;
541 end if;
543 declare
544 Command : constant String :=
545 Current_Command (Start_Of_Prefix .. End_Of_Prefix) &
546 Program_Name;
547 begin
548 Result := Locate_Exec_On_Path (Command);
550 if Result = null then
551 Error_Msg
552 (Command & ": installation problem, executable not found");
553 end if;
554 end;
556 return Result;
557 end Locate_Executable;
559 ---------------
560 -- Parse_EOL --
561 ---------------
563 procedure Parse_EOL (Source : access String; Ptr : in out Positive) is
564 begin
565 -- Skip to end of line
567 while Source (Ptr) /= ASCII.CR and then Source (Ptr) /= ASCII.LF
568 and then Source (Ptr) /= EOF
569 loop
570 Ptr := Ptr + 1;
571 end loop;
573 if Source (Ptr) /= EOF then
574 Ptr := Ptr + 1; -- skip CR or LF
575 end if;
577 -- Skip past CR/LF or LF/CR combination
579 if (Source (Ptr) = ASCII.CR or Source (Ptr) = ASCII.LF)
580 and then Source (Ptr) /= Source (Ptr - 1)
581 then
582 Ptr := Ptr + 1;
583 end if;
584 end Parse_EOL;
586 ----------------
587 -- Parse_File --
588 ----------------
590 function Parse_File (Num : File_Num) return Boolean is
591 Chop_Name : constant String_Access := File.Table (Num).Name;
592 Save_Stdout : constant File_Descriptor := dup (Standout);
593 Offset_Name : Temp_File_Name;
594 Offset_FD : File_Descriptor;
595 Buffer : String_Access;
596 Success : Boolean;
597 Failure : exception;
599 begin
600 -- Display copy of GNAT command if verbose mode
602 if Verbose_Mode then
603 Put (Gnat_Cmd.all);
605 for J in 1 .. Gnat_Args'Length loop
606 Put (' ');
607 Put (Gnat_Args (J).all);
608 end loop;
610 Put (' ');
611 Put_Line (Chop_Name.all);
612 end if;
614 -- Create temporary file
616 Create_Temp_File (Offset_FD, Offset_Name);
618 if Offset_FD = Invalid_FD then
619 Error_Msg ("gnatchop: cannot create temporary file");
620 Close (Save_Stdout);
621 return False;
622 end if;
624 -- Redirect Stdout to this temporary file in the Unix way
626 if dup2 (Offset_FD, Standout) = Invalid_FD then
627 Error_Msg ("gnatchop: cannot redirect stdout to temporary file");
628 Close (Save_Stdout);
629 Close (Offset_FD);
630 return False;
631 end if;
633 -- Call Gnat on the source filename argument with special options
634 -- to generate offset information. If this special compilation completes
635 -- successfully then we can do the actual gnatchop operation.
637 Spawn (Gnat_Cmd.all, Gnat_Args.all & Chop_Name, Success);
639 if not Success then
640 Error_Msg (Chop_Name.all & ": parse errors detected");
641 Error_Msg (Chop_Name.all & ": chop may not be successful");
642 end if;
644 -- Restore stdout
646 if dup2 (Save_Stdout, Standout) = Invalid_FD then
647 Error_Msg ("gnatchop: cannot restore stdout");
648 end if;
650 -- Reopen the file to start reading from the beginning
652 Close (Offset_FD);
653 Close (Save_Stdout);
654 Offset_FD := Open_Read (Offset_Name'Address, Binary);
656 if Offset_FD = Invalid_FD then
657 Error_Msg ("gnatchop: cannot access offset info");
658 raise Failure;
659 end if;
661 Read_File (Offset_FD, Buffer, Success);
663 if not Success then
664 Error_Msg ("gnatchop: error reading offset info");
665 Close (Offset_FD);
666 raise Failure;
667 else
668 Parse_Offset_Info (Num, Buffer);
669 end if;
671 -- Close and delete temporary file
673 Close (Offset_FD);
674 Delete_File (Offset_Name'Address, Success);
676 return Success;
678 exception
679 when Failure | Terminate_Program =>
680 Close (Offset_FD);
681 Delete_File (Offset_Name'Address, Success);
682 return False;
684 end Parse_File;
686 -----------------------
687 -- Parse_Offset_Info --
688 -----------------------
690 procedure Parse_Offset_Info
691 (Chop_File : File_Num;
692 Source : access String)
694 First_Unit : constant Unit_Num := Unit.Last + 1;
695 Bufferg : String_Access := null;
696 Parse_Ptr : File_Offset := Source'First;
697 Token_Ptr : File_Offset;
698 Info : Unit_Info;
700 function Match (Literal : String) return Boolean;
701 -- Checks if given string appears at the current Token_Ptr location
702 -- and if so, bumps Parse_Ptr past the token and returns True. If
703 -- the string is not present, sets Parse_Ptr to Token_Ptr and
704 -- returns False.
706 -----------
707 -- Match --
708 -----------
710 function Match (Literal : String) return Boolean is
711 begin
712 Parse_Token (Source, Parse_Ptr, Token_Ptr);
714 if Source'Last + 1 - Token_Ptr < Literal'Length
715 or else
716 Source (Token_Ptr .. Token_Ptr + Literal'Length - 1) /= Literal
717 then
718 Parse_Ptr := Token_Ptr;
719 return False;
720 end if;
722 Parse_Ptr := Token_Ptr + Literal'Length;
723 return True;
724 end Match;
726 -- Start of processing for Parse_Offset_Info
728 begin
729 loop
730 -- Set default values, should get changed for all
731 -- units/pragmas except for the last
733 Info.Chop_File := Chop_File;
734 Info.Length := 0;
736 -- Parse the current line of offset information into Info
737 -- and exit the loop if there are any errors or on EOF.
739 -- First case, parse a line in the following format:
741 -- Unit x (spec) line 7, file offset 142, [SR, ]file name x.ads
743 -- Note that the unit name can be an operator name in quotes.
744 -- This is of course illegal, but both GNAT and gnatchop handle
745 -- the case so that this error does not intefere with chopping.
747 -- The SR ir present indicates that a source reference pragma
748 -- was processed as part of this unit (and that therefore no
749 -- Source_Reference pragma should be generated.
751 if Match ("Unit") then
752 Parse_Token (Source, Parse_Ptr, Token_Ptr);
754 if Match ("(body)") then
755 Info.Kind := Unit_Body;
756 elsif Match ("(spec)") then
757 Info.Kind := Unit_Spec;
758 else
759 exit;
760 end if;
762 exit when not Match ("line");
763 Parse_Token (Source, Parse_Ptr, Token_Ptr);
764 Info.Start_Line := Line_Num'Value
765 (Source (Token_Ptr .. Parse_Ptr - 1));
767 exit when not Match ("file offset");
768 Parse_Token (Source, Parse_Ptr, Token_Ptr);
769 Info.Offset := File_Offset'Value
770 (Source (Token_Ptr .. Parse_Ptr - 1));
772 Info.SR_Present := Match ("SR, ");
774 exit when not Match ("file name");
775 Parse_Token (Source, Parse_Ptr, Token_Ptr);
776 Info.File_Name := new String'
777 (Directory.all & Source (Token_Ptr .. Parse_Ptr - 1));
778 Parse_EOL (Source, Parse_Ptr);
780 -- Second case, parse a line of the following form
782 -- Configuration pragmas at line 10, file offset 223
784 elsif Match ("Configuration pragmas at") then
785 Info.Kind := Config_Pragmas;
786 Info.File_Name := Config_File_Name;
788 exit when not Match ("line");
789 Parse_Token (Source, Parse_Ptr, Token_Ptr);
790 Info.Start_Line := Line_Num'Value
791 (Source (Token_Ptr .. Parse_Ptr - 1));
793 exit when not Match ("file offset");
794 Parse_Token (Source, Parse_Ptr, Token_Ptr);
795 Info.Offset := File_Offset'Value
796 (Source (Token_Ptr .. Parse_Ptr - 1));
798 Parse_EOL (Source, Parse_Ptr);
800 -- Third case, parse a line of the following form
802 -- Source_Reference pragma for file "filename"
804 -- This appears at the start of the file only, and indicates
805 -- the name to be used on any generated Source_Reference pragmas.
807 elsif Match ("Source_Reference pragma for file ") then
808 Parse_Token (Source, Parse_Ptr, Token_Ptr);
809 File.Table (Chop_File).SR_Name :=
810 new String'(Source (Token_Ptr + 1 .. Parse_Ptr - 2));
811 Parse_EOL (Source, Parse_Ptr);
812 goto Continue;
814 -- Unrecognized keyword or end of file
816 else
817 exit;
818 end if;
820 -- Store the data in the Info record in the Unit.Table
822 Unit.Increment_Last;
823 Unit.Table (Unit.Last) := Info;
825 -- If this is not the first unit from the file, calculate
826 -- the length of the previous unit as difference of the offsets
828 if Unit.Last > First_Unit then
829 Unit.Table (Unit.Last - 1).Length :=
830 Info.Offset - Unit.Table (Unit.Last - 1).Offset;
831 end if;
833 -- If not in compilation mode combine current unit with any
834 -- preceding configuration pragmas.
836 if not Compilation_Mode
837 and then Unit.Last > First_Unit
838 and then Unit.Table (Unit.Last - 1).Kind = Config_Pragmas
839 then
840 Info.Start_Line := Unit.Table (Unit.Last - 1).Start_Line;
841 Info.Offset := Unit.Table (Unit.Last - 1).Offset;
843 -- Delete the configuration pragma entry
845 Unit.Table (Unit.Last - 1) := Info;
846 Unit.Decrement_Last;
847 end if;
849 -- If in compilation mode, and previous entry is the initial
850 -- entry for the file and is for configuration pragmas, then
851 -- they are to be appended to every unit in the file.
853 if Compilation_Mode
854 and then Unit.Last = First_Unit + 1
855 and then Unit.Table (First_Unit).Kind = Config_Pragmas
856 then
857 Bufferg :=
858 Get_Config_Pragmas
859 (Unit.Table (Unit.Last - 1).Chop_File, First_Unit);
860 Unit.Table (Unit.Last - 1) := Info;
861 Unit.Decrement_Last;
862 end if;
864 Unit.Table (Unit.Last).Bufferg := Bufferg;
866 -- If in compilation mode, and this is not the first item,
867 -- combine configuration pragmas with previous unit, which
868 -- will cause an error message to be generated when the unit
869 -- is compiled.
871 if Compilation_Mode
872 and then Unit.Last > First_Unit
873 and then Unit.Table (Unit.Last).Kind = Config_Pragmas
874 then
875 Unit.Decrement_Last;
876 end if;
878 <<Continue>>
879 null;
881 end loop;
883 -- Find out if the loop was exited prematurely because of
884 -- an error or if the EOF marker was found.
886 if Source (Parse_Ptr) /= EOF then
887 Error_Msg
888 (File.Table (Chop_File).Name.all & ": error parsing offset info");
889 return;
890 end if;
892 -- Handle case of a chop file consisting only of config pragmas
894 if Unit.Last = First_Unit
895 and then Unit.Table (Unit.Last).Kind = Config_Pragmas
896 then
897 -- In compilation mode, we append such a file to gnat.adc
899 if Compilation_Mode then
900 Write_Config_File (Unit.Table (Unit.Last).Chop_File, First_Unit);
901 Unit.Decrement_Last;
903 -- In default (non-compilation) mode, this is invalid
905 else
906 Error_Msg
907 (File.Table (Chop_File).Name.all &
908 ": no units found (only pragmas)");
909 Unit.Decrement_Last;
910 end if;
911 end if;
913 -- Handle case of a chop file ending with config pragmas. This can
914 -- happen only in default non-compilation mode, since in compilation
915 -- mode such configuration pragmas are part of the preceding unit.
916 -- We simply concatenate such pragmas to the previous file which
917 -- will cause a compilation error, which is appropriate.
919 if Unit.Last > First_Unit
920 and then Unit.Table (Unit.Last).Kind = Config_Pragmas
921 then
922 Unit.Decrement_Last;
923 end if;
924 end Parse_Offset_Info;
926 -----------------
927 -- Parse_Token --
928 -----------------
930 procedure Parse_Token
931 (Source : access String;
932 Ptr : in out Positive;
933 Token_Ptr : out Positive)
935 In_Quotes : Boolean := False;
937 begin
938 -- Skip separators
940 while Source (Ptr) = ' ' or Source (Ptr) = ',' loop
941 Ptr := Ptr + 1;
942 end loop;
944 Token_Ptr := Ptr;
946 -- Find end-of-token
948 while (In_Quotes or else not (Source (Ptr) = ' ' or Source (Ptr) = ','))
949 and then Source (Ptr) >= ' '
950 loop
951 if Source (Ptr) = '"' then
952 In_Quotes := not In_Quotes;
953 end if;
955 Ptr := Ptr + 1;
956 end loop;
957 end Parse_Token;
959 ---------------
960 -- Read_File --
961 ---------------
963 procedure Read_File
964 (FD : File_Descriptor;
965 Contents : out String_Access;
966 Success : out Boolean)
968 Length : constant File_Offset := File_Offset (File_Length (FD));
969 -- Include room for EOF char
970 Buffer : constant String_Access := new String (1 .. Length + 1);
972 This_Read : Integer;
973 Read_Ptr : File_Offset := 1;
975 begin
977 loop
978 This_Read := Read (FD,
979 A => Buffer (Read_Ptr)'Address,
980 N => Length + 1 - Read_Ptr);
981 Read_Ptr := Read_Ptr + Integer'Max (This_Read, 0);
982 exit when This_Read <= 0;
983 end loop;
985 Buffer (Read_Ptr) := EOF;
986 Contents := new String (1 .. Read_Ptr);
987 Contents.all := Buffer (1 .. Read_Ptr);
989 -- Things aren't simple on VMS due to the plethora of file types
990 -- and organizations. It seems clear that there shouldn't be more
991 -- bytes read than are contained in the file though.
993 if Hostparm.OpenVMS then
994 Success := Read_Ptr <= Length + 1;
995 else
996 Success := Read_Ptr = Length + 1;
997 end if;
998 end Read_File;
1000 ----------------------------
1001 -- Report_Duplicate_Units --
1002 ----------------------------
1004 function Report_Duplicate_Units return Boolean is
1005 US : SUnit_Num;
1006 U : Unit_Num;
1008 Duplicates : Boolean := False;
1010 begin
1011 US := 1;
1012 while US < SUnit_Num (Unit.Last) loop
1013 U := Sorted_Units.Table (US);
1015 if Is_Duplicated (US) then
1016 Duplicates := True;
1018 -- Move to last two versions of duplicated file to make it clearer
1019 -- to understand which file is retained in case of overwriting.
1021 while US + 1 < SUnit_Num (Unit.Last) loop
1022 exit when not Is_Duplicated (US + 1);
1023 US := US + 1;
1024 end loop;
1026 U := Sorted_Units.Table (US);
1028 if Overwrite_Files then
1029 Warning_Msg (Unit.Table (U).File_Name.all
1030 & " is duplicated (all but last will be skipped)");
1032 elsif Unit.Table (U).Chop_File =
1033 Unit.Table (Sorted_Units.Table (US + 1)).Chop_File
1034 then
1035 Error_Msg (Unit.Table (U).File_Name.all
1036 & " is duplicated in "
1037 & File.Table (Unit.Table (U).Chop_File).Name.all);
1039 else
1040 Error_Msg (Unit.Table (U).File_Name.all
1041 & " in "
1042 & File.Table (Unit.Table (U).Chop_File).Name.all
1043 & " is duplicated in "
1044 & File.Table
1045 (Unit.Table
1046 (Sorted_Units.Table (US + 1)).Chop_File).Name.all);
1047 end if;
1048 end if;
1050 US := US + 1;
1051 end loop;
1053 if Duplicates and not Overwrite_Files then
1054 if Hostparm.OpenVMS then
1055 Put_Line
1056 ("use /OVERWRITE to overwrite files and keep last version");
1057 else
1058 Put_Line ("use -w to overwrite files and keep last version");
1059 end if;
1060 end if;
1062 return Duplicates;
1063 end Report_Duplicate_Units;
1065 --------------------
1066 -- Scan_Arguments --
1067 --------------------
1069 function Scan_Arguments return Boolean is
1070 Kset : Boolean := False;
1071 -- Set true if -k switch found
1073 begin
1074 Initialize_Option_Scan;
1076 -- Scan options first
1078 loop
1079 case Getopt ("c gnat? h k? p q r v w x -GCC=!") is
1080 when ASCII.NUL =>
1081 exit;
1083 when '-' =>
1084 Gcc := new String'(Parameter);
1085 Gcc_Set := True;
1087 when 'c' =>
1088 Compilation_Mode := True;
1090 when 'g' =>
1091 Gnat_Args :=
1092 new Argument_List'(Gnat_Args.all &
1093 new String'("-gnat" & Parameter));
1095 when 'h' =>
1096 Usage;
1097 raise Terminate_Program;
1099 when 'k' =>
1100 declare
1101 Param : String_Access := new String'(Parameter);
1103 begin
1104 if Param.all /= "" then
1105 for J in Param'Range loop
1106 if Param (J) not in '0' .. '9' then
1107 if Hostparm.OpenVMS then
1108 Error_Msg ("/FILE_NAME_MAX_LENGTH=nnn" &
1109 " requires numeric parameter");
1110 else
1111 Error_Msg ("-k# requires numeric parameter");
1112 end if;
1114 return False;
1115 end if;
1116 end loop;
1118 else
1119 if Hostparm.OpenVMS then
1120 Param := new String'("39");
1121 else
1122 Param := new String'("8");
1123 end if;
1124 end if;
1126 Gnat_Args :=
1127 new Argument_List'(Gnat_Args.all &
1128 new String'("-gnatk" & Param.all));
1129 Kset := True;
1130 end;
1132 when 'p' =>
1133 Preserve_Mode := True;
1135 when 'q' =>
1136 Quiet_Mode := True;
1138 when 'r' =>
1139 Source_References := True;
1141 when 'v' =>
1142 Verbose_Mode := True;
1144 -- Why is following written to standard error. Most other
1145 -- tools write to standard output ???
1147 Put (Standard_Error, "GNATCHOP ");
1148 Put_Line (Standard_Error, Gnatvsn.Gnat_Version_String);
1149 Put_Line
1150 (Standard_Error,
1151 "Copyright 1998-2004, Ada Core Technologies Inc.");
1153 when 'w' =>
1154 Overwrite_Files := True;
1156 when 'x' =>
1157 Exit_On_Error := True;
1159 when others =>
1160 null;
1161 end case;
1162 end loop;
1164 if not Kset and then Maximum_File_Name_Length > 0 then
1166 -- If this system has restricted filename lengths, tell gnat1
1167 -- about them, removing the leading blank from the image string.
1169 Gnat_Args :=
1170 new Argument_List'(Gnat_Args.all
1171 & new String'("-gnatk"
1172 & Maximum_File_Name_Length_String
1173 (Maximum_File_Name_Length_String'First + 1
1174 .. Maximum_File_Name_Length_String'Last)));
1175 end if;
1177 -- Scan file names
1179 loop
1180 declare
1181 S : constant String := Get_Argument (Do_Expansion => True);
1183 begin
1184 exit when S = "";
1185 File.Increment_Last;
1186 File.Table (File.Last).Name := new String'(S);
1187 File.Table (File.Last).SR_Name := null;
1188 end;
1189 end loop;
1191 -- Case of more than one file where last file is a directory
1193 if File.Last > 1
1194 and then Is_Directory (File.Table (File.Last).Name.all)
1195 then
1196 Directory := File.Table (File.Last).Name;
1197 File.Decrement_Last;
1199 -- Make sure Directory is terminated with a directory separator,
1200 -- so we can generate the output by just appending a filename.
1202 if Directory (Directory'Last) /= Directory_Separator
1203 and then Directory (Directory'Last) /= '/'
1204 then
1205 Directory := new String'(Directory.all & Directory_Separator);
1206 end if;
1208 -- At least one filename must be given
1210 elsif File.Last = 0 then
1211 Usage;
1212 return False;
1214 -- No directory given, set directory to null, so that we can just
1215 -- concatenate the directory name to the file name unconditionally.
1217 else
1218 Directory := new String'("");
1219 end if;
1221 -- Finally check all filename arguments
1223 for File_Num in 1 .. File.Last loop
1224 declare
1225 F : constant String := File.Table (File_Num).Name.all;
1227 begin
1229 if Is_Directory (F) then
1230 Error_Msg (F & " is a directory, cannot be chopped");
1231 return False;
1233 elsif not Is_Regular_File (F) then
1234 Error_Msg (F & " not found");
1235 return False;
1236 end if;
1237 end;
1238 end loop;
1240 return True;
1242 exception
1243 when Invalid_Switch =>
1244 Error_Msg ("invalid switch " & Full_Switch);
1245 return False;
1247 when Invalid_Parameter =>
1248 if Hostparm.OpenVMS then
1249 Error_Msg ("/FILE_NAME_MAX_LENGTH=nnn qualifier" &
1250 " requires numeric parameter");
1251 else
1252 Error_Msg ("-k switch requires numeric parameter");
1253 end if;
1255 return False;
1257 end Scan_Arguments;
1259 ----------------
1260 -- Sort_Units --
1261 ----------------
1263 procedure Sort_Units is
1265 procedure Move (From : Natural; To : Natural);
1266 -- Procedure used to sort the unit list
1267 -- Unit.Table (To) := Unit_List (From); used by sort
1269 function Lt (Left, Right : Natural) return Boolean;
1270 -- Compares Left and Right units based on file name (first),
1271 -- Chop_File (second) and Offset (third). This ordering is
1272 -- important to keep the last version in case of duplicate files.
1274 package Unit_Sort is new GNAT.Heap_Sort_G (Move, Lt);
1275 -- Used for sorting on filename to detect duplicates
1277 --------
1278 -- Lt --
1279 --------
1281 function Lt (Left, Right : Natural) return Boolean is
1282 L : Unit_Info renames
1283 Unit.Table (Sorted_Units.Table (SUnit_Num (Left)));
1285 R : Unit_Info renames
1286 Unit.Table (Sorted_Units.Table (SUnit_Num (Right)));
1288 begin
1289 return L.File_Name.all < R.File_Name.all
1290 or else (L.File_Name.all = R.File_Name.all
1291 and then (L.Chop_File < R.Chop_File
1292 or else (L.Chop_File = R.Chop_File
1293 and then L.Offset < R.Offset)));
1294 end Lt;
1296 ----------
1297 -- Move --
1298 ----------
1300 procedure Move (From : Natural; To : Natural) is
1301 begin
1302 Sorted_Units.Table (SUnit_Num (To)) :=
1303 Sorted_Units.Table (SUnit_Num (From));
1304 end Move;
1306 -- Start of processing for Sort_Units
1308 begin
1309 Sorted_Units.Set_Last (SUnit_Num (Unit.Last));
1311 for J in 1 .. Unit.Last loop
1312 Sorted_Units.Table (SUnit_Num (J)) := J;
1313 end loop;
1315 -- Sort Unit.Table, using Sorted_Units.Table (0) as scratch
1317 Unit_Sort.Sort (Natural (Unit.Last));
1319 -- Set the Sorted_Index fields in the unit tables.
1321 for J in 1 .. SUnit_Num (Unit.Last) loop
1322 Unit.Table (Sorted_Units.Table (J)).Sorted_Index := J;
1323 end loop;
1324 end Sort_Units;
1326 -----------
1327 -- Usage --
1328 -----------
1330 procedure Usage is
1331 begin
1332 Put_Line
1333 ("Usage: gnatchop [-c] [-h] [-k#] " &
1334 "[-r] [-p] [-q] [-v] [-w] [-x] [--GCC=xx] file [file ...] [dir]");
1336 New_Line;
1337 Put_Line
1338 (" -c compilation mode, configuration pragmas " &
1339 "follow RM rules");
1341 Put_Line
1342 (" -gnatxxx passes the -gnatxxx switch to gnat parser");
1344 Put_Line
1345 (" -h help: output this usage information");
1347 Put_Line
1348 (" -k# krunch file names of generated files to " &
1349 "no more than # characters");
1351 Put_Line
1352 (" -k krunch file names of generated files to " &
1353 "no more than 8 characters");
1355 Put_Line
1356 (" -p preserve time stamp, output files will " &
1357 "have same stamp as input");
1359 Put_Line
1360 (" -q quiet mode, no output of generated file " &
1361 "names");
1363 Put_Line
1364 (" -r generate Source_Reference pragmas refer" &
1365 "encing original source file");
1367 Put_Line
1368 (" -v verbose mode, output version and generat" &
1369 "ed commands");
1371 Put_Line
1372 (" -w overwrite existing filenames");
1374 Put_Line
1375 (" -x exit on error");
1377 Put_Line
1378 (" --GCC=xx specify the path of the gnat parser to be used");
1380 New_Line;
1381 Put_Line
1382 (" file... list of source files to be chopped");
1384 Put_Line
1385 (" dir directory location for split files (defa" &
1386 "ult = current directory)");
1387 end Usage;
1389 -----------------
1390 -- Warning_Msg --
1391 -----------------
1393 procedure Warning_Msg (Message : String) is
1394 begin
1395 Warning_Count := Warning_Count + 1;
1396 Put_Line (Standard_Error, "warning: " & Message);
1397 end Warning_Msg;
1399 -------------------------
1400 -- Write_Chopped_Files --
1401 -------------------------
1403 function Write_Chopped_Files (Input : File_Num) return Boolean is
1404 Name : aliased constant String :=
1405 File.Table (Input).Name.all & ASCII.Nul;
1406 FD : File_Descriptor;
1407 Buffer : String_Access;
1408 Success : Boolean;
1409 TS_Time : OS_Time;
1411 begin
1412 FD := Open_Read (Name'Address, Binary);
1413 TS_Time := File_Time_Stamp (FD);
1415 if FD = Invalid_FD then
1416 Error_Msg ("cannot open " & File.Table (Input).Name.all);
1417 return False;
1418 end if;
1420 Read_File (FD, Buffer, Success);
1422 if not Success then
1423 Error_Msg ("cannot read " & File.Table (Input).Name.all);
1424 Close (FD);
1425 return False;
1426 end if;
1428 if not Quiet_Mode then
1429 Put_Line ("splitting " & File.Table (Input).Name.all & " into:");
1430 end if;
1432 -- Only chop those units that come from this file
1434 for Num in 1 .. Unit.Last loop
1435 if Unit.Table (Num).Chop_File = Input then
1436 Write_Unit (Buffer, Num, TS_Time, Success);
1437 exit when not Success;
1438 end if;
1439 end loop;
1441 Close (FD);
1442 return Success;
1444 end Write_Chopped_Files;
1446 -----------------------
1447 -- Write_Config_File --
1448 -----------------------
1450 procedure Write_Config_File (Input : File_Num; U : Unit_Num) is
1451 FD : File_Descriptor;
1452 Name : aliased constant String := "gnat.adc" & ASCII.NUL;
1453 Buffer : String_Access;
1454 Success : Boolean;
1455 Append : Boolean;
1456 Buffera : String_Access;
1457 Bufferl : Natural;
1459 begin
1460 Write_gnat_adc := True;
1461 FD := Open_Read_Write (Name'Address, Binary);
1463 if FD = Invalid_FD then
1464 FD := Create_File (Name'Address, Binary);
1465 Append := False;
1467 if not Quiet_Mode then
1468 Put_Line ("writing configuration pragmas from " &
1469 File.Table (Input).Name.all & " to gnat.adc");
1470 end if;
1472 else
1473 Append := True;
1475 if not Quiet_Mode then
1476 Put_Line
1477 ("appending configuration pragmas from " &
1478 File.Table (Input).Name.all & " to gnat.adc");
1479 end if;
1480 end if;
1482 Success := FD /= Invalid_FD;
1484 if not Success then
1485 Error_Msg ("cannot create gnat.adc");
1486 return;
1487 end if;
1489 -- In append mode, acquire existing gnat.adc file
1491 if Append then
1492 Read_File (FD, Buffera, Success);
1494 if not Success then
1495 Error_Msg ("cannot read gnat.adc");
1496 return;
1497 end if;
1499 -- Find location of EOF byte if any to exclude from append
1501 Bufferl := 1;
1502 while Bufferl <= Buffera'Last
1503 and then Buffera (Bufferl) /= EOF
1504 loop
1505 Bufferl := Bufferl + 1;
1506 end loop;
1508 Bufferl := Bufferl - 1;
1509 Close (FD);
1511 -- Write existing gnat.adc to new gnat.adc file
1513 FD := Create_File (Name'Address, Binary);
1514 Success := Write (FD, Buffera (1)'Address, Bufferl) = Bufferl;
1516 if not Success then
1517 Error_Msg ("error writing gnat.adc");
1518 return;
1519 end if;
1520 end if;
1522 Buffer := Get_Config_Pragmas (Input, U);
1524 if Buffer /= null then
1525 Success := Write (FD, Buffer.all'Address, Buffer'Length) =
1526 Buffer'Length;
1528 if not Success then
1529 Error_Msg ("disk full writing gnat.adc");
1530 return;
1531 end if;
1532 end if;
1534 Close (FD);
1535 end Write_Config_File;
1537 -----------------------------------
1538 -- Write_Source_Reference_Pragma --
1539 -----------------------------------
1541 procedure Write_Source_Reference_Pragma
1542 (Info : Unit_Info;
1543 Line : Line_Num;
1544 FD : File_Descriptor;
1545 EOL : EOL_String;
1546 Success : in out Boolean)
1548 FTE : File_Entry renames File.Table (Info.Chop_File);
1549 Nam : String_Access;
1551 begin
1552 if Success and Source_References and not Info.SR_Present then
1553 if FTE.SR_Name /= null then
1554 Nam := FTE.SR_Name;
1555 else
1556 Nam := FTE.Name;
1557 end if;
1559 declare
1560 Reference : aliased String :=
1561 "pragma Source_Reference (000000, """
1562 & Nam.all & """);" & EOL.Str;
1564 Pos : Positive := Reference'First;
1565 Lin : Line_Num := Line;
1567 begin
1568 while Reference (Pos + 1) /= ',' loop
1569 Pos := Pos + 1;
1570 end loop;
1572 while Reference (Pos) = '0' loop
1573 Reference (Pos) := Character'Val
1574 (Character'Pos ('0') + Lin mod 10);
1575 Lin := Lin / 10;
1576 Pos := Pos - 1;
1577 end loop;
1579 -- Assume there are enough zeroes for any program length
1581 pragma Assert (Lin = 0);
1583 Success :=
1584 Write (FD, Reference'Address, Reference'Length)
1585 = Reference'Length;
1586 end;
1587 end if;
1588 end Write_Source_Reference_Pragma;
1590 ----------------
1591 -- Write_Unit --
1592 ----------------
1594 procedure Write_Unit
1595 (Source : access String;
1596 Num : Unit_Num;
1597 TS_Time : OS_Time;
1598 Success : out Boolean)
1600 Info : Unit_Info renames Unit.Table (Num);
1601 FD : File_Descriptor;
1602 Name : aliased constant String := Info.File_Name.all & ASCII.NUL;
1603 Length : File_Offset;
1604 EOL : constant EOL_String :=
1605 Get_EOL (Source, Source'First + Info.Offset);
1607 begin
1608 -- Skip duplicated files
1610 if Is_Duplicated (Info.Sorted_Index) then
1611 Put_Line (" " & Info.File_Name.all & " skipped");
1612 Success := Overwrite_Files;
1613 return;
1614 end if;
1616 if Overwrite_Files then
1617 FD := Create_File (Name'Address, Binary);
1618 else
1619 FD := Create_New_File (Name'Address, Binary);
1620 end if;
1622 Success := FD /= Invalid_FD;
1624 if not Success then
1625 Error_Msg ("cannot create " & Info.File_Name.all);
1626 return;
1627 end if;
1629 -- A length of 0 indicates that the rest of the file belongs to
1630 -- this unit. The actual length must be calculated now. Take into
1631 -- account that the last character (EOF) must not be written.
1633 if Info.Length = 0 then
1634 Length := Source'Last - (Source'First + Info.Offset);
1635 else
1636 Length := Info.Length;
1637 end if;
1639 -- Prepend configuration pragmas if necessary
1641 if Success and then Info.Bufferg /= null then
1642 Write_Source_Reference_Pragma (Info, 1, FD, EOL, Success);
1643 Success :=
1644 Write (FD, Info.Bufferg.all'Address, Info.Bufferg'Length) =
1645 Info.Bufferg'Length;
1646 end if;
1648 Write_Source_Reference_Pragma (Info, Info.Start_Line, FD, EOL, Success);
1650 if Success then
1651 Success := Write (FD, Source (Source'First + Info.Offset)'Address,
1652 Length) = Length;
1653 end if;
1655 if not Success then
1656 Error_Msg ("disk full writing " & Info.File_Name.all);
1657 return;
1658 end if;
1660 if not Quiet_Mode then
1661 Put_Line (" " & Info.File_Name.all);
1662 end if;
1664 Close (FD);
1666 if Preserve_Mode then
1667 File_Time_Stamp (Name'Address, TS_Time);
1668 end if;
1670 end Write_Unit;
1672 -- Start of processing for gnatchop
1674 begin
1675 -- Add the directory where gnatchop is invoked in front of the
1676 -- path, if gnatchop is invoked with directory information.
1677 -- Only do this if the platform is not VMS, where the notion of path
1678 -- does not really exist.
1680 if not Hostparm.OpenVMS then
1681 declare
1682 Command : constant String := Command_Name;
1684 begin
1685 for Index in reverse Command'Range loop
1686 if Command (Index) = Directory_Separator then
1687 declare
1688 Absolute_Dir : constant String :=
1689 Normalize_Pathname
1690 (Command (Command'First .. Index));
1692 PATH : constant String :=
1693 Absolute_Dir &
1694 Path_Separator &
1695 Getenv ("PATH").all;
1697 begin
1698 Setenv ("PATH", PATH);
1699 end;
1701 exit;
1702 end if;
1703 end loop;
1704 end;
1705 end if;
1707 -- Process command line options and initialize global variables
1709 if not Scan_Arguments then
1710 Set_Exit_Status (Failure);
1711 return;
1712 end if;
1714 -- Check presence of required executables
1716 Gnat_Cmd := Locate_Executable (Gcc.all, not Gcc_Set);
1718 if Gnat_Cmd = null then
1719 goto No_Files_Written;
1720 end if;
1722 -- First parse all files and read offset information
1724 for Num in 1 .. File.Last loop
1725 if not Parse_File (Num) then
1726 goto No_Files_Written;
1727 end if;
1728 end loop;
1730 -- Check if any units have been found (assumes non-empty Unit.Table)
1732 if Unit.Last = 0 then
1733 if not Write_gnat_adc then
1734 Error_Msg ("no compilation units found", Warning => True);
1735 end if;
1737 goto No_Files_Written;
1738 end if;
1740 Sort_Units;
1742 -- Check if any duplicate files would be created. If so, emit
1743 -- a warning if Overwrite_Files is true, otherwise generate an error.
1745 if Report_Duplicate_Units and then not Overwrite_Files then
1746 goto No_Files_Written;
1747 end if;
1749 -- Check if any files exist, if so do not write anything
1750 -- Because all files have been parsed and checked already,
1751 -- there won't be any duplicates
1753 if not Overwrite_Files and then Files_Exist then
1754 goto No_Files_Written;
1755 end if;
1757 -- After this point, all source files are read in succession
1758 -- and chopped into their destination files.
1760 -- As the Source_File_Name pragmas are handled as logical file 0,
1761 -- write it first.
1763 for F in 1 .. File.Last loop
1764 if not Write_Chopped_Files (F) then
1765 Set_Exit_Status (Failure);
1766 return;
1767 end if;
1768 end loop;
1770 if Warning_Count > 0 then
1771 declare
1772 Warnings_Msg : constant String := Warning_Count'Img & " warning(s)";
1773 begin
1774 Error_Msg (Warnings_Msg (2 .. Warnings_Msg'Last), Warning => True);
1775 end;
1776 end if;
1778 return;
1780 <<No_Files_Written>>
1782 -- Special error exit for all situations where no files have
1783 -- been written.
1785 if not Write_gnat_adc then
1786 Error_Msg ("no source files written", Warning => True);
1787 end if;
1789 return;
1791 exception
1792 when Terminate_Program =>
1793 null;
1795 end Gnatchop;