2005-12-29 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / ada / gnatchop.adb
blobeab7063253ed654bd8004349cf23d538c81e8375
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-2005, AdaCore --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 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. --
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 with System.CRTL; use System.CRTL;
40 procedure Gnatchop is
42 Terminate_Program : exception;
43 -- Used to terminate execution immediately
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 :=
58 new Argument_List'
59 (new String'("-c"),
60 new String'("-x"),
61 new String'("ada"),
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.
71 --------------------
72 -- File arguments --
73 --------------------
75 subtype File_Num is Natural;
76 subtype File_Offset is Natural;
78 type File_Entry is record
79 Name : String_Access;
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.
85 end record;
87 package File is new GNAT.Table
88 (Table_Component_Type => File_Entry,
89 Table_Index_Type => File_Num,
90 Table_Low_Bound => 1,
91 Table_Initial => 100,
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;
104 -- Global options
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.
110 ---------------
111 -- Unit list --
112 ---------------
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
149 Kind : Unit_Kind;
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.
159 end record;
161 -- The following table stores the unit offset information
163 package Unit is new GNAT.Table
164 (Table_Component_Type => Unit_Info,
165 Table_Index_Type => Unit_Count_Type,
166 Table_Low_Bound => 1,
167 Table_Initial => 500,
168 Table_Increment => 100);
170 -- The following table is used as a sorted index to the Unit.Table.
171 -- The entries in Unit.Table are not moved, instead we just shuffle
172 -- the entries in Sorted_Units. Note that the zeroeth entry in this
173 -- table is used by GNAT.Heap_Sort_G.
175 package Sorted_Units is new GNAT.Table
176 (Table_Component_Type => Unit_Num,
177 Table_Index_Type => SUnit_Num,
178 Table_Low_Bound => 0,
179 Table_Initial => 500,
180 Table_Increment => 100);
182 function Is_Duplicated (U : SUnit_Num) return Boolean;
183 -- Returns true if U is duplicated by a later unit.
184 -- Note that this function returns false for the last entry.
186 procedure Sort_Units;
187 -- Sort units and set up sorted unit table
189 ----------------------
190 -- File_Descriptors --
191 ----------------------
193 function dup (handle : File_Descriptor) return File_Descriptor;
194 function dup2 (from, to : File_Descriptor) return File_Descriptor;
196 ---------------------
197 -- Local variables --
198 ---------------------
200 Warning_Count : Natural := 0;
201 -- Count of warnings issued so far
203 -----------------------
204 -- Local subprograms --
205 -----------------------
207 procedure Error_Msg (Message : String; Warning : Boolean := False);
208 -- Produce an error message on standard error output
210 procedure File_Time_Stamp (Name : C_File_Name; Time : OS_Time);
211 -- Given the name of a file or directory, Name, set the
212 -- time stamp. This function must be used for an unopened file.
214 function Files_Exist return Boolean;
215 -- Check Unit.Table for possible file names that already exist
216 -- in the file system. Returns true if files exist, False otherwise
218 function Get_Maximum_File_Name_Length return Integer;
219 pragma Import (C, Get_Maximum_File_Name_Length,
220 "__gnat_get_maximum_file_name_length");
221 -- Function to get maximum file name length for system
223 Maximum_File_Name_Length : constant Integer := Get_Maximum_File_Name_Length;
224 Maximum_File_Name_Length_String : constant String :=
225 Integer'Image
226 (Maximum_File_Name_Length);
228 function Locate_Executable
229 (Program_Name : String;
230 Look_For_Prefix : Boolean := True)
231 return String_Access;
232 -- Locate executable for given program name. This takes into account
233 -- the target-prefix of the current command, if Look_For_Prefix is True.
235 subtype EOL_Length is Natural range 0 .. 2;
236 -- Possible lengths of end of line sequence
238 type EOL_String (Len : EOL_Length := 0) is record
239 Str : String (1 .. Len);
240 end record;
242 function Get_EOL
243 (Source : access String;
244 Start : Positive)
245 return EOL_String;
246 -- Return the line terminator used in the passed string
248 procedure Parse_EOL (Source : access String; Ptr : in out Positive);
249 -- On return Source (Ptr) is the first character of the next line
250 -- or EOF. Source.all must be terminated by EOF.
252 function Parse_File (Num : File_Num) return Boolean;
253 -- Calls the GNAT compiler to parse the given source file and parses the
254 -- output using Parse_Offset_Info. Returns True if parse operation
255 -- completes, False if some system error (e.g. failure to read the
256 -- offset information) occurs.
258 procedure Parse_Offset_Info (Chop_File : File_Num; Source : access String);
259 -- Parses the output of the compiler indicating the offsets
260 -- and names of the compilation units in Chop_File.
262 procedure Parse_Token
263 (Source : access String;
264 Ptr : in out Positive;
265 Token_Ptr : out Positive);
266 -- Skips any separators and stores the start of the token in Token_Ptr.
267 -- Then stores the position of the next separator in Ptr.
268 -- On return Source (Token_Ptr .. Ptr - 1) is the token.
270 procedure Read_File
271 (FD : File_Descriptor;
272 Contents : out String_Access;
273 Success : out Boolean);
274 -- Reads file associated with FS into the newly allocated
275 -- string Contents.
276 -- [VMS] Success is true iff the number of bytes read is less than or
277 -- equal to the file size.
278 -- [Other] Success is true iff the number of bytes read is equal to
279 -- the file size.
281 function Report_Duplicate_Units return Boolean;
282 -- Output messages about duplicate units in the input files in Unit.Table
283 -- Returns True if any duplicates found, Fals if no duplicates found.
285 function Scan_Arguments return Boolean;
286 -- Scan command line options and set global variables accordingly.
287 -- Also scan out file and directory arguments. Returns True if scan
288 -- was successful, and False if the scan fails for any reason.
290 procedure Usage;
291 -- Output message on standard output describing syntax of gnatchop command
293 procedure Warning_Msg (Message : String);
294 -- Output a warning message on standard error and update warning count
296 function Write_Chopped_Files (Input : File_Num) return Boolean;
297 -- Write all units that result from chopping the Input file
299 procedure Write_Config_File (Input : File_Num; U : Unit_Num);
300 -- Call to write configuration pragmas (append them to gnat.adc)
301 -- Input is the file number for the chop file and U identifies the
302 -- unit entry for the configuration pragmas.
304 function Get_Config_Pragmas
305 (Input : File_Num;
306 U : Unit_Num)
307 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
314 (Info : Unit_Info;
315 Line : Line_Num;
316 FD : File_Descriptor;
317 EOL : EOL_String;
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.
326 procedure Write_Unit
327 (Source : access String;
328 Num : Unit_Num;
329 TS_Time : OS_Time;
330 Success : out Boolean);
331 -- Write one compilation unit of the source to file
333 ---------
334 -- dup --
335 ---------
337 function dup (handle : File_Descriptor) return File_Descriptor is
338 begin
339 return File_Descriptor (System.CRTL.dup (int (handle)));
340 end dup;
342 ----------
343 -- dup2 --
344 ----------
346 function dup2 (from, to : File_Descriptor) return File_Descriptor is
347 begin
348 return File_Descriptor (System.CRTL.dup2 (int (from), int (to)));
349 end dup2;
351 ---------------
352 -- Error_Msg --
353 ---------------
355 procedure Error_Msg (Message : String; Warning : Boolean := False) is
356 begin
357 Put_Line (Standard_Error, Message);
359 if not Warning then
360 Set_Exit_Status (Failure);
362 if Exit_On_Error then
363 raise Terminate_Program;
364 end if;
365 end if;
366 end Error_Msg;
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");
376 begin
377 Set_File_Time (Name, Time);
378 end File_Time_Stamp;
380 -----------------
381 -- Files_Exist --
382 -----------------
384 function Files_Exist return Boolean is
385 Exists : Boolean := False;
387 begin
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
393 declare
394 Info : constant Unit_Info :=
395 Unit.Table (Sorted_Units.Table (SNum));
397 begin
398 if Is_Writable_File (Info.File_Name.all) then
399 if Hostparm.OpenVMS then
400 Error_Msg
401 (Info.File_Name.all
402 & " already exists, use /OVERWRITE to overwrite");
403 else
404 Error_Msg (Info.File_Name.all
405 & " already exists, use -w to overwrite");
406 end if;
408 Exists := True;
409 end if;
410 end;
411 end if;
412 end loop;
414 return Exists;
415 end Files_Exist;
417 ------------------------
418 -- Get_Config_Pragmas --
419 ------------------------
421 function Get_Config_Pragmas
422 (Input : File_Num;
423 U : Unit_Num)
424 return String_Access
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;
432 Success : Boolean;
433 Result : String_Access;
435 begin
436 FD := Open_Read (Name'Address, Binary);
438 if FD = Invalid_FD then
439 Error_Msg ("cannot open " & File.Table (Input).Name.all);
440 return null;
441 end if;
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);
451 else
452 Length := Info.Length;
453 end if;
455 Result := new String'(Buffer (1 .. Length));
456 Close (FD);
457 return Result;
458 end Get_Config_Pragmas;
460 -------------
461 -- Get_EOL --
462 -------------
464 function Get_EOL
465 (Source : access String;
466 Start : Positive)
467 return EOL_String
469 Ptr : Positive := Start;
470 First : Positive;
471 Last : Natural;
473 begin
474 -- Skip to end of line
476 while Source (Ptr) /= ASCII.CR and then
477 Source (Ptr) /= ASCII.LF and then
478 Source (Ptr) /= EOF
479 loop
480 Ptr := Ptr + 1;
481 end loop;
483 Last := Ptr;
485 if Source (Ptr) /= EOF then
487 -- Found CR or LF
489 First := Ptr;
491 else
492 First := Ptr + 1;
493 end if;
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)
499 then
500 Last := First + 1;
501 end if;
503 return (Len => Last + 1 - First, Str => Source (First .. Last));
504 end Get_EOL;
506 -------------------
507 -- Is_Duplicated --
508 -------------------
510 function Is_Duplicated (U : SUnit_Num) return Boolean is
511 begin
512 return U < SUnit_Num (Unit.Last)
513 and then
514 Unit.Table (Sorted_Units.Table (U)).File_Name.all =
515 Unit.Table (Sorted_Units.Table (U + 1)).File_Name.all;
516 end Is_Duplicated;
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;
531 begin
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) = ':'
543 then
544 Start_Of_Prefix := J + 1;
545 exit;
546 end if;
547 end loop;
549 -- Find End_Of_Prefix
551 for J in reverse Start_Of_Prefix .. Current_Command'Last loop
552 if Current_Command (J) = '-' then
553 End_Of_Prefix := J;
554 exit;
555 end if;
556 end loop;
557 end if;
559 declare
560 Command : constant String :=
561 Current_Command (Start_Of_Prefix .. End_Of_Prefix) &
562 Program_Name;
563 begin
564 Result := Locate_Exec_On_Path (Command);
566 if Result = null then
567 Error_Msg
568 (Command & ": installation problem, executable not found");
569 end if;
570 end;
572 return Result;
573 end Locate_Executable;
575 ---------------
576 -- Parse_EOL --
577 ---------------
579 procedure Parse_EOL (Source : access String; Ptr : in out Positive) is
580 begin
581 -- Skip to end of line
583 while Source (Ptr) /= ASCII.CR and then Source (Ptr) /= ASCII.LF
584 and then Source (Ptr) /= EOF
585 loop
586 Ptr := Ptr + 1;
587 end loop;
589 if Source (Ptr) /= EOF then
590 Ptr := Ptr + 1; -- skip CR or LF
591 end if;
593 -- Skip past CR/LF or LF/CR combination
595 if (Source (Ptr) = ASCII.CR or Source (Ptr) = ASCII.LF)
596 and then Source (Ptr) /= Source (Ptr - 1)
597 then
598 Ptr := Ptr + 1;
599 end if;
600 end Parse_EOL;
602 ----------------
603 -- Parse_File --
604 ----------------
606 function Parse_File (Num : File_Num) return Boolean is
607 Chop_Name : constant String_Access := File.Table (Num).Name;
608 Save_Stdout : constant File_Descriptor := dup (Standout);
609 Offset_Name : Temp_File_Name;
610 Offset_FD : File_Descriptor;
611 Buffer : String_Access;
612 Success : Boolean;
613 Failure : exception;
615 begin
616 -- Display copy of GNAT command if verbose mode
618 if Verbose_Mode then
619 Put (Gnat_Cmd.all);
621 for J in 1 .. Gnat_Args'Length loop
622 Put (' ');
623 Put (Gnat_Args (J).all);
624 end loop;
626 Put (' ');
627 Put_Line (Chop_Name.all);
628 end if;
630 -- Create temporary file
632 Create_Temp_File (Offset_FD, Offset_Name);
634 if Offset_FD = Invalid_FD then
635 Error_Msg ("gnatchop: cannot create temporary file");
636 Close (Save_Stdout);
637 return False;
638 end if;
640 -- Redirect Stdout to this temporary file in the Unix way
642 if dup2 (Offset_FD, Standout) = Invalid_FD then
643 Error_Msg ("gnatchop: cannot redirect stdout to temporary file");
644 Close (Save_Stdout);
645 Close (Offset_FD);
646 return False;
647 end if;
649 -- Call Gnat on the source filename argument with special options
650 -- to generate offset information. If this special compilation completes
651 -- successfully then we can do the actual gnatchop operation.
653 Spawn (Gnat_Cmd.all, Gnat_Args.all & Chop_Name, Success);
655 if not Success then
656 Error_Msg (Chop_Name.all & ": parse errors detected");
657 Error_Msg (Chop_Name.all & ": chop may not be successful");
658 end if;
660 -- Restore stdout
662 if dup2 (Save_Stdout, Standout) = Invalid_FD then
663 Error_Msg ("gnatchop: cannot restore stdout");
664 end if;
666 -- Reopen the file to start reading from the beginning
668 Close (Offset_FD);
669 Close (Save_Stdout);
670 Offset_FD := Open_Read (Offset_Name'Address, Binary);
672 if Offset_FD = Invalid_FD then
673 Error_Msg ("gnatchop: cannot access offset info");
674 raise Failure;
675 end if;
677 Read_File (Offset_FD, Buffer, Success);
679 if not Success then
680 Error_Msg ("gnatchop: error reading offset info");
681 Close (Offset_FD);
682 raise Failure;
683 else
684 Parse_Offset_Info (Num, Buffer);
685 end if;
687 -- Close and delete temporary file
689 Close (Offset_FD);
690 Delete_File (Offset_Name'Address, Success);
692 return Success;
694 exception
695 when Failure | Terminate_Program =>
696 Close (Offset_FD);
697 Delete_File (Offset_Name'Address, Success);
698 return False;
700 end Parse_File;
702 -----------------------
703 -- Parse_Offset_Info --
704 -----------------------
706 procedure Parse_Offset_Info
707 (Chop_File : File_Num;
708 Source : access String)
710 First_Unit : constant Unit_Num := Unit.Last + 1;
711 Bufferg : String_Access := null;
712 Parse_Ptr : File_Offset := Source'First;
713 Token_Ptr : File_Offset;
714 Info : Unit_Info;
716 function Match (Literal : String) return Boolean;
717 -- Checks if given string appears at the current Token_Ptr location
718 -- and if so, bumps Parse_Ptr past the token and returns True. If
719 -- the string is not present, sets Parse_Ptr to Token_Ptr and
720 -- returns False.
722 -----------
723 -- Match --
724 -----------
726 function Match (Literal : String) return Boolean is
727 begin
728 Parse_Token (Source, Parse_Ptr, Token_Ptr);
730 if Source'Last + 1 - Token_Ptr < Literal'Length
731 or else
732 Source (Token_Ptr .. Token_Ptr + Literal'Length - 1) /= Literal
733 then
734 Parse_Ptr := Token_Ptr;
735 return False;
736 end if;
738 Parse_Ptr := Token_Ptr + Literal'Length;
739 return True;
740 end Match;
742 -- Start of processing for Parse_Offset_Info
744 begin
745 loop
746 -- Set default values, should get changed for all
747 -- units/pragmas except for the last
749 Info.Chop_File := Chop_File;
750 Info.Length := 0;
752 -- Parse the current line of offset information into Info
753 -- and exit the loop if there are any errors or on EOF.
755 -- First case, parse a line in the following format:
757 -- Unit x (spec) line 7, file offset 142, [SR, ]file name x.ads
759 -- Note that the unit name can be an operator name in quotes.
760 -- This is of course illegal, but both GNAT and gnatchop handle
761 -- the case so that this error does not intefere with chopping.
763 -- The SR ir present indicates that a source reference pragma
764 -- was processed as part of this unit (and that therefore no
765 -- Source_Reference pragma should be generated.
767 if Match ("Unit") then
768 Parse_Token (Source, Parse_Ptr, Token_Ptr);
770 if Match ("(body)") then
771 Info.Kind := Unit_Body;
772 elsif Match ("(spec)") then
773 Info.Kind := Unit_Spec;
774 else
775 exit;
776 end if;
778 exit when not Match ("line");
779 Parse_Token (Source, Parse_Ptr, Token_Ptr);
780 Info.Start_Line := Line_Num'Value
781 (Source (Token_Ptr .. Parse_Ptr - 1));
783 exit when not Match ("file offset");
784 Parse_Token (Source, Parse_Ptr, Token_Ptr);
785 Info.Offset := File_Offset'Value
786 (Source (Token_Ptr .. Parse_Ptr - 1));
788 Info.SR_Present := Match ("SR, ");
790 exit when not Match ("file name");
791 Parse_Token (Source, Parse_Ptr, Token_Ptr);
792 Info.File_Name := new String'
793 (Directory.all & Source (Token_Ptr .. Parse_Ptr - 1));
794 Parse_EOL (Source, Parse_Ptr);
796 -- Second case, parse a line of the following form
798 -- Configuration pragmas at line 10, file offset 223
800 elsif Match ("Configuration pragmas at") then
801 Info.Kind := Config_Pragmas;
802 Info.File_Name := Config_File_Name;
804 exit when not Match ("line");
805 Parse_Token (Source, Parse_Ptr, Token_Ptr);
806 Info.Start_Line := Line_Num'Value
807 (Source (Token_Ptr .. Parse_Ptr - 1));
809 exit when not Match ("file offset");
810 Parse_Token (Source, Parse_Ptr, Token_Ptr);
811 Info.Offset := File_Offset'Value
812 (Source (Token_Ptr .. Parse_Ptr - 1));
814 Parse_EOL (Source, Parse_Ptr);
816 -- Third case, parse a line of the following form
818 -- Source_Reference pragma for file "filename"
820 -- This appears at the start of the file only, and indicates
821 -- the name to be used on any generated Source_Reference pragmas.
823 elsif Match ("Source_Reference pragma for file ") then
824 Parse_Token (Source, Parse_Ptr, Token_Ptr);
825 File.Table (Chop_File).SR_Name :=
826 new String'(Source (Token_Ptr + 1 .. Parse_Ptr - 2));
827 Parse_EOL (Source, Parse_Ptr);
828 goto Continue;
830 -- Unrecognized keyword or end of file
832 else
833 exit;
834 end if;
836 -- Store the data in the Info record in the Unit.Table
838 Unit.Increment_Last;
839 Unit.Table (Unit.Last) := Info;
841 -- If this is not the first unit from the file, calculate
842 -- the length of the previous unit as difference of the offsets
844 if Unit.Last > First_Unit then
845 Unit.Table (Unit.Last - 1).Length :=
846 Info.Offset - Unit.Table (Unit.Last - 1).Offset;
847 end if;
849 -- If not in compilation mode combine current unit with any
850 -- preceding configuration pragmas.
852 if not Compilation_Mode
853 and then Unit.Last > First_Unit
854 and then Unit.Table (Unit.Last - 1).Kind = Config_Pragmas
855 then
856 Info.Start_Line := Unit.Table (Unit.Last - 1).Start_Line;
857 Info.Offset := Unit.Table (Unit.Last - 1).Offset;
859 -- Delete the configuration pragma entry
861 Unit.Table (Unit.Last - 1) := Info;
862 Unit.Decrement_Last;
863 end if;
865 -- If in compilation mode, and previous entry is the initial
866 -- entry for the file and is for configuration pragmas, then
867 -- they are to be appended to every unit in the file.
869 if Compilation_Mode
870 and then Unit.Last = First_Unit + 1
871 and then Unit.Table (First_Unit).Kind = Config_Pragmas
872 then
873 Bufferg :=
874 Get_Config_Pragmas
875 (Unit.Table (Unit.Last - 1).Chop_File, First_Unit);
876 Unit.Table (Unit.Last - 1) := Info;
877 Unit.Decrement_Last;
878 end if;
880 Unit.Table (Unit.Last).Bufferg := Bufferg;
882 -- If in compilation mode, and this is not the first item,
883 -- combine configuration pragmas with previous unit, which
884 -- will cause an error message to be generated when the unit
885 -- is compiled.
887 if Compilation_Mode
888 and then Unit.Last > First_Unit
889 and then Unit.Table (Unit.Last).Kind = Config_Pragmas
890 then
891 Unit.Decrement_Last;
892 end if;
894 <<Continue>>
895 null;
897 end loop;
899 -- Find out if the loop was exited prematurely because of
900 -- an error or if the EOF marker was found.
902 if Source (Parse_Ptr) /= EOF then
903 Error_Msg
904 (File.Table (Chop_File).Name.all & ": error parsing offset info");
905 return;
906 end if;
908 -- Handle case of a chop file consisting only of config pragmas
910 if Unit.Last = First_Unit
911 and then Unit.Table (Unit.Last).Kind = Config_Pragmas
912 then
913 -- In compilation mode, we append such a file to gnat.adc
915 if Compilation_Mode then
916 Write_Config_File (Unit.Table (Unit.Last).Chop_File, First_Unit);
917 Unit.Decrement_Last;
919 -- In default (non-compilation) mode, this is invalid
921 else
922 Error_Msg
923 (File.Table (Chop_File).Name.all &
924 ": no units found (only pragmas)");
925 Unit.Decrement_Last;
926 end if;
927 end if;
929 -- Handle case of a chop file ending with config pragmas. This can
930 -- happen only in default non-compilation mode, since in compilation
931 -- mode such configuration pragmas are part of the preceding unit.
932 -- We simply concatenate such pragmas to the previous file which
933 -- will cause a compilation error, which is appropriate.
935 if Unit.Last > First_Unit
936 and then Unit.Table (Unit.Last).Kind = Config_Pragmas
937 then
938 Unit.Decrement_Last;
939 end if;
940 end Parse_Offset_Info;
942 -----------------
943 -- Parse_Token --
944 -----------------
946 procedure Parse_Token
947 (Source : access String;
948 Ptr : in out Positive;
949 Token_Ptr : out Positive)
951 In_Quotes : Boolean := False;
953 begin
954 -- Skip separators
956 while Source (Ptr) = ' ' or Source (Ptr) = ',' loop
957 Ptr := Ptr + 1;
958 end loop;
960 Token_Ptr := Ptr;
962 -- Find end-of-token
964 while (In_Quotes or else not (Source (Ptr) = ' ' or Source (Ptr) = ','))
965 and then Source (Ptr) >= ' '
966 loop
967 if Source (Ptr) = '"' then
968 In_Quotes := not In_Quotes;
969 end if;
971 Ptr := Ptr + 1;
972 end loop;
973 end Parse_Token;
975 ---------------
976 -- Read_File --
977 ---------------
979 procedure Read_File
980 (FD : File_Descriptor;
981 Contents : out String_Access;
982 Success : out Boolean)
984 Length : constant File_Offset := File_Offset (File_Length (FD));
985 -- Include room for EOF char
986 Buffer : constant String_Access := new String (1 .. Length + 1);
988 This_Read : Integer;
989 Read_Ptr : File_Offset := 1;
991 begin
993 loop
994 This_Read := Read (FD,
995 A => Buffer (Read_Ptr)'Address,
996 N => Length + 1 - Read_Ptr);
997 Read_Ptr := Read_Ptr + Integer'Max (This_Read, 0);
998 exit when This_Read <= 0;
999 end loop;
1001 Buffer (Read_Ptr) := EOF;
1002 Contents := new String (1 .. Read_Ptr);
1003 Contents.all := Buffer (1 .. Read_Ptr);
1005 -- Things aren't simple on VMS due to the plethora of file types
1006 -- and organizations. It seems clear that there shouldn't be more
1007 -- bytes read than are contained in the file though.
1009 if Hostparm.OpenVMS then
1010 Success := Read_Ptr <= Length + 1;
1011 else
1012 Success := Read_Ptr = Length + 1;
1013 end if;
1014 end Read_File;
1016 ----------------------------
1017 -- Report_Duplicate_Units --
1018 ----------------------------
1020 function Report_Duplicate_Units return Boolean is
1021 US : SUnit_Num;
1022 U : Unit_Num;
1024 Duplicates : Boolean := False;
1026 begin
1027 US := 1;
1028 while US < SUnit_Num (Unit.Last) loop
1029 U := Sorted_Units.Table (US);
1031 if Is_Duplicated (US) then
1032 Duplicates := True;
1034 -- Move to last two versions of duplicated file to make it clearer
1035 -- to understand which file is retained in case of overwriting.
1037 while US + 1 < SUnit_Num (Unit.Last) loop
1038 exit when not Is_Duplicated (US + 1);
1039 US := US + 1;
1040 end loop;
1042 U := Sorted_Units.Table (US);
1044 if Overwrite_Files then
1045 Warning_Msg (Unit.Table (U).File_Name.all
1046 & " is duplicated (all but last will be skipped)");
1048 elsif Unit.Table (U).Chop_File =
1049 Unit.Table (Sorted_Units.Table (US + 1)).Chop_File
1050 then
1051 Error_Msg (Unit.Table (U).File_Name.all
1052 & " is duplicated in "
1053 & File.Table (Unit.Table (U).Chop_File).Name.all);
1055 else
1056 Error_Msg (Unit.Table (U).File_Name.all
1057 & " in "
1058 & File.Table (Unit.Table (U).Chop_File).Name.all
1059 & " is duplicated in "
1060 & File.Table
1061 (Unit.Table
1062 (Sorted_Units.Table (US + 1)).Chop_File).Name.all);
1063 end if;
1064 end if;
1066 US := US + 1;
1067 end loop;
1069 if Duplicates and not Overwrite_Files then
1070 if Hostparm.OpenVMS then
1071 Put_Line
1072 ("use /OVERWRITE to overwrite files and keep last version");
1073 else
1074 Put_Line ("use -w to overwrite files and keep last version");
1075 end if;
1076 end if;
1078 return Duplicates;
1079 end Report_Duplicate_Units;
1081 --------------------
1082 -- Scan_Arguments --
1083 --------------------
1085 function Scan_Arguments return Boolean is
1086 Kset : Boolean := False;
1087 -- Set true if -k switch found
1089 begin
1090 Initialize_Option_Scan;
1092 -- Scan options first
1094 loop
1095 case Getopt ("c gnat? h k? p q r v w x -GCC=!") is
1096 when ASCII.NUL =>
1097 exit;
1099 when '-' =>
1100 Gcc := new String'(Parameter);
1101 Gcc_Set := True;
1103 when 'c' =>
1104 Compilation_Mode := True;
1106 when 'g' =>
1107 Gnat_Args :=
1108 new Argument_List'(Gnat_Args.all &
1109 new String'("-gnat" & Parameter));
1111 when 'h' =>
1112 Usage;
1113 raise Terminate_Program;
1115 when 'k' =>
1116 declare
1117 Param : String_Access := new String'(Parameter);
1119 begin
1120 if Param.all /= "" then
1121 for J in Param'Range loop
1122 if Param (J) not in '0' .. '9' then
1123 if Hostparm.OpenVMS then
1124 Error_Msg ("/FILE_NAME_MAX_LENGTH=nnn" &
1125 " requires numeric parameter");
1126 else
1127 Error_Msg ("-k# requires numeric parameter");
1128 end if;
1130 return False;
1131 end if;
1132 end loop;
1134 else
1135 if Hostparm.OpenVMS then
1136 Param := new String'("39");
1137 else
1138 Param := new String'("8");
1139 end if;
1140 end if;
1142 Gnat_Args :=
1143 new Argument_List'(Gnat_Args.all &
1144 new String'("-gnatk" & Param.all));
1145 Kset := True;
1146 end;
1148 when 'p' =>
1149 Preserve_Mode := True;
1151 when 'q' =>
1152 Quiet_Mode := True;
1154 when 'r' =>
1155 Source_References := True;
1157 when 'v' =>
1158 Verbose_Mode := True;
1160 -- Why is following written to standard error. Most other
1161 -- tools write to standard output ???
1163 Put (Standard_Error, "GNATCHOP ");
1164 Put_Line (Standard_Error, Gnatvsn.Gnat_Version_String);
1165 Put_Line
1166 (Standard_Error, "Copyright 1998-2005, AdaCore");
1168 when 'w' =>
1169 Overwrite_Files := True;
1171 when 'x' =>
1172 Exit_On_Error := True;
1174 when others =>
1175 null;
1176 end case;
1177 end loop;
1179 if not Kset and then Maximum_File_Name_Length > 0 then
1181 -- If this system has restricted filename lengths, tell gnat1
1182 -- about them, removing the leading blank from the image string.
1184 Gnat_Args :=
1185 new Argument_List'(Gnat_Args.all
1186 & new String'("-gnatk"
1187 & Maximum_File_Name_Length_String
1188 (Maximum_File_Name_Length_String'First + 1
1189 .. Maximum_File_Name_Length_String'Last)));
1190 end if;
1192 -- Scan file names
1194 loop
1195 declare
1196 S : constant String := Get_Argument (Do_Expansion => True);
1198 begin
1199 exit when S = "";
1200 File.Increment_Last;
1201 File.Table (File.Last).Name := new String'(S);
1202 File.Table (File.Last).SR_Name := null;
1203 end;
1204 end loop;
1206 -- Case of more than one file where last file is a directory
1208 if File.Last > 1
1209 and then Is_Directory (File.Table (File.Last).Name.all)
1210 then
1211 Directory := File.Table (File.Last).Name;
1212 File.Decrement_Last;
1214 -- Make sure Directory is terminated with a directory separator,
1215 -- so we can generate the output by just appending a filename.
1217 if Directory (Directory'Last) /= Directory_Separator
1218 and then Directory (Directory'Last) /= '/'
1219 then
1220 Directory := new String'(Directory.all & Directory_Separator);
1221 end if;
1223 -- At least one filename must be given
1225 elsif File.Last = 0 then
1226 Usage;
1227 return False;
1229 -- No directory given, set directory to null, so that we can just
1230 -- concatenate the directory name to the file name unconditionally.
1232 else
1233 Directory := new String'("");
1234 end if;
1236 -- Finally check all filename arguments
1238 for File_Num in 1 .. File.Last loop
1239 declare
1240 F : constant String := File.Table (File_Num).Name.all;
1242 begin
1244 if Is_Directory (F) then
1245 Error_Msg (F & " is a directory, cannot be chopped");
1246 return False;
1248 elsif not Is_Regular_File (F) then
1249 Error_Msg (F & " not found");
1250 return False;
1251 end if;
1252 end;
1253 end loop;
1255 return True;
1257 exception
1258 when Invalid_Switch =>
1259 Error_Msg ("invalid switch " & Full_Switch);
1260 return False;
1262 when Invalid_Parameter =>
1263 if Hostparm.OpenVMS then
1264 Error_Msg ("/FILE_NAME_MAX_LENGTH=nnn qualifier" &
1265 " requires numeric parameter");
1266 else
1267 Error_Msg ("-k switch requires numeric parameter");
1268 end if;
1270 return False;
1272 end Scan_Arguments;
1274 ----------------
1275 -- Sort_Units --
1276 ----------------
1278 procedure Sort_Units is
1280 procedure Move (From : Natural; To : Natural);
1281 -- Procedure used to sort the unit list
1282 -- Unit.Table (To) := Unit_List (From); used by sort
1284 function Lt (Left, Right : Natural) return Boolean;
1285 -- Compares Left and Right units based on file name (first),
1286 -- Chop_File (second) and Offset (third). This ordering is
1287 -- important to keep the last version in case of duplicate files.
1289 package Unit_Sort is new GNAT.Heap_Sort_G (Move, Lt);
1290 -- Used for sorting on filename to detect duplicates
1292 --------
1293 -- Lt --
1294 --------
1296 function Lt (Left, Right : Natural) return Boolean is
1297 L : Unit_Info renames
1298 Unit.Table (Sorted_Units.Table (SUnit_Num (Left)));
1300 R : Unit_Info renames
1301 Unit.Table (Sorted_Units.Table (SUnit_Num (Right)));
1303 begin
1304 return L.File_Name.all < R.File_Name.all
1305 or else (L.File_Name.all = R.File_Name.all
1306 and then (L.Chop_File < R.Chop_File
1307 or else (L.Chop_File = R.Chop_File
1308 and then L.Offset < R.Offset)));
1309 end Lt;
1311 ----------
1312 -- Move --
1313 ----------
1315 procedure Move (From : Natural; To : Natural) is
1316 begin
1317 Sorted_Units.Table (SUnit_Num (To)) :=
1318 Sorted_Units.Table (SUnit_Num (From));
1319 end Move;
1321 -- Start of processing for Sort_Units
1323 begin
1324 Sorted_Units.Set_Last (SUnit_Num (Unit.Last));
1326 for J in 1 .. Unit.Last loop
1327 Sorted_Units.Table (SUnit_Num (J)) := J;
1328 end loop;
1330 -- Sort Unit.Table, using Sorted_Units.Table (0) as scratch
1332 Unit_Sort.Sort (Natural (Unit.Last));
1334 -- Set the Sorted_Index fields in the unit tables
1336 for J in 1 .. SUnit_Num (Unit.Last) loop
1337 Unit.Table (Sorted_Units.Table (J)).Sorted_Index := J;
1338 end loop;
1339 end Sort_Units;
1341 -----------
1342 -- Usage --
1343 -----------
1345 procedure Usage is
1346 begin
1347 Put_Line
1348 ("Usage: gnatchop [-c] [-h] [-k#] " &
1349 "[-r] [-p] [-q] [-v] [-w] [-x] [--GCC=xx] file [file ...] [dir]");
1351 New_Line;
1352 Put_Line
1353 (" -c compilation mode, configuration pragmas " &
1354 "follow RM rules");
1356 Put_Line
1357 (" -gnatxxx passes the -gnatxxx switch to gnat parser");
1359 Put_Line
1360 (" -h help: output this usage information");
1362 Put_Line
1363 (" -k# krunch file names of generated files to " &
1364 "no more than # characters");
1366 Put_Line
1367 (" -k krunch file names of generated files to " &
1368 "no more than 8 characters");
1370 Put_Line
1371 (" -p preserve time stamp, output files will " &
1372 "have same stamp as input");
1374 Put_Line
1375 (" -q quiet mode, no output of generated file " &
1376 "names");
1378 Put_Line
1379 (" -r generate Source_Reference pragmas refer" &
1380 "encing original source file");
1382 Put_Line
1383 (" -v verbose mode, output version and generat" &
1384 "ed commands");
1386 Put_Line
1387 (" -w overwrite existing filenames");
1389 Put_Line
1390 (" -x exit on error");
1392 Put_Line
1393 (" --GCC=xx specify the path of the gnat parser to be used");
1395 New_Line;
1396 Put_Line
1397 (" file... list of source files to be chopped");
1399 Put_Line
1400 (" dir directory location for split files (defa" &
1401 "ult = current directory)");
1402 end Usage;
1404 -----------------
1405 -- Warning_Msg --
1406 -----------------
1408 procedure Warning_Msg (Message : String) is
1409 begin
1410 Warning_Count := Warning_Count + 1;
1411 Put_Line (Standard_Error, "warning: " & Message);
1412 end Warning_Msg;
1414 -------------------------
1415 -- Write_Chopped_Files --
1416 -------------------------
1418 function Write_Chopped_Files (Input : File_Num) return Boolean is
1419 Name : aliased constant String :=
1420 File.Table (Input).Name.all & ASCII.Nul;
1421 FD : File_Descriptor;
1422 Buffer : String_Access;
1423 Success : Boolean;
1424 TS_Time : OS_Time;
1426 begin
1427 FD := Open_Read (Name'Address, Binary);
1428 TS_Time := File_Time_Stamp (FD);
1430 if FD = Invalid_FD then
1431 Error_Msg ("cannot open " & File.Table (Input).Name.all);
1432 return False;
1433 end if;
1435 Read_File (FD, Buffer, Success);
1437 if not Success then
1438 Error_Msg ("cannot read " & File.Table (Input).Name.all);
1439 Close (FD);
1440 return False;
1441 end if;
1443 if not Quiet_Mode then
1444 Put_Line ("splitting " & File.Table (Input).Name.all & " into:");
1445 end if;
1447 -- Only chop those units that come from this file
1449 for Num in 1 .. Unit.Last loop
1450 if Unit.Table (Num).Chop_File = Input then
1451 Write_Unit (Buffer, Num, TS_Time, Success);
1452 exit when not Success;
1453 end if;
1454 end loop;
1456 Close (FD);
1457 return Success;
1459 end Write_Chopped_Files;
1461 -----------------------
1462 -- Write_Config_File --
1463 -----------------------
1465 procedure Write_Config_File (Input : File_Num; U : Unit_Num) is
1466 FD : File_Descriptor;
1467 Name : aliased constant String := "gnat.adc" & ASCII.NUL;
1468 Buffer : String_Access;
1469 Success : Boolean;
1470 Append : Boolean;
1471 Buffera : String_Access;
1472 Bufferl : Natural;
1474 begin
1475 Write_gnat_adc := True;
1476 FD := Open_Read_Write (Name'Address, Binary);
1478 if FD = Invalid_FD then
1479 FD := Create_File (Name'Address, Binary);
1480 Append := False;
1482 if not Quiet_Mode then
1483 Put_Line ("writing configuration pragmas from " &
1484 File.Table (Input).Name.all & " to gnat.adc");
1485 end if;
1487 else
1488 Append := True;
1490 if not Quiet_Mode then
1491 Put_Line
1492 ("appending configuration pragmas from " &
1493 File.Table (Input).Name.all & " to gnat.adc");
1494 end if;
1495 end if;
1497 Success := FD /= Invalid_FD;
1499 if not Success then
1500 Error_Msg ("cannot create gnat.adc");
1501 return;
1502 end if;
1504 -- In append mode, acquire existing gnat.adc file
1506 if Append then
1507 Read_File (FD, Buffera, Success);
1509 if not Success then
1510 Error_Msg ("cannot read gnat.adc");
1511 return;
1512 end if;
1514 -- Find location of EOF byte if any to exclude from append
1516 Bufferl := 1;
1517 while Bufferl <= Buffera'Last
1518 and then Buffera (Bufferl) /= EOF
1519 loop
1520 Bufferl := Bufferl + 1;
1521 end loop;
1523 Bufferl := Bufferl - 1;
1524 Close (FD);
1526 -- Write existing gnat.adc to new gnat.adc file
1528 FD := Create_File (Name'Address, Binary);
1529 Success := Write (FD, Buffera (1)'Address, Bufferl) = Bufferl;
1531 if not Success then
1532 Error_Msg ("error writing gnat.adc");
1533 return;
1534 end if;
1535 end if;
1537 Buffer := Get_Config_Pragmas (Input, U);
1539 if Buffer /= null then
1540 Success := Write (FD, Buffer.all'Address, Buffer'Length) =
1541 Buffer'Length;
1543 if not Success then
1544 Error_Msg ("disk full writing gnat.adc");
1545 return;
1546 end if;
1547 end if;
1549 Close (FD);
1550 end Write_Config_File;
1552 -----------------------------------
1553 -- Write_Source_Reference_Pragma --
1554 -----------------------------------
1556 procedure Write_Source_Reference_Pragma
1557 (Info : Unit_Info;
1558 Line : Line_Num;
1559 FD : File_Descriptor;
1560 EOL : EOL_String;
1561 Success : in out Boolean)
1563 FTE : File_Entry renames File.Table (Info.Chop_File);
1564 Nam : String_Access;
1566 begin
1567 if Success and Source_References and not Info.SR_Present then
1568 if FTE.SR_Name /= null then
1569 Nam := FTE.SR_Name;
1570 else
1571 Nam := FTE.Name;
1572 end if;
1574 declare
1575 Reference : aliased String :=
1576 "pragma Source_Reference (000000, """
1577 & Nam.all & """);" & EOL.Str;
1579 Pos : Positive := Reference'First;
1580 Lin : Line_Num := Line;
1582 begin
1583 while Reference (Pos + 1) /= ',' loop
1584 Pos := Pos + 1;
1585 end loop;
1587 while Reference (Pos) = '0' loop
1588 Reference (Pos) := Character'Val
1589 (Character'Pos ('0') + Lin mod 10);
1590 Lin := Lin / 10;
1591 Pos := Pos - 1;
1592 end loop;
1594 -- Assume there are enough zeroes for any program length
1596 pragma Assert (Lin = 0);
1598 Success :=
1599 Write (FD, Reference'Address, Reference'Length)
1600 = Reference'Length;
1601 end;
1602 end if;
1603 end Write_Source_Reference_Pragma;
1605 ----------------
1606 -- Write_Unit --
1607 ----------------
1609 procedure Write_Unit
1610 (Source : access String;
1611 Num : Unit_Num;
1612 TS_Time : OS_Time;
1613 Success : out Boolean)
1615 Info : Unit_Info renames Unit.Table (Num);
1616 FD : File_Descriptor;
1617 Name : aliased constant String := Info.File_Name.all & ASCII.NUL;
1618 Length : File_Offset;
1619 EOL : constant EOL_String :=
1620 Get_EOL (Source, Source'First + Info.Offset);
1622 begin
1623 -- Skip duplicated files
1625 if Is_Duplicated (Info.Sorted_Index) then
1626 Put_Line (" " & Info.File_Name.all & " skipped");
1627 Success := Overwrite_Files;
1628 return;
1629 end if;
1631 if Overwrite_Files then
1632 FD := Create_File (Name'Address, Binary);
1633 else
1634 FD := Create_New_File (Name'Address, Binary);
1635 end if;
1637 Success := FD /= Invalid_FD;
1639 if not Success then
1640 Error_Msg ("cannot create " & Info.File_Name.all);
1641 return;
1642 end if;
1644 -- A length of 0 indicates that the rest of the file belongs to
1645 -- this unit. The actual length must be calculated now. Take into
1646 -- account that the last character (EOF) must not be written.
1648 if Info.Length = 0 then
1649 Length := Source'Last - (Source'First + Info.Offset);
1650 else
1651 Length := Info.Length;
1652 end if;
1654 -- Prepend configuration pragmas if necessary
1656 if Success and then Info.Bufferg /= null then
1657 Write_Source_Reference_Pragma (Info, 1, FD, EOL, Success);
1658 Success :=
1659 Write (FD, Info.Bufferg.all'Address, Info.Bufferg'Length) =
1660 Info.Bufferg'Length;
1661 end if;
1663 Write_Source_Reference_Pragma (Info, Info.Start_Line, FD, EOL, Success);
1665 if Success then
1666 Success := Write (FD, Source (Source'First + Info.Offset)'Address,
1667 Length) = Length;
1668 end if;
1670 if not Success then
1671 Error_Msg ("disk full writing " & Info.File_Name.all);
1672 return;
1673 end if;
1675 if not Quiet_Mode then
1676 Put_Line (" " & Info.File_Name.all);
1677 end if;
1679 Close (FD);
1681 if Preserve_Mode then
1682 File_Time_Stamp (Name'Address, TS_Time);
1683 end if;
1685 end Write_Unit;
1687 -- Start of processing for gnatchop
1689 begin
1690 -- Add the directory where gnatchop is invoked in front of the
1691 -- path, if gnatchop is invoked with directory information.
1692 -- Only do this if the platform is not VMS, where the notion of path
1693 -- does not really exist.
1695 if not Hostparm.OpenVMS then
1696 declare
1697 Command : constant String := Command_Name;
1699 begin
1700 for Index in reverse Command'Range loop
1701 if Command (Index) = Directory_Separator then
1702 declare
1703 Absolute_Dir : constant String :=
1704 Normalize_Pathname
1705 (Command (Command'First .. Index));
1707 PATH : constant String :=
1708 Absolute_Dir &
1709 Path_Separator &
1710 Getenv ("PATH").all;
1712 begin
1713 Setenv ("PATH", PATH);
1714 end;
1716 exit;
1717 end if;
1718 end loop;
1719 end;
1720 end if;
1722 -- Process command line options and initialize global variables
1724 if not Scan_Arguments then
1725 Set_Exit_Status (Failure);
1726 return;
1727 end if;
1729 -- Check presence of required executables
1731 Gnat_Cmd := Locate_Executable (Gcc.all, not Gcc_Set);
1733 if Gnat_Cmd = null then
1734 goto No_Files_Written;
1735 end if;
1737 -- First parse all files and read offset information
1739 for Num in 1 .. File.Last loop
1740 if not Parse_File (Num) then
1741 goto No_Files_Written;
1742 end if;
1743 end loop;
1745 -- Check if any units have been found (assumes non-empty Unit.Table)
1747 if Unit.Last = 0 then
1748 if not Write_gnat_adc then
1749 Error_Msg ("no compilation units found", Warning => True);
1750 end if;
1752 goto No_Files_Written;
1753 end if;
1755 Sort_Units;
1757 -- Check if any duplicate files would be created. If so, emit
1758 -- a warning if Overwrite_Files is true, otherwise generate an error.
1760 if Report_Duplicate_Units and then not Overwrite_Files then
1761 goto No_Files_Written;
1762 end if;
1764 -- Check if any files exist, if so do not write anything
1765 -- Because all files have been parsed and checked already,
1766 -- there won't be any duplicates
1768 if not Overwrite_Files and then Files_Exist then
1769 goto No_Files_Written;
1770 end if;
1772 -- After this point, all source files are read in succession
1773 -- and chopped into their destination files.
1775 -- As the Source_File_Name pragmas are handled as logical file 0,
1776 -- write it first.
1778 for F in 1 .. File.Last loop
1779 if not Write_Chopped_Files (F) then
1780 Set_Exit_Status (Failure);
1781 return;
1782 end if;
1783 end loop;
1785 if Warning_Count > 0 then
1786 declare
1787 Warnings_Msg : constant String := Warning_Count'Img & " warning(s)";
1788 begin
1789 Error_Msg (Warnings_Msg (2 .. Warnings_Msg'Last), Warning => True);
1790 end;
1791 end if;
1793 return;
1795 <<No_Files_Written>>
1797 -- Special error exit for all situations where no files have
1798 -- been written.
1800 if not Write_gnat_adc then
1801 Error_Msg ("no source files written", Warning => True);
1802 end if;
1804 return;
1806 exception
1807 when Terminate_Program =>
1808 null;
1810 end Gnatchop;